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.djutils.draw.line.PolyLine3d;
12  import org.djutils.draw.point.Point3d;
13  import org.djutils.logger.CategoryLogger;
14  import org.opentrafficsim.core.dsol.OtsSimulatorInterface;
15  import org.opentrafficsim.core.geometry.DirectedPoint;
16  import org.opentrafficsim.core.geometry.OtsGeometryException;
17  import org.opentrafficsim.core.geometry.OtsLine3d;
18  import org.opentrafficsim.core.geometry.OtsPoint3d;
19  import org.opentrafficsim.core.network.Link;
20  import org.opentrafficsim.core.network.LinkType;
21  import org.opentrafficsim.draw.core.PaintLine;
22  import org.opentrafficsim.draw.core.TextAlignment;
23  import org.opentrafficsim.draw.core.TextAnimation;
24  
25  import nl.tudelft.simulation.dsol.animation.Locatable;
26  import nl.tudelft.simulation.dsol.animation.D2.Renderable2D;
27  import nl.tudelft.simulation.dsol.animation.D2.Renderable2DInterface;
28  import nl.tudelft.simulation.language.d2.Angle;
29  import nl.tudelft.simulation.naming.context.Contextualized;
30  
31  /**
32   * Draws a Link.
33   * <p>
34   * Copyright (c) 2013-2023 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
35   * BSD-style license. See <a href="https://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
36   * </p>
37   * @author <a href="https://github.com/averbraeck">Alexander Verbraeck</a>
38   */
39  public class LinkAnimation extends Renderable2D<Link> implements Renderable2DInterface<Link>, Serializable
40  {
41      /** */
42      private static final long serialVersionUID = 20140000L;
43  
44      /** */
45      private float width;
46  
47      /** the Text object to destroy when the animation is destroyed. */
48      private Text text;
49  
50      /**
51       * @param link Link; Link
52       * @param simulator OtsSimulatorInterface; simulator
53       * @param width float; width
54       * @throws NamingException for problems with registering in context
55       * @throws RemoteException on communication failure
56       */
57      public LinkAnimation(final Link link, final OtsSimulatorInterface simulator, final float width)
58              throws NamingException, RemoteException
59      {
60          super(link, simulator);
61          this.width = width;
62          this.text = new Text(link, link.getId(), 0.0f, 1.5f, TextAlignment.CENTER, Color.BLACK, simulator,
63                  TextAnimation.RENDERWHEN10);
64      }
65  
66      /** {@inheritDoc} */
67      @Override
68      public final void paint(final Graphics2D graphics, final ImageObserver observer)
69      {
70          Color color = getSource().isConnector() ? Color.PINK.darker() : Color.BLUE;
71          OtsLine3d designLine = getSource().getDesignLine();
72          PaintLine.paintLine(graphics, color, this.width, getSource().getLocation(), designLine);
73          // Accentuate the end points
74          try
75          {
76              drawEndPoint(designLine.getFirst(), designLine.get(1), graphics);
77              drawEndPoint(designLine.getLast(), designLine.get(designLine.size() - 2), graphics);
78          }
79          catch (OtsGeometryException exception)
80          {
81              // Cannot happen
82              CategoryLogger.always().error(exception);
83          }
84      }
85  
86      /**
87       * Draw end point on design line.
88       * @param endPoint OtsPoint3d; the end of the design line where a end point must be highlighted
89       * @param nextPoint OtsPoint3d; the point nearest <code>endPoint</code> (needed to figure out the direction of the design
90       *            line)
91       * @param graphics Graphics2D; graphics content
92       */
93      private void drawEndPoint(final OtsPoint3d endPoint, final OtsPoint3d nextPoint, final Graphics2D graphics)
94      {
95          // End point marker is 2 times the width of the design line
96          double dx = nextPoint.x - endPoint.x;
97          double dy = nextPoint.y - endPoint.y;
98          double length = endPoint.distanceSI(nextPoint);
99          // scale dx, dy so that size is this.width
100         dx *= this.width / length;
101         dy *= this.width / length;
102         PolyLine3d line = new PolyLine3d(new Point3d(endPoint.x - dy, endPoint.y + dx, endPoint.z),
103                 new Point3d(endPoint.x + dy, endPoint.y - dx, endPoint.z));
104         PaintLine.paintLine(graphics, getSource().isConnector() ? Color.PINK.darker() : Color.BLUE, this.width / 30,
105                 getSource().getLocation(), line);
106     }
107 
108     /** {@inheritDoc} */
109     @Override
110     public void destroy(final Contextualized contextProvider)
111     {
112         super.destroy(contextProvider);
113         this.text.destroy(contextProvider);
114     }
115 
116     /** {@inheritDoc} */
117     @Override
118     public final String toString()
119     {
120         return "LinkAnimation [width=" + this.width + ", link=" + super.getSource() + "]";
121     }
122 
123     /**
124      * Text animation for the Link. Separate class to be able to turn it on and off...
125      * <p>
126      * Copyright (c) 2013-2023 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved.
127      * <br>
128      * BSD-style license. See <a href="https://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
129      * </p>
130      * @author <a href="https://github.com/averbraeck">Alexander Verbraeck</a>
131      * @author <a href="https://tudelft.nl/staff/p.knoppers-1">Peter Knoppers</a>
132      * @author <a href="https://dittlab.tudelft.nl">Wouter Schakel</a>
133      */
134     public class Text extends TextAnimation
135     {
136         /** */
137         private static final long serialVersionUID = 20161211L;
138 
139         /**
140          * @param source Locatable; the object for which the text is displayed
141          * @param text String; the text to display
142          * @param dx float; the horizontal movement of the text, in meters
143          * @param dy float; the vertical movement of the text, in meters
144          * @param textPlacement TextAlignment; where to place the text
145          * @param color Color; the color of the text
146          * @param simulator OtsSimulatorInterface; the simulator
147          * @param scaleDependentRendering ScaleDependentRendering; enables rendering in a scale dependent fashion
148          * @throws NamingException when animation context cannot be created or retrieved
149          * @throws RemoteException - when remote context cannot be found
150          */
151         public Text(final Locatable source, final String text, final float dx, final float dy,
152                 final TextAlignment textPlacement, final Color color, final OtsSimulatorInterface simulator,
153                 final ScaleDependentRendering scaleDependentRendering) throws RemoteException, NamingException
154         {
155             super(source, text, dx, dy, textPlacement, color, 2.0f, 12.0f, 50f, simulator, null, scaleDependentRendering);
156         }
157 
158         /** {@inheritDoc} */
159         @Override
160         @SuppressWarnings("checkstyle:designforextension")
161         public DirectedPoint getLocation()
162         {
163             // draw always on top, and not upside down.
164             DirectedPoint p = ((Link) getSource()).getDesignLine().getLocationFractionExtended(0.5);
165             double a = Angle.normalizePi(p.getRotZ());
166             if (a > Math.PI / 2.0 || a < -0.99 * Math.PI / 2.0)
167             {
168                 a += Math.PI;
169             }
170             return new DirectedPoint(p.x, p.y, Double.MAX_VALUE, 0.0, 0.0, a);
171         }
172 
173         /** {@inheritDoc} */
174         @Override
175         public final String toString()
176         {
177             return "LinkAnimation.Text []";
178         }
179     }
180 
181 }