Browse Source

Added computeBoundingBox to Geometry2.

Mr.doob 11 years ago
parent
commit
d43d75245f
2 changed files with 64 additions and 4 deletions
  1. 3 4
      src/core/BufferGeometry.js
  2. 61 0
      src/core/Geometry2.js

+ 3 - 4
src/core/BufferGeometry.js

@@ -87,7 +87,6 @@ THREE.BufferGeometry.prototype = {
 		if ( positions ) {
 
 			var bb = this.boundingBox;
-			var x, y, z;
 
 			if( positions.length >= 3 ) {
 				bb.min.x = bb.max.x = positions[ 0 ];
@@ -97,9 +96,9 @@ THREE.BufferGeometry.prototype = {
 
 			for ( var i = 3, il = positions.length; i < il; i += 3 ) {
 
-				x = positions[ i ];
-				y = positions[ i + 1 ];
-				z = positions[ i + 2 ];
+				var x = positions[ i ];
+				var y = positions[ i + 1 ];
+				var z = positions[ i + 2 ];
 
 				// bounding box
 

+ 61 - 0
src/core/Geometry2.js

@@ -28,6 +28,67 @@ THREE.Geometry2.prototype = {
 
 	},
 
+	computeBoundingBox: function () {
+
+		if ( this.boundingBox === null ) {
+
+			this.boundingBox = new THREE.Box3();
+
+		}
+
+		var vertices = this.vertices;
+		var bb = this.boundingBox;
+
+		if ( vertices.length >= 3 ) {
+
+			bb.min.x = bb.max.x = vertices[ 0 ];
+			bb.min.y = bb.max.y = vertices[ 1 ];
+			bb.min.z = bb.max.z = vertices[ 2 ];
+
+		}
+
+		for ( var i = 3, il = vertices.length; i < il; i += 3 ) {
+
+			var x = vertices[ i ];
+			var y = vertices[ i + 1 ];
+			var z = vertices[ i + 2 ];
+
+			// bounding box
+
+			if ( x < bb.min.x ) {
+
+				bb.min.x = x;
+
+			} else if ( x > bb.max.x ) {
+
+				bb.max.x = x;
+
+			}
+
+			if ( y < bb.min.y ) {
+
+				bb.min.y = y;
+
+			} else if ( y > bb.max.y ) {
+
+				bb.max.y = y;
+
+			}
+
+			if ( z < bb.min.z ) {
+
+				bb.min.z = z;
+
+			} else if ( z > bb.max.z ) {
+
+				bb.max.z = z;
+
+			}
+
+		}
+
+	},
+
 	computeBoundingSphere: function () {
 
 		var box = new THREE.Box3();