View Javadoc
1   package org.opentrafficsim.road.network.sampling.data;
2   
3   import org.djunits.unit.SpeedUnit;
4   import org.djunits.value.vfloat.scalar.FloatSpeed;
5   import org.opentrafficsim.core.gtu.GtuException;
6   import org.opentrafficsim.core.network.NetworkException;
7   import org.opentrafficsim.kpi.sampling.data.ExtendedDataSpeed;
8   import org.opentrafficsim.road.gtu.lane.LaneBasedGtu;
9   import org.opentrafficsim.road.network.sampling.GtuDataRoad;
10  
11  /**
12   * Reference speed for trajectories.
13   * <p>
14   * Copyright (c) 2013-2023 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
15   * BSD-style license. See <a href="https://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
16   * </p>
17   * @author <a href="https://github.com/averbraeck">Alexander Verbraeck</a>
18   * @author <a href="https://tudelft.nl/staff/p.knoppers-1">Peter Knoppers</a>
19   * @author <a href="https://dittlab.tudelft.nl">Wouter Schakel</a>
20   */
21  public class ReferenceSpeed extends ExtendedDataSpeed<GtuDataRoad>
22  {
23  
24      /** Single instance. */
25      public static final ReferenceSpeed INSTANCE = new ReferenceSpeed();
26  
27      /**
28       * 
29       */
30      public ReferenceSpeed()
31      {
32          super("referenceSpeed", "Reference speed (minimum of speed limit and maximum vehicle speed)");
33      }
34  
35      /** {@inheritDoc} */
36      @Override
37      public final FloatSpeed getValue(final GtuDataRoad gtu)
38      {
39          LaneBasedGtu gtuObj = gtu.getGtu();
40          try
41          {
42              double v1 = gtuObj.getReferencePosition().getLane().getSpeedLimit(gtuObj.getType()).si;
43              double v2 = gtuObj.getMaximumSpeed().si;
44              return new FloatSpeed(v1 < v2 ? v1 : v2, SpeedUnit.SI);
45          }
46          catch (GtuException exception)
47          {
48              // GTU was destroyed and is without a reference location
49              return new FloatSpeed(Double.NaN, SpeedUnit.SI);
50          }
51          catch (NetworkException exception)
52          {
53              throw new RuntimeException("Could not obtain reference speed from GTU " + gtuObj, exception);
54          }
55      }
56  
57      /** {@inheritDoc} */
58      @Override
59      public final String toString()
60      {
61          return "Reference Speed";
62      }
63  
64  }