330 Chapter 5 Controller-Based Animation
Taking advantage of a few patterns in these equations, the general code is
Point X (float u)
{
int imin, imax;
GetIndices(u,imin,imax);
v[d] = 1;
for(intr=d-1;r>=0;r--)
{
inti0=imax+1,i1=r+imax+1-d;
float knot0 = GetKnot(i0), knot1 = GetKnot(i1);
float invdenom = 1/(knot0 - knot1);
float coeff1 = (knot0 - u)*invdenom, coeff0;
v[r] = coeff1*v[r+1];
for(intc=r+1;c<d;c++)
{
coeff0 = (t - knot1)*invdenom;
v[c] *= coeff0;
knot0 = GetKnot(++i0);
knot1 = GetKnot(++i1);
invdenom = 1/(knot0 - knot1);
coeff1 = (knot0 - u) * invdenom;
v[c] += coeff1*v[c+1];
}
coeff0 = (u - knot1)*invdenom;
v[d] *= coeff0;
}
Point result = ZERO;
for(inti=0,j=imin; i <= d; i++)
{
result += v[i] * Q[j];
}
return result;
}
First, we can inline the GetIndices function ...