View Javadoc
1   package org.opentrafficsim.draw.road;
2   
3   import java.awt.BasicStroke;
4   import java.awt.Color;
5   import java.awt.Graphics2D;
6   import java.awt.geom.Path2D;
7   import java.awt.image.ImageObserver;
8   import java.util.Set;
9   
10  import org.djutils.draw.line.PolyLine2d;
11  import org.opentrafficsim.base.geometry.OtsLocatable;
12  import org.opentrafficsim.draw.ClickableLocatable;
13  import org.opentrafficsim.draw.DrawLevel;
14  import org.opentrafficsim.draw.OtsRenderable;
15  import org.opentrafficsim.draw.PaintPolygons;
16  import org.opentrafficsim.draw.road.CrossSectionElementAnimation.CrossSectionElementData;
17  
18  import nl.tudelft.simulation.naming.context.Contextualized;
19  
20  /**
21   * Draws cross section elements (those that are not defined more specifically).
22   * <p>
23   * Copyright (c) 2013-2024 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
24   * BSD-style license. See <a href="https://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
25   * </p>
26   * @author <a href="https://github.com/averbraeck">Alexander Verbraeck</a>
27   * @author <a href="https://github.com/wjschakel">Wouter Schakel</a>
28   * @param <L> cross section element data type
29   */
30  public class CrossSectionElementAnimation<L extends CrossSectionElementData> extends OtsRenderable<L>
31  {
32      /** */
33      private static final long serialVersionUID = 20141017L;
34  
35      /** The animation color. */
36      private final Color color;
37  
38      /** Drawable paths. */
39      private final Set<Path2D.Float> paths;
40  
41      /**
42       * @param source cross section element
43       * @param contextualized context provider
44       * @param color the color to draw the shoulder with
45       */
46      public CrossSectionElementAnimation(final L source, final Contextualized contextualized, final Color color)
47      {
48          super(source, contextualized);
49          this.color = color;
50          this.paths = PaintPolygons.getPaths(OtsLocatable.relativeContour(source).getPointList());
51      }
52  
53      @Override
54      public void paint(final Graphics2D graphics, final ImageObserver observer)
55      {
56          setRendering(graphics);
57          PaintPolygons.paintPaths(graphics, this.color, this.paths, true);
58          // drawing some extra width by painting the edge (i.e. fill = false) prevents anti-alias lines between adjacent elements
59          double scale = Math.min(Math.max(3.0 / graphics.getTransform().getDeterminant(), 0.1), 0.5);
60          graphics.setStroke(new BasicStroke((float) scale, BasicStroke.CAP_SQUARE, BasicStroke.JOIN_MITER));
61          PaintPolygons.paintPaths(graphics, this.color, this.paths, false);
62          resetRendering(graphics);
63      }
64  
65      @Override
66      public String toString()
67      {
68          return "CrossSectionElementAnimation [source = " + getSource().toString() + ", color=" + this.color + "]";
69      }
70  
71      /**
72       * CrossSectionElementData provides the information required to draw a cross section element.
73       * <p>
74       * Copyright (c) 2023-2024 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved.
75       * <br>
76       * BSD-style license. See <a href="https://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
77       * </p>
78       * @author <a href="https://github.com/wjschakel">Wouter Schakel</a>
79       */
80      public interface CrossSectionElementData extends ClickableLocatable
81      {
82          /**
83           * Returns the center line in world coordinates.
84           * @return the center line in world coordinates
85           */
86          PolyLine2d getCenterLine();
87  
88          /**
89           * Return the id of the link.
90           * @return link id.
91           */
92          String getLinkId();
93      }
94  
95      /**
96       * ShoulderData provides the information required to draw a shoulder.
97       * <p>
98       * Copyright (c) 2023-2024 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved.
99       * <br>
100      * BSD-style license. See <a href="https://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
101      * </p>
102      * @author <a href="https://github.com/wjschakel">Wouter Schakel</a>
103      */
104     public interface ShoulderData extends CrossSectionElementData
105     {
106         @Override
107         default double getZ()
108         {
109             return DrawLevel.SHOULDER.getZ();
110         }
111     }
112 }