View Javadoc
1   package org.opentrafficsim.road.network.lane;
2   
3   import static org.junit.Assert.assertEquals;
4   import static org.junit.Assert.assertFalse;
5   import static org.junit.Assert.assertNotNull;
6   import static org.junit.Assert.assertTrue;
7   import static org.opentrafficsim.core.gtu.GTUType.CAR;
8   
9   import java.awt.geom.Point2D;
10  import java.util.LinkedHashMap;
11  import java.util.Map;
12  
13  import javax.media.j3d.BoundingBox;
14  import javax.media.j3d.Bounds;
15  import javax.vecmath.Point3d;
16  
17  import org.djunits.unit.DurationUnit;
18  import org.djunits.unit.UNITS;
19  import org.djunits.value.vdouble.scalar.Duration;
20  import org.djunits.value.vdouble.scalar.Length;
21  import org.djunits.value.vdouble.scalar.Speed;
22  import org.djunits.value.vdouble.scalar.Time;
23  import org.junit.Test;
24  import org.opentrafficsim.core.compatibility.GTUCompatibility;
25  import org.opentrafficsim.core.dsol.AbstractOTSModel;
26  import org.opentrafficsim.core.dsol.OTSSimulator;
27  import org.opentrafficsim.core.dsol.OTSSimulatorInterface;
28  import org.opentrafficsim.core.geometry.OTSLine3D;
29  import org.opentrafficsim.core.geometry.OTSPoint3D;
30  import org.opentrafficsim.core.gtu.GTUType;
31  import org.opentrafficsim.core.network.LinkType;
32  import org.opentrafficsim.core.network.LongitudinalDirectionality;
33  import org.opentrafficsim.core.network.Network;
34  import org.opentrafficsim.core.network.Node;
35  import org.opentrafficsim.core.network.OTSNetwork;
36  import org.opentrafficsim.core.network.OTSNode;
37  import org.opentrafficsim.road.network.lane.changing.LaneKeepingPolicy;
38  import org.opentrafficsim.road.network.lane.changing.OvertakingConditions;
39  
40  import com.vividsolutions.jts.geom.Coordinate;
41  import com.vividsolutions.jts.geom.Geometry;
42  import com.vividsolutions.jts.geom.GeometryFactory;
43  
44  import nl.tudelft.simulation.dsol.SimRuntimeException;
45  import nl.tudelft.simulation.language.d3.DirectedPoint;
46  
47  /**
48   * Test the Lane class.
49   * <p>
50   * Copyright (c) 2013-2019 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
51   * BSD-style license. See <a href="http://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
52   * <p>
53   * $LastChangedDate: 2015-09-16 19:20:07 +0200 (Wed, 16 Sep 2015) $, @version $Revision: 1405 $, by $Author: averbraeck $,
54   * initial version 21 jan. 2015 <br>
55   * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
56   */
57  public class LaneTest implements UNITS
58  {
59      /**
60       * Test the constructor.
61       * @throws Exception when something goes wrong (should not happen)
62       */
63      @Test
64      public void laneConstructorTest() throws Exception
65      {
66          // First we need two Nodes
67          Network network = new OTSNetwork("lane test network");
68          OTSNode nodeFrom = new OTSNode(network, "A", new OTSPoint3D(0, 0, 0));
69          OTSNode nodeTo = new OTSNode(network, "B", new OTSPoint3D(1000, 0, 0));
70          // Now we can make a Link
71          OTSPoint3D[] coordinates = new OTSPoint3D[2];
72          coordinates[0] = new OTSPoint3D(nodeFrom.getPoint().x, nodeFrom.getPoint().y, 0);
73          coordinates[1] = new OTSPoint3D(nodeTo.getPoint().x, nodeTo.getPoint().y, 0);
74          OTSSimulatorInterface simulator = new OTSSimulator();
75          Model model = new Model(simulator);
76          simulator.initialize(Time.ZERO, Duration.ZERO, new Duration(3600.0, DurationUnit.SECOND), model);
77          CrossSectionLink link = new CrossSectionLink(network, "A to B", nodeFrom, nodeTo, LinkType.ROAD,
78                  new OTSLine3D(coordinates), simulator, LaneKeepingPolicy.KEEP_RIGHT);
79          Length startLateralPos = new Length(2, METER);
80          Length endLateralPos = new Length(5, METER);
81          Length startWidth = new Length(3, METER);
82          Length endWidth = new Length(4, METER);
83          GTUType gtuTypeCar = CAR;
84  
85          GTUCompatibility<LaneType> gtuCompatibility = new GTUCompatibility<>((LaneType) null);
86          gtuCompatibility.addAllowedGTUType(GTUType.VEHICLE, LongitudinalDirectionality.DIR_PLUS);
87          LaneType laneType = new LaneType("One way", LaneType.FREEWAY, gtuCompatibility);
88          Map<GTUType, Speed> speedMap = new LinkedHashMap<>();
89          speedMap.put(GTUType.VEHICLE, new Speed(100, KM_PER_HOUR));
90          // Now we can construct a Lane
91          // FIXME what overtaking conditions do we want to test in this unit test?
92          Lane lane = new Lane(link, "lane", startLateralPos, endLateralPos, startWidth, endWidth, laneType, speedMap,
93                  new OvertakingConditions.LeftAndRight());
94          // Verify the easy bits
95          assertEquals("PrevLanes should be empty", 0, lane.prevLanes(gtuTypeCar).size()); // this one caught a bug!
96          assertEquals("NextLanes should be empty", 0, lane.nextLanes(gtuTypeCar).size());
97          double approximateLengthOfContour =
98                  2 * nodeFrom.getPoint().distanceSI(nodeTo.getPoint()) + startWidth.getSI() + endWidth.getSI();
99          assertEquals("Length of contour is approximately " + approximateLengthOfContour, approximateLengthOfContour,
100                 lane.getContour().getLengthSI(), 0.1);
101         assertEquals("Directionality should be " + LongitudinalDirectionality.DIR_PLUS, LongitudinalDirectionality.DIR_PLUS,
102                 lane.getLaneType().getDirectionality(GTUType.VEHICLE));
103         assertEquals("SpeedLimit should be " + (new Speed(100, KM_PER_HOUR)), new Speed(100, KM_PER_HOUR),
104                 lane.getSpeedLimit(GTUType.VEHICLE));
105         assertEquals("There should be no GTUs on the lane", 0, lane.getGtuList().size());
106         assertEquals("LaneType should be " + laneType, laneType, lane.getLaneType());
107         // TODO: This test for expectedLateralCenterOffset fails
108         /*-
109         for (int i = 0; i < 10; i++)
110         {
111             double expectedLateralCenterOffset =
112                     startLateralPos.getSI() + (endLateralPos.getSI() - startLateralPos.getSI()) * i / 10;
113             assertEquals(String.format("Lateral offset at %d%% should be %.3fm", 10 * i, expectedLateralCenterOffset),
114                     expectedLateralCenterOffset, lane.getLateralCenterPosition(i / 10.0).getSI(), 0.01);
115             Length longitudinalPosition = new Length(lane.getLength().getSI() * i / 10, METER);
116             assertEquals("Lateral offset at " + longitudinalPosition + " should be " + expectedLateralCenterOffset,
117                     expectedLateralCenterOffset, lane.getLateralCenterPosition(longitudinalPosition).getSI(), 0.01);
118             double expectedWidth = startWidth.getSI() + (endWidth.getSI() - startWidth.getSI()) * i / 10;
119             assertEquals(String.format("Width at %d%% should be %.3fm", 10 * i, expectedWidth), expectedWidth,
120                     lane.getWidth(i / 10.0).getSI(), 0.0001);
121             assertEquals("Width at " + longitudinalPosition + " should be " + expectedWidth, expectedWidth,
122                     lane.getWidth(longitudinalPosition).getSI(), 0.0001);
123             double expectedLeftOffset = expectedLateralCenterOffset - expectedWidth / 2;
124             // The next test caught a bug
125             assertEquals(String.format("Left edge at %d%% should be %.3fm", 10 * i, expectedLeftOffset), expectedLeftOffset,
126                     lane.getLateralBoundaryPosition(LateralDirectionality.LEFT, i / 10.0).getSI(), 0.001);
127             assertEquals("Left edge at " + longitudinalPosition + " should be " + expectedLeftOffset, expectedLeftOffset,
128                     lane.getLateralBoundaryPosition(LateralDirectionality.LEFT, longitudinalPosition).getSI(), 0.001);
129             double expectedRightOffset = expectedLateralCenterOffset + expectedWidth / 2;
130             assertEquals(String.format("Right edge at %d%% should be %.3fm", 10 * i, expectedRightOffset), expectedRightOffset,
131                     lane.getLateralBoundaryPosition(LateralDirectionality.RIGHT, i / 10.0).getSI(), 0.001);
132             assertEquals("Right edge at " + longitudinalPosition + " should be " + expectedRightOffset, expectedRightOffset,
133                     lane.getLateralBoundaryPosition(LateralDirectionality.RIGHT, longitudinalPosition).getSI(), 0.001);
134         }
135         */
136 
137         // Harder case; create a Link with form points along the way
138         // System.out.println("Constructing Link and Lane with one form point");
139         coordinates = new OTSPoint3D[3];
140         coordinates[0] = new OTSPoint3D(nodeFrom.getPoint().x, nodeFrom.getPoint().y, 0);
141         coordinates[1] = new OTSPoint3D(200, 100);
142         coordinates[2] = new OTSPoint3D(nodeTo.getPoint().x, nodeTo.getPoint().y, 0);
143         link = new CrossSectionLink(network, "A to B with Kink", nodeFrom, nodeTo, LinkType.ROAD, new OTSLine3D(coordinates),
144                 simulator, LaneKeepingPolicy.KEEP_RIGHT);
145         // FIXME what overtaking conditions do we ant to test in this unit test?
146         lane = new Lane(link, "lane.1", startLateralPos, endLateralPos, startWidth, endWidth, laneType, speedMap,
147                 new OvertakingConditions.LeftAndRight());
148         // Verify the easy bits
149         assertEquals("PrevLanes should be empty", 0, lane.prevLanes(gtuTypeCar).size());
150         assertEquals("NextLanes should be empty", 0, lane.nextLanes(gtuTypeCar).size());
151         approximateLengthOfContour = 2 * (coordinates[0].distanceSI(coordinates[1]) + coordinates[1].distanceSI(coordinates[2]))
152                 + startWidth.getSI() + endWidth.getSI();
153         assertEquals("Length of contour is approximately " + approximateLengthOfContour, approximateLengthOfContour,
154                 lane.getContour().getLengthSI(), 4); // This lane takes a path that is about 3m longer
155         assertEquals("There should be no GTUs on the lane", 0, lane.getGtuList().size());
156         assertEquals("LaneType should be " + laneType, laneType, lane.getLaneType());
157         // System.out.println("Add another Lane at the inside of the corner in the design line");
158         Length startLateralPos2 = new Length(-8, METER);
159         Length endLateralPos2 = new Length(-5, METER);
160         // FIXME what overtaking conditions do we ant to test in this unit test?
161         Lane lane2 = new Lane(link, "lane.2", startLateralPos2, endLateralPos2, startWidth, endWidth, laneType, speedMap,
162                 new OvertakingConditions.LeftAndRight());
163         // Verify the easy bits
164         assertEquals("PrevLanes should be empty", 0, lane2.prevLanes(gtuTypeCar).size());
165         assertEquals("NextLanes should be empty", 0, lane2.nextLanes(gtuTypeCar).size());
166         approximateLengthOfContour = 2 * (coordinates[0].distanceSI(coordinates[1]) + coordinates[1].distanceSI(coordinates[2]))
167                 + startWidth.getSI() + endWidth.getSI();
168         assertEquals("Length of contour is approximately " + approximateLengthOfContour, approximateLengthOfContour,
169                 lane2.getContour().getLengthSI(), 12); // This lane takes a path that is about 11 meters shorter
170         assertEquals("There should be no GTUs on the lane", 0, lane2.getGtuList().size());
171         assertEquals("LaneType should be " + laneType, laneType, lane2.getLaneType());
172     }
173 
174     /**
175      * Test that the contour of a constructed lane covers the expected area. Tests are only performed for straight lanes, but
176      * the orientation of the link and the offset of the lane from the link is varied in many ways.
177      * @throws Exception when something goes wrong (should not happen)
178      */
179     @Test
180     public final void contourTest() throws Exception
181     {
182         final int[] startPositions = { 0, 1, -1, 20, -20 };
183         final double[] angles = { 0, Math.PI * 0.01, Math.PI / 3, Math.PI / 2, Math.PI * 2 / 3, Math.PI * 0.99, Math.PI,
184                 Math.PI * 1.01, Math.PI * 4 / 3, Math.PI * 3 / 2, Math.PI * 1.99, Math.PI * 2, Math.PI * (-0.2) };
185         LaneType laneType = LaneType.TWO_WAY_LANE;
186         Map<GTUType, LongitudinalDirectionality> directionalityMap = new LinkedHashMap<>();
187         directionalityMap.put(GTUType.VEHICLE, LongitudinalDirectionality.DIR_PLUS);
188         Map<GTUType, Speed> speedMap = new LinkedHashMap<>();
189         speedMap.put(GTUType.VEHICLE, new Speed(50, KM_PER_HOUR));
190         int laneNum = 0;
191         for (int xStart : startPositions)
192         {
193             for (int yStart : startPositions)
194             {
195                 for (double angle : angles)
196                 {
197                     Network network = new OTSNetwork("contour test network");
198                     OTSNode start = new OTSNode(network, "start", new OTSPoint3D(xStart, yStart));
199                     double linkLength = 1000;
200                     double xEnd = xStart + linkLength * Math.cos(angle);
201                     double yEnd = yStart + linkLength * Math.sin(angle);
202                     OTSNode end = new OTSNode(network, "end", new OTSPoint3D(xEnd, yEnd));
203                     OTSPoint3D[] coordinates = new OTSPoint3D[2];
204                     coordinates[0] = start.getPoint();
205                     coordinates[1] = end.getPoint();
206                     OTSLine3D line = new OTSLine3D(coordinates);
207                     OTSSimulatorInterface simulator = new OTSSimulator();
208                     Model model = new Model(simulator);
209                     simulator.initialize(Time.ZERO, Duration.ZERO, new Duration(3600.0, DurationUnit.SECOND), model);
210                     CrossSectionLink link = new CrossSectionLink(network, "A to B", start, end, LinkType.ROAD, line, simulator,
211                             LaneKeepingPolicy.KEEP_RIGHT);
212                     final int[] lateralOffsets = { -10, -3, -1, 0, 1, 3, 10 };
213                     for (int startLateralOffset : lateralOffsets)
214                     {
215                         for (int endLateralOffset : lateralOffsets)
216                         {
217                             int startWidth = 4; // This one is not varied
218                             for (int endWidth : new int[] { 2, 4, 6 })
219                             {
220                                 // Now we can construct a Lane
221                                 // FIXME what overtaking conditions do we want to test in this unit test?
222                                 Lane lane = new Lane(link, "lane." + ++laneNum, new Length(startLateralOffset, METER),
223                                         new Length(endLateralOffset, METER), new Length(startWidth, METER),
224                                         new Length(endWidth, METER), laneType, speedMap,
225                                         new OvertakingConditions.LeftAndRight());
226                                 final Geometry geometry = lane.getContour().getLineString();
227                                 assertNotNull("geometry of the lane should not be null", geometry);
228                                 // Verify a couple of points that should be inside the contour of the Lane
229                                 // One meter along the lane design line
230                                 checkInside(lane, 1, startLateralOffset, true);
231                                 // One meter before the end along the lane design line
232                                 checkInside(lane, link.getLength().getSI() - 1, endLateralOffset, true);
233                                 // One meter before the start of the lane along the lane design line
234                                 checkInside(lane, -1, startLateralOffset, false);
235                                 // One meter beyond the end of the lane along the lane design line
236                                 checkInside(lane, link.getLength().getSI() + 1, endLateralOffset, false);
237                                 // One meter along the lane design line, left outside the lane
238                                 checkInside(lane, 1, startLateralOffset - startWidth / 2 - 1, false);
239                                 // One meter along the lane design line, right outside the lane
240                                 checkInside(lane, 1, startLateralOffset + startWidth / 2 + 1, false);
241                                 // One meter before the end, left outside the lane
242                                 checkInside(lane, link.getLength().getSI() - 1, endLateralOffset - endWidth / 2 - 1, false);
243                                 // One meter before the end, right outside the lane
244                                 checkInside(lane, link.getLength().getSI() - 1, endLateralOffset + endWidth / 2 + 1, false);
245                                 // Check the result of getBounds.
246                                 DirectedPoint l = lane.getLocation();
247                                 Bounds bb = lane.getBounds();
248                                 // System.out.println("bb is " + bb);
249                                 // System.out.println("l is " + l.x + "," + l.y + "," + l.z);
250                                 // System.out.println("start is at " + start.getX() + ", " + start.getY());
251                                 // System.out.println(" end is at " + end.getX() + ", " + end.getY());
252                                 Point2D.Double[] cornerPoints = new Point2D.Double[4];
253                                 cornerPoints[0] =
254                                         new Point2D.Double(xStart - (startLateralOffset + startWidth / 2) * Math.sin(angle),
255                                                 yStart + (startLateralOffset + startWidth / 2) * Math.cos(angle));
256                                 cornerPoints[1] =
257                                         new Point2D.Double(xStart - (startLateralOffset - startWidth / 2) * Math.sin(angle),
258                                                 yStart + (startLateralOffset - startWidth / 2) * Math.cos(angle));
259                                 cornerPoints[2] = new Point2D.Double(xEnd - (endLateralOffset + endWidth / 2) * Math.sin(angle),
260                                         yEnd + (endLateralOffset + endWidth / 2) * Math.cos(angle));
261                                 cornerPoints[3] = new Point2D.Double(xEnd - (endLateralOffset - endWidth / 2) * Math.sin(angle),
262                                         yEnd + (endLateralOffset - endWidth / 2) * Math.cos(angle));
263                                 // for (int i = 0; i < cornerPoints.length; i++)
264                                 // {
265                                 // System.out.println("p" + i + ": " + cornerPoints[i].x + "," + cornerPoints[i].y);
266                                 // }
267                                 double minX = cornerPoints[0].getX();
268                                 double maxX = cornerPoints[0].getX();
269                                 double minY = cornerPoints[0].getY();
270                                 double maxY = cornerPoints[0].getY();
271                                 for (int i = 1; i < cornerPoints.length; i++)
272                                 {
273                                     Point2D.Double p = cornerPoints[i];
274                                     minX = Math.min(minX, p.getX());
275                                     minY = Math.min(minY, p.getY());
276                                     maxX = Math.max(maxX, p.getX());
277                                     maxY = Math.max(maxY, p.getY());
278                                 }
279                                 Point3d bbLow = new Point3d();
280                                 ((BoundingBox) bb).getLower(bbLow);
281                                 Point3d bbHigh = new Point3d();
282                                 ((BoundingBox) bb).getUpper(bbHigh);
283                                 // System.out.println(" my bbox is " + minX + "," + minY + " - " + maxX + "," + maxY);
284                                 // System.out.println("the bbox is " + (bbLow.x + l.x) + "," + (bbLow.y + l.y) + " - "
285                                 // + (bbHigh.x + l.x) + "," + (bbHigh.y + l.y));
286                                 double boundsMinX = bbLow.x + l.x;
287                                 double boundsMinY = bbLow.y + l.y;
288                                 double boundsMaxX = bbHigh.x + l.x;
289                                 double boundsMaxY = bbHigh.y + l.y;
290                                 assertEquals("low x boundary", minX, boundsMinX, 0.1);
291                                 assertEquals("low y boundary", minY, boundsMinY, 0.1);
292                                 assertEquals("high x boundary", maxX, boundsMaxX, 0.1);
293                                 assertEquals("high y boundary", maxY, boundsMaxY, 0.1);
294                             }
295                         }
296                     }
297                 }
298             }
299         }
300     }
301 
302     /**
303      * Verify that a point at specified distance along and across from the design line of the parent Link of a Lane is inside
304      * c.q. outside the contour of a Lane. The test uses an implementation that is as independent as possible of the Geometry
305      * class methods.
306      * @param lane Lane; the lane
307      * @param longitudinal double; the longitudinal position along the design line of the parent Link of the Lane. This design
308      *            line is expected to be straight and the longitudinal position may be negative (indicating a point before the
309      *            start of the Link) and it may exceed the length of the Link (indicating a point beyond the end of the Link)
310      * @param lateral double; the lateral offset from the design line of the link (positive is left, negative is right)
311      * @param expectedResult boolean; true if the calling method expects the point to be within the contour of the Lane, false
312      *            if the calling method expects the point to be outside the contour of the Lane
313      */
314     private void checkInside(final Lane lane, final double longitudinal, final double lateral, final boolean expectedResult)
315     {
316         CrossSectionLink parentLink = lane.getParentLink();
317         Node start = parentLink.getStartNode();
318         Node end = parentLink.getEndNode();
319         double startX = start.getPoint().x;
320         double startY = start.getPoint().y;
321         double endX = end.getPoint().x;
322         double endY = end.getPoint().y;
323         double length = Math.sqrt((endX - startX) * (endX - startX) + (endY - startY) * (endY - startY));
324         double ratio = longitudinal / length;
325         double designLineX = startX + (endX - startX) * ratio;
326         double designLineY = startY + (endY - startY) * ratio;
327         double lateralAngle = Math.atan2(endY - startY, endX - startX) + Math.PI / 2;
328         double px = designLineX + lateral * Math.cos(lateralAngle);
329         double py = designLineY + lateral * Math.sin(lateralAngle);
330         Geometry contour = lane.getContour().getLineString();
331         GeometryFactory factory = new GeometryFactory();
332         Geometry p = factory.createPoint(new Coordinate(px, py));
333         // CrossSectionElement.printCoordinates("contour: ", contour);
334         // System.out.println("p: " + p);
335         boolean result = contour.contains(p);
336         Coordinate[] polygon = contour.getCoordinates();
337         result = pointInsidePolygon(new Coordinate(px, py), polygon);
338         if (expectedResult)
339         {
340             assertTrue("Point at " + longitudinal + " along and " + lateral + " lateral is within lane", result);
341         }
342         else
343         {
344             assertFalse("Point at " + longitudinal + " along and " + lateral + " lateral is outside lane", result);
345         }
346     }
347 
348     /**
349      * Algorithm of W. Randolph Franklin http://www.ecse.rpi.edu/Homepages/wrf/Research/Short_Notes/pnpoly.html, found via
350      * stackoverflow.com: http://stackoverflow.com/questions/217578/point-in-polygon-aka-hit-test.
351      * @param point Coordinate; the point
352      * @param polygon OTSPoint3D[]; the polygon (last coordinate is allowed to be identical to the first, but his is not a
353      *            requirement)
354      * @return boolean; true if the point is inside the polygon; false if it is outside the polygon; if the point lies <b>on</b>
355      *         an vertex or edge of the polygon the result is (of course) undefined
356      */
357     private boolean pointInsidePolygon(final Coordinate point, final Coordinate[] polygon)
358     {
359         boolean result = false;
360         for (int i = 0, j = polygon.length - 1; i < polygon.length; j = i++)
361         {
362             if ((polygon[i].y > point.y) != (polygon[j].y > point.y)
363                     && point.x < (polygon[j].x - polygon[i].x) * (point.y - polygon[i].y) / (polygon[j].y - polygon[i].y)
364                             + polygon[i].x)
365             {
366                 result = !result;
367             }
368         }
369         return result;
370     }
371 
372     /** The helper model. */
373     protected static class Model extends AbstractOTSModel
374     {
375         /** */
376         private static final long serialVersionUID = 20141027L;
377 
378         /**
379          * @param simulator the simulator to use
380          */
381         public Model(final OTSSimulatorInterface simulator)
382         {
383             super(simulator);
384         }
385 
386         /** {@inheritDoc} */
387         @Override
388         public final void constructModel() throws SimRuntimeException
389         {
390             //
391         }
392 
393         /** {@inheritDoc} */
394         @Override
395         public final OTSNetwork getNetwork()
396         {
397             return null;
398         }
399     }
400 
401 }