Debian Codegraph @

src/thirdparty/PoissonRecon/BSplineData.inl

raw
1 /*
2 Copyright (c) 2006, Michael Kazhdan and Matthew Bolitho
3 All rights reserved.
4
5 Redistribution and use in source and binary forms, with or without modification,
6 are permitted provided that the following conditions are met:
7
8 Redistributions of source code must retain the above copyright notice, this list of
9 conditions and the following disclaimer. Redistributions in binary form must reproduce
10 the above copyright notice, this list of conditions and the following disclaimer
11 in the documentation and/or other materials provided with the distribution.
12
13 Neither the name of the Johns Hopkins University nor the names of its contributors
14 may be used to endorse or promote products derived from this software without specific
15 prior written permission.
16
17 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
18 EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO THE IMPLIED WARRANTIES
19 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
20 SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
21 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
22 TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
23 BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
25 ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
26 DAMAGE.
27 */
28
29 ///////////////////////////
30 // BSplineEvaluationData //
31 ///////////////////////////
32 template< unsigned int FEMSig >
33 double BSplineEvaluationData< FEMSig >::Value( int depth , int off , double s , int d )
34 {
35 if( s<0 || s>1 ) return 0.;
36
37 int res = 1<<depth;
38 if( OutOfBounds( depth , off ) ) return 0;
39
40 typename BSplineData< FEMSig , Degree >::BSplineComponents components( depth , off );
41
42 // [NOTE] This is an ugly way to ensure that when s=1 we evaluate using a B-Spline component within the valid range.
43 int ii = std::max< int >( 0 , std::min< int >( res-1 , (int)floor( s * res ) ) ) - off;
44
45 if( ii<BSplineSupportSizes< Degree >::SupportStart || ii>BSplineSupportSizes< Degree >::SupportEnd ) return 0;
46 return d<=Degree ? components[ii-BSplineSupportSizes< Degree >::SupportStart][d](s) : 0;
47 }
48 template< unsigned int FEMSig >
49 double BSplineEvaluationData< FEMSig >::Integral( int depth , int off , double b , double e , int d )
50 {
51 double integral = 0;
52 // Check for valid integration bounds
53 if( OutOfBounds( depth , off ) ) return 0;
54 if( b>=e || b>=1 || e<=0 ) return 0;
55 if( b<0 ) b=0;
56 if( e>1 ) e=1;
57
58 int res = 1<<depth;
59 double _b = ( (double)( off + BSplineSupportSizes< Degree >::SupportStart ) )/res;
60 double _e = ( (double)( off + 1 + BSplineSupportSizes< Degree >::SupportEnd ) )/res;
61 if( b>=_e || e<=_b ) return 0;
62 typename BSplineData< FEMSig , Degree >::BSplineComponents components( depth , off );
63 for( int i=BSplineSupportSizes< Degree >::SupportStart ; i<=BSplineSupportSizes< Degree >::SupportEnd ; i++ )
64 {
65 // The index of the current cell
66 int c = off + i;
67 // The bounds of the current cell
68 _b = std::max< double >( b , ( (double)c ) / res ) , _e = std::min< double >( e , ( (double)(c+1) )/res );
69 if( _b<_e ) integral += d<=Degree ? components[i-BSplineSupportSizes< Degree >::SupportStart][d].integral( _b , _e ) : 0;
70 }
71 return integral;
72 }
73 template< unsigned int FEMSig >
74 template< unsigned int D >
75 void BSplineEvaluationData< FEMSig >::SetCenterEvaluator( typename CenterEvaluator< D >::Evaluator& evaluator , int depth )
76 {
77 evaluator._depth = depth;
78 int res = 1<<depth;
79 for( int i=0 ; i<IndexSize ; i++ ) for( int j=BSplineSupportSizes< Degree >::SupportStart ; j<=BSplineSupportSizes< Degree >::SupportEnd ; j++ )
80 {
81 int ii = IndexToOffset( depth , i );
82 double s = 0.5 + ii + j;
83 for( int d1=0 ; d1<=D ; d1++ ) evaluator._ccValues[d1][i][j-BSplineSupportSizes< Degree >::SupportStart] = Value( depth , ii , s/res , d1 );
84 }
85 }
86 template< unsigned int FEMSig >
87 template< unsigned int D >
88 void BSplineEvaluationData< FEMSig >::SetChildCenterEvaluator( typename CenterEvaluator< D >::ChildEvaluator& evaluator , int parentDepth )
89 {
90 evaluator._parentDepth = parentDepth;
91 int res = 1<<(parentDepth+1);
92 for( int i=0 ; i<IndexSize ; i++ ) for( int j=BSplineSupportSizes< Degree >::ChildSupportStart ; j<=BSplineSupportSizes< Degree >::ChildSupportEnd ; j++ )
93 {
94 int ii = IndexToOffset( parentDepth , i );
95 double s = 0.5 + 2*ii + j;
96 for( int d1=0 ; d1<=D ; d1++ ) evaluator._pcValues[d1][i][j-BSplineSupportSizes< Degree >::ChildSupportStart] = Value( parentDepth , ii , s/res , d1 );
97 }
98 }
99 template< unsigned int FEMSig >
100 template< unsigned int D >
101 double BSplineEvaluationData< FEMSig >::CenterEvaluator< D >::Evaluator::value( int fIdx , int cIdx , int d ) const
102 {
103 int dd = cIdx-fIdx , res = 1<<(_depth);
104 if( cIdx<0 || cIdx>=res || OutOfBounds( _depth , fIdx ) || dd<BSplineSupportSizes< Degree >::SupportStart || dd>BSplineSupportSizes< Degree >::SupportEnd ) return 0;
105 return _ccValues[d][ OffsetToIndex( _depth , fIdx ) ][dd-BSplineSupportSizes< Degree >::SupportStart];
106 }
107 template< unsigned int FEMSig >
108 template< unsigned int D >
109 double BSplineEvaluationData< FEMSig >::CenterEvaluator< D >::ChildEvaluator::value( int fIdx , int cIdx , int d ) const
110 {
111 int dd = cIdx-2*fIdx , res = 1<<(_parentDepth+1);
112 if( cIdx<0 || cIdx>=res || OutOfBounds( _parentDepth , fIdx ) || dd<BSplineSupportSizes< Degree >::ChildSupportStart || dd>BSplineSupportSizes< Degree >::ChildSupportEnd ) return 0;
113 return _pcValues[d][ OffsetToIndex( _parentDepth , fIdx ) ][dd-BSplineSupportSizes< Degree >::ChildSupportStart];
114 }
115 template< unsigned int FEMSig >
116 template< unsigned int D >
117 void BSplineEvaluationData< FEMSig >::SetCornerEvaluator( typename CornerEvaluator< D >::Evaluator& evaluator , int depth )
118 {
119 evaluator._depth = depth;
120 int res = 1<<depth;
121 for( int i=0 ; i<IndexSize ; i++ ) for( int j=BSplineSupportSizes< Degree >::BCornerStart ; j<=BSplineSupportSizes< Degree >::BCornerEnd ; j++ )
122 {
123 int ii = IndexToOffset( depth , i );
124 double s = ii + j;
125 int jj = j-BSplineSupportSizes< Degree >::BCornerStart;
126 for( int d1=0 ; d1<=D ; d1++ )
127 {
128 if( d1==Degree )
129 {
130 if ( j==BSplineSupportSizes< Degree >::BCornerStart ) evaluator._ccValues[d1][i][jj] = ( Value( depth , ii , ( s+0.5 )/res , d1 ) ) / 2;
131 else if( j==BSplineSupportSizes< Degree >::BCornerEnd ) evaluator._ccValues[d1][i][jj] = ( Value( depth , ii , ( s-0.5 )/res , d1 ) ) / 2;
132 else evaluator._ccValues[d1][i][jj] = ( Value( depth , ii , ( s-0.5 )/res , d1 ) + Value( depth , ii , ( s+0.5 )/res , d1 ) ) / 2;
133 }
134 else evaluator._ccValues[d1][i][jj] = Value( depth , ii , s /res , d1 );
135 }
136 }
137 }
138 template< unsigned int FEMSig >
139 template< unsigned int D >
140 void BSplineEvaluationData< FEMSig >::SetChildCornerEvaluator( typename CornerEvaluator< D >::ChildEvaluator& evaluator , int parentDepth )
141 {
142 evaluator._parentDepth = parentDepth;
143 int res = 1<<(parentDepth+1);
144 for( int i=0 ; i<IndexSize ; i++ ) for( int j=BSplineSupportSizes< Degree >::ChildBCornerStart ; j<=BSplineSupportSizes< Degree >::ChildBCornerEnd ; j++ )
145 {
146 int ii = IndexToOffset( parentDepth , i );
147 double s = 2*ii + j;
148 int jj = j-BSplineSupportSizes< Degree >::ChildBCornerStart;
149 for( int d1=0 ; d1<=D ; d1++ )
150 {
151 if( d1==Degree )
152 {
153 if ( j==BSplineSupportSizes< Degree >::ChildBCornerStart ) evaluator._pcValues[d1][i][jj] = ( Value( parentDepth , ii , ( s+0.5 )/res , d1 ) ) / 2;
154 else if( j==BSplineSupportSizes< Degree >::ChildBCornerEnd ) evaluator._pcValues[d1][i][jj] = ( Value( parentDepth , ii , ( s-0.5 )/res , d1 ) ) / 2;
155 else evaluator._pcValues[d1][i][jj] = ( Value( parentDepth , ii , ( s-0.5 )/res , d1 ) + Value( parentDepth , ii , ( s+0.5 )/res , d1 ) ) / 2;
156 }
157 else evaluator._pcValues[d1][i][jj] = Value( parentDepth , ii , s /res , d1 );
158 }
159 }
160 }
161 template< unsigned int FEMSig >
162 template< unsigned int D >
163 double BSplineEvaluationData< FEMSig >::CornerEvaluator< D >::Evaluator::value( int fIdx , int cIdx , int d ) const
164 {
165 int dd = cIdx-fIdx , res = ( 1<<_depth ) + 1;
166 if( cIdx<0 || cIdx>=res || OutOfBounds( _depth , fIdx ) || dd<BSplineSupportSizes< Degree >::BCornerStart || dd>BSplineSupportSizes< Degree >::BCornerEnd ) return 0;
167 return _ccValues[d][ OffsetToIndex( _depth , fIdx ) ][dd-BSplineSupportSizes< Degree >::BCornerStart];
168 }
169 template< unsigned int FEMSig >
170 template< unsigned int D >
171 double BSplineEvaluationData< FEMSig >::CornerEvaluator< D >::ChildEvaluator::value( int fIdx , int cIdx , int d ) const
172 {
173 int dd = cIdx-2*fIdx , res = ( 1<<(_parentDepth+1) ) + 1;
174 if( cIdx<0 || cIdx>=res || OutOfBounds( _parentDepth , fIdx ) || dd<BSplineSupportSizes< Degree >::ChildBCornerStart || dd>BSplineSupportSizes< Degree >::ChildBCornerEnd ) return 0;
175 return _pcValues[d][ OffsetToIndex( _parentDepth , fIdx ) ][dd-BSplineSupportSizes< Degree >::ChildBCornerStart];
176 }
177 template< unsigned int FEMSig >
178 void BSplineEvaluationData< FEMSig >::SetUpSampleEvaluator( UpSampleEvaluator& evaluator , int lowDepth )
179 {
180 evaluator._lowDepth = lowDepth;
181 for( int i=0 ; i<IndexSize ; i++ )
182 {
183 int ii = IndexToOffset( lowDepth , i );
184 BSplineUpSamplingCoefficients b( lowDepth , ii );
185 for( int j=0 ; j<BSplineSupportSizes< Degree >::UpSampleSize ; j++ ) evaluator._pcValues[i][j] = b[j];
186 }
187 }
188 template< unsigned int FEMSig >
189 double BSplineEvaluationData< FEMSig >::UpSampleEvaluator::value( int pIdx , int cIdx ) const
190 {
191 int dd = cIdx-2*pIdx;
192 if( OutOfBounds( _lowDepth+1 , cIdx ) || OutOfBounds( _lowDepth , pIdx ) || dd<BSplineSupportSizes< Degree >::UpSampleStart || dd>BSplineSupportSizes< Degree >::UpSampleEnd ) return 0;
193 return _pcValues[ OffsetToIndex( _lowDepth , pIdx ) ][dd-BSplineSupportSizes< Degree >::UpSampleStart];
194 }
195
196 //////////////////////////////////////////////////////////
197 // BSplineEvaluationData::BSplineUpSamplingCoefficients //
198 //////////////////////////////////////////////////////////
199 template< unsigned int FEMSig >
200 BSplineEvaluationData< FEMSig >::BSplineUpSamplingCoefficients::BSplineUpSamplingCoefficients( int depth , int offset )
201 {
202 static const BoundaryType BType = FEMSignature< FEMSig >::BType;
203 // [ 1/8 1/2 3/4 1/2 1/8]
204 // [ 1 , 1 ] -> [ 3/4 , 1/2 , 1/8 ] + [ 1/8 , 1/2 , 3/4 ] = [ 7/8 , 1 , 7/8 ]
205 int dim = BSplineSupportSizes< Degree >::Nodes(depth) , _dim = BSplineSupportSizes< Degree >::Nodes(depth+1);
206 bool reflect;
207 offset = BSplineData< FEMSig >::RemapOffset( depth , offset , reflect );
208 int multiplier = ( BType==BOUNDARY_DIRICHLET && reflect ) ? -1 : 1;
209 bool useReflected = ( BType!=BOUNDARY_FREE ) && ( BSplineSupportSizes< Degree >::Inset || ( offset % ( dim-1 ) ) );
210 int b[ BSplineSupportSizes< Degree >::UpSampleSize ];
211 Polynomial< Degree+1 >::BinomialCoefficients( b );
212
213 // Clear the values
214 memset( _coefficients , 0 , sizeof(int) * BSplineSupportSizes< Degree >::UpSampleSize );
215
216 // Get the array of coefficients, relative to the origin
217 #ifdef SANITIZED_PR
218 int baseOffset = - ( 2*offset + BSplineSupportSizes< Degree >::UpSampleStart );
219 #else // !SANITIZED_PR
220 Pointer( int ) coefficients = GetPointer( _coefficients , BSplineSupportSizes< Degree >::UpSampleSize ) - ( 2*offset + BSplineSupportSizes< Degree >::UpSampleStart );
221 #endif // SANITIZED_PR
222 for( int i=BSplineSupportSizes< Degree >::UpSampleStart ; i<=BSplineSupportSizes< Degree >::UpSampleEnd ; i++ )
223 {
224 int _offset = 2*offset+i;
225 _offset = BSplineData< FEMSig >::RemapOffset( depth+1 , _offset , reflect );
226 if( useReflected || !reflect )
227 {
228 int _multiplier = multiplier * ( ( BType==BOUNDARY_DIRICHLET && reflect ) ? -1 : 1 );
229 #ifdef SANITIZED_PR
230 _coefficients[ _offset + baseOffset ] += b[ i-BSplineSupportSizes< Degree >::UpSampleStart ] * _multiplier;
231 #else // !SANITIZED_PR
232 coefficients[ _offset ] += b[ i-BSplineSupportSizes< Degree >::UpSampleStart ] * _multiplier;
233 #endif // SANITIZED_PR
234 }
235 // If we are not inset and we are at the boundary, use the reflection as well
236 if( BType!=BOUNDARY_FREE && !BSplineSupportSizes< Degree >::Inset && ( offset % (dim-1) ) && !( _offset % (_dim-1) ) )
237 {
238 _offset = BSplineData< FEMSig >::RemapOffset( depth+1 , _offset , reflect );
239 int _multiplier = multiplier * ( ( BType==BOUNDARY_DIRICHLET && reflect ) ? -1 : 1 );
240 if( BType==BOUNDARY_DIRICHLET ) _multiplier *= -1;
241 #ifdef SANITIZED_PR
242 _coefficients[ _offset + baseOffset ] += b[ i-BSplineSupportSizes< Degree >::UpSampleStart ] * _multiplier;
243 #else // !SANITIZED_PR
244 coefficients[ _offset ] += b[ i-BSplineSupportSizes< Degree >::UpSampleStart ] * _multiplier;
245 #endif // SANITIZED_PR
246 }
247 }
248 }
249
250 ////////////////////////////
251 // BSplineIntegrationData //
252 ////////////////////////////
253 template< unsigned int FEMSig1 , unsigned int FEMSig2 >
254 template< unsigned int D1 , unsigned int D2 >
255 double BSplineIntegrationData< FEMSig1 , FEMSig2 >::Dot( int depth1 , int off1 , int depth2 , int off2 )
256 {
257 if( D1>Degree1 ) MK_THROW( "Taking more derivatives than the degree: " , D1 , " > " , Degree1 );
258 if( D2>Degree2 ) MK_THROW( "Taking more derivatives than the degree: " , D2 , " > " , Degree2 );
259 const int _Degree1 = ( Degree1>=D1 ) ? Degree1 - D1 : 0 , _Degree2 = ( Degree2>=D2 ) ? Degree2 - D2 : 0;
260 int sums[ Degree1+1 ][ Degree2+1 ];
261
262 int depth = std::max< int >( depth1 , depth2 );
263
264 BSplineElements< Degree1 > b1;
265 BSplineElements< Degree2 > b2;
266 if( BSplineSupportSizes< Degree1 >::IsInteriorlySupported( depth1 , off1 ) && BSplineSupportSizes< Degree2 >::IsInteriorlySupported( depth2 , off2 ) )
267 {
268 if( depth1<depth2 )
269 {
270 int begin1 , end1 , res = 1 - BSplineSupportSizes< Degree1 >::SupportStart + BSplineSupportSizes< Degree1 >::SupportEnd;
271 BSplineSupportSizes< Degree1 >::InteriorSupportedSpan( depth1 , begin1 , end1 );
272 b1 = BSplineElements< Degree1 >( res , begin1 , BOUNDARY_FREE );
273 for( int d=depth1 ; d<depth2 ; d++ )
274 {
275 BSplineElements< Degree1 > b=b1;
276 b.upSample( b1 );
277 res <<= 1;
278 }
279 b2 = BSplineElements< Degree2 >( res , off2 - ( (off1-begin1)<<(depth2-depth1) ) , BOUNDARY_FREE );
280 }
281 else
282 {
283 int begin2 , end2 , res = 1 - BSplineSupportSizes< Degree2 >::SupportStart + BSplineSupportSizes< Degree2 >::SupportEnd;
284 BSplineSupportSizes< Degree2 >::InteriorSupportedSpan( depth2 , begin2 , end2 );
285 b2 = BSplineElements< Degree2 >( res , begin2 , BOUNDARY_FREE );
286 for( int d=depth2 ; d<depth1 ; d++ )
287 {
288 BSplineElements< Degree2 > b=b2;
289 b.upSample( b2 );
290 res <<= 1;
291 }
292 b1 = BSplineElements< Degree1 >( res , off1 - ( (off2-begin2)<<(depth1-depth2) ) , BOUNDARY_FREE );
293 }
294 }
295 else
296 {
297 b1 = BSplineElements< Degree1 >( 1<<depth1 , off1 , FEMSignature< FEMSig1 >::BType );
298 b2 = BSplineElements< Degree2 >( 1<<depth2 , off2 , FEMSignature< FEMSig2 >::BType );
299 {
300 BSplineElements< Degree1 > b;
301 while( depth1<depth ) b=b1 , b.upSample( b1 ) , depth1++;
302 }
303 {
304 BSplineElements< Degree2 > b;
305 while( depth2<depth ) b=b2 , b.upSample( b2 ) , depth2++;
306 }
307 }
308
309 BSplineElements< Degree1-D1 > db1;
310 BSplineElements< Degree2-D2 > db2;
311 b1.template differentiate< D1 >( db1 ) , b2.template differentiate< D2 >( db2 );
312
313 int start1=-1 , end1=-1 , start2=-1 , end2=-1;
314 for( int i=0 ; i<int( b1.size() ) ; i++ )
315 {
316 for( int j=0 ; j<=Degree1 ; j++ )
317 {
318 if( b1[i][j] && start1==-1 ) start1 = i;
319 if( b1[i][j] ) end1 = i+1;
320 }
321 for( int j=0 ; j<=Degree2 ; j++ )
322 {
323 if( b2[i][j] && start2==-1 ) start2 = i;
324 if( b2[i][j] ) end2 = i+1;
325 }
326 }
327 if( start1==end1 || start2==end2 || start1>=end2 || start2>=end1 ) return 0.;
328 int start = std::max< int >( start1 , start2 ) , end = std::min< int >( end1 , end2 );
329 memset( sums , 0 , sizeof( sums ) );
330
331 // Iterate over the support
332 for( int i=start ; i<end ; i++ )
333 // Iterate over all pairs of elements within a node
334 for( int j=0 ; j<=_Degree1 ; j++ ) for( int k=0 ; k<=_Degree2 ; k++ )
335 // Accumulate the product of the coefficients
336 sums[j][k] += db1[i][j] * db2[i][k];
337
338 double _dot = 0;
339 {
340 double integrals[ _Degree1+1 ][ _Degree2+1 ];
341 SetBSplineElementIntegrals< _Degree1 , _Degree2 >( integrals );
342 for( int j=0 ; j<=_Degree1 ; j++ ) for( int k=0 ; k<=_Degree2 ; k++ ) _dot += integrals[j][k] * sums[j][k];
343 }
344 _dot /= b1.denominator;
345 _dot /= b2.denominator;
346 return ( !D1 && !D2 ) ? _dot / (1<<depth) : _dot * ( 1<<( depth*(D1+D2-1) ) );
347 }
348 template< unsigned int FEMSig1 , unsigned int FEMSig2 >
349 template< unsigned int D1 , unsigned int D2 , unsigned int _D1 , unsigned int _D2 , class Integrator >
350 void BSplineIntegrationData< FEMSig1 , FEMSig2 >::IntegratorSetter< D1 , D2 , _D1 , _D2 , Integrator >::Set2D( Integrator& integrator , int depth )
351 {
352 IntegratorSetter< D1-1 , D2 , _D1 , _D2 , Integrator >::Set2D( integrator , depth );
353 IntegratorSetter< D1 , D2 , _D1 , _D2 , Integrator >::Set1D( integrator , depth );
354 }
355 template< unsigned int FEMSig1 , unsigned int FEMSig2 >
356 template< unsigned int D1 , unsigned int D2 , unsigned int _D1 , unsigned int _D2 , class Integrator >
357 void BSplineIntegrationData< FEMSig1 , FEMSig2 >::IntegratorSetter< D1 , D2 , _D1 , _D2 , Integrator >::Set1D( Integrator& integrator , int depth )
358 {
359 IntegratorSetter< D1 , D2-1 , _D1 , _D2 , Integrator >::Set1D( integrator , depth );
360 _IntegratorSetter< D1 , D2 , _D1 , _D2 >::Set( integrator , depth );
361 }
362 template< unsigned int FEMSig1 , unsigned int FEMSig2 >
363 template< unsigned int D2 , unsigned int _D1 , unsigned int _D2 , class Integrator >
364 void BSplineIntegrationData< FEMSig1 , FEMSig2 >::IntegratorSetter< 0 , D2 , _D1 , _D2 , Integrator >::Set2D( Integrator& integrator , int depth )
365 {
366 IntegratorSetter< 0 , D2 , _D1 , _D2 , Integrator >::Set1D( integrator , depth );
367 }
368 template< unsigned int FEMSig1 , unsigned int FEMSig2 >
369 template< unsigned int D2 , unsigned int _D1 , unsigned int _D2 , class Integrator >
370 void BSplineIntegrationData< FEMSig1 , FEMSig2 >::IntegratorSetter< 0 , D2 , _D1 , _D2 , Integrator >::Set1D( Integrator& integrator , int depth )
371 {
372 IntegratorSetter< 0 , D2-1 , _D1 , _D2 , Integrator >::Set1D( integrator , depth );
373 _IntegratorSetter< 0 , D2 , _D1 , _D2 >::Set( integrator , depth );
374 }
375 template< unsigned int FEMSig1 , unsigned int FEMSig2 >
376 template< unsigned int D1 , unsigned int _D1 , unsigned int _D2 , class Integrator >
377 void BSplineIntegrationData< FEMSig1 , FEMSig2 >::IntegratorSetter< D1 , 0 , _D1 , _D2 , Integrator >::Set2D( Integrator& integrator , int depth )
378 {
379 IntegratorSetter< D1-1 , 0 , _D1 , _D2 , Integrator >::Set2D( integrator , depth );
380 IntegratorSetter< D1 , 0 , _D1 , _D2 , Integrator >::Set1D( integrator , depth );
381 }
382 template< unsigned int FEMSig1 , unsigned int FEMSig2 >
383 template< unsigned int D1 , unsigned int _D1 , unsigned int _D2 , class Integrator >
384 void BSplineIntegrationData< FEMSig1 , FEMSig2 >::IntegratorSetter< D1 , 0 , _D1 , _D2 , Integrator >::Set1D( Integrator& integrator , int depth )
385 {
386 _IntegratorSetter< D1 , 0 , _D1 , _D2 >::Set( integrator , depth );
387 }
388 template< unsigned int FEMSig1 , unsigned int FEMSig2 >
389 template< unsigned int _D1 , unsigned int _D2 , class Integrator >
390 void BSplineIntegrationData< FEMSig1 , FEMSig2 >::IntegratorSetter< 0 , 0 , _D1 , _D2 , Integrator >::Set2D( Integrator& integrator , int depth )
391 {
392 IntegratorSetter< 0 , 0 , _D1 , _D2 , Integrator >::Set1D( integrator , depth );
393 }
394 template< unsigned int FEMSig1 , unsigned int FEMSig2 >
395 template< unsigned int _D1 , unsigned int _D2 , class Integrator >
396 void BSplineIntegrationData< FEMSig1 , FEMSig2 >::IntegratorSetter< 0 , 0 , _D1 , _D2 , Integrator >::Set1D( Integrator& integrator , int depth )
397 {
398 _IntegratorSetter< 0 , 0 , _D1 , _D2 >::Set( integrator , depth );
399 }
400
401 template< unsigned int FEMSig1 , unsigned int FEMSig2 >
402 template< unsigned int D1 , unsigned int D2 , unsigned int _D1 , unsigned int _D2 >
403 void BSplineIntegrationData< FEMSig1 , FEMSig2 >::_IntegratorSetter< D1 , D2 , _D1 , _D2 >::Set( typename FunctionIntegrator::template Integrator< _D1 , _D2 >& integrator , int depth )
404 {
405 for( int i=0 ; i<IndexSize ; i++ ) for( int j=BSplineOverlapSizes< Degree1 , Degree2 >::OverlapStart ; j<=BSplineOverlapSizes< Degree1 , Degree2 >::OverlapEnd ; j++ )
406 {
407 int ii = IndexToOffset( depth , i );
408 integrator._ccIntegrals[D1][D2][i][j-BSplineOverlapSizes< Degree1 , Degree2 >::OverlapStart] = Dot< D1 , D2 >( depth , ii , depth , ii+j );
409 }
410 }
411 template< unsigned int FEMSig1 , unsigned int FEMSig2 >
412 template< unsigned int D1 , unsigned int D2 , unsigned int _D1 , unsigned int _D2 >
413 void BSplineIntegrationData< FEMSig1 , FEMSig2 >::_IntegratorSetter< D1 , D2 , _D1 , _D2 >::Set( typename FunctionIntegrator::template ChildIntegrator< _D1 , _D2 >& integrator , int pDepth )
414 {
415 for( int i=0 ; i<IndexSize ; i++ ) for( int j=BSplineOverlapSizes< Degree1 , Degree2 >::ChildOverlapStart ; j<=BSplineOverlapSizes< Degree1 , Degree2 >::ChildOverlapEnd ; j++ )
416 {
417 int ii = IndexToOffset( pDepth , i );
418 integrator._pcIntegrals[D1][D2][i][j-BSplineOverlapSizes< Degree1 , Degree2 >::ChildOverlapStart] = Dot< D1 , D2 >( pDepth , ii , pDepth+1 , 2*ii+j );
419 }
420 }
421
422 template< unsigned int FEMSig1 , unsigned int FEMSig2 >
423 template< unsigned int D1 , unsigned int D2 >
424 void BSplineIntegrationData< FEMSig1 , FEMSig2 >::SetIntegrator( typename FunctionIntegrator::template Integrator< D1 , D2 >& integrator , int depth )
425 {
426 integrator._depth = depth;
427 IntegratorSetter< D1 , D2 , D1 , D2 , typename FunctionIntegrator::template Integrator< D1 , D2 > >::Set2D( integrator , depth );
428 }
429 template< unsigned int FEMSig1 , unsigned int FEMSig2 >
430 template< unsigned int D1 , unsigned int D2 >
431 void BSplineIntegrationData< FEMSig1 , FEMSig2 >::SetChildIntegrator( typename FunctionIntegrator::template ChildIntegrator< D1 , D2 >& integrator , int parentDepth )
432 {
433 integrator._parentDepth = parentDepth;
434 IntegratorSetter< D1 , D2 , D1 , D2 , typename FunctionIntegrator::template ChildIntegrator< D1 , D2 > >::Set2D( integrator , parentDepth );
435 }
436 template< unsigned int FEMSig1 , unsigned int FEMSig2 >
437 template< unsigned int D1 , unsigned int D2 >
438 double BSplineIntegrationData< FEMSig1 , FEMSig2 >::FunctionIntegrator::Integrator< D1 , D2 >::dot( int off1 , int off2 , int d1 , int d2 ) const
439 {
440 int d = off2-off1;
441 if( BSplineEvaluationData< FEMSig1 >::OutOfBounds( _depth , off1 ) || BSplineEvaluationData< FEMSig2 >::OutOfBounds( _depth , off2 ) || d<BSplineOverlapSizes< Degree1 , Degree2 >::OverlapStart || d>BSplineOverlapSizes< Degree1 , Degree2 >::OverlapEnd ) return 0;
442 return _ccIntegrals[d1][d2][ OffsetToIndex( _depth , off1 ) ][d-BSplineOverlapSizes< Degree1 , Degree2 >::OverlapStart];
443 }
444 template< unsigned int FEMSig1 , unsigned int FEMSig2 >
445 template< unsigned int D1 , unsigned int D2 >
446 double BSplineIntegrationData< FEMSig1 , FEMSig2 >::FunctionIntegrator::ChildIntegrator< D1 , D2 >::dot( int off1 , int off2 , int d1 , int d2 ) const
447 {
448 int d = off2-2*off1;
449 if( BSplineEvaluationData< FEMSig1 >::OutOfBounds( _parentDepth , off1 ) || BSplineEvaluationData< FEMSig2 >::OutOfBounds( _parentDepth+1 , off2 ) || d<BSplineOverlapSizes< Degree1 , Degree2 >::ChildOverlapStart || d>BSplineOverlapSizes< Degree1 , Degree2 >::ChildOverlapEnd ) return 0;
450 return _pcIntegrals[d1][d2][ OffsetToIndex( _parentDepth , off1 ) ][d-BSplineOverlapSizes< Degree1 , Degree2 >::ChildOverlapStart];
451 }
452
453 /////////////////
454 // BSplineData //
455 /////////////////
456 #define PR_MODULO( A , B ) ( (A)<0 ? ( (B)-((-(A))%(B)) ) % (B) : (A) % (B) )
457
458 template< unsigned int FEMSig , unsigned int D >
459 BSplineData< FEMSig , D >::BSplineComponents::BSplineComponents( int depth , int offset )
460 {
461 static const int _Degree = Degree;
462 int res = 1<<depth;
463 BSplineElements< Degree > elements( res , offset , FEMSignature< FEMSig >::BType );
464
465 // The first index is the position, the second is the element type
466 Polynomial< Degree > components[Degree+1][Degree+1];
467 // Generate the elements that can appear in the base function corresponding to the base function at (depth,offset) = (0,0)
468 for( int d=0 ; d<=Degree ; d++ ) for( int dd=0 ; dd<=Degree ; dd++ ) components[d][dd] = Polynomial< Degree >::BSplineComponent( _Degree-dd ).shift( -( (_Degree+1)/2 ) + d );
469
470 // Now adjust to the desired depth and offset
471 double width = 1. / res;
472 for( int d=0 ; d<=Degree ; d++ ) for( int dd=0 ; dd<=Degree ; dd++ ) components[d][dd] = components[d][dd].scale( width ).shift( width*offset );
473
474 // Now write in the polynomials
475 for( int d=0 ; d<=Degree ; d++ )
476 {
477 int idx = offset + BSplineSupportSizes< Degree >::SupportStart + d;
478 _polys[d][0] = Polynomial< Degree >();
479
480 if( idx>=0 && idx<res ) for( int dd=0 ; dd<=Degree ; dd++ ) _polys[d][0] += components[d][dd] * ( ( double )( elements[idx][dd] ) ) / elements.denominator;
481 }
482 for( int d=1 ; d<=D ; d++ ) for( int dd=0 ; dd<=Degree ; dd++ ) _polys[dd][d] = _polys[dd][d-1].derivative();
483 }
484
485 template< unsigned int FEMSig , unsigned int D >
486 int BSplineData< FEMSig , D >::RemapOffset( int depth , int offset , bool& reflect )
487 {
488 const int I = ( Degree&1 ) ? 0 : 1;
489 if( FEMSignature< FEMSig >::BType==BOUNDARY_FREE ){ reflect = false ; return offset; }
490 int dim = BSplineEvaluationData< FEMDegreeAndBType< Degree , BOUNDARY_NEUMANN >::Signature >::End( depth ) - BSplineEvaluationData< FEMDegreeAndBType< Degree , BOUNDARY_NEUMANN >::Signature >::Begin( depth );
491 offset = PR_MODULO( offset , 2*(dim-1+I) );
492 reflect = offset>=dim;
493 if( reflect ) return 2*(dim-1+I) - (offset+I);
494 else return offset;
495 }
496 #undef PR_MODULO
497
498 template< unsigned int FEMSig , unsigned int D >
499 BSplineData< FEMSig , D >::BSplineData( void )
500 {
501 _maxDepth = 0;
502 _evaluators = NullPointer( SparseBSplineEvaluator );
503 }
504 template< unsigned int FEMSig , unsigned int D >
505 void BSplineData< FEMSig , D >::reset( int maxDepth )
506 {
507 if( _evaluators ) DeletePointer( _evaluators );
508
509 _maxDepth = maxDepth;
510 _evaluators = NewPointer< SparseBSplineEvaluator >( _maxDepth+1 );
511 for( unsigned int d=0 ; d<=_maxDepth ; d++ ) _evaluators[d].init( d );
512 }
513 template< unsigned int FEMSig , unsigned int D >
514 BSplineData< FEMSig , D >::BSplineData( int maxDepth )
515 {
516 _evaluators = NullPointer( SparseBSplineEvaluator );
517 reset( maxDepth );
518 }
519 template< unsigned int FEMSig , unsigned int D >
520 BSplineData< FEMSig , D >::~BSplineData( void )
521 {
522 DeletePointer( _evaluators );
523 }
524
525 /////////////////////
526 // BSplineElements //
527 /////////////////////
528 template< unsigned int Degree >
529 BSplineElements< Degree >::BSplineElements( int res , int offset , BoundaryType bType )
530 {
531 denominator = 1;
532 std::vector< BSplineElementCoefficients< Degree > >::resize( res , BSplineElementCoefficients< Degree >() );
533
534 // If we have primal dirichlet constraints, the boundary functions are necessarily zero
535 if( _Primal && bType==BOUNDARY_DIRICHLET && !(offset%res) ) return;
536
537 // Construct the B-Spline
538 for( int i=0 ; i<=Degree ; i++ )
539 {
540 int idx = -_Off + offset + i;
541 if( idx>=0 && idx<res ) (*this)[idx][i] = 1;
542 }
543 if( bType!=BOUNDARY_FREE )
544 {
545 // Fold in the periodic instances (which cancels the negation)
546 _addPeriodic< true >( _RotateLeft ( offset , res ) , false ) , _addPeriodic< false >( _RotateRight( offset , res ) , false );
547
548 // Recursively fold in the boundaries
549 if( _Primal && !(offset%res) ) return;
550
551 // Fold in the reflected instance (which may require negation)
552 _addPeriodic< true >( _ReflectLeft( offset , res ) , bType==BOUNDARY_DIRICHLET ) , _addPeriodic< false >( _ReflectRight( offset , res ) , bType==BOUNDARY_DIRICHLET );
553 }
554 }
555 template< unsigned int Degree > int BSplineElements< Degree >::_ReflectLeft ( int offset , int res ){ return (Degree&1) ? -offset : -1-offset; }
556 template< unsigned int Degree > int BSplineElements< Degree >::_ReflectRight( int offset , int res ){ return (Degree&1) ? 2*res-offset : 2*res-1-offset; }
557 template< unsigned int Degree > int BSplineElements< Degree >::_RotateLeft ( int offset , int res ){ return offset-2*res; }
558 template< unsigned int Degree > int BSplineElements< Degree >::_RotateRight ( int offset , int res ){ return offset+2*res; }
559
560 template< unsigned int Degree >
561 template< bool Left >
562 void BSplineElements< Degree >::_addPeriodic( int offset , bool negate )
563 {
564 int res = int( std::vector< BSplineElementCoefficients< Degree > >::size() );
565 bool set = false;
566 // Add in the corresponding B-spline elements (possibly negated)
567 for( int i=0 ; i<=Degree ; i++ )
568 {
569 int idx = -_Off + offset + i;
570 if( idx>=0 && idx<res ) (*this)[idx][i] += negate ? -1 : 1 , set = true;
571 }
572 // If there is a change for additional overlap, give it a go
573 if( set ) _addPeriodic< Left >( Left ? _RotateLeft( offset , res ) : _RotateRight( offset , res ) , negate );
574 }
575 template< unsigned int Degree >
576 void BSplineElements< Degree >::upSample( BSplineElements< Degree >& high ) const
577 {
578 int bCoefficients[ BSplineSupportSizes< Degree >::UpSampleSize ];
579 Polynomial< Degree+1 >::BinomialCoefficients( bCoefficients );
580
581 high.resize( std::vector< BSplineElementCoefficients< Degree > >::size()*2 );
582 high.assign( high.size() , BSplineElementCoefficients< Degree >() );
583 // [NOTE] We have flipped the order of the B-spline elements
584 for( int i=0 ; i<int(std::vector< BSplineElementCoefficients< Degree > >::size()) ; i++ ) for( int j=0 ; j<=Degree ; j++ )
585 {
586 // At index I , B-spline element J corresponds to a B-spline centered at:
587 // I - SupportStart - J
588 int idx = i - BSplineSupportSizes< Degree >::SupportStart - j;
589 for( int k=BSplineSupportSizes< Degree >::UpSampleStart ; k<=BSplineSupportSizes< Degree >::UpSampleEnd ; k++ )
590 {
591 // Index idx at the coarser resolution gets up-sampled into indices:
592 // 2*idx + [UpSampleStart,UpSampleEnd]
593 // at the finer resolution
594 int _idx = 2*idx + k;
595 // Compute the index of the B-spline element relative to 2*i and 2*i+1
596 int _j1 = -_idx + 2*i - BSplineSupportSizes< Degree >::SupportStart , _j2 = -_idx + 2*i + 1 - BSplineSupportSizes< Degree >::SupportStart;
597 if( _j1>=0 && _j1<=Degree ) high[2*i+0][_j1] += (*this)[i][j] * bCoefficients[k-BSplineSupportSizes< Degree >::UpSampleStart];
598 if( _j2>=0 && _j2<=Degree ) high[2*i+1][_j2] += (*this)[i][j] * bCoefficients[k-BSplineSupportSizes< Degree >::UpSampleStart];
599 }
600 }
601 high.denominator = denominator<<Degree;
602 }
603
604 template< unsigned int Degree >
605 template< unsigned int D >
606 void BSplineElements< Degree >::differentiate( BSplineElements< Degree-D >& d ) const{ Differentiator< Degree , Degree-D >::Differentiate( *this , d ); }
607 template< unsigned int Degree , unsigned int DDegree >
608 void Differentiator< Degree , DDegree >::Differentiate( const BSplineElements< Degree >& bse , BSplineElements< DDegree >& dbse )
609 {
610 BSplineElements< Degree-1 > _dbse;
611 _dbse.resize( bse.size() );
612 _dbse.assign( _dbse.size() , BSplineElementCoefficients< Degree-1 >() );
613 for( int i=0 ; i<(int)bse.size() ; i++ ) for( int j=0 ; j<=Degree ; j++ )
614 {
615 if( j-1>=0 ) _dbse[i][j-1] -= bse[i][j];
616 if( j<Degree ) _dbse[i][j ] += bse[i][j];
617 }
618 _dbse.denominator = bse.denominator;
619 return Differentiator< Degree-1 , DDegree >::Differentiate( _dbse , dbse );
620 }
621 template< unsigned int Degree >
622 void Differentiator< Degree , Degree >::Differentiate( const BSplineElements< Degree >& bse , BSplineElements< Degree >& dbse ){ dbse = bse; }
623
624 // If we were really good, we would implement this integral table to store
625 // rational values to improve precision...
626 template< unsigned int Degree1 , unsigned int Degree2 >
627 void SetBSplineElementIntegrals( double integrals[Degree1+1][Degree2+1] )
628 {
629 for( int i=0 ; i<=Degree1 ; i++ )
630 {
631 Polynomial< Degree1 > p1 = Polynomial< Degree1 >::BSplineComponent( Degree1-i );
632 for( int j=0 ; j<=Degree2 ; j++ )
633 {
634 Polynomial< Degree2 > p2 = Polynomial< Degree2 >::BSplineComponent( Degree2-j );
635 integrals[i][j] = ( p1 * p2 ).integral( 0 , 1 );
636 }
637 }
638 }