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