Browse Source

Reimplemented Box3 fixes and more code clean up.

Mr.doob 12 years ago
parent
commit
f303b67cb5
2 changed files with 96 additions and 50 deletions
  1. 5 0
      src/core/Box2.js
  2. 91 50
      src/core/Box3.js

+ 5 - 0
src/core/Box2.js

@@ -5,13 +5,18 @@
 THREE.Box2 = function ( min, max ) {
 THREE.Box2 = function ( min, max ) {
 
 
 	if ( min == undefined && max === undefined ) {
 	if ( min == undefined && max === undefined ) {
+
 		this.min = new THREE.Vector2();
 		this.min = new THREE.Vector2();
 		this.max = new THREE.Vector2();
 		this.max = new THREE.Vector2();
 		this.makeEmpty();
 		this.makeEmpty();
+
 	} else {
 	} else {
+
 		this.min = min || new THREE.Vector2();
 		this.min = min || new THREE.Vector2();
 		this.max = max || new THREE.Vector2().copy( this.min ); // This is done on purpose so you can make a box using a single point and then expand it.
 		this.max = max || new THREE.Vector2().copy( this.min ); // This is done on purpose so you can make a box using a single point and then expand it.
+
 	}
 	}
+
 };
 };
 
 
 THREE.Box2.prototype = {
 THREE.Box2.prototype = {

+ 91 - 50
src/core/Box3.js

@@ -4,15 +4,19 @@
 
 
 THREE.Box3 = function ( min, max ) {
 THREE.Box3 = function ( min, max ) {
 
 
-	if( ! min && ! max ) {			
+	if( min == undefined && max === undefined ) {
+
 		this.min = new THREE.Vector3();
 		this.min = new THREE.Vector3();
 		this.max = new THREE.Vector3();
 		this.max = new THREE.Vector3();
 		this.makeEmpty();
 		this.makeEmpty();
-	}
-	else {
+
+	} else {
+
 		this.min = min || new THREE.Vector3();
 		this.min = min || new THREE.Vector3();
-		this.max = max || new THREE.Vector3().copy( this.min );		// This is done on purpose so you can make a box using a single point and then expand it.
+		this.max = max || new THREE.Vector3().copy( this.min ); // This is done on purpose so you can make a box using a single point and then expand it.
+
 	}
 	}
+
 };
 };
 
 
 THREE.Box3.prototype = {
 THREE.Box3.prototype = {
@@ -25,44 +29,58 @@ THREE.Box3.prototype = {
 		this.max = max;
 		this.max = max;
 
 
 		return this;
 		return this;
+
 	},
 	},
 
 
 	setFromPoints: function ( points ) {
 	setFromPoints: function ( points ) {
 
 
-		if( points.length > 0 ) {
+		if ( points.length > 0 ) {
+
+			var p = points[ 0 ];
 
 
-			var p = points[0];
 			this.min.copy( p );
 			this.min.copy( p );
 			this.max.copy( p );
 			this.max.copy( p );
 
 
-			for( var i = 1, numPoints = points.length; i < numPoints; i ++ ) {
+			for ( var i = 1, il = points.length; i < il; i ++ ) {
 
 
-				p = points[ v ];
+				p = points[ i ];
 
 
 				if ( p.x < this.min.x ) {
 				if ( p.x < this.min.x ) {
+
 					this.min.x = p.x;
 					this.min.x = p.x;
-				}
-				else if ( p.x > this.max.x ) {
+
+				} else if ( p.x > this.max.x ) {
+
 					this.max.x = p.x;
 					this.max.x = p.x;
+
 				}
 				}
 
 
 				if ( p.y < this.min.y ) {
 				if ( p.y < this.min.y ) {
+
 					this.min.y = p.y;
 					this.min.y = p.y;
-				}
-				else if ( p.y > this.max.y ) {
+
+				} else if ( p.y > this.max.y ) {
+
 					this.max.y = p.y;
 					this.max.y = p.y;
+
 				}
 				}
 
 
 				if ( p.z < this.min.z ) {
 				if ( p.z < this.min.z ) {
+
 					this.min.z = p.z;
 					this.min.z = p.z;
-				}
-				else if ( p.z > this.max.z ) {
+
+				} else if ( p.z > this.max.z ) {
+
 					this.max.z = p.z;
 					this.max.z = p.z;
+
 				}
 				}
+
 			}
 			}
-		}
-		else {
+
+		} else {
+
 			this.makeEmpty();
 			this.makeEmpty();
+
 		}
 		}
 
 
 		return this;
 		return this;
@@ -71,10 +89,12 @@ THREE.Box3.prototype = {
 	setFromCenterAndSize: function ( center, size ) {
 	setFromCenterAndSize: function ( center, size ) {
 
 
 		var halfSize = THREE.Box3.__v1.copy( size ).multiplyScalar( 0.5 );
 		var halfSize = THREE.Box3.__v1.copy( size ).multiplyScalar( 0.5 );
+
 		this.min.copy( center ).subSelf( halfSize );
 		this.min.copy( center ).subSelf( halfSize );
 		this.max.copy( center ).addSelf( halfSize );
 		this.max.copy( center ).addSelf( halfSize );
 
 
-		return box;	
+		return this;
+
 	},
 	},
 
 
 	copy: function ( box ) {
 	copy: function ( box ) {
@@ -83,6 +103,7 @@ THREE.Box3.prototype = {
 		this.max.copy( box.max );
 		this.max.copy( box.max );
 
 
 		return this;
 		return this;
+
 	},
 	},
 
 
 	makeEmpty: function () {
 	makeEmpty: function () {
@@ -91,41 +112,41 @@ THREE.Box3.prototype = {
 		this.max.x = this.max.y = this.max.z = -Infinity;
 		this.max.x = this.max.y = this.max.z = -Infinity;
 
 
 		return this;
 		return this;
+
 	},
 	},
 
 
 	empty: function () {
 	empty: function () {
 
 
 		// this is a more robust check for empty than ( volume <= 0 ) because volume can get positive with two negative axes
 		// 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 );
+		return ( this.max.x < this.min.x ) || ( this.max.y < this.min.y ) || ( this.max.z < this.min.z );
+
 	},
 	},
 
 
 	volume: function () {
 	volume: function () {
 
 
-		return 
-			( this.max.x - this.min.x ) *
-			( this.max.y - this.min.y ) *
-			( this.max.z - this.min.z );
+		return ( this.max.x - this.min.x ) * ( this.max.y - this.min.y ) * ( this.max.z - this.min.z );
+
 	},
 	},
 
 
 	center: function () {
 	center: function () {
 
 
 		return new THREE.Vector3().add( this.min, this.max ).multiplyScalar( 0.5 );
 		return new THREE.Vector3().add( this.min, this.max ).multiplyScalar( 0.5 );
+
 	},
 	},
 
 
 	size: function () {
 	size: function () {
 
 
 		return new THREE.Vector3().sub( this.max, this.min );
 		return new THREE.Vector3().sub( this.max, this.min );
+
 	},
 	},
 
 
 	expandByPoint: function ( point ) {
 	expandByPoint: function ( point ) {
 
 
-		this.min.minSelf( point );		
+		this.min.minSelf( point );
 		this.max.maxSelf( point );
 		this.max.maxSelf( point );
 
 
 		return this;
 		return this;
+
 	},
 	},
 
 
 	expandByVector: function ( vector ) {
 	expandByVector: function ( vector ) {
@@ -134,76 +155,93 @@ THREE.Box3.prototype = {
 		this.max.addSelf( vector );
 		this.max.addSelf( vector );
 
 
 		return this;
 		return this;
+
 	},
 	},
 
 
 	expandByScalar: function ( scalar ) {
 	expandByScalar: function ( scalar ) {
 
 
 		this.min.addScalar( -scalar );
 		this.min.addScalar( -scalar );
 		this.max.addScalar( scalar );
 		this.max.addScalar( scalar );
-		
+
 		return this;
 		return this;
+
 	},
 	},
 
 
 	containsPoint: function ( point ) {
 	containsPoint: function ( point ) {
-		if( 
-			( this.min.x <= point.x ) && ( point.x <= this.max.x ) &&
+
+		if ( ( this.min.x <= point.x ) && ( point.x <= this.max.x ) &&
 			( this.min.y <= point.y ) && ( point.y <= this.max.y ) &&
 			( this.min.y <= point.y ) && ( point.y <= this.max.y ) &&
-			( this.min.z <= point.z ) && ( point.z <= this.max.z )
-			) {
+			( this.min.z <= point.z ) && ( point.z <= this.max.z ) ) {
+
 			return true;
 			return true;
+
 		}
 		}
+
 		return false;
 		return false;
+
 	},
 	},
 
 
 	containsBox: function ( box ) {
 	containsBox: function ( box ) {
-		if( 
-			( this.min.x <= box.min.x ) && ( box.max.x <= this.max.x ) &&
+
+		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.y <= box.min.y ) && ( box.max.y <= this.max.y ) &&
-			( this.min.z <= box.min.z ) && ( box.max.z <= this.max.z )
-			) {
+			( this.min.z <= box.min.z ) && ( box.max.z <= this.max.z ) ) {
+
 			return true;
 			return true;
+
 		}
 		}
+
 		return false;
 		return false;
+
 	},
 	},
 
 
 	getParameter: function ( point ) {
 	getParameter: function ( point ) {
+
 		// This can potentially have a divide by zero if the box
 		// This can potentially have a divide by zero if the box
 		// has a size dimension of 0.
 		// has a size dimension of 0.
 		return new THREE.Vector3(
 		return new THREE.Vector3(
 			( point.x - this.min.x ) / ( this.max.x - this.min.x ),
 			( point.x - this.min.x ) / ( this.max.x - this.min.x ),
 			( point.y - this.min.y ) / ( this.max.y - this.min.y ),
 			( point.y - this.min.y ) / ( this.max.y - this.min.y ),
 			( point.z - this.min.z ) / ( this.max.z - this.min.z )
 			( point.z - this.min.z ) / ( this.max.z - this.min.z )
-			);
+		);
+
+	},
+
+	isIntersection: function ( box ) {
+
+		// using 6 splitting planes to rule out intersections.
+
+		if ( ( this.max.x < box.min.x ) || ( box.min.x > this.max.x ) ||
+			( this.max.y < box.min.y ) || ( box.min.y > this.max.y ) ||
+			( this.max.z < box.min.z ) || ( box.min.z > this.max.z ) ) {
+
+			return false;
+
+		}
+
+		return true;
+
 	},
 	},
 
 
 	clampPoint: function ( point ) {
 	clampPoint: function ( point ) {
 
 
 		return new THREE.Vector3().copy( point ).clampSelf( this.min, this.max );
 		return new THREE.Vector3().copy( point ).clampSelf( this.min, this.max );
+
 	},
 	},
 
 
 	distanceToPoint: function ( point ) {
 	distanceToPoint: function ( point ) {
 
 
 		return this.clampPoint( point ).subSelf( point ).length();
 		return this.clampPoint( point ).subSelf( point ).length();
-	},
 
 
-	isIntersection: function ( box ) {
-		// using 6 splitting planes to rule out intersections.
-		if( 
-			( this.max.x < box.min.x ) || ( box.min.x > this.max.x ) ||
-			( this.max.y < box.min.y ) || ( box.min.y > this.max.y ) ||
-			( this.max.z < box.min.z ) || ( box.min.z > this.max.z )
-			) {
-			return false;
-		}
-		return true;
 	},
 	},
 
 
 	intersect: function ( box ) {
 	intersect: function ( box ) {
 
 
 		this.min.maxSelf( box.min );
 		this.min.maxSelf( box.min );
 		this.max.minSelf( box.max );
 		this.max.minSelf( box.max );
-		
+
 		return this;
 		return this;
+
 	},
 	},
 
 
 	union: function ( box ) {
 	union: function ( box ) {
@@ -212,6 +250,7 @@ THREE.Box3.prototype = {
 		this.max.maxSelf( box.max );
 		this.max.maxSelf( box.max );
 
 
 		return this;
 		return this;
+
 	},
 	},
 
 
 	translate: function ( offset ) {
 	translate: function ( offset ) {
@@ -220,6 +259,7 @@ THREE.Box3.prototype = {
 		this.max.addSelf( offset );
 		this.max.addSelf( offset );
 
 
 		return this;
 		return this;
+
 	},
 	},
 
 
 	scale: function ( factor ) {
 	scale: function ( factor ) {
@@ -228,6 +268,7 @@ THREE.Box3.prototype = {
 		this.expandByVector( sizeDeltaHalf );
 		this.expandByVector( sizeDeltaHalf );
 
 
 		return this;
 		return this;
+
 	},
 	},
 
 
 	equals: function ( box ) {
 	equals: function ( box ) {
@@ -241,7 +282,7 @@ THREE.Box3.prototype = {
 		return new THREE.Box3().copy( this );
 		return new THREE.Box3().copy( this );
 
 
 	}
 	}
-	
+
 };
 };
 
 
-THREE.Box3.__v1 = new THREE.Vector3();
+THREE.Box3.__v1 = new THREE.Vector3();