View Javadoc
1   package org.opentrafficsim.road.gtu.lane.perception;
2   
3   import java.util.LinkedHashSet;
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  import org.opentrafficsim.road.gtu.lane.perception.structure.LaneRecordInterface;
14  
15  /**
16   * Iterable to find downstream GTU's.<br>
17   * <br>
18   * The behavior of this search is slightly altered using {@code boolean ignoreIfUpstream}. This is to deal with the following
19   * situations in case a GTU with it's rear upstream of the considered lane is found:<br>
20   * <br>
21   * Following downstream GTUs ({@code ignoreIfUpstream = true})
22   * <ol>
23   * <li>From the same direction (or not a merge): the GTU can be ignored as it is also found on the upstream lane.</li>
24   * <li>From the other direction of a merge: the GTU can be ignored as it is followed through considering the merge conflict.
25   * 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
26   * can move up to the conflict without hitting the GTU from the other direction. Considering the GTU through the conflict deals
27   * with this, and the GTU can be ignored for regular following of downstream GTUs.</li>
28   * </ol>
29   * <br>
30   * GTUs downstream of a conflict ({@code ignoreIfUpstream = false})
31   * <ol>
32   * <li>From the same direction: the GTU is considered both through the conflict and as a regular downstream GTU.</li>
33   * <li>From the other direction of a merge: the GTU needs to be considered.</li>
34   * </ol>
35   * <p>
36   * Copyright (c) 2013-2024 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
37   * BSD-style license. See <a href="https://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
38   * </p>
39   * @author <a href="https://github.com/averbraeck">Alexander Verbraeck</a>
40   * @author <a href="https://tudelft.nl/staff/p.knoppers-1">Peter Knoppers</a>
41   * @author <a href="https://github.com/wjschakel">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.instantiateSI(-0.000001);
48  
49      /** Margin in case of a right lane. */
50      private static final Length RIGHT = Length.instantiateSI(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 LinkedHashSet<>();
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 LaneRecordInterface<?> root,
79              final Length initialPosition, final Length maxDistance, final RelativePosition relativePosition,
80              final HeadwayGtuType headwayGtuType, 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          if (perceivingGtu != null)
86          {
87              this.ids.add(perceivingGtu.getId());
88          }
89          this.ignoreIfUpstream = ignoreIfUpstream;
90      }
91  
92      /** {@inheritDoc} */
93      @Override
94      protected Entry getNext(final LaneRecordInterface<?> record, final Length position, final Integer counter)
95              throws GtuException
96      {
97          int n;
98          LaneBasedGtu next;
99          Length pos;
100         if (counter == null)
101         {
102             Length searchPos = position.plus(this.margin);
103             next = record.getLane().getGtuAhead(searchPos, RelativePosition.FRONT,
104                     record.getLane().getLink().getSimulator().getSimulatorAbsTime());
105             if (next == null)
106             {
107                 return null;
108             }
109             n = record.getLane().indexOfGtu(next);
110             pos = next.position(record.getLane(), next.getRear());
111 
112             if (this.ids.contains(next.getId()))
113             {
114                 // rear still on previous lane; it is found there, get next gtu
115                 pos = pos.plus(next.getLength());
116                 return getNext(record, pos, n);
117             }
118             if (this.ignoreIfUpstream)
119             {
120                 if (pos.si < 0.0)
121                 {
122                     pos = pos.plus(next.getLength());
123                     return getNext(record, pos, n);
124                 }
125             }
126         }
127         else
128         {
129             n = counter + 1;
130             if (n < 0 || n >= record.getLane().numberOfGtus())
131             {
132                 return null;
133             }
134             next = record.getLane().getGtu(n);
135             pos = next.position(record.getLane(), next.getRear());
136             if (this.ids.contains(next.getId()))
137             {
138                 // skip self
139                 pos = pos.plus(next.getLength());
140                 return getNext(record, pos, n);
141             }
142         }
143         return new Entry(next, n, pos);
144     }
145 
146     /** {@inheritDoc} */
147     @Override
148     protected Length getDistance(final LaneBasedGtu object, final LaneRecordInterface<?> record, final Length position)
149     {
150         return record.getDistanceToPosition(position).minus(getDx());
151     }
152 
153     /** {@inheritDoc} */
154     @Override
155     public HeadwayGtu perceive(final LaneBasedGtu perceivingGtu, final LaneBasedGtu object, final Length distance)
156             throws GtuException, ParameterException
157     {
158         return this.headwayGtuType.createDownstreamGtu(perceivingGtu, object, distance);
159     }
160 
161 }