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