Transformations - MATHEMATICAL PREREQUISITES - Introduction to 3D Game Programming with DirectX 12 (Computer Science) (2016)

Introduction to 3D Game Programming with DirectX 12 (Computer Science) (2016)

Part 1

MATHEMATICAL
P
REREQUISITES

Chapter 3

TRANSFORMATIONS

We describe objects in our 3D worlds geometrically; that is, as a collection of triangles that approximate the exterior surfaces of the objects. It would be an uninteresting world if our objects remained motionless. Thus we are interested in methods for transforming geometry; examples of geometric transformations are translation, rotation, and scaling. In this chapter, we develop matrix equations, which can be used to transform points and vectors in 3D space.

Objectives:

1. To understand how linear and affine transformations can be represented by matrices.

2. To learn the coordinate transformations for scaling, rotating, and translating geometry.

3. To discover how several transformation matrices can be combined into one net transformation matrix through matrix-matrix multiplication.

4. To find out how we can convert coordinates from one coordinate system to another, and how this change of coordinate transformation can be represented by a matrix.

5. To become familiar with the subset of functions provided by the DirectX Math library used for constructing transformation matrices.

3.1 LINEAR TRANSFORMATIONS

3.1.1 Definition

Consider the mathematical function τ(v) = τ(x, y, z) = (x′, y′, z′). This function inputs a 3D vector and outputs a 3D vector. We say that τ is a linear transformation if and only if the following properties hold:

image

where u = (ux, uy, uz) and v = (vx, vy, vz) are any 3D vectors, and k is a scalar.

image

A linear transformation can consist of input and output values other than 3D vectors, but we do not need such generality in a 3D graphics book.

image Example 3.1

Define the function τ(x, y, z) = (x2, y2, z2); for example, τ(1, 2, 3) = (1, 4, 9). This function is not linear since, for k = 2 and u = (1, 2, 3) we have:

τ(ku) = τ(2, 4, 6) = (4, 16, 36)

but

kτ(u) = 2(1, 4, 9) = (2, 8, 18)

So property 2 of Equation 3.1 is not satisfied.

If τ is linear, then it follows that:

image

We will use this result in the next section.

3.1.2 Matrix Representation

Let u = (x, y, z). Observe that we can always write this as:

u = (x, y, z) = xi + yj + zk = x(1, 0, 0) + y(0, 1, 0) + z(0, 0, 1)

The vectors i = (1, 0, 0), j = (0, 1, 0), and k = (0, 0, 1), which are unit vectors that aim along the working coordinate axes, respectively, are called the standard basis vectors for R3. (R3 denotes the set of all 3D coordinate vectors (x, y, z)). Now let τ be a linear transformation; by linearity (i.e., Equation 3.2), we have:

image

Observe that this is nothing more than a linear combination, which, as we learned in the previous chapter, can be written by a vector-matrix multiplication. By Equation 2.2 we may rewrite Equation 3.3 as:

image

where τ(i) = (A11, A12, A13), τ(j) = (A21, A22, A23), and τ(k) = (A31, A32, A33). We call the matrix A the matrix representation of the linear transformation τ.

3.1.3 Scaling

Scaling refers to changing the size of an object as shown in Figure 3.1.

image

Figure 3.1. The left pawn is the original object. The middle pawn is the original pawn scaled 2 units on the y-axis making it taller. The right pawn is the original pawn scaled 2 units on the x-axis making it fatter.

We define the scaling transformation by:

S(x, y, z) = (sxx, syy, szz)

This scales the vector by sx units on the x-axis, sy units on the y-axis, and sz units on the z-axis, relative to the origin of the working coordinate system. We now show that S is indeed a linear transformation. We have that:

image

image

Thus both properties of Equation 3.1 are satisfied, so S is linear, and thus there exists a matrix representation. To find the matrix representation, we just apply S to each of the standard basis vectors, as in Equation 3.3, and then place the resulting vectors into the rows of a matrix (as in Equation 3.4):

image

Thus the matrix representation of S is:

image

We call this matrix the scaling matrix.

The inverse of the scaling matrix is given by:

image

image Example 3.2

Suppose we have a square defined by a minimum point (−4, −4, 0) and a maximum point (4, 4, 0). Suppose now that we wish to scale the square 0.5 units on the x-axis, 2.0 units on the y-axis, and leave the z-axis unchanged. The corresponding scaling matrix is:

image

Now to actually scale (transform) the square, we multiply both the minimum point and maximum point by this matrix:

image

The result is shown in Figure 3.2.

image

Figure 3.2. Scaling by one-half units on the x-axis and two units on the y-axis. Note that when looking down the negative z-axis, the geometry is basically 2D since z = 0.

3.1.4 Rotation

In this section, we describe rotating a vector v about an axis n by an angle θ; see Figure 3.3. Note that we measure the angle clockwise when looking down the axis n; moreover, we assume ||n|| = 1.

image

Figure 3.3. The geometry of rotation about a vector n.

First, decompose v into two parts: one part parallel to n and the other part orthogonal to n. The parallel part is just projn(v) (recall Example 1.5); the orthogonal part is given by v = perpn(v) = v – projn(v). (Recall, also from Example 1.5, that since n is a unit vector, we have projn(v) = (n · v)n.) The key observation is that the part projn(v) that is parallel to n is invariant under the rotation, so we only need to figure out how to rotate the orthogonal part. That is, the rotated vector Rn(v) = projn(v) + Rn(v), by Figure 3.3.

To find Rn(v), we set up a 2D coordinate system in the plane of rotation. We will use v as one reference vector. To get a second reference vector orthogonal to v and n we take the cross product n × v (left-hand-thumb rule). From the trigonometry of Figure 3.3 and Exercise 14 of Chapter 1, we see that

image

where α is the angle between n and n. So both reference vectors have the same length and lie on the circle of rotation. Now that we have set up these two reference vectors, we see from trigonometry that:

image

This gives us the following rotation formula:

image

We leave it as an exercise to show that this is a linear transformation. To find the matrix representation, we just apply Rn to each of the standard basis vectors, as in Equation 3.3, and then place the resulting vectors into the rows of a matrix (as in Equation 3.4). The final result is:

image

where we let c = cosθ and s = sinθ.

The rotation matrices have an interesting property. Each row vector is unit length (verify) and the row vectors are mutually orthogonal (verify). Thus the row vectors are orthonormal (i.e., mutually orthogonal and unit length). A matrix whose rows are orthonormal is said to be an orthogonal matrix. An orthogonal matrix has the attractive property that its inverse is actually equal to its transpose. Thus, the inverse of Rn is:

image

In general, orthogonal matrices are desirable to work with since their inverses are easy and efficient to compute.

In particular, if we choose the x-, y-, and z-axes for rotation (i.e., n = (1, 0, 0), n = (0, 1, 0), and n = (0, 0, 1), respectively), then we get the following rotation matrices which rotate about the x-, y-, and z-axis, respectively:

image

image Example 3.3

Suppose we have a square defined by a minimum point (−1, 0, −1) and a maximum point (1, 0, 1). Suppose now that we wish to rotate the square −30° clockwise about the y-axis (i.e., 30° counterclockwise). In this case, n = (0, 1, 0), which simplifies Rn considerably; the corresponding y-axis rotation matrix is:

image

Now to actually rotate (transform) the square, we multiply both the minimum point and maximum point by this matrix:

image

The result is shown in Figure 3.4.

image

Figure 3.4. Rotating −30° clockwise around the y-axis. Note that when looking down the positive y-axis, the geometry is basically 2D since y = 0.

3.2 AFFINE TRANSFORMATIONS

3.2.1 Homogeneous Coordinates

We will see in the next section that an affine transformation is a linear transformation combined with a translation. However, translation does not make sense for vectors because a vector only describes direction and magnitude, independent of location; in other words, vectors should be unchanged under translations. Translations should only be applied to points (i.e., position vectors). Homogeneous coordinates provide a convenient notational mechanism that enables us to handle points and vectors uniformly. With homogeneous coordinates, we augment to 4-tuples and what we place in the fourth w-coordinate depends on whether we are describing a point or vector. Specifically, we write:

1. (x, y, z, 0) for vectors

2. (x, y, z, 1) for points

We will see later that setting w = 1 for points allows translations of points to work correctly, and setting w = 0 for vectors prevents the coordinates of vectors from being modified by translations (we do not want to translate the coordinates of a vector, as that would change its direction and magnitude—translations should not alter the properties of vectors).

image

The notation of homogeneous coordinates is consistent with the ideas shown in Figure 1.17. That is, the difference between two points qp = (qx, qy, qz, 1) − (px, py, pz, 1) = (qxpx, qypy, qzpz, 0) results in a vector, and a point plus a vector p + v = (px, py, pz, 1) + (vx, vy, vz, 1) = (px + vx, py + vy,pz + vz, 1) results in a point.

3.2.2 Definition and Matrix Representation

A linear transformation cannot describe all the transformations we wish to do; therefore, we augment to a larger class of functions called affine transformations. An affine transformation is a linear transformation plus a translation vector b; that is:

image

Or in matrix notation:

image

where A is the matrix representation of a linear transformation.

If we augment to homogeneous coordinates with w = 1, then we can write this more compactly as:

image

The 4 × 4 matrix in Equation 3.6 is called the matrix representation of the affine transformation.

Observe that the addition by b is essentially a translation (i.e., change in position). We do not want to apply this to vectors because vectors have no position. However, we still want to apply the linear part of the affine transformation to vectors. If we set w = 0 in the fourth component for vectors, then the translation by b is not applied (verify by doing the matrix multiplication).

image

Because the dot product of the row vector with the fourth column of the above 4 × 4 affine transformation matrix is: [x, y, z, w]·[0, 0, 0, 1] = w, this matrix does not modify the w-coordinate of the input vector.

3.2.3 Translation

The identity transformation is a linear transformation that just returns its argument; that is, I(u) = u. It can be shown that the matrix representation of this linear transformation is the identity matrix.

Now, we define the translation transformation to be the affine transformation whose linear transformation is the identity transformation; that is,

image

As you can see, this simply translates (or displaces) point u by b. Figure 3.5 illustrates how this could be used to displace objects—we translate every point on the object by the same vector b to move it.

image

Figure 3.5. Displacing the position of the ant by some displacement vector b.

By Equation 3.6, τ has the matrix representation:

image

This is called the translation matrix.

The inverse of the translation matrix is given by:

image

imageExample 3.4

Suppose we have a square defined by a minimum point (−8, 2, 0) and a maximum point (−2, 8, 0). Suppose now that we wish to translate the square 12 units on the x-axis, −10.0 units on the y-axis, and leave the z-axis unchanged. The corresponding translation matrix is:

image

Now to actually translate (transform) the square, we multiply both the minimum point and maximum point by this matrix:

image

The result is shown in Figure 3.6.

image

Figure 3.6. Translating 12 units on the x-axis and −10 units on the y-axis. Note that when looking down the negative z-axis, the geometry is basically 2D since z = 0.

image

Let T be a transformation matrix, and recall that we transform a point/vector by computing the product vT = v. Observe that if we transform a point/vector by T and then transform it again by the inverse T−1 we end up with the original vector: vTT−1 = vI = v. In other words, the inverse transformation undoes the transformation. For example, if we translate a point 5 units on the x-axis, and then translate by the inverse -5 units on the x-axis, we end up where we started. Likewise, if we rotate a point 30° about the y-axis, and then rotate by the inverse 30° about the y-axis, then we end up with our original point. In summary, the inverse of a transformation matrix does the opposite transformation such that the composition of the two transformations leaves the geometry unchanged.

3.2.4 Affine Matrices for Scaling and Rotation

Observe that if b = 0, the affine transformation reduces to a linear transformation. Thus we can express any linear transformation as an affine transformation with b = 0. This, in turn, means we can represent any linear transformation by a 4 × 4 affine matrix. For example, the scaling and rotation matrices written using 4 × 4 matrices are given as follows:

image

In this way, we can express all of our transformations consistently using 4 × 4 matrices and points and vectors using 1 × 4 homogeneous row vectors.

3.2.5 Geometric Interpretation of an Affine Transformation Matrix

In this section, we develop some intuition of what the numbers inside an affine transformation matrix mean geometrically. First, let us consider a rigid body transformation, which is essentially a shape preserving transformation. A real world example of a rigid body transformation might be picking a book off your desk and placing it on a bookshelf; during this process you are translating the book from your desk to the bookshelf, but also very likely changing the orientation of the book in the process (rotation). Let τ be a rotation transformation describing how we want to rotate an object and let b define a displacement vector describing how we want to translate an object. This rigid body transform can be described by the affine transformation:

image

In matrix notation, using homogeneous coordinates (w = 1 for points and w = 0 for vectors so that the translation is not applied to vectors), this is written as:

image

Now, to see what this equation is doing geometrically, all we need to do is graph the row vectors in the matrix (see Figure 3.7). Because τ is a rotation transformation it preserves lengths and angles; in particular, we see that τ is just rotating the standard basis vectors i, j, and k into a new orientation τ(i), τ(j), and τ(k). The vector b is just a position vector denoting a displacement from the origin. Now Figure 3.7 shows how the transformed point is obtained geometrically when α(x, y, z) = xτ(i) + yτ(j) + zτ(k) + b is computed.

image image

Figure 3.7. The geometry of the rows of an affine transformation matrix. The transformed point, α(p), is given as a linear combination of the transformed basis vectors τ(i) ,τ(j), τ(k), and the offset b.

The same idea applies to scaling or skew transformations. Consider the linear transformation τ that warps a square into a parallelogram as shown in Figure 3.8. The warped point is simply the linear combination of the warped basis vectors.

image

Figure 3.8. For a linear transformation that warps a square into a parallelogram, the transformed point τ(p) = (x, y) is given as a linear combination of the transformed basis vectors τ(i), τ(j).

3.3 COMPOSITION OF TRANSFORMATIONS

Suppose S is a scaling matrix, R is a rotation matrix, and T is a translation matrix. Assume we have a cube made up of eight vertices vi for i = 0, 1, …, 7, and we wish to apply these three transformations to each vertex successively. The obvious way to do this is step-by-step:

image

However, because matrix multiplication is associative, we can instead write this equivalently as:

image

We can think of the matrix C = SRT as a matrix that encapsulates all three transformations into one net transformation matrix. In other words, matrix-matrix multiplication allows us to concatenate transforms.

This has performance implications. To see this, assume that a 3D object is composed of 20,000 points and that we want to apply these three successive geometric transformations to the object. Using the step-by-step approach, we would require 20,000 × 3 vector-matrix multiplications. On the other hand, using the combined matrix approach requires 20,000 vector-matrix multiplications and 2 matrix-matrix multiplications. Clearly, two extra matrix-matrix multiplications is a cheap price to pay for the large savings in vector-matrix multiplications.

image

Again we point out that matrix multiplication is not commutative. This is even seen geometrically. For example, a rotation followed by a translation, which we can describe by the matrix product RT, does not result in the same transformation as the same translation followed by the same rotation, that is, TR. Figure 3.9 demonstrates this.

image

Figure 3.9. (a) Rotating first and then translating. (b) Translating first and then rotating.

3.4 CHANGE OF COORDINATE TRANSFORMATIONS

The scalar 100°C represents the temperature of boiling water relative to the Celsius scale. How do we describe the same temperature of boiling water relative to the Fahrenheit scale? In other words, what is the scalar, relative to the Fahrenheit scale, that represents the temperature of boiling water? To make this conversion (or change of frame), we need to know how the Celsius and Fahrenheit scales relate. They are related as follows: image

. Therefore, the temperature of boiling water relative to the Fahrenheit scale is given by image

.

This example illustrates that we can convert a scalar k that describes some quantity relative to a frame A into a new scalar k′ that describes the same quantity relative to a different frame B, provided that we knew how frame A and B were related. In the following subsections, we look at a similar problem, but instead of scalars, we are interested in how to convert the coordinates of a point/vector relative to one frame into coordinates relative to a different frame (see Figure 3.10). We call the transformation that converts coordinates from one frame into coordinates of another frame a change of coordinate transformation.

image

Figure 3.10. The same vector v has different coordinates when described relative to different frames. It has coordinates (x, y) relative to frame A and coordinates (x′, y′) relative to frame B.

It is worth emphasizing that in a change of coordinate transformation, we do not think of the geometry as changing; rather, we are changing the frame of reference, which thus changes the coordinate representation of the geometry. This is in contrast to how we usually think about rotations, translations, and scaling, where we think of actually physically moving or deforming the geometry.

In 3D computer graphics, we employ multiple coordinate systems, so we need to know how to convert from one to another. Because location is a property of points, but not of vectors, the change of coordinate transformation is different for points and vectors.

3.4.1 Vectors

Consider Figure 3.11, in which we have two frames A and B and a vector p. Suppose we are given the coordinates pA = (x, y) of p relative to frame A, and we wish to find the coordinates pB = (x′, y′) of p relative to frame B. In other words, given the coordinates identifying a vector relative to one frame, how do we find the coordinates that identify the same vector relative to a different frame?

image

Figure 3.11. The geometry of finding the coordinates of p relative to frame B.

From Figure 3.11, it is clear that

p = xu + yv

where u and v are unit vectors which aim, respectively, along the x- and y-axes of frame A. Expressing each vector in the above equation in frame B coordinates we get:

pB = xuB + yvB

Thus, if we are given pA = (x, y) and we know the coordinates of the vectors u and v relative to frame B, that is if we know uB = (ux, uy) and vB = (vx, vy), then we can always find pB = (x′, y′).

Generalizing to 3D, if pA = (x, y, z), then

pB = xuB + yvB + zwB

where u, v, and w are unit vectors which aim, respectively, along the x-, y- and z-axes of frame A.

3.4.2 Points

The change of coordinate transformation for points is slightly different than it is for vectors; this is because location is important for points, so we cannot translate points as we translated the vectors in Figure 3.11.

image

Figure 3.12. The geometry of finding the coordinates of p relative to frame B.

Figure 3.12 shows the situation, and we see that the point p can be expressed by the equation:

p = xu + yv + Q

where u and v are unit vectors which aim, respectively, along the x- and y-axes of frame A, and Q is the origin of frame A. Expressing each vector/point in the above equation in frame B coordinates we get:

pB = xuB + yvB + QB

Thus, if we are given pA = (x, y) and we know the coordinates of the vectors u and v, and origin Q relative to frame B, that is if we know uB = (ux, uy), vB = (vx, vy), and QB = (Qx, Qy), then we can always find pB = (x′, y′).

Generalizing to 3D, if pA = (x, y, z), then

pB = xuB + yvB + zwB + QB

where u, v, and w are unit vectors which aim, respectively, along the x-, y- and z-axes of frame A, and Q is the origin of frame A.

3.4.3 Matrix Representation

To review so far, the vector and point change of coordinate transformations are:

(x′, y′, z′) = xuB + yvB + zwB for vectors

(x′, y′, z′) = xuB + yvB + zwB + QB for points

If we use homogeneous coordinates, then we can handle vectors and points by one equation:

(x′, y′, z′, w) = xuB + yvB + zwB + wQB (eq. 3.8)

If w = 0, then this equation reduces to the change of coordinate transformation for vectors; if w = 1, then this equation reduces to the change of coordinate transformation for points. The advantage of Equation 3.8 is that it works for both vectors and points, provided we set the w-coordinates correctly; we no longer need two equations (one for vectors and one for points). Equation 2.3 says that we can write Equation 3.8 in the language of matrices:

image

where QB = (Qx, Qy, Qz, 1), uB = (ux, uy, uz, 0), vB = (vx, vy, vz, 0), and wB = (wx, wy, wz, 0) describe the origin and axes of frame A with homogeneous coordinates relative to frame B. We call the 4 × 4 matrix in Equation 3.9 a change of coordinate matrix or change of frame matrix, and we say it converts (or maps) frame A coordinates into frame B coordinates.

3.4.4 Associativity and Change of Coordinate Matrices

Suppose now that we have three frames F, G, and H. Moreover, let A be the change of frame matrix from F to G, and let B be the change of frame matrix from G to H. Suppose we have the coordinates pF of a vector relative to frame F and we want the coordinates of the same vector relative to frame H, that is, we want pH. One way to do this is step-by-step:

(pFA)B = pH

(pG)B = pH

However, because matrix multiplication is associative, we can instead rewrite (pFA)B = pH as:

pF(AB) = pH

In this sense, the matrix product C = AB can be thought of as the change of frame matrix from F directly to H; it combines the affects of A and B into a net matrix. (The idea is like composition of functions.)

This has performance implications. To see this, assume that a 3D object is composed of 20,000 points and that we want to apply two successive change of frame transformation to the object. Using the step-by-step approach, we would require 20,000 × 2 vector-matrix multiplications. On the other hand, using the combined matrix approach requires 20,000 vector-matrix multiplications and 1 matrix-matrix multiplication to combine the two change of frame matrices. Clearly, one extra matrix-matrix multiplication is a cheap price to pay for the large savings in vector-matrix multiplications.

image

Again, matrix multiplication is not commutative, so we expect that AB and BA do not represent the same composite transformation. More specifically, the order in which you multiply the matrices is the order in which the transformations are applied, and in general, it is not a commutative process.

3.4.5 Inverses and Change of Coordinate Matrices

Suppose that we are given pB (the coordinates of a vector p relative to frame B), and we are given the change of coordinate matrix M from frame A to frame B; that is, pB = pAM. We want to solve for pA. In other words, instead of mapping from frame A into frame B, we want the change of coordinate matrix that maps us from B into A. To find this matrix, suppose that M is invertible (i.e., M−1 exists). We can solve for pA like so:

image

Thus the matrix M−1 is the change of coordinate matrix from B into A.

Figure 3.13 illustrates the relationship between a change of coordinate matrix and its inverse. Also note that all of the change of frame mappings that we do in this book will be invertible, so we won’t have to worry about whether the inverse exists.

image

Figure 3.13. M maps A into B and M−1 maps from B into A.

Figure 3.14 shows how the matrix inverse property (AB)−1 = B−1A−1 can be interpreted in terms of change of coordinate matrices.

image

Figure 3.14. A maps from F into G, B maps from G into H, and AB maps from F directly into H. B−1 maps from H into G, A−1 maps from G into F and B−1A−1 maps from H directly into F.

3.5 TRANSFORMATION MATRIX VERSUS CHANGE OF COORDINATE MATRIX

So far we have distinguished between “active” transformations (scaling, rotation, translation) and change of coordinate transformations. We will see in this section that mathematically, the two are equivalent, and an active transformation can be interpreted as a change of coordinate transformation, and conversely.

Figure 3.15 shows the geometric resemblance between the rows in Equation 3.7 (rotation followed by translation affine transformation matrix) and the rows in Equation 3.9 (change of coordinate matrix).

image image

Figure 3.15. We see that b = Q, τ(i) = u, τ(j) = v, and τ(k) = w. (a) We work with one coordinate system, call it frame B, and we apply an affine transformation to the cube to change its position and orientation relative to frame B: α(x, y, z, w) = xτ(i) + yτ(j) + zτ(k) + wb. (b) We have two coordinate systems called frame A and frame B. The points of the cube relative to frame A can be converted to frame B coordinates by the formula pB = xuB + yvB + zwB + wQB, where pA =(x, y, z, w). In both cases, we have α(p) = (x′, y′, z′, w) = pB with coordinates relative to frame B.

If we think about this, it makes sense. For with a change of coordinate transformation, the frames differ in position and orientation. Therefore, the mathematical conversion formula to go from one frame to the other would require rotating and translating the coordinates, and so we end up with the same mathematical form. In either case, we end up with the same numbers; the difference is the way we interpret the transformation. For some situations, it is more intuitive to work with multiple coordinate systems and convert between the systems where the object remains unchanged, but its coordinate representation changes since it is being described relative to a different frame of reference (this situation corresponds with Figure 3.15b). Other times, we want to transform an object inside a coordinate system without changing our frame of reference (this situation corresponds with Figure 3.15a).

image

In particular, this discussion shows that we can interpret a composition of active transformations (scaling, rotation, translation) as a change of coordinate transformation. This is important because we will often define our world space (Chapter 5) change of coordinate matrix as a composition of scaling, rotation, and translation transformations.

3.6 DIRECTX MATH TRANSFORMATION FUNCTIONS

We summarize the DirectX Math related transformation functions for reference.

// Constructs a scaling matrix:

XMMATRIX XM_CALLCONV XMMatrixScaling(

float ScaleX,

float ScaleY,

float ScaleZ); // Scaling factors

// Constructs a scaling matrix from components in vector:

XMMATRIX XM_CALLCONV XMMatrixScalingFromVector(

FXMVECTOR Scale); // Scaling factors (sx, sy, sz)

// Constructs a x-axis rotation matrix Rx:

XMMATRIX XM_CALLCONV XMMatrixRotationX(

float Angle); // Clockwise angle θ to rotate

// Constructs a y-axis rotation matrix Ry:

XMMATRIX XM_CALLCONV XMMatrixRotationY(

float Angle); // Clockwise angle θ to rotate

// Constructs a z-axis rotation matrix Rz:

XMMATRIX XM_CALLCONV XMMatrixRotationZ(

float Angle); // Clockwise angle θ to rotate

// Constructs an arbitrary axis rotation matrix Rn:

XMMATRIX XM_CALLCONV XMMatrixRotationAxis(

FXMVECTOR Axis, // Axis n to rotate about

float Angle); // Clockwise angle θ to rotate

Constructs a translation matrix:

XMMATRIX XM_CALLCONV XMMatrixTranslation(

float OffsetX,

float OffsetY,

float OffsetZ); // Translation factors

Constructs a translation matrix from components in a vector:

XMMATRIX XM_CALLCONV XMMatrixTranslationFromVector(

FXMVECTOR Offset); // Translation factors (tx, ty, tz)

// Computes the vector-matrix product vM where vw = 1 for transforming points:

XMVECTOR XM_CALLCONV XMVector3TransformCoord(

FXMVECTOR V, // Input v

CXMMATRIX M); // Input M

// Computes the vector-matrix product vM where vw = 0 for transforming vectors:

XMVECTOR XM_CALLCONV XMVector3TransformNormal(

FXMVECTOR V, // Input v

CXMMATRIX M); // Input M

For the last two functions XMVector3TransformCoord and XMVector3TransformNormal, you do not need to explicitly set the w coordinate. The functions will always use vw = 1 and vw = 0 for XMVector3TransformCoord and XMVector3TransformNormal, respectively.

3.7 SUMMARY

1. The fundamental transformation matrices—scaling, rotation, and translation—are given by:

image

2. We use 4 × 4 matrices to represent transformations and 1 × 4 homogeneous coordinates to describe points and vectors, where we denote a point by setting the fourth component to w = 1 and a vector by setting w = 0. In this way, translations are applied to points but not to vectors.

3. A matrix is orthogonal if all of its row vectors are of unit length and mutually orthogonal. An orthogonal matrix has the special property that its inverse is equal to its transpose, thereby making the inverse easy and efficient to compute. All the rotation matrices are orthogonal.

4. From the associative property of matrix multiplication, we can combine several transformation matrices into one transformation matrix, which represents the net effect of applying the individual matrices sequentially.

5. Let QB, uB, vB, and WB describe the origin, x-, y-, and z-axes of frame A with coordinates relative to frame B, respectively. If a vector/point p has coordinates pA = (x, y, z) relative to frame A, then the same vector/point relative to frame B has coordinates:

1. pB = (x′, y′, z′) = xuB + yvB + zwB For vectors (direction and magnitude)

2. pB = (x′, y′, z′) = QB + xuB + yvB + zwB For position vectors (points)
These change of coordinate transformations can be written in terms of matrices using homogeneous coordinates.

6. Suppose we have three frames, F, G, and H, and let A be the change of frame matrix from F to G, and let B be the change of frame matrix from G to H. Using matrix-matrix multiplication, the matrix C = AB can be thought of as the change of frame matrix F directly to H; that is, matrix-matrix multiplication combines the effects of A and B into one net matrix, and so we can write: pF(AB) = pH.

7. If the matrix M maps frame A coordinates into frame B coordinates, then the matrix M−1 maps frame B coordinates into frame A coordinates.

8. An active transformation can be interpreted as a change of coordinate transformation, and conversely. For some situations, it is more intuitive to work with multiple coordinate systems and convert between the systems where the object remains unchanged, but its coordinate representation changes since it is being described relative to a different frame of reference. Other times, we want to transform an object inside a coordinate system without changing our frame of reference of reference.

3.8 EXERCISES

1. Let τ: R3R3 be defined by τ(x, y, z) = (x + y, x – 3, z). Is τ a linear transformation? If it is, find its standard matrix representation.

2. Let τ: R3R3 be defined by τ(x, y, z) = (3x + 4z, 2xz, x + y + z). Is τ a linear transformation? If it is, find its standard matrix representation.

3. Assume that τ: R3R3 is a linear transformation. Further suppose that τ(1, 0, 0) = (3, 1, 2), τ(0, 1, 0) = (2, -1, 3), and τ(0, 0, 1) = (4, 0, 2). Find τ(1, 1, 1).

4. Build a scaling matrix that scales 2 units on the x-axis, −3 units on the y-axis, and keeps the z-dimension unchanged.

5. Build a rotation matrix that rotates 30° along the axis (1, 1, 1).

6. Build a translation matrix that translates 4 units on the x-axis, no units on the y-axis, and -9 units on the z-axis.

7. Build a single transformation matrix that first scales 2 units on the x-axis, −3 units on the y-axis, and keeps the z-dimension unchanged, and then translates 4 units on the x-axis, no units on the y-axis, and -9 units on the z-axis.

8. Build a single transformation matrix that first rotates 45° about the y-axis and then translates −2 units on the x-axis, 5 units on the y-axis, and 1 unit on the z-axis.

9. Redo Example 3.2, but this time scale the square 1.5 units on the x-axis, 0.75 units on the y-axis, and leave the z-axis unchanged. Graph the geometry before and after the transformation to confirm your work.

10.Redo Example 3.3, but this time rotate the square −45° clockwise about the y-axis (i.e., 45° counterclockwise). Graph the geometry before and after the transformation to confirm your work.

11.Redo Example 3.4, but this time translate the square −5 units on the x-axis, -3.0 units on the y-axis, and 4.0 units on the z-axis. Graph the geometry before and after the transformation to confirm your work.

12.Show that Rn(v) = cosθv + (1 − cosθ) (n·v)n + sinθ(n × v) is a linear transformation and find its standard matrix representation.

13.Prove that the rows of Ry are orthonormal. For a more computational intensive exercise, the reader can do this for the general rotation matrix (rotation matrix about an arbitrary axis), too.

14.Prove the matrix M is orthogonal if and only if MT = M−1.

15.Compute:
image
Does the translation translate points? Does the translation translate vectors? Why does it not make sense to translate the coordinates of a vector in standard position?

16.Verify that the given scaling matrix inverse is indeed the inverse of the scaling matrix; that is, show, by directly doing the matrix multiplication, SS−1 = S−1S = I. Similarly, verify that the given translation matrix inverse is indeed the inverse of the translation matrix; that is, show that TT−1 = T−1T = I

17.Suppose that we have frames A and B. Let pA = (1, −2, 0) and qA = (1, 2, 0) represent a point and force, respectively, relative to frame A. Moreover, let
image
describe frame A with coordinates relative to frame B. Build the change of coordinate matrix that maps frame A coordinates into frame B coordinates, and find pB = (x, y, z) and qB = (x, y, z). Draw a picture on graph paper to verify that your answer is reasonable.

18.The analog for points to a linear combination of vectors is an affine combination: p = a1p1 + … + anpn where a1 + … + an = 1and p1, …, pn are points. The scalar coefficient ak can be thought of as a “point” weight that describe how much influence the point pk has in determining p; loosely speaking, the closer ak is to 1, the closer p will be to pk, and a negative ak “repels” p from pk. (The next exercise will help you develop some intuition on this.) The weights are also known as barycentric coordinates. Show that an affine combination can be written as a point plus a vector:

image

19.Consider the triangle defined by the points p1 = (0, 0, 0), p2 = (0, 1, 0), and p3 = (2, 0, 0). Graph the following points:

1. image

2. 0.7p1 + 0.2p2 + 0.1p3

3. 0.0p1 + 0.5p2 + 0.5p3

4. −0.2p1 + 0.6p2 + 0.6p3

5. 0.6p1 + 0.5p2 − 0.1p3

6. 0.8p1 − 0.3p2 + 0.5p3
What is special about the point in part (a)? What would be the barycentric coordinates of p2 and the point (1, 0, 0) in terms of p1, p2, p3? Can you make a conjecturer about where the point p will be located relative to the triangle if one of the barycentric coordinates is negative?

20.One of the defining factors of an affine transformation is that it preserves affine combinations. Prove that the affine transformation α(u) preserves affine transformations; that is, image

where a1 + … + an = 1.

21.Consider Figure 3.16. A common change of coordinate transformation in computer graphics is to map coordinates from frame A (the square [−1, 1]2) to frame B (the square [0, 1]2 where the y-axes aims opposite to the one in Frame A). Prove that the change of coordinate transformation from Frame A to Frame B is given by:
image

image

Figure 3.16. Change of coordinates from frame A (the square [−1, 1]2) to frame B (the square [0, 1]2 where the y-axes aims opposite to the one in Frame A)

22.It was mentioned in the last chapter that the determinant was related to the change in volume of a box under a linear transformation. Find the determinant of the scaling matrix and interpret the result in terms of volume.

23.Consider the transformation τ that warps a square into a parallelogram given by:

image

Find the standard matrix representation of this transformation, and show that the determinant of the transformation matrix is equal to the area of the parallelogram spanned by τ(i) and τ(j).

image

Figure 3.17. Transformation that maps square into parallelogram.

24.Show that the determinant of the y-axis rotation matrix is 1. Based on the above exercise, explain why it makes sense that it is 1. For a more computational intensive exercise, the reader can show the determinant of the general rotation matrix (rotation matrix about an arbitrary axis) is 1.

25.A rotation matrix can be characterized algebraically as an orthogonal matrix with determinant equal to 1. If we reexamine Figure 3.7 along with Exercise 24 this makes sense; the rotated basis vectors τ(i) , τ(j), and τ(k) are unit length and mutually orthogonal; moreover, rotation does not change the size of the object, so the determinant should be 1. Show that the product of two rotation matrices R1R2 = R is a rotation matrix. That is, show RRT = RTR = I (to show R is orthogonal), and show det R = 1.

26.Show that the following properties hold for a rotation matrix R:

image

Explain why all these properties make sense for a rotation transformation.

27.Find a scaling, rotation, and translation matrix whose product transforms the line segment with start point p = (0, 0, 0) and endpoint q = (0, 0, 1) into the line segment with length 2, parallel to the vector (1, 1, 1), with start point (3, 1, 2).

28.Suppose we have a box positioned at (x, y, z). The scaling transform we have defined uses the origin as a reference point for the scaling, so scaling this box (not centered about the origin) has the side effect of translating the box (Figure 3.18); this can be undesirable in some situations. Find a transformation that scales the box relative to its center point.

image

Change coordinates to the box coordinate system with origin at the center of the box, scale the box, then transform back to the original coordinate system.

image

Figure 3.18. (a) Scaling 2-units on the x-axis relative to the origin results in a translation of the rectangle. (b) Scaling 2-units on the x-axis relative to the center of the rectangle does not result in a translation (the rectangle maintains its original center point).





All materials on the site are licensed Creative Commons Attribution-Sharealike 3.0 Unported CC BY-SA 3.0 & GNU Free Documentation License (GFDL)

If you are the copyright holder of any material contained on our site and intend to remove it, please contact our site administrator for approval.

© 2016-2024 All site design rights belong to S.Y.A.