• Models
  • Contests
  • Slicer
  • Login
  • Start Here
    thingiverse-iconprintables-iconcults3d-iconmakerworld-iconmyminifactory-icon

    3D GO

    3D ModelsContestsCollectionsSaved ModelsOn a mobile device?

3D GO

Privacy Policy
Quaternions for OpenScad 3D Printer File Image 1
Quaternions for OpenScad 3D Printer File Thumbnail 1

Quaternions for OpenScad

WilliamAAdams avatarWilliamAAdams

July 22, 2011

thingiverse-icon
DescriptionCommentsTags

Description

Having some basic Matrix math operations available in OpenScad has been a wonderful thing. I found my life to be somewhat lacking though because rotations are a royal pain in the...

This thing adds Quaternion support to OpenScad

Besides sounding like a cool name for an 80s rock band, or a futuristic diabolical planetary leader, quaternions are a math construct that make doing rotations a fairly painless task.

basically: myquat = quat(axis, angle);

So, to do a 30 degree rotation around the z-axis for example, would look like this:

rotz = quat([0,0,1], 30);

Now, that's not going to do you much good in OpenScad by itself, so you need to turn it into a matrix that OpenScad can easily consume and apply as a transform, so you do this:

rotzmatrix = quat_to_mat4(rotz);

Once you have this, you can use it with multmatrix()

multmatrix(rotzmatrix);

or

multmatrix(quat_to_mat4(quat([0,0,1],30)));

Ok, so wow, big deal!!

OK. So, combinations can be done like this:

q1 = quat([0,0,1],30);
q2 = quat([0,1,0], 15);
combo = quat_mult(q1, q2);

multmatrix(quat_to_mat4(combo));

This will combine the 30 degree rotation around the z-axis, followed by the 15 degree rotation about the y-axis, or visa versa.

This is nice, because you can apply this transform anywhere you like, without having to go through gyrations such as:

rotate()
{
rotate()
}

If you've used an instance of this math library in the past, I've made a couple of changes. I've added more vecxxx calls, to support vec2, and vec3 more explicitly. The biggest change is to the mat4 (matrix4x4) routines. Basically, I was storing the data in 'row order' previously, which required a transpose of the matrix before using it with the multmatrix() module. So, I changed the ordering to be column major ordering (for those in the know). That aligns better with what OpenScad expects, so life gets easier as you don't need the transpose in the end.

At any rate, another batch of goodness for OpenScad.

NOTE: the 'quat_to_mat4()' function is an interesting piece of work. The equivalent 'C' code utilizes a lot of variables, which are not a part of OpenScad functions (as opposed to modules). This poses quite a challenge. So, how it's broken down into a cascade of functions demonstrates a general methodology for dealing with 'variables' in functions. Just turn each 'variable' into a function.

UPDATE: 22072011
I left a few routines out from the first release of this thing, so I've added them in:
quat_conj(q) - conjugate of the quaternion
quat_distance(q1, q2) - distance between two quaternions
quat_norm(q) - the 'length' if you will.
quat_normalize(q) - normalize the quaternion

There is one more monster function to add: quat_slerp (spherical interpolation). As this one is more complex than the 'to_mat4', I'm having to think about it for a while. Not too bad, and it will come. Perhaps this will warrant a new version.

License:

Public Domain

Related Models

NUT JOB | Nut, Bolt, Washer and Threaded Rod Factory preview image

NUT JOB | Nut, Bolt, Washer and Threaded Rod Factory

mike_mattala profile image

mike_mattala

51,785

Filament Sample Box | Infill / Color Swatch | Fully Printable preview image

Filament Sample Box | Infill / Color Swatch | Fully Printable

Extrutim profile image

Extrutim

8,969

Lock Library Shackle Tag preview image

Lock Library Shackle Tag

adam.fourney profile image

adam.fourney

17

Parametric pulley - lots of tooth profiles preview image

Parametric pulley - lots of tooth profiles

droftarts profile image

droftarts

35,580

Minimal Linework Bookmarks preview image

Minimal Linework Bookmarks

Bamingo Design profile image

Bamingo Design

3,167

Book Rating Tags preview image

Book Rating Tags

Michael.C profile image

Michael.C

43

Low-Poly Pikachu preview image

Low-Poly Pikachu

flowalistik profile image

flowalistik

30,455

Filament Swatch Library preview image

Filament Swatch Library

LinearRelation profile image

LinearRelation

843