57 template<
int Degree >
inline bool LeftOverlap(
unsigned int depth ,
int offset )
60 if( Degree & 1 )
return (offset < 1+Degree) && (offset > -1-Degree );
61 else return (offset < Degree) && (offset > -2-Degree );
63 template<
int Degree >
inline bool RightOverlap(
unsigned int depth ,
int offset )
67 if( Degree & 1 )
return (offset > 2-1-Degree) && (offset < 2+1+Degree );
68 else return (offset > 2-2-Degree) && (offset < 2+ Degree );
70 template<
int Degree >
inline int ReflectLeft(
unsigned int depth ,
int offset )
72 if( Degree&1 )
return -offset;
73 else return -1-offset;
75 template<
int Degree >
inline int ReflectRight(
unsigned int depth ,
int offset )
78 if( Degree&1 )
return r -offset;
79 else return r-1-offset;
82 template<
int Degree ,
class Real >
85 vvDotTable = dvDotTable = ddDotTable = NULL;
86 valueTables = dValueTables = NULL;
89 functionCount = sampleCount = 0;
92 template<
int Degree ,
class Real >
97 if( vvDotTable )
delete[] vvDotTable;
98 if( dvDotTable )
delete[] dvDotTable;
99 if( ddDotTable )
delete[] ddDotTable;
101 if( valueTables )
delete[] valueTables;
102 if( dValueTables )
delete[] dValueTables;
104 if( baseFunctions )
delete[] baseFunctions;
105 if( baseBSplines )
delete[] baseBSplines;
107 vvDotTable = dvDotTable = ddDotTable = NULL;
108 valueTables = dValueTables=NULL;
109 baseFunctions = NULL;
114 template<
int Degree,
class Real>
117 this->useDotRatios = useDotRatios;
118 this->reflectBoundary = reflectBoundary;
125 baseBSplines =
new BSplineComponents[functionCount];
128 for(
int i=0 ; i<=Degree ; i++ ) baseBSpline[i] = Polynomial< Degree >::BSplineComponent( i ).shift(
double(-(Degree+1)/2) + i - 0.5 );
129 dBaseFunction = baseFunction.derivative();
132 for(
int i=0 ; i<Degree+3 ; i++ )
134 sPolys[i].
start = double(-(Degree+1)/2) + i - 1.5;
136 if( i<=Degree ) sPolys[i].
p += baseBSpline[i ].shift( -1 );
137 if( i>=1 && i<=Degree+1 ) sPolys[i].
p += baseBSpline[i-1];
138 for(
int j=0 ; j<i ; j++ ) sPolys[i].p -= sPolys[j].p;
140 leftBaseFunction.set( sPolys , Degree+3 );
141 for(
int i=0 ; i<Degree+3 ; i++ )
143 sPolys[i].
start = double(-(Degree+1)/2) + i - 0.5;
145 if( i<=Degree ) sPolys[i].
p += baseBSpline[i ];
146 if( i>=1 && i<=Degree+1 ) sPolys[i].
p += baseBSpline[i-1].shift( 1 );
147 for(
int j=0 ; j<i ; j++ ) sPolys[i].p -= sPolys[j].p;
149 rightBaseFunction.set( sPolys , Degree+3 );
150 dLeftBaseFunction = leftBaseFunction.derivative();
151 dRightBaseFunction = rightBaseFunction.derivative();
152 leftBSpline = rightBSpline = baseBSpline;
153 leftBSpline [1] += leftBSpline[2].
shift( -1 ) , leftBSpline[0] *= 0;
154 rightBSpline[1] += rightBSpline[0].
shift( 1 ) , rightBSpline[2] *= 0;
156 for(
int i=0 ; i<functionCount ; i++ )
159 baseFunctions[i] = baseFunction.scale(w).shift(c);
160 baseBSplines[i] = baseBSpline.scale(w).shift(c);
161 if( reflectBoundary )
166 if ( off==0 ) baseFunctions[i] = leftBaseFunction.scale(w).shift(c);
167 else if( off==r-1 ) baseFunctions[i] = rightBaseFunction.scale(w).shift(c);
168 if ( off==0 ) baseBSplines[i] = leftBSpline.scale(w).shift(c);
169 else if( off==r-1 ) baseBSplines[i] = rightBSpline.scale(w).shift(c);
173 template<
int Degree,
class Real>
176 clearDotTables( flags );
177 int size = ( functionCount*functionCount + functionCount )>>1;
178 int fullSize = functionCount*functionCount;
179 if( flags & VV_DOT_FLAG )
181 vvDotTable =
new Real[size];
182 memset( vvDotTable , 0 ,
sizeof(
Real)*size );
184 if( flags & DV_DOT_FLAG )
186 dvDotTable =
new Real[fullSize];
187 memset( dvDotTable , 0 ,
sizeof(
Real)*fullSize );
189 if( flags & DD_DOT_FLAG )
191 ddDotTable =
new Real[size];
192 memset( ddDotTable , 0 ,
sizeof(
Real)*size );
194 double vvIntegrals[Degree+1][Degree+1];
195 double vdIntegrals[Degree+1][Degree ];
196 double dvIntegrals[Degree ][Degree+1];
197 double ddIntegrals[Degree ][Degree ];
198 int vvSums[Degree+1][Degree+1];
199 int vdSums[Degree+1][Degree ];
200 int dvSums[Degree ][Degree+1];
201 int ddSums[Degree ][Degree ];
202 SetBSplineElementIntegrals< Degree , Degree >( vvIntegrals );
207 for(
int d1=0 ; d1<=depth ; d1++ )
208 for(
int off1=0 ; off1<(1<<d1) ; off1++ )
218 for(
int i=0 ; i<int(b1.size()) ; i++ )
for(
int j=0 ; j<=Degree ; j++ )
220 if( b1[i][j] && start1==-1 ) start1 = i;
221 if( b1[i][j] ) end1 = i+1;
223 for(
int d2=d1 ; d2<=depth ; d2++ )
225 for(
int off2=0 ; off2<(1<<d2) ; off2++ )
227 int start2 = off2-Degree;
228 int end2 = off2+Degree+1;
229 if( start2>=end1 || start1>=end2 )
continue;
230 start2 = std::max< int >( start1 , start2 );
231 end2 = std::min< int >( end1 , end2 );
232 if( d1==d2 && off2<off1 )
continue;
238 int idx = SymmetricIndex( ii , jj );
239 int idx1 = Index( ii , jj ) , idx2 = Index( jj , ii );
241 memset( vvSums , 0 ,
sizeof(
int ) * ( Degree+1 ) * ( Degree+1 ) );
242 memset( vdSums , 0 ,
sizeof(
int ) * ( Degree+1 ) * ( Degree ) );
243 memset( dvSums , 0 ,
sizeof(
int ) * ( Degree ) * ( Degree+1 ) );
244 memset( ddSums , 0 ,
sizeof(
int ) * ( Degree ) * ( Degree ) );
245 for(
int i=start2 ; i<end2 ; i++ )
247 for(
int j=0 ; j<=Degree ; j++ )
for(
int k=0 ; k<=Degree ; k++ ) vvSums[j][k] += b1[i][j] * b2[i][k];
248 for(
int j=0 ; j<=Degree ; j++ )
for(
int k=0 ; k< Degree ; k++ ) vdSums[j][k] += b1[i][j] * db2[i][k];
249 for(
int j=0 ; j< Degree ; j++ )
for(
int k=0 ; k<=Degree ; k++ ) dvSums[j][k] += db1[i][j] * b2[i][k];
250 for(
int j=0 ; j< Degree ; j++ )
for(
int k=0 ; k< Degree ; k++ ) ddSums[j][k] += db1[i][j] * db2[i][k];
252 double vvDot = 0 , dvDot = 0 , vdDot = 0 , ddDot = 0;
253 for(
int j=0 ; j<=Degree ; j++ )
for(
int k=0 ; k<=Degree ; k++ ) vvDot += vvIntegrals[j][k] * vvSums[j][k];
254 for(
int j=0 ; j<=Degree ; j++ )
for(
int k=0 ; k< Degree ; k++ ) vdDot += vdIntegrals[j][k] * vdSums[j][k];
255 for(
int j=0 ; j< Degree ; j++ )
for(
int k=0 ; k<=Degree ; k++ ) dvDot += dvIntegrals[j][k] * dvSums[j][k];
256 for(
int j=0 ; j< Degree ; j++ )
for(
int k=0 ; k< Degree ; k++ ) ddDot += ddIntegrals[j][k] * ddSums[j][k];
263 if( fabs(vvDot)<1e-15 )
continue;
264 if( flags & VV_DOT_FLAG ) vvDotTable [idx] =
Real( vvDot );
267 if( flags & DV_DOT_FLAG ) dvDotTable[idx1] =
Real( dvDot / vvDot );
268 if( flags & DV_DOT_FLAG ) dvDotTable[idx2] =
Real( vdDot / vvDot );
269 if( flags & DD_DOT_FLAG ) ddDotTable[idx ] =
Real( ddDot / vvDot );
273 if( flags & DV_DOT_FLAG ) dvDotTable[idx1] =
Real( dvDot );
274 if( flags & DV_DOT_FLAG ) dvDotTable[idx2] =
Real( dvDot );
275 if( flags & DD_DOT_FLAG ) ddDotTable[idx ] =
Real( ddDot );
283 for(
int i=0 ; i<int(b1.size()) ; i++ )
for(
int j=0 ; j<=Degree ; j++ )
285 if( b1[i][j] && start1==-1 ) start1 = i;
286 if( b1[i][j] ) end1 = i+1;
291 template<
int Degree,
class Real>
294 if( (flags & VV_DOT_FLAG) && vvDotTable )
delete[] vvDotTable , vvDotTable = NULL;
295 if( (flags & DV_DOT_FLAG) && dvDotTable )
delete[] dvDotTable , dvDotTable = NULL;
296 if( (flags & DD_DOT_FLAG) && ddDotTable )
delete[] ddDotTable , ddDotTable = NULL;
298 template<
int Degree ,
class Real >
304 double _start = ( off + 0.5 - 0.5*(Degree+1) ) / res - smooth;
305 double _end = ( off + 0.5 + 0.5*(Degree+1) ) / res + smooth;
309 start = int( floor( _start * (sampleCount-1) + 1 ) );
310 if( start<0 ) start = 0;
314 end = int( ceil( _end * (sampleCount-1) - 1 ) );
315 if( end>=sampleCount ) end = sampleCount-1;
317 template<
int Degree,
class Real>
321 if( flags & VALUE_FLAG ) valueTables =
new Real[functionCount*sampleCount];
322 if( flags & D_VALUE_FLAG ) dValueTables =
new Real[functionCount*sampleCount];
325 for(
int i=0 ; i<functionCount ; i++ )
334 function = baseFunctions[i];
337 for(
int j=0 ; j<sampleCount ; j++ )
339 double x=double(j)/(sampleCount-1);
340 if(flags & VALUE_FLAG){ valueTables[j*functionCount+i]=
Real(
function(x));}
341 if(flags & D_VALUE_FLAG){dValueTables[j*functionCount+i]=
Real(dFunction(x));}
345 template<
int Degree,
class Real>
348 if(flags & VALUE_FLAG){ valueTables=
new Real[functionCount*sampleCount];}
349 if(flags & D_VALUE_FLAG){dValueTables=
new Real[functionCount*sampleCount];}
352 for(
int i=0;i<functionCount;i++){
353 if(valueSmooth>0) {
function=baseFunctions[i].
MovingAverage(valueSmooth);}
354 else {
function=baseFunctions[i];}
356 else {dFunction=baseFunctions[i].
derivative();}
358 for(
int j=0;j<sampleCount;j++){
359 double x=double(j)/(sampleCount-1);
360 if(flags & VALUE_FLAG){ valueTables[j*functionCount+i]=
Real(
function(x));}
361 if(flags & D_VALUE_FLAG){dValueTables[j*functionCount+i]=
Real(dFunction(x));}
367 template<
int Degree,
class Real>
369 if( valueTables){
delete[] valueTables;}
370 if(dValueTables){
delete[] dValueTables;}
371 valueTables=dValueTables=NULL;
374 template<
int Degree,
class Real>
376 template<
int Degree,
class Real>
379 if( i1>i2 )
return ((i1*i1+i1)>>1)+i2;
380 else return ((i2*i2+i2)>>1)+i1;
382 template<
int Degree,
class Real>
387 index = ((i2*i2+i2)>>1)+i1;
392 index = ((i1*i1+i1)>>1)+i2;
401 template<
int Degree >
407 for(
int i=0 ; i<=Degree ; i++ )
409 int idx = -_off + offset + i;
410 if( idx>=0 && idx<res ) (*this)[idx][i] = 1;
414 _addLeft( offset-2*res , boundary ) , _addRight( offset+2*res , boundary );
415 if( Degree&1 ) _addLeft( offset-res , boundary ) , _addRight( offset+res , boundary );
416 else _addLeft( -offset-1 , boundary ) , _addRight( -offset-1+2*res , boundary );
419 template<
int Degree >
422 int res = int( this->size() );
424 for(
int i=0 ; i<=Degree ; i++ )
426 int idx = -_off + offset + i;
427 if( idx>=0 && idx<res ) (*this)[idx][i] += boundary , set =
true;
429 if( set ) _addLeft( offset-2*res , boundary );
431 template<
int Degree >
434 int res = int( this->size() );
436 for(
int i=0 ; i<=Degree ; i++ )
438 int idx = -_off + offset + i;
439 if( idx>=0 && idx<res ) (*this)[idx][i] += boundary , set =
true;
441 if( set ) _addRight( offset+2*res , boundary );
443 template<
int Degree >
446 fprintf( stderr ,
"[ERROR] B-spline up-sampling not supported for degree %d\n" , Degree );
452 high.resize( size()*2 );
454 for(
int i=0 ; i<int(size()) ; i++ )
456 high[2*i+0][0] += 1 * (*this)[i][0];
457 high[2*i+0][1] += 0 * (*this)[i][0];
458 high[2*i+1][0] += 2 * (*this)[i][0];
459 high[2*i+1][1] += 1 * (*this)[i][0];
461 high[2*i+0][0] += 1 * (*this)[i][1];
462 high[2*i+0][1] += 2 * (*this)[i][1];
463 high[2*i+1][0] += 0 * (*this)[i][1];
464 high[2*i+1][1] += 1 * (*this)[i][1];
477 high.resize( size()*2 );
479 for(
int i=0 ; i<int(size()) ; i++ )
481 high[2*i+0][0] += 1 * (*this)[i][0];
482 high[2*i+0][1] += 0 * (*this)[i][0];
483 high[2*i+0][2] += 0 * (*this)[i][0];
484 high[2*i+1][0] += 3 * (*this)[i][0];
485 high[2*i+1][1] += 1 * (*this)[i][0];
486 high[2*i+1][2] += 0 * (*this)[i][0];
488 high[2*i+0][0] += 3 * (*this)[i][1];
489 high[2*i+0][1] += 3 * (*this)[i][1];
490 high[2*i+0][2] += 1 * (*this)[i][1];
491 high[2*i+1][0] += 1 * (*this)[i][1];
492 high[2*i+1][1] += 3 * (*this)[i][1];
493 high[2*i+1][2] += 3 * (*this)[i][1];
495 high[2*i+0][0] += 0 * (*this)[i][2];
496 high[2*i+0][1] += 1 * (*this)[i][2];
497 high[2*i+0][2] += 3 * (*this)[i][2];
498 high[2*i+1][0] += 0 * (*this)[i][2];
499 high[2*i+1][1] += 0 * (*this)[i][2];
500 high[2*i+1][2] += 1 * (*this)[i][2];
505 template<
int Degree >
508 d.resize( this->size() );
510 for(
int i=0 ; i<int(this->size()) ; i++ )
for(
int j=0 ; j<=Degree ; j++ )
512 if( j-1>=0 ) d[i][j-1] -= (*this)[i][j];
513 if( j<Degree ) d[i][j ] += (*this)[i][j];
519 template<
int Degree1 ,
int Degree2 >
522 for(
int i=0 ; i<=Degree1 ; i++ )
525 for(
int j=0 ; j<=Degree2 ; j++ )
528 integrals[i][j] = ( p1 * p2 ).integral( 0 , 1 );