package org.opentrafficsim.road.network;
import static org.junit.Assert.assertEquals;
import org.djunits.unit.LengthUnit;
import org.djunits.unit.util.UNITS;
import org.djunits.value.vdouble.scalar.Length;
import org.djutils.draw.line.PolyLine3d;
import org.djutils.draw.point.Point3d;
import org.junit.Test;
import org.opentrafficsim.core.dsol.OTSSimulatorInterface;
import org.opentrafficsim.core.network.LinkPosition;
import org.opentrafficsim.core.network.LinkType;
import org.opentrafficsim.core.network.NetworkException;
import org.opentrafficsim.core.network.Node;
import org.opentrafficsim.road.mock.MockSimulator;
import org.opentrafficsim.road.network.defaults.RoadNetworkDefaults;
import org.opentrafficsim.road.network.lane.CrossSectionLink;
import org.opentrafficsim.road.network.lane.changing.LaneKeepingPolicy;
/**
* Test the LinkPosition class.
*
* Copyright (c) 2013-2021 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved.
* BSD-style license. See OpenTrafficSim License.
*
* $LastChangedDate: 2015-09-16 19:20:07 +0200 (Wed, 16 Sep 2015) $, @version $Revision: 1405 $, by $Author: averbraeck $,
* initial version 20 jan. 2015
* @author Peter Knoppers
*/
public class LinkPositionTest implements UNITS
{
/**
* Test constructor and verify all getters.
* @throws NetworkException if that happens this test has failed
*/
@Test
public final void linkPositionTest() throws NetworkException
{
// Preparations
OTSSimulatorInterface simulator = MockSimulator.createMock();
RoadNetwork network = new RoadNetwork("link location test network", simulator);
RoadNetworkDefaults.registerDefaults(network);
Node nodeFrom = new Node(network, "From", new Point3d(0, 0, 0), 0.0);
Node nodeTo = new Node(network, "To", new Point3d(1000, 0, 0), 0.0);
PolyLine3d line = new PolyLine3d(new Point3d[] {new Point3d(0, 0, 0), new Point3d(1000, 0, 0)});
CrossSectionLink link = new CrossSectionLink(network, "Link", nodeFrom, nodeTo,
network.getLinkType(LinkType.DEFAULTS.ROAD), line, LaneKeepingPolicy.KEEPRIGHT);
Length linkLength = new Length(line.getLength(), LengthUnit.SI);
// Now we can make a LinkPosition.
Length referenceLocationDistance = new Length(123, METER);
LinkPosition referencePosition = new LinkPosition(link, referenceLocationDistance);
assertEquals("link should be the provided Link", link, referencePosition.getLink());
assertEquals("longitudinalPosition should be " + referenceLocationDistance, referenceLocationDistance.getSI(),
referencePosition.getLongitudinalPosition().getSI(), 0.0001);
for (int position = 0; position < 1000; position += 100)
{
final double fraction = position / linkLength.getSI();
LinkPosition linkPosition = new LinkPosition(link, fraction);
assertEquals("link should be the provided Link", link, linkPosition.getLink());
assertEquals("fractionalLongitudinalPosition should be " + fraction, fraction,
linkPosition.getFractionalLongitudinalPosition(), 0.000001);
assertEquals("longitudinalPosition should be " + position, position, linkPosition.getLongitudinalPosition().getSI(),
0.0001);
// Repeat with the other constructor
linkPosition = new LinkPosition(link, new Length(position, METER));
assertEquals("link should be the provided Link", link, linkPosition.getLink());
assertEquals("fractionalLongitudinalPosition should be " + fraction, fraction,
linkPosition.getFractionalLongitudinalPosition(), 0.000001);
assertEquals("longitudinalPosition should be " + position, position, linkPosition.getLongitudinalPosition().getSI(),
0.0001);
double delta = referenceLocationDistance.getSI() - position;
assertEquals("distance from reference should be " + delta, delta, linkPosition.distance(referencePosition).getSI(),
0.0001);
// TODO distance to location on another link (not yet possible; currently ALWAYS returns null)
}
}
}