BoundingBox.java

  1. package org.opentrafficsim.base.geometry;

  2. import org.djutils.draw.line.Polygon2d;
  3. import org.djutils.draw.point.Point2d;

  4. /**
  5.  * Bounds defined by a rectangle.
  6.  * <p>
  7.  * Copyright (c) 2024-2024 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
  8.  * BSD-style license. See <a href="https://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
  9.  * </p>
  10.  * @author <a href="https://github.com/wjschakel">Wouter Schakel</a>
  11.  */
  12. public class BoundingBox implements OtsBounds2d
  13. {

  14.     /** Half length along y dimension. */
  15.     private final double dx;

  16.     /** Half length along y dimension. */
  17.     private final double dy;

  18.     /** Polygon representation. */
  19.     private Polygon2d polygon;

  20.     /**
  21.      * Constructor.
  22.      * @param dx double; complete length along x dimension.
  23.      * @param dy double; complete length along y dimension.
  24.      */
  25.     public BoundingBox(final double dx, final double dy)
  26.     {
  27.         this.dx = Math.abs(dx) / 2.0;
  28.         this.dy = Math.abs(dy) / 2.0;
  29.     }

  30.     /** {@inheritDoc} */
  31.     @Override
  32.     public double getMinX()
  33.     {
  34.         return -this.dx;
  35.     }

  36.     /** {@inheritDoc} */
  37.     @Override
  38.     public double getMaxX()
  39.     {
  40.         return this.dx;
  41.     }

  42.     /** {@inheritDoc} */
  43.     @Override
  44.     public double getMinY()
  45.     {
  46.         return -this.dy;
  47.     }

  48.     /** {@inheritDoc} */
  49.     @Override
  50.     public double getMaxY()
  51.     {
  52.         return this.dy;
  53.     }

  54.     /** {@inheritDoc} */
  55.     @Override
  56.     public boolean contains(final Point2d point) throws NullPointerException
  57.     {
  58.         return Math.abs(point.x) < this.dx && Math.abs(point.y) < this.dy;
  59.     }

  60.     /** {@inheritDoc} */
  61.     @Override
  62.     public boolean covers(final Point2d point) throws NullPointerException
  63.     {
  64.         return Math.abs(point.x) <= this.dx && Math.abs(point.y) <= this.dy;
  65.     }

  66.     /**
  67.      * {@inheritDoc}
  68.      * @see <a href="https://iquilezles.org/articles/distfunctions/">Signed distance functions by Inigo Quilez</a>
  69.      */
  70.     @Override
  71.     public double signedDistance(final Point2d point)
  72.     {
  73.         double qx = Math.abs(point.x) - this.dx;
  74.         double qy = Math.abs(point.y) - this.dy;
  75.         return Math.hypot(Math.max(qx, 0.0), Math.max(qy, 0.0)) + Math.min(Math.max(qx, qy), 0.0);
  76.     }

  77.     /** {@inheritDoc} */
  78.     @Override
  79.     public Polygon2d asPolygon()
  80.     {
  81.         if (this.polygon == null)
  82.         {
  83.             this.polygon = new Polygon2d(new Point2d(this.dx, this.dy), new Point2d(-this.dx, this.dy),
  84.                     new Point2d(-this.dx, -this.dy), new Point2d(this.dx, -this.dy), new Point2d(this.dx, this.dy));
  85.         }
  86.         return this.polygon;
  87.     }

  88. }