Browse Source

adopt Sphere.js and Box3.js in Geometry.js.

Ben Houston 12 years ago
parent
commit
cb2b5cece2
3 changed files with 16 additions and 73 deletions
  1. 1 1
      src/core/Box3.js
  2. 3 72
      src/core/Geometry.js
  3. 12 0
      src/core/Sphere.js

+ 1 - 1
src/core/Box3.js

@@ -19,7 +19,7 @@
 	THREE.Box3.fromPoints = function ( points ) {
 
 		var boundingBox = new THREE.Box3();
-		for( var i = 0; i < points.length; i ++ ) {
+		for( var i = 0, numPoints = points.length; i < numPoints; i ++ ) {
 			boundingBox.extendByPoint( points[i] );
 		}
 

+ 3 - 72
src/core/Geometry.js

@@ -4,6 +4,7 @@
  * @author alteredq / http://alteredqualia.com/
  * @author mikael emtinger / http://gomo.se/
  * @author zz85 / http://www.lab4games.net/zz85/blog
+ * @author Ben Houston / [email protected] / http://github.com/bhouston
  */
 
 THREE.Geometry = function () {
@@ -576,82 +577,12 @@ THREE.Geometry.prototype = {
 
 	computeBoundingBox: function () {
 
-		if ( ! this.boundingBox ) {
-
-			this.boundingBox = { min: new THREE.Vector3(), max: new THREE.Vector3() };
-
-		}
-
-		if ( this.vertices.length > 0 ) {
-
-			var position, firstPosition = this.vertices[ 0 ];
-
-			this.boundingBox.min.copy( firstPosition );
-			this.boundingBox.max.copy( firstPosition );
-
-			var min = this.boundingBox.min,
-				max = this.boundingBox.max;
-
-			for ( var v = 1, vl = this.vertices.length; v < vl; v ++ ) {
-
-				position = this.vertices[ v ];
-
-				if ( position.x < min.x ) {
-
-					min.x = position.x;
-
-				} else if ( position.x > max.x ) {
-
-					max.x = position.x;
-
-				}
-
-				if ( position.y < min.y ) {
-
-					min.y = position.y;
-
-				} else if ( position.y > max.y ) {
-
-					max.y = position.y;
-
-				}
-
-				if ( position.z < min.z ) {
-
-					min.z = position.z;
-
-				} else if ( position.z > max.z ) {
-
-					max.z = position.z;
-
-				}
-
-			}
-
-		} else {
-
-			this.boundingBox.min.set( 0, 0, 0 );
-			this.boundingBox.max.set( 0, 0, 0 );
-
-		}
-
+		this.boundingBox = THREE.Box3.fromPoints( this.vertices );
 	},
 
 	computeBoundingSphere: function () {
 
-		var maxRadiusSq = 0;
-
-		if ( this.boundingSphere === null ) this.boundingSphere = { radius: 0 };
-
-		for ( var i = 0, l = this.vertices.length; i < l; i ++ ) {
-
-			var radiusSq = this.vertices[ i ].lengthSq();
-			if ( radiusSq > maxRadiusSq ) maxRadiusSq = radiusSq;
-
-		}
-
-		this.boundingSphere.radius = Math.sqrt( maxRadiusSq );
-
+		this.boundingSphere = THREE.Sphere.fromCenterAndPoints( new THREE.Vector3(), this.vertices );
 	},
 
 	/*

+ 12 - 0
src/core/Sphere.js

@@ -15,6 +15,18 @@
 
 	};
 
+	THREE.Sphere.fromCenterAndPoints = function ( center, points ) {
+		var maxRadiusSq = 0;
+		var delta = new THREE.Vector3().copy( center );
+
+		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.set = function ( center, radius ) {
 
 		this.center = center;