TimeToCollision.java

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

  2. import java.util.LinkedHashSet;
  3. import java.util.Set;

  4. import org.djunits.unit.DurationUnit;
  5. import org.djunits.value.vdouble.scalar.Length;
  6. import org.djunits.value.vdouble.scalar.Speed;
  7. import org.djunits.value.vdouble.scalar.Time;
  8. import org.djunits.value.vfloat.scalar.FloatDuration;
  9. import org.opentrafficsim.core.gtu.GtuException;
  10. import org.opentrafficsim.core.gtu.RelativePosition;
  11. import org.opentrafficsim.kpi.sampling.data.ExtendedDataDuration;
  12. import org.opentrafficsim.road.gtu.lane.LaneBasedGtu;
  13. import org.opentrafficsim.road.network.lane.Lane;
  14. import org.opentrafficsim.road.network.lane.LanePosition;
  15. import org.opentrafficsim.road.network.sampling.GtuDataRoad;

  16. /**
  17.  * Time-to-collision for trajectories.
  18.  * <p>
  19.  * Copyright (c) 2013-2023 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
  20.  * BSD-style license. See <a href="https://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
  21.  * </p>
  22.  * @author <a href="https://github.com/averbraeck">Alexander Verbraeck</a>
  23.  * @author <a href="https://tudelft.nl/staff/p.knoppers-1">Peter Knoppers</a>
  24.  * @author <a href="https://dittlab.tudelft.nl">Wouter Schakel</a>
  25.  */
  26. public class TimeToCollision extends ExtendedDataDuration<GtuDataRoad>
  27. {

  28.     /**
  29.      *
  30.      */
  31.     public TimeToCollision()
  32.     {
  33.         super("timeToCollision", "Time to collision");
  34.     }

  35.     /** {@inheritDoc} */
  36.     @Override
  37.     public final FloatDuration getValue(final GtuDataRoad gtu)
  38.     {
  39.         LaneBasedGtu gtuObj = gtu.getGtu();
  40.         try
  41.         {
  42.             LanePosition ref = gtuObj.getReferencePosition();
  43.             Set<Lane> set = new LinkedHashSet<>();
  44.             Set<Lane> visited = new LinkedHashSet<>();
  45.             set.add(ref.getLane());
  46.             Length pos = ref.getPosition();
  47.             Length cumulDist = Length.ZERO; // from start of lane
  48.             Time now = gtuObj.getSimulator().getSimulatorAbsTime();
  49.             LaneBasedGtu next = null;
  50.             while (set.size() == 1)
  51.             {
  52.                 Lane lane = set.iterator().next();
  53.                 if (cumulDist.gt0())
  54.                 {
  55.                     pos = Length.ZERO;
  56.                 }
  57.                 next = lane.getGtuAhead(pos, RelativePosition.REAR, now);
  58.                 if (next == null)
  59.                 {
  60.                     if (visited.contains(lane))
  61.                     {
  62.                         break;
  63.                     }
  64.                     visited.add(lane);
  65.                     cumulDist = cumulDist.plus(lane.getLength());
  66.                     set = new LinkedHashSet<>(lane.nextLanes(gtuObj.getType()));
  67.                 }
  68.                 else
  69.                 {
  70.                     // gtu found, calculate TTC
  71.                     if (next.getSpeed().ge(gtuObj.getSpeed()))
  72.                     {
  73.                         return new FloatDuration(Double.NaN, DurationUnit.SI);
  74.                     }
  75.                     Length ownPos = gtuObj.position(ref.getLane(), gtuObj.getFront());
  76.                     Length nextPos = next.position(lane, next.getRear());
  77.                     Length dist = nextPos.minus(ownPos).plus(cumulDist);
  78.                     Speed dv = gtuObj.getSpeed().minus(next.getSpeed());
  79.                     return new FloatDuration(dist.si / dv.si, DurationUnit.SI);
  80.                 }
  81.             }
  82.             return FloatDuration.NaN;
  83.         }
  84.         catch (GtuException exception)
  85.         {
  86.             // GTU was destroyed and is without a reference location
  87.             return FloatDuration.NaN;
  88.         }
  89.     }

  90.     /** {@inheritDoc} */
  91.     @Override
  92.     public final String toString()
  93.     {
  94.         return "TTC";
  95.     }

  96. }