Created
August 21, 2013 13:32
-
-
Save Madsy/6294494 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Matrix3f ComputeCoeffMatrix(const Vector4f& v1, const Vector4f& v2, const Vector4f& v3) | |
{ | |
Matrix3f m(Vector3f(v1.x, v2.x, v3.x), | |
Vector3f(v1.y, v2.y, v3.y), | |
Vector3f(v1.w, v2.w, v3.w)); | |
const float eps = 0.01f; | |
float det = | |
m[0]*(m[4]*m[8] - m[5]*m[7]) + | |
m[1]*(m[5]*m[6] - m[3]*m[8]) + | |
m[2]*(m[3]*m[7] - m[4]*m[6]); | |
ASSERT(std::abs(det) > eps); | |
// Matrix cofactors | |
float c00 = +(m[4]*m[8] - m[5]*m[7]); //4,5,7,8 | |
float c10 = -(m[3]*m[8] - m[5]*m[6]); //3,5,6,8 | |
float c20 = +(m[3]*m[7] - m[4]*m[6]); //3,4,6,7 | |
float c01 = -(m[1]*m[8] - m[2]*m[7]); //1,2,7,8 | |
float c11 = +(m[0]*m[8] - m[2]*m[6]); //0,2,6,8 | |
float c21 = -(m[0]*m[7] - m[1]*m[6]); //0,1,6,7 | |
float c02 = +(m[1]*m[5] - m[2]*m[4]); //1,2,4,5 | |
float c12 = -(m[0]*m[5] - m[2]*m[3]); //0,2,3,5 | |
float c22 = +(m[0]*m[4] - m[1]*m[3]); //0,1,3,4 | |
//Make an adjoint matrix (reuse m) | |
m[0] = c00; m[1] = c01; m[2] = c02; | |
m[3] = c10; m[4] = c11; m[5] = c12; | |
m[6] = c20; m[7] = c21; m[8] = c22; | |
det = 1.0f/det; | |
m[0] *= det; m[1] *= det; m[2] *= det; | |
m[3] *= det; m[4] *= det; m[5] *= det; | |
m[6] *= det; m[7] *= det; m[8] *= det; | |
return m; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment