Decompose matrix to split SRT transform.
21 {
22
23
24 using namespace glm;
25 using T = float;
26
27 mat4 LocalMatrix(transform);
28
29
30 if (epsilonEqual(LocalMatrix[3][3], static_cast<float>(0), epsilon<T>()))
31 {
32 return false;
33 }
34
35
36 if (
37 epsilonNotEqual(LocalMatrix[0][3], static_cast<T>(0), epsilon<T>()) ||
38 epsilonNotEqual(LocalMatrix[1][3], static_cast<T>(0), epsilon<T>()) ||
39 epsilonNotEqual(LocalMatrix[2][3], static_cast<T>(0), epsilon<T>()))
40 {
41
42 LocalMatrix[0][3] = LocalMatrix[1][3] = LocalMatrix[2][3] = static_cast<T>(0);
43 LocalMatrix[3][3] = static_cast<T>(1);
44 }
45
46
47 translation = vec3(LocalMatrix[3]);
48 LocalMatrix[3] = vec4(0, 0, 0, LocalMatrix[3].w);
49
50 vec3 Row[3], Pdum3;
51
52
53 for (length_t i = 0; i < 3; ++i)
54 {
55 for (length_t j = 0; j < 3; ++j)
56 {
57 Row[i][j] = LocalMatrix[i][j];
58 }
59 }
60
61
62 scale.x = length(Row[0]);
63 Row[0] = detail::scale(Row[0],
static_cast<T>(1));
64 scale.y = length(Row[1]);
65 Row[1] = detail::scale(Row[1],
static_cast<T>(1));
66 scale.z = length(Row[2]);
67 Row[2] = detail::scale(Row[2],
static_cast<T>(1));
68
69
70
71
72
73#if 0
74 Pdum3 = cross(Row[1], Row[2]);
75 if (dot(Row[0], Pdum3) < 0)
76 {
77 for (length_t i = 0; i < 3; i++)
78 {
79 scale[i] *=
static_cast<T>(-1);
80 Row[i] *=
static_cast<T>(-1);
81 }
82 }
83#endif
84
85 rotation.y = asin(-Row[0][2]);
86 if (cos(rotation.y) != 0.0f)
87 {
88 rotation.x = atan2(Row[1][2], Row[2][2]);
89 rotation.z = atan2(Row[0][1], Row[0][0]);
90 }
91 else
92 {
93 rotation.x = atan2(-Row[2][0], Row[1][1]);
94 rotation.z = 0;
95 }
96
97 return false;
98 }