2
3
4
5
6
7
8
9
10
11
14#ifndef _MSTD_QUAT_HPP_
15 #define _MSTD_QUAT_HPP_
17 #include <mstd/config.hpp>
20_MSTD_WARNING(
"this is only available for c++17 and greater!");
23 #include <mstd/vec.hpp>
29 template<
class T, std::enable_if_t<std::is_arithmetic_v<T>,
bool> >
39 #pragma region CONSTRUCTORS
47 template<arithmetic OT>
49 template<
class OT, std::enable_if_t<std::is_arithmetic_v<OT>,
bool> =
true>
56 #pragma region DESTRUCTOR
62 template<arithmetic OT>
64 template<
class OT, std::enable_if_t<std::is_arithmetic_v<OT>,
bool> =
true>
67 s =
static_cast<T>(other.s);
74 #pragma region PREDEFINED_QUATERNIONS
80 if (!axis.is_zero()) {
81 q = quat<T>(
static_cast<T>(std::cos(radians * half)),
82 axis.normalized() *
static_cast<T>(std::sin(radians * half)));
84 else { q = quat<T>(
static_cast<T>(std::cos(radians * half)), axis); }
85 if (q.magnitude() !=
static_cast<T>(0)) { q.normalize(); }
90 return from_radians({ deg_to_rad(eulerAngels[0]), deg_to_rad(eulerAngels[1]), deg_to_rad(eulerAngels[2]) });
94 quat<T> qx = rotation(vec_type(
static_cast<T>(1),
static_cast<T>(0),
static_cast<T>(0)), radians[0]);
95 quat<T> qy = rotation(vec_type(
static_cast<T>(0),
static_cast<T>(1),
static_cast<T>(0)), radians[1]);
96 quat<T> qz = rotation(vec_type(
static_cast<T>(0),
static_cast<T>(0),
static_cast<T>(1)), radians[2]);
99 quat<T> q = qz * qy * qx;
100 if (q.magnitude() !=
static_cast<T>(0)) { q.normalize(); }
106 #pragma region QUATERNION_OPERATIONS
118 return res.normalize();
128 return res.conjugate();
132 T magnitudes = magnitude();
133 magnitudes *= magnitudes;
134 magnitudes =
static_cast<T>(1.0 / magnitudes);
156 if (q.magnitude() !=
static_cast<T>(0)) { q.normalize(); }
159 T sinxCosp =
static_cast<T>(two * ((q.s * q.v[0]) + (q.v[1] * q.v[2])));
160 T cosxCosp =
static_cast<T>(1.0 - (two * ((q.v[0] * q.v[0]) + (q.v[1] * q.v[1]))));
161 res[0] =
static_cast<T>(std::atan2(sinxCosp, cosxCosp));
164 T siny =
static_cast<T>(std::sqrt(1.0 + (two * ((q.s * q.v[1]) - (q.v[0] * q.v[2])))));
165 T cosy =
static_cast<T>(std::sqrt(1.0 - (two * ((q.s * q.v[1]) - (q.v[0] * q.v[2])))));
166 res[1] =
static_cast<T>((two * std::atan2(siny, cosy)) - (M_PI * half));
169 T sinzCosp =
static_cast<T>(two * ((q.s * q.v[2]) + (q.v[0] * q.v[1])));
170 T coszCosp =
static_cast<T>(1.0 - (two * ((q.v[1] * q.v[1]) + (q.v[2] * q.v[2]))));
171 res[2] =
static_cast<T>(std::atan2(sinzCosp, coszCosp));
177 vec_type res = to_radians();
178 res[0] = rad_to_deg(res[0]);
179 res[1] = rad_to_deg(res[1]);
180 res[2] = rad_to_deg(res[2]);
188 #pragma region OPERATORS
204 s = (s * other.s) - v.dot(other.v);
205 v = (other.v * t) + (v * other.s) + v.cross(other.v);
210 quat<T> p(
static_cast<T>(0), other);
222 *
this *= other.inverted();
227 if (other ==
static_cast<T>(0)) {
return *
this; }
254 return quaternion * other;
306 friend std::ostream& operator<<(std::ostream& str,
const quat<T>& quaternion) {
307 return str <<
"(" << std::to_string(quaternion.s) <<
", " << quaternion.v <<
")";
_MSTD_CONSTEXPR20 quat< T > operator*(const vec_type &other) const
Definition quat.hpp:248
_MSTD_CONSTEXPR20 quat()
Definition quat.hpp:41
_MSTD_CONSTEXPR20 quat< T > operator-(const quat< T > &other) const
Definition quat.hpp:238
_MSTD_CONSTEXPR20 quat< T > & operator/=(const T &other)
Definition quat.hpp:226
_MSTD_CONSTEXPR20 quat< T > inverted() const
Definition quat.hpp:144
_MSTD_CONSTEXPR20 quat< T > & operator*=(const vec_type &other)
Definition quat.hpp:209
friend _MSTD_CONSTEXPR20 quat< T > operator*(const vec_type &other, const quat< T > &quaternion)
Definition quat.hpp:253
friend _MSTD_CONSTEXPR20 quat< T > operator*(const T &other, const quat< T > &quaternion)
Definition quat.hpp:262
vec< 3, T > vec_type
Definition quat.hpp:34
_MSTD_CONSTEXPR20 quat< T > & operator/=(const quat< T > &other)
Definition quat.hpp:221
_MSTD_CONSTEXPR20 quat(const T &scalar, const T &x, const T &y, const T &z)
Definition quat.hpp:45
_MSTD_CONSTEXPR20 ~quat()=default
_MSTD_CONSTEXPR20 quat< T > operator*(const quat< T > &other) const
Definition quat.hpp:243
T s
Definition quat.hpp:36
_MSTD_CONSTEXPR20 quat< T > operator++(int)
Definition quat.hpp:296
_MSTD_CONSTEXPR20 quat< T > operator--(int)
Definition quat.hpp:284
_MSTD_CONSTEXPR20 bool operator!=(const quat< T > &other) const
Definition quat.hpp:304
_MSTD_CONSTEXPR20 vec_type to_radians() const
Definition quat.hpp:149
_MSTD_CONSTEXPR20 quat< T > & operator*=(const T &other)
Definition quat.hpp:215
_MSTD_CONSTEXPR20 quat< T > conjugated() const
Definition quat.hpp:126
_MSTD_CONSTEXPR20 vec_type to_euler_angles() const
Definition quat.hpp:176
_MSTD_CONSTEXPR20 quat< T > & normalize()
Definition quat.hpp:110
_MSTD_CONSTEXPR20 quat< T > operator-() const
Definition quat.hpp:274
_MSTD_CONSTEXPR20 quat< T > & operator++()
Definition quat.hpp:290
_MSTD_CONSTEXPR20 T magnitude() const
Definition quat.hpp:108
_MSTD_CONSTEXPR20 quat< T > & operator--()
Definition quat.hpp:278
static _MSTD_CONSTEXPR20 quat< T > from_radians(const vec_type &radians)
Definition quat.hpp:93
vec_type v
Definition quat.hpp:37
_MSTD_CONSTEXPR20 quat(const T &scalar, const vec_type &vector)
Definition quat.hpp:43
_MSTD_CONSTEXPR20 quat< T > & invert()
Definition quat.hpp:131
_MSTD_CONSTEXPR20 bool operator==(const quat< T > &other) const
Definition quat.hpp:302
_MSTD_CONSTEXPR20 quat< T > operator/(const T &other) const
Definition quat.hpp:269
_MSTD_CONSTEXPR20 quat< T > operator*(const T &other) const
Definition quat.hpp:257
_MSTD_CONSTEXPR20 quat< T > operator+() const
Definition quat.hpp:276
_MSTD_CONSTEXPR20 quat< T > & operator*=(const quat< T > &other)
Definition quat.hpp:202
_MSTD_CONSTEXPR20 quat< T > normalized() const
Definition quat.hpp:116
_MSTD_CONSTEXPR20 quat< T > & operator+=(const quat< T > &other)
Definition quat.hpp:190
_MSTD_CONSTEXPR20 quat< T > & conjugate()
Definition quat.hpp:121
_MSTD_CONSTEXPR20 quat< T > & operator-=(const quat< T > &other)
Definition quat.hpp:196
T value_type
Definition quat.hpp:33
static _MSTD_CONSTEXPR20 quat< T > rotation(const vec_type &axis, const T &radians)
Definition quat.hpp:76
_MSTD_CONSTEXPR20 quat< T > operator/(const quat< T > &other) const
Definition quat.hpp:264
_MSTD_CONSTEXPR20 T scalar(const quat< T > &other)
Definition quat.hpp:184
_MSTD_CONSTEXPR20 quat< T > operator+(const quat< T > &other) const
Definition quat.hpp:233
static _MSTD_CONSTEXPR20 quat< T > from_euler_angels(const vec_type &eulerAngels)
Definition quat.hpp:89
_MSTD_CONSTEXPR20 quat(const quat< OT > &other)
Definition quat.hpp:51
_MSTD_CONSTEXPR20 quat< T > & operator=(const quat< OT > &other)
Definition quat.hpp:66
#define _MSTD_HAS_CXX17
Definition config.hpp:45
#define _MSTD_CONSTEXPR17
Definition config.hpp:76
#define _MSTD_HAS_CXX20
Definition config.hpp:52
#define _MSTD_CONSTEXPR20
Definition config.hpp:84
Definition arithmetic_types.hpp:23