Browse Source

Added sphere.center support to BufferGeometry. See #3748.
Haven't tested if the box.addPoint decreases performance...

Mr.doob 12 years ago
parent
commit
1de68da790
3 changed files with 67 additions and 45 deletions
  1. 29 16
      src/core/BufferGeometry.js
  2. 26 22
      src/math/Box3.js
  3. 12 7
      src/math/Sphere.js

+ 29 - 16
src/core/BufferGeometry.js

@@ -141,35 +141,48 @@ THREE.BufferGeometry.prototype = {
 
 
 	computeBoundingSphere: function () {
 	computeBoundingSphere: function () {
 
 
-		if ( this.boundingSphere === null ) {
+		var box = new THREE.Box3();
+		var vector = new THREE.Vector3();
 
 
-			this.boundingSphere = new THREE.Sphere();
+		return function () {
 
 
-		}
+			if ( this.boundingSphere === null ) {
 
 
-		var positions = this.attributes[ "position" ].array;
+				this.boundingSphere = new THREE.Sphere();
 
 
-		if ( positions ) {
+			}
 
 
-			var radiusSq, maxRadiusSq = 0;
-			var x, y, z;
+			var positions = this.attributes[ "position" ].array;
 
 
-			for ( var i = 0, il = positions.length; i < il; i += 3 ) {
+			if ( positions ) {
 
 
-				x = positions[ i ];
-				y = positions[ i + 1 ];
-				z = positions[ i + 2 ];
+				var center = this.boundingSphere.center;
 
 
-				radiusSq =  x * x + y * y + z * z;
-				if ( radiusSq > maxRadiusSq ) maxRadiusSq = radiusSq;
+				for ( var i = 0, il = positions.length; i < il; i += 3 ) {
 
 
-			}
+					vector.set( positions[ i ], positions[ i + 1 ], positions[ i + 2 ] );
+					box.addPoint( vector );
 
 
-			this.boundingSphere.radius = Math.sqrt( maxRadiusSq );
+				}
+
+				box.center( center );
+
+				var maxRadiusSq = 0;
+
+				for ( var i = 0, il = positions.length; i < il; i += 3 ) {
+
+					vector.set( positions[ i ], positions[ i + 1 ], positions[ i + 2 ] );
+					maxRadiusSq = Math.max( maxRadiusSq, center.distanceToSquared( vector ) );
+
+				}
+
+				this.boundingSphere.radius = Math.sqrt( maxRadiusSq );
+
+			}
 
 
 		}
 		}
 
 
-	},
+	}(),
 
 
 	computeVertexNormals: function () {
 	computeVertexNormals: function () {
 
 

+ 26 - 22
src/math/Box3.js

@@ -23,48 +23,52 @@ THREE.Box3.prototype = {
 
 
 	},
 	},
 
 
-	setFromPoints: function ( points ) {
+	addPoint: function ( point ) {
 
 
-		if ( points.length > 0 ) {
+		if ( point.x < this.min.x ) {
 
 
-			var point = points[ 0 ];
+			this.min.x = point.x;
 
 
-			this.min.copy( point );
-			this.max.copy( point );
+		} else if ( point.x > this.max.x ) {
 
 
-			for ( var i = 1, il = points.length; i < il; i ++ ) {
+			this.max.x = point.x;
 
 
-				point = points[ i ];
+		}
 
 
-				if ( point.x < this.min.x ) {
+		if ( point.y < this.min.y ) {
 
 
-					this.min.x = point.x;
+			this.min.y = point.y;
 
 
-				} else if ( point.x > this.max.x ) {
+		} else if ( point.y > this.max.y ) {
 
 
-					this.max.x = point.x;
+			this.max.y = point.y;
 
 
-				}
+		}
 
 
-				if ( point.y < this.min.y ) {
+		if ( point.z < this.min.z ) {
 
 
-					this.min.y = point.y;
+			this.min.z = point.z;
 
 
-				} else if ( point.y > this.max.y ) {
+		} else if ( point.z > this.max.z ) {
 
 
-					this.max.y = point.y;
+			this.max.z = point.z;
 
 
-				}
+		}
 
 
-				if ( point.z < this.min.z ) {
+	},
 
 
-					this.min.z = point.z;
+	setFromPoints: function ( points ) {
 
 
-				} else if ( point.z > this.max.z ) {
+		if ( points.length > 0 ) {
 
 
-					this.max.z = point.z;
+			var point = points[ 0 ];
 
 
-				}
+			this.min.copy( point );
+			this.max.copy( point );
+
+			for ( var i = 1, il = points.length; i < il; i ++ ) {
+
+				this.addPoint( points[ i ] )
 
 
 			}
 			}
 
 

+ 12 - 7
src/math/Sphere.js

@@ -25,25 +25,30 @@ THREE.Sphere.prototype = {
 
 
 	setFromPoints: function () {
 	setFromPoints: function () {
 
 
-		var _box = new THREE.Box3();
-		var _center = new THREE.Vector3();
+		var box = new THREE.Box3();
 
 
 		return function ( points, optionalCenter )  {
 		return function ( points, optionalCenter )  {
 
 
-			// use boundingBox center as sphere center
+			var center = this.center;
 
 
-			var center = optionalCenter || _box.setFromPoints( points ).center( _center );
+			if ( optionalCenter !== undefined ) {
+
+				center.copy( optionalCenter );
+
+			} else {
+
+				box.setFromPoints( points ).center( center );
+
+			}
 
 
 			var maxRadiusSq = 0;
 			var maxRadiusSq = 0;
 
 
 			for ( var i = 0, il = points.length; i < il; i ++ ) {
 			for ( var i = 0, il = points.length; i < il; i ++ ) {
 
 
-				var radiusSq = center.distanceToSquared( points[ i ] );
-				maxRadiusSq = Math.max( maxRadiusSq, radiusSq );
+				maxRadiusSq = Math.max( maxRadiusSq, center.distanceToSquared( points[ i ] ) );
 
 
 			}
 			}
 
 
-			this.center.copy( center );
 			this.radius = Math.sqrt( maxRadiusSq );
 			this.radius = Math.sqrt( maxRadiusSq );
 
 
 			return this;			
 			return this;