View Javadoc
1   package org.opentrafficsim.road.gtu.lane.perception;
2   
3   import java.util.HashSet;
4   import java.util.Set;
5   
6   import org.djunits.value.vdouble.scalar.Length;
7   import org.opentrafficsim.base.parameters.ParameterException;
8   import org.opentrafficsim.core.gtu.GTUException;
9   import org.opentrafficsim.core.gtu.RelativePosition;
10  import org.opentrafficsim.road.gtu.lane.LaneBasedGTU;
11  import org.opentrafficsim.road.gtu.lane.perception.categories.neighbors.HeadwayGtuType;
12  import org.opentrafficsim.road.gtu.lane.perception.headway.HeadwayGTU;
13  
14  /**
15   * Iterable to find downstream GTU's.<br>
16   * <br>
17   * The behavior of this search is slightly altered using {@code boolean ignoreIfUpstream}. This is to deal with the following
18   * situations in case a GTU with it's rear upstream of the considered lane is found:<br>
19   * <br>
20   * Following downstream GTUs ({@code ignoreIfUpstream = true})
21   * <ol>
22   * <li>From the same direction (or not a merge): the GTU can be ignored as it is also found on the upstream lane.</li>
23   * <li>From the other direction of a merge: the GTU can be ignored as it is followed through considering the merge conflict.
24   * Note that we cannot follow the GTU in a regular fashion. If the rear of the GTU is upstream of the conflict, the subject GTU
25   * can move up to the conflict without hitting the GTU from the other direction. Considering the GTU through the conflict deals
26   * with this, and the GTU can be ignored for regular following of downstream GTUs.</li>
27   * </ol>
28   * <br>
29   * GTUs downstream of a conflict ({@code ignoreIfUpstream = false})
30   * <ol>
31   * <li>From the same direction: the GTU is considered both through the conflict and as a regular downstream GTU.</li>
32   * <li>From the other direction of a merge: the GTU needs to be considered.</li>
33   * </ol>
34   * <p>
35   * Copyright (c) 2013-2019 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
36   * BSD-style license. See <a href="http://opentrafficsim.org/node/13">OpenTrafficSim License</a>.
37   * <p>
38   * @version $Revision$, $LastChangedDate$, by $Author$, initial version 19 feb. 2018 <br>
39   * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
40   * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
41   * @author <a href="http://www.transport.citg.tudelft.nl">Wouter Schakel</a>
42   */
43  public class DownstreamNeighborsIterable extends AbstractPerceptionIterable<HeadwayGTU, LaneBasedGTU, Integer>
44  {
45  
46      /** Margin in case of a left lane. */
47      private static final Length LEFT = Length.createSI(-0.000001);
48  
49      /** Margin in case of a right lane. */
50      private static final Length RIGHT = Length.createSI(0.000001);
51  
52      /** Headway GTU type that should be used. */
53      private final HeadwayGtuType headwayGtuType;
54  
55      /** Added GTU's so far. */
56      private final Set<String> ids = new HashSet<>();
57  
58      /**
59       * Margin used for neighbor search in some cases to prevent possible deadlock. This does not affect calculated distances to
60       * neighbors, but only whether they are considered a leader or follower.
61       */
62      private final Length margin;
63  
64      /** Ignore downstream GTU's if their rear is upstream of the lane start. Note that equal GTU id's are always ignored. */
65      private final boolean ignoreIfUpstream;
66  
67      /**
68       * Constructor.
69       * @param perceivingGtu LaneBasedGTU; perceiving GTU
70       * @param root LaneRecord&lt;?&gt;; root record
71       * @param initialPosition Length; position on the root record
72       * @param maxDistance Length; maximum distance to search
73       * @param relativePosition RelativePosition; position to which distance are calculated by subclasses
74       * @param headwayGtuType HeadwayGtuType; type of HeadwayGTU to return
75       * @param lane RelativeLane; relative lane (used for a left/right distinction to prevent dead-locks)
76       * @param ignoreIfUpstream boolean; whether to ignore GTU that are partially upstream of a record
77       */
78      public DownstreamNeighborsIterable(final LaneBasedGTU perceivingGtu, final LaneRecord<?> root, final Length initialPosition,
79              final Length maxDistance, final RelativePosition relativePosition, final HeadwayGtuType headwayGtuType,
80              final RelativeLane lane, final boolean ignoreIfUpstream)
81      {
82          super(perceivingGtu, root, initialPosition, true, maxDistance, relativePosition, null);
83          this.headwayGtuType = headwayGtuType;
84          this.margin = lane.getLateralDirectionality().isLeft() ? LEFT : RIGHT;
85          this.ids.add(perceivingGtu.getId());
86          this.ignoreIfUpstream = ignoreIfUpstream;
87      }
88  
89      /** {@inheritDoc} */
90      @Override
91      protected Entry getNext(final LaneRecord<?> record, final Length position, final Integer counter) throws GTUException
92      {
93          int n;
94          LaneBasedGTU next;
95          Length pos;
96          boolean plus = record.getDirection().isPlus();
97          if (counter == null)
98          {
99              Length searchPos = (plus ? position.plus(this.margin) : position.minus(this.margin));
100             next = record.getLane().getGtuAhead(searchPos, record.getDirection(), RelativePosition.FRONT,
101                     record.getLane().getParentLink().getSimulator().getSimulatorTime());
102             if (next == null)
103             {
104                 return null;
105             }
106             n = record.getLane().indexOfGtu(next);
107             pos = next.position(record.getLane(), next.getRear());
108 
109             if (this.ids.contains(next.getId()))
110             {
111                 // rear still on previous lane; it is found there, get next gtu
112                 pos = plus ? pos.plus(next.getLength()) : pos.minus(next.getLength());
113                 return getNext(record, pos, n);
114             }
115             if (this.ignoreIfUpstream)
116             {
117                 if (plus ? pos.si < 0.0 : pos.si > record.getLane().getLength().si)
118                 {
119                     pos = plus ? pos.plus(next.getLength()) : pos.minus(next.getLength());
120                     return getNext(record, pos, n);
121                 }
122             }
123         }
124         else
125         {
126             n = plus ? counter + 1 : counter - 1;
127             if (n < 0 || n >= record.getLane().numberOfGtus())
128             {
129                 return null;
130             }
131             next = record.getLane().getGtu(n);
132             pos = next.position(record.getLane(), next.getRear());
133         }
134         return new Entry(next, n, pos);
135     }
136 
137     /** {@inheritDoc} */
138     @Override
139     protected Length getDistance(final LaneBasedGTU object, final LaneRecord<?> record, final Length position)
140     {
141         return record.getDistanceToPosition(position).minus(getDx());
142     }
143 
144     /** {@inheritDoc} */
145     @Override
146     public HeadwayGTU perceive(final LaneBasedGTU perceivingGtu, final LaneBasedGTU object, final Length distance)
147             throws GTUException, ParameterException
148     {
149         return this.headwayGtuType.createDownstreamGtu(perceivingGtu, object, distance);
150     }
151 
152 }