/* * Moonlight|3D Copyright (C) 2005 The Moonlight|3D team * * This library is free software; you can redistribute it and/or modify it under * the terms of the GNU Lesser General Public License as published by the Free * Software Foundation; either version 2.1 of the License, or (at your option) * any later version. * * This library is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more * details. * * You should have received a copy of the GNU Lesser General Public License * along with this library; if not, write to the Free Software Foundation, Inc., * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA * * Created on Jan 5, 2005 */ package eu.moonlight3d.math; /** * A class representing an axis alligned bounding box * * @author gregor */ public class AxisAlignedBoundingBox implements Cloneable { /** * Vector with the coordinates of the corner with all minimal * component values. */ public Vector3D min; /** * Vector with the coordinates of the corner with all maximum * component values. */ public Vector3D max; /** * Simple constructor. */ public AxisAlignedBoundingBox() { min = new Vector3D(); max = new Vector3D(); } /** * Constructs an infinitely small bounding box containing only the given point. * * @param point the point to be contained in this bounding box */ public AxisAlignedBoundingBox(Vector3D point) { min = point.clone(); max = point.clone(); } /** * Return a list of the coordinates of all 8 corner points of * this bounding box. * * @return list of corner coordinates */ public Vector3D[] getAllCorners() { Vector3D[] corners=new Vector3D[8]; for(int i=0;i<8;i++) { corners[i]=new Vector3D(); } corners[0]=min.clone(); corners[1].X1=min.X1; corners[1].X2=min.X2; corners[1].X3=max.X3; corners[2].X1=min.X1; corners[2].X2=max.X2; corners[2].X3=min.X3; corners[3].X1=max.X1; corners[3].X2=min.X2; corners[3].X3=min.X3; corners[4].X1=max.X1; corners[4].X2=min.X2; corners[4].X3=max.X3; corners[5].X1=max.X1; corners[5].X2=max.X2; corners[5].X3=min.X3; corners[6].X1=min.X1; corners[6].X2=max.X2; corners[6].X3=max.X3; corners[7]=max.clone(); return corners; } /** * Generate a bounding box that contains this one and the given * one. * * @param b the bounding box to be merged with this one * @return the bounding box enclosing both boxes. */ public AxisAlignedBoundingBox merge(AxisAlignedBoundingBox b) { AxisAlignedBoundingBox res = new AxisAlignedBoundingBox(); if (min.X1 < b.min.X1) { res.min.X1 = min.X1; } else { res.min.X1 = b.min.X1; } if (max.X1 > b.max.X1) { res.max.X1 = max.X1; } else { res.max.X1 = b.max.X1; } if (min.X2 < b.min.X2) { res.min.X2 = min.X2; } else { res.min.X2 = b.min.X2; } if (max.X2 > b.max.X2) { res.max.X2 = max.X2; } else { res.max.X2 = b.max.X2; } if (min.X3 < b.min.X3) { res.min.X3 = min.X3; } else { res.min.X3 = b.min.X3; } if (max.X3 > b.max.X3) { res.max.X3 = max.X3; } else { res.max.X3 = b.max.X3; } return res; } /** * Generate a bounding box that contains this one and the point * given by the vector v. * * @param v the vector to include in new bounding box volume * @return the new bounding box volume */ public AxisAlignedBoundingBox merge(Vector3D v) { AxisAlignedBoundingBox res = new AxisAlignedBoundingBox(); if (min.X1 < v.X1) { res.min.X1 = min.X1; } else { res.min.X1 = v.X1; } if (max.X1 > v.X1) { res.max.X1 = max.X1; } else { res.max.X1 = v.X1; } if (min.X2 < v.X2) { res.min.X2 = min.X2; } else { res.min.X2 = v.X2; } if (max.X2 > v.X2) { res.max.X2 = max.X2; } else { res.max.X2 = v.X2; } if (min.X3 < v.X3) { res.min.X3 = min.X3; } else { res.min.X3 = v.X3; } if (max.X3 > v.X3) { res.max.X3 = max.X3; } else { res.max.X3 = v.X3; } return res; } /** * Tests if the given point is contained in this bounding box * * @param point the point to test * @return true if point is inside bounding, false otherwise */ public boolean contains(Vector3D point) { if(min.X1<=point.X1 && min.X2<=point.X2 && min.X3<=point.X3 && max.X1>=point.X1 && max.X2>=point.X2 && max.X3>=point.X3) { return true; } return false; } /** * Tests whether this bounding box overlaps the given bounding box. * * @param box bounding box to test against * @return true if bounding boxes overlap, false otherwise */ public boolean overlaps(AxisAlignedBoundingBox box) { if(min.X1>box.max.X1 || max.X1box.max.X2 || max.X2box.max.X3 || max.X3