View Javadoc
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   * Augments the AbstractLaneBasedIndividualGTU with a LaneBasedIndividualCarBuilder and animation support
31   * <p>
32   * Copyright (c) 2013-2022 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
33   * BSD-style license. See <a href="http://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
34   * <p>
35   * @version $Revision: 1401 $, $LastChangedDate: 2015-09-14 01:33:02 +0200 (Mon, 14 Sep 2015) $, by $Author: averbraeck $,
36   *          initial version Oct 22, 2014 <br>
37   * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
38   * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
39   */
40  public class LaneBasedIndividualGTU extends AbstractLaneBasedIndividualGTU
41  {
42      /** */
43      private static final long serialVersionUID = 20141025L;
44  
45      /** Sensing positions. */
46      private final Map<RelativePosition.TYPE, RelativePosition> relativePositions = new LinkedHashMap<>();
47  
48      /** cached front. */
49      private final RelativePosition frontPos;
50  
51      /** cached rear. */
52      private final RelativePosition rearPos;
53  
54      /** contour points. */
55      private final Set<RelativePosition> contourPoints = new LinkedHashSet<>();
56  
57      /**
58       * Construct a new LaneBasedIndividualGTU.
59       * @param id String; the id of the GTU
60       * @param gtuType GTUType; the type of GTU, e.g. TruckType, CarType, BusType
61       * @param length Length; the maximum length of the GTU (parallel with driving direction)
62       * @param width Length; the maximum width of the GTU (perpendicular to driving direction)
63       * @param maximumSpeed Speed;the maximum speed of the GTU (in the driving direction)
64       * @param front Length; front distance relative to the reference position
65       * @param simulator OTSSimulatorInterface; the simulator
66       * @param network OTSRoadNetwork; the network that the GTU is initially registered in
67       * @throws GTUException when a parameter is invalid
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       * Construct a new LaneBasedIndividualGTU.
79       * @param id String; the id of the GTU
80       * @param gtuType GTUType; the type of GTU, e.g. TruckType, CarType, BusType
81       * @param length Length; the maximum length of the GTU (parallel with driving direction)
82       * @param width Length; the maximum width of the GTU (perpendicular to driving direction)
83       * @param maximumSpeed Speed;the maximum speed of the GTU (in the driving direction)
84       * @param front Length; front distance relative to the reference position
85       * @param centerOfGravity Length; distance from the center of gravity to the reference position
86       * @param simulator OTSSimulatorInterface; the simulator
87       * @param network OTSRoadNetwork; the network that the GTU is initially registered in
88       * @throws GTUException when a parameter is invalid
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          // sensor positions.
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         // Contour positions. For now, a rectangle with the four corners.
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     /** {@inheritDoc} */
121     @Override
122     public final RelativePosition getFront()
123     {
124         return this.frontPos;
125     }
126 
127     /** {@inheritDoc} */
128     @Override
129     public final RelativePosition getRear()
130     {
131         return this.rearPos;
132     }
133 
134     /** {@inheritDoc} */
135     @Override
136     public final RelativePosition getCenter()
137     {
138         return this.relativePositions.get(RelativePosition.CENTER);
139     }
140 
141     /** {@inheritDoc} */
142     @Override
143     public final ImmutableMap<TYPE, RelativePosition> getRelativePositions()
144     {
145         return new ImmutableLinkedHashMap<>(this.relativePositions, Immutable.WRAP);
146     }
147 
148     /** {@inheritDoc} */
149     @Override
150     public final ImmutableSet<RelativePosition> getContourPoints()
151     {
152         return new ImmutableHashSet<>(this.contourPoints, Immutable.WRAP);
153     }
154 
155     /** {@inheritDoc} */
156     @Override
157     public final String toString()
158     {
159         return "LaneBasedIndividualGTU [id=" + getId() + "]";
160     }
161 
162     /**
163      * Build an individual car and use easy setter methods to instantiate the car. Typical use looks like:
164      * 
165      * <pre>
166      * LaneBasedIndividualCar&lt;String&gt; car = new LaneBasedIndividualCarBuilder&lt;String&gt;().setId("Car:"+nr)
167      *    .setLength(new Length(4.0, METER))....build(); 
168      *    
169      * or
170      * 
171      * LaneBasedIndividualCarBuilder&lt;String&gt; carBuilder = new LaneBasedIndividualCarBuilder&lt;String&gt;();
172      * carBuilder.setId("Car:"+nr);
173      * carBuilder.setLength(new Length(4.0, METER));
174      * carBuilder.setWidth(new Length(1.8, METER));
175      * ...
176      * LaneBasedIndividualCar&lt;String&gt; car = carBuilder.build();
177      * </pre>
178      * <p>
179      * Copyright (c) 2013-2022 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. <br>
180      * All rights reserved. <br>
181      * BSD-style license. See <a href="http://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
182      * <p>
183      * @version $Revision: 1401 $, $LastChangedDate: 2015-09-14 01:33:02 +0200 (Mon, 14 Sep 2015) $, by $Author: averbraeck $,
184      *          initial Feb 3, 2015 <br>
185      * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
186      */
187     @SuppressWarnings("checkstyle:hiddenfield")
188     public static class LaneBasedIndividualCarBuilder implements Serializable
189     {
190         /** */
191         private static final long serialVersionUID = 20160000L;
192 
193         /** The id of the GTU. */
194         private String id = null;
195 
196         /** The type of GTU, e.g. TruckType, CarType, BusType. */
197         private GTUType gtuType = null;
198 
199         /** The initial positions of the car on one or more lanes. */
200         private Set<DirectedLanePosition> initialLongitudinalPositions = null;
201 
202         /** The initial speed of the car on the lane. */
203         private Speed initialSpeed = null;
204 
205         /** The length of the GTU (parallel with driving direction). */
206         private Length length = null;
207 
208         /** The width of the GTU (perpendicular to driving direction). */
209         private Length width = null;
210 
211         /** The maximum speed of the GTU (in the driving direction). */
212         private Speed maximumSpeed = null;
213 
214         /** Maximum acceleration. */
215         private Acceleration maximumAcceleration = null;
216 
217         /** Maximum deceleration (a negative value). */
218         private Acceleration maximumDeceleration = null;
219 
220         /** The distance of the front relative to the reference position. */
221         private Length front = null;
222 
223         /** The simulator. */
224         private OTSSimulatorInterface simulator = null;
225 
226         /** Network. */
227         private OTSRoadNetwork network = null;
228 
229         /**
230          * @param id String; set id
231          * @return the class itself for chaining the setters
232          */
233         public final LaneBasedIndividualCarBuilder setId(final String id)
234         {
235             this.id = id;
236             return this;
237         }
238 
239         /**
240          * @param gtuType GTUType; set gtuType
241          * @return the class itself for chaining the setters
242          */
243         public final LaneBasedIndividualCarBuilder setGtuType(final GTUType gtuType)
244         {
245             this.gtuType = gtuType;
246             return this;
247         }
248 
249         /**
250          * @param initialLongitudinalPositions Set&lt;DirectedLanePosition&gt;; set initialLongitudinalPositions
251          * @return the class itself for chaining the setters
252          */
253         public final LaneBasedIndividualCarBuilder setInitialLongitudinalPositions(
254                 final Set<DirectedLanePosition> initialLongitudinalPositions)
255         {
256             this.initialLongitudinalPositions = initialLongitudinalPositions;
257             return this;
258         }
259 
260         /**
261          * @param initialSpeed Speed; set initialSpeed
262          * @return the class itself for chaining the setters
263          */
264         public final LaneBasedIndividualCarBuilder setInitialSpeed(final Speed initialSpeed)
265         {
266             this.initialSpeed = initialSpeed;
267             return this;
268         }
269 
270         /**
271          * @param length Length; set length
272          * @return the class itself for chaining the setters
273          */
274         public final LaneBasedIndividualCarBuilder setLength(final Length length)
275         {
276             this.length = length;
277             return this;
278         }
279 
280         /**
281          * @param width Length; set width
282          * @return the class itself for chaining the setters
283          */
284         public final LaneBasedIndividualCarBuilder setWidth(final Length width)
285         {
286             this.width = width;
287             return this;
288         }
289 
290         /**
291          * @param maximumSpeed Speed; set maximumSpeed
292          * @return the class itself for chaining the setters
293          */
294         public final LaneBasedIndividualCarBuilder setMaximumSpeed(final Speed maximumSpeed)
295         {
296             this.maximumSpeed = maximumSpeed;
297             return this;
298         }
299 
300         /**
301          * @param maximumAcceleration Acceleration; maximum acceleration
302          * @return the class itself for chaining the setters
303          */
304         public final LaneBasedIndividualCarBuilder setMaximumAcceleration(final Acceleration maximumAcceleration)
305         {
306             this.maximumAcceleration = maximumAcceleration;
307             return this;
308         }
309 
310         /**
311          * @param maximumDeceleration Acceleration; maximum deceleration (a negative value)
312          * @return the class itself for chaining the setters
313          */
314         public final LaneBasedIndividualCarBuilder setMaximumDeceleration(final Acceleration maximumDeceleration)
315         {
316             this.maximumDeceleration = maximumDeceleration;
317             return this;
318         }
319 
320         /**
321          * @param simulator OTSSimulatorInterface; set simulator
322          * @return the class itself for chaining the setters
323          */
324         public final LaneBasedIndividualCarBuilder setSimulator(final OTSSimulatorInterface simulator)
325         {
326             this.simulator = simulator;
327             return this;
328         }
329 
330         /**
331          * @param front Length; distance of the front relative to the reference point
332          * @return the class itself for chaining the setters
333          */
334         public final LaneBasedIndividualCarBuilder setFront(final Length front)
335         {
336             this.front = front;
337             return this;
338         }
339 
340         /**
341          * @param network OTSRoadNetwork; set network
342          * @return the class itself for chaining the setters
343          */
344         public final LaneBasedIndividualCarBuilder setNetwork(final OTSRoadNetwork network)
345         {
346             this.network = network;
347             return this;
348         }
349 
350         /**
351          * @return id.
352          */
353         public final String getId()
354         {
355             return this.id;
356         }
357 
358         /**
359          * @return gtuType.
360          */
361         public final GTUType getGtuType()
362         {
363             return this.gtuType;
364         }
365 
366         /**
367          * @return initialLongitudinalPositions.
368          */
369         public final Set<DirectedLanePosition> getInitialLongitudinalPositions()
370         {
371             return this.initialLongitudinalPositions;
372         }
373 
374         /**
375          * @return initialSpeed.
376          */
377         public final Speed getInitialSpeed()
378         {
379             return this.initialSpeed;
380         }
381 
382         /**
383          * @return length.
384          */
385         public final Length getLength()
386         {
387             return this.length;
388         }
389 
390         /**
391          * @return width.
392          */
393         public final Length getWidth()
394         {
395             return this.width;
396         }
397 
398         /**
399          * @return maximumSpeed.
400          */
401         public final Speed getMaximumSpeed()
402         {
403             return this.maximumSpeed;
404         }
405 
406         /**
407          * @return simulator.
408          */
409         public final OTSSimulatorInterface getSimulator()
410         {
411             return this.simulator;
412         }
413 
414         /**
415          * @return network
416          */
417         public final OTSRoadNetwork getNetwork()
418         {
419             return this.network;
420         }
421 
422         /**
423          * Build one LaneBasedIndividualCar.
424          * @param laneBasedStrategicalPlannerFactory LaneBasedStrategicalPlannerFactory&lt;? extends
425          *            LaneBasedStrategicalPlanner&gt;; LaneBasedStrategicalPlannerFactory&lt;? extends
426          *            LaneBasedStrategicalPlanner&gt;; LaneBasedStrategicalPlannerFactory&lt;? extends
427          *            LaneBasedStrategicalPlanner&gt;; LaneBasedStrategicalPlannerFactory&lt;? extends
428          *            LaneBasedStrategicalPlanner&gt;; LaneBasedStrategicalPlannerFactory&lt;? extends
429          *            LaneBasedStrategicalPlanner&gt;; factory for the strategical planner
430          * @param route Route; route
431          * @param origin Node; origin
432          * @param destination Node; destination
433          * @return the built Car with the set properties
434          * @throws Exception when not all required values have been set
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                 // TODO Should throw a more specific Exception type
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         /** {@inheritDoc} */
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 }