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