高效的 4x4 矩阵逆(仿射变换) [英] Efficient 4x4 matrix inverse (affine transform)

查看:57
本文介绍了高效的 4x4 矩阵逆(仿射变换)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望有人能指出一个有效的 4x4 仿射矩阵变换公式.目前我的代码使用辅因子扩展,它为每个辅因子分配一个临时数组.它很容易阅读,但比它应该的要慢.

I was hoping someone can point out an efficient formula for 4x4 affine matrix transform. Currently my code uses cofactor expansion and it allocates a temporary array for each cofactor. It's easy to read, but it's slower than it should be.

请注意,这不是家庭作业,我知道如何使用 4x4 辅助因子扩展手动解决它,这对我来说只是一个痛苦而不是真正有趣的问题.此外,我已经用谷歌搜索并想出了一些已经为您提供公式的网站(http://www.euclideanspace.com/maths/algebra/matrix/functions/inverse/fourD/index.htm).但是,可以通过预先计算一些产品来进一步优化这一点.我敢肯定有人曾为此想出最佳"公式?

Note, this isn't homework and I know how to work it out manually using 4x4 co-factor expansion, it's just a pain and not really an interesting problem for me. Also I've googled and came up with a few sites that give you the formula already (http://www.euclideanspace.com/maths/algebra/matrix/functions/inverse/fourD/index.htm). However this one could probably be optimized further by pre-computing some of the products. I'm sure someone came up with the "best" formula for this at one point or another?

推荐答案

您应该能够利用矩阵仿射这一事实来加快全逆矩阵的速度.也就是说,如果你的矩阵看起来像这样

You should be able to exploit the fact that the matrix is affine to speed things up over a full inverse. Namely, if your matrix looks like this

A = [ M   b  ]
    [ 0   1  ]

其中A是4x4,M是3x3,b是3x1,底行是(0,0,0,1),那么

where A is 4x4, M is 3x3, b is 3x1, and the bottom row is (0,0,0,1), then

inv(A) = [ inv(M)   -inv(M) * b ]
         [   0            1     ]

根据您的情况,计算 inv(A) * x 的结果可能比实际形成 inv(A) 更快.在这种情况下,事情简化为

Depending on your situation, it may be faster to compute the result of inv(A) * x instead of actually forming inv(A). In that case, things simplify to

inv(A) * [x] = [ inv(M) * (x - b) ]
         [1] = [        1         ] 

其中 x 是一个 3x1 的向量(通常是一个 3D 点).

where x is a 3x1 vector (usually a 3D point).

最后,如果 M 代表一个旋转(即它的列是正交的),那么你可以使用 inv(M) = transpose(M) 的事实.那么计算A的逆就是减去平移分量,再乘以3x3部分的转置.

Lastly, if M represents a rotation (i.e. its columns are orthonormal), then you can use the fact that inv(M) = transpose(M). Then computing the inverse of A is just a matter of subtracting the translation component, and multiplying by the transpose of the 3x3 part.

注意,矩阵是否正交是你从问题分析中应该知道的.在运行时检查它会相当昂贵;尽管您可能希望在调试版本中执行此操作以检查您的假设是否成立.

Note that whether or not the matrix is orthonormal is something that you should know from the analysis of the problem. Checking it during runtime would be fairly expensive; although you might want to do it in debug builds to check that your assumptions hold.

希望所有这些都清楚...

Hope all of that is clear...

这篇关于高效的 4x4 矩阵逆(仿射变换)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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