1 package org.opentrafficsim.road.gtu.lane.object;
2
3 import java.util.HashMap;
4 import java.util.Map;
5
6 import javax.media.j3d.Bounds;
7 import javax.naming.NamingException;
8 import javax.vecmath.Point3d;
9
10 import nl.tudelft.simulation.dsol.SimRuntimeException;
11 import nl.tudelft.simulation.dsol.formalisms.eventscheduling.SimEvent;
12 import nl.tudelft.simulation.language.d3.BoundingBox;
13 import nl.tudelft.simulation.language.d3.DirectedPoint;
14
15 import org.djunits.unit.LengthUnit;
16 import org.djunits.unit.TimeUnit;
17 import org.djunits.value.vdouble.scalar.Length;
18 import org.djunits.value.vdouble.scalar.Length.Rel;
19 import org.djunits.value.vdouble.scalar.Speed;
20 import org.djunits.value.vdouble.scalar.Time;
21 import org.opentrafficsim.core.dsol.OTSDEVSSimulatorInterface;
22 import org.opentrafficsim.core.dsol.OTSSimTimeDouble;
23 import org.opentrafficsim.core.geometry.OTSGeometryException;
24 import org.opentrafficsim.core.gtu.AbstractGTU;
25 import org.opentrafficsim.core.gtu.GTU;
26 import org.opentrafficsim.core.gtu.GTUDirectionality;
27 import org.opentrafficsim.core.gtu.GTUException;
28 import org.opentrafficsim.core.gtu.GTUType;
29 import org.opentrafficsim.core.gtu.RelativePosition;
30 import org.opentrafficsim.core.gtu.RelativePosition.TYPE;
31 import org.opentrafficsim.core.gtu.plan.operational.OperationalPlan;
32 import org.opentrafficsim.core.gtu.plan.operational.OperationalPlanException;
33 import org.opentrafficsim.core.gtu.plan.strategical.StrategicalPlanner;
34 import org.opentrafficsim.core.gtu.plan.tactical.TacticalPlanner;
35 import org.opentrafficsim.core.network.Link;
36 import org.opentrafficsim.core.network.LinkDirection;
37 import org.opentrafficsim.core.network.NetworkException;
38 import org.opentrafficsim.core.network.Node;
39 import org.opentrafficsim.core.network.OTSNetwork;
40 import org.opentrafficsim.road.gtu.lane.LaneBasedGTU;
41 import org.opentrafficsim.road.gtu.lane.driver.LaneBasedBehavioralCharacteristics;
42 import org.opentrafficsim.road.gtu.lane.perception.LanePerceptionFull;
43 import org.opentrafficsim.road.gtu.strategical.LaneBasedStrategicalPlanner;
44 import org.opentrafficsim.road.network.lane.CrossSectionElement;
45 import org.opentrafficsim.road.network.lane.CrossSectionLink;
46 import org.opentrafficsim.road.network.lane.Lane;
47
48
49
50
51
52
53
54
55
56
57
58 public class AbstractTrafficLight extends AbstractGTU implements LaneBasedGTU
59 {
60
61 private static final long serialVersionUID = 1L;
62
63
64 final Lane laneTL;
65
66
67 final Length.Rel positionTL;
68
69
70 private boolean blocked = true;
71
72
73 public static final GTUType BLOCK_GTU;
74
75
76 public static final StrategicalPlanner dummyStrategicalPlanner;
77
78
79 public static final Map<RelativePosition.TYPE, RelativePosition> RELATIVE_POSITIONS = new HashMap<>();
80
81 static
82 {
83 BLOCK_GTU = GTUType.makeGTUType("BLOCK");
84 dummyStrategicalPlanner = new DummyStrategicalPlanner();
85 RELATIVE_POSITIONS.put(RelativePosition.FRONT, new RelativePosition(Length.Rel.ZERO, Length.Rel.ZERO,
86 Length.Rel.ZERO, RelativePosition.FRONT));
87 RELATIVE_POSITIONS.put(RelativePosition.REAR, new RelativePosition(Length.Rel.ZERO, Length.Rel.ZERO,
88 Length.Rel.ZERO, RelativePosition.REAR));
89 RELATIVE_POSITIONS.put(RelativePosition.REFERENCE, RelativePosition.REFERENCE_POSITION);
90 }
91
92
93
94
95
96
97
98
99
100
101
102
103
104 public AbstractTrafficLight(final String name, final Lane lane, final Length.Rel position,
105 final OTSDEVSSimulatorInterface simulator, final OTSNetwork network) throws GTUException, NetworkException,
106 NamingException, SimRuntimeException, OTSGeometryException
107 {
108 super(name, BLOCK_GTU, simulator, dummyStrategicalPlanner, new LanePerceptionFull(), lane.getCenterLine()
109 .getLocation(position), Speed.ZERO, network);
110 this.positionTL = position;
111 this.laneTL = lane;
112
113
114 lane.addGTU(this, position);
115 }
116
117
118
119
120 public final void setBlocked(final boolean blocked)
121 {
122 try
123 {
124 if (this.blocked && !blocked)
125 {
126
127 this.laneTL.removeGTU(this);
128 }
129 else if (!this.blocked && blocked)
130 {
131
132 this.laneTL.addGTU(this, this.positionTL);
133 }
134 this.blocked = blocked;
135 }
136 catch (GTUException exception)
137 {
138 exception.printStackTrace();
139 }
140 }
141
142
143
144
145 public final boolean isBlocked()
146 {
147 return this.blocked;
148 }
149
150
151
152
153 public final Lane getLane()
154 {
155 return this.laneTL;
156 }
157
158
159
160
161 @Override
162 public Length.Rel getLength()
163 {
164 return Length.Rel.ZERO;
165 }
166
167
168 @Override
169 public Length.Rel getWidth()
170 {
171 return Length.Rel.ZERO;
172 }
173
174
175 @Override
176 public Speed getMaximumVelocity()
177 {
178 return Speed.ZERO;
179 }
180
181
182 @Override
183 public RelativePosition getFront()
184 {
185 return RELATIVE_POSITIONS.get(RelativePosition.FRONT);
186 }
187
188
189 @Override
190 public RelativePosition getRear()
191 {
192 return RELATIVE_POSITIONS.get(RelativePosition.REAR);
193 }
194
195
196 @Override
197 public Map<TYPE, RelativePosition> getRelativePositions()
198 {
199 return RELATIVE_POSITIONS;
200 }
201
202
203 @Override
204 public Bounds getBounds()
205 {
206 double dx = 2;
207 double dy = 1;
208 return new BoundingBox(new Point3d(-dx, -dy, 0.0), new Point3d(dx, dy, 0.0));
209 }
210
211
212 @Override
213 public LaneBasedBehavioralCharacteristics getBehavioralCharacteristics()
214 {
215 return null;
216 }
217
218
219 @Override
220 public Map<Lane, GTUDirectionality> getLanes()
221 {
222 return null;
223 }
224
225
226 @Override
227 public void enterLane(final Lane lane, final Rel position, final GTUDirectionality gtuDirection)
228 throws GTUException
229 {
230
231 }
232
233
234 @Override
235 public void leaveLane(final Lane lane)
236 {
237
238 }
239
240
241 @Override
242 public Map<Lane, Length.Rel> positions(final RelativePosition relativePosition) throws GTUException
243 {
244 Map<Lane, Length.Rel> map = new HashMap<Lane, Length.Rel>();
245 map.put(this.laneTL, this.positionTL);
246 return map;
247 }
248
249
250 @Override
251 public Map<Lane, Length.Rel> positions(final RelativePosition relativePosition, final Time.Abs when)
252 throws GTUException
253 {
254 return positions(relativePosition);
255 }
256
257
258 @Override
259 public Rel position(final Lane lane, final RelativePosition relativePosition) throws GTUException
260 {
261 if (this.laneTL.equals(lane))
262 {
263 return this.positionTL;
264 }
265 throw new GTUException("TrafficLight " + this.getId() + " not on lane " + lane);
266 }
267
268
269 @Override
270 public Rel position(final Lane lane, final RelativePosition relativePosition, final Time.Abs when)
271 throws GTUException
272 {
273 return position(lane, relativePosition);
274 }
275
276
277 @Override
278 public Map<Lane, Double> fractionalPositions(final RelativePosition relativePosition) throws GTUException
279 {
280 Map<Lane, Double> map = new HashMap<Lane, Double>();
281 map.put(this.laneTL, this.positionTL.getSI() / this.laneTL.getLength().getSI());
282 return map;
283 }
284
285
286 @Override
287 public Map<Lane, Double> fractionalPositions(final RelativePosition relativePosition, final Time.Abs when)
288 throws GTUException
289 {
290 return fractionalPositions(relativePosition);
291 }
292
293
294 @Override
295 public double fractionalPosition(final Lane lane, final RelativePosition relativePosition) throws GTUException
296 {
297 if (this.laneTL.equals(lane))
298 {
299 return this.positionTL.getSI() / this.laneTL.getLength().getSI();
300 }
301 throw new GTUException("TrafficLight " + this.getId() + " not on lane " + lane);
302 }
303
304
305 @Override
306 public double fractionalPosition(final Lane lane, final RelativePosition relativePosition, final Time.Abs when)
307 throws GTUException
308 {
309 return fractionalPosition(lane, relativePosition);
310 }
311
312
313 @Override
314 public Length.Rel projectedPosition(final Lane projectionLane, final RelativePosition relativePosition,
315 final Time.Abs when) throws GTUException
316 {
317 CrossSectionLink link = projectionLane.getParentLink();
318 for (CrossSectionElement cse : link.getCrossSectionElementList())
319 {
320 if (cse instanceof Lane)
321 {
322 Lane cseLane = (Lane) cse;
323 if (cseLane.equals(projectionLane))
324 {
325 double fractionalPosition = fractionalPosition(cseLane, relativePosition, when);
326 return new Length.Rel(projectionLane.getLength().getSI() * fractionalPosition, LengthUnit.SI);
327 }
328 }
329 }
330 throw new GTUException("TrafficLight " + getId() + " is not on any lane of Link " + link);
331 }
332
333
334 @Override
335 public LaneBasedStrategicalPlanner getStrategicalPlanner()
336 {
337 return (LaneBasedStrategicalPlanner) super.getStrategicalPlanner();
338 }
339
340
341 @Override
342 public LanePerceptionFull getPerception()
343 {
344 return (LanePerceptionFull) super.getPerception();
345 }
346
347
348
349
350
351
352 static class DummyStrategicalPlanner implements LaneBasedStrategicalPlanner
353 {
354
355 private LaneBasedBehavioralCharacteristics drivingCharacteristics;
356
357
358 @Override
359 public Node nextNode(Node node, Link previousLink, final GTUType gtuType) throws NetworkException
360 {
361 return null;
362 }
363
364
365 @Override
366 public Node nextNode(Link link, GTUDirectionality direction, final GTUType gtuType) throws NetworkException
367 {
368 return null;
369 }
370
371
372 @Override
373 public LinkDirection nextLinkDirection(Node node, Link previousLink, final GTUType gtuType)
374 throws NetworkException
375 {
376 return null;
377 }
378
379
380 @Override
381 public LinkDirection nextLinkDirection(Link link, GTUDirectionality direction, final GTUType gtuType)
382 throws NetworkException
383 {
384 return null;
385 }
386
387
388 @Override
389 public TacticalPlanner generateTacticalPlanner(GTU gtu)
390 {
391 return new DummyTacticalPlanner();
392 }
393
394
395 @Override
396 public LaneBasedBehavioralCharacteristics getDrivingCharacteristics()
397 {
398 return this.drivingCharacteristics;
399 }
400
401
402 @Override
403 public void setDrivingCharacteristics(final LaneBasedBehavioralCharacteristics drivingCharacteristics)
404 {
405 this.drivingCharacteristics = drivingCharacteristics;
406 }
407 }
408
409
410 static class DummyTacticalPlanner implements TacticalPlanner
411 {
412
413 private static final long serialVersionUID = 1L;
414
415
416 @Override
417 public OperationalPlan generateOperationalPlan(final GTU gtu, final Time.Abs startTime,
418 final DirectedPoint locationAtStartTime) throws OperationalPlanException, GTUException, NetworkException
419 {
420 return new OperationalPlan(gtu, locationAtStartTime, startTime, new Time.Rel(1.0, TimeUnit.MINUTE));
421 }
422
423 }
424
425
426 @Override
427 public void addTrigger(Lane lane, SimEvent<OTSSimTimeDouble> event)
428 {
429
430 }
431 }