Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Method of transforming 3D vectors with a matrix

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 :)

like image 925
Drew Noakes Avatar asked Feb 26 '23 22:02

Drew Noakes


1 Answers

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;
}
like image 146
walkytalky Avatar answered Mar 16 '23 19:03

walkytalky