/
AxisAlignedBoundingBox.js
243 lines (215 loc) · 9.68 KB
/
AxisAlignedBoundingBox.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
/*global define*/
define([
'./defaultValue',
'./DeveloperError',
'./Cartesian3',
'./Intersect'
], function(
defaultValue,
DeveloperError,
Cartesian3,
Intersect) {
"use strict";
/**
* Creates an instance of an AxisAlignedBoundingBox from the minimum and maximum points along the x, y, and z axes.
* @alias AxisAlignedBoundingBox
* @constructor
*
* @param {Cartesian3} [minimum=Cartesian3.ZERO] The minimum point along the x, y, and z axes.
* @param {Cartesian3} [maximum=Cartesian3.ZERO] The maximum point along the x, y, and z axes.
* @param {Cartesian3} [center] The center of the box; automatically computed if not supplied.
*
* @see BoundingSphere
*/
var AxisAlignedBoundingBox = function(minimum, maximum, center) {
/**
* The minimum point defining the bounding box.
* @type {Cartesian3}
* @default {@link Cartesian3.ZERO}
*/
this.minimum = Cartesian3.clone(defaultValue(minimum, Cartesian3.ZERO));
/**
* The maximum point defining the bounding box.
* @type {Cartesian3}
* @default {@link Cartesian3.ZERO}
*/
this.maximum = Cartesian3.clone(defaultValue(maximum, Cartesian3.ZERO));
//If center was not defined, compute it.
if (typeof center === 'undefined') {
center = Cartesian3.add(this.minimum, this.maximum);
Cartesian3.multiplyByScalar(center, 0.5, center);
} else {
center = Cartesian3.clone(center);
}
/**
* The center point of the bounding box.
* @type {Cartesian3}
*/
this.center = center;
};
/**
* Computes an instance of an AxisAlignedBoundingBox. The box is determined by
* finding the points spaced the farthest apart on the x, y, and z axes.
* @memberof AxisAlignedBoundingBox
*
* @param {Array} positions List of points that the bounding box will enclose. Each point must have a <code>x</code>, <code>y</code>, and <code>z</code> properties.
* @param {AxisAlignedBoundingBox} [result] The object onto which to store the result.
* @return {AxisAlignedBoundingBox} The modified result parameter or a new AxisAlignedBoundingBox instance if one was not provided.
*
* @example
* // Compute an axis aligned bounding box enclosing two points.
* var box = AxisAlignedBoundingBox.fromPoints([new Cartesian3(2, 0, 0), new Cartesian3(-2, 0, 0)]);
*/
AxisAlignedBoundingBox.fromPoints = function(positions, result) {
if (typeof result === 'undefined') {
result = new AxisAlignedBoundingBox();
}
if (typeof positions === 'undefined' || positions.length === 0) {
result.minimum = Cartesian3.clone(Cartesian3.ZERO, result.minimum);
result.maximum = Cartesian3.clone(Cartesian3.ZERO, result.maximum);
result.center = Cartesian3.clone(Cartesian3.ZERO, result.center);
return result;
}
var minimumX = positions[0].x;
var minimumY = positions[0].y;
var minimumZ = positions[0].z;
var maximumX = positions[0].x;
var maximumY = positions[0].y;
var maximumZ = positions[0].z;
var length = positions.length;
for ( var i = 1; i < length; i++) {
var p = positions[i];
var x = p.x;
var y = p.y;
var z = p.z;
minimumX = Math.min(x, minimumX);
maximumX = Math.max(x, maximumX);
minimumY = Math.min(y, minimumY);
maximumY = Math.max(y, maximumY);
minimumZ = Math.min(z, minimumZ);
maximumZ = Math.max(z, maximumZ);
}
var minimum = result.minimum;
minimum.x = minimumX;
minimum.y = minimumY;
minimum.z = minimumZ;
var maximum = result.maximum;
maximum.x = maximumX;
maximum.y = maximumY;
maximum.z = maximumZ;
var center = Cartesian3.add(minimum, maximum, result.center);
Cartesian3.multiplyByScalar(center, 0.5, center);
return result;
};
/**
* Duplicates a AxisAlignedBoundingBox instance.
* @memberof AxisAlignedBoundingBox
*
* @param {AxisAlignedBoundingBox} box The bounding box to duplicate.
* @param {AxisAlignedBoundingBox} [result] The object onto which to store the result.
* @return {AxisAlignedBoundingBox} The modified result parameter or a new AxisAlignedBoundingBox instance if none was provided. (Returns undefined if box is undefined)
*/
AxisAlignedBoundingBox.clone = function(box, result) {
if (typeof box === 'undefined') {
return undefined;
}
if (typeof result === 'undefined') {
return new AxisAlignedBoundingBox(box.minimum, box.maximum);
}
result.minimum = Cartesian3.clone(box.minimum, result.minimum);
result.maximum = Cartesian3.clone(box.maximum, result.maximum);
result.center = Cartesian3.clone(box.center, result.center);
return result;
};
/**
* Compares the provided AxisAlignedBoundingBox componentwise and returns
* <code>true</code> if they are equal, <code>false</code> otherwise.
* @memberof AxisAlignedBoundingBox
*
* @param {AxisAlignedBoundingBox} [left] The first AxisAlignedBoundingBox.
* @param {AxisAlignedBoundingBox} [right] The second AxisAlignedBoundingBox.
* @return {Boolean} <code>true</code> if left and right are equal, <code>false</code> otherwise.
*/
AxisAlignedBoundingBox.equals = function(left, right) {
return (left === right) ||
((typeof left !== 'undefined') &&
(typeof right !== 'undefined') &&
Cartesian3.equals(left.center, right.center) &&
Cartesian3.equals(left.minimum, right.minimum) &&
Cartesian3.equals(left.maximum, right.maximum));
};
var intersectScratch = new Cartesian3();
/**
* Determines which side of a plane a box is located.
* @memberof AxisAlignedBoundingBox
*
* @param {AxisAlignedBoundingBox} box The bounding box to test.
* @param {Cartesian4} plane The coefficients of the plane in the form <code>ax + by + cz + d = 0</code>
* where the coefficients a, b, c, and d are the components x, y, z, and w
* of the {Cartesian4}, respectively.
* @return {Intersect} {Intersect.INSIDE} if the entire box is on the side of the plane the normal is pointing,
* {Intersect.OUTSIDE} if the entire box is on the opposite side, and {Intersect.INTERSETING}
* if the box intersects the plane.
*
* @exception {DeveloperError} box is required.
* @exception {DeveloperError} plane is required.
*/
AxisAlignedBoundingBox.intersect = function(box, plane) {
if (typeof box === 'undefined') {
throw new DeveloperError('box is required.');
}
if (typeof plane === 'undefined') {
throw new DeveloperError('plane is required.');
}
intersectScratch = Cartesian3.subtract(box.maximum, box.minimum, intersectScratch);
var h = Cartesian3.multiplyByScalar(intersectScratch, 0.5, intersectScratch); //The positive half diagonal
var e = h.x * Math.abs(plane.x) + h.y * Math.abs(plane.y) + h.z * Math.abs(plane.z);
var s = Cartesian3.dot(box.center, plane) + plane.w; //signed distance from center
if (s - e > 0) {
return Intersect.INSIDE;
}
if (s + e < 0) {
//Not in front because normals point inward
return Intersect.OUTSIDE;
}
return Intersect.INTERSECTING;
};
/**
* Duplicates this AxisAlignedBoundingBox instance.
* @memberof AxisAlignedBoundingBox
*
* @param {AxisAlignedBoundingBox} [result] The object onto which to store the result.
* @return {AxisAlignedBoundingBox} The modified result parameter or a new AxisAlignedBoundingBox instance if one was not provided.
*/
AxisAlignedBoundingBox.prototype.clone = function(result) {
return AxisAlignedBoundingBox.clone(this, result);
};
/**
* Determines which side of a plane this box is located.
* @memberof AxisAlignedBoundingBox
*
* @param {Cartesian4} plane The coefficients of the plane in the form <code>ax + by + cz + d = 0</code>
* where the coefficients a, b, c, and d are the components x, y, z, and w
* of the {Cartesian4}, respectively.
* @return {Intersect} {Intersect.INSIDE} if the entire box is on the side of the plane the normal is pointing,
* {Intersect.OUTSIDE} if the entire box is on the opposite side, and {Intersect.INTERSETING}
* if the box intersects the plane.
*
* @exception {DeveloperError} plane is required.
*/
AxisAlignedBoundingBox.prototype.intersect = function(plane) {
return AxisAlignedBoundingBox.intersect(this, plane);
};
/**
* Compares this AxisAlignedBoundingBox against the provided AxisAlignedBoundingBox componentwise and returns
* <code>true</code> if they are equal, <code>false</code> otherwise.
* @memberof AxisAlignedBoundingBox
*
* @param {AxisAlignedBoundingBox} [right] The right hand side AxisAlignedBoundingBox.
* @return {Boolean} <code>true</code> if they are equal, <code>false</code> otherwise.
*/
AxisAlignedBoundingBox.prototype.equals = function(right) {
return AxisAlignedBoundingBox.equals(this, right);
};
return AxisAlignedBoundingBox;
});