1 package org.opentrafficsim.road.gtu.lane.perception.headway;
2
3 import java.util.EnumSet;
4
5 import org.djunits.value.vdouble.scalar.Acceleration;
6 import org.djunits.value.vdouble.scalar.Length;
7 import org.djunits.value.vdouble.scalar.Speed;
8 import org.opentrafficsim.base.parameters.Parameters;
9 import org.opentrafficsim.core.gtu.GTUException;
10 import org.opentrafficsim.core.gtu.GTUType;
11 import org.opentrafficsim.core.network.NetworkException;
12 import org.opentrafficsim.core.network.route.Route;
13 import org.opentrafficsim.road.gtu.lane.LaneBasedGTU;
14 import org.opentrafficsim.road.gtu.lane.tactical.following.CarFollowingModel;
15 import org.opentrafficsim.road.network.speed.SpeedLimitInfo;
16 import org.opentrafficsim.road.network.speed.SpeedLimitTypes;
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 HeadwayGTUReal extends AbstractHeadway implements HeadwayGTU
45 {
46
47 private static final long serialVersionUID = 20170324L;
48
49
50 private SpeedLimitInfo speedLimitInfo;
51
52
53 private final LaneBasedGTU gtu;
54
55
56 private final boolean facingSameDirection;
57
58
59
60
61
62
63
64
65 public HeadwayGTUReal(final LaneBasedGTU gtu, final Length distance, final boolean facingSameDirection) throws GTUException
66 {
67 super(distance);
68 this.gtu = gtu;
69 this.facingSameDirection = facingSameDirection;
70 }
71
72
73
74
75
76
77
78
79
80
81 public HeadwayGTUReal(final LaneBasedGTU gtu, final Length overlapFront, final Length overlap, final Length overlapRear,
82 final boolean facingSameDirection) throws GTUException
83 {
84 super(overlapFront, overlap, overlapRear);
85 this.gtu = gtu;
86 this.facingSameDirection = facingSameDirection;
87 }
88
89
90
91
92
93
94 private SpeedLimitInfo getSpeedLimitInfo(final LaneBasedGTU wrappedGtu)
95 {
96 SpeedLimitInfo sli = new SpeedLimitInfo();
97 sli.addSpeedInfo(SpeedLimitTypes.MAX_VEHICLE_SPEED, wrappedGtu.getMaximumSpeed());
98 try
99 {
100 sli.addSpeedInfo(SpeedLimitTypes.FIXED_SIGN,
101 wrappedGtu.getReferencePosition().getLane().getSpeedLimit(wrappedGtu.getGTUType()));
102 }
103 catch (NetworkException | GTUException exception)
104 {
105 throw new RuntimeException("Could not obtain speed limit from lane for perception.", exception);
106 }
107 return sli;
108 }
109
110
111 @Override
112 public final CarFollowingModel getCarFollowingModel()
113 {
114 return this.gtu.getTacticalPlanner().getCarFollowingModel();
115 }
116
117
118 @Override
119 public final Parameters getParameters()
120 {
121 return this.gtu.getParameters();
122 }
123
124
125 @Override
126 public final SpeedLimitInfo getSpeedLimitInfo()
127 {
128 if (this.speedLimitInfo == null)
129 {
130 this.speedLimitInfo = getSpeedLimitInfo(this.gtu);
131 }
132 return this.speedLimitInfo;
133 }
134
135
136 @Override
137 public final Route getRoute()
138 {
139 return this.gtu.getStrategicalPlanner().getRoute();
140 }
141
142
143
144
145
146
147
148 @Override
149 public final HeadwayGTU moved(final Length headway, final Speed speed, final Acceleration acceleration)
150 {
151 try
152 {
153 return new HeadwayGTURealCopy(getId(), getGtuType(), headway, getLength(), getWidth(), speed, acceleration,
154 getCarFollowingModel(), getParameters(), getSpeedLimitInfo(), getRoute(), getDesiredSpeed(),
155 getGtuStatus());
156 }
157 catch (GTUException exception)
158 {
159
160 throw new RuntimeException("Exception while copying Headway GTU.", exception);
161 }
162 }
163
164
165
166
167
168 private GTUStatus[] getGtuStatus()
169 {
170 EnumSet<GTUStatus> gtuStatus = EnumSet.noneOf(GTUStatus.class);
171 if (isLeftTurnIndicatorOn())
172 {
173 gtuStatus.add(GTUStatus.LEFT_TURNINDICATOR);
174 }
175 if (isRightTurnIndicatorOn())
176 {
177 gtuStatus.add(GTUStatus.RIGHT_TURNINDICATOR);
178 }
179 if (isBrakingLightsOn())
180 {
181 gtuStatus.add(GTUStatus.BRAKING_LIGHTS);
182 }
183 if (isEmergencyLightsOn())
184 {
185 gtuStatus.add(GTUStatus.EMERGENCY_LIGHTS);
186 }
187 if (isHonking())
188 {
189 gtuStatus.add(GTUStatus.HONK);
190 }
191 return gtuStatus.toArray(new GTUStatus[gtuStatus.size()]);
192 }
193
194
195 @Override
196 public final String getId()
197 {
198 return this.gtu.getId();
199 }
200
201
202 @Override
203 public final Length getLength()
204 {
205 return this.gtu.getLength();
206 }
207
208
209 @Override
210 public Length getWidth()
211 {
212 return this.gtu.getWidth();
213 }
214
215
216 @Override
217 public final Speed getSpeed()
218 {
219 return this.gtu.getSpeed();
220 }
221
222
223 @Override
224 public Speed getDesiredSpeed()
225 {
226 return this.gtu.getDesiredSpeed();
227 }
228
229
230 @Override
231 public final ObjectType getObjectType()
232 {
233 return ObjectType.GTU;
234 }
235
236
237 @Override
238 public final Acceleration getAcceleration()
239 {
240 return this.gtu.getAcceleration();
241 }
242
243
244 @Override
245 public final GTUType getGtuType()
246 {
247 return this.gtu.getGTUType();
248 }
249
250
251 @Override
252 public final boolean isFacingSameDirection()
253 {
254 return this.facingSameDirection;
255 }
256
257
258 @Override
259 public final boolean isBrakingLightsOn()
260 {
261
262 return false;
263 }
264
265
266 @Override
267 public final boolean isLeftTurnIndicatorOn()
268 {
269 return this.gtu.getTurnIndicatorStatus().isLeft();
270 }
271
272
273 @Override
274 public final boolean isRightTurnIndicatorOn()
275 {
276 return this.gtu.getTurnIndicatorStatus().isRight();
277 }
278
279
280 @Override
281 public final boolean isEmergencyLightsOn()
282 {
283 return this.gtu.getTurnIndicatorStatus().isHazard();
284 }
285
286
287 @Override
288 public final boolean isHonking()
289 {
290
291 return false;
292 }
293
294
295 @Override
296 public final String toString()
297 {
298 return "HeadwayGTUReal [speedLimitInfo=" + this.speedLimitInfo + ", gtu=" + this.gtu + ", facingSameDirection="
299 + this.facingSameDirection + "]";
300 }
301
302 }