Browse Source

Fixed compile errors and cleaned up code a bit.

Mr.doob 12 years ago
parent
commit
633f3a6caf
4 changed files with 186 additions and 98 deletions
  1. 64 39
      src/core/Box2.js
  2. 82 43
      src/core/Box3.js
  3. 19 10
      src/core/Plane.js
  4. 21 6
      src/core/Sphere.js

+ 64 - 39
src/core/Box2.js

@@ -4,14 +4,13 @@
 
 THREE.Box2 = function ( min, max ) {
 
-	if( ! min && ! max ) {			
+	if ( min == undefined && max === undefined ) {
 		this.min = new THREE.Vector2();
 		this.max = new THREE.Vector2();
 		this.makeEmpty();
-	}
-	else {
+	} else {
 		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.
 	}
 };
 
@@ -29,36 +28,47 @@ THREE.Box2.prototype = {
 
 	setFromPoints: function ( points ) {
 
-		if( points.length > 0 ) {
+		if ( points.length > 0 ) {
+
+			var p = points[ 0 ];
 
-			var p = points[0];
 			this.min.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 ) {
+
 					this.min.x = p.x;
-				}
-				else if ( p.x > this.max.x ) {
+
+				} else if ( p.x > this.max.x ) {
+
 					this.max.x = p.x;
+
 				}
 
 				if ( p.y < this.min.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;
+
 				}
+
 			}
-		}
-		else {
+
+		} else {
+
 			this.makeEmpty();
+
 		}
 
 		return this;
+
 	},
 
 	setFromCenterAndSize: function ( center, size ) {
@@ -67,7 +77,8 @@ THREE.Box2.prototype = {
 		this.min.copy( center ).subSelf( halfSize );
 		this.max.copy( center ).addSelf( halfSize );
 
-		return box;	
+		return this;
+
 	},
 
 	copy: function ( box ) {
@@ -76,6 +87,7 @@ THREE.Box2.prototype = {
 		this.max.copy( box.max );
 
 		return this;
+
 	},
 
 	makeEmpty: function () {
@@ -84,36 +96,37 @@ THREE.Box2.prototype = {
 		this.max.x = this.max.y = -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 );
+		return ( this.max.x < this.min.x ) || ( this.max.y < this.min.y );
+
 	},
 
 	volume: function () {
 
-		return 
-			( this.max.x - this.min.x ) *
-			( this.max.y - this.min.y );
+		return ( this.max.x - this.min.x ) * ( this.max.y - this.min.y );
+
 	},
 
 	center: function () {
 
 		return new THREE.Vector2().add( this.min, this.max ).multiplyScalar( 0.5 );
+
 	},
 
 	size: function () {
 
 		return new THREE.Vector2().sub( this.max, this.min );
+
 	},
 
 	expandByPoint: function ( point ) {
 
-		this.min.minSelf( point );		
+		this.min.minSelf( point );
 		this.max.maxSelf( point );
 
 		return this;
@@ -131,38 +144,45 @@ THREE.Box2.prototype = {
 
 		this.min.addScalar( -scalar );
 		this.max.addScalar( scalar );
-		
+
 		return this;
 	},
 
 	containsPoint: function ( point ) {
-		if( 
-			( this.min.x <= point.x ) && ( point.x <= this.max.x ) &&
-			( this.min.y <= point.y ) && ( point.y <= this.max.y )
-			) {
+
+		if ( ( this.min.x <= point.x ) && ( point.x <= this.max.x ) &&
+			( this.min.y <= point.y ) && ( point.y <= this.max.y ) ) {
+
 			return true;
+
 		}
+
 		return false;
+
 	},
 
 	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 )
-			) {
+
+		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 ) ) {
+
 			return true;
+
 		}
+
 		return false;
+
 	},
 
 	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 )
-			) {
+		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 ) ) {
+
 			return false;
+
 		}
+
 		return true;
 	},
 
@@ -172,25 +192,28 @@ THREE.Box2.prototype = {
 		return new THREE.Vector2(
 			( point.x - this.min.x ) / ( this.max.x - this.min.x ),
 			( point.y - this.min.y ) / ( this.max.y - this.min.y )
-			);
+		);
 	},
 
 	clampPoint: function ( point ) {
 
 		return new THREE.Vector2().copy( point ).clampSelf( this.min, this.max );
+
 	},
 
 	distanceToPoint: function ( point ) {
 
 		return this.clampPoint( point ).subSelf( point ).length();
+
 	},
 
 	intersect: function ( box ) {
 
 		this.min.maxSelf( box.min );
 		this.max.minSelf( box.max );
-		
+
 		return this;
+
 	},
 
 	union: function ( box ) {
@@ -199,6 +222,7 @@ THREE.Box2.prototype = {
 		this.max.maxSelf( box.max );
 
 		return this;
+
 	},
 
 	translate: function ( offset ) {
@@ -207,6 +231,7 @@ THREE.Box2.prototype = {
 		this.max.addSelf( offset );
 
 		return this;
+
 	},
 
 	scale: function ( factor ) {
@@ -215,6 +240,7 @@ THREE.Box2.prototype = {
 		this.expandByVector( sizeDeltaHalf );
 
 		return this;
+
 	},
 
 	equals: function ( box ) {
@@ -229,7 +255,6 @@ THREE.Box2.prototype = {
 
 	}
 
-	
 };
 
-THREE.Box2.__v1 = new THREE.Vector2();
+THREE.Box2.__v1 = new THREE.Vector2();

+ 82 - 43
src/core/Box3.js

@@ -4,16 +4,18 @@
 
 THREE.Box3 = function ( min, max ) {
 
-	if( ! min && ! max ) {			
+	if( min == undefined && max === undefined ) {
+
 		this.min = new THREE.Vector3();
 		this.max = new THREE.Vector3();
 		this.min.x = this.min.y = this.min.z = Infinity;
 		this.max.x = this.max.y = this.max.z = -Infinity;
 		console.log( this );
-	}
-	else {
+
+	} else {
+
 		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.
 	}
 };
 
@@ -27,44 +29,58 @@ THREE.Box3.prototype = {
 		this.max = max;
 
 		return this;
+
 	},
 
 	setFromPoints: function ( points ) {
 
-		if( points.length > 0 ) {
+		if ( points.length > 0 ) {
+
+			var p = points[ 0 ];
 
-			var p = points[0];
 			this.min.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 ) {
+
 					this.min.x = p.x;
-				}
-				else if ( p.x > this.max.x ) {
+
+				} else if ( p.x > this.max.x ) {
+
 					this.max.x = p.x;
+
 				}
 
 				if ( p.y < this.min.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;
+
 				}
 
 				if ( p.z < this.min.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;
+
 				}
+
 			}
-		}
-		else {
+
+		} else {
+
 			this.makeEmpty();
+
 		}
 
 		return this;
@@ -73,10 +89,12 @@ THREE.Box3.prototype = {
 	setFromCenterAndSize: function ( center, size ) {
 
 		var halfSize = THREE.Box3.__v1.copy( size ).multiplyScalar( 0.5 );
+
 		this.min.copy( center ).subSelf( halfSize );
 		this.max.copy( center ).addSelf( halfSize );
 
-		return box;	
+		return this;
+
 	},
 
 	copy: function ( box ) {
@@ -85,6 +103,7 @@ THREE.Box3.prototype = {
 		this.max.copy( box.max );
 
 		return this;
+
 	},
 
 	makeEmpty: function () {
@@ -93,41 +112,41 @@ THREE.Box3.prototype = {
 		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 );
+		return ( this.max.x < this.min.x ) || ( this.max.y < this.min.y ) || ( this.max.z < this.min.z );
+
 	},
 
 	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 () {
 
 		return new THREE.Vector3().add( this.min, this.max ).multiplyScalar( 0.5 );
+
 	},
 
 	size: function () {
 
 		return new THREE.Vector3().sub( this.max, this.min );
+
 	},
 
 	expandByPoint: function ( point ) {
 
-		this.min.minSelf( point );		
+		this.min.minSelf( point );
 		this.max.maxSelf( point );
 
 		return this;
+
 	},
 
 	expandByVector: function ( vector ) {
@@ -136,76 +155,93 @@ THREE.Box3.prototype = {
 		this.max.addSelf( vector );
 
 		return this;
+
 	},
 
 	expandByScalar: function ( scalar ) {
 
 		this.min.addScalar( -scalar );
 		this.max.addScalar( scalar );
-		
+
 		return this;
+
 	},
 
 	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.z <= point.z ) && ( point.z <= this.max.z )
-			) {
+			( this.min.z <= point.z ) && ( point.z <= this.max.z ) ) {
+
 			return true;
+
 		}
+
 		return false;
+
 	},
 
 	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.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 false;
+
 	},
 
 	isIntersection: function ( box ) {
+
 		// using 6 splitting planes to rule out intersections.
-		if( 
-			( this.max.x < box.min.x ) || ( box.min.x > this.max.x ) ||
+
+		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 )
-			) {
+			( this.max.z < box.min.z ) || ( box.min.z > this.max.z ) ) {
+
 			return false;
+
 		}
+
 		return true;
+
 	},
 
 	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 )
-			);
+		);
+
 	},
 
 	clampPoint: function ( point ) {
 
 		return new THREE.Vector3().copy( point ).clampSelf( this.min, this.max );
+
 	},
 
 	distanceToPoint: function ( point ) {
 
 		return this.clampPoint( point ).subSelf( point ).length();
+
 	},
 
 	intersect: function ( box ) {
 
 		this.min.maxSelf( box.min );
 		this.max.minSelf( box.max );
-		
+
 		return this;
+
 	},
 
 	union: function ( box ) {
@@ -214,6 +250,7 @@ THREE.Box3.prototype = {
 		this.max.maxSelf( box.max );
 
 		return this;
+
 	},
 
 	translate: function ( offset ) {
@@ -222,6 +259,7 @@ THREE.Box3.prototype = {
 		this.max.addSelf( offset );
 
 		return this;
+
 	},
 
 	scale: function ( factor ) {
@@ -230,6 +268,7 @@ THREE.Box3.prototype = {
 		this.expandByVector( sizeDeltaHalf );
 
 		return this;
+
 	},
 
 	equals: function ( box ) {
@@ -243,7 +282,7 @@ THREE.Box3.prototype = {
 		return new THREE.Box3().copy( this );
 
 	}
-	
+
 };
 
-THREE.Box3.__v1 = new THREE.Vector3();
+THREE.Box3.__v1 = new THREE.Vector3();

+ 19 - 10
src/core/Plane.js

@@ -44,8 +44,9 @@ THREE.Plane.prototype = {
 
 		// Q: should an error be thrown if normal is zero (e.g. degenerate plane)?
 		this.setFromNormalAndCoplanarPoint( normal, a );
-		
+
 		return this;
+
 	},
 
 	copy: function ( plane ) {
@@ -54,6 +55,7 @@ THREE.Plane.prototype = {
 		this.constant = plane.constant;
 
 		return this;
+
 	},
 
 	flip: function () {
@@ -61,6 +63,7 @@ THREE.Plane.prototype = {
 		this.normal.negate();
 
 		return this;
+
 	},
 
 	normalize: function () {
@@ -71,6 +74,7 @@ THREE.Plane.prototype = {
 		this.constant *= inverseNormalLength;
 
 		return this;
+
 	},
 
 	distanceToPoint: function ( point ) {
@@ -83,42 +87,47 @@ THREE.Plane.prototype = {
 		return this.distanceToPoint( sphere.center ) - sphere.radius;
 	},
 
-	projectPoint: function ( point ) {		
-		
+	projectPoint: function ( point ) {
+
 		return this.orthoPoint( point ).subSelf( point ).negate();
+
 	},
 
-	orthoPoint: function ( point ) {		
+	orthoPoint: function ( point ) {
 
 		var perpendicularMagnitude = this.distanceToPoint( point );
 
 		return new THREE.Vector3().copy( this.normal ).multiplyScalar( perpendicularMagnitude );
+
 	},
 
-	intersectsLine: function ( startPoint, endPoint ) {	
+	intersectsLine: function ( startPoint, endPoint ) {
 
 		// Note: this tests if a line intersects the plane, not whether it (or its end-points) are coplanar with it.
 		var startSign = this.distanceToPoint( startPoint );
 		var endSign = this.distanceToPoint( endPoint );
 
 		return ( startSign < 0 && endSign > 0 ) || ( endSign < 0 && startSign > 0 );
+
 	},
 
-	coplanarPoint: function () {		
-		
+	coplanarPoint: function () {
+
 		return new THREE.Vector3().copy( this.normal ).multiplyScalar( - this.constant );
+
 	},
 
 	translate: function ( offset ) {
 
-		this.constant =	- offset.dot( normal );
+		this.constant = - offset.dot( this.normal );
 
 		return this;
+
 	},
 
 	equals: function ( plane ) {
 
-		return plane.normal.equals( this.normal ) && ( sphere.constant == this.constant );
+		return plane.normal.equals( this.normal ) && ( plane.constant == this.constant );
 
 	},
 
@@ -131,4 +140,4 @@ THREE.Plane.prototype = {
 };
 
 THREE.Plane.__v1 = new THREE.Vector3();
-THREE.Plane.__v2 = new THREE.Vector3();
+THREE.Plane.__v2 = new THREE.Vector3();

+ 21 - 6
src/core/Sphere.js

@@ -19,20 +19,25 @@ THREE.Sphere.prototype = {
 		this.radius = radius;
 
 		return this;
+
 	},
 
 	setFromCenterAndPoints: function ( center, points ) {
 
 		var maxRadiusSq = 0;
-		for ( var i = 0, numPoints = points.length; i < numPoints; i ++ ) {			
-			var radiusSq = center.distanceToSquared( points[i] );
+
+		for ( var i = 0, il = points.length; i < il; i ++ ) {
+
+			var radiusSq = center.distanceToSquared( points[ i ] );
 			maxRadiusSq = Math.max( maxRadiusSq, radiusSq );
+
 		}
 
 		this.center = center;
 		this.radius = Math.sqrt( maxRadiusSq );
 
 		return this;
+
 	},
 
 	copy: function ( sphere ) {
@@ -41,26 +46,31 @@ THREE.Sphere.prototype = {
 		this.radius = sphere.radius;
 
 		return this;
+
 	},
 
 	empty: function () {
 
 		return ( this.radius <= 0 );
+
 	},
 
 	volume: function () {
 
 		return Math.PI * 4 / 3 * ( this.radius * this.radius * this.radius );
+
 	},
 
 	containsPoint: function ( point ) {
 
 		return ( point.distanceToSquared( this.center ) <= ( this.radius * this.radius ) );
+
 	},
 
 	distanceToPoint: function ( point ) {
 
 		return ( point.distanceTo( this.center ) - this.radius );
+
 	},
 
 	clampPoint: function ( point ) {
@@ -69,13 +79,15 @@ THREE.Sphere.prototype = {
 
 		var result = new THREE.Vector3().copy( point );
 
-		if( deltaLengthSq > ( this.radius * this.radius ) ) {
+		if ( deltaLengthSq > ( this.radius * this.radius ) ) {
 
-			result.subSelf( center ).normalize();
+			result.subSelf( this.center ).normalize();
 			result.multiplyScalar( this.radius ).addSelf( this.center );
+
 		}
 
 		return result;
+
 	},
 
 	bounds: function () {
@@ -84,20 +96,23 @@ THREE.Sphere.prototype = {
 		box.expandByScalar( this.radius );
 
 		return box;
+
 	},
 
 	translate: function ( offset ) {
 
 		this.center.addSelf( this.offset );
-		
+
 		return this;
+
 	},
 
 	scale: function ( factor ) {
 
 		this.radius *= factor;
-		
+
 		return this;
+
 	},
 
 	equals: function ( sphere ) {