/** * Check that two spatial objects have the same dimensionality. * * @param box1 First object * @param box2 Second object * @return Dimensionality * @throws IllegalArgumentException when the dimensionalities do not agree */ public static int assertSameDimensionality(SpatialComparable box1, SpatialComparable box2) { final int dim = box1.getDimensionality(); if (dim != box2.getDimensionality()) { throw new IllegalArgumentException( "The spatial objects do not have the same dimensionality!"); } return dim; }
/** * Test two SpatialComparables for equality. * * @param box1 First bounding box * @param box2 Second bounding box * @return true when the boxes are equal */ public static boolean equals(SpatialComparable box1, SpatialComparable box2) { if (box1.getDimensionality() != box2.getDimensionality()) { return false; } for (int i = 0; i < box1.getDimensionality(); i++) { if (box1.getMin(i) != box2.getMin(i)) { return false; } if (box1.getMax(i) != box2.getMax(i)) { return false; } } return true; }
/** * Returns a clone of the maximum hyper point. * * @param box spatial object * @return the maximum hyper point */ public static double[] getMax(SpatialComparable box) { final int dim = box.getDimensionality(); double[] max = new double[dim]; for (int i = 0; i < dim; i++) { max[i] = box.getMax(i); } return max; }
/** * Returns a clone of the minimum hyper point. * * @param box spatial object * @return the minimum hyper point */ public static double[] getMin(SpatialComparable box) { final int dim = box.getDimensionality(); double[] min = new double[dim]; for (int i = 0; i < dim; i++) { min[i] = box.getMin(i); } return min; }
/** * Returns the centroid of this SpatialComparable. * * @param obj Spatial object to process * @return the centroid of this SpatialComparable */ public static double[] centroid(SpatialComparable obj) { final int dim = obj.getDimensionality(); double[] centroid = new double[dim]; for (int d = 0; d < dim; d++) { centroid[d] = (obj.getMax(d) + obj.getMin(d)) * .5; } return centroid; }
/** * Computes the perimeter of this SpatialComparable. * * @param box spatial object * @return the perimeter of this SpatialComparable */ public static double perimeter(SpatialComparable box) { final int dim = box.getDimensionality(); double perimeter = 0.; for (int i = 0; i < dim; i++) { perimeter += box.getMax(i) - box.getMin(i); } return perimeter; }
/** * Computes the volume of this SpatialComparable. * * @param box Box * @param scale Scaling factor * @return the volume of this SpatialComparable */ public static double volumeScaled(SpatialComparable box, double scale) { final int dim = box.getDimensionality(); double vol = 1.; for (int i = 0; i < dim; i++) { double delta = box.getMax(i) - box.getMin(i); if (delta == 0.) { return 0.; } vol *= delta * scale; } return vol; }
/** * Returns true if this SpatialComparable contains the given point, false otherwise. * * @param box spatial object * @param point the point to be tested for containment * @return true if this SpatialComparable contains the given point, false otherwise */ public static boolean contains(SpatialComparable box, double[] point) { final int dim = box.getDimensionality(); if (dim != point.length) { throw new IllegalArgumentException( "This HyperBoundingBox and the given point need same dimensionality"); } for (int i = 0; i < dim; i++) { if (box.getMin(i) > point[i] || box.getMax(i) < point[i]) { return false; } } return true; }