Where can I find an explanation of all transform matrix fields?

HelloMoon picture HelloMoon · Aug 11, 2009 · Viewed 8k times · Source

I've seen in some document from apple that the matrix behind a CALayer's transform property has fields like m14, m21, m22 and so on. I also remember that I saw a table that explains those fields, about one or two months ago. I had a hard time trying to find it. Anybody knows a source?

Answer

Gabe picture Gabe · Aug 11, 2009

That struct looks like this:

struct CATransform3D
{
    CGFloat m11, m12, m13, m14;
    CGFloat m21, m22, m23, m24;
    CGFloat m31, m32, m33, m34;
    CGFloat m41, m42, m43, m44;
};
typedef struct CATransform3D CATransform3D;

This is simply a 4x4 transform matrix used to transform 4-vectors. It can be used to represent any number of linear transforms. See this wikipedia article on this type of matrix. Most of those elements can't really be interpreted independantly, but some can. For example m41, m42, and m43 represent a translation in 3-space. So for example if you multiply a point by this matrix:

[ 1  0  0  0 ]
[ 0  1  0  0 ]
[ 0  0  1  0 ]
[ 1  2  3  1 ]

Then it will translate that point by 1 toward +X, 2 units toward +Y, and 3 toward +Z.

                   [ 1  0  0  0 ]
[ x  y  z  1 ]  x  [ 0  1  0  0 ]  =  [ x+1  y+2  z+3  1 ]
                   [ 0  0  1  0 ]
                   [ 1  2  3  1 ]

Note that the point must be represented as a 4 vector with the 4th element being 1. Also note that this vector is actually a matrix itself and the format of this matrix differs from the one described in the wikipedia article on transform matrices. This is because a point is usually represented by a single column, 4 row matrix, however, Apple represents them as 4 column, single row matrices. This means that any transform matrix you see in the wikipedia article needs to be transposed before using it on the iPhone for it to work correctly.

Another example is a scaling transform:

[ 2  0  0  0 ]
[ 0  2  0  0 ]
[ 0  0  2  0 ]
[ 0  0  0  1 ]

It will double all of the coordinates of your point, so (1, 2, 3) will become (2, 4, 6). Other transforms, like rotations and perspective projections are more difficult to recognize on sight.

Here is more info on transforms from Apple. A bunch of utility transforms are provided by Apple for generating these matrices, see this link. They don't actually discuss the math behind CATransform3DMakeRotation, but this link does.