46 const T s =
static_cast<T>(
Sine(a));
53 static const T kOne =
static_cast<T>(1.0);
54 static const T kFour =
static_cast<T>(4.0);
56 const T d0 = mat(0, 0), d1 = mat(1, 1), d2 = mat(2, 2);
57 const T ww = kOne + d0 + d1 + d2;
58 const T xx = kOne + d0 - d1 - d2;
59 const T yy = kOne - d0 + d1 - d2;
60 const T zz = kOne - d0 - d1 + d2;
62 const T max = std::max(ww, std::max(xx, std::max(yy, zz)));
64 const T w4 =
Sqrt(ww * kFour);
66 (mat(2, 1) - mat(1, 2)) / w4,
67 (mat(0, 2) - mat(2, 0)) / w4,
68 (mat(1, 0) - mat(0, 1)) / w4,
73 const T x4 =
Sqrt(xx * kFour);
76 (mat(0, 1) + mat(1, 0)) / x4,
77 (mat(0, 2) + mat(2, 0)) / x4,
78 (mat(2, 1) - mat(1, 2)) / x4));
82 const T y4 =
Sqrt(yy * kFour);
84 (mat(0, 1) + mat(1, 0)) / y4,
86 (mat(1, 2) + mat(2, 1)) / y4,
87 (mat(0, 2) - mat(2, 0)) / y4));
91 const T z4 =
Sqrt(zz * kFour);
93 (mat(0, 2) + mat(2, 0)) / z4,
94 (mat(1, 2) + mat(2, 1)) / z4,
96 (mat(1, 0) - mat(0, 1)) / z4));
109 const T s =
static_cast<T>(1) /
Sqrt(static_cast<T>(1) -
Square(quat_[3]));
110 *axis =
VectorType(quat_[0], quat_[1], quat_[2]) * s;
114 template <
typename T>
120 const T& qx = quat_[0];
121 const T& qy = quat_[1];
122 const T& qz = quat_[2];
123 const T& qw = quat_[3];
125 const T test = qz * qy + qx * qw;
126 if (test > static_cast<T>(0.4999f)) {
129 *yaw = AngleType::FromRadians(static_cast<T>(2. * atan2(qz, qw)));
130 *pitch = AngleType::FromRadians(static_cast<T>(M_PI_2));
131 *roll = AngleType::FromRadians(static_cast<T>(0.));
132 }
else if (test < static_cast<T>(-0.4999f)) {
135 *yaw = AngleType::FromRadians(static_cast<T>(-2. * atan2(qz, qw)));
136 *pitch = AngleType::FromRadians(static_cast<T>(-M_PI_2));
137 *roll = AngleType::FromRadians(static_cast<T>(0.));
140 *yaw = AngleType::FromRadians(static_cast<T>(atan2(
141 2. * qy * qw - 2. * qz * qx, 1. - 2. * qy * qy - 2. * qx * qx)));
142 *pitch = AngleType::FromRadians(static_cast<T>(asin(
144 *roll = AngleType::FromRadians(static_cast<T>(atan2(
145 2. * qz * qw - 2. * qy * qx , 1. - 2. * qz * qz - 2. * qx * qx)));
149 template <
typename T>
152 static const T kTolerance = std::numeric_limits<T>::epsilon() * 100;
157 T real_part = norm_u_norm_v +
Dot(from, to);
159 if (real_part < kTolerance * norm_u_norm_v) {
164 w = (
Abs(from[0]) >
Abs(from[2])) ?
177 template <
typename T>
186 T dot =
Clamp(
Dot(q0, q1), static_cast<T>(-1), static_cast<T>(1));
190 if (dot < static_cast<T>(0.0)) {
196 static const T kMinDotForSlerp =
static_cast<T>(1.0 - 1e-5);
197 if (dot > kMinDotForSlerp) {
198 q_result = q0 + t * (q1 - q0);
205 q_result = q0 *
Cosine(theta) + q2 *
Sine(theta);
218 #define ION_INSTANTIATE_ROTATION_FUNCTIONS(type) \
219 template void ION_API Rotation<type>::SetAxisAndAngle( \
220 const Vector<3, type>& axis, const Angle<type>& angle); \
221 template void ION_API Rotation<type>::GetAxisAndAngle( \
222 Vector<3, type>* axis, Angle<type>* angle) const; \
223 template void ION_API Rotation<type>::GetEulerAngles( \
224 Angle<type>* yaw, Angle<type>* pitch, Angle<type>* roll) const; \
225 template Rotation<type> ION_API Rotation<type>::RotateInto( \
226 const Vector<3, type>& from, const Vector<3, type>& to); \
227 template Rotation<type> ION_API Rotation<type>::Slerp( \
228 const Rotation& r0, const Rotation& r1, type t); \
229 template Rotation<type> ION_API Rotation<type>::FromRotationMatrix( \
230 const Matrix<3, type>& mat)
235 #undef ION_INSTANTIATE_ROTATION_FUNCTIONS
static Rotation Slerp(const Rotation &r0, const Rotation &r1, T t)
Performs spherical linear interpolation between two Rotation instances.
bool Normalize(Vector< Dimension, T > *v)
Normalizes a Vector to unit length.
ION_INSTANTIATE_ROTATION_FUNCTIONS(double)
T Cosine(const ion::math::Angle< T > &angle)
ion::math::Angle specialization of Cosine.
T Dot(const Vector< Dimension, T > &v0, const Vector< Dimension, T > &v1)
Returns the dot (inner) product of two Vectors.
void GetEulerAngles(AngleType *yaw, AngleType *pitch, AngleType *roll) const
Returns the Euler angles which would result in this rotation if done in the order of rotate-Y by yaw...
T Sqrt(const T &val)
Returns the square root of a value.
A simple class to represent angles.
static Rotation FromQuaternion(const QuaternionType &quat)
Convienance function that constructs and returns a Rotation given a quaternion.
#define DCHECK_NE(val1, val2)
The Matrix class defines a square N-dimensional matrix.
const QuaternionType & GetQuaternion() const
Returns the Rotation as a normalized quaternion (4D vector).
void SetQuaternion(const QuaternionType &quaternion)
Sets the Rotation from a quaternion (4D vector), which is first normalized.
const T Clamp(const T &val, const T &min_val, const T &max_val)
Clamps a value to lie between a minimum and maximum, inclusive.
const Vector< Dimension, T > Normalized(const Vector< Dimension, T > &v)
Returns a unit-length version of a Vector.
static Rotation FromRotationMatrix(const Matrix< 3, T > &mat)
Convienance function that constructs and returns a Rotation given a rotation matrix R with $R^ R = I ...
Copyright 2016 Google Inc.
The Rotation class represents a rotation around a 3-dimensional axis.
Angle< T > ArcCosine(T v)
Returns the inverse cosine of the given value.
void GetAxisAndAngle(VectorType *axis, AngleType *angle) const
Returns the right-hand rule axis and angle corresponding to the Rotation.
static Rotation RotateInto(const VectorType &from, const VectorType &to)
Constructs and returns a Rotation that rotates one vector to another along the shortest arc...
T Sine(const ion::math::Angle< T > &angle)
ion::math::Angle specialization of Sine.
const T Square(const T &val)
Squares a value.
void SetAxisAndAngle(const VectorType &axis, const AngleType &angle)
Sets the Rotation to rotate by the given angle around the given axis, following the right-hand rule...
Angle< T > AngleType
Convenience typedefs for Angle and 3D vector of the correct type.
const T Abs(const T &val)
Returns the absolute value of a number in a type-safe way.
Vector< 3, T > Cross(const Vector< 3, T > &v0, const Vector< 3, T > &v1)
Returns the 3-dimensional cross product of 2 Vectors.
T LengthSquared(const Vector< Dimension, T > &v)
Returns the square of the length of a Vector.