Quellcode durchsuchen

more Frustum unit tests + minor optimization to Plane.

Ben Houston vor 12 Jahren
Ursprung
Commit
9d7a120b82
2 geänderte Dateien mit 117 neuen und 4 gelöschten Zeilen
  1. 3 4
      src/math/Plane.js
  2. 114 0
      test/unit/math/Frustum.js

+ 3 - 4
src/math/Plane.js

@@ -164,14 +164,13 @@ THREE.Plane.prototype = {
 
 	transform: function( matrix, optionalNormalMatrix ) {
 
-		var newNormal = THREE.Plane.__v1, newCoplanarPoint = THREE.Plane.__v2;
-
 		// compute new normal based on theory here:
 		// http://www.songho.ca/opengl/gl_normaltransform.html
 		optionalNormalMatrix = optionalNormalMatrix || new THREE.Matrix3().getInverse( matrix ).transpose();
-		newNormal = optionalNormalMatrix.multiplyVector3( newNormal.copy( this.normal ) );
 
-		newCoplanarPoint = this.coplanarPoint( newCoplanarPoint );
+		var newNormal = optionalNormalMatrix.multiplyVector3( THREE.Plane.__v1.copy( this.normal ) );
+
+		var newCoplanarPoint = this.coplanarPoint( THREE.Plane.__v2 );	// __v2 is the optionalTarget
 		newCoplanarPoint = matrix.multiplyVector3( newCoplanarPoint );
 
 		this.setFromNormalAndCoplanarPoint( newNormal, newCoplanarPoint );

+ 114 - 0
test/unit/math/Frustum.js

@@ -4,11 +4,67 @@
 
 module( "Frustum" );
 
+var unit3 = new THREE.Vector3( 1, 0, 0 );
+
+var planeEquals = function ( a, b, tolerance ) {
+	tolerance = tolerance || 0.0001;
+	if( a.normal.distanceTo( b.normal ) > tolerance ) {
+		return false;
+	}
+	if( Math.abs( a.constant - b.constant ) > tolerance ) {
+		return false;
+	}
+	return true;
+};
+
 test( "constructor", function() {
 	var a = new THREE.Frustum();
 
 	ok( a.planes !== undefined, "Passed!" );
 	ok( a.planes.length === 6, "Passed!" );
+
+	var pDefault = new THREE.Plane();
+	for( var i = 0; i < 6; i ++ ) {
+		ok( a.planes[i].equals( pDefault ), "Passed!" );
+	}
+
+	var p0 = new THREE.Plane( unit3, -1 );
+	var p1 = new THREE.Plane( unit3, 1 );
+	var p2 = new THREE.Plane( unit3, 2 );
+	var p3 = new THREE.Plane( unit3, 3 );
+	var p4 = new THREE.Plane( unit3, 4 );
+	var p5 = new THREE.Plane( unit3, 5 );
+
+	a = new THREE.Frustum( p0, p1, p2, p3, p4, p5 );
+	ok( a.planes[0].equals( p0 ), "Passed!" );
+	ok( a.planes[1].equals( p1 ), "Passed!" );
+	ok( a.planes[2].equals( p2 ), "Passed!" );
+	ok( a.planes[3].equals( p3 ), "Passed!" );
+	ok( a.planes[4].equals( p4 ), "Passed!" );
+	ok( a.planes[5].equals( p5 ), "Passed!" );
+});
+
+test( "copy", function() {
+
+	var p0 = new THREE.Plane( unit3, -1 );
+	var p1 = new THREE.Plane( unit3, 1 );
+	var p2 = new THREE.Plane( unit3, 2 );
+	var p3 = new THREE.Plane( unit3, 3 );
+	var p4 = new THREE.Plane( unit3, 4 );
+	var p5 = new THREE.Plane( unit3, 5 );
+
+	var b = new THREE.Frustum( p0, p1, p2, p3, p4, p5 );
+	var a = new THREE.Frustum().copy( b );
+	ok( a.planes[0].equals( p0 ), "Passed!" );
+	ok( a.planes[1].equals( p1 ), "Passed!" );
+	ok( a.planes[2].equals( p2 ), "Passed!" );
+	ok( a.planes[3].equals( p3 ), "Passed!" );
+	ok( a.planes[4].equals( p4 ), "Passed!" );
+	ok( a.planes[5].equals( p5 ), "Passed!" );
+
+	// ensure it is a true copy by modifying source
+	b.planes[0] = p1;
+	ok( a.planes[0].equals( p0 ), "Passed!" );
 });
 
 test( "setFromMatrix/makeOrthographic/containsPoint", function() {
@@ -48,4 +104,62 @@ test( "setFromMatrix/makeFrustum/containsPoint", function() {
 	ok( a.containsPoint( new THREE.Vector3( 99.999, 99.999, -99.999 ) ), "Passed!" );
 	ok( ! a.containsPoint( new THREE.Vector3( 100.1, 100.1, -100.1 ) ), "Passed!" );
 	ok( ! a.containsPoint( new THREE.Vector3( 0, 0, -101 ) ), "Passed!" );
+});
+
+test( "transform", function() {
+
+	var p0 = new THREE.Plane( unit3, -1 );
+	var p1 = new THREE.Plane( unit3, 1 );
+	var p2 = new THREE.Plane( unit3, 2 );
+	var p3 = new THREE.Plane( unit3, 3 );
+	var p4 = new THREE.Plane( unit3, 4 );
+	var p5 = new THREE.Plane( unit3, 5 );
+
+	var b = new THREE.Frustum( p0, p1, p2, p3, p4, p5 );
+	var a = new THREE.Frustum().copy( b ).transform( new THREE.Matrix4() );
+	ok( a.planes[0].equals( p0 ), "Passed!" );
+	ok( a.planes[1].equals( p1 ), "Passed!" );
+	ok( a.planes[2].equals( p2 ), "Passed!" );
+	ok( a.planes[3].equals( p3 ), "Passed!" );
+	ok( a.planes[4].equals( p4 ), "Passed!" );
+	ok( a.planes[5].equals( p5 ), "Passed!" );
+
+	a = new THREE.Frustum().copy( b ).transform( new THREE.Matrix4().makeRotationZ( Math.PI ) );
+	ok( ! planeEquals( a.planes[0], p0 ), "Passed!" );
+	ok( ! planeEquals( a.planes[1], p1 ), "Passed!" );
+	ok( ! planeEquals( a.planes[2], p2 ), "Passed!" );
+	ok( ! planeEquals( a.planes[3], p3 ), "Passed!" );
+	ok( ! planeEquals( a.planes[4], p4 ), "Passed!" );
+	ok( ! planeEquals( a.planes[5], p5 ), "Passed!" );
+
+	a = a.transform( new THREE.Matrix4().makeRotationZ( Math.PI ) );
+	ok( planeEquals( a.planes[0], p0 ), "Passed!" );
+	ok( planeEquals( a.planes[1], p1 ), "Passed!" );
+	ok( planeEquals( a.planes[2], p2 ), "Passed!" );
+	ok( planeEquals( a.planes[3], p3 ), "Passed!" );
+	ok( planeEquals( a.planes[4], p4 ), "Passed!" );
+	ok( planeEquals( a.planes[5], p5 ), "Passed!" );
+});
+
+test( "clone", function() {
+
+	var p0 = new THREE.Plane( unit3, -1 );
+	var p1 = new THREE.Plane( unit3, 1 );
+	var p2 = new THREE.Plane( unit3, 2 );
+	var p3 = new THREE.Plane( unit3, 3 );
+	var p4 = new THREE.Plane( unit3, 4 );
+	var p5 = new THREE.Plane( unit3, 5 );
+
+	var b = new THREE.Frustum( p0, p1, p2, p3, p4, p5 );
+	var a = b.clone();
+	ok( a.planes[0].equals( p0 ), "Passed!" );
+	ok( a.planes[1].equals( p1 ), "Passed!" );
+	ok( a.planes[2].equals( p2 ), "Passed!" );
+	ok( a.planes[3].equals( p3 ), "Passed!" );
+	ok( a.planes[4].equals( p4 ), "Passed!" );
+	ok( a.planes[5].equals( p5 ), "Passed!" );
+
+	// ensure it is a true copy by modifying source
+	b.planes[0] = p1;
+	ok( a.planes[0].equals( p0 ), "Passed!" );
 });