11 #ifndef CUBBYFLOW_QUATERNION_IMPL_HPP 12 #define CUBBYFLOW_QUATERNION_IMPL_HPP 25 Set(newW, newX, newY, newZ);
50 Set(axis0, axis1, axis2);
88 Set(other.
w, other.
x, other.
y, other.
z);
100 template <
typename T>
103 assert(list.size() == 4);
105 auto inputElem = list.begin();
112 template <
typename T>
115 static const T eps = std::numeric_limits<T>::epsilon();
119 if (axisLengthSquared < eps)
126 T s = std::sin(angle / 2);
128 x = normalizedAxis.x * s;
129 y = normalizedAxis.y * s;
130 z = normalizedAxis.z * s;
131 w = std::cos(angle / 2);
135 template <
typename T>
138 static const T eps = std::numeric_limits<T>::epsilon();
145 if (fromLengthSquared < eps || toLengthSquared < eps)
155 if (axisLengthSquared < eps)
160 Set(from.
Dot(to), axis.x, axis.y, axis.z);
167 template <
typename T>
181 template <
typename T>
184 static const T eps = std::numeric_limits<T>::epsilon();
185 static const T quarter =
static_cast<T
>(0.25);
187 T onePlusTrace = m.
Trace() + 1;
189 if (onePlusTrace > eps)
191 T S = std::sqrt(onePlusTrace) * 2;
193 x = (m(2, 1) - m(1, 2)) / S;
194 y = (m(0, 2) - m(2, 0)) / S;
195 z = (m(1, 0) - m(0, 1)) / S;
197 else if (m(0, 0) > m(1, 1) && m(0, 0) > m(2, 2))
199 T S = std::sqrt(1 + m(0, 0) - m(1, 1) - m(2, 2)) * 2;
200 w = (m(2, 1) - m(1, 2)) / S;
202 y = (m(0, 1) + m(1, 0)) / S;
203 z = (m(0, 2) + m(2, 0)) / S;
205 else if (m(1, 1) > m(2, 2))
207 T S = std::sqrt(1 + m(1, 1) - m(0, 0) - m(2, 2)) * 2;
208 w = (m(0, 2) - m(2, 0)) / S;
209 x = (m(0, 1) + m(1, 0)) / S;
211 z = (m(1, 2) + m(2, 1)) / S;
215 T S = std::sqrt(1 + m(2, 2) - m(0, 0) - m(1, 1)) * 2;
216 w = (m(1, 0) - m(0, 1)) / S;
217 x = (m(0, 2) + m(2, 0)) / S;
218 y = (m(1, 2) + m(2, 1)) / S;
223 template <
typename T>
224 template <
typename U>
228 static_cast<U
>(y), static_cast<U>(z) };
231 template <
typename T>
239 template <
typename T>
253 (1 - _2yy - _2zz) * v.x + (_2xy - _2zw) * v.y + (_2xz + _2yw) * v.z,
254 (_2xy + _2zw) * v.x + (1 - _2zz - _2xx) * v.y + (_2yz - _2xw) * v.z,
255 (_2xz - _2yw) * v.x + (_2yz + _2xw) * v.y + (1 - _2yy - _2xx) * v.z
259 template <
typename T>
262 return Quaternion{ w * other.
w - x * other.
x - y * other.
y - z * other.
z,
263 w * other.
x + x * other.
w + y * other.
z - z * other.
y,
264 w * other.
y - x * other.
z + y * other.
w + z * other.
x,
265 w * other.
z + x * other.
y - y * other.
x + z * other.
w };
268 template <
typename T>
271 return w * other.
w + x * other.
x + y * other.
y + z * other.
z;
274 template <
typename T>
277 return Quaternion{ other.
w * w - other.
x * x - other.
y * y - other.
z * z,
278 other.
w * x + other.
x * w + other.
y * z - other.
z * y,
279 other.
w * y - other.
x * z + other.
y * w + other.
z * x,
280 other.
w * z + other.
x * y - other.
y * x + other.
z * w };
283 template <
typename T>
289 template <
typename T>
295 template <
typename T>
301 GetAxisAngle(&axis, ¤tAngle);
303 currentAngle += angleInRadians;
305 Set(axis, currentAngle);
308 template <
typename T>
322 template <
typename T>
328 if (2 * std::acos(w) < PI<T>())
336 template <
typename T>
339 T result = 2 * std::acos(w);
341 if (result < PI<T>())
347 return 2 * PI<T>() - result;
350 template <
typename T>
355 *angle = 2 * std::acos(w);
357 if (*angle > PI<T>())
361 *angle = 2 * PI<T>() - (*angle);
365 template <
typename T>
368 const T denom = w * w + x * x + y * y + z * z;
369 return Quaternion{ w / denom, -x / denom, -y / denom, -z / denom };
372 template <
typename T>
385 Matrix3x3<T> m{ 1 - _2yy - _2zz, _2xy - _2zw, _2xz + _2yw,
386 _2xy + _2zw, 1 - _2zz - _2xx, _2yz - _2xw,
387 _2xz - _2yw, _2yz + _2xw, 1 - _2yy - _2xx };
392 template <
typename T>
425 template <
typename T>
428 return std::sqrt(w * w + x * x + y * y + z * z);
431 template <
typename T>
438 template <
typename T>
441 assert(i >= 0 && i < 4);
461 template <
typename T>
464 assert(i >= 0 && i < 4);
484 template <
typename T>
487 return (w == other.
w && x == other.
x && y == other.
y && z == other.
z);
490 template <
typename T>
493 return (w != other.
w || x != other.
x || y != other.
y || z != other.
z);
496 template <
typename T>
502 template <
typename T>
505 static const double threshold = 0.01;
506 static const T eps = std::numeric_limits<T>::epsilon();
508 T cosHalfAngle = a.
Dot(b);
512 if (1.0 - std::fabs(cosHalfAngle) < threshold)
519 T halfAngle = std::acos(cosHalfAngle);
520 T sinHalfAngle = std::sqrt(1 - cosHalfAngle * cosHalfAngle);
524 if (std::fabs(sinHalfAngle) < eps)
526 weightA =
static_cast<T
>(0.5);
527 weightB =
static_cast<T
>(0.5);
531 weightA = std::sin((1 - t) * halfAngle) / sinHalfAngle;
532 weightB = std::sin(t * halfAngle) / sinHalfAngle;
537 weightA * a.
x + weightB * b.
x,
538 weightA * a.
y + weightB * b.
y,
539 weightA * a.
z + weightB * b.
z };
542 template <
typename T>
548 template <
typename T>
T z
Definition: Quaternion.hpp:168
T L2Norm() const
Returns L2 norm of this quaternion.
Definition: Quaternion-Impl.hpp:426
Quaternion RMul(const Quaternion &other) const
Returns other quaternion * this quaternion.
Definition: Quaternion-Impl.hpp:275
ValueType LengthSquared() const
Definition: MatrixExpression-Impl.hpp:286
static Quaternion MakeIdentity()
Returns identity matrix.
Definition: Quaternion-Impl.hpp:497
Quaternion< T > Slerp(const Quaternion< T > &a, const Quaternion< T > &b, T t)
Computes spherical linear interpolation.
Definition: Quaternion-Impl.hpp:503
T w
Real part.
Definition: Quaternion.hpp:159
Matrix4x4< T > Matrix4() const
Converts to the 4x4 rotation matrix.
Definition: Quaternion-Impl.hpp:393
Quaternion Normalized() const
Returns normalized quaternion.
Definition: Quaternion-Impl.hpp:232
Matrix3x3< T > Matrix3() const
Converts to the 3x3 rotation matrix.
Definition: Quaternion-Impl.hpp:373
T Angle() const
Returns the rotational angle.
Definition: Quaternion-Impl.hpp:337
void Rotate(T angleInRadians)
Rotate this quaternion with given angle in radians.
Definition: Quaternion-Impl.hpp:296
Quaternion()
Make an identity quaternion.
Definition: Quaternion-Impl.hpp:17
void SetColumn(size_t j, const MatrixExpression< T, R, C, E > &col)
Sets j-th column with input vector.
Definition: MatrixDenseBase-Impl.hpp:74
Definition: Matrix.hpp:27
std::enable_if_t<(IsMatrixSizeDynamic< Rows, Cols >)||Cols==1) &&(IsMatrixSizeDynamic< R, C >)||C==1), U > Dot(const MatrixExpression< T, R, C, E > &expression) const
Definition: MatrixExpression-Impl.hpp:391
bool operator==(const Quaternion &other) const
Returns true if equal.
Definition: Quaternion-Impl.hpp:485
Quaternion Inverse() const
Returns the inverse quaternion.
Definition: Quaternion-Impl.hpp:366
Definition: pybind11Utils.hpp:20
void Normalize()
Normalizes the quaternion.
Definition: Quaternion-Impl.hpp:309
Quaternion & operator*=(const Quaternion &other)
Returns this quaternion *= other quaternion.
Definition: Quaternion-Impl.hpp:432
Quaternion< U > CastTo() const
Returns quaternion with other base type.
Definition: Quaternion-Impl.hpp:225
ValueType Trace() const
Definition: MatrixExpression-Impl.hpp:183
T y
Imaginary part (k).
Definition: Quaternion.hpp:165
T & operator[](size_t i)
Returns the reference to the i-th element.
Definition: Quaternion-Impl.hpp:439
std::enable_if_t<(IsMatrixSizeDynamic< Rows, Cols >)||(Rows==2 &&Cols==1)) &&(IsMatrixSizeDynamic< R, C >)||(R==2 &&C==1)), U > Cross(const MatrixExpression< T, R, C, E > &expression) const
Definition: MatrixExpression-Impl.hpp:412
Vector3< T > Mul(const Vector3< T > &v) const
Returns this quaternion * vector.
Definition: Quaternion-Impl.hpp:240
void IMul(const Quaternion &other)
Returns this quaternion *= other quaternion.
Definition: Quaternion-Impl.hpp:284
void Normalize()
Definition: MatrixDenseBase-Impl.hpp:86
T Dot(const Quaternion< T > &other) const
Computes the dot product with other quaternion.
Definition: Quaternion-Impl.hpp:269
Vector3< T > Axis() const
Returns the rotational axis.
Definition: Quaternion-Impl.hpp:323
void Set(const Quaternion &other)
Sets the quaternion with other quaternion.
Definition: Quaternion-Impl.hpp:86
bool operator!=(const Quaternion &other) const
Returns true if not equal.
Definition: Quaternion-Impl.hpp:491
Vector< T, 3 > operator*(const Quaternion< T > &q, const Vector< T, 3 > &v)
Returns quaternion q * vector v.
Definition: Quaternion-Impl.hpp:543
std::enable_if_t<(IsMatrixSizeDynamic< Rows, Cols >)||(Rows==3 &&Cols==1)), std::tuple< Matrix< U, 3, 1 >, Matrix< U, 3, 1 > > > Tangentials() const
Returns the tangential vectors for this vector.
Definition: MatrixExpression-Impl.hpp:492
MatrixScalarElemWiseBinaryOp< T, Rows, Cols, const Matrix< T, Rows, Cols > &, std::divides< T > > Normalized() const
Definition: MatrixExpression-Impl.hpp:315
void GetAxisAngle(Vector3< T > *axis, T *angle) const
Returns the axis and angle.
Definition: Quaternion-Impl.hpp:351
Quaternion & operator=(const Quaternion &other)
Copy assignment operator.
Definition: Quaternion-Impl.hpp:72
void SetIdentity()
Makes this quaternion identity.
Definition: Quaternion-Impl.hpp:290
Quaternion class defined as q = w + xi + yj + zk.
Definition: Quaternion.hpp:22
T x
Imaginary part (j).
Definition: Quaternion.hpp:162