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