9 #ifndef TurtleBrains_MatrixQuaternion_hpp
10 #define TurtleBrains_MatrixQuaternion_hpp
12 #include <turtle_brains/math/tb_matrix.hpp>
13 #include <turtle_brains/math/tb_quaternion.hpp>
26 if (matrix(2, 2) < Type(0.0))
28 if (matrix(0, 0) > matrix(1, 1))
30 const Type t = (Type(1.0) + matrix(0, 0) - matrix(1, 1) - matrix(2, 2));
31 TypedQuaternion<Type> q(t, matrix(1, 0) + matrix(0, 1), matrix(0, 2) + matrix(2, 0), matrix(2, 1) - matrix(1, 2));
32 q *= (Type(0.5) / std::sqrt(t));
37 const Type t = (Type(1.0) - matrix(0, 0) + matrix(1, 1) - matrix(2, 2));
38 TypedQuaternion<Type> q(matrix(1, 0) + matrix(0, 1), t, matrix(2, 1) + matrix(1, 2), matrix(0, 2) - matrix(2, 0));
39 q *= (Type(0.5) / std::sqrt(t));
45 if (matrix(0, 0) < -matrix(1, 1))
47 const Type t = (Type(1.0) - matrix(0, 0) - matrix(1, 1) + matrix(2, 2));
48 TypedQuaternion<Type> q(matrix(0, 2) + matrix(2, 0), matrix(2, 1) + matrix(1, 2), t, matrix(1, 0) - matrix(0, 1));
49 q *= (Type(0.5) / std::sqrt(t));
54 const Type t = (Type(1.0) + matrix(0, 0) + matrix(1, 1) + matrix(2, 2));
55 TypedQuaternion<Type> q(matrix(2, 1) - matrix(1, 2), matrix(0, 2) - matrix(2, 0), matrix(1, 0) - matrix(0, 1), t);
56 q *= (Type(0.5) / std::sqrt(t));
67 Type fourXSquaredMinus1 = matrix(0, 0) - matrix(1, 1) - matrix(2, 2);
68 Type fourYSquaredMinus1 = matrix(1, 1) - matrix(0, 0) - matrix(2, 2);
69 Type fourZSquaredMinus1 = matrix(2, 2) - matrix(0, 0) - matrix(1, 1);
70 Type fourWSquaredMinus1 = matrix(0, 0) + matrix(1, 1) + matrix(2, 2);
73 Type fourBiggestSquaredMinus1 = fourWSquaredMinus1;
74 if (fourXSquaredMinus1 > fourBiggestSquaredMinus1)
76 fourBiggestSquaredMinus1 = fourXSquaredMinus1;
79 if (fourYSquaredMinus1 > fourBiggestSquaredMinus1)
81 fourBiggestSquaredMinus1 = fourYSquaredMinus1;
84 if (fourZSquaredMinus1 > fourBiggestSquaredMinus1)
86 fourBiggestSquaredMinus1 = fourZSquaredMinus1;
90 Type biggestVal = std::sqrt(fourBiggestSquaredMinus1 +
static_cast<Type
>(1)) *
static_cast<Type
>(0.5);
91 Type mult =
static_cast<Type
>(0.25) / biggestVal;
96 return TypedQuaternion<Type>((matrix(1, 2) - matrix(2, 1)) * mult, (matrix(2, 0) - matrix(0, 2)) * mult, (matrix(0, 1) - matrix(1, 0)) * mult, biggestVal);
98 return TypedQuaternion<Type>(biggestVal, (matrix(0, 1) + matrix(1, 0)) * mult, (matrix(2, 0) + matrix(0, 2)) * mult, (matrix(1, 2) - matrix(2, 1)) * mult);
100 return TypedQuaternion<Type>((matrix(0, 1) + matrix(1, 0)) * mult, biggestVal, (matrix(1, 2) + matrix(2, 1)) * mult, (matrix(2, 0) - matrix(0, 2)) * mult);
102 return TypedQuaternion<Type>((matrix(2, 0) + matrix(0, 2)) * mult, (matrix(1, 2) + matrix(2, 1)) * mult, biggestVal, (matrix(0, 1) - matrix(1, 0)) * mult);
104 tb_error(
"Something went bad with quat from matrix.");
105 return TypedQuaternion<Type>(Type(0.0), Type(0.0), Type(0.0), Type(1.0));
153 const Type& qx(quaternion.x);
154 const Type& qy(quaternion.y);
155 const Type& qz(quaternion.z);
156 const Type& qw(quaternion.w);
158 const Type qxSq(quaternion.x * quaternion.x);
159 const Type qySq(quaternion.y * quaternion.y);
160 const Type qzSq(quaternion.z * quaternion.z);
164 return TypedMatrix3<Type>(
165 Type(1.0) - Type(2.0) * qySq - Type(2.0) * qzSq, Type(2.0) * qx * qy - Type(2.0) * qz * qw, Type(2.0) * qx * qz + Type(2.0) * qy * qw,
166 Type(2.0) * qx * qy + Type(2.0) * qz * qw, Type(1.0) - Type(2.0) * qxSq - Type(2.0) * qzSq, Type(2.0) * qy * qz - Type(2.0) * qx * qw,
167 Type(2.0) * qx * qz - Type(2.0) * qy * qw, Type(2.0) * qy * qz + Type(2.0) * qx * qw, Type(1.0) - Type(2.0) * qxSq - Type(2.0) * qySq);
180 const Type& qx(quaternion.x);
181 const Type& qy(quaternion.y);
182 const Type& qz(quaternion.z);
183 const Type& qw(quaternion.w);
185 const Type qxSq(quaternion.x * quaternion.x);
186 const Type qySq(quaternion.y * quaternion.y);
187 const Type qzSq(quaternion.z * quaternion.z);
191 return TypedMatrix4<Type>(
192 Type(1.0) - Type(2.0) * qySq - Type(2.0) * qzSq, Type(2.0) * qx * qy + Type(2.0) * qz * qw, Type(2.0) * qx * qz - Type(2.0) * qy * qw, Type(0.0),
193 Type(2.0) * qx * qy - Type(2.0) * qz * qw, Type(1.0) - Type(2.0) * qxSq - Type(2.0) * qzSq, Type(2.0) * qy * qz + Type(2.0) * qx * qw, Type(0.0),
194 Type(2.0) * qx * qz + Type(2.0) * qy * qw, Type(2.0) * qy * qz - Type(2.0) * qx * qw, Type(1.0) - Type(2.0) * qxSq - Type(2.0) * qySq, Type(0.0),
195 translation.x, translation.y, translation.z, Type(1.0));
Definition: tb_matrix.hpp:156
Definition: tb_matrix.hpp:596
Definition: tb_quaternion.hpp:41
Definition: tb_vector.hpp:472
#define tb_error(message,...)
Definition: tb_error.hpp:23