In [ ]:
%matplotlib inline

In [ ]:
import math
import numpy as np
import matplotlib.pyplot as plt

On 2D vectors

Rotation matrix

$$ R = \begin{pmatrix} cos(\theta) & -sin(\theta) \\ sin(\theta) & cos(\theta) \\ \end{pmatrix} $$

In [ ]:
X = np.random.randint(10, size=[5,2])
X

In [ ]:
plt.plot(X[:,0], X[:,1]);

In [ ]:
angle = math.radians(180)

R = np.array([[math.cos(angle), -math.sin(angle)],
              [math.sin(angle), math.cos(angle)]])

In [ ]:
Xr = np.dot(R, X.T).T

In [ ]:
Xr

In [ ]:
plt.plot(X[:,0], X[:,1])
plt.plot(Xr[:,0], Xr[:,1]);

Rotate and shear

https://github.com/cta-observatory/ctapipe/blob/master/ctapipe/image/geometry_converter.py#L44

The correction on the pixel position r can be described by a rotation R around one angle and a sheer S along a certain axis:

$$r' = S \cdot R \cdot r$$$$ \begin{pmatrix} x' \\ y' \end{pmatrix} = \begin{pmatrix} 1 & 0 \\ -1/\tan & 1 \end{pmatrix} \cdot \begin{pmatrix} \cos & -\sin \\ \sin & \cos \end{pmatrix} \cdot \begin{pmatrix} x \\ y \end{pmatrix} $$$$ \begin{pmatrix} x' \\ y' \end{pmatrix} = \begin{pmatrix} \cos & -\sin \\ \sin-\cos/\tan & \sin/\tan+\cos \end{pmatrix} \cdot \begin{pmatrix} x \\ y \end{pmatrix} $$

To revert the rotation, we need to find matrices $S'$ and $R'$ with $S' \cdot S = 1$ and $R' \cdot R = 1$ so that $r = R' \cdot S' \cdot S \cdot R \cdot r = R' \cdot S' \cdot r'$:

$$ \begin{pmatrix} x \\ y \end{pmatrix} = \begin{pmatrix} \cos & \sin \\ -\sin & \cos \end{pmatrix} \cdot \begin{pmatrix} 1 & 0 \\ 1/\tan & 1 \end{pmatrix} \cdot \begin{pmatrix} x' \\ y' \end{pmatrix} $$$$ \begin{pmatrix} x \\ y \end{pmatrix} = \begin{pmatrix} \cos+\sin/\tan & \sin \\ \cos/\tan-\sin & \cos \end{pmatrix} \cdot \begin{pmatrix} x' \\ y' \end{pmatrix} $$

On 3D vectors

Along the $z$ axis

Source:

 Translation matrix

$$ T = \begin{pmatrix} 1 & 0 & 0 \\ 0 & 1 & 0 \\ t_x & t_y & 1 \\ \end{pmatrix} $$

Scale matrix

$$ S = \begin{pmatrix} s_x & 0 & 0 \\ 0 & s_x & 0 \\ 0 & 0 & 1 \\ \end{pmatrix} $$

Rotation matrix

$$ R = \begin{pmatrix} cos(\theta) & -sin(\theta) & 0 \\ sin(\theta) & cos(\theta) & 0 \\ 0 & 0 & 1 \\ \end{pmatrix} $$

General transformation matrix

$$ R = \begin{pmatrix} s_x . cos(\theta) & s_x . -sin(\theta) & 0 \\ s_y sin(\theta) & s_y cos(\theta) & 0 \\ t_x & t_y & 1 \\ \end{pmatrix} $$