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 &lt;-- amazing walkthrough!

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

courses.cms.caltech.edu/cs171/quatut.pdf &lt;-- Ken Shoemake Quaternions
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}
&amp;\begin{bmatrix}
\color{orange}1 &amp; \color{orange}0 &amp; \color{orange}0 &amp; \color{green}0\\
\color{orange}0 &amp; \color{orange}{\cos{\theta}} &amp; \color{orange}{-\sin{\theta}} &amp; \color{green}0 \\
\color{orange}0 &amp; \color{orange}{\sin{\theta}} &amp; \color{orange}{\cos{\theta}} &amp; \color{green}0 \\
\color{green}0 &amp; \color{green}0 &amp; \color{green}0 &amp; \color{green}1\\
\end{bmatrix}\\
R_x = &amp;\begin{bmatrix}
1 &amp; 0 &amp; 0 &amp; 0 \\
0 &amp; \cos{\theta} &amp; -\sin{\theta} &amp; 0\\
0 &amp; \sin{\theta} &amp; \cos{\theta} &amp; 0\\
0 &amp; 0 &amp; 0 &amp; 1
\end{bmatrix}\\
R_y = &amp;\begin{bmatrix}
\cos{\theta} &amp; 0 &amp; \sin{\theta} &amp; 0\\
0 &amp; 1 &amp; 0 &amp; 0 \\
-\sin{\theta} &amp; 0 &amp; \cos{\theta} &amp; 0\\
0 &amp; 0 &amp; 0 &amp; 1
\end{bmatrix}\\
R_z = &amp;\begin{bmatrix}
\cos{\theta} &amp; 0 &amp; -\sin{\theta} &amp; 0\\
\sin{\theta} &amp; 1 &amp; \cos{\theta} &amp; 0\\
0 &amp; 0 &amp; 1 &amp; 0\\
0 &amp; 0 &amp; 0 &amp; 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) &amp;= \begin{bmatrix}
1 &amp; 0 &amp; 0 &amp; d_x\\
0 &amp; 1 &amp; 0 &amp; d_y\\
0 &amp; 0 &amp; 1 &amp; d_z\\
0 &amp; 0 &amp; 0 &amp; 1\\
\end{bmatrix}\\
S(s_x, s_y, s_y) &amp;= \begin{bmatrix}
s_x &amp; 0 &amp; 0 &amp; 0\\
0 &amp; s_y &amp; 0 &amp; 0\\
0 &amp; 0 &amp; s_z &amp; 0\\
0 &amp; 0 &amp; 0 &amp; 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:


What is the order of transformations when exporting to Collada? 
Could someone please explain gimbal lock?
Apply rotation to camera?