Browse Source

optimize determinant calculation in Matrix4.getInverse()

Ben Houston 12 years ago
parent
commit
bffacabbf3
1 changed files with 22 additions and 22 deletions
  1. 22 22
      src/math/Matrix4.js

+ 22 - 22
src/math/Matrix4.js

@@ -546,28 +546,6 @@ THREE.Matrix4.prototype = {
 
 	getInverse: function ( m, throwOnInvertible ) {
 
-		var det = m.determinant();
-
-		if( det == 0 ) {
-
-			var msg = "Matrix4.getInverse(): can't invert matrix, determinant is 0";
-
-			if( throwOnInvertible || false ) {
-
-				throw new Error( msg ); 
-
-			}
-			else {
-
-				console.warn( msg );
-
-			}
-
-			this.identity();
-
-			return this;
-		}
-
 		// based on http://www.euclideanspace.com/maths/algebra/matrix/functions/inverse/fourD/index.htm
 		var te = this.elements;
 		var me = m.elements;
@@ -594,6 +572,28 @@ THREE.Matrix4.prototype = {
 		te[11] = n13*n22*n41 - n12*n23*n41 - n13*n21*n42 + n11*n23*n42 + n12*n21*n43 - n11*n22*n43;
 		te[15] = n12*n23*n31 - n13*n22*n31 + n13*n21*n32 - n11*n23*n32 - n12*n21*n33 + n11*n22*n33;
 
+		var det = me[ 0 ] * te[ 0 ] + me[ 1 ] * te[ 4 ] + me[ 2 ] * te[ 8 ] + me[ 3 ] * te[ 12 ];
+
+		if( det == 0 ) {
+
+			var msg = "Matrix4.getInverse(): can't invert matrix, determinant is 0";
+
+			if( throwOnInvertible || false ) {
+
+				throw new Error( msg ); 
+
+			}
+			else {
+
+				console.warn( msg );
+
+			}
+
+			this.identity();
+
+			return this;
+		}
+
 		this.multiplyScalar( 1 / det );
 
 		return this;