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://github.com/peter-knoppers">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 the id of the GTU for comparison purposes, can not be null.
63       * @param gtuType the perceived GTU Type, or null if unknown.
64       * @param distance if this constructor is used, distance cannot be null.
65       * @param facingSameDirection 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 desired speed
71       * @param 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 the id of the GTU for comparison purposes, can not be null.
94       * @param gtuType the perceived GTU Type, or null if unknown.
95       * @param distance the distance to the other Gtu; if this constructor is used, distance cannot be null.
96       * @param facingSameDirection 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 desired speed
100      * @param 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 the id of the GTU for comparison purposes, can not be null.
121      * @param 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 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 desired speed
131      * @param 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 the id of the GTU for comparison purposes, can not be null.
155      * @param 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 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 desired speed
163      * @param 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     @Override
193     public final Speed getDesiredSpeed()
194     {
195         return this.desiredSpeed;
196     }
197 
198     /**
199      * @return facingSameDirection
200      */
201     @Override
202     public final boolean isFacingSameDirection()
203     {
204         return this.facingSameDirection;
205     }
206 
207     /** @return were the braking lights on? */
208     @Override
209     public final boolean isBrakingLightsOn()
210     {
211         return this.gtuStatus.contains(GtuStatus.BRAKING_LIGHTS);
212     }
213 
214     /** @return was the left turn indicator on? */
215     @Override
216     public final boolean isLeftTurnIndicatorOn()
217     {
218         return this.gtuStatus.contains(GtuStatus.LEFT_TURNINDICATOR);
219     }
220 
221     /** @return was the right turn indicator on? */
222     @Override
223     public final boolean isRightTurnIndicatorOn()
224     {
225         return this.gtuStatus.contains(GtuStatus.RIGHT_TURNINDICATOR);
226     }
227 
228     /** @return were the emergency lights on? */
229     @Override
230     public final boolean isEmergencyLightsOn()
231     {
232         return this.gtuStatus.contains(GtuStatus.EMERGENCY_LIGHTS);
233     }
234 
235     /** @return was the vehicle honking or ringing its bell when being observed for the headway? */
236     @Override
237     public final boolean isHonking()
238     {
239         return this.gtuStatus.contains(GtuStatus.HONK);
240     }
241 
242     /**
243      * For subclasses that create a copy of themselves.
244      * @return set of gtu status
245      */
246     protected final GtuStatus[] getGtuStatus()
247     {
248         return this.gtuStatus.toArray(new GtuStatus[this.gtuStatus.size()]);
249     }
250 
251     /**
252      * Collects GTU statuses from a gtu.
253      * @param gtu gtu
254      * @param when time
255      * @return GTU statuses
256      */
257     public static final GtuStatus[] getGtuStatuses(final LaneBasedGtu gtu, final Time when)
258     {
259         List<GtuStatus> statuses = new ArrayList<>();
260         if (gtu.isBrakingLightsOn(when))
261         {
262             statuses.add(GtuStatus.BRAKING_LIGHTS);
263         }
264         if (gtu.getTurnIndicatorStatus(when).isHazard())
265         {
266             statuses.add(GtuStatus.EMERGENCY_LIGHTS);
267         }
268         else if (gtu.getTurnIndicatorStatus(when).isLeft())
269         {
270             statuses.add(GtuStatus.LEFT_TURNINDICATOR);
271         }
272         else if (gtu.getTurnIndicatorStatus(when).isRight())
273         {
274             statuses.add(GtuStatus.RIGHT_TURNINDICATOR);
275         }
276         return statuses.toArray(new GtuStatus[statuses.size()]);
277     }
278 
279     /**
280      * Creates speed limit info for given GTU.
281      * @param gtu gtu to the the speed limit info for
282      * @return speed limit info for given GTU
283      */
284     public static SpeedLimitInfo getSpeedLimitInfo(final LaneBasedGtu gtu)
285     {
286         SpeedLimitInfo sli = new SpeedLimitInfo();
287         sli.addSpeedInfo(SpeedLimitTypes.MAX_VEHICLE_SPEED, gtu.getMaximumSpeed());
288         try
289         {
290             sli.addSpeedInfo(SpeedLimitTypes.FIXED_SIGN, gtu.getReferencePosition().lane().getSpeedLimit(gtu.getType()));
291         }
292         catch (NetworkException | GtuException exception)
293         {
294             throw new RuntimeException("Could not obtain speed limit from lane for perception.", exception);
295         }
296         return sli;
297     }
298 
299     @Override
300     public Length getWidth()
301     {
302         return this.width;
303     }
304 
305     @Override
306     @SuppressWarnings("checkstyle:designforextension")
307     public String toString()
308     {
309         return "AbstractHeadwayGtu [gtuType=" + this.gtuType + ", gtuStatus=" + this.gtuStatus + ", getSpeed()="
310                 + this.getSpeed() + ", getDistance()=" + this.getDistance() + ", getAcceleration()=" + this.getAcceleration()
311                 + "]";
312     }
313 
314 }