View Javadoc
1   package org.opentrafficsim.road.network;
2   
3   import static org.junit.Assert.assertEquals;
4   
5   import org.djunits.unit.util.UNITS;
6   import org.djunits.value.vdouble.scalar.Direction;
7   import org.djunits.value.vdouble.scalar.Length;
8   import org.junit.Test;
9   import org.opentrafficsim.core.dsol.OTSSimulatorInterface;
10  import org.opentrafficsim.core.geometry.OTSGeometryException;
11  import org.opentrafficsim.core.geometry.OTSLine3D;
12  import org.opentrafficsim.core.geometry.OTSPoint3D;
13  import org.opentrafficsim.core.network.LinkLocation;
14  import org.opentrafficsim.core.network.LinkType;
15  import org.opentrafficsim.core.network.NetworkException;
16  import org.opentrafficsim.road.mock.MockSimulator;
17  import org.opentrafficsim.road.network.lane.CrossSectionLink;
18  import org.opentrafficsim.road.network.lane.OTSRoadNode;
19  import org.opentrafficsim.road.network.lane.changing.LaneKeepingPolicy;
20  
21  /**
22   * Test the LinkLocation class.
23   * <p>
24   * Copyright (c) 2013-2022 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
25   * BSD-style license. See <a href="http://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
26   * <p>
27   * $LastChangedDate: 2015-09-16 19:20:07 +0200 (Wed, 16 Sep 2015) $, @version $Revision: 1405 $, by $Author: averbraeck $,
28   * initial version 20 jan. 2015 <br>
29   * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
30   */
31  public class LinkLocationTest implements UNITS
32  {
33      /**
34       * Test constructor and verify all getters.
35       * @throws OTSGeometryException if that happens this test has failed
36       * @throws NetworkException if that happens this test has failed
37       */
38      @Test
39      public final void linkLocationTest() throws OTSGeometryException, NetworkException
40      {
41          // Preparations
42          OTSSimulatorInterface simulator = MockSimulator.createMock();
43          OTSRoadNetwork network = new OTSRoadNetwork("link location test network", true, simulator);
44          OTSRoadNode nodeFrom = new OTSRoadNode(network, "From", new OTSPoint3D(0, 0, 0), Direction.ZERO);
45          OTSRoadNode nodeTo = new OTSRoadNode(network, "To", new OTSPoint3D(1000, 0, 0), Direction.ZERO);
46          OTSLine3D line = new OTSLine3D(new OTSPoint3D[] {new OTSPoint3D(0, 0, 0), new OTSPoint3D(1000, 0, 0)});
47          CrossSectionLink link = new CrossSectionLink(network, "Link", nodeFrom, nodeTo,
48                  network.getLinkType(LinkType.DEFAULTS.ROAD), line, LaneKeepingPolicy.KEEPRIGHT);
49          Length linkLength = line.getLength();
50          // Now we can make a LinkLocation.
51          Length referenceLocationDistance = new Length(123, METER);
52          LinkLocation referenceLocation = new LinkLocation(link, referenceLocationDistance);
53          assertEquals("link should be the provided Link", link, referenceLocation.getLink());
54          assertEquals("longitudinalPosition should be " + referenceLocationDistance, referenceLocationDistance.getSI(),
55                  referenceLocation.getLongitudinalPosition().getSI(), 0.0001);
56          for (int position = 0; position < 1000; position += 100)
57          {
58              final double fraction = position / linkLength.getSI();
59              LinkLocation linkLocation = new LinkLocation(link, fraction);
60              assertEquals("link should be the provided Link", link, linkLocation.getLink());
61              assertEquals("fractionalLongitudinalPosition should be " + fraction, fraction,
62                      linkLocation.getFractionalLongitudinalPosition(), 0.000001);
63              assertEquals("longitudinalPosition should be " + position, position, linkLocation.getLongitudinalPosition().getSI(),
64                      0.0001);
65              // Repeat with the other constructor
66              linkLocation = new LinkLocation(link, new Length(position, METER));
67              assertEquals("link should be the provided Link", link, linkLocation.getLink());
68              assertEquals("fractionalLongitudinalPosition should be " + fraction, fraction,
69                      linkLocation.getFractionalLongitudinalPosition(), 0.000001);
70              assertEquals("longitudinalPosition should be " + position, position, linkLocation.getLongitudinalPosition().getSI(),
71                      0.0001);
72              double delta = referenceLocationDistance.getSI() - position;
73              assertEquals("distance from reference should be " + delta, delta, linkLocation.distance(referenceLocation).getSI(),
74                      0.0001);
75              // TODO distance to location on another link (not yet possible; currently ALWAYS returns null)
76          }
77      }
78  }