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-2020 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: 2020-04-01 12:03:44 +0200 (Wed, 01 Apr 2020) $, @version $Revision: 6424 $, by $Author: pknoppers $,
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 Link; The link of the location of a point relative to the GTU.
32 * @param fractionalLongitudinalPosition double; The fractional position (between 0.0 and 1.0) of the reference point on the
33 * link.
34 */
35 public LinkLocation(final Link link, final double fractionalLongitudinalPosition)
36 {
37 this.link = link;
38 this.fractionalLongitudinalPosition = fractionalLongitudinalPosition;
39 }
40
41 /**
42 * @param link Link; The link of the location of a point relative to the GTU.
43 * @param position Length; The position as a length of the reference point on the link.
44 */
45 public LinkLocation(final Link link, final Length position)
46 {
47 this.link = link;
48 this.fractionalLongitudinalPosition = position.divide(this.link.getLength()).doubleValue();
49 }
50
51 /**
52 * @return lane.
53 */
54 public final Link getLink()
55 {
56 return this.link;
57 }
58
59 /**
60 * @return fractionalLongitudinalPosition.
61 */
62 public final double getFractionalLongitudinalPosition()
63 {
64 return this.fractionalLongitudinalPosition;
65 }
66
67 /**
68 * @return position as a length as a traveled length on this link.
69 */
70 public final Length getLongitudinalPosition()
71 {
72 return new Length(this.link.getLength().getSI() * getFractionalLongitudinalPosition(), LengthUnit.METER);
73 }
74
75 /**
76 * Returns the distance to another LinkLocation. If the other location is in front of us, the distance is positive. If it is
77 * behind us, it is negative.
78 * @param loc LinkLocation; the link location to find the distance to.
79 * @return the distance to another LinkLocation.
80 */
81 public final Length distance(final LinkLocation loc)
82 {
83 if (this.link.equals(loc.getLink()))
84 {
85 return loc.getLongitudinalPosition().minus(this.getLongitudinalPosition());
86 }
87
88 // TODO not on the same link. Find shortest path...
89 return null;
90 }
91
92 /** {@inheritDoc} */
93 @Override
94 public final String toString()
95 {
96 return String.format("%s %.3f%s", getLink(), getLongitudinalPosition().getInUnit(),
97 getLongitudinalPosition().getDisplayUnit());
98 }
99 }