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  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  public class DirectedLanePosition implements Serializable
25  {
26      
27      private static final long serialVersionUID = 20151111L;
28  
29      
30      private final Lane lane;
31  
32      
33      private final Length position;
34  
35      
36      private final GTUDirectionality gtuDirection;
37  
38      
39      private LinkDirection linkDirection = null;
40  
41      
42  
43  
44  
45  
46  
47  
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  
62  
63  
64      public final Lane getLane()
65      {
66          return this.lane;
67      }
68  
69      
70  
71  
72  
73      public final Length getPosition()
74      {
75          return this.position;
76      }
77  
78      
79  
80  
81  
82  
83      public final GTUDirectionality getGtuDirection()
84      {
85          return this.gtuDirection;
86      }
87  
88      
89  
90  
91  
92      public final DirectedPoint getLocation()
93      {
94          
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 
108 
109 
110     public final LaneDirection getLaneDirection()
111     {
112         return new LaneDirection(this.lane, this.gtuDirection);
113     }
114 
115     
116 
117 
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     
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     
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     
172     @Override
173     public final String toString()
174     {
175         return "DirectedLanePosition [lane=" + this.lane + ", position=" + this.position + ", gtuDirection=" + this.gtuDirection
176                 + "]";
177     }
178 
179 }