View Javadoc
1   package org.opentrafficsim.draw.road;
2   
3   import java.awt.Color;
4   import java.awt.Graphics2D;
5   import java.awt.image.ImageObserver;
6   import java.io.Serializable;
7   import java.rmi.RemoteException;
8   
9   import javax.media.j3d.Bounds;
10  import javax.naming.NamingException;
11  
12  import org.opentrafficsim.core.geometry.OTSLine3D;
13  import org.opentrafficsim.draw.core.ClonableRenderable2DInterface;
14  import org.opentrafficsim.draw.core.PaintLine;
15  import org.opentrafficsim.draw.core.PaintPolygons;
16  import org.opentrafficsim.draw.core.TextAlignment;
17  import org.opentrafficsim.draw.core.TextAnimation;
18  import org.opentrafficsim.road.network.lane.Lane;
19  
20  import nl.tudelft.simulation.dsol.animation.Locatable;
21  import nl.tudelft.simulation.dsol.animation.D2.Renderable2D;
22  import nl.tudelft.simulation.dsol.simulators.SimulatorInterface;
23  import nl.tudelft.simulation.dsol.simulators.SimulatorInterface.TimeDoubleUnit;
24  import nl.tudelft.simulation.language.d2.Angle;
25  import nl.tudelft.simulation.language.d3.DirectedPoint;
26  
27  /**
28   * <p>
29   * Copyright (c) 2013-2020 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
30   * BSD-style license. See <a href="http://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
31   * <p>
32   * $LastChangedDate: 2015-09-14 01:33:02 +0200 (Mon, 14 Sep 2015) $, @version $Revision: 1401 $, by $Author: averbraeck $,
33   * initial version Oct 17, 2014 <br>
34   * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
35   */
36  public class LaneAnimation extends Renderable2D<Lane> implements ClonableRenderable2DInterface<Lane>, Serializable
37  {
38      /** */
39      private static final long serialVersionUID = 20141017L;
40  
41      /** Color of the lane. */
42      private final Color color;
43  
44      /** the Text object to destroy when the animation is destroyed. */
45      private final Text text;
46  
47      /**
48       * Animate a Lane.
49       * @param lane Lane; the lane
50       * @param simulator SimulatorInterface.TimeDoubleUnit; the simulator
51       * @param color Color; Color of the lane.
52       * @throws NamingException in case of registration failure of the animation
53       * @throws RemoteException on communication failure
54       */
55      public LaneAnimation(final Lane lane, final SimulatorInterface.TimeDoubleUnit simulator, final Color color)
56              throws NamingException, RemoteException
57      {
58          super(lane, simulator);
59          this.color = color;
60          this.text = new Text(lane, lane.getParentLink().getId() + "." + lane.getId(), 0.0f, 0.0f, TextAlignment.CENTER,
61                  Color.BLACK, simulator);
62          new CenterLineAnimation(new CenterLine(lane.getCenterLine()), simulator);
63      }
64  
65      /**
66       * @return text.
67       */
68      public final Text getText()
69      {
70          return this.text;
71      }
72  
73      /** {@inheritDoc} */
74      @Override
75      public final void paint(final Graphics2D graphics, final ImageObserver observer)
76      {
77          Lane lane = getSource();
78          if (this.color != null)
79          {
80              PaintPolygons.paintMultiPolygon(graphics, this.color, lane.getLocation(), lane.getContour(), true);
81          }
82      }
83  
84      /** {@inheritDoc} */
85      @Override
86      public final void destroy() throws NamingException, RemoteException
87      {
88          super.destroy();
89          this.text.destroy();
90      }
91  
92      /** {@inheritDoc} */
93      @Override
94      @SuppressWarnings("checkstyle:designforextension")
95      public ClonableRenderable2DInterface<Lane> clone(final Lane newSource, final SimulatorInterface.TimeDoubleUnit newSimulator)
96              throws NamingException, RemoteException
97      {
98          // the constructor also constructs the corresponding Text object
99          return new LaneAnimation(newSource, newSimulator, this.color);
100     }
101 
102     /** {@inheritDoc} */
103     @Override
104     public final String toString()
105     {
106         return "LaneAnimation [lane = " + getSource().toString() + ", color=" + this.color + "]";
107     }
108 
109     /**
110      * Draw center line of a lane.
111      */
112     public static class CenterLine implements Locatable
113     {
114         /** The center line. */
115         private final OTSLine3D centerLine;
116 
117         /**
118          * Construct a new CenterLine.
119          * @param centerLine OTSLine3D; the center line of a lane
120          */
121         CenterLine(final OTSLine3D centerLine)
122         {
123             this.centerLine = centerLine;
124         }
125 
126         @Override
127         public final DirectedPoint getLocation() throws RemoteException
128         {
129             DirectedPoint dp = this.centerLine.getLocation();
130             return new DirectedPoint(dp.x, dp.y, dp.z + 0.1);
131         }
132 
133         @Override
134         public final Bounds getBounds() throws RemoteException
135         {
136             return this.centerLine.getBounds();
137         }
138 
139         /**
140          * Retrieve the center line.
141          * @return OTSLine3D; the center line
142          */
143         public OTSLine3D getCenterLine()
144         {
145             return centerLine;
146         }
147 
148     }
149 
150     /**
151      * Animation for center line of a lane.
152      */
153     public static class CenterLineAnimation extends Renderable2D<CenterLine>
154             implements ClonableRenderable2DInterface<CenterLine>, Serializable
155     {
156         /** Drawing color for the center line. */
157         private static final Color COLOR = Color.MAGENTA.darker().darker();
158 
159         /**  */
160         private static final long serialVersionUID = 20180426L;
161 
162         /**
163          * Construct a new CenterLineAnimation.
164          * @param centerLine CemterLine; the center line of a lane
165          * @param simulator SimulatorInterface.TimeDoubleUnit; the simulator
166          * @throws NamingException when the name of this object is not unique
167          * @throws RemoteException when communication with a remote process fails
168          */
169         public CenterLineAnimation(final CenterLine centerLine, final SimulatorInterface.TimeDoubleUnit simulator)
170                 throws NamingException, RemoteException
171         {
172             super(centerLine, simulator);
173         }
174 
175         @Override
176         public final ClonableRenderable2DInterface<CenterLine> clone(final CenterLine newSource,
177                 final TimeDoubleUnit newSimulator) throws NamingException, RemoteException
178         {
179             // TODO Auto-generated method stub
180             return null;
181         }
182 
183         @Override
184         public final void paint(final Graphics2D graphics, final ImageObserver observer) throws RemoteException
185         {
186             PaintLine.paintLine(graphics, COLOR, 0.1, getSource().getLocation(), ((CenterLine) getSource()).getCenterLine());
187         }
188 
189     }
190 
191     /**
192      * Text animation for the Node. Separate class to be able to turn it on and off...
193      * <p>
194      * Copyright (c) 2013-2020 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved.
195      * <br>
196      * BSD-style license. See <a href="http://opentrafficsim.org/docs/current/license.html">OpenTrafficSim License</a>.
197      * </p>
198      * $LastChangedDate: 2015-07-24 02:58:59 +0200 (Fri, 24 Jul 2015) $, @version $Revision: 1147 $, by $Author: averbraeck $,
199      * initial version Dec 11, 2016 <br>
200      * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
201      * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
202      * @author <a href="http://www.transport.citg.tudelft.nl">Wouter Schakel</a>
203      */
204     public class Text extends TextAnimation
205     {
206         /** */
207         private static final long serialVersionUID = 20161211L;
208 
209         /**
210          * @param source Locatable; the object for which the text is displayed
211          * @param text String; the text to display
212          * @param dx float; the horizontal movement of the text, in meters
213          * @param dy float; the vertical movement of the text, in meters
214          * @param textPlacement TextAlignment; where to place the text
215          * @param color Color; the color of the text
216          * @param simulator SimulatorInterface.TimeDoubleUnit; the simulator
217          * @throws NamingException when animation context cannot be created or retrieved
218          * @throws RemoteException - when remote context cannot be found
219          */
220         public Text(final Locatable source, final String text, final float dx, final float dy,
221                 final TextAlignment textPlacement, final Color color, final SimulatorInterface.TimeDoubleUnit simulator)
222                 throws RemoteException, NamingException
223         {
224             super(source, text, dx, dy, textPlacement, color, simulator, TextAnimation.RENDERALWAYS);
225         }
226 
227         /** {@inheritDoc} */
228         @Override
229         @SuppressWarnings("checkstyle:designforextension")
230         public DirectedPoint getLocation() throws RemoteException
231         {
232             // draw always on top.
233             DirectedPoint p = ((Lane) getSource()).getCenterLine().getLocationFractionExtended(0.5);
234             double a = Angle.normalizePi(p.getRotZ());
235             if (a > Math.PI / 2.0 || a < -0.99 * Math.PI / 2.0)
236             {
237                 a += Math.PI;
238             }
239             return new DirectedPoint(p.x, p.y, Double.MAX_VALUE, 0.0, 0.0, a);
240         }
241 
242         /** {@inheritDoc} */
243         @Override
244         @SuppressWarnings("checkstyle:designforextension")
245         public TextAnimation clone(final Locatable newSource, final SimulatorInterface.TimeDoubleUnit newSimulator)
246                 throws RemoteException, NamingException
247         {
248             return new Text(newSource, getText(), getDx(), getDy(), getTextAlignment(), getColor(), newSimulator);
249         }
250 
251         /** {@inheritDoc} */
252         @Override
253         public final String toString()
254         {
255             return "Text []";
256         }
257 
258     }
259 
260 }