1 package org.opentrafficsim.road.gtu.lane;
2
3 import java.io.Serializable;
4 import java.lang.reflect.Constructor;
5 import java.lang.reflect.InvocationTargetException;
6 import java.util.HashSet;
7 import java.util.LinkedHashMap;
8 import java.util.Map;
9 import java.util.Set;
10
11 import javax.naming.NamingException;
12
13 import org.djunits.unit.AccelerationUnit;
14 import org.djunits.unit.LengthUnit;
15 import org.djunits.value.vdouble.scalar.Acceleration;
16 import org.djunits.value.vdouble.scalar.Length;
17 import org.djunits.value.vdouble.scalar.Speed;
18 import org.opentrafficsim.core.dsol.OTSAnimatorInterface;
19 import org.opentrafficsim.core.dsol.OTSDEVSSimulatorInterface;
20 import org.opentrafficsim.core.geometry.OTSGeometryException;
21 import org.opentrafficsim.core.gtu.GTUException;
22 import org.opentrafficsim.core.gtu.GTUType;
23 import org.opentrafficsim.core.gtu.RelativePosition;
24 import org.opentrafficsim.core.gtu.RelativePosition.TYPE;
25 import org.opentrafficsim.core.gtu.animation.GTUColorer;
26 import org.opentrafficsim.core.network.NetworkException;
27 import org.opentrafficsim.core.network.OTSNetwork;
28 import org.opentrafficsim.core.network.route.Route;
29 import org.opentrafficsim.road.gtu.strategical.LaneBasedStrategicalPlanner;
30 import org.opentrafficsim.road.gtu.strategical.LaneBasedStrategicalPlannerFactory;
31 import org.opentrafficsim.road.network.lane.DirectedLanePosition;
32
33 import nl.tudelft.simulation.dsol.SimRuntimeException;
34 import nl.tudelft.simulation.dsol.animation.D2.Renderable2D;
35 import nl.tudelft.simulation.immutablecollections.Immutable;
36 import nl.tudelft.simulation.immutablecollections.ImmutableHashSet;
37 import nl.tudelft.simulation.immutablecollections.ImmutableLinkedHashMap;
38 import nl.tudelft.simulation.immutablecollections.ImmutableMap;
39 import nl.tudelft.simulation.immutablecollections.ImmutableSet;
40 import nl.tudelft.simulation.language.reflection.ClassUtil;
41
42
43
44
45
46
47
48
49
50
51
52
53 public class LaneBasedIndividualGTU extends AbstractLaneBasedIndividualGTU
54 {
55
56 private static final long serialVersionUID = 20141025L;
57
58
59 private Renderable2D animation;
60
61
62 private final Map<RelativePosition.TYPE, RelativePosition> relativePositions = new LinkedHashMap<>(4);
63
64
65 private final Set<RelativePosition> contourPoints = new HashSet<>(4);
66
67
68
69
70
71
72
73
74
75
76
77
78
79 @SuppressWarnings("checkstyle:parameternumber")
80 public LaneBasedIndividualGTU(final String id, final GTUType gtuType, final Length length, final Length width,
81 final Speed maximumSpeed, final OTSDEVSSimulatorInterface simulator, final OTSNetwork network)
82 throws NamingException, GTUException
83 {
84 super(id, gtuType, length, width, maximumSpeed, simulator, network);
85
86
87
88
89 Length dx2 = new Length(getLength().getSI() / 2.0, LengthUnit.METER);
90 Length dy2 = new Length(getWidth().getSI() / 2.0, LengthUnit.METER);
91 this.relativePositions.put(RelativePosition.FRONT,
92 new RelativePosition(dx2, Length.ZERO, Length.ZERO, RelativePosition.FRONT));
93 this.relativePositions.put(RelativePosition.REAR,
94 new RelativePosition(dx2.neg(), Length.ZERO, Length.ZERO, RelativePosition.REAR));
95 this.relativePositions.put(RelativePosition.REFERENCE, RelativePosition.REFERENCE_POSITION);
96 this.relativePositions.put(RelativePosition.CENTER,
97 new RelativePosition(Length.ZERO, Length.ZERO, Length.ZERO, RelativePosition.CENTER));
98
99
100 for (int i = -1; i <= 1; i += 2)
101 {
102 for (int j = -1; j <= 1; j += 2)
103 {
104 this.contourPoints
105 .add(new RelativePosition(dx2.multiplyBy(i), dy2.multiplyBy(j), Length.ZERO, RelativePosition.CONTOUR));
106 }
107 }
108
109 setMaximumAcceleration(new Acceleration(1.8, AccelerationUnit.METER_PER_SECOND_2));
110 setMaximumDeceleration(new Acceleration(-3.5, AccelerationUnit.METER_PER_SECOND_2));
111 }
112
113
114
115
116
117
118
119
120
121
122
123
124
125 public final void initWithAnimation(final LaneBasedStrategicalPlanner strategicalPlanner,
126 final Set<DirectedLanePosition> initialLongitudinalPositions, final Speed initialSpeed,
127 final Class<? extends Renderable2D> animationClass, final GTUColorer gtuColorer)
128 throws NetworkException, SimRuntimeException, GTUException, OTSGeometryException
129 {
130 super.init(strategicalPlanner, initialLongitudinalPositions, initialSpeed);
131
132
133 if (getSimulator() instanceof OTSAnimatorInterface && animationClass != null)
134 {
135 try
136 {
137 Constructor<?> constructor;
138
139 if (null == gtuColorer)
140 {
141 constructor = ClassUtil.resolveConstructor(animationClass, new Object[] { this, getSimulator() });
142 this.animation = (Renderable2D) constructor.newInstance(this, getSimulator());
143 }
144 else
145 {
146 constructor =
147 ClassUtil.resolveConstructor(animationClass, new Object[] { this, getSimulator(), gtuColorer });
148 this.animation = (Renderable2D) constructor.newInstance(this, getSimulator(), gtuColorer);
149 }
150 }
151 catch (InstantiationException | IllegalAccessException | NoSuchMethodException | SecurityException
152 | IllegalArgumentException | InvocationTargetException exception)
153 {
154 throw new GTUException("Could not instantiate car animation of type " + animationClass.getName(), exception);
155 }
156 }
157 }
158
159
160 @Override
161 public final RelativePosition getFront()
162 {
163 return this.relativePositions.get(RelativePosition.FRONT);
164 }
165
166
167 @Override
168 public final RelativePosition getRear()
169 {
170 return this.relativePositions.get(RelativePosition.REAR);
171 }
172
173
174 @Override
175 public final RelativePosition getCenter()
176 {
177 return this.relativePositions.get(RelativePosition.CENTER);
178 }
179
180
181 @Override
182 public final ImmutableMap<TYPE, RelativePosition> getRelativePositions()
183 {
184 return new ImmutableLinkedHashMap<>(this.relativePositions, Immutable.WRAP);
185 }
186
187
188 @Override
189 public final ImmutableSet<RelativePosition> getContourPoints()
190 {
191 return new ImmutableHashSet<>(this.contourPoints, Immutable.WRAP);
192 }
193
194
195 @Override
196 public final void destroy()
197 {
198 if (this.animation != null)
199 {
200 try
201 {
202 this.animation.destroy();
203 this.animation = null;
204 }
205 catch (Exception e)
206 {
207 System.err.println("Error when destroying the animation of car: " + this.getId());
208 }
209 }
210 super.destroy();
211 }
212
213
214 @Override
215 public final String toString()
216 {
217 return "LaneBasedIndividualGTU [id=" + getId() + "]";
218 }
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245 @SuppressWarnings("checkstyle:hiddenfield")
246 public static class LaneBasedIndividualCarBuilder implements Serializable
247 {
248
249 private static final long serialVersionUID = 20160000L;
250
251
252 private String id = null;
253
254
255 private GTUType gtuType = null;
256
257
258 private Set<DirectedLanePosition> initialLongitudinalPositions = null;
259
260
261 private Speed initialSpeed = null;
262
263
264 private Length length = null;
265
266
267 private Length width = null;
268
269
270 private Speed maximumSpeed = null;
271
272
273 private OTSDEVSSimulatorInterface simulator = null;
274
275
276 private Class<? extends Renderable2D> animationClass = null;
277
278
279 private GTUColorer gtuColorer = null;
280
281
282 private OTSNetwork network = null;
283
284
285
286
287
288 public final LaneBasedIndividualCarBuilder setId(final String id)
289 {
290 this.id = id;
291 return this;
292 }
293
294
295
296
297
298 public final LaneBasedIndividualCarBuilder setGtuType(final GTUType gtuType)
299 {
300 this.gtuType = gtuType;
301 return this;
302 }
303
304
305
306
307
308 public final LaneBasedIndividualCarBuilder setInitialLongitudinalPositions(
309 final Set<DirectedLanePosition> initialLongitudinalPositions)
310 {
311 this.initialLongitudinalPositions = initialLongitudinalPositions;
312 return this;
313 }
314
315
316
317
318
319 public final LaneBasedIndividualCarBuilder setInitialSpeed(final Speed initialSpeed)
320 {
321 this.initialSpeed = initialSpeed;
322 return this;
323 }
324
325
326
327
328
329 public final LaneBasedIndividualCarBuilder setLength(final Length length)
330 {
331 this.length = length;
332 return this;
333 }
334
335
336
337
338
339 public final LaneBasedIndividualCarBuilder setWidth(final Length width)
340 {
341 this.width = width;
342 return this;
343 }
344
345
346
347
348
349 public final LaneBasedIndividualCarBuilder setMaximumSpeed(final Speed maximumSpeed)
350 {
351 this.maximumSpeed = maximumSpeed;
352 return this;
353 }
354
355
356
357
358
359 public final LaneBasedIndividualCarBuilder setSimulator(final OTSDEVSSimulatorInterface simulator)
360 {
361 this.simulator = simulator;
362 return this;
363 }
364
365
366
367
368
369 public final LaneBasedIndividualCarBuilder setAnimationClass(final Class<? extends Renderable2D> animationClass)
370 {
371 this.animationClass = animationClass;
372 return this;
373 }
374
375
376
377
378
379 public final LaneBasedIndividualCarBuilder setGtuColorer(final GTUColorer gtuColorer)
380 {
381 this.gtuColorer = gtuColorer;
382 return this;
383 }
384
385
386
387
388
389 public final LaneBasedIndividualCarBuilder setNetwork(final OTSNetwork network)
390 {
391 this.network = network;
392 return this;
393 }
394
395
396
397
398 public final String getId()
399 {
400 return this.id;
401 }
402
403
404
405
406 public final GTUType getGtuType()
407 {
408 return this.gtuType;
409 }
410
411
412
413
414 public final Set<DirectedLanePosition> getInitialLongitudinalPositions()
415 {
416 return this.initialLongitudinalPositions;
417 }
418
419
420
421
422 public final Speed getInitialSpeed()
423 {
424 return this.initialSpeed;
425 }
426
427
428
429
430 public final Length getLength()
431 {
432 return this.length;
433 }
434
435
436
437
438 public final Length getWidth()
439 {
440 return this.width;
441 }
442
443
444
445
446 public final Speed getMaximumSpeed()
447 {
448 return this.maximumSpeed;
449 }
450
451
452
453
454 public final OTSDEVSSimulatorInterface getSimulator()
455 {
456 return this.simulator;
457 }
458
459
460
461
462 public final Class<? extends Renderable2D> getAnimationClass()
463 {
464 return this.animationClass;
465 }
466
467
468
469
470 public final GTUColorer getGtuColorer()
471 {
472 return this.gtuColorer;
473 }
474
475
476
477
478 public final OTSNetwork getNetwork()
479 {
480 return this.network;
481 }
482
483
484
485
486
487
488
489
490 public final LaneBasedIndividualGTU build(
491 final LaneBasedStrategicalPlannerFactory<? extends LaneBasedStrategicalPlanner> laneBasedStrategicalPlannerFactory,
492 final Route route) throws Exception
493 {
494 if (null == this.id || null == this.gtuType || null == this.initialLongitudinalPositions
495 || null == this.initialSpeed || null == this.length || null == this.width || null == this.maximumSpeed
496 || null == this.simulator || null == this.network)
497 {
498
499 throw new GTUException("factory settings incomplete");
500 }
501 LaneBasedIndividualGTU gtu = new LaneBasedIndividualGTU(this.id, this.gtuType, this.length, this.width,
502 this.maximumSpeed, this.simulator, this.network);
503 gtu.initWithAnimation(laneBasedStrategicalPlannerFactory.create(gtu, route), this.initialLongitudinalPositions,
504 this.initialSpeed, this.animationClass, this.gtuColorer);
505 return gtu;
506
507 }
508
509
510 @Override
511 public final String toString()
512 {
513 return "LaneBasedIndividualCarBuilder [id=" + this.id + ", gtuType=" + this.gtuType
514 + ", initialLongitudinalPositions=" + this.initialLongitudinalPositions + ", initialSpeed="
515 + this.initialSpeed + ", length=" + this.length + ", width=" + this.width + ", maximumSpeed="
516 + this.maximumSpeed + ", strategicalPlanner=" + "]";
517 }
518
519 }
520 }