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