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