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