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.base.parameters.Parameters;
9   import org.opentrafficsim.core.gtu.GtuException;
10  import org.opentrafficsim.core.gtu.GtuType;
11  import org.opentrafficsim.core.network.NetworkException;
12  import org.opentrafficsim.core.network.route.Route;
13  import org.opentrafficsim.road.gtu.lane.LaneBasedGtu;
14  import org.opentrafficsim.road.gtu.lane.tactical.following.CarFollowingModel;
15  import org.opentrafficsim.road.network.speed.SpeedLimitInfo;
16  import org.opentrafficsim.road.network.speed.SpeedLimitTypes;
17  
18  /**
19   * Container for a reference to information about a (lane based) GTU and a headway. The Headway can store information about GTUs
20   * or objects ahead of the reference GTU, behind the reference GTU, or (partially) parallel to the reference GTU. In addition to
21   * the (perceived) headway, several other pieces of information can be stored, such as (perceived) speed, (perceived)
22   * acceleration, (perceived) turn indicators, and (perceived) braking lights. <br>
23   * This particular version returns behavioral information about the observed GTU objects based on their real state.<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://tudelft.nl/staff/p.knoppers-1">Peter Knoppers</a>
39   * @author <a href="https://github.com/wjschakel">Wouter Schakel</a>
40   */
41  public class HeadwayGtuReal extends AbstractHeadway implements HeadwayGtu
42  {
43      /** */
44      private static final long serialVersionUID = 20170324L;
45  
46      /** Stored speed limit info of the observed GTU. */
47      private SpeedLimitInfo speedLimitInfo;
48  
49      /** Wrapped GTU. */
50      private final LaneBasedGtu gtu;
51  
52      /** Whether the GTU is facing the same direction. */
53      private final boolean facingSameDirection;
54      
55      /**
56       * Construct a new Headway information object, for a GTU ahead of us or behind us.
57       * @param gtu LaneBasedGtu; the observed GTU, can not be null.
58       * @param distance the distance to the other object; if this constructor is used, distance cannot be null.
59       * @param facingSameDirection boolean; whether the GTU is facing the same direction.
60       * @throws GtuException when id is null, objectType is null, or parameters are inconsistent
61       */
62      public HeadwayGtuReal(final LaneBasedGtu gtu, final Length distance, final boolean facingSameDirection) throws GtuException
63      {
64          super(distance);
65          this.gtu = gtu;
66          this.facingSameDirection = facingSameDirection;
67      }
68  
69      /**
70       * Construct a new Headway information object, for a GTU parallel with us.
71       * @param gtu LaneBasedGtu; the observed GTU, can not be null.
72       * @param overlapFront the front-front distance to the other Gtu; if this constructor is used, this value cannot be null.
73       * @param overlap the 'center' overlap with the other Gtu; if this constructor is used, this value cannot be null.
74       * @param overlapRear the rear-rear distance to the other Gtu; if this constructor is used, this value cannot be null.
75       * @param facingSameDirection boolean; whether the GTU is facing the same direction.
76       * @throws GtuException when id is null, or parameters are inconsistent
77       */
78      public HeadwayGtuReal(final LaneBasedGtu gtu, final Length overlapFront, final Length overlap, final Length overlapRear,
79              final boolean facingSameDirection) throws GtuException
80      {
81          super(overlapFront, overlap, overlapRear);
82          this.gtu = gtu;
83          this.facingSameDirection = facingSameDirection;
84      }
85  
86      /**
87       * Creates speed limit prospect for given GTU.
88       * @param wrappedGtu LaneBasedGtu; gtu to the the speed limit prospect for
89       * @return speed limit prospect for given GTU
90       */
91      private SpeedLimitInfo getSpeedLimitInfo(final LaneBasedGtu wrappedGtu)
92      {
93          SpeedLimitInfo sli = new SpeedLimitInfo();
94          sli.addSpeedInfo(SpeedLimitTypes.MAX_VEHICLE_SPEED, wrappedGtu.getMaximumSpeed());
95          try
96          {
97              sli.addSpeedInfo(SpeedLimitTypes.FIXED_SIGN,
98                      wrappedGtu.getReferencePosition().lane().getSpeedLimit(wrappedGtu.getType()));
99          }
100         catch (NetworkException | GtuException exception)
101         {
102             throw new RuntimeException("Could not obtain speed limit from lane for perception.", exception);
103         }
104         return sli;
105     }
106 
107     /** {@inheritDoc} */
108     @Override
109     public final CarFollowingModel getCarFollowingModel()
110     {
111         return this.gtu.getTacticalPlanner().getCarFollowingModel();
112     }
113 
114     /** {@inheritDoc} */
115     @Override
116     public final Parameters getParameters()
117     {
118         return this.gtu.getParameters();
119     }
120 
121     /** {@inheritDoc} */
122     @Override
123     public final SpeedLimitInfo getSpeedLimitInfo()
124     {
125         if (this.speedLimitInfo == null)
126         {
127             this.speedLimitInfo = getSpeedLimitInfo(this.gtu);
128         }
129         return this.speedLimitInfo;
130     }
131 
132     /** {@inheritDoc} */
133     @Override
134     public final Route getRoute()
135     {
136         return this.gtu.getStrategicalPlanner().getRoute();
137     }
138 
139     /**
140      * {@inheritDoc} <br>
141      * <br>
142      * <b>Note: when moving a {@code HeadwayGtuRealDirect}, only headway, speed and acceleration may be considered to be delayed
143      * and anticipated. Other information is taken from the actual GTU at the time {@code moved()} is called.</b>
144      */
145     @Override
146     public final HeadwayGtu moved(final Length headway, final Speed speed, final Acceleration acceleration)
147     {
148         try
149         {
150             return new HeadwayGtuRealCopy(getId(), getGtuType(), headway, getLength(), getWidth(), speed, acceleration,
151                     getCarFollowingModel(), getParameters(), getSpeedLimitInfo(), getRoute(), getDesiredSpeed(),
152                     getGtuStatus());
153         }
154         catch (GtuException exception)
155         {
156             // input should be consistent
157             throw new RuntimeException("Exception while copying Headway GTU.", exception);
158         }
159     }
160 
161     /**
162      * Returns an array with GTU status.
163      * @return array with GTU status
164      */
165     private GtuStatus[] getGtuStatus()
166     {
167         EnumSet<GtuStatus> gtuStatus = EnumSet.noneOf(GtuStatus.class);
168         if (isLeftTurnIndicatorOn())
169         {
170             gtuStatus.add(GtuStatus.LEFT_TURNINDICATOR);
171         }
172         if (isRightTurnIndicatorOn())
173         {
174             gtuStatus.add(GtuStatus.RIGHT_TURNINDICATOR);
175         }
176         if (isBrakingLightsOn())
177         {
178             gtuStatus.add(GtuStatus.BRAKING_LIGHTS);
179         }
180         if (isEmergencyLightsOn())
181         {
182             gtuStatus.add(GtuStatus.EMERGENCY_LIGHTS);
183         }
184         if (isHonking())
185         {
186             gtuStatus.add(GtuStatus.HONK);
187         }
188         return gtuStatus.toArray(new GtuStatus[gtuStatus.size()]);
189     }
190 
191     /** {@inheritDoc} */
192     @Override
193     public final String getId()
194     {
195         return this.gtu.getId();
196     }
197 
198     /** {@inheritDoc} */
199     @Override
200     public final Length getLength()
201     {
202         return this.gtu.getLength();
203     }
204 
205     /** {@inheritDoc} */
206     @Override
207     public Length getWidth()
208     {
209         return this.gtu.getWidth();
210     }
211 
212     /** {@inheritDoc} */
213     @Override
214     public final Speed getSpeed()
215     {
216         return this.gtu.getSpeed();
217     }
218 
219     /** {@inheritDoc} */
220     @Override
221     public Speed getDesiredSpeed()
222     {
223         return this.gtu.getDesiredSpeed();
224     }
225 
226     /** {@inheritDoc} */
227     @Override
228     public final ObjectType getObjectType()
229     {
230         return ObjectType.GTU;
231     }
232 
233     /** {@inheritDoc} */
234     @Override
235     public final Acceleration getAcceleration()
236     {
237         return this.gtu.getAcceleration();
238     }
239 
240     /** {@inheritDoc} */
241     @Override
242     public final GtuType getGtuType()
243     {
244         return this.gtu.getType();
245     }
246 
247     /** {@inheritDoc} */
248     @Override
249     public final boolean isFacingSameDirection()
250     {
251         return this.facingSameDirection;
252     }
253 
254     /** {@inheritDoc} */
255     @Override
256     public final boolean isBrakingLightsOn()
257     {
258         // TODO
259         return false;
260     }
261 
262     /** {@inheritDoc} */
263     @Override
264     public final boolean isLeftTurnIndicatorOn()
265     {
266         return this.gtu.getTurnIndicatorStatus().isLeft();
267     }
268 
269     /** {@inheritDoc} */
270     @Override
271     public final boolean isRightTurnIndicatorOn()
272     {
273         return this.gtu.getTurnIndicatorStatus().isRight();
274     }
275 
276     /** {@inheritDoc} */
277     @Override
278     public final boolean isEmergencyLightsOn()
279     {
280         return this.gtu.getTurnIndicatorStatus().isHazard();
281     }
282 
283     /** {@inheritDoc} */
284     @Override
285     public final boolean isHonking()
286     {
287         // TODO
288         return false;
289     }
290 
291     /** {@inheritDoc} */
292     @Override
293     public final String toString()
294     {
295         return "HeadwayGtuReal [speedLimitInfo=" + this.speedLimitInfo + ", gtu=" + this.gtu + ", facingSameDirection="
296                 + this.facingSameDirection + "]";
297     }
298 
299 }