Add first derivative calculation utilities for Curve
This commit is contained in:
@@ -84,21 +84,21 @@ namespace AnimationUtils
|
||||
}
|
||||
|
||||
template<class T>
|
||||
FORCE_INLINE static void Interpolate(const T& a, const T& b, float alpha, T& result)
|
||||
FORCE_INLINE static void Interpolate(const T& a, const T& b, float t, T& result)
|
||||
{
|
||||
result = (T)(a + alpha * (b - a));
|
||||
result = (T)(a + t * (b - a));
|
||||
}
|
||||
|
||||
template<>
|
||||
FORCE_INLINE void Interpolate<Vector3>(const Vector3& a, const Vector3& b, float alpha, Vector3& result)
|
||||
FORCE_INLINE void Interpolate<Vector3>(const Vector3& a, const Vector3& b, float t, Vector3& result)
|
||||
{
|
||||
Vector3::Lerp(a, b, alpha, result);
|
||||
Vector3::Lerp(a, b, t, result);
|
||||
}
|
||||
|
||||
template<>
|
||||
FORCE_INLINE void Interpolate<Quaternion>(const Quaternion& a, const Quaternion& b, float alpha, Quaternion& result)
|
||||
FORCE_INLINE void Interpolate<Quaternion>(const Quaternion& a, const Quaternion& b, float t, Quaternion& result)
|
||||
{
|
||||
Quaternion::Slerp(a, b, alpha, result);
|
||||
Quaternion::Slerp(a, b, t, result);
|
||||
}
|
||||
|
||||
static void WrapTime(float& time, float start, float end, bool loop)
|
||||
@@ -134,98 +134,111 @@ namespace AnimationUtils
|
||||
/// Evaluates a cubic Hermite curve at a specific point.
|
||||
/// </summary>
|
||||
/// <param name="t">The time parameter that at which to evaluate the curve, in range [0, 1].</param>
|
||||
/// <param name="pointA">The starting point (at t=0).</param>
|
||||
/// <param name="pointB">The ending point (at t=1).</param>
|
||||
/// <param name="tangentA">The starting tangent (at t=0).</param>
|
||||
/// <param name="tangentB">The ending tangent (at t = 1).</param>
|
||||
/// <param name="p0">The starting point (at t=0).</param>
|
||||
/// <param name="p1">The ending point (at t=1).</param>
|
||||
/// <param name="t0">The starting tangent (at t=0).</param>
|
||||
/// <param name="t1">The ending tangent (at t = 1).</param>
|
||||
/// <param name="result">The evaluated value.</param>
|
||||
template<class T>
|
||||
static void CubicHermite(const float t, const T& pointA, const T& pointB, const T& tangentA, const T& tangentB, T* result)
|
||||
static void CubicHermite(const T& p0, const T& p1, const T& t0, const T& t1, float t, T& result)
|
||||
{
|
||||
const float t2 = t * t;
|
||||
const float t3 = t2 * t;
|
||||
|
||||
float a = 2 * t3 - 3 * t2 + 1;
|
||||
float b = t3 - 2 * t2 + t;
|
||||
float c = -2 * t3 + 3 * t2;
|
||||
float d = t3 - t2;
|
||||
|
||||
*result = a * pointA + b * tangentA + c * pointB + d * tangentB;
|
||||
const float tt = t * t;
|
||||
const float ttt = tt * t;
|
||||
result = (2 * ttt - 3 * tt + 1) * p0 + (ttt - 2 * tt + t) * t0 + (-2 * ttt + 3 * tt) * p1 + (ttt - tt) * t1;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Evaluates the first derivative of a cubic Hermite curve at a specific point.
|
||||
/// </summary>
|
||||
/// <param name="t">The time parameter that at which to evaluate the curve, in range [0, 1].</param>
|
||||
/// <param name="pointA">The starting point (at t=0).</param>
|
||||
/// <param name="pointB">The ending point (at t=1).</param>
|
||||
/// <param name="tangentA">The starting tangent (at t=0).</param>
|
||||
/// <param name="tangentB">The ending tangent (at t = 1).</param>
|
||||
/// <param name="p0">The starting point (at t=0).</param>
|
||||
/// <param name="p1">The ending point (at t=1).</param>
|
||||
/// <param name="t0">The starting tangent (at t=0).</param>
|
||||
/// <param name="t1">The ending tangent (at t=1).</param>
|
||||
/// <param name="result">The evaluated value.</param>
|
||||
template<class T>
|
||||
static void CubicHermiteD1(const float t, const T& pointA, const T& pointB, const T& tangentA, const T& tangentB, T* result)
|
||||
static void CubicHermiteFirstDerivative(const T& p0, const T& p1, const T& t0, const T& t1, float t, T& result)
|
||||
{
|
||||
const float t2 = t * t;
|
||||
|
||||
float a = 6 * t2 - 6 * t;
|
||||
float b = 3 * t2 - 4 * t + 1;
|
||||
float c = -6 * t2 + 6 * t;
|
||||
float d = 3 * t2 - 2 * t;
|
||||
|
||||
*result = a * pointA + b * tangentA + c * pointB + d * tangentB;
|
||||
const float tt = t * t;
|
||||
result = (6 * tt - 6 * t) * p0 + (3 * tt - 4 * t + 1) * t0 + (-6 * tt + 6 * t) * p1 + (3 * tt - 2 * t) * t1;
|
||||
}
|
||||
|
||||
template<class T>
|
||||
static void Bezier(const T& p0, const T& p1, const T& p2, const T& p3, float alpha, T& result)
|
||||
static void Bezier(const T& p0, const T& p1, const T& p2, const T& p3, float t, T& result)
|
||||
{
|
||||
T p01, p12, p23, p012, p123;
|
||||
Interpolate(p0, p1, alpha, p01);
|
||||
Interpolate(p1, p2, alpha, p12);
|
||||
Interpolate(p2, p3, alpha, p23);
|
||||
Interpolate(p01, p12, alpha, p012);
|
||||
Interpolate(p12, p23, alpha, p123);
|
||||
Interpolate(p012, p123, alpha, result);
|
||||
Interpolate(p0, p1, t, p01);
|
||||
Interpolate(p1, p2, t, p12);
|
||||
Interpolate(p2, p3, t, p23);
|
||||
Interpolate(p01, p12, t, p012);
|
||||
Interpolate(p12, p23, t, p123);
|
||||
Interpolate(p012, p123, t, result);
|
||||
}
|
||||
|
||||
template<>
|
||||
void Bezier<Vector2>(const Vector2& p0, const Vector2& p1, const Vector2& p2, const Vector2& p3, float alpha, Vector2& result)
|
||||
void Bezier<Vector2>(const Vector2& p0, const Vector2& p1, const Vector2& p2, const Vector2& p3, float t, Vector2& result)
|
||||
{
|
||||
const float u = 1.0f - alpha;
|
||||
const float tt = alpha * alpha;
|
||||
const float u = 1.0f - t;
|
||||
const float tt = t * t;
|
||||
const float uu = u * u;
|
||||
const float uuu = uu * u;
|
||||
const float ttt = tt * alpha;
|
||||
result = uuu * p0 + 3 * uu * alpha * p1 + 3 * u * tt * p2 + ttt * p3;
|
||||
const float ttt = tt * t;
|
||||
result = uuu * p0 + 3 * uu * t * p1 + 3 * u * tt * p2 + ttt * p3;
|
||||
}
|
||||
|
||||
template<>
|
||||
void Bezier<Vector3>(const Vector3& p0, const Vector3& p1, const Vector3& p2, const Vector3& p3, float alpha, Vector3& result)
|
||||
void Bezier<Vector3>(const Vector3& p0, const Vector3& p1, const Vector3& p2, const Vector3& p3, float t, Vector3& result)
|
||||
{
|
||||
const float u = 1.0f - alpha;
|
||||
const float tt = alpha * alpha;
|
||||
const float u = 1.0f - t;
|
||||
const float tt = t * t;
|
||||
const float uu = u * u;
|
||||
const float uuu = uu * u;
|
||||
const float ttt = tt * alpha;
|
||||
result = uuu * p0 + 3 * uu * alpha * p1 + 3 * u * tt * p2 + ttt * p3;
|
||||
const float ttt = tt * t;
|
||||
result = uuu * p0 + 3 * uu * t * p1 + 3 * u * tt * p2 + ttt * p3;
|
||||
}
|
||||
|
||||
template<>
|
||||
void Bezier<Quaternion>(const Quaternion& p0, const Quaternion& p1, const Quaternion& p2, const Quaternion& p3, float alpha, Quaternion& result)
|
||||
void Bezier<Quaternion>(const Quaternion& p0, const Quaternion& p1, const Quaternion& p2, const Quaternion& p3, float t, Quaternion& result)
|
||||
{
|
||||
Quaternion p01, p12, p23, p012, p123;
|
||||
Quaternion::Slerp(p0, p1, alpha, p01);
|
||||
Quaternion::Slerp(p1, p2, alpha, p12);
|
||||
Quaternion::Slerp(p2, p3, alpha, p23);
|
||||
Quaternion::Slerp(p01, p12, alpha, p012);
|
||||
Quaternion::Slerp(p12, p23, alpha, p123);
|
||||
Quaternion::Slerp(p012, p123, alpha, result);
|
||||
Quaternion::Slerp(p0, p1, t, p01);
|
||||
Quaternion::Slerp(p1, p2, t, p12);
|
||||
Quaternion::Slerp(p2, p3, t, p23);
|
||||
Quaternion::Slerp(p01, p12, t, p012);
|
||||
Quaternion::Slerp(p12, p23, t, p123);
|
||||
Quaternion::Slerp(p012, p123, t, result);
|
||||
}
|
||||
|
||||
template<>
|
||||
void Bezier<Transform>(const Transform& p0, const Transform& p1, const Transform& p2, const Transform& p3, float alpha, Transform& result)
|
||||
void Bezier<Transform>(const Transform& p0, const Transform& p1, const Transform& p2, const Transform& p3, float t, Transform& result)
|
||||
{
|
||||
Bezier<Vector3>(p0.Translation, p1.Translation, p2.Translation, p3.Translation, alpha, result.Translation);
|
||||
Bezier<Quaternion>(p0.Orientation, p1.Orientation, p2.Orientation, p3.Orientation, alpha, result.Orientation);
|
||||
Bezier<Vector3>(p0.Scale, p1.Scale, p2.Scale, p3.Scale, alpha, result.Scale);
|
||||
Bezier<Vector3>(p0.Translation, p1.Translation, p2.Translation, p3.Translation, t, result.Translation);
|
||||
Bezier<Quaternion>(p0.Orientation, p1.Orientation, p2.Orientation, p3.Orientation, t, result.Orientation);
|
||||
Bezier<Vector3>(p0.Scale, p1.Scale, p2.Scale, p3.Scale, t, result.Scale);
|
||||
}
|
||||
|
||||
template<class T>
|
||||
static void BezierFirstDerivative(const T& p0, const T& p1, const T& p2, const T& p3, float t, T& result)
|
||||
{
|
||||
const float u = 1.0f - t;
|
||||
const float tt = t * t;
|
||||
const float uu = u * u;
|
||||
result = 3.0f * uu * (p1 - p0) + 6.0f * u * t * (p2 - p1) + 3.0f * tt * (p3 - p2);
|
||||
}
|
||||
|
||||
template<>
|
||||
static void BezierFirstDerivative<Quaternion>(const Quaternion& p0, const Quaternion& p1, const Quaternion& p2, const Quaternion& p3, float t, Quaternion& result)
|
||||
{
|
||||
Vector3 euler;
|
||||
BezierFirstDerivative<Vector3>(p0.GetEuler(), p1.GetEuler(), p2.GetEuler(), p3.GetEuler(), t, euler);
|
||||
result = Quaternion::Euler(euler);
|
||||
}
|
||||
|
||||
template<>
|
||||
static void BezierFirstDerivative<Transform>(const Transform& p0, const Transform& p1, const Transform& p2, const Transform& p3, float t, Transform& result)
|
||||
{
|
||||
BezierFirstDerivative<Vector3>(p0.Translation, p1.Translation, p2.Translation, p3.Translation, t, result.Translation);
|
||||
BezierFirstDerivative<Quaternion>(p0.Orientation, p1.Orientation, p2.Orientation, p3.Orientation, t, result.Orientation);
|
||||
BezierFirstDerivative<Vector3>(p0.Scale, p1.Scale, p2.Scale, p3.Scale, t, result.Scale);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user