-
Notifications
You must be signed in to change notification settings - Fork 64
quaternions.scad
Support for Quaternions. To use, add the following line to the beginning of your file:
use <BOSL/quaternions.scad>
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] = W + Xi + Yj + Zk
Usage:
- Quat(ax, ang);
Description: Create a new Quaternion from axis and angle of rotation.
Argument | What it does |
---|---|
ax |
Vector of axis of rotation. |
ang |
Number of degrees to rotate around the axis counter-clockwise, when facing the origin. |
Usage:
- QuatX(a);
Description: Create a new Quaternion for rotating around the X axis [1,0,0].
Argument | What it does |
---|---|
a |
Number of degrees to rotate around the axis counter-clockwise, when facing the origin. |
Usage:
- QuatY(a);
Description: Create a new Quaternion for rotating around the Y axis [0,1,0].
Argument | What it does |
---|---|
a |
Number of degrees to rotate around the axis counter-clockwise, when facing the origin. |
Usage:
- QuatZ(a);
Description: Create a new Quaternion for rotating around the Z axis [0,0,1].
Argument | What it does |
---|---|
a |
Number of degrees to rotate around the axis counter-clockwise, when facing the origin. |
Usage:
- QuatXYZ([X,Y,Z])
Description: Creates a quaternion from standard [X,Y,Z] rotation angles in degrees.
Argument | What it does |
---|---|
a |
The triplet of rotation angles, [X,Y,Z] |
Description: Returns the "Identity" zero-rotation Quaternion.
Usage:
- Q_Add_S(q, s)
Description:
Adds a scalar value s
to the W part of a quaternion q
.
Usage:
- Q_Sub_S(q, s)
Description:
Subtracts a scalar value s
from the W part of a quaternion q
.
Usage:
- Q_Mul_S(q, s)
Description:
Multiplies each part of a quaternion q
by a scalar value s
.
Usage:
- Q_Div_S(q, s)
Description:
Divides each part of a quaternion q
by a scalar value s
.
Usage:
- Q_Add(a, b)
Description: Adds each part of two quaternions together.
Usage:
- Q_Sub(a, b)
Description:
Subtracts each part of quaternion b
from quaternion a
.
Usage:
- Q_Mul(a, b)
Description:
Multiplies quaternion a
by quaternion b
.
Usage:
- Q_Dot(a, b)
Description:
Calculates the dot product between quaternions a
and b
.
Usage:
- Q_Neg(q)
Description:
Returns the negative of quaternion q
.
Usage:
- Q_Conj(q)
Description:
Returns the conjugate of quaternion q
.
Usage:
- Q_Norm(q)
Description:
Returns the norm()
"length" of quaternion q
.
Usage:
- Q_Normalize(q)
Description:
Normalizes quaternion q
, so that norm([W,X,Y,Z]) == 1.
Usage:
- Q_Dist(q1, q2)
Description: Returns the "distance" between two quaternions.
Usage:
- Q_Slerp(q1, q2, u);
Description: Returns a quaternion that is a spherical interpolation between two quaternions.
Argument | What it does |
---|---|
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:
a = QuatY(15);
b = QuatY(75);
color("blue",0.25) Qrot(a) cylinder(d=1, h=80);
color("red",0.25) Qrot(b) cylinder(d=1, h=80);
Qrot(Q_Slerp(a, b, 0.6)) cylinder(d=1, h=80);
Usage:
- Q_Matrix3(q);
Description: Returns the 3x3 rotation matrix for the given normalized quaternion q.
Usage:
- Q_Matrix4(q);
Description: Returns the 4x4 rotation matrix for the given normalized quaternion q.
Usage:
- Q_Axis(q)
Description:
Returns the axis of rotation of a normalized quaternion q
.
Usage:
- Q_Angle(q)
Usage:
- Q_Rot_Vector(v,q);
Description:
Returns the vector v
after rotating it by the quaternion q
.
Usage:
- Qrot(q) ...
Description:
Rotate all children by the rotation stored in quaternion q
.
Example:
q = QuatXYZ([45,35,10]);
color("red",0.25) cylinder(d=1,h=80);
Qrot(q) cylinder(d=1,h=80);