View Javadoc
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   * DefaultAnimationFactory.java. <br>
58   * <br>
59   * Copyright (c) 2003-2020 Delft University of Technology, Jaffalaan 5, 2628 BX Delft, the Netherlands. All rights reserved. See
60   * for project information <a href="https://www.simulation.tudelft.nl/" target="_blank">www.simulation.tudelft.nl</a>. The
61   * source code and binary code of this software is proprietary information of Delft University of Technology.
62   * @author <a href="https://www.tudelft.nl/averbraeck" target="_blank">Alexander Verbraeck</a>
63   */
64  public class DefaultAnimationFactory implements EventListenerInterface
65  {
66      /** the simulator. */
67      private final OTSSimulatorInterface simulator;
68  
69      /** GTU colorer. */
70      private final GTUColorer gtuColorer;
71  
72      /** rendered gtus. */
73      private Map<LaneBasedGTU, Renderable2D<LaneBasedGTU>> animatedGTUs = Collections.synchronizedMap(new LinkedHashMap<>());
74  
75      /** rendered static objects. */
76      public Map<ObjectInterface, Renderable2D<?>> animatedObjects = Collections.synchronizedMap(new LinkedHashMap<>());
77  
78      /**
79       * Creates animations for nodes, links and lanes. The class will subscribe to the network and listen to changes, so the
80       * adding and removing of GTUs and Objects is animated correctly.
81       * @param network OTSNetwork; the network
82       * @param gtuColorer GTUColorer; GTU colorer
83       * @param animateNetwork boolean; whether to animate the current network objects
84       * @throws OTSDrawingException on drawing error
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          // subscribe to adding and removing events
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         // model the current infrastructure
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      * Creates animations for nodes, links, lanes and GTUs. This can be used if the network is not read from XML. The class will
172      * subscribe to the network and listen to changes, so the adding and removing of GTUs and Objects is animated correctly.
173      * @param network OTSNetwork; the network
174      * @param simulator OTSSimulatorInterface; the simulator
175      * @param gtuColorer GTUColorer; GTU colorer
176      * @return the DefaultAnimationFactory
177      * @throws OTSDrawingException on drawing error
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      * Creates animations for nodes, links, lanes and GTUs. This can be used if the network is read from XML. The class will
187      * subscribe to the network and listen to changes, so the adding and removing of GTUs and Objects is animated correctly.
188      * @param network OTSNetwork; the network
189      * @param gtuColorer GTUColorer; GTU colorer
190      * @return the DefaultAnimationFactory
191      * @throws OTSDrawingException on drawing error
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     /** {@inheritDoc} */
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                 // schedule the addition of the GTU to prevent it from not having an operational plan
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                 // TODO: change the way generators are animated
241             }
242         }
243         catch (NamingException | SimRuntimeException exception)
244         {
245             CategoryLogger.always().error(exception, "Exception while updating network animation.");
246         }
247     }
248 
249     /**
250      * Draw the GTU (scheduled method).
251      * @param gtu LaneBasedGTU; the GTU to draw
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      * Draw the static object.
268      * @param object ObjectInterface; the object to draw
269      */
270     protected void animateStaticObject(final ObjectInterface object)
271     {
272         try
273         {
274             if (object instanceof SinkSensor)
275             {
276                 SinkSensor sensor = (SinkSensor) object;
277                 // Renderable2D<SinkSensor> objectAnimation = new SinkAnimation(sensor, this.simulator);
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                 // Renderable2D<DestinationSensor> objectAnimation = new DestinationAnimation(sensor, this.simulator);
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      * Draw the GTUGenerator.
324      * @param gtuGenerator GtuGenerator; the GTUGenerator to draw
325      */
326     protected void animateGTUGenerator(final GtuGenerator gtuGenerator)
327     {
328         // TODO: default animation of GTU generator
329     }
330 
331 }