高效的4x4矩阵逆(仿射变换) [英] Efficient 4x4 matrix inverse (affine transform)
问题描述
我希望有人可以指出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辅助因子扩展手动进行处理,这对我来说只是一个痛苦,并不是一个真正有趣的问题.另外,我已经在Google上搜索并找到了一些已经为您提供公式的网站( 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屋!