# Understanding 3D transforms and rotations

I am aware that a 3-D linear transform (combination of: stretch, shear, rotation) takes the form of a 3x3 matrix.

Also that if the transform is required to also handle translations, then one can either use 3x3 matrix + translation vector, or 4x4 matrix

However, I'm also aware that there is a different technique for handling rotations: to use quaternions, which looks like a 4x4 matrix (but does it behave in the same way?).

So my question is: how do these objects play with one another?

Say I start up my default Blender scene with a cube and a camera.

Now that transform panel is confusing to me. As it stands in that picture, I can imagine blender is using either 3x3 matrix + translation vector, or 4x4 matrix

But then there is a 'Rotation Order' pulldown which allows quaternions.

Can anyone explain what blender does behind the scenes?

The reason for my asking is that I'm attempting a script that transforms an object into camera space (i.e. If you were to transform the scene so that the camera is at the origin pointing downwards, wherever the object ends up, this is the location I use)

def main(context, num):
bpy.ops.scene.new(type='FULL_COPY')
cloneScene = bpy.context.scene
cloneScene.name = 'clone'

camera = cloneScene.camera

for ob in cloneScene.objects:
if ob.type == "MESH":
me = ob.data

# WorldToCam( ObToWorld( ObCoord ) )
world2cam = camera.matrix_world.inverted()
ob2world = ob.matrix_world

obj2cam = world2cam * ob2world

bm = bmesh.new()
bm.from_mesh(me)

bm.transform(obj2cam)

for v in bm.verts:
v.co = MyTransform(v.co)

cam2obj = obj2cam.inverted()
bm.transform(cam2obj)

# save back into orig mesh
bm.to_mesh(me)


And so I would like to have a clear understanding of what is happening when I apply a transform.

Although I've tested that this method works in simple cases, it looks like it might fall over when dealing with translations and quaternions (seeing as I'm inverting the matrix not the transform, I'm not sure if these are guaranteed to be the same. In a straight linear transformation they are...)

EDIT: http://www.essentialmath.com/GDC2013/GDC13_quaternions_final.pdf <-- amazing walkthrough!

ftp://ftp.cs.indiana.edu/pub/hanson/Siggraph01QuatCourse/vqr.pdf <-- starts good, lost me with the links

courses.cms.caltech.edu/cs171/quatut.pdf <-- Ken Shoemake Quaternions

• en.wikipedia.org/wiki/Quaternions_and_spatial_rotation covers the use of quaternions for rotation in some detail. I'm too lazy to digest the entire article, but about 2/3 of the way down you can see "from a quaternion to an orthogonal matrix" . One thing to keep in mind is that the 4 numbers of a quaternion are NOT fully independent. I can't remember if blender uses the wxyz where ww + xx + yy + zz = 1 . Also, quaternions can end up looking really screwy when you're animating rotations and don't tune the curves properly. – Mutant Bob Apr 4 '14 at 16:16
• I thought I should say something about the statement that was made in the OP's question which was,"to use quaternions, which looks like a 4x4 matrix" . It may help clear things up a bit to say that quaternions start off looking very different from a matrix. In graphics, quaternions are translated into matrix format to become compatible with the graphics pipeline, however they start off looking very different(unless you are a trigonomic prodigy. Then they may look pretty much exactly the same... I don't know, I can't speak for those people :) – MarcClintDion Jan 27 '15 at 3:00
• The best resource I've found for quaternions is: Quaternions and Rotation Sequences: A Primer with Applications to Orbits, Aerospace and Virtual Reality by J. B. Kuipers Link: amzn.com/0691102988 – Andrew Hundt Nov 19 '15 at 5:16

The transformations are applied in this order: scale, rotation then translation. Note that you would need to multiply the matrices in the reverse order to get a single transformation matrix.

For a rotation around one axis you would only need a $2\times 2$ matrix, e.g. a rotation around the z-axis wouldn't change the z-values of the vertices. For $(x,y,z)$ vertices a $3\times 3$ matrix is more convenient since it can hold all rotations. The conversion from $3\times 3$ (orange) to $4\times 4$ is done by adding (green) a $1$ to the bottom right cell and fill the remaining with $0$.

\begin{aligned} &\begin{bmatrix} \color{orange}1 & \color{orange}0 & \color{orange}0 & \color{green}0\\ \color{orange}0 & \color{orange}{\cos{\theta}} & \color{orange}{-\sin{\theta}} & \color{green}0 \\ \color{orange}0 & \color{orange}{\sin{\theta}} & \color{orange}{\cos{\theta}} & \color{green}0 \\ \color{green}0 & \color{green}0 & \color{green}0 & \color{green}1\\ \end{bmatrix}\\ R_x = &\begin{bmatrix} 1 & 0 & 0 & 0 \\ 0 & \cos{\theta} & -\sin{\theta} & 0\\ 0 & \sin{\theta} & \cos{\theta} & 0\\ 0 & 0 & 0 & 1 \end{bmatrix}\\ R_y = &\begin{bmatrix} \cos{\theta} & 0 & \sin{\theta} & 0\\ 0 & 1 & 0 & 0 \\ -\sin{\theta} & 0 & \cos{\theta} & 0\\ 0 & 0 & 0 & 1 \end{bmatrix}\\ R_z = &\begin{bmatrix} \cos{\theta} & 0 & -\sin{\theta} & 0\\ \sin{\theta} & 1 & \cos{\theta} & 0\\ 0 & 0 & 1 & 0\\ 0 & 0 & 0 & 1 \end{bmatrix} \end{aligned}

A translation vector doesn't fit into this matrix, therefore it is expanded to a 4x4 matrix. This is in order to make translation compatible to the other transformations (This is called Homogeneous coordinates.)

\begin{aligned} T(d_x, d_y, d_y) &= \begin{bmatrix} 1 & 0 & 0 & d_x\\ 0 & 1 & 0 & d_y\\ 0 & 0 & 1 & d_z\\ 0 & 0 & 0 & 1\\ \end{bmatrix}\\ S(s_x, s_y, s_y) &= \begin{bmatrix} s_x & 0 & 0 & 0\\ 0 & s_y & 0 & 0\\ 0 & 0 & s_z & 0\\ 0 & 0 & 0 & 1\\ \end{bmatrix} \end{aligned}

Quaternions are a more complex way to describe rotations which avoids a Gimbal Lock, a situation where you would loose one degree of freedom.

Related questions:

• Quaternions have always been a mystery to me. I still use them, and know how to leverage them, but they are just a black box of wonder to me. No site that I have been to has been able to explain in a reasonable concise manner how they actually work. Do you know any sites that could explain them? – zero298 Apr 4 '14 at 15:28
• @zero298 There are different interpretations of quaternions, See the resources mathworld.wolfram.com/Quaternion.html , Ken Shoemake's original paper: run.usc.edu/cs520-s12/assign2/p245-shoemake.pdf – stacker Apr 4 '14 at 15:49
• @zero298 http://www.euclideanspace.com/ is also a very good place that explains a lot of different topics about transformations in space. It has also a lot of useful examples. – user2859 Apr 4 '14 at 16:14
• Thankyou Stacker for laying out the 4x4 matrix method of extending 3D linear transforms to accommodate translations. The question of how this method combines with quaternion math in Blender still remains to be answered, however. +1 for Ken Shoemake's article! 1985!!! – P i Apr 4 '14 at 21:16