-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathdualquat.hpp
429 lines (324 loc) · 12.4 KB
/
dualquat.hpp
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
#ifndef DUALQUAT_HPP
#define DUALQUAT_HPP
#include <iostream>
#include <limits>
#include <cmath>
#include <assert.h>
#define EPS(value_t) (std::numeric_limits<value_t>::epsilon())
template <class value_t>
struct quat {
value_t w, x, y, z;
quat() : w(1), x(0), y(0), z(0) {}
quat(value_t w_,
value_t x_,
value_t y_,
value_t z_) : w(w_), x(x_) , y(y_), z(z_) {}
void print () const {
std::cout << "(" << w << "," << x
<< "," << y << "," << z
<< ")" << std::endl;
}
quat<value_t> operator+(const quat<value_t>& other) const {
return quat<value_t>(w+other.w, x+other.x,
y+other.y, z+other.z);
}
quat<value_t>& operator+=(const quat<value_t>& other) {
w += other.w; x += other.x; y += other.y; z += other.z;
return *this;
}
quat<value_t> operator-(const quat<value_t>& other) const {
return quat<value_t>(w-other.w, x-other.x,
y-other.y, z-other.z);
}
quat<value_t>& operator-=(const quat<value_t>& other) {
w -= other.w; x -= other.x; y -= other.y; z -= other.z;
return *this;
}
quat<value_t> operator^(const quat<value_t>& other) const {
return quat<value_t>(w*other.w-x*other.x-y*other.y-z*other.z,
w*other.x+other.w*x+y*other.z-z*other.y,
w*other.y+other.w*y+z*other.x-x*other.z,
w*other.z+other.w*z+x*other.y-y*other.x);
}
quat<value_t>& operator^=(const quat<value_t>& other) {
*this = (*this)^other;
return *this;
}
quat<value_t> operator*(const value_t alpha) const {
return quat<value_t>(w*alpha, x*alpha,
y*alpha, z*alpha);
}
quat<value_t> operator/(const value_t alpha) const {
return quat<value_t>(w/alpha, x/alpha,
y/alpha, z/alpha);
}
quat<value_t> N() const {
const value_t rho = std::sqrt(w*w+x*x+y*y+z*z);
return quat<value_t>(w/rho, x/rho, y/rho, z/rho);
}
quat<value_t> C() const {
return quat<value_t>(w, -x, -y, -z);
}
quat<value_t> I() const {
const value_t rho2 = w*w+x*x+y*y+z*z;
return quat<value_t>(w/rho2, -x/rho2, -y/rho2, -z/rho2);
}
quat<value_t> exp () const {
assert(w*w < EPS(value_t));
value_t dst = std::sqrt(x*x+y*y+z*z+EPS(value_t));
value_t fac = std::sin(dst)/dst;
return quat<value_t>(std::cos(dst), x*fac, y*fac, z*fac);
}
quat<value_t> numexp (value_t eps = EPS(value_t)) const {
quat<value_t> pow;
quat<value_t> sum;
size_t i = 1;
while (pow.dot(pow) > eps) {
pow ^= (*this)/i++;
sum += pow;
}
return sum;
}
quat<value_t> log () const {
assert(isunit());
const value_t inv = 1.0/std::sqrt(x*x+y*y+z*z+EPS(value_t));
const value_t fac = std::acos(w > 1 ? 1 : w < -1 ? -1 : w)*inv;
return quat<value_t> (0, x*fac, y*fac, z*fac);
}
value_t dot (const quat<value_t> other) const {
return w*other.w+x*other.x+y*other.y+z*other.z;
}
value_t eucnorm () const {
return dot(*this);
}
value_t lognorm () const {
const auto& generator = log();
return generator.dot(generator);
}
value_t eucdist (const quat<value_t>& other) const {
if (dot(other) < 0.0) {
const auto& difference = (*this)+other;
return difference.dot(difference);
}
const auto& difference = (*this)-other;
return difference.dot(difference);
}
value_t logdist (const quat<value_t>& other) const {
const auto& difference = ((*this)^(other.C())).log();
return difference.dot(difference);
}
value_t baddist (const quat<value_t>& other) const {
const bool I_know_what_I_do = false;
assert(I_know_what_I_do);
if (dot(other) < 0.0) {
const auto& difference = log()-(other*(-1.0)).log();
return difference.dot(difference);
}
const auto& difference = log()-other.log();
return difference.dot(difference);
}
bool isunit () const {
const value_t residue = w*w+x*x+y*y+z*z-1.0;
return residue*residue < EPS(value_t);
}
};
template <class value_t>
struct dualquat {
value_t w, x, y, z, W, X, Y, Z;
dualquat() : w(1) , x(0), y(0), z(0), W(0), X(0), Y(0), Z(0) {};
dualquat(value_t w_,
value_t x_,
value_t y_,
value_t z_,
value_t W_,
value_t X_,
value_t Y_,
value_t Z_) : w(w_), x(x_), y(y_), z(z_),
W(W_), X(X_), Y(Y_), Z(Z_) {}
dualquat(const quat<value_t>& real, const quat<value_t>& dual) {
w = real.w; x = real.x; y = real.y; z = real.z;
W = dual.w; X = dual.x; Y = dual.y; Z = dual.z;
}
quat<value_t> real () const {
return quat<value_t>(w, x, y, z);
}
quat<value_t> dual () const {
return quat<value_t>(W, X, Y, Z);
}
void print () const {
std::cout << "(" << w << "," << x
<< "," << y << "," << z
<< "," << W << "," << X
<< "," << Y << "," << Z
<< ")" << std::endl;
}
dualquat<value_t> operator+(const dualquat<value_t>& other) const {
return dualquat<value_t>(w+other.w, x+other.x,
y+other.y, z+other.z,
W+other.W, X+other.X,
Y+other.Y, Z+other.Z);
}
dualquat<value_t>& operator+=(const dualquat<value_t>& other) {
w += other.w; x += other.x; y += other.y; z += other.z;
W += other.W; X += other.X; Y += other.Y; Z += other.Z;
return *this;
}
dualquat<value_t> operator-(const dualquat<value_t>& other) const {
return dualquat<value_t>(w-other.w, x-other.x,
y-other.y, z-other.z,
W-other.W, X-other.X,
Y-other.Y, Z-other.Z);
}
dualquat<value_t>& operator-=(const dualquat<value_t>& other) {
w -= other.w; x -= other.x; y -= other.y; z -= other.z;
W -= other.W; X -= other.X; Y -= other.Y; Z -= other.Z;
return *this;
}
// TODO: expand this
dualquat<value_t> operator^(const dualquat<value_t>& other) const {
const auto& a = real();
const auto& A = dual();
const auto& b = other.real();
const auto& B = other.dual();
return dualquat<value_t>(a^b, (a^B)+(A^b));
}
dualquat<value_t>& operator^=(const dualquat<value_t>& other) {
*this = (*this)^other;
return *this;
}
dualquat<value_t> operator*(const value_t alpha) const {
return dualquat<value_t>(w*alpha, x*alpha,
y*alpha, z*alpha,
W*alpha, X*alpha,
Y*alpha, Z*alpha);
}
dualquat<value_t> operator/(const value_t alpha) const {
return dualquat<value_t>(w/alpha, x/alpha,
y/alpha, z/alpha,
W/alpha, X/alpha,
Y/alpha, Z/alpha);
}
dualquat<value_t> N() const {
const value_t qq = w*w+x*x+y*y+z*z+EPS(value_t);
const value_t qQ = w*W+x*X+y*Y+z*Z;
const value_t invqq = 1.0/qq;
const value_t invsq = 1.0/std::sqrt(qq);
const value_t alpha = qQ*invqq*invqq;
return dualquat<value_t>(w*invsq, x*invsq,
y*invsq, z*invsq,
W*invqq-w*alpha, X*invqq-x*alpha,
Y*invqq-y*alpha, Z*invqq-z*alpha);
}
dualquat<value_t> C() const {
return dualquat<value_t>(w, -x, -y, -z, W, -X, -Y, -Z);
}
dualquat<value_t> D() const {
return dualquat<value_t>(w, x, y, z, -W, -X, -Y, -Z);
}
dualquat<value_t> I() const {
const value_t qq = w*w+x*x+y*y+z*z;
const value_t qQ = w*W+x*X+y*Y+z*Z;
const value_t invqq = 1.0/qq;
const value_t alpha = 2.0*qQ*invqq*invqq;
return dualquat<value_t>(w*invqq, -x*invqq,
-y*invqq, -z*invqq,
W*invqq-w*alpha, -X*invqq-x*alpha,
-Y*invqq-y*alpha, -Z*invqq-z*alpha);
}
dualquat<value_t> exp() const {
assert(w*w < EPS(value_t) && W*W < EPS(value_t));
const value_t theta = 2.0*std::sqrt(x*x+y*y+z*z+EPS(value_t));
const value_t invvv = 2.0/theta;
const value_t lx = x*invvv;
const value_t ly = y*invvv;
const value_t lz = z*invvv;
const value_t pitch = 2.0*(lx*X+ly*Y+lz*Z);
const value_t mx = (2.0*X-pitch*lx)/theta;
const value_t my = (2.0*Y-pitch*ly)/theta;
const value_t mz = (2.0*Z-pitch*lz)/theta;
assert((lx*mx+ly*my+lz*mz)*(lx*mx+ly*my+lz*mz) < EPS(value_t));
const value_t cost2 = std::cos(0.5*theta);
const value_t sint2 = std::sin(0.5*theta);
const value_t alpha = 0.5*pitch*cost2;
return dualquat<value_t> (cost2,
sint2*lx,
sint2*ly,
sint2*lz,
-0.5*pitch*sint2,
sint2*mx+alpha*lx,
sint2*my+alpha*ly,
sint2*mz+alpha*lz);
}
dualquat<value_t> numexp (value_t eps = EPS(value_t)) const {
dualquat<value_t> pow;
dualquat<value_t> sum;
size_t i = 1;
while (pow.dot(pow) > eps) {
pow ^= (*this)/i++;
sum += pow;
}
return sum;
}
dualquat<value_t> log () const {
assert(isunit());
const value_t theta = 2.0*std::acos(w > 1 ? 1 : w < -1 ? -1 : w);
const value_t invvv = 0.5/std::sqrt(x*x+y*y+z*z+EPS(value_t));
const value_t pitch = -4.0*W*invvv;
const value_t alpha = pitch*w;
const value_t lx = x*invvv;
const value_t ly = y*invvv;
const value_t lz = z*invvv;
const value_t mx = (X-lx*alpha)*invvv;
const value_t my = (Y-ly*alpha)*invvv;
const value_t mz = (Z-lz*alpha)*invvv;
assert((lx*mx+ly*my+lz*mz)*(lx*mx+ly*my+lz*mz) < EPS(value_t));
return dualquat<value_t> (0, theta*lx,
theta*ly,
theta*lz,
0, (pitch*lx+theta*mx),
(pitch*ly+theta*my),
(pitch*lz+theta*mz));
}
value_t dot(const dualquat<value_t> other) const {
return w*other.w+x*other.x+y*other.y+z*other.z+
W*other.W+X*other.X+Y*other.Y+Z*other.Z;
}
value_t eucnorm () const {
return dot(*this);
}
value_t lognorm () const {
const auto& generator = log();
return generator.dot(generator);
}
value_t eucdist (const dualquat<value_t>& other) const {
// TODO: could still be errornous
if (dot(other) < 0.0) {
const auto& difference = (*this)+other;
return difference.dot(difference);
}
const auto& difference = (*this)-other;
return difference.dot(difference);
}
value_t logdist (const dualquat<value_t>& other) const {
const auto& difference = ((*this)^(other.C())).log();
return difference.dot(difference);
}
value_t baddist (const dualquat<value_t>& other) const {
const bool I_know_what_I_do = false;
assert(I_know_what_I_do);
// TODO: could still be errornous
if (dot(other) < 0.0) {
const auto& difference = log()-(other*(-1.0)).log();
return difference.dot(difference);
}
const auto& difference = log()-other.log();
return difference.dot(difference);
}
bool isunit() const {
const value_t residue0 = w*w+x*x+y*y+z*z-1.0;
const value_t residue1 = w*W+x*X+y*Y+z*Z;
return residue0*residue0 < EPS(value_t) &&
residue1*residue1 < EPS(value_t);
}
};
#endif