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