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 vec<3, T, P> combine( 11 vec<3, T, P> const & a, 12 vec<3, 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 vec<3, T, P> scale(vec<3, 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(mat<4, 4, T, P> const & ModelMatrix, vec<3, T, P> & Scale, tquat<T, P> & Orientation, vec<3, T, P> & Translation, vec<3, T, P> & Skew, vec<4, T, P> & Perspective) 31 { 32 mat<4, 4, 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 mat<4, 4, 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 vec<4, 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 mat<4, 4, T, P> InversePerspectiveMatrix = glm::inverse(PerspectiveMatrix);// inverse(PerspectiveMatrix, inversePerspectiveMatrix); 68 mat<4, 4, 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 = vec<4, T, P>(0, 0, 0, 1); 81 } 82 83 // Next take care of translation (easy). 84 Translation = vec<3, T, P>(LocalMatrix[3]); 85 LocalMatrix[3] = vec<4, T, P>(0, 0, 0, LocalMatrix[3].w); 86 87 vec<3, 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 int i, j, k = 0; 151 float root, trace = Row[0].x + Row[1].y + Row[2].z; 152 if(trace > static_cast<T>(0)) 153 { 154 root = sqrt(trace + static_cast<T>(1.0)); 155 Orientation.w = static_cast<T>(0.5) * root; 156 root = static_cast<T>(0.5) / root; 157 Orientation.x = root * (Row[1].z - Row[2].y); 158 Orientation.y = root * (Row[2].x - Row[0].z); 159 Orientation.z = root * (Row[0].y - Row[1].x); 160 } // End if > 0 161 else 162 { 163 static int Next[3] = {1, 2, 0}; 164 i = 0; 165 if(Row[1].y > Row[0].x) i = 1; 166 if(Row[2].z > Row[i][i]) i = 2; 167 j = Next[i]; 168 k = Next[j]; 169 170 root = sqrt(Row[i][i] - Row[j][j] - Row[k][k] + static_cast<T>(1.0)); 171 172 Orientation[i] = static_cast<T>(0.5) * root; 173 root = static_cast<T>(0.5) / root; 174 Orientation[j] = root * (Row[i][j] + Row[j][i]); 175 Orientation[k] = root * (Row[i][k] + Row[k][i]); 176 Orientation.w = root * (Row[j][k] - Row[k][j]); 177 } // End if <= 0 178 179 return true; 180 } 181}//namespace glm 182