1 package org.opentrafficsim.road.network.lane;
2
3 import static org.junit.jupiter.api.Assertions.assertEquals;
4 import static org.junit.jupiter.api.Assertions.assertFalse;
5 import static org.junit.jupiter.api.Assertions.assertTrue;
6
7 import java.util.ArrayList;
8 import java.util.LinkedHashSet;
9 import java.util.List;
10 import java.util.Map;
11 import java.util.Set;
12
13 import org.djunits.unit.DirectionUnit;
14 import org.djunits.unit.LengthUnit;
15 import org.djunits.unit.SpeedUnit;
16 import org.djunits.value.vdouble.scalar.Direction;
17 import org.djunits.value.vdouble.scalar.Duration;
18 import org.djunits.value.vdouble.scalar.Length;
19 import org.djunits.value.vdouble.scalar.Speed;
20 import org.djutils.draw.Export;
21 import org.djutils.draw.line.Polygon2d;
22 import org.djutils.draw.point.Point2d;
23 import org.djutils.event.Event;
24 import org.djutils.event.EventListener;
25 import org.junit.jupiter.api.Test;
26 import org.mockito.Mockito;
27 import org.opentrafficsim.base.geometry.OtsLine2d;
28 import org.opentrafficsim.core.definitions.DefaultsNl;
29 import org.opentrafficsim.core.dsol.OtsReplication;
30 import org.opentrafficsim.core.dsol.OtsSimulatorInterface;
31 import org.opentrafficsim.core.network.LinkType;
32 import org.opentrafficsim.core.network.NetworkException;
33 import org.opentrafficsim.core.network.Node;
34 import org.opentrafficsim.core.perception.HistoryManagerDevs;
35 import org.opentrafficsim.road.definitions.DefaultsRoadNl;
36 import org.opentrafficsim.road.mock.MockDevsSimulator;
37 import org.opentrafficsim.road.network.LaneKeepingPolicy;
38 import org.opentrafficsim.road.network.RoadNetwork;
39 import org.opentrafficsim.road.network.lane.conflict.Conflict;
40 import org.opentrafficsim.road.network.lane.conflict.ConflictType;
41 import org.opentrafficsim.road.network.lane.conflict.DefaultConflictRule;
42
43
44
45
46
47
48
49
50
51 public final class ConflictTest implements EventListener
52 {
53
54 private static final boolean VERBOSE = false;
55
56
57 private List<Event> collectedEvents = new ArrayList<>();
58
59
60 private ConflictTest()
61 {
62
63 }
64
65
66
67
68
69 @Test
70 public void testConstructor() throws NetworkException
71 {
72 OtsSimulatorInterface simulator = MockDevsSimulator.createMock();
73 OtsReplication replication = Mockito.mock(OtsReplication.class);
74 HistoryManagerDevs hmd = Mockito.mock(HistoryManagerDevs.class);
75 Mockito.when(hmd.now()).thenReturn(Duration.ZERO);
76 Mockito.when(replication.getHistoryManager(simulator)).thenReturn(hmd);
77 Mockito.when(simulator.getReplication()).thenReturn(replication);
78 Mockito.when(simulator.getSimulatorTime()).thenReturn(Duration.ZERO);
79 RoadNetwork network = new RoadNetwork("Network for conflict test", simulator);
80 LinkType linkType = DefaultsNl.ROAD;
81 LaneType laneType = DefaultsRoadNl.ONE_WAY_LANE;
82 Point2d pointAFrom = new Point2d(0, 0);
83 Node nodeAFrom = new Node(network, "A from", pointAFrom, Direction.ZERO);
84 Point2d pointATo = new Point2d(100, 0);
85 Node nodeATo = new Node(network, "A to", pointATo, Direction.ZERO);
86 CrossSectionLink linkA = new CrossSectionLink(network, "Link A", nodeAFrom, nodeATo, linkType,
87 new OtsLine2d(pointAFrom, pointATo), null, LaneKeepingPolicy.KEEPRIGHT);
88 Lane laneA = LaneGeometryUtil.createStraightLane(linkA, "lane A", Length.ZERO, new Length(2, LengthUnit.METER),
89 laneType, Map.of(DefaultsNl.VEHICLE, new Speed(50, SpeedUnit.KM_PER_HOUR)));
90 laneA.addListener(this, Lane.OBJECT_ADD_EVENT);
91
92 Point2d pointBFrom = new Point2d(30, -15);
93 Point2d pointBTo = new Point2d(60, 60);
94 Direction bDirection =
95 new Direction(Math.atan2(pointBTo.y - pointBFrom.y, pointBTo.x - pointBFrom.x), DirectionUnit.EAST_RADIAN);
96 Node nodeBFrom = new Node(network, "B from", pointBFrom, bDirection);
97 Node nodeBTo = new Node(network, "B to", pointBTo, bDirection);
98 CrossSectionLink linkB = new CrossSectionLink(network, "Link B", nodeBFrom, nodeBTo, linkType,
99 new OtsLine2d(pointBFrom, pointBTo), null, LaneKeepingPolicy.KEEPRIGHT);
100 Lane laneB = LaneGeometryUtil.createStraightLane(linkB, "lane B", Length.ZERO, new Length(4, LengthUnit.METER),
101 laneType, Map.of(DefaultsNl.VEHICLE, new Speed(50, SpeedUnit.KM_PER_HOUR)));
102 laneB.addListener(this, Lane.OBJECT_ADD_EVENT);
103
104 if (VERBOSE)
105 {
106 System.out.print(Export.toPlot(laneA.getAbsoluteContour()));
107 System.out.print(Export.toPlot(laneB.getAbsoluteContour()));
108 System.out.println("c0,1,0");
109 System.out.print(Export.toPlot(laneA.getCenterLine()));
110 System.out.print(Export.toPlot(laneB.getCenterLine()));
111 System.out.println("c1,0,0");
112 }
113
114
115
116 Point2d conflictStart = null;
117 double closestDistance = Double.MAX_VALUE;
118 Point2d conflictEnd = null;
119 double furthestDistance = 0.0;
120 for (Point2d intersection : intersections(laneA.getAbsoluteContour(), laneB.getAbsoluteContour()))
121 {
122 double distance = pointAFrom.distance(intersection);
123 if (distance < closestDistance)
124 {
125 conflictStart = intersection;
126 closestDistance = distance;
127 }
128 if (distance > furthestDistance)
129 {
130 conflictEnd = intersection;
131 furthestDistance = distance;
132 }
133 }
134
135
136
137
138 Polygon2d geometry1 =
139 new Polygon2d(conflictStart, conflictEnd, new Point2d(conflictStart.x, conflictEnd.y), conflictStart);
140
141 Polygon2d geometry2 = new Polygon2d(conflictStart,
142 new Point2d(conflictStart.x + laneB.getWidth(0).si * Math.sin(bDirection.si),
143 conflictStart.y - laneB.getWidth(0).si * Math.cos(bDirection.si)),
144 conflictEnd, new Point2d(conflictEnd.x - laneB.getWidth(0).si * Math.sin(bDirection.si),
145 conflictEnd.y + laneB.getWidth(0).si * Math.cos(bDirection.si)),
146 conflictStart);
147
148 Length conflictBStart =
149 new Length(pointBFrom.distance(new Point2d(conflictStart.x + laneB.getWidth(0).si / 2 * Math.sin(bDirection.si),
150 conflictStart.y - laneB.getWidth(0).si / 2 * Math.cos(bDirection.si))), LengthUnit.SI);
151
152 Length conflictBLength = new Length(
153 laneA.getWidth(0).si / Math.sin(bDirection.si) + laneB.getWidth(0).si / Math.tan(bDirection.si), LengthUnit.SI);
154
155 if (VERBOSE)
156 {
157 System.out.print(Export.toPlot(geometry1));
158 System.out.print(Export.toPlot(geometry2));
159 System.out.println("#angle B: " + bDirection.toString(DirectionUnit.EAST_DEGREE));
160 System.out.println("#conflict B start: " + conflictBStart);
161 System.out.println("#conflict B length: " + conflictBLength);
162 System.out.println("c0,0,1");
163 System.out.println(String.format("M%.3f,%.3f <%f l%f,0", pointBFrom.x, pointBFrom.y, Math.toDegrees(bDirection.si),
164 conflictBStart.si));
165 System.out.println(String.format("c0,0,0 l%f,0", conflictBLength.si));
166 }
167
168 assertEquals(0, this.collectedEvents.size(), "not events received yet");
169
170
171 Conflict.generateConflictPair(ConflictType.CROSSING, new DefaultConflictRule(), false, laneA,
172 new Length(conflictStart.x, LengthUnit.SI), new Length(conflictEnd.x - conflictStart.x, LengthUnit.SI),
173 geometry1, laneB, conflictBStart, conflictBLength, geometry2, simulator);
174
175
176 assertEquals(1, laneA.getLaneBasedObjects().size(), "one conflict on lane A");
177 assertEquals(1, laneB.getLaneBasedObjects().size(), "one conflict on lane B");
178
179 Conflict conflictA = (Conflict) laneA.getLaneBasedObjects().get(0);
180 Conflict conflictB = (Conflict) laneB.getLaneBasedObjects().get(0);
181 if (VERBOSE)
182 {
183 System.out.println("Conflict A: " + conflictA);
184 System.out.println("Conflict B: " + conflictB);
185 }
186
187 assertEquals(conflictA, conflictB.getOtherConflict(), "the conflicts are each others counter part");
188 assertEquals(conflictB, conflictA.getOtherConflict(), "the conflicts are each others counter part");
189 assertEquals(new Length(conflictStart.x, LengthUnit.SI), conflictA.getLongitudinalPosition(), "longitudinal position");
190 assertEquals(conflictBStart, conflictB.getLongitudinalPosition(), "longitudinal position");
191 assertEquals(new Length(conflictEnd.x - conflictStart.x, LengthUnit.SI), conflictA.getLength(), "length");
192 assertEquals(conflictBLength, conflictB.getLength(), "length");
193 assertEquals(geometry1, conflictA.getAbsoluteContour(), "contour");
194 assertEquals(geometry2, conflictB.getAbsoluteContour(), "contour");
195 assertTrue(conflictA.getConflictRule() instanceof DefaultConflictRule, "conflict rule");
196 assertTrue(conflictB.getConflictRule() instanceof DefaultConflictRule, "conflict rule");
197 assertFalse(conflictA.isPermitted(), "conflict A is not permitted");
198 assertFalse(conflictB.isPermitted(), "conflict B is not permitted");
199 assertEquals(2, this.collectedEvents.size(), "construction of two conflicts has generated two events");
200
201
202 }
203
204
205
206
207
208
209
210 public Set<Point2d> intersections(final Polygon2d a, final Polygon2d b)
211 {
212
213 Set<Point2d> result = new LinkedHashSet<>();
214 Point2d prevA = null;
215 for (Point2d nextA : a.getPointList())
216 {
217 if (null != prevA)
218 {
219 Point2d prevB = null;
220 for (Point2d nextB : b.getPointList())
221 {
222 if (null != prevB)
223 {
224 Point2d intersection = Point2d.intersectionOfLineSegments(prevA, nextA, prevB, nextB);
225 if (null != intersection)
226 {
227 result.add(intersection);
228 }
229 }
230 prevB = nextB;
231 }
232 }
233 prevA = nextA;
234 }
235 return result;
236 }
237
238 @Override
239 public void notify(final Event event)
240 {
241
242 this.collectedEvents.add(event);
243 }
244
245 }