forked from BelfrySCAD/BOSL2
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathquaternions.scad
658 lines (574 loc) · 22.1 KB
/
quaternions.scad
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
///////////////////////////////////////////
// LibFile: quaternions.scad
// Support for Quaternions.
// Includes:
// include <BOSL2/std.scad>
///////////////////////////////////////////
// Section: Quaternions
// Quaternions are fast methods of storing and calculating arbitrary rotations.
// Quaternions contain information on both axis of rotation, and rotation angle.
// You can chain multiple rotation together by multiplying quaternions together.
// They don't suffer from the gimbal-lock issues that `[X,Y,Z]` rotation angles do.
// Quaternions are stored internally as a 4-value vector:
// `[X,Y,Z,W]`, where the quaternion formula is `W+Xi+Yj+Zk`
// Internal
function _quat(a,s,w) = [a[0]*s, a[1]*s, a[2]*s, w];
function _qvec(q) = [q.x,q.y,q.z];
function _qreal(q) = q[3];
function _qset(v,r) = concat( v, r );
// normalizes without checking
function _qnorm(q) = q/norm(q);
// Function: is_quaternion()
// Usage:
// if(is_quaternion(q)) a=0;
// Description: Return true if q is a valid non-zero quaternion.
// Arguments:
// q = object to check.
function is_quaternion(q) = is_vector(q,4) && ! approx(norm(q),0) ;
// Function: quat()
// Usage:
// quat(ax, ang);
// Description: Create a normalized Quaternion from axis and angle of rotation.
// Arguments:
// ax = Vector of axis of rotation.
// ang = Number of degrees to rotate around the axis counter-clockwise, when facing the origin.
function quat(ax=[0,0,1], ang=0) =
assert( is_vector(ax,3) && is_finite(ang), "Invalid input")
let( n = norm(ax) )
approx(n,0)
? _quat([0,0,0], sin(ang/2), cos(ang/2))
: _quat(ax/n, sin(ang/2), cos(ang/2));
// Function: quat_x()
// Usage:
// quat_x(a);
// Description: Create a normalized Quaternion for rotating around the X axis [1,0,0].
// Arguments:
// a = Number of degrees to rotate around the axis counter-clockwise, when facing the origin.
function quat_x(a=0) =
assert( is_finite(a), "Invalid angle" )
quat([1,0,0],a);
// Function: quat_y()
// Usage:
// quat_y(a);
// Description: Create a normalized Quaternion for rotating around the Y axis [0,1,0].
// Arguments:
// a = Number of degrees to rotate around the axis counter-clockwise, when facing the origin.
function quat_y(a=0) =
assert( is_finite(a), "Invalid angle" )
quat([0,1,0],a);
// Function: quat_z()
// Usage:
// quat_z(a);
// Description: Create a normalized Quaternion for rotating around the Z axis [0,0,1].
// Arguments:
// a = Number of degrees to rotate around the axis counter-clockwise, when facing the origin.
function quat_z(a=0) =
assert( is_finite(a), "Invalid angle" )
quat([0,0,1],a);
// Function: quat_xyz()
// Usage:
// quat_xyz([X,Y,Z])
// Description:
// Creates a normalized quaternion from standard [X,Y,Z] rotation angles in degrees.
// Arguments:
// a = The triplet of rotation angles, [X,Y,Z]
function quat_xyz(a=[0,0,0]) =
assert( is_vector(a,3), "Invalid angles")
let(
qx = quat_x(a[0]),
qy = quat_y(a[1]),
qz = quat_z(a[2])
)
q_mul(qz, q_mul(qy, qx));
// Function: q_from_to()
// Usage:
// q = q_from_to(v1, v2);
// Description:
// Returns the normalized quaternion that rotates the non zero 3D vector v1
// to the non zero 3D vector v2.
function q_from_to(v1, v2) =
assert( is_vector(v1,3) && is_vector(v2,3)
&& ! approx(norm(v1),0) && ! approx(norm(v2),0)
, "Invalid vector(s)")
let( ax = cross(v1,v2),
n = norm(ax) )
approx(n, 0)
? v1*v2>0 ? q_ident() : quat([ v1.y, -v1.x, 0], 180)
: quat(ax, atan2( n , v1*v2 ));
// Function: q_ident()
// Description: Returns the "Identity" zero-rotation Quaternion.
function q_ident() = [0, 0, 0, 1];
// Function: q_add_s()
// Usage:
// q_add_s(q, s)
// Description:
// Adds a scalar value `s` to the W part of a quaternion `q`.
// The returned quaternion is usually not normalized.
function q_add_s(q, s) =
assert( is_finite(s), "Invalid scalar" )
q+[0,0,0,s];
// Function: q_sub_s()
// Usage:
// q_sub_s(q, s)
// Description:
// Subtracts a scalar value `s` from the W part of a quaternion `q`.
// The returned quaternion is usually not normalized.
function q_sub_s(q, s) =
assert( is_finite(s), "Invalid scalar" )
q-[0,0,0,s];
// Function: q_mul_s()
// Usage:
// q_mul_s(q, s)
// Description:
// Multiplies each part of a quaternion `q` by a scalar value `s`.
// The returned quaternion is usually not normalized.
function q_mul_s(q, s) =
assert( is_finite(s), "Invalid scalar" )
q*s;
// Function: q_div_s()
// Usage:
// q_div_s(q, s)
// Description:
// Divides each part of a quaternion `q` by a scalar value `s`.
// The returned quaternion is usually not normalized.
function q_div_s(q, s) =
assert( is_finite(s) && ! approx(s,0) , "Invalid scalar" )
q/s;
// Function: q_add()
// Usage:
// q_add(a, b)
// Description:
// Adds each part of two quaternions together.
// The returned quaternion is usually not normalized.
function q_add(a, b) =
assert( is_quaternion(a) && is_quaternion(a), "Invalid quaternion(s)")
assert( ! approx(norm(a+b),0), "Quaternions cannot be opposed" )
a+b;
// Function: q_sub()
// Usage:
// q_sub(a, b)
// Description:
// Subtracts each part of quaternion `b` from quaternion `a`.
// The returned quaternion is usually not normalized.
function q_sub(a, b) =
assert( is_quaternion(a) && is_quaternion(a), "Invalid quaternion(s)")
assert( ! approx(a,b), "Quaternions cannot be equal" )
a-b;
// Function: q_mul()
// Usage:
// q_mul(a, b)
// Description:
// Multiplies quaternion `a` by quaternion `b`.
// The returned quaternion is normalized if both `a` and `b` are normalized
function q_mul(a, b) =
assert( is_quaternion(a) && is_quaternion(b), "Invalid quaternion(s)")
[
a[3]*b.x + a.x*b[3] + a.y*b.z - a.z*b.y,
a[3]*b.y - a.x*b.z + a.y*b[3] + a.z*b.x,
a[3]*b.z + a.x*b.y - a.y*b.x + a.z*b[3],
a[3]*b[3] - a.x*b.x - a.y*b.y - a.z*b.z,
];
// Function: q_cumulative()
// Usage:
// q_cumulative(v);
// Description:
// Given a list of Quaternions, cumulatively multiplies them, returning a list
// of each cumulative Quaternion product. It starts with the first quaternion
// given in the list, and applies successive quaternion rotations in list order.
// The quaternion in the returned list are normalized if each quaternion in v
// is normalized.
function q_cumulative(v, _i=0, _acc=[]) =
_i==len(v) ? _acc :
q_cumulative(
v, _i+1,
concat(
_acc,
[_i==0 ? v[_i] : q_mul(v[_i], last(_acc))]
)
);
// Function: q_dot()
// Usage:
// q_dot(a, b)
// Description: Calculates the dot product between quaternions `a` and `b`.
function q_dot(a, b) =
assert( is_quaternion(a) && is_quaternion(b), "Invalid quaternion(s)" )
a*b;
// Function: q_neg()
// Usage:
// q_neg(q)
// Description: Returns the negative of quaternion `q`.
function q_neg(q) =
assert( is_quaternion(q), "Invalid quaternion" )
-q;
// Function: q_conj()
// Usage:
// q_conj(q)
// Description: Returns the conjugate of quaternion `q`.
function q_conj(q) =
assert( is_quaternion(q), "Invalid quaternion" )
[-q.x, -q.y, -q.z, q[3]];
// Function: q_inverse()
// Usage:
// qc = q_inverse(q)
// Description: Returns the multiplication inverse of quaternion `q` that is normalized only if `q` is normalized.
function q_inverse(q) =
assert( is_quaternion(q), "Invalid quaternion" )
let(q = _qnorm(q) )
[-q.x, -q.y, -q.z, q[3]];
// Function: q_norm()
// Usage:
// q_norm(q)
// Description:
// Returns the `norm()` "length" of quaternion `q`.
// Normalized quaternions have unitary norm.
function q_norm(q) =
assert( is_quaternion(q), "Invalid quaternion" )
norm(q);
// Function: q_normalize()
// Usage:
// q_normalize(q)
// Description: Normalizes quaternion `q`, so that norm([W,X,Y,Z]) == 1.
function q_normalize(q) =
assert( is_quaternion(q) , "Invalid quaternion" )
q/norm(q);
// Function: q_dist()
// Usage:
// q_dist(q1, q2)
// Description: Returns the "distance" between two quaternions.
function q_dist(q1, q2) =
assert( is_quaternion(q1) && is_quaternion(q2), "Invalid quaternion(s)" )
norm(q2-q1);
// Function: q_slerp()
// Usage:
// q_slerp(q1, q2, u);
// Description:
// Returns a quaternion that is a spherical interpolation between two quaternions.
// Arguments:
// q1 = The first quaternion. (u=0)
// q2 = The second quaternion. (u=1)
// u = The proportional value, from 0 to 1, of what part of the interpolation to return.
// Example(3D): Giving `u` as a Scalar
// a = quat_y(-135);
// b = quat_xyz([0,-30,30]);
// for (u=[0:0.1:1])
// q_rot(q_slerp(a, b, u))
// right(80) cube([10,10,1]);
// #sphere(r=80);
// Example(3D): Giving `u` as a Range
// a = quat_z(-135);
// b = quat_xyz([90,0,-45]);
// for (q = q_slerp(a, b, [0:0.1:1]))
// q_rot(q) right(80) cube([10,10,1]);
// #sphere(r=80);
function q_slerp(q1, q2, u, _dot) =
is_undef(_dot)
? assert(is_finite(u) || is_range(u) || is_vector(u), "Invalid interpolation coefficient(s)")
assert(is_quaternion(q1) && is_quaternion(q2), "Invalid quaternion(s)" )
let(
_dot = q1*q2,
q1 = q1/norm(q1),
q2 = _dot<0 ? -q2/norm(q2) : q2/norm(q2),
dot = abs(_dot)
)
! is_finite(u) ? [for (uu=u) q_slerp(q1, q2, uu, dot)] :
q_slerp(q1, q2, u, dot)
: _dot>0.9995
? _qnorm(q1 + u*(q2-q1))
: let( theta = u*acos(_dot),
q3 = _qnorm(q2 - _dot*q1)
)
_qnorm(q1*cos(theta) + q3*sin(theta));
// Function: q_matrix3()
// Usage:
// q_matrix3(q);
// Description:
// Returns the 3x3 rotation matrix for the given normalized quaternion q.
function q_matrix3(q) =
let( q = q_normalize(q) )
[
[1-2*q[1]*q[1]-2*q[2]*q[2], 2*q[0]*q[1]-2*q[2]*q[3], 2*q[0]*q[2]+2*q[1]*q[3]],
[ 2*q[0]*q[1]+2*q[2]*q[3], 1-2*q[0]*q[0]-2*q[2]*q[2], 2*q[1]*q[2]-2*q[0]*q[3]],
[ 2*q[0]*q[2]-2*q[1]*q[3], 2*q[1]*q[2]+2*q[0]*q[3], 1-2*q[0]*q[0]-2*q[1]*q[1]]
];
// Function: q_matrix4()
// Usage:
// q_matrix4(q);
// Description:
// Returns the 4x4 rotation matrix for the given normalized quaternion q.
function q_matrix4(q) =
let( q = q_normalize(q) )
[
[1-2*q[1]*q[1]-2*q[2]*q[2], 2*q[0]*q[1]-2*q[2]*q[3], 2*q[0]*q[2]+2*q[1]*q[3], 0],
[ 2*q[0]*q[1]+2*q[2]*q[3], 1-2*q[0]*q[0]-2*q[2]*q[2], 2*q[1]*q[2]-2*q[0]*q[3], 0],
[ 2*q[0]*q[2]-2*q[1]*q[3], 2*q[1]*q[2]+2*q[0]*q[3], 1-2*q[0]*q[0]-2*q[1]*q[1], 0],
[ 0, 0, 0, 1]
];
// Function: q_axis()
// Usage:
// q_axis(q)
// Description:
// Returns the axis of rotation of a normalized quaternion `q`.
// The input doesn't need to be normalized.
function q_axis(q) =
assert( is_quaternion(q) , "Invalid quaternion" )
let( d = norm(_qvec(q)) )
approx(d,0)? [0,0,1] : _qvec(q)/d;
// Function: q_angle()
// Usage:
// a = q_angle(q)
// a12 = q_angle(q1,q2);
// Description:
// If only q1 is given, returns the angle of rotation (in degrees) of that quaternion.
// If both q1 and q2 are given, returns the angle (in degrees) between them.
// The input quaternions don't need to be normalized.
function q_angle(q1,q2) =
assert(is_quaternion(q1) && (is_undef(q2) || is_quaternion(q2)), "Invalid quaternion(s)" )
let( n1 = is_undef(q2)? norm(_qvec(q1)): norm(q1) )
is_undef(q2)
? 2 * atan2(n1,_qreal(q1))
: let( q1 = q1/norm(q1),
q2 = q2/norm(q2) )
4 * atan2(norm(q1 - q2), norm(q1 + q2));
// Function&Module: q_rot()
// Usage: As Module
// q_rot(q) ...
// Usage: As Function
// pts = q_rot(q,p);
// Description:
// When called as a module, rotates all children by the rotation stored in quaternion `q`.
// When called as a function with a `p` argument, rotates the point or list of points in `p` by the rotation stored in quaternion `q`.
// When called as a function without a `p` argument, returns the affine3d rotation matrix for the rotation stored in quaternion `q`.
// Example(FlatSpin,VPD=225,VPT=[71,-26,16]):
// module shape() translate([80,0,0]) cube([10,10,1]);
// q = quat_xyz([90,-15,-45]);
// q_rot(q) shape();
// #shape();
// Example(NORENDER):
// q = quat_xyz([45,35,10]);
// mat4x4 = q_rot(q);
// Example(NORENDER):
// q = quat_xyz([45,35,10]);
// pt = q_rot(q, p=[4,5,6]);
// Example(NORENDER):
// q = quat_xyz([45,35,10]);
// pts = q_rot(q, p=[[2,3,4], [4,5,6], [9,2,3]]);
module q_rot(q) {
multmatrix(q_matrix4(q)) {
children();
}
}
function q_rot(q,p) =
is_undef(p)? q_matrix4(q) :
is_vector(p)? q_rot(q,[p])[0] :
apply(q_matrix4(q), p);
// Module: q_rot_copies()
// Usage:
// q_rot_copies(quats) ...
// Description:
// For each quaternion given in the list `quats`, rotates to that orientation and creates a copy
// of all children. This is equivalent to `for (q=quats) q_rot(q) ...`.
// Arguments:
// quats = A list containing all quaternions to rotate to and create copies of all children for.
// Example:
// a = quat_z(-135);
// b = quat_xyz([0,-30,30]);
// q_rot_copies(q_slerp(a, b, [0:0.1:1]))
// right(80) cube([10,10,1]);
// #sphere(r=80);
module q_rot_copies(quats) for (q=quats) q_rot(q) children();
// Function: q_rotation()
// Usage:
// q_rotation(R)
// Description:
// Returns a normalized quaternion corresponding to the rotation matrix R.
// R may be a 3x3 rotation matrix or a homogeneous 4x4 rotation matrix.
// The last row and last column of R are ignored for 4x4 matrices.
// It doesn't check whether R is in fact a rotation matrix.
// If R is not a rotation, the returned quaternion is an unpredictable quaternion .
function q_rotation(R) =
assert( is_matrix(R,3,3) || is_matrix(R,4,4) ,
"Matrix is neither 3x3 nor 4x4")
let( tr = R[0][0]+R[1][1]+R[2][2] ) // R trace
tr>0
? let( r = 1+tr )
_qnorm( _qset([ R[1][2]-R[2][1], R[2][0]-R[0][2], R[0][1]-R[1][0] ], -r ) )
: let( i = max_index([ R[0][0], R[1][1], R[2][2] ]),
r = 1 + 2*R[i][i] -R[0][0] -R[1][1] -R[2][2] )
i==0 ? _qnorm( _qset( [ 4*r, (R[1][0]+R[0][1]), (R[0][2]+R[2][0]) ], (R[2][1]-R[1][2])) ):
i==1 ? _qnorm( _qset( [ (R[1][0]+R[0][1]), 4*r, (R[2][1]+R[1][2]) ], (R[0][2]-R[2][0])) ):
_qnorm( _qset( [ (R[2][0]+R[0][2]), (R[1][2]+R[2][1]), 4*r ], (R[1][0]-R[0][1])) ) ;
// Function&Module: q_rotation_path()
// Usage: As a function
// path = q_rotation_path(q1, n, q2);
// path = q_rotation_path(q1, n);
// Usage: As a module
// q_rotation_path(q1, n, q2) ...
// Description:
// If q2 is undef and it is called as a function, the path, with length n+1 (n>=1), will be the
// cumulative multiplications of the matrix rotation of q1 by itself.
// If q2 is defined and it is called as a function, returns a rotation matrix path of length n+1 (n>=1)
// that interpolates two given rotation quaternions. The first matrix of the sequence is the
// matrix rotation of q1 and the last one, the matrix rotation of q2. The intermediary matrix
// rotations are an uniform interpolation of the path extreme matrices.
// When called as a module, applies to its children() each rotation of the sequence computed
// by the function.
// The input quaternions don't need to be normalized.
// Arguments:
// q1 = The quaternion of the first rotation.
// q2 = The quaternion of the last rotation.
// n = An integer defining the path length ( path length = n+1).
// Example(3D): as a function
// a = quat_y(-135);
// b = quat_xyz([0,-30,30]);
// for (M=q_rotation_path(a, 10, b))
// multmatrix(M)
// right(80) cube([10,10,1]);
// #sphere(r=80);
// Example(3D): as a module
// a = quat_y(-135);
// b = quat_xyz([0,-30,30]);
// q_rotation_path(a, 10, b)
// right(80) cube([10,10,1]);
// #sphere(r=80);
// Example(3D): as a function
// a = quat_y(5);
// for (M=q_rotation_path(a, 10))
// multmatrix(M)
// right(80) cube([10,10,1]);
// #sphere(r=80);
// Example(3D): as a module
// a = quat_y(5);
// q_rotation_path(a, 10)
// right(80) cube([10,10,1]);
// #sphere(r=80);
function q_rotation_path(q1, n=1, q2) =
assert( is_quaternion(q1) && (is_undef(q2) || is_quaternion(q2) ), "Invalid quaternion(s)" )
assert( is_finite(n) && n>=1 && n==floor(n), "Invalid integer" )
assert( is_undef(q2) || ! approx(norm(q1+q2),0), "Quaternions cannot be opposed" )
is_undef(q2)
? [for( i=0, dR=q_matrix4(q1), R=dR; i<=n; i=i+1, R=dR*R ) R]
: let( q2 = q_normalize( q1*q2<0 ? -q2: q2 ),
dq = q_pow( q_mul( q2, q_inverse(q1) ), 1/n ),
dR = q_matrix4(dq) )
[for( i=0, R=q_matrix4(q1); i<=n; i=i+1, R=dR*R ) R];
module q_rotation_path(q1, n=1, q2) {
for(Mi=q_rotation_path(q1, n, q2))
multmatrix(Mi)
children();
}
// Function: q_nlerp()
// Usage:
// q = q_nlerp(q1, q2, u);
// Description:
// Returns a quaternion that is a normalized linear interpolation between two quaternions
// when u is a number.
// If u is a list of numbers, computes the interpolations for each value in the
// list and returns the interpolated quaternions in a list.
// The input quaternions don't need to be normalized.
// Arguments:
// q1 = The first quaternion. (u=0)
// q2 = The second quaternion. (u=1)
// u = A value (or a list of values), between 0 and 1, of the proportion(s) of each quaternion in the interpolation.
// Example(3D): Giving `u` as a Scalar
// a = quat_y(-135);
// b = quat_xyz([0,-30,30]);
// for (u=[0:0.1:1])
// q_rot(q_nlerp(a, b, u))
// right(80) cube([10,10,1]);
// #sphere(r=80);
// Example(3D): Giving `u` as a Range
// a = quat_z(-135);
// b = quat_xyz([90,0,-45]);
// for (q = q_nlerp(a, b, [0:0.1:1]))
// q_rot(q) right(80) cube([10,10,1]);
// #sphere(r=80);
function q_nlerp(q1,q2,u) =
assert(is_finite(u) || is_range(u) || is_vector(u) ,
"Invalid interpolation coefficient(s)" )
assert(is_quaternion(q1) && is_quaternion(q2), "Invalid quaternion(s)" )
assert( ! approx(norm(q1+q2),0), "Quaternions cannot be opposed" )
let( q1 = q_normalize(q1),
q2 = q_normalize(q2) )
is_num(u)
? _qnorm((1-u)*q1 + u*q2 )
: [for (ui=u) _qnorm((1-ui)*q1 + ui*q2 ) ];
// Function: q_squad()
// Usage:
// qn = q_squad(q1,q2,q3,q4,u);
// Description:
// Returns a quaternion that is a cubic spherical interpolation of the quaternions
// q1 and q4 taking the other two quaternions, q2 and q3, as parameter of a cubic
// on the sphere similar to the control points of a Bezier curve.
// If u is a number, usually between 0 and 1, returns the quaternion that results
// from the interpolation.
// If u is a list of numbers, computes the interpolations for each value in the
// list and returns the interpolated quaternions in a list.
// The input quaternions don't need to be normalized.
// Arguments:
// q1 = The start quaternion. (u=0)
// q1 = The first intermediate quaternion.
// q2 = The second intermediate quaternion.
// q4 = The end quaternion. (u=1)
// u = A value (or a list of values), of the proportion(s) of each quaternion in the cubic interpolation.
// Example(3D): Giving `u` as a Scalar
// a = quat_y(-135);
// b = quat_xyz([-50,-50,120]);
// c = quat_xyz([-50,-40,30]);
// d = quat_y(-45);
// color("red"){
// q_rot(b) right(80) cube([10,10,1]);
// q_rot(c) right(80) cube([10,10,1]);
// }
// for (u=[0:0.05:1])
// q_rot(q_squad(a, b, c, d, u))
// right(80) cube([10,10,1]);
// #sphere(r=80);
// Example(3D): Giving `u` as a Range
// a = quat_y(-135);
// b = quat_xyz([-50,-50,120]);
// c = quat_xyz([-50,-40,30]);
// d = quat_y(-45);
// for (q = q_squad(a, b, c, d, [0:0.05:1]))
// q_rot(q) right(80) cube([10,10,1]);
// #sphere(r=80);
function q_squad(q1,q2,q3,q4,u) =
assert(is_finite(u) || is_range(u) || is_vector(u) ,
"Invalid interpolation coefficient(s)" )
is_num(u)
? q_slerp( q_slerp(q1,q4,u), q_slerp(q2,q3,u), 2*u*(1-u))
: [for(ui=u) q_slerp( q_slerp(q1,q4,ui), q_slerp(q2,q3,ui), 2*ui*(1-ui) ) ];
// Function: q_exp()
// Usage:
// q2 = q_exp(q);
// Description:
// Returns the quaternion that is the exponential of the quaternion q in base e
// The returned quaternion is usually not normalized.
function q_exp(q) =
assert( is_vector(q,4), "Input is not a valid quaternion")
let( nv = norm(_qvec(q)) ) // q may be equal to zero here!
exp(_qreal(q))*quat(_qvec(q),2*nv);
// Function: q_ln()
// Usage:
// q2 = q_ln(q);
// Description:
// Returns the quaternion that is the natural logarithm of the quaternion q.
// The returned quaternion is usually not normalized and may be zero.
function q_ln(q) =
assert(is_quaternion(q), "Input is not a valid quaternion")
let(
nq = norm(q),
nv = norm(_qvec(q))
)
approx(nv,0) ? _qset([0,0,0] , ln(nq) ) :
_qset(_qvec(q)*atan2(nv,_qreal(q))/nv, ln(nq));
// Function: q_pow()
// Usage:
// q2 = q_pow(q, r);
// Description:
// Returns the quaternion that is the power of the quaternion q to the real exponent r.
// The returned quaternion is normalized if `q` is normalized.
function q_pow(q,r=1) =
assert( is_quaternion(q) && is_finite(r), "Invalid inputs")
let( theta = 2*atan2(norm(_qvec(q)),_qreal(q)) )
quat(_qvec(q), r*theta); // q_exp(r*q_ln(q));
// vim: expandtab tabstop=4 shiftwidth=4 softtabstop=4 nowrap