Browse Source

polishing Box3, Plane and Sphere.

Ben Houston 12 years ago
parent
commit
6f4995b677
3 changed files with 38 additions and 7 deletions
  1. 19 2
      src/core/Box3.js
  2. 9 1
      src/core/Plane.js
  3. 10 4
      src/core/Sphere.js

+ 19 - 2
src/core/Box3.js

@@ -26,6 +26,16 @@
 		return boundingBox;
 	};
 
+	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 );
+
+		return box;	
+	};
+
 	THREE.Box3.prototype.set = function ( min, max ) {
 
 		this.min = min;
@@ -53,7 +63,6 @@
 	THREE.Box3.prototype.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 ) ||
@@ -78,7 +87,7 @@
 		return new THREE.Vector3().sub( this.max, this.min );
 	};
 
-	THREE.Box3.prototype.extendByPoint = function ( point ) {
+	THREE.Box3.prototype.expandByPoint = function ( point ) {
 
 		this.min.minSelf( point );		
 		this.max.maxSelf( point );
@@ -180,4 +189,12 @@
 		return this;
 	};
 
+	THREE.Box3.prototype.scale = function ( factor ) {
+
+		var sizeDeltaHalf = this.size().multiplyScalar( ( 1 - factor )  * 0.5 );
+		this.expandByVector( sizeDeltaHalf );
+
+		return this;
+	};
+
 }( THREE ) );

+ 9 - 1
src/core/Plane.js

@@ -110,7 +110,15 @@
 
 	THREE.Plane.prototype.coplanarPoint = function () {		
 		
-		return new THREE.Vector3().copy( this.normal ).multiplyByScalar( - this.constant );
+		return new THREE.Vector3().copy( this.normal ).multiplyScalar( - this.constant );
+	};
+
+	THREE.Plane.prototype.translate = function ( offset ) {
+
+		// TODO: test this.
+		this.constant =	- offset.dot( normal );
+
+		return this;
 	};
 
 }( THREE ) );

+ 10 - 4
src/core/Sphere.js

@@ -6,7 +6,7 @@
 
 	var sphereVolumeConstant = Math.PI * 4 / 3;
 	var square = function( x ) { return x*x; }
-	var cube = function( x ) { return x*x; }
+	var cube = function( x ) { return x*x*x; }
 
 	THREE.Sphere = function ( center, radius ) {
 
@@ -16,9 +16,8 @@
 	};
 
 	THREE.Sphere.fromCenterAndPoints = function ( center, points ) {
-		var maxRadiusSq = 0;
-		var delta = new THREE.Vector3().copy( center );
 
+		var maxRadiusSq = 0;
 		for ( var i = 0, numPoints = points.length; i < numPoints; i ++ ) {			
 			var radiusSq = center.distanceToSquared( points[i] );
 			maxRadiusSq = Math.max( maxRadiusSq, radiusSq );
@@ -71,7 +70,7 @@
 		if( deltaLengthSq > square( this.radius ) ) {
 
 			var delta = new THREE.Vector3().sub( point, center ).normalize();
-			delta.multiplyByScalar( this.radius ).addSelf( this.center );
+			delta.multiplyScalar( this.radius ).addSelf( this.center );
 
 			return delta;
 		}
@@ -94,4 +93,11 @@
 		return this;
 	};
 
+	THREE.Sphere.prototype.scale = function ( factor ) {
+
+		this.radius *= factor;
+		
+		return this;
+	};
+
 }( THREE ) );