View Javadoc

1   /**
2    * 
3    */
4   
5   package math.geom3d;
6   
7   import javax.vecmath.Tuple3d;
8   
9   /**
10   * @author dlegland
11   */
12  public class Box3D {
13  
14      // ===================================================================
15      // class variables
16  
17      private double xmin = 0;
18      private double xmax = 0;
19      private double ymin = 0;
20      private double ymax = 0;
21      private double zmin = 0;
22      private double zmax = 0;
23  
24      /** Empty constructor (size and position zero) */
25      public Box3D() {
26          this(0, 0, 0, 0, 0, 0);
27      }
28  
29      /**
30       * Main constructor, given bounds for x coord, bounds for y coord, and
31       * bounds for z coord. A check is performed to ensure first bound is lower
32       * than second bound.
33       */
34      public Box3D(double x0, double x1, double y0, double y1, double z0,
35              double z1) {
36          xmin = Math.min(x0, x1);
37          xmax = Math.max(x0, x1);
38          ymin = Math.min(y0, y1);
39          ymax = Math.max(y0, y1);
40          zmin = Math.min(z0, z1);
41          zmax = Math.max(z0, z1);
42      }
43  
44      /** Constructor from 2 points, giving extreme coordinates of the box. */
45      public Box3D(Point3D p1, Point3D p2) {
46          this(p1.getX(), p2.getX(), p1.getY(), p2.getY(), p1.getZ(), p2.getZ());
47      }
48      
49      public Box3D(Tuple3d p1, Tuple3d p2) {
50      	this(p1.getX(), p2.getX(), p1.getY(), p2.getY(), p1.getZ(), p2.getZ());
51      }
52  
53      // ===================================================================
54      // accessors to Box2D fields
55  
56      public double getMinX() {
57          return xmin;
58      }
59  
60      public double getMaxX() {
61          return xmax;
62      }
63  
64      public double getMinY() {
65          return ymin;
66      }
67  
68      public double getMaxY() {
69          return ymax;
70      }
71  
72      public double getMinZ() {
73          return zmin;
74      }
75  
76      public double getMaxZ() {
77          return zmax;
78      }
79  
80      /** Returns the width, i.e. the difference between the min and max x coord */
81      public double getWidth() {
82          return xmax-xmin;
83      }
84  
85      /** Returns the height, i.e. the difference between the min and max y coord */
86      public double getHeight() {
87          return ymax-ymin;
88      }
89  
90      /** Returns the depth, i.e. the difference between the min and max z coord */
91      public double getDepth() {
92          return zmax-zmin;
93      }
94  
95      /**
96       * Returns the Box2D which contains both this box and the specified box.
97       * 
98       * @param box the bounding box to include
99       * @return this
100      */
101     public Box3D union(Box3D box) {
102         double xmin = Math.min(this.xmin, box.xmin);
103         double xmax = Math.max(this.xmax, box.xmax);
104         double ymin = Math.min(this.ymin, box.ymin);
105         double ymax = Math.max(this.ymax, box.ymax);
106         double zmin = Math.min(this.zmin, box.zmin);
107         double zmax = Math.max(this.zmax, box.zmax);
108         return new Box3D(xmin, xmax, ymin, ymax, zmin, zmax);
109     }
110 
111     /**
112      * Returns the Box2D which is contained both by this box and by the
113      * specified box.
114      * 
115      * @param box the bounding box to include
116      * @return this
117      */
118     public Box3D intersection(Box3D box) {
119         double xmin = Math.max(this.xmin, box.xmin);
120         double xmax = Math.min(this.xmax, box.xmax);
121         double ymin = Math.max(this.ymin, box.ymin);
122         double ymax = Math.min(this.ymax, box.ymax);
123         double zmin = Math.max(this.zmin, box.zmin);
124         double zmax = Math.min(this.zmax, box.zmax);
125         return new Box3D(xmin, xmax, ymin, ymax, zmin, zmax);
126     }
127 
128 }