1 package org.opentrafficsim.draw.factory;
2
3 import java.awt.Color;
4 import java.rmi.RemoteException;
5 import java.util.Collections;
6 import java.util.LinkedHashMap;
7 import java.util.Map;
8
9 import javax.naming.NamingException;
10
11 import org.djutils.event.EventInterface;
12 import org.djutils.event.EventListenerInterface;
13 import org.djutils.logger.CategoryLogger;
14 import org.opentrafficsim.core.animation.gtu.colorer.GTUColorer;
15 import org.opentrafficsim.core.dsol.OTSSimulatorInterface;
16 import org.opentrafficsim.core.geometry.OTSGeometryException;
17 import org.opentrafficsim.core.gtu.GTU;
18 import org.opentrafficsim.core.gtu.GTUType;
19 import org.opentrafficsim.core.gtu.GtuGenerator;
20 import org.opentrafficsim.core.network.LateralDirectionality;
21 import org.opentrafficsim.core.network.Link;
22 import org.opentrafficsim.core.network.Network;
23 import org.opentrafficsim.core.network.Node;
24 import org.opentrafficsim.core.network.OTSNetwork;
25 import org.opentrafficsim.core.object.ObjectInterface;
26 import org.opentrafficsim.draw.core.OTSDrawingException;
27 import org.opentrafficsim.draw.gtu.DefaultCarAnimation;
28 import org.opentrafficsim.draw.network.LinkAnimation;
29 import org.opentrafficsim.draw.network.NodeAnimation;
30 import org.opentrafficsim.draw.road.BusStopAnimation;
31 import org.opentrafficsim.draw.road.ConflictAnimation;
32 import org.opentrafficsim.draw.road.LaneAnimation;
33 import org.opentrafficsim.draw.road.SensorAnimation;
34 import org.opentrafficsim.draw.road.ShoulderAnimation;
35 import org.opentrafficsim.draw.road.SpeedSignAnimation;
36 import org.opentrafficsim.draw.road.StripeAnimation;
37 import org.opentrafficsim.draw.road.StripeAnimation.TYPE;
38 import org.opentrafficsim.draw.road.TrafficLightAnimation;
39 import org.opentrafficsim.road.gtu.lane.LaneBasedGTU;
40 import org.opentrafficsim.road.network.lane.CrossSectionElement;
41 import org.opentrafficsim.road.network.lane.CrossSectionLink;
42 import org.opentrafficsim.road.network.lane.Lane;
43 import org.opentrafficsim.road.network.lane.Shoulder;
44 import org.opentrafficsim.road.network.lane.Stripe;
45 import org.opentrafficsim.road.network.lane.conflict.Conflict;
46 import org.opentrafficsim.road.network.lane.object.BusStop;
47 import org.opentrafficsim.road.network.lane.object.SpeedSign;
48 import org.opentrafficsim.road.network.lane.object.sensor.DestinationSensor;
49 import org.opentrafficsim.road.network.lane.object.sensor.SingleSensor;
50 import org.opentrafficsim.road.network.lane.object.sensor.SinkSensor;
51 import org.opentrafficsim.road.network.lane.object.trafficlight.TrafficLight;
52
53 import nl.tudelft.simulation.dsol.SimRuntimeException;
54 import nl.tudelft.simulation.dsol.animation.D2.Renderable2D;
55
56
57
58
59
60
61
62
63
64 public class DefaultAnimationFactory implements EventListenerInterface
65 {
66
67 private final OTSSimulatorInterface simulator;
68
69
70 private final GTUColorer gtuColorer;
71
72
73 private Map<LaneBasedGTU, Renderable2D<LaneBasedGTU>> animatedGTUs = Collections.synchronizedMap(new LinkedHashMap<>());
74
75
76 public Map<ObjectInterface, Renderable2D<?>> animatedObjects = Collections.synchronizedMap(new LinkedHashMap<>());
77
78
79
80
81
82
83
84
85
86 protected DefaultAnimationFactory(final OTSNetwork network, final GTUColorer gtuColorer, final boolean animateNetwork)
87 throws OTSDrawingException
88 {
89 this.simulator = network.getSimulator();
90 this.gtuColorer = gtuColorer;
91
92
93 network.addListener(this, Network.ANIMATION_GTU_ADD_EVENT);
94 network.addListener(this, Network.ANIMATION_GTU_REMOVE_EVENT);
95 network.addListener(this, Network.ANIMATION_OBJECT_ADD_EVENT);
96 network.addListener(this, Network.ANIMATION_OBJECT_REMOVE_EVENT);
97 network.addListener(this, Network.ANIMATION_GENERATOR_ADD_EVENT);
98 network.addListener(this, Network.ANIMATION_GENERATOR_REMOVE_EVENT);
99
100
101 try
102 {
103 if (animateNetwork)
104 {
105 for (Node node : network.getNodeMap().values())
106 {
107 new NodeAnimation(node, this.simulator);
108 }
109 for (Link link : network.getLinkMap().values())
110 {
111 new LinkAnimation(link, this.simulator, 0.5f);
112 if (link instanceof CrossSectionLink)
113 {
114 for (CrossSectionElement element : ((CrossSectionLink) link).getCrossSectionElementList())
115 {
116 if (element instanceof Lane)
117 {
118 new LaneAnimation((Lane) element, this.simulator, Color.GRAY.brighter());
119 }
120 else if (element instanceof Shoulder)
121 {
122 new ShoulderAnimation((Shoulder) element, this.simulator, Color.DARK_GRAY);
123 }
124 else if (element instanceof Stripe)
125 {
126 Stripe stripe = (Stripe) element;
127 TYPE type;
128 if (stripe.isPermeable(network.getGtuType(GTUType.DEFAULTS.CAR), LateralDirectionality.LEFT))
129 {
130 type = stripe.isPermeable(network.getGtuType(GTUType.DEFAULTS.CAR),
131 LateralDirectionality.RIGHT) ? TYPE.DASHED : TYPE.LEFTONLY;
132 }
133 else
134 {
135 type = stripe.isPermeable(network.getGtuType(GTUType.DEFAULTS.CAR),
136 LateralDirectionality.RIGHT) ? TYPE.RIGHTONLY : TYPE.SOLID;
137 }
138 new StripeAnimation((Stripe) element, this.simulator, type);
139 }
140 }
141 }
142 }
143
144 for (TrafficLight tl : network.getObjectMap(TrafficLight.class).values())
145 {
146 new TrafficLightAnimation(tl, this.simulator);
147 }
148
149 }
150
151 for (GTU gtu : network.getGTUs())
152 {
153 Renderable2D<LaneBasedGTU> gtuAnimation =
154 new DefaultCarAnimation((LaneBasedGTU) gtu, this.simulator, this.gtuColorer);
155 this.animatedGTUs.put((LaneBasedGTU) gtu, gtuAnimation);
156 }
157
158 for (ObjectInterface object : network.getObjectMap().values())
159 {
160 animateStaticObject(object);
161 }
162 }
163 catch (RemoteException | NamingException | OTSGeometryException exception)
164 {
165 throw new OTSDrawingException("Exception while creating network animation.", exception);
166 }
167
168 }
169
170
171
172
173
174
175
176
177
178
179 public static DefaultAnimationFactory animateNetwork(final OTSNetwork network, final OTSSimulatorInterface simulator,
180 final GTUColorer gtuColorer) throws OTSDrawingException
181 {
182 return new DefaultAnimationFactory(network, gtuColorer, true);
183 }
184
185
186
187
188
189
190
191
192
193 public static DefaultAnimationFactory animateXmlNetwork(final OTSNetwork network, final GTUColorer gtuColorer)
194 throws OTSDrawingException
195 {
196 return new DefaultAnimationFactory(network, gtuColorer, false);
197 }
198
199
200 @Override
201 public void notify(final EventInterface event) throws RemoteException
202 {
203 try
204 {
205 if (event.getType().equals(Network.ANIMATION_GTU_ADD_EVENT))
206 {
207
208 LaneBasedGTU gtu = (LaneBasedGTU) event.getContent();
209 this.simulator.scheduleEventNow(this, this, "animateGTU", new Object[] {gtu});
210 }
211 else if (event.getType().equals(Network.ANIMATION_GTU_REMOVE_EVENT))
212 {
213 LaneBasedGTU gtu = (LaneBasedGTU) event.getContent();
214 if (this.animatedGTUs.containsKey(gtu))
215 {
216 this.animatedGTUs.get(gtu).destroy(gtu.getSimulator());
217 this.animatedGTUs.remove(gtu);
218 }
219 }
220 else if (event.getType().equals(Network.ANIMATION_OBJECT_ADD_EVENT))
221 {
222 ObjectInterface object = (ObjectInterface) event.getContent();
223 animateStaticObject(object);
224 }
225 else if (event.getType().equals(Network.ANIMATION_OBJECT_REMOVE_EVENT))
226 {
227 ObjectInterface object = (ObjectInterface) event.getContent();
228 if (this.animatedObjects.containsKey(object))
229 {
230
231
232 this.animatedObjects.remove(object);
233 }
234 }
235 else if (event.getType().equals(Network.ANIMATION_GENERATOR_ADD_EVENT))
236 {
237 GtuGenerator gtuGenerator = (GtuGenerator) event.getContent();
238 animateGTUGenerator(gtuGenerator);
239 }
240 else if (event.getType().equals(Network.ANIMATION_GENERATOR_REMOVE_EVENT))
241 {
242
243 }
244 }
245 catch (SimRuntimeException exception)
246 {
247 CategoryLogger.always().error(exception, "Exception while updating network animation.");
248 }
249 }
250
251
252
253
254
255 protected void animateGTU(final LaneBasedGTU gtu)
256 {
257 try
258 {
259 Renderable2D<LaneBasedGTU> gtuAnimation = new DefaultCarAnimation(gtu, this.simulator, this.gtuColorer);
260 this.animatedGTUs.put(gtu, gtuAnimation);
261 }
262 catch (RemoteException | NamingException exception)
263 {
264 gtu.getSimulator().getLogger().always().error(exception, "Exception while drawing GTU.");
265 }
266 }
267
268
269
270
271
272 protected void animateStaticObject(final ObjectInterface object)
273 {
274 try
275 {
276 if (object instanceof SinkSensor)
277 {
278 SinkSensor sensor = (SinkSensor) object;
279
280 Renderable2D<SingleSensor> objectAnimation =
281 new SensorAnimation(sensor, sensor.getLongitudinalPosition(), this.simulator, Color.YELLOW);
282 this.animatedObjects.put(object, objectAnimation);
283 }
284 else if (object instanceof DestinationSensor)
285 {
286 DestinationSensor sensor = (DestinationSensor) object;
287
288 Renderable2D<SingleSensor> objectAnimation =
289 new SensorAnimation(sensor, sensor.getLongitudinalPosition(), this.simulator, Color.ORANGE);
290 this.animatedObjects.put(object, objectAnimation);
291 }
292 else if (object instanceof SingleSensor)
293 {
294 SingleSensor sensor = (SingleSensor) object;
295 Renderable2D<SingleSensor> objectAnimation =
296 new SensorAnimation(sensor, sensor.getLongitudinalPosition(), this.simulator, Color.GREEN);
297 this.animatedObjects.put(object, objectAnimation);
298 }
299 else if (object instanceof Conflict)
300 {
301 Conflict conflict = (Conflict) object;
302 Renderable2D<Conflict> objectAnimation = new ConflictAnimation(conflict, this.simulator);
303 this.animatedObjects.put(object, objectAnimation);
304 }
305 else if (object instanceof SpeedSign)
306 {
307 SpeedSign speedSign = (SpeedSign) object;
308 Renderable2D<SpeedSign> objectAnimation = new SpeedSignAnimation(speedSign, this.simulator);
309 this.animatedObjects.put(object, objectAnimation);
310 }
311 else if (object instanceof BusStop)
312 {
313 BusStop busStop = (BusStop) object;
314 Renderable2D<BusStop> objectAnimation = new BusStopAnimation(busStop, this.simulator);
315 this.animatedObjects.put(object, objectAnimation);
316 }
317 }
318 catch (RemoteException | NamingException exception)
319 {
320 CategoryLogger.always().error(exception, "Exception while drawing Object of class ObjectInterface.");
321 }
322 }
323
324
325
326
327
328 protected void animateGTUGenerator(final GtuGenerator gtuGenerator)
329 {
330
331 }
332
333 }