I am confused about premultiply and postmuliply in vtk transform.
I searched the information from internet as folllowing.
The product of matrices A and B, AB and BA are not the same. It is common to use the terms “pre-multiplication” and “post-multiplication” in the multiplication of matrices. “A is post-multiplied by B,” or “B is pre-multiplied by A,” refers to the product AB. “B is post-multiplied by A,” or “A is pre-multiplied by B,” refers to the product BA.
According to this description, A is post-multiplied by B, that is A*B.
In VTK vtkTransform.h, it saids as below.
* Sets the internal state of the transform to PreMultiply. All subsequent
* operations will occur before those already represented in the
* current transformation. In homogeneous matrix notation, M = M*A where
* M is the current transformation matrix and A is the applied matrix.
* The default is PreMultiply.
The default mode is PRE-MULTIPLY, if you declear a transform variable, like Trans_A. This means Trans_A is pre-multiplied by another matrix.
That is [ another matrix ] * Trans_A.
When users are using vtkTransform to transform an object in vtk.
Premultiply means, AfterTransformOfObject = TransOfCurrentObject * Trans_A, in vtk description is M = M * A, A is the applied matrix -> also the transformation.
For example:
- set original matrix by identity matrix
When an object's transformation is set by identity matrix, M(current) = E - rotate by z axis with 30 degree
M(after1) = M(Current) * M(rotationByZWith30) - translate by x axis with 1 unit
M(after2) = M(after1) * M(translateByAWith1Unit)
So, the final result should be M(final) = M(current) * M(rotationByZWith30) * M(translateByAWith1Unit)
对于Premultiply, M = M * A
所以例如,设置Premultiply的模式,如果 对于一个物体 先沿X轴正向移动1个单位(Tran),再逆时针旋转30°(Rot),则带入premultiply的公式为 M = (M * Trans) * Rot, 去掉括号即 M(final) = M * Trans * Rot, 即对于原始物体先平移再旋转.
对于Postmultiply, M = A * M
同样的逻辑, 先沿X轴正向移动1个单位,再逆时针旋转30°, 则为 M = Rot * (Trans * M ), 去掉括号为 M(final) = Rot * Trans * M, 由于是左乘, 它相当于先对原始物体 先旋转 再平移. 即左乘的矩阵链为
M(final) = A4A3A2A1M, 那么实际上是等价于先对M做A4,再做A3,再做A2,再做A1
验证:
关于物体的变换的的物理意义:
vtk中的一个物体默认的 user transform是单位矩阵E, 如果人为设置了物体的变换矩阵M,那么它的物理意义就是
WorldCoordinate = Transform(Object) * LocalCoordinate
计算世界坐标系到局部坐标轴的变换计算过程:
假设局部坐标系的原点为Pos, 坐标轴为 XDir, YDir, ZDir
(1) 先获取这个坐标轴的变换矩阵,然后求逆矩阵
例如,设置为premultiply,先平移,再旋转. 则根据Pre的定义 M = M * A, 所以变换矩阵为 T * R
如果是postmultiply, 则先旋转,再平移, 根据post定义 M = A * M, T * R
(2) 直接计算
例如,设置为postMultiply, 可以先反向平移, 即 Trans(-Pos), 然后旋转, 即此坐标轴的单位矩阵,
Rot * Trans(-Pos)
对于1和2, 1中 W = (T * R) * Local , 两边都乘以Inv(TR), Inv(T * R) * World = Inv(TR) * (T*R) * Local ,
Local = Inv(T * R) * World, 即 Local = Inv(R) * Inv(T) * World, 对比2中,我们可以看到
Transform (-Pos) 就是 Pos的逆矩阵,例如
pos为(1,1,0), -pos 为(-1,-1,0)
1, 0, 0, 1 1, 0, 0 , -1
0, 1, 0, 1 * 0, 1, 0, -1 = E
0, 0, 1, 0 0, 0, 1, 0
0, 0, 0, 1 0, 0, 0, 1
对于R, 例如, 在1中,如果是先平移到(1,1,0),绕z旋转45°,那么旋转矩阵R为
0.707, -0.707, 0, 0
0.707, 0.707, 0, 0
0, 0, 1, 0
0, 0, 0, 1
计算一下这个矩阵的逆矩阵
(0.707, 0.707, 0 , 0)
(-0.707, 0.707, 0, 0)
(0, 0, 1, 0)
(0,0,01)
在第二种方式中次坐标的单位阵为XDir(1,1,0) YDir(-1, 1, 0) ZDir(0,0,1),单位化后为
(0.707, 0.707, 0)
(-0.707, 0.707, 0)
(0, 0, 1)
和上面的矩阵刚好相符,说明他们是互为逆矩阵的