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.NetworkException;
15  import org.opentrafficsim.core.network.OTSNode;
16  import org.opentrafficsim.road.mock.MockSimulator;
17  import org.opentrafficsim.road.network.lane.CrossSectionLink;
18  import org.opentrafficsim.road.network.lane.changing.LaneKeepingPolicy;
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  public class LinkLocationTest implements UNITS
31  {
32      
33  
34  
35  
36  
37      @Test
38      public final void linkLocationTest() throws OTSGeometryException, NetworkException
39      {
40          
41          OTSRoadNetwork network = new OTSRoadNetwork("link location test network", true);
42          OTSNode nodeFrom = new OTSNode(network, "From", new OTSPoint3D(0, 0, 0));
43          OTSNode nodeTo = new OTSNode(network, "To", new OTSPoint3D(1000, 0, 0));
44          OTSLine3D line = new OTSLine3D(new OTSPoint3D[] {new OTSPoint3D(0, 0, 0), new OTSPoint3D(1000, 0, 0)});
45          OTSSimulatorInterface simulator = MockSimulator.createMock();
46          CrossSectionLink link = new CrossSectionLink(network, "Link", nodeFrom, nodeTo,
47                  network.getLinkType(LinkType.DEFAULTS.ROAD), line, simulator, LaneKeepingPolicy.KEEPRIGHT);
48          Length linkLength = line.getLength();
49          
50          Length referenceLocationDistance = new Length(123, METER);
51          LinkLocation referenceLocation = new LinkLocation(link, referenceLocationDistance);
52          assertEquals("link should be the provided Link", link, referenceLocation.getLink());
53          assertEquals("longitudinalPosition should be " + referenceLocationDistance, referenceLocationDistance.getSI(),
54                  referenceLocation.getLongitudinalPosition().getSI(), 0.0001);
55          for (int position = 0; position < 1000; position += 100)
56          {
57              final double fraction = position / linkLength.getSI();
58              LinkLocation linkLocation = new LinkLocation(link, fraction);
59              assertEquals("link should be the provided Link", link, linkLocation.getLink());
60              assertEquals("fractionalLongitudinalPosition should be " + fraction, fraction,
61                      linkLocation.getFractionalLongitudinalPosition(), 0.000001);
62              assertEquals("longitudinalPosition should be " + position, position, linkLocation.getLongitudinalPosition().getSI(),
63                      0.0001);
64              
65              linkLocation = new LinkLocation(link, new Length(position, METER));
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              double delta = referenceLocationDistance.getSI() - position;
72              assertEquals("distance from reference should be " + delta, delta, linkLocation.distance(referenceLocation).getSI(),
73                      0.0001);
74              
75          }
76      }
77  }