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
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44 public abstract class AbstractHeadwayGTU extends AbstractHeadwayCopy implements HeadwayGTU
45 {
46
47 private static final long serialVersionUID = 20160410L;
48
49
50 private final GTUType gtuType;
51
52
53 private final boolean facingSameDirection;
54
55
56 private final EnumSet<GTUStatus> gtuStatus = EnumSet.noneOf(GTUStatus.class);
57
58
59 private final Speed desiredSpeed;
60
61
62 private final Length width;
63
64
65
66
67
68
69
70
71
72
73
74
75
76
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
97
98
99
100
101
102
103
104
105
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
124
125
126
127
128
129
130
131
132
133
134
135
136
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
158
159
160
161
162
163
164
165
166
167
168
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
189
190 @Override
191 public final GTUType getGtuType()
192 {
193 return this.gtuType;
194 }
195
196
197 @Override
198 public final Speed getDesiredSpeed()
199 {
200 return this.desiredSpeed;
201 }
202
203
204
205
206 @Override
207 public final boolean isFacingSameDirection()
208 {
209 return this.facingSameDirection;
210 }
211
212
213 @Override
214 public final boolean isBrakingLightsOn()
215 {
216 return this.gtuStatus.contains(GTUStatus.BRAKING_LIGHTS);
217 }
218
219
220 @Override
221 public final boolean isLeftTurnIndicatorOn()
222 {
223 return this.gtuStatus.contains(GTUStatus.LEFT_TURNINDICATOR);
224 }
225
226
227 @Override
228 public final boolean isRightTurnIndicatorOn()
229 {
230 return this.gtuStatus.contains(GTUStatus.RIGHT_TURNINDICATOR);
231 }
232
233
234 @Override
235 public final boolean isEmergencyLightsOn()
236 {
237 return this.gtuStatus.contains(GTUStatus.EMERGENCY_LIGHTS);
238 }
239
240
241 @Override
242 public final boolean isHonking()
243 {
244 return this.gtuStatus.contains(GTUStatus.HONK);
245 }
246
247
248
249
250
251 protected final GTUStatus[] getGtuStatus()
252 {
253 return this.gtuStatus.toArray(new GTUStatus[this.gtuStatus.size()]);
254 }
255
256
257
258
259
260
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
286
287
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
305 @Override
306 public Length getWidth()
307 {
308 return this.width;
309 }
310
311
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 }