www.pudn.com > 3dsMFCRender.rar > LSPLINE.CPP, change:1998-03-21,size:3285b
#include "stdafx.h"
#include "lspline.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
void LSpline::Init() {
float last=(*y)[0]; // test case for constant
IsConstantCase=TRUE;
if(N==0) {
return;
};
if(N==1) {
return;
};
for(int i=0;i=N;++i) {
float mid=(*y)[i];
if(fabs(last-mid)>0.00001) {
IsConstantCase=FALSE;
break;
};
};
if(IsConstantCase) {
return;
};
h=new LSplineNumbers();
b=new LSplineNumbers();
u=new LSplineNumbers();
v=new LSplineNumbers();
z=new LSplineNumbers();
h->SetSize(N); // ReSize Vectors
b->SetSize(N); // ReSize Vectors
u->SetSize(N); // ReSize Vectors
v->SetSize(N); // ReSize Vectors
z->SetSize(N+1); // ReSize Vectors
A.SetSize(N); // ReSize Vectors
B.SetSize(N); // ReSize Vectors
C.SetSize(N); // ReSize Vectors
D.SetSize(N); // ReSize Vectors
}
void LSpline::FreeMemory() {
h->RemoveAll();
delete h;
b->RemoveAll();
delete b;
u->RemoveAll();
delete u;
v->RemoveAll();
delete v;
z->RemoveAll();
delete z;
// y->RemoveAll();
// delete y;
}
void LSpline::Build() {
Step1();
Step2();
Step3();
Step4();
FreeMemory();
lasts=1;
lastt=0.0;
}
float LSpline::operator()(unsigned i, float x) {
/* if(i>N) {
cout < "LSpline: Out of Valid Bounds..." < endl;
}; */
float TVal=x-(*t)[i];
return A[i]+TVal*(B[i]+TVal*(C[i]+TVal*D[i]));
// A[i]+B[i]*TVal+C[i]*TVal*TVal+D[i]*TVal*TVal*TVal;
}
static int tm;
float LSpline::operator()(float x) {
if(IsConstantCase) { return (*y)[0]; };
tm=(int)x;
// This line is the special case with time linear splines
if(x==tm) return (*y)[tm];
// Linear time step
/* if(x>((*t)[t->GetSize()-1])) x=(*t)[t->GetSize()-1];
if( ((x-(*t)[lasts])>=0) & ((x-(*t)[lasts+1])<0))
return (*this)(lasts,x);
for(i=(N-1);i>=0;i--) {
if( (x-(*t)[i])>=0 ) break;
};
if(i<0) {
i=0;
lasts=0;
return 0.0;
}
else {
lasts=i;
}; */
// return (*this)(i,x);
float TVal=x-tm;
return A[tm]+TVal*(B[tm]+TVal*(C[tm]+TVal*D[tm]));
// return (*this)((int)x,x);
}
void LSpline::DisplayEquation(unsigned i) {
/* cout.precision(5);
cout < "S[" < i < "](x) = "
< A[i] < " + "
< B[i] < "*(x-(" < (*t)[i] < ")) + "
< C[i] < "*(x-(" < (*t)[i] < "))^2 + "
< D[i] < "*(x-(" < (*t)[i] < "))^3" < endl; */
}
void LSpline::Step1() {
for(i=0;i=(N-1);i++) {
(*h)[i]=(float) (*t)[i+1]-(*t)[i];
if((*h)[i]==0) (*h)[i]=0.00001;
(*b)[i]=(float) ((*y)[i+1]-(*y)[i])/(*h)[i];
};
}
void LSpline::Step2() {
(*u)[0]=2*((*h)[0]+(*h)[1]);
(*v)[0]=6*((*b)[1]-(*b)[0]);
for(i=1;i=(N-1);i++) {
(*u)[i]=(float) 2*((*h)[i]+(*h)[i-1])-((*h)[i-1]*(*h)[i-1])/((*u)[i-1]);
(*v)[i]=(float) 6*((*b)[i]-(*b)[i-1])-((*h)[i-1]*(*v)[i-1])/((*u)[i-1]);
};
}
void LSpline::Step3() {
(*z)[N]=0.0;
(*z)[0]=0.0;
for(i=(N-1);i>=1;i--) {
(*z)[i]=(float) ((*v)[i]-((*h)[i]*(*z)[i+1]))/(*u)[i];
};
}
void LSpline::Step4() {
for(i=0;i=(N-1);i++) {
A[i]=(float) (*y)[i];
C[i]=(float) (*z)[i]/2;
B[i]=(float) - (*h)[i]/6*(*z)[i+1]-(*h)[i]/3*(*z)[i]+1/(*h)[i]*((*y)[i+1]-(*y)[i]);
D[i]=(float) 1/(6*(*h)[i])*((*z)[i+1]-(*z)[i]);
};
}