1 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
2 Transformations in 3D Transformations are used to –define and manipulate objects –project from 3D to 2D Different levels of abstraction –Matrix4d –Transform3D –TransformGroup
3 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
4 Representing 3D Transformations 3D affine transformation In homogeneous coordinates
5 Matrix Classes Low-level representation for transformations Matrix classes for 3x3 and 4x4 matrices for both float and double type Matrix3f Matrix3d Matrix4f Matrix4d GMatrix can be used for matrices of arbitrary size, not necessarily square GMatrix
6 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 Matrix void add(Matrix4d m1, Matrix4d m2) void sub(Matrix4d m1, Matrix4d m2) void mul(Matrix4d m1, Matrix4d m2)
7 Matrix Operations Matrix times its inverse gives identity matrix void invert() void invert(Matrix4d m1) Exchange rows and columns void transpose() Multiply all elements by the same value void mul(double scalar) Determinant as a scalar value associated with a matrix (sort of like the magnitude of a vector) double determinant()
8 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 4 x 4 matrix Can also set a particular type of transformation
9 Translation in 3D Translation can be specified by a vector which becomes the fourth column of the transformation matrix void set(Vector3d trans) void set(Vector3f trans) void setTranslation(Vector3d trans) void setTranslation(Vector3f trans)
10 Scaling in 3D Can scale the entire object uniformly by specifying a single scale factor void set(double scale) void setScale(double scale) Can scale the object non- uniformly by specifying a vector with a scale factor for each direction void setScale(Vector3d scales)
11 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
12 Shear in 3D In 3D, shear along a plane Can affect a single coordinate x ' = x + sh x z or two coordinates x ' = x + sh x z y ' = y + sh y z
13 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 AxisAngle4d void set(AxisAngle4d r)
14 Quaternions Quaternions provide an extension of complex numbers –instead of two values, a quaternion has 4 An arbitrary quaternion has the form p = A + b i + c j + d k i, j and k satisfy i 2 = j 2 = k 2 = ijk = -1 Tuple operations plus conjugate and inverse
15 Quaternions for Rotation A point represented as a pure quaternion –no real part p = 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
16 Euler Angles Another way to think about rotations in 3D Use three rotations about the coordinate axes –elevation, azimuth, tilt –roll, pitch, yaw –precession, nutation, spin –heading, altitude, bank Transform3D method void setEuler(Vector3d eulerAngles)
17 Quaternion to Euler Angle No Transform3D method for retrieving Euler angles Compute them from the quaternion public 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); }
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
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
20 Scene Graph for Axes class
10/17/08 Rotation.java Create ColorCubes Use a series of rotation transformations to position the cubes uniformly in a ring around an axis
22 Composite Transforms Composition of T 1 and T 2 is (T 2 T 1 )(P) = (T 2 ( T 1 (P) ) ) Order matters –The first transformation to be applied is the second in the list T 2 T 1 != T 1 T 2
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 -1 RT –Translate the scene so that the desired operation is trivial to write down –Do the desired transfromation –Translate back after the transformation
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
10/17/08 Example 2: Mirror.java Reflection into an arbitrary plane a x + b y + c z = 0 Solution –Rotate to map the plane into the x-y plane d = sqrt( a 2 + b 2 ) cos( ) = c / sqrt( a 2 + b 2 + c 2 ) 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
26 Transform3D methods Transform Point3D objects void transform(Point3d p) void transform(Point3d p, Point3d pOut) void transform(Point3f p) void transform(Point3f p, Point3f pOut) Transform Vectors void 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)
27 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
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 Methods int currentSegment( double [] coordinates) boolean isDone() void next() int getWindingRule()
29 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
30 Transformation and Shared Branch