views:

85

answers:

1

I've been doing some reading on transforming Vector3 with matrices, and am tossing up digging deeper into the math and coding this myself versus using existing code. For whatever reason my school curriculum never included matrices, so I'm filling a gap in my knowledge. Thankfully I only need a few simple things, I think.

Context is that I'm programming a robot for the RoboCup 3D league. I'm coding it in C# but it'll have to run on Mono. Ideally I wouldn't use any existing graphics libraries for this (WinForms/WPF/XNA) as all I really need is a neat subset of matrix transformations.

Specifically, I need translation and x/y/z rotations, and a way of combining multiple transformations into a single matrix. This will then be applied to my own Vector3 type to produce the transformed Vector3.

I've read different advice about this. For example, some model the transformation with a 4x3 matrix, others with a 4x4 matrix.

Also, some examples show that you need a forth value for the vector's matrix of 1. What happens to this value when it's included in the output?

            [1 0 0 0]
[x y z 1] * [0 1 0 0] = [a b c d]
            [0 0 1 0]
            [2 4 6 1]

The parts I'm missing are:

  • What sizes my matrices should be
  • Compositing transformations by multiplying the transformation matrices together
  • Transforming 3D vectors with the resulting matrix

As I mostly just want to get this running, any psuedo-code would be great. Information about what matrix values perform what transformations is quite clearly defined on many pages, so need not be discussed here unless you're very keen :)

+3  A: 

3 x 3 matrices can encode transformations such as rotation and reflection, but not translation. For that you need to add a fourth element and represent your vectors in terms of homogenous coordinates. It is possible to use non-square matrices for certain purposes, but if you want to be able to compose them in any order they should be square (because you can only multiply two matrices if the number of columns in the first is equal to the number of rows in the second).

So, for your purposes you should use 4x4 matrices and 4-element homogenous vectors, adding a fourth w coordinate with value 1.

Applying a transformation to a bunch of vectors is simply a matter of multiplying.

Traditionally the vectors are represented as columns and the matrix goes on the left. You represent them above as rows and multiply on the right. Both are valid, but the transformation matrix needs to be transposed between the two cases. The matrix you show has the translation values along the bottom, which is correct for your multiplication order.

After the vectors have been transformed, you need to divide through by the w coordinate to scale x, y and z back to conventional 3-space.

In C-ish pseudocode, using a row-vector convention:

Vector transform (Vector v, Matrix m)
{
    Vector result;
    for ( int i = 0; i < 4; ++i )
       result[i] = v[0] * m[0][i] + v[1] * m[1][i] + v[2] + m[2][i] + v[3] * m[3][i];
    result[0] = result[0]/result[3];
    result[1] = result[1]/result[3];
    result[2] = result[2]/result[3];
    return result;
}

A sequence of transformations can be composed by multiplying the matrices for each together in turn. Note that matrix multiplication is not commutative, so the order in which you multiply is important. In turn, this means it is important whether you are multiplying row vectors on the left or columns on the right. If you multiply A x B x C, then with column vectors that is the same as performing transformation C first, then B, then finally A. With row vectors it is A first, then B and then C. So it is important to keep everything consistent when constructing, composing and applying your transformations.

Again, in pseudocode that should be consistent with transform above:

Matrix compose (Matrix first, Matrix second)
{
    Matrix result;
    for ( int i = 0; i < 4; ++i )
        for ( int j = 0; j < 4; ++j )
            result[i][j] = first[i][0] * second[0][j]
                           + first[i][1] * second[1][j]
                           + first[i][2] * second[2][j]
                           + first[i][3] * second[3][j];
    return result;
}
walkytalky
Thank you very much for this detailed answer. Worked perfectly!
Drew Noakes
Your point about needing square matrices in order to combine them makes sense, but this article http://www.oocities.com/evilsnack/matrix.htm (which discusses an implementation in POV-RAY) makes it sound like 4x3 is all they use. Not sure why. Could they somehow normalise from 4x4 to 4x3 and then mutliply with 1x3 vector matrices, rather than normalising a 1x4 result to 1x3? Is that a performance thing?
Drew Noakes
The doc states they use 4x4 internally, although it's likely there is some optimisation in the matrix mult code. But the 4x3 described is in effect the external interface. POV-RAY uses a user-created text file to define the scene to render, so reducing the amount of data the user has to enter by hand saves time and reduces the chance of error. Standard transformations all have the far column (0 0 0 1), and when these are combined the result also has that far column, so it is possible to save a few ops by assuming that. The cost is that you have a non-general matrix implementation.
walkytalky
On your question about reducing to 1x3 before multiplication, the answer is no, since you need the fourth element as a source of translation. [To understand this, try doing some matrix ops by hand, and in particular seeing what happens to the zero vector (0 0 0).] It would, on the other hand, be perfectly feasible to use 1x3 for the interface, append w=1 internally, transform, divide through and then return only the x y z; indeed, this is very common, since most users don't really want to think about invisible 4th dimensions that exist only for mathematical convenience.
walkytalky