用矩阵变换3D向量的方法 [英] Method of transforming 3D vectors with a matrix

查看:233
本文介绍了用矩阵变换3D向量的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在阅读关于用矩阵变换Vector3的内容,并且正在深入研究数学,并且使用现有的代码对自己进行编码。无论出于何种原因,我的学校课程从未包含矩阵,所以我填补了我的知识空白。幸运的是我只需要一些简单的东西,我想。



上下文是我正在为 RoboCup 3D联赛。我用C#编写它,但它必须在Mono上运行。理想情况下,我不会为这个(WinForms / WPF / XNA)使用任何现有的图形库,因为我真正需要的是矩阵转换的一个简单子集。



具体来说,我需要翻译和x / y / z旋转,以及将多个转换组合成单个矩阵的方式。然后这将应用于我自己的 Vector3 类型以生成转换的 Vector3



我已阅读关于此的不同建议。例如,一些使用4x3矩阵对转换进行建模, others with a 4x4 matrix。



另外,一些例子显示你需要第一个向量矩阵为1的值。当它包含在输出中时,这个值会发生什么变化?

 
[ 1 0 0 0]
[xyz 1] * [0 1 0 0] = [abcd]
[0 0 1 0]
[2 4 6 1]

 

我缺少的部分是:


  • 我的矩阵大小是多少应该是

  • 通过将变换矩阵相乘来合成变换

  • 用结果矩阵变换3D矢量
>

由于我大多只是想让它运行,所以任何伪代码都会很好。关于什么样的矩阵值执行什么样的转换的信息在很多页面上都有明确的定义,所以不需要在这里讨论,除非你非常敏锐:) div>

3 x 3矩阵可以编码转换,例如旋转和反射,但不能转换。为此,您需要添加第四个元素,并根据同调坐标来表示您的矢量。为了某些目的,可以使用非方形矩阵,但是如果你想能够以任何顺序组成它们,它们应该是方形的(因为如果第一列中的列数等于因此,为了您的目的,您应该使用4x4矩阵和4元素同质向量,并添加第四个 w >与值1协调。



对一组向量应用转换只是一个乘法问题。

传统上,矢量表示为专栏,矩阵显示在左侧。您在上面将它们表示为并在右侧相乘。两者都是有效的,但是转换矩阵需要在两种情况之间转换。您显示的矩阵具有沿底部的平移值,这对您的乘法顺序而言是正确的。



在矢量变换之后,您需要用 w 坐标可以将 x,y z 缩放到传统的三维空间。

在C-ish伪代码中,使用行向量约定:

 向量变换(向量v,矩阵m)
{
向量结果;
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];
结果[0] =结果[0] /结果[3];
result [1] = result [1] / result [3];
result [2] = result [2] / result [3];
返回结果;





$ b

一系列变换可以通过将每个矩阵的矩阵依次相乘。请注意,矩阵乘法不可交换,所以乘法的顺序很重要。反过来,这意味着无论您将左侧的行向量还是右侧的列相乘都很重要。如果您乘以 A x B x C ,则列向量与首先执行转换 C ,然后 B ,最后 A 。使用行向量先是 A ,然后是 B ,然后是 C 。因此,在构建,组合和应用转换时保持一致性是非常重要的。



再一次,伪代码应该与 transform 以上:

pre $ 矩阵组合(矩阵第一,矩阵第二)
{
矩阵结果; (int j = 0; j <4; ++ j)
for(int i = 0; i <4; ++ i)

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];
返回结果;
}


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

这篇关于用矩阵变换3D向量的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆