1
2
3
4
5 package math.geom3d;
6
7 import javax.vecmath.Tuple3d;
8
9
10
11
12 public class Box3D {
13
14
15
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
25 public Box3D() {
26 this(0, 0, 0, 0, 0, 0);
27 }
28
29
30
31
32
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
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
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
81 public double getWidth() {
82 return xmax-xmin;
83 }
84
85
86 public double getHeight() {
87 return ymax-ymin;
88 }
89
90
91 public double getDepth() {
92 return zmax-zmin;
93 }
94
95
96
97
98
99
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
113
114
115
116
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 }