Beispiel #1
0
void mat3_invert(Real in[3][3], Real out[3][3])
{
  Real det(mat3_determinant(in));

	if (det == 0.0)
    return;

  det = 1.0 / det;

  out[0][0] =   (in[1][1] * in[2][2] -
                       in[1][2] * in[2][1]) * det;

  out[1][0] = - (in[1][0] * in[2][2] -
                       in[1][2] * in[2][0]) * det;

  out[2][0] =   (in[1][0] * in[2][1] -
                       in[1][1] * in[2][0]) * det;

  out[0][1] = - (in[0][1] * in[2][2] -
                       in[0][2] * in[2][1]) * det;

  out[1][1] =   (in[0][0] * in[2][2] -
                       in[0][2] * in[2][0]) * det;

  out[2][1] = - (in[0][0] * in[2][1] -
                       in[0][1] * in[2][0]) * det;

  out[0][2] =   (in[0][1] * in[1][2] -
                       in[0][2] * in[1][1]) * det;

  out[1][2] = - (in[0][0] * in[1][2] -
                       in[0][2] * in[1][0]) * det;

  out[2][2] =   (in[0][0] * in[1][1] -
                       in[0][1] * in[1][0]) * det;

}
/*
How do you find the adjoint of a 4x4 matrix?

Find the cofactor (determinant of the signed minor) of each entry, keeping in mind the sign array
(starting with + in the upper left corner):
[+ - + -]
[- + - +]
[+ - + -]
[- + - +]

//
//	11  12  13  14
//  21  22  23  24
//  31  32  33  34
//  41  42  43  44
//

// c = adjoint(a)
*/
void mat4_adjoint(mat4 &c, const mat4 &a)
{
	mat3 m3;
	mat4 m4;

	// Create matrix of minors
	// Apply +- pattern to create matrix of cofactors
	// 11
	m3.m11 = a.m22; m3.m12 = a.m23; m3.m13 = a.m24;
	m3.m21 = a.m32; m3.m22 = a.m33; m3.m23 = a.m34;
	m3.m31 = a.m42; m3.m32 = a.m43; m3.m33 = a.m44;
	m4.m11 = mat3_determinant(m3);

	// 12
	m3.m11 = a.m21; m3.m12 = a.m23; m3.m13 = a.m24;
	m3.m21 = a.m31; m3.m22 = a.m33; m3.m23 = a.m34;
	m3.m31 = a.m41; m3.m32 = a.m43; m3.m33 = a.m44;
	m4.m12 = -mat3_determinant(m3);

	// 13
	m3.m11 = a.m21; m3.m12 = a.m22; m3.m13 = a.m24;
	m3.m21 = a.m31; m3.m22 = a.m32; m3.m23 = a.m34;
	m3.m31 = a.m41; m3.m32 = a.m42; m3.m33 = a.m44;
	m4.m13 = mat3_determinant(m3);

	// 14
	m3.m11 = a.m21; m3.m12 = a.m22; m3.m13 = a.m23;
	m3.m21 = a.m31; m3.m22 = a.m32; m3.m23 = a.m33;
	m3.m31 = a.m41; m3.m32 = a.m42; m3.m33 = a.m43;
	m4.m14 = -mat3_determinant(m3);

	// 21
	m3.m11 = a.m12; m3.m12 = a.m13; m3.m13 = a.m14;
	m3.m21 = a.m32; m3.m22 = a.m33; m3.m23 = a.m34;
	m3.m31 = a.m42; m3.m32 = a.m43; m3.m33 = a.m44;
	m4.m21 = -mat3_determinant(m3);

	// 22
	m3.m11 = a.m11; m3.m12 = a.m13; m3.m13 = a.m14;
	m3.m21 = a.m31; m3.m22 = a.m33; m3.m23 = a.m34;
	m3.m31 = a.m41; m3.m32 = a.m43; m3.m33 = a.m44;
	m4.m22 = mat3_determinant(m3);

	// 23
	m3.m11 = a.m11; m3.m12 = a.m12; m3.m13 = a.m14;
	m3.m21 = a.m31; m3.m22 = a.m32; m3.m23 = a.m34;
	m3.m31 = a.m41; m3.m32 = a.m42; m3.m33 = a.m44;
	m4.m23 = -mat3_determinant(m3);

	// 24
	m3.m11 = a.m11; m3.m12 = a.m12; m3.m13 = a.m13;
	m3.m21 = a.m31; m3.m22 = a.m32; m3.m23 = a.m33;
	m3.m31 = a.m41; m3.m32 = a.m42; m3.m33 = a.m43;
	m4.m24 = mat3_determinant(m3);


	// 31
	m3.m11 = a.m12; m3.m12 = a.m13; m3.m13 = a.m14;
	m3.m21 = a.m22; m3.m22 = a.m23; m3.m23 = a.m24;
	m3.m31 = a.m42; m3.m32 = a.m43; m3.m33 = a.m44;
	m4.m31 = mat3_determinant(m3);

	// 32
	m3.m11 = a.m11; m3.m12 = a.m13; m3.m13 = a.m14;
	m3.m21 = a.m21; m3.m22 = a.m23; m3.m23 = a.m24;
	m3.m31 = a.m41; m3.m32 = a.m43; m3.m33 = a.m44;
	m4.m32 = -mat3_determinant(m3);

	// 33
	m3.m11 = a.m11; m3.m12 = a.m12; m3.m13 = a.m14;
	m3.m21 = a.m21; m3.m22 = a.m22; m3.m23 = a.m24;
	m3.m31 = a.m41; m3.m32 = a.m42; m3.m33 = a.m44;
	m4.m33 = mat3_determinant(m3);

	// 34
	m3.m11 = a.m11; m3.m12 = a.m12; m3.m13 = a.m13;
	m3.m21 = a.m21; m3.m22 = a.m22; m3.m23 = a.m23;
	m3.m31 = a.m41; m3.m32 = a.m42; m3.m33 = a.m43;
	m4.m34 = -mat3_determinant(m3);


	// 41
	m3.m11 = a.m12; m3.m12 = a.m13; m3.m13 = a.m14;
	m3.m21 = a.m22; m3.m22 = a.m23; m3.m23 = a.m24;
	m3.m31 = a.m32; m3.m32 = a.m33; m3.m33 = a.m34;
	m4.m41 = -mat3_determinant(m3);

	// 42
	m3.m11 = a.m11; m3.m12 = a.m13; m3.m13 = a.m14;
	m3.m21 = a.m21; m3.m22 = a.m23; m3.m23 = a.m24;
	m3.m31 = a.m31; m3.m32 = a.m33; m3.m33 = a.m34;
	m4.m42 = mat3_determinant(m3);

	// 43
	m3.m11 = a.m11; m3.m12 = a.m12; m3.m13 = a.m14;
	m3.m21 = a.m21; m3.m22 = a.m22; m3.m23 = a.m24;
	m3.m31 = a.m31; m3.m32 = a.m32; m3.m33 = a.m34;
	m4.m43 = -mat3_determinant(m3);

	// 44
	m3.m11 = a.m11; m3.m12 = a.m12; m3.m13 = a.m13;
	m3.m21 = a.m21; m3.m22 = a.m22; m3.m23 = a.m23;
	m3.m31 = a.m31; m3.m32 = a.m32; m3.m33 = a.m33;
	m4.m44 = mat3_determinant(m3);


	// create adjoint by transposing
	mat4_transpose(c, m4);
}