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
26
27
28
29
30
31
32
33
34 public class LinkLocationTest implements UNITS
35 {
36
37
38
39
40
41 @Test
42 public void linkLocationTest() throws OTSGeometryException, NetworkException
43 {
44
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
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
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
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
82 }
83 }
84 }