Rotation and Translation in Computer Graphics using 4 x 4 Homogenous Matrices and Quaternions Lab. Eng., MSc Matti Pitkälä Department of Automation Laboratory, Lahti Polytechnic Lahti, Finland Email: [email protected] Abstract This paper introduces some of the key ideas in computer graphics, for example how we use the principles of mathematics using transformations such as rotation and translation in 2D and 3D-viewing. What are possibilities to achieve them in computer graphics. 1. Introduction 1.1 Overview of the problem Computer graphics deals with the problem of image synthesis. Given a model (usually mathematically based) the problem of computer graphics is to produce realistic image data which may be viewed on a graphics display device. The process of producing the image data from the scene model is called rendering. That images are synthesized from mathematical models implies that computer graphics is a mathematically based subject. Image synthesis involves the physics of light, properties of materials, etc. Animated imagery involves finite element analysis, kinematics, sampling theory and other mathematically based fields. The study of computer graphics necessarily involves the study of many areas of mathematics. An elementary view of some of these topics are given in the following sections. Persons, who have an interest in computer graphics should study so much mathematics as possible. The reverse problem of starting with image data and recovering information is called image processing. 1.2 2D Viewing We consider the problem of representing 2D graphics images, which may be drawn as a sequence of connected line segments. Such images may be represented as a matrix of 2D points [xi yi]. For example pseudo-programming notation is used to describe the viewing transformations and data object representations. square =:0 0 10 0 10 10 0 10 0 0 Figure 1: A square The idea behind this representation is that the first point represents the starting point of the first line segment drawn while the second point represents the end of the first line segment and the starting point of the second line segment. The drawing of line segments continues in similar fashion until all line segments have been drawn. A matrix having n+1 points describes a figure consisting of n-line segments. It is sometimes useful to think of each pair of consecutive points in this matrix representation: As a vector so that the square shown in Figure1 is the result of drawing the vectors shown in Figure 2. Figure 2: The Vectors in a square 1.2. 1 Rotation Now we wish to rotate a figure around the origin of 2D coordinate system. Figure 3 shows the point being rotated degrees (by convention, counter clock-wise direction is positive) about the origin. Figure 3: Rotating a point about the origin To rotate point (x1,y1) to point (x2,y2) we observe: From the illustration we know that sin(A+B) = y2/r cos(A+B) = x2/r sin(A) = y1/r cos(A) = x1/r And now, if we for example want to rotate our square 90degrees counter-clockwise (+), we get for the matrix: cos90=0, sin90=1,-sin90=-1 and cos90=0 Æ 0 1 -1 0 1) (0, 0)* 0 1 Æ new point1 is (0, 0) -1 0 2) (10, 0)* matrixÆ new point2 is (0, 10) and so on….. Figure 4: The square, rotated 90 degrees. 1.2.2 Translation Moving an object is called a translation. We translate the point by moving to x and y coordinates, respectively, the amount of the point should be shifted in the x and y directions. We translate an object by translating each vertex in the object. The translation equations may be written as: From the double angle formulas: sin(A+B) = sin(A)*cos(B) + cos(A)*sin(B) cos(A+B) = cos(A)*cos(B) – sin(A)*sin(B) Substituting : y2/r = (y1/r)*cos(B) + (x1/r)*sin(B) Therefore: y2 = y1*cos(B) + x1*sin(B) x2/r =(x1/r)*cos(B) - (y1/r)*sin(B) And therefore: x2 = x1*cos(B) – y1*sin(B) We have now: x2 = x1*cos(B) - y1*sin(B) y2 = x1*sin(B) + y1*cos(B) We can write same as a single matrix equation. This requires that we use a 2 by 2 matrix: such that x*a+y*c = x+Tx and similarly x*b+y*d= y +Ty. To rotate a line or polygon, we must rotate each of its vertices. If we consider the coordinates of the point (x, y) as o one row two column matrix [x y] and the matrix then, given the pseudo-definition for matrix product, MP : = + / . or * , we can write equations as the matrix-equation: Figure 5 Translation 2. Rotation and translation using 4 x 4 homogenous matrices 2.1 Homogenous Coordinates From the above text we now can see the impossibility of representing a translation as 2 x 2 matrix. What is required at this point is to change the setting (2D coordinate space) in which there was the original problem. In geometry, when one encounters difficulty when trying to solve a problem in n-space, it is customary to attempt to re-phrase and solve the problem in n+1 space. In our case this means that we should look at our 2D problem in 3- dimensional space. But how can we solve this problem? Consider that, given a point (x, y) in 2-space, we map that point to (x, y, 1). That is, we inject each point in the 2D plane into the corresponding point in 3 space in the plane z=1. If we are able to solve our problem in this plane and find that the solution lies in the plane z=1, then we may project this solution back to 2 space by mapping each point (x, y, 1) to (x, y). To summarize, we inject the 2D plane into 3 space by the mapping (x, y)Æ(x, y, 1). The solution of our example square is now: 0 0 1 10 0 1 10 10 1 0 10 1 0 0 1 And we can rewrite the translation in form: x’ = x + tx y’ = y + ty z‘=z And using the 3 x 3 matrix: Same for rotation: x’ = x * cos(θ) – y * sin(θ) y’ = x * sin(θ) + y * cos(θ) z’ = z And using the 3 x 3 matrix: 2.2 3D Rotation and Translation Using 4 x 4 Homogenous Matrices Now we extend methods for translating and rotating in 3D from 2D methods by including the z-axis (now it doesn’t equal by value one). Translation in a 3D homogeneous coordinate representation; a point is translated from (x, y, z) to (x’, y’, z’) using 4 x 4 matrix: Tx, Ty and Tz specify the translation for the coordinates. The above matrix is equivalent with this representation: x’ = x + Tx y’ = y + Ty z’ = z + Tz Example: We have an origin point (0, 0, 0) and let us move it along z- and y-axis both 100 unit. We get: x’ 1 0 0 0 x 0 y’ 0 1 0 100 y 100 z’ 0 0 0 100 z 100 1 0 0 0 1 1 1 When Rotation will be specified in 3-D, we need to designate a rotation axis (about which the object is to be rotated), and the amount of angular rotation. In 2-D, we always rotated about the xy-plane. The easiest rotations to handle in 3-D are those that are parallel to a coordinate axis. To simplify things, we will adopt the convention that counterclockwise rotations about a coordinate axis are produced with positive rotation angles, if we are looking along the positive half of the axis toward the origin. If we are thinking about 2-D rotations, then there are always rotated about the z axis. So the standard 2-D rotation equations give us what we need for rotations about the z-axis: The above matrix is equivalent with this representation and this is called Rz: x’ = x * cos(θ) – y * sin(θ) y’ = x * sin(θ) + y * cos(θ) z’ = z Transformations for rotations about the other two axes can be obtained by cyclic permutation of the parameters in the equations above. In other words, we replace x with y, y with z and z with x. For the x-axis (in the yz plane) Rx: y’ = y cos(θ) - z sin(θ) z’ = y sin(θ) + z cos(θ) x’ = x For the y-axis (in the xz plane) Ry: z’ = z cosθ - x sinθ x’ = z sinθ + x cosθ y’ = y Here is the transformation matrices for Rx: And here is the transformation matrices for Ry: Even though quaternions remained a “solution in search of a problem” for many years after their discovery, applications in the fields of classical mechanics and the theory of relativity were identified in the early twentieth century. The capability of quaternions to represent threedimensional rotations about an arbitrary axis motivated researchers to use quaternion algebra in rotational kinematics equations. As a result, several new application areas involving quaternion based algorithms, such as robotics, orbital mechanics, aerospace technologies, and inertial navigation systems have started to use. 3.2 Algebra for quaternions As we know, the system of complex numbers is defined in terms of i, a square root of -1 ( i*i = -1). Although i is not a real number, we can write any complex number in terms of real numbers, using a real and a complex part. We can also define a conjugate and a magnitude (or absolute value) for these numbers. For example a point 1 unit along the z-axis rotated 90degrees about y: Using the properties above, we can describe multiplication for complex numbers: A point 1 unit along the x-axis z rotates positively to x about y: Quaternions are an extension of complex numbers. Instead of just i, we have three different numbers that are all square roots of -1 labeled i, j, and k. When we multiply two of these numbers together, they behave similarly to cross products of the unit basis vectors: ij = -ji = k jk = -kj = i ki = -ik = j The conjugate and magnitude of a quaternion are found in much the same way as complex conjugate and magnitude. 3. Rotation and translation using quaternions 3.1 Introduction for quaternions Quaternions are developed by Irish mathematician Rowan Hamilton in mid nineteen century, when he was trying to find a way to rotate a 3D vector by multiplying it by another. After 15 years of hard work he solved the problem by using a 4D notation. Quaternions provide useful mechanism for describing rotations about some arbitrary axis in 3D Graphics or Virtual Reality. Unit quaternion is The inverse of a quaternion refers to the multiplicative inverse (or 1/q) and can be computed by q-1=q'/(q*q') If a quaternion q has length 1, we say that q is a unit quaternion. The inverse of a unit quaternion is its conjugate, q-1=q' . We can represent a quaternion in several ways: • as a linear combination of 1, i, j, and k, • as a vector of the four coefficients in this linear combination, • as a scalar for the coefficient of 1 and a vector for the coefficients of the imaginary terms. We can write the product of two quaternions in terms of the (s, v) representation using standard vector products in the following way: Representing rotations with quaternions :We will compute a rotation about the unit vector, u by an angle theta. The quaternion that computes this rotation is so we can save a lot of time and preserve more numerical accuracy with quaternions than with matrices. Although let us look a matrix representation for quaternion multiplication. We can use the rules above to compute the product of two quaternions and we get: If we examine each term in this product, we can see that each term depends linearly on the coefficients for q1. Also each term depends linearly on the coefficients for q2. So, we can write the product of two quaternions in terms of a matrix multiplication. When the matrix Lrow(q1) multiplies a row vector q2, the result is a row vector representation for q1 * q2. When the matrix Rrow(q2) multiplies a row vector q1, the result is also a row vector representation for q1*q2. ⎛ cos(θ / 2 ) ⎞ q = ⎜⎜ ⎟⎟ ⎝ sin (θ / 2 ) u ⎠ We will represent a point p in space by the quaternion P=(0,p) We compute the desired rotation of that point by this formula: Now we want to perform two rotations on an object. This may come up in a manipulation interface where each movement of the mouse adds another rotation to the current object pose. This is very easy and numerically stable with a quaternion representation. Suppose q1 and q2 are unit quaternions representing two rotations. We want to perform q1 first and then q2. To do this, we apply q2 to the result of q1, regroup the product using associativity, and find that the composite rotation is represented by the quaternion q2*q1. Therefore, the only time we need to compute the matrix is when we want to transform the object. For other operations we need only look at the quaternions. A matrix product requires many more operations than a quaternion product And, what we have, when computing rotation matrices from quaternions. Now we have all the tools we need to use quaternions to generate a rotation matrix for the given rotation. We have a matrix form for left-multiplication by q : And a matrix form for right-multiplication by q’ The resulting rotation matrix is the product of these two matrices. q * P * q' = q * ( P * q') = q * (P Rrow(q')) = (P Rrow(q')) Lrow(q) = P (Rrow(q') Lrow(q)) = P Qrow(q) A matrix product requires many more operations than a quaternion product so we can save a lot of time and preserve more numerical accuracy with quaternions than with matrices. Therefore, the only time we need to compute the matrix is when we want to transform the object. For other operations we need only look at the quaternions. So using this matrix, we could compute Protated another way: Protated = P * Qrow There are applications in computer vision and robot vision where quaternions could be effectively used in extracting three dimensional orientation attitude information. The importance of quaternions to graphics and games programmers is also obvious, and also there are possibilities to stimulate further research towards extending/improving current applications. 4. Conclusion Denavit and Hartenberg (1955) and later Fu et al. (1987) and Foley et al. (1990) chose 4 x 4 homogeneous matrices for transformations such as how to express object translations, rotations and scaling. The 4 x 4 homogeneous transformation matrices offer certain computational advantages. First, they treat both rotations and translations in a uniform way. Second, they can be compounded, such that complex kinematics relationships can be modeled. Finally, they are easily invertible. Before 1980 there were many attempts to use also quaternions for transformations in computer graphics, but quaternions were not so useful. But after that and nowadays there are various applications of quaternions, particularly in the context of its increasing importance in the fields of computer graphics, animation and virtual reality. The fundamental properties of quaternions have been outlined, and the mathematical preliminaries for using quaternions as a rotation operator have been presented. References [1] R. Mukundan. Quaternions: From Classical Mechanics to Computer Graphics, and Beyond. Proceedings of the 7th Asian Technology Conference in Mathematics 2002 Pages 97-106. [2] Leonard McMillan and Gary Bishop. Plenoptic Modeling: An ImageBased Rendering System. Proceedings of SIGGRAPGH 95, Los Angeles, California, August 6-11,1995. [3] Humberto Calderon and Stamatis Vassiliadis. Computer Graphics and MOLEN paradigm: a survey.
© Copyright 2025 Paperzz