# How to multiply vector 3 with 4by4 matrix, more precisely position * transformation matrix

All geometry in computer graphics are transformed by position * transform matrix; The issue is the fact that position is a vector with 3 components (x,y,z); And transform matrix is a 4 by 4 with one column that can be dumped(at least in my case). So my transform matrix is now a 3 by 4 matrix:
axis x { x, y, z }
axis y { x, y, z }
axis z { x, y, z }
position axis { x, y, z }

                         multiplied with position vector { x, y ,z }


If I dump position axis this can be done with standard formula of matrix multiplication. But it does not transform it. I can make the position vector with 4 components { x, y, z, w } but don't know what to do with the w? My only solution is a slow one, put position vector in a new transform matrix in position axis and multiply them. But it is computationally expensive. How to approach such problem?

-
I suspect the 3 by 4 matrix encodes an affine transformation, perhaps multiplying by the 3x3 initial columns, then adding the last column. Another possibility is this isometry. Your description isn't enough to tell what the 4x4 transformation matrix means, but this is my best guess. – hardmath Dec 8 '11 at 15:45
euclideanspace.com, nice link; I'll look into it. My transform matrix has a rotation matrix made of first 3 by 3 part and the last is the position. More precisely axis x = will indicate were the axis x is pointing, same for axis y and z; And the position axis tells were is in space located this transform. A box can be placed somewhere by specifying position in position axis, and rotated with the rest 3 axis vectors. X Y Z axis are unit vectors. – Palax Dec 8 '11 at 16:08
Thanks hardmath, euclideanspace.com/maths/geometry/affine/matrix4x4/index.htm solution works like charm. – Palax Dec 8 '11 at 16:49

Here's the "math" way of looking at this. In three dimensions there are various "isometries", mappings that preserve distances between points. Some of these are linear transformations, and these can be represented in the usual way as multiplication by an orthogonal matrix, real 3x3 matrix $P$ such that $P^{-1} = P^T$, the inverse is the transpose. If $P$ has determinant 1, then the mapping realized as multiplication by $P$ is a rotation. Otherwise the determinant can be -1, and the mapping is termed a reflection.

Now linear transformations always fix the origin. If you want an isometry that maps the origin to some other point, you need a translation mapping.

Combining linear transformations with translations gives the larger class of mappings we call affine transformations. The theorem here is that every isometry in Euclidean space (of whatever dimension) is an affine transformation, but not every affine transformation is necessarily an isometry (distance preserving).

The real 4x4 matrices being discussed here are a clever way of representing 3D affine transformations generally (and isometries in particular) with matrix multiplication in one dimension higher. The special structure of these is:

$$\left( \begin{array}{cc} P & v \\ 0 & 1 \end{array} \right)$$

where $P$ is a 3x3 matrix representing the linear transformation part of the affine mapping, $v$ is a 3x1 column representing offset of the origin, "0" denotes a 1x3 row of zeroes, and 1 is just scalar one.

Consider what happens if we take a 3D vector $u^T = (x,y,z)$, tack a synthetic component 1 onto the end, transpose it to a column and multiply the result by the special matrix above:

$$\left( \begin{array}{cc} P & v \\ 0 & 1 \end{array} \right) \times \left( \begin{array}{c} x \\ y \\ z \\ 1 \end{array} \right) = \left( \begin{array}{c} {Pu + v} \\ 1 \end{array} \right)$$

Thus a matrix multiplication can be used to compute $Pu + v$, which amounts to applying the linear transformation to source point $u$, then adding the offset $v$ to get the destination point. If $P$ is orthogonal and has determinant 1, then we might speak of rotating the source point and adding the offset. Such an operation is often required in computer graphics to "pan" and "dolly" a virtual camera.

While not "space efficient" in terms of the extra dimension, this representation is convenient for programmming because a general matrix multiplication routine can be used instead of coding separate steps of adding the offset after a matrix multiplication.

Note also that "undoing" the represented mapping will amount to multiplying by a matrix inverse. It is left as an exercise for the reader to work out what the inverse of the 4x4 matrix is in the special (isometry) case of orthogonal $P$.

-

Thanks for the answer hardmath; The solution is at here. More precisely to move and rotate a point (vector x y z) with a transform matrix (4 by 4) you must add to the point a new component. This will make the point a vector x y z w; By setting w to be 1 and multiply the point * transform = transformed point in space; If the w = 0 then point * transform = only rotated point. Max Script function:

function transformPoint mat x y z w =(
--remember w = 1 for move in space; w = 0 rotate in space;
local res = [0, 0, 0];
res.x = mat.row1.x*x + mat.row2.x*y + mat.row3.x*z + mat.row4.x*w;
res.y = mat.row1.y*x + mat.row2.y*y + mat.row3.y*z + mat.row4.y*w;
res.z = mat.row1.z*x + mat.row2.z*y + mat.row3.z*z + mat.row4.z*w;
return res;
)


matrix in max script is made of 4 row components [x, y, z]; row1=x axis; row2=y axis; row3=z axis; row4= position;

-