Browse Source

implemented Box3.transform, tested. Removed Box3/Box3.scale as it doesn't work in the general case. move tests to tests/math from tests/core to replace move of tested source files.

Ben Houston 12 years ago
parent
commit
05af1b28ce

+ 0 - 9
src/math/Box2.js

@@ -237,15 +237,6 @@ THREE.Box2.prototype = {
 
 
 	},
 	},
 
 
-	scale: function ( factor ) {
-
-		var sizeDeltaHalf = this.size().multiplyScalar( ( factor - 1 )  * 0.5 );
-		this.expandByVector( sizeDeltaHalf );
-
-		return this;
-
-	},
-
 	equals: function ( box ) {
 	equals: function ( box ) {
 
 
 		return box.min.equals( this.min ) && box.max.equals( this.max );
 		return box.min.equals( this.min ) && box.max.equals( this.max );

+ 26 - 7
src/math/Box3.js

@@ -244,19 +244,31 @@ THREE.Box3.prototype = {
 
 
 	},
 	},
 
 
-	translate: function ( offset ) {
-
-		this.min.addSelf( offset );
-		this.max.addSelf( offset );
+	transform: function ( matrix ) {
+		
+		// NOTE: I am using a binary pattern to specify all 2^3 combinations below
+		var newPoints = [
+			matrix.multiplyVector3( THREE.Box3.__v0.set( this.min.x, this.min.y, this.min.z ) ), // 000
+			matrix.multiplyVector3( THREE.Box3.__v1.set( this.min.x, this.min.y, this.max.z ) ), // 001
+			matrix.multiplyVector3( THREE.Box3.__v2.set( this.min.x, this.max.y, this.min.z ) ), // 010
+			matrix.multiplyVector3( THREE.Box3.__v3.set( this.min.x, this.max.y, this.max.z ) ), // 011
+			matrix.multiplyVector3( THREE.Box3.__v4.set( this.max.x, this.min.y, this.min.z ) ), // 100
+			matrix.multiplyVector3( THREE.Box3.__v5.set( this.max.x, this.min.y, this.max.z ) ), // 101
+			matrix.multiplyVector3( THREE.Box3.__v6.set( this.max.x, this.max.y, this.min.z ) ), // 110
+			matrix.multiplyVector3( THREE.Box3.__v7.set( this.max.x, this.max.y, this.max.z ) )  // 111
+		];
+
+		this.makeEmpty();
+		this.setFromPoints( newPoints );
 
 
 		return this;
 		return this;
 
 
 	},
 	},
 
 
-	scale: function ( factor ) {
+	translate: function ( offset ) {
 
 
-		var sizeDeltaHalf = this.size().multiplyScalar( ( factor - 1 )  * 0.5 );
-		this.expandByVector( sizeDeltaHalf );
+		this.min.addSelf( offset );
+		this.max.addSelf( offset );
 
 
 		return this;
 		return this;
 
 
@@ -276,4 +288,11 @@ THREE.Box3.prototype = {
 
 
 };
 };
 
 
+THREE.Box3.__v0 = new THREE.Vector3();
 THREE.Box3.__v1 = new THREE.Vector3();
 THREE.Box3.__v1 = new THREE.Vector3();
+THREE.Box3.__v2 = new THREE.Vector3();
+THREE.Box3.__v3 = new THREE.Vector3();
+THREE.Box3.__v4 = new THREE.Vector3();
+THREE.Box3.__v5 = new THREE.Vector3();
+THREE.Box3.__v6 = new THREE.Vector3();
+THREE.Box3.__v7 = new THREE.Vector3();

+ 1 - 13
test/core/Box2.js → test/math/Box2.js

@@ -242,16 +242,4 @@ test( "translate", function() {
 	ok( a.clone().translate( one2 ).translate( one2.clone().negate() ).equals( a ), "Passed!" );
 	ok( a.clone().translate( one2 ).translate( one2.clone().negate() ).equals( a ), "Passed!" );
 	ok( d.clone().translate( one2 ).equals( b ), "Passed!" );
 	ok( d.clone().translate( one2 ).equals( b ), "Passed!" );
 	ok( b.clone().translate( one2.clone().negate() ).equals( d ), "Passed!" );
 	ok( b.clone().translate( one2.clone().negate() ).equals( d ), "Passed!" );
-});
-
-test( "scale", function() {
-	var a = new THREE.Box2( zero2, zero2 );
-	var b = new THREE.Box2( zero2, one2 );
-	var c = new THREE.Box2( one2.clone().negate(), one2 );
-	var d = new THREE.Box2( one2.clone().negate(), zero2 );
-
-	ok( a.clone().scale( 0 ).equals( a ), "Passed!" );
-	ok( c.clone().scale( 0 ).equals( a ), "Passed!" );
-	ok( b.clone().scale( 3 ).equals( new THREE.Box2( new THREE.Vector2( -1, -1, -1 ), new THREE.Vector2( 2, 2, 2 ) ) ), "Passed!" );
-	ok( d.clone().scale( 3 ).equals( new THREE.Box2( new THREE.Vector2( 2, 2, 2 ).negate(), new THREE.Vector2( 1, 1, 1 ) ) ), "Passed!" );
-});
+});

+ 20 - 10
test/core/Box3.js → test/math/Box3.js

@@ -232,26 +232,36 @@ test( "union", function() {
 	ok( b.clone().union( c ).equals( c ), "Passed!" );
 	ok( b.clone().union( c ).equals( c ), "Passed!" );
 });
 });
 
 
-test( "translate", function() {
+var compareBox = function ( a, b, threshold ) {
+	threshold = threshold || 0.0001;
+	return ( a.min.distanceTo( b.min ) < threshold &&
+	a.max.distanceTo( b.max ) < threshold );
+};
+
+test( "transform", 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 );
 	var c = new THREE.Box3( one3.clone().negate(), one3 );
 	var c = new THREE.Box3( one3.clone().negate(), one3 );
 	var d = new THREE.Box3( one3.clone().negate(), zero3 );
 	var d = new THREE.Box3( one3.clone().negate(), zero3 );
 
 
-	ok( a.clone().translate( one3 ).equals( new THREE.Box3( one3, one3 ) ), "Passed!" );
-	ok( a.clone().translate( one3 ).translate( one3.clone().negate() ).equals( a ), "Passed!" );
-	ok( d.clone().translate( one3 ).equals( b ), "Passed!" );
-	ok( b.clone().translate( one3.clone().negate() ).equals( d ), "Passed!" );
+	var m = new THREE.Matrix4();
+
+	var t1 = new THREE.Vector3( 1, -2, 1 );
+	m.makeTranslation( t1.x, t1.y, t1.z );
+	ok( compareBox( a.clone().transform( m ), a.clone().translate( t1 ) ), "Passed!" );
+	ok( compareBox( b.clone().transform( m ), b.clone().translate( t1 ) ), "Passed!" );
+	ok( compareBox( c.clone().transform( m ), c.clone().translate( t1 ) ), "Passed!" );
+	ok( compareBox( d.clone().transform( m ), d.clone().translate( t1 ) ), "Passed!" );
 });
 });
 
 
-test( "scale", function() {
+test( "translate", 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 );
 	var c = new THREE.Box3( one3.clone().negate(), one3 );
 	var c = new THREE.Box3( one3.clone().negate(), one3 );
 	var d = new THREE.Box3( one3.clone().negate(), zero3 );
 	var d = new THREE.Box3( one3.clone().negate(), zero3 );
 
 
-	ok( a.clone().scale( 0 ).equals( a ), "Passed!" );
-	ok( c.clone().scale( 0 ).equals( a ), "Passed!" );
-	ok( b.clone().scale( 3 ).equals( new THREE.Box3( new THREE.Vector3( -1, -1, -1 ), new THREE.Vector3( 2, 2, 2 ) ) ), "Passed!" );
-	ok( d.clone().scale( 3 ).equals( new THREE.Box3( new THREE.Vector3( 2, 2, 2 ).negate(), new THREE.Vector3( 1, 1, 1 ) ) ), "Passed!" );
+	ok( a.clone().translate( one3 ).equals( new THREE.Box3( one3, one3 ) ), "Passed!" );
+	ok( a.clone().translate( one3 ).translate( one3.clone().negate() ).equals( a ), "Passed!" );
+	ok( d.clone().translate( one3 ).equals( b ), "Passed!" );
+	ok( b.clone().translate( one3.clone().negate() ).equals( d ), "Passed!" );
 });
 });

+ 0 - 0
test/core/Constants.js → test/math/Constants.js


+ 0 - 0
test/core/Plane.js → test/math/Plane.js


+ 5 - 5
test/core/Ray.js → test/math/Ray.js

@@ -149,26 +149,26 @@ test( "intersectPlane", function() {
 });
 });
 
 
 
 
-test( "transformSelf", function() {
+test( "transform", function() {
 	var a = new THREE.Ray( one3, new THREE.Vector3( 0, 0, 1 ) );
 	var a = new THREE.Ray( one3, new THREE.Vector3( 0, 0, 1 ) );
 	var m = new THREE.Matrix4().identity();
 	var m = new THREE.Matrix4().identity();
 
 
-	ok( a.clone().transformSelf( m ).equals( a ), "Passed!" );
+	ok( a.clone().transform( m ).equals( a ), "Passed!" );
 
 
 	a = new THREE.Ray( zero3, new THREE.Vector3( 0, 0, 1 ) );
 	a = new THREE.Ray( zero3, new THREE.Vector3( 0, 0, 1 ) );
 	m.rotateByAxis( new THREE.Vector3( 0, 0, 1 ), Math.PI );
 	m.rotateByAxis( new THREE.Vector3( 0, 0, 1 ), Math.PI );
-	ok( a.clone().transformSelf( m ).equals( a ), "Passed!" );
+	ok( a.clone().transform( m ).equals( a ), "Passed!" );
 
 
 	m.identity().rotateX( Math.PI );
 	m.identity().rotateX( Math.PI );
 	var b = a.clone();
 	var b = a.clone();
 	b.direction.negate();
 	b.direction.negate();
-	var a2 = a.clone().transformSelf( m );
+	var a2 = a.clone().transform( m );
 	ok( a2.origin.distanceTo( b.origin ) < 0.0001, "Passed!" );
 	ok( a2.origin.distanceTo( b.origin ) < 0.0001, "Passed!" );
 	ok( a2.direction.distanceTo( b.direction ) < 0.0001, "Passed!" );
 	ok( a2.direction.distanceTo( b.direction ) < 0.0001, "Passed!" );
 
 
 	a.origin = new THREE.Vector3( 0, 0, 1 );
 	a.origin = new THREE.Vector3( 0, 0, 1 );
 	b.origin = new THREE.Vector3( 0, 0, -1 );
 	b.origin = new THREE.Vector3( 0, 0, -1 );
-	var a2 = a.clone().transformSelf( m );
+	var a2 = a.clone().transform( m );
 	ok( a2.origin.distanceTo( b.origin ) < 0.0001, "Passed!" );
 	ok( a2.origin.distanceTo( b.origin ) < 0.0001, "Passed!" );
 	ok( a2.direction.distanceTo( b.direction ) < 0.0001, "Passed!" );
 	ok( a2.direction.distanceTo( b.direction ) < 0.0001, "Passed!" );
 });
 });

+ 0 - 0
test/core/Sphere.js → test/math/Sphere.js


+ 0 - 0
test/core/Triangle.js → test/math/Triangle.js


+ 0 - 0
test/core/Vector2.js → test/math/Vector2.js


+ 0 - 0
test/core/Vector3.js → test/math/Vector3.js


+ 0 - 0
test/core/Vector4.js → test/math/Vector4.js


+ 10 - 10
test/sources.html

@@ -26,16 +26,16 @@
 
 
   <!-- add class-based unit tests below -->
   <!-- add class-based unit tests below -->
 
 
-  <script src="core/Constants.js"></script>
-  <script src="core/Box2.js"></script>
-  <script src="core/Box3.js"></script>
-  <script src="core/Plane.js"></script>
-  <script src="core/Ray.js"></script>
-  <script src="core/Sphere.js"></script>
-  <script src="core/Triangle.js"></script>
-  <script src="core/Vector2.js"></script>
-  <script src="core/Vector3.js"></script>
-  <script src="core/Vector4.js"></script>
+  <script src="math/Constants.js"></script>
+  <script src="math/Box2.js"></script>
+  <script src="math/Box3.js"></script>
+  <script src="math/Plane.js"></script>
+  <script src="math/Ray.js"></script>
+  <script src="math/Sphere.js"></script>
+  <script src="math/Triangle.js"></script>
+  <script src="math/Vector2.js"></script>
+  <script src="math/Vector3.js"></script>
+  <script src="math/Vector4.js"></script>
   
   
 </body>
 </body>
 </html>
 </html>

+ 10 - 10
test/three-math.html

@@ -15,16 +15,16 @@
 
 
   <!-- add class-based unit tests below -->
   <!-- add class-based unit tests below -->
 
 
-  <script src="core/Constants.js"></script>
-  <script src="core/Box2.js"></script>
-  <script src="core/Box3.js"></script>
-  <script src="core/Plane.js"></script>
-  <script src="core/Ray.js"></script>
-  <script src="core/Sphere.js"></script>
-  <script src="core/Triangle.js"></script>
-  <script src="core/Vector2.js"></script>
-  <script src="core/Vector3.js"></script>
-  <script src="core/Vector4.js"></script>
+  <script src="math/Constants.js"></script>
+  <script src="math/Box2.js"></script>
+  <script src="math/Box3.js"></script>
+  <script src="math/Plane.js"></script>
+  <script src="math/Ray.js"></script>
+  <script src="math/Sphere.js"></script>
+  <script src="math/Triangle.js"></script>
+  <script src="math/Vector2.js"></script>
+  <script src="math/Vector3.js"></script>
+  <script src="math/Vector4.js"></script>
   
   
 </body>
 </body>
 </html>
 </html>

+ 10 - 10
test/three.html

@@ -15,16 +15,16 @@
 
 
   <!-- add class-based unit tests below -->
   <!-- add class-based unit tests below -->
 
 
-  <script src="core/Constants.js"></script>
-  <script src="core/Box2.js"></script>
-  <script src="core/Box3.js"></script>
-  <script src="core/Plane.js"></script>
-  <script src="core/Ray.js"></script>
-  <script src="core/Sphere.js"></script>
-  <script src="core/Triangle.js"></script>
-  <script src="core/Vector2.js"></script>
-  <script src="core/Vector3.js"></script>
-  <script src="core/Vector4.js"></script>
+  <script src="math/Constants.js"></script>
+  <script src="math/Box2.js"></script>
+  <script src="math/Box3.js"></script>
+  <script src="math/Plane.js"></script>
+  <script src="math/Ray.js"></script>
+  <script src="math/Sphere.js"></script>
+  <script src="math/Triangle.js"></script>
+  <script src="math/Vector2.js"></script>
+  <script src="math/Vector3.js"></script>
+  <script src="math/Vector4.js"></script>
   
   
 </body>
 </body>
 </html>
 </html>

+ 10 - 10
test/three.min.html

@@ -15,16 +15,16 @@
 
 
   <!-- add class-based unit tests below -->
   <!-- add class-based unit tests below -->
 
 
-  <script src="core/Constants.js"></script>
-  <script src="core/Box2.js"></script>
-  <script src="core/Box3.js"></script>
-  <script src="core/Plane.js"></script>
-  <script src="core/Ray.js"></script>
-  <script src="core/Sphere.js"></script>
-  <script src="core/Triangle.js"></script>
-  <script src="core/Vector2.js"></script>
-  <script src="core/Vector3.js"></script>
-  <script src="core/Vector4.js"></script>
+  <script src="math/Constants.js"></script>
+  <script src="math/Box2.js"></script>
+  <script src="math/Box3.js"></script>
+  <script src="math/Plane.js"></script>
+  <script src="math/Ray.js"></script>
+  <script src="math/Sphere.js"></script>
+  <script src="math/Triangle.js"></script>
+  <script src="math/Vector2.js"></script>
+  <script src="math/Vector3.js"></script>
+  <script src="math/Vector4.js"></script>
   
   
 </body>
 </body>
 </html>
 </html>