From 2b4373bee2f765584f67d40aeb41bb2041e23442 Mon Sep 17 00:00:00 2001 From: Ben Houston Date: Mon, 17 Dec 2012 16:02:07 -0500 Subject: [PATCH] +Box3.boundingSphere(), Sphere.bounds()->Sphere.boundingBox(), unit tests. --- src/math/Box3.js | 11 +++++++++++ src/math/Sphere.js | 2 +- test/math/Box3.js | 10 ++++++++++ test/math/Sphere.js | 8 ++++---- 4 files changed, 26 insertions(+), 5 deletions(-) diff --git a/src/math/Box3.js b/src/math/Box3.js index 00999d7c57..bed02f3e9b 100644 --- a/src/math/Box3.js +++ b/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 ) { this.min.maxSelf( box.min ); diff --git a/src/math/Sphere.js b/src/math/Sphere.js index aab56ed463..a322748ee0 100644 --- a/src/math/Sphere.js +++ b/src/math/Sphere.js @@ -85,7 +85,7 @@ THREE.Sphere.prototype = { }, - bounds: function ( optionalTarget ) { + boundingBox: function ( optionalTarget ) { var box = optionalTarget || new THREE.Box3(); diff --git a/test/math/Box3.js b/test/math/Box3.js index 8aa4a3241d..700ee96e43 100644 --- a/test/math/Box3.js +++ b/test/math/Box3.js @@ -208,6 +208,16 @@ test( "isIntersectionBox", function() { 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() { var a = new THREE.Box3( zero3, zero3 ); var b = new THREE.Box3( zero3, one3 ); diff --git a/test/math/Sphere.js b/test/math/Sphere.js index a7117d1fd8..cdcd0ae058 100644 --- a/test/math/Sphere.js +++ b/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!" ); }); -test( "bounds", function() { +test( "boundingBox", function() { 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 ) - ok( a.bounds().equals( new THREE.Box3( zero3, zero3 ) ), "Passed!" ); + ok( a.boundingBox().equals( new THREE.Box3( zero3, zero3 ) ), "Passed!" ); }); test( "transform", function() { @@ -83,7 +83,7 @@ test( "transform", function() { var t1 = new THREE.Vector3( 1, -2, 1 ); 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() { -- GitLab