通过矩阵平移向量

发布于 2024-08-05 20:17:09 字数 97 浏览 2 评论 0原文

我有一个 4*4 矩阵和一个 3d 向量。 我需要通过矩阵翻译我的向量。

请不要太多疯狂的数学符号,因为我不明白。

一个接近java的例子就太棒了!

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

Not too much crazy maths notation please because I don't understand it.

An example in something close to java would be fab!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

匿名的好友 2024-08-12 20:17:09

一旦你看到它,就很容易了。

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

意味着...

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

意味着...

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 可以生成“1”,因此您不需要使用它。

希望这有帮助。

It quite 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 are there to make '1' so you don't need to use it.

Hope this helps.

风柔一江水 2024-08-12 20:17:09

不过,你需要数学。我会尽量温和,但如果你不知道如何进行矩阵乘法,你需要立即查一下!

首先,您需要通过添加额外的 1 将 3D 向量变成 4 向量。计算机图形学通常将其称为“w”坐标(与 x、y 和 z 坐标配合)。

3D矢量:(X,Y,Z) -> 4D 向量:(x=X, y=Y, z=Z, w=1)

然后,您可以将 4 向量乘以 4x4 矩阵。它的作用取决于矩阵——但是你可以创建一个平移向量的矩阵:

[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]

毫不奇怪,这种矩阵被称为平移矩阵。

这样做的好处是,您还可以创建一个旋转矩阵或缩放矩阵,然后您可以将任意数量的它们相乘成一个矩阵

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

将向量乘以得到的矩阵相当于按顺序乘以所有分量矩阵,正如您想象的那样,这可以节省您大量的时间和麻烦。


将 4 向量乘以 4x4 矩阵的代码可能类似于:

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;
}

You need the maths, though. I will try to be gentle, but if you don't know how to do matrix multiplication, you need to look it up, right now!

First, you need to make your 3D vector into a 4-vector, by adding an extra 1 to it. Computer graphics usually calls this a "w" coordinate (to 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 the 4x4 matrix. What this does depends on the matrix -- but you can make a matrix that translates 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 kind of matrix is called a translation matrix.

The advantage of doing things this way is that you can also make a rotation matrix, or a scaling matrix, then you can multiply as many of them as you want together 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 component matrices in sequence, which as you might imagine can save you a lot of time and hassle.


Code for multiplying a 4-vector by a 4x4 matrix might be something like:

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;
}
苯莒 2024-08-12 20:17:09

如果您将(通常是 3d)向量 (x, y, z) 视为四向量 (x, y, z, 1),您可以这样做:w = AvT,其中 T 是转置操作(垂直扭曲水平向量,反之亦然),A 是正确选择的矩阵,w 是转换后的矩阵。

您必须知道如何进行矩阵乘法。

要通过 (a, b, c) 获得转置,而无需将 A 定义为:

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

If you treat your (generally 3d) vector (x, y, z) as a four vector (x, y, z, 1) you can do this: w = AvT, where T is the transpose operation (twist a horzontal vector vertical or vice versa) and A is a correctly chosen matrix, and w is the translated matrix.

You do have to know how to do matrix multiplication.

To get a transposition by (a, b, c) and nothing else define A as:

1  0  0  a
0  1  0  b
0  0  1  c
0  0  0  1
嗼ふ静 2024-08-12 20:17:09

我不确定您的数据到底以什么形式出现,但乍一看,您可能正在处理

无需太深入数学,有几种方法可以表示三维矩阵变换。有欧拉角和四元数。欧拉角肯定更简单,但四元数具有不易受万向节锁定影响的优点。

三维实值的四元数用 4x4 矩阵表示,这就是为什么我认为这可能适用于您。当然,其他解决方案的作者可能是正确的,您正在处理的内容可能完全不同,但无论如何它可能值得学习。

希望这有帮助。祝你好运!

I'm not sure exactly what form your data is coming in but just on first glance it looks like you might be dealing with quaternions.

Without getting too deep into the math, there are a couple ways to represent a three dimensional matrix transform. There are Euler angles and quaternions. Euler angles are definitely simpler but quaternions have the advantage of not being susceptible to gimble lock.

Quaternions of real values in three dimensions are represented with 4x4 matrices which is why I thought this may apply to you. Of course, the authors of the other solutions may be right and what you're dealing with could be something totally different but it might be worth learning about anyway.

Hope this helps. Good luck!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文