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.definitions.DefaultsNl;
10  import org.opentrafficsim.core.dsol.OtsSimulatorInterface;
11  import org.opentrafficsim.core.geometry.OtsGeometryException;
12  import org.opentrafficsim.core.geometry.OtsLine3d;
13  import org.opentrafficsim.core.geometry.OtsPoint3d;
14  import org.opentrafficsim.core.network.LinkLocation;
15  import org.opentrafficsim.core.network.NetworkException;
16  import org.opentrafficsim.core.network.Node;
17  import org.opentrafficsim.road.mock.MockSimulator;
18  import org.opentrafficsim.road.network.lane.CrossSectionLink;
19  import org.opentrafficsim.road.network.lane.changing.LaneKeepingPolicy;
20  
21  /**
22   * Test the LinkLocation class.
23   * <p>
24   * Copyright (c) 2013-2023 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
25   * BSD-style license. See <a href="https://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
26   * </p>
27   * @author <a href="https://tudelft.nl/staff/p.knoppers-1">Peter Knoppers</a>
28   */
29  public class LinkLocationTest implements UNITS
30  {
31      /**
32       * Test constructor and verify all getters.
33       * @throws OtsGeometryException if that happens this test has failed
34       * @throws NetworkException if that happens this test has failed
35       */
36      @Test
37      public final void linkLocationTest() throws OtsGeometryException, NetworkException
38      {
39          // Preparations
40          OtsSimulatorInterface simulator = MockSimulator.createMock();
41          RoadNetwork network = new RoadNetwork("link location test network", simulator);
42          Node nodeFrom = new Node(network, "From", new OtsPoint3d(0, 0, 0), Direction.ZERO);
43          Node nodeTo = new Node(network, "To", new OtsPoint3d(1000, 0, 0), Direction.ZERO);
44          OtsLine3d line = new OtsLine3d(new OtsPoint3d[] {new OtsPoint3d(0, 0, 0), new OtsPoint3d(1000, 0, 0)});
45          CrossSectionLink link =
46                  new CrossSectionLink(network, "Link", nodeFrom, nodeTo, DefaultsNl.ROAD, line, LaneKeepingPolicy.KEEPRIGHT);
47          Length linkLength = line.getLength();
48          // Now we can make a LinkLocation.
49          Length referenceLocationDistance = new Length(123, METER);
50          LinkLocation referenceLocation = new LinkLocation(link, referenceLocationDistance);
51          assertEquals("link should be the provided Link", link, referenceLocation.getLink());
52          assertEquals("longitudinalPosition should be " + referenceLocationDistance, referenceLocationDistance.getSI(),
53                  referenceLocation.getLongitudinalPosition().getSI(), 0.0001);
54          for (int position = 0; position < 1000; position += 100)
55          {
56              final double fraction = position / linkLength.getSI();
57              LinkLocation linkLocation = new LinkLocation(link, fraction);
58              assertEquals("link should be the provided Link", link, linkLocation.getLink());
59              assertEquals("fractionalLongitudinalPosition should be " + fraction, fraction,
60                      linkLocation.getFractionalLongitudinalPosition(), 0.000001);
61              assertEquals("longitudinalPosition should be " + position, position, linkLocation.getLongitudinalPosition().getSI(),
62                      0.0001);
63              // Repeat with the other constructor
64              linkLocation = new LinkLocation(link, new Length(position, METER));
65              assertEquals("link should be the provided Link", link, linkLocation.getLink());
66              assertEquals("fractionalLongitudinalPosition should be " + fraction, fraction,
67                      linkLocation.getFractionalLongitudinalPosition(), 0.000001);
68              assertEquals("longitudinalPosition should be " + position, position, linkLocation.getLongitudinalPosition().getSI(),
69                      0.0001);
70              double delta = referenceLocationDistance.getSI() - position;
71              assertEquals("distance from reference should be " + delta, delta, linkLocation.distance(referenceLocation).getSI(),
72                      0.0001);
73              // TODO distance to location on another link (not yet possible; currently ALWAYS returns null)
74          }
75      }
76  }