View Javadoc
1   package org.opentrafficsim.base.geometry;
2   
3   import java.util.Iterator;
4   
5   import org.djutils.draw.Transform2d;
6   import org.djutils.draw.line.PolyLine2d;
7   import org.djutils.draw.line.Polygon2d;
8   import org.djutils.draw.point.OrientedPoint2d;
9   import org.djutils.draw.point.Point2d;
10  
11  /**
12   * Bounds defined by a polygon.
13   * <p>
14   * Copyright (c) 2024-2024 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
15   * BSD-style license. See <a href="https://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
16   * </p>
17   * @author <a href="https://github.com/wjschakel">Wouter Schakel</a>
18   */
19  public class BoundingPolygon implements OtsBounds2d
20  {
21  
22      /** Polygon. */
23      private final Polygon2d polygon;
24  
25      /**
26       * Constructor.
27       * @param polygon Polygon2d; polygon.
28       */
29      public BoundingPolygon(final Polygon2d polygon)
30      {
31          this.polygon = polygon;
32      }
33  
34      /** {@inheritDoc} */
35      @Override
36      public Polygon2d asPolygon()
37      {
38          return this.polygon;
39      }
40  
41      /**
42       * Translates absolute geometry to bounds relative to location, including rotation.
43       * @param location OrientedPoint2d; location.
44       * @param geometry PolyLine2d; geometry..
45       * @return BoundingPolygon; bounded polygon.
46       */
47      public static BoundingPolygon geometryToBounds(final OrientedPoint2d location, final PolyLine2d geometry)
48      {
49          Transform2d transformation = OtsRenderable.toBoundsTransform(location);
50          Iterator<Point2d> itSource = geometry.getPoints();
51          Iterator<Point2d> itTarget = new Iterator<>()
52          {
53              /** {@inheritDoc} */
54              @Override
55              public boolean hasNext()
56              {
57                  return itSource.hasNext();
58              }
59  
60              /** {@inheritDoc} */
61              @Override
62              public Point2d next()
63              {
64                  return transformation.transform(itSource.next());
65              }
66          };
67          BoundingPolygon b = new BoundingPolygon(new Polygon2d(itTarget));
68          return b;
69      }
70  
71  }