1///////////////////////////////////////////////////////////////////////////////////
2/// OpenGL Mathematics (glm.g-truc.net)
3///
4/// Copyright (c) 2005 - 2014 G-Truc Creation (www.g-truc.net)
5/// Permission is hereby granted, free of charge, to any person obtaining a copy
6/// of this software and associated documentation files (the "Software"), to deal
7/// in the Software without restriction, including without limitation the rights
8/// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9/// copies of the Software, and to permit persons to whom the Software is
10/// furnished to do so, subject to the following conditions:
11///
12/// The above copyright notice and this permission notice shall be included in
13/// all copies or substantial portions of the Software.
14///
15/// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16/// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17/// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18/// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19/// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20/// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21/// THE SOFTWARE.
22///
23/// @ref gtc_matrix_inverse
24/// @file glm/gtc/matrix_inverse.inl
25/// @date 2005-12-21 / 2011-06-15
26/// @author Christophe Riccio
27///////////////////////////////////////////////////////////////////////////////////
28
29#include "../mat2x2.hpp"
30#include "../mat3x3.hpp"
31#include "../mat4x4.hpp"
32
33namespace glm
34{
35	template <typename T, precision P>
36	GLM_FUNC_QUALIFIER detail::tmat3x3<T, P> affineInverse
37	(
38		detail::tmat3x3<T, P> const & m
39	)
40	{
41		detail::tmat3x3<T, P> Result(m);
42		Result[2] = detail::tvec3<T, P>(0, 0, 1);
43		Result = transpose(Result);
44		detail::tvec3<T, P> Translation = Result * detail::tvec3<T, P>(-detail::tvec2<T, P>(m[2]), m[2][2]);
45		Result[2] = Translation;
46		return Result;
47	}
48
49	template <typename T, precision P>
50	GLM_FUNC_QUALIFIER detail::tmat4x4<T, P> affineInverse
51	(
52		detail::tmat4x4<T, P> const & m
53	)
54	{
55		detail::tmat4x4<T, P> Result(m);
56		Result[3] = detail::tvec4<T, P>(0, 0, 0, 1);
57		Result = transpose(Result);
58		detail::tvec4<T, P> Translation = Result * detail::tvec4<T, P>(-detail::tvec3<T, P>(m[3]), m[3][3]);
59		Result[3] = Translation;
60		return Result;
61	}
62
63	template <typename T, precision P>
64	GLM_FUNC_QUALIFIER detail::tmat2x2<T, P> inverseTranspose
65	(
66		detail::tmat2x2<T, P> const & m
67	)
68	{
69		T Determinant = m[0][0] * m[1][1] - m[1][0] * m[0][1];
70
71		detail::tmat2x2<T, P> Inverse(
72			+ m[1][1] / Determinant,
73			- m[0][1] / Determinant,
74			- m[1][0] / Determinant,
75			+ m[0][0] / Determinant);
76
77		return Inverse;
78	}
79
80	template <typename T, precision P>
81	GLM_FUNC_QUALIFIER detail::tmat3x3<T, P> inverseTranspose
82	(
83		detail::tmat3x3<T, P> const & m
84	)
85	{
86		T Determinant =
87			+ m[0][0] * (m[1][1] * m[2][2] - m[1][2] * m[2][1])
88			- m[0][1] * (m[1][0] * m[2][2] - m[1][2] * m[2][0])
89			+ m[0][2] * (m[1][0] * m[2][1] - m[1][1] * m[2][0]);
90
91		detail::tmat3x3<T, P> Inverse;
92		Inverse[0][0] = + (m[1][1] * m[2][2] - m[2][1] * m[1][2]);
93		Inverse[0][1] = - (m[1][0] * m[2][2] - m[2][0] * m[1][2]);
94		Inverse[0][2] = + (m[1][0] * m[2][1] - m[2][0] * m[1][1]);
95		Inverse[1][0] = - (m[0][1] * m[2][2] - m[2][1] * m[0][2]);
96		Inverse[1][1] = + (m[0][0] * m[2][2] - m[2][0] * m[0][2]);
97		Inverse[1][2] = - (m[0][0] * m[2][1] - m[2][0] * m[0][1]);
98		Inverse[2][0] = + (m[0][1] * m[1][2] - m[1][1] * m[0][2]);
99		Inverse[2][1] = - (m[0][0] * m[1][2] - m[1][0] * m[0][2]);
100		Inverse[2][2] = + (m[0][0] * m[1][1] - m[1][0] * m[0][1]);
101		Inverse /= Determinant;
102
103		return Inverse;
104	}
105
106	template <typename T, precision P>
107	GLM_FUNC_QUALIFIER detail::tmat4x4<T, P> inverseTranspose
108	(
109		detail::tmat4x4<T, P> const & m
110	)
111	{
112		T SubFactor00 = m[2][2] * m[3][3] - m[3][2] * m[2][3];
113		T SubFactor01 = m[2][1] * m[3][3] - m[3][1] * m[2][3];
114		T SubFactor02 = m[2][1] * m[3][2] - m[3][1] * m[2][2];
115		T SubFactor03 = m[2][0] * m[3][3] - m[3][0] * m[2][3];
116		T SubFactor04 = m[2][0] * m[3][2] - m[3][0] * m[2][2];
117		T SubFactor05 = m[2][0] * m[3][1] - m[3][0] * m[2][1];
118		T SubFactor06 = m[1][2] * m[3][3] - m[3][2] * m[1][3];
119		T SubFactor07 = m[1][1] * m[3][3] - m[3][1] * m[1][3];
120		T SubFactor08 = m[1][1] * m[3][2] - m[3][1] * m[1][2];
121		T SubFactor09 = m[1][0] * m[3][3] - m[3][0] * m[1][3];
122		T SubFactor10 = m[1][0] * m[3][2] - m[3][0] * m[1][2];
123		T SubFactor11 = m[1][1] * m[3][3] - m[3][1] * m[1][3];
124		T SubFactor12 = m[1][0] * m[3][1] - m[3][0] * m[1][1];
125		T SubFactor13 = m[1][2] * m[2][3] - m[2][2] * m[1][3];
126		T SubFactor14 = m[1][1] * m[2][3] - m[2][1] * m[1][3];
127		T SubFactor15 = m[1][1] * m[2][2] - m[2][1] * m[1][2];
128		T SubFactor16 = m[1][0] * m[2][3] - m[2][0] * m[1][3];
129		T SubFactor17 = m[1][0] * m[2][2] - m[2][0] * m[1][2];
130		T SubFactor18 = m[1][0] * m[2][1] - m[2][0] * m[1][1];
131
132		detail::tmat4x4<T, P> Inverse;
133		Inverse[0][0] = + (m[1][1] * SubFactor00 - m[1][2] * SubFactor01 + m[1][3] * SubFactor02);
134		Inverse[0][1] = - (m[1][0] * SubFactor00 - m[1][2] * SubFactor03 + m[1][3] * SubFactor04);
135		Inverse[0][2] = + (m[1][0] * SubFactor01 - m[1][1] * SubFactor03 + m[1][3] * SubFactor05);
136		Inverse[0][3] = - (m[1][0] * SubFactor02 - m[1][1] * SubFactor04 + m[1][2] * SubFactor05);
137
138		Inverse[1][0] = - (m[0][1] * SubFactor00 - m[0][2] * SubFactor01 + m[0][3] * SubFactor02);
139		Inverse[1][1] = + (m[0][0] * SubFactor00 - m[0][2] * SubFactor03 + m[0][3] * SubFactor04);
140		Inverse[1][2] = - (m[0][0] * SubFactor01 - m[0][1] * SubFactor03 + m[0][3] * SubFactor05);
141		Inverse[1][3] = + (m[0][0] * SubFactor02 - m[0][1] * SubFactor04 + m[0][2] * SubFactor05);
142
143		Inverse[2][0] = + (m[0][1] * SubFactor06 - m[0][2] * SubFactor07 + m[0][3] * SubFactor08);
144		Inverse[2][1] = - (m[0][0] * SubFactor06 - m[0][2] * SubFactor09 + m[0][3] * SubFactor10);
145		Inverse[2][2] = + (m[0][0] * SubFactor11 - m[0][1] * SubFactor09 + m[0][3] * SubFactor12);
146		Inverse[2][3] = - (m[0][0] * SubFactor08 - m[0][1] * SubFactor10 + m[0][2] * SubFactor12);
147
148		Inverse[3][0] = - (m[0][1] * SubFactor13 - m[0][2] * SubFactor14 + m[0][3] * SubFactor15);
149		Inverse[3][1] = + (m[0][0] * SubFactor13 - m[0][2] * SubFactor16 + m[0][3] * SubFactor17);
150		Inverse[3][2] = - (m[0][0] * SubFactor14 - m[0][1] * SubFactor16 + m[0][3] * SubFactor18);
151		Inverse[3][3] = + (m[0][0] * SubFactor15 - m[0][1] * SubFactor17 + m[0][2] * SubFactor18);
152
153		T Determinant =
154			+ m[0][0] * Inverse[0][0]
155			+ m[0][1] * Inverse[0][1]
156			+ m[0][2] * Inverse[0][2]
157			+ m[0][3] * Inverse[0][3];
158
159		Inverse /= Determinant;
160
161		return Inverse;
162	}
163}//namespace glm
164