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