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. 
33   * <p>
34   * Copyright (c) 2013-2024 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
35   * BSD-style license. See <a href="https://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
36   * </p>
37   * @author <a href="https://github.com/averbraeck">Alexander Verbraeck</a>
38   * @author <a href="https://tudelft.nl/staff/p.knoppers-1">Peter Knoppers</a>
39   */
40  public abstract class AbstractHeadwayGtu extends AbstractHeadwayCopy implements HeadwayGtu
41  {
42      /** */
43      private static final long serialVersionUID = 20160410L;
44  
45      /** The perceived GTU Type, or null if unknown. */
46      private final GtuType gtuType;
47  
48      /** Whether the GTU is facing the same direction. */
49      private final boolean facingSameDirection;
50  
51      /** The observable characteristics of the GTU. */
52      private final EnumSet<GtuStatus> gtuStatus = EnumSet.noneOf(GtuStatus.class);
53  
54      /** Perceived desired speed. */
55      private final Speed desiredSpeed;
56  
57      /** Perceived width. */
58      private final Length width;
59  
60      /**
61       * Construct a new Headway information object, for a moving GTU ahead of us or behind us.
62       * @param id String; the id of the GTU for comparison purposes, can not be null.
63       * @param gtuType GtuType; the perceived GTU Type, or null if unknown.
64       * @param distance the distance to the other object; if this constructor is used, distance cannot be null.
65       * @param facingSameDirection boolean; whether the GTU is facing the same direction.
66       * @param length the (perceived) length of the other object; can not be null.
67       * @param width the (perceived) width of the other object; can not be null.
68       * @param speed the (perceived) speed of the other object; can be null if unknown.
69       * @param acceleration the (perceived) acceleration of the other object; can be null if unknown.
70       * @param desiredSpeed Speed; desired speed
71       * @param gtuStatus GtuStatus...; the observable characteristics of the GTU.
72       * @throws GtuException when id is null, objectType is null, or parameters are inconsistent
73       */
74      @SuppressWarnings("checkstyle:parameternumber")
75      public AbstractHeadwayGtu(final String id, final GtuType gtuType, final Length distance, final boolean facingSameDirection,
76              final Length length, final Length width, final Speed speed, final Acceleration acceleration,
77              final Speed desiredSpeed, final GtuStatus... gtuStatus) throws GtuException
78      {
79          super(ObjectType.GTU, id, distance, length, speed, acceleration);
80          Throw.whenNull(width, "Width may not be null.");
81          this.width = width;
82          this.facingSameDirection = facingSameDirection;
83          this.gtuType = gtuType;
84          this.desiredSpeed = desiredSpeed;
85          for (GtuStatus status : gtuStatus)
86          {
87              this.gtuStatus.add(status);
88          }
89      }
90  
91      /**
92       * Construct a new Headway information object, for a non-moving GTU ahead of us or behind us.
93       * @param id String; the id of the GTU for comparison purposes, can not be null.
94       * @param gtuType GtuType; the perceived GTU Type, or null if unknown.
95       * @param distance Length; the distance to the other Gtu; if this constructor is used, distance cannot be null.
96       * @param facingSameDirection boolean; whether the GTU is facing the same direction.
97       * @param length the (perceived) length of the other object; can not be null.
98       * @param width the (perceived) width of the other object; can not be null.
99       * @param desiredSpeed Speed; desired speed
100      * @param gtuStatus GtuStatus...; the observable characteristics of the GTU.
101      * @throws GtuException when id is null, or parameters are inconsistent
102      */
103     public AbstractHeadwayGtu(final String id, final GtuType gtuType, final Length distance, final boolean facingSameDirection,
104             final Length length, final Length width, final Speed desiredSpeed, final GtuStatus... gtuStatus) throws GtuException
105     {
106         super(ObjectType.GTU, id, distance, length);
107         Throw.whenNull(width, "Width may not be null.");
108         this.width = width;
109         this.facingSameDirection = facingSameDirection;
110         this.gtuType = gtuType;
111         this.desiredSpeed = desiredSpeed;
112         for (GtuStatus status : gtuStatus)
113         {
114             this.gtuStatus.add(status);
115         }
116     }
117 
118     /**
119      * Construct a new Headway information object, for a moving GTU parallel with us.
120      * @param id String; the id of the GTU for comparison purposes, can not be null.
121      * @param gtuType GtuType; the perceived GTU Type, or null if unknown.
122      * @param overlapFront the front-front distance to the other Gtu; if this constructor is used, this value cannot be null.
123      * @param overlap the 'center' overlap with the other Gtu; if this constructor is used, this value cannot be null.
124      * @param overlapRear the rear-rear distance to the other Gtu; if this constructor is used, this value cannot be null.
125      * @param facingSameDirection boolean; whether the GTU is facing the same direction.
126      * @param length the (perceived) length of the other object; can not be null.
127      * @param width the (perceived) width of the other object; can not be null.
128      * @param speed the (perceived) speed of the other Gtu; can be null if unknown.
129      * @param acceleration the (perceived) acceleration of the other Gtu; can be null if unknown.
130      * @param desiredSpeed Speed; desired speed
131      * @param gtuStatus GtuStatus...; the observable characteristics of the GTU.
132      * @throws GtuException when id is null, or parameters are inconsistent
133      */
134     @SuppressWarnings("checkstyle:parameternumber")
135     public AbstractHeadwayGtu(final String id, final GtuType gtuType, final Length overlapFront, final Length overlap,
136             final Length overlapRear, final boolean facingSameDirection, final Length length, final Length width,
137             final Speed speed, final Acceleration acceleration, final Speed desiredSpeed, final GtuStatus... gtuStatus)
138             throws GtuException
139     {
140         super(ObjectType.GTU, id, overlapFront, overlap, overlapRear, length, speed, acceleration);
141         Throw.whenNull(width, "Width may not be null.");
142         this.width = width;
143         this.facingSameDirection = facingSameDirection;
144         this.gtuType = gtuType;
145         this.desiredSpeed = desiredSpeed;
146         for (GtuStatus status : gtuStatus)
147         {
148             this.gtuStatus.add(status);
149         }
150     }
151 
152     /**
153      * Construct a new Headway information object, for a non-moving GTU parallel with us.
154      * @param id String; the id of the GTU for comparison purposes, can not be null.
155      * @param gtuType GtuType; the perceived GTU Type, or null if unknown.
156      * @param overlapFront the front-front distance to the other Gtu; if this constructor is used, this value cannot be null.
157      * @param overlap the 'center' overlap with the other Gtu; if this constructor is used, this value cannot be null.
158      * @param overlapRear the rear-rear distance to the other Gtu; if this constructor is used, this value cannot be null.
159      * @param facingSameDirection boolean; whether the GTU is facing the same direction.
160      * @param length the (perceived) length of the other object; can not be null.
161      * @param width the (perceived) width of the other object; can not be null.
162      * @param desiredSpeed Speed; desired speed
163      * @param gtuStatus GtuStatus...; the observable characteristics of the GTU.
164      * @throws GtuException when id is null, or parameters are inconsistent
165      */
166     @SuppressWarnings("checkstyle:parameternumber")
167     public AbstractHeadwayGtu(final String id, final GtuType gtuType, final Length overlapFront, final Length overlap,
168             final Length overlapRear, final boolean facingSameDirection, final Length length, final Length width,
169             final Speed desiredSpeed, final GtuStatus... gtuStatus) throws GtuException
170     {
171         super(ObjectType.GTU, id, overlapFront, overlap, overlapRear, length);
172         Throw.whenNull(width, "Width may not be null.");
173         this.width = width;
174         this.facingSameDirection = facingSameDirection;
175         this.gtuType = gtuType;
176         this.desiredSpeed = desiredSpeed;
177         for (GtuStatus status : gtuStatus)
178         {
179             this.gtuStatus.add(status);
180         }
181     }
182 
183     /**
184      * @return gtuType
185      */
186     @Override
187     public final GtuType getGtuType()
188     {
189         return this.gtuType;
190     }
191 
192     /** {@inheritDoc} */
193     @Override
194     public final Speed getDesiredSpeed()
195     {
196         return this.desiredSpeed;
197     }
198 
199     /**
200      * @return facingSameDirection
201      */
202     @Override
203     public final boolean isFacingSameDirection()
204     {
205         return this.facingSameDirection;
206     }
207 
208     /** @return were the braking lights on? */
209     @Override
210     public final boolean isBrakingLightsOn()
211     {
212         return this.gtuStatus.contains(GtuStatus.BRAKING_LIGHTS);
213     }
214 
215     /** @return was the left turn indicator on? */
216     @Override
217     public final boolean isLeftTurnIndicatorOn()
218     {
219         return this.gtuStatus.contains(GtuStatus.LEFT_TURNINDICATOR);
220     }
221 
222     /** @return was the right turn indicator on? */
223     @Override
224     public final boolean isRightTurnIndicatorOn()
225     {
226         return this.gtuStatus.contains(GtuStatus.RIGHT_TURNINDICATOR);
227     }
228 
229     /** @return were the emergency lights on? */
230     @Override
231     public final boolean isEmergencyLightsOn()
232     {
233         return this.gtuStatus.contains(GtuStatus.EMERGENCY_LIGHTS);
234     }
235 
236     /** @return was the vehicle honking or ringing its bell when being observed for the headway? */
237     @Override
238     public final boolean isHonking()
239     {
240         return this.gtuStatus.contains(GtuStatus.HONK);
241     }
242 
243     /**
244      * For subclasses that create a copy of themselves.
245      * @return set of gtu status
246      */
247     protected final GtuStatus[] getGtuStatus()
248     {
249         return this.gtuStatus.toArray(new GtuStatus[this.gtuStatus.size()]);
250     }
251 
252     /**
253      * Collects GTU statuses from a gtu.
254      * @param gtu LaneBasedGtu; gtu
255      * @param when Time; time
256      * @return GTU statuses
257      */
258     public static final GtuStatus[] getGtuStatuses(final LaneBasedGtu gtu, final Time when)
259     {
260         List<GtuStatus> statuses = new ArrayList<>();
261         if (gtu.isBrakingLightsOn(when))
262         {
263             statuses.add(GtuStatus.BRAKING_LIGHTS);
264         }
265         if (gtu.getTurnIndicatorStatus(when).isHazard())
266         {
267             statuses.add(GtuStatus.EMERGENCY_LIGHTS);
268         }
269         else if (gtu.getTurnIndicatorStatus(when).isLeft())
270         {
271             statuses.add(GtuStatus.LEFT_TURNINDICATOR);
272         }
273         else if (gtu.getTurnIndicatorStatus(when).isRight())
274         {
275             statuses.add(GtuStatus.RIGHT_TURNINDICATOR);
276         }
277         return statuses.toArray(new GtuStatus[statuses.size()]);
278     }
279 
280     /**
281      * Creates speed limit info for given GTU.
282      * @param gtu LaneBasedGtu; gtu to the the speed limit info for
283      * @return speed limit info for given GTU
284      */
285     public static SpeedLimitInfo getSpeedLimitInfo(final LaneBasedGtu gtu)
286     {
287         SpeedLimitInfo sli = new SpeedLimitInfo();
288         sli.addSpeedInfo(SpeedLimitTypes.MAX_VEHICLE_SPEED, gtu.getMaximumSpeed());
289         try
290         {
291             sli.addSpeedInfo(SpeedLimitTypes.FIXED_SIGN, gtu.getReferencePosition().lane().getSpeedLimit(gtu.getType()));
292         }
293         catch (NetworkException | GtuException exception)
294         {
295             throw new RuntimeException("Could not obtain speed limit from lane for perception.", exception);
296         }
297         return sli;
298     }
299 
300     /** {@inheritDoc} */
301     @Override
302     public Length getWidth()
303     {
304         return this.width;
305     }
306 
307     /** {@inheritDoc} */
308     @Override
309     @SuppressWarnings("checkstyle:designforextension")
310     public String toString()
311     {
312         return "AbstractHeadwayGtu [gtuType=" + this.gtuType + ", gtuStatus=" + this.gtuStatus + ", getSpeed()="
313                 + this.getSpeed() + ", getDistance()=" + this.getDistance() + ", getAcceleration()=" + this.getAcceleration()
314                 + "]";
315     }
316 
317 }