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. See examples in
23   * <a href= "http://simulation.tudelft.nl:8085/browse/OTS-113">http://simulation.tudelft.nl:8085/browse/OTS-113</a>.
24   * <p>
25   * Copyright (c) 2013-2019 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
26   * BSD-style license. See <a href="http://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
27   * <p>
28   * @version $Revision: 1368 $, $LastChangedDate: 2015-09-02 00:20:20 +0200 (Wed, 02 Sep 2015) $, by $Author: averbraeck $,
29   *          initial version 11 feb. 2015 <br>
30   * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
31   * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
32   * @author <a href="http://www.transport.citg.tudelft.nl">Wouter Schakel</a>
33   */
34  public abstract class AbstractHeadwayCopy extends AbstractHeadway
35  {
36      /** */
37      private static final long serialVersionUID = 20160410L;
38  
39      /** The id of the other object for comparison purposes, cannot be null. */
40      private final String id;
41  
42      /** The (perceived) length of the other object. Can be null if unknown. */
43      private final Length length;
44  
45      /** The (perceived) speed of the other object. v&gt;0 seen from driver chair. Can be null if unknown. */
46      private final Speed speed;
47  
48      /** The (perceived) acceleration of the other object. Can be null if unknown. */
49      private final Acceleration acceleration;
50  
51      /** The object type. */
52      private final ObjectType objectType;
53  
54      /**
55       * Construct a new Headway information object, for an object in front, behind, or in parallel with us. <br>
56       * @param objectType ObjectType; the perceived object type, can be null if object type unknown.
57       * @param id String; the id of the object for comparison purposes, can not be null.
58       * @param distance Length; the distance to the other object
59       * @param length Length; the length of the other object, can be null if not applicable.
60       * @param overlapFront Length; the front-front distance to the other object
61       * @param overlap Length; the 'center' overlap with the other object
62       * @param overlapRear Length; the rear-rear distance to the other object
63       * @param speed the (perceived) speed of the other object; can be null if unknown.
64       * @param acceleration the (perceived) acceleration of the other object; can be null if unknown.
65       * @throws GTUException when id is null, or parameters are inconsistent
66       */
67      @SuppressWarnings("checkstyle:parameternumber")
68      private AbstractHeadwayCopy(final ObjectType objectType, final String id, final Length distance, final Length length,
69              final Speed speed, final Acceleration acceleration, final Length overlapFront, final Length overlap,
70              final Length overlapRear) throws GTUException
71      {
72          super(distance, overlapFront, overlap, overlapRear);
73          Throw.when(id == null, GTUException.class, "Object id of a headway cannot be null");
74          this.id = id;
75  
76          this.objectType = objectType;
77          this.length = length;
78          this.speed = speed;
79          this.acceleration = acceleration;
80      }
81  
82      /**
83       * Construct a new Headway information object, for a moving object ahead of us or behind us.
84       * @param objectType ObjectType; the perceived object type, can be null if object type unknown.
85       * @param id String; the id of the object for comparison purposes, can not be null.
86       * @param distance the distance to the other object; if this constructor is used, distance cannot be null.
87       * @param speed the (perceived) speed of the other object; can be null if unknown.
88       * @param acceleration the (perceived) acceleration of the other object; can be null if unknown.
89       * @throws GTUException when id is null, or parameters are inconsistent
90       */
91      public AbstractHeadwayCopy(final ObjectType objectType, final String id, final Length distance, final Speed speed,
92              final Acceleration acceleration) throws GTUException
93      {
94          this(objectType, id, distance, null, speed, acceleration, null, null, null);
95      }
96  
97      /**
98       * Construct a new Headway information object, for a non-moving object ahead of us or behind us.
99       * @param objectType ObjectType; the perceived object type, can be null if object type unknown.
100      * @param id String; the id of the object for comparison purposes, can not be null.
101      * @param distance the distance to the other object; if this constructor is used, distance cannot be null.
102      * @throws GTUException when id is null, or parameters are inconsistent
103      */
104     public AbstractHeadwayCopy(final ObjectType objectType, final String id, final Length distance) throws GTUException
105     {
106         this(objectType, id, distance, null, Speed.ZERO, Acceleration.ZERO, null, null, null);
107     }
108 
109     /**
110      * Construct a new Headway information object, for a moving object parallel with us.
111      * @param objectType ObjectType; the perceived object type, can be null if object type unknown.
112      * @param id String; the id of the object for comparison purposes, can not be null.
113      * @param overlapFront the front-front distance to the other object; if this constructor is used, this value cannot be null.
114      * @param overlap the 'center' overlap with the other object; if this constructor is used, this value cannot be null.
115      * @param overlapRear the rear-rear distance to the other object; if this constructor is used, this value cannot be null.
116      * @param speed the (perceived) speed of the other object; can be null if unknown.
117      * @param acceleration the (perceived) acceleration of the other object; can be null if unknown.
118      * @throws GTUException when id is null, or parameters are inconsistent
119      */
120     @SuppressWarnings("checkstyle:parameternumber")
121     public AbstractHeadwayCopy(final ObjectType objectType, final String id, final Length overlapFront, final Length overlap,
122             final Length overlapRear, final Speed speed, final Acceleration acceleration) throws GTUException
123     {
124         this(objectType, id, null, null, speed, acceleration, overlapFront, overlap, overlapRear);
125     }
126 
127     /**
128      * Construct a new Headway information object, for a non-moving object parallel with us.
129      * @param objectType ObjectType; the perceived object type, can be null if object type unknown.
130      * @param id String; the id of the object for comparison purposes, can not be null.
131      * @param overlapFront the front-front distance to the other object; if this constructor is used, this value cannot be null.
132      * @param overlap the 'center' overlap with the other object; if this constructor is used, this value cannot be null.
133      * @param overlapRear the rear-rear distance to the other object; if this constructor is used, this value cannot be null.
134      * @throws GTUException when id is null, or parameters are inconsistent
135      */
136     public AbstractHeadwayCopy(final ObjectType objectType, final String id, final Length overlapFront, final Length overlap,
137             final Length overlapRear) throws GTUException
138     {
139         this(objectType, id, null, null, null, null, overlapFront, overlap, overlapRear);
140     }
141 
142     /**
143      * Construct a new Headway information object, for a moving object ahead of us or behind us.
144      * @param objectType ObjectType; the perceived object type, can be null if object type unknown.
145      * @param id String; the id of the object for comparison purposes, can not be null.
146      * @param distance the distance to the other object; if this constructor is used, distance cannot be null.
147      * @param length the length of the other object; if this constructor is used, length cannot be null.
148      * @param speed the (perceived) speed of the other object; can be null if unknown.
149      * @param acceleration the (perceived) acceleration of the other object; can be null if unknown.
150      * @throws GTUException when id is null, or parameters are inconsistent
151      */
152     public AbstractHeadwayCopy(final ObjectType objectType, final String id, final Length distance, final Length length,
153             final Speed speed, final Acceleration acceleration) throws GTUException
154     {
155         this(objectType, id, distance, length, speed, acceleration, null, null, null);
156         Throw.whenNull(length, "Length may not be null.");
157     }
158 
159     /**
160      * Construct a new Headway information object, for a non-moving object ahead of us or behind us.
161      * @param objectType ObjectType; the perceived object type, can be null if object type unknown.
162      * @param id String; the id of the object for comparison purposes, can not be null.
163      * @param distance the distance to the other object; if this constructor is used, distance cannot be null.
164      * @param length the length of the other object; if this constructor is used, length cannot be null.
165      * @throws GTUException when id is null, or parameters are inconsistent
166      */
167     public AbstractHeadwayCopy(final ObjectType objectType, final String id, final Length distance, final Length length)
168             throws GTUException
169     {
170         this(objectType, id, distance, length, null, null, null, null, null);
171         Throw.whenNull(length, "Length may not be null.");
172     }
173 
174     /**
175      * Construct a new Headway information object, for a moving object parallel with us.
176      * @param objectType ObjectType; the perceived object type, can be null if object type unknown.
177      * @param id String; the id of the object for comparison purposes, can not be null.
178      * @param overlapFront the front-front distance to the other object; if this constructor is used, this value cannot be null.
179      * @param overlap the 'center' overlap with the other object; if this constructor is used, this value cannot be null.
180      * @param overlapRear the rear-rear distance to the other object; if this constructor is used, this value cannot be null.
181      * @param length the length of the other object; if this constructor is used, length cannot be null.
182      * @param speed the (perceived) speed of the other object; can be null if unknown.
183      * @param acceleration the (perceived) acceleration of the other object; can be null if unknown.
184      * @throws GTUException when id is null, or parameters are inconsistent
185      */
186     @SuppressWarnings("checkstyle:parameternumber")
187     public AbstractHeadwayCopy(final ObjectType objectType, final String id, final Length overlapFront, final Length overlap,
188             final Length overlapRear, final Length length, final Speed speed, final Acceleration acceleration)
189             throws GTUException
190     {
191         this(objectType, id, null, length, speed, acceleration, overlapFront, overlap, overlapRear);
192         Throw.whenNull(length, "Length may not be null.");
193     }
194 
195     /**
196      * Construct a new Headway information object, for a non-moving object parallel with us.
197      * @param objectType ObjectType; the perceived object type, can be null if object type unknown.
198      * @param id String; the id of the object for comparison purposes, can not be null.
199      * @param overlapFront the front-front distance to the other object; if this constructor is used, this value cannot be null.
200      * @param overlap the 'center' overlap with the other object; if this constructor is used, this value cannot be null.
201      * @param overlapRear the rear-rear distance to the other object; if this constructor is used, this value cannot be null.
202      * @param length the length of the other object; if this constructor is used, length cannot be null.
203      * @throws GTUException when id is null, or parameters are inconsistent
204      */
205     public AbstractHeadwayCopy(final ObjectType objectType, final String id, final Length overlapFront, final Length overlap,
206             final Length overlapRear, final Length length) throws GTUException
207     {
208         this(objectType, id, null, length, null, null, overlapFront, overlap, overlapRear);
209         Throw.whenNull(length, "Length may not be null.");
210     }
211 
212     /** {@inheritDoc} */
213     @Override
214     public final String getId()
215     {
216         return this.id;
217     }
218 
219     /** {@inheritDoc} */
220     @Override
221     public final Length getLength()
222     {
223         return this.length;
224     }
225 
226     /** {@inheritDoc} */
227     @Override
228     public final Speed getSpeed()
229     {
230         return this.speed;
231     }
232 
233     /** {@inheritDoc} */
234     @Override
235     public final ObjectType getObjectType()
236     {
237         return this.objectType;
238     }
239 
240     /** {@inheritDoc} */
241     @Override
242     public final Acceleration getAcceleration()
243     {
244         return this.acceleration;
245     }
246 
247     /** {@inheritDoc} */
248     @Override
249     public int hashCode()
250     {
251         final int prime = 31;
252         int result = super.hashCode();
253         result = prime * result + ((this.acceleration == null) ? 0 : this.acceleration.hashCode());
254         result = prime * result + ((this.id == null) ? 0 : this.id.hashCode());
255         result = prime * result + ((this.length == null) ? 0 : this.length.hashCode());
256         result = prime * result + ((this.objectType == null) ? 0 : this.objectType.hashCode());
257         result = prime * result + ((this.speed == null) ? 0 : this.speed.hashCode());
258         return result;
259     }
260 
261     /** {@inheritDoc} */
262     @Override
263     public boolean equals(final Object obj)
264     {
265         if (this == obj)
266         {
267             return true;
268         }
269         if (!super.equals(obj))
270         {
271             return false;
272         }
273         if (getClass() != obj.getClass())
274         {
275             return false;
276         }
277         AbstractHeadwayCopy other = (AbstractHeadwayCopy) obj;
278         if (this.acceleration == null)
279         {
280             if (other.acceleration != null)
281             {
282                 return false;
283             }
284         }
285         else if (!this.acceleration.equals(other.acceleration))
286         {
287             return false;
288         }
289         if (this.id == null)
290         {
291             if (other.id != null)
292             {
293                 return false;
294             }
295         }
296         else if (!this.id.equals(other.id))
297         {
298             return false;
299         }
300         if (this.length == null)
301         {
302             if (other.length != null)
303             {
304                 return false;
305             }
306         }
307         else if (!this.length.equals(other.length))
308         {
309             return false;
310         }
311         if (this.objectType != other.objectType)
312         {
313             return false;
314         }
315         if (this.speed == null)
316         {
317             if (other.speed != null)
318             {
319                 return false;
320             }
321         }
322         else if (!this.speed.equals(other.speed))
323         {
324             return false;
325         }
326         return true;
327     }
328 
329 }