9 #ifndef CUBBYFLOW_QUATERNION_IMPL_H 10 #define CUBBYFLOW_QUATERNION_IMPL_H 23 Set(newW, newX, newY, newZ);
47 Set(axis0, axis1, axis2);
65 Set(other.
w, other.
x, other.
y, other.
z);
80 assert(list.size() == 4);
82 auto inputElem = list.begin();
92 static const T eps = std::numeric_limits<T>::epsilon();
96 if (axisLengthSquared < eps)
103 T s = std::sin(angle / 2);
105 x = normalizedAxis.
x * s;
106 y = normalizedAxis.
y * s;
107 z = normalizedAxis.
z * s;
108 w = std::cos(angle / 2);
112 template <
typename T>
115 static const T eps = std::numeric_limits<T>::epsilon();
122 if (fromLengthSquared < eps || toLengthSquared < eps)
131 if (axisLengthSquared < eps)
136 Set(from.
Dot(to), axis.
x, axis.
y, axis.
z);
143 template <
typename T>
155 template <
typename T>
158 static const T eps = std::numeric_limits<T>::epsilon();
159 static const T quarter =
static_cast<T
>(0.25);
161 T onePlusTrace = m.
Trace() + 1;
163 if (onePlusTrace > eps)
165 T S = std::sqrt(onePlusTrace) * 2;
167 x = (m(2, 1) - m(1, 2)) / S;
168 y = (m(0, 2) - m(2, 0)) / S;
169 z = (m(1, 0) - m(0, 1)) / S;
171 else if (m(0, 0) > m(1, 1) && m(0, 0) > m(2, 2))
173 T S = std::sqrt(1 + m(0, 0) - m(1, 1) - m(2, 2)) * 2;
174 w = (m(2, 1) - m(1, 2)) / S;
176 y = (m(0, 1) + m(1, 0)) / S;
177 z = (m(0, 2) + m(2, 0)) / S;
179 else if (m(1, 1) > m(2, 2))
181 T S = std::sqrt(1 + m(1, 1) - m(0, 0) - m(2, 2)) * 2;
182 w = (m(0, 2) - m(2, 0)) / S;
183 x = (m(0, 1) + m(1, 0)) / S;
185 z = (m(1, 2) + m(2, 1)) / S;
189 T S = std::sqrt(1 + m(2, 2) - m(0, 0) - m(1, 1)) * 2;
190 w = (m(1, 0) - m(0, 1)) / S;
191 x = (m(0, 2) + m(2, 0)) / S;
192 y = (m(1, 2) + m(2, 1)) / S;
197 template <
typename T>
198 template <
typename U>
201 return Quaternion<U>(
static_cast<U
>(w), static_cast<U>(x),
static_cast<U
>(y), static_cast<U>(z));
204 template <
typename T>
212 template <
typename T>
226 (1 - _2yy - _2zz) * v.
x + (_2xy - _2zw) * v.
y + (_2xz + _2yw) * v.
z,
227 (_2xy + _2zw) * v.
x + (1 - _2zz - _2xx) * v.
y + (_2yz - _2xw) * v.
z,
228 (_2xz - _2yw) * v.
x + (_2yz + _2xw) * v.
y + (1 - _2yy - _2xx) * v.
z);
231 template <
typename T>
235 w * other.
w - x * other.
x - y * other.
y - z * other.
z,
236 w * other.
x + x * other.
w + y * other.
z - z * other.
y,
237 w * other.
y - x * other.
z + y * other.
w + z * other.
x,
238 w * other.
z + x * other.
y - y * other.
x + z * other.
w);
241 template <
typename T>
244 return w * other.
w + x * other.
x + y * other.
y + z * other.
z;
247 template <
typename T>
251 other.
w * w - other.
x * x - other.
y * y - other.
z * z,
252 other.
w * x + other.
x * w + other.
y * z - other.
z * y,
253 other.
w * y - other.
x * z + other.
y * w + other.
z * x,
254 other.
w * z + other.
x * y - other.
y * x + other.
z * w);
257 template <
typename T>
263 template <
typename T>
269 template <
typename T>
275 GetAxisAngle(&axis, ¤tAngle);
277 currentAngle += angleInRadians;
279 Set(axis, currentAngle);
282 template <
typename T>
296 template <
typename T>
302 if (2 * std::acos(w) < PI<T>())
310 template <
typename T>
313 T result = 2 * std::acos(w);
315 if (result < PI<T>())
321 return 2 * PI<T>() - result;
324 template <
typename T>
329 *angle = 2 * std::acos(w);
331 if (*angle > PI<T>())
335 *angle = 2 * PI<T>() - (*angle);
339 template <
typename T>
342 T denom = w * w + x * x + y * y + z * z;
343 return Quaternion(w / denom, -x / denom, -y / denom, -z / denom);
346 template <
typename T>
360 1 - _2yy - _2zz, _2xy - _2zw, _2xz + _2yw,
361 _2xy + _2zw, 1 - _2zz - _2xx, _2yz - _2xw,
362 _2xz - _2yw, _2yz + _2xw, 1 - _2yy - _2xx);
367 template <
typename T>
381 1 - _2yy - _2zz, _2xy - _2zw, _2xz + _2yw, 0,
382 _2xy + _2zw, 1 - _2zz - _2xx, _2yz - _2xw, 0,
383 _2xz - _2yw, _2yz + _2xw, 1 - _2yy - _2xx, 0,
389 template <
typename T>
392 return std::sqrt(w * w + x * x + y * y + z * z);
395 template <
typename T>
402 template <
typename T>
409 template <
typename T>
415 template <
typename T>
421 template <
typename T>
424 return (w == other.
w && x == other.
x && y == other.
y && z == other.
z);
427 template <
typename T>
430 return (w != other.
w || x != other.
x || y != other.
y || z != other.
z);
433 template <
typename T>
439 template <
typename T>
442 static const double threshold = 0.01;
443 static const T eps = std::numeric_limits<T>::epsilon();
445 T cosHalfAngle = dot(a, b);
449 if (1.0 - std::fabs(cosHalfAngle) < threshold)
456 T halfAngle = std::acos(cosHalfAngle);
457 T sinHalfAngle = std::sqrt(1 - cosHalfAngle * cosHalfAngle);
461 if (std::fabs(sinHalfAngle) < eps)
463 weightA =
static_cast<T
>(0.5);
464 weightB =
static_cast<T
>(0.5);
468 weightA = std::sin((1 - t) * halfAngle) / sinHalfAngle;
469 weightB = std::sin(t * halfAngle) / sinHalfAngle;
474 weightA * a.
w + weightB * b.
w,
475 weightA * a.
x + weightB * b.
x,
476 weightA * a.
y + weightB * b.
y,
477 weightA * a.
z + weightB * b.
z);
480 template <
typename T>
486 template <
typename T>
T z
Definition: Quaternion.h:35
3-D vector class.
Definition: Vector3.h:26
T L2Norm() const
Returns L2 norm of this quaternion.
Definition: Quaternion-Impl.h:390
Quaternion RMul(const Quaternion &other) const
Returns other quaternion * this quaternion.
Definition: Quaternion-Impl.h:248
void Set(T s)
Set all x, y, and z components to s.
Definition: Vector3-Impl.h:27
T Trace() const
Returns sum of all diagonal elements.
Definition: Matrix3x3-Impl.h:515
static Quaternion MakeIdentity()
Returns identity matrix.
Definition: Quaternion-Impl.h:434
Quaternion< T > Slerp(const Quaternion< T > &a, const Quaternion< T > &b, T t)
Computes spherical linear interpolation.
Definition: Quaternion-Impl.h:440
T z
Z (or the third) component of the vector.
Definition: Vector3.h:38
T w
Real part.
Definition: Quaternion.h:23
Vector Normalized() const
Returns normalized vector.
Definition: Vector3-Impl.h:308
T Dot(const Vector &v) const
Computes dot product.
Definition: Vector3-Impl.h:136
Matrix4x4< T > Matrix4() const
Converts to the 4x4 rotation matrix.
Definition: Quaternion-Impl.h:368
T LengthSquared() const
Returns the squared length of the vector.
Definition: Vector3-Impl.h:320
Quaternion Normalized() const
Returns normalized quaternion.
Definition: Quaternion-Impl.h:205
Matrix3x3< T > Matrix3() const
Converts to the 3x3 rotation matrix.
Definition: Quaternion-Impl.h:347
T Angle() const
Returns the rotational angle.
Definition: Quaternion-Impl.h:311
void Rotate(T angleInRadians)
Rotate this quaternion with given angle in radians.
Definition: Quaternion-Impl.h:270
Quaternion()
Make an identity quaternion.
Definition: Quaternion-Impl.h:15
void SetColumn(size_t i, const Vector3< T > &col)
Sets i-th column with input vector.
Definition: Matrix3x3-Impl.h:161
bool operator==(const Quaternion &other) const
Returns true if equal.
Definition: Quaternion-Impl.h:422
T x
X (or the first) component of the vector.
Definition: Vector3.h:29
Quaternion Inverse() const
Returns the inverse quaternion.
Definition: Quaternion-Impl.h:340
Definition: pybind11Utils.h:24
3-D matrix class.
Definition: Matrix3x3.h:30
4-D matrix class.
Definition: Matrix4x4.h:29
void Normalize()
Normalizes the quaternion.
Definition: Quaternion-Impl.h:283
Quaternion & operator*=(const Quaternion &other)
Returns this quaternion *= other quaternion.
Definition: Quaternion-Impl.h:403
Quaternion< U > CastTo() const
Returns quaternion with other base type.
Definition: Quaternion-Impl.h:199
void Normalize()
Normalizes this vector.
Definition: Vector3-Impl.h:79
T y
Imaginary part (k).
Definition: Quaternion.h:32
T & operator[](size_t i)
Returns the reference to the i-th element.
Definition: Quaternion-Impl.h:410
Vector3< T > Mul(const Vector3< T > &v) const
Returns this quaternion * vector.
Definition: Quaternion-Impl.h:213
void IMul(const Quaternion &other)
Returns this quaternion *= other quaternion.
Definition: Quaternion-Impl.h:258
std::tuple< Vector< T, 3 >, Vector< T, 3 > > Tangential() const
Returns the tangential vector for this vector.
Definition: Vector3-Impl.h:352
Vector3< T > Axis() const
Returns the rotational axis.
Definition: Quaternion-Impl.h:297
void Set(const Quaternion &other)
Sets the quaternion with other quaternion.
Definition: Quaternion-Impl.h:63
bool operator!=(const Quaternion &other) const
Returns true if not equal.
Definition: Quaternion-Impl.h:428
Vector< T, 3 > operator*(const Quaternion< T > &q, const Vector< T, 3 > &v)
Returns quaternion q * vector v.
Definition: Quaternion-Impl.h:481
T Dot(const Quaternion< T > &other)
Computes the dot product with other quaternion.
Definition: Quaternion-Impl.h:242
void GetAxisAngle(Vector3< T > *axis, T *angle) const
Returns the axis and angle.
Definition: Quaternion-Impl.h:325
T y
Y (or the second) component of the vector.
Definition: Vector3.h:35
Quaternion & operator=(const Quaternion &other)
Assigns other quaternion.
Definition: Quaternion-Impl.h:396
void SetIdentity()
Makes this quaternion identity.
Definition: Quaternion-Impl.h:264
Quaternion class defined as q = w + xi + yj + zk.
Definition: Quaternion.h:20
Vector Cross(const Vector &v) const
Computes cross product.
Definition: Vector3-Impl.h:142
T x
Imaginary part (j).
Definition: Quaternion.h:29