
\documentclass[11pt]{article}
\usepackage{fleqn}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{amssymb}
\usepackage{epsfig}
\usepackage{makeidx}
% if you want to include PostScript figures
\usepackage{graphicx}

\parindent =0pt
\parskip = 6pt
\tolerance = 10000

\newcommand{\aaa}{\ensuremath{\mathbf{a}}}
\newcommand{\ee}{\ensuremath{\mathbf{e}}}
\newcommand{\vv}{\ensuremath{\mathbf{v}}}
\newcommand{\uu}{\ensuremath{\mathbf{u}}}
\newcommand{\ww}{\ensuremath{\mathbf{w}}}
\newcommand{\xx}{\ensuremath{\mathbf{x}}}
\newcommand{\rr}{\ensuremath{\mathbf{r}}}
\newcommand{\pp}{\ensuremath{\mathbf{p}}}
\newcommand{\oo}{\ensuremath{\mathbf{o}}}
\newcommand{\sss}{\ensuremath{\mathbf{s}}}
% ===========================================================

\title{3D Modeling Transformations\\ Introduction to Computer Graphics \\ Arizona State University}

\author{Dianne Hansford}
\date{\today}


\begin{document}


\maketitle


\section{Introduction}\label{sec:intro}

In our last course notes, we stepped-through 2D transformations.
Now, 3D transformations should be easy!
The 3D transformation matrix $A$ is a $3\times 3$ matrix:
$$
    A=\begin{bmatrix}\aaa_1 & \aaa_2 & \aaa_3\end{bmatrix} =
    \begin{bmatrix}
       a_{11} & a_{12} & a_{13} \\
       a_{21} & a_{22} & a_{23} \\
       a_{31} & a_{32} & a_{33} \\
    \end{bmatrix}.
$$
The linear map, which takes a vector $\vv$ in the $[\ee_1, \ee_2, \ee_3]$ system to
a vector ${\hat \vv}$ in the $[\aaa_1,  \aaa_2,  \aaa_3]$ system, has the form
$\hat{\vv} = A\vv$. Therefore,
$$
\ee_1 \rightarrow \aaa_1 \qquad \ee_2 \rightarrow \aaa_2 \qquad
\ee_3 \rightarrow \aaa_3.
$$

One measure of the behavior of a linear map is the {\em determinant} of the matrix $A$, or $|A|$.
In 3D the determinant measures the {\em volume} of the parallelepiped formed by the
$\aaa_i$ vectors (positioned at the origin). Recall that you use co-factor expansion
to compute the determinant of a $3\times 3$ matrix.

One other notable difference between 2D and 3D transformations: rotations are not commutative in 3D. More on rotations below.


\section{3D Linear Maps}

\subsection{Scaling}
$$
A = \begin{bmatrix}
        s_{11} & 0 & 0\\
        0 & s_{22} & 0 \\
        0 & 0 & s_{33}\\
    \end{bmatrix}
    \qquad
    |A| = s_{11}s_{22}s_{33}
$$

\subsection{Reflection}
$$
A = \begin{bmatrix}
    \pm 1 & 0 & 0\\
    0 & \pm 1 & 0\\
    0 & 0 & \pm 1\\
    \end{bmatrix}
    \qquad\textrm{such that}\quad
    |A| = -1
$$

\subsection{Shear}
We can shear in one or more coordinate-axes. Let's look at some examples.

Shear parallel to the $\ee_1, \ee_2$-axes:
$$
    \ee_1 \rightarrow \ee_1 \qquad \ee_2 \rightarrow \ee_2 \qquad
    \ee_3 \rightarrow \begin{bmatrix} a \\ b \\ 1\end{bmatrix}.
$$
Therefore,
$$
A = \begin{bmatrix}
    1 & 0 & a\\
    0 & 1 & b \\
    0 & 0 & 1\\
    \end{bmatrix}.
$$
An arbitrary vector $\vv \rightarrow
\begin{bmatrix} v_1 + av_3\\ v_2 + bv_3 \\ v_3\end{bmatrix}$.

\vspace{12pt}

Shear parallel to the $\ee_2, \ee_3$-axes:
$$
A = \begin{bmatrix}
    1 & 0 & 0\\
    \star & 1 & 0\\
    \star & 0 & 1\\
    \end{bmatrix}.
$$

\vspace{12pt}


Shear parallel to the $\ee_1, \ee_3$-axes:
$$
A = \begin{bmatrix}
    1 & \star & 0\\
    0 & 1 & 0\\
    0 & \star & 1\\
    \end{bmatrix}.
$$

\vspace{12pt}
The determinant of a shear: $|A| = 1$, thus the volume of a sheared object does not change.

\vspace{12pt}
Here is a special shear that you have encountered before, most likely unknowingly:
$$
 \ee_1 \rightarrow \begin{bmatrix}1 \\ -b/a \\ -c/a\end{bmatrix}\qquad
 \ee_2 \rightarrow \ee_2 \qquad \ee_3 \rightarrow \ee_3,
$$
resulting in the following mapping
$$
    \begin{bmatrix}1 & 0 & 0 \\ -b/a & 1 & 0 \\ -c/a & 0 & 1\end{bmatrix}
    \begin{bmatrix}a \\ b \\ c \end{bmatrix} = \begin{bmatrix}a \\ 0 \\ 0 \end{bmatrix}.
$$
This is a Gauss Elimination step!



\subsection{Projection}
Projections ``flatten" our geometry. In computer graphics, we are interested in {\em parallel projections},
which are either {\em orthographic} or {\em oblique}.  The most common (linear map) projection
is the following
orthographic projection:
$$
A = \begin{bmatrix} 1 & 0 & 0\\ 0 & 1 & 0\\ 0 & 0 & 0\end{bmatrix},
$$
which projects all vectors in to the $\ee_1,\ee_2$-plane.

We will discuss projections in more detail when we get to Viewing. Then we will introduce the {\em perspective projection}, which isn't a linear map.
(The foreshortening process doesn't preserve linear combinations of vectors.)

\subsection{Rotation}
The rotation matrices will be set up so that the {\em right-hand rule} is preserved:
Align your right thumb with the positive axis about which you will rotate. Your fingers
will curl from one axis to another: A positive angle of rotation will rotate in
this direction.
Rotation about $\ee_3$:
$$
A = \begin{bmatrix}
    \cos(\alpha) & -\sin(\alpha) & 0\\
    \sin(\alpha) & \cos(\alpha) & 0\\
    0 & 0 & 1\\
    \end{bmatrix},
$$
rotates ``from $\ee_1$ to $\ee_2$". (Recall this is our 2D transformation.)

Rotation about $\ee_2$:
$$
A = \begin{bmatrix}
    \cos(\alpha) & 0 & \sin(\alpha)\\
    0 & 1 & 0\\
    -\sin(\alpha) & 0 & \cos(\alpha)\\

    \end{bmatrix},
$$
rotates from $\ee_3$ to $\ee_1$.

Rotation about $\ee_1$:
$$
A = \begin{bmatrix}
    1 & 0 & 0 \\
    0 & \cos(\alpha) & -\sin(\alpha)\\
    0 & \sin(\alpha) & \cos(\alpha)\\
    \end{bmatrix},
$$
rotates from $\ee_2$ to $\ee_3$.

The column vectors of a rotation matrix are orthogonal and unit length,
and thus {\em orthonormal}. A matrix with orthonormal column vectors is
 called {\em orthogonal}. These matrices have special
a property that is quite handy for computer graphics:
$$
  R^{-1}(\alpha) = R(-\alpha) = R^T(\alpha),
$$
so to reverse a rotation is as simple as applying the transpose of the rotation!

In 3D, matrix products {\em do not  commute}, and this includes rotations. (Recall
that rotations commute in 2D.) Sketch the following example. Let
\begin{itemize}
\item $R_{\ee 3}$ be a $90^\circ$ rotation
about $\ee_3$, and
\item $R_{\ee 1}$ be a $90^\circ$ rotation about $\ee_1$.
\end{itemize}
Try applying these rotations to the vector $\ee_1$. You'll see that
$$
\ee_2 = R_{\ee 3}R_{\ee 1}\ee_1 \qquad {\rm and} \qquad \ee_3=R_{\ee 1}R_{\ee 3}\ee_1.
$$
This simple example illustrates the non-commutivity of matrix multiplication.

\subsection{Properties}

A linear map,  $\hat{\vv} = A\vv$,
\begin{itemize}
\item Maps vectors $\rightarrow$ vectors
\item Preserves linear combinations of vectors. Suppose we have
$$
{\bf w} = \alpha {\bf u} + \beta {\bf v}
$$
Apply the linear map:
$$
\hat{\vv} = A\vv \qquad \hat{\uu} = A\uu \qquad \hat{\ww} = A\ww
$$
And the relationship still holds:
$$
\hat{\ww} = \alpha \hat{\uu} + \beta \hat{\vv}
$$
\end{itemize}


\section{3D Affine Maps}

An affine map in 3D takes the form
$$
  \hat{\xx} = A(\xx - {\bf o}) + \pp = A\xx + \pp.
$$
As before, for a point $\xx$ in the coordinate
system defined by ${\bf o}, [\ee_1, \ee_2, \ee_3]$, an affine map gives the
coordinates for the corresponding point $\hat{\xx}$ in the
 coordinate
system defined by ${\bf p}, [\aaa_1, \aaa_2, \aaa_3]$.

Affine maps have the following nice properties.
\begin{itemize}
\item The ratio of three collinear points is invariant.
\item Barycentric combinations are invariant.
\item Parallel planes are  mapped to parallel planes.
\end{itemize}



\section{Homogeneous Coordinates}

The homogeneous form of a 3D point
$\xx = \begin{bmatrix} x_1\\ x_2\\x_3\end{bmatrix}$ is
$\bar{\xx} = \begin{bmatrix} x_1x_4\\ x_2x_4\\x_3x_4 \\ x_4\end{bmatrix}$.
Most of the time, when you have a 3D point that you want to represent in
homogeneous coordinates, you will write it as
$\bar{\xx} = \begin{bmatrix} x_1\\ x_2\\x_3 \\ 1\end{bmatrix}$, that is, $x_4=1$.



Homogenous coordinates represent 4D points. The fourth coordinate is called the
$w$-coordinate. When we form a 3D point from a homogeneous point, we divide each coordinate by the $w$-coordinate,
$$
  \xx \approx \bar{\xx}
               = \begin{bmatrix} x_1x_4/x_4\\
                                x_2x_4/x_4\\x_3x_4/x_4\\ x_4/x_4\end{bmatrix}
               = \begin{bmatrix} x_1\\ x_2\\x_3 \\ 1\end{bmatrix}.
$$
We use the $\approx$ sign and say that ``$\xx$ corresponds to $\bar{\xx}$."
Commonly we think of the 3D point corresponding  to the 4D point with $w =1 $.
Although, all points on a line through the origin
are considered to be equivalent. In other words, there are many homogeneous representations of the same (affine) 3D point. This idea is more easily understood (and sketched) by considering the homogeneous form of a 2D point, which is a 3D point. All
points with $z=1$ are affine points.

Homogeneous coordinates serve two purposes in computer graphics:
\begin{enumerate}
\item A notation that allows  an affine map ($A$ and $\pp$) to be consolidated into one matrix.
\item A representation for a perspective projection in matrix form.
\end{enumerate}

In OpenGL, matrices are the cornerstone of the mathematics of the pipeline:
e.g., the modelview and projection matrices. Both of these matrices are $4\times 4$ in size, and they
arranged such that
$$
\begin{bmatrix} a_{11} & a_{12} & a_{13} & p_1\\
       a_{21} & a_{22} & a_{23}  & p_2\\
       a_{31} & a_{32} & a_{33} & p_3 \\
       0 & 0 & 0 & 1\end{bmatrix}
$$
or in short-hand notation:
$$
\begin{bmatrix} A & \pp \\
            \begin{matrix} 0 & 0 & 0 \end{matrix} & 1\end{bmatrix}.
$$
Later, the zeros in the bottom row will  be replaced with a perspective map definition.

Previously we defined an affine map as
$$
  \xx' = A\xx + \pp.
$$
To put this transformation in the context of OpenGL's matrix-oriented pipeline, let's
rewrite the affine map in matrix form with homogeneous coordinates:
\begin{align*}
  \bar{\xx}' &= \begin{bmatrix} I & \pp\\
               \begin{matrix} 0 & 0 & 0 \end{matrix} & 1\end{bmatrix}
            \begin{bmatrix} A & {\bf 0} \\
            \begin{matrix} 0 & 0 & 0 \end{matrix} & 1\end{bmatrix}\bar{\xx}\\
            &= \bar{T}\bar{A}\bar{\xx}\\
            &= \begin{bmatrix} A & \pp \\
            \begin{matrix} 0 & 0 & 0 \end{matrix} & 1\end{bmatrix}\bar{\xx}.
\end{align*}

\end{document}
