16
$\begingroup$

An affine transformation is composed of rotations, translations, scaling and shearing.

In 2D, such a transformation can be represented using an augmented matrix by $$ \begin{bmatrix} \vec{y} \\ 1 \end{bmatrix} =\begin{bmatrix} A & \vec{b} \ \\ 0, \ldots, 0 & 1 \end{bmatrix} \begin{bmatrix} \vec{x} \\ 1 \end{bmatrix} $$

vector b represents the translation. Bu how can I decompose A into rotation, scaling and shearing? I am trying to "interpolate" an affine transform so that, if I have points in a frame 1 and in a frame 3 such that a transform $T$ takes points from frame 1 to points in frame 3, then the interpolated transform would take my points from frame 1 to frame 2. If, for example, an 60° rotation is needed to go from frame 1 to frame 3, then a 30° one would be used to go from frame 1 to frame 2.

Thank you for your help.

$\endgroup$
  • 1
    $\begingroup$ I find this question of interpolation an interesting one, but also difficult. Essentially you'd be asking about “nice” paths in the parameter space of all transformations. But characterizing what is a nice path might be more difficult than it looks at first glance. While your example with rotation only looks easy enough, it might be difficult to distinguish rotational and other effects. Take e.g. a 180° rotation. For interpolation, you can choose either direction, but you could also treat this as a scaling by $-1$, and interpolate that. Can you define which interpretation is correct, and why? $\endgroup$ – MvG Dec 21 '13 at 9:27
  • 1
    $\begingroup$ What also has me worried is the fact that most shearing definitions have a close relation to the chosen coordinate system. As do anisotropic scalings. I'd concentrate on aspects that do not depend on coordinate system. Diagonalization of the matrix might proove useful here: you could then do a linear interpolation of the diagonal matrix between identity and the final matrix, and with a bit of luck that would result in reasonable geometric behavior. You'd have to be prepared to deal with complex eigenvalues, though. And this is quite a different approach to what your question here asks. $\endgroup$ – MvG Dec 21 '13 at 9:36
  • $\begingroup$ @PeterSheldrick but quaternions would work only for rotations wouldn't they? $\endgroup$ – bigTree Dec 21 '13 at 17:49
11
$\begingroup$

If you scale, shear, and rotate (in that order) then you'll have:

$$\begin{bmatrix} A_{11} & A_{12} \ \\ A_{21} & A_{22} \end{bmatrix} = \begin{bmatrix} \cos \theta & -\sin \theta \\ \sin \theta & \cos \theta \end{bmatrix} \begin{bmatrix} 1 & m \\ 0 & 1 \end{bmatrix} \begin{bmatrix} s & 0 \\ 0 & s \end{bmatrix}.$$

Then

$$\begin{align*} A_{11} &= s \cos \theta\\ A_{12} &= s m \cos \theta - s \sin \theta\\ A_{21} &= s \sin \theta\\ A_{22} &= s m \sin \theta + s \cos \theta\\ \end{align*} $$

From the first and third equations,

$$s = \sqrt{A_{11}^2 + A_{21}^2},$$

and

$$\theta = \tan^{-1}\left( \frac{A_{21}}{A_{11}} \right).$$

Finally, substituting the first and third into the second and fourth,

$$m = \frac{A_{12} + A_{21}}{A_{11}} = \frac{A_{22} - A_{11}}{A_{21}}.$$

$\endgroup$
  • 1
    $\begingroup$ I solved the equations and found the same results as you, but when I tried to test the equality for the matrix coefficients given by m (last substitution) on a real affine transformation matrix, it didn't work (the values are different...) $\endgroup$ – bigTree Dec 19 '13 at 20:40
  • $\begingroup$ It may depend on how you're doing the transformation. I guessed at the order of operations. I also assumed an $x$ shear rather than a $y$ shear. As well as uniform scaling, now that I look at it again. But if you know specifically how the transformation is made, then just order the matrices right to left and multiply them out. $\endgroup$ – John Dec 19 '13 at 21:05
  • 1
    $\begingroup$ Your equations are underspecified: you're solving for three variables $(s, m, \theta)$, but there's four parameters. One way to deal with this would be to add a $y$ shear as well. $\endgroup$ – Steve Jun 6 '15 at 4:16
  • 1
    $\begingroup$ I might miss something here, but affine transformation has scale x and scale y $\endgroup$ – TripleS Dec 21 '15 at 9:38
14
$\begingroup$

I am trying to "interpolate" an affine transform […]

I'm suggesting a different approach, which should be well suited to interpolation, although it does not depend on splitting the transformation into separate elementary operations the way your question suggests. Instead, I'd use fractional powers of a matrix, as I'll describe now.

Suppose $A$ is diagonalizable (which will be the case for most transformations), then there exists an orthogonal matrix $P$ and a diagonal matrix $D$ such that $A=P\,D\,P^{-1}$. The entries of $D$ are the eigenvalues of $A$, which I'll call $\lambda_1$ and $\lambda_2$. Now you can define $A$ raised to the $t$-th power like this:

$$ A^t = P\,D^t\,P^{-1} = P\,\begin{pmatrix}\lambda_1^t&0\\0&\lambda_2^t\end{pmatrix}\,P^{-1} $$

Now if you change $t$ continuously from $0$ to $1$, the matrix $A^t$ will change from identity to the matrix $A$. So this is your interpolation.

One thing you have to be careful about is the fact that the $\lambda_k$ will very likely be a conjugate pair of complex number. You can express them as $\lambda_k=e^{z_k}$, where $z_k=\log\lambda_k$, but the logarithm of a number is only defined up to multiples of $2\pi i$. So in this case, you should make sure that the imaginary part doesn't become too big, namely you want $-\pi\le\operatorname{Im}(z_k)\le\pi$. This ensures that the interpolation will not take more turns for a rotation than actually required. Furthermore, you should maintain $z_2=\bar{z_1}$ to make sure that the interpolating matrices will be real as well. With this choice, $\lambda_k^t=e^{t\cdot z_k}$ is well defined and behaves as you described for the case of rotation.

I've created a proof of concept implementation which you can use to experiment, in order to decide whether this is what you want. Here is a snapshot of the kind of interpolation this will create:

Interpolation snapshot

With that experiment, I realized that one should include the translative part into the matrix as well, in just the way you composed a matrix including $A$ and $b$ in your question. Otherwise, the positions of the interpolated frames will depend on the location of the defining triples in the plane, which I consider undesirable.

$\endgroup$
  • $\begingroup$ Therefore if we need to include b, the matrices P and D would be 3 by 3 matrices right? (I am currently trying to include your method in the code I have and will let you know when I have results) $\endgroup$ – bigTree Dec 21 '13 at 19:57
  • $\begingroup$ @bigTree: Yes, they are $3\times3$ matrices. I'll probably edit my post one day to use that version from the start, but not just now. Your code is matlab, right? Reading the docs, it seems mpower should help there, and do most of the work already. If you end up with a working matlab code snippet, it would be nice if you could share it. $\endgroup$ – MvG Dec 21 '13 at 21:08
  • $\begingroup$ I see the matrix no worries. My code is in matlab. I'll write the code and run some tests to see whether I encounter non diagonalizable matrices and share it. Thanks $\endgroup$ – bigTree Dec 21 '13 at 21:37
  • $\begingroup$ So I just tried to implement the code. It turns out it doesn't work because many of the matrices I have are singular... $\endgroup$ – bigTree Dec 22 '13 at 2:59
  • $\begingroup$ @bigTree: I think that this should work even for singular or non-diagonalizable matrices. But I'm not sure, and asked this question for details of matrices raised to fractional powers. $\endgroup$ – MvG Dec 22 '13 at 14:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.