This repository was archived by the owner on Sep 15, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvector.c
717 lines (549 loc) · 16.9 KB
/
vector.c
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
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
/*
Copyright (c) 2009 Technische Universitaet Muenchen, Informatik Lehrstuhl IX.
Author: Ingo Kresse <kresse at in.tum.de>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <assert.h>
#include <stdio.h>
#include "vector.h"
//////////////////////////////////////////////////////////////////////////////
// Create a zero vector
ias_vector ias_vector_zero()
{
ias_vector v;
v.x = 0;
v.y = 0;
v.z = 0;
return v;
}
//////////////////////////////////////////////////////////////////////////////
// Create a vector from the given values
ias_vector ias_vector_set(double x, double y, double z)
{
ias_vector v;
v.x = x;
v.y = y;
v.z = z;
return v;
}
//////////////////////////////////////////////////////////////////////////////
// Create a vector from a quaternion
ias_vector ias_vector_set_quatern(ias_quatern q)
{
ias_vector v;
v.x = q.x;
v.y = q.y;
v.z = q.z;
return v;
}
//////////////////////////////////////////////////////////////////////////////
// Add one vector to another (element by element)
// c = b + a
ias_vector ias_vector_add(ias_vector b, ias_vector a)
{
return ias_vector_set(b.x + a.x, b.y + a.y, b.z + a.z);
}
//////////////////////////////////////////////////////////////////////////////
// Subtract one vector from another (element by element)
// c = b - a
ias_vector ias_vector_sub(ias_vector b, ias_vector a)
{
return ias_vector_set(b.x - a.x, b.y - a.y, b.z - a.z);
}
//////////////////////////////////////////////////////////////////////////////
// Mutliply vector by scalar
ias_vector ias_vector_mul(double s, ias_vector a)
{
ias_vector b;
b.x = s * a.x;
b.y = s * a.y;
b.z = s * a.z;
return b;
}
//////////////////////////////////////////////////////////////////////////////
// Compute vector magnitude
double ias_vector_mag(ias_vector a)
{
return sqrt(a.x * a.x + a.y * a.y + a.z * a.z);
}
//////////////////////////////////////////////////////////////////////////////
// Normalize a vector to unit length
ias_vector ias_vector_unit(ias_vector a)
{
double d;
ias_vector b;
d = sqrt(a.x * a.x + a.y * a.y + a.z * a.z);
b.x = a.x / d;
b.y = a.y / d;
b.z = a.z / d;
return b;
}
//////////////////////////////////////////////////////////////////////////////
// Take cross product of two vectors
ias_vector ias_vector_cross(ias_vector a, ias_vector b)
{
ias_vector c;
c.x = a.y * b.z - a.z * b.y;
c.y = -a.x * b.z + a.z * b.x;
c.z = a.x * b.y - a.y * b.x;
return c;
}
//////////////////////////////////////////////////////////////////////////////
// Take dot product of two vectors
double ias_vector_dot(ias_vector a, ias_vector b)
{
return a.x * b.x + a.y * b.y + a.x * b.z;
}
//////////////////////////////////////////////////////////////////////////////
// See if a vector is finite (e.g., not nan)
int ias_vector_finite(ias_vector a)
{
return finite(a.x) && finite(a.y) && finite(a.z);
}
//////////////////////////////////////////////////////////////////////////////
// Create a quaternion from elements
ias_quatern ias_quatern_set(double u, double x, double y, double z)
{
ias_quatern q;
q.u = u;
q.x = x;
q.y = y;
q.z = z;
return q;
}
//////////////////////////////////////////////////////////////////////////////
// Create a quaternion from a vector
ias_quatern ias_quatern_set_vector(ias_vector v)
{
ias_quatern q;
q.u = 0.0;
q.x = v.x;
q.y = v.y;
q.z = v.z;
return q;
}
//////////////////////////////////////////////////////////////////////////////
// Create an identity quaternion
ias_quatern ias_quatern_ident()
{
ias_quatern q;
q.u = 1.0;
q.x = 0.0;
q.y = 0.0;
q.z = 0.0;
return q;
}
//////////////////////////////////////////////////////////////////////////////
// Create a quaternion from an axis and angle
ias_quatern ias_quatern_from_axis(double x, double y, double z, double a)
{
double l;
ias_quatern q;
l = x * x + y * y + z * z;
if (l > 0.0)
{
a *= 0.5;
l = sin(a) / sqrt(l);
q.u = cos(a);
q.x = x * l;
q.y = y * l;
q.z = z * l;
}
else
{
q.u = 1;
q.x = 0;
q.y = 0;
q.z = 0;
}
q = ias_quatern_normal(q);
return q;
}
//////////////////////////////////////////////////////////////////////////////
// Convert quaterion to axis and angle
ias_quatern ias_quatern_to_axis(ias_quatern a)
{
ias_quatern b;
b.x = a.x;
b.y = a.y;
b.z = a.z;
b.u = acos(a.u) * 2;
return b;
}
//////////////////////////////////////////////////////////////////////////////
// Create a quaternion from Euler angles
ias_quatern ias_quatern_from_euler(double roll, double pitch, double yaw)
{
double phi, the, psi;
ias_quatern p;
phi = roll / 2;
the = pitch / 2;
psi = yaw / 2;
p.u = cos(phi) * cos(the) * cos(psi) + sin(phi) * sin(the) * sin(psi);
p.x = sin(phi) * cos(the) * cos(psi) - cos(phi) * sin(the) * sin(psi);
p.y = cos(phi) * sin(the) * cos(psi) + sin(phi) * cos(the) * sin(psi);
p.z = cos(phi) * cos(the) * sin(psi) - sin(phi) * sin(the) * cos(psi);
p = ias_quatern_normal(p);
return p;
}
//////////////////////////////////////////////////////////////////////////////
// Create a quaternion from rotation matrix
ias_quatern ias_quatern_from_homo(ias_homo a)
{
ias_quatern b;
double trace = a.m[0][0] + a.m[1][1] + a.m[2][2];
if(trace > 0) {
double s = 0.5 / sqrt(1.0 + trace);
b.u = 0.25 / s;
b.x = (a.m[2][1] - a.m[1][2]) * s;
b.y = (a.m[0][2] - a.m[2][0]) * s;
b.z = (a.m[1][0] - a.m[0][1]) * s;
} else {
if (a.m[0][0] > a.m[1][1] && a.m[0][0] > a.m[2][2]) {
double s = 0.5 / sqrt(1.0 + a.m[0][0] - a.m[1][1] - a.m[2][2]);
b.u = (a.m[2][1] - a.m[1][2]) * s;
b.x = 0.25 / s;
b.y = (a.m[0][1] + a.m[1][0]) * s;
b.z = (a.m[0][2] + a.m[2][0]) * s;
} else if (a.m[1][1] > a.m[2][2]) {
double s = 0.5 / sqrt(1.0 + a.m[1][1] - a.m[0][0] - a.m[2][2]);
b.u = (a.m[0][2] - a.m[2][0]) * s;
b.x = (a.m[0][1] + a.m[1][0]) * s;
b.y = 0.25 / s;
b.z = (a.m[1][2] + a.m[2][1]) * s;
} else {
double s = 0.5 / sqrt(1.0 + a.m[2][2] - a.m[0][0] - a.m[1][1]);
b.u = (a.m[1][0] - a.m[0][1]) * s;
b.x = (a.m[0][2] + a.m[2][0]) * s;
b.y = (a.m[1][2] + a.m[2][1]) * s;
b.z = 0.25 / s;
}
}
b = ias_quatern_normal(b);
return b;
}
//////////////////////////////////////////////////////////////////////////////
// Convert quaternion to Euler angles
ias_vector ias_quatern_to_euler(ias_quatern q)
{
double phi, the, psi;
q = ias_quatern_normal(q);
phi = atan2(2 * (q.y*q.z + q.u*q.x), (q.u*q.u - q.x*q.x - q.y*q.y + q.z*q.z));
the = asin(-2 * (q.x*q.z - q.u * q.y));
psi = atan2(2 * (q.x*q.y + q.u*q.z), (q.u*q.u + q.x*q.x - q.y*q.y - q.z*q.z));
return ias_vector_set(phi, the, psi);
}
/* REMOVE (not correct)
//////////////////////////////////////////////////////////////////////////////
// Create a Quaternion from a 3x3 rotation matrix
ias_quatern ias_quaternFromRot( double rot[9] )
{
int which;
double temp;
ias_quatern q;
q.u = (1 + rot[0] + rot[4] + rot[8]) / 4;
q.x = (1 + rot[0] - rot[4] - rot[8]) / 4;
q.y = (1 - rot[0] + rot[4] - rot[8]) / 4;
q.z = (1 - rot[0] - rot[4] + rot[8]) / 4;
temp = q.u;
which = 1;
if (q.x > temp)
{
temp = q.x;
which = 2;
}
if (q.y > temp)
{
temp = q.y;
which = 3;
}
if (q.z > temp)
{
which = 4;
}
switch(which)
{
case 1:
q.u = sqrt(q.u);
temp = 1.0/(4.0*q.u);
q.x = (rot[7] - rot[5]) * temp;
q.y = (rot[2] - rot[6]) * temp;
q.z = (rot[3] - rot[1]) * temp;
break;
case 2:
q.x = sqrt(q.x);
temp = 1.0/(4.0*q.x);
q.u = (rot[7] - rot[5]) * temp;
q.y = (rot[1] + rot[3]) * temp;
q.z = (rot[2] + rot[6]) * temp;
break;
case 3:
q.y = sqrt(q.y);
temp = 1.0/(4.0*q.y);
q.y = (rot[2] - rot[6]) * temp;
q.x = (rot[1] + rot[3]) * temp;
q.z = (rot[5] + rot[7]) * temp;
break;
case 4:
q.z = sqrt(q.z);
temp = 1.0/(4.0*q.z);
q.u = (rot[3] - rot[1]) * temp;
q.x = (rot[2] + rot[6]) * temp;
q.y = (rot[5] + rot[7]) * temp;
break;
}
return q;
}
*/
/* REMOVE
//////////////////////////////////////////////////////////////////////////////
// Create a quaternion from a pair of points.
ias_quatern ias_quaternFromLookAt(ias_vector center, ias_vector up)
{
ias_quatern a;
ias_vector x, y, z;
double R[9];
x = ias_vectorUnit(center);
y = ias_vectorCross(ias_vectorUnit(up), x);
z = ias_vectorCross(x, y);
R[0] = x.x;
R[3] = x.y;
R[6] = x.z;
R[1] = y.x;
R[4] = y.y;
R[7] = y.z;
R[2] = z.x;
R[5] = z.y;
R[8] = z.z;
a = ias_quaternFromRot(R);
return a;
}
*/
//////////////////////////////////////////////////////////////////////////////
// Invert a quaternion
ias_quatern ias_quatern_inverse(ias_quatern a)
{
ias_quatern b;
b.u = a.u;
b.x = -a.x;
b.y = -a.y;
b.z = -a.z;
return b;
}
//////////////////////////////////////////////////////////////////////////////
// Normalize a quaternion
ias_quatern ias_quatern_normal(ias_quatern a)
{
ias_quatern b;
double s;
s = sqrt(a.u * a.u + a.x * a.x + a.y * a.y + a.z * a.z);
b.u = a.u / s;
b.x = a.x / s;
b.y = a.y / s;
b.z = a.z / s;
return b;
}
//////////////////////////////////////////////////////////////////////////////
// Multiple two quaternions
ias_quatern ias_quatern_mul(ias_quatern a, ias_quatern b)
{
ias_quatern c;
c.u = a.u * b.u - a.x * b.x - a.y * b.y - a.z * b.z;
c.x = a.u * b.x + a.x * b.u + a.y * b.z - a.z * b.y;
c.y = a.u * b.y - a.x * b.z + a.y * b.u + a.z * b.x;
c.z = a.u * b.z + a.x * b.y - a.y * b.x + a.z * b.u;
return c;
}
//////////////////////////////////////////////////////////////////////////////
// Scale a quaternion rotation
ias_quatern ias_quatern_scale(double s, ias_quatern a)
{
ias_quatern b;
// Convert to axis-and-angle
b = ias_quatern_to_axis(a);
// Scale angle
b.u *= s;
// Convert back again
return ias_quatern_from_axis(b.x, b.y, b.z, b.u);
}
//////////////////////////////////////////////////////////////////////////////
// See if a quatern is finite (e.g., not nan)
int ias_quatern_finite(ias_quatern a)
{
return finite(a.x) && finite(a.y) && finite(a.z) && finite(a.u);
}
//////////////////////////////////////////////////////////////////////////////
// Create a pose from the given position and rotation
ias_pose ias_pose_set(ias_vector pos, ias_quatern rot)
{
ias_pose p;
p.pos = pos;
p.rot = rot;
return p;
}
//////////////////////////////////////////////////////////////////////////////
// Create a pose from a triplet of vectors
ias_pose ias_pose_point_at(ias_vector pos, ias_vector at, ias_vector up)
{
ias_pose pose;
ias_quatern b;
ias_vector x, y, z;
double R[3][3];
x = ias_vector_unit(ias_vector_sub(at, pos));
y = ias_vector_unit(ias_vector_cross(ias_vector_unit(up), x));
z = ias_vector_cross(x, y);
R[0][0] = x.x;
R[1][0] = x.y;
R[2][0] = x.z;
R[0][1] = y.x;
R[1][1] = y.y;
R[2][1] = y.z;
R[0][2] = z.x;
R[1][2] = z.y;
R[2][2] = z.z;
// TODO: switch cases to avoid instability
b.u = sqrt(1 + R[0][0] + R[1][1] + R[2][2]) / 2;
if (b.u < 1e-16)
fprintf(stderr,"pointing singularity (gimbal lock)");
b.x = (R[2][1] - R[1][2]) / (4 * b.u);
b.y = (R[0][2] - R[2][0]) / (4 * b.u);
b.z = (R[1][0] - R[0][1]) / (4 * b.u);
b = ias_quatern_normal(b);
/*
printf("x = %f %f %f \n", x.x, x.y, x.z);
printf("y = %f %f %f \n", y.x, y.y, y.z);
printf("z = %f %f %f \n", z.x, z.y, z.z);
printf("%f \n", b.u);
*/
pose.pos = pos;
pose.rot = b;
return pose;
}
//////////////////////////////////////////////////////////////////////////////
// See if a pose is finite (e.g., not nan)
int ias_pose_finite(ias_pose a)
{
return ias_vector_finite(a.pos) && ias_quatern_finite(a.rot);
}
//////////////////////////////////////////////////////////////////////////////
// Add one pose to another: c = b + a
ias_pose ias_coord_pose_add(ias_pose bpose, ias_pose apose)
{
ias_pose cpose;
cpose.pos = ias_coord_position_add(bpose.pos, apose.pos, apose.rot);
cpose.rot = ias_coord_rotation_add(bpose.rot, apose.rot);
return cpose;
}
//////////////////////////////////////////////////////////////////////////////
// Subtract one pose from another: c = b - a
ias_pose ias_coord_pose_sub(ias_pose bpose, ias_pose apose)
{
ias_pose cpose;
cpose.pos = ias_coord_position_sub(bpose.pos, apose.pos, apose.rot);
cpose.rot = ias_coord_rotation_sub(bpose.rot, apose.rot);
return cpose;
}
//////////////////////////////////////////////////////////////////////////////
// Find the inverse of a pose; i.e., if b = ba + a, given b and ba,
// find a
ias_pose ias_coord_pose_solve(ias_pose ba, ias_pose b)
{
ias_quatern q;
ias_pose a;
a.rot = ias_quatern_mul(ias_quatern_inverse(ba.rot), b.rot);
q = ias_quatern_mul(a.rot, ias_quatern_set_vector(ba.pos));
q = ias_quatern_mul(q, ias_quatern_inverse(a.rot));
a.pos = ias_vector_sub(b.pos, ias_vector_set_quatern(q));
return a;
}
//////////////////////////////////////////////////////////////////////////////
// Add one position to another: c = b + a
ias_vector ias_coord_position_add(ias_vector bpos, ias_vector apos, ias_quatern arot)
{
ias_vector cpos;
// cpos = apos + arot * bpos * arot!
cpos = ias_vector_set_quatern(ias_quatern_mul(arot, ias_quatern_mul(ias_quatern_set_vector(bpos), ias_quatern_inverse(arot))));
cpos = ias_vector_add(apos, cpos);
return cpos;
}
//////////////////////////////////////////////////////////////////////////////
// Subtract one position from another: c = b - a
ias_vector ias_coord_position_sub(ias_vector bpos, ias_vector apos, ias_quatern arot)
{
ias_vector cpos;
// cpos = arot! * (bpos - apos) * arot
cpos = ias_vector_sub(bpos, apos);
cpos = ias_vector_set_quatern(ias_quatern_mul(ias_quatern_inverse(arot), ias_quatern_mul(ias_quatern_set_vector(cpos), arot)));
return cpos;
}
//////////////////////////////////////////////////////////////////////////////
// Add one rotation to another: b = r + a
ias_quatern ias_coord_rotation_add(ias_quatern r, ias_quatern a)
{
ias_quatern b;
// b = a * r
b = ias_quatern_mul(a, r);
return b;
}
//////////////////////////////////////////////////////////////////////////////
// Subtract one rotation from another: r = b - a
ias_quatern ias_coord_rotation_sub(ias_quatern b, ias_quatern a)
{
ias_quatern r;
// r = a! * b
r = ias_quatern_mul(ias_quatern_inverse(a), b);
return r;
}
//////////////////////////////////////////////////////////////////////////////
// Compute a homogeneous transform matrix from a Quatern
ias_homo ias_homo_from_quatern(ias_quatern q)
{
ias_homo b;
b.m[0][0] = 1 - (2*(q.y*q.y) + 2*(q.z*q.z));
b.m[0][1] = 2 * q.x * q.y - 2 * q.z * q.u;
b.m[0][2] = 2 * q.x * q.z + 2 * q.y * q.u;
b.m[1][0] = 2 * q.x * q.y + 2 * q.z * q.u;
b.m[1][1] = 1 - (2*(q.x*q.x) + 2*(q.z*q.z));
b.m[1][2] = 2 * q.y * q.z - 2 * q.x * q.u;
b.m[2][0] = 2 * q.x * q.z - 2 * q.y * q.u;
b.m[2][1] = 2 * q.y * q.z + 2 * q.x * q.u;
b.m[2][2] = 1 - (2*(q.x*q.x) + 2*(q.y*q.y));
return b;
}
//////////////////////////////////////////////////////////////////////////////
// Compute the inverse of the given transform matrix
ias_homo ias_homo_inverse(ias_homo a)
{
double det;
ias_homo b;
det = ( + a.m[0][0] * (a.m[1][1] * a.m[2][2] - a.m[1][2] * a.m[2][1]) - a.m[0][1] * (a.m[1][0] * a.m[2][2] - a.m[1][2] * a.m[2][0]) + a.m[0][2] * (a.m[1][0] * a.m[2][1] - a.m[1][1] * a.m[2][0]));
b.m[0][0] = ( + (a.m[1][1] * a.m[2][2] - a.m[1][2] * a.m[2][1])) / det;
b.m[0][1] = ( - (a.m[0][1] * a.m[2][2] - a.m[0][2] * a.m[2][1])) / det;
b.m[0][2] = ( + (a.m[0][1] * a.m[1][2] - a.m[0][2] * a.m[1][1])) / det;
b.m[1][0] = ( - (a.m[1][0] * a.m[2][2] - a.m[1][2] * a.m[2][0])) / det;
b.m[1][1] = ( + (a.m[0][0] * a.m[2][2] - a.m[0][2] * a.m[2][0])) / det;
b.m[1][2] = ( - (a.m[0][0] * a.m[1][2] - a.m[0][2] * a.m[1][0])) / det;
b.m[2][0] = ( + (a.m[1][0] * a.m[2][1] - a.m[1][1] * a.m[2][0])) / det;
b.m[2][1] = ( - (a.m[0][0] * a.m[2][1] - a.m[0][1] * a.m[2][0])) / det;
b.m[2][2] = ( + (a.m[0][0] * a.m[1][1] - a.m[0][1] * a.m[1][0])) / det;
return b;
}
//////////////////////////////////////////////////////////////////////////////
/// Apply a homogeneous transform
ias_vector ias_homo_mul(ias_homo m, ias_vector a)
{
ias_vector b;
b.x = m.m[0][0] * a.x + m.m[0][1] * a.y + m.m[0][2] * a.z;
b.y = m.m[1][0] * a.x + m.m[1][1] * a.y + m.m[1][2] * a.z;
b.z = m.m[2][0] * a.x + m.m[2][1] * a.y + m.m[2][2] * a.z;
return b;
}