Xcode Simd-有关平移和旋转矩阵示例的问题 [英] Xcode simd - issue with Translation and Rotation Matrix Example

查看:464
本文介绍了Xcode Simd-有关平移和旋转矩阵示例的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Apple不仅使用列优先与行优先的反直观,而且苹果公司有关使用矩阵"的文档还通过在2D中构造"翻译矩阵"和旋转矩阵"的示例进一步加剧了混淆. .

Not only is using column-major vs row-major counter-intuitive, Apple's documentation on "Working with Matrices" further exacerbates the confusion by their examples of "constructing" a "Translate Matrix" and a "Rotation Matrix" in 2D.

根据Apple的文档()转换矩阵

翻译翻译矩阵采用以下形式:

Translate A translate matrix takes the following form:

1  0  0
0  1  0 
tx ty 1

simd库为身份矩阵(矩阵)提供常量 沿对角线为1,在其他位置为0). 3 x 3浮动 单位矩阵是matrix_identity_float3x3.

The simd library provides constants for identity matrices (matrices with ones along the diagonal, and zeros elsewhere). The 3 x 3 Float identity matrix is matrix_identity_float3x3.

以下函数使用以下命令返回simd_float3x3矩阵 通过设置元素中指定的tx和ty转换值 身份矩阵:

The following function returns a simd_float3x3 matrix using the specified tx and ty translate values by setting the elements in an identity matrix:

func makeTranslationMatrix(tx: Float, ty: Float) -> simd_float3x3 {
    var matrix = matrix_identity_float3x3

    matrix[0, 2] = tx
    matrix[1, 2] = ty

    return matrix 
}

我的问题

代码行matrix[0, 2] = tx将第一列和第三行的值设置为tx. let translationMatrix = makeTranslationMatrix(tx: 1, ty: 3)并打印出第二列print(translationMatrix.columns.2)将产生float3(0.0, 0.0, 1.0).我对为什么它是包含转换值而不是列的最后一行感到非常困惑.使用SCNMatrix4MakeTranslation并从SCNMatrix4对象创建simd_float4x4时不使用此约定.

The line of code matrix[0, 2] = tx sets the value of the first column and the third row to tx. let translationMatrix = makeTranslationMatrix(tx: 1, ty: 3) and printing out the 2nd column print(translationMatrix.columns.2) will yield float3(0.0, 0.0, 1.0). I am very confused regarding why it is the last row that contains the translation values, rather than the column. This convention is not used when using SCNMatrix4MakeTranslation and creating a simd_float4x4 out of the SCNMatrix4 object.

var A = SCNMatrix4MakeTranslation(1,2,3)
var Asimd = simd_float4x4(A)

A.m41 // 1
A.m42 // 2
A.m43 // 3
A.m44 // 1

Asimd.columns.3 // float4(1.0, 2.0, 3.0, 1.0)

SCNMatrix4simd_float4x4都遵循 column major 命名约定.在Apple的2D示例中,它是包含转换值的最后一行,而SCNMatrix4并转换为simd_float4x4时,它是包含转换值的最后一列.苹果公司的例子似乎在旋转矩阵方面也是如此.

Both SCNMatrix4 and simd_float4x4 follow the column major naming convention. In the 2D example from Apple, it is the last row that contains the translation values, whereas with SCNMatrix4 and converting to simd_float4x4, it is the last column that contains the translation values. Apple's example seems to be doing the same with the Rotation Matrices as well.

我想念什么?

推荐答案

这可能会令人困惑,是的.

This can be confusing, yes.

您提到的文档使您进行以下操作计算:

The documentation you mentions makes the following computation:

let translatedVector = positionVector * translationMatrix

请注意,矩阵位于乘法的右侧. 您可能已经习惯了b = M * a表示法,但是如果您进行移调,则会得到b' = a' * M',这就是示例所做的.

Note that the matrix is on the right side of the multiplication. You are probably used to the notation b = M * a but if you take the transpose you get b' = a' * M' which is what the sample does.

在SIMD中,无法将向量与其转置(bb')区分开来,并且该库允许您通过两种方式进行乘法:

In SIMD there's no way to differentiate a vector from its transpose (b from b') and the library allows you to make the multiplication in both ways:

static simd_float3 SIMD_CFUNC simd_mul(simd_float3x3 __x, simd_float3 __y);
static simd_float3 SIMD_CFUNC simd_mul(simd_float3 __x,  simd_float3x3 __y) { return simd_mul(simd_transpose(__y), __x); }

这篇关于Xcode Simd-有关平移和旋转矩阵示例的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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