View Javadoc
1   package org.opentrafficsim.road.network.lane;
2   
3   import nl.tudelft.simulation.language.d3.DirectedPoint;
4   
5   import org.djunits.value.vdouble.scalar.Length;
6   import org.opentrafficsim.core.gtu.GTUDirectionality;
7   import org.opentrafficsim.core.gtu.GTUException;
8   
9   /**
10   * <p>
11   * Copyright (c) 2013-2015 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: 2015-07-24 02:58:59 +0200 (Fri, 24 Jul 2015) $, @version $Revision: 1147 $, by $Author: averbraeck $,
15   * initial version Nov 11, 2015 <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 DirectedLanePosition
20  {
21      /** The lane for the position. */
22      private final Lane lane;
23  
24      /** The position on the lane, relative to the cross section link (design line). */
25      private final Length.Rel position;
26  
27      /** The direction the vehicle is driving to -- either in the direction of the design line, or against it. */
28      private final GTUDirectionality gtuDirection;
29  
30      /**
31       * @param lane the lane for the position
32       * @param position the position on the lane, relative to the cross section link (design line)
33       * @param gtuDirection the direction the vehicle is driving to -- either in the direction of the design line, or against it
34       * @throws GTUException when preconditions fail
35       */
36      public DirectedLanePosition(final Lane lane, final Length.Rel position, final GTUDirectionality gtuDirection)
37          throws GTUException
38      {
39          super();
40          GTUException.failIf(lane == null, "lane is null");
41          GTUException.failIf(position == null, "position is null");
42          GTUException.failIf(gtuDirection == null, "gtuDirection is null");
43          this.lane = lane;
44          this.position = position;
45          this.gtuDirection = gtuDirection;
46      }
47  
48      /**
49       * @return lane the lane for the position
50       */
51      public final Lane getLane()
52      {
53          return this.lane;
54      }
55  
56      /**
57       * @return position the position on the lane, relative to the cross section link (design line)
58       */
59      public final Length.Rel getPosition()
60      {
61          return this.position;
62      }
63  
64      /**
65       * @return gtuDirection the direction the vehicle is driving to -- either in the direction of the design line, or against it
66       */
67      public final GTUDirectionality getGtuDirection()
68      {
69          return this.gtuDirection;
70      }
71  
72      /**
73       * @return the location of the GTU on the lane, in the right direction.
74       */
75      public DirectedPoint getLocation()
76      {
77          double fraction = this.position.si / this.lane.getParentLink().getLength().si;
78          DirectedPoint p = this.lane.getCenterLine().getLocationFractionExtended(fraction);
79          if (this.gtuDirection.equals(GTUDirectionality.DIR_PLUS))
80          {
81              return p;
82          }
83          return new DirectedPoint(p.x, p.y, p.z, p.getRotX(), p.getRotY(), p.getRotZ() + Math.PI);
84      }
85  
86      /** {@inheritDoc} */
87      @Override
88      public int hashCode()
89      {
90          final int prime = 31;
91          int result = 1;
92          result = prime * result + ((this.gtuDirection == null) ? 0 : this.gtuDirection.hashCode());
93          result = prime * result + ((this.lane == null) ? 0 : this.lane.hashCode());
94          result = prime * result + ((this.position == null) ? 0 : this.position.hashCode());
95          return result;
96      }
97  
98      /** {@inheritDoc} */
99      @Override
100     public boolean equals(final Object obj)
101     {
102         if (this == obj)
103             return true;
104         if (obj == null)
105             return false;
106         if (getClass() != obj.getClass())
107             return false;
108         DirectedLanePosition other = (DirectedLanePosition) obj;
109         if (this.gtuDirection != other.gtuDirection)
110             return false;
111         if (this.lane == null)
112         {
113             if (other.lane != null)
114                 return false;
115         }
116         else if (!this.lane.equals(other.lane))
117             return false;
118         if (this.position == null)
119         {
120             if (other.position != null)
121                 return false;
122         }
123         else if (!this.position.equals(other.position))
124             return false;
125         return true;
126     }
127 
128     /** {@inheritDoc} */
129     @Override
130     public String toString()
131     {
132         return "DirectedLanePosition [lane=" + this.lane + ", position=" + this.position + ", gtuDirection="
133             + this.gtuDirection + "]";
134     }
135 
136 }