View Javadoc
1   package org.opentrafficsim.core.network;
2   
3   import static org.junit.Assert.assertEquals;
4   
5   import org.junit.Test;
6   import org.opentrafficsim.core.network.geotools.NodeGeotools;
7   import org.opentrafficsim.core.network.lane.CrossSectionLink;
8   import org.opentrafficsim.core.unit.LengthUnit;
9   import org.opentrafficsim.core.value.vdouble.scalar.DoubleScalar;
10  
11  import com.vividsolutions.jts.geom.Coordinate;
12  
13  /**
14   * Test the LinkLocation class.
15   * <p>
16   * Copyright (c) 2013-2014 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights
17   * reserved. <br>
18   * BSD-style license. See <a href="http://opentrafficsim.org/node/13">OpenTrafficSim License</a>.
19   * <p>
20   * @version 20 jan. 2015 <br>
21   * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
22   */
23  public class LinkLocationTest
24  {
25      /**
26       * Test constructor and verify all getters.
27       */
28      @Test
29      public void linkLocationTest()
30      {
31          // Preparations
32          NodeGeotools.STR nodeFrom = new NodeGeotools.STR("From", new Coordinate(0, 0, 0));
33          NodeGeotools.STR nodeTo = new NodeGeotools.STR("To", new Coordinate(1000, 0, 0));
34          DoubleScalar.Rel<LengthUnit> linkLength = new DoubleScalar.Rel<LengthUnit>(1000, LengthUnit.METER);
35          CrossSectionLink<?, ?> link = new CrossSectionLink<String, String>("Link", nodeFrom, nodeTo, linkLength);
36          // Now we can make a LinkLocation.
37          DoubleScalar.Rel<LengthUnit> referenceLocationDistance =
38                  new DoubleScalar.Rel<LengthUnit>(123, LengthUnit.METER);
39          LinkLocation referenceLocation = new LinkLocation(link, referenceLocationDistance);
40          assertEquals("link should be the provided Link", link, referenceLocation.getLink());
41          assertEquals("longitudinalPosition should be " + referenceLocationDistance, referenceLocationDistance.getSI(),
42                  referenceLocation.getLongitudinalPosition().getSI(), 0.0001);
43          for (int position = 0; position < 1000; position += 100)
44          {
45              final double fraction = position / linkLength.getSI();
46              LinkLocation linkLocation = new LinkLocation(link, fraction);
47              assertEquals("link should be the provided Link", link, linkLocation.getLink());
48              assertEquals("fractionalLongitudinalPosition should be " + fraction, fraction,
49                      linkLocation.getFractionalLongitudinalPosition(), 0.000001);
50              assertEquals("longitudinalPosition should be " + position, position, linkLocation.getLongitudinalPosition()
51                      .getSI(), 0.0001);
52              // Repeat with the other constructor
53              linkLocation = new LinkLocation(link, new DoubleScalar.Rel<LengthUnit>(position, LengthUnit.METER));
54              assertEquals("link should be the provided Link", link, linkLocation.getLink());
55              assertEquals("fractionalLongitudinalPosition should be " + fraction, fraction,
56                      linkLocation.getFractionalLongitudinalPosition(), 0.000001);
57              assertEquals("longitudinalPosition should be " + position, position, linkLocation.getLongitudinalPosition()
58                      .getSI(), 0.0001);
59              double delta = referenceLocationDistance.getSI() - position;
60              assertEquals("distance from reference should be " + delta, delta, linkLocation.distance(referenceLocation)
61                      .getSI(), 0.0001);
62              // TODO distance to location on another link (not yet possible; currently ALWAYS returns null)
63          }
64      }
65  }