-
Notifications
You must be signed in to change notification settings - Fork 6
/
DualQuaternion.h
351 lines (282 loc) · 9.56 KB
/
DualQuaternion.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
/*
This file is part of the Geometry library.
Copyright (C) 2007-2012 Benjamin Eikel <[email protected]>
Copyright (C) 2007-2012 Claudius Jähn <[email protected]>
Copyright (C) 2007-2012 Ralf Petring <[email protected]>
This library is subject to the terms of the Mozilla Public License, v. 2.0.
You should have received a copy of the MPL along with this library; see the
file LICENSE. If not, you can obtain one at http://mozilla.org/MPL/2.0/.
*/
#ifndef GEOMETRY_DUALQUATERNION_H
#define GEOMETRY_DUALQUATERNION_H
#include "Interpolation.h"
#include "Quaternion.h"
#include "Matrix4x4.h"
#include "Vec3.h"
namespace Geometry {
template <typename T_>
/**
* Class used to represent a rotation and positoin replacement as dual numbers based on quaternions.
* Currently does not support scaling in any form!
*
* Dual Quaternions allow transformation and deformation of an mesh without artefacts. Such gimbal Lock
* or texture artefacts caused by false interpolation and / or transformation.
*
* The dual part describes the rotation and the non dualpart the position replacement.
*
* @author Lukas Kopecki
* @date 2012-08-07
*/
class _DualQuaternion {
public:
using value_t = T_;
using vec3_t = _Vec3<value_t>;
/*
* [ Constructor ]
*/
_DualQuaternion() {
setRotation(0.0, 0.0, 0.0, 1.0);
setTranslation(0.0, 0.0, 0.0, 1.0);
}
_DualQuaternion(value_t _rx, value_t _ry, value_t _rz, value_t _rw, value_t _tx, value_t _ty, value_t _tz,
value_t _tw) {
setRotation(_rx, _ry, _rz, _rw);
setTranslation(_tx, _ty, _tz, _tw);
}
_DualQuaternion(const Quaternion & rotation, const vec3_t & translation) {
set(rotation, translation);
}
_DualQuaternion(const Quaternion & rotation, const Quaternion & translation) {
setRotation(rotation.x(), rotation.y(), rotation.z(), rotation.w());
setTranslation(translation.x(), translation.y(), translation.z(), translation.w());
}
_DualQuaternion(const _DualQuaternion & src) {
setRotation(src.getRotation());
setTranslation(src.getTranslation());
}
/*
* [ getter & setter ]
*/
const value_t & rX() const {
return rot[0];
}
const value_t & rY() const {
return rot[1];
}
const value_t & rZ() const {
return rot[2];
}
const value_t & rW() const {
return rot[3];
}
const value_t & tX() const {
return trans[0];
}
const value_t & tY() const {
return trans[1];
}
const value_t & tZ() const {
return trans[2];
}
const value_t & tW() const {
return trans[3];
}
void setRX(value_t _rx) {
rot[0] = _rx;
}
void setRY(value_t _ry) {
rot[1] = _ry;
}
void setRZ(value_t _rz) {
rot[2] = _rz;
}
void setRW(value_t _rw) {
rot[3] = _rw;
}
void setTX(value_t _tx) {
trans[0] = _tx;
}
void setTY(value_t _ty) {
trans[1] = _ty;
}
void setTZ(value_t _tz) {
trans[2] = _tz;
}
void setTW(value_t _tw) {
trans[3] = _tw;
}
void setRotation(value_t _x, value_t _y, value_t _z, value_t _w) {
setRX(_x);
setRY(_y);
setRZ(_z);
setRW(_w);
}
void setRotation(const Quaternion & _rot) {
setRotation(_rot.x(), _rot.y(), _rot.z(), _rot.w());
}
const Quaternion getRotation() const {
return Quaternion(rot[0], rot[1], rot[2], rot[3]);
}
void setTranslation(value_t _x, value_t _y, value_t _z, value_t _w) {
setTX(_x);
setTY(_y);
setTZ(_z);
setTW(_w);
}
void setTranslation(const Quaternion & _trans) {
setTranslation(_trans.x(), _trans.y(), _trans.z(), _trans.w());
}
const Quaternion getTranslation() const {
return Quaternion(trans[0], trans[1], trans[2], trans[3]);
}
const vec3_t getTranslationAsVec3() const {
return vec3_t((2.0 * (-trans[3] * rot[0] + trans[0] * rot[3] - trans[1] * rot[2] + trans[2] * rot[1])),
(2.0 * (-trans[3] * rot[1] + trans[0] * rot[2] + trans[1] * rot[3] - trans[2] * rot[0])),
(2.0 * (-trans[3] * rot[2] - trans[0] * rot[1] + trans[1] * rot[0] + trans[2] * rot[3])));
}
const _DualQuaternion normalizeRotation() const {
value_t magn = 1 / std::sqrt(getRotation().dot(getRotation()));
return _DualQuaternion(getRotation() * magn, getTranslation() * magn);
}
const _DualQuaternion normalizeTranslation() const {
value_t magn = 1.0 / std::sqrt(getRotation().dot(getRotation()));
return _DualQuaternion(getRotation(), getTranslation()
- getRotation().operator*((getRotation().dot(getTranslation())) * magn));
}
const _DualQuaternion normalize() const {
return normalizeRotation().normalizeTranslation();
}
const value_t dotRotation(const Quaternion & rotation) const {
return getRotation().dot(rotation);
}
const value_t dotTranslation(const Quaternion & translation) const {
return getTranslation().dot(translation);
}
const value_t dot(const _DualQuaternion & dua) const {
return dotRotation(dua.getRotation()) + dotTranslation(dua.getTranslation());
}
_DualQuaternion conjugate() const {
return _DualQuaternion(getRotation().conjugate(), getTranslation().conjugate());
}
/* source:
* http://isg.cs.tcd.ie/kavanl/dq/dqconv.c
* Converts a unit quaternion and a translation vector into one dual quaternion.
*/
void set(const Quaternion & q, const vec3_t & t) {
// regular quaternion (just copy the non-dual part):
rot[0] = q.x();
rot[1] = q.y();
rot[2] = q.z();
rot[3] = q.w();
// translation vector:
trans[0] = 0.5 * (t.x() * q.w() + t.y() * q.z() - t.z() * q.y());
trans[1] = 0.5 * (-t.x() * q.z() + t.y() * q.w() + t.z() * q.x());
trans[2] = 0.5 * (t.x() * q.y() - t.y() * q.x() + t.z() * q.w());
trans[3] = -0.5 * (t.x() * q.x() + t.y() * q.y() + t.z() * q.z());
}
/* source:
* http://isg.cs.tcd.ie/kavanl/dq/dqconv.c
* Converts one dual quaternion into one unit quaternion and one vector.
*/
void getQuaternionAndVec3(Quaternion & q, vec3_t & t) const {
// regular quaternion (just copy the non-dual part):
q.set(rot[0], rot[1], rot[2], rot[3]);
// translation vector:
t.setX(2.0 * (-trans[3] * rot[0] + trans[0] * rot[3] - trans[1] * rot[2] + trans[2] * rot[1]));
t.setY(2.0 * (-trans[3] * rot[1] + trans[0] * rot[2] + trans[1] * rot[3] - trans[2] * rot[0]));
t.setZ(2.0 * (-trans[3] * rot[2] - trans[0] * rot[1] + trans[1] * rot[0] + trans[2] * rot[3]));
}
/**
* [ Basic functions ]
*/
const value_t lengthRotation() const {
return std::sqrt(length2Rotation());
}
const value_t length2Rotation() const {
return rot[0] * rot[0] + rot[1] * rot[1] + rot[2] * rot[2] + rot[3] * rot[3];
}
const value_t lengthTranslation() const {
return std::sqrt(length2Rotation());
}
const value_t length2Translation() const {
return trans[0] * trans[0] + trans[1] * trans[1] + trans[2] * trans[2] + trans[3] * trans[3];
}
const value_t length() const {
value_t l = getRotation().length();
return l + getRotation().dot(getTranslation()) / l;
}
_DualQuaternion & operator=(const _DualQuaternion & src) {
if (this != &src) {
setRotation(src.getRotation());
setTranslation(src.getTranslation());
}
return *this;
}
const _DualQuaternion operator*(const value_t factor) const {
return _DualQuaternion(rot[0] * factor, rot[1] * factor, rot[2] * factor, rot[3] * factor, trans[0] * factor,
trans[1] * factor, trans[2] * factor, trans[3] * factor);
}
const _DualQuaternion operator/(const value_t factor) const {
return (factor != 0.0) ? operator*(1.0 / factor) : _DualQuaternion::identity();
}
const _DualQuaternion operator+(const _DualQuaternion & dq) const {
return _DualQuaternion(rX() + dq.rX(), rY() + dq.rY(), rZ() + dq.rZ(), rW() + dq.rW(), tX() + dq.tX(),
tY() + dq.tY(), tZ() + dq.tZ(), tW() + dq.tW());
}
const _Matrix4x4<value_t> toMatrix() const {
Quaternion rotation = Quaternion(rot[0], rot[1], rot[2], rot[3]);
if (rotation.length() != 0)
rotation /= rotation.length();
_Matrix4x4<value_t> mat(rotation.toMatrix());
vec3_t v = getTranslationAsVec3();
mat[3] = v.x();
mat[7] = v.y();
mat[11] = v.z();
return mat;
}
void setFromMatrix(const _Matrix4x4<value_t> & matrix) {
_DualQuaternion tmp = convertFromMatrix(matrix);
set(tmp.getRotation(), tmp.getTranslationAsVec3());
}
/*
* [ Static ]
*/
static _DualQuaternion identity() {
return _DualQuaternion(0, 0, 0, 1, 0, 0, 0, 1);
}
static _DualQuaternion dualQuaternionLinearInterpolation(const _DualQuaternion & dq1, const _DualQuaternion & dq2,
const value_t factor, bool takeShortestArc = false) {
value_t usedFactor = factor;
if (takeShortestArc) {
if (dq1.dot(dq2) < 0.0)
usedFactor = -factor;
}
_DualQuaternion idq((dq1 * (1.0 - usedFactor)) + (dq2 * usedFactor));
value_t length = idq.length();
idq.normalize();
return (length != 0) ? idq / idq.length() : _DualQuaternion::identity();
}
static _DualQuaternion convertFromMatrix(const _Matrix4x4<value_t> & matrix) {
Quaternion rotation;
vec3_t translation(matrix.at(3), matrix.at(7), matrix.at(11));
_Matrix3x3<value_t> rotMat(matrix.at(0), matrix.at(1), matrix.at(2), matrix.at(4), matrix.at(5), matrix.at(6),
matrix.at(8), matrix.at(9), matrix.at(10));
rotation = Quaternion::matrixToQuaternion(rotMat);
return _DualQuaternion(rotation, translation);
}
/*
* [ Serialization ]
*/
friend std::ostream & operator<<(std::ostream & out, const _DualQuaternion & dq) {
return out << dq.rX() << ' ' << dq.rY() << ' ' << dq.rZ() << ' ' << dq.rW() << ' ' << dq.tX() << ' ' << dq.tY()
<< ' ' << dq.tZ() << ' ' << dq.tW();
}
private:
value_t rot[4]; // dual part representing rotation
value_t trans[4]; // non dual part for position displacement
};
using DualQuaternion = _DualQuaternion<float>;
using DualQuaternionf = _DualQuaternion<float>;
using DualQuaterniond = _DualQuaternion<double>;
}
#endif /* GEOMETRY_DUALQUATERNION_H */