123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331 |
- /**
- * @author bhouston / http://exocortex.com
- */
- THREE.Box3 = function ( min, max ) {
- this.min = ( min !== undefined ) ? min : new THREE.Vector3( Infinity, Infinity, Infinity );
- this.max = ( max !== undefined ) ? max : new THREE.Vector3( -Infinity, -Infinity, -Infinity );
- };
- THREE.extend( THREE.Box3.prototype, {
- constructor: THREE.Box3,
- set: function ( min, max ) {
- this.min.copy( min );
- this.max.copy( max );
- return this;
- },
- setFromPoints: function ( points ) {
- if ( points.length > 0 ) {
- var point = points[ 0 ];
- this.min.copy( point );
- this.max.copy( point );
- for ( var i = 1, il = points.length; i < il; i ++ ) {
- point = points[ i ];
- if ( point.x < this.min.x ) {
- this.min.x = point.x;
- } else if ( point.x > this.max.x ) {
- this.max.x = point.x;
- }
- if ( point.y < this.min.y ) {
- this.min.y = point.y;
- } else if ( point.y > this.max.y ) {
- this.max.y = point.y;
- }
- if ( point.z < this.min.z ) {
- this.min.z = point.z;
- } else if ( point.z > this.max.z ) {
- this.max.z = point.z;
- }
- }
- } else {
- this.makeEmpty();
- }
- return this;
- },
- setFromCenterAndSize: function() {
- var v1 = new THREE.Vector3();
-
- return function ( center, size ) {
- var halfSize = v1.copy( size ).multiplyScalar( 0.5 );
- this.min.copy( center ).sub( halfSize );
- this.max.copy( center ).add( halfSize );
- return this;
- };
- }(),
- copy: function ( box ) {
- this.min.copy( box.min );
- this.max.copy( box.max );
- return this;
- },
- makeEmpty: function () {
- this.min.x = this.min.y = this.min.z = Infinity;
- this.max.x = this.max.y = this.max.z = -Infinity;
- return this;
- },
- empty: function () {
- // this is a more robust check for empty than ( volume <= 0 ) because volume can get positive with two negative axes
- return ( this.max.x < this.min.x ) || ( this.max.y < this.min.y ) || ( this.max.z < this.min.z );
- },
- center: function ( optionalTarget ) {
- var result = optionalTarget || new THREE.Vector3();
- return result.addVectors( this.min, this.max ).multiplyScalar( 0.5 );
- },
- size: function ( optionalTarget ) {
- var result = optionalTarget || new THREE.Vector3();
- return result.subVectors( this.max, this.min );
- },
- expandByPoint: function ( point ) {
- this.min.min( point );
- this.max.max( point );
- return this;
- },
- expandByVector: function ( vector ) {
- this.min.sub( vector );
- this.max.add( vector );
- return this;
- },
- expandByScalar: function ( scalar ) {
- this.min.addScalar( -scalar );
- this.max.addScalar( scalar );
- return this;
- },
- containsPoint: function ( point ) {
- if ( point.x < this.min.x || point.x > this.max.x ||
- point.y < this.min.y || point.y > this.max.y ||
- point.z < this.min.z || point.z > this.max.z ) {
- return false;
- }
- return true;
- },
- containsBox: function ( box ) {
- if ( ( this.min.x <= box.min.x ) && ( box.max.x <= this.max.x ) &&
- ( this.min.y <= box.min.y ) && ( box.max.y <= this.max.y ) &&
- ( this.min.z <= box.min.z ) && ( box.max.z <= this.max.z ) ) {
- return true;
- }
- return false;
- },
- getParameter: function ( point ) {
- // This can potentially have a divide by zero if the box
- // has a size dimension of 0.
- return new THREE.Vector3(
- ( point.x - this.min.x ) / ( this.max.x - this.min.x ),
- ( point.y - this.min.y ) / ( this.max.y - this.min.y ),
- ( point.z - this.min.z ) / ( this.max.z - this.min.z )
- );
- },
- isIntersectionBox: function ( box ) {
- // using 6 splitting planes to rule out intersections.
- if ( box.max.x < this.min.x || box.min.x > this.max.x ||
- box.max.y < this.min.y || box.min.y > this.max.y ||
- box.max.z < this.min.z || box.min.z > this.max.z ) {
- return false;
- }
- return true;
- },
- clampPoint: function ( point, optionalTarget ) {
- var result = optionalTarget || new THREE.Vector3();
- return result.copy( point ).clamp( this.min, this.max );
- },
- distanceToPoint: function() {
- var v1 = new THREE.Vector3();
-
- return function ( point ) {
- var clampedPoint = v1.copy( point ).clamp( this.min, this.max );
- return clampedPoint.sub( point ).length();
- };
- }(),
- getBoundingSphere: function() {
- var v1 = new THREE.Vector3();
-
- return function ( optionalTarget ) {
- var result = optionalTarget || new THREE.Sphere();
- result.center = this.center();
- result.radius = this.size( v1 ).length() * 0.5;
- return result;
- };
- }(),
- intersect: function ( box ) {
- this.min.max( box.min );
- this.max.min( box.max );
- return this;
- },
- union: function ( box ) {
- this.min.min( box.min );
- this.max.max( box.max );
- return this;
- },
- transform: function() {
- var points = [
- new THREE.Vector3(),
- new THREE.Vector3(),
- new THREE.Vector3(),
- new THREE.Vector3(),
- new THREE.Vector3(),
- new THREE.Vector3(),
- new THREE.Vector3(),
- new THREE.Vector3()
- ];
- return function ( matrix ) {
- // NOTE: I am using a binary pattern to specify all 2^3 combinations below
- points[0].set( this.min.x, this.min.y, this.min.z ).applyMatrix4( matrix ); // 000
- points[1].set( this.min.x, this.min.y, this.max.z ).applyMatrix4( matrix ); // 001
- points[2].set( this.min.x, this.max.y, this.min.z ).applyMatrix4( matrix ); // 010
- points[3].set( this.min.x, this.max.y, this.max.z ).applyMatrix4( matrix ); // 011
- points[4].set( this.max.x, this.min.y, this.min.z ).applyMatrix4( matrix ); // 100
- points[5].set( this.max.x, this.min.y, this.max.z ).applyMatrix4( matrix ); // 101
- points[6].set( this.max.x, this.max.y, this.min.z ).applyMatrix4( matrix ); // 110
- points[7].set( this.max.x, this.max.y, this.max.z ).applyMatrix4( matrix ); // 111
-
- this.makeEmpty();
- this.setFromPoints( points );
- return this;
- };
- }(),
- translate: function ( offset ) {
- this.min.add( offset );
- this.max.add( offset );
- return this;
- },
- equals: function ( box ) {
- return box.min.equals( this.min ) && box.max.equals( this.max );
- },
- clone: function () {
- return new THREE.Box3().copy( this );
- }
- } );
|