-
Notifications
You must be signed in to change notification settings - Fork 5
/
tiny_quaternion.h
469 lines (395 loc) · 15.5 KB
/
tiny_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
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
/*
* Copyright 2020 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef TINY_QUATERNION_H
#define TINY_QUATERNION_H
#ifdef USE_EIGEN
#include <Eigen/Geometry>
#endif
#include "tiny_vector3.h"
template <typename TinyScalar, typename TinyConstants>
struct TinyQuaternion {
typedef ::TinyVector3<TinyScalar, TinyConstants> TinyVector3;
TinyScalar m_x;
TinyScalar m_y;
TinyScalar m_z;
TinyScalar m_w;
// TinyQuaternion() = default;
TinyQuaternion() {
set_zero();
}
TinyQuaternion(TinyScalar x, TinyScalar y, TinyScalar z, TinyScalar w)
: m_x(x), m_y(y), m_z(z), m_w(w) {
if (TinyConstants::getBool(x == TinyConstants::zero()) &&
TinyConstants::getBool(y == TinyConstants::zero()) &&
TinyConstants::getBool(z == TinyConstants::zero()) &&
TinyConstants::getBool(w == TinyConstants::zero())) {
fprintf(stderr,
"Error: cannot construct a quaternion with x = y = z = w = 0.");
assert(0);
}
}
const TinyScalar& getX() const { return m_x; }
const TinyScalar& getY() const { return m_y; }
const TinyScalar& getZ() const { return m_z; }
const TinyScalar& getW() const { return m_w; }
const TinyScalar& x() const { return m_x; }
const TinyScalar& y() const { return m_y; }
const TinyScalar& z() const { return m_z; }
const TinyScalar& w() const { return m_w; }
void setValue(const TinyScalar& x, const TinyScalar& y, const TinyScalar& z,
const TinyScalar& w) {
m_x = x;
m_y = y;
m_z = z;
m_w = w;
}
void set_zero() {
setValue(TinyConstants::zero(), TinyConstants::zero(),
TinyConstants::zero(), TinyConstants::zero());
}
void set_identity() {
setValue(TinyConstants::zero(), TinyConstants::zero(),
TinyConstants::zero(), TinyConstants::one());
}
static TinyQuaternion create(const TinyScalar& x, const TinyScalar& y,
const TinyScalar& z, const TinyScalar& w) {
TinyQuaternion res;
res.setValue(x, y, z, w);
return res;
}
inline TinyQuaternion operator-() const {
const TinyQuaternion& q2 = *this;
return TinyQuaternion::create(-q2.getX(), -q2.getY(), -q2.getZ(),
-q2.getW());
}
TinyQuaternion inversed() const {
return TinyQuaternion::create(-m_x, -m_y, -m_z, m_w);
}
void adj_inversed(const TinyQuaternion& R, TinyQuaternion& Rori) {
Rori.m_x += -R.m_x;
Rori.m_y += -R.m_y;
Rori.m_z += -R.m_z;
Rori.m_w += R.m_w;
}
TinyScalar dot(const TinyQuaternion& q) const {
return m_x * q.getX() + m_y * q.getY() + m_z * q.getZ() + m_w * q.getW();
}
TinyScalar length2() const { return dot(*this); }
TinyQuaternion& operator*=(const TinyQuaternion& q) {
setValue(m_w * q.getX() + m_x * q.m_w + m_y * q.getZ() - m_z * q.getY(),
m_w * q.getY() + m_y * q.m_w + m_z * q.getX() - m_x * q.getZ(),
m_w * q.getZ() + m_z * q.m_w + m_x * q.getY() - m_y * q.getX(),
m_w * q.m_w - m_x * q.getX() - m_y * q.getY() - m_z * q.getZ());
return *this;
}
inline TinyQuaternion operator*(const TinyScalar& s) const {
return TinyQuaternion::create(getX() * s, getY() * s, getZ() * s,
getW() * s);
}
inline TinyQuaternion operator/(const TinyScalar& s) const {
return TinyQuaternion::create(getX() / s, getY() / s, getZ() / s,
getW() / s);
}
TinyQuaternion& operator*=(const TinyScalar& s) {
m_x = m_x * s;
m_y = m_y * s;
m_z = m_z * s;
m_w = m_w * s;
return *this;
}
TinyQuaternion& operator/=(const TinyScalar& s) {
assert(TinyConstants::getBool(
s != TinyConstants::zero()));
return *this *= (TinyConstants::one() / s);
}
inline TinyQuaternion& operator+=(const TinyQuaternion& q) {
m_x = m_x + q.getX();
m_y = m_y + q.getY();
m_z = m_z + q.getZ();
m_w = m_w + q.getW();
return *this;
}
inline TinyScalar& operator[](int i) {
switch (i) {
case 0: {
return m_x;
}
case 1: {
return m_y;
}
case 2: {
return m_z;
}
case 3: {
return m_w;
}
default: {}
}
assert(0);
return m_x;
}
inline const TinyScalar& operator[](int i) const {
switch (i) {
case 0: {
return m_x;
}
case 1: {
return m_y;
}
case 2: {
return m_z;
}
case 3: {
return m_w;
}
default: {}
}
assert(0);
return m_x;
}
inline TinyScalar length() const {
TinyScalar res = (*this).dot(*this);
res = TinyConstants::sqrt1(res);
return res;
}
inline TinyVector3 rotate(const TinyVector3& v) const {
TinyQuaternion q = (*this) * v;
q = q * this->inversed();
return TinyVector3::create(q.m_x, q.m_y, q.m_z);
}
void adj_rotate(const TinyVector3 &Rans,const TinyVector3& v,
TinyQuaternion& Rthis, TinyVector3& Rv) {
TinyQuaternion q = (*this) * v;
TinyQuaternion this_inv = this->inversed();
TinyQuaternion Rq_, Rthis_inv;
Rq_.set_zero(); Rthis_inv.set_zero();
TinyQuaternion Rq;
Rq.set_zero();
Rq.m_x = Rans[0];Rq.m_y = Rans[1];Rq.m_z = Rans[2];
q.adj_mul_q(Rq, this_inv, Rq_, Rthis_inv);
this->adj_inversed(Rthis_inv, Rthis);
this->adj_mul_vec(Rq_, v, Rthis, Rv);
// Rv.print("Rv");
// Rq_.print("Rq_");
// Rq.print("Rq");
// q.print("q");
// this_inv.print("this_inv");
}
void setRotation(const TinyVector3& axis, const TinyScalar& _angle) {
TinyScalar d = axis.length();
TinyScalar s = TinyConstants::sin1(_angle * TinyConstants::half()) / d;
setValue(axis.x() * s, axis.y() * s, axis.z() * s,
TinyConstants::cos1(_angle * TinyConstants::half()));
}
void adj_setRotation(const TinyVector3& axis, const TinyScalar& _angle,
TinyScalar& R_angle) {
TinyScalar d = axis.length();
TinyScalar s = TinyConstants::sin1(_angle * TinyConstants::half()) / d;
TinyScalar Rs = TinyConstants::zero();
TinyScalar s1_2 = TinyConstants::half();
Rs += m_x*axis.x() + m_y*axis.y() + m_z*axis.z();
R_angle += - TinyConstants::sin1(_angle * s1_2)* s1_2 * m_w
+ TinyConstants::cos1(_angle * s1_2)* s1_2*Rs / d;
}
/**@brief Set the quaternion using euler angles
* @param yaw Angle around Z
* @param pitch Angle around Y
* @param roll Angle around X */
void set_euler_rpy(const TinyVector3& rpy) {
const TinyScalar& yaw_z = rpy[0];
const TinyScalar& pitch_y = rpy[1];
const TinyScalar& roll_x = rpy[2];
TinyScalar halfYaw = TinyScalar(yaw_z) * TinyConstants::half();
TinyScalar halfPitch = TinyScalar(pitch_y) * TinyConstants::half();
TinyScalar halfRoll = TinyScalar(roll_x) * TinyConstants::half();
TinyScalar cy = TinyConstants::cos1(halfYaw);
TinyScalar sy = TinyConstants::sin1(halfYaw);
TinyScalar cp = TinyConstants::cos1(halfPitch);
TinyScalar sp = TinyConstants::sin1(halfPitch);
TinyScalar cr = TinyConstants::cos1(halfRoll);
TinyScalar sr = TinyConstants::sin1(halfRoll);
setValue(sr * cp * cy - cr * sp * sy, // x
cr * sp * cy + sr * cp * sy, // y
cr * cp * sy - sr * sp * cy, // z
cr * cp * cy + sr * sp * sy); // formerly yzx
}
TinyVector3 get_euler_rpy() const {
// Adapted from
// https://en.wikipedia.org/wiki/Conversion_between_quaternions_and_Euler_angles
TinyVector3 rpy;
// roll (x-axis rotation)
TinyScalar sinr_cosp = TinyConstants::two() * (m_w * m_x + m_y * m_z);
TinyScalar cosr_cosp =
TinyConstants::one() - TinyConstants::two() * (m_x * m_x + m_y * m_y);
rpy[2] = TinyConstants::atan2(sinr_cosp, cosr_cosp);
// pitch (y-axis rotation)
TinyScalar sinp = TinyConstants::two() * (m_w * m_y - m_z * m_x);
if (TinyConstants::abs(sinp) >= TinyConstants::one()) {
// use 90 degrees if out of range
rpy[1] = TinyConstants::copysign(TinyConstants::half_pi(), sinp);
} else {
rpy[1] = TinyConstants::asin(sinp);
}
// yaw (z-axis rotation)
TinyScalar siny_cosp = TinyConstants::two() * (m_w * m_z + m_x * m_y);
TinyScalar cosy_cosp =
TinyConstants::one() - TinyConstants::two() * (m_y * m_y + m_z * m_z);
rpy[0] = TinyConstants::atan2(siny_cosp, cosy_cosp);
return rpy;
}
#ifdef USE_EIGEN
void set_euler_rpy2(const TinyVector3& rpy) {
Eigen::Quaternion<TinyScalar> quat(
Eigen::AngleAxis<TinyScalar>(rpy.x(),
Eigen::Matrix<TinyScalar, 3, 1>::UnitX()) *
Eigen::AngleAxis<TinyScalar>(rpy.y(),
Eigen::Matrix<TinyScalar, 3, 1>::UnitY()) *
Eigen::AngleAxis<TinyScalar>(rpy.z(),
Eigen::Matrix<TinyScalar, 3, 1>::UnitZ()));
setValue(quat.x(), quat.y(), quat.z(), quat.w()); // formerly yzx
}
#endif // USE_EIGEN
TinyVector3 get_euler_rpy2() const {
// Adapted from ETHZ kindr
// First convert quaternion to rotation matrix
TinyScalar m00, m01, m02;
TinyScalar m10, m11, m12;
TinyScalar m20, m21, m22;
const TinyScalar tx = TinyConstants::two() * m_x;
const TinyScalar ty = TinyConstants::two() * m_y;
const TinyScalar tz = TinyConstants::two() * m_z;
const TinyScalar twx = tx * m_w;
const TinyScalar twy = ty * m_w;
const TinyScalar twz = tz * m_w;
const TinyScalar txx = tx * m_x;
const TinyScalar txy = ty * m_x;
const TinyScalar txz = tz * m_x;
const TinyScalar tyy = ty * m_y;
const TinyScalar tyz = tz * m_y;
const TinyScalar tzz = tz * m_z;
m00 = TinyConstants::one() - (tyy + tzz);
m01 = txy - twz;
m02 = txz + twy;
m10 = txy + twz;
m11 = TinyConstants::one() - (txx + tzz);
m12 = tyz - twx;
m20 = txz - twy;
m21 = tyz + twx;
m22 = TinyConstants::one() - (txx + tyy);
// Next extract Euler angles
TinyVector3 rpy;
// const Index i = a0;
// const Index j = (a0 + 1 + odd)%3;
// const Index k = (a0 + 2 - odd)%3;
rpy[0] = TinyConstants::atan2(m12, m22);
TinyScalar c2 = TinyConstants::sqrt1(m00 * m00 + m01 * m01);
if (rpy[0] > TinyConstants::zero()) {
rpy[0] -= TinyConstants::pi();
rpy[1] = -TinyConstants::atan2(-m02, -c2);
} else {
rpy[1] = -TinyConstants::atan2(-m02, c2);
}
TinyScalar s1 = TinyConstants::sin1(rpy[0]);
TinyScalar c1 = TinyConstants::cos1(rpy[0]);
rpy[0] = -rpy[0];
rpy[2] = -TinyConstants::atan2(s1 * m20 - c1 * m10, c1 * m11 - s1 * m21);
return rpy;
}
TinyQuaternion& normalize() { return *this /= length(); }
TinyQuaternion adj_normalize(TinyQuaternion& adj_R) {
TinyQuaternion res;
TinyScalar l = length();
res.set_zero();
res += adj_R / l;
res += (*this) * (-adj_R.dot(*this) / l / l / l);
return res;
}
void adj_mul_q(const TinyQuaternion& Rq, const TinyQuaternion& q2,
TinyQuaternion& Rq1, TinyQuaternion& Rq2) {
TinyQuaternion& q1 = (*this);
Rq1.m_x += Rq.m_x*q2.m_w - Rq.m_y*q2.m_z + Rq.m_z*q2.m_y - Rq.m_w*q2.m_x;
Rq1.m_y += Rq.m_x*q2.m_z + Rq.m_y*q2.m_w - Rq.m_z*q2.m_x - Rq.m_w*q2.m_y;
Rq1.m_z += -Rq.m_x*q2.m_y+ Rq.m_y*q2.m_x + Rq.m_z*q2.m_w - Rq.m_w*q2.m_z;
Rq1.m_w += Rq.m_x*q2.m_x + Rq.m_y*q2.m_y + Rq.m_z*q2.m_z + Rq.m_w*q2.m_w;
Rq2.m_x += Rq.m_x*q1.m_w + Rq.m_y*q1.m_z - Rq.m_z*q1.m_y - Rq.m_w*q1.m_x;
Rq2.m_y += -Rq.m_x*q1.m_z+ Rq.m_y*q1.m_w + Rq.m_z*q1.m_x - Rq.m_w*q1.m_y;
Rq2.m_z += Rq.m_x*q1.m_y - Rq.m_y*q1.m_x + Rq.m_z*q1.m_w - Rq.m_w*q1.m_z;
Rq2.m_w += Rq.m_x*q1.m_x + Rq.m_y*q1.m_y + Rq.m_z*q1.m_z + Rq.m_w*q1.m_w;
}
void adj_mul_vec(const TinyQuaternion& Rq, const TinyVector3& v,
TinyQuaternion& Rq2, TinyVector3& Rv) {
TinyQuaternion& q = (*this);
Rq2.m_x +=-Rq.m_y * v.m_z + Rq.m_z * v.m_y - Rq.m_w * v.m_x;
Rq2.m_y += Rq.m_x * v.m_z - Rq.m_z * v.m_x - Rq.m_w * v.m_y;
Rq2.m_z +=-Rq.m_x * v.m_y + Rq.m_y * v.m_x - Rq.m_w * v.m_z;
Rq2.m_w += Rq.m_x * v.m_x + Rq.m_y * v.m_y + Rq.m_z * v.m_z;
Rv.m_x += Rq.m_x*q.m_w + Rq.m_y*q.m_z - Rq.m_z*q.m_y - Rq.m_w*q.m_x;
Rv.m_y +=-Rq.m_x*q.m_z + Rq.m_y*q.m_w + Rq.m_z*q.m_x - Rq.m_w*q.m_y;
Rv.m_z += Rq.m_x*q.m_y - Rq.m_y*q.m_x + Rq.m_z*q.m_w - Rq.m_w*q.m_z;
}
void adj_vec_mul(const TinyQuaternion& Rq, const TinyVector3& v,
TinyQuaternion& Rq2, TinyVector3& Rv) {
TinyQuaternion& q = (*this);
Rq2.m_x += Rq.m_y * v.m_z - Rq.m_z * v.m_y - Rq.m_w * v.m_x;
Rq2.m_y +=-Rq.m_x * v.m_z + Rq.m_z * v.m_x - Rq.m_w * v.m_y;
Rq2.m_z += Rq.m_x * v.m_y - Rq.m_y * v.m_x - Rq.m_w * v.m_z;
Rq2.m_w += Rq.m_x * v.m_x + Rq.m_y * v.m_y + Rq.m_z * v.m_z;
Rv.m_x += Rq.m_x*q.m_w - Rq.m_y*q.m_z + Rq.m_z*q.m_y - Rq.m_w*q.m_x;
Rv.m_y += Rq.m_x*q.m_z + Rq.m_y*q.m_w - Rq.m_z*q.m_x - Rq.m_w*q.m_y;
Rv.m_z +=-Rq.m_x*q.m_y + Rq.m_y*q.m_x + Rq.m_z*q.m_w - Rq.m_w*q.m_z;
}
void print(const std::string& label) const {
printf("%s (xyzw): \t", label.c_str());
printf("%.6f %.6f %.6f %.6f\n", TinyConstants::getDouble(m_x),
TinyConstants::getDouble(m_y), TinyConstants::getDouble(m_z),
TinyConstants::getDouble(m_w));
}
};
template <typename TinyScalar, typename TinyConstants>
inline TinyQuaternion<TinyScalar, TinyConstants> operator*(
const TinyQuaternion<TinyScalar, TinyConstants>& q1,
const TinyQuaternion<TinyScalar, TinyConstants>& q2) {
return TinyQuaternion<TinyScalar, TinyConstants>::create(
q1.getW() * q2.getX() + q1.getX() * q2.getW() + q1.getY() * q2.getZ() -
q1.getZ() * q2.getY(),
q1.getW() * q2.getY() + q1.getY() * q2.getW() + q1.getZ() * q2.getX() -
q1.getX() * q2.getZ(),
q1.getW() * q2.getZ() + q1.getZ() * q2.getW() + q1.getX() * q2.getY() -
q1.getY() * q2.getX(),
q1.getW() * q2.getW() - q1.getX() * q2.getX() - q1.getY() * q2.getY() -
q1.getZ() * q2.getZ());
}
template <typename TinyScalar, typename TinyConstants>
inline TinyQuaternion<TinyScalar, TinyConstants> operator*(
const TinyQuaternion<TinyScalar, TinyConstants>& q,
const TinyVector3<TinyScalar, TinyConstants>& w) {
return TinyQuaternion<TinyScalar, TinyConstants>::create(
q.getW() * w.getX() + q.getY() * w.getZ() - q.getZ() * w.getY(),
q.getW() * w.getY() + q.getZ() * w.getX() - q.getX() * w.getZ(),
q.getW() * w.getZ() + q.getX() * w.getY() - q.getY() * w.getX(),
-q.getX() * w.getX() - q.getY() * w.getY() - q.getZ() * w.getZ());
}
template <typename TinyScalar, typename TinyConstants>
inline TinyQuaternion<TinyScalar, TinyConstants> operator*(
const TinyVector3<TinyScalar, TinyConstants>& w,
const TinyQuaternion<TinyScalar, TinyConstants>& q) {
return TinyQuaternion<TinyScalar, TinyConstants>::create(
w.getX() * q.getW() + w.getY() * q.getZ() - w.getZ() * q.getY(),
w.getY() * q.getW() + w.getZ() * q.getX() - w.getX() * q.getZ(),
w.getZ() * q.getW() + w.getX() * q.getY() - w.getY() * q.getX(),
-w.getX() * q.getX() - w.getY() * q.getY() - w.getZ() * q.getZ());
}
#endif // TINY_QUATERNION_H