分解二维变换矩阵(Decompose 2D Transformation Matrix)

所以,我有一个Direct2D Matrix3x2F ,我用它来存储几何上的变换。 我希望这些转换是用户可编辑的,我不希望用户必须直接编辑矩阵。 是否有可能将3x2矩阵分解为缩放,旋转,倾斜和平移?

So, I have a Direct2D Matrix3x2F that I use to store transformations on geometries. I want these transformations to be user-editable, and I don't want the user to have to edit a matrix directly. Is it possible to decompose a 3x2 matrix into scaling, rotation, skewing, and translation?

最满意答案

将主要转换存储在具有可编辑属性的类中

scaling rotation skewing translation

然后从那些构建最终的变换矩阵。 这样会更容易。 但是,如果必须有分解矩阵的算法。 它们并不像你想象的那么简单。

System.Numerics有一种分解3D变换矩阵的方法

https://github.com/dotnet/corefx/blob/master/src/System.Numerics.Vectors/src/System/Numerics/Matrix4x4.cs#L1497

This is the solution I found for a Direct2D transformation matrix:

scale x = sqrt(M11 * M11 + M12 * M12)

scale y = sqrt(M21 * M21 + M22 * M22) * cos(shear)

rotation = atan2(M12, M11)

shear (y) = atan2(M22, M21) - PI/2 - rotation

translation x = M31

translation y = M32

If you multiply these values back together in the order scale(x, y) * skew(0, shear) * rotate(angle) * translate(x, y) you will get a matrix that performs an equivalent transformation.

分解二维变换矩阵(Decompose 2D Transformation Matrix)

所以,我有一个Direct2D Matrix3x2F ,我用它来存储几何上的变换。 我希望这些转换是用户可编辑的,我不希望用户必须直接编辑矩阵。 是否有可能将3x2矩阵分解为缩放,旋转,倾斜和平移?

So, I have a Direct2D Matrix3x2F that I use to store transformations on geometries. I want these transformations to be user-editable, and I don't want the user to have to edit a matrix directly. Is it possible to decompose a 3x2 matrix into scaling, rotation, skewing, and translation?

最满意答案

将主要转换存储在具有可编辑属性的类中

scaling rotation skewing translation

然后从那些构建最终的变换矩阵。 这样会更容易。 但是,如果必须有分解矩阵的算法。 它们并不像你想象的那么简单。

System.Numerics有一种分解3D变换矩阵的方法

https://github.com/dotnet/corefx/blob/master/src/System.Numerics.Vectors/src/System/Numerics/Matrix4x4.cs#L1497

This is the solution I found for a Direct2D transformation matrix:

scale x = sqrt(M11 * M11 + M12 * M12)

scale y = sqrt(M21 * M21 + M22 * M22) * cos(shear)

rotation = atan2(M12, M11)

shear (y) = atan2(M22, M21) - PI/2 - rotation

translation x = M31

translation y = M32

If you multiply these values back together in the order scale(x, y) * skew(0, shear) * rotate(angle) * translate(x, y) you will get a matrix that performs an equivalent transformation.