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