quaternion: add slerp and nlerp

This commit is contained in:
Danny Robson 2019-02-07 15:45:08 +11:00
parent cbf4a716ac
commit 8706c39ab7
2 changed files with 57 additions and 0 deletions

View File

@ -104,6 +104,49 @@ quaternion<T>::operator- () const noexcept
} }
///////////////////////////////////////////////////////////////////////////////
template <typename T>
quaternion<T>
cruft::slerp (quaternion<T> a, quaternion<T> b, T t)
{
// calc cosine theta
T cosine = dot (a, b);
// adjust signs (if necessary)
if (cosine < 0) {
cosine = -cosine;
b = -b;
}
// Calculate coefficients
T p, q;
if (T(0.9995) > cosine) {
T omega = std::acos (cosine); // extract theta from dot product's cos theta
T sine = std::sin (omega);
p = std::sin ((1 - t) * omega) / sine;
q = std::sin ( t * omega) / sine;
} else {
// Very close, do linear interp (because it's faster)
p = 1 - t;
q = t;
}
return a * p + b * q;
}
//-----------------------------------------------------------------------------
template <typename T>
quaternion<T>
cruft::nlerp (cruft::quaternion<T> a, cruft::quaternion<T> b, T t)
{
return cruft::normalised (
cruft::mix (a, b, t)
);
}
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
template <typename T> template <typename T>
quaternion<T> quaternion<T>
@ -311,6 +354,8 @@ namespace cruft::debug {
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
#define INSTANTIATE(T) \ #define INSTANTIATE(T) \
template cruft::vector3<T> cruft::rotate (cruft::vector3<T>, cruft::quaternion<T>); \ template cruft::vector3<T> cruft::rotate (cruft::vector3<T>, cruft::quaternion<T>); \
template quaternion<T> cruft::slerp (quaternion<T>, quaternion<T>, T); \
template quaternion<T> cruft::nlerp (quaternion<T>, quaternion<T>, T); \
template quaternion<T> cruft::conjugate (quaternion<T>); \ template quaternion<T> cruft::conjugate (quaternion<T>); \
template quaternion<T> cruft::operator* (quaternion<T>, quaternion<T>); \ template quaternion<T> cruft::operator* (quaternion<T>, quaternion<T>); \
template quaternion<T>& cruft::operator*= (quaternion<T>&, quaternion<T>); \ template quaternion<T>& cruft::operator*= (quaternion<T>&, quaternion<T>); \

View File

@ -141,6 +141,18 @@ namespace cruft {
} }
///////////////////////////////////////////////////////////////////////////
template <typename T>
quaternion<T>
slerp (quaternion<T> a, quaternion<T> b, T t);
//-------------------------------------------------------------------------
template <typename T>
quaternion<T>
nlerp (quaternion<T> a, quaternion<T> b, T t);
/////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////
template <typename T> template <typename T>
quaternion<T> quaternion<T>