1 package org.opentrafficsim.road.gtu.lane.perception.categories.neighbors;
2
3 import org.djunits.value.vdouble.scalar.Duration;
4 import org.djunits.value.vdouble.scalar.Length;
5 import org.djunits.value.vdouble.scalar.Time;
6 import org.djutils.exceptions.Throw;
7 import org.opentrafficsim.base.parameters.ParameterException;
8 import org.opentrafficsim.base.parameters.ParameterTypes;
9 import org.opentrafficsim.core.gtu.GTUException;
10 import org.opentrafficsim.road.gtu.lane.LaneBasedGTU;
11 import org.opentrafficsim.road.gtu.lane.perception.headway.HeadwayGTU;
12 import org.opentrafficsim.road.gtu.lane.perception.headway.HeadwayGTUPerceived;
13 import org.opentrafficsim.road.gtu.lane.perception.headway.HeadwayGTUReal;
14 import org.opentrafficsim.road.gtu.lane.perception.headway.HeadwayGTURealCopy;
15
16
17
18
19
20
21
22
23
24
25
26
27 public interface HeadwayGtuType
28 {
29
30
31 HeadwayGtuType WRAP = new HeadwayGtuType()
32 {
33 @Override
34 public HeadwayGTUReal createDownstreamGtu(final LaneBasedGTU perceivingGtu, final LaneBasedGTU perceivedGtu,
35 final Length distance) throws GTUException
36 {
37 return new HeadwayGTUReal(perceivedGtu, distance, true);
38 }
39
40 @Override
41 public HeadwayGTUReal createUpstreamGtu(final LaneBasedGTU perceivingGtu, final LaneBasedGTU perceivedGtu,
42 final Length distance) throws GTUException
43 {
44 return new HeadwayGTUReal(perceivedGtu, distance, true);
45 }
46
47 @Override
48 public HeadwayGTUReal createParallelGtu(final LaneBasedGTU perceivingGtu, final LaneBasedGTU perceivedGtu,
49 final Length overlapFront, final Length overlap, final Length overlapRear) throws GTUException
50 {
51 return new HeadwayGTUReal(perceivedGtu, overlapFront, overlap, overlapRear, true);
52 }
53 };
54
55
56 HeadwayGtuType COPY = new HeadwayGtuType()
57 {
58 @Override
59 public HeadwayGTURealCopy createDownstreamGtu(final LaneBasedGTU perceivingGtu, final LaneBasedGTU perceivedGtu,
60 final Length distance) throws GTUException
61 {
62 return new HeadwayGTURealCopy(perceivedGtu, distance);
63 }
64
65 @Override
66 public HeadwayGTURealCopy createUpstreamGtu(final LaneBasedGTU perceivingGtu, final LaneBasedGTU perceivedGtu,
67 final Length distance) throws GTUException
68 {
69 return new HeadwayGTURealCopy(perceivedGtu, distance);
70 }
71
72 @Override
73 public HeadwayGTURealCopy createParallelGtu(final LaneBasedGTU perceivingGtu, final LaneBasedGTU perceivedGtu,
74 final Length overlapFront, final Length overlap, final Length overlapRear) throws GTUException
75 {
76 return new HeadwayGTURealCopy(perceivedGtu, overlapFront, overlap, overlapRear);
77 }
78 };
79
80
81
82
83
84
85
86
87
88
89
90
91 default HeadwayGTU createHeadwayGtu(LaneBasedGTU perceivingGtu, LaneBasedGTU perceivedGtu, Length distance,
92 boolean downstream) throws GTUException, ParameterException
93 {
94 if (distance.ge0())
95 {
96 if (downstream)
97 {
98 return createDownstreamGtu(perceivingGtu, perceivedGtu, distance);
99 }
100 return createUpstreamGtu(perceivingGtu, perceivedGtu, distance);
101 }
102 Throw.when(-distance.si > perceivingGtu.getLength().si + perceivedGtu.getLength().si, IllegalStateException.class,
103 "A GTU that is supposedly %s is actually %s.", downstream ? "downstream" : "upstream",
104 downstream ? "upstream" : "downstream");
105 Length overlapRear = distance.plus(perceivingGtu.getLength());
106 Length overlap = distance.neg();
107 Length overlapFront = distance.plus(perceivedGtu.getLength());
108 if (overlapRear.lt0())
109 {
110 overlap = overlap.plus(overlapRear);
111 }
112 if (overlapFront.lt0())
113 {
114 overlap = overlap.plus(overlapFront);
115 }
116 return createParallelGtu(perceivingGtu, perceivedGtu, overlapFront, overlap, overlapRear);
117 }
118
119
120
121
122
123
124
125
126
127
128 HeadwayGTU createDownstreamGtu(LaneBasedGTU perceivingGtu, LaneBasedGTU perceivedGtu, Length distance)
129 throws GTUException, ParameterException;
130
131
132
133
134
135
136
137
138
139
140 HeadwayGTU createUpstreamGtu(LaneBasedGTU perceivingGtu, LaneBasedGTU perceivedGtu, Length distance)
141 throws GTUException, ParameterException;
142
143
144
145
146
147
148
149
150
151
152
153 HeadwayGTU createParallelGtu(LaneBasedGTU perceivingGtu, LaneBasedGTU perceivedGtu, Length overlapFront, Length overlap,
154 Length overlapRear) throws GTUException;
155
156
157
158
159
160
161
162
163
164
165
166
167
168 class PerceivedHeadwayGtuType implements HeadwayGtuType
169 {
170
171
172 private final Estimation estimation;
173
174
175 private final Anticipation anticipation;
176
177
178 private Time updateTime = null;
179
180
181 private Duration tr;
182
183
184 private Time when;
185
186
187 private Length traveledDistance;
188
189
190
191
192
193
194 public PerceivedHeadwayGtuType(final Estimation estimation, final Anticipation anticipation)
195 {
196 this.estimation = estimation;
197 this.anticipation = anticipation;
198 }
199
200
201 @Override
202 public HeadwayGTU createHeadwayGtu(final LaneBasedGTU perceivingGtu, final LaneBasedGTU perceivedGtu,
203 final Length distance, final boolean downstream) throws GTUException, ParameterException
204 {
205 Time now = perceivedGtu.getSimulator().getSimulatorTime();
206 if (this.updateTime == null || now.si > this.updateTime.si)
207 {
208 this.updateTime = now;
209 this.tr = perceivingGtu.getParameters().getParameter(ParameterTypes.TR);
210 Time whenTemp = now.minus(this.tr);
211 if (this.when == null || (this.when != null && whenTemp.si > this.when.si))
212 {
213
214 this.when = whenTemp;
215 }
216 this.traveledDistance = perceivingGtu.getOdometer().minus(perceivingGtu.getOdometer(this.when));
217 }
218 NeighborTriplet triplet = this.estimation.estimate(perceivingGtu, perceivedGtu, distance, downstream, this.when);
219 triplet = this.anticipation.anticipate(triplet, this.tr, this.traveledDistance, downstream);
220 return new HeadwayGTUPerceived(perceivedGtu, triplet.getHeadway(), triplet.getSpeed(), triplet.getAcceleration());
221 }
222
223
224 @Override
225 public HeadwayGTU createDownstreamGtu(final LaneBasedGTU perceivingGtu, final LaneBasedGTU perceivedGtu,
226 final Length distance) throws GTUException, ParameterException
227 {
228 return createHeadwayGtu(perceivingGtu, perceivedGtu, distance, true);
229 }
230
231
232 @Override
233 public HeadwayGTU createUpstreamGtu(final LaneBasedGTU perceivingGtu, final LaneBasedGTU perceivedGtu,
234 final Length distance) throws GTUException, ParameterException
235 {
236 return createHeadwayGtu(perceivingGtu, perceivedGtu, distance, false);
237 }
238
239
240 @Override
241 public HeadwayGTU createParallelGtu(final LaneBasedGTU perceivingGtu, final LaneBasedGTU perceivedGtu,
242 final Length overlapFront, final Length overlap, final Length overlapRear) throws GTUException
243 {
244 return new HeadwayGTUPerceived(perceivedGtu, overlapFront, overlap, overlapRear, perceivedGtu.getSpeed(),
245 perceivedGtu.getAcceleration());
246 }
247
248 }
249
250 }