Geometric hash code or is there a unique affine transformation mapping two 2D points onto (0,0) and (1,1)? How to compute it

linear algebra

I have a 2D transformation $T$ composed by a scale $\lambda$, a rotation by angle $\theta$ and a translation vector $\begin{bmatrix}t_x\\t_y\end{bmatrix}$:
$$
T=\begin{bmatrix}
\lambda\cos(\theta) & -\lambda\sin(\theta) & t_x \\
\lambda\sin(\theta) & \lambda\cos(\theta) & t_y \\
0 & 0 & 1 \\
\end{bmatrix}
$$

$T$ operates on 2D point $P$ expressed with homogeneous coordinates:
$$
P=\begin{bmatrix}
x\\
y\\
1
\end{bmatrix}
$$

$T$ maps 2D $(x,y)$ point to 2D $(u,v)$ point according to:
$$
\begin{bmatrix}u\\v\\1\end{bmatrix}=TP
$$

Now I have
$$
A=\begin{bmatrix}x_A\\y_A\\1\end{bmatrix},B=\begin{bmatrix}x_B\\y_B\\1\end{bmatrix}
$$

I would like to map $A$ to $(0,0)$ and $B$ to $(1,1)$:

$$\begin{bmatrix}0\\0\\1\end{bmatrix}=TA$$

$$\begin{bmatrix}1\\1\\1\end{bmatrix}=TB$$

Is $T$ unique?

How can I compute $T$?


Background information:

I am trying to compute an hash code as described in

LANG, Dustin, et al. Astrometry.net: Blind astrometric calibration of arbitrary astronomical images. The astronomical journal, 2010, 139.5: 1782.

See here Figure 1 from the above paper:
geometric hash code for a quad of stars

Best Answer

Herein bold Latin letters, ${\bf A}, {\bf B}, \ldots$, denote vectors in $\Bbb R^2$, so that the corresponding homogeneous coordinates are $\pmatrix{{\bf A}\\1}$, etc.

Yes, there is a unique solution, and we can see this uniqueness by carrying out a more-or-less explicit construction of the map itself. One way to do this intuitively is to apply three transformations of the form $T$ successively:

  • one translation, $T_{\bf R} = \pmatrix{1&&r\\&1&s\\&&1}$---we denote ${\bf R} := \pmatrix{r\\s}$---
  • one rotation, $T_{\theta} = \pmatrix{\cos \theta&-\sin\theta&\\ \sin\theta&\cos\theta&\\&&1}$, and
  • one dilation, $T_{\lambda} = \pmatrix{\lambda&&\\&\lambda&\\&&1}$.

It's straightforward to check that a composition of two transformations of the given form also has that form, so $$T := T_{\lambda} \circ T_{\theta} \circ T_{\bf R}$$ will have that form.

First, note that rotations and dilations fix $\pmatrix{{\bf 0}\\1}$. So, since $T\pmatrix{{\bf A}\\1} = 0$, we must have $$\pmatrix{{\bf 0}\\1} = T_{\bf R}\pmatrix{{\bf A}\\1} = \pmatrix{{\bf A} + {\bf R}\\1}, $$ that is, $${\bf R} = -{\bf A} .$$

Any dilation by a positive constant $\lambda$ fixes rays, so if $T$ maps $\pmatrix{{\bf B}\\1}$ to $\pmatrix{1\\1\\1}$, the rotation by $\theta$ must map ${\bf B}' := {\bf B} - {\bf A}$ to the ray spanned by $(1, 1)$, and there is only one rotation that achieves this: If ${\bf B}'$ makes a (signed) angle $\alpha$ with the positive $x$-axis, then we can take $\theta = \frac{\pi}{4} - \alpha$. If you prefer an explicit formula, we can take $$\theta = \frac{\pi}{4} - \operatorname{atan2}(x_{{\bf B}'}, y_{{\bf B}'}) = \frac{\pi}{4} - \operatorname{atan2}(y_{\bf B} - y_{\bf A}, x_{\bf B} - x_{\bf A}) .$$ A little trigonometry, including the angle sum identities, then gives the more convenient formulas $$\begin{align*} \cos \theta &= \frac{x_{{\bf B}'} + y_{{\bf B}'}}{\sqrt{2} |{\bf B}'|} \\ \sin \theta &= \frac{x_{{\bf B}'} - y_{{\bf B}'}}{\sqrt{2} |{\bf B}'|} \end{align*} .$$

Finally, the vector ${\bf B}''$ produced by rotating ${\bf B}'$ by an angle $\theta$ has length $|{\bf B}''| = |{\bf B} - {\bf A}|$ and $(1, 1)$ has length $\sqrt{2}$, so $$\lambda = \sqrt{2} |{\bf B} - {\bf A}|^{-1} .$$

If we want to produce explicit parameters, expanding $T = T_{\lambda} \circ T_{\theta} \circ T_{-{\bf A}}$ gives that $T$ is the transformation $$T = \pmatrix{\lambda \cos \theta & -\lambda \sin \theta & t_x \\ \lambda \sin \theta & \lambda \cos \theta & t_y \\ &&1},$$ where $$\begin{align} t_x &= \lambda (-x_{\bf A} \cos \theta + y_{\bf A} \sin \theta) \\ t_y &= \lambda (-y_{\bf A} \sin \theta - x_{\bf A} \cos \theta) \end{align} .$$

Related Question