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.opentrafficsim.core.geometry.OTSLine3D;
7   import org.opentrafficsim.core.gtu.GTUDirectionality;
8   import org.opentrafficsim.core.gtu.GTUException;
9   
10  import nl.tudelft.simulation.language.Throw;
11  import nl.tudelft.simulation.language.d3.DirectedPoint;
12  
13  /**
14   * Store one position, direction and lane of a GTU.
15   * <p>
16   * Copyright (c) 2013-2016 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      /**
39       * Construct a new DirectedLanePosition.
40       * @param lane Lane; the lane for the position
41       * @param position Length; the position on the lane, relative to the cross section link (design line)
42       * @param gtuDirection GTUDirectionality; the direction the vehicle is driving to -- either in the direction of the design
43       *            line, or against it
44       * @throws GTUException when preconditions fail
45       */
46      public DirectedLanePosition(final Lane lane, final Length position, final GTUDirectionality gtuDirection)
47              throws GTUException
48      {
49          super();
50          Throw.when(lane == null, GTUException.class, "lane is null");
51          Throw.when(position == null, GTUException.class, "position is null");
52          Throw.when(gtuDirection == null, GTUException.class, "gtuDirection is null");
53          this.lane = lane;
54          this.position = position;
55          this.gtuDirection = gtuDirection;
56      }
57  
58      /**
59       * Retrieve the lane.
60       * @return Lane; the lane for the position
61       */
62      public final Lane getLane()
63      {
64          return this.lane;
65      }
66  
67      /**
68       * Retrieve the position on the lane.
69       * @return Length; the position on the lane, relative to the cross section link (design line)
70       */
71      public final Length getPosition()
72      {
73          return this.position;
74      }
75  
76      /**
77       * Retrieve the gtuDirection.
78       * @return GTUDirectionality; gtuDirection the direction the vehicle is driving to -- either in the direction of the design
79       *         line, or against it
80       */
81      public final GTUDirectionality getGtuDirection()
82      {
83          return this.gtuDirection;
84      }
85  
86      /**
87       * Retrieve the location and direction of the GTU on the lane.
88       * @return DirectedPoint; the location and direction of the GTU on the lane
89       */
90      public DirectedPoint getLocation()
91      {
92          // double fraction = this.position.si / this.lane.getParentLink().getLength().si;
93          OTSLine3D centerLine = this.lane.getCenterLine();
94          double centerLineLength = centerLine.getLengthSI();
95          double fraction = this.position.si / centerLineLength;
96          DirectedPoint p = centerLine.getLocationFractionExtended(fraction);
97          if (this.gtuDirection.equals(GTUDirectionality.DIR_PLUS))
98          {
99              return p;
100         }
101         return new DirectedPoint(p.x, p.y, p.z, p.getRotX(), p.getRotY(), p.getRotZ() + Math.PI);
102     }
103 
104     /** {@inheritDoc} */
105     @Override
106     public int hashCode()
107     {
108         final int prime = 31;
109         int result = 1;
110         result = prime * result + ((this.gtuDirection == null) ? 0 : this.gtuDirection.hashCode());
111         result = prime * result + ((this.lane == null) ? 0 : this.lane.hashCode());
112         result = prime * result + ((this.position == null) ? 0 : this.position.hashCode());
113         return result;
114     }
115 
116     /** {@inheritDoc} */
117     @Override
118     public boolean equals(final Object obj)
119     {
120         if (this == obj)
121             return true;
122         if (obj == null)
123             return false;
124         if (getClass() != obj.getClass())
125             return false;
126         DirectedLanePosition other = (DirectedLanePosition) obj;
127         if (this.gtuDirection != other.gtuDirection)
128             return false;
129         if (this.lane == null)
130         {
131             if (other.lane != null)
132                 return false;
133         }
134         else if (!this.lane.equals(other.lane))
135             return false;
136         if (this.position == null)
137         {
138             if (other.position != null)
139                 return false;
140         }
141         else if (!this.position.equals(other.position))
142             return false;
143         return true;
144     }
145 
146     /** {@inheritDoc} */
147     @Override
148     public String toString()
149     {
150         return "DirectedLanePosition [lane=" + this.lane + ", position=" + this.position + ", gtuDirection="
151                 + this.gtuDirection + "]";
152     }
153 
154 }