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