// By Jason Yu-Tseh Chi // From http://chi3x10.wordpress.com/2008/05/28/calculate-matrix-inversion-in-c/ // Modified to work with valve's matrix_3x4_t #include "mathlib\mathlib.h" // Calculate the cofactor of element (row,col) int GetMatrixMinor(float **src, float **dest, int row, int col, int order) { // Indicate which col and row is being copied to dest int colCount = 0, rowCount = 0; for (int i = 0; i < order; i++) { if (i != row) { colCount = 0; for (int j = 0; j < order; j++) { // When j is not the element if (j != col) { dest[rowCount][colCount] = src[i][j]; colCount++; } } rowCount++; } } return 1; } // Calculate the determinant recursively. double CalcMatrixDeterminant(float **mat, int order) { // Order must be >= 0 // Stop the recursion when matrix is a single element if (order == 1) return mat[0][0]; // The determinant value float det = 0; // Allocate the cofactor matrix float **minor; minor = new float*[order - 1]; for (int i = 0; i