1 | /**
|
---|
2 | * @author bhouston / http://exocortex.com
|
---|
3 | * @author WestLangley / http://github.com/WestLangley
|
---|
4 | */
|
---|
5 |
|
---|
6 | THREE.Box3 = function ( min, max ) {
|
---|
7 |
|
---|
8 | this.min = ( min !== undefined ) ? min : new THREE.Vector3( Infinity, Infinity, Infinity );
|
---|
9 | this.max = ( max !== undefined ) ? max : new THREE.Vector3( -Infinity, -Infinity, -Infinity );
|
---|
10 |
|
---|
11 | };
|
---|
12 |
|
---|
13 | THREE.Box3.prototype = {
|
---|
14 |
|
---|
15 | constructor: THREE.Box3,
|
---|
16 |
|
---|
17 | set: function ( min, max ) {
|
---|
18 |
|
---|
19 | this.min.copy( min );
|
---|
20 | this.max.copy( max );
|
---|
21 |
|
---|
22 | return this;
|
---|
23 |
|
---|
24 | },
|
---|
25 |
|
---|
26 | addPoint: function ( point ) {
|
---|
27 |
|
---|
28 | if ( point.x < this.min.x ) {
|
---|
29 |
|
---|
30 | this.min.x = point.x;
|
---|
31 |
|
---|
32 | } else if ( point.x > this.max.x ) {
|
---|
33 |
|
---|
34 | this.max.x = point.x;
|
---|
35 |
|
---|
36 | }
|
---|
37 |
|
---|
38 | if ( point.y < this.min.y ) {
|
---|
39 |
|
---|
40 | this.min.y = point.y;
|
---|
41 |
|
---|
42 | } else if ( point.y > this.max.y ) {
|
---|
43 |
|
---|
44 | this.max.y = point.y;
|
---|
45 |
|
---|
46 | }
|
---|
47 |
|
---|
48 | if ( point.z < this.min.z ) {
|
---|
49 |
|
---|
50 | this.min.z = point.z;
|
---|
51 |
|
---|
52 | } else if ( point.z > this.max.z ) {
|
---|
53 |
|
---|
54 | this.max.z = point.z;
|
---|
55 |
|
---|
56 | }
|
---|
57 |
|
---|
58 | },
|
---|
59 |
|
---|
60 | setFromPoints: function ( points ) {
|
---|
61 |
|
---|
62 | if ( points.length > 0 ) {
|
---|
63 |
|
---|
64 | var point = points[ 0 ];
|
---|
65 |
|
---|
66 | this.min.copy( point );
|
---|
67 | this.max.copy( point );
|
---|
68 |
|
---|
69 | for ( var i = 1, il = points.length; i < il; i ++ ) {
|
---|
70 |
|
---|
71 | this.addPoint( points[ i ] )
|
---|
72 |
|
---|
73 | }
|
---|
74 |
|
---|
75 | } else {
|
---|
76 |
|
---|
77 | this.makeEmpty();
|
---|
78 |
|
---|
79 | }
|
---|
80 |
|
---|
81 | return this;
|
---|
82 |
|
---|
83 | },
|
---|
84 |
|
---|
85 | setFromCenterAndSize: function() {
|
---|
86 |
|
---|
87 | var v1 = new THREE.Vector3();
|
---|
88 |
|
---|
89 | return function ( center, size ) {
|
---|
90 |
|
---|
91 | var halfSize = v1.copy( size ).multiplyScalar( 0.5 );
|
---|
92 |
|
---|
93 | this.min.copy( center ).sub( halfSize );
|
---|
94 | this.max.copy( center ).add( halfSize );
|
---|
95 |
|
---|
96 | return this;
|
---|
97 |
|
---|
98 | };
|
---|
99 |
|
---|
100 | }(),
|
---|
101 |
|
---|
102 | setFromObject: function() {
|
---|
103 |
|
---|
104 | // Computes the world-axis-aligned bounding box of an object (including its children),
|
---|
105 | // accounting for both the object's, and childrens', world transforms
|
---|
106 |
|
---|
107 | var v1 = new THREE.Vector3();
|
---|
108 |
|
---|
109 | return function( object ) {
|
---|
110 |
|
---|
111 | var scope = this;
|
---|
112 |
|
---|
113 | object.updateMatrixWorld( true );
|
---|
114 |
|
---|
115 | this.makeEmpty();
|
---|
116 |
|
---|
117 | object.traverse( function ( node ) {
|
---|
118 |
|
---|
119 | if ( node.geometry !== undefined && node.geometry.vertices !== undefined ) {
|
---|
120 |
|
---|
121 | var vertices = node.geometry.vertices;
|
---|
122 |
|
---|
123 | for ( var i = 0, il = vertices.length; i < il; i++ ) {
|
---|
124 |
|
---|
125 | v1.copy( vertices[ i ] );
|
---|
126 |
|
---|
127 | v1.applyMatrix4( node.matrixWorld );
|
---|
128 |
|
---|
129 | scope.expandByPoint( v1 );
|
---|
130 |
|
---|
131 | }
|
---|
132 |
|
---|
133 | }
|
---|
134 |
|
---|
135 | } );
|
---|
136 |
|
---|
137 | return this;
|
---|
138 |
|
---|
139 | };
|
---|
140 |
|
---|
141 | }(),
|
---|
142 |
|
---|
143 | copy: function ( box ) {
|
---|
144 |
|
---|
145 | this.min.copy( box.min );
|
---|
146 | this.max.copy( box.max );
|
---|
147 |
|
---|
148 | return this;
|
---|
149 |
|
---|
150 | },
|
---|
151 |
|
---|
152 | makeEmpty: function () {
|
---|
153 |
|
---|
154 | this.min.x = this.min.y = this.min.z = Infinity;
|
---|
155 | this.max.x = this.max.y = this.max.z = -Infinity;
|
---|
156 |
|
---|
157 | return this;
|
---|
158 |
|
---|
159 | },
|
---|
160 |
|
---|
161 | empty: function () {
|
---|
162 |
|
---|
163 | // this is a more robust check for empty than ( volume <= 0 ) because volume can get positive with two negative axes
|
---|
164 |
|
---|
165 | return ( this.max.x < this.min.x ) || ( this.max.y < this.min.y ) || ( this.max.z < this.min.z );
|
---|
166 |
|
---|
167 | },
|
---|
168 |
|
---|
169 | center: function ( optionalTarget ) {
|
---|
170 |
|
---|
171 | var result = optionalTarget || new THREE.Vector3();
|
---|
172 | return result.addVectors( this.min, this.max ).multiplyScalar( 0.5 );
|
---|
173 |
|
---|
174 | },
|
---|
175 |
|
---|
176 | size: function ( optionalTarget ) {
|
---|
177 |
|
---|
178 | var result = optionalTarget || new THREE.Vector3();
|
---|
179 | return result.subVectors( this.max, this.min );
|
---|
180 |
|
---|
181 | },
|
---|
182 |
|
---|
183 | expandByPoint: function ( point ) {
|
---|
184 |
|
---|
185 | this.min.min( point );
|
---|
186 | this.max.max( point );
|
---|
187 |
|
---|
188 | return this;
|
---|
189 |
|
---|
190 | },
|
---|
191 |
|
---|
192 | expandByVector: function ( vector ) {
|
---|
193 |
|
---|
194 | this.min.sub( vector );
|
---|
195 | this.max.add( vector );
|
---|
196 |
|
---|
197 | return this;
|
---|
198 |
|
---|
199 | },
|
---|
200 |
|
---|
201 | expandByScalar: function ( scalar ) {
|
---|
202 |
|
---|
203 | this.min.addScalar( -scalar );
|
---|
204 | this.max.addScalar( scalar );
|
---|
205 |
|
---|
206 | return this;
|
---|
207 |
|
---|
208 | },
|
---|
209 |
|
---|
210 | containsPoint: function ( point ) {
|
---|
211 |
|
---|
212 | if ( point.x < this.min.x || point.x > this.max.x ||
|
---|
213 | point.y < this.min.y || point.y > this.max.y ||
|
---|
214 | point.z < this.min.z || point.z > this.max.z ) {
|
---|
215 |
|
---|
216 | return false;
|
---|
217 |
|
---|
218 | }
|
---|
219 |
|
---|
220 | return true;
|
---|
221 |
|
---|
222 | },
|
---|
223 |
|
---|
224 | containsBox: function ( box ) {
|
---|
225 |
|
---|
226 | if ( ( this.min.x <= box.min.x ) && ( box.max.x <= this.max.x ) &&
|
---|
227 | ( this.min.y <= box.min.y ) && ( box.max.y <= this.max.y ) &&
|
---|
228 | ( this.min.z <= box.min.z ) && ( box.max.z <= this.max.z ) ) {
|
---|
229 |
|
---|
230 | return true;
|
---|
231 |
|
---|
232 | }
|
---|
233 |
|
---|
234 | return false;
|
---|
235 |
|
---|
236 | },
|
---|
237 |
|
---|
238 | getParameter: function ( point, optionalTarget ) {
|
---|
239 |
|
---|
240 | // This can potentially have a divide by zero if the box
|
---|
241 | // has a size dimension of 0.
|
---|
242 |
|
---|
243 | var result = optionalTarget || new THREE.Vector3();
|
---|
244 |
|
---|
245 | return result.set(
|
---|
246 | ( point.x - this.min.x ) / ( this.max.x - this.min.x ),
|
---|
247 | ( point.y - this.min.y ) / ( this.max.y - this.min.y ),
|
---|
248 | ( point.z - this.min.z ) / ( this.max.z - this.min.z )
|
---|
249 | );
|
---|
250 |
|
---|
251 | },
|
---|
252 |
|
---|
253 | isIntersectionBox: function ( box ) {
|
---|
254 |
|
---|
255 | // using 6 splitting planes to rule out intersections.
|
---|
256 |
|
---|
257 | if ( box.max.x < this.min.x || box.min.x > this.max.x ||
|
---|
258 | box.max.y < this.min.y || box.min.y > this.max.y ||
|
---|
259 | box.max.z < this.min.z || box.min.z > this.max.z ) {
|
---|
260 |
|
---|
261 | return false;
|
---|
262 |
|
---|
263 | }
|
---|
264 |
|
---|
265 | return true;
|
---|
266 |
|
---|
267 | },
|
---|
268 |
|
---|
269 | clampPoint: function ( point, optionalTarget ) {
|
---|
270 |
|
---|
271 | var result = optionalTarget || new THREE.Vector3();
|
---|
272 | return result.copy( point ).clamp( this.min, this.max );
|
---|
273 |
|
---|
274 | },
|
---|
275 |
|
---|
276 | distanceToPoint: function() {
|
---|
277 |
|
---|
278 | var v1 = new THREE.Vector3();
|
---|
279 |
|
---|
280 | return function ( point ) {
|
---|
281 |
|
---|
282 | var clampedPoint = v1.copy( point ).clamp( this.min, this.max );
|
---|
283 | return clampedPoint.sub( point ).length();
|
---|
284 |
|
---|
285 | };
|
---|
286 |
|
---|
287 | }(),
|
---|
288 |
|
---|
289 | getBoundingSphere: function() {
|
---|
290 |
|
---|
291 | var v1 = new THREE.Vector3();
|
---|
292 |
|
---|
293 | return function ( optionalTarget ) {
|
---|
294 |
|
---|
295 | var result = optionalTarget || new THREE.Sphere();
|
---|
296 |
|
---|
297 | result.center = this.center();
|
---|
298 | result.radius = this.size( v1 ).length() * 0.5;
|
---|
299 |
|
---|
300 | return result;
|
---|
301 |
|
---|
302 | };
|
---|
303 |
|
---|
304 | }(),
|
---|
305 |
|
---|
306 | intersect: function ( box ) {
|
---|
307 |
|
---|
308 | this.min.max( box.min );
|
---|
309 | this.max.min( box.max );
|
---|
310 |
|
---|
311 | return this;
|
---|
312 |
|
---|
313 | },
|
---|
314 |
|
---|
315 | union: function ( box ) {
|
---|
316 |
|
---|
317 | this.min.min( box.min );
|
---|
318 | this.max.max( box.max );
|
---|
319 |
|
---|
320 | return this;
|
---|
321 |
|
---|
322 | },
|
---|
323 |
|
---|
324 | applyMatrix4: function() {
|
---|
325 |
|
---|
326 | var points = [
|
---|
327 | new THREE.Vector3(),
|
---|
328 | new THREE.Vector3(),
|
---|
329 | new THREE.Vector3(),
|
---|
330 | new THREE.Vector3(),
|
---|
331 | new THREE.Vector3(),
|
---|
332 | new THREE.Vector3(),
|
---|
333 | new THREE.Vector3(),
|
---|
334 | new THREE.Vector3()
|
---|
335 | ];
|
---|
336 |
|
---|
337 | return function ( matrix ) {
|
---|
338 |
|
---|
339 | // NOTE: I am using a binary pattern to specify all 2^3 combinations below
|
---|
340 | points[0].set( this.min.x, this.min.y, this.min.z ).applyMatrix4( matrix ); // 000
|
---|
341 | points[1].set( this.min.x, this.min.y, this.max.z ).applyMatrix4( matrix ); // 001
|
---|
342 | points[2].set( this.min.x, this.max.y, this.min.z ).applyMatrix4( matrix ); // 010
|
---|
343 | points[3].set( this.min.x, this.max.y, this.max.z ).applyMatrix4( matrix ); // 011
|
---|
344 | points[4].set( this.max.x, this.min.y, this.min.z ).applyMatrix4( matrix ); // 100
|
---|
345 | points[5].set( this.max.x, this.min.y, this.max.z ).applyMatrix4( matrix ); // 101
|
---|
346 | points[6].set( this.max.x, this.max.y, this.min.z ).applyMatrix4( matrix ); // 110
|
---|
347 | points[7].set( this.max.x, this.max.y, this.max.z ).applyMatrix4( matrix ); // 111
|
---|
348 |
|
---|
349 | this.makeEmpty();
|
---|
350 | this.setFromPoints( points );
|
---|
351 |
|
---|
352 | return this;
|
---|
353 |
|
---|
354 | };
|
---|
355 |
|
---|
356 | }(),
|
---|
357 |
|
---|
358 | translate: function ( offset ) {
|
---|
359 |
|
---|
360 | this.min.add( offset );
|
---|
361 | this.max.add( offset );
|
---|
362 |
|
---|
363 | return this;
|
---|
364 |
|
---|
365 | },
|
---|
366 |
|
---|
367 | equals: function ( box ) {
|
---|
368 |
|
---|
369 | return box.min.equals( this.min ) && box.max.equals( this.max );
|
---|
370 |
|
---|
371 | },
|
---|
372 |
|
---|
373 | clone: function () {
|
---|
374 |
|
---|
375 | return new THREE.Box3().copy( this );
|
---|
376 |
|
---|
377 | }
|
---|
378 |
|
---|
379 | };
|
---|