# 各种插值算法的金字塔算法

//

//

//        para

//

double power ( double base, int para )
{
double tmp=1;
int i=0;
for ( i=0; i<para; ++i )
tmp *= base;
return tmp;
}

//

//

//         dec

//

//

double Normalize ( double value, int dec )
{
int tmp = value * power ( 10, dec );
return tmp/power ( 10, dec );
}

//

//

//           t

//           PolynomialsValues

//

bool LagrangePolynomials ( int num, double t, std::deque<double>& PolynomialsValues )
{
double        parentL, parentR, delta, cPL, cPR, value;
double        bakLeft, tCurLeft = 1, tCurRight = 0;
int                i, j, oldLength;

delta = 1.0/(num-1);
PolynomialsValues.push_back ( 1 );
//

for ( i=0; i<num-1; ++i ){
bakLeft = tCurLeft;
oldLength = PolynomialsValues.size();

if ( oldLength == 1 )
parentL = parentR = PolynomialsValues[0];
else{
parentL = PolynomialsValues[0];
parentR = PolynomialsValues[1];
}
//

cPL = tCurLeft - tCurRight;
value = Normalize( parentL * (tCurLeft-t) / cPL, 4 );
PolynomialsValues.push_back ( value );
//

tCurLeft += delta;
for ( j=0; j<oldLength-1; ++j ){
parentL = PolynomialsValues[j];
parentR = PolynomialsValues[j+1];
cPL = tCurLeft - delta - tCurRight;
cPR = tCurLeft - tCurRight - delta;

value = ((t-tCurRight)*parentL)/cPL + ((tCurLeft-t)*parentR)/cPR;
PolynomialsValues.push_back ( Normalize( value, 4 ) );

tCurLeft += delta;
tCurRight += delta;
}
//

tCurLeft -= delta;
cPR = tCurLeft - tCurRight;
value = Normalize( parentR * (t - tCurRight) / cPR, 4 );
PolynomialsValues.push_back ( value );

//

PolynomialsValues.erase(PolynomialsValues.begin(), PolynomialsValues.begin()+oldLength );

//

tCurLeft = bakLeft - delta;
tCurRight = 0;
}

return true;
}

bersteinlagrange间的差别很小,当利用lagrange进行线性插值时,每次使用的插值参数都是不同的,berstein则刚好 相反,berstein却不要求曲线必过控制点,所以berstein的多项式是逼近方法而不是插值方法,所以实现和lagrange的差不多,只是更简 单些:)
//

//

//        t

//        PolynomialsValues

//

bool BernsteinBase ( int num, double t, std::deque<double>& PolynomialsValues )
{
double        parentL, parentR, delta, value;
double        tCurLeft = 1, tCurRight = 0;
int            i, j, oldLength;
//

delta = 1.0/(num-1);
PolynomialsValues.push_back ( 1 );
//

for ( i=0; i<num-1; ++i ){
oldLength = PolynomialsValues.size();

if ( oldLength == 1 )
parentL = parentR = PolynomialsValues[0];
else{
parentL = PolynomialsValues[0];
parentR = PolynomialsValues[1];
}
//

value = Normalize( parentL * (tCurLeft-t), 4 );
PolynomialsValues.push_back ( value );
//

for ( j=0; j<oldLength-1; ++j ){
parentL = PolynomialsValues[j];
parentR = PolynomialsValues[j+1];

value = ((t-tCurRight)*parentL) + ((tCurLeft-t)*parentR);
PolynomialsValues.push_back ( Normalize( value, 4 ) );

}
//

value = Normalize( parentR * (t - tCurRight), 4 );
PolynomialsValues.push_back ( value );

//

PolynomialsValues.erase(PolynomialsValues.begin(),        PolynomialsValues.begin()+oldLength );

}

return true;
}

/* B-样条基函数,金字塔算法通式
@remark

@para num

@para t

@para SectNum            B-

@para PolynomialsValues

@return

*/
bool BSplineBase ( int stage, double t, int SectNum, std::deque<double>& PolynomialsValues )
{
double        parentL, parentR, delta, cPL, cPR, value;
double        tCurLeft, tCurRight;
int            i, j, oldLength;
//

delta = 1.0/(2*stage-1);

tCurLeft = (3+SectNum) * delta;
tCurRight = tCurLeft - delta;
PolynomialsValues.push_back ( 1 );
//

for ( i=0; i<stage; ++i ){
oldLength = PolynomialsValues.size();

if ( oldLength == 1 )
parentL = parentR = PolynomialsValues[0];
else{
parentL = PolynomialsValues[0];
parentR = PolynomialsValues[1];
}
//

cPL = tCurLeft - tCurRight;
value = Normalize( parentL * (tCurLeft-t) / cPL, 4 );
PolynomialsValues.push_back ( value );
//

tCurLeft += delta;
for ( j=0; j<oldLength-1; ++j ){
parentL = PolynomialsValues[j];
parentR = PolynomialsValues[j+1];
cPL = tCurLeft - delta - tCurRight;
cPR = tCurLeft - tCurRight - delta;

value = ((t-tCurRight)*parentL)/cPL + ((tCurLeft-t)*parentR)/cPR;
PolynomialsValues.push_back ( Normalize( value, 4 ) );

tCurLeft += delta;
tCurRight += delta;
}
//

tCurLeft -= delta;
cPR = tCurLeft - tCurRight;
value = Normalize( parentR * (t - tCurRight) / cPR, 4 );
PolynomialsValues.push_back ( value );

//

PolynomialsValues.erase(PolynomialsValues.begin(), PolynomialsValues.begin()+oldLength );

//

tCurLeft = (3+SectNum) * delta;
tCurRight = tCurLeft - (i+2)*delta;
}

return true;
}

• 本文已收录于以下专栏：

举报原因： 您举报文章：各种插值算法的金字塔算法 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)