1 package org.opentrafficsim.core.network;
2
3 import java.io.Serializable;
4
5 import org.djunits.unit.LengthUnit;
6 import org.djunits.value.vdouble.scalar.Length;
7
8 /**
9 * "1D" implementation. Mapping on the design line (often the center line) of a road.
10 * <p>
11 * Copyright (c) 2013-2018 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
12 * BSD-style license. See <a href="http://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
13 * <p>
14 * $LastChangedDate: 2018-09-19 13:55:45 +0200 (Wed, 19 Sep 2018) $, @version $Revision: 4006 $, by $Author: averbraeck $,
15 * initial version Oct 22, 2014 <br>
16 * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
17 * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
18 */
19 public class LinkLocation implements Serializable
20 {
21 /** */
22 private static final long serialVersionUID = 20150000L;
23
24 /** The link of the location of a point relative to the GTU. */
25 private final Link link;
26
27 /** The fractional position (between 0.0 and 1.0) of the reference point on the lane. */
28 private final double fractionalLongitudinalPosition;
29
30 /**
31 * @param link The link of the location of a point relative to the GTU.
32 * @param fractionalLongitudinalPosition The fractional position (between 0.0 and 1.0) of the reference point on the link.
33 */
34 public LinkLocation(final Link link, final double fractionalLongitudinalPosition)
35 {
36 super();
37 this.link = link;
38 this.fractionalLongitudinalPosition = fractionalLongitudinalPosition;
39 }
40
41 /**
42 * @param link The link of the location of a point relative to the GTU.
43 * @param position The position as a length of the reference point on the link.
44 */
45 public LinkLocation(final Link link, final Length position)
46 {
47 super();
48 this.link = link;
49 this.fractionalLongitudinalPosition = position.divideBy(this.link.getLength()).doubleValue();
50 }
51
52 /**
53 * @return lane.
54 */
55 public final Link getLink()
56 {
57 return this.link;
58 }
59
60 /**
61 * @return fractionalLongitudinalPosition.
62 */
63 public final double getFractionalLongitudinalPosition()
64 {
65 return this.fractionalLongitudinalPosition;
66 }
67
68 /**
69 * @return position as a length as a traveled length on this link.
70 */
71 public final Length getLongitudinalPosition()
72 {
73 return new Length(this.link.getLength().getSI() * getFractionalLongitudinalPosition(), LengthUnit.METER);
74 }
75
76 /**
77 * Returns the distance to another LinkLocation. If the other location is in front of us, the distance is positive. If it is
78 * behind us, it is negative.
79 * @param loc the link location to find the distance to.
80 * @return the distance to another LinkLocation.
81 */
82 public final Length distance(final LinkLocation loc)
83 {
84 if (this.link.equals(loc.getLink()))
85 {
86 return loc.getLongitudinalPosition().minus(this.getLongitudinalPosition());
87 }
88
89 // TODO not on the same link. Find shortest path...
90 return null;
91 }
92
93 /** {@inheritDoc} */
94 @Override
95 public final String toString()
96 {
97 return String.format("%s %.3f%s", getLink(), getLongitudinalPosition().getInUnit(),
98 getLongitudinalPosition().getUnit());
99 }
100 }