quaternion: add slerp and nlerp
This commit is contained in:
parent
cbf4a716ac
commit
8706c39ab7
@ -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>
|
||||
quaternion<T>
|
||||
@ -311,6 +354,8 @@ namespace cruft::debug {
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
#define INSTANTIATE(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::operator* (quaternion<T>, quaternion<T>); \
|
||||
template quaternion<T>& cruft::operator*= (quaternion<T>&, quaternion<T>); \
|
||||
|
@ -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>
|
||||
quaternion<T>
|
||||
|
Loading…
Reference in New Issue
Block a user