SAN FRANCISCO JULY 22-26 Volume 19, Number 3, 1985 I Animating Rotation with Quaternion Curves Ken Shoemaker The Singer C o m p a n y Link Flight Simulation Division ABSTRA CT Solid bodies roll and t u m b l e through space. In c o m p u t e r a n i m a t i o n , so do cameras. The r o t a t i o n s of these objects are best described using a four coordinate system, quaternions, as is shown in this paper. Of all quaternions, those on the unit sphere are most suitable for a n i m a t i o n , but the question of how to construct curves on spheres has not been much explored. This p a p e r gives one answer by presenting a new kind of spline curve, c r e a t e d on a sphere, suitable for smoothly in-hetweening (i.e. i n t e r p o l a t i n g ) sequences of a r b i t r a r y rotations. Both t h e o r y a n d experiment show t h a t the motion g e n e r a t e d is smooth and n a t u r a l , w i t h o u t quirks found in earlier methods. C.R. Classification: G . l . 1 [Numerical Analysis] Interpolation--Spline and piecewise polynomial interpolation; G.1.2 [Numerical Analysis] Approximation--Spline and piecewise polynomial a p p r o x i m a t i o n ; 1.2.9 [Artificial Intelligence] R o b o t i c s - M a n i p u l a t o r s ; 1.3.5 [Computer Graphics] C o m p u t a t i o n a l G e o m e t r y and O b j e c t M o d e l l i n g - - C u r v e , surface, solid, and object representation, - - G e o m e t r i c algorithms, languages, and systems, - - H i e r a r c h y a n d geometric transformations General Terms: Algorithms, Theory phrases: quaternlon, rotation, spherical geometry, spline, B6zier curve, B-spline, a n i m a t i o n , interpolation, a p p r o x i m a t i o n , in-betweening ieywords and 1. I n t r o d u c t i o n C o m p u t e r a n i m a t i o n of three dimensional objects i m i t a t e s the key .frame techniques of t r a d i t i o n a l a n i m a t i o n , using key positions in space i n s t e a d of key Permission to copy without fee all or part of this material is granted provided that the copics arc not made or distributed for direct commercial advantage, the ACM copyright notice and the title of the publication and its date appear, and notice is given that copying is by permission of the Association for Computing Machinery. To copy otherwise, or to republish, requires a fee and/or specific permission. © 1985 A C M 0-89791-166-0/85/007/0245 $00.75 drawings. Physics says t h a t the general position of a rigid b o d y can be given b y combining a t r a n s l a t i o n with a rotation. Computer animators key such t r a n s f o r m a t i o n s to control both s i m u l a t e d cameras a n d objects to be rendered. In following such an a p p r o a c h , one is n a t u r a l l y led to ask: W h a t is the best r e p r e s e n t a t i o n for general rotations, and how does one in-between them? Surprisingly little has been published on these topics, and the answers are not trivial. This p a p e r suggests t h a t the common solution, using three Euler's angles i n t e r p o l a t e d independently, is not ideal. T h e more recent (1843) n o t a t i o n of quaternions is proposed instead, along with i n t e r p o l a t i o n on the quaternion unit sphere. A l t h o u g h quaternions are less familiar, conversion to quaternions and generation of in-between frames can be completely a u t o m a t i c , no m a t t e r how key frames were originally specified, so users d o n ' t need to k n o w - - o r c a r e - - a b o u t inner details. The s a m e cannot be said for Euier's angles, which are more difficult to use. Spherical i n t e r p o l a t i o n itself can be used for purposes besides a n i m a t i n g rotations. For example, the set of all possible directions in space forms a sphere, the so-called G a u s s i a n sphere, on which one might w a n t to control the positions of infinitely d i s t a n t light sources. Modelling features on a globe is a n o t h e r possible application. It is simple to use a n d to p r o g r a m the method proposed here. i t is more difficult to follow its development. This stems from two causes: 1) rotations in space are more confusing t h a n one might think, a n d 2) i n t e r p o l a t i n g on a sphere is trickier t h a n i n t e r p o l a t i n g in, say, a plane. R e a d e r s well a c q u a i n t e d with splines and t h e i r use in c o m p u t e r a n i m a t i o n should have little difficulty, although e~en t h e y m a y stumble a bit over quaternions. 2. D e s c r i b i n g rotations 2.1 Rigid m o t i o n Imagine hurling a brick t o w a r d s a plate glass window. As the brick flies closer and closer, a n e a r b y physicist t Author's current address: 1700 Santa Cruz Ave., Menlo Park, CA 94025 245 @ S I G G R A P H '85 might observe t h a t , while it does not change shape or size, it can t u m b l e freely. L e o n h a r d Euler proved two centuries ago t h a t , however the brick tumbles, each position can be achieved by a single r o t a t i o n from a reference position. [Euler,1752] [Goldstein] The same is true for any rigid body. ( S h a t t e r i n g glass is obviously not a single rigid body.) T h a t is, the change m a t r i x M is orthogonal; its columns (and rows) are m u t u a l l y p e r p e n d i c u l a r unit m a g n i t u d e vectors. Because M m u s t also preserve d e t e r m i n a n t s , it is a special orthogonal m a t r i x , satisfying W h i l e t r a n s l a t i o n s are well a n i m a t e d by using vectors, r o t a t i o n a n i m a t i o n can be improved b y using the progenitor of vectors, quaternions. Q u a t e r n i o n s were discovered b y Sir W i l l i a m R o w a n H a m i l t o n in O c t o b e r of 1843. The m o m e n t is well recorded, for he considered t h e m his most i m p o r t a n t contribution, the inspired answer to a fifteen-year search for a successor to complex numbers. [Hamilton] By an odd quirk of m a t h e m a t i c s , only systems of two, four, or eight c o m p o n e n t s will m u l t i p l y as H a m i l t o n desired; triples h a d been his s t u m b l i n g block. It is well known, a n d a n y h o w easy to show, t h a t the special o r t h o g o n a l m a t r i c e s form a group, SO(3), u n d e r m u l t i p l i c a t i o n . [MacLane][Goldstein][Misner] In this r o t a t i o n group, the inverse of M is j u s t M t, the opposite r o t a t i o n . Soon a f t e r quaternions were introduced, A r t h u r Cayley published a w a y to describe r o t a t i o n s using the new m u l t i p l i c a t i o n . [Cayley] The n o t a t i o n in his p a p e r so closely a n t i c i p a t e s m a t r i x notation, which he devised several y e a r s later, t h a t it m a y be t a k e n as a formula for converting a quaternion to a r o t a t i o n matrix. It t u r n s out t h a t the four values m a k i n g up a quaternion describe r o t a t i o n in a n a t u r a l way: three of t h e m give the coordinates for the axis of r o t a t i o n , while the f o u r t h is d e t e r m i n e d by t h e angle r o t a t e d through. [Courant & Hilbert] Since c o m p u t e r graphics leans o p e r a t i o n s , it is p e r h a p s easiest to and r o t a t i o n m a t r i c e s in terms history. However quaternions can as an elegant a l g e b r a of space. [MacLane] 2.2 R o t a t i o n heavily on v e c t o r explain quaternlons of these, reversing s t a n d on their own [Herstein] [Pickert] matrices T h a t a t u m b l i n g brick does not change size, shape, nor "handedness" is m a t h e m a t i c a l l y expressed as the p r e s e r v a t i o n of dot products a n d cross products, since these measure lengths, angles, and handedness. A n d since the d e t e r m i n a n t of a 3X3 m a t r i x can be c o m p u t e d as the dot p r o d u c t of one column w i t h the cross p r o d u c t of the o t h e r two, d e t e r m i n a n t s are also preserved. Symbolically: Rot(~l)'Rot(-u2) = ~i'~2 Rot(-/11)XRot(~2) = Rot(.121)O22) det(Rot(_ul),Rot(~2),Rot(.u3) ) ----det(.ul,.U2,~3) A n i m m e d i a t e consequence is t h a t o r i e n t a t i o n changes m u s t be linear o p e r a t i o n s , since the preserved operations are; hence they have a matrix r e p r e s e n t a t i o n , M . Using the m a t r i x form of a dot p r o d u c t , 32~ a22, we can s a y more precisely t h a t ( M a21)t ( M 322) = 32~ J22, from which it follows t h a t M t M=I. 246 d e t ( M ) = +1 To illustrate, the m a t r i x M = 1 0 0 0 cos ~ --sin 0 sin ~ ] cos effects a r o t a t i o n t h r o u g h an angle of ~9 a r o u n d t h e x axis. A f t e r verifying the properties discussed so far, note t h a t the diagonal entries sum to l + 2 c o s 0. While it is too lengthy to show here, the diagonal sum m e a s u r e s the same q u a n t i t y for m a t r i c e s g e n e r a t i n g r o t a t i o n a r o u n d a n y axis. [MaeLane] 2.3 Quaternions Quaternions, like rotations, also form a nonc o m m u t a t i v e group u n d e r their multiplication, a n d these two groups are closely related. [Goldstein] [Pickert][Misner] In fact, we can s u b s t i t u t e quaternion m u l t i p l i c a t i o n for r o t a t i o n m a t r i x multiplication, a n d do less c o m p u t i n g as a result. [Taylor] To perform quaternion a r i t h m e t i c , group the four c o m p o n e n t s into a real p a r t - - a scalar, a n d an i m a g i n a r y p a r t - - a vector. A d d i t i o n is easy: a d d s c a l a r to s c a l a r a n d v e c t o r to vector. But our m a j o r i n t e r e s t is in m u l t i p l i c a t i o n . S t a r t with a simple case: m u l t i p l y two quaternions w i t h o u t real p a r t s , or more precisely, with zero real p a r t s . T h e result quaternion has a vector t h a t is the cross p r o d u c t of the two vector p a r t s , and a s c a l a r t h a t is t h e i r dot p r o d u c t , negated: • 1~2 = [ ( - a l " ~ ) , ( ~ l x - ~ ) ] It is c e r t a i n l y convenient to e h e o m p a s s b o t h v e c t o r p r o d u c t s with a single quaternion p r o d u c t . (One early lover of quaternion a l g e b r a called v e c t o r a l g e b r a a " h e r m a p h r o d i t e monster", since it required two kinds of p r o d u c t , each yielding a different t y p e of result.) If one q u a t e r n i o n has only a s c a l a r p a r t , with its v e c t o r c o m p o n e n t s all zero, multiplication is j u s t real m u l t i p l i c a t i o n a n d vector scaling. Combining the two effects gives the general rule [Brady]: [81,~1] ['52,.-/22]= [(8182--121".122),(81.122+82jj.1-Jr-.illX222)]. SAN FRANCISCO JULY 22-26 E x c e p t for the cross p r o d u c t this looks like complex multiplication, (al+ibl)(a2+ib2) ---- (ala2--blb2) + i(a lb2Wa2bl), as H a m i l t o n intended, t Q u a t e r n i o n s m u l t i p l y with a cross product because r o t a t i o n s confound axes. To illustrate , place a book in front of you, face up, w i t h the top f a r t h e s t away. Use this orientatiori as a reference. Now hold the sides and flip it t o w a r d you onto its face, r o t a t i n g 180 degrees a r o u n d a left-to-right axis, y. Then, keeping it face down, spin it clockwise 180 degrees a r o u n d an up-down z axis. Two rotations a r o u n d two p e r p e n d i c u l a r axes; yet the t o t a l change in o r i e n t a t i o n must be, according to Euler, a single rotation. Indeed, if you hold the ends of the spine a n d flip the book 180 degrees a r o u n d this third, o u t w a r d - p o i n t i n g , x axis, you should restore the original orientation. As quaternions, this is - - a n t i c i p a t i n g developments a h e a d - - [0,(0,1,0)] times [0,(0,0,1)] equals [0,(1,0,0)]; the cross product is essential. Notice how quaternion o p e r a t i o n s give a new orientation, in "quaternion coordinates", much as t r a n s l a t i o n s give a position, relative to some s t a r t i n g reference. A central message of this p a p e r is t h a t quaternlon coordinates are best for interpolating orientations. F o r comparison, imagine using spherical coordinates for translations! Q u a t e r n i o n s represent o r i e n t a t i o n as a single rotation, j u s t as r e c t a n g u l a r coordinates represent position as a single vector. T r a n s l a t i o n s combine by adding vectors; rotations, by multiplying quaternions. The s e p a r a t e axes of t r a n s l a t i o n s d o n ' t i n t e r a c t ; the axes of r o t a t i o n s must. Q u a t e r n i o n s preserve this interdependence n a t u r a l l y ; Euler's angle coordinates ignore it. 2.4 E u l e r ' s angles W h y , then, do so m a n y a n i m a t o r s use Euler's angles? Mostly, I suspect, because quaternions are unfamiliar. Unlike Euler's angles, quaternions are not t a u g h t early in s t a n d a r d m a t h a n d physics curricula. C e r t a i n l y there is a p l e t h o r a of a r g u m e n t s a g a i n s t angle coordinates. Euler's angle coordinates specify orientation as a series of three independent, rotations about pre-chosen axes. For example, the orientation of an airplane is sometimes given as "yaw" (or "heading") around a vertical axis, followed by "pitch" around a horizontal axis through the wings, followed by "roll" around the nose-to-tail line. These three angles must be used in exactly the order given because rotations do not commute. The ordering of rotation axes used is a matter of convention, as is the particular set of axes, no matter what the order. For instance some physicists use the body centered axes z-x-z, in contrast to the aeronautics z-y-x. At least a dozen different conventions are possible for which series of axes to use. [Kane][Goldstein] The geometry of orientations in Euler's angle coordinates is contorted, a n d varies with choice of initial coordinate axes. There is no Hamilton wrote a quaternion as s+iv~÷jv~+kv~, with i 2 = jq = k 2 = i j k ------1. The multiplication rules given before are consequences of this elegant formulation. Volume 19, Number 3, 1985 reasonable w a y to "multiply" or otherwise combine two rotations. Even converting between r o t a t i o n m a t r i c e s a n d angle coordinates is difficult a n d expensive, involving a r b i t r a r y a s s u m p t i o n s a n d trigonometric functions. In their defense, it must be said t h a t t h e y are h a n d y for solving differential e q u a t i o n s - - w h i c h is how Euler used them. [Euler,1758] 3. I n - b e t w e e n i n g a l t e r n a t i v e s 3.1 S t r a i g h t line i n - b e t w e e n i n g It is not i m m e d i a t e l y obvious how to in-between even two r o t a t i o n keys. W h a t o r i e n t a t i o n s should an o b j e c t assume on its j o u r n e y between them? A n a t u r a l answer is: t a k e the first key as a reference, a n d represent the second b y describing the single r o t a t i o n t h a t t a k e s you to it, according to Euler's theorem. The in-between o r i e n t a t i o n s should be positioned along t h a t r o t a t i o n . If we plot quaternions as points in four-dimensional space, the s t r a i g h t lines between t h e m give o r i e n t a t i o n s i n t e r p o l a t i n g the end points in exactly the above sense. If we plot Euler's angle coordinates instead, the inbetween orientations will t r y to twist a r o u n d three different axes simultaneously. This angle i n t e r p o l a t i o n t r e a t s the three angles of r o t a t i o n at each key o r i e n t a t i o n as a three-dimensional vector whose components are i n t e r p o l a t e d i n d e p e n d e n t l y from key to key. P a r a d o x i c a l l y , we can not r o t a t e simply except a r o u n d the special axes chosen for composition. W e m a y even encounter so-called "gimbal lock", the loss of one degree of r o t a t i o n a l freedom. G i m b a l lock results from trying to ignore the cross p r o d u c t i n t e r a c t i o n of rotations, which can align two of the three axes. Q u a t e r n i o n s are safe from gimbal lock, a n d so have been used for y e a r s to handle s p a c e c r a f t , where it is unacceptable. [Kane][Mitchell] 3.2 H o w q u a t e r n i o n s r o t a t e S t r a i g h t lines between quaternions, however, ignore some of the n a t u r a l g e o m e t r y of r o t a t i o n space. If our i n t e r p o l a t e d points were evenly s p a c e d along a line, the a n i m a t e d r o t a t i o n would speed up in the middle. To see why, we must look a t how a quaternion converts to a r o t a t i o n matrix. We r o t a t e a vector b y a quaternion so: m u l t i p l y it on the right by the quaternion and on the left by the inverse of the quaternion, t r e a t i n g the vector as [0,~]. vr = Rot(v) = q--I 32 q Though it is not obvious, the result will always be a vector, with a zero s c a l a r component. Notice how this guarantees R o t ( v l ) Rot(v2) = Rot(.ul V2) which implies t h a t dot and cross products preserved, e m b e d d e d in t h e q u a t e r n i o n p r o d u c t . are The inverse of a quaternlon is o b t a i n e d by negating its 247 @ S I G G R A P H '85 I IIII vector p a r t and dividing both parts by the magnitude squared. F o r q ~ [s,_~], q-1 1 Itlqll 2 ; while from the 4-D geometry comes'~ Slerp(ql,q2;u) _-- sin ( 1 - - u ) ~ sin0 ql+ [ [ q I [2 = s2"4".IZ'.IZ . Because all effects of magnitude are divided out,, any scalar multiple of a quaternlon gives the same rotation. (This kind of behavior is not unknown in computer graphics; any scalar multiple of a point in homogeneous coordinates gives the same non-homogeneous point.) If the scalar p a r t has value w, and the vector p a r t values x, y, and z, the corresponding matrix can be worked out to be sin u_.~ sin0 q2' where ql'q2 ~ cos 0. The first is simpler for analysis, while the second is more pra.ctical for applications. But a n i m a t i o n s t y p i c a l l y have more t h a n two key poses to connect, and here even our spherical elaboration of simple linear interpolation shows flaws. While orientation changes seamlessly, the direction of r o t a t i o n changes a b r u p t l y . In m a t h e m a t i c a l terms, we w a n t higher order continuity. There are lots of ways to achieve it---off the sphere; u n f o r t u n a t e l y we've learned too much. 3.4 Rotation geometry and topology M = 1--2y2--2z 2 2xy+2wz 2xz--2wy 2xy--2wz 1--2x2--2z 2 2yz++2wx 2xz.+2wy 2yz--2wx 1--2x2--2y 2 when the magnitude w2-l-x2-4-y2.+z2 equals 1. The m a g n i t u d e restriction implies t h a t , plotted in fourdimensional space, these quaternions lie on a sphere of radius one. Deeper investigation shows t h a t such unit quaternions carry the amount of rotation in w, as cos 0/2, while the vector p a r t points along the r o t a t i o n axis with magnitude sin 0/2. The axis of a rotation is t h a t line in space which remains unmoved; but notice t h a t ' s exactly w h a t happens when scalar multiples of .u are r o t a t e d by [s,~]. Because the cross product drops out, multiplication commutes, q - ! meets q, m u t u a l annihilation occurs, a n d the vector emerges unscathed. Summing the m a t r i x diagonal leads to the formula s t a t e d for w. The sum equals 4w2~1, but must also be l + 2 c o s 0. A trig identity, cos 2t9 = 2cos 2 0--1, finishes the demonstation. 3.3 G r e a t a r c in-betweening This sphere of unit quaternions forms a sub-group, S 3, of the quaternion group. F u r t h e r m o r e , the spherical metric of S 3 is the same as the angular metric of SO(3). [Misner] F r o m this it follows t h a t we can rotate without speeding up by interpolating on the sphere. Simply plot the two given orientations on the sphere and draw the great circle arc between them. T h a t arc is the curve where the sphere intersects a plane through the two points and the origin. We sped up before because we were cutting across instead of following the arc; otherwise the p a t h s of rotation are the same. A formula for spherical linear interpolation from ql to q2, with p a r a m e t e r u moving from 0 to 1, can be o b t a i n e d two different ways. F r o m the group structure we find Slerp(ql,q2;u ) = q l ( q ~ l q 2 ) ~ ; No m a t t e r w h a t we do in general quaternion space, the u l t i m a t e effect must be i n t e r p r e t e d via the sphere; so we h a d best work there in spite of the difficulty. It is i m p o r t a n t to grasp this point. The metric structure, hence the intrinsic geometry, of the rotation group SO(3) is t h a t of a sphere. Over small regions, meaning in this case small rotation angles, a sphere looks as if it is flat. But if we go far enough along a "straight line", we end up back where we s t a r t e d . W h a t could be more evident a b o u t rotations? Their very essence is moving in circles. Looking b a c k to the book-turning experiment, the confounding of axes is like traveling on a sphere: if we go in some direction to a quarter of the way a r o u n d the sphere, turn 90 degrees, travel the same distance, then t u r n and travel again, we will arrive b a c k home, coming in at right angles to the direction we h e a d e d out. Even more revealing, we can leave the north pole in a n y direction and end up at the south pole, j u s t as we can r o t a t e 360 degrees around a n y axis and end up oriented the same way. Local geometry does not, however, determine global topology. C o n t r a d i c t o r y though it m a y seem, the geometry curves like a sphere, but the topology says north and south poles are the same! In fact, each pair of opposite points represents the same rotation. The reader m a y preserve s a n i t y through two expedients. One is to see t h a t this, like homogeneous coordinates, is geometry under perspective projection. The second is to restore spherical topology • by including "entanglements". Physically, t a k i n g an object with strings a t t a c h e d and rotating it 360 degrees leaves the strings tangled; y e t - - m o s t o d d - - r o t a t i n g 720 degrees does not. [Misner][Gardner] Accepting the topological o d d i t y is more useful here, but it leaves a minor inconvenience. Namely, when converting an o r i e n t a t i o n in some foreign form, such as a matrix, to a quaternion form, which quaternion should we choose? W h i c h side of the sphere? A n answer t h a t works well is this. Construct a string of quuternions through which to interpolate by choosing t 248 Glenn Davis suggested this formula. SAN FRANCISCO JULY 22-26 Volume 19, Number 3, 1985 each a d d e d quaternion on the side closest to the one before. T h e n small changes in o r i e n t a t i o n will yield small d i s p l a c e m e n t s on the sphere. \ points at o r d e r continuity, p r o b a b l y enough for us. As the curve goes t h r o u g h its end points it is t a n g e n t to its end segments. Line up the segments across a join, m a t c h t h e i r lengths, a n d the curves will piece t o g e t h e r smoothly. If the key o r i e n t a t i o n s are placed at joints, t h e n each short curve moves us from one key to the next, because each piece passes t h r o u g h its ends. Now, a l t h o u g h the two segments a b u t t i n g a curve j u n c t i o n should m a t c h each other, one of the segments can be chosen freely. These choices determine the axis and speed of r o t a t i o n as we pass t h r o u g h the keys. The b u r d e n of choice can be passed to the a n i m a t o r of course, but a u t o m a t i o n is feasible, a n d generally preferable. 4.2 C h o o s i n g j o i n t s e g m e n t s Rep,-eSenti~ a proje~ive pJa~e 3.5 Splines W e are left with the problem of constructing smooth curves on spheres. About a hundred years after quaternions appeared, Isaac Schoenberg published a two p a r t a t t a c k on ballistics a n d a c t u a r i a l problems, using w h a t he called splines. [Schoenberg] N a m e d by a n a l o g y to a d r a f t m a n ' s tool, these are i n t e r p o l a t i n g curves c o n s t r u c t e d from cubic polynomial pieces, with second order c o n t i n u i t y between pieces. Cubic spllnes solve an integral equation which says to minimize the t o t a l "wiggle" of the curve, as measured by the second derivative. These i n t e r p o l a n t s are very popular, and the equation can be a u g m e n t e d with L a g r a n g e multipliers to constrain t h e solution curves t o lie on a sphere [Courant h: Hilbert]; y e t there are problems. F i r s t , the a u g m e n t e d equation is much more difficult a n d expensive t o solve. Second, the curve must a d j u s t everywhere if one of the points changes; t h a t is, we have no local control. 3.6 B~zier curves While Schoenberg invented splines based on numerical analysis, Pierre Bdzier invented a class of curves, now called by his name, based on geometrical ideas. In fact, he showed how to find points on such a curve by drawing lines and splitting t h e m in regular proportions. [Bdzier] This is e x a c t l y w h a t is needed. We a l r e a d y know how to do the e q u i v a l e n t - - d r a w g r e a t arcs and proportions of arcs---on a sphere. A complete solution needs only a little more. 4. Spherical B~zier curves Spherical linear i n t e r p o l a t i o n gives two conflicting arc segments a t a joint, one on each side. Smooth the difference with a n even compromise, aiming for a point h a l f w a y between where the incoming segment would proceed, a n d where the outgoing segment must a r r i v e . t a~ Co~$tt'u~ft~ g pot~t for f a ~ e r ~ t Given successive key quaternions q n - 1 , qn, qn+l i n t e r p r e t t e d as 4-D unit vectors, the c o m p u t a t i o n for a segment point a n after q~ is a n = Biseet(Donble(qn_l,%),q,+]) ' where D o u b l e ( p , q ) ~. 2(p-q)q Bisect(p,q) = -- p ; t tPWq ,,p+qlZ The m a t c h i n g point for the segment before qn should he 4.1 Joining curves Bgzler curves go t h r o u g h only t h e i r first and last defining points, b u t we w a n t to i n t e r p o l a t e all our o r i e n t a t i o n s . The t r i c k is to splice t o g e t h e r s h o r t Bdzier curves in the m a n n e r of splines. Their c r e a t o r showed an easy w a y to do this which g u a r a n t e e s first bn -~ D°uble(an ,qn) For the numerically knowledgeable, this construction approximates the derivative at points of a sampled function by averaging the central differences of the sample sequence. [Dahlquist & Bj6rk] 249 ~~ S I G G R A P H '85 b~÷j = ~2 (o) to ensure a smooth join, regardless of how a n is chosen. a~ % C=lcvl$tingg a $pliging B¢2iCr 3e3mentS lo~¢¢her B ~ z i e r ct)rve polnf recursivel y 5, R e s u l t s 4.3 Evaluating on t h e sphere 5.1 T h e g r a n d s c h e m e Everything is now in h a n d to imitate B~zier's curve technique. Each short curve is defined by four quaternions, qn, an, bn+l, qn+l" Let the p a r a m e t e r u vary from 0 to 1 as the curve departs qn towards a n and arrives at qn+l t a n g e n t to the arc from bn+ 1. Spherically interpolate by proportion u between qn and an, a n and bn+l, bn+ 1 and qn+l, to obtain three new quaternions. Then interpolate between those to get two more; and finally interpolate again, reducing to a single point. A b b r e v i a t i n g Slerp(p,q;u) as (P:q)=, the c o m p u t a t i o n looks like this: qn =p~0) W h a t have we ended up with? An a n i m a t o r sits at a workstation and interactively establishes a sequence of keys for, say, camera orientation. The interpolating algorithm does not depend on the n a t u r e of the interface the a n i m a t o r sees; all needed information is contained in the sequence of keys. P r o b a b l y the orientations will be represented internally as matrices, so a conversion step follows. The matrices are "lifted" to a sequence of neighboring quaternions, qn, on the unit sphere. Each quaternion within the sequence will become the endpoint of two spherical B6zier curves. Between each q u a t e r n i o n pair, qn a n d qn+l, two (p~O):piO))u=p~ 1) an =P t °) (P~') :P i ') )u =p~2) (ptO):plO))u=pt 1) bn+l----p~ O) (p;2):pi2))u=p~3)=qn+u (p t1) :phi) )u ~-p t2) qn +1=P t 0) additional points, a n and bn+l, are added to control motion through the joints. At this point, time becomes a p a r a m e t e r along the composite curve. As the frame n u m b e r increments, the p a r a m e t e r enters and leaves successive curve pieces. Within each piece a local version of the p a r a m e t e r is adjusted to run from 0 to 1. Now the B6zier geometric construction comes into play, producing a n interpolated quaternion, qn+~, from qn, an, bn+l, qn+l, and the local parameter, u. F i n a l l y the mint-fresh interpolated quaternion is t r a n s m u t e d into a matrix, to be used in rotating a list of object vectors for rendering. 5.2 Properties 4.4 Tangents revisited A simple check proves the curve touches q,~ and qn+l at its ends. A rather challenging differentiation shows it is t a n g e n t there to the segments determined by a n and bn+ 1. However, as with B6zi~r's original curve, the m a g n i t u d e of the t a n g e n t is three times t h a t of the segment itself. T h a t is, we are spinning three times faster t h a n spherical interpolation along the arc. F o r t u n a t e l y we can correct the speed by merely t r u n c a t i n g the end segments to one third their original length, so t h a t a n is closer to qn and b,+ 1 closer to qn+l" 250 A look at one special case is revealinG. Suppose all the points to interpolate are spread along a single arc. This means they represent different a m o u n t s of rotation a r o u n d a single axis, in which case quaternion multiplication commutes. Under these special conditions, the formula for the curve sections reduces to qn+u = qn(l-u)8 a : (1-u)~ bn3~'l]-~)u~ qn+l us W h e n this is compared to the s t a n d a r d B6zier polynomial, pn(1--u) 3 + an3(1--u)2u + bn+13(1--u)u ~ + q,,+lu 3 , it is a p p a r e n t t h a t addition a n d multiplication SAN FRANCISCO JULY 2 2 " 2 6 have become m u l t i p l i c a t i o n a n d exponentiation. Of course, when the points are not on one arc, c o m m u t a t i v i t y fails, so the formula looks much messier. In the interesting r e s t r i c t e d case when the points are s p a c e d evenly a n d consecutively a r o u n d an arc, the resulting a n i m a t i o n behaves exactly as we would hope: we get smooth,, c o n s t a n t speed r o t a t i o n a r o u n d the a p p r o p r i a t e axis. Notice t h a t we can choose a n y axis for this r o t a t i o n . This is clearly preferable to i n t e r p o l a t i o n with Euler's angles, where the coordinate axes are special. A more subtle p r o p e r t y of all quaternion i n t e r p o l a t i o n is t h a t the motion is i n d e p e n d e n t of coordinate axes. So, for example, if we design a move, t h e n r o t a t e the coordinate system a r b i t r a r i l y , the g e o m e t r y of the motion will not change. Euler i n t e r p o l a n t s , u n f o r t u n a t e l y , will do wildly different things. 5.3 A p p l i c a b i l i t y R o t a t i o n s in space are significantly more c o m p l i c a t e d t h a n r o t a t i o n s in a plane. It is easy to deal with the latter, since only one parameter is involved. Q u a t e r n i o n s are o u t of place in a plane. Joint control in robotics simulations has its own highly specialized body of techniques; and though quaternions have shown up in the l i t e r a t u r e , t h e y seem less useful in t h a t context. [Brady] [Taylor] However, B.K.P. Horn has used a tessellation of the quaternion unit sphere to identify the o r i e n t a t i o n of an object from its extended G a u s s i a n image; a good reference is [Brou]. Non-rigid motion obviously needs to be h a n d l e d specially. But for moving a c a m e r a eye-point, a n d for m a n y kinds of o b j e c t motion, quaternion i n t e r p o l a t i o n has strong advantages. 5.4 Comparisons and complaints Cost a d v a n t a g e s are difficult to e s t i m a t e . Converting a m a t r i x to a quaternion requires only one square root a n d three divides plus some adds, at worst. Converting b a c k requires g multiplies and 15 adds. While the conversions d o n ' t use trigonometric functions, the arc proportioning does. For comparison, angle i n t e r p o l a t i o n requires several trigonometric functions as well as quite a few multiplies a n d adds to create each i n t e r p o l a t e d m a t r i x . My experience is t h a t the B6zier scheme is c o m f o r t a b l y fast enough for design work, which is the only time speed has m a t t e r e d . (If, for some application, more speed is essential, non-spherical quaternion splines will u n d o u b t e d l y be faster t h a n angle i n t e r p o l a t i o n , while still free of axis bias a n d glmbal lock.) These i n t e r p o l a n t s are not perfect, of course. Like all i n t e r p o l a n t s , t h e y can develop kinks between the i n t e r p o l a t e d points. There are simple algorithms for adding new sequence points to o r d i n a r y splines w i t h o u t altering the original curve [Boehm]; t h e y do not work for this i n t e r p o l a n t . A n d if these curves can be shown to s a t i s f y some v a r i a t i o n a l principal, it will be b y chance. It is useful to do this, because a n y solution to an integral equation like t h a t for splines a d m i t s subdivision [Lane et all; m i n i m u m c u r v a t u r e between Volume 19, Number 3, 1985 end points implies minimum c u r v a t u r e between i n t e r m e d i a t e points as well. Along these lines, Gabriel and K a j i y a , m o t i v a t e d b y quaternions, have been developing a technique to find splines on a r b i t r a r y R e i m a n n i a n manifolds b y solving differential equations. [Gabriel & K a j i y a ] 6. Questions F u t u r e research could answer some interesting p r a c t i c a l questions. W h a t are these spherical B6zier curves? Is there some a b s t r a c t c h a r a c t e r i z a t i o n of them? Or is there some related interpolant that is wellcharacterized? In light of the success of the geometric a d a p t a t i o n a p p r o a c h , it a p p e a r s reasonable to a p p l y the idea to B-splines, which also have a known geometric e v a l u a t i o n technique. [Cordon & Riesenfeld] How do spherical B-splines behave? Is it possible to add new points to a sequence for either kind of curve without disturbing it? How? C a n B-.splines be m a d e to i n t e r p o l a t e , not j u s t a p p r o x i m a t e , with a simple a d j u s t m e n t of control points? Is there a w a y to construct a curve p a r a m e t e r i z e d by arc length? This would be very useful. W h a t is the best w a y to allow varying intervals between sequence points in p a r a m e t e r space? A b a n d o n i n g the unit sphere, one could work with the four-dimensional Euclidean space of a r b i t r a r y quaternlons. How do s t a n d a r d i n t e r p o l a t i o n m e t h o d s applied there behave when m a p p e d back to matrices? Note t h a t we now have little guidance in picking the inverse image for a matrix, and t h a t cusp-free 1~4 p a t h s do not always project to cusp-free S 3 paths. However these questions are answered, quaternion spline i n t e r p o l a n t s a l r e a d y offer a well-behaved i m p r o v e m e n t over t r a d i t i o n a l techniques. T h e y are simple to use, simple to implement, robust, efficient, consistent, a n d flexible. More research would m a k e them even more so. 7. Acknowledgments This work was begun for an a n i m a t i o n system I designed and i m p l e m e n t e d a t Singer-Link. Several people there deserve t h a n k s , but I especially t h a n k Glenn Davis, who befriended me with his good humor and m a t h e m a t i c a l efforts as I struggled through trying times. I prefer not to invent the wheel if I can find the plans; so I p e s t e r e d Don Venhaus, Brian Bar~ky, Tom Duff, Lance Williams, and Jim Blinn, whom I t h a n k for their time, their comments, a n d t h e i r assurances t h a t t h e y h a d not seen this p a r t i c u l a r wheel roll p a s t PDI, Berkeley, Lucasfilm, NYIT, or JPL. T h a n k s to everyone a t Pacific D a t a Images for the interest a n d e n c o u r a g e m e n t t h a t got me s t a r t e d . The folks at Ridge C o m p u t e r were generous above and beyond the call of customer s u p p o r t in letting me use their Imagen t y p e s e t t i n g system to produce this paper. Lastly, I t h a n k Nori Hail for commenting on numerous drafts, and more. 251 Q References 1. BEZIER, P.E., Numerical Control - - Mathematics and Applications, John Wiley and Sons, London (1072). 2. BOEHM, WOLFGANG, "Inserting new knots into ]3spline curves," Computer-Aided Design 12(4)pp. 199-201 (July 1980). 3. BRADY,MICHAEL, "Trajectory Planning," in Robot Motion: Planning and Control, ed. Michael Brady, John M. Hollerbach, Timothy L. Hohnson, Tomas Lozano-Perez and Matthew T. Mason,The M.IT Press (1982). 4. BROU, PHILIPPE, "Using the Gaussian Image to Find the Orientation of Objects," The International Journal off Robotics Research 3(4) pp. 89-125 (Winter 1984). 5. CAYLEY, ARTHUR, "On certain results relating to quaternions," Philosophical Magazine xxvi pp. 141-145 (February 1845). 6. COURANT, R. AND HILBERT, D., Methods o] Mathematical Physics, Volume I, Interscience Publishers, Inc., New York (1953). 7. DAttLQUIST, GERMUND AND BJiSRCK, A_KE, Numerical Methods, Prentice-Hall, Inc., Englewood CLiffs, N.J. (1974). Translated by Ned Anderson. 8. EULER, LEONHARD, "Decouverte d'un nouveau principe de m~canique (1752)," pp. 81-108 in Opera omnia, Ser. secunda, v. 5, Orell Ffisli Turici, Lausannae (1957). 9. EULER, LEONItARD, "Du mouvement de rotation des corps solides autour d'un axe variable (1758)," in Opera omnia, Ser. seeunda, v. 8, Orell F/isli Turici, Lausannae 0" 10. GABRIEL, STEVEN A. AND KAJIYA, JAMES T., "Spline Interpolation in Curved Manifolds," , (1985). Submitted 11. GARDNER, MARTIN, New Mathematical Diversions from Scientific American, Fireside, St. Louis, Missouri (1971). Chapter 2 12. GOLDSTEIN,HERBERT, Classical Mechanics, second edition, Addison-Wesley Publishing Company, Inc., Reading, Mass. (1980). Chapter 4 and Appendix B. 13. GORDON, WILLIAM J. AND RIESENFELD, RICHARD F., "Bernstein-B~zier methods for the computeraided design of free-form curves and surfaces," J. A C M 21(2) pp. 293-310 (April 1974). 14. GORDON, WILLIAM J. AND RIESENFELD, RICHARD F., "B-spline curves and surfaces," in Compute~ Aided Geometric Design, ed. Robert E. Barnhill and Richard F. Riesenfeld,Academic Press, New York (1974). 15. HAMILTON, SIR WILLIAM ROWAN, "On quaternions; or on a new system of imaginaries in algebra," Philosophical Magazine xxv pp. 10-13 (July 1844). 16. HERSTEIN, I.N., Topics in Algebra, second edition, John Wiley and Sons, Inc., New York (1975). 252 S I G G R A P H '85 17. KANE, THOMAS R., LIKINS, PETER W. AND LEVINSON, DAVID A., Spacecraft Dynamics, McGraw-Hill, Inc. (1083). 18. LANE, JEFFREY M., CARPENTER, LOREN C., WHITTED, TURNER, .AND BLINN~ JAMES F., "Scan line methods for displaying parametrically defined surfaces," Comm. A C M 2a(1)pp. 23-34 (January 1980). 19. MACLANE, SAUNDERS AND BIRKHOFF, GARRETT, Algebra, second edition, Macmillan Publishing Co., Inc., New York (1979). 20. MISNER, CHARLES W., THORNE, KIP S., AND WHEELER, JOHN ARCHIBALD, Gravitation, W.H. Freeman and Company, San Francisco (1973). Chapter 41 - - Spinors. 21. MITCHELL, E.E.L. AND ROGERS, A.E., "Quaternion Parameters in the Simulation of a Spinning Rigid Body," in Simulation The Dynamic Modeling of Ideas and Systems with Computers, ed. John McLeod, P.E., (1968). 22. NEWMAN, WH LIAM M. AND SPROULL, ROBERT F., Principles of Interactive Computer Graphics, second edition, McGraw-Hill, Inc., New York (1979). Chapter 21 - - Curves and surfaces. 23. PICKERT, G. AND STEINER; H.-G., "Chapter 8 - Complex numbers and quaternions," in Fundamentals off Mathematics, Volume I Foundations of Mathematics: The Real Number System and Algebra, ed. H. Behnke, F. Bachmann, K. Fladt, and W. Sfiss, (1983). Translated by S.H. Gould. 24. SCHMEIDLER,W. AND DREETZ, W., "Chapter 11 - Functional analysis," in Fundamentals of Mathematics, Volume III - - Analysis, ed. H. Behnke, F. Bachmann, K. Fladt, and W. Sfiss,MlT Press, Cambridge, Mass. (1983). Translated by S.H. Gould. 25. SCHOENBERG, I.J., "Contributions to the problem of approximation of equidistant data by analytic functions," Quart. AppL Math. 4 pp. 45-99 and 112-141 (1946). 2B. SMITH, ALVY RAY, "Spline tutorial notes," Technical Memo No. 77, Computer Graphics Project, Lucasfilm Ltd. (May 1983). 27. SOSS, W., GERICKE, H., AND BERGER, K.H., "Chapter 14 - - Differential geometry of curves and surfaces," in Fundamentals of Mathematics, Volume II -- Geometry, ed. H.'Behnke, F. Bachmann, K. Fladt, and W. S/iss,MIT Press (1983). Translated by S.H. Gould. 28. TAYLOR, RUSSELL H., "Planning and Execution of Straight Line Manipulator Trajectories," in Robot Motion: Planning and Control, ed. Michael Brady, John M. Hollerbach, Timothy L. Hohnson, Tomas Lozano-Perez and Matthew T. Mason,The MIT Press (1982). SAN FRANCISCO JULY 22-26 Volume 19, Number 3,1985 L8 Euler angles to quaternion Appendix I--Conversions L1 Quatcrnion to matrix Using the restriction t h a t w2-t-x2-Fy2-t-z2= 1 for u quaternion q = [w,(x,y,z)], the formula for the corresponding matrix is M [ = 1--2y2--2z2 2xy+2wz 2xy--2wz 2xz+2wy 1--2x2--2z2 2xz--2wy ] 2yz+2wx ]. 2yz--2wx 1--2x2--2y 2] There are twelve possible axis conventions for Euler angles. The one used here is roll, pitch, and yaw, as used in aeronautics. A general rotation is obtained by first yawing around the z axis by a n angle of ¢, t h e n pitching around the y axis by 0, and finally rolling around the x axis by ¢. Using the way quaternion components describe a rotation, we first o b t a i n a quaternion for each simple rotation. qrott = [cos@,(sin-~,O,O)] If the quaternion does not have unit m~gnitude, a n additional 4 multiplies and divides, 3 adds, and a square root will normalize it. (For the matrix conversion, the square root can be avoided in favor of divides if desirable.) Now we can obtain the operation count for creating the matrix. Most terms of the entries are a product of two factors, one of which is doubled. So we proceed as follows. First double x, y, and z, and form their products with w, x, y, and z. That will take 3 adds and 9 multiplies. Then form the sum for each of the 9 entries using 1 add each, plus a n extra add for each of the 3 diagonal elements, for a total of 12 adds. Thus 9 multiplies a n d 15 adds suffice to convert a unit quaternion to a matrix. L2 Matrix to quaternion An efficient way to determine quaternion components w, x, y, z from a matrix is to use linear combinations of the entries Mra,~. Notice that the diagonal entries are formed from the squares of the quaternion components, while off-diagonal entries are the sum of a symmetric and a skew-symmetric part. Thus linear combinations of the diagonal entries will isolate squares of components; sums and differences of opposite offdiagonal entries will isolate products among x, y, a n d z and products with w. Using off-diagonals risks dividing by a component that may be zero, or within ¢ (the machine precision) of zero. However we can avoid t h a t pitfall, and easily compute all components as follows. w 2 ~ 1/4 (1 + M l l + M22 + M33) Z = (MI2 -- FALSE w=O X2 -- --1/2 (M22 + 2~'/33) Multlplying these together in the right order gives the desired quaternion q -- qyaw qpitch qroll, with components = cos --¢ eos --0 cos --¢ + sin -C-sln--0sin = si.- 2 y = cos 2 2 2 2 2 cos --0cos --¢ -- cos -C-sin--0sin--¢ 2 2 sin £¢os 2 2 2 2 + sin -C-cos 2 2 2 2 2 sin--¢ 2 z = cos -.f-cos 2-sin-¢ - sin -C-sin £eos £ 2 2 2 2 2 2 1.4 Euler angles to matrix Combining the results of the previous two conversions gives M = cos 8cos¢ cos 0sin¢ --sin 0 / sin ~bsin8cosC--cos~sin ¢ sin ~bsin0sin¢+cos ¢cos 8 cos 0sin~], cos Csin 0cos¢q.-sinCsin ¢ cos ~sin 0sin¢--sln ¢cos ¢ cos 0cos ~J 1.5 Matrix to Euler angles x2>c? M21 ) / 4t.V q~aw = [c°s 2~,(O,O,sin 2~)] where ~b, 0, and ¢ are the angles of roll, pitch, and yaw, respe ctively. w2>e? TRUE qpitch '~ [cos~,(O,sin~,O)] TRUE FALSE !,'2 = 1/2 (1 -- M ~ ) z ~ M m / 2:~ y2>c? TRUE z ~ M~3 / 2y FALSE z=l No more t h a n one square root, three divides, and a few adds and binary scales are required for any conversion. While converting a matrix to a unit quaternion only involves the sign ambiguity of square roots, converting to Euler angles involves inverse trigonometric functions, as we con only directly determine the sin's and cos's of the angles. Some convention, such as principle angles, must be adopted. However interpolation paths will vary greatly, depending on choice of angles. Setting t h a t problem aside, here's a way to extract the sin's and cos's. Looking at the previous equation, sin0 can be read off directly as --M13. Use the trigonometric identity cos 0 = ::t=%~"~"~0 to compute cos @to within a sign, which is the best we can do. Assuming cos0 is not zero, o b t a i n the sin's and cos's of the other angles from 253 SIGG sin 0 = --Mi3 cosO = sine = M23/cos cos~b = M a 3 / c o s / ~ s i n e = Ml~ / c o s ~os¢ = M l l / c o s e If cos t? is zero, t h e n we must avoid dividing by zero. It also becomes impossible to distinguish roll from yaw. A d o p t i n g the convention t h a t the yaw angle ¢ is 0 allows sin ¢ = --M32 cos ¢ = M22 sine = cos¢ = 0 1 F r o m these values a two a r g u m e n t t a n -1 will give angles between --Tr a n d +Tr, or 0 and 275 or some other conventional range; take your pick. (For a faster conversion, j u s t compute, say, sin -1 a n d check the sign of the cosine term with respect to cos6~.) Because of the u n c e r t a i n t i e s of square roots, inverse trigonometric functions, and yaw-roll separation, matrix to Euler angle conversion is inherently very ill-defined. 1.6 Quaternion to Euler angles Use the most straight-forward approach: convert the q u a t e r n i o n to a matrix, t h e n the matrix to Euler angles. Of course it is unnecessary to compute matrix elements t h a t are never used. This conversion is also u n a v o i d a b l y ill-defined, as quaternions contain no more information a b o u t angles t h a n matrices do. 254 RAPH '85