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