User:Thenewandyl/CSE4413 Notes

=Transformations=

Basics
Transformations manipulate points and vectors. For our purposes, they will be represented as matrices which are multiplied by a vector or point:

$$ \begin{bmatrix} a & b & c \\ d & e & f \\ g & h & i \end{bmatrix}

\begin{bmatrix} x \\ y \\ z \end{bmatrix} $$

$$ Mp = p'\, $$

$$ Mv = v'\, $$

Orthonormal Transforms
The matrices that define orthonormal transforms can be inversed simply by taking the transpose of the matrix.

Identity
The identity transform leaves things unchanged.

$$ \begin{bmatrix} 1 & 0 & 0 \\ 0 & 1 & 0 \\ 0 & 0 & 1 \end{bmatrix} $$

Rotations (about basis vector/axis)
Note: the convention is a right-handed coordinate system. Use the right-hand rule to figure out what is a positive rotation.

Rotations about $$\hat{x}$$
$$ \begin{bmatrix} 1 & 0 & 0 \\ 0 & \cos \theta & -\sin \theta \\ 0 & \sin \theta & \cos \theta \end{bmatrix} $$

Rotations about $$\hat{y}$$
$$ \begin{bmatrix} \cos \theta & 0 & \sin \theta \\ 0 & 1 & 0 \\ -\sin \theta & 0 & \cos \theta \end{bmatrix} $$

Rotations about $$\hat{z}$$
$$ \begin{bmatrix} \cos \theta & -\sin \theta & 0 \\ \sin \theta & \cos \theta & 0 \\ 0 & 0 & 1 \end{bmatrix} $$

Change of Basis
Change of Basis transforms change the frame of reference. They are usually used to transform from a local to global coordinate frame (and vis versa)

Say we have a coordinate system defined by $$\hat{u}, \hat{v}, \hat{n}$$ and want to transform to global $$\hat{x}, \hat{y}, \hat{z}$$ coordinates:

Assuming we know that:

$$ \hat{u}_{xyz} = [ u_x, u_y, u_z ] $$

$$ \hat{v}_{xyz} = [ v_x, v_y, v_z ] $$

$$ \hat{n}_{xyz} = [ n_x, n_y, n_z ] $$

We can derive that:

$$ p_{xyz} = (u_xp_u + v_xp_v + n_xp_n) \hat{x} + (u_yp_u + v_yp_v + n_yp_n) \hat{y} + (u_zp_u + v_zp_v + n_zp_n) \hat{z} $$

As a matrix:

$$ C_{{uvn} \rightarrow {xyz}} = \begin{bmatrix} u_x & v_x & n_x \\ u_y & v_y & n_y \\ u_z & v_z & n_z \end{bmatrix} $$

This matrix would be multiplied by a point/vector:

$$ \begin{bmatrix} u_x & v_x & n_x \\ u_y & v_y & n_y \\ u_z & v_z & n_z \end{bmatrix}

\begin{bmatrix} p_u \\ p_v \\ p_n \end{bmatrix} $$

Such that:

$$ C_{{uvn} \rightarrow {xyz}}\ p_{uvn} = p_{xyz} $$

Remember that the inverse is the transpose:

$$ [{C_{{uvn} \rightarrow {xyz}}}]^T = C_{{xyz} \rightarrow {uvn}} = \begin{bmatrix} u_x & u_y & u_z \\ v_x & v_y & v_z \\ n_x & n_y & n_z \end{bmatrix} $$

Rigid Body Transforms
Rigid Body Transforms do not have the property whereby their inverse is simply the transpose of the matrix.

Because some operations are additive (e.g., translations), we need to operate with a 4x4 matrix instead of a 3x3. The 3x3 partition in the top left will deal with rotations, shears and change of basis. The 3x1 partition in the top right will deal with translations. And the 1x4 partition on the bottom will deal with projections.

Translations
Translations move a point from one location to another. The matrix (in 4x4 form) for a translation is defined by:

$$ \begin{bmatrix} 1 & 0 & 0 & x \\ 0 & 1 & 0 & y \\ 0 & 0 & 1 & z \\ 0 & 0 & 0 & 1 \end{bmatrix} $$

Note the identity matrix in the 3x3 portion. This matrix would move the point a distance x along $$\hat{x}$$, y along $$\hat{y}$$, and z along $$\hat{z}$$.

The inverse of a translation is simply the negation.

Affine Transforms
Affine transforms are only guaranteed to preserve parallel lines, not angles or distances.

Scales
Scales make objects bigger or smaller in along chosen axes. If the scale vector for all axes are the same, we say the object is being scaled uniformally

The matrix representation of a scale is defined by:

$$ \begin{bmatrix} S_x & 0 & 0 & 0 \\ 0 & S_y & 0 & 0 \\ 0 & 0 & S_z & 0 \\ 0 & 0 & 0 & 1 \end{bmatrix} $$

Where $$S_x, S_y, S_z\,$$ are the scaling factors for each axis.

Reflection
Reflection reflects an object about a certain axis or axes. A reflection can be thought of as "negative scale," as the matrix is defined by:

$$ \begin{bmatrix} R_x & 0 & 0 & 0 \\ 0 & R_y & 0 & 0 \\ 0 & 0 & R_z & 0 \\ 0 & 0 & 0 & 1 \end{bmatrix} $$

Where $$R_x, R_y, R_z\,$$ are -1 or 1 depending on which coordinate is being changed. Note that you must think of it as the coordinate being changed, because, for instance $$R_y = -1\,$$ will result in a reflection across the x-axis, as all the y coordinates are "scaled" by -1.

Shearing
Shearing "pushes" an object along a dimension.

$$ S_{ab} = \tan \phi\ {along\ b\ axis} $$

Shearing along $$\hat{x}$$
$$ S_{along\ x} = \begin{bmatrix} 1 & 0 & 0 & 0 \\ S_{xy} & 1 & 0 & 0 \\ S_{xz} & 0 & 1 & 0 \\ 0 & 0 & 0 & 1 \end{bmatrix} $$

Shearing along $$\hat{y}$$
$$ S_{along\ y} = \begin{bmatrix} 1 & S_{yx} & 0 & 0 \\ 0 & 1 & 0 & 0 \\ 0 & S_{yz} & 1 & 0 \\ 0 & 0 & 0 & 1 \end{bmatrix} $$

Shearing along $$\hat{z}$$
$$ S_{along\ z} = \begin{bmatrix} 1 & 0 & S_{zx} & 0 \\ 0 & 1 & S_{zy} & 0\\ 0 & 0 & 1 & 0 \\ 0 & 0 & 0 & 1 \end{bmatrix} $$

Rotation about Arbitrary Axis
Note: There are different ways to accomplish this, especially during steps 2 and 3.


 * 1) Move to origin (translation)
 * 2) Rotation on $$xz\,$$ plane (about $$\hat{x}$$)
 * 3) Rotation onto $$z\,$$ (about $$\hat{y}$$)
 * 4) Do actual rotation about $$\hat{z}$$ (using original angle $$\theta\,$$)
 * 5) Undo first three steps in reverse order

The end result looks like:

$$ M = T(p)\ R_x(-\alpha)\ R_y(\beta)\ R_z(\theta)\ R_y(-\beta)\ R_x(\alpha)\ T(-p) $$

Coordinate Hierarchies
In real applications, each "object" will probably be defined in its own coordinate space. Furthermore, if an object can be broken down into parts that interact with one another in a hierarchical manner, such as a robot arm, each part may be described by its own coordinate system, which in turn is described in terms of its parent's coordinate system, and so forth, until the global coordinate system is reached.

In these cases, it's necessary to use a string of change of basis transforms to move from local to global coordinate systems.

For example, if we have a coordinate system like:

$$ {arm} \rightarrow {elbow} \rightarrow {wrist} \rightarrow {finger} $$

To change a point in "finger" coordinates to "arm" coordinates, we might use this string of change of basis matrices:

$$ p_{arm} = C_{{elbow} \rightarrow {arm}}\ C_{{wrist} \rightarrow {elbow}}\ C_{{finger} \rightarrow {wrist}}\ p_{finger} $$

All other transformations can be inserted into this string; however, you must remember to insert them between the appropriate change of basis matrices (i.e., if a transformation operates on "wrist" coordinates, make sure it is inserted between the $$C_{{wrist} \rightarrow {elbow}}$$ and $$C_{{finger} \rightarrow {wrist}}$$ matrices, as at that point, the coordinate system is defined using "wrist" coordinates.

OpenGL
To achieve coordinate hierarchies in OpenGL, call functions in the order they appear from left to right when written out as a matrix product (in reverse of the order they are actually applied).

In terms of a hierarchy, you would want to operate "down" the hierarchy from top to bottom. From the example, first specify the transforms to go from arm to elbow, elbow to wrist, wrist to finger. At each level, it might be helpful to use glPushMatrix and glPopMatrix, especially if the hierarchy branches.