Browse Source

+Box3.boundingSphere(), Sphere.bounds()->Sphere.boundingBox(), unit tests.

Ben Houston 12 years ago
parent
commit
2b4373bee2
4 changed files with 26 additions and 5 deletions
  1. 11 0
      src/math/Box3.js
  2. 1 1
      src/math/Sphere.js
  3. 10 0
      test/math/Box3.js
  4. 4 4
      test/math/Sphere.js

+ 11 - 0
src/math/Box3.js

@@ -226,6 +226,17 @@ THREE.Box3.prototype = {
 
 
 	},
 	},
 
 
+	boundingSphere: function ( optionalTarget ) {
+
+		var result = optionalTarget || new THREE.Sphere();
+		
+		result.center = this.center();
+		result.radius = this.size( THREE.Box3.__v0 ).length() * 0.5;;
+
+		return result;
+
+	},
+
 	intersect: function ( box ) {
 	intersect: function ( box ) {
 
 
 		this.min.maxSelf( box.min );
 		this.min.maxSelf( box.min );

+ 1 - 1
src/math/Sphere.js

@@ -85,7 +85,7 @@ THREE.Sphere.prototype = {
 
 
 	},
 	},
 
 
-	bounds: function ( optionalTarget ) {
+	boundingBox: function ( optionalTarget ) {
 
 
 		var box = optionalTarget || new THREE.Box3();
 		var box = optionalTarget || new THREE.Box3();
 
 

+ 10 - 0
test/math/Box3.js

@@ -208,6 +208,16 @@ test( "isIntersectionBox", function() {
 	ok( ! b.isIntersectionBox( c ), "Passed!" );
 	ok( ! b.isIntersectionBox( c ), "Passed!" );
 });
 });
 
 
+test( "boundingSphere", function() {
+	var a = new THREE.Box3( zero3, zero3 );
+	var b = new THREE.Box3( zero3, one3 );
+	var c = new THREE.Box3( one3.clone().negate(), one3 );
+
+	ok( a.boundingSphere().equals( new THREE.Sphere( zero3, 0 ) ), "Passed!" );
+	ok( b.boundingSphere().equals( new THREE.Sphere( one3.clone().multiplyScalar( 0.5 ), Math.sqrt( 3 ) * 0.5 ) ), "Passed!" );
+	ok( c.boundingSphere().equals( new THREE.Sphere( zero3, Math.sqrt( 12 ) * 0.5 ) ), "Passed!" );
+});
+
 test( "intersect", function() {
 test( "intersect", function() {
 	var a = new THREE.Box3( zero3, zero3 );
 	var a = new THREE.Box3( zero3, zero3 );
 	var b = new THREE.Box3( zero3, one3 );
 	var b = new THREE.Box3( zero3, one3 );

+ 4 - 4
test/math/Sphere.js

@@ -67,13 +67,13 @@ test( "clampPoint", function() {
 	ok( a.clampPoint( new THREE.Vector3( 1, 1, -3 ) ).equals( new THREE.Vector3( 1, 1, 0 ) ), "Passed!" );
 	ok( a.clampPoint( new THREE.Vector3( 1, 1, -3 ) ).equals( new THREE.Vector3( 1, 1, 0 ) ), "Passed!" );
 });
 });
 
 
-test( "bounds", function() {
+test( "boundingBox", function() {
 	var a = new THREE.Sphere( one3, 1 );
 	var a = new THREE.Sphere( one3, 1 );
 
 
-	ok( a.bounds().equals( new THREE.Box3( zero3, two3 ) ), "Passed!" );
+	ok( a.boundingBox().equals( new THREE.Box3( zero3, two3 ) ), "Passed!" );
 
 
 	a.set( zero3, 0 )
 	a.set( zero3, 0 )
-	ok( a.bounds().equals( new THREE.Box3( zero3, zero3 ) ), "Passed!" );
+	ok( a.boundingBox().equals( new THREE.Box3( zero3, zero3 ) ), "Passed!" );
 });
 });
 
 
 test( "transform", function() {
 test( "transform", function() {
@@ -83,7 +83,7 @@ test( "transform", function() {
 	var t1 = new THREE.Vector3( 1, -2, 1 );
 	var t1 = new THREE.Vector3( 1, -2, 1 );
 	m.makeTranslation( t1 );
 	m.makeTranslation( t1 );
 
 
-	ok( a.clone().transform( m ).bounds().equals( a.bounds().transform( m ) ), "Passed!" );
+	ok( a.clone().transform( m ).boundingBox().equals( a.boundingBox().transform( m ) ), "Passed!" );
 });
 });
 
 
 test( "translate", function() {
 test( "translate", function() {