Cortex  10.0.0-a4
QuatAlgo.h
Go to the documentation of this file.
1 //
3 // Copyright (c) 2009-2011, Image Engine Design Inc. All rights reserved.
4 //
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are
7 // met:
8 //
9 // * Redistributions of source code must retain the above copyright
10 // notice, this list of conditions and the following disclaimer.
11 //
12 // * Redistributions in binary form must reproduce the above copyright
13 // notice, this list of conditions and the following disclaimer in the
14 // documentation and/or other materials provided with the distribution.
15 //
16 // * Neither the name of Image Engine Design nor the names of any
17 // other contributors to this software may be used to endorse or
18 // promote products derived from this software without specific prior
19 // written permission.
20 //
21 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
22 // IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
23 // THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
25 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
26 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
27 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
28 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
29 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
30 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
31 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 //
34 
38 
39 #ifndef IECORE_QUATALGO_H
40 #define IECORE_QUATALGO_H
41 
42 #include "OpenEXR/ImathQuat.h"
43 
44 namespace IECore
45 {
46 
49 template <class T>
50 inline T
52 {
53  if (x * x < Imath::limits<T>::epsilon())
54  return T (1);
55  else
56  return Imath::Math<T>::sin (x) / x;
57 }
58 
61 template<class T>
62 T
63 angle4D (const Imath::Quat<T> &q1, const Imath::Quat<T> &q2)
64 {
65  //
66  // Compute the angle between two quaternions,
67  // interpreting the quaternions as 4D vectors.
68  //
69 
70  Imath::Quat<T> d = q1 - q2;
71  T lengthD = Imath::Math<T>::sqrt (d ^ d);
72 
73  Imath::Quat<T> s = q1 + q2;
74  T lengthS = Imath::Math<T>::sqrt (s ^ s);
75 
76  return 2 * Imath::Math<T>::atan2 (lengthD, lengthS);
77 }
78 
82 template<class T>
83 Imath::Quat<T>
84 slerp(const Imath::Quat<T> &q1,const Imath::Quat<T> &q2, T t)
85 {
86 
87  //
88  // Spherical linear interpolation.
89  // Assumes q1 and q2 are normalized and that q1 != -q2.
90  //
91  // This method does *not* interpolate along the shortest
92  // arc between q1 and q2. If you desire interpolation
93  // along the shortest arc, and q1^q2 is negative, then
94  // consider flipping the second quaternion explicitly.
95  //
96  // The implementation of squad() depends on a slerp()
97  // that interpolates as is, without the automatic
98  // flipping.
99  //
100  // Don Hatch explains the method we use here on his
101  // web page, The Right Way to Calculate Stuff, at
102  // http://www.plunk.org/~hatch/rightway.php
103  //
104 
105  T a = IECore::angle4D (q1, q2);
106  T s = 1 - t;
107 
108  Imath::Quat<T> q = IECore::sinx_over_x (s * a) / IECore::sinx_over_x (a) * s * q1 +
109  IECore::sinx_over_x (t * a) / IECore::sinx_over_x (a) * t * q2;
110 
111  return q.normalized();
112 }
113 
117 template<class T>
118 Imath::Quat<T>
119 slerpShortestArc (const Imath::Quat<T> &q1, const Imath::Quat<T> &q2, T t)
120 {
121  //
122  // Spherical linear interpolation along the shortest
123  // arc from q1 to either q2 or -q2, whichever is closer.
124  // Assumes q1 and q2 are unit quaternions.
125  //
126 
127  if ((q1 ^ q2) >= 0)
128  return IECore::slerp (q1, q2, t);
129  else
130  return IECore::slerp (q1, -q2, t);
131 }
132 
133 } // namespace IECore
134 
135 #endif // IECORE_QUATALGO_H
T sinx_over_x(T x)
Definition: QuatAlgo.h:51
T angle4D(const Imath::Quat< T > &q1, const Imath::Quat< T > &q2)
Definition: QuatAlgo.h:63
Imath::Quat< T > slerp(const Imath::Quat< T > &q1, const Imath::Quat< T > &q2, T t)
Definition: QuatAlgo.h:84
Imath::Quat< T > slerpShortestArc(const Imath::Quat< T > &q1, const Imath::Quat< T > &q2, T t)
Definition: QuatAlgo.h:119
This namespace contains all components of the core library.
Definition: AddSmoothSkinningInfluencesOp.h:43