View Javadoc
1   package org.opentrafficsim.road.network.lane;
2   
3   import java.io.Serializable;
4   
5   import org.djunits.value.vdouble.scalar.Length;
6   import org.djutils.exceptions.Throw;
7   import org.opentrafficsim.core.geometry.DirectedPoint;
8   import org.opentrafficsim.core.geometry.OtsLine3d;
9   
10  /**
11   * Store one position and lane of a GTU.
12   * <p>
13   * Copyright (c) 2013-2023 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
14   * BSD-style license. See <a href="https://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
15   * </p>
16   * @author <a href="https://github.com/averbraeck">Alexander Verbraeck</a>
17   * @author <a href="https://tudelft.nl/staff/p.knoppers-1">Peter Knoppers</a>
18   * @author <a href="https://dittlab.tudelft.nl">Wouter Schakel</a>
19   */
20  public class LanePosition implements Serializable
21  {
22      /** */
23      private static final long serialVersionUID = 20151111L;
24  
25      /** The lane for the position. */
26      private final Lane lane;
27  
28      /** The position on the lane, relative to the cross section link (design line). */
29      private final Length position;
30  
31      /**
32       * Construct a new LanePosition.
33       * @param lane Lane; the lane for the position
34       * @param position Length; the position on the lane, relative to the cross section link (design line) line, or against it
35       */
36      public LanePosition(final Lane lane, final Length position)
37      {
38          Throw.whenNull(lane, "lane is null");
39          Throw.whenNull(position, "position is null");
40          this.lane = lane;
41          this.position = position;
42      }
43  
44      /**
45       * Retrieve the lane.
46       * @return Lane; the lane for the position
47       */
48      public final Lane getLane()
49      {
50          return this.lane;
51      }
52  
53      /**
54       * Retrieve the position on the lane.
55       * @return Length; the position on the lane, relative to the cross section link (design line)
56       */
57      public final Length getPosition()
58      {
59          return this.position;
60      }
61  
62      /**
63       * Retrieve the location and direction of the GTU on the lane.
64       * @return DirectedPoint; the location and direction of the GTU on the lane
65       */
66      public final DirectedPoint getLocation()
67      {
68          // double fraction = this.position.si / this.lane.getParentLink().getLength().si;
69          OtsLine3d centerLine = this.lane.getCenterLine();
70          double centerLineLength = centerLine.getLengthSI();
71          double fraction = this.position.si / centerLineLength;
72          return centerLine.getLocationFractionExtended(fraction);
73      }
74  
75      /** {@inheritDoc} */
76      @Override
77      public int hashCode()
78      {
79          final int prime = 31;
80          int result = 1;
81          result = prime * result + ((this.lane == null) ? 0 : this.lane.hashCode());
82          result = prime * result + ((this.position == null) ? 0 : this.position.hashCode());
83          return result;
84      }
85  
86      /** {@inheritDoc} */
87      @SuppressWarnings("checkstyle:needbraces")
88      @Override
89      public boolean equals(final Object obj)
90      {
91          if (this == obj)
92              return true;
93          if (obj == null)
94              return false;
95          if (getClass() != obj.getClass())
96              return false;
97          LanePosition other = (LanePosition) obj;
98          if (this.lane == null)
99          {
100             if (other.lane != null)
101                 return false;
102         }
103         else if (!this.lane.equals(other.lane))
104             return false;
105         if (this.position == null)
106         {
107             if (other.position != null)
108                 return false;
109         }
110         else if (!this.position.equals(other.position))
111             return false;
112         return true;
113     }
114 
115     /** {@inheritDoc} */
116     @Override
117     public final String toString()
118     {
119         return "LanePosition [lane=" + this.lane + ", position=" + this.position + "]";
120     }
121 
122 }