1/// @ref gtx_matrix_decompose
2
3#include "../gtc/constants.hpp"
4#include "../gtc/epsilon.hpp"
5
6namespace glm{
7namespace detail
8{
9	/// Make a linear combination of two vectors and return the result.
10	// result = (a * ascl) + (b * bscl)
11	template<typename T, qualifier Q>
12	GLM_FUNC_QUALIFIER vec<3, T, Q> combine(
13		vec<3, T, Q> const& a,
14		vec<3, T, Q> const& b,
15		T ascl, T bscl)
16	{
17		return (a * ascl) + (b * bscl);
18	}
19
20	template<typename T, qualifier Q>
21	GLM_FUNC_QUALIFIER vec<3, T, Q> scale(vec<3, T, Q> const& v, T desiredLength)
22	{
23		return v * desiredLength / length(v);
24	}
25}//namespace detail
26
27	// Matrix decompose
28	// http://www.opensource.apple.com/source/WebCore/WebCore-514/platform/graphics/transforms/TransformationMatrix.cpp
29	// Decomposes the mode matrix to translations,rotation scale components
30
31	template<typename T, qualifier Q>
32	GLM_FUNC_QUALIFIER bool decompose(mat<4, 4, T, Q> const& ModelMatrix, vec<3, T, Q> & Scale, qua<T, Q> & Orientation, vec<3, T, Q> & Translation, vec<3, T, Q> & Skew, vec<4, T, Q> & Perspective)
33	{
34		mat<4, 4, T, Q> LocalMatrix(ModelMatrix);
35
36		// Normalize the matrix.
37		if(epsilonEqual(LocalMatrix[3][3], static_cast<T>(0), epsilon<T>()))
38			return false;
39
40		for(length_t i = 0; i < 4; ++i)
41		for(length_t j = 0; j < 4; ++j)
42			LocalMatrix[i][j] /= LocalMatrix[3][3];
43
44		// perspectiveMatrix is used to solve for perspective, but it also provides
45		// an easy way to test for singularity of the upper 3x3 component.
46		mat<4, 4, T, Q> PerspectiveMatrix(LocalMatrix);
47
48		for(length_t i = 0; i < 3; i++)
49			PerspectiveMatrix[i][3] = static_cast<T>(0);
50		PerspectiveMatrix[3][3] = static_cast<T>(1);
51
52		/// TODO: Fixme!
53		if(epsilonEqual(determinant(PerspectiveMatrix), static_cast<T>(0), epsilon<T>()))
54			return false;
55
56		// First, isolate perspective.  This is the messiest.
57		if(
58			epsilonNotEqual(LocalMatrix[0][3], static_cast<T>(0), epsilon<T>()) ||
59			epsilonNotEqual(LocalMatrix[1][3], static_cast<T>(0), epsilon<T>()) ||
60			epsilonNotEqual(LocalMatrix[2][3], static_cast<T>(0), epsilon<T>()))
61		{
62			// rightHandSide is the right hand side of the equation.
63			vec<4, T, Q> RightHandSide;
64			RightHandSide[0] = LocalMatrix[0][3];
65			RightHandSide[1] = LocalMatrix[1][3];
66			RightHandSide[2] = LocalMatrix[2][3];
67			RightHandSide[3] = LocalMatrix[3][3];
68
69			// Solve the equation by inverting PerspectiveMatrix and multiplying
70			// rightHandSide by the inverse.  (This is the easiest way, not
71			// necessarily the best.)
72			mat<4, 4, T, Q> InversePerspectiveMatrix = glm::inverse(PerspectiveMatrix);//   inverse(PerspectiveMatrix, inversePerspectiveMatrix);
73			mat<4, 4, T, Q> TransposedInversePerspectiveMatrix = glm::transpose(InversePerspectiveMatrix);//   transposeMatrix4(inversePerspectiveMatrix, transposedInversePerspectiveMatrix);
74
75			Perspective = TransposedInversePerspectiveMatrix * RightHandSide;
76			//  v4MulPointByMatrix(rightHandSide, transposedInversePerspectiveMatrix, perspectivePoint);
77
78			// Clear the perspective partition
79			LocalMatrix[0][3] = LocalMatrix[1][3] = LocalMatrix[2][3] = static_cast<T>(0);
80			LocalMatrix[3][3] = static_cast<T>(1);
81		}
82		else
83		{
84			// No perspective.
85			Perspective = vec<4, T, Q>(0, 0, 0, 1);
86		}
87
88		// Next take care of translation (easy).
89		Translation = vec<3, T, Q>(LocalMatrix[3]);
90		LocalMatrix[3] = vec<4, T, Q>(0, 0, 0, LocalMatrix[3].w);
91
92		vec<3, T, Q> Row[3], Pdum3;
93
94		// Now get scale and shear.
95		for(length_t i = 0; i < 3; ++i)
96		for(length_t j = 0; j < 3; ++j)
97			Row[i][j] = LocalMatrix[i][j];
98
99		// Compute X scale factor and normalize first row.
100		Scale.x = length(Row[0]);// v3Length(Row[0]);
101
102		Row[0] = detail::scale(Row[0], static_cast<T>(1));
103
104		// Compute XY shear factor and make 2nd row orthogonal to 1st.
105		Skew.z = dot(Row[0], Row[1]);
106		Row[1] = detail::combine(Row[1], Row[0], static_cast<T>(1), -Skew.z);
107
108		// Now, compute Y scale and normalize 2nd row.
109		Scale.y = length(Row[1]);
110		Row[1] = detail::scale(Row[1], static_cast<T>(1));
111		Skew.z /= Scale.y;
112
113		// Compute XZ and YZ shears, orthogonalize 3rd row.
114		Skew.y = glm::dot(Row[0], Row[2]);
115		Row[2] = detail::combine(Row[2], Row[0], static_cast<T>(1), -Skew.y);
116		Skew.x = glm::dot(Row[1], Row[2]);
117		Row[2] = detail::combine(Row[2], Row[1], static_cast<T>(1), -Skew.x);
118
119		// Next, get Z scale and normalize 3rd row.
120		Scale.z = length(Row[2]);
121		Row[2] = detail::scale(Row[2], static_cast<T>(1));
122		Skew.y /= Scale.z;
123		Skew.x /= Scale.z;
124
125		// At this point, the matrix (in rows[]) is orthonormal.
126		// Check for a coordinate system flip.  If the determinant
127		// is -1, then negate the matrix and the scaling factors.
128		Pdum3 = cross(Row[1], Row[2]); // v3Cross(row[1], row[2], Pdum3);
129		if(dot(Row[0], Pdum3) < 0)
130		{
131			for(length_t i = 0; i < 3; i++)
132			{
133				Scale[i] *= static_cast<T>(-1);
134				Row[i] *= static_cast<T>(-1);
135			}
136		}
137
138		// Now, get the rotations out, as described in the gem.
139
140		// FIXME - Add the ability to return either quaternions (which are
141		// easier to recompose with) or Euler angles (rx, ry, rz), which
142		// are easier for authors to deal with. The latter will only be useful
143		// when we fix https://bugs.webkit.org/show_bug.cgi?id=23799, so I
144		// will leave the Euler angle code here for now.
145
146		// ret.rotateY = asin(-Row[0][2]);
147		// if (cos(ret.rotateY) != 0) {
148		//     ret.rotateX = atan2(Row[1][2], Row[2][2]);
149		//     ret.rotateZ = atan2(Row[0][1], Row[0][0]);
150		// } else {
151		//     ret.rotateX = atan2(-Row[2][0], Row[1][1]);
152		//     ret.rotateZ = 0;
153		// }
154
155		int i, j, k = 0;
156		float root, trace = Row[0].x + Row[1].y + Row[2].z;
157		if(trace > static_cast<T>(0))
158		{
159			root = sqrt(trace + static_cast<T>(1.0));
160			Orientation.w = static_cast<T>(0.5) * root;
161			root = static_cast<T>(0.5) / root;
162			Orientation.x = root * (Row[1].z - Row[2].y);
163			Orientation.y = root * (Row[2].x - Row[0].z);
164			Orientation.z = root * (Row[0].y - Row[1].x);
165		} // End if > 0
166		else
167		{
168			static int Next[3] = {1, 2, 0};
169			i = 0;
170			if(Row[1].y > Row[0].x) i = 1;
171			if(Row[2].z > Row[i][i]) i = 2;
172			j = Next[i];
173			k = Next[j];
174
175			root = sqrt(Row[i][i] - Row[j][j] - Row[k][k] + static_cast<T>(1.0));
176
177			Orientation[i] = static_cast<T>(0.5) * root;
178			root = static_cast<T>(0.5) / root;
179			Orientation[j] = root * (Row[i][j] + Row[j][i]);
180			Orientation[k] = root * (Row[i][k] + Row[k][i]);
181			Orientation.w = root * (Row[j][k] - Row[k][j]);
182		} // End if <= 0
183
184		return true;
185	}
186}//namespace glm
187