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