Skip to content
Snippets Groups Projects
Commit 1ac45cd4 authored by Radek Ošlejšek's avatar Radek Ošlejšek
Browse files

BoundingBox made immutable + fix

parent b47a413c
No related branches found
No related tags found
No related merge requests found
......@@ -4,34 +4,50 @@ import java.util.List;
import javax.vecmath.Vector3d;
/**
* @author Natália Bebjaková
*
* Represent min-max box.
* It is automatically maintained by given point array of the model.
* @author Natalia Bebjakova
*
* 3D bounding box (cube) of <code>MeshPoint</code>s.
*/
public class BoundingBox {
private MeshPoint maxPoint;
private MeshPoint minPoint;
private MeshPoint midPoint;
private double maxDiag;
private final MeshPoint maxPoint;
private final MeshPoint minPoint;
private final MeshPoint midPoint;
private final double maxDiag;
/**
* Creates bounding box that is automatically maintained with respect to given array.
* @param points array of points, must not be null or pempty
* Creates bounding box from given mesh points.
* @param points List of mesh points, must not be null or empty
* @throws IllegalArgumentException if the @code{points} param is null or empty
*/
public BoundingBox(List<MeshPoint> points) {
if (points == null || points.isEmpty()) {
throw new IllegalArgumentException("points");
}
this.computeMinMax(points);
this.computeMidDiag();
minPoint = new MeshPointImpl(new Vector3d(Double.POSITIVE_INFINITY,Double.POSITIVE_INFINITY,Double.POSITIVE_INFINITY), null, null);
maxPoint = new MeshPointImpl(new Vector3d(Double.NEGATIVE_INFINITY,Double.NEGATIVE_INFINITY,Double.NEGATIVE_INFINITY), null, null);
for (int i = 0; i < points.size(); i++) {
MeshPoint point = points.get(i);
minPoint.getPosition().x = Math.min(minPoint.getPosition().x, point.getPosition().x);
minPoint.getPosition().y = Math.min(minPoint.getPosition().y, point.getPosition().y);
minPoint.getPosition().z = Math.min(minPoint.getPosition().z, point.getPosition().z);
maxPoint.getPosition().x = Math.max(maxPoint.getPosition().x, point.getPosition().x);
maxPoint.getPosition().y = Math.max(maxPoint.getPosition().y, point.getPosition().y);
maxPoint.getPosition().z = Math.max(maxPoint.getPosition().z, point.getPosition().z);
}
midPoint = (minPoint.addPosition(maxPoint)).multiplyPosition(0.5);
MeshPoint diag = maxPoint.subtractPosition(minPoint);
this.maxDiag = diag.abs();
}
/**
*
* Return the upper-bound corner of the bounding cube
* @return max point of the bounding box
*/
public MeshPoint getMaxPoint() {
......@@ -39,7 +55,7 @@ public class BoundingBox {
}
/**
*
* Return centroid of the bounding cube.
* @return middle point of the bounding box
*/
public MeshPoint getMidPoint() {
......@@ -47,7 +63,7 @@ public class BoundingBox {
}
/**
*
* Return the lower-bound corner of the bounding cube
* @return min point of the bounding box
*/
public MeshPoint getMinPoint() {
......@@ -62,35 +78,6 @@ public class BoundingBox {
return maxDiag;
}
/**
* Recomputes the BoundingBox from all points
*/
private void computeMinMax(List<MeshPoint> points) {
minPoint = new MeshPointImpl(new Vector3d(Double.MAX_VALUE,Double.MAX_VALUE,Double.MAX_VALUE), null, null);
maxPoint = new MeshPointImpl(new Vector3d(-100000.0,-100000.0,-100000.0), null, null);
for (int i = 0; i < points.size(); i++) {
MeshPoint point = points.get(i);
minPoint.getPosition().x = Math.min(minPoint.getPosition().x, point.getPosition().x);
minPoint.getPosition().y = Math.min(minPoint.getPosition().y, point.getPosition().y);
minPoint.getPosition().z = Math.min(minPoint.getPosition().z, point.getPosition().z);
maxPoint.getPosition().x = Math.max(maxPoint.getPosition().x, point.getPosition().x);
maxPoint.getPosition().y = Math.max(maxPoint.getPosition().y, point.getPosition().y);
maxPoint.getPosition().z = Math.max(maxPoint.getPosition().z, point.getPosition().z);
}
}
/**
* Recompute mid-point and max diagonal length.
*/
private void computeMidDiag() {
midPoint = (minPoint.addPosition(maxPoint)).multiplyPosition(0.5);
MeshPoint diag = maxPoint.subtractPosition(minPoint);
this.maxDiag = diag.abs();
}
/**
* Returns description of BoundignBox.
*
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment