TrafficLightSensorAnimation.java

  1. package org.opentrafficsim.draw.road;

  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 java.util.ArrayList;
  8. import java.util.List;

  9. import javax.naming.NamingException;

  10. import org.opentrafficsim.core.dsol.OTSSimulatorInterface;
  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.road.network.lane.object.sensor.TrafficLightSensor;

  15. import nl.tudelft.simulation.dsol.animation.D2.Renderable2D;

  16. /**
  17.  * sink sensor animation.
  18.  * <p>
  19.  * Copyright (c) 2013-2022 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands.<br>
  20.  * All rights reserved. <br>
  21.  * BSD-style license. See <a href="http://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
  22.  * <p>
  23.  * $LastChangedDate: 2015-08-12 16:37:45 +0200 (Wed, 12 Aug 2015) $, @version $Revision: 1240 $, by $Author: averbraeck $,
  24.  * initial version Jan 30, 2015 <br>
  25.  * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
  26.  * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
  27.  */
  28. public class TrafficLightSensorAnimation extends Renderable2D<TrafficLightSensor> implements Serializable
  29. {
  30.     /** */
  31.     private static final long serialVersionUID = 20150130L;

  32.     /** The traffic light sensor. */
  33.     private final TrafficLightSensor sensor;

  34.     /** Path of the detector. */
  35.     private final OTSLine3D path;

  36.     /**
  37.      * Construct a SensorAnimation.
  38.      * @param sensor TrafficLightSensor; the traffic light sensor that will be animated
  39.      * @param simulator OTSSimulatorInterface; the simulator to schedule on
  40.      * @throws NamingException in case of registration failure of the animation
  41.      * @throws RemoteException in case of remote registration failure of the animation
  42.      * @throws OTSGeometryException when the geometry is bad
  43.      */
  44.     public TrafficLightSensorAnimation(final TrafficLightSensor sensor, final OTSSimulatorInterface simulator)
  45.             throws NamingException, RemoteException, OTSGeometryException
  46.     {
  47.         super(sensor, simulator);
  48.         this.sensor = sensor;
  49.         OTSLine3D coordinates = this.sensor.getPath();
  50.         double dx = this.sensor.getLocation().x;
  51.         double dy = this.sensor.getLocation().y;
  52.         double dz = this.sensor.getLocation().z;
  53.         List<OTSPoint3D> points = new ArrayList<>(coordinates.size());
  54.         for (OTSPoint3D p : coordinates.getPoints())
  55.         {
  56.             points.add(new OTSPoint3D(p.x - dx, p.y - dy, p.z - dz));
  57.         }
  58.         this.path = new OTSLine3D(points);
  59.     }

  60.     /** {@inheritDoc} */
  61.     @Override
  62.     public final void paint(final Graphics2D graphics, final ImageObserver observer)
  63.     {
  64.         graphics.setColor(this.sensor.getOccupancy() ? Color.BLUE : Color.BLACK);
  65.         OTSPoint3D prevPoint = null;
  66.         for (OTSPoint3D p : this.path.getPoints())
  67.         {
  68.             if (null != prevPoint)
  69.             {
  70.                 // System.out.println("Drawing sensor line from " + prevPoint + " to " + p);
  71.                 graphics.drawLine((int) prevPoint.x, (int) prevPoint.y, (int) p.x, (int) p.y);
  72.             }
  73.             prevPoint = p;
  74.         }
  75.     }

  76.     /** {@inheritDoc} */
  77.     @Override
  78.     public final String toString()
  79.     {
  80.         return "SensorAnimation [getSource()=" + this.getSource() + "]";
  81.     }

  82. }