Transformations

2.1 Transformations and Homogeneous Coordinates

Transformations: Translation (not linear), rotation, reflection, scaling, and shear.

We could leverage matrix multiplication without translation.

Homogeneous Coordinates: Use 4×4 matrices to represent translation and linear transformations together in 3D.

Scale:

\[ S_s = \begin{bmatrix} S_x & 0 & 0 & 0 \\ 0 & S_y & 0 & 0 \\ 0 & 0 & S_z & 0 \\ 0 & 0 & 0 & 1 \end{bmatrix} \]

Shear:

\[ H_{x,d} = \begin{bmatrix} 1 & d_y & d_z & 0 \\ 0 & 1 & 0 & 0 \\ 0 & 0 & 1 & 0 \\ 0 & 0 & 0 & 1 \end{bmatrix} \]

Translation:

\[ T_b = \begin{bmatrix} 1 & 0 & 0 & b_x \\ 0 & 1 & 0 & b_y \\ 0 & 0 & 1 & b_z \\ 0 & 0 & 0 & 1 \end{bmatrix} \]

2.2 Rotation

Topology: Structural properties of a manifold.

Two surfaces \(M\) and \(N\) are topologically equivalent if there is a differentiable bijection between \(M\) and \(N\).

Orientation: Space frame and body frame.

The Set of Rotations: The special orthogonal group is

\[ SO(n) = \{R \in \mathbb{R}^{n\times n} : \det(R)=1,\ RR^T=I\}. \]

The topology of \(SO(2)\) and \(SO(3)\) is different from \((-1,1)^n\), so parameterizing rotations in networks is tricky.

2.3 3D Rotation Representations

2.3.1 Euler Angles

\[ R_x(\alpha) := \begin{bmatrix} 1 & 0 & 0 \\ 0 & \cos\alpha & -\sin\alpha \\ 0 & \sin\alpha & \cos\alpha \end{bmatrix} \]

\[ R_y(\beta) := \begin{bmatrix} \cos\beta & 0 & \sin\beta \\ 0 & 1 & 0 \\ -\sin\beta & 0 & \cos\beta \end{bmatrix} \]

\[ R_z(\gamma) := \begin{bmatrix} \cos\gamma & -\sin\gamma & 0 \\ \sin\gamma & \cos\gamma & 0 \\ 0 & 0 & 1 \end{bmatrix} \]

For an arbitrary rotation,

\[ R = R_z(\gamma)R_y(\beta)R_x(\alpha). \]

Euler Angle is not unique for some rotations (gimbal lock).

2.3.2 Axis-Angle

Euler’s Rotation Theorem:

  • Any rotation in \(SO(3)\) is equivalent to rotation about a fixed axis \(\omega \in \mathbb{R}^3\) through a positive angle \(\theta\).

  • \(\hat{\omega}\): unit vector of rotation axis \((\|\hat{\omega}\| = 1)\).

  • \(\theta\): angle of rotation.

  • \(R \in SO(3) := \operatorname{Rot}(\hat{\omega}, \theta)\).

Skew-Symmetric Matrix:

  • \(A\) is skew-symmetric if

    \[ A = -A^T. \]

  • Skew-symmetric matrix operator:

    \[ \omega = \begin{bmatrix} \omega_1 \\ \omega_2 \\ \omega_3 \end{bmatrix}, \qquad [\omega] := \begin{bmatrix} 0 & -\omega_3 & \omega_2 \\ \omega_3 & 0 & -\omega_1 \\ -\omega_2 & \omega_1 & 0 \end{bmatrix}. \]

  • Cross product can be represented as a linear transformation:

    \[ a \times b = [a]b. \]

Rodrigues Formula:

  • Can prove that

    \[ [\hat{\omega}]^3 = -[\hat{\omega}]. \]

Then, use Taylor expansion of \(\sin\) and \(\cos\):

\[ e^{[\hat{\omega}]\theta} = I + [\hat{\omega}]\sin\theta + [\hat{\omega}]^2(1-\cos\theta). \]

Recover Axis-Angle from Rotation Matrix:

  • For \(0 < \theta < \pi\),

    \[ \begin{aligned} \theta &= \arccos\left(\frac{\operatorname{tr}(R)-1}{2}\right), \\ [\hat{\omega}] &= \frac{R-R^T}{2\sin\theta}. \end{aligned} \]

  • When \(\theta=0\), \(R=I\). There is no actual rotation, so the rotation axis \(\hat{\omega}\) can be arbitrary.

  • When \(\theta=\pi\), \((\hat{\omega},\pi)\) and \((-\hat{\omega},\pi)\) represent the same rotation.

2.3.3 Quaternion

Mathematical Definition:

Quaternion is a more generalized complex number:

\[ q = w + xi + yj + zk \]

  • \(w\) is the real part and \(\vec{v} = (x,y,z)\) is the imaginary part.

  • Imaginary units satisfy

    \[ i^2 = j^2 = k^2 = ijk = -1 \]

  • They are anti-commutative:

    \[ \begin{aligned} ij &= k = -ji, \\ jk &= i = -kj, \\ ki &= j = -ik. \end{aligned} \]

Properties of General Quaternions:

  • In vector form, the product of two quaternions:

    For \(q_1 = (w_1, \vec{v}_1)\) and \(q_2 = (w_2, \vec{v}_2)\),

    \[ q_1 q_2 = \left( w_1 w_2 - \vec{v}_1^{\,T}\vec{v}_2,\; w_1 \vec{v}_2 + w_2 \vec{v}_1 + \vec{v}_1 \times \vec{v}_2 \right). \]

  • Conjugate, norm, and inverse:

    \[ \begin{aligned} q^* &= (w, -\vec{v}), \\ \|q\|^2 &= w^2 + \vec{v}^{\,T}\vec{v} = qq^* = q^*q, \\ q^{-1} &:= \frac{q^*}{\|q\|^2}. \end{aligned} \]

Unit Quaternion as Rotation:

  • Rotate a vector \(\vec{x}\) by quaternion \(q\):

    1. Augment \(\vec{x}\) to

      \[ x = (0, \vec{x}) \]

    2. Rotate by

      \[ x' = qxq^{-1} \]

  • Compose rotations by quaternion:

    • First rotate by \(q_1\) and then by \(q_2\):

      \[ q_2(q_1xq_1^*)q_2^* \]

    • Since

      \[ q_2(q_1xq_1^*)q_2^* = (q_2q_1)x(q_1^*q_2^*), \]

      and

      \[ (q_2q_1)^* = q_1^*q_2^*, \]

      we conclude that composing rotations is as simple as multiplying quaternions:

      \[ q_{\text{total}} = q_2q_1. \]

Conversion between Quaternions and Axis-Angle:

  • Given the rotation axis \(\hat{\omega}\) and rotation angle \(\theta\),

    \[ q = \left[ \cos\left(\frac{\theta}{2}\right), \sin\left(\frac{\theta}{2}\right)\hat{\omega} \right]. \]

    Quaternion is very close to the angle-axis representation.

  • For a unit quaternion \(q=(w,\vec{v})\), the rotation angle is

    \[ \theta = 2\arccos(w). \]

    The rotation axis is

    \[ \hat{\omega} = \begin{cases} \displaystyle \frac{1}{\sin(\theta/2)}\vec{v}, & \theta\neq 0, \\[8pt] 0, & \theta=0. \end{cases} \]

Learning Perspective:

Each rotation corresponds to two quaternions. Networks need to normalize quaternions to unit length, which may cause large or small gradients in practice.

Summary of Rotation Representations:

Representation Inverse? Composing?
Rotation Matrix ✓ ✓
Euler Angle Complicated Complicated
Angle-axis ✓ Complicated
Skew-symmetrical Matrix ✓ Complicated
Quaternion ✓ ✓

2.4 Viewing Transformation

The standard graphics transformation pipeline is

\[ \begin{aligned} p_{\text{object}} &\xrightarrow{M} p_{\text{world}} \xrightarrow{V} p_{\text{camera}} \\ p_{\text{camera}} &\xrightarrow{P} p_{\text{canonical}} \xrightarrow{\text{viewport}} p_{\text{screen}}. \end{aligned} \]

where:

  • \(M\): modeling transformation
  • \(V\): view / camera transformation
  • \(P\): projection transformation
  • viewport: maps canonical coordinates to screen coordinates

The first three transformations are often combined as

\[ p_{\text{clip}} = PVM\,p_{\text{object}}. \]

2.4.1 Projection Transformation

  • 3D to 2D
  • Orthographic Projection vs. Perspective Projection

Orthographic Projection:

  • Camera located at origin, looking at -Z, up at Y
  • Drop Z coordinate
  • Translate and scale the resulting rectangle to \([-1, 1]^2\)

Translate the center to the origin first, then scale each dimension to length 2.

\[ M_{\text{ortho}} = \begin{bmatrix} \frac{2}{r-l} & 0 & 0 & 0 \\ 0 & \frac{2}{t-b} & 0 & 0 \\ 0 & 0 & \frac{2}{n-f} & 0 \\ 0 & 0 & 0 & 1 \end{bmatrix} \begin{bmatrix} 1 & 0 & 0 & -\frac{r+l}{2} \\ 0 & 1 & 0 & -\frac{t+b}{2} \\ 0 & 0 & 1 & -\frac{n+f}{2} \\ 0 & 0 & 0 & 1 \end{bmatrix} \]

Perspective Projection:

  • Most common in Computer Graphics
  • Further objects are smaller
  • Parallel lines not parallel; converge to single point

First “squish” the frustum into a cuboid, then apply orthographic projection.

\[ M_{\mathrm{persp}\to\mathrm{ortho}} = \begin{bmatrix} n & 0 & 0 & 0 \\ 0 & n & 0 & 0 \\ 0 & 0 & n+f & -nf \\ 0 & 0 & 1 & 0 \end{bmatrix} \]

\[ M_{\mathrm{ortho}} = \begin{bmatrix} \dfrac{2}{r-l} & 0 & 0 & -\dfrac{r+l}{r-l} \\[6pt] 0 & \dfrac{2}{t-b} & 0 & -\dfrac{t+b}{t-b} \\[6pt] 0 & 0 & -\dfrac{2}{f-n} & -\dfrac{f+n}{f-n} \\[6pt] 0 & 0 & 0 & 1 \end{bmatrix} \]

  • Aspect ratio = width / height
  • Field of view (FOV) = angle of the camera lens = \(2\arctan(\frac{h}{2n})\) where \(h\) is the height of the near plane and \(n\) is the distance from the camera to the near plane.

2.4.2 Viewport Transformation

  • After MVP (Model-View-Projection) transformation \(\rightarrow\) normalized device coordinate (NDC).

  • Transform the NDC space \((-1,1)^3\) to the screen space

    \[ (s_x,s_x+w_s)\times(s_y,s_y+h_s)\times(f_s,n_s) \]

    \(\rightarrow\) another affine transformation.

  • Usually set \(s_x,s_y\) to \(0\), treat \(w_s,h_s\) as the screen width and height, and set \(f_s,n_s\) to \(-1,1\) so that the \(z\) coordinates are untouched.