-
Notifications
You must be signed in to change notification settings - Fork 45
/
Copy pathQuaternion.h
368 lines (322 loc) · 10.2 KB
/
Quaternion.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
#ifndef BIORBD_UTILS_QUATERNION_H
#define BIORBD_UTILS_QUATERNION_H
#include <memory>
#include "Utils/Vector3d.h"
#include "Utils/Matrix3d.h"
#include "Utils/Scalar.h"
#include "biorbdConfig.h"
namespace BIORBD_NAMESPACE
{
namespace utils
{
class Vector3d;
class Vector;
class RotoTrans;
class Rotation;
class String;
///
/// Description of a quaternion in the format (w, x, y, z)
///
/// The definition for conversions are taken from
/// https://www.euclideanspace.com/maths/geometry/rotations/conversions/index.htm
///
#ifdef SWIG
class BIORBD_API Quaternion
#else
class BIORBD_API Quaternion : public RigidBodyDynamics::Math::Vector4d
#endif
{
public:
///
/// \brief Construct Quaternion
/// \param kStabilizer The value of the kstabilizer
///
Quaternion (
double kStabilizer = 1);
///
/// \brief Construct Quaternion from another quaternion
/// \param other Other quaternion
///
Quaternion(
const Quaternion &other);
///
/// \brief Construct Quaternion
/// \param vec4 The vector describing the quaternion
/// \param kStabilizer The value of the kstabilizer
///
Quaternion (
const RigidBodyDynamics::Math::Vector4d &vec4,
double kStabilizer = 1);
///
/// \brief Construct Quaternion
/// \param w The W-Component of quaternion
/// \param x The X-Component of quaternion
/// \param y The Y-Component of quaternion
/// \param z The Z-Component of quaternion
/// \param kStabilizer The value of the kstabilizer
///
Quaternion (
const Scalar& w,
const Scalar& x,
const Scalar& y,
const Scalar& z,
double kStabilizer = 1);
///
/// \brief Construct Quaternion
/// \param w The W-Component of quaternion
/// \param vec3 The vector describing the imaginary part
/// \param kStabilizer The value of the kstabilizer
///
Quaternion (
const Scalar& w,
const Vector3d &vec3,
double kStabilizer = 1);
///
/// \brief Return the real part (w) the Quaternion
/// \return The real part of the Quaternion
///
Scalar w() const;
///
/// \brief Return the X-Component of the imaginary part of the Quaternion
/// \return The X-Component of the imaginary part of the Quaternion
///
Scalar x() const;
///
/// \brief Return the Y-Component of the imaginary part of the Quaternion
/// \return The Y-Component of the imaginary part of the Quaternion
///
Scalar y() const;
///
/// \brief Return the Z-Component of the imaginary part of the Quaternion
/// \return The Z-Component of the imaginary part of the Quaternion
///
Scalar z() const;
///
/// \brief Set the k stabilizer
/// \param newKStab The new value
///
void setKStab(double newKStab);
///
/// \brief Return the k stabilizer
/// \return The k stabilizer
///
/// The k statilizer value is used during several operation to make the
/// quaternion tending toward a norm of \f$1\f$
///
double kStab() const;
#ifndef SWIG
#ifdef BIORBD_USE_EIGEN3_MATH
///
/// \brief Allows for the operation= assignation
/// \param other The other quaternion
///
template<typename OtherDerived>
Quaternion& operator=(
const Eigen::MatrixBase <OtherDerived>& other)
{
this->Eigen::Vector4d::operator=(other);
// I don't understand why the next line doesn't SegFault...
this->m_Kstab = static_cast<Quaternion>(other).m_Kstab;
return *this;
}
#endif
#endif
///
/// \brief Quaternion multiplication
/// \param other The other quaternion
///
Quaternion operator*(
const Quaternion& other) const;
///
/// \brief Multiply the quaternion with a scalar
/// \param scalar The scalar to multiply with
///
Quaternion operator*(
const Scalar& scalar) const;
///
/// \brief Multiply the quaternion with a scalar
/// \param scalar The scalar to multiply with
///
Quaternion operator*(
float scalar) const;
#ifdef BIORBD_USE_CASADI_MATH
///
/// \brief Multiply the quaternion with a scalar
/// \param scalar The scalar to multiply with
///
Quaternion operator*(
double scalar) const;
#endif
///
/// \brief Add the quaternion to another
/// \param other The other quaternion to add
///
Quaternion operator+(
const Quaternion& other) const;
///
/// \brief Subtract the quaternion to another
/// \param other Other quaternion to substract
///
Quaternion operator-(
const Quaternion& other) const;
///
/// \brief Construct Quaternion from a GL
/// \param angle The angle in radians
/// \param x The X-Component of quaternion
/// \param y The Y-Component of quaternion
/// \param z The Z-Component of quaternion
/// \param kStab The value of the kstabilizer
///
static Quaternion fromGLRotate (
const Scalar& angle,
const Scalar& x,
const Scalar& y,
const Scalar& z,
double kStab = 1);
///
/// \brief Construct Quaternion from an axis angle
/// \param angle The angle in radians
/// \param axis The 3d vector of the axis
/// \param kStab The value of the kstabilizer
///
static Quaternion fromAxisAngle (
const Scalar& angle,
const Vector3d &axis,
double kStab = 1);
///
/// \brief Construct Quaternion from a RotoTrans matrix
/// \param rt RotoTrans matrix
/// \param kStab The value of the kstabilizer
///
static Quaternion fromMatrix (
const RotoTrans& rt,
double kStab = 1);
///
/// \brief Construct Quaternion from a Rotation matrix
/// \param mat The rotation matrix
/// \param kStab The value of the kstabilizer
///
static Quaternion fromMatrix (
const Rotation &mat,
double kStab = 1);
///
/// \brief Construct Quaternion from Euler angles (sequece ZYX)
/// \param zyx_angles The Euler angles in a sequence where the first element is the Z-component
/// \param kStab The value of the kstabilizer
///
static Quaternion fromZYXAngles (
const Vector3d &zyx_angles,
double kStab = 1);
///
/// \brief Construct Quaternion from Euler angles (sequece YXZ)
/// \param yxz_angles The Euler angles in a sequence where the first element is the Y-component
/// \param kStab The value of the kstabilizer
///
static Quaternion fromYXZAngles (
const Vector3d &yxz_angles,
double kStab = 1);
///
/// \brief Construct Quaternion from Euler angles (sequece XYZ)
/// \param xyz_angles The Euler angles in a sequence where the first element is the X-component
/// \param kStab The value of the kstabilizer
///
static Quaternion fromXYZAngles (
const Vector3d &xyz_angles,
double kStab = 1);
///
/// \brief Convert the quaternion to a RotoTrans
/// \param skipAsserts Check if the norm of the quaternion is approximately 1
/// \return The rotation matrix
///
/// The function throws a runtime_error if the skipAsserts is false and the
/// norm of the quaternion is not almost one. In order to accelerate the
/// computation of the norm, the norm-squared is evaluated. The threshold is
/// 1e-10 for the norm-squared
///
Rotation toMatrix(
bool skipAsserts = false) const;
#ifndef BIORBD_USE_CASADI_MATH
///
/// \brief Interpolation of the quaternion between to position
/// \param alpha The proportion of the rotation
/// \param quat The quaternion to targe
///
Quaternion slerp (
double alpha,
const Quaternion &quat) const;
#endif
///
/// \brief Return the conjugate of the quaternion
/// \return The conjugate of the quaternion
///
Quaternion conjugate() const;
///
/// \brief Integrate the quaternion from its velocity
/// \param omega 3D vector of the angular velocity
/// \param dt The time step to intergrate on
/// \return The rotated quaternion
///
Quaternion timeStep (
const Vector3d &omega,
double dt);
///
/// \brief Return a rotated vector from the quaternion
/// \param vec The vector to rotate
/// \return The rotated vector
///
Vector3d rotate (
const Vector3d &vec) const;
///
/// \brief Converts a 3d angular velocity vector into a 4d derivative of the components of the quaternion
/// \param omega the angular velocity
/// \return a 4d vector containing the derivatives of the 4 components of the quaternion corresponding to omega
///
Quaternion omegaToQuatDot(
const Vector3d& omega) const;
///
/// \brief Generate the velocity matrix which allows to go from/to euler angles to/from omega(body velocity)
/// \param euler the Euler angles
/// \param seq the Euler angles sequence
/// \return a 3d matrix
///
Matrix3d velocityMatrix (
const Vector3d &euler,
const String& seq);
///
/// \brief Converts a 3d angular velocity vector
/// \param eulerDot the Euler angle rates
/// \param euler the Euler angles
/// \param seq the Euler angles sequence
/// \return a 3d vector of the body angular velocity
///
Vector3d eulerDotToOmega(
const Vector3d &eulerDot,
const Vector3d &euler,
const String& seq);
///
/// \brief converts a 3d vector of the body angular velocity (omega) into a 3d vector of the euler angles rate.
/// \param euler the Euler angles
/// \param w the body velocity (omega)
/// \param seq the Euler angles sequence
/// \return a 3d vector of the euler angles rate
///
Vector3d omegaToEulerDot(
const Vector3d &euler,
const Vector3d &w,
const String& seq);
///
/// \brief Return the time derivative of the quaterion
/// \param w The vector of time derivative (output)
///
void derivate(
const Vector &w);
///
/// \brief Force the normalization of the quaternion
///
void normalize();
protected:
double m_Kstab; ///< Stabilization factor for the derivation
};
}
}
#endif // BIORBD_UTILS_QUATERNION_H