1 package org.opentrafficsim.road.gtu.lane.perception.headway;
2
3 import java.util.ArrayList;
4 import java.util.EnumSet;
5 import java.util.List;
6
7 import org.djunits.value.vdouble.scalar.Acceleration;
8 import org.djunits.value.vdouble.scalar.Length;
9 import org.djunits.value.vdouble.scalar.Speed;
10 import org.djunits.value.vdouble.scalar.Time;
11 import org.opentrafficsim.core.gtu.GTUException;
12 import org.opentrafficsim.core.gtu.GTUType;
13 import org.opentrafficsim.core.network.NetworkException;
14 import org.opentrafficsim.road.gtu.lane.LaneBasedGTU;
15 import org.opentrafficsim.road.network.speed.SpeedLimitInfo;
16 import org.opentrafficsim.road.network.speed.SpeedLimitTypes;
17
18 import nl.tudelft.simulation.language.Throw;
19
20 /**
21 * Container for a reference to information about a (lane based) GTU and a headway. The Headway can store information about GTUs
22 * or objects ahead of the reference GTU, behind the reference GTU, or (partially) parallel to the reference GTU. In addition to
23 * the (perceived) headway, several other pieces of information can be stored, such as (perceived) speed, (perceived)
24 * acceleration, (perceived) turn indicators, and (perceived) braking lights. <br>
25 * Special care must be taken in curves when perceiving headway of a GTU or object on an adjacent lane.The question is whether
26 * we perceive the parallel or ahead/behind based on a line perpendicular to the front/back of the GTU (rectangular), or
27 * perpendicular to the center line of the lane (wedge-shaped in case of a curve). The difficulty of a wedge-shaped situation is
28 * that reciprocity might be violated: in case of a clothoid, for instance, it is not sure that the point on the center line
29 * when projected from lane 1 to lane 2 is the same as the projection from lane 2 to lane 1. The same holds for shapes with
30 * sharp bends. Therefore, algorithms implementing headway should only project the <i>reference point</i> of the reference GTU
31 * on the center line of the adjacent lane, and then calculate the forward position and backward position on the adjacent lane
32 * based on the reference point. Still, our human perception of what is parallel and what not, is not reflected by fractional
33 * positions. See examples in
34 * <a href= "http://simulation.tudelft.nl:8085/browse/OTS-113">http://simulation.tudelft.nl:8085/browse/OTS-113</a>.
35 * <p>
36 * Copyright (c) 2013-2018 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
37 * BSD-style license. See <a href="http://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
38 * <p>
39 * @version $Revision: 1368 $, $LastChangedDate: 2015-09-02 00:20:20 +0200 (Wed, 02 Sep 2015) $, by $Author: averbraeck $,
40 * initial version 11 feb. 2015 <br>
41 * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
42 * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
43 */
44 public abstract class AbstractHeadwayGTU extends AbstractHeadwayCopy implements HeadwayGTU
45 {
46 /** */
47 private static final long serialVersionUID = 20160410L;
48
49 /** The perceived GTU Type, or null if unknown. */
50 private final GTUType gtuType;
51
52 /** Whether the GTU is facing the same direction. */
53 private final boolean facingSameDirection;
54
55 /** The observable characteristics of the GTU. */
56 private final EnumSet<GTUStatus> gtuStatus = EnumSet.noneOf(GTUStatus.class);
57
58 /** Perceived desired speed. */
59 private final Speed desiredSpeed;
60
61 /** Perceived width. */
62 private final Length width;
63
64 /**
65 * Construct a new Headway information object, for a moving GTU ahead of us or behind us.
66 * @param id the id of the GTU for comparison purposes, can not be null.
67 * @param gtuType the perceived GTU Type, or null if unknown.
68 * @param distance the distance to the other object; if this constructor is used, distance cannot be null.
69 * @param facingSameDirection whether the GTU is facing the same direction.
70 * @param length the (perceived) length of the other object; can not be null.
71 * @param width the (perceived) width of the other object; can not be null.
72 * @param speed the (perceived) speed of the other object; can be null if unknown.
73 * @param acceleration the (perceived) acceleration of the other object; can be null if unknown.
74 * @param desiredSpeed desired speed
75 * @param gtuStatus the observable characteristics of the GTU.
76 * @throws GTUException when id is null, objectType is null, or parameters are inconsistent
77 */
78 @SuppressWarnings("checkstyle:parameternumber")
79 public AbstractHeadwayGTU(final String id, final GTUType gtuType, final Length distance, final boolean facingSameDirection,
80 final Length length, final Length width, final Speed speed, final Acceleration acceleration,
81 final Speed desiredSpeed, final GTUStatus... gtuStatus) throws GTUException
82 {
83 super(ObjectType.GTU, id, distance, length, speed, acceleration);
84 Throw.whenNull(width, "Width may not be null.");
85 this.width = width;
86 this.facingSameDirection = facingSameDirection;
87 this.gtuType = gtuType;
88 this.desiredSpeed = desiredSpeed;
89 for (GTUStatus status : gtuStatus)
90 {
91 this.gtuStatus.add(status);
92 }
93 }
94
95 /**
96 * Construct a new Headway information object, for a non-moving GTU ahead of us or behind us.
97 * @param id String; the id of the GTU for comparison purposes, can not be null.
98 * @param gtuType GTUType; the perceived GTU Type, or null if unknown.
99 * @param distance Length; the distance to the other GTU; if this constructor is used, distance cannot be null.
100 * @param facingSameDirection whether the GTU is facing the same direction.
101 * @param length the (perceived) length of the other object; can not be null.
102 * @param width the (perceived) width of the other object; can not be null.
103 * @param desiredSpeed desired speed
104 * @param gtuStatus the observable characteristics of the GTU.
105 * @throws GTUException when id is null, or parameters are inconsistent
106 */
107 public AbstractHeadwayGTU(final String id, final GTUType gtuType, final Length distance, final boolean facingSameDirection,
108 final Length length, final Length width, final Speed desiredSpeed, final GTUStatus... gtuStatus) throws GTUException
109 {
110 super(ObjectType.GTU, id, distance, length);
111 Throw.whenNull(width, "Width may not be null.");
112 this.width = width;
113 this.facingSameDirection = facingSameDirection;
114 this.gtuType = gtuType;
115 this.desiredSpeed = desiredSpeed;
116 for (GTUStatus status : gtuStatus)
117 {
118 this.gtuStatus.add(status);
119 }
120 }
121
122 /**
123 * Construct a new Headway information object, for a moving GTU parallel with us.
124 * @param id the id of the GTU for comparison purposes, can not be null.
125 * @param gtuType the perceived GTU Type, or null if unknown.
126 * @param overlapFront the front-front distance to the other GTU; if this constructor is used, this value cannot be null.
127 * @param overlap the 'center' overlap with the other GTU; if this constructor is used, this value cannot be null.
128 * @param overlapRear the rear-rear distance to the other GTU; if this constructor is used, this value cannot be null.
129 * @param facingSameDirection whether the GTU is facing the same direction.
130 * @param length the (perceived) length of the other object; can not be null.
131 * @param width the (perceived) width of the other object; can not be null.
132 * @param speed the (perceived) speed of the other GTU; can be null if unknown.
133 * @param acceleration the (perceived) acceleration of the other GTU; can be null if unknown.
134 * @param desiredSpeed desired speed
135 * @param gtuStatus the observable characteristics of the GTU.
136 * @throws GTUException when id is null, or parameters are inconsistent
137 */
138 @SuppressWarnings("checkstyle:parameternumber")
139 public AbstractHeadwayGTU(final String id, final GTUType gtuType, final Length overlapFront, final Length overlap,
140 final Length overlapRear, final boolean facingSameDirection, final Length length, final Length width,
141 final Speed speed, final Acceleration acceleration, final Speed desiredSpeed, final GTUStatus... gtuStatus)
142 throws GTUException
143 {
144 super(ObjectType.GTU, id, overlapFront, overlap, overlapRear, length, speed, acceleration);
145 Throw.whenNull(width, "Width may not be null.");
146 this.width = width;
147 this.facingSameDirection = facingSameDirection;
148 this.gtuType = gtuType;
149 this.desiredSpeed = desiredSpeed;
150 for (GTUStatus status : gtuStatus)
151 {
152 this.gtuStatus.add(status);
153 }
154 }
155
156 /**
157 * Construct a new Headway information object, for a non-moving GTU parallel with us.
158 * @param id the id of the GTU for comparison purposes, can not be null.
159 * @param gtuType the perceived GTU Type, or null if unknown.
160 * @param overlapFront the front-front distance to the other GTU; if this constructor is used, this value cannot be null.
161 * @param overlap the 'center' overlap with the other GTU; if this constructor is used, this value cannot be null.
162 * @param overlapRear the rear-rear distance to the other GTU; if this constructor is used, this value cannot be null.
163 * @param facingSameDirection whether the GTU is facing the same direction.
164 * @param length the (perceived) length of the other object; can not be null.
165 * @param width the (perceived) width of the other object; can not be null.
166 * @param desiredSpeed desired speed
167 * @param gtuStatus the observable characteristics of the GTU.
168 * @throws GTUException when id is null, or parameters are inconsistent
169 */
170 @SuppressWarnings("checkstyle:parameternumber")
171 public AbstractHeadwayGTU(final String id, final GTUType gtuType, final Length overlapFront, final Length overlap,
172 final Length overlapRear, final boolean facingSameDirection, final Length length, final Length width,
173 final Speed desiredSpeed, final GTUStatus... gtuStatus) throws GTUException
174 {
175 super(ObjectType.GTU, id, overlapFront, overlap, overlapRear, length);
176 Throw.whenNull(width, "Width may not be null.");
177 this.width = width;
178 this.facingSameDirection = facingSameDirection;
179 this.gtuType = gtuType;
180 this.desiredSpeed = desiredSpeed;
181 for (GTUStatus status : gtuStatus)
182 {
183 this.gtuStatus.add(status);
184 }
185 }
186
187 /**
188 * @return gtuType
189 */
190 @Override
191 public final GTUType getGtuType()
192 {
193 return this.gtuType;
194 }
195
196 /** {@inheritDoc} */
197 @Override
198 public final Speed getDesiredSpeed()
199 {
200 return this.desiredSpeed;
201 }
202
203 /**
204 * @return facingSameDirection
205 */
206 @Override
207 public final boolean isFacingSameDirection()
208 {
209 return this.facingSameDirection;
210 }
211
212 /** @return were the braking lights on? */
213 @Override
214 public final boolean isBrakingLightsOn()
215 {
216 return this.gtuStatus.contains(GTUStatus.BRAKING_LIGHTS);
217 }
218
219 /** @return was the left turn indicator on? */
220 @Override
221 public final boolean isLeftTurnIndicatorOn()
222 {
223 return this.gtuStatus.contains(GTUStatus.LEFT_TURNINDICATOR);
224 }
225
226 /** @return was the right turn indicator on? */
227 @Override
228 public final boolean isRightTurnIndicatorOn()
229 {
230 return this.gtuStatus.contains(GTUStatus.RIGHT_TURNINDICATOR);
231 }
232
233 /** @return were the emergency lights on? */
234 @Override
235 public final boolean isEmergencyLightsOn()
236 {
237 return this.gtuStatus.contains(GTUStatus.EMERGENCY_LIGHTS);
238 }
239
240 /** @return was the vehicle honking or ringing its bell when being observed for the headway? */
241 @Override
242 public final boolean isHonking()
243 {
244 return this.gtuStatus.contains(GTUStatus.HONK);
245 }
246
247 /**
248 * For subclasses that create a copy of themselves.
249 * @return set of gtu status
250 */
251 protected final GTUStatus[] getGtuStatus()
252 {
253 return this.gtuStatus.toArray(new GTUStatus[this.gtuStatus.size()]);
254 }
255
256 /**
257 * Collects GTU statuses from a gtu.
258 * @param gtu LaneBasedGTU; gtu
259 * @param when Time; time
260 * @return GTU statuses
261 */
262 public static final GTUStatus[] getGTUStatuses(final LaneBasedGTU gtu, final Time when)
263 {
264 List<GTUStatus> statuses = new ArrayList<>();
265 if (gtu.isBrakingLightsOn(when))
266 {
267 statuses.add(GTUStatus.BRAKING_LIGHTS);
268 }
269 if (gtu.getTurnIndicatorStatus(when).isHazard())
270 {
271 statuses.add(GTUStatus.EMERGENCY_LIGHTS);
272 }
273 else if (gtu.getTurnIndicatorStatus(when).isLeft())
274 {
275 statuses.add(GTUStatus.LEFT_TURNINDICATOR);
276 }
277 else if (gtu.getTurnIndicatorStatus(when).isRight())
278 {
279 statuses.add(GTUStatus.RIGHT_TURNINDICATOR);
280 }
281 return statuses.toArray(new GTUStatus[statuses.size()]);
282 }
283
284 /**
285 * Creates speed limit info for given GTU.
286 * @param gtu gtu to the the speed limit info for
287 * @return speed limit info for given GTU
288 */
289 public static SpeedLimitInfo getSpeedLimitInfo(final LaneBasedGTU gtu)
290 {
291 SpeedLimitInfo sli = new SpeedLimitInfo();
292 sli.addSpeedInfo(SpeedLimitTypes.MAX_VEHICLE_SPEED, gtu.getMaximumSpeed());
293 try
294 {
295 sli.addSpeedInfo(SpeedLimitTypes.FIXED_SIGN, gtu.getReferencePosition().getLane().getSpeedLimit(gtu.getGTUType()));
296 }
297 catch (NetworkException | GTUException exception)
298 {
299 throw new RuntimeException("Could not obtain speed limit from lane for perception.", exception);
300 }
301 return sli;
302 }
303
304 /** {@inheritDoc} */
305 @Override
306 public Length getWidth()
307 {
308 return this.width;
309 }
310
311 /** {@inheritDoc} */
312 @Override
313 @SuppressWarnings("checkstyle:designforextension")
314 public String toString()
315 {
316 return "AbstractHeadwayGTU [gtuType=" + this.gtuType + ", gtuStatus=" + this.gtuStatus + ", getSpeed()="
317 + this.getSpeed() + ", getDistance()=" + this.getDistance() + ", getAcceleration()=" + this.getAcceleration()
318 + "]";
319 }
320
321 }