ACF $AcfVersion:0$
CQuaternion3d.h
Go to the documentation of this file.
1// SPDX-License-Identifier: LGPL-2.1-or-later OR GPL-2.0-or-later OR GPL-3.0-or-later OR LicenseRef-ACF-Commercial
2#pragma once
3
4
5// ACF includes
6#include <i3d/CVector3d.h>
7
8
9namespace i3d
10{
11
12
13class CMatrix3d;
14
15
21{
22public:
28
36 CQuaternion3d(double w, double x, double y, double z);
37
43 static CQuaternion3d FromAxisAngle(const CVector3d& axis, double angle);
44
48 static CQuaternion3d FromMatrix(const CMatrix3d& matrix);
49
56 static CQuaternion3d FromEulerAngles(double roll, double pitch, double yaw);
57
61 double GetW() const;
62
66 void SetW(double w);
67
71 double GetX() const;
72
76 void SetX(double x);
77
81 double GetY() const;
82
86 void SetY(double y);
87
91 double GetZ() const;
92
96 void SetZ(double z);
97
101 CVector3d GetVectorPart() const;
102
106 double GetLength() const;
107
111 double GetLengthSq() const;
112
117
121 void Normalize();
122
127
132
137
143 void ToAxisAngle(CVector3d& axis, double& angle) const;
144
148 CVector3d Rotate(const CVector3d& vector) const;
149
153 double GetDotProduct(const CQuaternion3d& other) const;
154
161 static CQuaternion3d Slerp(const CQuaternion3d& q1, const CQuaternion3d& q2, double t);
162
166 bool Serialize(iser::IArchive& archive);
167
169 CQuaternion3d operator*(double scalar) const;
172
174 CQuaternion3d& operator*=(double scalar);
175
176 bool operator==(const CQuaternion3d& other) const;
177 bool operator!=(const CQuaternion3d& other) const;
178
179private:
180 double m_w;
181 double m_x;
182 double m_y;
183 double m_z;
184};
185
186
187// inline methods
188
190: m_w(1.0)
191, m_x(0.0)
192, m_y(0.0)
193, m_z(0.0)
194{
195}
196
197
198inline CQuaternion3d::CQuaternion3d(double w, double x, double y, double z)
199: m_w(w)
200, m_x(x)
201, m_y(y)
202, m_z(z)
203{
204}
205
206
207inline double CQuaternion3d::GetW() const
208{
209 return m_w;
210}
211
212
213inline void CQuaternion3d::SetW(double w)
214{
215 m_w = w;
216}
217
218
219inline double CQuaternion3d::GetX() const
220{
221 return m_x;
222}
223
224
225inline void CQuaternion3d::SetX(double x)
226{
227 m_x = x;
228}
229
230
231inline double CQuaternion3d::GetY() const
232{
233 return m_y;
234}
235
236
237inline void CQuaternion3d::SetY(double y)
238{
239 m_y = y;
240}
241
242
243inline double CQuaternion3d::GetZ() const
244{
245 return m_z;
246}
247
248
249inline void CQuaternion3d::SetZ(double z)
250{
251 m_z = z;
252}
253
254
256{
257 return CVector3d(m_x, m_y, m_z);
258}
259
260
261inline double CQuaternion3d::GetLengthSq() const
262{
263 return m_w * m_w + m_x * m_x + m_y * m_y + m_z * m_z;
264}
265
266
267inline double CQuaternion3d::GetLength() const
268{
269 return qSqrt(GetLengthSq());
270}
271
272
274{
275 return CQuaternion3d(m_w, -m_x, -m_y, -m_z);
276}
277
278
279inline double CQuaternion3d::GetDotProduct(const CQuaternion3d& other) const
280{
281 return m_w * other.m_w + m_x * other.m_x + m_y * other.m_y + m_z * other.m_z;
282}
283
284
285inline bool CQuaternion3d::operator==(const CQuaternion3d& other) const
286{
287 return qAbs(m_w - other.m_w) < I_BIG_EPSILON &&
288 qAbs(m_x - other.m_x) < I_BIG_EPSILON &&
289 qAbs(m_y - other.m_y) < I_BIG_EPSILON &&
290 qAbs(m_z - other.m_z) < I_BIG_EPSILON;
291}
292
293
294inline bool CQuaternion3d::operator!=(const CQuaternion3d& other) const
295{
296 return !(*this == other);
297}
298
299
300} // namespace i3d
301
302
3D-matrix definition.
Definition CMatrix3d.h:18
Quaternion for representing rotations in 3D space.
void SetY(double y)
Set Y component.
static CQuaternion3d FromEulerAngles(double roll, double pitch, double yaw)
Construct quaternion from Euler angles (XYZ order).
CVector3d Rotate(const CVector3d &vector) const
Rotate vector by this quaternion.
double GetLengthSq() const
Calculate squared length of quaternion.
CQuaternion3d GetConjugate() const
Get conjugate of quaternion.
static CQuaternion3d FromAxisAngle(const CVector3d &axis, double angle)
Construct quaternion from axis and angle.
double GetW() const
Get W component (scalar part).
double GetDotProduct(const CQuaternion3d &other) const
Calculate dot product with another quaternion.
static CQuaternion3d Slerp(const CQuaternion3d &q1, const CQuaternion3d &q2, double t)
Spherical linear interpolation (SLERP) between two quaternions.
void SetX(double x)
Set X component.
CVector3d GetVectorPart() const
Get vector part of quaternion.
CQuaternion3d & operator*=(double scalar)
double GetLength() const
Calculate length (norm) of quaternion.
double GetY() const
Get Y component.
CQuaternion3d GetInverse() const
Get inverse of quaternion.
bool Serialize(iser::IArchive &archive)
Serialize this quaternion to specified archive.
void Normalize()
Normalize this quaternion in place.
CQuaternion3d()
Default constructor.
CMatrix3d ToMatrix() const
Convert quaternion to rotation matrix.
static CQuaternion3d FromMatrix(const CMatrix3d &matrix)
Construct quaternion from rotation matrix.
void SetW(double w)
Set W component.
CQuaternion3d operator*(const CQuaternion3d &other) const
CQuaternion3d operator-(const CQuaternion3d &other) const
bool operator==(const CQuaternion3d &other) const
void ToAxisAngle(CVector3d &axis, double &angle) const
Convert quaternion to axis and angle.
void SetZ(double z)
Set Z component.
bool operator!=(const CQuaternion3d &other) const
CQuaternion3d & operator*=(const CQuaternion3d &other)
CQuaternion3d operator+(const CQuaternion3d &other) const
CQuaternion3d operator*(double scalar) const
double GetX() const
Get X component.
CQuaternion3d GetNormalized() const
Get normalized quaternion (unit quaternion).
double GetZ() const
Get Z component.
Represents a position or mathematical vector in 3D space with double precision.
Definition CVector3d.h:74
Represents an input/output persistence archive for object serialization.
Definition IArchive.h:164
static const double I_BIG_EPSILON
Definition istd.h:26
Contains the 3D objects.
Definition CAffine3d.h:12