Browse Source

change Box3, Plane and Sphere declarations from Frustum.js-style to Vector3.js-style.

Ben Houston 12 years ago
parent
commit
4f7ceca8af
3 changed files with 152 additions and 153 deletions
  1. 69 67
      src/core/Box3.js
  2. 45 45
      src/core/Plane.js
  3. 38 41
      src/core/Sphere.js

+ 69 - 67
src/core/Box3.js

@@ -2,116 +2,118 @@
  * @author Ben Houston / [email protected] / http://github.com/bhouston
  * @author Ben Houston / [email protected] / http://github.com/bhouston
  */
  */
 
 
-( function ( THREE ) {
+THREE.Box3 = function ( min, max ) {
 
 
-	THREE.Box3 = function ( min, max ) {
+	// TODO: Is this valid JavaScript to check if the parameters are specified?
+	if( ! min && ! max ) {			
+		this.makeEmpty();
+	}
+	else {
+		this.min = min || new THREE.Vector3();
+		this.max = max || this.min;		// This is done on purpose so you can make a box using a single point and then expand it.
+	}
+};
 
 
-		// TODO: Is this valid JavaScript to check if the parameters are specified?
-		if( ! min && ! max ) {			
-			this.makeEmpty();
-		}
-		else {
-			this.min = min || new THREE.Vector3();
-			this.max = max || this.min;		// This is done on purpose so you can make a box using a single point and then expand it.
-		}
-	};
+THREE.Box3.fromPoints = function ( points ) {
 
 
-	THREE.Box3.fromPoints = function ( points ) {
+	var boundingBox = new THREE.Box3();
+	for( var i = 0, numPoints = points.length; i < numPoints; i ++ ) {
+		boundingBox.extendByPoint( points[i] );
+	}
 
 
-		var boundingBox = new THREE.Box3();
-		for( var i = 0, numPoints = points.length; i < numPoints; i ++ ) {
-			boundingBox.extendByPoint( points[i] );
-		}
+	return boundingBox;
+};
 
 
-		return boundingBox;
-	};
+THREE.Box3.fromCenterAndSize = function ( center, size ) {
 
 
-	THREE.Box3.fromCenterAndSize = function ( center, size ) {
+	var halfSize = new THREE.Vector3().copy( size ).multiplyScalar( 0.5 );
+	var box = new THREE.Box3( center, center );
+	box.min.subSelf( halfSize );
+	box.min.addSelf( halfSize );
 
 
-		var halfSize = new THREE.Vector3().copy( size ).multiplyScalar( 0.5 );
-		var box = new THREE.Box3( center, center );
-		box.min.subSelf( halfSize );
-		box.min.addSelf( halfSize );
+	return box;	
+};
 
 
-		return box;	
-	};
+THREE.Box3.prototype = {
 
 
-	THREE.Box3.prototype.set = function ( min, max ) {
+	constructor: THREE.Box3,
+
+	set: function ( min, max ) {
 
 
 		this.min = min;
 		this.min = min;
 		this.max = max;
 		this.max = max;
 
 
 		return this;
 		return this;
-	};
+	},
 
 
-	THREE.Box3.prototype.copy = function ( box ) {
+	copy: function ( box ) {
 
 
 		this.min = box.min;
 		this.min = box.min;
 		this.max = box.max;
 		this.max = box.max;
 
 
 		return this;
 		return this;
-	};
+	},
 
 
-	THREE.Box3.prototype.makeEmpty = function () {
+	makeEmpty: function () {
 
 
 		this.min.x = this.min.y = this.min.z = Number.MAX_VALUE;
 		this.min.x = this.min.y = this.min.z = Number.MAX_VALUE;
 		this.max.x = this.max.y = this.max.z = -Number.MAX_VALUE;
 		this.max.x = this.max.y = this.max.z = -Number.MAX_VALUE;
 
 
 		return this;
 		return this;
-	};
+	},
 
 
-	THREE.Box3.prototype.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 
 		return 
 			( this.max.x < this.min.x ) ||
 			( this.max.x < this.min.x ) ||
 			( this.max.y < this.min.y ) ||
 			( this.max.y < this.min.y ) ||
 			( this.max.z < this.min.z );
 			( this.max.z < this.min.z );
-	};
+	},
 
 
-	THREE.Box3.prototype.volume = function () {
+	volume: function () {
 
 
 		return 
 		return 
 			( this.max.x - this.min.x ) *
 			( this.max.x - this.min.x ) *
 			( this.max.y - this.min.y ) *
 			( this.max.y - this.min.y ) *
 			( this.max.z - this.min.z );
 			( this.max.z - this.min.z );
-	};
+	},
 
 
-	THREE.Box3.prototype.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 );
-	};
+	},
 
 
-	THREE.Box3.prototype.size = function () {
+	size: function () {
 
 
 		return new THREE.Vector3().sub( this.max, this.min );
 		return new THREE.Vector3().sub( this.max, this.min );
-	};
+	},
 
 
-	THREE.Box3.prototype.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;
-	};
+	},
 
 
-	THREE.Box3.prototype.expandByVector = function ( vector ) {
+	expandByVector: function ( vector ) {
 
 
 		this.min.subSelf( vector );
 		this.min.subSelf( vector );
 		this.max.addSelf( vector );
 		this.max.addSelf( vector );
 
 
 		return this;
 		return this;
-	};
+	},
 
 
-	THREE.Box3.prototype.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;
-	};
+	},
 
 
-	THREE.Box3.prototype.containsPoint = function ( point ) {
+	containsPoint: function ( point ) {
 		if( 
 		if( 
 			( this.min.x <= point.x ) && ( point.x <= this.max.x ) &&
 			( 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 ) &&
@@ -120,9 +122,9 @@
 			return true;
 			return true;
 		}
 		}
 		return false;
 		return false;
-	};
+	},
 
 
-	THREE.Box3.prototype.containsBox = function ( box ) {
+	containsBox: function ( box ) {
 		if( 
 		if( 
 			( this.min.x <= box.min.x ) && ( box.max.x <= this.max.x ) &&
 			( 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 ) &&
@@ -131,9 +133,9 @@
 			return true;
 			return true;
 		}
 		}
 		return false;
 		return false;
-	};
+	},
 
 
-	THREE.Box3.prototype.isIntersection = function ( box ) {
+	isIntersection: function ( box ) {
 		// using 6 splitting planes to rule out intersections.
 		// using 6 splitting planes to rule out intersections.
 		if( 
 		if( 
 			( this.max.x < box.min.x ) || ( box.min.x > this.max.x ) ||
 			( this.max.x < box.min.x ) || ( box.min.x > this.max.x ) ||
@@ -143,9 +145,9 @@
 			return false;
 			return false;
 		}
 		}
 		return true;
 		return true;
-	};
+	},
 
 
-	THREE.Box3.prototype.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(
@@ -153,48 +155,48 @@
 			( 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 )
 			);
 			);
-	};
+	},
 
 
-	THREE.Box3.prototype.clampPoint = function ( point ) {
+	clampPoint: function ( point ) {
 
 
 		return new THREE.Vector3().copy( point ).maxSelf( this.min ).minSelf( this.max );
 		return new THREE.Vector3().copy( point ).maxSelf( this.min ).minSelf( this.max );
-	};
+	},
 
 
-	THREE.Box3.prototype.distanceToPoint = function ( point ) {
+	distanceToPoint: function ( point ) {
 
 
 		return this.clampPoint( point ).subSelf( point ).length();
 		return this.clampPoint( point ).subSelf( point ).length();
-	};
+	},
 
 
-	THREE.Box3.prototype.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;
-	};
+	},
 
 
-	THREE.Box3.prototype.union = function ( box ) {
+	union: function ( box ) {
 
 
 		this.min.minSelf( box.min );
 		this.min.minSelf( box.min );
 		this.max.maxSelf( box.max );
 		this.max.maxSelf( box.max );
 
 
 		return this;
 		return this;
-	};
+	},
 
 
-	THREE.Box3.prototype.translate = function ( offset ) {
+	translate: function ( offset ) {
 
 
 		this.min.addSelf( offset );
 		this.min.addSelf( offset );
 		this.max.addSelf( offset );
 		this.max.addSelf( offset );
 
 
 		return this;
 		return this;
-	};
+	},
 
 
-	THREE.Box3.prototype.scale = function ( factor ) {
+	scale: function ( factor ) {
 
 
 		var sizeDeltaHalf = this.size().multiplyScalar( ( 1 - factor )  * 0.5 );
 		var sizeDeltaHalf = this.size().multiplyScalar( ( 1 - factor )  * 0.5 );
 		this.expandByVector( sizeDeltaHalf );
 		this.expandByVector( sizeDeltaHalf );
 
 
 		return this;
 		return this;
-	};
-
-}( THREE ) );
+	}
+	
+};

+ 45 - 45
src/core/Plane.js

@@ -2,70 +2,70 @@
  * @author Ben Houston / [email protected] / http://github.com/bhouston
  * @author Ben Houston / [email protected] / http://github.com/bhouston
  */
  */
 
 
-( function ( THREE ) {
+THREE.Plane = function ( normal, constant ) {
 
 
-	var zeroPoint = new THREE.Vector3();
+	this.normal = normal || new THREE.Vector3();
+	this.constant = constant || 0;
 
 
-	THREE.Plane = function ( normal, constant ) {
+};
 
 
-		this.normal = normal || new THREE.Vector3();
-		this.constant = constant || 0;
+THREE.Plane.fromNormalAndCoplanarPoint = function ( normal, point ) {
+	// NOTE: This function doens't support optional parameters like the constructor.
 
 
-	};
+	return new THREE.Plane( 
+		normal,
+		- point.dot( normal )
+		);
+};
 
 
-	THREE.Plane.fromNormalAndCoplanarPoint = function ( normal, point ) {
-		// NOTE: This function doens't support optional parameters like the constructor.
+THREE.Plane.fromCoplanarPoints = function ( a, b, c ) {
+	// NOTE: This function doens't support optional parameters like the constructor.
 
 
-		return new THREE.Plane( 
-			normal,
-			- point.dot( normal )
-			);
-	};
+	var normal = new THREE.Vector3().sub( b, a ).cross(
+		new THREE.Vector3().sub( c, a ) );
 
 
-	THREE.Plane.fromCoplanarPoints = function ( a, b, c ) {
-		// NOTE: This function doens't support optional parameters like the constructor.
+	// Q: should an error be thrown if normal is zero (e.g. degenerate plane)?
 
 
-		var normal = new THREE.Vector3().sub( b, a ).cross(
-			new THREE.Vector3().sub( c, a ) );
+	return THREE.Plane.fromNormalAndCoplanarPoint( normal, a );
+};
 
 
-		// Q: should an error be thrown if normal is zero (e.g. degenerate plane)?
+THREE.Plane.prototype = {
 
 
-		return THREE.Plane.fromNormalAndCoplanarPoint( normal, a );
-	};
+	constructor: THREE.Plane,
 
 
-	THREE.Plane.prototype.set = function ( normal, constant ) {
+	set: function ( normal, constant ) {
 
 
 		this.normal = normal;
 		this.normal = normal;
 		this.constant = constant;
 		this.constant = constant;
 
 
 		return this;
 		return this;
-	};
+	},
 
 
-	THREE.Plane.prototype.setComponents = function ( x, y, z, w ) {
+	setComponents: function ( x, y, z, w ) {
 
 
 		this.normal.set( x, y, z );
 		this.normal.set( x, y, z );
 		this.constant = w;
 		this.constant = w;
 
 
 		return this;
 		return this;
-	};
+	},
 
 
-	THREE.Plane.prototype.copy = function ( plane ) {
+	copy: function ( plane ) {
 
 
 		this.normal = plane.normal;
 		this.normal = plane.normal;
 		this.constant = plane.constant;
 		this.constant = plane.constant;
 
 
 		return this;
 		return this;
-	};
+	},
 
 
-	THREE.Plane.prototype.flip = function () {
+	flip: function () {
 
 
 		// Note: can also be flipped by inverting constant, but I like constant to stay positive generally.
 		// Note: can also be flipped by inverting constant, but I like constant to stay positive generally.
 		this.normal.negate();
 		this.normal.negate();
 
 
 		return this;
 		return this;
-	};
+	},
 
 
-	THREE.Plane.prototype.normalize = function () {
+	normalize: function () {
 
 
 		// Note: will lead to a divide by zero if the plane is invalid.
 		// Note: will lead to a divide by zero if the plane is invalid.
 		var inverseNormalLength = 1.0 / this.normal.length();
 		var inverseNormalLength = 1.0 / this.normal.length();
@@ -73,52 +73,52 @@
 		this.constant *= inverseNormalLength;
 		this.constant *= inverseNormalLength;
 
 
 		return this;
 		return this;
-	};
+	},
 
 
-	THREE.Plane.prototype.distanceToPoint = function ( point ) {
+	distanceToPoint: function ( point ) {
 
 
 		return this.normal.dot( point ) + this.constant;
 		return this.normal.dot( point ) + this.constant;
-	};
+	},
 
 
-	THREE.Sphere.prototype.distanceToSphere = function ( sphere ) {
+	distanceToSphere: function ( sphere ) {
 
 
 		return this.distanceToPoint( sphere.center ) - sphere.radius;
 		return this.distanceToPoint( sphere.center ) - sphere.radius;
-	};
+	},
 
 
-	THREE.Plane.prototype.projectPoint = function ( point ) {		
+	projectPoint: function ( point ) {		
 		
 		
 		// TODO: optimize this by expanding and simplifying
 		// TODO: optimize this by expanding and simplifying
 		
 		
 		return new THREE.Vector3().copy( point ).sub( this.orthoPoint( point ) );
 		return new THREE.Vector3().copy( point ).sub( this.orthoPoint( point ) );
-	};
+	},
 
 
-	THREE.Plane.prototype.orthoPoint = function ( point ) {		
+	orthoPoint: function ( point ) {		
 
 
 		var perpendicularMagnitude = this.distanceToPoint( point );
 		var perpendicularMagnitude = this.distanceToPoint( point );
 
 
 		return new THREE.Vector3().copy( this.normal ).multipleByScalar( perpendicularMagnitude );
 		return new THREE.Vector3().copy( this.normal ).multipleByScalar( perpendicularMagnitude );
-	};
+	},
 
 
-	THREE.Plane.prototype.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.
 		// 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 startSign = this.distanceToPoint( startPoint );
 		var endSign = this.distanceToPoint( endPoint );
 		var endSign = this.distanceToPoint( endPoint );
 
 
 		return ( startSign < 0 && endSign > 0 ) || ( endSign < 0 && startSign > 0 );
 		return ( startSign < 0 && endSign > 0 ) || ( endSign < 0 && startSign > 0 );
-	};
+	},
 
 
-	THREE.Plane.prototype.coplanarPoint = function () {		
+	coplanarPoint: function () {		
 		
 		
 		return new THREE.Vector3().copy( this.normal ).multiplyScalar( - this.constant );
 		return new THREE.Vector3().copy( this.normal ).multiplyScalar( - this.constant );
-	};
+	},
 
 
-	THREE.Plane.prototype.translate = function ( offset ) {
+	translate: function ( offset ) {
 
 
 		// TODO: test this.
 		// TODO: test this.
 		this.constant =	- offset.dot( normal );
 		this.constant =	- offset.dot( normal );
 
 
 		return this;
 		return this;
-	};
+	}
 
 
-}( THREE ) );
+};

+ 38 - 41
src/core/Sphere.js

@@ -2,72 +2,69 @@
  * @author Ben Houston / [email protected] / http://github.com/bhouston
  * @author Ben Houston / [email protected] / http://github.com/bhouston
  */
  */
 
 
-( function ( THREE ) {
+THREE.Sphere = function ( center, radius ) {
 
 
-	var sphereVolumeConstant = Math.PI * 4 / 3;
-	var square = function( x ) { return x*x; }
-	var cube = function( x ) { return x*x*x; }
+	this.center = center || new THREE.Vector3();
+	this.radius = radius || 0;
 
 
-	THREE.Sphere = function ( center, radius ) {
+};
 
 
-		this.center = center || new THREE.Vector3();
-		this.radius = radius || 0;
+THREE.Sphere.fromCenterAndPoints = function ( center, points ) {
 
 
-	};
+	var maxRadiusSq = 0;
+	for ( var i = 0, numPoints = points.length; i < numPoints; i ++ ) {			
+		var radiusSq = center.distanceToSquared( points[i] );
+		maxRadiusSq = Math.max( maxRadiusSq, radiusSq );
+	}
 
 
-	THREE.Sphere.fromCenterAndPoints = function ( center, points ) {
+	return new THREE.Sphere( center, Math.sqrt( maxRadiusSq ) );
+};
 
 
-		var maxRadiusSq = 0;
-		for ( var i = 0, numPoints = points.length; i < numPoints; i ++ ) {			
-			var radiusSq = center.distanceToSquared( points[i] );
-			maxRadiusSq = Math.max( maxRadiusSq, radiusSq );
-		}
+THREE.Sphere.prototype = {
 
 
-		return new THREE.Sphere( center, Math.sqrt( maxRadiusSq ) );
-	};
+	constructor: THREE.Sphere,
 
 
-	THREE.Sphere.prototype.set = function ( center, radius ) {
+	set: function ( center, radius ) {
 
 
 		this.center = center;
 		this.center = center;
 		this.radius = radius;
 		this.radius = radius;
 
 
 		return this;
 		return this;
-	};
+	},
 
 
-	THREE.Sphere.prototype.copy = function ( sphere ) {
+	copy: function ( sphere ) {
 
 
 		this.center = sphere.center;
 		this.center = sphere.center;
 		this.radius = sphere.radius;
 		this.radius = sphere.radius;
 
 
 		return this;
 		return this;
-	};
+	},
 
 
-	THREE.Sphere.prototype.empty = function () {
+	empty: function () {
 
 
 		return ( this.radius <= 0 );
 		return ( this.radius <= 0 );
+	},
 
 
-	};
-
-	THREE.Sphere.prototype.volume = function () {
+	volume: function () {
 
 
-		return sphereVolumeConstant * cube( this.radius );
-	};
+		return Math.PI * 4 / 3 * ( this.radius * this.radius * this.radius );
+	},
 
 
-	THREE.Sphere.prototype.containsPoint = function ( point ) {
+	containsPoint: function ( point ) {
 
 
-		return ( point.distanceToSquared( this.center ) <= square( this.radius ) );
-	};
+		return ( point.distanceToSquared( this.center ) <= ( this.radius * this.radius ) );
+	},
 
 
-	THREE.Sphere.prototype.distanceToPoint = function ( point ) {
+	distanceToPoint: function ( point ) {
 
 
 		return ( point.distanceTo( this.center ) - this.radius );
 		return ( point.distanceTo( this.center ) - this.radius );
-	};
+	},
 
 
-	THREE.Sphere.prototype.clampPoint = function ( point ) {
+	clampPoint: function ( point ) {
 
 
 		var deltaLengthSq = this.center.distanceToSquared( point );
 		var deltaLengthSq = this.center.distanceToSquared( point );
 
 
-		if( deltaLengthSq > square( this.radius ) ) {
+		if( deltaLengthSq > ( this.radius * this.radius ) ) {
 
 
 			var delta = new THREE.Vector3().sub( point, center ).normalize();
 			var delta = new THREE.Vector3().sub( point, center ).normalize();
 			delta.multiplyScalar( this.radius ).addSelf( this.center );
 			delta.multiplyScalar( this.radius ).addSelf( this.center );
@@ -76,28 +73,28 @@
 		}
 		}
 
 
 		return point;
 		return point;
-	};
+	},
 
 
-	THREE.Sphere.prototype.bounds = function () {
+	bounds: function () {
 
 
 		var box =  new THREE.Box3( this.center, this.center );
 		var box =  new THREE.Box3( this.center, this.center );
 		box.expandByScalar( this.radius );
 		box.expandByScalar( this.radius );
 
 
 		return box;
 		return box;
-	};
+	},
 
 
-	THREE.Sphere.prototype.translate = function ( offset ) {
+	translate: function ( offset ) {
 
 
 		this.center.addSelf( this.offset );
 		this.center.addSelf( this.offset );
 		
 		
 		return this;
 		return this;
-	};
+	},
 
 
-	THREE.Sphere.prototype.scale = function ( factor ) {
+	scale: function ( factor ) {
 
 
 		this.radius *= factor;
 		this.radius *= factor;
 		
 		
 		return this;
 		return this;
-	};
+	}
 
 
-}( THREE ) );
+};