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
20
21
22
23
24
25
26
27
28 public class LinkLocationTest implements UNITS
29 {
30
31
32
33
34 @Test
35 public void linkLocationTest() throws OTSGeometryException
36 {
37
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
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
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
71 }
72 }
73 }