Skip to main content

quat Class Template

Quaternion with scalar part w and vector part v. More...

Declaration

template <typename T> class helios::math::quat<T> { ... }

Public Constructors Index

template <typename T>
constexprquat (const vec3< T > v, const T w) noexcept

Constructs a quaternion from vector and scalar parts. More...

Public Operators Index

template <typename T>
auto operator* (const quat< T > &other) const noexcept -> constexpr quat< T >

Multiplies this quaternion by another quaternion. More...

Public Member Functions Index

template <typename T>
auto conjugate () const noexcept -> constexpr quat< T >

Returns the quaternion conjugate. More...

template <typename T>
constexpr Tw () const noexcept

Returns the scalar part. More...

template <typename T>
auto v () const noexcept -> constexpr vec3< T >

Returns the vector part. More...

template <typename T>
constexpr Tlength () const noexcept

Returns the Euclidean norm of the quaternion. More...

template <typename T>
auto normalized () const noexcept -> constexpr quat< T >

Returns a normalized copy of the quaternion. More...

template <typename T>
auto rotationMatrix () const noexcept -> constexpr mat4< T >

Converts the (normalized) quaternion to a 4x4 rotation matrix. More...

template <typename T>
auto rotate (const vec3< T > v) const noexcept -> constexpr vec3< T >

Rotates a vector by this (normalized) quaternion. More...

Private Member Attributes Index

template <typename T>
Tw_
template <typename T>
vec3< T >v_

Public Static Functions Index

template <typename TFrameRelation>
static auto fromEulerAngles (const T xAngle, const T yAngle, const T zAngle) noexcept -> constexpr quat< T >

Creates a quaternion from Euler angles. More...

template <typename T>
static auto identity () noexcept -> constexpr quat< T >

Returns the identity quaternion. More...

template <typename T>
static auto fromAxisAngle (const helios::math::vec3< T > v, const T angle) noexcept -> constexpr quat< T >

Creates a quaternion from axis-angle representation. More...

Description

Quaternion with scalar part w and vector part v.

Template Parameters
T

Floating-point scalar type.

Definition at line 36 of file quat.ixx.

Public Constructors

quat()

template <typename T>
constexpr helios::math::quat< T >::quat (const vec3< T > v, const T w)
inline explicit constexpr noexcept

Constructs a quaternion from vector and scalar parts.

Parameters
v

Vector (imaginary) part.

w

Scalar (real) part.

Definition at line 103 of file quat.ixx.

103 explicit constexpr quat(const vec3<T> v, const T w) noexcept
104 : w_(w), v_(v) {}

Public Operators

operator*()

template <typename T>
constexpr quat< T > helios::math::quat< T >::operator* (const quat< T > & other)
inline constexpr noexcept

Multiplies this quaternion by another quaternion.

Parameters
other

Right-hand quaternion operand.

Returns

Product quaternion.

Definition at line 113 of file quat.ixx.

113 constexpr quat<T> operator*(const quat<T>& other) const noexcept {
114
115 const auto v = v_.cross(other.v_) + w_ * other.v_ + other.w_ * v_;
116
117 const auto w = w_ * other.w_ - dot(v_, other.v_);
118
119 return quat<T>{v, w};
120 }

References helios::math::vec3< T >::cross, helios::math::dot, helios::math::quat< T >::v and helios::math::quat< T >::w.

Public Member Functions

conjugate()

template <typename T>
constexpr quat< T > helios::math::quat< T >::conjugate ()
inline constexpr noexcept

Returns the quaternion conjugate.

Returns

Conjugated quaternion with negated vector part.

Definition at line 127 of file quat.ixx.

127 [[nodiscard]] constexpr quat<T> conjugate() const noexcept {
128 return quat<T>{v_ * static_cast<T>(-1), w_};
129 }

length()

template <typename T>
constexpr T helios::math::quat< T >::length ()
inline constexpr noexcept

Returns the Euclidean norm of the quaternion.

Returns

Quaternion length.

Definition at line 155 of file quat.ixx.

155 [[nodiscard]] constexpr T length() const noexcept {
156 return static_cast<T>(std::sqrt(
157 v_[0] * v_[0] + v_[1] * v_[1] + v_[2] * v_[2] + w_ * w_
158 ));
159 }

Referenced by helios::math::quat< T >::normalized.

normalized()

template <typename T>
constexpr quat< T > helios::math::quat< T >::normalized ()
inline constexpr noexcept

Returns a normalized copy of the quaternion.

Returns

Unit-length quaternion.

Definition at line 166 of file quat.ixx.

166 [[nodiscard]] constexpr quat<T> normalized() const noexcept {
167 const auto l = length();
168 return quat<T>{v_ * (static_cast<T>(1) / l), w_ / l};
169 }

Reference helios::math::quat< T >::length.

Referenced by helios::math::quat< T >::fromEulerAngles, helios::math::quat< T >::rotate and helios::math::quat< T >::rotationMatrix.

rotate()

template <typename T>
constexpr vec3< T > helios::math::quat< T >::rotate (const vec3< T > v)
inline constexpr noexcept

Rotates a vector by this (normalized) quaternion.

Parameters
v

Vector to rotate.

Returns

Rotated vector.

Definition at line 222 of file quat.ixx.

222 [[nodiscard]] constexpr vec3<T> rotate(const vec3<T> v) const noexcept {
223 const auto q_norm = normalized();
224 const auto other = quat<T>{v, 0};
225
226 return (q_norm * other * q_norm.conjugate()).v() ;
227 }

References helios::math::quat< T >::normalized and helios::math::quat< T >::v.

rotationMatrix()

template <typename T>
constexpr mat4< T > helios::math::quat< T >::rotationMatrix ()
inline constexpr noexcept

Converts the (normalized) quaternion to a 4x4 rotation matrix.

Returns

Rotation matrix derived from the normalized quaternion.

Definition at line 176 of file quat.ixx.

176 [[nodiscard]] constexpr mat4<T> rotationMatrix() const noexcept {
177
178 const auto q_norm = normalized();
179 const auto v = q_norm.v();
180 const auto w = q_norm.w();
181
182 const auto x2 = v[0] * v[0];
183 const auto y2 = v[1] * v[1];
184 const auto z2 = v[2] * v[2];
185 const auto xy = v[0] * v[1];
186 const auto xz = v[0] * v[2];
187 const auto yz = v[1] * v[2];
188 const auto wx = w * v[0];
189 const auto wy = w * v[1];
190 const auto wz = w * v[2];
191
192 return mat4<T>{
193 static_cast<T>(1) - static_cast<T>(2) * (y2 + z2),
194 static_cast<T>(2) * (xy + wz),
195 static_cast<T>(2) * (xz - wy),
196 static_cast<T>(0),
197
198 static_cast<T>(2) * (xy - wz),
199 static_cast<T>(1) - static_cast<T>(2) * (x2 + z2),
200 static_cast<T>(2) * (yz + wx),
201 static_cast<T>(0),
202
203 static_cast<T>(2) * (xz + wy),
204 static_cast<T>(2) * (yz - wx),
205 static_cast<T>(1) - static_cast<T>(2) * (x2 + y2),
206 static_cast<T>(0),
207
208 static_cast<T>(0),
209 static_cast<T>(0),
210 static_cast<T>(0),
211 static_cast<T>(1)
212 };
213
214 }

References helios::math::quat< T >::normalized, helios::math::quat< T >::v and helios::math::quat< T >::w.

v()

template <typename T>
constexpr vec3< T > helios::math::quat< T >::v ()
inline constexpr noexcept

Returns the vector part.

Returns

Vector component v.

Definition at line 146 of file quat.ixx.

146 [[nodiscard]] constexpr vec3<T> v() const noexcept {
147 return v_;
148 }

Referenced by helios::math::quat< T >::fromAxisAngle, helios::math::quat< T >::operator*, helios::math::quat< T >::rotate and helios::math::quat< T >::rotationMatrix.

w()

template <typename T>
constexpr T helios::math::quat< T >::w ()
inline constexpr noexcept

Returns the scalar part.

Returns

Scalar component w.

Definition at line 137 of file quat.ixx.

137 [[nodiscard]] constexpr T w() const noexcept {
138 return w_;
139 }

Referenced by helios::math::quat< T >::operator* and helios::math::quat< T >::rotationMatrix.

Private Member Attributes

v_

template <typename T>
vec3<T> helios::math::quat< T >::v_

Definition at line 40 of file quat.ixx.

40 vec3<T> v_;

w_

template <typename T>
T helios::math::quat< T >::w_

Definition at line 38 of file quat.ixx.

38 T w_;

Public Static Functions

fromAxisAngle()

template <typename T>
constexpr quat< T > helios::math::quat< T >::fromAxisAngle (const helios::math::vec3< T > v, const T angle)
inline constexpr noexcept static

Creates a quaternion from axis-angle representation.

Parameters
v

Rotation axis (normalized internally).

angle

Rotation angle in radians.

Returns

Quaternion representing the axis-angle rotation.

Definition at line 86 of file quat.ixx.

86 static constexpr quat<T> fromAxisAngle(const helios::math::vec3<T> v, const T angle) noexcept {
87
88 const auto rad = angle * static_cast<T>(0.5);
89
90 return quat<T>{
91 std::sin(rad) * v.normalize(),
92 std::cos(rad)
93 };
94
95 }

Reference helios::math::quat< T >::v.

Referenced by helios::math::quat< T >::fromEulerAngles.

fromEulerAngles()

template <typename TFrameRelation>
constexpr quat< T > helios::math::quat< T >::fromEulerAngles (const T xAngle, const T yAngle, const T zAngle)
inline constexpr noexcept static

Creates a quaternion from Euler angles.

Template Parameters
TFrameRelation

Rotation composition mode (Intrinsic or Extrinsic).

Parameters
xAngle

Rotation angle around the x-axis in radians.

yAngle

Rotation angle around the y-axis in radians.

zAngle

Rotation angle around the z-axis in radians.

Returns

Quaternion representing the composed rotation.

Definition at line 56 of file quat.ixx.

56 static constexpr quat<T> fromEulerAngles(const T xAngle, const T yAngle, const T zAngle) noexcept {
57
58 const auto x = vec3<T>{static_cast<T>(1), static_cast<T>(0), static_cast<T>(0)};
59 const auto y = vec3<T>{static_cast<T>(0), static_cast<T>(1), static_cast<T>(0)};
60 const auto z = vec3<T>{static_cast<T>(0), static_cast<T>(0), static_cast<T>(1)};
61
62 if constexpr (std::same_as<TFrameRelation, Intrinsic>) {
63 return (fromAxisAngle(x, xAngle) * fromAxisAngle(y, yAngle) * fromAxisAngle(z, zAngle)).normalized();
64 } else {
65 return (fromAxisAngle(z, zAngle) * fromAxisAngle(y, yAngle) * fromAxisAngle(x, xAngle)).normalized();
66 }
67
68 }

References helios::math::quat< T >::fromAxisAngle and helios::math::quat< T >::normalized.

identity()

template <typename T>
constexpr quat< T > helios::math::quat< T >::identity ()
inline constexpr noexcept static

Returns the identity quaternion.

Returns

Quaternion representing no rotation.

Definition at line 75 of file quat.ixx.

75 static constexpr quat<T> identity() noexcept {
76 return quat<T>{vec3<T>{static_cast<T>(0), static_cast<T>(0), static_cast<T>(0)}, static_cast<T>(1)};
77 }

The documentation for this class was generated from the following file:


Generated via doxygen2docusaurus 2.0.0 by Doxygen 1.9.8.