30
1 Zhang & Liang, Computer Graphics Using Java 2D and 3D (c) 2007 Pearso n Education, Inc. All rights reserved. Chapter 7 Geometric Transformation 3D transformations 3D affine transforms including translation, rotation, scaling, shearing, and reflections Transformation matrices Applying transforms in scene graphs Composite transformations Using transforms in constructing geometries

1 Chapter 7 Geometric Transformation 3D transformations 3D affine transforms including translation, rotation, scaling, shearing, and reflections

  • View
    233

  • Download
    1

Embed Size (px)

Citation preview

Page 1: 1 Chapter 7 Geometric Transformation  3D transformations  3D affine transforms including translation, rotation, scaling, shearing, and reflections

1Zhang & Liang, Computer Graphics Using Java 2D and 3D (c) 2007 Pearson Education, Inc. All rights reserved.

Chapter 7 Geometric Transformation

3D transformations 3D affine transforms including translation,

rotation, scaling, shearing, and reflections Transformation matrices Applying transforms in scene graphs Composite transformations Using transforms in constructing geometries

Page 2: 1 Chapter 7 Geometric Transformation  3D transformations  3D affine transforms including translation, rotation, scaling, shearing, and reflections

2Zhang & Liang, Computer Graphics Using Java 2D and 3D (c) 2007 Pearson Education, Inc. All rights reserved.

Transformations in 3D

Transformations are used to– define and manipulate objects– project from 3D to 2D

Different levels of abstraction– Matrix4d– Transform3D– TransformGroup

Page 3: 1 Chapter 7 Geometric Transformation  3D transformations  3D affine transforms including translation, rotation, scaling, shearing, and reflections

3Zhang & Liang, Computer Graphics Using Java 2D and 3D (c) 2007 Pearson Education, Inc. All rights reserved.

Types of Transformations

Affine transform maps lines to lines – preserves parallelism

Rigid motions (aka isometries, Euclidean motions) preserve shape

There are the same types of transformations in 3D as in 2D but they can be more complicated

Page 4: 1 Chapter 7 Geometric Transformation  3D transformations  3D affine transforms including translation, rotation, scaling, shearing, and reflections

4Zhang & Liang, Computer Graphics Using Java 2D and 3D (c) 2007 Pearson Education, Inc. All rights reserved.

Representing 3D Transformations

3D affine transformation

In homogeneous coordinates

Page 5: 1 Chapter 7 Geometric Transformation  3D transformations  3D affine transforms including translation, rotation, scaling, shearing, and reflections

5Zhang & Liang, Computer Graphics Using Java 2D and 3D (c) 2007 Pearson Education, Inc. All rights reserved.

Matrix Classes

Low-level representation for transformations Matrix classes for 3x3 and 4x4 matrices for both

float and double typeMatrix3fMatrix3dMatrix4fMatrix4d

GMatrix can be used for matrices of arbitrary size, not necessarily squareGMatrix

Page 6: 1 Chapter 7 Geometric Transformation  3D transformations  3D affine transforms including translation, rotation, scaling, shearing, and reflections

6Zhang & Liang, Computer Graphics Using Java 2D and 3D (c) 2007 Pearson Education, Inc. All rights reserved.

Matrix Operations

Update the current matrix (similar to compound assignment)void add(Matrix4d m1)void sub(Matrix4d m1)void mul(Matrix4d m1)

Combine two MatrixObjects and put result into current Matrixvoid add(Matrix4d m1, Matrix4d m2)void sub(Matrix4d m1, Matrix4d m2)void mul(Matrix4d m1, Matrix4d m2)

Page 7: 1 Chapter 7 Geometric Transformation  3D transformations  3D affine transforms including translation, rotation, scaling, shearing, and reflections

7Zhang & Liang, Computer Graphics Using Java 2D and 3D (c) 2007 Pearson Education, Inc. All rights reserved.

Matrix Operations

Matrix times its inverse gives identity matrixvoid invert()void invert(Matrix4d m1)

Exchange rows and columnsvoid transpose()

Multiply all elements by the same valuevoid mul(double scalar)

Determinant as a scalar value associated with a matrix (sort of like the magnitude of a vector)double determinant()

Page 8: 1 Chapter 7 Geometric Transformation  3D transformations  3D affine transforms including translation, rotation, scaling, shearing, and reflections

8Zhang & Liang, Computer Graphics Using Java 2D and 3D (c) 2007 Pearson Education, Inc. All rights reserved.

Transform3D

A higher-level representation for transformations

Has constructors to create from both Matrix objects and from vectors

Also has set and get methods for the internal 4x4 matrix

Can also set a particular type of transformation

Page 9: 1 Chapter 7 Geometric Transformation  3D transformations  3D affine transforms including translation, rotation, scaling, shearing, and reflections

9Zhang & Liang, Computer Graphics Using Java 2D and 3D (c) 2007 Pearson Education, Inc. All rights reserved.

Translation in 3D Translation can be specified by a vector

which becomes the fourth column of the transformation matrixvoid set(Vector3d trans)void set(Vector3f trans)void setTranslation(Vector3d trans)void setTranslation(Vector3f trans)

Page 10: 1 Chapter 7 Geometric Transformation  3D transformations  3D affine transforms including translation, rotation, scaling, shearing, and reflections

10Zhang & Liang, Computer Graphics Using Java 2D and 3D (c) 2007 Pearson Education, Inc. All rights reserved.

Scaling in 3D

Can scale the entire object uniformly by specifying a single scale factorvoid set(double scale)void setScale(double scale)

Can scale the object non-uniformly by specifying a vector with a scale factor for each directionvoid setScale(Vector3d scales)

Page 11: 1 Chapter 7 Geometric Transformation  3D transformations  3D affine transforms including translation, rotation, scaling, shearing, and reflections

11Zhang & Liang, Computer Graphics Using Java 2D and 3D (c) 2007 Pearson Education, Inc. All rights reserved.

Reflection in 3D

In 3D, reflection is performed into a plane Reflection of plane through origin with

normal vector u can be expressed as– vector equation

Matrix representation:– inverse is same matrix

Page 12: 1 Chapter 7 Geometric Transformation  3D transformations  3D affine transforms including translation, rotation, scaling, shearing, and reflections

12Zhang & Liang, Computer Graphics Using Java 2D and 3D (c) 2007 Pearson Education, Inc. All rights reserved.

Shear in 3D

In 3D, shear along a plane Can affect a single coordinate

x' = x + shx z

or two coordinatesx' = x + shx z

y' = y + shy z

Page 13: 1 Chapter 7 Geometric Transformation  3D transformations  3D affine transforms including translation, rotation, scaling, shearing, and reflections

13Zhang & Liang, Computer Graphics Using Java 2D and 3D (c) 2007 Pearson Education, Inc. All rights reserved.

3D Rotation In 3D, can rotate about an

arbitrary line– hard to come up with a matrix for an

arbitrary rotation AxisAngle4d allows you to

specify the line to rotate about– AxisAngle4d( x, y, z, q)

represents rotation by angle about the vector (x, y, z)

Set a general rotation in Transform3D using AxisAngle4dvoid set(AxisAngle4d r)

Page 14: 1 Chapter 7 Geometric Transformation  3D transformations  3D affine transforms including translation, rotation, scaling, shearing, and reflections

14Zhang & Liang, Computer Graphics Using Java 2D and 3D (c) 2007 Pearson Education, Inc. All rights reserved.

Quaternions

Quaternions provide an extension of complex numbers– instead of two values, a quaternion has 4

An arbitrary quaternion has the formp = A + b i + c j + d k

i, j and k satisfyi2 = j2 = k2 = ijk = -1

Tuple operations plus conjugate and inverse

Page 15: 1 Chapter 7 Geometric Transformation  3D transformations  3D affine transforms including translation, rotation, scaling, shearing, and reflections

15Zhang & Liang, Computer Graphics Using Java 2D and 3D (c) 2007 Pearson Education, Inc. All rights reserved.

Quaternions for Rotation

A point represented as a pure quaternion– no real partp = x i + y j + z k

A rotation defined by quaternion operations

– θ is the angle of rotation and u defines the axis of rotation through the origin

Page 16: 1 Chapter 7 Geometric Transformation  3D transformations  3D affine transforms including translation, rotation, scaling, shearing, and reflections

16Zhang & Liang, Computer Graphics Using Java 2D and 3D (c) 2007 Pearson Education, Inc. All rights reserved.

Euler Angles

Another way to think about rotations in 3DUse three rotations about the coordinate

axes– elevation, azimuth, tilt– roll, pitch, yaw– precession, nutation, spin – heading, altitude, bank

Transform3D methodvoid setEuler(Vector3d eulerAngles)

Page 17: 1 Chapter 7 Geometric Transformation  3D transformations  3D affine transforms including translation, rotation, scaling, shearing, and reflections

17Zhang & Liang, Computer Graphics Using Java 2D and 3D (c) 2007 Pearson Education, Inc. All rights reserved.

Quaternion to Euler Angle

No Transform3D method for retrieving Euler angles

Compute them from the quaternionpublic static Vector3d quatToEuler(Quat4d q1) { double sqw = q1.w*q1.w; double sqx = q1.x*q1.x; double sqy = q1.y*q1.y; double sqz = q1.z*q1.z; double heading = Math.atan2(2.0 * (q1.x*q1.y + q1.z*q1.w), (sqx - sqy - sqz + sqw)); double bank = Math.atan2(2.0 * (q1.y*q1.z + q1.x*q1.w), (-sqx - sqy + sqz + sqw)); double attitude = Math.asin(-2.0 * (q1.x*q1.z - q1.y*q1.w)); return new Vector3d(bank, attitude, heading);}

Page 18: 1 Chapter 7 Geometric Transformation  3D transformations  3D affine transforms including translation, rotation, scaling, shearing, and reflections

10/17/08

TestTransform.java

Use this to explore different transformations– Enter a transformation matrix and see what it

does– Enter data for particular kind of transformation

and see what it does

Page 19: 1 Chapter 7 Geometric Transformation  3D transformations  3D affine transforms including translation, rotation, scaling, shearing, and reflections

10/17/08

Transform3D Node

Within a SceneGraph, a TransformGroup node implements a transformation that is applied to all of its children

This allows us to build hierarchical scenes

Page 20: 1 Chapter 7 Geometric Transformation  3D transformations  3D affine transforms including translation, rotation, scaling, shearing, and reflections

20Zhang & Liang, Computer Graphics Using Java 2D and 3D (c) 2007 Pearson Education, Inc. All rights reserved.

Scene Graph for Axes class

Page 21: 1 Chapter 7 Geometric Transformation  3D transformations  3D affine transforms including translation, rotation, scaling, shearing, and reflections

10/17/08

Rotation.java

Create ColorCubes Use a series of rotation transformations to

position the cubes uniformly in a ring around an axis

Page 22: 1 Chapter 7 Geometric Transformation  3D transformations  3D affine transforms including translation, rotation, scaling, shearing, and reflections

22Zhang & Liang, Computer Graphics Using Java 2D and 3D (c) 2007 Pearson Education, Inc. All rights reserved.

Composite Transforms

Composition of T1 and T2 is(T2T1)(P) = (T2 ( T1 (P) ) )

Order matters– The first transformation to be applied is the

second in the list

T2T1 != T1T2

Page 23: 1 Chapter 7 Geometric Transformation  3D transformations  3D affine transforms including translation, rotation, scaling, shearing, and reflections

10/17/08

A Common Pattern

Arbitrary transformations are hard to write matrices for– Rotation about an arbitrary axis– Reflection into an arbitrary plane

Solution: T-1RT– Translate the scene so that the desired operation

is trivial to write down – Do the desired transfromation– Translate back after the transformation

Page 24: 1 Chapter 7 Geometric Transformation  3D transformations  3D affine transforms including translation, rotation, scaling, shearing, and reflections

10/17/08

Example 1

Rotation about an arbitrary axis– / 3 rotation about line through (1,1,0) and

(1,2,1) Translate by (-1, -1, 0) so first point goes through

origin Use a quaternion to specify the rotation Translate by (1, 1, 0) to get back to original position

Page 25: 1 Chapter 7 Geometric Transformation  3D transformations  3D affine transforms including translation, rotation, scaling, shearing, and reflections

10/17/08

Example 2: Mirror.java

Reflection into an arbitrary planea x + b y + c z = 0

Solution– Rotate to map the plane into the x-y plane

d = sqrt( a2 + b2)cos() = c / sqrt( a2 + b2 + c2)q = cos(/2) + (b/d i - a/d j) sin (/2)

– Reflection into x-y plane (z -> -z)– Do inverse rotation to get back to original

Page 26: 1 Chapter 7 Geometric Transformation  3D transformations  3D affine transforms including translation, rotation, scaling, shearing, and reflections

26Zhang & Liang, Computer Graphics Using Java 2D and 3D (c) 2007 Pearson Education, Inc. All rights reserved.

Transform3D methods

Transform Point3D objectsvoid transform(Point3d p)void transform(Point3d p, Point3d pOut)void transform(Point3f p)void transform(Point3f p, Point3f pOut)

Transform Vectorsvoid transform(Vector3d v)void transform(Vector3d v, Vector3d vOut)void transform(Vector3f v)void transform(Vector3f v, Vector3f vOut)void transform(Vector4d v)void transform(Vector4d v, Vector4d vOut)void transform(Vector4f v)void transform(Vector4f v, Vector4f vOut)

Page 27: 1 Chapter 7 Geometric Transformation  3D transformations  3D affine transforms including translation, rotation, scaling, shearing, and reflections

27Zhang & Liang, Computer Graphics Using Java 2D and 3D (c) 2007 Pearson Education, Inc. All rights reserved.

Shape Construction by Extrusion

Extend a 2D shape to 3D– Get a PathIterator for the shape– Construct quadrilateral (or triangular) polygon

mesh between successive points along the path– see extrudeShape.java for method that does this

Java3D provides a class to do this automatically to create Font3D objects– See Hello3D example from chapter 5

Page 28: 1 Chapter 7 Geometric Transformation  3D transformations  3D affine transforms including translation, rotation, scaling, shearing, and reflections

10/17/08

PathIterator

An interface that allows Shape objects to return an iterator to their sub-parts

Defines constants to represent the different types of path segments

Methodsint currentSegment( double [] coordinates)

boolean isDone()

void next()

int getWindingRule()

Page 29: 1 Chapter 7 Geometric Transformation  3D transformations  3D affine transforms including translation, rotation, scaling, shearing, and reflections

29Zhang & Liang, Computer Graphics Using Java 2D and 3D (c) 2007 Pearson Education, Inc. All rights reserved.

Shape Construction by Rotation

Many shapes can be created by rotating a curve around an axis– create a polygon mesh between points at

successive angles Example: constructing a torus

– See Torus.java, TestTorus.java

Page 30: 1 Chapter 7 Geometric Transformation  3D transformations  3D affine transforms including translation, rotation, scaling, shearing, and reflections

30Zhang & Liang, Computer Graphics Using Java 2D and 3D (c) 2007 Pearson Education, Inc. All rights reserved.

Transformation and Shared Branch