Skip to content
Revar Desmera edited this page Mar 31, 2019 · 13 revisions

Library File quaternions.scad

Support for Quaternions. To use, add the following line to the beginning of your file:

use <BOSL/quaternions.scad>

Table of Contents

  1. Quaternions

1. 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] = W + Xi + Yj + Zk

Quat()

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.

QuatX()

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.

QuatY()

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.

QuatZ()

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.

QuatXYZ()

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]

Q_Ident()

Description: Returns the "Identity" zero-rotation Quaternion.


Q_Add_S()

Usage:

  • Q_Add_S(q, s)

Description: Adds a scalar value s to the W part of a quaternion q.


Q_Sub_S()

Usage:

  • Q_Sub_S(q, s)

Description: Subtracts a scalar value s from the W part of a quaternion q.


Q_Mul_S()

Usage:

  • Q_Mul_S(q, s)

Description: Multiplies each part of a quaternion q by a scalar value s.


Q_Div_S()

Usage:

  • Q_Div_S(q, s)

Description: Divides each part of a quaternion q by a scalar value s.


Q_Add()

Usage:

  • Q_Add(a, b)

Description: Adds each part of two quaternions together.


Q_Sub()

Usage:

  • Q_Sub(a, b)

Description: Subtracts each part of quaternion b from quaternion a.


Q_Mul()

Usage:

  • Q_Mul(a, b)

Description: Multiplies quaternion a by quaternion b.


Q_Dot()

Usage:

  • Q_Dot(a, b)

Description: Calculates the dot product between quaternions a and b.


Q_Neg()

Usage:

  • Q_Neg(q)

Description: Returns the negative of quaternion q.


Q_Conj()

Usage:

  • Q_Conj(q)

Description: Returns the conjugate of quaternion q.


Q_Norm()

Usage:

  • Q_Norm(q)

Description: Returns the norm() "length" of quaternion q.


Q_Normalize()

Usage:

  • Q_Normalize(q)

Description: Normalizes quaternion q, so that norm([W,X,Y,Z]) == 1.


Q_Dist()

Usage:

  • Q_Dist(q1, q2)

Description: Returns the "distance" between two quaternions.


Q_Slerp()

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);

Q_Slerp() Example


Q_Matrix3()

Usage:

  • Q_Matrix3(q);

Description: Returns the 3x3 rotation matrix for the given normalized quaternion q.


Q_Matrix4()

Usage:

  • Q_Matrix4(q);

Description: Returns the 4x4 rotation matrix for the given normalized quaternion q.


Q_Axis()

Usage:

  • Q_Axis(q)

Description: Returns the axis of rotation of a normalized quaternion q.


Q_Angle()

Usage:

  • Q_Angle(q)

Q_Rot_Vector()

Usage:

  • Q_Rot_Vector(v,q);

Description: Returns the vector v after rotating it by the quaternion q.


Qrot()

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);

Qrot() Example