Explorar el Código

adopt @mrdoob's suggested Box2,Box3 constructor (update tests), make Triangle.normal also available as a static (easy consistent normal generation.)

Ben Houston hace 12 años
padre
commit
a48922b97c
Se han modificado 5 ficheros con 52 adiciones y 76 borrados
  1. 2 15
      src/math/Box2.js
  2. 2 21
      src/math/Box3.js
  3. 22 14
      src/math/Triangle.js
  4. 13 13
      test/core/Box2.js
  5. 13 13
      test/core/Box3.js

+ 2 - 15
src/math/Box2.js

@@ -4,21 +4,8 @@
 
 THREE.Box2 = function ( min, max ) {
 
-	if ( min === undefined && max === undefined ) {
-
-		this.min = new THREE.Vector2();
-		this.max = new THREE.Vector2();
-		this.makeEmpty();
-
-	} else {
-		this.min = min.clone();
-		if( max === undefined ) {
-			this.max = new THREE.Vector2().copy( this.min ); // This is done on purpose so you can make a box using a single point and then expand it.
-		}
-		else {
-			this.max = max.clone();
-		}
-	}
+	this.min = min !== undefined ? min.clone() : new THREE.Vector2( Infinity, Infinity );
+	this.max = max !== undefined ? max.clone() : new THREE.Vector2( -Infinity, -Infinity );
 
 };
 

+ 2 - 21
src/math/Box3.js

@@ -4,27 +4,8 @@
 
 THREE.Box3 = function ( min, max ) {
 
-	if ( min === undefined && max === undefined ) {
-
-		this.min = new THREE.Vector3();
-		this.max = new THREE.Vector3();
-		this.makeEmpty();
-
-	} else {
-
-		this.min = min.clone();
-
-		if( max === undefined ) {
-
-			this.max = new THREE.Vector3().copy( this.min ); // This is done on purpose so you can make a box using a single point and then expand it.
-
-		} else {
-
-			this.max = max.clone();
-
-		}
-
-	}
+	this.min = min !== undefined ? min.clone() : new THREE.Vector3( Infinity, Infinity, Infinity );
+	this.max = max !== undefined ? max.clone() : new THREE.Vector3( -Infinity, -Infinity, -Infinity );
 
 };
 

+ 22 - 14
src/math/Triangle.js

@@ -19,6 +19,25 @@ THREE.Triangle = function ( a, b, c ) {
 
 };
 
+THREE.Triangle.normal = function( a, b, c, optionalTarget ) {
+
+	var result = optionalTarget || new THREE.Vector3();
+
+	result.sub( c, b );
+	THREE.Triangle.__v0.sub( a, b );
+	result.crossSelf( THREE.Triangle.__v0 );
+
+	var resultLengthSq = result.lengthSq();
+	if( resultLengthSq > 0 ) {
+
+		return result.multiplyScalar( 1 / Math.sqrt( resultLengthSq ) );
+
+	}
+
+	return result.set( 0, 0, 0 );
+
+};
+
 // static/instance method to calculate barycoordinates
 THREE.Triangle.barycoordFromPoint = function ( point, a, b, c, optionalTarget ) {
 
@@ -49,6 +68,7 @@ THREE.Triangle.barycoordFromPoint = function ( point, a, b, c, optionalTarget )
 
 	// barycoordinates must always sum to 1
 	return result.set( 1 - u - v, v, u );
+
 };
 
 THREE.Triangle.containsPoint = function ( point, a, b, c ) {
@@ -57,6 +77,7 @@ THREE.Triangle.containsPoint = function ( point, a, b, c ) {
 	var result = THREE.Triangle.barycoordFromPoint( point, a, b, c, THREE.Triangle.__v3 );
 
 	return ( result.x >= 0 ) && ( result.y >= 0 ) && ( ( result.x + result.y ) <= 1 );
+
 };
 
 THREE.Triangle.prototype = {
@@ -111,20 +132,7 @@ THREE.Triangle.prototype = {
 
 	normal: function ( optionalTarget ) {
 
-		var result = optionalTarget || new THREE.Vector3();
-
-		result.sub( this.c, this.b );
-		THREE.Triangle.__v0.sub( this.a, this.b );
-		result.crossSelf( THREE.Triangle.__v0 );
-
-		var resultLengthSq = result.lengthSq();
-		if( resultLengthSq > 0 ) {
-
-			return result.multiplyScalar( 1 / Math.sqrt( resultLengthSq ) );
-
-		}
-
-		return result.set( 0, 0, 0 );
+		return THREE.Triangle.normal( this.a, this.b, this.c, optionalTarget );
 
 	},
 

+ 13 - 13
test/core/Box2.js

@@ -9,7 +9,7 @@ test( "constructor", function() {
 	ok( a.min.equals( posInf2 ), "Passed!" );
 	ok( a.max.equals( negInf2 ), "Passed!" );
 
-	a = new THREE.Box2( zero2 );
+	a = new THREE.Box2( zero2, zero2 );
 	ok( a.min.equals( zero2 ), "Passed!" );
 	ok( a.max.equals( zero2 ), "Passed!" );
 
@@ -52,7 +52,7 @@ test( "empty/makeEmpty", function() {
 });
 
 test( "center", function() {
-	var a = new THREE.Box2( zero2 );
+	var a = new THREE.Box2( zero2, zero2 );
 
 	ok( a.center().equals( zero2 ), "Passed!" );
 
@@ -62,7 +62,7 @@ test( "center", function() {
 });
 
 test( "size", function() {
-	var a = new THREE.Box2( zero2 );
+	var a = new THREE.Box2( zero2, zero2 );
 
 	ok( a.size().equals( zero2 ), "Passed!" );
 
@@ -71,7 +71,7 @@ test( "size", function() {
 });
 
 test( "expandByPoint", function() {
-	var a = new THREE.Box2( zero2 );
+	var a = new THREE.Box2( zero2, zero2 );
 
 	a.expandByPoint( zero2 );
 	ok( a.size().equals( zero2 ), "Passed!" );
@@ -85,7 +85,7 @@ test( "expandByPoint", function() {
 });
 
 test( "expandByVector", function() {
-	var a = new THREE.Box2( zero2 );
+	var a = new THREE.Box2( zero2, zero2 );
 
 	a.expandByVector( zero2 );
 	ok( a.size().equals( zero2 ), "Passed!" );
@@ -96,7 +96,7 @@ test( "expandByVector", function() {
 });
 
 test( "expandByScalar", function() {
-	var a = new THREE.Box2( zero2 );
+	var a = new THREE.Box2( zero2, zero2 );
 
 	a.expandByScalar( 0 );
 	ok( a.size().equals( zero2 ), "Passed!" );
@@ -107,7 +107,7 @@ test( "expandByScalar", function() {
 });
 
 test( "containsPoint", function() {
-	var a = new THREE.Box2( zero2 );
+	var a = new THREE.Box2( zero2, zero2 );
 
 	ok( a.containsPoint( zero2 ), "Passed!" );
 	ok( ! a.containsPoint( one2 ), "Passed!" );
@@ -119,7 +119,7 @@ test( "containsPoint", function() {
 });
 
 test( "containsBox", function() {
-	var a = new THREE.Box2( zero2 );
+	var a = new THREE.Box2( zero2, zero2 );
 	var b = new THREE.Box2( zero2, one2 );
 	var c = new THREE.Box2( one2.clone().negate(), one2 );
 
@@ -190,7 +190,7 @@ test( "distanceToPoint", function() {
 });
 
 test( "isIntersectionBox", function() {
-	var a = new THREE.Box2( zero2 );
+	var a = new THREE.Box2( zero2, zero2 );
 	var b = new THREE.Box2( zero2, one2 );
 	var c = new THREE.Box2( one2.clone().negate(), one2 );
 
@@ -209,7 +209,7 @@ test( "isIntersectionBox", function() {
 });
 
 test( "intersect", function() {
-	var a = new THREE.Box2( zero2 );
+	var a = new THREE.Box2( zero2, zero2 );
 	var b = new THREE.Box2( zero2, one2 );
 	var c = new THREE.Box2( one2.clone().negate(), one2 );
 
@@ -222,7 +222,7 @@ test( "intersect", function() {
 });
 
 test( "union", function() {
-	var a = new THREE.Box2( zero2 );
+	var a = new THREE.Box2( zero2, zero2 );
 	var b = new THREE.Box2( zero2, one2 );
 	var c = new THREE.Box2( one2.clone().negate(), one2 );
 
@@ -233,7 +233,7 @@ test( "union", function() {
 });
 
 test( "translate", function() {
-	var a = new THREE.Box2( zero2 );
+	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 );
@@ -245,7 +245,7 @@ test( "translate", function() {
 });
 
 test( "scale", function() {
-	var a = new THREE.Box2( zero2 );
+	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 );

+ 13 - 13
test/core/Box3.js

@@ -9,7 +9,7 @@ test( "constructor", function() {
 	ok( a.min.equals( posInf3 ), "Passed!" );
 	ok( a.max.equals( negInf3 ), "Passed!" );
 
-	a = new THREE.Box3( zero3 );
+	a = new THREE.Box3( zero3, zero3 );
 	ok( a.min.equals( zero3 ), "Passed!" );
 	ok( a.max.equals( zero3 ), "Passed!" );
 
@@ -52,7 +52,7 @@ test( "empty/makeEmpty", function() {
 });
 
 test( "center", function() {
-	var a = new THREE.Box3( zero3 );
+	var a = new THREE.Box3( zero3, zero3 );
 
 	ok( a.center().equals( zero3 ), "Passed!" );
 
@@ -62,7 +62,7 @@ test( "center", function() {
 });
 
 test( "size", function() {
-	var a = new THREE.Box3( zero3 );
+	var a = new THREE.Box3( zero3, zero3 );
 
 	ok( a.size().equals( zero3 ), "Passed!" );
 
@@ -71,7 +71,7 @@ test( "size", function() {
 });
 
 test( "expandByPoint", function() {
-	var a = new THREE.Box3( zero3 );
+	var a = new THREE.Box3( zero3, zero3 );
 
 	a.expandByPoint( zero3 );
 	ok( a.size().equals( zero3 ), "Passed!" );
@@ -85,7 +85,7 @@ test( "expandByPoint", function() {
 });
 
 test( "expandByVector", function() {
-	var a = new THREE.Box3( zero3 );
+	var a = new THREE.Box3( zero3, zero3 );
 
 	a.expandByVector( zero3 );
 	ok( a.size().equals( zero3 ), "Passed!" );
@@ -96,7 +96,7 @@ test( "expandByVector", function() {
 });
 
 test( "expandByScalar", function() {
-	var a = new THREE.Box3( zero3 );
+	var a = new THREE.Box3( zero3, zero3 );
 
 	a.expandByScalar( 0 );
 	ok( a.size().equals( zero3 ), "Passed!" );
@@ -107,7 +107,7 @@ test( "expandByScalar", function() {
 });
 
 test( "containsPoint", function() {
-	var a = new THREE.Box3( zero3 );
+	var a = new THREE.Box3( zero3, zero3 );
 
 	ok( a.containsPoint( zero3 ), "Passed!" );
 	ok( ! a.containsPoint( one3 ), "Passed!" );
@@ -119,7 +119,7 @@ test( "containsPoint", function() {
 });
 
 test( "containsBox", function() {
-	var a = new THREE.Box3( zero3 );
+	var a = new THREE.Box3( zero3, zero3 );
 	var b = new THREE.Box3( zero3, one3 );
 	var c = new THREE.Box3( one3.clone().negate(), one3 );
 
@@ -190,7 +190,7 @@ test( "distanceToPoint", function() {
 });
 
 test( "isIntersectionBox", function() {
-	var a = new THREE.Box3( zero3 );
+	var a = new THREE.Box3( zero3, zero3 );
 	var b = new THREE.Box3( zero3, one3 );
 	var c = new THREE.Box3( one3.clone().negate(), one3 );
 
@@ -209,7 +209,7 @@ test( "isIntersectionBox", function() {
 });
 
 test( "intersect", function() {
-	var a = new THREE.Box3( zero3 );
+	var a = new THREE.Box3( zero3, zero3 );
 	var b = new THREE.Box3( zero3, one3 );
 	var c = new THREE.Box3( one3.clone().negate(), one3 );
 
@@ -222,7 +222,7 @@ test( "intersect", function() {
 });
 
 test( "union", function() {
-	var a = new THREE.Box3( zero3 );
+	var a = new THREE.Box3( zero3, zero3 );
 	var b = new THREE.Box3( zero3, one3 );
 	var c = new THREE.Box3( one3.clone().negate(), one3 );
 
@@ -233,7 +233,7 @@ test( "union", function() {
 });
 
 test( "translate", function() {
-	var a = new THREE.Box3( zero3 );
+	var a = new THREE.Box3( zero3, zero3 );
 	var b = new THREE.Box3( zero3, one3 );
 	var c = new THREE.Box3( one3.clone().negate(), one3 );
 	var d = new THREE.Box3( one3.clone().negate(), zero3 );
@@ -245,7 +245,7 @@ test( "translate", function() {
 });
 
 test( "scale", function() {
-	var a = new THREE.Box3( zero3 );
+	var a = new THREE.Box3( zero3, zero3 );
 	var b = new THREE.Box3( zero3, one3 );
 	var c = new THREE.Box3( one3.clone().negate(), one3 );
 	var d = new THREE.Box3( one3.clone().negate(), zero3 );