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