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

查看:35
本文介绍了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,它是包含翻译值的最后一列.Apple 的例子似乎对旋转矩阵也做了同样的事情.

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 translateVector = 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 中,无法区分向量与其转置(b from b'),并且该库允许您以两种方式进行乘法:

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天全站免登陆