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.GTU;
9 import org.opentrafficsim.core.gtu.GTUException;
10 import org.opentrafficsim.core.gtu.RelativePosition;
11 import org.opentrafficsim.road.gtu.lane.LaneBasedGTU;
12 import org.opentrafficsim.road.gtu.lane.perception.categories.neighbors.HeadwayGtuType;
13 import org.opentrafficsim.road.gtu.lane.perception.headway.HeadwayGTU;
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
44 public class DownstreamNeighborsIterable extends AbstractPerceptionIterable<HeadwayGTU, LaneBasedGTU, Integer>
45 {
46
47
48 private static final Length LEFT = Length.createSI(-0.000001);
49
50
51 private static final Length RIGHT = Length.createSI(0.000001);
52
53
54 private final HeadwayGtuType headwayGtuType;
55
56
57 private final Set<String> ids = new HashSet<>();
58
59
60
61
62
63 private final Length margin;
64
65
66 private final boolean ignoreIfUpstream;
67
68
69
70
71
72
73
74
75
76
77
78
79
80 public DownstreamNeighborsIterable(final LaneBasedGTU perceivingGtu, final LaneRecord<?> root, final Length initialPosition,
81 final Length maxDistance, final RelativePosition relativePosition, final HeadwayGtuType headwayGtuType,
82 final GTU gtu, final RelativeLane lane, final boolean ignoreIfUpstream)
83 {
84 super(perceivingGtu, root, initialPosition, true, maxDistance, relativePosition, null);
85 this.headwayGtuType = headwayGtuType;
86 this.margin = lane.getLateralDirectionality().isLeft() ? LEFT : RIGHT;
87 if (gtu != null)
88 {
89 this.ids.add(gtu.getId());
90 }
91 this.ignoreIfUpstream = ignoreIfUpstream;
92 }
93
94
95 @Override
96 protected Entry getNext(final LaneRecord<?> record, final Length position, final Integer counter) throws GTUException
97 {
98 int n;
99 LaneBasedGTU next;
100 Length pos;
101 boolean plus = record.getDirection().isPlus();
102 if (counter == null)
103 {
104 Length searchPos = (plus ? position.plus(this.margin) : position.minus(this.margin));
105 next = record.getLane().getGtuAhead(searchPos, record.getDirection(), RelativePosition.FRONT,
106 record.getLane().getParentLink().getSimulator().getSimulatorTime());
107 if (next == null)
108 {
109 return null;
110 }
111 n = record.getLane().indexOfGtu(next);
112 pos = next.position(record.getLane(), next.getRear());
113
114 if (this.ids.contains(next.getId()))
115 {
116
117 pos = plus ? pos.plus(next.getLength()) : pos.minus(next.getLength());
118 return getNext(record, pos, n);
119 }
120 if (this.ignoreIfUpstream)
121 {
122 if (plus ? pos.si < 0.0 : pos.si > record.getLane().getLength().si)
123 {
124 pos = plus ? pos.plus(next.getLength()) : pos.minus(next.getLength());
125 return getNext(record, pos, n);
126 }
127 }
128 }
129 else
130 {
131 n = plus ? counter + 1 : counter - 1;
132 if (n < 0 || n >= record.getLane().numberOfGtus())
133 {
134 return null;
135 }
136 next = record.getLane().getGtu(n);
137 pos = next.position(record.getLane(), next.getRear());
138 }
139 return new Entry(next, n, pos);
140 }
141
142
143 @Override
144 protected Length getDistance(final LaneBasedGTU object, final LaneRecord<?> record, final Length position)
145 {
146 return record.getDistanceToPosition(position).minus(getDx());
147 }
148
149
150 @Override
151 public HeadwayGTU perceive(final LaneBasedGTU perceivingGtu, final LaneBasedGTU object, final Length distance)
152 throws GTUException, ParameterException
153 {
154 return this.headwayGtuType.createDownstreamGtu(perceivingGtu, object, distance);
155 }
156
157 }