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.OTSLine3D;
8   import org.opentrafficsim.core.gtu.GTUDirectionality;
9   import org.opentrafficsim.core.gtu.GTUException;
10  import org.opentrafficsim.core.network.LinkDirection;
11  
12  import nl.tudelft.simulation.language.d3.DirectedPoint;
13  
14  /**
15   * Store one position, direction and lane of a GTU.
16   * <p>
17   * Copyright (c) 2013-2020 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
18   * BSD-style license. See <a href="http://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
19   * </p>
20   * $LastChangedDate: 2015-07-24 02:58:59 +0200 (Fri, 24 Jul 2015) $, @version $Revision: 1147 $, by $Author: averbraeck $,
21   * initial version Nov 11, 2015 <br>
22   * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
23   * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
24   */
25  public class DirectedLanePosition implements Serializable
26  {
27      /** */
28      private static final long serialVersionUID = 20151111L;
29  
30      /** The lane for the position. */
31      private final Lane lane;
32  
33      /** The position on the lane, relative to the cross section link (design line). */
34      private final Length position;
35  
36      /** The direction the vehicle is driving to -- either in the direction of the design line, or against it. */
37      private final GTUDirectionality gtuDirection;
38  
39      /** Link direction. */
40      private LinkDirection linkDirection = null;
41  
42      /**
43       * Construct a new DirectedLanePosition.
44       * @param lane Lane; the lane for the position
45       * @param position Length; the position on the lane, relative to the cross section link (design line)
46       * @param gtuDirection GTUDirectionality; the direction the vehicle is driving to -- either in the direction of the design
47       *            line, or against it
48       * @throws GTUException when preconditions fail
49       */
50      public DirectedLanePosition(final Lane lane, final Length position, final GTUDirectionality gtuDirection)
51              throws GTUException
52      {
53          Throw.when(lane == null, GTUException.class, "lane is null");
54          Throw.when(position == null, GTUException.class, "position is null");
55          Throw.when(gtuDirection == null, GTUException.class, "gtuDirection is null");
56          this.lane = lane;
57          this.position = position;
58          this.gtuDirection = gtuDirection;
59      }
60  
61      /**
62       * Retrieve the lane.
63       * @return Lane; the lane for the position
64       */
65      public final Lane getLane()
66      {
67          return this.lane;
68      }
69  
70      /**
71       * Retrieve the position on the lane.
72       * @return Length; the position on the lane, relative to the cross section link (design line)
73       */
74      public final Length getPosition()
75      {
76          return this.position;
77      }
78  
79      /**
80       * Retrieve the gtuDirection.
81       * @return GTUDirectionality; gtuDirection the direction the vehicle is driving to -- either in the direction of the design
82       *         line, or against it
83       */
84      public final GTUDirectionality getGtuDirection()
85      {
86          return this.gtuDirection;
87      }
88  
89      /**
90       * Retrieve the location and direction of the GTU on the lane.
91       * @return DirectedPoint; the location and direction of the GTU on the lane
92       */
93      public final DirectedPoint getLocation()
94      {
95          // double fraction = this.position.si / this.lane.getParentLink().getLength().si;
96          OTSLine3D centerLine = this.lane.getCenterLine();
97          double centerLineLength = centerLine.getLengthSI();
98          double fraction = this.position.si / centerLineLength;
99          DirectedPoint p = centerLine.getLocationFractionExtended(fraction);
100         if (this.gtuDirection.equals(GTUDirectionality.DIR_PLUS))
101         {
102             return p;
103         }
104         return new DirectedPoint(p.x, p.y, p.z, p.getRotX(), p.getRotY(), p.getRotZ() + Math.PI);
105     }
106 
107     /**
108      * Returns the lane direction in the direction of this lane direction.
109      * @return lane direction in the direction of this lane direction
110      */
111     public final LaneDirection getLaneDirection()
112     {
113         return new LaneDirection(this.lane, this.gtuDirection);
114     }
115 
116     /**
117      * Returns the link direction in the direction of this lane direction.
118      * @return link direction in the direction of this lane direction
119      */
120     public final LinkDirection getLinkDirection()
121     {
122         if (this.linkDirection == null)
123         {
124             this.linkDirection = new LinkDirection(this.lane.getParentLink(), this.gtuDirection);
125         }
126         return this.linkDirection;
127     }
128 
129     /** {@inheritDoc} */
130     @Override
131     public int hashCode()
132     {
133         final int prime = 31;
134         int result = 1;
135         result = prime * result + ((this.gtuDirection == null) ? 0 : this.gtuDirection.hashCode());
136         result = prime * result + ((this.lane == null) ? 0 : this.lane.hashCode());
137         result = prime * result + ((this.position == null) ? 0 : this.position.hashCode());
138         return result;
139     }
140 
141     /** {@inheritDoc} */
142     @SuppressWarnings("checkstyle:needbraces")
143     @Override
144     public boolean equals(final Object obj)
145     {
146         if (this == obj)
147             return true;
148         if (obj == null)
149             return false;
150         if (getClass() != obj.getClass())
151             return false;
152         DirectedLanePosition./org/opentrafficsim/road/network/lane/DirectedLanePosition.html#DirectedLanePosition">DirectedLanePosition other = (DirectedLanePosition) obj;
153         if (this.gtuDirection != other.gtuDirection)
154             return false;
155         if (this.lane == null)
156         {
157             if (other.lane != null)
158                 return false;
159         }
160         else if (!this.lane.equals(other.lane))
161             return false;
162         if (this.position == null)
163         {
164             if (other.position != null)
165                 return false;
166         }
167         else if (!this.position.equals(other.position))
168             return false;
169         return true;
170     }
171 
172     /** {@inheritDoc} */
173     @Override
174     public final String toString()
175     {
176         return "DirectedLanePosition [lane=" + this.lane + ", position=" + this.position + ", gtuDirection=" + this.gtuDirection
177                 + "]";
178     }
179 
180 }