Browse Source

finish Sphere unit test coverage (it was previously incomplete) and fix uncovered errors.

Ben Houston 12 years ago
parent
commit
d2c43e69e4
2 changed files with 58 additions and 11 deletions
  1. 3 11
      src/math/Sphere.js
  2. 55 0
      test/math/Sphere.js

+ 3 - 11
src/math/Sphere.js

@@ -98,7 +98,7 @@ THREE.Sphere.prototype = {
 
 	transform: function ( matrix ) {
 		
-		this.center = matrix.multipleVector3( this.center );
+		this.center = matrix.multiplyVector3( this.center );
 		this.radius = this.radius * matrix.getMaxScaleOnAxis();
 
 		return this;
@@ -107,15 +107,7 @@ THREE.Sphere.prototype = {
 
 	translate: function ( offset ) {
 
-		this.center.addSelf( this.offset );
-
-		return this;
-
-	},
-
-	scale: function ( factor ) {
-
-		this.radius *= factor;
+		this.center.addSelf( offset );
 
 		return this;
 
@@ -129,7 +121,7 @@ THREE.Sphere.prototype = {
 
 	clone: function () {
 
-		return new THREE.Sphere3().copy( this );
+		return new THREE.Sphere().copy( this );
 
 	}
 

+ 55 - 0
test/math/Sphere.js

@@ -37,3 +37,58 @@ test( "set", function() {
 	ok( a.center.equals( one3 ), "Passed!" );
 	ok( a.radius == 1, "Passed!" );
 });
+
+test( "empty", function() {
+	var a = new THREE.Sphere();
+	ok( a.empty(), "Passed!" );
+
+	a.set( one3, 1 );
+	ok( ! a.empty(), "Passed!" );
+});
+
+test( "containsPoint", function() {
+	var a = new THREE.Sphere( one3, 1 );
+
+	ok( ! a.containsPoint( zero3 ), "Passed!" );
+	ok( a.containsPoint( one3 ), "Passed!" );
+});
+
+test( "distanceToPoint", function() {
+	var a = new THREE.Sphere( one3, 1 );
+
+	ok( ( a.distanceToPoint( zero3 ) - 0.7320 ) < 0.001, "Passed!" );
+	ok( a.distanceToPoint( one3 ) === -1, "Passed!" );
+});
+
+test( "clampPoint", function() {
+	var a = new THREE.Sphere( one3, 1 );
+
+	ok( a.clampPoint( new THREE.Vector3( 1, 1, 3 ) ).equals( new THREE.Vector3( 1, 1, 2 ) ), "Passed!" );
+	ok( a.clampPoint( new THREE.Vector3( 1, 1, -3 ) ).equals( new THREE.Vector3( 1, 1, 0 ) ), "Passed!" );
+});
+
+test( "bounds", function() {
+	var a = new THREE.Sphere( one3, 1 );
+
+	ok( a.bounds().equals( new THREE.Box3( zero3, two3 ) ), "Passed!" );
+
+	a.set( zero3, 0 )
+	ok( a.bounds().equals( new THREE.Box3( zero3, zero3 ) ), "Passed!" );
+});
+
+test( "transform", function() {
+	var a = new THREE.Sphere( one3, 1 );
+
+	var m = new THREE.Matrix4();
+	var t1 = new THREE.Vector3( 1, -2, 1 );
+	m.makeTranslation( t1.x, t1.y, t1.z );
+
+	ok( a.clone().transform( m ).bounds().equals( a.bounds().transform( m ) ), "Passed!" );
+});
+
+test( "translate", function() {
+	var a = new THREE.Sphere( one3, 1 );
+
+	a.translate( one3.clone().negate() );
+	ok( a.center.equals( zero3 ), "Passed!" );
+});