PeriDyno 1.0.0
Loading...
Searching...
No Matches
Quat.h
Go to the documentation of this file.
1
20#pragma once
21#include "Vector.h"
22#include "Matrix.h"
23#define GLM_FORCE_RADIANS
24
25namespace dyno
26{
27 /*
28 * Quaternion is defined for float and double, all functions are taking radians as parameters
29 */
30 template <typename Real>
31 class Quat
32 {
33 public:
34 /* Constructors */
35 DYN_FUNC Quat();
36
42 DYN_FUNC Quat(Real x, Real y, Real z, Real w);
43
51 DYN_FUNC Quat(Real rot, const Vector<Real, 3> &axis); //init from the rotation axis and angle(in radian)
52
53 DYN_FUNC Quat(const Vector<Real, 3> u0, const Vector<Real, 3> u1); // u0 --[rot]--> u1
54 DYN_FUNC Quat(const Quat<Real> &);
55 DYN_FUNC explicit Quat(const SquareMatrix<Real, 3> &); //init from a 3x3matrix
56 DYN_FUNC explicit Quat(const SquareMatrix<Real, 4> &); //init from a 4x4matrix
57
58 // yaw (Z), pitch (Y), roll (X);
59 DYN_FUNC explicit Quat(const Real yaw, const Real pitch, const Real roll);
60
61 /* Assignment operators */
62 DYN_FUNC Quat<Real> &operator = (const Quat<Real> &);
65
66 /* Special functions */
67 DYN_FUNC Real norm() const;
68 DYN_FUNC Real normSquared() const;
69 DYN_FUNC Quat<Real>& normalize();
70
71 DYN_FUNC Quat<Real> inverse() const;
72
73 DYN_FUNC Real angle() const; // return the angle between this quat and the identity quaternion.
74 DYN_FUNC Real angle(const Quat<Real>&) const; // return the angle between this and the argument
75 DYN_FUNC Quat<Real> conjugate() const; // return the conjugate
76
83 DYN_FUNC Vector<Real, 3> rotate(const Vector<Real, 3>& v) const;
84
85 DYN_FUNC void toRotationAxis(Real &rot, Vector<Real, 3> &axis) const;
86
87 DYN_FUNC void toEulerAngle(Real& yaw, Real& pitch, Real& roll) const;
88
89 DYN_FUNC SquareMatrix<Real, 3> toMatrix3x3() const; //return 3x3matrix format
90 DYN_FUNC SquareMatrix<Real, 4> toMatrix4x4() const; //return 4x4matrix with a identity transform.
91
92
93 /* Operator overloading */
94 DYN_FUNC Quat<Real> operator - (const Quat<Real>&) const;
95 DYN_FUNC Quat<Real> operator - (void) const;
96 DYN_FUNC Quat<Real> operator + (const Quat<Real>&) const;
97 DYN_FUNC Quat<Real> operator * (const Quat<Real>&) const;
98 DYN_FUNC Quat<Real> operator * (const Real&) const;
99 DYN_FUNC Quat<Real> operator / (const Real&) const;
100 DYN_FUNC bool operator == (const Quat<Real>&) const;
101 DYN_FUNC bool operator != (const Quat<Real>&) const;
102 DYN_FUNC Real dot(const Quat<Real> &) const;
103
104 DYN_FUNC static inline Quat<Real> identity() { return Quat<Real>(0, 0, 0, 1); }
105 DYN_FUNC static inline Quat<Real> fromEulerAngles(const Real& yaw, const Real& pitch, const Real& roll) {
106 Real cr = glm::cos(roll * 0.5);
107 Real sr = glm::sin(roll * 0.5);
108 Real cp = glm::cos(pitch * 0.5);
109 Real sp = glm::sin(pitch * 0.5);
110 Real cy = glm::cos(yaw * 0.5);
111 Real sy = glm::sin(yaw * 0.5);
112
113 Quat<Real> q;
114 q.w = cr * cp * cy + sr * sp * sy;
115 q.x = sr * cp * cy - cr * sp * sy;
116 q.y = cr * sp * cy + sr * cp * sy;
117 q.z = cr * cp * sy - sr * sp * cy;
118
119 return q;
120 }
121
122 public:
123 Real x, y, z, w;
124 };
125
126 //make * operator commutative
127 template <typename S, typename T>
128 DYN_FUNC inline Quat<T> operator *(S scale, const Quat<T> &quad)
129 {
130 return quad * scale;
131 }
132
133 template class Quat<float>;
134 template class Quat<double>;
135 //convenient typedefs
138
139}//end of namespace dyno
140#include "Quat.inl"
double Real
Definition Typedef.inl:23
DYN_FUNC Quat< Real > & operator-=(const Quat< Real > &)
Definition Quat.inl:98
DYN_FUNC Quat< Real > & normalize()
Definition Quat.inl:183
DYN_FUNC Quat(Real rot, const Vector< Real, 3 > &axis)
Construct a quaternion from a rotation and a unit vector.
Definition Quat.inl:29
DYN_FUNC Quat< Real > operator-(void) const
Definition Quat.inl:114
DYN_FUNC void toRotationAxis(Real &rot, Vector< Real, 3 > &axis) const
Definition Quat.inl:392
DYN_FUNC Real angle(const Quat< Real > &) const
Definition Quat.inl:237
DYN_FUNC Quat(const Real yaw, const Real pitch, const Real roll)
Definition Quat.inl:61
DYN_FUNC Real normSquared() const
Definition Quat.inl:177
DYN_FUNC Quat(const Quat< Real > &)
Definition Quat.inl:51
static DYN_FUNC Quat< Real > fromEulerAngles(const Real &yaw, const Real &pitch, const Real &roll)
Definition Quat.h:105
DYN_FUNC Quat< Real > conjugate() const
Definition Quat.inl:252
DYN_FUNC Quat(Real x, Real y, Real z, Real w)
Construct a quaternion, be aware that w is the scalar part and (x, y, z) is the vector part.
Definition Quat.inl:20
DYN_FUNC Quat(const SquareMatrix< Real, 4 > &)
Definition Quat.inl:355
DYN_FUNC Quat< Real > inverse() const
Definition Quat.inl:201
DYN_FUNC Quat< Real > operator/(const Real &) const
Definition Quat.inl:146
DYN_FUNC Quat< Real > operator+(const Quat< Real > &) const
Definition Quat.inl:120
DYN_FUNC bool operator==(const Quat< Real > &) const
Definition Quat.inl:152
DYN_FUNC Real dot(const Quat< Real > &) const
Definition Quat.inl:246
DYN_FUNC Quat< Real > & operator=(const Quat< Real > &)
Definition Quat.inl:78
DYN_FUNC void toEulerAngle(Real &yaw, Real &pitch, Real &roll) const
Definition Quat.inl:207
DYN_FUNC Quat(const Vector< Real, 3 > u0, const Vector< Real, 3 > u1)
Definition Quat.inl:40
DYN_FUNC Quat(const SquareMatrix< Real, 3 > &)
Definition Quat.inl:318
DYN_FUNC Real norm() const
Definition Quat.inl:168
DYN_FUNC Quat< Real > operator*(const Quat< Real > &) const
Definition Quat.inl:132
DYN_FUNC Real angle() const
Definition Quat.inl:231
DYN_FUNC SquareMatrix< Real, 3 > toMatrix3x3() const
Definition Quat.inl:275
static DYN_FUNC Quat< Real > identity()
Definition Quat.h:104
DYN_FUNC Quat< Real > & operator+=(const Quat< Real > &)
Definition Quat.inl:88
DYN_FUNC bool operator!=(const Quat< Real > &) const
Definition Quat.inl:160
DYN_FUNC Vector< Real, 3 > rotate(const Vector< Real, 3 > &v) const
Rotate a vector by the quaternion, guarantee the quaternion is normalized before rotating the vector.
Definition Quat.inl:259
DYN_FUNC Quat()
Definition Quat.inl:11
DYN_FUNC SquareMatrix< Real, 4 > toMatrix4x4() const
Definition Quat.inl:287
This is an implementation of AdditiveCCD based on peridyno.
Definition Array.h:25
Quat< float > Quat1f
Definition Quat.h:136
Quat< double > Quat1d
Definition Quat.h:137
DYN_FUNC const Complex< T > operator*(S scale, const Complex< T > &complex)
Definition Complex.inl:247