View Javadoc
1   package org.opentrafficsim.road.gtu.lane.perception.headway;
2   
3   import java.util.ArrayList;
4   import java.util.EnumSet;
5   import java.util.List;
6   
7   import org.djunits.value.vdouble.scalar.Acceleration;
8   import org.djunits.value.vdouble.scalar.Length;
9   import org.djunits.value.vdouble.scalar.Speed;
10  import org.djunits.value.vdouble.scalar.Time;
11  import org.djutils.exceptions.Throw;
12  import org.opentrafficsim.core.gtu.GTUException;
13  import org.opentrafficsim.core.gtu.GTUType;
14  import org.opentrafficsim.core.network.NetworkException;
15  import org.opentrafficsim.road.gtu.lane.LaneBasedGTU;
16  import org.opentrafficsim.road.network.speed.SpeedLimitInfo;
17  import org.opentrafficsim.road.network.speed.SpeedLimitTypes;
18  
19  /**
20   * Container for a reference to information about a (lane based) GTU and a headway. The Headway can store information about GTUs
21   * or objects ahead of the reference GTU, behind the reference GTU, or (partially) parallel to the reference GTU. In addition to
22   * the (perceived) headway, several other pieces of information can be stored, such as (perceived) speed, (perceived)
23   * acceleration, (perceived) turn indicators, and (perceived) braking lights. <br>
24   * Special care must be taken in curves when perceiving headway of a GTU or object on an adjacent lane.The question is whether
25   * we perceive the parallel or ahead/behind based on a line perpendicular to the front/back of the GTU (rectangular), or
26   * perpendicular to the center line of the lane (wedge-shaped in case of a curve). The difficulty of a wedge-shaped situation is
27   * that reciprocity might be violated: in case of a clothoid, for instance, it is not sure that the point on the center line
28   * when projected from lane 1 to lane 2 is the same as the projection from lane 2 to lane 1. The same holds for shapes with
29   * sharp bends. Therefore, algorithms implementing headway should only project the <i>reference point</i> of the reference GTU
30   * on the center line of the adjacent lane, and then calculate the forward position and backward position on the adjacent lane
31   * based on the reference point. Still, our human perception of what is parallel and what not, is not reflected by fractional
32   * positions. See examples in
33   * <a href= "http://simulation.tudelft.nl:8085/browse/OTS-113">http://simulation.tudelft.nl:8085/browse/OTS-113</a>.
34   * <p>
35   * Copyright (c) 2013-2019 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
36   * BSD-style license. See <a href="http://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
37   * <p>
38   * @version $Revision: 1368 $, $LastChangedDate: 2015-09-02 00:20:20 +0200 (Wed, 02 Sep 2015) $, by $Author: averbraeck $,
39   *          initial version 11 feb. 2015 <br>
40   * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
41   * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
42   */
43  public abstract class AbstractHeadwayGTU extends AbstractHeadwayCopy implements HeadwayGTU
44  {
45      /** */
46      private static final long serialVersionUID = 20160410L;
47  
48      /** The perceived GTU Type, or null if unknown. */
49      private final GTUType gtuType;
50  
51      /** Whether the GTU is facing the same direction. */
52      private final boolean facingSameDirection;
53  
54      /** The observable characteristics of the GTU. */
55      private final EnumSet<GTUStatus> gtuStatus = EnumSet.noneOf(GTUStatus.class);
56  
57      /** Perceived desired speed. */
58      private final Speed desiredSpeed;
59  
60      /** Perceived width. */
61      private final Length width;
62  
63      /**
64       * Construct a new Headway information object, for a moving GTU ahead of us or behind us.
65       * @param id String; the id of the GTU for comparison purposes, can not be null.
66       * @param gtuType GTUType; the perceived GTU Type, or null if unknown.
67       * @param distance the distance to the other object; if this constructor is used, distance cannot be null.
68       * @param facingSameDirection boolean; whether the GTU is facing the same direction.
69       * @param length the (perceived) length of the other object; can not be null.
70       * @param width the (perceived) width of the other object; can not be null.
71       * @param speed the (perceived) speed of the other object; can be null if unknown.
72       * @param acceleration the (perceived) acceleration of the other object; can be null if unknown.
73       * @param desiredSpeed Speed; desired speed
74       * @param gtuStatus GTUStatus...; the observable characteristics of the GTU.
75       * @throws GTUException when id is null, objectType is null, or parameters are inconsistent
76       */
77      @SuppressWarnings("checkstyle:parameternumber")
78      public AbstractHeadwayGTU(final String id, final GTUType gtuType, final Length distance, final boolean facingSameDirection,
79              final Length length, final Length width, final Speed speed, final Acceleration acceleration,
80              final Speed desiredSpeed, final GTUStatus... gtuStatus) throws GTUException
81      {
82          super(ObjectType.GTU, id, distance, length, speed, acceleration);
83          Throw.whenNull(width, "Width may not be null.");
84          this.width = width;
85          this.facingSameDirection = facingSameDirection;
86          this.gtuType = gtuType;
87          this.desiredSpeed = desiredSpeed;
88          for (GTUStatus status : gtuStatus)
89          {
90              this.gtuStatus.add(status);
91          }
92      }
93  
94      /**
95       * Construct a new Headway information object, for a non-moving GTU ahead of us or behind us.
96       * @param id String; the id of the GTU for comparison purposes, can not be null.
97       * @param gtuType GTUType; the perceived GTU Type, or null if unknown.
98       * @param distance Length; the distance to the other GTU; if this constructor is used, distance cannot be null.
99       * @param facingSameDirection boolean; whether the GTU is facing the same direction.
100      * @param length the (perceived) length of the other object; can not be null.
101      * @param width the (perceived) width of the other object; can not be null.
102      * @param desiredSpeed Speed; desired speed
103      * @param gtuStatus GTUStatus...; the observable characteristics of the GTU.
104      * @throws GTUException when id is null, or parameters are inconsistent
105      */
106     public AbstractHeadwayGTU(final String id, final GTUType gtuType, final Length distance, final boolean facingSameDirection,
107             final Length length, final Length width, final Speed desiredSpeed, final GTUStatus... gtuStatus) throws GTUException
108     {
109         super(ObjectType.GTU, id, distance, length);
110         Throw.whenNull(width, "Width may not be null.");
111         this.width = width;
112         this.facingSameDirection = facingSameDirection;
113         this.gtuType = gtuType;
114         this.desiredSpeed = desiredSpeed;
115         for (GTUStatus status : gtuStatus)
116         {
117             this.gtuStatus.add(status);
118         }
119     }
120 
121     /**
122      * Construct a new Headway information object, for a moving GTU parallel with us.
123      * @param id String; the id of the GTU for comparison purposes, can not be null.
124      * @param gtuType GTUType; the perceived GTU Type, or null if unknown.
125      * @param overlapFront the front-front distance to the other GTU; if this constructor is used, this value cannot be null.
126      * @param overlap the 'center' overlap with the other GTU; if this constructor is used, this value cannot be null.
127      * @param overlapRear the rear-rear distance to the other GTU; if this constructor is used, this value cannot be null.
128      * @param facingSameDirection boolean; whether the GTU is facing the same direction.
129      * @param length the (perceived) length of the other object; can not be null.
130      * @param width the (perceived) width of the other object; can not be null.
131      * @param speed the (perceived) speed of the other GTU; can be null if unknown.
132      * @param acceleration the (perceived) acceleration of the other GTU; can be null if unknown.
133      * @param desiredSpeed Speed; desired speed
134      * @param gtuStatus GTUStatus...; the observable characteristics of the GTU.
135      * @throws GTUException when id is null, or parameters are inconsistent
136      */
137     @SuppressWarnings("checkstyle:parameternumber")
138     public AbstractHeadwayGTU(final String id, final GTUType gtuType, final Length overlapFront, final Length overlap,
139             final Length overlapRear, final boolean facingSameDirection, final Length length, final Length width,
140             final Speed speed, final Acceleration acceleration, final Speed desiredSpeed, final GTUStatus... gtuStatus)
141             throws GTUException
142     {
143         super(ObjectType.GTU, id, overlapFront, overlap, overlapRear, length, speed, acceleration);
144         Throw.whenNull(width, "Width may not be null.");
145         this.width = width;
146         this.facingSameDirection = facingSameDirection;
147         this.gtuType = gtuType;
148         this.desiredSpeed = desiredSpeed;
149         for (GTUStatus status : gtuStatus)
150         {
151             this.gtuStatus.add(status);
152         }
153     }
154 
155     /**
156      * Construct a new Headway information object, for a non-moving GTU parallel with us.
157      * @param id String; the id of the GTU for comparison purposes, can not be null.
158      * @param gtuType GTUType; the perceived GTU Type, or null if unknown.
159      * @param overlapFront the front-front distance to the other GTU; if this constructor is used, this value cannot be null.
160      * @param overlap the 'center' overlap with the other GTU; if this constructor is used, this value cannot be null.
161      * @param overlapRear the rear-rear distance to the other GTU; if this constructor is used, this value cannot be null.
162      * @param facingSameDirection boolean; whether the GTU is facing the same direction.
163      * @param length the (perceived) length of the other object; can not be null.
164      * @param width the (perceived) width of the other object; can not be null.
165      * @param desiredSpeed Speed; desired speed
166      * @param gtuStatus GTUStatus...; the observable characteristics of the GTU.
167      * @throws GTUException when id is null, or parameters are inconsistent
168      */
169     @SuppressWarnings("checkstyle:parameternumber")
170     public AbstractHeadwayGTU(final String id, final GTUType gtuType, final Length overlapFront, final Length overlap,
171             final Length overlapRear, final boolean facingSameDirection, final Length length, final Length width,
172             final Speed desiredSpeed, final GTUStatus... gtuStatus) throws GTUException
173     {
174         super(ObjectType.GTU, id, overlapFront, overlap, overlapRear, length);
175         Throw.whenNull(width, "Width may not be null.");
176         this.width = width;
177         this.facingSameDirection = facingSameDirection;
178         this.gtuType = gtuType;
179         this.desiredSpeed = desiredSpeed;
180         for (GTUStatus status : gtuStatus)
181         {
182             this.gtuStatus.add(status);
183         }
184     }
185 
186     /**
187      * @return gtuType
188      */
189     @Override
190     public final GTUType getGtuType()
191     {
192         return this.gtuType;
193     }
194 
195     /** {@inheritDoc} */
196     @Override
197     public final Speed getDesiredSpeed()
198     {
199         return this.desiredSpeed;
200     }
201 
202     /**
203      * @return facingSameDirection
204      */
205     @Override
206     public final boolean isFacingSameDirection()
207     {
208         return this.facingSameDirection;
209     }
210 
211     /** @return were the braking lights on? */
212     @Override
213     public final boolean isBrakingLightsOn()
214     {
215         return this.gtuStatus.contains(GTUStatus.BRAKING_LIGHTS);
216     }
217 
218     /** @return was the left turn indicator on? */
219     @Override
220     public final boolean isLeftTurnIndicatorOn()
221     {
222         return this.gtuStatus.contains(GTUStatus.LEFT_TURNINDICATOR);
223     }
224 
225     /** @return was the right turn indicator on? */
226     @Override
227     public final boolean isRightTurnIndicatorOn()
228     {
229         return this.gtuStatus.contains(GTUStatus.RIGHT_TURNINDICATOR);
230     }
231 
232     /** @return were the emergency lights on? */
233     @Override
234     public final boolean isEmergencyLightsOn()
235     {
236         return this.gtuStatus.contains(GTUStatus.EMERGENCY_LIGHTS);
237     }
238 
239     /** @return was the vehicle honking or ringing its bell when being observed for the headway? */
240     @Override
241     public final boolean isHonking()
242     {
243         return this.gtuStatus.contains(GTUStatus.HONK);
244     }
245 
246     /**
247      * For subclasses that create a copy of themselves.
248      * @return set of gtu status
249      */
250     protected final GTUStatus[] getGtuStatus()
251     {
252         return this.gtuStatus.toArray(new GTUStatus[this.gtuStatus.size()]);
253     }
254 
255     /**
256      * Collects GTU statuses from a gtu.
257      * @param gtu LaneBasedGTU; gtu
258      * @param when Time; time
259      * @return GTU statuses
260      */
261     public static final GTUStatus[] getGTUStatuses(final LaneBasedGTU gtu, final Time when)
262     {
263         List<GTUStatus> statuses = new ArrayList<>();
264         if (gtu.isBrakingLightsOn(when))
265         {
266             statuses.add(GTUStatus.BRAKING_LIGHTS);
267         }
268         if (gtu.getTurnIndicatorStatus(when).isHazard())
269         {
270             statuses.add(GTUStatus.EMERGENCY_LIGHTS);
271         }
272         else if (gtu.getTurnIndicatorStatus(when).isLeft())
273         {
274             statuses.add(GTUStatus.LEFT_TURNINDICATOR);
275         }
276         else if (gtu.getTurnIndicatorStatus(when).isRight())
277         {
278             statuses.add(GTUStatus.RIGHT_TURNINDICATOR);
279         }
280         return statuses.toArray(new GTUStatus[statuses.size()]);
281     }
282 
283     /**
284      * Creates speed limit info for given GTU.
285      * @param gtu LaneBasedGTU; gtu to the the speed limit info for
286      * @return speed limit info for given GTU
287      */
288     public static SpeedLimitInfo getSpeedLimitInfo(final LaneBasedGTU gtu)
289     {
290         SpeedLimitInfo sli = new SpeedLimitInfo();
291         sli.addSpeedInfo(SpeedLimitTypes.MAX_VEHICLE_SPEED, gtu.getMaximumSpeed());
292         try
293         {
294             sli.addSpeedInfo(SpeedLimitTypes.FIXED_SIGN, gtu.getReferencePosition().getLane().getSpeedLimit(gtu.getGTUType()));
295         }
296         catch (NetworkException | GTUException exception)
297         {
298             throw new RuntimeException("Could not obtain speed limit from lane for perception.", exception);
299         }
300         return sli;
301     }
302 
303     /** {@inheritDoc} */
304     @Override
305     public Length getWidth()
306     {
307         return this.width;
308     }
309 
310     /** {@inheritDoc} */
311     @Override
312     @SuppressWarnings("checkstyle:designforextension")
313     public String toString()
314     {
315         return "AbstractHeadwayGTU [gtuType=" + this.gtuType + ", gtuStatus=" + this.gtuStatus + ", getSpeed()="
316                 + this.getSpeed() + ", getDistance()=" + this.getDistance() + ", getAcceleration()=" + this.getAcceleration()
317                 + "]";
318     }
319 
320 }