Debian Codegraph @

src/thirdparty/PoissonRecon/BSplineData.h

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 #ifndef BSPLINE_DATA_INCLUDED
30 #define BSPLINE_DATA_INCLUDED
31
32 #include <string.h>
33
34 #include "BinaryNode.h"
35 #include "PPolynomial.h"
36 #include "Array.h"
37
38 namespace PoissonRecon
39 {
40 enum BoundaryType
41 {
42 BOUNDARY_FREE ,
43 BOUNDARY_DIRICHLET ,
44 BOUNDARY_NEUMANN ,
45 BOUNDARY_COUNT
46 };
47 static const char* BoundaryNames[] = { "free" , "Dirichlet" , "Neumann" };
48 template< BoundaryType BType > inline bool HasPartitionOfUnity( void ){ return BType!=BOUNDARY_DIRICHLET; }
49 inline bool HasPartitionOfUnity( BoundaryType bType ){ return bType!=BOUNDARY_DIRICHLET; }
50 template< BoundaryType BType , unsigned int D > struct DerivativeBoundary{};
51 template< unsigned int D > struct DerivativeBoundary< BOUNDARY_FREE , D >{ static const BoundaryType BType = BOUNDARY_FREE; };
52 template< unsigned int D > struct DerivativeBoundary< BOUNDARY_DIRICHLET , D >{ static const BoundaryType BType = DerivativeBoundary< BOUNDARY_NEUMANN , D-1 >::BType; };
53 template< unsigned int D > struct DerivativeBoundary< BOUNDARY_NEUMANN , D >{ static const BoundaryType BType = DerivativeBoundary< BOUNDARY_DIRICHLET , D-1 >::BType; };
54 template< > struct DerivativeBoundary< BOUNDARY_FREE , 0 >{ static const BoundaryType BType = BOUNDARY_FREE; };
55 template< > struct DerivativeBoundary< BOUNDARY_DIRICHLET , 0 >{ static const BoundaryType BType = BOUNDARY_DIRICHLET; };
56 template< > struct DerivativeBoundary< BOUNDARY_NEUMANN , 0 >{ static const BoundaryType BType = BOUNDARY_NEUMANN; };
57
58
59 // Generate a single signature that combines the degree, boundary type, and number of supported derivatives
60 template< unsigned int Degree , BoundaryType BType=BOUNDARY_FREE > struct FEMDegreeAndBType { static const unsigned int Signature = Degree * BOUNDARY_COUNT + BType; };
61
62 // Extract the degree and boundary type from the signaure
63 template< unsigned int Signature > struct FEMSignature
64 {
65 static const unsigned int Degree = ( Signature / BOUNDARY_COUNT );
66 static const BoundaryType BType = (BoundaryType)( Signature % BOUNDARY_COUNT );
67 template< unsigned int D=1 >
68 static constexpr typename std::enable_if< (Degree>=D) , unsigned int >::type DSignature( void ){ return FEMDegreeAndBType< Degree-D , DerivativeBoundary< BType , D >::BType >::Signature; }
69 };
70
71 inline unsigned int FEMSignatureDegree( unsigned int signature ){ return signature / BOUNDARY_COUNT; }
72 inline BoundaryType FEMSignatureBType ( unsigned int signature ){ return (BoundaryType)( signature % BOUNDARY_COUNT ); }
73
74 static const unsigned int FEMTrivialSignature = FEMDegreeAndBType< 0 , BOUNDARY_FREE >::Signature;
75
76 // This class represents a function that is a linear combination of B-spline elements,
77 // with the coeff member indicating how much of each element is present.
78 // [WARNING] The ordering of B-spline elements is in the opposite order from that returned by Polynomial::BSplineComponent
79 template< unsigned int Degree >
80 struct BSplineElementCoefficients
81 {
82 int coeffs[Degree+1];
83 BSplineElementCoefficients( void ){ memset( coeffs , 0 , sizeof( coeffs ) ); }
84 int& operator[]( int idx ){ return coeffs[idx]; }
85 const int& operator[]( int idx ) const { return coeffs[idx]; }
86 };
87
88 // This class represents a function on the the interval, partitioned into "res" blocks.
89 // On each block, the function is a degree-Degree polynomial, represented by the coefficients
90 // in the associated BSplineElementCoefficients.
91 // [NOTE] This representation of a function is agnostic to the type of boundary conditions (though the constructor is not).
92 template< unsigned int Degree >
93 struct BSplineElements : public std::vector< BSplineElementCoefficients< Degree > >
94 {
95 static const bool _Primal = (Degree&1)==1;
96 static const int _Off = (Degree+1)/2;
97 static int _ReflectLeft ( int offset , int res );
98 static int _ReflectRight( int offset , int res );
99 static int _RotateLeft ( int offset , int res );
100 static int _RotateRight ( int offset , int res );
101 template< bool Left > void _addPeriodic( int offset , bool negate );
102 public:
103 // Coefficients are ordered as "/" "-" "\"
104 // [WARNING] This is the opposite of the order in Polynomial::BSplineComponent
105 int denominator;
106
107 BSplineElements( void ) { denominator = 1; }
108 BSplineElements( int res , int offset , BoundaryType bType );
109
110 void upSample( BSplineElements& high ) const;
111 template< unsigned int D >
112 void differentiate( BSplineElements< Degree-D >& d ) const;
113
114 void print( FILE* fp=stdout ) const
115 {
116 for( int i=0 ; i<std::vector< BSplineElementCoefficients< Degree > >::size() ; i++ )
117 {
118 printf( "%d]" , i );
119 for( int j=0 ; j<=Degree ; j++ ) printf( " %d" , (*this)[i][j] );
120 printf( " (%d)\n" , denominator );
121 }
122 }
123 Polynomial< Degree > polynomial( int idx ) const
124 {
125 int res = (int)std::vector< BSplineElementCoefficients< Degree > >::size();
126 Polynomial< Degree > P;
127 if( idx>=0 && idx<res ) for( int d=0 ; d<=Degree ; d++ ) P += Polynomial< Degree >::BSplineComponent( Degree-d ).scale( 1./res ).shift( (idx+0.)/res ) * ( (*this)[idx][d] );
128 return P / denominator;
129 }
130 PPolynomial< Degree > pPolynomial( void ) const
131 {
132 int res = (int)std::vector< BSplineElementCoefficients< Degree > >::size();
133 PPolynomial< Degree > P;
134 P.polyCount = res + 1;
135 P.polys = AllocPointer< StartingPolynomial< Degree > >( P.polyCount );
136 for( int i=0 ; i<P.polyCount ; i++ ) P.polys[i].start = (i+0.) / res , P.polys[i].p = polynomial(i);
137 for( int i=res ; i>=1 ; i-- ) P.polys[i].p -= P.polys[i-1].p;
138 return P.compress(0);
139 }
140 };
141
142 template< unsigned int Degree , unsigned int DDegree > struct Differentiator { static void Differentiate( const BSplineElements< Degree >& bse , BSplineElements< DDegree >& dbse ); };
143 template< unsigned int Degree > struct Differentiator< Degree , Degree >{ static void Differentiate( const BSplineElements< Degree >& bse , BSplineElements< Degree >& dbse ); };
144
145 // Note that these bounds are inclusive -- [s,e]
146 #define PR_BSPLINE_SET_BOUNDS( name , s , e ) \
147 static const int name ## Start = (s); \
148 static const int name ## End = (e); \
149 static const unsigned int name ## Size = (e)-(s)+1
150
151 // Assumes that x is non-negative
152 #define PR__FLOOR_OF_HALF( x ) ( (x) >>1 )
153 #define PR__CEIL_OF_HALF( x ) ( ( (x)+1 )>>1 )
154 // Done with the assumption
155 #define PR_FLOOR_OF_HALF( x ) ( (x)<0 ? - PR__CEIL_OF_HALF( -(x) ) : PR__FLOOR_OF_HALF( x ) )
156 #define PR_CEIL_OF_HALF( x ) ( (x)<0 ? - PR__FLOOR_OF_HALF( -(x) ) : PR__CEIL_OF_HALF( x ) )
157 #define PR_SMALLEST_INTEGER_LARGER_THAN_HALF( x ) ( PR_CEIL_OF_HALF( (x)+1 ) )
158 #define PR_LARGEST_INTEGER_SMALLER_THAN_HALF( x ) ( PR_FLOOR_OF_HALF( (x)-1 ) )
159 #define PR_SMALLEST_INTEGER_LARGER_THAN_OR_EQUAL_TO_HALF( x ) ( PR_CEIL_OF_HALF( x ) )
160 #define PR_LARGEST_INTEGER_SMALLER_THAN_OR_EQUAL_TO_HALF( x ) ( PR_FLOOR_OF_HALF( x ) )
161
162 template< unsigned int Degree >
163 struct BSplineSupportSizes
164 {
165 protected:
166 static const int _Degree = Degree;
167 public:
168 inline static int Nodes( int depth ){ return ( 1<<depth ) + ( Degree&1 ); }
169 inline static bool OutOfBounds( int depth , int offset ){ return offset>=0 || offset<Nodes(depth); }
170 // An index is interiorly supported if its support is in the range [0,1<<depth)
171 inline static void InteriorSupportedSpan( int depth , int& begin , int& end ){ begin = -SupportStart , end = (1<<depth)-SupportEnd; }
172 inline static bool IsInteriorlySupported( int depth , int offset ){ return offset+SupportStart>=0 && offset+SupportEnd<(1<<depth); }
173
174 // If the degree is even, we use a dual basis and functions are centered at the center of the interval
175 // It the degree is odd, we use a primal basis and functions are centered at the left end of the interval
176 // The function at index I is supported in:
177 // Support( I ) = [ I - (Degree+1-Inset)/2 , I + (Degree+1+Inset)/2 ]
178 // [NOTE] The value of ( Degree + 1 +/- Inset ) is always even
179 static const int Inset = (Degree&1) ? 0 : 1;
180 PR_BSPLINE_SET_BOUNDS( Support , -( (_Degree+1)/2 ) , _Degree/2 );
181 PR_BSPLINE_SET_BOUNDS( ChildSupport , 2*SupportStart , 2*(SupportEnd+1)-1 );
182 PR_BSPLINE_SET_BOUNDS( Corner , SupportStart+1 , SupportEnd );
183 PR_BSPLINE_SET_BOUNDS( ChildCorner , 2*SupportStart+1 , 2*SupportEnd + 1 );
184 PR_BSPLINE_SET_BOUNDS( BCorner , CornerStart-1 , CornerEnd+1 );
185 PR_BSPLINE_SET_BOUNDS( ChildBCorner , ChildCornerStart-1 , ChildCornerEnd+1 );
186
187 // Setting I=0, we are looking for the smallest/largest integers J such that:
188 // Support( 0 ) CONTAINS Support( J )
189 // <=> [-(Degree+1-Inset) , (Degree+1+Inset) ] CONTAINS [ J-(Degree+1-Inset)/2 , J+(Degree+1+Inset)/2 ]
190 // Which is the same as the smallest/largest integers J such that:
191 // J - (Degree+1-Inset)/2 >= -(Degree+1-Inset) | J + (Degree+1+Inset)/2 <= (Degree+1+Inset)
192 // <=> J >= -(Degree+1-Inset)/2 | J <= (Degree+1+Inset)/2
193 PR_BSPLINE_SET_BOUNDS( UpSample , - ( _Degree + 1 - Inset ) / 2 , ( _Degree + 1 + Inset ) /2 );
194
195 // Setting I=0/1, we are looking for the smallest/largest integers J such that:
196 // Support( J ) CONTAINS Support( 0/1 )
197 // <=> [ 2*J - (Degree+1-Inset) , 2*J + (Degree+1+Inset) ] CONTAINS [ 0/1 - (Degree+1-Inset)/2 , 0/1 + (Degree+1+Inset)/2 ]
198 // Which is the same as the smallest/largest integers J such that:
199 // 2*J + (Degree+1+Inset) >= 0/1 + (Degree+1+Inset)/2 | 2*J - (Degree+1-Inset) <= 0/1 - (Degree+1-Inset)/2
200 // <=> 2*J >= 0/1 - (Degree+1+Inset)/2 | 2*J <= 0/1 + (Degree+1-Inset)/2
201 PR_BSPLINE_SET_BOUNDS( DownSample0 , PR_SMALLEST_INTEGER_LARGER_THAN_OR_EQUAL_TO_HALF( 0 - ( _Degree + 1 + Inset ) / 2 ) , PR_LARGEST_INTEGER_SMALLER_THAN_OR_EQUAL_TO_HALF( 0 + ( _Degree + 1 - Inset ) / 2 ) );
202 PR_BSPLINE_SET_BOUNDS( DownSample1 , PR_SMALLEST_INTEGER_LARGER_THAN_OR_EQUAL_TO_HALF( 1 - ( _Degree + 1 + Inset ) / 2 ) , PR_LARGEST_INTEGER_SMALLER_THAN_OR_EQUAL_TO_HALF( 1 + ( _Degree + 1 - Inset ) / 2 ) );
203 static const int DownSampleStart[] , DownSampleEnd[];
204 static const unsigned int DownSampleSize[];
205 };
206 template< unsigned int Degree > const int BSplineSupportSizes< Degree >::DownSampleStart[] = { DownSample0Start , DownSample1Start };
207 template< unsigned int Degree > const int BSplineSupportSizes< Degree >::DownSampleEnd [] = { DownSample0End , DownSample1End };
208 template< unsigned int Degree > const unsigned int BSplineSupportSizes< Degree >::DownSampleSize [] = { DownSample0Size , DownSample1Size };
209
210 template< unsigned int Degree1 , unsigned int Degree2=Degree1 >
211 struct BSplineOverlapSizes
212 {
213 protected:
214 static const int _Degree1 = Degree1;
215 static const int _Degree2 = Degree2;
216 public:
217 typedef BSplineSupportSizes< Degree1 > EData1;
218 typedef BSplineSupportSizes< Degree2 > EData2;
219 PR_BSPLINE_SET_BOUNDS( Overlap , EData1:: SupportStart - EData2::SupportEnd , EData1:: SupportEnd - EData2::SupportStart );
220 PR_BSPLINE_SET_BOUNDS( ChildOverlap , EData1::ChildSupportStart - EData2::SupportEnd , EData1::ChildSupportEnd - EData2::SupportStart );
221 PR_BSPLINE_SET_BOUNDS( OverlapSupport , OverlapStart + EData2::SupportStart , OverlapEnd + EData2::SupportEnd );
222 PR_BSPLINE_SET_BOUNDS( ChildOverlapSupport , ChildOverlapStart + EData2::SupportStart , ChildOverlapEnd + EData2::SupportEnd );
223
224 // Setting I=0/1, we are looking for the smallest/largest integers J such that:
225 // Support( 2*J ) * 2 INTERSECTION Support( 0/1 ) NON-EMPTY
226 // <=> [ 2*J - (Degree2+1-Inset2) , 2*J + (Degree2+1+Inset2) ] INTERSECTION [ 0/1 - (Degree1+1-Inset1)/2 , 0/1 + (Degree1+1+Inset1)/2 ] NON-EMPTY
227 // Which is the same as the smallest/largest integers J such that:
228 // 0/1 - (Degree1+1-Inset1)/2 < 2*J + (Degree2+1+Inset2) | 0/1 + (Degree1+1+Inset1)/2 > 2*J - (Degree2+1-Inset2)
229 // <=> 2*J > 0/1 - ( 2*Degree2 + Degree1 + 3 + 2*Inset2 - Inset1 ) / 2 | 2*J < 0/1 + ( 2*Degree2 + Degree1 + 3 - 2*Inset2 + Inset1 ) / 2
230 PR_BSPLINE_SET_BOUNDS( ParentOverlap0 , PR_SMALLEST_INTEGER_LARGER_THAN_HALF( 0 - ( 2*_Degree2 + _Degree1 + 3 + 2*EData2::Inset - EData1::Inset ) / 2 ) , PR_LARGEST_INTEGER_SMALLER_THAN_HALF( 0 + ( 2*_Degree2 + _Degree1 + 3 - 2*EData2::Inset + EData1::Inset ) / 2 ) );
231 PR_BSPLINE_SET_BOUNDS( ParentOverlap1 , PR_SMALLEST_INTEGER_LARGER_THAN_HALF( 1 - ( 2*_Degree2 + _Degree1 + 3 + 2*EData2::Inset - EData1::Inset ) / 2 ) , PR_LARGEST_INTEGER_SMALLER_THAN_HALF( 1 + ( 2*_Degree2 + _Degree1 + 3 - 2*EData2::Inset + EData1::Inset ) / 2 ) );
232 static const int ParentOverlapStart[] , ParentOverlapEnd[] , ParentOverlapSize[];
233 };
234 template< unsigned int Degree1 , unsigned int Degree2 > const int BSplineOverlapSizes< Degree1 , Degree2 >::ParentOverlapStart[] = { ParentOverlap0Start , ParentOverlap1Start };
235 template< unsigned int Degree1 , unsigned int Degree2 > const int BSplineOverlapSizes< Degree1 , Degree2 >::ParentOverlapEnd [] = { ParentOverlap0End , ParentOverlap1End };
236 template< unsigned int Degree1 , unsigned int Degree2 > const int BSplineOverlapSizes< Degree1 , Degree2 >::ParentOverlapSize [] = { ParentOverlap0Size , ParentOverlap1Size };
237
238 struct EvaluationData
239 {
240 struct CornerEvaluator
241 {
242 virtual double value( int fIdx , int cIdx , int d ) const = 0;
243 virtual void set( int depth ) = 0;
244 virtual ~CornerEvaluator( void ){}
245 };
246 struct CenterEvaluator
247 {
248 virtual double value( int fIdx , int cIdx , int d ) const = 0;
249 virtual void set( int depth ) = 0;
250 virtual ~CenterEvaluator( void ){}
251 };
252 struct UpSampleEvaluator
253 {
254 virtual double value( int pIdx , int cIdx ) const = 0;
255 virtual void set( int depth ) = 0;
256 virtual ~UpSampleEvaluator( void ){}
257 };
258 };
259
260 template< unsigned int FEMSig >
261 class BSplineEvaluationData
262 {
263 public:
264 static const unsigned int Degree = FEMSignature< FEMSig >::Degree;
265 static const int Pad = (FEMSignature< FEMSig >::BType==BOUNDARY_FREE ) ? BSplineSupportSizes< Degree >::SupportEnd : ( (Degree&1) && FEMSignature< FEMSig >::BType==BOUNDARY_DIRICHLET ) ? -1 : 0;
266 inline static int Begin( int depth ){ return -Pad; }
267 inline static int End ( int depth ){ return (1<<depth) + (Degree&1) + Pad; }
268 inline static bool OutOfBounds( int depth , int offset ){ return offset<Begin(depth) || offset>=End(depth); }
269
270 static const int OffsetStart = -BSplineSupportSizes< Degree >::SupportStart , OffsetStop = BSplineSupportSizes< Degree >::SupportEnd + ( Degree&1 ) , IndexSize = OffsetStart + OffsetStop + 1 + 2 * Pad;
271 static int OffsetToIndex( int depth , int offset )
272 {
273 int dim = BSplineSupportSizes< Degree >::Nodes( depth );
274 if ( offset<OffsetStart ) return Pad + offset;
275 else if( offset>=dim-OffsetStop ) return Pad + OffsetStart + 1 + offset - ( dim-OffsetStop );
276 else return Pad + OffsetStart;
277 }
278 static inline int IndexToOffset( int depth , int idx ){ return ( idx-Pad<=OffsetStart ? idx - Pad : ( BSplineSupportSizes< Degree >::Nodes(depth) + Pad - IndexSize + idx ) ); }
279
280 BSplineEvaluationData( void );
281 static double Value( int depth , int off , double s , int d );
282 static double Integral( int depth , int off , double b , double e , int d );
283
284 struct BSplineUpSamplingCoefficients
285 {
286 protected:
287 int _coefficients[ BSplineSupportSizes< Degree >::UpSampleSize ];
288 public:
289 BSplineUpSamplingCoefficients( void ){ ; }
290 BSplineUpSamplingCoefficients( int depth , int offset );
291 double operator[] ( int idx ){ return (double)_coefficients[idx] / (1<<Degree); }
292 };
293
294 template< unsigned int D >
295 struct CenterEvaluator
296 {
297 struct Evaluator : public EvaluationData::CenterEvaluator
298 {
299 protected:
300 friend BSplineEvaluationData;
301 int _depth;
302 double _ccValues[D+1][IndexSize][BSplineSupportSizes< Degree >::SupportSize];
303 public:
304 Evaluator( void ){ _depth = 0 ; memset( _ccValues , 0 , sizeof(_ccValues) ); }
305 double value( int fIdx , int cIdx , int d ) const;
306 int depth( void ) const { return _depth; }
307 void set( int depth ){ BSplineEvaluationData< FEMSig >::template SetCenterEvaluator< D >( *this , depth ); }
308 };
309 struct ChildEvaluator : public EvaluationData::CenterEvaluator
310 {
311 protected:
312 friend BSplineEvaluationData;
313 int _parentDepth;
314 double _pcValues[D+1][IndexSize][BSplineSupportSizes< Degree >::ChildSupportSize];
315 public:
316 ChildEvaluator( void ){ _parentDepth = 0 ; memset( _pcValues , 0 , sizeof(_pcValues) ); }
317 double value( int fIdx , int cIdx , int d ) const;
318 int parentDepth( void ) const { return _parentDepth; }
319 int childDepth( void ) const { return _parentDepth+1; }
320 void set( int parentDepth ){ BSplineEvaluationData< FEMSig >::template SetChildCenterEvaluator< D >( *this , parentDepth ); }
321 };
322 };
323 template< unsigned int D > static void SetCenterEvaluator( typename CenterEvaluator< D >::Evaluator& evaluator , int depth );
324 template< unsigned int D > static void SetChildCenterEvaluator( typename CenterEvaluator< D >::ChildEvaluator& evaluator , int parentDepth );
325
326 template< unsigned int D >
327 struct CornerEvaluator
328 {
329 struct Evaluator : public EvaluationData::CornerEvaluator
330 {
331 protected:
332 friend BSplineEvaluationData;
333 int _depth;
334 double _ccValues[D+1][IndexSize][BSplineSupportSizes< Degree >::BCornerSize];
335 public:
336 Evaluator( void ){ _depth = 0 ; memset( _ccValues , 0 , sizeof( _ccValues ) ); }
337 double value( int fIdx , int cIdx , int d ) const;
338 int depth( void ) const { return _depth; }
339 void set( int depth ){ BSplineEvaluationData< FEMSig >::template SetCornerEvaluator< D >( *this , depth ); }
340 };
341 struct ChildEvaluator : public EvaluationData::CornerEvaluator
342 {
343 protected:
344 friend BSplineEvaluationData;
345 int _parentDepth;
346 double _pcValues[D+1][IndexSize][BSplineSupportSizes< Degree >::ChildBCornerSize];
347 public:
348 ChildEvaluator( void ){ _parentDepth = 0 ; memset( _pcValues , 0 , sizeof( _pcValues ) ); }
349 double value( int fIdx , int cIdx , int d ) const;
350 int parentDepth( void ) const { return _parentDepth; }
351 int childDepth( void ) const { return _parentDepth+1; }
352 void set( int parentDepth ){ BSplineEvaluationData< FEMSig >::template SetChildCornerEvaluator< D >( *this , parentDepth ); }
353 };
354 };
355 template< unsigned int D > static void SetCornerEvaluator( typename CornerEvaluator< D >::Evaluator& evaluator , int depth );
356 template< unsigned int D > static void SetChildCornerEvaluator( typename CornerEvaluator< D >::ChildEvaluator& evaluator , int parentDepth );
357
358 template< unsigned int D >
359 struct Evaluator
360 {
361 typename CenterEvaluator< D >::Evaluator centerEvaluator;
362 typename CornerEvaluator< D >::Evaluator cornerEvaluator;
363 double centerValue( int fIdx , int cIdx , int d ) const { return centerEvaluator.value( fIdx , cIdx , d ); }
364 double cornerValue( int fIdx , int cIdx , int d ) const { return cornerEvaluator.value( fIdx , cIdx , d ); }
365 };
366 template< unsigned int D > static void SetEvaluator( Evaluator< D >& evaluator , int depth ){ SetCenterEvaluator< D >( evaluator.centerEvaluator , depth ) , SetCornerEvaluator< D >( evaluator.cornerEvaluator , depth ); }
367 template< unsigned int D >
368 struct ChildEvaluator
369 {
370 typename CenterEvaluator< D >::ChildEvaluator centerEvaluator;
371 typename CornerEvaluator< D >::ChildEvaluator cornerEvaluator;
372 double centerValue( int fIdx , int cIdx , int d ) const { return centerEvaluator.value( fIdx , cIdx , d ); }
373 double cornerValue( int fIdx , int cIdx , int d ) const { return cornerEvaluator.value( fIdx , cIdx , d ); }
374 };
375 template< unsigned int D > static void SetChildEvaluator( ChildEvaluator< D >& evaluator , int depth ){ SetChildCenterEvaluator< D >( evaluator.centerEvaluator , depth ) , SetChildCornerEvaluator< D >( evaluator.cornerEvaluator , depth ); }
376
377 struct UpSampleEvaluator : public EvaluationData::UpSampleEvaluator
378 {
379 protected:
380 friend BSplineEvaluationData;
381 int _lowDepth;
382 double _pcValues[IndexSize][BSplineSupportSizes< Degree >::UpSampleSize];
383 public:
384 UpSampleEvaluator( void ){ _lowDepth = 0 ; memset( _pcValues , 0 , sizeof( _pcValues ) ); }
385 double value( int pIdx , int cIdx ) const;
386 int lowDepth( void ) const { return _lowDepth; }
387 void set( int lowDepth ){ BSplineEvaluationData::SetUpSampleEvaluator( *this , lowDepth ); }
388 };
389 static void SetUpSampleEvaluator( UpSampleEvaluator& evaluator , int lowDepth );
390 };
391
392 template< unsigned int FEMSig1 , unsigned int FEMSig2 >
393 class BSplineIntegrationData
394 {
395 public:
396 static const unsigned int Degree1 = FEMSignature< FEMSig1 >::Degree;
397 static const unsigned int Degree2 = FEMSignature< FEMSig2 >::Degree;
398 static const int OffsetStart = - BSplineOverlapSizes< Degree1 , Degree2 >::OverlapSupportStart;
399 static const int OffsetStop = BSplineOverlapSizes< Degree1 , Degree2 >::OverlapSupportEnd + ( Degree1&1 );
400 static const int IndexSize = OffsetStart + OffsetStop + 1 + 2 * BSplineEvaluationData< FEMSig1 >::Pad;
401 static int OffsetToIndex( int depth , int offset )
402 {
403 int dim = BSplineSupportSizes< Degree1 >::Nodes( depth );
404 if ( offset<OffsetStart ) return BSplineEvaluationData< FEMSig1 >::Pad + offset;
405 else if( offset>=dim-OffsetStop ) return BSplineEvaluationData< FEMSig1 >::Pad + OffsetStart + 1 + offset - ( dim-OffsetStop );
406 else return BSplineEvaluationData< FEMSig1 >::Pad + OffsetStart;
407 }
408 static inline int IndexToOffset( int depth , int idx ){ return ( idx-BSplineEvaluationData< FEMSig1 >::Pad<=OffsetStart ? idx-BSplineEvaluationData< FEMSig1 >::Pad : ( BSplineSupportSizes< Degree1 >::Nodes(depth) + BSplineEvaluationData< FEMSig1 >::Pad - IndexSize + idx ) ); }
409
410 template< unsigned int D1 , unsigned int D2 > static double Dot( int depth1 , int off1 , int depth2 , int off2 );
411 // An index is interiorly overlapped if the support of its overlapping neighbors is in the range [0,1<<depth)
412 inline static void InteriorOverlappedSpan( int depth , int& begin , int& end ){ begin = -BSplineOverlapSizes< Degree1 , Degree2 >::OverlapStart-BSplineSupportSizes< Degree2 >::SupportStart , end = (1<<depth)-BSplineOverlapSizes< Degree1 , Degree2 >::OverlapEnd-BSplineSupportSizes< Degree2 >::SupportEnd; }
413
414 struct FunctionIntegrator
415 {
416 template< unsigned int D1=Degree1 , unsigned int D2=Degree2 >
417 struct Integrator
418 {
419 protected:
420 friend BSplineIntegrationData;
421 int _depth;
422 double _ccIntegrals[D1+1][D2+1][IndexSize][BSplineOverlapSizes< Degree1 , Degree2 >::OverlapSize];
423 public:
424 Integrator( void )
425 {
426 _depth = 0;
427 memset(_ccIntegrals, 0, sizeof(_ccIntegrals));
428 }
429 double dot( int fIdx1 , int fidx2 , int d1 , int d2 ) const;
430 int depth( void ) const { return _depth; }
431 void set( int depth ){ BSplineIntegrationData::SetIntegrator( *this , depth ); }
432 };
433 template< unsigned int D1=Degree1 , unsigned int D2=Degree2 >
434 struct ChildIntegrator
435 {
436 protected:
437 friend BSplineIntegrationData;
438 int _parentDepth;
439 double _pcIntegrals[D1+1][D2+1][IndexSize][BSplineOverlapSizes< Degree1 , Degree2 >::ChildOverlapSize];
440 public:
441 ChildIntegrator( void )
442 {
443 _parentDepth = 0;
444 memset( _pcIntegrals , 0 , sizeof( _pcIntegrals ) );
445 }
446 double dot( int fIdx1 , int fidx2 , int d1 , int d2 ) const;
447 int parentDepth( void ) const { return _parentDepth; }
448 int childDepth( void ) const { return _parentDepth+1; }
449 void set( int depth ){ BSplineIntegrationData::SetChildIntegrator( *this , depth ); }
450 };
451 };
452 // D1 and D2 indicate the number of derivatives that should be taken
453 template< unsigned int D1 , unsigned int D2 >
454 static void SetIntegrator( typename FunctionIntegrator::template Integrator< D1 , D2 >& integrator , int depth );
455 template< unsigned int D1 , unsigned int D2 >
456 static void SetChildIntegrator( typename FunctionIntegrator::template ChildIntegrator< D1 , D2 >& integrator , int parentDepth );
457
458 protected:
459 // _D1 and _D2 indicate the total number of derivatives the integrator will be storing
460 template< unsigned int D1 , unsigned int D2 , unsigned int _D1 , unsigned int _D2 >
461 struct _IntegratorSetter
462 {
463 static void Set( typename FunctionIntegrator::template Integrator< _D1 , _D2 >& integrator , int depth );
464 static void Set( typename FunctionIntegrator::template ChildIntegrator< _D1 , _D2 >& integrator , int depth );
465 };
466
467 template< unsigned int D1 , unsigned int D2 , unsigned int _D1 , unsigned int _D2 , class Integrator >
468 struct IntegratorSetter
469 {
470 static void Set2D( Integrator& integrator , int depth );
471 static void Set1D( Integrator& integrator , int depth );
472 };
473 template< unsigned int D1 , unsigned int _D1 , unsigned int _D2 , class Integrator >
474 struct IntegratorSetter< D1 , 0 , _D1 , _D2 , Integrator >
475 {
476 static void Set2D( Integrator& integrator , int depth );
477 static void Set1D( Integrator& integrator , int depth );
478 };
479 template< unsigned int D2 , unsigned int _D1 , unsigned int _D2 , class Integrator >
480 struct IntegratorSetter< 0 , D2 , _D1 , _D2 , Integrator >
481 {
482 static void Set2D( Integrator& integrator , int depth );
483 static void Set1D( Integrator& integrator , int depth );
484 };
485 template< unsigned int _D1 , unsigned int _D2 , class Integrator >
486 struct IntegratorSetter< 0 , 0 , _D1 , _D2 , Integrator >
487 {
488 static void Set2D( Integrator& integrator , int depth );
489 static void Set1D( Integrator& integrator , int depth );
490 };
491 };
492 #undef PR_BSPLINE_SET_BOUNDS
493 #undef PR__FLOOR_OF_HALF
494 #undef PR__CEIL_OF_HALF
495 #undef PR_FLOOR_OF_HALF
496 #undef PR_CEIL_OF_HALF
497 #undef PR_SMALLEST_INTEGER_LARGER_THAN_HALF
498 #undef PR_LARGEST_INTEGER_SMALLER_THAN_HALF
499 #undef PR_SMALLEST_INTEGER_LARGER_THAN_OR_EQUAL_TO_HALF
500 #undef PR_LARGEST_INTEGER_SMALLER_THAN_OR_EQUAL_TO_HALF
501
502
503 template< unsigned int FEMSig , unsigned int D=0 >
504 struct BSplineData
505 {
506 static const unsigned int Degree = FEMSignature< FEMSig >::Degree;
507 static const int _Degree = Degree;
508 // Note that this struct stores the components in left-to-right order
509 struct BSplineComponents
510 {
511 BSplineComponents( void ){ ; }
512 BSplineComponents( int depth , int offset );
513 const Polynomial< Degree >* operator[] ( int idx ) const { return _polys[idx]; }
514 protected:
515 Polynomial< Degree > _polys[Degree+1][D+1];
516 };
517 struct SparseBSplineEvaluator
518 {
519 void init( unsigned int depth )
520 {
521 _depth = depth , _width = 1./(1<<depth);
522 // _preStart + BSplineSupportSizes< _Degree >::SupportEnd >=0
523 _preStart = -BSplineSupportSizes< _Degree >::SupportEnd;
524 // _postStart + BSplineSupportSizes< _Degree >::SupportEnd <= (1<<depth)-1
525 _postStart = (1<<depth) - 1 - BSplineSupportSizes< _Degree >::SupportEnd;
526 _preEnd = _preStart + _Degree + 1;
527 _postEnd = _postStart + _Degree + 1;
528 _centerIndex = ( ( _preStart + _Degree + 1 ) + ( _postStart - 1 ) ) / 2;
529 _centerComponents = BSplineComponents( depth , _centerIndex );
530 for( int i=0 ; i<=Degree ; i++ ) _preComponents[i] = BSplineComponents( depth , _preStart+i ) , _postComponents[i] = BSplineComponents( depth , _postStart+i );
531 }
532 double value( double p , int fIdx , int d ) const { return value( p , (int)( p * (1<<_depth ) ) , fIdx , d ); }
533 double value( double p , int pIdx , int fIdx , int d ) const
534 {
535 if ( fIdx<_preStart ) return 0;
536 else if( fIdx<_preEnd ) return _preComponents [fIdx-_preStart ][pIdx-fIdx+_LeftSupportRadius][d]( p );
537 else if( fIdx<_postStart ) return _centerComponents [pIdx-fIdx+_LeftSupportRadius][d]( p+_width*(_centerIndex-fIdx) );
538 else if( fIdx<_postEnd ) return _postComponents[fIdx-_postStart][pIdx-fIdx+_LeftSupportRadius][d]( p );
539 else return 0;
540 }
541 const Polynomial< _Degree >* polynomialsAndOffset( double& p , int fIdx ) const { return polynomialsAndOffset( p , (int)( p * (1<<_depth ) ) , fIdx ); }
542 const Polynomial< _Degree >* polynomialsAndOffset( double& p , int pIdx , int fIdx ) const
543 {
544 if ( fIdx<_preEnd ){ return _preComponents [fIdx-_preStart ][pIdx-fIdx+_LeftSupportRadius]; }
545 else if( fIdx<_postStart ){ p += _width*(_centerIndex-fIdx) ; return _centerComponents [pIdx-fIdx+_LeftSupportRadius]; }
546 else { return _postComponents[fIdx-_postStart][pIdx-fIdx+_LeftSupportRadius]; }
547 }
548 protected:
549 static const int _LeftSupportRadius = -BSplineSupportSizes< _Degree >::SupportStart;
550 BSplineComponents _preComponents[_Degree+1] , _postComponents[_Degree+1] ,_centerComponents;
551 int _preStart , _preEnd , _postStart , _postEnd , _centerIndex;
552 unsigned int _depth;
553 double _width;
554 };
555 const SparseBSplineEvaluator& operator[]( int depth ) const { return _evaluators[depth]; }
556
557 inline static int RemapOffset( int depth , int idx , bool& reflect );
558
559 BSplineData( void );
560 void reset( int maxDepth );
561 BSplineData( int maxDepth );
562 ~BSplineData( void );
563
564 protected:
565 unsigned int _maxDepth;
566 Pointer( SparseBSplineEvaluator ) _evaluators;
567 };
568
569 template< unsigned int Degree1 , unsigned int Degree2 > void SetBSplineElementIntegrals( double integrals[Degree1+1][Degree2+1] );
570
571 #include "BSplineData.inl"
572 }
573
574 #endif // BSPLINE_DATA_INCLUDED