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
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43 public class DownstreamNeighborsIterable extends AbstractPerceptionIterable<HeadwayGtu, LaneBasedGtu, Integer>
44 {
45
46
47 private static final Length LEFT = Length.instantiateSI(-0.000001);
48
49
50 private static final Length RIGHT = Length.instantiateSI(0.000001);
51
52
53 private final HeadwayGtuType headwayGtuType;
54
55
56 private final Set<String> ids = new LinkedHashSet<>();
57
58
59
60
61
62 private final Length margin;
63
64
65 private final boolean ignoreIfUpstream;
66
67
68
69
70
71
72
73
74
75
76
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
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
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
139 pos = pos.plus(next.getLength());
140 return getNext(record, pos, n);
141 }
142 }
143 return new Entry(next, n, pos);
144 }
145
146
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
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 }