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.djutils.exceptions.Throw;
12 import org.opentrafficsim.core.gtu.GTUException;
13 import org.opentrafficsim.core.gtu.GTUType;
14 import org.opentrafficsim.core.network.NetworkException;
15 import org.opentrafficsim.road.gtu.lane.LaneBasedGTU;
16 import org.opentrafficsim.road.network.speed.SpeedLimitInfo;
17 import org.opentrafficsim.road.network.speed.SpeedLimitTypes;
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43 public abstract class AbstractHeadwayGTU extends AbstractHeadwayCopy implements HeadwayGTU
44 {
45
46 private static final long serialVersionUID = 20160410L;
47
48
49 private final GTUType gtuType;
50
51
52 private final boolean facingSameDirection;
53
54
55 private final EnumSet<GTUStatus> gtuStatus = EnumSet.noneOf(GTUStatus.class);
56
57
58 private final Speed desiredSpeed;
59
60
61 private final Length width;
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77 @SuppressWarnings("checkstyle:parameternumber")
78 public AbstractHeadwayGTU(final String id, final GTUType gtuType, final Length distance, final boolean facingSameDirection,
79 final Length length, final Length width, final Speed speed, final Acceleration acceleration,
80 final Speed desiredSpeed, final GTUStatus... gtuStatus) throws GTUException
81 {
82 super(ObjectType.GTU, id, distance, length, speed, acceleration);
83 Throw.whenNull(width, "Width may not be null.");
84 this.width = width;
85 this.facingSameDirection = facingSameDirection;
86 this.gtuType = gtuType;
87 this.desiredSpeed = desiredSpeed;
88 for (GTUStatus status : gtuStatus)
89 {
90 this.gtuStatus.add(status);
91 }
92 }
93
94
95
96
97
98
99
100
101
102
103
104
105
106 public AbstractHeadwayGTU(final String id, final GTUType gtuType, final Length distance, final boolean facingSameDirection,
107 final Length length, final Length width, final Speed desiredSpeed, final GTUStatus... gtuStatus) throws GTUException
108 {
109 super(ObjectType.GTU, id, distance, length);
110 Throw.whenNull(width, "Width may not be null.");
111 this.width = width;
112 this.facingSameDirection = facingSameDirection;
113 this.gtuType = gtuType;
114 this.desiredSpeed = desiredSpeed;
115 for (GTUStatus status : gtuStatus)
116 {
117 this.gtuStatus.add(status);
118 }
119 }
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137 @SuppressWarnings("checkstyle:parameternumber")
138 public AbstractHeadwayGTU(final String id, final GTUType gtuType, final Length overlapFront, final Length overlap,
139 final Length overlapRear, final boolean facingSameDirection, final Length length, final Length width,
140 final Speed speed, final Acceleration acceleration, final Speed desiredSpeed, final GTUStatus... gtuStatus)
141 throws GTUException
142 {
143 super(ObjectType.GTU, id, overlapFront, overlap, overlapRear, length, speed, acceleration);
144 Throw.whenNull(width, "Width may not be null.");
145 this.width = width;
146 this.facingSameDirection = facingSameDirection;
147 this.gtuType = gtuType;
148 this.desiredSpeed = desiredSpeed;
149 for (GTUStatus status : gtuStatus)
150 {
151 this.gtuStatus.add(status);
152 }
153 }
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169 @SuppressWarnings("checkstyle:parameternumber")
170 public AbstractHeadwayGTU(final String id, final GTUType gtuType, final Length overlapFront, final Length overlap,
171 final Length overlapRear, final boolean facingSameDirection, final Length length, final Length width,
172 final Speed desiredSpeed, final GTUStatus... gtuStatus) throws GTUException
173 {
174 super(ObjectType.GTU, id, overlapFront, overlap, overlapRear, length);
175 Throw.whenNull(width, "Width may not be null.");
176 this.width = width;
177 this.facingSameDirection = facingSameDirection;
178 this.gtuType = gtuType;
179 this.desiredSpeed = desiredSpeed;
180 for (GTUStatus status : gtuStatus)
181 {
182 this.gtuStatus.add(status);
183 }
184 }
185
186
187
188
189 @Override
190 public final GTUType getGtuType()
191 {
192 return this.gtuType;
193 }
194
195
196 @Override
197 public final Speed getDesiredSpeed()
198 {
199 return this.desiredSpeed;
200 }
201
202
203
204
205 @Override
206 public final boolean isFacingSameDirection()
207 {
208 return this.facingSameDirection;
209 }
210
211
212 @Override
213 public final boolean isBrakingLightsOn()
214 {
215 return this.gtuStatus.contains(GTUStatus.BRAKING_LIGHTS);
216 }
217
218
219 @Override
220 public final boolean isLeftTurnIndicatorOn()
221 {
222 return this.gtuStatus.contains(GTUStatus.LEFT_TURNINDICATOR);
223 }
224
225
226 @Override
227 public final boolean isRightTurnIndicatorOn()
228 {
229 return this.gtuStatus.contains(GTUStatus.RIGHT_TURNINDICATOR);
230 }
231
232
233 @Override
234 public final boolean isEmergencyLightsOn()
235 {
236 return this.gtuStatus.contains(GTUStatus.EMERGENCY_LIGHTS);
237 }
238
239
240 @Override
241 public final boolean isHonking()
242 {
243 return this.gtuStatus.contains(GTUStatus.HONK);
244 }
245
246
247
248
249
250 protected final GTUStatus[] getGtuStatus()
251 {
252 return this.gtuStatus.toArray(new GTUStatus[this.gtuStatus.size()]);
253 }
254
255
256
257
258
259
260
261 public static final GTUStatus[] getGTUStatuses(final LaneBasedGTU gtu, final Time when)
262 {
263 List<GTUStatus> statuses = new ArrayList<>();
264 if (gtu.isBrakingLightsOn(when))
265 {
266 statuses.add(GTUStatus.BRAKING_LIGHTS);
267 }
268 if (gtu.getTurnIndicatorStatus(when).isHazard())
269 {
270 statuses.add(GTUStatus.EMERGENCY_LIGHTS);
271 }
272 else if (gtu.getTurnIndicatorStatus(when).isLeft())
273 {
274 statuses.add(GTUStatus.LEFT_TURNINDICATOR);
275 }
276 else if (gtu.getTurnIndicatorStatus(when).isRight())
277 {
278 statuses.add(GTUStatus.RIGHT_TURNINDICATOR);
279 }
280 return statuses.toArray(new GTUStatus[statuses.size()]);
281 }
282
283
284
285
286
287
288 public static SpeedLimitInfo getSpeedLimitInfo(final LaneBasedGTU gtu)
289 {
290 SpeedLimitInfo sli = new SpeedLimitInfo();
291 sli.addSpeedInfo(SpeedLimitTypes.MAX_VEHICLE_SPEED, gtu.getMaximumSpeed());
292 try
293 {
294 sli.addSpeedInfo(SpeedLimitTypes.FIXED_SIGN, gtu.getReferencePosition().getLane().getSpeedLimit(gtu.getGTUType()));
295 }
296 catch (NetworkException | GTUException exception)
297 {
298 throw new RuntimeException("Could not obtain speed limit from lane for perception.", exception);
299 }
300 return sli;
301 }
302
303
304 @Override
305 public Length getWidth()
306 {
307 return this.width;
308 }
309
310
311 @Override
312 @SuppressWarnings("checkstyle:designforextension")
313 public String toString()
314 {
315 return "AbstractHeadwayGTU [gtuType=" + this.gtuType + ", gtuStatus=" + this.gtuStatus + ", getSpeed()="
316 + this.getSpeed() + ", getDistance()=" + this.getDistance() + ", getAcceleration()=" + this.getAcceleration()
317 + "]";
318 }
319
320 }