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,
87 final boolean animateNetwork) 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, simulator);
108 }
109 for (Link link : network.getLinkMap().values())
110 {
111 new LinkAnimation(link, 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, simulator, Color.GRAY.brighter());
119 }
120 else if (element instanceof Shoulder)
121 {
122 new ShoulderAnimation((Shoulder) element, 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, simulator, type);
139 }
140 }
141 }
142 }
143
144 for (TrafficLight tl : network.getObjectMap(TrafficLight.class).values())
145 {
146 new TrafficLightAnimation(tl, simulator);
147 }
148
149 }
150
151 for (GTU gtu : network.getGTUs())
152 {
153 Renderable2D<LaneBasedGTU> gtuAnimation =
154 new DefaultCarAnimation((LaneBasedGTU) gtu, 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) throws OTSDrawingException
194 {
195 return new DefaultAnimationFactory(network, gtuColorer, false);
196 }
197
198
199 @Override
200 public void notify(final EventInterface event) throws RemoteException
201 {
202 try
203 {
204 if (event.getType().equals(Network.ANIMATION_GTU_ADD_EVENT))
205 {
206
207 LaneBasedGTU gtu = (LaneBasedGTU) event.getContent();
208 this.simulator.scheduleEventNow(this, this, "animateGTU", new Object[] {gtu});
209 }
210 else if (event.getType().equals(Network.ANIMATION_GTU_REMOVE_EVENT))
211 {
212 LaneBasedGTU gtu = (LaneBasedGTU) event.getContent();
213 if (this.animatedGTUs.containsKey(gtu))
214 {
215 this.animatedGTUs.get(gtu).destroy();
216 this.animatedGTUs.remove(gtu);
217 }
218 }
219 else if (event.getType().equals(Network.ANIMATION_OBJECT_ADD_EVENT))
220 {
221 ObjectInterface object = (ObjectInterface) event.getContent();
222 animateStaticObject(object);
223 }
224 else if (event.getType().equals(Network.ANIMATION_OBJECT_REMOVE_EVENT))
225 {
226 ObjectInterface object = (ObjectInterface) event.getContent();
227 if (this.animatedObjects.containsKey(object))
228 {
229 this.animatedObjects.get(object).destroy();
230 this.animatedObjects.remove(object);
231 }
232 }
233 else if (event.getType().equals(Network.ANIMATION_GENERATOR_ADD_EVENT))
234 {
235 GtuGenerator gtuGenerator = (GtuGenerator) event.getContent();
236 animateGTUGenerator(gtuGenerator);
237 }
238 else if (event.getType().equals(Network.ANIMATION_GENERATOR_REMOVE_EVENT))
239 {
240
241 }
242 }
243 catch (NamingException | SimRuntimeException exception)
244 {
245 CategoryLogger.always().error(exception, "Exception while updating network animation.");
246 }
247 }
248
249
250
251
252
253 protected void animateGTU(final LaneBasedGTU gtu)
254 {
255 try
256 {
257 Renderable2D<LaneBasedGTU> gtuAnimation = new DefaultCarAnimation(gtu, this.simulator, this.gtuColorer);
258 this.animatedGTUs.put(gtu, gtuAnimation);
259 }
260 catch (RemoteException | NamingException exception)
261 {
262 gtu.getSimulator().getLogger().always().error(exception, "Exception while drawing GTU.");
263 }
264 }
265
266
267
268
269
270 protected void animateStaticObject(final ObjectInterface object)
271 {
272 try
273 {
274 if (object instanceof SinkSensor)
275 {
276 SinkSensor sensor = (SinkSensor) object;
277
278 Renderable2D<SingleSensor> objectAnimation =
279 new SensorAnimation(sensor, sensor.getLongitudinalPosition(), this.simulator, Color.YELLOW);
280 this.animatedObjects.put(object, objectAnimation);
281 }
282 else if (object instanceof DestinationSensor)
283 {
284 DestinationSensor sensor = (DestinationSensor) object;
285
286 Renderable2D<SingleSensor> objectAnimation =
287 new SensorAnimation(sensor, sensor.getLongitudinalPosition(), this.simulator, Color.ORANGE);
288 this.animatedObjects.put(object, objectAnimation);
289 }
290 else if (object instanceof SingleSensor)
291 {
292 SingleSensor sensor = (SingleSensor) object;
293 Renderable2D<SingleSensor> objectAnimation =
294 new SensorAnimation(sensor, sensor.getLongitudinalPosition(), this.simulator, Color.GREEN);
295 this.animatedObjects.put(object, objectAnimation);
296 }
297 else if (object instanceof Conflict)
298 {
299 Conflict conflict = (Conflict) object;
300 Renderable2D<Conflict> objectAnimation = new ConflictAnimation(conflict, this.simulator);
301 this.animatedObjects.put(object, objectAnimation);
302 }
303 else if (object instanceof SpeedSign)
304 {
305 SpeedSign speedSign = (SpeedSign) object;
306 Renderable2D<SpeedSign> objectAnimation = new SpeedSignAnimation(speedSign, this.simulator);
307 this.animatedObjects.put(object, objectAnimation);
308 }
309 else if (object instanceof BusStop)
310 {
311 BusStop busStop = (BusStop) object;
312 Renderable2D<BusStop> objectAnimation = new BusStopAnimation(busStop, this.simulator);
313 this.animatedObjects.put(object, objectAnimation);
314 }
315 }
316 catch (RemoteException | NamingException exception)
317 {
318 CategoryLogger.always().error(exception, "Exception while drawing Object of class ObjectInterface.");
319 }
320 }
321
322
323
324
325
326 protected void animateGTUGenerator(final GtuGenerator gtuGenerator)
327 {
328
329 }
330
331 }