View Javadoc
1   package org.opentrafficsim.road.gtu.lane.perception.headway;
2   
3   import org.djunits.value.vdouble.scalar.Acceleration;
4   import org.djunits.value.vdouble.scalar.Length;
5   import org.djunits.value.vdouble.scalar.Speed;
6   import org.djutils.exceptions.Throw;
7   import org.opentrafficsim.core.gtu.GtuException;
8   
9   /**
10   * Container for a reference to information about a (lane based) GTU and a headway. The Headway can store information about GTUs
11   * or objects ahead of the reference GTU, behind the reference GTU, or (partially) parallel to the reference GTU. In addition to
12   * the (perceived) headway, several other pieces of information can be stored, such as (perceived) speed, (perceived)
13   * acceleration, (perceived) turn indicators, and (perceived) braking lights. <br>
14   * Special care must be taken in curves when perceiving headway of a GTU or object on an adjacent lane.The question is whether
15   * we perceive the parallel or ahead/behind based on a line perpendicular to the front/back of the GTU (rectangular), or
16   * perpendicular to the center line of the lane (wedge-shaped in case of a curve). The difficulty of a wedge-shaped situation is
17   * that reciprocity might be violated: in case of a clothoid, for instance, it is not sure that the point on the center line
18   * 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
19   * sharp bends. Therefore, algorithms implementing headway should only project the <i>reference point</i> of the reference GTU
20   * on the center line of the adjacent lane, and then calculate the forward position and backward position on the adjacent lane
21   * based on the reference point. Still, our human perception of what is parallel and what not, is not reflected by fractional
22   * positions. 
23   * <p>
24   * Copyright (c) 2013-2024 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
25   * BSD-style license. See <a href="https://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
26   * </p>
27   * @author <a href="https://github.com/averbraeck">Alexander Verbraeck</a>
28   * @author <a href="https://tudelft.nl/staff/p.knoppers-1">Peter Knoppers</a>
29   * @author <a href="https://github.com/wjschakel">Wouter Schakel</a>
30   */
31  public abstract class AbstractHeadwayCopy extends AbstractHeadway
32  {
33      /** */
34      private static final long serialVersionUID = 20160410L;
35  
36      /** The id of the other object for comparison purposes, cannot be null. */
37      private final String id;
38  
39      /** The (perceived) length of the other object. Can be null if unknown. */
40      private final Length length;
41  
42      /** The (perceived) speed of the other object. v&gt;0 seen from driver chair. Can be null if unknown. */
43      private final Speed speed;
44  
45      /** The (perceived) acceleration of the other object. Can be null if unknown. */
46      private final Acceleration acceleration;
47  
48      /** The object type. */
49      private final ObjectType objectType;
50  
51      /**
52       * Construct a new Headway information object, for an object in front, behind, or in parallel with us. <br>
53       * @param objectType ObjectType; the perceived object type, can be null if object type unknown.
54       * @param id String; the id of the object for comparison purposes, can not be null.
55       * @param distance Length; the distance to the other object
56       * @param length Length; the length of the other object, can be null if not applicable.
57       * @param overlapFront Length; the front-front distance to the other object
58       * @param overlap Length; the 'center' overlap with the other object
59       * @param overlapRear Length; the rear-rear distance to the other object
60       * @param speed the (perceived) speed of the other object; can be null if unknown.
61       * @param acceleration the (perceived) acceleration of the other object; can be null if unknown.
62       * @throws GtuException when id is null, or parameters are inconsistent
63       */
64      @SuppressWarnings("checkstyle:parameternumber")
65      private AbstractHeadwayCopy(final ObjectType objectType, final String id, final Length distance, final Length length,
66              final Speed speed, final Acceleration acceleration, final Length overlapFront, final Length overlap,
67              final Length overlapRear) throws GtuException
68      {
69          super(distance, overlapFront, overlap, overlapRear);
70          Throw.when(id == null, GtuException.class, "Object id of a headway cannot be null");
71          this.id = id;
72  
73          this.objectType = objectType;
74          this.length = length;
75          this.speed = speed;
76          this.acceleration = acceleration;
77      }
78  
79      /**
80       * Construct a new Headway information object, for a moving object ahead of us or behind us.
81       * @param objectType ObjectType; the perceived object type, can be null if object type unknown.
82       * @param id String; the id of the object for comparison purposes, can not be null.
83       * @param distance the distance to the other object; if this constructor is used, distance cannot be null.
84       * @param speed the (perceived) speed of the other object; can be null if unknown.
85       * @param acceleration the (perceived) acceleration of the other object; can be null if unknown.
86       * @throws GtuException when id is null, or parameters are inconsistent
87       */
88      public AbstractHeadwayCopy(final ObjectType objectType, final String id, final Length distance, final Speed speed,
89              final Acceleration acceleration) throws GtuException
90      {
91          this(objectType, id, distance, null, speed, acceleration, null, null, null);
92      }
93  
94      /**
95       * Construct a new Headway information object, for a non-moving object ahead of us or behind us.
96       * @param objectType ObjectType; the perceived object type, can be null if object type unknown.
97       * @param id String; the id of the object for comparison purposes, can not be null.
98       * @param distance the distance to the other object; if this constructor is used, distance cannot be null.
99       * @throws GtuException when id is null, or parameters are inconsistent
100      */
101     public AbstractHeadwayCopy(final ObjectType objectType, final String id, final Length distance) throws GtuException
102     {
103         this(objectType, id, distance, null, Speed.ZERO, Acceleration.ZERO, null, null, null);
104     }
105 
106     /**
107      * Construct a new Headway information object, for a moving object parallel with us.
108      * @param objectType ObjectType; the perceived object type, can be null if object type unknown.
109      * @param id String; the id of the object for comparison purposes, can not be null.
110      * @param overlapFront the front-front distance to the other object; if this constructor is used, this value cannot be null.
111      * @param overlap the 'center' overlap with the other object; if this constructor is used, this value cannot be null.
112      * @param overlapRear the rear-rear distance to the other object; if this constructor is used, this value cannot be null.
113      * @param speed the (perceived) speed of the other object; can be null if unknown.
114      * @param acceleration the (perceived) acceleration of the other object; can be null if unknown.
115      * @throws GtuException when id is null, or parameters are inconsistent
116      */
117     @SuppressWarnings("checkstyle:parameternumber")
118     public AbstractHeadwayCopy(final ObjectType objectType, final String id, final Length overlapFront, final Length overlap,
119             final Length overlapRear, final Speed speed, final Acceleration acceleration) throws GtuException
120     {
121         this(objectType, id, null, null, speed, acceleration, overlapFront, overlap, overlapRear);
122     }
123 
124     /**
125      * Construct a new Headway information object, for a non-moving object parallel with us.
126      * @param objectType ObjectType; the perceived object type, can be null if object type unknown.
127      * @param id String; the id of the object for comparison purposes, can not be null.
128      * @param overlapFront the front-front distance to the other object; if this constructor is used, this value cannot be null.
129      * @param overlap the 'center' overlap with the other object; if this constructor is used, this value cannot be null.
130      * @param overlapRear the rear-rear distance to the other object; if this constructor is used, this value cannot be null.
131      * @throws GtuException when id is null, or parameters are inconsistent
132      */
133     public AbstractHeadwayCopy(final ObjectType objectType, final String id, final Length overlapFront, final Length overlap,
134             final Length overlapRear) throws GtuException
135     {
136         this(objectType, id, null, null, null, null, overlapFront, overlap, overlapRear);
137     }
138 
139     /**
140      * Construct a new Headway information object, for a moving object ahead of us or behind us.
141      * @param objectType ObjectType; the perceived object type, can be null if object type unknown.
142      * @param id String; the id of the object for comparison purposes, can not be null.
143      * @param distance the distance to the other object; if this constructor is used, distance cannot be null.
144      * @param length the length of the other object; if this constructor is used, length cannot be null.
145      * @param speed the (perceived) speed of the other object; can be null if unknown.
146      * @param acceleration the (perceived) acceleration of the other object; can be null if unknown.
147      * @throws GtuException when id is null, or parameters are inconsistent
148      */
149     public AbstractHeadwayCopy(final ObjectType objectType, final String id, final Length distance, final Length length,
150             final Speed speed, final Acceleration acceleration) throws GtuException
151     {
152         this(objectType, id, distance, length, speed, acceleration, null, null, null);
153         Throw.whenNull(length, "Length may not be null.");
154     }
155 
156     /**
157      * Construct a new Headway information object, for a non-moving object ahead of us or behind us.
158      * @param objectType ObjectType; the perceived object type, can be null if object type unknown.
159      * @param id String; the id of the object for comparison purposes, can not be null.
160      * @param distance the distance to the other object; if this constructor is used, distance cannot be null.
161      * @param length the length of the other object; if this constructor is used, length cannot be null.
162      * @throws GtuException when id is null, or parameters are inconsistent
163      */
164     public AbstractHeadwayCopy(final ObjectType objectType, final String id, final Length distance, final Length length)
165             throws GtuException
166     {
167         this(objectType, id, distance, length, null, null, null, null, null);
168         Throw.whenNull(length, "Length may not be null.");
169     }
170 
171     /**
172      * Construct a new Headway information object, for a moving object parallel with us.
173      * @param objectType ObjectType; the perceived object type, can be null if object type unknown.
174      * @param id String; the id of the object for comparison purposes, can not be null.
175      * @param overlapFront the front-front distance to the other object; if this constructor is used, this value cannot be null.
176      * @param overlap the 'center' overlap with the other object; if this constructor is used, this value cannot be null.
177      * @param overlapRear the rear-rear distance to the other object; if this constructor is used, this value cannot be null.
178      * @param length the length of the other object; if this constructor is used, length cannot be null.
179      * @param speed the (perceived) speed of the other object; can be null if unknown.
180      * @param acceleration the (perceived) acceleration of the other object; can be null if unknown.
181      * @throws GtuException when id is null, or parameters are inconsistent
182      */
183     @SuppressWarnings("checkstyle:parameternumber")
184     public AbstractHeadwayCopy(final ObjectType objectType, final String id, final Length overlapFront, final Length overlap,
185             final Length overlapRear, final Length length, final Speed speed, final Acceleration acceleration)
186             throws GtuException
187     {
188         this(objectType, id, null, length, speed, acceleration, overlapFront, overlap, overlapRear);
189         Throw.whenNull(length, "Length may not be null.");
190     }
191 
192     /**
193      * Construct a new Headway information object, for a non-moving object parallel with us.
194      * @param objectType ObjectType; the perceived object type, can be null if object type unknown.
195      * @param id String; the id of the object for comparison purposes, can not be null.
196      * @param overlapFront the front-front distance to the other object; if this constructor is used, this value cannot be null.
197      * @param overlap the 'center' overlap with the other object; if this constructor is used, this value cannot be null.
198      * @param overlapRear the rear-rear distance to the other object; if this constructor is used, this value cannot be null.
199      * @param length the length of the other object; if this constructor is used, length cannot be null.
200      * @throws GtuException when id is null, or parameters are inconsistent
201      */
202     public AbstractHeadwayCopy(final ObjectType objectType, final String id, final Length overlapFront, final Length overlap,
203             final Length overlapRear, final Length length) throws GtuException
204     {
205         this(objectType, id, null, length, null, null, overlapFront, overlap, overlapRear);
206         Throw.whenNull(length, "Length may not be null.");
207     }
208 
209     /** {@inheritDoc} */
210     @Override
211     public final String getId()
212     {
213         return this.id;
214     }
215 
216     /** {@inheritDoc} */
217     @Override
218     public final Length getLength()
219     {
220         return this.length;
221     }
222 
223     /** {@inheritDoc} */
224     @Override
225     public final Speed getSpeed()
226     {
227         return this.speed;
228     }
229 
230     /** {@inheritDoc} */
231     @Override
232     public final ObjectType getObjectType()
233     {
234         return this.objectType;
235     }
236 
237     /** {@inheritDoc} */
238     @Override
239     public final Acceleration getAcceleration()
240     {
241         return this.acceleration;
242     }
243 
244     /** {@inheritDoc} */
245     @Override
246     public int hashCode()
247     {
248         final int prime = 31;
249         int result = super.hashCode();
250         result = prime * result + ((this.acceleration == null) ? 0 : this.acceleration.hashCode());
251         result = prime * result + ((this.id == null) ? 0 : this.id.hashCode());
252         result = prime * result + ((this.length == null) ? 0 : this.length.hashCode());
253         result = prime * result + ((this.objectType == null) ? 0 : this.objectType.hashCode());
254         result = prime * result + ((this.speed == null) ? 0 : this.speed.hashCode());
255         return result;
256     }
257 
258     /** {@inheritDoc} */
259     @Override
260     public boolean equals(final Object obj)
261     {
262         if (this == obj)
263         {
264             return true;
265         }
266         if (!super.equals(obj))
267         {
268             return false;
269         }
270         if (getClass() != obj.getClass())
271         {
272             return false;
273         }
274         AbstractHeadwayCopy other = (AbstractHeadwayCopy) obj;
275         if (this.acceleration == null)
276         {
277             if (other.acceleration != null)
278             {
279                 return false;
280             }
281         }
282         else if (!this.acceleration.equals(other.acceleration))
283         {
284             return false;
285         }
286         if (this.id == null)
287         {
288             if (other.id != null)
289             {
290                 return false;
291             }
292         }
293         else if (!this.id.equals(other.id))
294         {
295             return false;
296         }
297         if (this.length == null)
298         {
299             if (other.length != null)
300             {
301                 return false;
302             }
303         }
304         else if (!this.length.equals(other.length))
305         {
306             return false;
307         }
308         if (this.objectType != other.objectType)
309         {
310             return false;
311         }
312         if (this.speed == null)
313         {
314             if (other.speed != null)
315             {
316                 return false;
317             }
318         }
319         else if (!this.speed.equals(other.speed))
320         {
321             return false;
322         }
323         return true;
324     }
325 
326 }