Browse Source

add Frustum.containsPoint, containsSphere + first Frustum unit tests.

Ben Houston 12 years ago
parent
commit
9b503c1d98

+ 36 - 1
src/math/Frustum.js

@@ -42,6 +42,8 @@ THREE.Frustum.prototype.setFromMatrix = function ( m ) {
 
 	}
 
+	return this;
+
 };
 
 THREE.Frustum.prototype.contains = function ( object ) {
@@ -65,4 +67,37 @@ THREE.Frustum.prototype.contains = function ( object ) {
 
 };
 
-THREE.Frustum.__v1 = new THREE.Vector3();
+
+THREE.Frustum.prototype.containsSphere = function ( sphere ) {
+	
+	for ( var i = 0; i < 6; i ++ ) {
+
+		var distance = this.planes[ i ].distanceToPoint( sphere.center );
+
+		if( distance <= sphere.radius ) {
+
+			return false;
+
+		}
+
+	}
+
+	return true;
+
+};
+
+THREE.Frustum.prototype.containsPoint = function ( point ) {
+	
+	for ( var i = 0; i < 6; i ++ ) {
+
+		if( this.planes[ i ].distanceToPoint( point ) < 0 ) {
+
+			return false;
+
+		}
+
+	}
+
+	return true;
+
+};

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

@@ -0,0 +1,51 @@
+/**
+ * @author bhouston / http://exocortex.com
+ */
+
+module( "Frustum" );
+
+test( "constructor", function() {
+	var a = new THREE.Frustum();
+
+	ok( a.planes !== undefined, "Passed!" );
+	ok( a.planes.length === 6, "Passed!" );
+});
+
+test( "setFromMatrix/makeOrthographic/containsPoint", function() {
+	var m = new THREE.Matrix4().makeOrthographic( -1, 1, -1, 1, 1, 100 )
+	var a = new THREE.Frustum().setFromMatrix( m );
+
+	ok( ! a.containsPoint( new THREE.Vector3( 0, 0, 0 ) ), "Passed!" );
+	ok( a.containsPoint( new THREE.Vector3( 0, 0, -50 ) ), "Passed!" );
+	ok( a.containsPoint( new THREE.Vector3( 0, 0, -1.001 ) ), "Passed!" );
+	ok( a.containsPoint( new THREE.Vector3( -1, -1, -1.001 ) ), "Passed!" );
+	ok( ! a.containsPoint( new THREE.Vector3( -1.1, -1.1, -1.001 ) ), "Passed!" );
+	ok( a.containsPoint( new THREE.Vector3( 1, 1, -1.001 ) ), "Passed!" );
+	ok( ! a.containsPoint( new THREE.Vector3( 1.1, 1.1, -1.001 ) ), "Passed!" );
+	ok( a.containsPoint( new THREE.Vector3( 0, 0, -100 ) ), "Passed!" );
+	ok( a.containsPoint( new THREE.Vector3( -1, -1, -100 ) ), "Passed!" );
+	ok( ! a.containsPoint( new THREE.Vector3( -1.1, -1.1, -100.1 ) ), "Passed!" );
+	ok( a.containsPoint( new THREE.Vector3( 1, 1, -100 ) ), "Passed!" );
+	ok( ! a.containsPoint( new THREE.Vector3( 1.1, 1.1, -100.1 ) ), "Passed!" );
+	ok( ! a.containsPoint( new THREE.Vector3( 0, 0, -101 ) ), "Passed!" );
+
+});
+
+test( "setFromMatrix/makeFrustum/containsPoint", function() {
+	var m = new THREE.Matrix4().makeFrustum( -1, 1, -1, 1, 1, 100 )
+	var a = new THREE.Frustum().setFromMatrix( m );
+
+	ok( ! a.containsPoint( new THREE.Vector3( 0, 0, 0 ) ), "Passed!" );
+	ok( a.containsPoint( new THREE.Vector3( 0, 0, -50 ) ), "Passed!" );
+	ok( a.containsPoint( new THREE.Vector3( 0, 0, -1.001 ) ), "Passed!" );
+	ok( a.containsPoint( new THREE.Vector3( -1, -1, -1.001 ) ), "Passed!" );
+	ok( ! a.containsPoint( new THREE.Vector3( -1.1, -1.1, -1.001 ) ), "Passed!" );
+	ok( a.containsPoint( new THREE.Vector3( 1, 1, -1.001 ) ), "Passed!" );
+	ok( ! a.containsPoint( new THREE.Vector3( 1.1, 1.1, -1.001 ) ), "Passed!" );
+	ok( a.containsPoint( new THREE.Vector3( 0, 0, -99.999 ) ), "Passed!" );
+	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( 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!" );
+});

+ 2 - 0
test/unit/unittests_sources.html

@@ -26,6 +26,7 @@
   <script src="../../src/math/Matrix4.js"></script>
   <script src="../../src/math/Color.js"></script>
   <script src="../../src/math/Quaternion.js"></script>
+  <script src="../../src/math/Frustum.js"></script>
 
   <!-- add class-based unit tests below -->
 
@@ -42,6 +43,7 @@
   <script src="math/Quaternion.js"></script>
   <script src="math/Matrix3.js"></script>
   <script src="math/Matrix4.js"></script>
+  <script src="math/Frustum.js"></script>
   
 </body>
 </html>

+ 2 - 1
test/unit/unittests_three-math.html

@@ -28,6 +28,7 @@
   <script src="math/Quaternion.js"></script>
   <script src="math/Matrix3.js"></script>
   <script src="math/Matrix4.js"></script>
-  
+  <script src="math/Frustum.js"></script>
+
 </body>
 </html>

+ 1 - 0
test/unit/unittests_three.html

@@ -28,6 +28,7 @@
   <script src="math/Quaternion.js"></script>
   <script src="math/Matrix3.js"></script>
   <script src="math/Matrix4.js"></script>
+  <script src="math/Frustum.js"></script>
 
 </body>
 </html>

+ 1 - 0
test/unit/unittests_three.min.html

@@ -28,6 +28,7 @@
   <script src="math/Quaternion.js"></script>
   <script src="math/Matrix3.js"></script>
   <script src="math/Matrix4.js"></script>
+  <script src="math/Frustum.js"></script>
 
 </body>
 </html>