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