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