Browse Source

move away from static fromXXX-style constructors to setXXX style member functions widely used in ThreeJS.

Ben Houston 12 years ago
parent
commit
5c373708ee
4 changed files with 57 additions and 53 deletions
  1. 20 20
      src/core/Box3.js
  2. 2 2
      src/core/Geometry.js
  3. 21 20
      src/core/Plane.js
  4. 14 11
      src/core/Sphere.js

+ 20 - 20
src/core/Box3.js

@@ -14,26 +14,6 @@ THREE.Box3 = function ( min, max ) {
 	}
 };
 
-THREE.Box3.fromPoints = function ( points ) {
-
-	var boundingBox = new THREE.Box3();
-	for( var i = 0, numPoints = points.length; i < numPoints; i ++ ) {
-		boundingBox.extendByPoint( points[i] );
-	}
-
-	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 = {
 
 	constructor: THREE.Box3,
@@ -46,6 +26,26 @@ THREE.Box3.prototype = {
 		return this;
 	},
 
+	setFromPoints: function ( points ) {
+
+		this.makeEmpty();
+		
+		for( var i = 0, numPoints = points.length; i < numPoints; i ++ ) {
+			this.expandByPoint( points[i] );
+		}
+
+		return this;
+	};
+
+	setFromCenterAndSize: function ( center, size ) {
+
+		var halfSize = new THREE.Vector3().copy( size ).multiplyScalar( 0.5 );
+		this.min.copy( center ).subSelf( halfSize );
+		this.max.copy( center ).addSelf( halfSize );
+
+		return box;	
+	},
+
 	copy: function ( box ) {
 
 		this.min = box.min;

+ 2 - 2
src/core/Geometry.js

@@ -577,12 +577,12 @@ THREE.Geometry.prototype = {
 
 	computeBoundingBox: function () {
 
-		this.boundingBox = THREE.Box3.fromPoints( this.vertices );
+		this.boundingBox = new THREE.Box3().setFromPoints( this.vertices );
 	},
 
 	computeBoundingSphere: function () {
 
-		this.boundingSphere = THREE.Sphere.fromCenterAndPoints( new THREE.Vector3(), this.vertices );
+		this.boundingSphere = new THREE.Sphere().setFromCenterAndPoints( new THREE.Vector3(), this.vertices );
 	},
 
 	/*

+ 21 - 20
src/core/Plane.js

@@ -9,26 +9,6 @@ THREE.Plane = function ( normal, constant ) {
 
 };
 
-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.fromCoplanarPoints = function ( a, b, c ) {
-	// NOTE: This function doens't support optional parameters like the constructor.
-
-	var normal = new THREE.Vector3().sub( b, a ).cross(
-		new THREE.Vector3().sub( c, a ) );
-
-	// Q: should an error be thrown if normal is zero (e.g. degenerate plane)?
-
-	return THREE.Plane.fromNormalAndCoplanarPoint( normal, a );
-};
-
 THREE.Plane.prototype = {
 
 	constructor: THREE.Plane,
@@ -49,6 +29,27 @@ THREE.Plane.prototype = {
 		return this;
 	},
 
+	setFromNormalAndCoplanarPoint: function ( normal, point ) {
+		// NOTE: This function doens't support optional parameters like the constructor.
+
+		this.normal = normal;
+		this.constant = - point.dot( normal );
+
+		return this;
+	},
+
+	setFromCoplanarPoints: function ( a, b, c ) {
+		// NOTE: This function doens't support optional parameters like the constructor.
+
+		var normal = new THREE.Vector3().sub( b, a ).cross(
+			new THREE.Vector3().sub( c, a ) );
+
+		// Q: should an error be thrown if normal is zero (e.g. degenerate plane)?
+		this.setFromNormalAndCoplanarPoint( normal, a );
+		
+		return this;
+	},
+
 	copy: function ( plane ) {
 
 		this.normal = plane.normal;

+ 14 - 11
src/core/Sphere.js

@@ -9,17 +9,6 @@ THREE.Sphere = function ( center, radius ) {
 
 };
 
-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 );
-	}
-
-	return new THREE.Sphere( center, Math.sqrt( maxRadiusSq ) );
-};
-
 THREE.Sphere.prototype = {
 
 	constructor: THREE.Sphere,
@@ -32,6 +21,20 @@ THREE.Sphere.prototype = {
 		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] );
+			maxRadiusSq = Math.max( maxRadiusSq, radiusSq );
+		}
+
+		this.center = center;
+		this.radius = Math.sqrt( maxRadiusSq );
+
+		return this;
+	},
+
 	copy: function ( sphere ) {
 
 		this.center = sphere.center;