Convert vector by matrix

I have a 4 * 4 matrix and a 3d vector. I need to translate a vector by matrix.

Not too crazy math notation because I don't understand it.

An example in something close to java would be fab!

+2


source to share


4 answers


It's pretty easy once you see it.

(New-3d-point) = Metrix-4x4 x (Old-3d-point)

      

means ...

|x_new|   |a1 a2 a3 a4|   |x_old|
|y_new| = |b1 b2 b3 b4| x |y_old|
|z_new|   |c1 c2 c3 c4|   |z_old|
|  1  |   |d1 d2 d3 d4|   |  1  |

      



means ...

x_new = a1*x_old + a2*y_old + a3*z_old + a4
y_new = b1*x_old + b2*y_old + b3*z_old + b4
z_new = c1*x_old + c2*y_old + c3*z_old + c4

      

d1-d4 should make "1" so you don't need to use it.

Hope it helps.

+10


source


You need mathematicians. I'll try to be gentle, but if you don't know how to do matrix multiplication, you need to watch it right now!

First, you need to render a 3D vector in a 4-vector, adding an extra 1. Computer graphics usually call this the "w" coordinate (go with the x, y and z coordinates).

3D vector: (X, Y, Z) โ†’ 4D vector: (x = X, y = Y, z = Z, w = 1)

Then you can multiply the 4-vector by a 4x4 matrix. It depends on the matrix, but you can make a matrix that transforms the vector:

[x,y,z,1] * [1 0 0 0]  =  [x+a,y+b,z+c,1]
            [0 1 0 0]
            [0 0 1 0]
            [a b c 1]

      

Unsurprisingly, this matrix is โ€‹โ€‹called the translation matrix.



The advantage with this is that you can also make a rotation matrix or a scaling matrix, then you can multiply as many of them as you like into one matrix:

v * M1 * M2 * M3 = v * (M1*M2*M3)

      

Multiplying a vector by the resulting matrix is โ€‹โ€‹equivalent to multiplying by all the constituent matrixes in sequence, which, as you can imagine, can save you a lot of time and hassle.


The code for multiplying a 4-vector by a 4x4 matrix might look something like this:

for(int i=0; i<4; ++i) {
  double accumulator= 0.0;
  for(int j=0; j<4; ++j) {
    accumulator+= rowVectorIn[j]*matrix[j][i]; // matrix is stored by rows
  }
  rowVectorOut[i]= accumulator;
}

      

+3


source


If you process your (usually 3d) vector (x, y, z) as four vectors (x, y, z, 1), you can do this: w = Av

T

where T

is the transpose operation (twisting a vertical vertical vector or vice versa), and A

- a correctly matched matrix, and w

is a transformed matrix.

You need to know how to do matrix multiplication.

To get transposed with (a, b, c)

, and nothing else defines A

as:

1  0  0  a
0  1  0  b
0  0  1  c
0  0  0  1

      

+2


source


I don't know exactly what form your data is coming in, but at first glance it looks like you might be dealing with quaternions .

Without going into the math, there are several ways to represent a 3D matrix transformation. There are Euler angles and quaternions. Euler angles are certainly simpler, but quaternions have the advantage that they cannot be gimbled.

Quaternions of real values โ€‹โ€‹in three dimensions are represented by 4x4 matrices, so I thought this might apply to you. Of course, the authors of other solutions may be right, and what you are dealing with may be something completely different, but in any case it might be interesting to know.

Hope this helps. Good luck!

+1


source







All Articles