@@ -1468,19 +1468,35 @@ RMAPI int Vector4Equals(Vector4 p, Vector4 q)
1468
1468
RMAPI float MatrixDeterminant (Matrix mat )
1469
1469
{
1470
1470
float result = 0.0f ;
1471
-
1471
+ /*
1472
1472
// Cache the matrix values (speed optimization)
1473
1473
float a00 = mat.m0, a01 = mat.m1, a02 = mat.m2, a03 = mat.m3;
1474
1474
float a10 = mat.m4, a11 = mat.m5, a12 = mat.m6, a13 = mat.m7;
1475
1475
float a20 = mat.m8, a21 = mat.m9, a22 = mat.m10, a23 = mat.m11;
1476
1476
float a30 = mat.m12, a31 = mat.m13, a32 = mat.m14, a33 = mat.m15;
1477
1477
1478
+ // NOTE: It takes 72 multiplication to calculate 4x4 matrix determinant
1478
1479
result = a30*a21*a12*a03 - a20*a31*a12*a03 - a30*a11*a22*a03 + a10*a31*a22*a03 +
1479
1480
a20*a11*a32*a03 - a10*a21*a32*a03 - a30*a21*a02*a13 + a20*a31*a02*a13 +
1480
1481
a30*a01*a22*a13 - a00*a31*a22*a13 - a20*a01*a32*a13 + a00*a21*a32*a13 +
1481
1482
a30*a11*a02*a23 - a10*a31*a02*a23 - a30*a01*a12*a23 + a00*a31*a12*a23 +
1482
1483
a10*a01*a32*a23 - a00*a11*a32*a23 - a20*a11*a02*a33 + a10*a21*a02*a33 +
1483
1484
a20*a01*a12*a33 - a00*a21*a12*a33 - a10*a01*a22*a33 + a00*a11*a22*a33;
1485
+ */
1486
+ // Using Laplace expansion (https://en.wikipedia.org/wiki/Laplace_expansion),
1487
+ // previous operation can be simplified to 40 multiplications, decreasing matrix
1488
+ // size from 4x4 to 2x2 using minors
1489
+
1490
+ // Cache the matrix values (speed optimization)
1491
+ float m0 = mat .m0 , m1 = mat .m1 , m2 = mat .m2 , m3 = mat .m3 ;
1492
+ float m4 = mat .m4 , m5 = mat .m5 , m6 = mat .m6 , m7 = mat .m7 ;
1493
+ float m8 = mat .m8 , m9 = mat .m9 , m10 = mat .m10 , m11 = mat .m11 ;
1494
+ float m12 = mat .m12 , m13 = mat .m13 , m14 = mat .m14 , m15 = mat .m15 ;
1495
+
1496
+ result = (m0 * ((m5 * (m10 * m15 - m11 * m14 ) - m9 * (m6 * m15 - m7 * m14 ) + m13 * (m6 * m11 - m7 * m10 ))) -
1497
+ m4 * ((m1 * (m10 * m15 - m11 * m14 ) - m9 * (m2 * m15 - m3 * m14 ) + m13 * (m2 * m11 - m3 * m10 ))) +
1498
+ m8 * ((m1 * (m6 * m15 - m7 * m14 ) - m5 * (m2 * m15 - m3 * m14 ) + m13 * (m2 * m7 - m3 * m6 ))) -
1499
+ m12 * ((m1 * (m6 * m11 - m7 * m10 ) - m5 * (m2 * m11 - m3 * m10 ) + m9 * (m2 * m7 - m3 * m6 ))))
1484
1500
1485
1501
return result ;
1486
1502
}
0 commit comments