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