Browse Source

Protect Triangle against the closure circular reference bug

Tristan VALCKE 8 years ago
parent
commit
39b35d32b0
1 changed files with 57 additions and 53 deletions
  1. 57 53
      src/math/Triangle.js

+ 57 - 53
src/math/Triangle.js

@@ -15,90 +15,94 @@ function Triangle( a, b, c ) {
 
 
 }
 }
 
 
-Triangle.normal = function () {
+Object.assign( Triangle, {
 
 
-	var v0 = new Vector3();
+	normal: function () {
 
 
-	return function normal( a, b, c, optionalTarget ) {
+		var v0 = new Vector3();
 
 
-		var result = optionalTarget || new Vector3();
+		return function normal( a, b, c, optionalTarget ) {
 
 
-		result.subVectors( c, b );
-		v0.subVectors( a, b );
-		result.cross( v0 );
+			var result = optionalTarget || new Vector3();
 
 
-		var resultLengthSq = result.lengthSq();
-		if ( resultLengthSq > 0 ) {
+			result.subVectors( c, b );
+			v0.subVectors( a, b );
+			result.cross( v0 );
 
 
-			return result.multiplyScalar( 1 / Math.sqrt( resultLengthSq ) );
+			var resultLengthSq = result.lengthSq();
+			if ( resultLengthSq > 0 ) {
 
 
-		}
+				return result.multiplyScalar( 1 / Math.sqrt( resultLengthSq ) );
 
 
-		return result.set( 0, 0, 0 );
+			}
 
 
-	};
+			return result.set( 0, 0, 0 );
 
 
-}();
+		};
 
 
-// static/instance method to calculate barycentric coordinates
-// based on: http://www.blackpawn.com/texts/pointinpoly/default.html
-Triangle.barycoordFromPoint = function () {
+	}(),
 
 
-	var v0 = new Vector3();
-	var v1 = new Vector3();
-	var v2 = new Vector3();
+	// static/instance method to calculate barycentric coordinates
+	// based on: http://www.blackpawn.com/texts/pointinpoly/default.html
+	barycoordFromPoint: function () {
 
 
-	return function barycoordFromPoint( point, a, b, c, optionalTarget ) {
+		var v0 = new Vector3();
+		var v1 = new Vector3();
+		var v2 = new Vector3();
 
 
-		v0.subVectors( c, a );
-		v1.subVectors( b, a );
-		v2.subVectors( point, a );
+		return function barycoordFromPoint( point, a, b, c, optionalTarget ) {
 
 
-		var dot00 = v0.dot( v0 );
-		var dot01 = v0.dot( v1 );
-		var dot02 = v0.dot( v2 );
-		var dot11 = v1.dot( v1 );
-		var dot12 = v1.dot( v2 );
+			v0.subVectors( c, a );
+			v1.subVectors( b, a );
+			v2.subVectors( point, a );
 
 
-		var denom = ( dot00 * dot11 - dot01 * dot01 );
+			var dot00 = v0.dot( v0 );
+			var dot01 = v0.dot( v1 );
+			var dot02 = v0.dot( v2 );
+			var dot11 = v1.dot( v1 );
+			var dot12 = v1.dot( v2 );
 
 
-		var result = optionalTarget || new Vector3();
+			var denom = ( dot00 * dot11 - dot01 * dot01 );
 
 
-		// collinear or singular triangle
-		if ( denom === 0 ) {
+			var result = optionalTarget || new Vector3();
+
+			// collinear or singular triangle
+			if ( denom === 0 ) {
 
 
-			// arbitrary location outside of triangle?
-			// not sure if this is the best idea, maybe should be returning undefined
-			return result.set( - 2, - 1, - 1 );
+				// arbitrary location outside of triangle?
+				// not sure if this is the best idea, maybe should be returning undefined
+				return result.set( - 2, - 1, - 1 );
+
+			}
 
 
-		}
+			var invDenom = 1 / denom;
+			var u = ( dot11 * dot02 - dot01 * dot12 ) * invDenom;
+			var v = ( dot00 * dot12 - dot01 * dot02 ) * invDenom;
 
 
-		var invDenom = 1 / denom;
-		var u = ( dot11 * dot02 - dot01 * dot12 ) * invDenom;
-		var v = ( dot00 * dot12 - dot01 * dot02 ) * invDenom;
+			// barycentric coordinates must always sum to 1
+			return result.set( 1 - u - v, v, u );
 
 
-		// barycentric coordinates must always sum to 1
-		return result.set( 1 - u - v, v, u );
+		};
 
 
-	};
+	}(),
 
 
-}();
+	containsPoint: function () {
 
 
-Triangle.containsPoint = function () {
+		var v1 = new Vector3();
 
 
-	var v1 = new Vector3();
+		return function containsPoint( point, a, b, c ) {
 
 
-	return function containsPoint( point, a, b, c ) {
+			var result = Triangle.barycoordFromPoint( point, a, b, c, v1 );
 
 
-		var result = Triangle.barycoordFromPoint( point, a, b, c, v1 );
+			return ( result.x >= 0 ) && ( result.y >= 0 ) && ( ( result.x + result.y ) <= 1 );
 
 
-		return ( result.x >= 0 ) && ( result.y >= 0 ) && ( ( result.x + result.y ) <= 1 );
+		};
 
 
-	};
+	}()
 
 
-}();
+} );
 
 
-Triangle.prototype = {
+Object.assign( Triangle.prototype, {
 
 
 	constructor: Triangle,
 	constructor: Triangle,
 
 
@@ -256,7 +260,7 @@ Triangle.prototype = {
 
 
 	}
 	}
 
 
-};
+} );
 
 
 
 
 export { Triangle };
 export { Triangle };