LinkAnimation.java

  1. package org.opentrafficsim.draw.network;

  2. import java.awt.Color;
  3. import java.awt.Graphics2D;
  4. import java.awt.image.ImageObserver;
  5. import java.io.Serializable;
  6. import java.rmi.RemoteException;

  7. import javax.naming.NamingException;

  8. import org.djutils.logger.CategoryLogger;
  9. import org.opentrafficsim.core.geometry.OTSGeometryException;
  10. import org.opentrafficsim.core.geometry.OTSLine3D;
  11. import org.opentrafficsim.core.geometry.OTSPoint3D;
  12. import org.opentrafficsim.core.network.Link;
  13. import org.opentrafficsim.core.network.LinkType;
  14. import org.opentrafficsim.draw.core.ClonableRenderable2DInterface;
  15. import org.opentrafficsim.draw.core.PaintLine;
  16. import org.opentrafficsim.draw.core.TextAlignment;
  17. import org.opentrafficsim.draw.core.TextAnimation;

  18. import nl.tudelft.simulation.dsol.animation.Locatable;
  19. import nl.tudelft.simulation.dsol.animation.D2.Renderable2D;
  20. import nl.tudelft.simulation.dsol.simulators.SimulatorInterface;
  21. import nl.tudelft.simulation.language.d2.Angle;
  22. import nl.tudelft.simulation.language.d3.DirectedPoint;

  23. /**
  24.  * Draws a Link.
  25.  * <p>
  26.  * Copyright (c) 2013-2020 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
  27.  * BSD-style license. See <a href="http://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
  28.  * <p>
  29.  * $LastChangedDate: 2018-10-11 22:54:04 +0200 (Thu, 11 Oct 2018) $, @version $Revision: 4696 $, by $Author: averbraeck $,
  30.  * initial version Sep 13, 2014 <br>
  31.  * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
  32.  */
  33. public class LinkAnimation extends Renderable2D<Link> implements ClonableRenderable2DInterface<Link>, Serializable
  34. {
  35.     /** */
  36.     private static final long serialVersionUID = 20140000L;

  37.     /** */
  38.     private float width;

  39.     /** the Text object to destroy when the animation is destroyed. */
  40.     private Text text;

  41.     /**
  42.      * @param link Link; Link
  43.      * @param simulator SimulatorInterface.TimeDoubleUnit; simulator
  44.      * @param width float; width
  45.      * @throws NamingException for problems with registering in context
  46.      * @throws RemoteException on communication failure
  47.      */
  48.     public LinkAnimation(final Link link, final SimulatorInterface.TimeDoubleUnit simulator, final float width)
  49.             throws NamingException, RemoteException
  50.     {
  51.         super(link, simulator);
  52.         this.width = width;
  53.         this.text = new Text(link, link.getId(), 0.0f, 1.5f, TextAlignment.CENTER, Color.BLACK, simulator,
  54.                 link.getLinkType().getId().equals(LinkType.DEFAULTS.FREEWAY.getId()) ? TextAnimation.RENDERWHEN10
  55.                         : TextAnimation.RENDERWHEN1);
  56.     }

  57.     /** {@inheritDoc} */
  58.     @Override
  59.     public final void paint(final Graphics2D graphics, final ImageObserver observer) throws RemoteException
  60.     {
  61.         Color color = getSource().getLinkType().isConnector() ? Color.PINK.darker() : Color.BLUE;
  62.         OTSLine3D designLine = getSource().getDesignLine();
  63.         PaintLine.paintLine(graphics, color, this.width, getSource().getLocation(), designLine);
  64.         // Accentuate the end points
  65.         try
  66.         {
  67.             drawEndPoint(designLine.getFirst(), designLine.get(1), graphics);
  68.             drawEndPoint(designLine.getLast(), designLine.get(designLine.size() - 2), graphics);
  69.         }
  70.         catch (OTSGeometryException exception)
  71.         {
  72.             // Cannot happen
  73.             CategoryLogger.always().error(exception);
  74.         }
  75.     }

  76.     /**
  77.      * Draw end point on design line.
  78.      * @param endPoint OTSPoint3D; the end of the design line where a end point must be highlighted
  79.      * @param nextPoint OTSPoint3D; the point nearest <code>endPoint</code> (needed to figure out the direction of the design
  80.      *            line)
  81.      * @param graphics Graphics2D; graphics content
  82.      */
  83.     private void drawEndPoint(final OTSPoint3D endPoint, final OTSPoint3D nextPoint, final Graphics2D graphics)
  84.     {
  85.         // End point marker is 2 times the width of the design line
  86.         double dx = nextPoint.x - endPoint.x;
  87.         double dy = nextPoint.y - endPoint.y;
  88.         double length = endPoint.distanceSI(nextPoint);
  89.         // scale dx, dy so that size is this.width
  90.         dx *= this.width / length;
  91.         dy *= this.width / length;
  92.         try
  93.         {
  94.             OTSLine3D line = new OTSLine3D(new OTSPoint3D(endPoint.x - dy, endPoint.y + dx, endPoint.z),
  95.                     new OTSPoint3D(endPoint.x + dy, endPoint.y - dx, endPoint.z));
  96.             PaintLine.paintLine(graphics, getSource().getLinkType().isConnector() ? Color.PINK.darker() : Color.BLUE,
  97.                     this.width / 30, getSource().getLocation(), line);
  98.         }
  99.         catch (OTSGeometryException exception)
  100.         {
  101.             CategoryLogger.always().error(exception);
  102.         }
  103.         catch (RemoteException exception)
  104.         {
  105.             CategoryLogger.always().error(exception);
  106.         }
  107.     }

  108.     /** {@inheritDoc} */
  109.     @Override
  110.     public final void destroy() throws NamingException, RemoteException
  111.     {
  112.         super.destroy();
  113.         this.text.destroy();
  114.     }

  115.     /** {@inheritDoc} */
  116.     @Override
  117.     @SuppressWarnings("checkstyle:designforextension")
  118.     public ClonableRenderable2DInterface<Link> clone(final Link newSource, final SimulatorInterface.TimeDoubleUnit newSimulator)
  119.             throws NamingException, RemoteException
  120.     {
  121.         // the constructor also constructs the corresponding Text object
  122.         return new LinkAnimation(newSource, newSimulator, this.width);
  123.     }

  124.     /** {@inheritDoc} */
  125.     @Override
  126.     public final String toString()
  127.     {
  128.         return "LinkAnimation [width=" + this.width + ", link=" + super.getSource() + "]";
  129.     }

  130.     /**
  131.      * Text animation for the Link. Separate class to be able to turn it on and off...
  132.      * <p>
  133.      * Copyright (c) 2013-2020 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved.
  134.      * <br>
  135.      * BSD-style license. See <a href="http://opentrafficsim.org/docs/current/license.html">OpenTrafficSim License</a>.
  136.      * </p>
  137.      * $LastChangedDate: 2018-10-11 22:54:04 +0200 (Thu, 11 Oct 2018) $, @version $Revision: 4696 $, by $Author: averbraeck $,
  138.      * initial version Dec 11, 2016 <br>
  139.      * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
  140.      * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
  141.      * @author <a href="http://www.transport.citg.tudelft.nl">Wouter Schakel</a>
  142.      */
  143.     public class Text extends TextAnimation
  144.     {
  145.         /** */
  146.         private static final long serialVersionUID = 20161211L;

  147.         /**
  148.          * @param source Locatable; the object for which the text is displayed
  149.          * @param text String; the text to display
  150.          * @param dx float; the horizontal movement of the text, in meters
  151.          * @param dy float; the vertical movement of the text, in meters
  152.          * @param textPlacement TextAlignment; where to place the text
  153.          * @param color Color; the color of the text
  154.          * @param simulator SimulatorInterface.TimeDoubleUnit; the simulator
  155.          * @param scaleDependentRendering ScaleDependentRendering; enables rendering in a scale dependent fashion
  156.          * @throws NamingException when animation context cannot be created or retrieved
  157.          * @throws RemoteException - when remote context cannot be found
  158.          */
  159.         public Text(final Locatable source, final String text, final float dx, final float dy,
  160.                 final TextAlignment textPlacement, final Color color, final SimulatorInterface.TimeDoubleUnit simulator,
  161.                 final ScaleDependentRendering scaleDependentRendering) throws RemoteException, NamingException
  162.         {
  163.             super(source, text, dx, dy, textPlacement, color, 2.0f, 12.0f, 50f, simulator, null, scaleDependentRendering);
  164.         }

  165.         /** {@inheritDoc} */
  166.         @Override
  167.         @SuppressWarnings("checkstyle:designforextension")
  168.         public DirectedPoint getLocation() throws RemoteException
  169.         {
  170.             // draw always on top, and not upside down.
  171.             DirectedPoint p = ((Link) getSource()).getDesignLine().getLocationFractionExtended(0.5);
  172.             double a = Angle.normalizePi(p.getRotZ());
  173.             if (a > Math.PI / 2.0 || a < -0.99 * Math.PI / 2.0)
  174.             {
  175.                 a += Math.PI;
  176.             }
  177.             return new DirectedPoint(p.x, p.y, Double.MAX_VALUE, 0.0, 0.0, a);
  178.         }

  179.         /** {@inheritDoc} */
  180.         @Override
  181.         @SuppressWarnings("checkstyle:designforextension")
  182.         public TextAnimation clone(final Locatable newSource, final SimulatorInterface.TimeDoubleUnit newSimulator)
  183.                 throws RemoteException, NamingException
  184.         {
  185.             return new Text(newSource, getText(), getDx(), getDy(), getTextAlignment(), getColor(), newSimulator,
  186.                     super.getScaleDependentRendering());
  187.         }

  188.         /** {@inheritDoc} */
  189.         @Override
  190.         public final String toString()
  191.         {
  192.             return "LinkAnimation.Text []";
  193.         }
  194.     }

  195. }