Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save petrsm/c17e33dbbee5048ae7dd634b823df59e to your computer and use it in GitHub Desktop.
Save petrsm/c17e33dbbee5048ae7dd634b823df59e to your computer and use it in GitHub Desktop.
//
// CatmullRomLength()
//
//
//*******************************************************************************************
template <typename T>
T CatmullRomLength( const CVector2<T> &p0,
const CVector2<T> &p1,
const CVector2<T> &p2,
const CVector2<T> &p3,
T t)
{
#if 1
static const T consts[][2] =
{
// coeff, root
{ 0.118463442f, 0.046910077f },
{ 0.239314335f, 0.230765345f },
{ 0.284444444f, 0.500000000f },
{ 0.239314335f, 0.769234655f },
{ 0.118463442f, 0.953089922f }
};
#else
static const T consts[][2] =
{
{ 0.055668567116174 / 2, (-0.978228658146057 + 1) / 2 },
{ 0.125580369464905 / 2, (-0.887062599768095 + 1) / 2 },
{ 0.186290210927734 / 2, (-0.730152005574049 + 1) / 2 },
{ 0.233193764591990 / 2, (-0.519096129110681 + 1) / 2 },
{ 0.262804544510247 / 2, (-0.269543155952345 + 1) / 2 },
{ 0.272925086777901 / 2, (0.000000000000000 + 1) / 2 },
{ 0.262804544510247 / 2, (0.269543155952345 + 1) / 2 },
{ 0.233193764591990 / 2, (0.519096129110681 + 1) / 2 },
{ 0.186290210927734 / 2, (0.730152005574049 + 1) / 2 },
{ 0.125580369464905 / 2, (0.887062599768095 + 1) / 2 },
{ 0.055668567116174 / 2, (0.978228658146057 + 1) / 2 }
};
#endif
typedef CVector2<T> T_Vec;
const T_Vec a = 4.5f * (p1 - p2) + 1.5f * (p3 - p0);
const T_Vec b = 2.0f * p0 - 5.0f * p1 + 4.0f * p2 - p3;
const T_Vec c = (p2 - p0) * 0.5f;
T res = T(0);
for (uint i = 0; i < NX_COUNTOF(consts); i++)
{
const T tp = t * consts[i][1];
const T_Vec deriv = (a * tp + b) * tp + c;
const T speed = Sqrt(deriv.Dot(deriv));
res += consts[i][0] * speed;
}
return res * t;
}
//*******************************************************************************************
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment