View Javadoc
1   package org.opentrafficsim.road.network.sampling.data;
2   
3   import java.util.HashMap;
4   import java.util.HashSet;
5   import java.util.Map;
6   import java.util.Set;
7   
8   import org.djunits.unit.DurationUnit;
9   import org.djunits.value.vdouble.scalar.Length;
10  import org.djunits.value.vdouble.scalar.Speed;
11  import org.djunits.value.vdouble.scalar.Time;
12  import org.djunits.value.vfloat.scalar.FloatDuration;
13  import org.opentrafficsim.core.gtu.GTUDirectionality;
14  import org.opentrafficsim.core.gtu.GTUException;
15  import org.opentrafficsim.core.gtu.RelativePosition;
16  import org.opentrafficsim.kpi.sampling.data.ExtendedDataTypeDuration;
17  import org.opentrafficsim.road.gtu.lane.LaneBasedGTU;
18  import org.opentrafficsim.road.network.lane.DirectedLanePosition;
19  import org.opentrafficsim.road.network.lane.Lane;
20  import org.opentrafficsim.road.network.lane.LaneDirection;
21  import org.opentrafficsim.road.network.sampling.GtuData;
22  
23  /**
24   * Time-to-collision for trajectories.
25   * <p>
26   * Copyright (c) 2013-2019 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
27   * BSD-style license. See <a href="http://opentrafficsim.org/node/13">OpenTrafficSim License</a>.
28   * <p>
29   * @version $Revision$, $LastChangedDate$, by $Author$, initial version 28 feb. 2017 <br>
30   * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
31   * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
32   * @author <a href="http://www.transport.citg.tudelft.nl">Wouter Schakel</a>
33   */
34  public class TimeToCollision extends ExtendedDataTypeDuration<GtuData>
35  {
36  
37      /**
38       * 
39       */
40      public TimeToCollision()
41      {
42          super("timeToCollision");
43      }
44  
45      /** {@inheritDoc} */
46      @Override
47      public final FloatDuration getValue(final GtuData gtu)
48      {
49          LaneBasedGTU gtuObj = gtu.getGtu();
50          try
51          {
52              DirectedLanePosition ref = gtuObj.getReferencePosition();
53              Map<Lane, GTUDirectionality> map = new HashMap<>();
54              Set<LaneDirection> visited = new HashSet<>();
55              map.put(ref.getLane(), ref.getGtuDirection());
56              Length pos = ref.getPosition();
57              Length cumulDist = Length.ZERO; // from start of lane
58              Time now = gtuObj.getSimulator().getSimulatorTime();
59              LaneBasedGTU next = null;
60              while (map.size() == 1)
61              {
62                  Lane lane = map.keySet().iterator().next();
63                  GTUDirectionality dir = map.get(lane);
64                  if (cumulDist.gt0())
65                  {
66                      pos = dir.isPlus() ? Length.ZERO : lane.getLength();
67                  }
68                  next = lane.getGtuAhead(pos, dir, RelativePosition.REAR, now);
69                  if (next == null)
70                  {
71                      LaneDirection laneDir = new LaneDirection(lane, map.get(lane));
72                      if (visited.contains(laneDir))
73                      {
74                          break;
75                      }
76                      visited.add(laneDir);
77                      cumulDist = cumulDist.plus(lane.getLength());
78                      map = lane.downstreamLanes(dir, gtuObj.getGTUType());
79                  }
80                  else
81                  {
82                      // gtu found, calculate TTC
83                      if (next.getSpeed().ge(gtuObj.getSpeed()))
84                      {
85                          return new FloatDuration(Double.NaN, DurationUnit.SI);
86                      }
87                      Length ownPos = gtuObj.position(ref.getLane(), gtuObj.getFront());
88                      Length nextPos = next.position(lane, next.getRear());
89                      Length dist = nextPos.minus(ownPos).plus(cumulDist);
90                      Speed dv = gtuObj.getSpeed().minus(next.getSpeed());
91                      return new FloatDuration(dist.si / dv.si, DurationUnit.SI);
92                  }
93              }
94              return FloatDuration.NaN;
95          }
96          catch (GTUException exception)
97          {
98              // GTU was destroyed and is without a reference location
99              return FloatDuration.NaN;
100         }
101     }
102 
103     /** {@inheritDoc} */
104     @Override
105     public final String toString()
106     {
107         return "TTC";
108     }
109 
110 }