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.OTSLine3D;
8 import org.opentrafficsim.core.gtu.GTUDirectionality;
9 import org.opentrafficsim.core.gtu.GTUException;
10 import org.opentrafficsim.core.network.LinkDirection;
11
12 import nl.tudelft.simulation.language.d3.DirectedPoint;
13
14
15
16
17
18
19
20
21
22
23
24
25 public class DirectedLanePosition implements Serializable
26 {
27
28 private static final long serialVersionUID = 20151111L;
29
30
31 private final Lane lane;
32
33
34 private final Length position;
35
36
37 private final GTUDirectionality gtuDirection;
38
39
40 private LinkDirection linkDirection = null;
41
42
43
44
45
46
47
48
49
50 public DirectedLanePosition(final Lane lane, final Length position, final GTUDirectionality gtuDirection)
51 throws GTUException
52 {
53 Throw.when(lane == null, GTUException.class, "lane is null");
54 Throw.when(position == null, GTUException.class, "position is null");
55 Throw.when(gtuDirection == null, GTUException.class, "gtuDirection is null");
56 this.lane = lane;
57 this.position = position;
58 this.gtuDirection = gtuDirection;
59 }
60
61
62
63
64
65 public final Lane getLane()
66 {
67 return this.lane;
68 }
69
70
71
72
73
74 public final Length getPosition()
75 {
76 return this.position;
77 }
78
79
80
81
82
83
84 public final GTUDirectionality getGtuDirection()
85 {
86 return this.gtuDirection;
87 }
88
89
90
91
92
93 public final DirectedPoint getLocation()
94 {
95
96 OTSLine3D centerLine = this.lane.getCenterLine();
97 double centerLineLength = centerLine.getLengthSI();
98 double fraction = this.position.si / centerLineLength;
99 DirectedPoint p = centerLine.getLocationFractionExtended(fraction);
100 if (this.gtuDirection.equals(GTUDirectionality.DIR_PLUS))
101 {
102 return p;
103 }
104 return new DirectedPoint(p.x, p.y, p.z, p.getRotX(), p.getRotY(), p.getRotZ() + Math.PI);
105 }
106
107
108
109
110
111 public final LaneDirection getLaneDirection()
112 {
113 return new LaneDirection(this.lane, this.gtuDirection);
114 }
115
116
117
118
119
120 public final LinkDirection getLinkDirection()
121 {
122 if (this.linkDirection == null)
123 {
124 this.linkDirection = new LinkDirection(this.lane.getParentLink(), this.gtuDirection);
125 }
126 return this.linkDirection;
127 }
128
129
130 @Override
131 public int hashCode()
132 {
133 final int prime = 31;
134 int result = 1;
135 result = prime * result + ((this.gtuDirection == null) ? 0 : this.gtuDirection.hashCode());
136 result = prime * result + ((this.lane == null) ? 0 : this.lane.hashCode());
137 result = prime * result + ((this.position == null) ? 0 : this.position.hashCode());
138 return result;
139 }
140
141
142 @SuppressWarnings("checkstyle:needbraces")
143 @Override
144 public boolean equals(final Object obj)
145 {
146 if (this == obj)
147 return true;
148 if (obj == null)
149 return false;
150 if (getClass() != obj.getClass())
151 return false;
152 DirectedLanePosition./org/opentrafficsim/road/network/lane/DirectedLanePosition.html#DirectedLanePosition">DirectedLanePosition other = (DirectedLanePosition) obj;
153 if (this.gtuDirection != other.gtuDirection)
154 return false;
155 if (this.lane == null)
156 {
157 if (other.lane != null)
158 return false;
159 }
160 else if (!this.lane.equals(other.lane))
161 return false;
162 if (this.position == null)
163 {
164 if (other.position != null)
165 return false;
166 }
167 else if (!this.position.equals(other.position))
168 return false;
169 return true;
170 }
171
172
173 @Override
174 public final String toString()
175 {
176 return "DirectedLanePosition [lane=" + this.lane + ", position=" + this.position + ", gtuDirection=" + this.gtuDirection
177 + "]";
178 }
179
180 }