Download presentation
Presentation is loading. Please wait.
Published byMaryann Atkinson Modified over 6 years ago
1
COMP 175: Computer Graphics February 21, 2016
Lecture 05: Transform 2 COMP 175: Computer Graphics February 21, 2016
2
Refresher How do you multiply a (3x3) matrix by 3D vector?
How do you multiply two 3x3 matrices? Given this matrix: , what are the 4 basis vectors? How did you find those? Matrix as a coordinate transform What happens if you have a 2x3 matrix (2 rows, 3 columns) and we multiply it by a 3D vector? What happens if we have a 3x2 matrix (3 rows, 2 columns) and we multiply it by a 3D vector?
3
Write out the basic forms for a 2D:
Refresher Write out the basic forms for a 2D: Scaling matrix Rotation matrix Translation matrix Why is Translation transform affine but not linear? Why do we use 4x4 Matrices in 3D? How are Points and Vectors represented differently, and why?
4
Window Transformations:
Exercise Window Transformations: Given a window (rectangle) with bounding coordinates π’ 1 , π£ 2 , π’ 2 , π£ 2 Create a matrix that can both move and scale this matrix so that the new bounding box is π₯ 1 , π¦ 1 , ( π₯ 2 , π¦ 2 )
5
Exercise - Solution (π₯ 2 β π₯ 1 )/( π’ 2 β π’ 1 ) 0 ( π₯ 1 π’ 2 β π₯ 2 π’ 1 )/( π’ 2 β π’ 1 ) 0 ( π¦ 2 β π¦ 1 )/( π£ 2 β π£ 1 ) ( π¦ 1 π£ 2 β π¦ 2 π£ 1 )/( π£ 2 β π£ 1 ) 0 0 1 This is important! Think about what this means for 2D graphics and visualization. In 3D graphics, this is also commonly used to create viewports. So this matrix is known as the βwindow-to-viewportβ transformation
6
Questions?
7
Matrix Inverse For Scaling, we have:
π£ β² =ππ£ where π₯β² π¦β² 1 = π π₯ π π¦ π₯ π¦ 1 If I want to go backwards, that is, if I was given xβ, yβ, how would I find x and y? In other words, I want: π£= π β1 π£β² Find π β1
8
If we look at it at a component level:
Matrix Inverse If I want to go backwards, that is, if I was given xβ, yβ, how would I find x and y? In other words, I want: π£= π β1 π£β² Find π β1 If we look at it at a component level: π₯ β² = π π₯ π₯, πππ π¦ β² = π π¦ π¦, then π₯= 1 π π₯ π₯ β² , πππ π¦= 1 π π¦ π¦β²
9
If we look at it at a component level:
Matrix Inverse If we look at it at a component level: π₯ β² = π π₯ π₯, πππ π¦ β² = π π¦ π¦, then π₯= 1 π π₯ π₯ β² , πππ π¦= 1 π π¦ π¦β² Letβs put that back into a matrix form: π£= π β1 π£β² where π₯ π¦ 1 = 1 π π₯ π π¦ π₯β² π¦β² 1
10
Notice that if we were to multiply the two together:
Matrix Inverse π= π π₯ π π¦ , π β1 = 1 π π₯ π π¦ Notice that if we were to multiply the two together: π π β1 or π β1 π, we get back the identity matrix πΌ= In other words: π π β1 =πΌ,= π β1 π
11
Matrix Inverse Definition: Inverting composed matrices:
π΄ π΄ β1 =πΌ= π΄ β1 π΄ Inverting composed matrices: π΄π΅ β1 = π΅ β1 π΄ β1 Note, π΄π΅ β1 β π΄ β1 π΅ β1 It is important to note that a matrix is not always invertable. A matrix will not be invertible if: It is not a square matrix (ππ₯π matrix) It has row/column of all zeros (because the row/col can be deleted) If any row/col is a multiple of any other row/col (if a row is not linearly independent) Matrices for Rotation, Scaling, Translation (using homogeneous coordinates) will always have inverses!
12
One Way To Think About Inversesβ¦
Is to think of an inverse as an βundoβ For example, if A scales by a factor of 2 and rotates 135 degrees, then A-1 will rotate by -135 degrees and scale by 0.5 Rectangle(B) A-1*A*B A-1*B A*B
13
Finding Inverse Matricesβ¦
We have found the inverse matrix of a Scaling matrix. π β1 = 1 π π₯ π π¦ Letβs find the inverse matrix of a Translation matrix π= 1 0 ππ₯ 0 1 ππ¦ 0 0 1
14
Finding Inverse Matricesβ¦
This is pretty simple, we just want to βsubtractβ the changeβ¦ π β1 = 1 0 βππ₯ 0 1 βππ¦ 0 0 1 What about Rotation matrix? π
π = cosπ βπ πππ 0 π πππ cosπ
15
Inverse Rotation Matrix
Regular Rotation Matrix: π
π = cosπ βπ πππ 0 π πππ cosπ Inverse Rotation Matrix: π
π β1 = cosπ π πππ 0 βπ πππ cosπ
16
Recap of Inverses For Scaling, we have: For Rotation, we have:
π£= π β1 π£β² where π₯ π¦ 1 = 1 π π₯ π π¦ π₯β² π¦β² 1 For Rotation, we have: π£= π
π β1 π£β² where π₯ π¦ 1 = β cosπ π πππ 0 π πππ cosπ π₯β² π¦β² 1 For Translation, we have: π£= π β1 π£β² where π₯ π¦ 1 = 1 0 βππ₯ 0 1 βππ¦ π₯β² π¦β² 1
17
Questions?
18
What is the transpose of a matrix A?
Matrix Transpose What is the transpose of a matrix A? Itβs making the rows of the matrix its columns, and its columns become rows Or, you can think of it as: βturning the matrix by 90 degrees π΄= π£ 1 π₯ π£ 1 π¦ π£ 1 π§ π£ 2 π₯ π£ 2 π¦ π£ 2 π§ π£ 3 π₯ π£ 3 π¦ π£ 3 π§ , π΄ π = π£ 1 π₯ π£ 2 π₯ π£ 3 π₯ π£ 1 π¦ π£ 2 π¦ π£ 3 π¦ π£ 1 π§ π£ 2 π§ π£ 3 π§
19
Neat Fact about Rotation Matrix
The inverse of a Rotation Matrix π
π is the same as its transpose π
π π . In other words, π
π β1 = π
π π Letβs prove thisβ¦ First, we note the properties of π
π = π£ 1 π£ 2 π£ 3 : Columns are orthogonal to each other (e.g., π£ 1 β π£ 2 =0) Columns represent unit vectors: π£ π =0 Letβs multiply π
π π ππ¦ π
π : π£ 1 π₯ π£ 1 π¦ π£ 1 π§ π£ 2 π₯ π£ 2 π¦ π£ 2 π§ π£ 3 π₯ π£ 3 π¦ π£ 3 π§ π£ 1 π₯ π£ 2 π₯ π£ 3 π₯ π£ 1 π¦ π£ 2 π¦ π£ 3 π¦ π£ 1 π§ π£ 2 π§ π£ 3 π§ = π£1βπ£1 π£1βπ£2 π£1βπ£3 π£2βπ£1 π£2βπ£2 π£2βπ£3 π£3βπ£1 π£3βπ£2 π£3βπ£3
20
Neat Fact about Rotation Matrix
Letβs multiply π
π π ππ¦ π
π : π£ 1 π₯ π£ 1 π¦ π£ 1 π§ π£ 2 π₯ π£ 2 π¦ π£ 2 π§ π£ 3 π₯ π£ 3 π¦ π£ 3 π§ π£ 1 π₯ π£ 2 π₯ π£ 3 π₯ π£ 1 π¦ π£ 2 π¦ π£ 3 π¦ π£ 1 π§ π£ 2 π§ π£ 3 π§ = π£1βπ£1 π£1βπ£2 π£1βπ£3 π£2βπ£1 π£2βπ£2 π£2βπ£3 π£3βπ£1 π£3βπ£2 π£3βπ£3 Based on our rules, the right hand side comes out to: , which is the identity matrix. This means that π
π π π
π =πΌ, by definition, π
π π =π
π β1
21
Questions?
22
Composition of Transformations!
π₯β² π¦β² 1 = π π₯ π π¦ π₯ π¦ 1 π₯β² π¦β² 1 = cosπ βπ πππ 0 π πππ cosπ π₯ π¦ 1 π₯β² π¦β² 1 = 1 0 ππ₯ 0 1 ππ¦ π₯ π¦ 1 For Scaling, we have: π£ β² =ππ£ For Rotation, we have: π£ β² = π
π π£ For Translation, we have: π£ β² =ππ£
23
Composition of Transformations!
So, if I want to combine the 3 transformationsβ¦ π£ β² =ππ£ π£ β²β² = π
π π£β² π£ β²β²β² =ππ£β²β² This means: π£ β²β²β² =π( π
π π£ β² ) π£ β²β²β² =π π
π ππ£ π£ β²β²β² =π π
π ππ£
24
Composition of Transformations!
π£ β²β²β² =π π
π ππ£ π₯β² π¦β² 1 = 1 0 ππ₯ 0 1 ππ¦ cosπ βπ πππ 0 π πππ πππ π s π₯ s π¦ π₯ π¦ 1 Recall that matrix operations are associative. Meaning that: (1+2)+3 = 1+(2+3), But it is not commutative: 1+2+3 β 3+2+1 This means that I can pre-multiply π π
π π=π, so that π£ β²β²β² =ππ£, where π= π π₯ cosπ π π¦ (βπ πππ) ππ₯ π π₯ π πππ π π¦ πππ π ππ¦ 0 0 1
25
Composition of Transformations!
Remember, ORDER MATTERS!! So π£ β² =π π
π π£β π
π ππ£ For Exampleβ¦.
26
Not commutative Translate by π₯=6, π¦=0 then rotate by 45ΒΊ
1 2 3 4 5 6 7 8 9 10 Translate by π₯=6, π¦=0 then rotate by 45ΒΊ Y X 1 2 3 4 5 6 7 8 9 10 Rotate by 45ΒΊ then translate by π₯=6, π¦=0
27
Composition (an example) (2D)
Rotate 90Β° Uniform Scale 3x Both around objectβs center, not the origin Start: Goal: Important concept: Make the problem simpler Translate object to origin first, scale , rotate, and translate back π» βπ πΉπΊπ» cos90 βπ ππ90 0 π ππ90 πππ β2 0 1 β Apply to all vertices
28
Composition (an example) (2D) (2/2)
π» βπ πΉπΊπ» But what if we mixed up the order? Letβs try πΉπ» βπ πΊπ» cos90 βπ ππ90 0 π ππ90 πππ β2 0 1 β Oops! We managed to scale it properly but when we rotated it we rotated the object about the origin, not its own center, shifting its positionβ¦Order Matters! es/applets/transformationGame/transformation_game_guide.html (Transformations applet)
29
Questions?
30
Inverse Composite Matrix
Recall that: π΄π΅πΆ β1 = πΆ β1 π΅ β1 π΄ β1 Why is that? For example: Let π=π π
π , then π β1 = π π
π β1 = π
π β1 π β1 In terms of operations, it makes sense: 1) Rotate 2) Translate In reverse, I would want to 1) Reverse Translate 2) Reverse Rotate
31
Inverses Revisited π΄ π π΄ π β¦ π΄ π βπ = π΄ π βπ π΄ πβπ βπ β¦ π΄ π
What is the inverse of a sequence of transformations? π΄ π π΄ π β¦ π΄ π βπ = π΄ π βπ π΄ πβπ βπ β¦ π΄ π Inverse of a sequence of transformations is the composition of the inverses of each transformation in reverse order Say from our from our previous example we wanted do the opposite, what will our sequence look like? π» βπ πΉπΊπ» βπ = π» βπ πΊ βπ πΉ βπ π» / / cos90 π ππ90 0 βπ ππ90 πππ β2 0 1 β We still translate to origin first, then translate back at the end!
32
Questions?
33
Transforming Coordinate Axes
We understand linear transformations as changing the position of vertices relative to the standard axes Can also think of transforming the coordinate axes themselves Just as in matrix composition, be careful of which order you modify your coordinate system Rotation Scaling Translation
34
Mapping It to 3D We have been doing everything in 2D. What happens in 3D Cartesian Coordinate System?
35
Composition of Transformations!
π₯β² π¦β² π§β² 1 = π π₯ π π¦ π π§ π₯ π¦ π§ 1 π₯β² π¦β² π§β² 1 =π
π₯ π¦ π§ (will be explained) π₯β² π¦β² π§β² 1 = ππ₯ ππ¦ ππ§ π₯ π¦ π§ 1 For Scaling, we have: π£ β² =ππ£ For Rotation, we have: π£ β² = π
π π£ For Translation, we have: π£ β² =ππ£
36
Rotation Rotation by angle π around vector π = π π π π π π
Hereβs a not so friendly rotation matrix: π€ π₯ 2 +πππ π( π€ π¦ 2 + π€ π§ 2 ) π€ π₯ π€ π¦ 1βπππ π + π€ π§ π πππ π€ π₯ π€ π§ 1βπππ π + π€ π¦ π πππ 0 π€ π₯ π€ π¦ 1βπππ π + π€ π§ π πππ π€ π¦ 2 +πππ π( π€ π₯ 2 + π€ π§ 2 ) π€ π§ π€ π¦ 1βπππ π β π€ π₯ π πππ 0 π€ π₯ π€ π§ 1βπππ π β π€ π¦ π πππ π€ π§ π€ π¦ 1βπππ π + π€ π₯ π πππ π€ π§ 2 +πππ π( π€ π¦ 2 + π€ π₯ 2 ) This is called the coordinate form of Rodriguesβs formula Letβs try a different wayβ¦
37
Rotating axis by axis (1/2)
Every rotation can be represented as the composition of 3 different angles of CLOCKWISE rotation around 3 axes, namely π₯-axis in the π¦π§ plane by π π¦-axis in the π₯π§ plane by π π§-axis in the π₯π¦ plane by π Also known as Euler angles, makes the problem of rotation much easier to deal with πΉ ππ : rotation around the π₯ axis, πΉ ππ : rotation about the π¦ axis, πΉ ππ : rotation about the π§ axis You can compose these matrices to form a composite rotation matrix π
π₯π¦ (π) π
π¦π§ π π
π₯π§ π πππ π βπ πππ 0 0 π πππ πππ π πππ π βπ πππ 0 0 π πππ πππ π πππ π 0 π πππ βπ πππ 0 πππ π italic
38
Rotating axis by axis (2/2)
It would still be difficult to find the 3 angles to rotate by given arbitrary axis π€ and specified angle π Solution? Make the problem easier Step 1: Find a π to rotate around π¦ axis to put π€ in the π₯π¦ plane Step 2: Then find a π to rotate around the π§ axis to align π€ with theπ₯ axis Step 3: Rotate by π around π₯ axis = π€ axis Step 4: Finally, undo the alignment rotations (inverse) Rotation Matrix: π΄= πΉ ππ βπ π½ πΉ ππ βπ (π)πΉ ππ π πΉ ππ π πΉ ππ π½
39
Inverses and Composition in 3D!
Inverses are once again parallel to their 2D versionsβ¦ Composition works exactly the same wayβ¦ Transformation Matrix Inverse Scaling 1/ π π₯ / π π¦ / π π§ Rotation πππ π π πππ 0 0 βπ πππ πππ π πππ π π πππ 0 0 βπ πππ πππ π πππ π 0 βπ πππ π πππ 0 πππ π Translation 1 0 0 βππ₯ βππ¦ βππ§
40
Example in 3D! Letβs take some 3D object, say a cube, centered at (2,2,2) Rotate in objectβs space by 30Β° around π₯ axis, 60Β° around π¦ and 90Β° around π§ Scale in object space by 1 in the π₯, 2 in the π¦, 3 in the π§ Translate by (2,2,4) Transformation Sequence:π» π» π βπ πΉ ππ πΉ ππ πΉ ππ πΊπ» π , where π» π translates to (0,0) πππ 90 π ππ βπ ππ90 πππ πππ 60 π ππ βπ ππ60 πππ πππ 30 0 βπ ππ π ππ30 0 πππ β β β
41
Questions?
42
How to Invert a Matrix Weβre going to use Gauss-Jordan elimination
Finding A-1 with Gauss-Jordan elimination is done by augmenting A with I to get [A|I], then reducing the new matrix into reduced row echelon form (rref) to get a new matrix. This new matrix will be of the form [I|A-1] What does rref really mean? If a row does not consist entirely of zeros, then the first nonzero number in the row is a 1. (Call this a leading 1) If there are any rows that consist entirely of zeros, then they are grouped together at the bottom of the matrix. If any two successive rows do not consist entirely of zeros, the leading 1 in the lower row occurs farther to the right than the leading 1 in the higher row Each column that contains a leading 1 has zeros everywhere else.
43
How to Invert a Matrix To transform a matrix into rref we are allowed to perform any of the three elementary row operations. These are: Multiply a row by a nonzero constant Interchange two rows Add a multiple of one row to another row
44
How to Invert a Matrix, Example
Given: π΄= , letβs find π΄ β1 : Augment this with the identity: π΄ πΌ = Row operation 1: multiply row 1 by 1/11 Row operation 3: multiply row 1 by -17 and add it to row 2: β β
45
How to Invert a Matrix, Example
Row operation 1, multiply row 2 by β11/12 β 11 12 Row operation 3: multiply row 2 by β13/11 and add to row 1 πΌ π΄ = β β 11 12 Therefore: π΄ β1 = β β 11 12
46
Questions?
47
Addendum β Matrix Notation
The application of matrices in the row vector notation is executed in the reverse order of applications in the column vector notation: π₯ π¦ π§ β π₯ π¦ π§ Column format: vector follows transformation matrix: π₯β² π¦β² π§β² = π π π π π π π β π π₯ π¦ π§ Row format: vector precedes matrix and is post-multiplied by it: π₯β² π¦β² π§β² = π₯ π¦ π§ π π π π π π π β π By convention, we always use Column Format
48
Addendum β Matrix Notation
Uhβ¦ A problem: π π π π π π π β π π₯ π¦ π§ = ππ₯+ππ¦+ππ§ ππ₯+ππ¦+ππ§ ππ₯+βπ¦+ππ§ While: π₯ π¦ π§ π π π π π π π β π = ππ₯+ππ¦+ππ§ ππ₯+ππ¦+βπ§ ππ₯+ππ¦+ππ§
49
Addendum β Matrix Notation
In order for both types of notations to yield the same result, a matrix in the row system must be the transpose of the matrix in the column system Recall: π= π π π π π π π β π , π π = π π π π π β π π π In order to make the two notations line up: π₯ π¦ π§ π π π π π β π π π = ππ₯+ππ¦+ππ§ ππ₯+ππ¦+ππ§ ππ₯+βπ¦+ππ§ β ππ₯+ππ¦+ππ§ ππ₯+ππ¦+ππ§ ππ₯+βπ¦+ππ§ = π π π π π π π β π π₯ π¦ π§ Be careful! Different textbooks and different graphics packages use different notations! First understand which one they use!!
50
Why Column-Format Notation?
Because it fits more naturally to OpenGLβs stacks For example, for a series of matrix operations: π» βπ πΉπΊπ», how would you write this using GL calls? What happens when I have multiple objects? For example, the solar system?...
51
Questions?
52
Converting Math to OpenGL Code
Letβs say that you have a set of transforms: π΄=π» βπ πΉπΊπ» Writing this in OpenGL would look something like: glTranslate3f(-xtrans, -ytrans, -ztrans); glRotate3f (angle, x_axis, y_axis, z_axis); glScale3f (xscale, yscale, zscale); glTranslate3f (xtrans, ytrans, ztrans); DrawObject(); Or, you can do this in software (SLOW) using the Algebra.h library. Matrix t_invM = inv_trans_mat (transVec); Matrix rotM = rot_mat (rotVec, angle); Matrix scaleM = scale_mat(scaleVec); Matrix tM = trans_mat (transVec); Matrix composite = t_invM * rotM * scaleM * tM; for (each vertex in object) { Point newPos = composite * vertex->getPosition(); }
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.