Class SpatialGrid


  • public class SpatialGrid
    extends Object
    Fast implementation for tracking data overlap and closest point in a 2D region. The region of interest is divided into non-overlapping cells. Each cell may contain zero or one data objects. This allows quickly finding if a data object may be drawn (markIfClear) and closest drawn object to a point (findClosest).
    • Constructor Detail

      • SpatialGrid

        public SpatialGrid​(int nx,
                           int ny)
        Constructor
        Parameters:
        nx - maximum number of grid cells in x
        ny - : maximum number of grid cells in y
    • Method Detail

      • setGrid

        public void setGrid​(Rectangle2D bbox,
                            double width,
                            double height)
        Set the grid scale.
        Parameters:
        bbox - bounding box: we are only interested in points inside of this
        width - : divide the bounding box into cells of this width.
        height - : divide the bounding box into cells of this height. maximum number of grid cells is nx x ny
      • setOverlap

        public void setOverlap​(int overlap)
        Set how much the data may overlap.
        Parameters:
        overlap - : percent overlap
      • clear

        public void clear()
        clear all the grid cells
      • markIfClear

        public boolean markIfClear​(Rectangle2D rect,
                                   Object o)
        Check if the given rect intersects an already drawn one. If not, set the corresponding cell as marked, store object, return true, meaning "ok to draw".
        Parameters:
        rect - the bounding box of the thing we want to draw
        o - store this object
        Returns:
        true if inside the bounding box and no intersection
      • findIntersection

        public Object findIntersection​(Rectangle2D rect)
        Check if the given rect intersects an already drawn object
        Parameters:
        rect - the bounding box of the thing we want to draw
        Returns:
        object that intersects, or null if no intersection
      • findIntersection

        public Object findIntersection​(Point2D p)
        Check if the given point is contained in already drawn object
        Parameters:
        p - the point to check
        Returns:
        object that intersects, or null if no intersection
      • findClosest

        public Object findClosest​(Point2D pt)
        Find the closest marked cell to the given point
        Parameters:
        pt - find the closest marked cell to this point
        Returns:
        the object associated with the closest cell, or null if none