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
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-2020 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.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<?>; 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 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 LaneRecord<?> record, final Length position, final Integer counter) throws GTUException
95 {
96 int n;
97 LaneBasedGTU next;
98 Length pos;
99 boolean plus = record.getDirection().isPlus();
100 if (counter == null)
101 {
102 Length searchPos = (plus ? position.plus(this.margin) : position.minus(this.margin));
103 next = record.getLane().getGtuAhead(searchPos, record.getDirection(), RelativePosition.FRONT,
104 record.getLane().getParentLink().getSimulator().getSimulatorTime());
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 = plus ? pos.plus(next.getLength()) : pos.minus(next.getLength());
116 return getNext(record, pos, n);
117 }
118 if (this.ignoreIfUpstream)
119 {
120 if (plus ? pos.si < 0.0 : pos.si > record.getLane().getLength().si)
121 {
122 pos = plus ? pos.plus(next.getLength()) : pos.minus(next.getLength());
123 return getNext(record, pos, n);
124 }
125 }
126 }
127 else
128 {
129 n = plus ? counter + 1 : 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 = plus ? pos.plus(next.getLength()) : pos.minus(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 LaneRecord<?> record, final Length position)
149 {
150 return record.getDistanceToPosition(position).minus(getDx());
151 }
152
153 /** {@inheritDoc} */
154 @Override
155 public HeadwayGTU perceive(final LaneBasedGTUeBasedGTU.html#LaneBasedGTU">LaneBasedGTU perceivingGtu, final LaneBasedGTU object, final Length distance)
156 throws GTUException, ParameterException
157 {
158 return this.headwayGtuType.createDownstreamGtu(perceivingGtu, object, distance);
159 }
160
161 }