LaneAnimationOD.java

  1. package org.opentrafficsim.road.network.factory.opendrive;

  2. import java.awt.BasicStroke;
  3. import java.awt.Color;
  4. import java.awt.Graphics2D;
  5. import java.awt.geom.Path2D;
  6. import java.awt.image.ImageObserver;
  7. import java.rmi.RemoteException;

  8. import javax.naming.NamingException;
  9. import javax.vecmath.Point2d;

  10. import nl.tudelft.simulation.dsol.animation.D2.Renderable2D;
  11. import nl.tudelft.simulation.language.d3.DirectedPoint;

  12. import org.opentrafficsim.core.dsol.OTSSimulatorInterface;
  13. import org.opentrafficsim.core.geometry.OTSGeometryException;
  14. import org.opentrafficsim.core.geometry.OTSLine3D;
  15. import org.opentrafficsim.core.geometry.OTSPoint3D;
  16. import org.opentrafficsim.core.gtu.GTUType;
  17. import org.opentrafficsim.core.network.LongitudinalDirectionality;
  18. import org.opentrafficsim.core.network.animation.PaintPolygons;
  19. import org.opentrafficsim.road.network.lane.Lane;

  20. /**
  21.  * <p>
  22.  * Copyright (c) 2013-2015 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
  23.  * BSD-style license. See <a href="http://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
  24.  * <p>
  25.  * $LastChangedDate: 2015-09-14 01:33:02 +0200 (Mon, 14 Sep 2015) $, @version $Revision: 1401 $, by $Author: averbraeck $,
  26.  * initial version Oct 17, 2014 <br>
  27.  * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
  28.  */
  29. public class LaneAnimationOD extends Renderable2D
  30. {
  31.     /** Color of the lane. */
  32.     private final Color color;

  33.     /**
  34.      * @param source s
  35.      * @param simulator s
  36.      * @param color color of the lane.
  37.      * @throws NamingException ne
  38.      * @throws RemoteException on communication failure
  39.      */
  40.     public LaneAnimationOD(final Lane source, final OTSSimulatorInterface simulator, final Color color)
  41.         throws NamingException, RemoteException
  42.     {
  43.         super(source, simulator);
  44.         this.color = color;
  45.     }

  46.     /**
  47.      * Paint a road stripe.
  48.      * @param graphics Graphics2D; the graphics context
  49.      * @param color Color; the color of the road stripe
  50.      * @param referencePoint DirectedPoint; offset of the reference point of the lane from the origin
  51.      * @param line OTSLine3D; the coordinates of the center line of the stripe
  52.      */
  53.     public static void paintLine(final Graphics2D graphics, final Color color, final DirectedPoint referencePoint,
  54.         final OTSLine3D line)
  55.     {
  56.         graphics.setColor(color);
  57.         graphics.setStroke(new BasicStroke(0.1f)); // width of the stripe is 0.1m
  58.         Path2D.Double path = new Path2D.Double();
  59.         boolean start = true;
  60.         for (OTSPoint3D point : line.getPoints())
  61.         {
  62.             if (start)
  63.             {
  64.                 path.moveTo(point.x - referencePoint.x, -point.y + referencePoint.y);
  65.                 start = false;
  66.             }
  67.             else
  68.             {
  69.                 path.lineTo(point.x - referencePoint.x, -point.y + referencePoint.y);
  70.             }
  71.         }
  72.         graphics.draw(path);
  73.     }

  74.     /**
  75.      * Draw one arrow on the lane at a specified relative position in a specified color.
  76.      * @param graphics Graphics2D; the graphics context
  77.      * @param color Color; the color of the arrow
  78.      * @param ref DirectedPoint; offset of the reference point of the lane from the origin
  79.      * @param line OTSLine3D; the coordinates of the center line of the lane
  80.      * @param fraction double; the relative position on the lane
  81.      * @param dir LongitudinalDirectionality; the driving direction of the lane
  82.      */
  83.     private static void paintArrow(final Graphics2D graphics, final Color color, final DirectedPoint ref,
  84.         final OTSLine3D line, final double fraction, final LongitudinalDirectionality dir)
  85.     {
  86.         try
  87.         {
  88.             double len = 3.0;
  89.             double ar = 0.5;
  90.             DirectedPoint start = line.getLocationFraction(fraction);
  91.             graphics.setColor(color);
  92.             graphics.setStroke(new BasicStroke(0.2f));
  93.             Path2D.Double path = new Path2D.Double();
  94.             path.moveTo(start.x - ref.x, -start.y + ref.y);
  95.             Point2d end =
  96.                 new Point2d(start.x + len * Math.cos(start.getRotZ()), start.y + len * Math.sin(start.getRotZ()));
  97.             path.lineTo(end.x - ref.x, -end.y + ref.y);
  98.             graphics.draw(path);

  99.             if (dir.equals(LongitudinalDirectionality.DIR_PLUS) || dir.equals(LongitudinalDirectionality.DIR_BOTH))
  100.             {
  101.                 path = new Path2D.Double();
  102.                 path.moveTo(end.x - ref.x, -end.y + ref.y);
  103.                 Point2d ar1 =
  104.                     new Point2d(end.x + ar * Math.cos(start.getRotZ() + 0.75 * Math.PI), end.y + ar
  105.                         * Math.sin(start.getRotZ() + 0.75 * Math.PI));
  106.                 path.lineTo(ar1.x - ref.x, -ar1.y + ref.y);
  107.                 path.moveTo(end.x - ref.x, -end.y + ref.y);
  108.                 Point2d ar2 =
  109.                     new Point2d(end.x + ar * Math.cos(start.getRotZ() + 1.25 * Math.PI), end.y + ar
  110.                         * Math.sin(start.getRotZ() + 1.25 * Math.PI));
  111.                 path.lineTo(ar2.x - ref.x, -ar2.y + ref.y);
  112.                 graphics.draw(path);
  113.             }

  114.             if (dir.equals(LongitudinalDirectionality.DIR_MINUS) || dir.equals(LongitudinalDirectionality.DIR_BOTH))
  115.             {
  116.                 path = new Path2D.Double();
  117.                 path.moveTo(start.x - ref.x, -start.y + ref.y);
  118.                 Point2d ar1 =
  119.                     new Point2d(start.x + ar * Math.cos(start.getRotZ() + 0.25 * Math.PI), start.y + ar
  120.                         * Math.sin(start.getRotZ() + 0.25 * Math.PI));
  121.                 path.lineTo(ar1.x - ref.x, -ar1.y + ref.y);
  122.                 path.moveTo(start.x - ref.x, -start.y + ref.y);
  123.                 Point2d ar2 =
  124.                     new Point2d(start.x + ar * Math.cos(start.getRotZ() - 0.25 * Math.PI), start.y + ar
  125.                         * Math.sin(start.getRotZ() - 0.25 * Math.PI));
  126.                 path.lineTo(ar2.x - ref.x, -ar2.y + ref.y);
  127.                 graphics.draw(path);
  128.             }

  129.         }
  130.         catch (OTSGeometryException exception)
  131.         {
  132.             exception.printStackTrace();
  133.         }
  134.     }

  135.     /** {@inheritDoc} */
  136.     @Override
  137.     public final void paint(final Graphics2D graphics, final ImageObserver observer)
  138.     {
  139.         Lane lane = (Lane) getSource();
  140.         PaintPolygons.paintMultiPolygon(graphics, this.color, lane.getLocation(), lane.getContour(), true);
  141.         // paintLine(graphics, Color.yellow, lane.getLocation(), lane.getCenterLine());
  142.         paintLine(graphics, Color.white, lane.getLocation(), lane.getContour());
  143.         paintArrow(graphics, Color.yellow, lane.getLocation(), lane.getCenterLine(), 0.25,
  144.             lane.getDirectionality(GTUType.ALL));
  145.         paintArrow(graphics, Color.green, lane.getLocation(), lane.getCenterLine(), 0.50,
  146.             lane.getDirectionality(GTUType.ALL));
  147.         paintArrow(graphics, Color.blue, lane.getLocation(), lane.getCenterLine(), 0.75,
  148.             lane.getDirectionality(GTUType.ALL));
  149.     }
  150. }