Math for Game Programmers: Dual Numbers. Gino van den Bergen gino@dtecta.com



Similar documents
Math 241, Exam 1 Information.

Solving Simultaneous Equations and Matrices

Grassmann Algebra in Game Development. Eric Lengyel, PhD Terathon Software

MATH 304 Linear Algebra Lecture 9: Subspaces of vector spaces (continued). Span. Spanning set.

Vectors 2. The METRIC Project, Imperial College. Imperial College of Science Technology and Medicine, 1996.

Adding vectors We can do arithmetic with vectors. We ll start with vector addition and related operations. Suppose you have two vectors

Content. Chapter 4 Functions Basic concepts on real functions 62. Credits 11

Solutions to old Exam 1 problems

The Essentials of CAGD

Figure 1.1 Vector A and Vector F

discuss how to describe points, lines and planes in 3 space.

Essential Mathematics for Computer Graphics fast

12.5 Equations of Lines and Planes

Section 1.1. Introduction to R n

Metrics on SO(3) and Inverse Kinematics

Math 215 HW #6 Solutions

Linear algebra and the geometry of quadratic equations. Similarity transformations and orthogonal matrices

Path Tracking for a Miniature Robot

Understanding Rotations

Mechanics lecture 7 Moment of a force, torque, equilibrium of a body

9 Multiplication of Vectors: The Scalar or Dot Product

Problem Set 5 Due: In class Thursday, Oct. 18 Late papers will be accepted until 1:00 PM Friday.

GeoGebra. 10 lessons. Gerrit Stols

Lecture L3 - Vectors, Matrices and Coordinate Transformations

Estimated Pre Calculus Pacing Timeline

Vector Algebra II: Scalar and Vector Products

DEFINITION A complex number is a matrix of the form. x y. , y x

Precalculus REVERSE CORRELATION. Content Expectations for. Precalculus. Michigan CONTENT EXPECTATIONS FOR PRECALCULUS CHAPTER/LESSON TITLES

A QUICK GUIDE TO THE FORMULAS OF MULTIVARIABLE CALCULUS

H.Calculating Normal Vectors

Eigenvalues and Eigenvectors

11.1. Objectives. Component Form of a Vector. Component Form of a Vector. Component Form of a Vector. Vectors and the Geometry of Space

13.4 THE CROSS PRODUCT

SOLID MECHANICS TUTORIAL MECHANISMS KINEMATICS - VELOCITY AND ACCELERATION DIAGRAMS

Unified Lecture # 4 Vectors

Parametric Curves. (Com S 477/577 Notes) Yan-Bin Jia. Oct 8, 2015

A vector is a directed line segment used to represent a vector quantity.

Kinematics of Robots. Alba Perez Gracia

L 2 : x = s + 1, y = s, z = 4s Suppose that C has coordinates (x, y, z). Then from the vector equality AC = BD, one has

State of Stress at Point

3D Tranformations. CS 4620 Lecture 6. Cornell CS4620 Fall 2013 Lecture Steve Marschner (with previous instructors James/Bala)

Solutions to Practice Problems

Animation (-4, -2, 0 ) + (( 2, 6, -4 ) - (-4, -2, 0 ))*.75 = (-4, -2, 0 ) + ( 6, 8, -4)*.75 = (.5, 4, -3 ).

THREE DIMENSIONAL GEOMETRY

December 4, 2013 MATH 171 BASIC LINEAR ALGEBRA B. KITCHENS

Vectors Math 122 Calculus III D Joyce, Fall 2012

Geometry of Vectors. 1 Cartesian Coordinates. Carlo Tomasi

FURTHER VECTORS (MEI)

Vector Algebra CHAPTER 13. Ü13.1. Basic Concepts

Concepts in Calculus III

Computer Graphics. Geometric Modeling. Page 1. Copyright Gotsman, Elber, Barequet, Karni, Sheffer Computer Science - Technion. An Example.

Geometric description of the cross product of the vectors u and v. The cross product of two vectors is a vector! u x v is perpendicular to u and v

We can display an object on a monitor screen in three different computer-model forms: Wireframe model Surface Model Solid model

α = u v. In other words, Orthogonal Projection

South Carolina College- and Career-Ready (SCCCR) Pre-Calculus

CS 4620 Practicum Programming Assignment 6 Animation

28 CHAPTER 1. VECTORS AND THE GEOMETRY OF SPACE. v x. u y v z u z v y u y u z. v y v z

1.3. DOT PRODUCT If θ is the angle (between 0 and π) between two non-zero vectors u and v,

Rotation Rate of a Trajectory of an Algebraic Vector Field Around an Algebraic Curve

Dot product and vector projections (Sect. 12.3) There are two main ways to introduce the dot product

animation animation shape specification as a function of time

CATIA V5 Tutorials. Mechanism Design & Animation. Release 18. Nader G. Zamani. University of Windsor. Jonathan M. Weaver. University of Detroit Mercy

Appendix 3 IB Diploma Programme Course Outlines

Exam 1 Sample Question SOLUTIONS. y = 2x

MA 323 Geometric Modelling Course Notes: Day 02 Model Construction Problem

v 1 v 3 u v = (( 1)4 (3)2, [1(4) ( 2)2], 1(3) ( 2)( 1)) = ( 10, 8, 1) (d) u (v w) = (u w)v (u v)w (Relationship between dot and cross product)

Continuous Groups, Lie Groups, and Lie Algebras

Vector Math Computer Graphics Scott D. Anderson

Curves and Surfaces. Goals. How do we draw surfaces? How do we specify a surface? How do we approximate a surface?

a.) Write the line 2x - 4y = 9 into slope intercept form b.) Find the slope of the line parallel to part a

Part I. Basic Maths for Game Design

Identifying second degree equations

Thnkwell s Homeschool Precalculus Course Lesson Plan: 36 weeks

Geometric algebra rotors for skinned character animation blending

The Inversion Transformation

Common Core Unit Summary Grades 6 to 8

3 Orthogonal Vectors and Matrices

PHYSICS 151 Notes for Online Lecture #6

Core Maths C2. Revision Notes

w = COI EYE view direction vector u = w ( 010,, ) cross product with y-axis v = w u up vector

Bicycle Math. presented to the Olivetti Club. Timothy E. Goldberg. March 30, Cornell University Ithaca, New York

Basic Linear Algebra

Vector has a magnitude and a direction. Scalar has a magnitude

Goldsmiths, University of London. Computer Animation. Goldsmiths, University of London

Cross product and determinants (Sect. 12.4) Two main ways to introduce the cross product

LINES AND PLANES CHRIS JOHNSON

8-3 Dot Products and Vector Projections

Recall that two vectors in are perpendicular or orthogonal provided that their dot

CS 4204 Computer Graphics

Orthogonal Projections

Mathematics Notes for Class 12 chapter 10. Vector Algebra

Review Sheet for Test 1

Problem set on Cross Product

B4 Computational Geometry

Equations Involving Lines and Planes Standard equations for lines in space

The Geometry of the Dot and Cross Products

ISOMETRIES OF R n KEITH CONRAD

MAT 1341: REVIEW II SANGHOON BAEK

Computer Graphics CS 543 Lecture 12 (Part 1) Curves. Prof Emmanuel Agu. Computer Science Dept. Worcester Polytechnic Institute (WPI)

NEW YORK STATE TEACHER CERTIFICATION EXAMINATIONS

Transcription:

Math for Game Programmers: Dual Numbers Gino van den Bergen gino@dtecta.com

Introduction Dual numbers extend real numbers, similar to complex numbers. Complex numbers adjoin an element i, for which i 2 = -1. Dual numbers adjoin an element ε, for which ε 2 = 0.

Complex Numbers Complex numbers have the form z = a + b i where a and b are real numbers. a = real(z) is the real part, and b = imag(z) is the imaginary part.

Complex Numbers (cont d) Complex operations pretty much follow rules for real operators: Addition: (a + b i) + (c + d i) = (a + c) + (b + d) i Subtraction: (a + b i) (c + d i) = (a c) + (b d) i

Complex Numbers (cont d) Multiplication: (a + b i) (c + d i) = (ac bd) + (ad + bc) i Products of imaginary parts feed back into real parts.

Dual Numbers Dual numbers have the form z = a + b ε similar to complex numbers. a = real(z) is the real part, and b = dual(z) is the dual part.

Dual Numbers (cont d) Operations are similar to complex numbers, however since ε 2 = 0, we have: (a + b ε) (c + d ε) = (ac + 0) + (ad + bc)ε Dual parts do not feed back into real parts!

Dual Numbers (cont d) The real part of a dual calculation is independent of the dual parts of the inputs. The dual part of a multiplication is a cross product of real and dual parts.

Taylor Series Any value f(a + h) of a smooth function f can be expressed as an infinite sum: f f ( a) f ( a) ( a h) f ( a) h h 1! 2! 2 where f, f,, f (n) are the first, second,, n-th derivative of f.

Taylor Series Example

Taylor Series Example

Taylor Series Example

Taylor Series Example

Taylor Series Example

Taylor Series and Dual Numbers For f(a + b ε), the Taylor series is: f ( a) f ( a b ) f ( a) b 0 1! All second- and higher-order terms vanish! We have a closed-form expression that holds the function and its derivative.

Real Functions on Dual Numbers Any differentiable real function f can be extended to dual numbers, as: f(a + b ε) = f(a) + b f (a) ε For example, sin(a + b ε) = sin(a) + b cos(a) ε

Automatic Differentiation Add a unit dual part to the input value of a real function. Evaluate function using dual arithmetic. The output has the function value as real part and the derivate s value as dual part: f(a + ε) = f(a) + f (a) ε

How does it work? Check out the product rule of differentiation: ( f g) f g f g Notice the cross product of functions and their derivatives. Recall that (a + a ε)(b + b ε) = ab + (ab + a b)ε

Automatic Differentiation in C++ We need some easy way of extending functions on floating-point types to dual numbers and we need a type that holds dual numbers and offers operators for performing dual arithmetic.

Extension by Abstraction C++ allows you to abstract from the numerical type through: Typedefs Function templates Constructors and conversion operators Overloading Traits class templates

Abstract Scalar Type Never use built-in floating-point types, such as float or double, explicitly. Instead use a type name, e.g. Scalar, either as template parameter or as typedef, typedef float Scalar;

Constructors Built-in types have constructors as well: Default: float() == 0.0f Conversion: float(2) == 2.0f Use constructors for defining constants, e.g. use Scalar(2), rather than 2.0f or (Scalar)2.

Overloading Operators and functions on built-in types can be overloaded in numerical classes, such as std::complex. Built-in types support operators: +,-,*,/ and functions: sqrt, pow, sin, NB: Use <cmath> rather than <math.h>. That is, use sqrt NOT sqrtf on floats.

Traits Class Templates Type-dependent constants, such as the machine epsilon, are obtained through a traits class defined in <limits>. Use std::numeric_limits<scalar>::epsilon() rather than FLT_EPSILON in C++. Either specialize std::numeric_limits for your numerical classes or write your own traits class.

Example Code (before) float smoothstep(float x) { if (x < 0.0f) x = 0.0f; else if (x > 1.0f) x = 1.0f; return (3.0f 2.0f * x) * x * x; }

Example Code (after) template <typename T> T smoothstep(t x) { if (x < T()) x = T(); else if (x > T(1)) x = T(1); return (T(3) T(2) * x) * x * x; }

Dual Numbers in C++ C++ has a standard class template std::complex<t> for complex numbers. We create a similar class template Dual<T> for dual numbers. Dual<T> defines constructors, accessors, operators, and standard math functions.

Dual<T> template <typename T> class Dual { private: T mreal; T mdual; };

Dual<T>: Constructor template <typename T> Dual<T>::Dual(T real = T(), T dual = T()) : mreal(real), mdual(dual) {} Dual<Scalar> z1; // zero initialized Dual<Scalar> z2(2); // zero dual part Dual<Scalar> z3(2, 1);

Dual<T>: operators template <typename T> Dual<T> operator*(dual<t> a, Dual<T> b) { return Dual<T>( a.real() * b.real(), a.real() * b.dual() + a.dual() * b.real() ); }

Dual<T>: Standard Math template <typename T> Dual<T> sqrt(dual<t> z) { T tmp = sqrt(z.real()); return Dual<T>( tmp, z.dual() / (T(2) * tmp) ); }

Curve Tangent For a 3D curve The tangent is ], [ where )), ( ), ( ), ( ( ) ( b a t t z t y t x t p )) ( ), ( ), ( ( ) ( where, ) ( ) ( t z t y t x t t t p p p

Curve Tangent Curve tangents are often computed by approximation: p( t1) p( t0), where p( t ) p( t ) 1 0 t 1 t 0 h for tiny values of h.

Curve Tangent: Bad #1 P(t 0 ) Actual tangent P(t 1 )

Curve Tangent: Bad #2 t 1 drops outside parameter domain (t 1 > b) P(t 1 ) P(t 0 )

Curve Tangent: Duals Make a curve function template using a class template for 3D vectors: template <typename T> Vector3<T> curvefunc(t x);

Curve Tangent: Duals (cont d) Call the curve function using a dual number x = Dual<Scalar>(t, 1), (add ε to parameter t): Vector3<Dual<Scalar> > y = curvefunc(dual<scalar>(t, 1));

Curve Tangent: Duals (cont d) The real part is the evaluated position: Vector3<Scalar> position = real(y); The normalized dual part is the tangent at this position: Vector3<Scalar> tangent = normalize(dual(y));

Line Geometry The line through points p and q can be expressed explicitly as: x(t) = p + (q p)t, and Implicitly, as a set of points x for which: (q p) x + p q = 0

Line Geometry q p q p q is orthogonal to the plane 0pq, and its length is equal to the area of the parallellogram spanned by p and q 0 p

Line Geometry q p q 0 x p All points x on the line pq span with q p a parallellogram that has the same area and orientation as the one spanned by p and q.

Plücker Coordinates Plücker coordinates are 6-tuples of the form (u x, u y, u z, v x, v y, v z ), where u = (u x, u y, u z ) = q p, and v = (v x, v y, v z ) = p q

Plücker Coordinates (cont d) For (u 1 :v 1 ) and (u 2 :v 2 ) directed lines, if u 1 v 2 + v 1 u 2 is zero: the lines intersect positive: the lines cross right-handed negative: the lines cross left-handed

Triangle vs. Ray If the signs of permuted dot products of the ray and edges are all equal, then the ray intersects the triangle.

Plücker Coordinates and Duals Dual 3D vectors conveniently represent Plücker coordinates: Vector3<Dual<Scalar> > For a line (u:v), u is the real part and v is the dual part.

Dot Product of Dual Vectors The dot product of dual vectors u 1 + v 1 ε and u 2 + v 2 ε is a dual number z, for which real(z) = u 1 u 2, and dual(z) = u 1 v 2 + v 1 u 2 The dual part is the permuted dot product

Angle of Dual Vectors For a and b dual vectors, we have a b d arccos a b where θ is the angle and d is the signed distance between the lines a and b.

Translation Translation of lines only affects the dual part. Translation of line pq over c gives: Real: (q + c) (p + c) = q - p Dual: (p + c) (q + c) = p q + c (q p) q p pops up in the dual part!

Rotation Real and dual parts are rotated in the same way. For a rotation matrix R: Real: Rq Rp = R(q p) Dual: Rp Rq = R(p q) The latter holds for rotations only! That is, R performs no scaling or reflection.

Rigid-Body Transform For rotation matrix R and translation vector c, the dual 3 3 matrix M with real(m) = R, and dual(m) = maps Plücker coordinates to the new reference frame. R R c 0 0 0 ] [ x y x z y z c c c c c c

Screw Theory A screw motion is a rotation about a line and a translation along the same line. Any rigid body displacement can be defined by a screw motion. (Chasles)

Chasles Theorem (Sketchy Proof) Decompose translation into a term along the line and a term orthogonal to the line. Translation orthogonal to the axis of rotation offsets the axis. Translation along the axis does not care about the position of the axis.

Example: Rolling Ball

Dual Quaternions Unit dual quaternions represent screw motions. The rigid body transform over a unit quaternion q and vector c is: 1 q cq 2 Here, c is a quaternion with zero scalar part.

Where is the Screw? A unit dual quaternion can be written as d d cos sin 2 2 ( u v where θ is the rotation angle, d, the translation distance, and u + vε, the line given in Plücker coordinates. )

Two Conjugates For dual quaternion q = q r + q d ε, the dual conjugate is q q r q d And the quaternion conjugate is q q r qd

Rigid-Body Transform Revisited Similar to 3D vectors, Plücker coordinates can be transformed using dual quaternions. The mapping of a dual vector v according to a screw motion q is v = q v q*

Traditional Skinning Bones are defined by transformation matrices T i relative to the rest pose. Each vertex is transformed as p' Tp T p ( T 1 n n 1 1 T 1 n n ) p Here, λ i are blend weights.

Traditional Skinning (cont d) A weighted sum of matrices is not necessarily a rigid-body transformation. Most notable artifact is candy wrapper : The skin collapses while transiting from one bone to the other.

Candy Wrapper

Dual Quaternion Skinning Use a blend operation that always returns a rigid-body transformation. Several options exists. The simplest one is a normalized lerp of dual quaternions: n n n n q q q q q 1 1 1 1

Dual Quaternion Skinning (cont d) Can the weighted sum of dual quaternions ever get zero? Not if all dual quaternions lie in the same hemisphere. Observe that q and q are the same pose. If necessary, negate each q i to dot positively with q 0.

Rigid-Body Transform Revisited 2 Points can also be transformed by dual quaternions. For a point p, the image under transformation p is obtained by 1 p' q(1 p ) q Notice the use of both dual and quaternion conjugate! *

Further Uses Motor Algebra: Linear and angular velocity of a rigid body combined in a dual 3D vector. Spatial Vector Algebra: Featherstone uses 6D vectors for representing velocities and forces in robot dynamics.

Conclusions Abstract from numerical types in your C++ code. Differentiation is easy, fast, and exact with dual numbers. Dual numbers have other uses as well. Explore yourself!

References D. Vandevoorde and N. M. Josuttis. C++ Templates: The Complete Guide. Addison-Wesley, 2003. K. Shoemake. Plücker Coordinate Tutorial. Ray Tracing News, Vol. 11, No. 1 R. Featherstone. Robot Dynamics Algorithms. Kluwer Academic Publishers, 1987. L. Kavan et al. Skinning with dual quaternions. Proc. ACM SIGGRAPH Symposium on Interactive 3D Graphics and Games, 2007

Thank You! For sample code, check out free* MoTo C++ template library on: http://www.dtecta.com (*) gratis (as in free beer ) and libre (as in free speech )