View Javadoc
1   package org.opentrafficsim.draw.network;
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.naming.NamingException;
10  
11  import org.opentrafficsim.core.geometry.OTSGeometryException;
12  import org.opentrafficsim.core.geometry.OTSLine3D;
13  import org.opentrafficsim.core.geometry.OTSPoint3D;
14  import org.opentrafficsim.core.network.Link;
15  import org.opentrafficsim.draw.core.ClonableRenderable2DInterface;
16  import org.opentrafficsim.draw.core.PaintLine;
17  import org.opentrafficsim.draw.core.TextAlignment;
18  import org.opentrafficsim.draw.core.TextAnimation;
19  
20  import nl.tudelft.simulation.dsol.animation.Locatable;
21  import nl.tudelft.simulation.dsol.animation.D2.Renderable2D;
22  import nl.tudelft.simulation.dsol.logger.SimLogger;
23  import nl.tudelft.simulation.dsol.simulators.SimulatorInterface;
24  import nl.tudelft.simulation.language.d2.Angle;
25  import nl.tudelft.simulation.language.d3.DirectedPoint;
26  
27  /**
28   * Draws a Link.
29   * <p>
30   * Copyright (c) 2013-2019 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
31   * BSD-style license. See <a href="http://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
32   * <p>
33   * $LastChangedDate: 2018-10-11 22:54:04 +0200 (Thu, 11 Oct 2018) $, @version $Revision: 4696 $, by $Author: averbraeck $,
34   * initial version Sep 13, 2014 <br>
35   * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
36   */
37  public class LinkAnimation extends Renderable2D<Link> implements ClonableRenderable2DInterface<Link>, Serializable
38  {
39      /** */
40      private static final long serialVersionUID = 20140000L;
41  
42      /** */
43      private float width;
44  
45      /** the Text object to destroy when the animation is destroyed. */
46      private Text text;
47  
48      /**
49       * @param link Link; Link
50       * @param simulator SimulatorInterface.TimeDoubleUnit; simulator
51       * @param width float; width
52       * @throws NamingException for problems with registering in context
53       * @throws RemoteException on communication failure
54       */
55      public LinkAnimation(final Link link, final SimulatorInterface.TimeDoubleUnit simulator, final float width)
56              throws NamingException, RemoteException
57      {
58          super(link, simulator);
59          this.width = width;
60          this.text = new Text(link, link.getId(), 0.0f, 1.5f, TextAlignment.CENTER, Color.BLACK, simulator);
61      }
62  
63      /** {@inheritDoc} */
64      @Override
65      public final void paint(final Graphics2D graphics, final ImageObserver observer) throws RemoteException
66      {
67          Color color = getSource().getLinkType().isConnector() ? Color.BLUE : Color.RED;
68          OTSLine3D designLine = getSource().getDesignLine();
69          PaintLine.paintLine(graphics, color, this.width, getSource().getLocation(), designLine);
70          // Accentuate the end points
71          try
72          {
73              drawEndPoint(designLine.getFirst(), designLine.get(1), graphics);
74              drawEndPoint(designLine.getLast(), designLine.get(designLine.size() - 2), graphics);
75          }
76          catch (OTSGeometryException exception)
77          {
78              // Cannot happen
79              SimLogger.always().error(exception);
80          }
81      }
82  
83      /**
84       * Draw end point on design line.
85       * @param endPoint OTSPoint3D; the end of the design line where a end point must be highlighted
86       * @param nextPoint OTSPoint3D; the point nearest <code>endPoint</code> (needed to figure out the direction of the design
87       *            line)
88       * @param graphics Graphics2D; graphics content
89       */
90      private void drawEndPoint(final OTSPoint3D endPoint, final OTSPoint3D nextPoint, final Graphics2D graphics)
91      {
92          // End point marker is 2 times the width of the design line
93          double dx = nextPoint.x - endPoint.x;
94          double dy = nextPoint.y - endPoint.y;
95          double length = endPoint.distanceSI(nextPoint);
96          // scale dx, dy so that size is this.width
97          dx *= this.width / length;
98          dy *= this.width / length;
99          try
100         {
101             OTSLine3D line = new OTSLine3D(new OTSPoint3D(endPoint.x - dy, endPoint.y + dx, endPoint.z),
102                     new OTSPoint3D(endPoint.x + dy, endPoint.y - dx, endPoint.z));
103             PaintLine.paintLine(graphics, getSource().getLinkType().isConnector() ? Color.BLUE : Color.RED, this.width / 30,
104                     getSource().getLocation(), line);
105         }
106         catch (OTSGeometryException exception)
107         {
108             SimLogger.always().error(exception);
109         }
110         catch (RemoteException exception)
111         {
112             SimLogger.always().error(exception);
113         }
114     }
115 
116     /** {@inheritDoc} */
117     @Override
118     public final void destroy() throws NamingException
119     {
120         super.destroy();
121         this.text.destroy();
122     }
123 
124     /** {@inheritDoc} */
125     @Override
126     @SuppressWarnings("checkstyle:designforextension")
127     public ClonableRenderable2DInterface<Link> clone(final Link newSource, final SimulatorInterface.TimeDoubleUnit newSimulator)
128             throws NamingException, RemoteException
129     {
130         // the constructor also constructs the corresponding Text object
131         return new LinkAnimation(newSource, newSimulator, this.width);
132     }
133 
134     /** {@inheritDoc} */
135     @Override
136     public final String toString()
137     {
138         return "LinkAnimation [width=" + this.width + ", link=" + super.getSource() + "]";
139     }
140 
141     /**
142      * Text animation for the Link. Separate class to be able to turn it on and off...
143      * <p>
144      * Copyright (c) 2013-2019 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved.
145      * <br>
146      * BSD-style license. See <a href="http://opentrafficsim.org/docs/current/license.html">OpenTrafficSim License</a>.
147      * </p>
148      * $LastChangedDate: 2018-10-11 22:54:04 +0200 (Thu, 11 Oct 2018) $, @version $Revision: 4696 $, by $Author: averbraeck $,
149      * initial version Dec 11, 2016 <br>
150      * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
151      * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
152      * @author <a href="http://www.transport.citg.tudelft.nl">Wouter Schakel</a>
153      */
154     public class Text extends TextAnimation
155     {
156         /** */
157         private static final long serialVersionUID = 20161211L;
158 
159         /**
160          * @param source Locatable; the object for which the text is displayed
161          * @param text String; the text to display
162          * @param dx float; the horizontal movement of the text, in meters
163          * @param dy float; the vertical movement of the text, in meters
164          * @param textPlacement TextAlignment; where to place the text
165          * @param color Color; the color of the text
166          * @param simulator SimulatorInterface.TimeDoubleUnit; the simulator
167          * @throws NamingException when animation context cannot be created or retrieved
168          * @throws RemoteException - when remote context cannot be found
169          */
170         public Text(final Locatable source, final String text, final float dx, final float dy,
171                 final TextAlignment textPlacement, final Color color, final SimulatorInterface.TimeDoubleUnit simulator)
172                 throws RemoteException, NamingException
173         {
174             super(source, text, dx, dy, textPlacement, color, simulator);
175         }
176 
177         /** {@inheritDoc} */
178         @Override
179         @SuppressWarnings("checkstyle:designforextension")
180         public DirectedPoint getLocation() throws RemoteException
181         {
182             // draw always on top, and not upside down.
183             DirectedPoint p = ((Link) getSource()).getDesignLine().getLocationFractionExtended(0.5);
184             double a = Angle.normalizePi(p.getRotZ());
185             if (a > Math.PI / 2.0 || a < -0.99 * Math.PI / 2.0)
186             {
187                 a += Math.PI;
188             }
189             return new DirectedPoint(p.x, p.y, Double.MAX_VALUE, 0.0, 0.0, a);
190         }
191 
192         /** {@inheritDoc} */
193         @Override
194         @SuppressWarnings("checkstyle:designforextension")
195         public TextAnimation clone(final Locatable newSource, final SimulatorInterface.TimeDoubleUnit newSimulator)
196                 throws RemoteException, NamingException
197         {
198             return new Text(newSource, getText(), getDx(), getDy(), getTextAlignment(), getColor(), newSimulator);
199         }
200 
201         /** {@inheritDoc} */
202         @Override
203         public final String toString()
204         {
205             return "LinkAnimation.Text []";
206         }
207     }
208 
209 }