Scanframe Modular Application 0.1.0
Loading...
Searching...
No Matches
TQuaternion.h
Go to the documentation of this file.
1#pragma once
2#include <math/TVector3D.h>
3
4namespace sf
5{
6
7#pragma option push pack(1)
8
12template<typename T>
14{
15 public:
19 typedef T value_type;
20
24 TQuaternion() = default;
25
30
34 TQuaternion(T real, const TVector3D<T>& imag);
35
40 TQuaternion(const TVector3D<T>& axis, T angle);
41
45 TQuaternion(T real_w, T imag_x, T imag_y, T imag_z);
46
50 TQuaternion& assign(T real_w, T imag_x, T imag_y, T imag_z);
55
60
64 enum EElement : size_t
65 {
67 realW = 0,
69 imagX = 1,
71 imagY = 2,
73 imagZ = 3
74 };
75
80 T& operator[](size_t);
81
86 const T& operator[](size_t) const;
87
91 T real() const;
92
96 T& real();
97
102
107 T w() const;
108
113 T& w();
114
118 T x() const;
119
123 T& x();
124
128 T y() const;
129
133 T& y();
134
138 T z() const;
139
143 T& z();
144
152
161
167
172
177
182 T magnitudeSqr() const;
183
190 T magnitude() const;
191
196
202
208 TQuaternion& fromMatrix(const T[4][4]);
213
218
223
228
233
238
243
248
253
258
263
270 bool isEqual(const TQuaternion& quat, T tol = tolerance) const;
271
275 bool operator==(const TQuaternion&) const;
276
280 bool operator!=(const TQuaternion&) const;
281
291
306
313
323 TQuaternion interpolate(const TQuaternion& q, T t) const;
324
332
336 std::string toString() const;
337
343 TQuaternion& fromString(const std::string& s) noexcept(false);
344
350 static constexpr auto tolerance = TVector3D<T>::tolerance;
351
352 protected:
357 {
358 T array[4];
360 {
361 T w;
362 T x;
363 T y;
364 T z;
365 } q;
366 } _data{1, 0, 0, 0};
367};
368
376template<typename T>
378{
379 return TQuaternion<T>(lhs) += rhs;
380}
381
389template<typename T>
391{
392 return TQuaternion<T>(lhs) -= rhs;
393}
394
402template<typename T>
404{
405 return TQuaternion<T>(lhs) *= rhs;
406}
407
415template<typename T>
417{
418 return TQuaternion<T>(lhs) /= rhs;
419}
420
428template<typename T>
430{
431 return {c * quat.w(), c * quat.x(), c * quat.y(), c * quat.z()};
432}
433
442template<typename T>
443std::ostream& operator<<(std::ostream& os, const TQuaternion<T>& quat)
444{
445 return os << quat.toString();
446}
447
456template<typename T>
457std::istream& operator>>(std::istream& is, TQuaternion<T>& quat) noexcept(false)
458{
459 std::string s;
460 constexpr auto delimiter = ')';
461 std::getline(is, s, delimiter);
462 quat.fromString(s.append(1, delimiter));
463 return is;
464}
465
466}// namespace sf
467
468// Include all inlined functions and template implementations.
469#include <math/TQuaternion.hpp>
Generic 4 x 4 matrix template.
Definition TMatrix44.h:38
Generic type Quaternion.
Definition TQuaternion.h:14
const T & operator[](size_t) const
Array access const operator to the elements.
TQuaternion & normalize()
Normalizes this instance.
union sf::TQuaternion::data_type _data
bool operator==(const TQuaternion &) const
operator.
EElement
Enumerate for identifying a matrix axis.
Definition TQuaternion.h:65
@ imagY
Y imaginary part of the quaternion.
Definition TQuaternion.h:71
@ imagX
X imaginary part of the quaternion.
Definition TQuaternion.h:69
@ realW
Translation axis of the matrix.
Definition TQuaternion.h:67
@ imagZ
Z imaginary part of the quaternion.
Definition TQuaternion.h:73
bool isEqual(const TQuaternion &quat, T tol=tolerance) const
Compares the passed quaternion within the set tolerance.
TQuaternion(const TQuaternion &)
Copy Constructor.
TQuaternion conjugate() const
Gets a copy of this instance where the imaginary (vector) part of a quaternion is negated....
TQuaternion(T real_w, T imag_x, T imag_y, T imag_z)
Initializing constructor for 4 element parts.
T y() const
Get the imaginary Y-part of this instance.
TQuaternion & operator^=(const TQuaternion &)
operator.
TVector3D< T > transform(TVector3D< T > v) const
Transforms a 3D-vector not using the quaternion directly instead of a matrix.
TQuaternion & operator*=(const TQuaternion &)
Multiplication assignment operator.
TQuaternion & operator/=(const TQuaternion &)
Division assignment operator.
TQuaternion & fromMatrix(const T[4][4])
Transforms a rotation matrix to this Quaternion. Throws an exception when the passed matrix is not a ...
T x() const
Get the imaginary X-part of this instance.
TMatrix44< T > toMatrix(TMatrix44< T > &) const
Sets the orientation part of the passed TMatrix instance.
TQuaternion()=default
Default Constructor.
TQuaternion exp() const
Gets the exponential of a quaternion which is the inverse of log(). Moves back from the logarithmic f...
TQuaternion interpolateLogarithmic(const TQuaternion &q, T t) const
Implements a logarithmic interpolation function.
TQuaternion(T real, const TVector3D< T > &imag)
Initializing constructor is used in operators and sets the member variables directly.
TQuaternion & assign(T real_w, T imag_x, T imag_y, T imag_z)
Assigns of another instance elements.
std::string toString() const
Gets the string representation of the quaternion formed like '(x,y,z,r)'.
T z() const
Get the imaginary Z-part of this instance.
TQuaternion & operator*=(T c)
Multiplication assignment operator.
T magnitude() const
Gets the magnitude of this quaternion. The magnitude (or norm) of a quaternion q=a+bi+cj+dk,...
TQuaternion log() const
Gets the logarithm form of this instance. The logarithm log(q) of a quaternion is a mathematical oper...
TQuaternion operator/(T) const
operator.
TQuaternion operator-() const
Negation operator.
TQuaternion(const TVector3D< T > &axis, T angle)
Constructor for creating rotations by specifying the angle of rotation and the axis around which to r...
T w() const
Gets the w or real part of this instance. Same as real() method.
T & operator[](size_t)
Array access operator.
T & y()
Get the imaginary Y-part reference of this instance.
T real() const
Gets the real part of this instance.
TQuaternion & fromString(const std::string &s) noexcept(false)
Gets the quaternion values from the string representation formed like '(1,2,3,4)'....
T & real()
Gets the real part reference of this instance.
TQuaternion & invert()
Turns this instance into it inverse.
T value_type
Type declaration of the coordinate storage values.
Definition TQuaternion.h:19
TQuaternion & operator+=(const TQuaternion &)
Addition assignment operator.
TVector3D< T > imaginary() const
Get the imaginary part as a 3D-vector.
T & z()
Get the imaginary Z-part reference of this instance.
T magnitudeSqr() const
Gets the squared magnitude which is used in computations and the magnitude() method for the actual ma...
static constexpr auto tolerance
Tolerance for when comparing in the equal operator.
Definition TQuaternion.h:350
TQuaternion normalized() const
Get a normalizes copy of this instance.
TQuaternion squared() const
Squares this instance. If the quaternion is a unit quaternion (meaning it has magnitude 1),...
TQuaternion & operator=(const TQuaternion &)
Assignment operator.
TQuaternion operator*(T) const
Pointer cast operator.
T & w()
Gets the w or real part reference of this instance. Same as real() method.
bool operator!=(const TQuaternion &) const
operator.
TQuaternion & assign(const TQuaternion &)
Assigns of another instance elements.
TMatrix44< T > toMatrix() const
Get the matrix having only the orientation part set using this instance normalized version....
TQuaternion interpolate(const TQuaternion &q, T t) const
Spherical Linear Interpolation (SLERP) function. Used to smoothly blending or transitioning between t...
TQuaternion inverse() const
Gets the inverse of the current instance. The inverse of a quaternion is a quaternion that "reverses"...
TQuaternion & operator/=(T c)
Division operator.
T & x()
Get the imaginary X-part reference of this instance.
TQuaternion & operator-=(const TQuaternion &)
Subtraction assignment operator.
3-dimensional vector for math operations.
Definition TVector3D.h:17
Definition Application.h:10
_GII_FUNC std::istream & operator>>(std::istream &is, ResultData &)
Stream operator for setting up this instance with a setup std::string.
TMatrix44< T > operator*(const TMatrix44< T > &lm, const TMatrix44< T > &rm)
Multiplies to matrices into a single one.
TQuaternion< T > operator/(const TQuaternion< T > &lhs, const TQuaternion< T > &rhs)
Divides two quaternions.
Definition TQuaternion.h:416
TQuaternion< T > operator+(const TQuaternion< T > &lhs, const TQuaternion< T > &rhs)
Adds two quaternions.
Definition TQuaternion.h:377
_GII_FUNC std::ostream & operator<<(std::ostream &os, const ResultData &)
Stream operator for the setup std::string.
TQuaternion< T > operator-(const TQuaternion< T > &lhs, const TQuaternion< T > &rhs)
Subtracts two quaternions.
Definition TQuaternion.h:390
Definition TQuaternion.h:360
T x
Definition TQuaternion.h:362
T y
Definition TQuaternion.h:363
T z
Definition TQuaternion.h:364
T w
Definition TQuaternion.h:361
Storage union of the 3D coordinate making the x,y,z,w accessible as an array.
Definition TQuaternion.h:357
T array[4]
Definition TQuaternion.h:358
struct sf::TQuaternion::data_type::quat_type q