1/// @ref gtc_matrix_inverse
2/// @file glm/gtc/matrix_inverse.inl
3
4namespace glm
5{
6	template <typename T, precision P>
7	GLM_FUNC_QUALIFIER tmat3x3<T, P> affineInverse(tmat3x3<T, P> const & m)
8	{
9		tmat2x2<T, P> const Inv(inverse(tmat2x2<T, P>(m)));
10
11		return tmat3x3<T, P>(
12			tvec3<T, P>(Inv[0], static_cast<T>(0)),
13			tvec3<T, P>(Inv[1], static_cast<T>(0)),
14			tvec3<T, P>(-Inv * tvec2<T, P>(m[2]), static_cast<T>(1)));
15	}
16
17	template <typename T, precision P>
18	GLM_FUNC_QUALIFIER tmat4x4<T, P> affineInverse(tmat4x4<T, P> const & m)
19	{
20		tmat3x3<T, P> const Inv(inverse(tmat3x3<T, P>(m)));
21
22		return tmat4x4<T, P>(
23			tvec4<T, P>(Inv[0], static_cast<T>(0)),
24			tvec4<T, P>(Inv[1], static_cast<T>(0)),
25			tvec4<T, P>(Inv[2], static_cast<T>(0)),
26			tvec4<T, P>(-Inv * tvec3<T, P>(m[3]), static_cast<T>(1)));
27	}
28
29	template <typename T, precision P>
30	GLM_FUNC_QUALIFIER tmat2x2<T, P> inverseTranspose(tmat2x2<T, P> const & m)
31	{
32		T Determinant = m[0][0] * m[1][1] - m[1][0] * m[0][1];
33
34		tmat2x2<T, P> Inverse(
35			+ m[1][1] / Determinant,
36			- m[0][1] / Determinant,
37			- m[1][0] / Determinant,
38			+ m[0][0] / Determinant);
39
40		return Inverse;
41	}
42
43	template <typename T, precision P>
44	GLM_FUNC_QUALIFIER tmat3x3<T, P> inverseTranspose(tmat3x3<T, P> const & m)
45	{
46		T Determinant =
47			+ m[0][0] * (m[1][1] * m[2][2] - m[1][2] * m[2][1])
48			- m[0][1] * (m[1][0] * m[2][2] - m[1][2] * m[2][0])
49			+ m[0][2] * (m[1][0] * m[2][1] - m[1][1] * m[2][0]);
50
51		tmat3x3<T, P> Inverse(uninitialize);
52		Inverse[0][0] = + (m[1][1] * m[2][2] - m[2][1] * m[1][2]);
53		Inverse[0][1] = - (m[1][0] * m[2][2] - m[2][0] * m[1][2]);
54		Inverse[0][2] = + (m[1][0] * m[2][1] - m[2][0] * m[1][1]);
55		Inverse[1][0] = - (m[0][1] * m[2][2] - m[2][1] * m[0][2]);
56		Inverse[1][1] = + (m[0][0] * m[2][2] - m[2][0] * m[0][2]);
57		Inverse[1][2] = - (m[0][0] * m[2][1] - m[2][0] * m[0][1]);
58		Inverse[2][0] = + (m[0][1] * m[1][2] - m[1][1] * m[0][2]);
59		Inverse[2][1] = - (m[0][0] * m[1][2] - m[1][0] * m[0][2]);
60		Inverse[2][2] = + (m[0][0] * m[1][1] - m[1][0] * m[0][1]);
61		Inverse /= Determinant;
62
63		return Inverse;
64	}
65
66	template <typename T, precision P>
67	GLM_FUNC_QUALIFIER tmat4x4<T, P> inverseTranspose(tmat4x4<T, P> const & m)
68	{
69		T SubFactor00 = m[2][2] * m[3][3] - m[3][2] * m[2][3];
70		T SubFactor01 = m[2][1] * m[3][3] - m[3][1] * m[2][3];
71		T SubFactor02 = m[2][1] * m[3][2] - m[3][1] * m[2][2];
72		T SubFactor03 = m[2][0] * m[3][3] - m[3][0] * m[2][3];
73		T SubFactor04 = m[2][0] * m[3][2] - m[3][0] * m[2][2];
74		T SubFactor05 = m[2][0] * m[3][1] - m[3][0] * m[2][1];
75		T SubFactor06 = m[1][2] * m[3][3] - m[3][2] * m[1][3];
76		T SubFactor07 = m[1][1] * m[3][3] - m[3][1] * m[1][3];
77		T SubFactor08 = m[1][1] * m[3][2] - m[3][1] * m[1][2];
78		T SubFactor09 = m[1][0] * m[3][3] - m[3][0] * m[1][3];
79		T SubFactor10 = m[1][0] * m[3][2] - m[3][0] * m[1][2];
80		T SubFactor11 = m[1][1] * m[3][3] - m[3][1] * m[1][3];
81		T SubFactor12 = m[1][0] * m[3][1] - m[3][0] * m[1][1];
82		T SubFactor13 = m[1][2] * m[2][3] - m[2][2] * m[1][3];
83		T SubFactor14 = m[1][1] * m[2][3] - m[2][1] * m[1][3];
84		T SubFactor15 = m[1][1] * m[2][2] - m[2][1] * m[1][2];
85		T SubFactor16 = m[1][0] * m[2][3] - m[2][0] * m[1][3];
86		T SubFactor17 = m[1][0] * m[2][2] - m[2][0] * m[1][2];
87		T SubFactor18 = m[1][0] * m[2][1] - m[2][0] * m[1][1];
88
89		tmat4x4<T, P> Inverse(uninitialize);
90		Inverse[0][0] = + (m[1][1] * SubFactor00 - m[1][2] * SubFactor01 + m[1][3] * SubFactor02);
91		Inverse[0][1] = - (m[1][0] * SubFactor00 - m[1][2] * SubFactor03 + m[1][3] * SubFactor04);
92		Inverse[0][2] = + (m[1][0] * SubFactor01 - m[1][1] * SubFactor03 + m[1][3] * SubFactor05);
93		Inverse[0][3] = - (m[1][0] * SubFactor02 - m[1][1] * SubFactor04 + m[1][2] * SubFactor05);
94
95		Inverse[1][0] = - (m[0][1] * SubFactor00 - m[0][2] * SubFactor01 + m[0][3] * SubFactor02);
96		Inverse[1][1] = + (m[0][0] * SubFactor00 - m[0][2] * SubFactor03 + m[0][3] * SubFactor04);
97		Inverse[1][2] = - (m[0][0] * SubFactor01 - m[0][1] * SubFactor03 + m[0][3] * SubFactor05);
98		Inverse[1][3] = + (m[0][0] * SubFactor02 - m[0][1] * SubFactor04 + m[0][2] * SubFactor05);
99
100		Inverse[2][0] = + (m[0][1] * SubFactor06 - m[0][2] * SubFactor07 + m[0][3] * SubFactor08);
101		Inverse[2][1] = - (m[0][0] * SubFactor06 - m[0][2] * SubFactor09 + m[0][3] * SubFactor10);
102		Inverse[2][2] = + (m[0][0] * SubFactor11 - m[0][1] * SubFactor09 + m[0][3] * SubFactor12);
103		Inverse[2][3] = - (m[0][0] * SubFactor08 - m[0][1] * SubFactor10 + m[0][2] * SubFactor12);
104
105		Inverse[3][0] = - (m[0][1] * SubFactor13 - m[0][2] * SubFactor14 + m[0][3] * SubFactor15);
106		Inverse[3][1] = + (m[0][0] * SubFactor13 - m[0][2] * SubFactor16 + m[0][3] * SubFactor17);
107		Inverse[3][2] = - (m[0][0] * SubFactor14 - m[0][1] * SubFactor16 + m[0][3] * SubFactor18);
108		Inverse[3][3] = + (m[0][0] * SubFactor15 - m[0][1] * SubFactor17 + m[0][2] * SubFactor18);
109
110		T Determinant =
111			+ m[0][0] * Inverse[0][0]
112			+ m[0][1] * Inverse[0][1]
113			+ m[0][2] * Inverse[0][2]
114			+ m[0][3] * Inverse[0][3];
115
116		Inverse /= Determinant;
117
118		return Inverse;
119	}
120}//namespace glm
121