SPH
Quat.h
Go to the documentation of this file.
1 #pragma once
2 
7 
8 #include "math/AffineMatrix.h"
9 
11 
16 class Quat {
17 private:
18  Vector v;
19 
20 public:
21  Quat() = default;
22 
26  Quat(const Vector& axis, const Float angle) {
27  SPH_ASSERT(getSqrLength(axis) > 0._f);
28  Vector normAxis;
29  Float length;
30  tieToTuple(normAxis, length) = getNormalizedWithLength(axis);
31 
32  const Float s = sin(0.5_f * angle);
33  const Float c = cos(0.5_f * angle);
34 
35  v = normAxis * s;
36  v[3] = c;
37  }
38 
42  explicit Quat(const AffineMatrix& m) {
43  SPH_ASSERT(m.translation() == Vector(0._f));
45  const Float w = 0.5_f * sqrt(1._f + m(0, 0) + m(1, 1) + m(2, 2));
46  const Float n = 0.25_f / w;
47  v[X] = (m(2, 1) - m(1, 2)) * n;
48  v[Y] = (m(0, 2) - m(2, 0)) * n;
49  v[Z] = (m(1, 0) - m(0, 1)) * n;
50  v[H] = w;
51  }
52 
54  INLINE Vector axis() const {
55  SPH_ASSERT(v[H] != 1._f);
56  return v / sqrt(1._f - sqr(v[H]));
57  }
58 
60  INLINE Float angle() const {
61  return acos(v[H]) * 2._f;
62  }
63 
64  INLINE Float& operator[](const Size idx) {
65  return v[idx];
66  }
67 
68  INLINE Float operator[](const Size idx) const {
69  return v[idx];
70  }
71 
74  const Float n = getSqrLength(v) + sqr(v[3]);
75  const Vector s = v * (n > 0._f ? 2._f / n : 0._f);
76  const Vector w = s * v[3];
77 
78  const Float xx = v[X] * s[X];
79  const Float xy = v[X] * s[Y];
80  const Float xz = v[X] * s[Z];
81  const Float yy = v[Y] * s[Y];
82  const Float yz = v[Y] * s[Z];
83  const Float zz = v[Z] * s[Z];
84 
85  return AffineMatrix( //
86  Vector(1._f - yy - zz, xy - w[Z], xz + w[Y]),
87  Vector(xy + w[Z], 1._f - xx - zz, yz - w[X]),
88  Vector(xz - w[Y], yz + w[X], 1._f - xx - yy));
89  }
90 };
91 
Three-dimensional affine matrix.
#define SPH_ASSERT(x,...)
Definition: Assert.h:94
NAMESPACE_SPH_BEGIN
Definition: BarnesHut.cpp:13
uint32_t Size
Integral type used to index arrays (by default).
Definition: Globals.h:16
double Float
Precision used withing the code. Use Float instead of float or double where precision is important.
Definition: Globals.h:13
constexpr INLINE T sqr(const T &f) noexcept
Return a squared value.
Definition: MathUtils.h:67
INLINE T sin(const T f)
Definition: MathUtils.h:296
INLINE T cos(const T f)
Definition: MathUtils.h:291
INLINE T sqrt(const T f)
Return a squared root of a value.
Definition: MathUtils.h:78
INLINE T acos(const T f)
Definition: MathUtils.h:306
#define INLINE
Macros for conditional compilation based on selected compiler.
Definition: Object.h:31
#define NAMESPACE_SPH_END
Definition: Object.h:12
INLINE Tuple< TArgs &... > tieToTuple(TArgs &... args)
Creates a tuple of l-value references. Use IGNORE placeholder to omit one or more parameters.
Definition: Tuple.h:304
INLINE Tuple< Vector, Float > getNormalizedWithLength(const Vector &v)
Returns normalized vector and length of the input vector as tuple.
Definition: Vector.h:597
INLINE Float getSqrLength(const Vector &v)
Definition: Vector.h:574
BasicVector< Float > Vector
Definition: Vector.h:539
@ H
Definition: Vector.h:25
@ Y
Definition: Vector.h:23
@ X
Definition: Vector.h:22
@ Z
Definition: Vector.h:24
INLINE Vector translation() const
Definition: AffineMatrix.h:49
bool isOrthogonal() const
Definition: AffineMatrix.h:111
Quaternion representing an axis of rotation and a (half of) rotation angle.
Definition: Quat.h:16
Quat(const Vector &axis, const Float angle)
Creates a quaternion given rotation axis and angle of rotation.
Definition: Quat.h:26
INLINE Vector axis() const
Returns the normalized rotational axis.
Definition: Quat.h:54
INLINE Float angle() const
Returns the angle of rotation (in radians).
Definition: Quat.h:60
AffineMatrix convert() const
Converts the quaternion into a rotation matrix.
Definition: Quat.h:73
Quat(const AffineMatrix &m)
Creates a quaternion given a rotation matrix.
Definition: Quat.h:42
INLINE Float operator[](const Size idx) const
Definition: Quat.h:68
Quat()=default
INLINE Float & operator[](const Size idx)
Definition: Quat.h:64