/** * Unify the BoundingBoxes of this and the other BoundingBox * * @param other * another BoundingBox * @return The union of the two BoundingBoxes */ public BoundingBox unionWith(BoundingBox other) { if (other.isNull()) { return new BoundingBox(this); } if (isNull()) { return new BoundingBox(other); } else { return new BoundingBox(Math.min(minX, other.minX), Math.max(maxX, other.maxX), Math.min(minY, other.minY), Math.max(maxY, other.maxY), Math.min(minZ, other.minZ), Math.max(maxZ, other.maxZ)); } }
/** * Create a point at the center of a cell * * @param xIndex * horizontal cell index * @param yIndex * vertical cell index * @return Point at the center of the cell at (xIndex, yIndex) */ private Point middleOfCell(int xIndex, int yIndex) { double middleXCell = indexRegion.minX() + xIndex * xSize + xSize / 2; double middleYCell = indexRegion.minY() + yIndex * ySize + ySize / 2; return new Point(middleXCell, middleYCell); } }
BoundingBox updatedRegion = new BoundingBox(); updatedRegion = updatedRegion.unionWith(updatedTriangles.next().getBoundingBox()); if (updatedRegion.isNull()) // No update... return; if (!indexRegion.contains(updatedRegion)) { init(indexDelaunay, (int) (indexRegion.getWidth() / xSize), (int) (indexRegion.getHeight() / ySize), indexRegion.unionWith(updatedRegion)); } else { PointInt minInvalidCell = getCellOf(updatedRegion.getMinPoint()); PointInt maxInvalidCell = getCellOf(updatedRegion.getMaxPoint());
/** * Initialize the grid index * * @param delaunay * delaunay triangulation to index * @param xCellCount * number of grid cells in a row * @param yCellCount * number of grid cells in a column * @param region * geographic region to index */ private void init(DelaunayTriangulation delaunay, int xCellCount, int yCellCount, BoundingBox region) { indexDelaunay = delaunay; indexRegion = region; xSize = region.getWidth() / yCellCount; ySize = region.getHeight() / xCellCount; // The grid will hold a trinagle for each cell, so a point (x,y) will // lie // in the cell representing the grid partition of region to a // xCellCount on yCellCount grid grid = new Triangle[xCellCount][yCellCount]; Triangle colStartTriangle = indexDelaunay.find(middleOfCell(0, 0)); updateCellValues(0, 0, xCellCount - 1, yCellCount - 1, colStartTriangle); }
/** * @return The bounding rectange between the minimum and maximum coordinates */ public BoundingBox getBoundingBox() { if (bbMin == null || bbMax == null) return null; return new BoundingBox(bbMin, bbMax); }
/** * Creates a bounding box given the extent * * @param minx * minimum x coordinate * @param maxx * maximum x coordinate * @param miny * minimum y coordinate * @param maxy * maximum y coordinate */ public BoundingBox(double minx, double maxx, double miny, double maxy, double minz, double maxz) { init(minx, maxx, miny, maxy, minz, maxz); }
/** * Tests if the other BoundingBox lies wholely inside this BoundingBox * * @param other * the BoundingBox to check * @return true if this BoundingBox contains the other BoundingBox */ public boolean contains(BoundingBox other) { return !(isNull() || other.isNull()) && other.minX >= minX && other.maxY <= maxX && other.minY >= minY && other.maxY <= maxY; }
/** * @return The bounding rectange between the minimum and maximum coordinates * of the triangle */ public BoundingBox getBoundingBox() { Point lowerLeft, upperRight; lowerLeft = new Point(Math.min(a.getX(), Math.min(b.getX(), c.getX())), Math.min(a.getY(), Math.min(b.getY(), c.getY()))); upperRight = new Point(Math.max(a.getX(), Math.max(b.getX(), c.getX())), Math.max(a.getY(), Math.max(b.getY(), c.getY()))); return new BoundingBox(lowerLeft, upperRight); }
/** * Create a bounding box between lowerLeft and upperRight * * @param lowerLeft * lower left point of the box * @param upperRight * upper left point of the box */ public BoundingBox(Point lowerLeft, Point upperRight) { init(lowerLeft.getX(), upperRight.getX(), lowerLeft.getY(), upperRight.getY(), lowerLeft.getZ(), upperRight.getZ()); }
/** * Finds a triangle near the given point * * @param point * a query point * @return a triangle at the same cell of the point */ public Triangle findCellTriangleOf(Point point) { int xIndex = (int) ((point.getX() - indexRegion.minX()) / xSize); int yIndex = (int) ((point.getY() - indexRegion.minY()) / ySize); return grid[xIndex][yIndex]; }
/** * Locates the grid cell point covering the given coordinate * * @param coordinate * world coordinate to locate * @return cell covering the coordinate */ private PointInt getCellOf(Point coordinate) { int xCell = (int) ((coordinate.getX() - indexRegion.minX()) / xSize); int yCell = (int) ((coordinate.getY() - indexRegion.minY()) / ySize); return new PointInt(xCell, yCell); }