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