View Javadoc
1   package org.opentrafficsim.core.gtu;
2   
3   import java.io.Serializable;
4   
5   import org.djunits.value.vdouble.scalar.Acceleration;
6   import org.djunits.value.vdouble.scalar.Length;
7   import org.djunits.value.vdouble.scalar.Speed;
8   import org.djutils.exceptions.Throw;
9   import org.opentrafficsim.core.distributions.ConstantGenerator;
10  import org.opentrafficsim.core.distributions.Generator;
11  
12  /**
13   * Stores some of the information that is needed to generate a GTU.
14   * <p>
15   * Copyright (c) 2013-2024 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
16   * BSD-style license. See <a href="https://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
17   * <p>
18   * $LastChangedDate$, @version $Revision$, by $Author$, initial version Jul 8, 2014 <br>
19   * @author <a href="https://github.com/averbraeck">Alexander Verbraeck</a>
20   */
21  public class GtuTemplate implements Serializable, Generator<GtuCharacteristics>
22  {
23      /** */
24      private static final long serialVersionUID = 20141230L;
25  
26      /** The type of the GTU. */
27      private final GtuType gtuType;
28  
29      /** Generator for the length of the GTU. */
30      private final Generator<Length> lengthGenerator;
31  
32      /** Generator for the width of the GTU. */
33      private final Generator<Length> widthGenerator;
34  
35      /** Generator for the maximum speed of the GTU. */
36      private final Generator<Speed> maximumSpeedGenerator;
37  
38      /** Generator for the maximum acceleration of the GTU. */
39      private final Generator<Acceleration> maxAcceleration;
40  
41      /** Generator for the maximum deceleration of the GTU. */
42      private final Generator<Acceleration> maxDeceleration;
43  
44      /**
45       * @param gtuType GtuType, the GtuType to make it identifiable.
46       * @param lengthGenerator generator for the length of the GTU type (parallel with driving direction).
47       * @param widthGenerator generator for the width of the GTU type (perpendicular to driving direction).
48       * @param maximumSpeedGenerator generator for the maximum speed of the GTU type (in the driving direction).
49       * @throws NullPointerException when one of the arguments is null
50       */
51      public GtuTemplate(final GtuType gtuType, final Generator<Length> lengthGenerator, final Generator<Length> widthGenerator,
52              final Generator<Speed> maximumSpeedGenerator) throws NullPointerException
53      {
54          this(gtuType, lengthGenerator, widthGenerator, maximumSpeedGenerator,
55                  new ConstantGenerator<>(Acceleration.instantiateSI(3.0)),
56                  new ConstantGenerator<>(Acceleration.instantiateSI(-8.0)));
57      }
58  
59      /**
60       * @param gtuType GtuType, the GtuType to make it identifiable.
61       * @param lengthGenerator generator for the length of the GTU type (parallel with driving direction).
62       * @param widthGenerator generator for the width of the GTU type (perpendicular to driving direction).
63       * @param maximumSpeedGenerator generator for the maximum speed of the GTU type (in the driving direction).
64       * @param maximumAccelerationGenerator generator for the maximum acceleration of the GTU type
65       * @param maximumDecelerationGenerator generator for the maximum deceleration of the GTU type
66       * @throws NullPointerException when one of the arguments is null
67       */
68      public GtuTemplate(final GtuType gtuType, final Generator<Length> lengthGenerator, final Generator<Length> widthGenerator,
69              final Generator<Speed> maximumSpeedGenerator, final Generator<Acceleration> maximumAccelerationGenerator,
70              final Generator<Acceleration> maximumDecelerationGenerator) throws NullPointerException
71      {
72          Throw.whenNull(gtuType, "gtuType is null");
73          Throw.whenNull(lengthGenerator, "lengthGenerator is null");
74          Throw.whenNull(widthGenerator, "widthGenerator is null");
75          Throw.whenNull(maximumSpeedGenerator, "maximumSpeedGenerator is null");
76          Throw.whenNull(maximumAccelerationGenerator, "maximumAccelerationGenerator is null");
77          Throw.whenNull(maximumDecelerationGenerator, "maximumDecelerationGenerator is null");
78  
79          this.gtuType = gtuType;
80          this.lengthGenerator = lengthGenerator;
81          this.widthGenerator = widthGenerator;
82          this.maximumSpeedGenerator = maximumSpeedGenerator;
83          this.maxAcceleration = maximumAccelerationGenerator;
84          this.maxDeceleration = maximumDecelerationGenerator;
85      }
86  
87      /**
88       * Returns the same Characteristics, but pertaining to a different GTU type. This is useful for when the GTU type is used
89       * for other purposes in simulation, where the {@code GtuCharacteristics} should be the same.
90       * @param newGtuType the new GTU type.
91       * @return Copy of this {@code TemplateGTUType} linked to the new GTU type.
92       */
93      public GtuTemplate copyForGtuType(final GtuType newGtuType)
94      {
95          return new GtuTemplate(newGtuType, this.lengthGenerator, this.widthGenerator, this.maximumSpeedGenerator,
96                  this.maxAcceleration, this.maxDeceleration);
97      }
98  
99      /**
100      * Returns characteristics for the given GTU.
101      * @return characteristics for the given GTU
102      */
103     @Override
104     @SuppressWarnings("checkstyle:designforextension")
105     public GtuCharacteristics draw()
106     {
107         Acceleration acceleration = this.maxAcceleration.draw();
108         Acceleration deceleration = this.maxDeceleration.draw();
109         Throw.when(acceleration.si <= 0, IllegalArgumentException.class, "Acceleration should be above 0.");
110         Throw.when(deceleration.si >= 0, IllegalArgumentException.class, "Deceleration should be below 0.");
111         Length length = this.lengthGenerator.draw();
112         return new GtuCharacteristics(this.gtuType, length, this.widthGenerator.draw(), this.maximumSpeedGenerator.draw(),
113                 acceleration, deceleration, length.times(0.75));
114     }
115 
116     /**
117      * @return gtuType.
118      */
119     @SuppressWarnings("checkstyle:designforextension")
120     public GtuType getGtuType()
121     {
122         return this.gtuType;
123     }
124 
125     @Override
126     @SuppressWarnings("checkstyle:designforextension")
127     public String toString()
128     {
129         return String.format("TemplateGTUType [%s, %s, %s, %s]", this.gtuType, this.lengthGenerator, this.widthGenerator,
130                 this.maximumSpeedGenerator);
131     }
132 
133 }