2D Matrix Decomposition

This demonstrates the polar decomposition of 2D matrices into their angle, scale, and shear components. The polarDecomp algorithm is taken from Shoemake & Duff "Matrix Animation and Polar Decomposition", 1992.

I found that the formula for the polar decomposition, on page 3, produces rotation matrices that don't account for diagonal axis flipping (swapping x with y). The article seems to factor out the flipping, but I didn't want to have a fourth component. Removing the sign(det(M)) factor eliminated this problem by incorporating the flipping into the rotation matrix.

It's very desirable to decompose a matrix generated by {angle, scale, shear} back into the same part values, but this happens only when scale is positive and shear is zero. If not, the rotation is affected. At least, the parts are stable once derived from the first matrix. This may help in UI, and certainly in interpolation.

Matrix generators

All the matrix ops are Vec . Mat

Rotation

In:= Scaling

In:= In:= Shear

In:= In:= A handy pre-defined symbolic matrix

In:= Out//MatrixForm= Returns Q (the rotation matrix) and S, the stretch matrix

Unlike the version in Shoemake & Duff's polar_decomp paper, this one always returns a normal rotation matrix, and thereby accounts for axis flipping

In:= Returns the angle of a rotation matrix

In:= Returns the {x, y} scale of a stretch matrix

In:= Returns the {x, y} shear of a stretch matrix

In:= Converts the Q and S matrices into {angle, scale, shear}

In:= Composition to produce the parts directly from the original matrix

In:= Recreate the original matrix from the parts

In:= Testing

This loses precision enough to require the SetPrecision

In:= Test that matrix decomposition and recomposition is equal to the original

In:= In:= Out= Test that the same components are recovered from matrices generated by random components

Note that they aren't the same unless scale is positive and shear is zero

In:= In:= In:= Out= Test that components are equal after two passes of decomp/comp. The 1st pass output is "canonical"

This always succeeds, so long as the precision is kept down

In:= In:= In:= Out= Examples

Note that Geometric transform does M . V, but that doesn't matter so long as it's consistent

Draw a transformed unit square, colored to make it easy to see axis flipping

In:= Draw some random examples

In:= Out= 