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