View Javadoc
1   package org.opentrafficsim.road.gtu.lane.perception.headway;
2   
3   import java.util.EnumSet;
4   
5   import org.djunits.value.vdouble.scalar.Acceleration;
6   import org.djunits.value.vdouble.scalar.Length;
7   import org.djunits.value.vdouble.scalar.Speed;
8   import org.opentrafficsim.core.gtu.GTUException;
9   import org.opentrafficsim.core.gtu.GTUType;
10  
11  /**
12   * Container for a reference to information about a (lane based) GTU and a headway. The Headway can store information about GTUs
13   * or objects ahead of the reference GTU, behind the reference GTU, or (partially) parallel to the reference GTU. In addition to
14   * the (perceived) headway, several other pieces of information can be stored, such as (perceived) speed, (perceived)
15   * acceleration, (perceived) turn indicators, and (perceived) braking lights. <br>
16   * Special care must be taken in curves when perceiving headway of a GTU or object on an adjacent lane.The question is whether
17   * we perceive the parallel or ahead/behind based on a line perpendicular to the front/back of the GTU (rectangular), or
18   * perpendicular to the center line of the lane (wedge-shaped in case of a curve). The difficulty of a wedge-shaped situation is
19   * that reciprocity might be violated: in case of a clothoid, for instance, it is not sure that the point on the center line
20   * 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
21   * sharp bends. Therefore, algorithms implementing headway should only project the <i>reference point</i> of the reference GTU
22   * on the center line of the adjacent lane, and then calculate the forward position and backward position on the adjacent lane
23   * based on the reference point. Still, our human perception of what is parallel and what not, is not reflected by fractional
24   * positions. See examples in
25   * <a href= "http://simulation.tudelft.nl:8085/browse/OTS-113">http://simulation.tudelft.nl:8085/browse/OTS-113</a>.
26   * <p>
27   * Copyright (c) 2013-2017 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
28   * BSD-style license. See <a href="http://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
29   * <p>
30   * @version $Revision: 1368 $, $LastChangedDate: 2015-09-02 00:20:20 +0200 (Wed, 02 Sep 2015) $, by $Author: averbraeck $,
31   *          initial version 11 feb. 2015 <br>
32   * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
33   * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
34   */
35  public abstract class AbstractHeadwayGTU extends AbstractHeadwayCopy implements HeadwayGTU
36  {
37      /** */
38      private static final long serialVersionUID = 20160410L;
39  
40      /** The perceived GTU Type, or null if unknown. */
41      private final GTUType gtuType;
42  
43      /** Whether the GTU is facing the same direction. */
44      private final boolean facingSameDirection;
45  
46      /** The observable characteristics of the GTU. */
47      private final EnumSet<GTUStatus> gtuStatus = EnumSet.noneOf(GTUStatus.class);
48  
49      /**
50       * Construct a new Headway information object, for a moving GTU ahead of us or behind us.
51       * @param id the id of the GTU for comparison purposes, can not be null.
52       * @param gtuType the perceived GTU Type, or null if unknown.
53       * @param distance the distance to the other object; if this constructor is used, distance cannot be null.
54       * @param facingSameDirection whether the GTU is facing the same direction.
55       * @param length the (perceived) length of the other object; can not be null.
56       * @param speed the (perceived) speed of the other object; can be null if unknown.
57       * @param acceleration the (perceived) acceleration of the other object; can be null if unknown.
58       * @param gtuStatus the observable characteristics of the GTU.
59       * @throws GTUException when id is null, objectType is null, or parameters are inconsistent
60       */
61      @SuppressWarnings("checkstyle:parameternumber")
62      public AbstractHeadwayGTU(final String id, final GTUType gtuType, final Length distance, final boolean facingSameDirection,
63              final Length length, final Speed speed, final Acceleration acceleration, final GTUStatus... gtuStatus)
64              throws GTUException
65      {
66          super(ObjectType.GTU, id, distance, length, speed, acceleration);
67          this.facingSameDirection = facingSameDirection;
68          this.gtuType = gtuType;
69          for (GTUStatus status : gtuStatus)
70          {
71              this.gtuStatus.add(status);
72          }
73      }
74  
75      /**
76       * Construct a new Headway information object, for a non-moving GTU ahead of us or behind us.
77       * @param id String; the id of the GTU for comparison purposes, can not be null.
78       * @param gtuType GTUType; the perceived GTU Type, or null if unknown.
79       * @param distance Length; the distance to the other GTU; if this constructor is used, distance cannot be null.
80       * @param facingSameDirection whether the GTU is facing the same direction.
81       * @param length the (perceived) length of the other object; can not be null.
82       * @param gtuStatus the observable characteristics of the GTU.
83       * @throws GTUException when id is null, or parameters are inconsistent
84       */
85      public AbstractHeadwayGTU(final String id, final GTUType gtuType, final Length distance, final boolean facingSameDirection,
86              final Length length, final GTUStatus... gtuStatus) throws GTUException
87      {
88          super(ObjectType.GTU, id, distance, length);
89          this.facingSameDirection = facingSameDirection;
90          this.gtuType = gtuType;
91          for (GTUStatus status : gtuStatus)
92          {
93              this.gtuStatus.add(status);
94          }
95      }
96  
97      /**
98       * Construct a new Headway information object, for a moving GTU parallel with us.
99       * @param id the id of the GTU for comparison purposes, can not be null.
100      * @param gtuType the perceived GTU Type, or null if unknown.
101      * @param overlapFront the front-front distance to the other GTU; if this constructor is used, this value cannot be null.
102      * @param overlap the 'center' overlap with the other GTU; if this constructor is used, this value cannot be null.
103      * @param overlapRear the rear-rear distance to the other GTU; if this constructor is used, this value cannot be null.
104      * @param facingSameDirection whether the GTU is facing the same direction.
105      * @param length the (perceived) length of the other object; can not be null.
106      * @param speed the (perceived) speed of the other GTU; can be null if unknown.
107      * @param acceleration the (perceived) acceleration of the other GTU; can be null if unknown.
108      * @param gtuStatus the observable characteristics of the GTU.
109      * @throws GTUException when id is null, or parameters are inconsistent
110      */
111     @SuppressWarnings("checkstyle:parameternumber")
112     public AbstractHeadwayGTU(final String id, final GTUType gtuType, final Length overlapFront, final Length overlap,
113             final Length overlapRear, final boolean facingSameDirection, final Length length, final Speed speed,
114             final Acceleration acceleration, final GTUStatus... gtuStatus) throws GTUException
115     {
116         super(ObjectType.GTU, id, overlapFront, overlap, overlapRear, length, speed, acceleration);
117         this.facingSameDirection = facingSameDirection;
118         this.gtuType = gtuType;
119         for (GTUStatus status : gtuStatus)
120         {
121             this.gtuStatus.add(status);
122         }
123     }
124 
125     /**
126      * Construct a new Headway information object, for a non-moving GTU parallel with us.
127      * @param id the id of the GTU for comparison purposes, can not be null.
128      * @param gtuType the perceived GTU Type, or null if unknown.
129      * @param overlapFront the front-front distance to the other GTU; if this constructor is used, this value cannot be null.
130      * @param overlap the 'center' overlap with the other GTU; if this constructor is used, this value cannot be null.
131      * @param overlapRear the rear-rear distance to the other GTU; if this constructor is used, this value cannot be null.
132      * @param facingSameDirection whether the GTU is facing the same direction.
133      * @param length the (perceived) length of the other object; can not be null.
134      * @param gtuStatus the observable characteristics of the GTU.
135      * @throws GTUException when id is null, or parameters are inconsistent
136      */
137     @SuppressWarnings("checkstyle:parameternumber")
138     public AbstractHeadwayGTU(final String id, final GTUType gtuType, final Length overlapFront, final Length overlap,
139             final Length overlapRear, final boolean facingSameDirection, final Length length, final GTUStatus... gtuStatus)
140             throws GTUException
141     {
142         super(ObjectType.GTU, id, overlapFront, overlap, overlapRear, length);
143         this.facingSameDirection = facingSameDirection;
144         this.gtuType = gtuType;
145         for (GTUStatus status : gtuStatus)
146         {
147             this.gtuStatus.add(status);
148         }
149     }
150 
151     /**
152      * @return gtuType
153      */
154     public final GTUType getGtuType()
155     {
156         return this.gtuType;
157     }
158 
159     /**
160      * @return facingSameDirection
161      */
162     public final boolean isFacingSameDirection()
163     {
164         return this.facingSameDirection;
165     }
166 
167     /** @return were the braking lights on? */
168     public final boolean isBrakingLightsOn()
169     {
170         return this.gtuStatus.contains(GTUStatus.BRAKING_LIGHTS);
171     }
172 
173     /** @return was the left turn indicator on? */
174     public final boolean isLeftTurnIndicatorOn()
175     {
176         return this.gtuStatus.contains(GTUStatus.LEFT_TURNINDICATOR);
177     }
178 
179     /** @return was the right turn indicator on? */
180     public final boolean isRightTurnIndicatorOn()
181     {
182         return this.gtuStatus.contains(GTUStatus.RIGHT_TURNINDICATOR);
183     }
184 
185     /** @return were the emergency lights on? */
186     public final boolean isEmergencyLightsOn()
187     {
188         return this.gtuStatus.contains(GTUStatus.EMERGENCY_LIGHTS);
189     }
190 
191     /** @return was the vehicle honking or ringing its bell when being observed for the headway? */
192     public final boolean isHonking()
193     {
194         return this.gtuStatus.contains(GTUStatus.HONK);
195     }
196 
197     /**
198      * For subclasses that create a copy of themselves.
199      * @return set of gtu status
200      */
201     protected final GTUStatus[] getGtuStatus()
202     {
203         return this.gtuStatus.toArray(new GTUStatus[this.gtuStatus.size()]);
204     }
205 
206     /** {@inheritDoc} */
207     @Override
208     @SuppressWarnings("checkstyle:designforextension")
209     public String toString()
210     {
211         return "AbstractHeadwayGTU [gtuType=" + this.gtuType + ", gtuStatus=" + this.gtuStatus + ", getSpeed()="
212                 + this.getSpeed() + ", getDistance()=" + this.getDistance() + ", getAcceleration()=" + this.getAcceleration()
213                 + "]";
214     }
215 
216 }