Transformaciones geométricas

Download Report

Transcript Transformaciones geométricas

Computación Gráfica

Transformaciones geométricas 2D y 3D

Semestre 201321 CRN Septiembre 2012 – Febrero 2013 Ciro Durán Ingeniero en Computación [email protected]

http://www.ciroduran.com

@chiguire Universidad Católica Andrés Bello » Computación Gráfica » Ciro Durán

¿Cómo usamos las transformaciones geométricas?

• Los objetos de una escena son colecciones de puntos … • Estos objetos tiene posición, orientación, tamaño • Corresponde a las transformaciones, Translación ( 𝑻 ), Rotación ( 𝑹 ), y Escalamiento ( 𝑺 ) Universidad Católica Andrés Bello » Computación Gráfica » Ciro Durán

¿Cómo usamos las transformaciones geométricas?

renderiza la escena.

• La cámara tiene una

(2/2)

• Una escena tiene una cámara/punto de vista desde el cual se

posición

y

orientación

en el espacio 3D… • Éstos corresponden a las transformaciones de Traslación y Rotación • Se necesitan otros tipos de transformaciones de vista también – las veremos ahora Universidad Católica Andrés Bello » Computación Gráfica » Ciro Durán

Algunos conceptos de álgebra lineal…

• Geometría de coordenadas 3D • Vectores en espacios 2D y 3D • Producto punto y producto cruz – definiciones y usos • Notación y álgebra vectorial y matricial • Matriz identidad • Asociatividad multiplicativa – Ej. A(BC) = (AB)C • Traspuesta e inversa de matriz – definición, uso y cálculo • Coordenadas homogéneas ( 𝑥, 𝑦, 𝑧, 𝒘 ) • ¡Necesitarán entender estos conceptos!

• Una “ayudaíta” (en inglés) -> http://cs.brown.edu/courses/cs123/resources/Linear_Algebra.

pdf Universidad Católica Andrés Bello » Computación Gráfica » Ciro Durán

Transformaciones lineales (1/3)

• Representamos los vectores como letras en

bold itálica

( 𝒗 ) y escalares como letras en

itálica

solamente (

c

) • Cualquier vector en el plano puede ser definido como la suma de dos vectores base no co-lineares en el plano – Recordemos que una base es un conjunto de vectores que cumplen dos propiedades: – Los vectores son linealmente independientes – Cualquier vector en el espacio vectorial puede ser generado por una combinación lineal de la base • Las constantes escalares pueden ser usadas para ajustar magnitud y dirección del vector resultante a b 𝒗 = a + b Universidad Católica Andrés Bello » Computación Gráfica » Ciro Durán

• •

Transformaciones lineales (2/3)

Definición de una función lineal,

f

: –

f

𝒗 + 𝒘 =

f

𝒗 +

f

(𝒘) donde el dominio y co-dominio de

f

son idénticos • La función de una adición de vectores es equivalente a la adición de la función aplicada a –

f

cada uno de los vectores 𝑐𝒗 = 𝑐

f

𝒗 • La función de una multiplicación escalar con un vector es un escalar multiplicado por la función aplicada al vector Ambas de estas propiedades deben ser satisfechas para que

f

sea un operador lineal Universidad Católica Andrés Bello » Computación Gráfica » Ciro Durán

Transformaciones lineales (3/3)

– Uso gráfico: transformaciones de puntos alrededor del origen (

dejando invariante al origen

) • • • Esto incluye al

escalamiento

y a las

rotaciones

translación), (pero no a la La

translación

no es una lineal (mueve el origen) función Cualquier transformación lineal de un punto resultará en otro punto en el mismo sistema de coordenadas, transformado sobre el origen Universidad Católica Andrés Bello » Computación Gráfica » Ciro Durán

• • • • • •

Transformaciones lineales como matrices (1/2)

Las Transformaciones Lineales pueden ser representadas como matrices no singulares (invertibles) Comencemos con las transformaciones 2D: 𝑻 = La matriz 𝑎 𝑻 𝑐 𝑏 𝑑 puede ser también escrita como: 𝑻 𝑒1 𝑻 𝑒2 , donde 𝑻 𝑒1 = 𝑎 𝑐 , 𝑻 𝑒2 = 𝑏 𝑑 Donde 𝑒1 y 𝑒2 son los vectores unitarios estandar sobre los vectores x e y: 𝑒1 = 1 0 , 𝑒2 = 0 1 ¿Por qué es importante esto? Esto significa que podemos computar las columnas de la matriz de transformación una por una determinando como nuestra transformación afecta cada uno de los vectores unitarios estándar. Por lo que 𝑻 “ manda 𝑒1 a = 𝑐 “ Use esta estrategia para derivar las matrices de transformación Universidad Católica Andrés Bello » Computación Gráfica » Ciro Durán

Transformaciones lineales como matrices (2/2)

• Una transformación de un vector columna arbitrario la forma: 𝑻 𝑥 𝑦 = 𝑎 𝑐 𝑏 𝑑 𝑥 𝑦 𝑥 𝑦 tiene • Sustituyamos 1 0 por 𝑥 𝑦 : 𝑻 – La transformación aplicada a 1 0 1 0 = 𝑎 𝑐 𝑏 𝑑 1 0 = 𝑎 𝑐 es la primera columna de 𝑻 • Ahora sustituyamos 0 1 por 𝑥 𝑦 : 𝑻 – La transformación aplicacada a 0 1 0 1 = 𝑎 𝑐 𝑏 𝑑 0 1 = 𝑏 𝑑 es la segunda columna de 𝑻 Universidad Católica Andrés Bello » Computación Gráfica » Ciro Durán

Escalando en 2D (1/2)

• • • • Escalemos 𝑥 3,

S y

– = 2) 𝒗 = 𝑥 𝑦 (nuevo por 3, ( vértice original); 𝒗’ vértice) 𝑦 por 2 (

=

S x

𝑥′ 𝑦′ = 𝒗’ = 𝑺 𝒗 Derivemos 𝑺 determinando transformadas – cómo 𝑒1 y 𝑒2 deberían ser 𝑒1 = de 𝑺 1 0 en X por  𝑠 𝑥 * 𝑒1 = 𝑠 𝑥 0 (Escalar 𝑠 𝑥 ) , la primera columna – 𝑒2 = 0 1 en Y por  𝑠 𝑦 * 𝑒2 = 0 𝑠 𝑦 𝑠 𝑦 ), la segunda columna de 𝑺 (Escalar Por lo tanto obtenemos 𝑺 : 𝑠 𝑥 0 0 𝑠 𝑦    2 1    Efecto lateral: la casa cambia su posición relativa al origen    3 1      6 2     9 2   Universidad Católica Andrés Bello » Computación Gráfica » Ciro Durán

• • • •

Escalando en 2D (2/2)

𝑺 es una matriz diagonal – se puede confirmar la derivación simplemente mirando las propiedades de las matrices diagonales: 𝑫𝒗 = 𝑎 0 0 𝑏 – En donde 𝑫 𝑥 𝑦 = 𝑎𝑥 𝑏𝑦 = 𝒗’ es una matriz diagonal La 𝑖 𝑒𝑟𝑎 posición de 𝒗’ sobre la diagonal de = ( 𝑖 𝑟𝑎 𝑫 ∗ 𝑖 𝑟𝑎 posición posición de 𝒗 ) 𝑺 𝒗 multiplica cada coordenada de una por los factores de escalamiento ( 𝑠 𝑥 , 𝑠 𝑦 ) especificados por las posiciones sobre la diagonal, como se espera –

𝑠

𝑥 =

𝑎

,

𝑠

𝑦 =

𝑏

    Otras propiedades del escalamiento: No conserva longitudes en los objetos No conserva los ángulos entre partes de los objetos, a menos que el escalamiento sea uniforme, 𝑠 𝑥 = 𝑠 𝑦 ) Si el objeto no está en su origen, traslada la casa relativa al origen algo comúnmente no deseado… – Universidad Católica Andrés Bello » Computación Gráfica » Ciro Durán

Rotación en 2D (1/2)

• • Rotar por 𝜃 alrededor del origen 𝒗’ = 𝑺 𝒗 – 𝒗 = 𝑥 𝑦 donde (vértice original) – 𝒗’

=

𝑥′ 𝑦′ (vértice nuevo)  – • • Derivemos 𝑒1 𝑒2 = = 1 0 0 1 𝑹 Ө   determinando cómo cos

Ө

sen sen

Ө

cos

Ө Ө

𝑒1 y 𝑒2 deberían transformarse , primera columna de 𝑹 , segunda columna de Ө 𝑹 Ө – Y así obtenemos 𝑹 Ө

:

cos 𝜃 𝑠𝑒𝑛𝜃 −𝑠𝑒𝑛𝜃 𝑐𝑜𝑠𝜃 Universidad Católica Andrés Bello » Computación Gráfica » Ciro Durán

Rotación en 2D (2/2)

• • • Intentemos multiplicar las matrices 𝑹 𝜃 ∗ 𝒗 = cos 𝜃 𝑠𝑒𝑛𝜃 −𝑠𝑒𝑛𝜃 𝑐𝑜𝑠𝜃 𝑥 𝑦 = x x cos sen Ө – Ө + y y sen cos Ө Ө = 𝑥′ 𝑦′ = 𝒗

𝑥

= 𝑥 cos 𝜃 − 𝑦 sen 𝜃

𝑦

= 𝑥 sen 𝜃 + 𝑦 cos 𝜃

Otras propiedades de la

– rotación de la casa)

rotación:

– Conserva longitudes y ángulos entre partes de los objetos – La rotación es de cuerpo rígido Para objetos que no están en el origen, nuevamente una traslación puede no ser deseada (i.e. esto rota alrededor del origen, no alrededor del centro de Universidad Católica Andrés Bello » Computación Gráfica » Ciro Durán

¿Qué hay sobre la traslación?

• La traslación no es una transformación linea (no centrada alrededor del origen) • • • No puede ser representada como una matriz 2x2 invertible…

Pregunta:

solución?

¿Hay alguna otra

Respuesta:

Sí, 𝒗 ′ donde 𝒕 = 𝑑𝑥 𝑑𝑦 = 𝒗 + 𝒕 , • Sumar para trasladar, esto es inconsistente.

   Si tratamos todas las transformaciones de una manera consistente, es decir, con representación de matrices, entonces podríamos combinar las trnasformaciones al componer sus matrices Usemos nuevamente una matriz  ¿Cómo? Con

coordenadas homogéneas

: agrega una dimensión adicional, el eje w, y una coordenada extra, el componente w.

Y así 2D -> 3D (efectivamente el hiperespacio para colocar el espacio 2D) Universidad Católica Andrés Bello » Computación Gráfica » Ciro Durán

Coordenadas homogéneas

• Permite la expresión de

(1/3)

todas las transformaciones 2D como matrices 3x3 – Comenzamos con el punto 𝑃 2𝑑 en el plano 𝑥𝑦 y aplicamos un mapeo para llevarlo al plano-w en el hiperespacio • 𝑃 2𝑑 𝑥, 𝑦 → 𝑃 ℎ 𝑤𝑥, 𝑤𝑦, 𝑤 , 𝑤 ≠ 0 • Las coordenadas resultantes ( 𝑥 ’, 𝑦 ’) en nuestro nuevo punto 𝑃 ℎ son diferentes del original ( 𝑥 , 𝑦 ) , donde 𝑥 ’ = 𝑤𝑥 , 𝑦 ’ = 𝑤𝑦 – 𝑃 ℎ 𝑥 ′ , 𝑦 ′ , 𝑤 , 𝑤 ≠ 0 Universidad Católica Andrés Bello » Computación Gráfica » Ciro Durán

• • • •

Coordenadas homogéneas

Una vez que tenemos este punto podemos aplicar una de versión transformación (próximas láminas) para llevarlo a un nuevo punto en el hiperespacio

(2/3)

homogeneizada de nuestras matrices Finalmente, queremos obtener el punto resultante en el espacio 2D nuevamente, así que hacemos un reverso del mapeo anterior (dividimos todas las entradas por 𝑤 ) Esto convierte nuestro punto en el hiperespacio al punto correspondiente en el epsacio 2D – 𝑃 2𝑑 𝑥, 𝑦 = 𝑃 2𝑑 𝑥 El vértice 𝒗 = 𝑦 𝑥 ′ 𝑤 , 𝑦 ′ 𝑤 ahora es 𝑥 representado como 𝒗 = 𝑦 1 Universidad Católica Andrés Bello » Computación Gráfica » Ciro Durán

Coordenadas homogéneas (3/3)

• Hacer que las transformaciones mapeen puntos en el hiperplano a otro punto en el hiperplano. Las transformaciones aplicadas a un punto en el hiperplano siempre sacar un resultado también en el mismo hiperplano (clausura matemática) • La transformación 𝑻 aplicada a 𝒗 = 𝑥 𝑦 1 se corresponde con 𝒗′ = • ¿Cómo aplicamos esto a nuestras matrices de transformación?

𝑥′ 𝑦′ 1 • Para transformaciones lineales, mantenemos una sub-matriz 2x2, expandimos la matriz como aparece a continuación, donde para las transformaciones 2D, la submatriz superior izquierda es la que incluye el escalamiento o la matriz de rotación derivada anteriormente: – 𝑎 𝑐 0 𝑏 𝑑 0 0 0 1 Universidad Católica Andrés Bello » Computación Gráfica » Ciro Durán

De vuelta a la traslación

• Nuestra matriz de traslación ( 𝑻 ) puede ahora ser representada al incluir el vector de traslación en la columna derecha en el tope como: • 1 0 0 1 𝑑𝑥 𝑑𝑦 𝑥 𝑦 0 0 1 • 𝑻 1 𝒗 = 1 0 𝑑𝑥 𝑥 𝑦 = = 𝒗

siguen Universidad Católica Andrés Bello » Computación Gráfica » Ciro Durán

Transformaciones homogenizadas

• La traslación usa una matriz 3x3, pero el escalamiento y la rotación son matrices 2x2 • ¡Homogenicemos! Esto no afecta la propiedad lineal del escalamiento y la rotación • Nuetras nuevas matrices de transformación se verán así…

Transformación

Escalado Rotación Traslación

Matriz

𝑠 𝑥 0 cos𝜃 𝑠𝑒𝑛𝜃 0 0 1 0 0 0 𝑠 𝑦 0 0 0 1 −𝑠𝑒𝑛𝜃 𝑐𝑜𝑠𝜃 0 0 𝑑𝑥 1 𝑑𝑦 0 1 0 0 1 • Nota: estas 3 transformaciones se le llaman transformaciones

afines

.

Universidad Católica Andrés Bello » Computación Gráfica » Ciro Durán

Ejemplos

• Escalado:

Escalar por 15 en la dirección

𝑥

, 17 en la

𝑦 15 0 0 0 17 0 0 0 1 • Rotación:

Rotar por 1

23° cos(123) sen(123) 0 −sen(123) 0 cos(123) 0 0 1 • Traslación:

Trasladar por -16 en el eje

1 0 −16 0 1 0 0 18 1 𝑥

, +18 en el

𝑦 Universidad Católica Andrés Bello » Computación Gráfica » Ciro Durán

¡Antes de continuar! Vectores vs. Puntos

• Hasta ahora, sólo hemos usar la noción de un punto en nuestro espacio 2D.

• Ahora debemos presentar una distinción entre vectores y puntos.

• Usamos las coordenadas homogéneas para representar más convenientemente la traslación; luego los puntos se representan como

y, 1 )

T

• Un vector puede ser rotado o escalado, pero no trasladado (porque siempre comienza en el origen), no uses la coordenada homogénea

(x, (x, y, 0 )

T

• Por ahora, enfoquémonos solamente en los puntos (típicamente vértices) Universidad Católica Andrés Bello » Computación Gráfica » Ciro Durán

Inversas

• How do we find the inverse of a transformation?

• Take the inverse of the transformation matrix (thanks to homogenization, they’re all invertible!):

Transformation

Scaling Rotation

Matrix Inverse

1/𝑠 0 0 𝑥 0 1/𝑠 𝑦 0 − cos𝜃 𝑠𝑖𝑛𝜃 0 𝑠𝑖𝑛𝜃 𝑐𝑜𝑠𝜃 0 0 0 1 0 0 1

Translation

1 0 −𝑑𝑥 0 1 −𝑑𝑦 0 0 1 Universidad Católica Andrés Bello » Computación Gráfica » Ciro Durán

Does it make sense?

If you scale something by factor X, the inverse is scaling by 1/X Not so obvious, but can use math! Rotation Matrix is orthonormal, so inverse should just be the transpose, (proof on slide 23) If you translate by X, the inverse is translating by -X

More with Homogeneous Coordinates

Some uses we’ll be seeing later

• Placing sub-objects in parent’s coordinate system to construct hierarchical scene graph – transforming primitives their own coordinate systems • View volume normalization – mapping arbitrary view volume into canonical view volume along 𝑧 -axis • Parallel (orthographic, oblique) and perspective projection • Perspective transformation (turn viewing pyramid into a cuboid to turn perspective projection into parallel projection) Universidad Católica Andrés Bello » Computación Gráfica » Ciro Durán

A moment of appreciation for linear algebra

• The inverse of a rotation matrix 𝑀 is just its transpose, 𝑀 𝑇 !

really convenient, so let’s understand how it works… • Take a rotation matrix 𝑀 = 𝑣1 𝑣2 𝑣3 • First note these properties of 𝑀 – • 

j

) Columns are orthogonal to each other:

v i v j

= 0 (

i

– Columns represent unit length vectors: ||

v i

|| = 1 • Let’s see what happens when we multiply 𝑀 𝑇 and 𝑀: That’s • 𝑣1 𝑣2 𝑣3 𝑥 𝑥 𝑥 𝑣1 𝑣2 𝑣3 𝑦 𝑦 𝑦 𝑣1 𝑣2 𝑣3 𝑧 𝑧 𝑧 𝑣1 𝑥 𝑣1 𝑦 𝑣1 𝑧 𝑣2 𝑣2 𝑣2 𝑥 𝑦 𝑧 𝑣3 𝑣3 𝑣3 𝑥 𝑦 𝑧 = 𝑣1 • 𝑣1 𝑣1 • 𝑣2 𝑣1 • 𝑣3 𝑣2 • 𝑣1 𝑣2 • 𝑣2 𝑣2 • 𝑣3 𝑣3 • 𝑣1 𝑣3 • 𝑣2 𝑣3 • 𝑣3 • Using the properties we defined we can see that the result is the identity matrix 1 0 0 0 1 0 0 0 1 , thus 𝑀 𝑇 𝑀 = I , and by definition of matrix inverse, matrix 𝑀 𝑇 must be inverse of 𝑀 Universidad Católica Andrés Bello » Computación Gráfica » Ciro Durán

Composition of Transformations (2D) (1/2)

• We now have a number of tools at our disposal, we can combine them!

• An object in a scene uses many transformations in sequence, how do we represent this in terms of functions?

• Transformation is a function; by associativity we can compose functions: ( 𝑓 ° 𝑔) 𝑖 • This is the same as first applying 𝑔 then applying 𝑓 : ( 𝑓(𝑔(𝑖))) to some input 𝑖 and • Consider our functions 𝑓 and 𝑔 as matrices ( 𝑀 1 respectively) and our input as a vector ( 𝒗 ) and 𝑀 2 • Our composition is equivalent to 𝑀 1 𝑀 2 𝑣 Universidad Católica Andrés Bello » Computación Gráfica » Ciro Durán

Composition of Transformations (2D) (2/2)

• We can now form compositions of transformation matrices to form a more complex transformation • For example, 𝑻𝑹𝑺𝒗 , which scales point, then rotates, then translates: • 1 0 0 𝑑𝑥 1 𝑑𝑦 cos𝜃 𝑠𝑖𝑛𝜃 −𝑠𝑖𝑛𝜃 𝑐𝑜𝑠𝜃 0 0 s 𝑥 0 0 s 𝑦 0 0 𝑥 𝑦 0 0 1 0 0 1 0 0 1 1 • Note that we apply the matrices in sequence right to left, but practically, given associativity, we can compose them and apply the composite to all the vertices in, say, a mesh.

Important: Order Matters!

• Matrix Multiplication is

not commutative.

• Be sure to check out the Transformation Game at http://www.cs.brown.edu/exploratories/freeSoftware/repository/edu/brown/cs/exploratories/applets/transfo rmationGame/transformation_game_guide.html

• Let’s see an example… Universidad Católica Andrés Bello » Computación Gráfica » Ciro Durán

Not commutative

Translate by 𝑥 =6, 𝑦 =0 then rotate by 45º 3 2 1 6 5 4 0 1 2 3 4 5 6 7 8 9 10 Rotate by 45º then translate by 𝑥 =6, 𝑦 =0 Y 6 5 2 1 4 3 0 1 2 3 4 5 6 7 8 9 10 X Universidad Católica Andrés Bello » Computación Gráfica » Ciro Durán

Composition (an example) (2D)

Goal: Rotate 90 ° Uniform Scale 3x Both around object’s center, not the origin • • Important concept: Make the problem simpler • Translate object to origin first, scale , rotate, and translate back: – 𝑻 −𝟏 𝑹𝑺𝑻 = 1 0 0 1 0 0 Apply to all vertices 3 3 1 cos90 −𝑠𝑖𝑛90 0 𝑠𝑖𝑛90 𝑐𝑜𝑠90 0 0 0 1 3 0 0 0 3 0 0 0 1 1 0 −3 0 1 −3 0 0 1 Universidad Católica Andrés Bello » Computación Gráfica » Ciro Durán

Composition (an example) (2D) (2/2)

• 𝑻 −𝟏 𝑹𝑺𝑻 • But what if we mixed up the order? Let’s try 𝑹𝑻 −𝟏 𝑺𝑻 – cos90 −𝑠𝑖𝑛90 𝑠𝑖𝑛90 𝑐𝑜𝑠90 0 0 0 0 1 1 0 2 0 1 2 0 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!

3 0 0 0 0 3 0 0 1 1 0 0 1 0 0 −2 −2 1 Universidad Católica Andrés Bello » Computación Gráfica » Ciro Durán

Aside: Skewing/shearing

    “Skew” an object to the side, like shearing a card deck by displacing each card relative to the previous one What physical situations mirror this behavior?!?

Squares become parallelograms coordinates skew to right, 𝑦 𝑥 stays same Notice that base of house (at 𝑦 =1) remains horizontal, but shifts right - why?

Y 6 5 2 1 4 3 0 1  2 3 4 5 6    4 7 8 9 10 X

Skew

      1 0 1 tan 1      2D non-Homogeneous

Skew

    1     0 0 1 tan 1 0  0   0 1     2D Homogeneous Universidad Católica Andrés Bello » Computación Gráfica » Ciro Durán

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 we want to do the opposite transform of the example on Slide 27, what will our sequence look like?

𝑻 −𝟏 𝑹𝑺𝑻 −𝟏 = 𝑻 −𝟏 𝑺 −𝟏 𝑹 −𝟏 𝑻 • 1 0 2 0 1 2 0 0 1 1/3 0 0 0 0 0 1/3 0 1 cos90 −𝑠𝑖𝑛90 𝑐𝑜𝑠90 0 0 𝑠𝑖𝑛90 0 0 1 1 0 −2 0 1 −2 0 0 1 • We still translate to origin first, then translate back at the end!

Universidad Católica Andrés Bello » Computación Gráfica » Ciro Durán

• •

Aside: Windowing Transformations

Windowing transformation maps contents of a 2D clip rectangle ("window") to a rectangle on the screen, e.g., interior canvas of a window manager's window ("viewport"), so also known as window-to-viewport transformation Sends rectangle with bounding coordinates 𝑥 1 , 𝑦 1 , 𝑥 2 , 𝑦 2 𝑢 1 , 𝑣 1 , (𝑢 2 , 𝑣 2 ) to • (𝑥 2 −𝑥 1 )/(𝑢 2 0 0 − 𝑢 1 ) (𝑦 2 0 − 𝑦 1 )/(𝑣 2 0 − 𝑣 1 ) (𝑥 (𝑦 1 1 𝑢 𝑣 Universidad Católica Andrés Bello » Computación Gráfica » Ciro Durán 2 2 − 𝑥 2 𝑢 1 )/(𝑢 2 − 𝑦 2 𝑣 1 )/(𝑣 2 1 − 𝑢 1 ) − 𝑣 1 )

Aside: Transforming Coordinate Axes

vertices relative to the standard axes • Can also think of transforming the coordinate axes themselves Rotation Scaling Translation • Just as in matrix composition, be careful of which order you modify your coordinate system Universidad Católica Andrés Bello » Computación Gráfica » Ciro Durán

Dimension++ (3D!)

• How should we treat geometric transformations in 3D?

• Just add one more coordinate/axis!

• A point is represented as represented as the z-coordinate, 𝑥 𝑦 𝑧 • A matrix for a linear transformation 𝑻(𝑒1) 𝑻(𝑒2) 𝑻(𝑒3) 𝑻 where 𝑒3 can be corresponds to 0 0 1 • But remember to use homogeneous coordinates! Thus embed the scale and rotation matrices upper left submatrix, translation vector upper right column Universidad Católica Andrés Bello » Computación Gráfica » Ciro Durán

Transformation

Transformations in 3D

Scaling

Matrix

𝑠 𝑥 0 0 0 0 𝑠 𝑦 0 0 0 0 𝑠 𝑧 0 0 0 0 1

Comments

Looks just like the 2D version right? We just added an s z term.

Rotation Translation

See next slide 1 0 0 0 0 1 0 0 0 0 1 0 𝑑𝑥 𝑑𝑦 𝑑𝑧 1 This one’s more complicated. In 2D there is only one axis of rotation. In 3D there are infinitely many, thus the matrix has to take into account all possible axes.

See next slide… Similar to the 2D version, we just have one more term, 𝑑𝑧 , representing change in the 𝑧 axis 35/45 Universidad Católica Andrés Bello » Computación Gráfica » Ciro Durán

Rodrigues’s Formula…

• Rotation by angle 𝜃 around v ector 𝒖

=

𝒖 𝒙 𝒖 𝒚 𝒖 𝒛 Note: This is an arbitrary

unit

vector 𝒖 in 𝑥𝑦𝑧 space • Here’s a not so friendly rotation matrix: • This is called the coordinate form of Rodrigues’s formula • Let’s try a different way… Universidad Católica Andrés Bello » Computación Gráfica » Ciro Durán 36/45

Rotating axis by axis (1/2)

• Every rotation can be represented as the composition of 3 different angles of

counter-clockwise

rotation around 3 axes, namely – – – 𝑥 -axis in the 𝑦𝑧 𝑦 -axis in the 𝑥𝑧 𝑧 -axis in the 𝑥𝑦 plane by 𝜓 plane by 𝜃 plane by 𝜙 • Also known as Euler angles, makes problem of rotation much easier 𝑅 𝑥𝑦 (𝜙) 𝑅 𝑦𝑧 𝜓 𝑅 𝑥𝑧 𝜃 𝑐𝑜𝑠𝜙 𝑠𝑖𝑛𝜙 0 0 −𝑠𝑖𝑛𝜙 𝑐𝑜𝑠𝜙 0 0 0 0 1 0 0 0 0 1 1 0 0 0 0 𝑐𝑜𝑠𝜓 𝑠𝑖𝑛𝜓 0 0 −𝑠𝑖𝑛𝜓 𝑐𝑜𝑠𝜓 0 0 0 0 1 𝑐𝑜𝑠𝜃 0 −𝑠𝑖𝑛𝜃 0 0 1 𝑠𝑖𝑛𝜃 0 0 𝑐𝑜𝑠𝜃 0 0 0 0 0 1 • 𝑹 𝒚𝒛 : rotation around about matrix 𝑧 axis 𝑥 axis, the homogeneous matrix 𝑹 𝒙𝒛 : rotation about 𝑦 axis, 𝑹 𝒙𝒚 : rotation • Note these differ only in where the 3x3 submatrix is embedded in • You can compose these matrices to form a composite rotation

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 by mapping

u

to one of the principal axes •

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 = coincident 𝒖 axis •

Step 4:

Finally, undo the alignment rotations (inverse) • Rotation Matrix: 𝑴 = 𝑹 −𝟏 𝒙𝒛 𝜽 𝑹 −𝟏 𝒙𝒚 (𝝓)𝑹 𝒚𝒛 𝝍 𝑹 𝒙𝒚 𝝓 𝑹 𝒙𝒛 𝜽 38/45 Universidad Católica Andrés Bello » Computación Gráfica » Ciro Durán

Inverses and Composition in 3D!

• Inverses are once again parallel to their 2D versions…

Transformation Matrix Inverse

Scaling

1/𝑠 𝑥 0 0 0 0 1/𝑠 𝑦 0 0 0 0 1/𝑠 𝑧 0 0 0 0 1

Rotation

1 0 0 0 0 𝑐𝑜𝑠𝜓 −𝑠𝑖𝑛𝜓 0 0 𝑠𝑖𝑛𝜓 𝑐𝑜𝑠𝜓 0 0 0 0 1 𝑐𝑜𝑠𝜙 −𝑠𝑖𝑛𝜙 0 0 𝑠𝑖𝑛𝜙 𝑐𝑜𝑠𝜙 0 0 0 0 0 0 1 0 0 1 𝑐𝑜𝑠𝜃 0 𝑠𝑖𝑛𝜃 0 0 1 0 0 −𝑠𝑖𝑛𝜃 0 𝑐𝑜𝑠𝜃 0 0 0 0 1

Translation

1 0 0 1 0 0 −𝑑𝑥 −𝑑𝑦 0 0 0 0 1 0 −𝑑𝑧 1 39/45 Universidad Católica Andrés Bello » Computación Gráfica » Ciro Durán

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 𝑦 90 ° around 𝑧 • Scale in object space by 1 in the 𝑥 , 2 in the 𝑦 , 3 in the 𝑧 • Translate by (2,2,4) in world space • Transformation Sequence: 𝑻𝑻 −𝟏 𝟎 to (0,0) 𝑺 𝒙𝒚 𝑹 𝒙𝒚 𝑹 𝒙𝒛 𝑹 𝒚𝒛 𝑻 𝟎 , where 𝑻 𝟎 and translates • 1 0 0 0 2 0 1 0 2 0 1 4 0 0 0 1 1 0 0 0 0 0 2 1 0 2 0 0 1 2 0 1 1 0 0 2 0 0 0 0 0 0 0 0 3 0 0 1 𝑐𝑜𝑠90 −𝑠𝑖𝑛90 0 0 𝑠𝑖𝑛90 𝑐𝑜𝑠90 0 0 0 0 0 0 1 0 0 1 𝑐𝑜𝑠60 0 0 1 −𝑠𝑖𝑛60 0 0 0 𝑠𝑖𝑛60 0 𝑐𝑜𝑠60 0 0 0 0 1 1 0 0 0 0 𝑐𝑜𝑠30 0 0 𝑠𝑖𝑛30 −𝑠𝑖𝑛30 𝑐𝑜𝑠30 0 Universidad Católica Andrés Bello » Computación Gráfica » Ciro Durán 0 0 0 1 1 0 0 0 0 0 −2 1 0 −2 0 0 1 0 −2 1 40/45

Transformations and the scene

Objects can be complex:

graph (1/4)

Scene Graph upper body ROBOT lower body head trunk arm arm stanchion base • • 3D scenes are often stored in a directed acyclic graph (DAG) called a

scene graph

– WPF – Open Scene Graph (used in the Cave) – Sun’s Java3D™ – X3D ™ (VRML ™ was a precursor to X3D) Typical scene graph format: –

objects

(cubes, sphere, cone, polyhedra etc.): stored as nodes (default: unit size at origin) – –

attributes

(color, texture map, etc.) stored as separate nodes

transformations

are also nodes 41/45 Universidad Católica Andrés Bello » Computación Gráfica » Ciro Durán

Transformations and the scene

    Attributes stored as a components of each object node (no separate attribute node) Transform node affects its subtree Only leaf nodes are graphical objects  All internal nodes that are not transform nodes are group nodes Represents a transformation

Step 1:

Various transformations are applied to each of the leaves (object primitives —head, base, etc.)

Step 2:

Transformations are then applied to groups of these objects as a whole (upper body, lower body) Together this hierarchy of transformations forms the “robot” scene as a whole 42/45 Universidad Católica Andrés Bello » Computación Gráfica » Ciro Durán

Transformations and the scene

matrix that builds as you move up the tree ( 𝑪𝑻𝑴 ), appending higher level transformation matrices to the front of your sequence • Example: – For o1, 𝑪𝑻𝑴 = 𝑴 𝟏 – For o2, 𝑪𝑻𝑴 = 𝑴 𝟐 𝑴 𝟑 – For o3, 𝑪𝑻𝑴 = 𝑴 𝟐 𝑴 𝟒 𝑴 𝟓 – For a vertex

v

in o3, position in world (root) coordinate system is: – 𝑪𝑻𝑴 𝒗 = (𝑴 𝟐 𝑴 𝟒 𝑴 𝟓 ) 𝒗 𝑴 𝟏 𝑴 𝟑 𝑴 𝟐 object nodes (geometry) transformation nodes group nodes 𝑴 𝟒 𝑴 𝟓 43/45 Universidad Católica Andrés Bello » Computación Gráfica » Ciro Durán

Transformations and the scene

graph (4/4)

of objects (sub-trees) if they have been defined • Group 3 has been used twice here • Transformations defined within • Different 𝑪𝑻𝑴

group 3

itself are the same s for each use of

group 3

as obj1 𝑻 𝟏 root 𝑻 𝟎 group1 group3 𝑻 𝟐 a whole 𝑻 𝟎 𝑻 𝟏 𝑣𝑠. 𝑻 𝟎 𝑻 𝟐 𝑻 𝟒 obj2 𝑻 𝟑 group2 𝑻 𝟒 obj3 𝑻 𝟓 group3 𝑻 𝟔 group3 obj4 44/45 Universidad Católica Andrés Bello » Computación Gráfica » Ciro Durán