1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415
|
/*
Copyright (c) 2006, Michael Kazhdan and Matthew Bolitho
All rights reserved.
Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:
Redistributions of source code must retain the above copyright notice, this list of
conditions and the following disclaimer. Redistributions in binary form must reproduce
the above copyright notice, this list of conditions and the following disclaimer
in the documentation and/or other materials provided with the distribution.
Neither the name of the Johns Hopkins University nor the names of its contributors
may be used to endorse or promote products derived from this software without specific
prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO THE IMPLIED WARRANTIES
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
DAMAGE.
*/
//////////////////
// FunctionData //
//////////////////
template<int Degree,class Real>
FunctionData<Degree,Real>::FunctionData(void)
{
dotTable=dDotTable=d2DotTable=NULL;
valueTables=dValueTables=NULL;
res=0;
}
template<int Degree,class Real>
FunctionData<Degree,Real>::~FunctionData(void)
{
if(res)
{
if( dotTable) delete[] dotTable;
if( dDotTable) delete[] dDotTable;
if(d2DotTable) delete[] d2DotTable;
if( valueTables) delete[] valueTables;
if(dValueTables) delete[] dValueTables;
}
dotTable=dDotTable=d2DotTable=NULL;
valueTables=dValueTables=NULL;
res=0;
}
template<int Degree,class Real>
#if BOUNDARY_CONDITIONS
void FunctionData<Degree,Real>::set( const int& maxDepth , const PPolynomial<Degree>& F , const int& normalize , bool useDotRatios , bool reflectBoundary )
#else // !BOUNDARY_CONDITIONS
void FunctionData<Degree,Real>::set(const int& maxDepth,const PPolynomial<Degree>& F,const int& normalize , bool useDotRatios )
#endif // BOUNDARY_CONDITIONS
{
this->normalize = normalize;
this->useDotRatios = useDotRatios;
#if BOUNDARY_CONDITIONS
this->reflectBoundary = reflectBoundary;
#endif // BOUNDARY_CONDITIONS
depth = maxDepth;
res = BinaryNode<double>::CumulativeCenterCount( depth );
res2 = (1<<(depth+1))+1;
baseFunctions = new PPolynomial<Degree+1>[res];
// Scale the function so that it has:
// 0] Value 1 at 0
// 1] Integral equal to 1
// 2] Square integral equal to 1
switch( normalize )
{
case 2:
baseFunction=F/sqrt((F*F).integral(F.polys[0].start,F.polys[F.polyCount-1].start));
break;
case 1:
baseFunction=F/F.integral(F.polys[0].start,F.polys[F.polyCount-1].start);
break;
default:
baseFunction=F/F(0);
}
dBaseFunction = baseFunction.derivative();
#if BOUNDARY_CONDITIONS
leftBaseFunction = baseFunction + baseFunction.shift( -1 );
rightBaseFunction = baseFunction + baseFunction.shift( 1 );
dLeftBaseFunction = leftBaseFunction.derivative();
dRightBaseFunction = rightBaseFunction.derivative();
#endif // BOUNDARY_CONDITIONS
double c1,w1;
for( int i=0 ; i<res ; i++ )
{
BinaryNode< double >::CenterAndWidth( i , c1 , w1 );
#if BOUNDARY_CONDITIONS
if( reflectBoundary )
{
int d , off;
BinaryNode< double >::DepthAndOffset( i , d , off );
if ( off==0 ) baseFunctions[i] = leftBaseFunction.scale( w1 ).shift( c1 );
else if( off==((1<<d)-1) ) baseFunctions[i] = rightBaseFunction.scale( w1 ).shift( c1 );
else baseFunctions[i] = baseFunction.scale( w1 ).shift( c1 );
}
else baseFunctions[i] = baseFunction.scale(w1).shift(c1);
#else // !BOUNDARY_CONDITIONS
baseFunctions[i] = baseFunction.scale(w1).shift(c1);
#endif // BOUNDARY_CONDITIONS
// Scale the function so that it has L2-norm equal to one
switch( normalize )
{
case 2:
baseFunctions[i]/=sqrt(w1);
break;
case 1:
baseFunctions[i]/=w1;
break;
}
}
}
template<int Degree,class Real>
void FunctionData<Degree,Real>::setDotTables( const int& flags )
{
clearDotTables( flags );
int size;
size = ( res*res + res )>>1;
if( flags & DOT_FLAG )
{
dotTable = new Real[size];
memset( dotTable , 0 , sizeof(Real)*size );
}
if( flags & D_DOT_FLAG )
{
dDotTable = new Real[size];
memset( dDotTable , 0 , sizeof(Real)*size );
}
if( flags & D2_DOT_FLAG )
{
d2DotTable = new Real[size];
memset( d2DotTable , 0 , sizeof(Real)*size );
}
double t1 , t2;
t1 = baseFunction.polys[0].start;
t2 = baseFunction.polys[baseFunction.polyCount-1].start;
for( int i=0 ; i<res ; i++ )
{
double c1 , c2 , w1 , w2;
BinaryNode<double>::CenterAndWidth( i , c1 , w1 );
#if BOUNDARY_CONDITIONS
int d1 , d2 , off1 , off2;
BinaryNode< double >::DepthAndOffset( i , d1 , off1 );
int boundary1 = 0;
if ( reflectBoundary && off1==0 ) boundary1 = -1;
else if( reflectBoundary && off1==( (1<<d1)-1 ) ) boundary1 = 1;
#endif // BOUNDARY_CONDITIONS
double start1 = t1 * w1 + c1;
double end1 = t2 * w1 + c1;
for( int j=0 ; j<=i ; j++ )
{
BinaryNode<double>::CenterAndWidth( j , c2 , w2 );
#if BOUNDARY_CONDITIONS
BinaryNode< double >::DepthAndOffset( j , d2 , off2 );
int boundary2 = 0;
if ( reflectBoundary && off2==0 ) boundary2 = -1;
else if( reflectBoundary && off2==( (1<<d2)-1 ) ) boundary2 = 1;
#endif // BOUNDARY_CONDITIONS
int idx = SymmetricIndex( i , j );
double start = t1 * w2 + c2;
double end = t2 * w2 + c2;
#if BOUNDARY_CONDITIONS
if( reflectBoundary )
{
if( start<0 ) start = 0;
if( start>1 ) start = 1;
if( end <0 ) end = 0;
if( end >1 ) end = 1;
}
#endif // BOUNDARY_CONDITIONS
if( start< start1 ) start = start1;
if( end > end1 ) end = end1;
if( start>= end ) continue;
#if BOUNDARY_CONDITIONS
Real dot = dotProduct( c1 , w1 , c2 , w2 , boundary1 , boundary2 );
#else // !BOUNDARY_CONDITIONS
Real dot = dotProduct( c1 , w1 , c2 , w2 );
#endif // BOUNDARY_CONDITIONS
if( fabs(dot)<1e-15 ) continue;
if( flags & DOT_FLAG ) dotTable[idx]=dot;
if( useDotRatios )
{
#if BOUNDARY_CONDITIONS
if( flags & D_DOT_FLAG ) dDotTable[idx] = -dDotProduct( c1 , w1 , c2 , w2 , boundary1 , boundary2 ) / dot;
if( flags & D2_DOT_FLAG ) d2DotTable[idx] = d2DotProduct( c1 , w1 , c2 , w2 , boundary1 , boundary2 ) / dot;
#else // !BOUNDARY_CONDITIONS
if( flags & D_DOT_FLAG ) dDotTable[idx] = -dDotProduct(c1,w1,c2,w2)/dot;
if( flags & D2_DOT_FLAG ) d2DotTable[idx] = d2DotProduct(c1,w1,c2,w2)/dot;
#endif // BOUNDARY_CONDITIONS
}
else
{
#if BOUNDARY_CONDITIONS
if( flags & D_DOT_FLAG ) dDotTable[idx] = dDotProduct( c1 , w1 , c2 , w2 , boundary1 , boundary2 );
if( flags & D2_DOT_FLAG ) d2DotTable[idx] = d2DotProduct( c1 , w1 , c2 , w2 , boundary1 , boundary2 );
#else // !BOUNDARY_CONDTIONS
if( flags & D_DOT_FLAG ) dDotTable[idx] = dDotProduct(c1,w1,c2,w2);
if( flags & D2_DOT_FLAG ) d2DotTable[idx] = d2DotProduct(c1,w1,c2,w2);
#endif // BOUNDARY_CONDITIONS
}
}
}
}
template<int Degree,class Real>
void FunctionData<Degree,Real>::clearDotTables( const int& flags )
{
if((flags & DOT_FLAG) && dotTable)
{
delete[] dotTable;
dotTable=NULL;
}
if((flags & D_DOT_FLAG) && dDotTable)
{
delete[] dDotTable;
dDotTable=NULL;
}
if((flags & D2_DOT_FLAG) && d2DotTable)
{
delete[] d2DotTable;
d2DotTable=NULL;
}
}
template<int Degree,class Real>
void FunctionData<Degree,Real>::setValueTables( const int& flags , const double& smooth )
{
clearValueTables();
if( flags & VALUE_FLAG ) valueTables = new Real[res*res2];
if( flags & D_VALUE_FLAG ) dValueTables = new Real[res*res2];
PPolynomial<Degree+1> function;
PPolynomial<Degree> dFunction;
for( int i=0 ; i<res ; i++ )
{
if(smooth>0)
{
function=baseFunctions[i].MovingAverage(smooth);
dFunction=baseFunctions[i].derivative().MovingAverage(smooth);
}
else
{
function=baseFunctions[i];
dFunction=baseFunctions[i].derivative();
}
for( int j=0 ; j<res2 ; j++ )
{
double x=double(j)/(res2-1);
if(flags & VALUE_FLAG){ valueTables[j*res+i]=Real( function(x));}
if(flags & D_VALUE_FLAG){dValueTables[j*res+i]=Real(dFunction(x));}
}
}
}
template<int Degree,class Real>
void FunctionData<Degree,Real>::setValueTables(const int& flags,const double& valueSmooth,const double& normalSmooth){
clearValueTables();
if(flags & VALUE_FLAG){ valueTables=new Real[res*res2];}
if(flags & D_VALUE_FLAG){dValueTables=new Real[res*res2];}
PPolynomial<Degree+1> function;
PPolynomial<Degree> dFunction;
for(int i=0;i<res;i++){
if(valueSmooth>0) { function=baseFunctions[i].MovingAverage(valueSmooth);}
else { function=baseFunctions[i];}
if(normalSmooth>0) {dFunction=baseFunctions[i].derivative().MovingAverage(normalSmooth);}
else {dFunction=baseFunctions[i].derivative();}
for(int j=0;j<res2;j++){
double x=double(j)/(res2-1);
if(flags & VALUE_FLAG){ valueTables[j*res+i]=Real( function(x));}
if(flags & D_VALUE_FLAG){dValueTables[j*res+i]=Real(dFunction(x));}
}
}
}
template<int Degree,class Real>
void FunctionData<Degree,Real>::clearValueTables(void){
if( valueTables){delete[] valueTables;}
if(dValueTables){delete[] dValueTables;}
valueTables=dValueTables=NULL;
}
#if BOUNDARY_CONDITIONS
template<int Degree,class Real>
Real FunctionData<Degree,Real>::dotProduct( const double& center1 , const double& width1 , const double& center2 , const double& width2 , int boundary1 , int boundary2 ) const
{
const PPolynomial< Degree > *b1 , *b2;
if ( boundary1==-1 ) b1 = & leftBaseFunction;
else if( boundary1== 0 ) b1 = & baseFunction;
else if( boundary1== 1 ) b1 = &rightBaseFunction;
if ( boundary2==-1 ) b2 = & leftBaseFunction;
else if( boundary2== 0 ) b2 = & baseFunction;
else if( boundary2== 1 ) b2 = &rightBaseFunction;
double r=fabs( baseFunction.polys[0].start );
switch( normalize )
{
case 2:
return Real(((*b1)*b2->scale(width2/width1).shift((center2-center1)/width1)).integral(-2*r,2*r)*width1/sqrt(width1*width2));
case 1:
return Real(((*b1)*b2->scale(width2/width1).shift((center2-center1)/width1)).integral(-2*r,2*r)*width1/(width1*width2));
default:
return Real(((*b1)*b2->scale(width2/width1).shift((center2-center1)/width1)).integral(-2*r,2*r)*width1);
}
}
template<int Degree,class Real>
Real FunctionData<Degree,Real>::dDotProduct( const double& center1 , const double& width1 , const double& center2 , const double& width2 , int boundary1 , int boundary2 ) const
{
const PPolynomial< Degree-1 > *b1;
const PPolynomial< Degree > *b2;
if ( boundary1==-1 ) b1 = & dLeftBaseFunction;
else if( boundary1== 0 ) b1 = & dBaseFunction;
else if( boundary1== 1 ) b1 = &dRightBaseFunction;
if ( boundary2==-1 ) b2 = & leftBaseFunction;
else if( boundary2== 0 ) b2 = & baseFunction;
else if( boundary2== 1 ) b2 = & rightBaseFunction;
double r=fabs(baseFunction.polys[0].start);
switch(normalize){
case 2:
return Real(((*b1)*b2->scale(width2/width1).shift((center2-center1)/width1)).integral(-2*r,2*r)/sqrt(width1*width2));
case 1:
return Real(((*b1)*b2->scale(width2/width1).shift((center2-center1)/width1)).integral(-2*r,2*r)/(width1*width2));
default:
return Real(((*b1)*b2->scale(width2/width1).shift((center2-center1)/width1)).integral(-2*r,2*r));
}
}
template<int Degree,class Real>
Real FunctionData<Degree,Real>::d2DotProduct( const double& center1 , const double& width1 , const double& center2 , const double& width2 , int boundary1 , int boundary2 ) const
{
const PPolynomial< Degree-1 > *b1 , *b2;
if ( boundary1==-1 ) b1 = & dLeftBaseFunction;
else if( boundary1== 0 ) b1 = & dBaseFunction;
else if( boundary1== 1 ) b1 = &dRightBaseFunction;
if ( boundary2==-1 ) b2 = & dLeftBaseFunction;
else if( boundary2== 0 ) b2 = & dBaseFunction;
else if( boundary2== 1 ) b2 = &dRightBaseFunction;
double r=fabs(baseFunction.polys[0].start);
switch( normalize )
{
case 2:
return Real(((*b1)*b2->scale(width2/width1).shift((center2-center1)/width1)).integral(-2*r,2*r)/width2/sqrt(width1*width2));
case 1:
return Real(((*b1)*b2->scale(width2/width1).shift((center2-center1)/width1)).integral(-2*r,2*r)/width2/(width1*width2));
default:
return Real(((*b1)*b2->scale(width2/width1).shift((center2-center1)/width1)).integral(-2*r,2*r)/width2);
}
}
#else // !BOUNDARY_CONDITIONS
template<int Degree,class Real>
Real FunctionData<Degree,Real>::dotProduct(const double& center1,const double& width1,const double& center2,const double& width2) const{
double r=fabs(baseFunction.polys[0].start);
switch( normalize )
{
case 2:
return Real((baseFunction*baseFunction.scale(width2/width1).shift((center2-center1)/width1)).integral(-2*r,2*r)*width1/sqrt(width1*width2));
case 1:
return Real((baseFunction*baseFunction.scale(width2/width1).shift((center2-center1)/width1)).integral(-2*r,2*r)*width1/(width1*width2));
default:
return Real((baseFunction*baseFunction.scale(width2/width1).shift((center2-center1)/width1)).integral(-2*r,2*r)*width1);
}
}
template<int Degree,class Real>
Real FunctionData<Degree,Real>::dDotProduct(const double& center1,const double& width1,const double& center2,const double& width2) const{
double r=fabs(baseFunction.polys[0].start);
switch(normalize){
case 2:
return Real((dBaseFunction*baseFunction.scale(width2/width1).shift((center2-center1)/width1)).integral(-2*r,2*r)/sqrt(width1*width2));
case 1:
return Real((dBaseFunction*baseFunction.scale(width2/width1).shift((center2-center1)/width1)).integral(-2*r,2*r)/(width1*width2));
default:
return Real((dBaseFunction*baseFunction.scale(width2/width1).shift((center2-center1)/width1)).integral(-2*r,2*r));
}
}
template<int Degree,class Real>
Real FunctionData<Degree,Real>::d2DotProduct(const double& center1,const double& width1,const double& center2,const double& width2) const{
double r=fabs(baseFunction.polys[0].start);
switch(normalize){
case 2:
return Real((dBaseFunction*dBaseFunction.scale(width2/width1).shift((center2-center1)/width1)).integral(-2*r,2*r)/width2/sqrt(width1*width2));
case 1:
return Real((dBaseFunction*dBaseFunction.scale(width2/width1).shift((center2-center1)/width1)).integral(-2*r,2*r)/width2/(width1*width2));
default:
return Real((dBaseFunction*dBaseFunction.scale(width2/width1).shift((center2-center1)/width1)).integral(-2*r,2*r)/width2);
}
}
#endif // BOUNDARY_CONDITIONS
template<int Degree,class Real>
inline int FunctionData<Degree,Real>::SymmetricIndex( const int& i1 , const int& i2 )
{
if( i1>i2 ) return ((i1*i1+i1)>>1)+i2;
else return ((i2*i2+i2)>>1)+i1;
}
template<int Degree,class Real>
inline int FunctionData<Degree,Real>::SymmetricIndex( const int& i1 , const int& i2 , int& index )
{
if( i1<i2 )
{
index = ((i2*i2+i2)>>1)+i1;
return 1;
}
else{
index = ((i1*i1+i1)>>1)+i2;
return 0;
}
}
|