ReferenceSpeed.java

  1. package org.opentrafficsim.road.network.sampling.data;

  2. import org.djunits.unit.SpeedUnit;
  3. import org.djunits.value.vfloat.scalar.FloatSpeed;
  4. import org.opentrafficsim.core.gtu.GTUException;
  5. import org.opentrafficsim.core.network.NetworkException;
  6. import org.opentrafficsim.kpi.sampling.data.ExtendedDataTypeSpeed;
  7. import org.opentrafficsim.road.gtu.lane.LaneBasedGTU;
  8. import org.opentrafficsim.road.network.sampling.GtuData;

  9. /**
  10.  * Reference speed for trajectories.
  11.  * <p>
  12.  * Copyright (c) 2013-2020 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
  13.  * BSD-style license. See <a href="http://opentrafficsim.org/node/13">OpenTrafficSim License</a>.
  14.  * <p>
  15.  * @version $Revision$, $LastChangedDate$, by $Author$, initial version 21 nov. 2016 <br>
  16.  * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
  17.  * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
  18.  * @author <a href="http://www.transport.citg.tudelft.nl">Wouter Schakel</a>
  19.  */
  20. public class ReferenceSpeed extends ExtendedDataTypeSpeed<GtuData>
  21. {

  22.     /** Single instance. */
  23.     public static final ReferenceSpeed INSTANCE = new ReferenceSpeed();

  24.     /**
  25.      *
  26.      */
  27.     public ReferenceSpeed()
  28.     {
  29.         super("referenceSpeed");
  30.     }

  31.     /** {@inheritDoc} */
  32.     @Override
  33.     public final FloatSpeed getValue(final GtuData gtu)
  34.     {
  35.         LaneBasedGTU gtuObj = gtu.getGtu();
  36.         try
  37.         {
  38.             double v1 = gtuObj.getReferencePosition().getLane().getSpeedLimit(gtuObj.getGTUType()).si;
  39.             double v2 = gtuObj.getMaximumSpeed().si;
  40.             return new FloatSpeed(v1 < v2 ? v1 : v2, SpeedUnit.SI);
  41.         }
  42.         catch (GTUException exception)
  43.         {
  44.             // GTU was destroyed and is without a reference location
  45.             return new FloatSpeed(Double.NaN, SpeedUnit.SI);
  46.         }
  47.         catch (NetworkException exception)
  48.         {
  49.             throw new RuntimeException("Could not obtain reference speed from GTU " + gtuObj, exception);
  50.         }
  51.     }

  52.     /** {@inheritDoc} */
  53.     @Override
  54.     public final String toString()
  55.     {
  56.         return "Reference Speed";
  57.     }

  58. }