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