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 @Override
193 public final Speed getDesiredSpeed()
194 {
195 return this.desiredSpeed;
196 }
197
198
199
200
201 @Override
202 public final boolean isFacingSameDirection()
203 {
204 return this.facingSameDirection;
205 }
206
207
208 @Override
209 public final boolean isBrakingLightsOn()
210 {
211 return this.gtuStatus.contains(GtuStatus.BRAKING_LIGHTS);
212 }
213
214
215 @Override
216 public final boolean isLeftTurnIndicatorOn()
217 {
218 return this.gtuStatus.contains(GtuStatus.LEFT_TURNINDICATOR);
219 }
220
221
222 @Override
223 public final boolean isRightTurnIndicatorOn()
224 {
225 return this.gtuStatus.contains(GtuStatus.RIGHT_TURNINDICATOR);
226 }
227
228
229 @Override
230 public final boolean isEmergencyLightsOn()
231 {
232 return this.gtuStatus.contains(GtuStatus.EMERGENCY_LIGHTS);
233 }
234
235
236 @Override
237 public final boolean isHonking()
238 {
239 return this.gtuStatus.contains(GtuStatus.HONK);
240 }
241
242
243
244
245
246 protected final GtuStatus[] getGtuStatus()
247 {
248 return this.gtuStatus.toArray(new GtuStatus[this.gtuStatus.size()]);
249 }
250
251
252
253
254
255
256
257 public static final GtuStatus[] getGtuStatuses(final LaneBasedGtu gtu, final Time when)
258 {
259 List<GtuStatus> statuses = new ArrayList<>();
260 if (gtu.isBrakingLightsOn(when))
261 {
262 statuses.add(GtuStatus.BRAKING_LIGHTS);
263 }
264 if (gtu.getTurnIndicatorStatus(when).isHazard())
265 {
266 statuses.add(GtuStatus.EMERGENCY_LIGHTS);
267 }
268 else if (gtu.getTurnIndicatorStatus(when).isLeft())
269 {
270 statuses.add(GtuStatus.LEFT_TURNINDICATOR);
271 }
272 else if (gtu.getTurnIndicatorStatus(when).isRight())
273 {
274 statuses.add(GtuStatus.RIGHT_TURNINDICATOR);
275 }
276 return statuses.toArray(new GtuStatus[statuses.size()]);
277 }
278
279
280
281
282
283
284 public static SpeedLimitInfo getSpeedLimitInfo(final LaneBasedGtu gtu)
285 {
286 SpeedLimitInfo sli = new SpeedLimitInfo();
287 sli.addSpeedInfo(SpeedLimitTypes.MAX_VEHICLE_SPEED, gtu.getMaximumSpeed());
288 try
289 {
290 sli.addSpeedInfo(SpeedLimitTypes.FIXED_SIGN, gtu.getReferencePosition().lane().getSpeedLimit(gtu.getType()));
291 }
292 catch (NetworkException | GtuException exception)
293 {
294 throw new RuntimeException("Could not obtain speed limit from lane for perception.", exception);
295 }
296 return sli;
297 }
298
299 @Override
300 public Length getWidth()
301 {
302 return this.width;
303 }
304
305 @Override
306 @SuppressWarnings("checkstyle:designforextension")
307 public String toString()
308 {
309 return "AbstractHeadwayGtu [gtuType=" + this.gtuType + ", gtuStatus=" + this.gtuStatus + ", getSpeed()="
310 + this.getSpeed() + ", getDistance()=" + this.getDistance() + ", getAcceleration()=" + this.getAcceleration()
311 + "]";
312 }
313
314 }