View Javadoc
1   package org.opentrafficsim.base.geometry;
2   
3   import org.djutils.draw.Drawable2d;
4   import org.djutils.draw.bounds.Bounds;
5   import org.djutils.draw.line.Polygon2d;
6   import org.djutils.draw.point.Point2d;
7   
8   /**
9    * Bounds for generic usage within the OTS context. All input is assumed to be transformed to this bound's space. Default
10   * methods use the polygon representation of the bounds. For many shapes there may be faster methods to determine information.
11   * <p>
12   * Copyright (c) 2024-2024 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
13   * BSD-style license. See <a href="https://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
14   * </p>
15   * @author <a href="https://github.com/wjschakel">Wouter Schakel</a>
16   */
17  public interface OtsBounds2d extends Bounds<OtsBounds2d, Point2d, Drawable2d>
18  {
19  
20      /** Standard mid point. */
21      static Point2d CENTER = new Point2d(0.0, 0.0);
22      
23      /** {@inheritDoc} */
24      @Override
25      default double getMinX()
26      {
27          return asPolygon().getBounds().getMinX();
28      }
29  
30      /** {@inheritDoc} */
31      @Override
32      default double getMaxX()
33      {
34          return asPolygon().getBounds().getMaxX();
35      }
36  
37      /** {@inheritDoc} */
38      @Override
39      default double getMinY()
40      {
41          return asPolygon().getBounds().getMinY();
42      }
43  
44      /** {@inheritDoc} */
45      @Override
46      default double getMaxY()
47      {
48          return asPolygon().getBounds().getMaxY();
49      }
50  
51      /** {@inheritDoc} */
52      @Override
53      default Point2d midPoint()
54      {
55          return CENTER;
56      }
57  
58      /** {@inheritDoc} */
59      @Override
60      default boolean contains(final Point2d point) throws NullPointerException
61      {
62          // do not use signedDistance(), it uses this function
63          return asPolygon().contains(point);
64      }
65  
66      /** {@inheritDoc} */
67      @Override
68      default boolean covers(final Point2d point) throws NullPointerException
69      {
70          boolean contians = contains(point);
71          if (contians)
72          {
73              return true;
74          }
75          return asPolygon().closestPointOnPolyLine(point).distance(point) == 0.0;
76      }
77  
78      /** {@inheritDoc} */
79      @Override
80      default boolean covers(final Drawable2d drawable) throws NullPointerException
81      {
82          return contains(drawable);
83      }
84  
85      /** {@inheritDoc} */
86      @Override
87      default boolean contains(final Drawable2d drawable) throws NullPointerException
88      {
89          // TODO: This checks whether the bounding box of the drawable is contained
90          return asPolygon().contains(drawable.getBounds());
91      }
92  
93      /** {@inheritDoc} */
94      @Override
95      default boolean disjoint(final Drawable2d drawable) throws NullPointerException
96      {
97          return !asPolygon().intersects(new Polygon2d(drawable.getPointList()));
98      }
99  
100     /** {@inheritDoc} */
101     @Override
102     default boolean intersects(final OtsBounds2d otherBounds)
103     {
104         throw new UnsupportedOperationException("Intersects between bounds is not supported.");
105     }
106 
107     /** {@inheritDoc} */
108     @Override
109     default OtsBounds2d intersection(final OtsBounds2d otherBounds)
110     {
111         throw new UnsupportedOperationException("Intersection between bounds is not supported.");
112     }
113 
114     /**
115      * Returns a polygon representation of the bounds, such that an intersection can be derived.
116      * @return Polygon2d; polygon representation of the bounds.
117      */
118     Polygon2d asPolygon();
119 
120     /**
121      * Signed distance function. The coordinates must be transformed to this bound's space. Negative distances returned are
122      * inside the bounds, with the absolute value of the distance towards the edge. The default implementation is based on the
123      * polygon representation and is expensive.
124      * @param point Point2d; point for which distance is returned.
125      * @return double; distance from point to these bounds.
126      */
127     default double signedDistance(final Point2d point)
128     {
129         double dist = asPolygon().closestPointOnPolyLine(point).distance(point);
130         return contains(point) ? -dist : dist;
131     }
132 
133 }