View Javadoc
1   package org.opentrafficsim.road.network.factory.opendrive;
2   
3   import java.awt.Color;
4   import java.awt.Graphics2D;
5   import java.awt.geom.Rectangle2D;
6   import java.awt.image.ImageObserver;
7   import java.io.Serializable;
8   import java.rmi.RemoteException;
9   
10  import javax.media.j3d.Bounds;
11  import javax.naming.NamingException;
12  
13  import org.djunits.value.vdouble.scalar.Length;
14  import org.opentrafficsim.core.geometry.OTSGeometryException;
15  import org.opentrafficsim.road.network.lane.Lane;
16  
17  import nl.tudelft.simulation.dsol.animation.Locatable;
18  import nl.tudelft.simulation.dsol.animation.D2.Renderable2D;
19  import nl.tudelft.simulation.dsol.simulators.SimulatorInterface;
20  import nl.tudelft.simulation.language.d3.BoundingBox;
21  import nl.tudelft.simulation.language.d3.DirectedPoint;
22  
23  /**
24   * sink sensor animation.
25   * <p>
26   * Copyright (c) 2013-2019 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands.<br>
27   * All rights reserved. <br>
28   * BSD-style license. See <a href="http://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
29   * <p>
30   * $LastChangedDate: 2015-08-12 16:37:45 +0200 (Wed, 12 Aug 2015) $, @version $Revision: 1240 $, by $Author: averbraeck $,
31   * initial version Jan 30, 2015 <br>
32   * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
33   * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
34   */
35  public class GeneratorAnimation extends Renderable2D implements Serializable
36  {
37      /** */
38      private static final long serialVersionUID = 20150130L;
39  
40      /** The half width left and right of the center line that is used to draw the block. */
41      private final double halfWidth;
42  
43      /**
44       * Construct the DefaultCarAnimation for a LaneBlock (road block).
45       * @param lane Lane; the lane where the generator is
46       * @param position Length; the position on the lane
47       * @param simulator SimulatorInterface.TimeDoubleUnit; the simulator to schedule on
48       * @throws NamingException in case of registration failure of the animation
49       * @throws RemoteException in case of remote registration failure of the animation
50       * @throws OTSGeometryException if position out of bounds
51       */
52      public GeneratorAnimation(final Lane lane, final Length position, final SimulatorInterface.TimeDoubleUnit simulator)
53              throws NamingException, RemoteException, OTSGeometryException
54      {
55          super(new GenPos(lane, position), simulator);
56          this.halfWidth = 0.4 * (lane.getBeginWidth().si + lane.getEndWidth().si) / 2.0;
57          // based on avg width
58      }
59  
60      /** {@inheritDoc} */
61      @Override
62      public final void paint(final Graphics2D graphics, final ImageObserver observer)
63      {
64          graphics.setColor(Color.BLUE);
65          Rectangle2D rectangle = new Rectangle2D.Double(-0.25, -this.halfWidth, 0.5, 2 * this.halfWidth);
66          graphics.fill(rectangle);
67      }
68  
69      /** {@inheritDoc} */
70      @Override
71      public final String toString()
72      {
73          return "GeneratorAnimation [getSource()=" + this.getSource() + "]";
74      }
75  
76      /** Generator position. */
77      private static class GenPos implements Locatable, Serializable
78      {
79          /** */
80          private static final long serialVersionUID = 20150000L;
81  
82          /** Location. */
83          private DirectedPoint location;
84  
85          /** Lane width. */
86          private double width;
87  
88          /**
89           * @param lane Lane; the lane where the generator is
90           * @param position Length; the position on the lane
91           * @throws OTSGeometryException on position out of bounds
92           */
93          public GenPos(final Lane lane, final Length position) throws OTSGeometryException
94          {
95              this.location = lane.getCenterLine().getLocation(position);
96              this.width = (lane.getBeginWidth().si + lane.getEndWidth().si) / 2.0; // avg width
97          }
98  
99          /** {@inheritDoc} */
100         @Override
101         public DirectedPoint getLocation() throws RemoteException
102         {
103             return this.location;
104         }
105 
106         /** {@inheritDoc} */
107         @Override
108         public Bounds getBounds() throws RemoteException
109         {
110             return new BoundingBox(this.width, this.width, 0.0);
111         }
112 
113         /** {@inheritDoc} */
114         @Override
115         public final String toString()
116         {
117             return "GenPos [location=" + this.location + ", width=" + this.width + "]";
118         }
119 
120     }
121 }