Debian Codegraph @

src/thirdparty/PoissonRecon/FEMTree.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 // -- [TODO] Make as many of the functions (related to the solver) const as possible.
30 // -- [TODO] Move the point interpolation constraint scaling by 1<<maxDepth
31 // -- [TODO] Add support for staggered-grid test functions
32 // -- [TODO] Store signatures with constraints/systems/restriction-prolongations
33 // -- [TODO] Make a virtual evaluation that only needs to know the degree
34 // -- [TODO] Modify (public) functions so that template parameters don't need to be passed when they are called
35 // -- [TODO] Confirm that whenever _isValidFEM*Node is called, the flags have already been set.
36 // -- [TODO] Make weight evaluation more efficient in _getSamplesPerNode by reducing the number of calls to getNeighbors
37 // -- [TODO] For point evaluation:
38 // 1. Have the evaluator store stencils for all depths [DONE]
39 // 2. When testing centers/corners, don't use generic evaluation
40 // -- [TODO] Support nested parallelism with thread pools
41 // -- [TODO] Make the node flags protected
42 // -- [TODO] Identify members that are only valid after finalization
43 // -- [TODO] Force the MaxDegree and finite-element degrees into the template parameters for the FEMTree so that root vs space-root are set up on construction
44
45 #ifndef FEM_TREE_INCLUDED
46 #define FEM_TREE_INCLUDED
47
48 #include <atomic>
49 #ifdef SANITIZED_PR
50 #include <shared_mutex>
51 #endif // SANITIZED_PR
52 #include "MyMiscellany.h"
53 #include "BSplineData.h"
54 #include "Geometry.h"
55 #include "DataStream.h"
56 #include "RegularTree.h"
57 #include "SparseMatrix.h"
58 #include "NestedVector.h"
59 #include "Rasterizer.h"
60 #include <limits>
61 #include <functional>
62 #include <string>
63 #include <tuple>
64 #include <functional>
65 #include <cmath>
66 #include <climits>
67 #include "MarchingCubes.h"
68 #include <sstream>
69 #include <sstream>
70 #include <iomanip>
71 #include <unordered_map>
72 #include "MAT.h"
73
74 namespace PoissonRecon
75 {
76
77 #ifdef BIG_DATA
78 // The integer type used for indexing the nodes in the octree
79 typedef long long node_index_type;
80 // The integer type used for indexing the entries of the matrix
81 typedef int matrix_index_type;
82 #else // !BIG_DATA
83 typedef int node_index_type;
84 typedef int matrix_index_type;
85 #endif // BIG_DATA
86 #ifdef USE_DEEP_TREE_NODES
87 // The integer type used for storing the depth and offset within an octree node
88 typedef unsigned int depth_and_offset_type;
89 #else // !USE_DEEP_TREE_NODES
90 typedef unsigned short depth_and_offset_type;
91 #endif // USE_DEEP_TREE_NODES
92
93 template< unsigned int Dim , class Real > class FEMTree;
94
95 enum
96 {
97 SHOW_GLOBAL_RESIDUAL_NONE ,
98 SHOW_GLOBAL_RESIDUAL_LAST ,
99 SHOW_GLOBAL_RESIDUAL_ALL ,
100 SHOW_GLOBAL_RESIDUAL_COUNT
101 };
102 static const char* ShowGlobalResidualNames[] = { "show none" , "show last" , "show all" };
103
104 class FEMTreeNodeData
105 {
106 public:
107 enum
108 {
109 SPACE_FLAG = 1 << 0 , // Part of the partition of the unit cube
110 FEM_FLAG_1 = 1 << 1 , // Indexes a valid finite element
111 FEM_FLAG_2 = 1 << 2 , // Indexes a valid finite element
112 DIRICHLET_NODE_FLAG = 1 << 3 , // The finite elements should evaluate to zero on this node
113 DIRICHLET_ELEMENT_FLAG = 1 << 4 , // Coefficient of this node should be locked to zero
114 GEOMETRY_SUPPORTED_FLAG = 1 << 5 , // The finite element defined by this node has support overlapping geometry constraints
115 GHOST_FLAG = 1 << 6 , // Children are pruned out
116 SCRATCH_FLAG = 1 << 7 ,
117 };
118 node_index_type nodeIndex;
119 #ifdef SANITIZED_PR
120 mutable std::atomic< unsigned char > flags;
121 #else // !SANITIZED_PR
122 mutable char flags;
123 #endif // SANITIZED_PR
124 void setGhostFlag( bool f ) const { if( f ) flags |= GHOST_FLAG ; else flags &= ~GHOST_FLAG; }
125 bool getGhostFlag( void ) const { return ( flags & GHOST_FLAG )!=0; }
126 void setDirichletNodeFlag( bool f ) const { if( f ) flags |= DIRICHLET_NODE_FLAG ; else flags &= ~DIRICHLET_NODE_FLAG; }
127 bool getDirichletNodeFlag( void ) const { return ( flags & DIRICHLET_NODE_FLAG )!=0; }
128 void setDirichletElementFlag( bool f ) const { if( f ) flags |= DIRICHLET_ELEMENT_FLAG ; else flags &= ~DIRICHLET_ELEMENT_FLAG; }
129 bool getDirichletElementFlag( void ) const { return ( flags & DIRICHLET_ELEMENT_FLAG )!=0; }
130 void setScratchFlag( bool f ) const { if( f ) flags |= SCRATCH_FLAG ; else flags &= (unsigned char)~SCRATCH_FLAG; }
131 bool getScratchFlag( void ) const { return ( flags & SCRATCH_FLAG )!=0; }
132 inline FEMTreeNodeData( void );
133 inline ~FEMTreeNodeData( void );
134 #ifdef SANITIZED_PR
135 inline FEMTreeNodeData &operator = ( const FEMTreeNodeData &data );
136 #endif // SANITIZED_PR
137 };
138
139 template< unsigned int Dim >
140 class SortedTreeNodes
141 {
142 typedef RegularTreeNode< Dim , FEMTreeNodeData , depth_and_offset_type > TreeNode;
143 protected:
144 Pointer( Pointer( node_index_type ) ) _sliceStart;
145 int _levels;
146
147 void _set( TreeNode& root );
148 public:
149 Pointer( TreeNode* ) treeNodes;
150 node_index_type begin( int depth ) const { return _sliceStart[depth][0]; }
151 node_index_type end( int depth ) const { return _sliceStart[depth][(size_t)1<<depth]; }
152 node_index_type begin( int depth , int slice ) const { return _sliceStart[depth][ slice<0 ? 0 : ( slice>(1<<depth) ? (1<<depth) : slice ) ]; }
153 node_index_type end( int depth , int slice ) const { return begin( depth , slice+1 ); }
154 size_t size( void ) const { return _levels ? _sliceStart[_levels-1][(size_t)1<<(_levels-1)] : 0; }
155 size_t size( int depth ) const
156 {
157 if( depth<0 || depth>=_levels ) MK_THROW( "bad depth: 0 <= " , depth , " < " , _levels );
158 return _sliceStart[depth][(size_t)1<<depth] - _sliceStart[depth][0];
159 }
160 size_t size( int depth , int slice ) const { return end( depth , slice ) - begin( depth , slice ); }
161 int levels( void ) const { return _levels; }
162
163 SortedTreeNodes( void );
164 ~SortedTreeNodes( void );
165 // Resets the sorted tree nodes and sets map[i] to the index previously stored with the i-th node.
166 void reset( TreeNode& root , std::vector< node_index_type > &map );
167 void set( TreeNode& root );
168
169 void write( BinaryStream &stream ) const;
170 void read( BinaryStream &stream , TreeNode &root );
171 };
172
173 template< typename T > struct DotFunctor{};
174 template< > struct DotFunctor< float >
175 {
176 double operator()( float v1 , float v2 ){ return v1*v2; }
177 unsigned int dimension( void ) const { return 1; }
178 };
179 template< > struct DotFunctor< double >
180 {
181 double operator()( double v1 , double v2 ){ return v1*v2; }
182 unsigned int dimension( void ) const { return 1; }
183 };
184 template< class Real , unsigned int Dim > struct DotFunctor< Point< Real , Dim > >
185 {
186 double operator()( Point< Real , Dim > v1 , Point< Real , Dim > v2 ){ return Point< Real , Dim >::Dot( v1 , v2 ); }
187 unsigned int dimension( void ) const { return Dim; }
188 };
189
190 template< typename Pack > struct SupportKey{ };
191 template< unsigned int ... Degrees >
192 struct SupportKey< UIntPack< Degrees ... > > : public RegularTreeNode< sizeof...(Degrees) , FEMTreeNodeData , depth_and_offset_type >::template NeighborKey< UIntPack< (-BSplineSupportSizes< Degrees >::SupportStart) ... > , UIntPack< BSplineSupportSizes< Degrees >::SupportEnd ... > >
193 {
194 typedef UIntPack< (-BSplineSupportSizes< Degrees >::SupportStart ) ... > LeftRadii;
195 typedef UIntPack< ( BSplineSupportSizes< Degrees >::SupportEnd ) ... > RightRadii;
196 typedef UIntPack< ( BSplineSupportSizes< Degrees >::SupportSize ) ... > Sizes;
197 };
198
199 template< typename Pack > struct ConstSupportKey{ };
200 template< unsigned int ... Degrees >
201 struct ConstSupportKey< UIntPack< Degrees ... > > : public RegularTreeNode< sizeof...(Degrees) , FEMTreeNodeData , depth_and_offset_type >::template ConstNeighborKey< UIntPack< (-BSplineSupportSizes< Degrees >::SupportStart ) ... > , UIntPack< BSplineSupportSizes< Degrees >::SupportEnd ... > >
202 {
203 typedef UIntPack< (-BSplineSupportSizes< Degrees >::SupportStart ) ... > LeftRadii;
204 typedef UIntPack< ( BSplineSupportSizes< Degrees >::SupportEnd ) ... > RightRadii;
205 typedef UIntPack< ( BSplineSupportSizes< Degrees >::SupportSize ) ... > Sizes;
206 };
207
208 template< typename Pack > struct OverlapKey{ };
209 template< unsigned int ... Degrees >
210 struct OverlapKey< UIntPack< Degrees ... > > : public RegularTreeNode< sizeof...(Degrees) , FEMTreeNodeData , depth_and_offset_type >::template NeighborKey< UIntPack< (-BSplineOverlapSizes< Degrees , Degrees >::OverlapStart ) ... > , UIntPack< BSplineOverlapSizes< Degrees , Degrees >::OverlapEnd ... > >
211 {
212 typedef UIntPack< (-BSplineOverlapSizes< Degrees , Degrees >::OverlapStart ) ... > LeftRadii;
213 typedef UIntPack< ( BSplineOverlapSizes< Degrees , Degrees >::OverlapEnd ) ... > RightRadii;
214 typedef UIntPack< ( BSplineOverlapSizes< Degrees , Degrees >::OverlapSize ) ... > Sizes;
215 };
216
217 template< typename Pack > struct ConstOverlapKey{ };
218 template< unsigned int ... Degrees >
219 struct ConstOverlapKey< UIntPack< Degrees ... > > : public RegularTreeNode< sizeof...(Degrees) , FEMTreeNodeData , depth_and_offset_type >::template ConstNeighborKey< UIntPack< (-BSplineOverlapSizes< Degrees , Degrees >::OverlapStart ) ... > , UIntPack< BSplineOverlapSizes< Degrees , Degrees >::OverlapEnd ... > >
220 {
221 typedef UIntPack< (-BSplineOverlapSizes< Degrees , Degrees >::OverlapStart ) ... > LeftRadii;
222 typedef UIntPack< ( BSplineOverlapSizes< Degrees , Degrees >::OverlapEnd ) ... > RightRadii;
223 typedef UIntPack< ( BSplineOverlapSizes< Degrees , Degrees >::OverlapSize ) ... > Sizes;
224 };
225
226 template< typename Pack > struct PointSupportKey{ };
227 template< unsigned int ... Degrees >
228 struct PointSupportKey< UIntPack< Degrees ... > > : public RegularTreeNode< sizeof...(Degrees) , FEMTreeNodeData , depth_and_offset_type >::template NeighborKey< UIntPack< BSplineSupportSizes< Degrees >::SupportEnd ... > , UIntPack< (-BSplineSupportSizes< Degrees >::SupportStart ) ... > >
229 {
230 typedef UIntPack< ( BSplineSupportSizes< Degrees >::SupportEnd ) ... > LeftRadii;
231 typedef UIntPack< (-BSplineSupportSizes< Degrees >::SupportStart ) ... > RightRadii;
232 typedef UIntPack< ( BSplineSupportSizes< Degrees >::SupportEnd - BSplineSupportSizes< Degrees >::SupportStart + 1 ) ... > Sizes;
233 };
234
235 template< typename Pack > struct ConstPointSupportKey{ };
236 template< unsigned int ... Degrees >
237 struct ConstPointSupportKey< UIntPack< Degrees ... > > : public RegularTreeNode< sizeof...(Degrees) , FEMTreeNodeData , depth_and_offset_type >::template ConstNeighborKey< UIntPack< BSplineSupportSizes< Degrees >::SupportEnd ... > , UIntPack< (-BSplineSupportSizes< Degrees >::SupportStart ) ... > >
238 {
239 typedef UIntPack< ( BSplineSupportSizes< Degrees >::SupportEnd ) ... > LeftRadii;
240 typedef UIntPack< (-BSplineSupportSizes< Degrees >::SupportStart ) ... > RightRadii;
241 typedef UIntPack< ( BSplineSupportSizes< Degrees >::SupportEnd - BSplineSupportSizes< Degrees >::SupportStart + 1 ) ... > Sizes;
242 };
243
244 template< typename Pack > struct CornerSupportKey{ };
245 template< unsigned int ... Degrees >
246 struct CornerSupportKey< UIntPack< Degrees ... > > : public RegularTreeNode< sizeof...(Degrees) , FEMTreeNodeData , depth_and_offset_type >::template NeighborKey< UIntPack< BSplineSupportSizes< Degrees >::BCornerEnd ... > , UIntPack< ( -BSplineSupportSizes< Degrees >::BCornerStart + 1 ) ... > >
247 {
248 typedef UIntPack< ( BSplineSupportSizes< Degrees >::BCornerEnd ) ... > LeftRadii;
249 typedef UIntPack< (-BSplineSupportSizes< Degrees >::BCornerStart + 1 ) ... > RightRadii;
250 typedef UIntPack< ( BSplineSupportSizes< Degrees >::BCornerSize + 1 ) ... > Sizes;
251 };
252
253 template< typename Pack > struct ConstCornerSupportKey{ };
254 template< unsigned int ... Degrees >
255 struct ConstCornerSupportKey< UIntPack< Degrees ... > > : public RegularTreeNode< sizeof...(Degrees) , FEMTreeNodeData , depth_and_offset_type >::template ConstNeighborKey< UIntPack< BSplineSupportSizes< Degrees >::BCornerEnd ... > , UIntPack< ( -BSplineSupportSizes< Degrees >::BCornerStart + 1 ) ... > >
256 {
257 typedef UIntPack< ( BSplineSupportSizes< Degrees >::BCornerEnd ) ... > LeftRadii;
258 typedef UIntPack< (-BSplineSupportSizes< Degrees >::BCornerStart + 1 ) ... > RightRadii;
259 typedef UIntPack< ( BSplineSupportSizes< Degrees >::BCornerSize + 1 ) ... > Sizes;
260 };
261
262
263 template< class Data , typename Pack > struct _SparseOrDenseNodeData{};
264 template< class Data , unsigned int ... FEMSigs >
265 struct _SparseOrDenseNodeData< Data , UIntPack< FEMSigs ... > >
266 {
267 static const unsigned int Dim = sizeof ... ( FEMSigs );
268 typedef UIntPack< FEMSigs ... > FEMSignatures;
269 typedef Data data_type;
270
271 // Methods for accessing as an array
272 virtual size_t size( void ) const = 0;
273 virtual const Data& operator[] ( size_t idx ) const = 0;
274 virtual Data& operator[] ( size_t idx ) = 0;
275
276 // Method for accessing (and inserting if necessary) using a node
277 virtual Data& operator[]( const RegularTreeNode< Dim , FEMTreeNodeData , depth_and_offset_type > *node ) = 0;
278 // Methods for accessing using a node
279 virtual Data* operator()( const RegularTreeNode< Dim , FEMTreeNodeData , depth_and_offset_type > *node ) = 0;
280 virtual const Data* operator()( const RegularTreeNode< Dim , FEMTreeNodeData , depth_and_offset_type >* node ) const = 0;
281
282 // Method for getting the actual index associated with a node
283 virtual node_index_type index( const RegularTreeNode< Dim , FEMTreeNodeData , depth_and_offset_type >* node ) const = 0;
284 };
285
286 template< class Data , typename Pack > struct SparseNodeData{};
287 template< class Data , unsigned int ... FEMSigs >
288 struct SparseNodeData< Data , UIntPack< FEMSigs ... > > : public _SparseOrDenseNodeData< Data , UIntPack< FEMSigs ... > >
289 {
290 static const unsigned int Dim = sizeof ... ( FEMSigs );
291
292 static void WriteSignatures( BinaryStream &stream )
293 {
294 unsigned int dim = sizeof ... ( FEMSigs );
295 stream.write( dim );
296 unsigned int femSigs[] = { FEMSigs ... };
297 stream.write( femSigs , dim );
298 }
299 void write( BinaryStream &stream , const Serializer< Data > &serializer ) const
300 {
301 _indices.write( stream );
302 _data.write( stream , serializer );
303 }
304 void read( BinaryStream &stream , const Serializer< Data > &serializer )
305 {
306 _indices.read( stream );
307 _data.read( stream , serializer );
308 }
309 void write( BinaryStream &stream ) const
310 {
311 _indices.write( stream );
312 _data.write( stream );
313 }
314 void read( BinaryStream &stream )
315 {
316 _indices.read( stream );
317 _data.read( stream );
318 }
319 SparseNodeData( void ){}
320 SparseNodeData( BinaryStream &stream ){ read(stream); }
321 SparseNodeData( BinaryStream &stream , const Serializer< Data > &serializer ){ read(stream,serializer); }
322 // [WARNING] Default constructing the mutex
323 SparseNodeData( const SparseNodeData &d ) : _indices(d._indices) , _data(d._data){};
324 SparseNodeData( SparseNodeData &&d ) : _indices(d._indices) , _data(d._data){};
325 // [WARNING] Not copying the mutex
326 SparseNodeData &operator = ( const SparseNodeData &d ){ _indices = d._indices , _data = d._data ; return *this; }
327 SparseNodeData &operator = ( SparseNodeData &&d ){ std::swap( _indices , d._indices ) , std::swap( _data , d._data ); return *this; }
328
329 size_t size( void ) const { return _data.size(); }
330 const Data& operator[] ( size_t idx ) const { return _data[idx]; }
331 Data& operator[] ( size_t idx ) { return _data[idx]; }
332
333 void reserve( size_t sz ){ if( sz>_indices.size() ) _indices.resize( sz , -1 ); }
334 size_t reserved( void ) const { return _indices.size(); }
335 Data* operator()( const RegularTreeNode< Dim , FEMTreeNodeData , depth_and_offset_type >* node ){ return ( node->nodeData.nodeIndex<0 || node->nodeData.nodeIndex>=(node_index_type)_indices.size() || _indices[ node->nodeData.nodeIndex ]==-1 ) ? NULL : &_data[ _indices[ node->nodeData.nodeIndex ] ]; }
336 const Data* operator()( const RegularTreeNode< Dim , FEMTreeNodeData , depth_and_offset_type >* node ) const { return ( node->nodeData.nodeIndex<0 || node->nodeData.nodeIndex>=(node_index_type)_indices.size() || _indices[ node->nodeData.nodeIndex ]==-1 ) ? NULL : &_data[ _indices[ node->nodeData.nodeIndex ] ]; }
337
338 Data& operator[]( const RegularTreeNode< Dim , FEMTreeNodeData , depth_and_offset_type >* node ){ return at( node ); }
339 Data &at( const RegularTreeNode< Dim , FEMTreeNodeData , depth_and_offset_type > *node , Data zero=Data() )
340 {
341 _indices.resize( node->nodeData.nodeIndex+1 , -1 );
342
343 // If the node hasn't been allocated yet
344 #ifdef SANITIZED_PR
345 volatile node_index_type *indexPtr = &_indices[ node->nodeData.nodeIndex ];
346 node_index_type _index = ReadAtomic( *indexPtr );
347 #else // !SANITIZED_PR
348 volatile node_index_type &_index = _indices[ node->nodeData.nodeIndex ];
349 #endif // SANITIZED_PR
350 if( _index==-1 )
351 {
352 std::lock_guard< std::mutex > lock( _updateMutex );
353 #ifdef SANITIZED_PR
354 _index = ReadAtomic( *indexPtr );
355 #endif // SANITIZED_PR
356 if( _index==-1 )
357 {
358 size_t sz = _data.size();
359 _data.resize( sz+1 , zero );
360 #ifdef SANITIZED_PR
361 _index = (node_index_type)sz;
362 SetAtomic( *indexPtr , _index );
363 #else // !SANITIZED_PR
364 *(node_index_type*)&_index = (node_index_type)sz;
365 #endif // SANITIZED_PR
366 }
367 }
368 return _data[ _index ];
369 }
370 node_index_type index( const RegularTreeNode< Dim , FEMTreeNodeData , depth_and_offset_type > *node ) const
371 {
372 if( !node || node->nodeData.nodeIndex<0 || node->nodeData.nodeIndex>=(node_index_type)_indices.size() ) return -1;
373 else return _indices[ node->nodeData.nodeIndex ];
374 }
375
376 node_index_type index( node_index_type idx ) const
377 {
378 if( idx<0 || idx>=(node_index_type)_indices.size() ) return -1;
379 else return _indices[ idx ];
380 }
381
382 void merge( const SparseNodeData &data ){ return merge( data , []( const Data &data ){ return data; } ); }
383
384 template< typename MergeFunctor >
385 void merge( const SparseNodeData &data , const MergeFunctor &mergeFunctor )
386 {
387 size_t sz = _indices.size();
388 node_index_type newDataCount = 0;
389 for( unsigned int j=0 ; j<data._indices.size() ; j++ ) if( data._indices[j]!=-1 && data._indices[j]<(node_index_type)sz && _indices[j]==-1 ) newDataCount++;
390 size_t oldSize = _data.size();
391 _data.resize( oldSize + newDataCount );
392 newDataCount = 0;
393 for( unsigned int j=0 ; j<data._indices.size() ; j++ ) if( data._indices[j]!=-1 && data._indices[j]<(node_index_type)sz )
394 if( _indices[j]==-1 )
395 {
396 _indices[j] = (node_index_type)oldSize + newDataCount;
397 _data[ oldSize + newDataCount ] = mergeFunctor( data._data[ data._indices[j] ] );
398 newDataCount++;
399 }
400 else _data[ _indices[j] ] += mergeFunctor( data._data[ data._indices[j] ] );
401 }
402
403 template< typename TargetToSourceFunctor >
404 void mergeFromTarget( const SparseNodeData &data , const TargetToSourceFunctor &targetToSourceFunctor ){ return mergeFromTarget( data , targetToSourceFunctor , []( const Data &data ){ return data; } ); }
405
406 template< typename TargetToSourceFunctor , typename MergeFunctor >
407 void mergeFromTarget( const SparseNodeData &target , const TargetToSourceFunctor &targetToSourceFunctor , const MergeFunctor &mergeFunctor )
408 {
409 size_t sz = _indices.size();
410 node_index_type newDataCount = 0;
411 for( unsigned int j=0 ; j<target._indices.size() ; j++ ) if( target._indices[j]!=-1 && target._indices[j]<(node_index_type)sz && _indices[ targetToSourceFunctor(j) ]==-1 ) newDataCount++;
412 size_t oldSize = _data.size();
413 _data.resize( oldSize + newDataCount );
414 newDataCount = 0;
415 for( unsigned int j=0 ; j<target._indices.size() ; j++ ) if( target._indices[j]!=-1 && target._indices[j]<(node_index_type)sz )
416 {
417 node_index_type _j = targetToSourceFunctor( j );
418 if( _indices[_j]==-1 )
419 {
420 _indices[_j] = (node_index_type)oldSize + newDataCount;
421 _data[ oldSize + newDataCount ] = mergeFunctor( target._data[ target._indices[j] ] );
422 newDataCount++;
423 }
424 else _data[ _indices[_j] ] += mergeFunctor( target._data[ target._indices[j] ] );
425 }
426 }
427
428 template< typename SourceToTargetFunctor >
429 void mergeToSource( const SparseNodeData &data , const SourceToTargetFunctor &sourceToTargetFunctor ){ return mergeToSource( data , sourceToTargetFunctor , []( const Data &data ){ return data; } ); }
430
431 template< typename SourceToTargetFunctor , typename MergeFunctor >
432 void mergeToSource( const SparseNodeData &target , const SourceToTargetFunctor &sourceToTargetFunctor , const MergeFunctor &mergeFunctor )
433 {
434 size_t _sz = target._indices.size();
435 node_index_type newDataCount = 0;
436 for( unsigned int j=0 ; j<_indices.size() ; j++ ) if( _indices[j]==-1 )
437 {
438 unsigned int _j = sourceToTargetFunctor( j );
439 if( _j<(node_index_type)_sz && target._indices[_j]!=-1 ) newDataCount++;
440 }
441 size_t oldSize = _data.size();
442 _data.resize( oldSize + newDataCount );
443 newDataCount = 0;
444 for( unsigned int j=0 ; j<_indices.size() ; j++ )
445 {
446 unsigned int _j = sourceToTargetFunctor( j );
447 if( _j<(node_index_type)_sz && target._indices[_j]!=-1 )
448 if( _indices[j]==-1 )
449 {
450 _indices[j] = (node_index_type)oldSize + newDataCount;
451 _data[ oldSize + newDataCount ] = mergeFunctor( target._data[ target._indices[_j] ] );
452 newDataCount++;
453 }
454 else _data[ _indices[j] ] += mergeFunctor( target._data[ target._indices[_j] ] );
455 }
456 }
457
458 protected:
459 template< unsigned int _Dim , class _Real > friend class FEMTree;
460
461 // Map should be the size of the new number of entries and map[i] should give the old index of the i-th node
462 void _remapIndices( ConstPointer( node_index_type )oldNodeIndices , size_t newNodeCount )
463 {
464 NestedVector< node_index_type , NESTED_VECTOR_LEVELS > newIndices;
465 newIndices.resize( newNodeCount );
466 for( node_index_type i=0 ; i<(node_index_type)newNodeCount ; i++ )
467 {
468 newIndices[i] = -1;
469 if( oldNodeIndices[i]!=-1 && oldNodeIndices[i]<(node_index_type)_indices.size() ) newIndices[i] = _indices[ oldNodeIndices[i] ];
470 }
471 std::swap( _indices , newIndices );
472 }
473
474 SparseNodeData _trim( node_index_type endIndex ) const
475 {
476 size_t dataCount = 0;
477 for( node_index_type i=0 ; i<endIndex ; i++ ) if( _indices[i]!=-1 ) dataCount++;
478 SparseNodeData sparseNodeData;
479 sparseNodeData._indices.resize( endIndex );
480 sparseNodeData._data.resize( dataCount );
481 node_index_type idx = 0;
482 for( node_index_type i=0 ; i<endIndex ; i++ )
483 if( _indices[i]!=-1 )
484 {
485 sparseNodeData._indices[i] = idx;
486 sparseNodeData._data[idx] = _data[ _indices[i] ];
487 idx++;
488 }
489 else sparseNodeData._indices[i] = -1;
490 return sparseNodeData;
491 }
492
493 std::mutex _updateMutex;
494 NestedVector< node_index_type , NESTED_VECTOR_LEVELS > _indices;
495 NestedVector< Data , NESTED_VECTOR_LEVELS > _data;
496 };
497
498 template< class Data , typename Pack > struct DenseNodeData{};
499 template< class Data , unsigned int ... FEMSigs >
500 struct DenseNodeData< Data , UIntPack< FEMSigs ... > > : public _SparseOrDenseNodeData< Data , UIntPack< FEMSigs ... > >
501 {
502 static const unsigned int Dim = sizeof ... ( FEMSigs );
503 DenseNodeData( void ) { _data = NullPointer( Data ) ; _sz = 0; }
504 DenseNodeData( size_t sz ){ _sz = sz ; if( sz ) _data = NewPointer< Data >( sz ) ; else _data = NullPointer( Data ); }
505 DenseNodeData( const DenseNodeData& d ) : DenseNodeData() { _resize( d._sz ) ; if( _sz ) memcpy( _data , d._data , sizeof(Data) * _sz ); }
506 DenseNodeData( DenseNodeData&& d ){ _data = d._data , _sz = d._sz ; d._data = NullPointer( Data ) , d._sz = 0; }
507 DenseNodeData& operator = ( const DenseNodeData& d ){ _resize( d._sz ) ; if( _sz ) memcpy( _data , d._data , sizeof(Data) * _sz ) ; return *this; }
508 DenseNodeData& operator = ( DenseNodeData&& d ){ size_t __sz = _sz ; Pointer( Data ) __data = _data ; _data = d._data , _sz = d._sz ; d._data = __data , d._sz = __sz ; return *this; }
509 DenseNodeData( BinaryStream &stream ) : DenseNodeData() { read(stream); }
510 ~DenseNodeData( void ){ DeletePointer( _data ) ; _sz = 0; }
511 void resize( size_t sz ){ DeletePointer( _data ) ; _sz = sz ; if( sz ) _data = NewPointer< Data >( sz ) ; else _data = NullPointer( Data ); }
512 static void WriteSignatures( BinaryStream &stream )
513 {
514 unsigned int dim = sizeof ... ( FEMSigs );
515 stream.write( dim );
516 unsigned int femSigs[] = { FEMSigs ... };
517 stream.write( GetPointer( femSigs , dim ) , dim );
518 }
519 void write( BinaryStream &stream ) const
520 {
521 stream.write( _sz );
522 stream.write( _data , _sz );
523 }
524 void read( BinaryStream &stream )
525 {
526 if( !stream.read( _sz ) ) MK_THROW( "Failed to read size" );
527 _data = NewPointer< Data >( _sz );
528 if( !stream.read( _data , _sz ) ) MK_THROW( "failed to read data" );
529 }
530
531 Data& operator[] ( size_t idx ) { return _data[idx]; }
532 const Data& operator[] ( size_t idx ) const { return _data[idx]; }
533 size_t size( void ) const { return _sz; }
534 Data& operator[]( const RegularTreeNode< Dim , FEMTreeNodeData , depth_and_offset_type >* node ) { return _data[ node->nodeData.nodeIndex ]; }
535 Data* operator()( const RegularTreeNode< Dim , FEMTreeNodeData , depth_and_offset_type >* node ) { return ( node==NULL || node->nodeData.nodeIndex>=(node_index_type)_sz ) ? NULL : &_data[ node->nodeData.nodeIndex ]; }
536 const Data* operator()( const RegularTreeNode< Dim , FEMTreeNodeData , depth_and_offset_type >* node ) const { return ( node==NULL || node->nodeData.nodeIndex>=(node_index_type)_sz ) ? NULL : &_data[ node->nodeData.nodeIndex ]; }
537 node_index_type index( const RegularTreeNode< Dim , FEMTreeNodeData , depth_and_offset_type >* node ) const { return ( !node || node->nodeData.nodeIndex<0 || node->nodeData.nodeIndex>=(node_index_type)_sz ) ? -1 : node->nodeData.nodeIndex; }
538 Pointer( Data ) operator()( void ) { return _data; }
539 ConstPointer( Data ) operator()( void ) const { return ( ConstPointer( Data ) )_data; }
540 protected:
541 template< unsigned int _Dim , class _Real > friend class FEMTree;
542
543 // Map should be the size of the new number of entries and map[i] should give the old index of the i-th node
544 void _remapIndices( ConstPointer( node_index_type )oldNodeIndices , size_t newNodeCount )
545 {
546 Pointer( Data ) newData = NewPointer< Data >( newNodeCount );
547 memset( newData , 0 , sizeof(Data)*newNodeCount );
548 for( size_t i=0 ; i<newNodeCount ; i++ ) if( oldNodeIndices[i]>=0 && oldNodeIndices[i]<(node_index_type)_sz ) newData[i] = _data[ oldNodeIndices[i] ];
549 DeletePointer( _data );
550 _data = newData;
551 _sz = newNodeCount;
552 }
553
554 DenseNodeData _trim( node_index_type endIndex ) const
555 {
556 DenseNodeData denseNodeData;
557 denseNodeData._sz = endIndex;
558 denseNodeData._data = NewPointer< Data >( endIndex );
559 memcpy( denseNodeData._data , _data , sizeof(Data) * denseNodeData._sz );
560 return denseNodeData;
561 }
562
563 size_t _sz;
564 void _resize( size_t sz ){ DeletePointer( _data ) ; if( sz ) _data = NewPointer< Data >( sz ) ; else _data = NullPointer( Data ) ; _sz = sz; }
565 Pointer( Data ) _data;
566 };
567
568 enum FEMTreeRealType
569 {
570 FEM_TREE_REAL_FLOAT ,
571 FEM_TREE_REAL_DOUBLE ,
572 FEM_TREE_REAL_COUNT
573 };
574 static const char *FEMTreeRealNames[] = { "float" , "double" };
575
576 inline void ReadFEMTreeParameter( BinaryStream &stream , FEMTreeRealType& realType , unsigned int &dimension )
577 {
578 if( !stream.read( realType ) ) MK_THROW( "Failed to read real type" );
579 if( !stream.read( dimension ) ) MK_THROW( "Failed to read dimension" );
580 }
581
582 inline unsigned int* ReadDenseNodeDataSignatures( BinaryStream &stream , unsigned int &dim )
583 {
584 if( !stream.read( dim ) ) MK_THROW( "Failed to read dimension" );
585 unsigned int* femSigs = new unsigned int[dim];
586 if( !stream.read( GetPointer( femSigs , dim ) , dim ) ) MK_THROW( "Failed to read signatures" );
587 return femSigs;
588 }
589
590 // The Derivative method needs static members:
591 // Dim: the dimensionality of the space in which derivatives are evaluated
592 // Size: the total number of derivatives
593 // and static methods:
594 // Index: takes the number of partials along each dimension and returns the index
595 // Factor: takes an index and sets the number of partials along each dimension
596
597 template< typename T > struct TensorDerivatives{ };
598 template< class Real , typename T > struct TensorDerivativeValues{ };
599
600 // Specify the derivatives for each dimension separately
601 template< unsigned int D , unsigned int ... Ds >
602 struct TensorDerivatives< UIntPack< D , Ds ... > >
603 {
604 typedef TensorDerivatives< UIntPack< Ds ... > > _TensorDerivatives;
605 static const unsigned int LastDerivative = UIntPack< D , Ds ... >::template Get< sizeof ... (Ds) >();
606 static const unsigned int Dim = _TensorDerivatives::Dim + 1;
607 static const unsigned int Size = _TensorDerivatives::Size * ( D+1 );
608 static void Factor( unsigned int idx , unsigned int derivatives[Dim] ){ derivatives[0] = idx / _TensorDerivatives::Size ; _TensorDerivatives::Factor( idx % _TensorDerivatives::Size , derivatives+1 ); }
609 static unsigned int Index( const unsigned int derivatives[Dim] ){ return _TensorDerivatives::Index( derivatives + 1 ) + _TensorDerivatives::Size * derivatives[0]; }
610 };
611 template< unsigned int D >
612 struct TensorDerivatives< UIntPack< D > >
613 {
614 static const unsigned int LastDerivative = D;
615 static const unsigned int Dim = 1;
616 static const unsigned int Size = D+1;
617 static void Factor( unsigned int idx , unsigned int derivatives[1] ){ derivatives[0] = idx; }
618 static unsigned int Index( const unsigned int derivatives[1] ){ return derivatives[0]; }
619 };
620 template< class Real , unsigned int ... Ds > struct TensorDerivativeValues< Real , UIntPack< Ds ... > > : public Point< Real , TensorDerivatives< UIntPack< Ds ... > >::Size >{ };
621
622 // Specify the sum of the derivatives
623 template< unsigned int Dim , unsigned int D >
624 struct CumulativeDerivatives
625 {
626 typedef CumulativeDerivatives< Dim , D-1 > _CumulativeDerivatives;
627 static const unsigned int LastDerivative = D;
628 static const unsigned int Size = _CumulativeDerivatives::Size * Dim + 1;
629 static void Factor( unsigned int idx , unsigned int d[Dim] )
630 {
631 if( idx<_CumulativeDerivatives::Size ) return _CumulativeDerivatives::Factor( idx , d );
632 else _Factor( idx - _CumulativeDerivatives::Size , d );
633 }
634 static unsigned int Index( const unsigned int derivatives[Dim] )
635 {
636 unsigned int dCount = 0;
637 for( unsigned int d=0 ; d<Dim ; d++ ) dCount += derivatives[d];
638 if( dCount>=D ) MK_THROW( "More derivatives than allowed" );
639 else if( dCount<D ) return _CumulativeDerivatives::Index( derivatives );
640 else return _CumulativeDerivatives::Size + _Index( derivatives );
641 }
642 protected:
643 static const unsigned int _Size = _CumulativeDerivatives::_Size * Dim;
644 static void _Factor( unsigned int idx , unsigned int d[Dim] )
645 {
646 _CumulativeDerivatives::_Factor( idx % _CumulativeDerivatives::_Size , d );
647 d[ idx / _CumulativeDerivatives::_Size ]++;
648 }
649 static unsigned int _Index( const unsigned int d[Dim] )
650 {
651 unsigned int _d[Dim];
652 memcpy( _d , d , sizeof(_d) );
653 for( unsigned int i=0 ; i<Dim ; i++ ) if( _d[i] )
654 {
655 _d[i]--;
656 return _CumulativeDerivatives::Index( _d ) * Dim + i;
657 }
658 MK_THROW( "No derivatives specified" );
659 return -1;
660 }
661 friend CumulativeDerivatives< Dim , D+1 >;
662 };
663 template< unsigned int Dim >
664 struct CumulativeDerivatives< Dim , 0 >
665 {
666 static const unsigned int LastDerivative = 0;
667 static const unsigned int Size = 1;
668 static void Factor( unsigned int idx , unsigned int d[Dim] ){ memset( d , 0 , sizeof(unsigned int)*Dim ); }
669 static unsigned int Index( const unsigned int derivatives[Dim] ){ return 0; }
670 protected:
671 static const unsigned int _Size = 1;
672 static void _Factor( unsigned int idx , unsigned int d[Dim] ){ memset( d , 0 , sizeof(unsigned int)*Dim ); }
673 friend CumulativeDerivatives< Dim , 1 >;
674 };
675 template< typename Real , unsigned int Dim , unsigned int D > using CumulativeDerivativeValues = Point< Real , CumulativeDerivatives< Dim , D >::Size >;
676
677
678 template< unsigned int Dim , class Real , unsigned int D >
679 CumulativeDerivativeValues< Real , Dim , D > Evaluate( const double dValues[Dim][D+1] )
680 {
681 CumulativeDerivativeValues< Real , Dim , D > v;
682 unsigned int _d[Dim];
683 for( unsigned int d=0 ; d<CumulativeDerivatives< Dim , D >::Size ; d++ )
684 {
685 CumulativeDerivatives< Dim , D >::Factor( d , _d );
686 double value = dValues[0][ _d[0] ];
687 for( unsigned int dd=1 ; dd<Dim ; dd++ ) value *= dValues[dd][ _d[dd] ];
688 v[d] = (Real)value;
689 }
690 return v;
691 }
692
693 template< unsigned int Dim , class Real , typename T , unsigned int D >
694 struct DualPointInfo
695 {
696 Point< Real , Dim > position;
697 Real weight;
698 CumulativeDerivativeValues< T , Dim , D > dualValues;
699 DualPointInfo operator + ( const DualPointInfo& p ) const { return DualPointInfo( position + p.position , dualValues + p.dualValues , weight + p.weight ); }
700 DualPointInfo& operator += ( const DualPointInfo& p ){ position += p.position ; weight += p.weight , dualValues += p.dualValues ; return *this; }
701 DualPointInfo operator * ( Real s ) const { return DualPointInfo( position*s , weight*s , dualValues*s ); }
702 DualPointInfo& operator *= ( Real s ){ position *= s , weight *= s , dualValues *= s ; return *this; }
703 DualPointInfo operator / ( Real s ) const { return DualPointInfo( position/s , weight/s , dualValues/s ); }
704 DualPointInfo& operator /= ( Real s ){ position /= s , weight /= s , dualValues /= s ; return *this; }
705 DualPointInfo( void ) : weight(0) { }
706 DualPointInfo( Point< Real , Dim > p , CumulativeDerivativeValues< T , Dim , D > c , Real w ) { position = p , dualValues = c , weight = w; }
707 };
708 template< unsigned int Dim , class Real , typename Data , typename T , unsigned int D >
709 struct DualPointAndDataInfo
710 {
711 DualPointInfo< Dim , Real , T , D > pointInfo;
712 Data data;
713 DualPointAndDataInfo operator + ( const DualPointAndDataInfo& p ) const { return DualPointAndDataInfo( pointInfo + p.pointInfo , data + p.data ); }
714 DualPointAndDataInfo operator * ( Real s ) const { return DualPointAndDataInfo( pointInfo * s , data * s ); }
715 DualPointAndDataInfo operator / ( Real s ) const { return DualPointAndDataInfo( pointInfo / s , data / s ); }
716 DualPointAndDataInfo& operator += ( const DualPointAndDataInfo& p ){ pointInfo += p.pointInfo ; data += p.data ; return *this; }
717 DualPointAndDataInfo& operator *= ( Real s ) { pointInfo *= s , data *= s ; return *this; }
718 DualPointAndDataInfo& operator /= ( Real s ) { pointInfo /= s , data /= s ; return *this; }
719 DualPointAndDataInfo( void ){ }
720 DualPointAndDataInfo( DualPointInfo< Dim , Real , T , D > p , Data d ) { pointInfo = p , data = d; }
721 };
722 template< unsigned int Dim , class Real , typename T , unsigned int D >
723 struct DualPointInfoBrood
724 {
725 DualPointInfo< Dim , Real , T , D >& operator[]( size_t idx ){ return _dpInfo[idx]; }
726 const DualPointInfo< Dim , Real , T , D >& operator[]( size_t idx ) const { return _dpInfo[idx]; }
727 void finalize( void ){ _size = 0 ; for( unsigned int i=0 ; i<(1<<Dim) ; i++ ) if( _dpInfo[i].weight>0 ) _dpInfo[_size++] = _dpInfo[i]; }
728 unsigned int size( void ) const { return _size; }
729
730 DualPointInfoBrood operator + ( const DualPointInfoBrood& p ) const { DualPointInfoBrood d ; for( unsigned int i=0 ; i<(1<<Dim) ; i++ ) d._dpInfo[i] = _dpInfo[i] + p._dpInfo[i] ; return d; }
731 DualPointInfoBrood operator * ( Real s ) const { DualPointInfoBrood d ; for( unsigned int i=0 ; i<(1<<Dim) ; i++ ) d._dpInfo[i] = _dpInfo[i] * s ; return d; }
732 DualPointInfoBrood operator / ( Real s ) const { DualPointInfoBrood d ; for( unsigned int i=0 ; i<(1<<Dim) ; i++ ) d._dpInfo[i] = _dpInfo[i] / s ; return d; }
733 DualPointInfoBrood& operator += ( const DualPointInfoBrood& p ){ for( unsigned int i=0 ; i<(1<<Dim) ; i++ ) _dpInfo[i] += p._dpInfo[i] ; return *this; }
734 DualPointInfoBrood& operator *= ( Real s ) { for( unsigned int i=0 ; i<(1<<Dim) ; i++ ) _dpInfo[i] *= s ; return *this; }
735 DualPointInfoBrood& operator /= ( Real s ) { for( unsigned int i=0 ; i<(1<<Dim) ; i++ ) _dpInfo[i] /= s ; return *this; }
736 protected:
737 DualPointInfo< Dim , Real , T , D > _dpInfo[1<<Dim];
738 unsigned int _size;
739 };
740 template< unsigned int Dim , class Real , typename Data , typename T , unsigned int D >
741 struct DualPointAndDataInfoBrood
742 {
743 DualPointAndDataInfo< Dim , Real , Data , T , D >& operator[]( size_t idx ){ return _dpInfo[idx]; }
744 const DualPointAndDataInfo< Dim , Real , Data , T , D >& operator[]( size_t idx ) const { return _dpInfo[idx]; }
745 void finalize( void ){ _size = 0 ; for( unsigned int i=0 ; i<(1<<Dim) ; i++ ) if( _dpInfo[i].pointInfo.weight>0 ) _dpInfo[_size++] = _dpInfo[i]; }
746 unsigned int size( void ) const { return _size; }
747
748 DualPointAndDataInfoBrood operator + ( const DualPointAndDataInfoBrood& p ) const { DualPointAndDataInfoBrood d ; for( unsigned int i=0 ; i<(1<<Dim) ; i++ ) d._dpInfo[i] = _dpInfo[i] + p._dpInfo[i] ; return d; }
749 DualPointAndDataInfoBrood operator * ( Real s ) const { DualPointAndDataInfoBrood d ; for( unsigned int i=0 ; i<(1<<Dim) ; i++ ) d._dpInfo[i] = _dpInfo[i] * s ; return d; }
750 DualPointAndDataInfoBrood operator / ( Real s ) const { DualPointAndDataInfoBrood d ; for( unsigned int i=0 ; i<(1<<Dim) ; i++ ) d._dpInfo[i] = _dpInfo[i] / s ; return d; }
751 DualPointAndDataInfoBrood& operator += ( const DualPointAndDataInfoBrood& p ){ for( unsigned int i=0 ; i<(1<<Dim) ; i++ ) _dpInfo[i] += p._dpInfo[i] ; return *this; }
752 DualPointAndDataInfoBrood& operator *= ( Real s ) { for( unsigned int i=0 ; i<(1<<Dim) ; i++ ) _dpInfo[i] *= s ; return *this; }
753 DualPointAndDataInfoBrood& operator /= ( Real s ) { for( unsigned int i=0 ; i<(1<<Dim) ; i++ ) _dpInfo[i] /= s ; return *this; }
754 protected:
755 DualPointAndDataInfo< Dim , Real , Data , T , D > _dpInfo[1<<Dim];
756 unsigned int _size;
757 };
758
759
760 ////////////////////////////
761 // The virtual integrator //
762 ////////////////////////////
763 struct BaseFEMIntegrator
764 {
765 template< typename TDegreePack > struct System{};
766 template< typename TDegreePack > struct RestrictionProlongation{};
767 template< typename TDegreePack , typename CDegreePack , unsigned int CDim > struct Constraint{};
768 template< typename TDegreePack > struct SystemConstraint{};
769 template< typename TDegreePack > struct PointEvaluator{};
770
771 protected:
772 template< unsigned int Degree , unsigned int ... Degrees >
773 static typename std::enable_if< sizeof ... ( Degrees )==0 , bool >::type _IsSupported( UIntPack< Degree , Degrees ... > , unsigned int femDepth , const int femOffset[] , unsigned int spaceDepth , const int spaceOffset[] )
774 {
775 int femRes = 1<<femDepth , spaceRes = 1<<spaceDepth;
776 int femBegin = ( 0 + femOffset[0] + BSplineSupportSizes< Degree >::SupportStart ) * spaceRes;
777 int femEnd = ( 1 + femOffset[0] + BSplineSupportSizes< Degree >::SupportEnd ) * spaceRes;
778 int spaceBegin = ( 0 + spaceOffset[0] + BSplineSupportSizes< 0 >::SupportStart ) * femRes;
779 int spaceEnd = ( 1 + spaceOffset[0] + BSplineSupportSizes< 0 >::SupportEnd ) * femRes;
780 return spaceBegin<femEnd && spaceEnd>femBegin;
781 }
782 template< unsigned int Degree , unsigned int ... Degrees >
783 static typename std::enable_if< sizeof ... ( Degrees )!=0 , bool >::type _IsSupported( UIntPack< Degree , Degrees ... > , unsigned int femDepth , const int femOffset[] , unsigned int spaceDepth , const int spaceOffset[] )
784 {
785 int femRes = 1<<femDepth , spaceRes = 1<<spaceDepth;
786 int femBegin = ( 0 + femOffset[0] + BSplineSupportSizes< Degree >::SupportStart ) * spaceRes;
787 int femEnd = ( 1 + femOffset[0] + BSplineSupportSizes< Degree >::SupportEnd ) * spaceRes;
788 int spaceBegin = ( 0 + spaceOffset[0] + BSplineSupportSizes< 0 >::SupportStart ) * femRes;
789 int spaceEnd = ( 1 + spaceOffset[0] + BSplineSupportSizes< 0 >::SupportEnd ) * femRes;
790 return ( spaceBegin<femEnd && spaceEnd>femBegin ) && _IsSupported( UIntPack< Degrees ... >() , femDepth , femOffset+1 , spaceDepth , spaceOffset+1 );
791 }
792 template< unsigned int Degree , unsigned int ... Degrees >
793 static typename std::enable_if< sizeof ... ( Degrees )==0 , bool >::type _IsInteriorlySupported( UIntPack< Degree , Degrees ... > , unsigned int depth , const int off[] )
794 {
795 int begin , end;
796 BSplineSupportSizes< Degree >::InteriorSupportedSpan( depth , begin , end );
797 return off[0]>=begin && off[0]<end;
798 }
799 template< unsigned int Degree , unsigned int ... Degrees >
800 static typename std::enable_if< sizeof ... ( Degrees )!=0 , bool >::type _IsInteriorlySupported( UIntPack< Degree , Degrees ... > , unsigned int depth , const int off[] )
801 {
802 int begin , end;
803 BSplineSupportSizes< Degree >::InteriorSupportedSpan( depth , begin , end );
804 return ( off[0]>=begin && off[0]<end ) && _IsInteriorlySupported( UIntPack< Degrees ... >() , depth , off+1 );
805 }
806 template< unsigned int Degree , unsigned int ... Degrees >
807 static typename std::enable_if< sizeof ... ( Degrees )==0 , bool >::type _IsInteriorlySupported( UIntPack< Degree , Degrees ... > , unsigned int depth , const int off[] , const double begin[] , const double end[] )
808 {
809 int res = 1<<depth;
810 double b = ( 0. + off[0] + BSplineSupportSizes< Degree >::SupportStart ) / res;
811 double e = ( 1. + off[0] + BSplineSupportSizes< Degree >::SupportEnd ) / res;
812 return b>=begin[0] && e<=end[0];
813 }
814 template< unsigned int Degree , unsigned int ... Degrees >
815 static typename std::enable_if< sizeof ... ( Degrees )!=0 , bool >::type _IsInteriorlySupported( UIntPack< Degree , Degrees ... > , unsigned int depth , const int off[] , const double begin[] , const double end[] )
816 {
817 int res = 1<<depth;
818 double b = ( 0. + off[0] + BSplineSupportSizes< Degree >::SupportStart ) / res;
819 double e = ( 1. + off[0] + BSplineSupportSizes< Degree >::SupportEnd ) / res;
820 return b>=begin[0] && e<=end[0] && _IsInteriorlySupported( UIntPack< Degrees ... >() , depth , off+1 , begin+1 , end+1 );
821 }
822 template< unsigned int Degree1 , unsigned int ... Degrees1 , unsigned int Degree2 , unsigned int ... Degrees2 >
823 static typename std::enable_if< sizeof ... ( Degrees1 )==0 >::type _InteriorOverlappedSpan( UIntPack< Degree1 , Degrees1 ... > , UIntPack< Degree2 , Degrees2 ... > , int depth , int begin[] , int end[] )
824 {
825 BSplineIntegrationData< FEMDegreeAndBType< Degree1 , BOUNDARY_NEUMANN >::Signature , FEMDegreeAndBType< Degree2 , BOUNDARY_NEUMANN >::Signature >::InteriorOverlappedSpan( depth , begin[0] , end[0] );
826 }
827 template< unsigned int Degree1 , unsigned int ... Degrees1 , unsigned int Degree2 , unsigned int ... Degrees2 >
828 static typename std::enable_if< sizeof ... ( Degrees1 )!=0 >::type _InteriorOverlappedSpan( UIntPack< Degree1 , Degrees1 ... > , UIntPack< Degree2 , Degrees2 ... > , int depth , int begin[] , int end[] )
829 {
830 BSplineIntegrationData< FEMDegreeAndBType< Degree1 , BOUNDARY_NEUMANN >::Signature , FEMDegreeAndBType< Degree2 , BOUNDARY_NEUMANN >::Signature >::InteriorOverlappedSpan( depth , begin[0] , end[0] );
831 _InteriorOverlappedSpan( UIntPack< Degrees1 ... >() , UIntPack< Degrees2 ... >() , depth , begin+1 , end+1 );
832 }
833 template< unsigned int Degree1 , unsigned int ... Degrees1 , unsigned int Degree2 , unsigned int ... Degrees2 >
834 static typename std::enable_if< sizeof ... ( Degrees1 )==0 , bool >::type _IsInteriorlyOverlapped( UIntPack< Degree1 , Degrees1 ... > , UIntPack< Degree2 , Degrees2 ... > , unsigned int depth , const int off[] )
835 {
836 int begin , end;
837 BSplineIntegrationData< FEMDegreeAndBType< Degree1 , BOUNDARY_NEUMANN >::Signature , FEMDegreeAndBType< Degree2 , BOUNDARY_NEUMANN >::Signature >::InteriorOverlappedSpan( depth , begin , end );
838 return off[0]>= begin && off[0]<end;
839 }
840 template< unsigned int Degree1 , unsigned int ... Degrees1 , unsigned int Degree2 , unsigned int ... Degrees2 >
841 static typename std::enable_if< sizeof ... ( Degrees1 )!=0 , bool >::type _IsInteriorlyOverlapped( UIntPack< Degree1 , Degrees1 ... > , UIntPack< Degree2 , Degrees2 ... > , unsigned int depth , const int off[] )
842 {
843 int begin , end;
844 BSplineIntegrationData< FEMDegreeAndBType< Degree1 , BOUNDARY_NEUMANN >::Signature , FEMDegreeAndBType< Degree2 , BOUNDARY_NEUMANN >::Signature >::InteriorOverlappedSpan( depth , begin , end );
845 return ( off[0]>= begin && off[0]<end ) && _IsInteriorlyOverlapped( UIntPack< Degrees1 ... >() , UIntPack< Degrees2 ... >() , depth , off+1 );
846 }
847 template< unsigned int Degree1 , unsigned int ... Degrees1 , unsigned int Degree2 , unsigned int ... Degrees2 >
848 static typename std::enable_if< sizeof ... ( Degrees1 )==0 >::type _ParentOverlapBounds( UIntPack< Degree1 , Degrees1 ... > , UIntPack< Degree2 , Degrees2 ... > , unsigned int depth , const int off[] , int start[] , int end[] )
849 {
850 const int OverlapStart = BSplineOverlapSizes< Degree1 , Degree2 >::OverlapStart;
851 start[0] = BSplineOverlapSizes< Degree1 , Degree2 >::ParentOverlapStart[ off[0] & 1 ] - OverlapStart;
852 end [0] = BSplineOverlapSizes< Degree1 , Degree2 >::ParentOverlapEnd [ off[0] & 1 ] - OverlapStart + 1;
853 }
854 template< unsigned int Degree1 , unsigned int ... Degrees1 , unsigned int Degree2 , unsigned int ... Degrees2 >
855 static typename std::enable_if< sizeof ... ( Degrees1 )!=0 >::type _ParentOverlapBounds( UIntPack< Degree1 , Degrees1 ... > , UIntPack< Degree2 , Degrees2 ... > , unsigned int depth , const int off[] , int start[] , int end[] )
856 {
857 const int OverlapStart = BSplineOverlapSizes< Degree1 , Degree2 >::OverlapStart;
858 start[0] = BSplineOverlapSizes< Degree1 , Degree2 >::ParentOverlapStart[ off[0] & 1 ] - OverlapStart;
859 end [0] = BSplineOverlapSizes< Degree1 , Degree2 >::ParentOverlapEnd [ off[0] & 1 ] - OverlapStart + 1;
860 _ParentOverlapBounds( UIntPack< Degrees1 ... >() , UIntPack< Degrees2 ... >() , depth , off+1 , start+1 , end+1 );
861 }
862 template< unsigned int Degree1 , unsigned int ... Degrees1 , unsigned int Degree2 , unsigned int ... Degrees2 >
863 static typename std::enable_if< sizeof ... ( Degrees1 )==0 >::type _ParentOverlapBounds( UIntPack< Degree1 , Degrees1 ... > , UIntPack< Degree2 , Degrees2 ... > , int corner , int start[] , int end[] )
864 {
865 const int OverlapStart = BSplineOverlapSizes< Degree1 , Degree2 >::OverlapStart;
866 start[0] = BSplineOverlapSizes< Degree1 , Degree2 >::ParentOverlapStart[ corner & 1 ] - OverlapStart;
867 end [0] = BSplineOverlapSizes< Degree1 , Degree2 >::ParentOverlapEnd [ corner & 1 ] - OverlapStart + 1;
868 }
869 template< unsigned int Degree1 , unsigned int ... Degrees1 , unsigned int Degree2 , unsigned int ... Degrees2 >
870 static typename std::enable_if< sizeof ... ( Degrees1 )!=0 >::type _ParentOverlapBounds( UIntPack< Degree1 , Degrees1 ... > , UIntPack< Degree2 , Degrees2 ... > , int corner , int start[] , int end[] )
871 {
872 const int OverlapStart = BSplineOverlapSizes< Degree1 , Degree2 >::OverlapStart;
873 start[0] = BSplineOverlapSizes< Degree1 , Degree2 >::ParentOverlapStart[ corner & 1 ] - OverlapStart;
874 end [0] = BSplineOverlapSizes< Degree1 , Degree2 >::ParentOverlapEnd [ corner & 1 ] - OverlapStart + 1;
875 _ParentOverlapBounds( UIntPack< Degrees1 ... >() , UIntPack< Degrees2 ... >() , corner>>1 , start+1 , end+1 );
876 }
877
878 public:
879 template< unsigned int ... Degrees >
880 static bool IsSupported( UIntPack< Degrees ... > , int femDepth , const int femOffset[] , int spaceDepth , const int spaceOffset[] ){ return femDepth>=0 && spaceDepth>=0 && _IsSupported( UIntPack< Degrees ... >() , femDepth , femOffset , spaceDepth , spaceOffset ); }
881 template< unsigned int ... Degrees >
882 static bool IsInteriorlySupported( UIntPack< Degrees ... > , int depth , const int offset[] ){ return depth>=0 && _IsInteriorlySupported( UIntPack< Degrees ... >() , depth , offset ); }
883 template< unsigned int ... Degrees >
884 static bool IsInteriorlySupported( UIntPack< Degrees ... > , int depth , const int offset[] , const double begin[] , const double end[] ){ return depth>=0 && _IsInteriorlySupported( UIntPack< Degrees ... >() , depth , offset , begin , end ); }
885
886 template< unsigned int ... Degrees1 , unsigned int ... Degrees2 >
887 static void InteriorOverlappedSpan( UIntPack< Degrees1 ... > , UIntPack< Degrees2 ... > , int depth , int begin[] , int end[] )
888 {
889 static_assert( sizeof ... ( Degrees1 ) == sizeof ... ( Degrees2 ) , "[ERROR] Dimensions don't match" );
890 _InteriorOverlappedSpan( UIntPack< Degrees1 ... >() , UIntPack< Degrees2 ... >() , depth , begin , end );
891 }
892 template< unsigned int ... Degrees1 , unsigned int ... Degrees2 >
893 static bool IsInteriorlyOverlapped( UIntPack< Degrees1 ... > , UIntPack< Degrees2 ... > , int depth , const int offset[] )
894 {
895 static_assert( sizeof ... ( Degrees1 ) == sizeof ... ( Degrees2 ) , "[ERROR] Dimensions don't match" );
896 return depth>=0 && _IsInteriorlyOverlapped( UIntPack< Degrees1 ... >() , UIntPack< Degrees2 ... >() , depth , offset );
897 }
898
899 template< unsigned int ... Degrees1 , unsigned int ... Degrees2 >
900 static void ParentOverlapBounds( UIntPack< Degrees1 ... > , UIntPack< Degrees2 ... > , int depth , const int offset[] , int start[] , int end[] )
901 {
902 static_assert( sizeof ... ( Degrees1 ) == sizeof ... ( Degrees2 ) , "[ERROR] Dimensions don't match" );
903 if( depth>0 ) _ParentOverlapBounds( UIntPack< Degrees1 ... >() , UIntPack< Degrees2 ... >() , depth , offset , start , end );
904 }
905 template< unsigned int ... Degrees1 , unsigned int ... Degrees2 >
906 static void ParentOverlapBounds( UIntPack< Degrees1 ... > , UIntPack< Degrees2 ... > , int corner , int start[] , int end[] )
907 {
908 static_assert( sizeof ... ( Degrees1 ) == sizeof ... ( Degrees2 ) , "[ERROR] Dimensions don't match" );
909 _ParentOverlapBounds( UIntPack< Degrees1 ... >() , UIntPack< Degrees2 ... >() , corner , start , end );
910 }
911
912 template< unsigned int Dim >
913 struct PointEvaluatorState
914 {
915 virtual double value( const int offset[] , const unsigned int d[] ) const = 0;
916 virtual double subValue( const int offset[] , const unsigned int d[] ) const = 0;
917 template< class Real , typename DerivativeType >
918 Point< Real , DerivativeType::Size > dValues( const int offset[] ) const
919 {
920 Point< Real , DerivativeType::Size > v;
921 unsigned int _d[Dim];
922 for( int d=0 ; d<DerivativeType::Size ; d++ )
923 {
924 DerivativeType::Factor( d , _d );
925 v[d] = (Real)value( offset , _d );
926 }
927 return v;
928 }
929 template< class Real , typename DerivativeType >
930 Point< Real , DerivativeType::LastDerivative+1 > partialDotDValues( Point< Real , DerivativeType::Size > v , const int offset[] ) const
931 {
932 Point< Real , DerivativeType::LastDerivative+1 > dot;
933 unsigned int _d[Dim];
934 for( int d=0 ; d<DerivativeType::Size ; d++ )
935 {
936 DerivativeType::Factor( d , _d );
937 dot[ _d[Dim-1] ] += (Real)( subValue( offset , _d ) * v[d] );
938 }
939 return dot;
940 }
941 };
942
943 template< unsigned int ... TDegrees >
944 struct PointEvaluator< UIntPack< TDegrees ... > >
945 {
946 static const unsigned int Dim = sizeof ... ( TDegrees );
947 };
948
949 template< unsigned int ... TDegrees >
950 struct RestrictionProlongation< UIntPack< TDegrees ... > >
951 {
952 virtual void init( void ){ }
953 virtual double upSampleCoefficient( const int pOff[] , const int cOff[] ) const = 0;
954
955 typedef DynamicWindow< double , UIntPack< ( - BSplineSupportSizes< TDegrees >::DownSample0Start + BSplineSupportSizes< TDegrees >::DownSample1End + 1 ) ... > > DownSampleStencil;
956 struct UpSampleStencil : public DynamicWindow< double , UIntPack< BSplineSupportSizes< TDegrees >::UpSampleSize ... > > { };
957 struct DownSampleStencils : public DynamicWindow< DownSampleStencil , IsotropicUIntPack< sizeof ... ( TDegrees ) , 2 > > { };
958
959 void init( int highDepth ){ _highDepth = highDepth ; init(); }
960 void setStencil ( UpSampleStencil & stencil ) const;
961 void setStencils( DownSampleStencils& stencils ) const;
962 int highDepth( void ) const { return _highDepth; }
963
964 protected:
965 int _highDepth;
966 };
967
968
969 template< unsigned int ... TDegrees >
970 struct System< UIntPack< TDegrees ... > >
971 {
972 virtual void init( void ){ }
973 virtual double ccIntegrate( const int off1[] , const int off2[] ) const = 0;
974 virtual double pcIntegrate( const int off1[] , const int off2[] ) const = 0;
975 virtual bool vanishesOnConstants( void ) const { return false; }
976 virtual RestrictionProlongation< UIntPack< TDegrees ... > >& restrictionProlongation( void ) = 0;
977
978 struct CCStencil : public DynamicWindow< double , UIntPack< BSplineOverlapSizes< TDegrees , TDegrees >::OverlapSize ... > >{ };
979 #ifdef SHOW_WARNINGS
980 #pragma message ( "[WARNING] Why are the parent/child stencils so big?" )
981 #endif // SHOW_WARNINGS
982 struct PCStencils : public DynamicWindow< CCStencil , IsotropicUIntPack< sizeof ... ( TDegrees ) , 2 > >{ };
983
984 void init( int highDepth ){ _highDepth = highDepth ; init(); }
985 template< bool IterateFirst > void setStencil ( CCStencil & stencil ) const;
986 template< bool IterateFirst > void setStencils( PCStencils& stencils ) const;
987 int highDepth( void ) const { return _highDepth; }
988
989 protected:
990 int _highDepth;
991 };
992
993 template< unsigned int ... TDegrees , unsigned int ... CDegrees , unsigned int CDim >
994 struct Constraint< UIntPack< TDegrees ... > , UIntPack< CDegrees ... > , CDim >
995 {
996 static_assert( sizeof...(TDegrees)==sizeof...(CDegrees) , "[ERROR] BaseFEMIntegrator::Constraint: Test and constraint dimensions don't match" );
997
998 virtual void init( void ){ ; }
999 virtual Point< double , CDim > ccIntegrate( const int off1[] , const int off2[] ) const = 0;
1000 virtual Point< double , CDim > pcIntegrate( const int off1[] , const int off2[] ) const = 0;
1001 virtual Point< double , CDim > cpIntegrate( const int off1[] , const int off2[] ) const = 0;
1002 virtual RestrictionProlongation< UIntPack< TDegrees ... > >& tRestrictionProlongation( void ) = 0;
1003 virtual RestrictionProlongation< UIntPack< CDegrees ... > >& cRestrictionProlongation( void ) = 0;
1004
1005 struct CCStencil : public DynamicWindow< Point< double , CDim > , UIntPack< BSplineOverlapSizes< TDegrees , CDegrees >::OverlapSize ... > >{ };
1006 #ifdef SHOW_WARNINGS
1007 #pragma message ( "[WARNING] Why are the parent/child stencils so big?" )
1008 #endif // SHOW_WARNINGS
1009 struct PCStencils : public DynamicWindow< CCStencil , IsotropicUIntPack< sizeof ... ( TDegrees ) , 2 > >{ };
1010 struct CPStencils : public DynamicWindow< CCStencil , IsotropicUIntPack< sizeof ... ( TDegrees ) , 2 > >{ };
1011
1012 void init( int highDepth ){ _highDepth = highDepth ; init(); }
1013 template< bool IterateFirst > void setStencil ( CCStencil & stencil ) const;
1014 template< bool IterateFirst > void setStencils( PCStencils& stencils ) const;
1015 template< bool IterateFirst > void setStencils( CPStencils& stencils ) const;
1016 int highDepth( void ) const { return _highDepth; }
1017
1018 protected:
1019 int _highDepth;
1020 };
1021
1022 template< unsigned int ... TDegrees >
1023 struct SystemConstraint< UIntPack< TDegrees ... > > : public Constraint< UIntPack< TDegrees ... > , UIntPack< TDegrees ... > , 1 >
1024 {
1025 typedef Constraint< UIntPack< TDegrees ... > , UIntPack< TDegrees ... > , 1 > Base;
1026 SystemConstraint( System< UIntPack< TDegrees ... > >& sys ) : _sys( sys ){;}
1027 void init( void ){ _sys.init( Base::highDepth() ) ; _sys.init(); }
1028 Point< double , 1 > ccIntegrate( const int off1[] , const int off2[] ) const{ return Point< double , 1 >( _sys.ccIntegrate( off1 , off2 ) ); }
1029 Point< double , 1 > pcIntegrate( const int off1[] , const int off2[] ) const{ return Point< double , 1 >( _sys.pcIntegrate( off1 , off2 ) ); }
1030 Point< double , 1 > cpIntegrate( const int off1[] , const int off2[] ) const{ return Point< double , 1 >( _sys.pcIntegrate( off2 , off1 ) ); }
1031 RestrictionProlongation< UIntPack< TDegrees ... > >& tRestrictionProlongation( void ){ return _sys.restrictionProlongation(); }
1032 RestrictionProlongation< UIntPack< TDegrees ... > >& cRestrictionProlongation( void ){ return _sys.restrictionProlongation(); }
1033 protected:
1034 System< UIntPack< TDegrees ... > >& _sys;
1035 };
1036 };
1037
1038 /////////////////////////////////////////////////
1039 // An implementation of the virtual integrator //
1040 /////////////////////////////////////////////////
1041 struct FEMIntegrator
1042 {
1043 protected:
1044 template< unsigned int FEMSig , unsigned int ... FEMSigs >
1045 static typename std::enable_if< sizeof ... ( FEMSigs )==0 , bool >::type _IsValidFEMNode( UIntPack< FEMSig , FEMSigs ... > , unsigned int depth , const int offset[] )
1046 {
1047 return !BSplineEvaluationData< FEMSig >::OutOfBounds( depth , offset[0] );
1048 }
1049 template< unsigned int FEMSig , unsigned int ... FEMSigs >
1050 static typename std::enable_if< sizeof ... ( FEMSigs )!=0 , bool >::type _IsValidFEMNode( UIntPack< FEMSig , FEMSigs ... > , unsigned int depth , const int offset[] )
1051 {
1052 return !BSplineEvaluationData< FEMSig >::OutOfBounds( depth , offset[0] ) && _IsValidFEMNode( UIntPack< FEMSigs ... >() , depth , offset+1 );
1053 }
1054 template< unsigned int FEMSig , unsigned ... FEMSigs >
1055 static typename std::enable_if< sizeof ... ( FEMSigs )==0 , bool >::type _IsOutOfBounds( UIntPack< FEMSig , FEMSigs ... > , unsigned int depth , const int offset[] )
1056 {
1057 return BSplineEvaluationData< FEMSig >::OutOfBounds( depth , offset[0] );
1058 }
1059 template< unsigned int FEMSig , unsigned ... FEMSigs >
1060 static typename std::enable_if< sizeof ... ( FEMSigs )!=0 , bool >::type _IsOutOfBounds( UIntPack< FEMSig , FEMSigs ... > , unsigned int depth , const int offset[] )
1061 {
1062 return BSplineEvaluationData< FEMSig >::OutOfBounds( depth , offset[0] ) || _IsOutOfBounds( UIntPack< FEMSigs ... >() , depth , offset+1 );
1063 }
1064 template< unsigned int FEMSig , unsigned int ... FEMSigs >
1065 static typename std::enable_if< sizeof ... ( FEMSigs )==0 >::type _BSplineBegin( UIntPack< FEMSig , FEMSigs ... > , unsigned int depth , int begin[] )
1066 {
1067 begin[0] = BSplineEvaluationData< FEMSig >::Begin( depth );
1068 }
1069 template< unsigned int FEMSig , unsigned int ... FEMSigs >
1070 static typename std::enable_if< sizeof ... ( FEMSigs )!=0 >::type _BSplineBegin( UIntPack< FEMSig , FEMSigs ... > , unsigned int depth , int begin[] )
1071 {
1072 begin[0] = BSplineEvaluationData< FEMSig >::Begin( depth ) ; _BSplineBegin( UIntPack< FEMSigs ... >() , depth , begin+1 );
1073 }
1074 template< unsigned int FEMSig , unsigned int ... FEMSigs >
1075 static typename std::enable_if< sizeof ... ( FEMSigs )==0 >::type _BSplineEnd( UIntPack< FEMSig , FEMSigs ... > , unsigned int depth , int end[] )
1076 {
1077 end[0] = BSplineEvaluationData< FEMSig >::End( depth );
1078 }
1079 template< unsigned int FEMSig , unsigned int ... FEMSigs >
1080 static typename std::enable_if< sizeof ... ( FEMSigs )!=0 >::type _BSplineEnd( UIntPack< FEMSig , FEMSigs ... > , unsigned int depth , int end[] )
1081 {
1082 end[0] = BSplineEvaluationData< FEMSig >::End( depth ) ; _BSplineEnd( UIntPack< FEMSigs ... >() , depth , end+1 );
1083 }
1084 template< unsigned int FEMSig , unsigned int ... FEMSigs >
1085 static typename std::enable_if< sizeof ... ( FEMSigs )==0 , double >::type _Integral( UIntPack< FEMSig , FEMSigs ... > , unsigned int depth , const int offset[] , const double begin[] , const double end[] )
1086 {
1087 return BSplineEvaluationData< FEMSig >::Integral( depth , offset[0] , begin[0] , end[0] , 0 );
1088 }
1089 template< unsigned int FEMSig , unsigned int ... FEMSigs >
1090 static typename std::enable_if< sizeof ... ( FEMSigs )!=0 , double >::type _Integral( UIntPack< FEMSig , FEMSigs ... > , unsigned int depth , const int offset[] , const double begin[] , const double end[] )
1091 {
1092 return BSplineEvaluationData< FEMSig >::Integral( depth , offset[0] , begin[0] , end[0] , 0 ) * _Integral( UIntPack< FEMSigs ... >() , depth , offset+1 , begin+1 , end+1 );
1093 }
1094 public:
1095 template< unsigned int ... FEMSigs >
1096 static double Integral( UIntPack< FEMSigs ... > , int depth , const int offset[] , const double begin[] , const double end[] )
1097 {
1098 if( depth<0 ) return 0;
1099 else return _Integral( UIntPack< FEMSigs ... >() , depth , offset , begin , end );
1100 }
1101 template< unsigned int ... FEMSigs > static bool IsValidFEMNode( UIntPack< FEMSigs ... > , int depth , const int offset[] ){ return _IsValidFEMNode( UIntPack< FEMSigs ... >() , depth , offset ); }
1102 template< unsigned int ... FEMSigs > static bool IsOutOfBounds( UIntPack< FEMSigs ... > , int depth , const int offset[] ){ return depth<0 || _IsOutOfBounds( UIntPack< FEMSigs ... >() , depth , offset ); }
1103 template< unsigned int ... FEMSigs > static void BSplineBegin( UIntPack< FEMSigs ... > , int depth , int begin[] ){ if( depth>=0 ) _BSplineBegin( UIntPack< FEMSigs ... >() , depth , begin ); }
1104 template< unsigned int ... FEMSigs > static void BSplineEnd ( UIntPack< FEMSigs ... > , int depth , int end [] ){ if( depth>=0 ) _BSplineEnd ( UIntPack< FEMSigs ... >() , depth , end ); }
1105
1106 template< typename TSignatures , typename TDerivatives > struct System{};
1107 template< typename TSignatures , typename TDerivatives , typename CSignatures , typename CDerivatives , unsigned int CDim > struct Constraint{};
1108 template< typename TSignatures , typename TDerivatives , typename CSignatures , typename CDerivatives > struct ScalarConstraint{};
1109 template< typename TSignatures > struct RestrictionProlongation{};
1110 template< typename TSignatures , typename TDerivatives > struct PointEvaluator{};
1111 template< typename TSignatures , typename TDerivatives > struct PointEvaluatorState{};
1112
1113 template< unsigned int ... TSignatures , unsigned int ... TDs >
1114 struct PointEvaluatorState< UIntPack< TSignatures ... > , UIntPack< TDs ... > > : public BaseFEMIntegrator::template PointEvaluatorState< sizeof ... ( TSignatures ) >
1115 {
1116 static_assert( sizeof...(TSignatures)==sizeof...(TDs) , "[ERROR] Degree and derivative dimensions don't match" );
1117 static_assert( ParameterPack::Comparison< UIntPack< FEMSignature< TSignatures >::Degree ... > , UIntPack< TDs ... > >::GreaterThanOrEqual , "[ERROR] PointEvaluatorState: More derivatives than degrees" );
1118
1119 static const unsigned int Dim = sizeof...(TSignatures);
1120
1121 double value ( const int offset[] , const unsigned int derivatives[] ) const { return _value< Dim >( offset , derivatives ); }
1122 double subValue( const int offset[] , const unsigned int derivatives[] ) const { return _value< Dim-1 >( offset , derivatives ); }
1123 // Bypassing the "auto" keyword
1124 template< unsigned int _Dim >
1125 const double (*(values)( void ) const )[ UIntPack< TDs ... >::template Get< _Dim >()+1 ] { return std::template get< _Dim >( _oneDValues ).values; }
1126 protected:
1127 int _pointOffset[Dim];
1128
1129 template< unsigned int Degree , unsigned int D > struct _OneDValues
1130 {
1131 double values[ BSplineSupportSizes< Degree >::SupportSize ][ D+1 ];
1132 double value( int dOff , unsigned int d ) const
1133 {
1134 if( dOff>=-BSplineSupportSizes< Degree >::SupportEnd && dOff<=-BSplineSupportSizes< Degree >::SupportStart && d<=D ) return values[ dOff+BSplineSupportSizes< Degree >::SupportEnd][d];
1135 else return 0;
1136 }
1137 };
1138 std::tuple< _OneDValues< FEMSignature< TSignatures >::Degree , TDs > ... > _oneDValues;
1139 template< unsigned int MaxDim=Dim , unsigned int I=0 > typename std::enable_if< I==MaxDim , double >::type _value( const int off[] , const unsigned int d[] ) const { return 1.; }
1140 template< unsigned int MaxDim=Dim , unsigned int I=0 > typename std::enable_if< I!=MaxDim , double >::type _value( const int off[] , const unsigned int d[] ) const { return std::get< I >( _oneDValues ).value( off[I]-_pointOffset[I] , d[I] ) * _value< MaxDim , I+1 >( off , d ); }
1141 template< typename T1 , typename T2 > friend struct PointEvaluator;
1142 };
1143
1144 template< unsigned int ... TSignatures , unsigned int ... TDs >
1145 struct PointEvaluator< UIntPack< TSignatures ... > , UIntPack< TDs ... > > : public BaseFEMIntegrator::template PointEvaluator< UIntPack< FEMSignature< TSignatures >::Degree ... > >
1146 {
1147 static_assert( sizeof...(TSignatures)==sizeof...(TDs) , "[ERROR] PointEvaluator: Degree and derivative dimensions don't match" );
1148 static_assert( ParameterPack::Comparison< UIntPack< FEMSignature< TSignatures >::Degree ... > , UIntPack< TDs ... > >::GreaterThanOrEqual , "[ERROR] PointEvaluator: More derivatives than degrees" );
1149
1150 static const unsigned int Dim = sizeof ... ( TSignatures );
1151
1152 typedef typename BaseFEMIntegrator::template PointEvaluator< UIntPack< FEMSignature< TSignatures >::Degree ... > > Base;
1153
1154 PointEvaluator( unsigned int maxDepth ) : _maxDepth( maxDepth ) { _init(); }
1155 template< unsigned int ... EDs >
1156 void initEvaluationState( Point< double , Dim > p , unsigned int depth , PointEvaluatorState< UIntPack< TSignatures ... > , UIntPack< EDs ... > >& state ) const
1157 {
1158 unsigned int res = 1<<depth;
1159 for( int d=0 ; d<Dim ; d++ ) state._pointOffset[d] = (int)( p[d] * res );
1160 initEvaluationState( p , depth , state._pointOffset , state );
1161 }
1162 template< unsigned int ... EDs >
1163 void initEvaluationState( Point< double , Dim > p , unsigned int depth , const int* offset , PointEvaluatorState< UIntPack< TSignatures ... > , UIntPack< EDs ... > >& state ) const
1164 {
1165 static_assert( ParameterPack::Comparison< UIntPack< TDs ... > , UIntPack< EDs ... > >::GreaterThanOrEqual , "[ERROR] PointEvaluator::init: More evaluation derivatives than stored derivatives" );
1166 for( int d=0 ; d<Dim ; d++ ) state._pointOffset[d] = (int)offset[d];
1167 _initEvaluationState( UIntPack< TSignatures ... >() , UIntPack< EDs ... >() , &p[0] , depth , state );
1168 }
1169 protected:
1170 unsigned int _maxDepth;
1171 std::tuple< BSplineData< TSignatures , TDs > ... > _bSplineData;
1172 template< unsigned int I=0 > typename std::enable_if< I==Dim >::type _init( void ){}
1173 template< unsigned int I=0 > typename std::enable_if< (I<Dim) >::type _init( void ){ std::get< I >( _bSplineData ).reset( _maxDepth ) ; _init< I+1 >( ); }
1174
1175 template< unsigned int I , unsigned int TSig , unsigned int D , typename State >
1176 void _setEvaluationState( const double* p , unsigned int depth , State& state ) const
1177 {
1178 static const int LeftSupportRadius = -BSplineSupportSizes< FEMSignature< TSig >::Degree >::SupportStart;
1179 static const int LeftPointSupportRadius = BSplineSupportSizes< FEMSignature< TSig >::Degree >::SupportEnd ;
1180 static const int RightSupportRadius = BSplineSupportSizes< FEMSignature< TSig >::Degree >::SupportEnd ;
1181 static const int RightPointSupportRadius = -BSplineSupportSizes< FEMSignature< TSig >::Degree >::SupportStart;
1182 for( int s=-LeftPointSupportRadius ; s<=RightPointSupportRadius ; s++ )
1183 {
1184 int pIdx = state._pointOffset[I];
1185 int fIdx = state._pointOffset[I]+s;
1186 double _p = p[I];
1187 const Polynomial< FEMSignature< TSig >::Degree >* components = std::get< I >( _bSplineData )[depth].polynomialsAndOffset( _p , pIdx , fIdx );
1188 for( int d=0 ; d<=D ; d++ ) std::get< I >( state._oneDValues ).values[ s+LeftPointSupportRadius ][d] = components[d]( _p );
1189 }
1190 }
1191 template< typename State , unsigned int TSig , unsigned int ... TSigs , unsigned int D , unsigned int ... Ds >
1192 typename std::enable_if< sizeof...(TSigs)==0 >::type _initEvaluationState( UIntPack< TSig , TSigs ... > , UIntPack< D , Ds ... > , const double* p , unsigned int depth , State& state ) const
1193 {
1194 _setEvaluationState< Dim-1 , TSig , D >( p , depth , state );
1195 }
1196 template< typename State , unsigned int TSig , unsigned int ... TSigs , unsigned int D , unsigned int ... Ds >
1197 typename std::enable_if< sizeof...(TSigs)!=0 >::type _initEvaluationState( UIntPack< TSig , TSigs ... > , UIntPack< D , Ds ... > , const double* p , unsigned int depth , State& state ) const
1198 {
1199 _setEvaluationState< Dim-1-sizeof...(TSigs) , TSig , D >( p , depth , state );
1200 _initEvaluationState( UIntPack< TSigs ... >() , UIntPack< Ds ... >() , p , depth , state );
1201 }
1202 };
1203
1204 template< unsigned int ... TSignatures >
1205 struct RestrictionProlongation< UIntPack< TSignatures ... > > : public BaseFEMIntegrator::template RestrictionProlongation< UIntPack< FEMSignature< TSignatures >::Degree ... > >
1206 {
1207 static const unsigned int Dim = sizeof ... ( TSignatures );
1208 typedef typename BaseFEMIntegrator::template RestrictionProlongation< UIntPack< FEMSignature< TSignatures >::Degree ... > > Base;
1209
1210 double upSampleCoefficient( const int pOff[] , const int cOff[] ) const { return _coefficient( pOff , cOff ); }
1211 void init( unsigned int depth ){ Base::init( depth ); }
1212 void init( void ){ _init( Base::highDepth() ); }
1213
1214 protected:
1215 std::tuple< typename BSplineEvaluationData< TSignatures >::UpSampleEvaluator ... > _upSamplers;
1216
1217 template< unsigned int D=0 > typename std::enable_if< D==Dim >::type _init( int highDepth ){ }
1218 template< unsigned int D=0 > typename std::enable_if< D< Dim >::type _init( int highDepth ){ std::get< D >( _upSamplers ).set( highDepth-1 ) ; _init< D+1 >( highDepth ); }
1219 template< unsigned int D=0 > typename std::enable_if< D==Dim , double >::type _coefficient( const int pOff[] , const int cOff[] ) const { return 1.; }
1220 template< unsigned int D=0 > typename std::enable_if< D< Dim , double >::type _coefficient( const int pOff[] , const int cOff[] ) const { return _coefficient< D+1 >( pOff , cOff ) * std::get< D >( _upSamplers ).value( pOff[D] , cOff[D] ); }
1221 };
1222
1223 template< unsigned int ... TSignatures , unsigned int ... TDerivatives , unsigned int ... CSignatures , unsigned int ... CDerivatives , unsigned int CDim >
1224 struct Constraint< UIntPack< TSignatures ... > , UIntPack< TDerivatives ... > , UIntPack< CSignatures ... > , UIntPack< CDerivatives ... > , CDim > : public BaseFEMIntegrator::template Constraint< UIntPack< FEMSignature< TSignatures >::Degree ... > , UIntPack< FEMSignature< CSignatures >::Degree ... > , CDim >
1225 {
1226 static_assert( sizeof ... ( TSignatures ) == sizeof ... ( CSignatures ) , "[ERROR] Test signatures and contraint signatures must have the same dimension" );
1227 static_assert( sizeof ... ( TSignatures ) == sizeof ... ( TDerivatives ) , "[ERROR] Test signatures and derivatives must have the same dimension" );
1228 static_assert( sizeof ... ( CSignatures ) == sizeof ... ( CDerivatives ) , "[ERROR] Constraint signatures and derivatives must have the same dimension" );
1229 static_assert( ParameterPack::Comparison< UIntPack< FEMSignature< TSignatures >::Degree ... > , UIntPack< TDerivatives ... > >::GreaterThanOrEqual , "[ERROR] Test functions cannot have more derivatives than the degree" );
1230 static_assert( ParameterPack::Comparison< UIntPack< FEMSignature< CSignatures >::Degree ... > , UIntPack< CDerivatives ... > >::GreaterThanOrEqual , "[ERROR] Test functions cannot have more derivatives than the degree" );
1231
1232 static const unsigned int Dim = sizeof ... ( TSignatures );
1233 typedef typename BaseFEMIntegrator::template Constraint< UIntPack< FEMSignature< TSignatures >::Degree ... > , UIntPack< FEMSignature< CSignatures >::Degree ... > , CDim > Base;
1234
1235 static const unsigned int TDerivativeSize = TensorDerivatives< UIntPack< TDerivatives ... > >::Size;
1236 static const unsigned int CDerivativeSize = TensorDerivatives< UIntPack< CDerivatives ... > >::Size;
1237 static inline void TFactorDerivatives( unsigned int idx , unsigned int d[ Dim ] ){ TensorDerivatives< UIntPack< TDerivatives ... > >::Factor( idx , d ); }
1238 static inline void CFactorDerivatives( unsigned int idx , unsigned int d[ Dim ] ){ TensorDerivatives< UIntPack< CDerivatives ... > >::Factor( idx , d ); }
1239 static inline unsigned int TDerivativeIndex( const unsigned int d[ Dim ] ){ return TensorDerivatives< UIntPack< TDerivatives ... > >::Index( d ); }
1240 static inline unsigned int CDerivativeIndex( const unsigned int d[ Dim ] ){ return TensorDerivatives< UIntPack< CDerivatives ... > >::Index( d ); }
1241 Matrix< double , TDerivativeSize , CDerivativeSize > weights[CDim];
1242
1243 Point< double , CDim > ccIntegrate( const int off1[] , const int off2[] ) const { return _integrate( INTEGRATE_CHILD_CHILD , off1 , off2 ); }
1244 Point< double , CDim > pcIntegrate( const int off1[] , const int off2[] ) const { return _integrate( INTEGRATE_PARENT_CHILD , off1 , off2 ); }
1245 Point< double , CDim > cpIntegrate( const int off1[] , const int off2[] ) const { return _integrate( INTEGRATE_CHILD_PARENT , off1 , off2 ); }
1246
1247 void init( unsigned int depth ){ Base::init( depth ); }
1248 void init( void )
1249 {
1250
1251 _init( Base::highDepth() );
1252 _weightedIndices.resize(0);
1253 for( unsigned int d1=0 ; d1<TDerivativeSize ; d1++ ) for( unsigned int d2=0 ; d2<CDerivativeSize ; d2++ )
1254 {
1255 _WeightedIndices w(d1,d2);
1256 for( unsigned int c=0 ; c<CDim ; c++ ) if( weights[c](d1,d2)>0 ) w.indices.push_back( std::pair< unsigned int , double >( c , weights[c](d1,d2) ) );
1257 if( w.indices.size() ) _weightedIndices.push_back(w);
1258 }
1259 }
1260 typename BaseFEMIntegrator::template RestrictionProlongation< UIntPack< FEMSignature< TSignatures >::Degree ... > >& tRestrictionProlongation( void ){ return _tRestrictionProlongation; }
1261 typename BaseFEMIntegrator::template RestrictionProlongation< UIntPack< FEMSignature< CSignatures >::Degree ... > >& cRestrictionProlongation( void ){ return _cRestrictionProlongation; }
1262 protected:
1263 RestrictionProlongation< UIntPack< TSignatures ... > > _tRestrictionProlongation;
1264 RestrictionProlongation< UIntPack< CSignatures ... > > _cRestrictionProlongation;
1265 struct _WeightedIndices
1266 {
1267 _WeightedIndices( unsigned int _d1=0 , unsigned int _d2=0 ) : d1(_d1) , d2(_d2) { ; }
1268 unsigned int d1 , d2;
1269 std::vector< std::pair< unsigned int , double > > indices;
1270 };
1271 std::vector< _WeightedIndices > _weightedIndices;
1272 enum IntegrationType
1273 {
1274 INTEGRATE_CHILD_CHILD ,
1275 INTEGRATE_PARENT_CHILD ,
1276 INTEGRATE_CHILD_PARENT
1277 };
1278
1279 template< unsigned int _TSig , unsigned int _TDerivatives , unsigned int _CSig , unsigned int _CDerivatives >
1280 struct _Integrators
1281 {
1282 typename BSplineIntegrationData< _TSig , _CSig >::FunctionIntegrator::template Integrator< _TDerivatives , _CDerivatives > ccIntegrator;
1283 typename BSplineIntegrationData< _TSig , _CSig >::FunctionIntegrator::template ChildIntegrator< _TDerivatives , _CDerivatives > pcIntegrator;
1284 typename BSplineIntegrationData< _CSig , _TSig >::FunctionIntegrator::template ChildIntegrator< _CDerivatives , _TDerivatives > cpIntegrator;
1285 };
1286 std::tuple< _Integrators< TSignatures , TDerivatives , CSignatures , CDerivatives > ... > _integrators;
1287
1288 template< unsigned int D=0 >
1289 typename std::enable_if< D==Dim >::type _init( int depth ){ ; }
1290 template< unsigned int D=0 >
1291 typename std::enable_if< D< Dim >::type _init( int depth )
1292 {
1293 std::get< D >( _integrators ).ccIntegrator.set( depth );
1294 if( depth ) std::get< D >( _integrators ).pcIntegrator.set( depth-1 ) , std::get< D >( _integrators ).cpIntegrator.set( depth-1 );
1295 _init< D+1 >( depth );
1296 }
1297 template< unsigned int D=0 >
1298 typename std::enable_if< D==Dim , double >::type _integral( IntegrationType iType , const int off1[] , const int off2[] , const unsigned int d1[] , const unsigned int d2[] ) const { return 1.; }
1299 template< unsigned int D=0 >
1300 typename std::enable_if< D< Dim , double >::type _integral( IntegrationType iType , const int off1[] , const int off2[] , const unsigned int d1[] , const unsigned int d2[] ) const
1301 {
1302 double remainingIntegral = _integral< D+1 >( iType , off1 , off2 , d1 , d2 );
1303 switch( iType )
1304 {
1305 case INTEGRATE_CHILD_CHILD: return std::get< D >( _integrators ).ccIntegrator.dot( off1[D] , off2[D] , d1[D] , d2[D] ) * remainingIntegral;
1306 case INTEGRATE_PARENT_CHILD: return std::get< D >( _integrators ).pcIntegrator.dot( off1[D] , off2[D] , d1[D] , d2[D] ) * remainingIntegral;
1307 case INTEGRATE_CHILD_PARENT: return std::get< D >( _integrators ).cpIntegrator.dot( off2[D] , off1[D] , d2[D] , d1[D] ) * remainingIntegral;
1308 default: MK_THROW( "Undefined integration type" );
1309 }
1310 return 0;
1311 }
1312 Point< double , CDim > _integrate( IntegrationType iType , const int off1[] , const int off[] ) const;
1313 };
1314
1315 template< unsigned int ... TSignatures , unsigned int ... TDerivatives , unsigned int ... CSignatures , unsigned int ... CDerivatives >
1316 struct ScalarConstraint< UIntPack< TSignatures ... > , UIntPack< TDerivatives ... > , UIntPack< CSignatures ... > , UIntPack< CDerivatives ... > > : public Constraint< UIntPack< TSignatures ... > , UIntPack< TDerivatives ... > , UIntPack< CSignatures ... > , UIntPack< CDerivatives ... > , 1 >
1317 {
1318 static const unsigned int Dim = sizeof ... ( TSignatures );
1319 typedef typename BaseFEMIntegrator::template Constraint< UIntPack< FEMSignature< TSignatures >::Degree ... > , UIntPack< FEMSignature< CSignatures >::Degree ... > , 1 > Base;
1320
1321 typedef Constraint< UIntPack< TSignatures ... > , UIntPack< TDerivatives ... > , UIntPack< CSignatures ... > , UIntPack< CDerivatives ... > , 1 > FullConstraint;
1322 using FullConstraint::weights;
1323 // [NOTE] We define the constructor using a recursive function call to take into account multiplicity (e.g. so that d^2/dxdy and d^2/dydx each contribute)
1324 ScalarConstraint( const std::initializer_list< double >& w )
1325 {
1326 std::function< void ( unsigned int[] , const double[] , unsigned int ) > SetDerivativeWeights = [&]( unsigned int derivatives[Dim] , const double w[] , unsigned int d )
1327 {
1328 unsigned int idx1 = FullConstraint::TDerivativeIndex( derivatives ) , idx2 = FullConstraint::CDerivativeIndex( derivatives );
1329 weights[0][idx1][idx2] += w[0];
1330 if( d>0 ) for( int dd=0 ; dd<Dim ; dd++ ){ derivatives[dd]++ ; SetDerivativeWeights( derivatives , w+1 , d-1 ) ; derivatives[dd]--; }
1331 };
1332 static const unsigned int DMax = std::min< unsigned int >( UIntPack< TDerivatives ... >::Min() , UIntPack< CDerivatives ... >::Min() );
1333
1334 unsigned int derivatives[Dim];
1335 double _w[DMax+1];
1336 memset( _w , 0 , sizeof(_w) );
1337 {
1338 unsigned int dd=0;
1339 for( typename std::initializer_list< double >::const_iterator iter=w.begin() ; iter!=w.end() && dd<=DMax ; dd++ , iter++ ) _w[dd] = *iter;
1340 }
1341 for( int d=0 ; d<Dim ; d++ ) derivatives[d] = 0;
1342 if( w.size() ) SetDerivativeWeights( derivatives , _w , std::min< unsigned int >( DMax+1 , (unsigned int)w.size() )-1 );
1343 }
1344 };
1345
1346 template< unsigned int ... TSignatures , unsigned int ... TDerivatives >
1347 struct System< UIntPack< TSignatures ... > , UIntPack< TDerivatives ... > > : public BaseFEMIntegrator::template System< UIntPack< FEMSignature< TSignatures >::Degree... > >
1348 {
1349 static_assert( sizeof ... ( TSignatures ) == sizeof ... ( TDerivatives ) , "[ERROR] Test signatures and derivatives must have the same dimension" );
1350
1351 static const unsigned int Dim = sizeof ... ( TSignatures );
1352 typedef typename BaseFEMIntegrator::template System< UIntPack< FEMSignature< TSignatures >::Degree... > > Base;
1353
1354 System( const std::initializer_list< double >& w ) : _sc( w ){ ; }
1355 void init( unsigned int depth ){ Base::init( depth ); }
1356 void init( void ){ ( (BaseFEMIntegrator::template Constraint< UIntPack< FEMSignature< TSignatures >::Degree ... > , UIntPack< FEMSignature< TSignatures >::Degree ... > , 1 >&)_sc ).init( BaseFEMIntegrator::template System< UIntPack< FEMSignature< TSignatures >::Degree... > >::_highDepth ); }
1357 double ccIntegrate( const int off1[] , const int off2[] ) const { return _sc.ccIntegrate( off1 , off2 )[0]; }
1358 double pcIntegrate( const int off1[] , const int off2[] ) const { return _sc.pcIntegrate( off1 , off2 )[0]; }
1359 bool vanishesOnConstants( void ) const { return _sc.weights[0][0][0]==0; }
1360
1361 typename BaseFEMIntegrator::template RestrictionProlongation< UIntPack< FEMSignature< TSignatures >::Degree ... > >& restrictionProlongation( void ){ return _sc.tRestrictionProlongation(); }
1362
1363 protected:
1364 ScalarConstraint< UIntPack< TSignatures ... > , UIntPack< TDerivatives ... > , UIntPack< TSignatures ... > , UIntPack< TDerivatives ... > > _sc;
1365 };
1366 };
1367
1368 //////////////////////////////////////////
1369
1370 template< unsigned int Dim > inline void SetGhostFlag( RegularTreeNode< Dim , FEMTreeNodeData , depth_and_offset_type >* node , bool flag ){ if( node && node->parent ) node->parent->nodeData.setGhostFlag( flag ); }
1371 template< unsigned int Dim > inline bool GetGhostFlag( const RegularTreeNode< Dim , FEMTreeNodeData , depth_and_offset_type >* node ){ return node==NULL || node->parent==NULL || node->parent->nodeData.getGhostFlag( ); }
1372 template< unsigned int Dim > inline bool IsActiveNode( const RegularTreeNode< Dim , FEMTreeNodeData , depth_and_offset_type >* node ){ return !GetGhostFlag( node ); }
1373
1374 // A class representing an extractot with and without auxiliary data
1375 template< typename Real , unsigned int Dim , typename ... Params > struct LevelSetExtractor;
1376
1377 // A helper class which consolidates the two extractors, templated over HasData
1378 template< bool HasData , typename Real , unsigned int Dim , typename Data > struct _LevelSetExtractor;
1379
1380 template< unsigned int Dim , class Data >
1381 struct NodeSample
1382 {
1383 RegularTreeNode< Dim , FEMTreeNodeData , depth_and_offset_type > *node;
1384 Data data;
1385 };
1386 template< unsigned int Dim , class Real >
1387 struct NodeAndPointSample
1388 {
1389 RegularTreeNode< Dim , FEMTreeNodeData , depth_and_offset_type > *node;
1390 ProjectiveData< Point< Real , Dim > , Real > sample;
1391 };
1392 template< unsigned int Dim , class Real > using NodeSimplices = NodeSample< Dim , std::vector< std::pair< node_index_type , Simplex< Real , Dim , Dim-1 > > > >;
1393
1394
1395 template< typename T > struct WindowLoopData{ };
1396
1397 template< unsigned int ... Sizes >
1398 struct WindowLoopData< UIntPack< Sizes ... > >
1399 {
1400 static const int Dim = sizeof ... ( Sizes );
1401 unsigned int size[1<<Dim];
1402 unsigned int indices[1<<Dim][ WindowSize< UIntPack< Sizes ... > >::Size ];
1403 template< typename BoundsFunction >
1404 WindowLoopData( const BoundsFunction &boundsFunction )
1405 {
1406 int start[Dim] , end[Dim];
1407 for( int c=0 ; c<(1<<Dim) ; c++ )
1408 {
1409 size[c] = 0;
1410 boundsFunction( c , start , end );
1411 unsigned int idx[Dim];
1412 WindowLoop< Dim >::Run
1413 (
1414 start , end ,
1415 [&]( int d , int i ){ idx[d] = i; } ,
1416 [&]( void ){ indices[c][ size[c]++ ] = Window::GetIndex< Sizes ... >( idx ); }
1417 );
1418 }
1419 }
1420 };
1421
1422 template< class Real , unsigned int Dim >
1423 struct Atomic< Point< Real , Dim > >
1424 {
1425 using Value = Point< Real , Dim >;
1426 static void Add( volatile Value &a , const Value &b ){ for( unsigned int d=0 ; d<Dim ; d++ ) AddAtomic( a.coords[d] , b[d] ); }
1427 };
1428
1429 template< class Real >
1430 struct Atomic< Point< Real > >
1431 {
1432 using Value = Point< Real >;
1433 static void Add( volatile Value &a , const Value &b )
1434 {
1435 if( a._dim !=b._dim ) MK_THROW( "Sizes don't match: " , a._dim , " != " , b._dim );
1436 for( unsigned int d=0 ; d<a._dim && d<b.dim() ; d++ ) AddAtomic( a._coords[d] , b[d] );
1437 }
1438 };
1439
1440 template< typename Data , typename Real >
1441 struct Atomic< ProjectiveData< Data , Real > >
1442 {
1443 using Value = ProjectiveData< Data , Real >;
1444 static void Add( volatile Value &a , const Value &b ){ Atomic< Real >::Add( a.weight , b.weight ) ; Atomic< Data >::Add( a.data , b.data ); }
1445 };
1446
1447 template< typename Real , typename FirstType , typename ... RestTypes >
1448 struct Atomic< DirectSum< Real , FirstType , RestTypes... > >
1449 {
1450 using Value = DirectSum< Real , FirstType , RestTypes... >;
1451 static void Add( volatile Value &a , const Value &b )
1452 {
1453 Atomic< FirstType >::Add( a._first , b._first );
1454 Atomic< DirectSum< Real , RestTypes... > >::Add( a._rest , b._rest );
1455 }
1456 };
1457
1458 template< typename Real >
1459 struct Atomic< DirectSum< Real > >
1460 {
1461 using Value = DirectSum< Real >;
1462 static void Add( volatile Value &a , const Value &b ){}
1463 };
1464
1465 template< class Real , unsigned int Dim >
1466 Point< Real , Dim > ReadAtomic( const volatile Point< Real , Dim > & a )
1467 {
1468 Point< Real , Dim > p;
1469 for( int d=0 ; d<Dim ; d++ ) p[d] = ReadAtomic( a.coords[d] );
1470 return p;
1471 }
1472
1473 template< class Real , unsigned int Dim >
1474 Point< Real , Dim > SetAtomic( volatile Point< Real , Dim > & p , Point< Real , Dim > newP )
1475 {
1476 Point< Real , Dim > oldP;
1477 for( int d=0 ; d<Dim ; d++ ) oldP[d] = SetAtomic( p.coords[d] , newP[d] );
1478 return oldP;
1479 }
1480
1481 template< class Data >
1482 bool IsZero( const Data& data ){ return false; }
1483 template< class Real , unsigned int Dim >
1484 bool IsZero( const Point< Real , Dim >& d )
1485 {
1486 bool zero = true;
1487 for( int i=0 ; i<Dim ; i++ ) zero &= (d[i]==0);
1488 return zero;
1489 }
1490 inline bool IsZero( const float& f ){ return f==0.f; }
1491 inline bool IsZero( const double& f ){ return f==0.; }
1492
1493 template< unsigned int Dim , class Real >
1494 class FEMTree
1495 {
1496 public:
1497 typedef RegularTreeNode< Dim , FEMTreeNodeData , depth_and_offset_type > FEMTreeNode;
1498 template< unsigned int ... FEMSigs > using SystemMatrixType = SparseMatrix< Real , matrix_index_type , WindowSize< UIntPack< BSplineOverlapSizes< FEMSignature< FEMSigs >::Degree >::OverlapSize ... > >::Size >;
1499 std::vector< Allocator< FEMTreeNode > * > nodeAllocators;
1500 protected:
1501 template< unsigned int _Dim , class _Real > friend class FEMTree;
1502 struct SubTreeExtractor : public FEMTreeNode::SubTreeExtractor
1503 {
1504 SubTreeExtractor( FEMTreeNode &node , int &depthOffset ) : FEMTreeNode::SubTreeExtractor( node ) , _depthOffset( depthOffset )
1505 {
1506 _depthOffsetValue = _depthOffset;
1507 _depthOffset = 0;
1508 }
1509 SubTreeExtractor( FEMTreeNode *node , int &depthOffset ) : SubTreeExtractor( *node , depthOffset ){}
1510 ~SubTreeExtractor( void ){ _depthOffset = _depthOffsetValue; }
1511 protected:
1512 int &_depthOffset , _depthOffsetValue;
1513 };
1514
1515 // Don't need this first one
1516 template< typename _Real , unsigned int _Dim , typename ... _Params > friend struct LevelSetExtractor;
1517 template< bool _HasData , typename _Real , unsigned int _Dim , typename _Data > friend struct _LevelSetExtractor;
1518 std::atomic< node_index_type > _nodeCount;
1519 struct _NodeInitializer
1520 {
1521 FEMTree& femTree;
1522 _NodeInitializer( FEMTree& f ) : femTree(f){;}
1523 void operator() ( FEMTreeNode& node ){ node.nodeData.nodeIndex = femTree._nodeCount++; }
1524 };
1525 _NodeInitializer _nodeInitializer;
1526 public:
1527 typedef int LocalDepth;
1528 typedef int LocalOffset[Dim];
1529
1530 node_index_type nodeCount( void ) const { return _nodeCount; }
1531
1532 typedef NodeAndPointSample< Dim , Real > PointSample;
1533
1534 typedef typename FEMTreeNode::template NeighborKey< IsotropicUIntPack< Dim , 1 > , IsotropicUIntPack< Dim , 1 > > OneRingNeighborKey;
1535 typedef typename FEMTreeNode::template ConstNeighborKey< IsotropicUIntPack< Dim , 1 > , IsotropicUIntPack< Dim , 1 > > ConstOneRingNeighborKey;
1536 typedef typename FEMTreeNode::template Neighbors< IsotropicUIntPack< Dim , 3 > > OneRingNeighbors;
1537 typedef typename FEMTreeNode::template ConstNeighbors< IsotropicUIntPack< Dim , 3 > > ConstOneRingNeighbors;
1538
1539 template< typename FEMDegreePack > using BaseSystem = typename BaseFEMIntegrator::template System< FEMDegreePack >;
1540 template< typename FEMSigPack , typename DerivativePack > using PointEvaluator = typename FEMIntegrator::template PointEvaluator< FEMSigPack , DerivativePack >;
1541 template< typename FEMSigPack , typename DerivativePack > using PointEvaluatorState = typename FEMIntegrator::template PointEvaluatorState< FEMSigPack , DerivativePack >;
1542 template< typename FEMDegreePack > using CCStencil = typename BaseSystem< FEMDegreePack >::CCStencil;
1543 template< typename FEMDegreePack > using PCStencils = typename BaseSystem< FEMDegreePack >::PCStencils;
1544
1545 template< unsigned int ... FEMSigs > bool isValidFEMNode( UIntPack< FEMSigs ... > , const FEMTreeNode* node ) const;
1546 bool isValidSpaceNode( const FEMTreeNode* node ) const;
1547 const FEMTreeNode* leaf( Point< Real , Dim > p ) const;
1548 protected:
1549 template< bool ThreadSafe >
1550 FEMTreeNode* _leaf( Allocator< FEMTreeNode > *nodeAllocator , Point< Real , Dim > p , LocalDepth maxDepth=-1 );
1551 template< bool ThreadSafe >
1552 FEMTreeNode* _leaf( Allocator< FEMTreeNode > *nodeAllocator , Point< Real , Dim > p , std::function< int ( Point< Real , Dim > ) > &pointDepthFunctor );
1553 public:
1554
1555 // [NOTE] In the case that T != double, we require both operators() for computing the system dual
1556 template< typename T , unsigned int PointD >
1557 struct InterpolationInfo
1558 {
1559 virtual void range( const FEMTreeNode* node , size_t& begin , size_t& end ) const = 0;
1560 virtual Point< T , CumulativeDerivatives< Dim , PointD >::Size > operator() ( size_t pointIdx ) const = 0;
1561 virtual Point< T , CumulativeDerivatives< Dim , PointD >::Size > operator() ( size_t pointIdx , const Point< T , CumulativeDerivatives< Dim , PointD >::Size >& dValues ) const = 0;
1562 virtual Point< double , CumulativeDerivatives< Dim , PointD >::Size > operator() ( size_t pointIdx , const Point< double , CumulativeDerivatives< Dim , PointD >::Size >& dValues ) const = 0;
1563 virtual const DualPointInfo< Dim , Real , T , PointD >& operator[]( size_t pointIdx ) const = 0;
1564 virtual bool constrainsDCTerm( void ) const = 0;
1565 virtual ~InterpolationInfo( void ){}
1566
1567 DualPointInfo< Dim , Real , T , PointD >& operator[]( size_t pointIndex ){ return const_cast< DualPointInfo< Dim , Real , T , PointD >& >( ( ( const InterpolationInfo* )this )->operator[]( pointIndex ) ); }
1568 protected:
1569 virtual void _remapIndices( ConstPointer( node_index_type )oldNodeIndices , size_t newNodeCount ) = 0;
1570 friend class FEMTree< Dim , Real >;
1571 };
1572
1573 template< unsigned int PointD >
1574 struct InterpolationInfo< double , PointD >
1575 {
1576 virtual void range( const FEMTreeNode* node , size_t& begin , size_t& end ) const = 0;
1577 virtual Point< double , CumulativeDerivatives< Dim , PointD >::Size > operator() ( size_t pointIdx ) const = 0;
1578 virtual Point< double , CumulativeDerivatives< Dim , PointD >::Size > operator() ( size_t pointIdx , const Point< double , CumulativeDerivatives< Dim , PointD >::Size >& dValues ) const = 0;
1579 virtual const DualPointInfo< Dim , Real , double , PointD >& operator[]( size_t pointIdx ) const = 0;
1580 virtual bool constrainsDCTerm( void ) const = 0;
1581 virtual ~InterpolationInfo( void ){}
1582
1583 DualPointInfo< Dim , Real , double , PointD >& operator[]( size_t pointIndex ){ return const_cast< DualPointInfo< Dim , Real , double , PointD >& >( ( ( const InterpolationInfo* )this )->operator[]( pointIndex ) ); }
1584 protected:
1585 virtual void _remapIndices( ConstPointer( node_index_type )oldNodeIndices , size_t newNodeCount ) = 0;
1586 friend class FEMTree< Dim , Real >;
1587 };
1588
1589 template< typename T , unsigned int PointD , typename ConstraintDual , typename SystemDual >
1590 struct ApproximatePointInterpolationInfo : public InterpolationInfo< T , PointD >
1591 {
1592 using Data = DualPointInfo< Dim , Real , T , PointD >;
1593 void range( const FEMTreeNode *node , size_t &begin , size_t &end ) const
1594 {
1595 node_index_type idx = iData.index( node );
1596 if( idx==-1 ) begin = end = 0;
1597 else begin = idx , end = idx+1;
1598 }
1599 bool constrainsDCTerm( void ) const { return _constrainsDCTerm; }
1600 const DualPointInfo< Dim , Real , T , PointD >& operator[]( size_t pointIdx ) const { return iData[ pointIdx ]; }
1601 Point< T , CumulativeDerivatives< Dim , PointD >::Size > operator() ( size_t pointIdx ) const { return _constraintDual( iData[ pointIdx ].position ); }
1602 Point< T , CumulativeDerivatives< Dim , PointD >::Size > operator() ( size_t pointIdx , const Point< T , CumulativeDerivatives< Dim , PointD >::Size >& dValues ) const { return _systemDual( iData[ pointIdx ].position , dValues ); }
1603 Point< double , CumulativeDerivatives< Dim , PointD >::Size > operator() ( size_t pointIdx , const Point< double , CumulativeDerivatives< Dim , PointD >::Size >& dValues ) const { return _systemDual( iData[ pointIdx ].position , dValues ); }
1604
1605 ApproximatePointInterpolationInfo( ConstraintDual constraintDual , SystemDual systemDual , bool constrainsDCTerm ) : _constraintDual( constraintDual ) , _systemDual( systemDual ) , _constrainsDCTerm( constrainsDCTerm ) { }
1606
1607 void write( BinaryStream &stream ) const
1608 {
1609 unsigned char constrainsDCTerm = _constrainsDCTerm ? 1 : 0;
1610 stream.write( constrainsDCTerm );
1611 stream.write( _constraintDual );
1612 stream.write( _systemDual );
1613 iData.write( stream );
1614 }
1615 void read( BinaryStream &stream )
1616 {
1617 unsigned char constrainsDCTerm;
1618 if( !stream.read( constrainsDCTerm ) ) MK_THROW( "Failed to read constrainsDCTerm" );
1619 _constrainsDCTerm = constrainsDCTerm!=0;
1620 if( !stream.read( _constraintDual ) ) MK_THROW( "Failed to read _constraintDual" );
1621 if( !stream.read( _systemDual ) ) MK_THROW( "Failed to read _systemDual" );
1622 iData.read( stream );
1623 }
1624 ApproximatePointInterpolationInfo( BinaryStream &stream ){ read(stream); }
1625 ApproximatePointInterpolationInfo( void ){}
1626
1627 SparseNodeData< DualPointInfo< Dim , Real , T , PointD > , ZeroUIntPack< Dim > > iData;
1628
1629 protected:
1630
1631 bool _constrainsDCTerm;
1632 ConstraintDual _constraintDual;
1633 SystemDual _systemDual;
1634
1635 void _remapIndices( ConstPointer( node_index_type )oldNodeIndices , size_t newNodeCount ){ iData._remapIndices( oldNodeIndices , newNodeCount ); }
1636
1637 friend class FEMTree< Dim , Real >;
1638 };
1639
1640 template< unsigned int PointD , typename ConstraintDual , typename SystemDual >
1641 struct ApproximatePointInterpolationInfo< double , PointD , ConstraintDual , SystemDual > : public InterpolationInfo< double , PointD >
1642 {
1643 typedef double T;
1644 using Data = DualPointInfo< Dim , Real , T , PointD >;
1645 void range( const FEMTreeNode* node , size_t& begin , size_t& end ) const
1646 {
1647 node_index_type idx = iData.index( node );
1648 if( idx==-1 ) begin = end = 0;
1649 else begin = idx , end = idx+1;
1650 }
1651 bool constrainsDCTerm( void ) const { return _constrainsDCTerm; }
1652 const DualPointInfo< Dim , Real , T , PointD >& operator[]( size_t pointIdx ) const { return iData[ pointIdx ]; }
1653 Point< T , CumulativeDerivatives< Dim , PointD >::Size > operator() ( size_t pointIdx ) const { return _constraintDual( iData[ pointIdx ].position ); }
1654 Point< T , CumulativeDerivatives< Dim , PointD >::Size > operator() ( size_t pointIdx , const Point< T , CumulativeDerivatives< Dim , PointD >::Size >& dValues ) const { return _systemDual( iData[ pointIdx ].position , dValues ); }
1655
1656 ApproximatePointInterpolationInfo( ConstraintDual constraintDual , SystemDual systemDual , bool constrainsDCTerm ) : _constraintDual( constraintDual ) , _systemDual( systemDual ) , _constrainsDCTerm( constrainsDCTerm ) { }
1657
1658 void write( BinaryStream &stream ) const
1659 {
1660 unsigned char constrainsDCTerm = _constrainsDCTerm ? 1 : 0;
1661 stream.write( constrainsDCTerm );
1662 stream.write( _constraintDual );
1663 stream.write( _systemDual );
1664 iData.write( stream );
1665 }
1666 void read( BinaryStream &stream )
1667 {
1668 unsigned char constrainsDCTerm;
1669 if( !stream.read( constrainsDCTerm ) ) MK_THROW( "Failed to read constrainsDCTerm" );
1670 _constrainsDCTerm = constrainsDCTerm!=0;
1671 if( !stream.read( _constraintDual ) ) MK_THROW( "Failed to read _constraintDual" );
1672 if( !stream.read( _systemDual ) ) MK_THROW( "Failed to read _systemDual" );
1673 iData.read( stream );
1674 }
1675 ApproximatePointInterpolationInfo( BinaryStream &stream ){ read(stream); }
1676 ApproximatePointInterpolationInfo( void ){}
1677
1678 SparseNodeData< DualPointInfo< Dim , Real , T , PointD > , ZeroUIntPack< Dim > > iData;
1679
1680 protected:
1681
1682 bool _constrainsDCTerm;
1683 ConstraintDual _constraintDual;
1684 SystemDual _systemDual;
1685
1686 void _remapIndices( ConstPointer( node_index_type )oldNodeIndices , size_t newNodeCount ){ iData._remapIndices( oldNodeIndices , newNodeCount ); }
1687
1688 friend class FEMTree< Dim , Real >;
1689 };
1690
1691 template< typename T , typename Data , unsigned int PointD , typename ConstraintDual , typename SystemDual >
1692 struct ApproximatePointAndDataInterpolationInfo : public InterpolationInfo< T , PointD >
1693 {
1694 void range( const FEMTreeNode* node , size_t& begin , size_t& end ) const
1695 {
1696 node_index_type idx = iData.index( node );
1697 if( idx==-1 ) begin = end = 0;
1698 else begin = idx , end = idx+1;
1699 }
1700 bool constrainsDCTerm( void ) const { return _constrainsDCTerm; }
1701 const DualPointInfo< Dim , Real , T , PointD >& operator[]( size_t pointIdx ) const { return iData[ pointIdx ].pointInfo; }
1702 Point< T , CumulativeDerivatives< Dim , PointD >::Size > operator() ( size_t pointIdx ) const { return _constraintDual( iData[ pointIdx ].pointInfo.position , iData[ pointIdx ].data ); }
1703 Point< T , CumulativeDerivatives< Dim , PointD >::Size > operator() ( size_t pointIdx , const Point< T , CumulativeDerivatives< Dim , PointD >::Size >& dValues ) const { return _systemDual( iData[ pointIdx ].pointInfo.position , iData[ (int)pointIdx ].data , dValues ); }
1704 typename std::enable_if< !std::is_same< T , double >::value , Point< double , CumulativeDerivatives< Dim , PointD >::Size > >::type operator() ( size_t pointIdx , const Point< double , CumulativeDerivatives< Dim , PointD >::Size >& dValues ) const { return _systemDual( iData[ (int)pointIdx ].pointInfo.position , iData[ (int)pointIdx ].data , dValues ); }
1705
1706 ApproximatePointAndDataInterpolationInfo( ConstraintDual constraintDual , SystemDual systemDual , bool constrainsDCTerm ) : _constraintDual( constraintDual ) , _systemDual( systemDual ) , _constrainsDCTerm( constrainsDCTerm ) { }
1707
1708 void write( BinaryStream &stream ) const
1709 {
1710 unsigned char constrainsDCTerm = _constrainsDCTerm ? 1 : 0;
1711 stream.write( constrainsDCTerm );
1712 stream.write( _constraintDual );
1713 stream.write( _systemDual );
1714 iData.write( stream );
1715 }
1716 void read( BinaryStream &stream )
1717 {
1718 unsigned char constrainsDCTerm;
1719 if( !stream.read( constrainsDCTerm ) ) MK_THROW( "Failed to read constrainsDCTerm" );
1720 _constrainsDCTerm = constrainsDCTerm!=0;
1721 if( !stream.read( _constraintDual ) ) MK_THROW( "Failed to read _constraintDual" );
1722 if( !stream.read( _systemDual ) ) MK_THROW( "Failed to read _systemDual" );
1723 iData.read( stream );
1724 }
1725 ApproximatePointAndDataInterpolationInfo( BinaryStream &stream ){ read(stream); }
1726
1727 SparseNodeData< DualPointAndDataInfo< Dim , Real , Data , T , PointD > , ZeroUIntPack< Dim > > iData;
1728
1729 protected:
1730
1731 bool _constrainsDCTerm;
1732 ConstraintDual _constraintDual;
1733 SystemDual _systemDual;
1734
1735 void _remapIndices( ConstPointer( node_index_type )oldNodeIndices , size_t newNodeCount ){ iData._remapIndices( oldNodeIndices , newNodeCount ); }
1736
1737 ApproximatePointAndDataInterpolationInfo( void ){}
1738 friend class FEMTree< Dim , Real >;
1739 };
1740
1741 template< typename Data , unsigned int PointD , typename ConstraintDual , typename SystemDual >
1742 struct ApproximatePointAndDataInterpolationInfo< double , Data , PointD , ConstraintDual , SystemDual > : public InterpolationInfo< double , PointD >
1743 {
1744 typedef double T;
1745 void range( const FEMTreeNode* node , size_t& begin , size_t& end ) const
1746 {
1747 node_index_type idx = iData.index( node );
1748 if( idx==-1 ) begin = end = 0;
1749 else begin = idx , end = idx+1;
1750 }
1751 bool constrainsDCTerm( void ) const { return _constrainsDCTerm; }
1752 const DualPointInfo< Dim , Real , T , PointD >& operator[]( size_t pointIdx ) const { return iData[ pointIdx ].pointInfo; }
1753 Point< T , CumulativeDerivatives< Dim , PointD >::Size > operator() ( size_t pointIdx ) const { return _constraintDual( iData[ pointIdx ].pointInfo.position , iData[ pointIdx ].data ); }
1754 Point< T , CumulativeDerivatives< Dim , PointD >::Size > operator() ( size_t pointIdx , const Point< T , CumulativeDerivatives< Dim , PointD >::Size >& dValues ) const { return _systemDual( iData[ pointIdx ].pointInfo.position , iData[ pointIdx ].data , dValues ); }
1755
1756 ApproximatePointAndDataInterpolationInfo( ConstraintDual constraintDual , SystemDual systemDual , bool constrainsDCTerm ) : _constraintDual( constraintDual ) , _systemDual( systemDual ) , _constrainsDCTerm( constrainsDCTerm ) { }
1757
1758 void write( BinaryStream &stream ) const
1759 {
1760 unsigned char constrainsDCTerm = _constrainsDCTerm ? 1 : 0;
1761 stream.write( constrainsDCTerm );
1762 stream.write( _constraintDual );
1763 stream.write( _systemDual );
1764 iData.write( stream );
1765 }
1766 void read( BinaryStream &stream )
1767 {
1768 unsigned char constrainsDCTerm;
1769 if( !stream.read( constrainsDCTerm ) ) MK_THROW( "Failed to read constrainsDCTerm" );
1770 _constrainsDCTerm = constrainsDCTerm!=0;
1771 if( !stream.read( _constraintDual ) ) MK_THROW( "Failed to read _constraintDual" );
1772 if( !stream.read( _systemDual ) ) MK_THROW( "Failed to read _systemDual" );
1773 iData.read( stream );
1774 }
1775
1776 ApproximatePointAndDataInterpolationInfo( BinaryStream &stream ){ read(stream); }
1777
1778 SparseNodeData< DualPointAndDataInfo< Dim , Real , Data , T , PointD > , ZeroUIntPack< Dim > > iData;
1779
1780 protected:
1781
1782 bool _constrainsDCTerm;
1783 ConstraintDual _constraintDual;
1784 SystemDual _systemDual;
1785
1786 void _remapIndices( ConstPointer( node_index_type )oldNodeIndices , size_t newNodeCount ){ iData._remapIndices( oldNodeIndices , newNodeCount ); }
1787
1788 ApproximatePointAndDataInterpolationInfo( void ){}
1789 friend class FEMTree< Dim , Real >;
1790 };
1791
1792 template< typename T , unsigned int PointD , typename ConstraintDual , typename SystemDual >
1793 struct ExactPointInterpolationInfo : public InterpolationInfo< T , PointD >
1794 {
1795 void range( const FEMTreeNode *node , size_t &begin , size_t &end ) const { begin = _sampleSpan[ node->nodeData.nodeIndex ].first , end = _sampleSpan[ node->nodeData.nodeIndex ].second; }
1796 bool constrainsDCTerm( void ) const { return _constrainsDCTerm; }
1797 const DualPointInfo< Dim , Real , T , PointD >& operator[]( size_t pointIdx ) const { return _iData[ pointIdx ]; }
1798 Point< T , CumulativeDerivatives< Dim , PointD >::Size > operator() ( size_t pointIdx ) const { return _constraintDual( _iData[ pointIdx ].position ); }
1799 Point< T , CumulativeDerivatives< Dim , PointD >::Size > operator() ( size_t pointIdx , const Point< T , CumulativeDerivatives< Dim , PointD >::Size >& dValues ) const { return _systemDual( _iData[ pointIdx ].position , dValues ); }
1800 typename std::enable_if< !std::is_same< T , double >::value , Point< double , CumulativeDerivatives< Dim , PointD >::Size > >::type operator() ( size_t pointIdx , const Point< double , CumulativeDerivatives< Dim , PointD >::Size >& dValues ) const { return _systemDual( _iData[ pointIdx ].position , dValues ); }
1801
1802 ExactPointInterpolationInfo( ConstraintDual constraintDual , SystemDual systemDual , bool constrainsDCTerm ) : _constraintDual( constraintDual ) , _systemDual( systemDual ) , _constrainsDCTerm( constrainsDCTerm ) { }
1803 protected:
1804 void _init( const class FEMTree< Dim , Real >& tree , const std::vector< PointSample >& samples , bool noRescale );
1805
1806 std::vector< std::pair< node_index_type , node_index_type > > _sampleSpan;
1807 std::vector< DualPointInfo< Dim , Real , T , PointD > > _iData;
1808 bool _constrainsDCTerm;
1809 ConstraintDual _constraintDual;
1810 SystemDual _systemDual;
1811
1812 void _remapIndices( ConstPointer( node_index_type )oldNodeIndices , size_t newNodeCount )
1813 {
1814 std::vector< std::pair< node_index_type , node_index_type > > _newSampleSpan( newNodeCount );
1815 for( size_t i=0 ; i<newNodeCount ; i++ ) if( oldNodeIndices[i]!=-1 && oldNodeIndices[i]<(node_index_type)_sampleSpan.size() ) _newSampleSpan[i] = _sampleSpan[ oldNodeIndices[i] ];
1816 _sampleSpan = _newSampleSpan;
1817 }
1818
1819 friend class FEMTree< Dim , Real >;
1820 };
1821
1822 template< unsigned int PointD , typename ConstraintDual , typename SystemDual >
1823 struct ExactPointInterpolationInfo< double , PointD , ConstraintDual , SystemDual > : public InterpolationInfo< double , PointD >
1824 {
1825 typedef double T;
1826 void range( const FEMTreeNode* node , size_t& begin , size_t& end ) const { begin = _sampleSpan[ node->nodeData.nodeIndex ].first , end = _sampleSpan[ node->nodeData.nodeIndex ].second; }
1827 bool constrainsDCTerm( void ) const { return _constrainsDCTerm; }
1828 const DualPointInfo< Dim , Real , T , PointD >& operator[]( size_t pointIdx ) const { return _iData[ pointIdx ]; }
1829 Point< T , CumulativeDerivatives< Dim , PointD >::Size > operator() ( size_t pointIdx ) const { return _constraintDual( _iData[ pointIdx ].position ); }
1830 Point< T , CumulativeDerivatives< Dim , PointD >::Size > operator() ( size_t pointIdx , const Point< T , CumulativeDerivatives< Dim , PointD >::Size >& dValues ) const { return _systemDual( _iData[ pointIdx ].position , dValues ); }
1831
1832 ExactPointInterpolationInfo( ConstraintDual constraintDual , SystemDual systemDual , bool constrainsDCTerm ) : _constraintDual( constraintDual ) , _systemDual( systemDual ) , _constrainsDCTerm( constrainsDCTerm ) { }
1833 protected:
1834 void _init( const class FEMTree< Dim , Real >& tree , const std::vector< PointSample >& samples , bool noRescale );
1835
1836 std::vector< std::pair< node_index_type , node_index_type > > _sampleSpan;
1837 std::vector< DualPointInfo< Dim , Real , T , PointD > > _iData;
1838 bool _constrainsDCTerm;
1839 ConstraintDual _constraintDual;
1840 SystemDual _systemDual;
1841
1842 void _remapIndices( ConstPointer( node_index_type )oldNodeIndices , size_t newNodeCount )
1843 {
1844 std::vector< std::pair< node_index_type , node_index_type > > _newSampleSpan( newNodeCount );
1845 for( size_t i=0 ; i<newNodeCount ; i++ ) if( oldNodeIndices[i]!=-1 && oldNodeIndices[i]<(node_index_type)_sampleSpan.size() ) _newSampleSpan[i] = _sampleSpan[ oldNodeIndices[i] ];
1846 _sampleSpan = _newSampleSpan;
1847 }
1848
1849 friend class FEMTree< Dim , Real >;
1850 };
1851
1852 template< typename T , typename Data , unsigned int PointD , typename ConstraintDual , typename SystemDual >
1853 struct _ExactPointAndDataInterpolationInfo : public InterpolationInfo< T , PointD >
1854 {
1855 _ExactPointAndDataInterpolationInfo( ConstraintDual constraintDual , SystemDual systemDual , bool constrainsDCTerm ) : _constraintDual( constraintDual ) , _systemDual( systemDual ) , _constrainsDCTerm( constrainsDCTerm ) { }
1856
1857 protected:
1858 void _init( const class FEMTree< Dim , Real >& tree , const std::vector< PointSample >& samples , ConstPointer( Data ) sampleData , bool noRescale );
1859
1860 std::vector< std::pair< node_index_type , node_index_type > > _sampleSpan;
1861 std::vector< DualPointAndDataInfo< Dim , Real , Data , T , PointD > > _iData;
1862 bool _constrainsDCTerm;
1863 ConstraintDual _constraintDual;
1864 SystemDual _systemDual;
1865
1866 void _remapIndices( ConstPointer( node_index_type )oldNodeIndices , size_t newNodeCount )
1867 {
1868 std::vector< std::pair< node_index_type , node_index_type > > _newSampleSpan( newNodeCount );
1869 for( size_t i=0 ; i<newNodeCount ; i++ ) if( oldNodeIndices[i]!=-1 && oldNodeIndices[i]<(node_index_type)_sampleSpan.size() ) _newSampleSpan[i] = _sampleSpan[ oldNodeIndices[i] ];
1870 _sampleSpan = _newSampleSpan;
1871 }
1872
1873 friend class FEMTree< Dim , Real >;
1874 };
1875
1876 template< typename T , typename Data , unsigned int PointD , typename ConstraintDual , typename SystemDual >
1877 struct ExactPointAndDataInterpolationInfo : public _ExactPointAndDataInterpolationInfo< T , Data , PointD , ConstraintDual , SystemDual >
1878 {
1879 using _ExactPointAndDataInterpolationInfo< T , Data , PointD , ConstraintDual , SystemDual >::_sampleSpan;
1880 using _ExactPointAndDataInterpolationInfo< T , Data , PointD , ConstraintDual , SystemDual >::_constrainsDCTerm;
1881 using _ExactPointAndDataInterpolationInfo< T , Data , PointD , ConstraintDual , SystemDual >::_iData;
1882 using _ExactPointAndDataInterpolationInfo< T , Data , PointD , ConstraintDual , SystemDual >::_constraintDual;
1883 using _ExactPointAndDataInterpolationInfo< T , Data , PointD , ConstraintDual , SystemDual >::_systemDual;
1884 void range( const FEMTreeNode* node , size_t& begin , size_t& end ) const { begin = _sampleSpan[ node->nodeData.nodeIndex ].first , end = _sampleSpan[ node->nodeData.nodeIndex ].second; }
1885 bool constrainsDCTerm( void ) const { return _constrainsDCTerm; }
1886 const DualPointInfo< Dim , Real , T , PointD >& operator[]( size_t pointIdx ) const { return _iData[ pointIdx ].pointInfo; }
1887 Point< T , CumulativeDerivatives< Dim , PointD >::Size > operator() ( size_t pointIdx ) const { return _constraintDual( _iData[ pointIdx ].pointInfo.position , _iData[ pointIdx ].data ); }
1888 Point< T , CumulativeDerivatives< Dim , PointD >::Size > operator() ( size_t pointIdx , const Point< T , CumulativeDerivatives< Dim , PointD >::Size >& dValues ) const { return _systemDual( _iData[ pointIdx ].pointInfo.position , _iData[ pointIdx ].data , dValues ); }
1889 Point< double , CumulativeDerivatives< Dim , PointD >::Size > operator() ( size_t pointIdx , const Point< double , CumulativeDerivatives< Dim , PointD >::Size >& dValues ) const { return _systemDual( _iData[ pointIdx ].pointInfo.position , _iData[ (int)pointIdx ].data , dValues ); }
1890
1891 ExactPointAndDataInterpolationInfo( ConstraintDual constraintDual , SystemDual systemDual , bool constrainsDCTerm ) : _ExactPointAndDataInterpolationInfo< T , Data , PointD , ConstraintDual , SystemDual >( constraintDual , systemDual , constrainsDCTerm ) { }
1892 };
1893
1894 template< typename Data , unsigned int PointD , typename ConstraintDual , typename SystemDual >
1895 struct ExactPointAndDataInterpolationInfo< double , Data , PointD , ConstraintDual , SystemDual > : public _ExactPointAndDataInterpolationInfo< double , Data , PointD , ConstraintDual , SystemDual >
1896 {
1897 using _ExactPointAndDataInterpolationInfo< double , Data , PointD , ConstraintDual , SystemDual >::_sampleSpan;
1898 using _ExactPointAndDataInterpolationInfo< double , Data , PointD , ConstraintDual , SystemDual >::_constrainsDCTerm;
1899 using _ExactPointAndDataInterpolationInfo< double , Data , PointD , ConstraintDual , SystemDual >::_iData;
1900 using _ExactPointAndDataInterpolationInfo< double , Data , PointD , ConstraintDual , SystemDual >::_constraintDual;
1901 using _ExactPointAndDataInterpolationInfo< double , Data , PointD , ConstraintDual , SystemDual >::_systemDual;
1902 void range( const FEMTreeNode* node , size_t& begin , size_t& end ) const { begin = _sampleSpan[ node->nodeData.nodeIndex ].first , end = _sampleSpan[ node->nodeData.nodeIndex ].second; }
1903 bool constrainsDCTerm( void ) const { return _constrainsDCTerm; }
1904 const DualPointInfo< Dim , Real , double , PointD >& operator[]( size_t pointIdx ) const { return _iData[ pointIdx ].pointInfo; }
1905 Point< double , CumulativeDerivatives< Dim , PointD >::Size > operator() ( size_t pointIdx ) const { return _constraintDual( _iData[ pointIdx ].pointInfo.position , _iData[ pointIdx ].data ); }
1906 Point< double , CumulativeDerivatives< Dim , PointD >::Size > operator() ( size_t pointIdx , const Point< double , CumulativeDerivatives< Dim , PointD >::Size >& dValues ) const { return _systemDual( _iData[ pointIdx ].pointInfo.position , _iData[ (int)pointIdx ].data , dValues ); }
1907
1908 ExactPointAndDataInterpolationInfo( ConstraintDual constraintDual , SystemDual systemDual , bool constrainsDCTerm ) : _ExactPointAndDataInterpolationInfo< double , Data , PointD , ConstraintDual , SystemDual >( constraintDual , systemDual , constrainsDCTerm ) { }
1909 };
1910
1911 template< typename T , unsigned int PointD , typename ConstraintDual , typename SystemDual >
1912 static ApproximatePointInterpolationInfo< T , PointD , ConstraintDual , SystemDual >* InitializeApproximatePointInterpolationInfo( const class FEMTree< Dim , Real >& tree , const std::vector< PointSample >& samples , ConstraintDual constraintDual , SystemDual systemDual , bool constrainsDCTerm , LocalDepth maxDepth , int adaptiveExponent )
1913 {
1914 ApproximatePointInterpolationInfo< T , PointD , ConstraintDual , SystemDual >* a = new ApproximatePointInterpolationInfo< T , PointD , ConstraintDual , SystemDual >( constraintDual , systemDual , constrainsDCTerm );
1915 a->iData = tree._densifyInterpolationInfoAndSetDualConstraints< T , PointD >( samples , constraintDual , maxDepth , adaptiveExponent );
1916 return a;
1917 }
1918 template< typename T , typename Data , unsigned int PointD , typename ConstraintDual , typename SystemDual >
1919 static ApproximatePointAndDataInterpolationInfo< T , Data , PointD , ConstraintDual , SystemDual >* InitializeApproximatePointAndDataInterpolationInfo( const class FEMTree< Dim , Real >& tree , const std::vector< PointSample >& samples , ConstPointer( Data ) sampleData , ConstraintDual constraintDual , SystemDual systemDual , bool constrainsDCTerm , LocalDepth maxDepth , int adaptiveExponent )
1920 {
1921 ApproximatePointAndDataInterpolationInfo< T , Data , PointD , ConstraintDual , SystemDual >* a = new ApproximatePointAndDataInterpolationInfo< T , Data , PointD , ConstraintDual , SystemDual >( constraintDual , systemDual , constrainsDCTerm );
1922 a->iData = tree._densifyInterpolationInfoAndSetDualConstraints< T , Data , PointD >( samples , sampleData , constraintDual , maxDepth , adaptiveExponent );
1923 return a;
1924 }
1925
1926 template< typename T , unsigned int PointD , typename ConstraintDual , typename SystemDual >
1927 static ExactPointInterpolationInfo< T , PointD , ConstraintDual , SystemDual >* InitializeExactPointInterpolationInfo( const class FEMTree< Dim , Real >& tree , const std::vector< PointSample >& samples , ConstraintDual constraintDual , SystemDual systemDual , bool constrainsDCTerm , bool noRescale )
1928 {
1929 ExactPointInterpolationInfo< T , PointD , ConstraintDual , SystemDual >* e = new ExactPointInterpolationInfo< T , PointD , ConstraintDual , SystemDual >( constraintDual , systemDual , constrainsDCTerm );
1930 e->_init( tree , samples , noRescale );
1931 return e;
1932 }
1933 template< typename T , typename Data , unsigned int PointD , typename ConstraintDual , typename SystemDual >
1934 static ExactPointAndDataInterpolationInfo< T , Data , PointD , ConstraintDual , SystemDual >* InitializeExactPointAndDataInterpolationInfo( const class FEMTree< Dim , Real >& tree , const std::vector< PointSample >& samples , ConstPointer( Data ) sampleData , ConstraintDual constraintDual , SystemDual systemDual , bool constrainsDCTerm , bool noRescale )
1935 {
1936 ExactPointAndDataInterpolationInfo< T , Data , PointD , ConstraintDual , SystemDual >* e = new ExactPointAndDataInterpolationInfo< T , Data , PointD , ConstraintDual , SystemDual >( constraintDual , systemDual , constrainsDCTerm );
1937 e->_init( tree , samples , sampleData , noRescale );
1938 return e;
1939 }
1940
1941 template< typename T , unsigned int PointD , typename ConstraintDual , typename SystemDual > friend struct ExactPointInterpolationInfo;
1942 template< typename T , typename Data , unsigned int PointD , typename ConstraintDual , typename SystemDual > friend struct ExactPointAndDataInterpolationInfo;
1943
1944 template< typename ... InterpolationInfos >
1945 static bool ConstrainsDCTerm( std::tuple< InterpolationInfos *... > interpolationInfos ){ return _ConstrainsDCTerm< 0 >( interpolationInfos ); }
1946 protected:
1947 template< unsigned int Idx , typename ... InterpolationInfos >
1948 static typename std::enable_if< ( Idx==sizeof...(InterpolationInfos) ) , bool >::type _ConstrainsDCTerm( std::tuple< InterpolationInfos *... > interpolationInfos ){ return false; }
1949 template< unsigned int Idx , typename ... InterpolationInfos >
1950 static typename std::enable_if< ( Idx <sizeof...(InterpolationInfos) ) , bool >::type _ConstrainsDCTerm( std::tuple< InterpolationInfos *... > interpolationInfos )
1951 {
1952 if( std::get< Idx >( interpolationInfos ) ) return _ConstrainsDCTerm< Idx+1 >( interpolationInfos ) || std::get< Idx >( interpolationInfos )->constrainsDCTerm();
1953 else return _ConstrainsDCTerm< Idx+1 >( interpolationInfos );
1954 }
1955 public:
1956
1957 #ifdef SHOW_WARNINGS
1958 #pragma message( "[WARNING] This should not be isotropic" )
1959 #endif // SHOW_WARNINGS
1960 template< unsigned int DensityDegree > struct DensityEstimator : public SparseNodeData< Real , IsotropicUIntPack< Dim , FEMDegreeAndBType< DensityDegree >::Signature > >
1961 {
1962 DensityEstimator( int kernelDepth , int coDimension , Real samplesPerNode ) : _kernelDepth( kernelDepth ) , _coDimension( coDimension ) , _samplesPerNode( samplesPerNode ){ }
1963 Real samplesPerNode( void ) const { return _samplesPerNode; }
1964 int coDimension( void ) const { return _coDimension; }
1965 int kernelDepth( void ) const { return _kernelDepth; }
1966
1967 void write( BinaryStream &stream ) const
1968 {
1969 stream.write( _kernelDepth );
1970 stream.write( _coDimension );
1971 stream.write( _samplesPerNode );
1972 SparseNodeData< Real , IsotropicUIntPack< Dim , FEMDegreeAndBType< DensityDegree >::Signature > >::write( stream );
1973 }
1974 void read( BinaryStream &stream )
1975 {
1976 if( !stream.read( _kernelDepth ) ) MK_THROW( "Failed to read _kernelDepth" );
1977 if( !stream.read( _coDimension ) ) MK_THROW( "Failed to read _coDimension" );
1978 if( !stream.read( _samplesPerNode ) ) MK_THROW( "Failed to read _samplesPerNode" );
1979 SparseNodeData< Real , IsotropicUIntPack< Dim , FEMDegreeAndBType< DensityDegree >::Signature > >::read( stream );
1980 }
1981 DensityEstimator( BinaryStream &stream ){ read(stream); }
1982
1983 protected:
1984 Real _samplesPerNode;
1985 int _kernelDepth , _coDimension;
1986 };
1987
1988 protected:
1989 bool _isValidSpaceNode( const FEMTreeNode* node ) const { return !GetGhostFlag< Dim >( node ) && ( node->nodeData.flags & FEMTreeNodeData::SPACE_FLAG ); }
1990 bool _isValidFEM1Node ( const FEMTreeNode* node ) const { return !GetGhostFlag< Dim >( node ) && ( node->nodeData.flags & FEMTreeNodeData::FEM_FLAG_1 ); }
1991 bool _isValidFEM2Node ( const FEMTreeNode* node ) const { return !GetGhostFlag< Dim >( node ) && ( node->nodeData.flags & FEMTreeNodeData::FEM_FLAG_2 ); }
1992
1993 FEMTreeNode _tree;
1994 mutable FEMTreeNode* _spaceRoot;
1995 SortedTreeNodes< Dim > _sNodes;
1996 LocalDepth _maxDepth;
1997 int _depthOffset;
1998 LocalDepth _baseDepth;
1999 #ifdef USE_EXACT_PROLONGATION
2000 LocalDepth _exactDepth;
2001 #endif // USE_EXACT_PROLONGATION
2002 mutable unsigned int _femSigs1[ Dim ];
2003 mutable unsigned int _femSigs2[ Dim ];
2004 void _init( void );
2005
2006 static bool _InBounds( Point< Real , Dim > p );
2007 int _localToGlobal( LocalDepth d ) const { return d + _depthOffset; }
2008 LocalDepth _globalToLocal( int d ) const { return d - _depthOffset; }
2009 LocalDepth _localDepth( const FEMTreeNode* node ) const { return node->depth() - _depthOffset; }
2010 int _localInset( LocalDepth d ) const { return _depthOffset==0 ? 0 : 1<<( d + _depthOffset - 1 ); }
2011 void _localDepthAndOffset( const FEMTreeNode* node , LocalDepth& d , LocalOffset& off ) const
2012 {
2013 node->depthAndOffset( d , off );
2014 d -= _depthOffset;
2015 if( d<0 ) for( int d=0 ; d<Dim ; d++ ) off[d] = -1;
2016 else
2017 {
2018 int inset = _localInset( d );
2019 for( int d=0 ; d<Dim ; d++ ) off[d] -= inset;
2020 }
2021 }
2022 template< unsigned int FEMSig > static int _BSplineBegin( LocalDepth depth ){ return BSplineEvaluationData< FEMSig >::Begin( depth ); }
2023 template< unsigned int FEMSig > static int _BSplineEnd ( LocalDepth depth ){ return BSplineEvaluationData< FEMSig >::End ( depth ); }
2024 template< unsigned int ... FEMSigs >
2025 bool _outOfBounds( UIntPack< FEMSigs ... > , const FEMTreeNode* node ) const
2026 {
2027 if( !node ) return true;
2028 LocalDepth d ; LocalOffset off ; _localDepthAndOffset( node , d , off );
2029 return FEMIntegrator::IsOutOfBounds( UIntPack< FEMSigs ... >() , d , off );
2030 }
2031 node_index_type _sNodesBegin( LocalDepth d ) const { return _sNodes.begin( _localToGlobal( d ) ); }
2032 node_index_type _sNodesBegin( LocalDepth d , int slice ) const { return _sNodes.begin( _localToGlobal( d ) , slice + _localInset( d ) ); }
2033 node_index_type _sNodesBeginSlice( LocalDepth d ) const { return _localInset(d); }
2034 node_index_type _sNodesEnd( LocalDepth d ) const { return _sNodes.end ( _localToGlobal( d ) ); }
2035 node_index_type _sNodesEnd( LocalDepth d , int slice ) const { return _sNodes.end ( _localToGlobal( d ) , slice + _localInset( d ) ); }
2036 node_index_type _sNodesEndSlice( LocalDepth d ) const{ return ( 1<<_localToGlobal(d) ) - _localInset(d) - 1; }
2037 size_t _sNodesSize( LocalDepth d ) const { return _sNodes.size( _localToGlobal( d ) ); }
2038 size_t _sNodesSize( LocalDepth d , int slice ) const { return _sNodes.size( _localToGlobal( d ) , slice + _localInset( d ) ); }
2039
2040 template< unsigned int ... FEMDegrees > static bool _IsSupported( UIntPack< FEMDegrees ... > , LocalDepth femDepth , const LocalOffset femOffset , LocalDepth spaceDepth , const LocalOffset spaceOffset){ return BaseFEMIntegrator::IsSupported( UIntPack< FEMDegrees ... >() , femDepth , femOffset , spaceDepth , spaceOffset ); }
2041 template< unsigned int ... FEMDegrees > bool _isSupported( UIntPack< FEMDegrees ... > , const FEMTreeNode *femNode , const FEMTreeNode *spaceNode ) const
2042 {
2043 if( !femNode || !spaceNode ) return false;
2044 LocalDepth femDepth , spaceDepth ; LocalOffset femOffset , spaceOffset;
2045 _localDepthAndOffset( femNode , femDepth , femOffset ) , _localDepthAndOffset( spaceNode , spaceDepth , spaceOffset );
2046 return _IsSupported( UIntPack< FEMDegrees ... >() , femDepth , femOffset , spaceDepth , spaceOffset );
2047 }
2048
2049 template< unsigned int FEMDegree > static bool _IsInteriorlySupported( LocalDepth depth , const LocalOffset off )
2050 {
2051 if( depth>=0 )
2052 {
2053 int begin , end;
2054 BSplineSupportSizes< FEMDegree >::InteriorSupportedSpan( depth , begin , end );
2055 bool interior = true;
2056 for( int dd=0 ; dd<Dim ; dd++ ) interior &= off[dd]>=begin && off[dd]<end;
2057 return interior;
2058 }
2059 else return false;
2060 }
2061
2062 template< unsigned int FEMDegree > bool _isInteriorlySupported( const FEMTreeNode* node ) const
2063 {
2064 if( !node ) return false;
2065 LocalDepth d ; LocalOffset off;
2066 _localDepthAndOffset( node , d , off );
2067 return _IsInteriorlySupported< FEMDegree >( d , off );
2068 }
2069 template< unsigned int ... FEMDegrees > static bool _IsInteriorlySupported( UIntPack< FEMDegrees ... > , LocalDepth depth , const LocalOffset off ){ return BaseFEMIntegrator::IsInteriorlySupported( UIntPack< FEMDegrees ... >() , depth , off ); }
2070 template< unsigned int ... FEMDegrees > bool _isInteriorlySupported( UIntPack< FEMDegrees ... > , const FEMTreeNode* node ) const
2071 {
2072 if( !node ) return false;
2073 LocalDepth d ; LocalOffset off ; _localDepthAndOffset( node , d , off );
2074 return _IsInteriorlySupported< FEMDegrees ... >( UIntPack< FEMDegrees ... >() , d , off );
2075 }
2076 template< unsigned int FEMDegree1 , unsigned int FEMDegree2 > static bool _IsInteriorlyOverlapped( LocalDepth depth , const LocalOffset off )
2077 {
2078 if( depth>=0 )
2079 {
2080 int begin , end;
2081 BSplineIntegrationData< FEMDegreeAndBType< FEMDegree1 , BOUNDARY_NEUMANN >::Signature , FEMDegreeAndBType< FEMDegree2 , BOUNDARY_NEUMANN >::Signature >::InteriorOverlappedSpan( depth , begin , end );
2082 bool interior = true;
2083 for( int dd=0 ; dd<Dim ; dd++ ) interior &= off[dd]>=begin && off[dd]<end;
2084 return interior;
2085 }
2086 else return false;
2087 }
2088
2089 template< unsigned int FEMDegree1 , unsigned int FEMDegree2 > bool _isInteriorlyOverlapped( const FEMTreeNode* node ) const
2090 {
2091 if( !node ) return false;
2092 LocalDepth d ; LocalOffset off;
2093 _localDepthAndOffset( node , d , off );
2094 return _IsInteriorlyOverlapped< FEMDegree1 , FEMDegree2 >( d , off );
2095 }
2096 template< unsigned int ... FEMDegrees1 , unsigned int ... FEMDegrees2 > static bool _IsInteriorlyOverlapped( UIntPack< FEMDegrees1 ... > , UIntPack< FEMDegrees2 ... > , LocalDepth depth , const LocalOffset off ){ return BaseFEMIntegrator::IsInteriorlyOverlapped( UIntPack< FEMDegrees1 ... >() , UIntPack< FEMDegrees2 ... >() , depth , off ); }
2097 template< unsigned int ... FEMDegrees1 , unsigned int ... FEMDegrees2 > bool _isInteriorlyOverlapped( UIntPack< FEMDegrees1 ... > , UIntPack< FEMDegrees2 ... > , const FEMTreeNode* node ) const
2098 {
2099 if( !node ) return false;
2100 LocalDepth d ; LocalOffset off ; _localDepthAndOffset( node , d , off );
2101 return _IsInteriorlyOverlapped( UIntPack< FEMDegrees1 ... >() , UIntPack< FEMDegrees2 ... >() , d , off );
2102 }
2103
2104 void _startAndWidth( const FEMTreeNode* node , Point< Real , Dim >& start , Real& width ) const
2105 {
2106 LocalDepth d ; LocalOffset off;
2107 _localDepthAndOffset( node , d , off );
2108 if( d>=0 ) width = Real( 1.0 / (1<< d ) );
2109 else width = Real( 1.0 * (1<<(-d)) );
2110 for( int dd=0 ; dd<Dim ; dd++ ) start[dd] = Real( off[dd] ) * width;
2111 }
2112
2113 void _centerAndWidth( const FEMTreeNode* node , Point< Real , Dim >& center , Real& width ) const
2114 {
2115 int d , off[Dim];
2116 _localDepthAndOffset( node , d , off );
2117 width = Real( 1.0 / (1<<d) );
2118 for( int dd=0 ; dd<Dim ; dd++ ) center[dd] = Real( off[dd] + 0.5 ) * width;
2119 }
2120
2121 int _childIndex( const FEMTreeNode* node , Point< Real , Dim > p ) const
2122 {
2123 Point< Real , Dim > c ; Real w;
2124 _centerAndWidth( node , c , w );
2125 int cIdx = 0;
2126 for( int d=0 ; d<Dim ; d++ ) if( p[d]>=c[d] ) cIdx |= (1<<d);
2127 return cIdx;
2128 }
2129
2130 template< bool ThreadSafe , unsigned int ... Degrees > void _setFullDepth( UIntPack< Degrees ... > , Allocator< FEMTreeNode > *nodeAllocator , FEMTreeNode* node , LocalDepth depth );
2131 template< bool ThreadSafe , unsigned int ... Degrees > void _setFullDepth( UIntPack< Degrees ... > , Allocator< FEMTreeNode > *nodeAllocator , LocalDepth depth );
2132 template< unsigned int ... Degrees > LocalDepth _getFullDepth( UIntPack< Degrees ... > , const FEMTreeNode* node ) const;
2133 template< unsigned int ... Degrees > LocalDepth _getFullDepth( UIntPack< Degrees ... > , const LocalDepth depth , const LocalOffset begin , const LocalOffset end , const FEMTreeNode *node ) const;
2134
2135 template< bool ThreadSafe , typename AddNodeFunctor , unsigned int ... Degrees > void _refine( UIntPack< Degrees ... > , Allocator< FEMTreeNode > *nodeAllocator , const AddNodeFunctor &addNodeFunctor , FEMTreeNode *node );
2136 template< bool ThreadSafe , typename AddNodeFunctor , unsigned int ... Degrees > void _refine( UIntPack< Degrees ... > , Allocator< FEMTreeNode > *nodeAllocator , const AddNodeFunctor &addNodeFunctor );
2137
2138 public:
2139 template< unsigned int ... Degrees > LocalDepth getFullDepth( UIntPack< Degrees ... > ) const;
2140 template< unsigned int ... Degrees > LocalDepth getFullDepth( UIntPack< Degrees ... > , const LocalDepth depth , const LocalOffset begin , const LocalOffset end ) const;
2141
2142 LocalDepth depth( const FEMTreeNode* node ) const { return _localDepth( node ); }
2143 void depthAndOffset( const FEMTreeNode* node , LocalDepth& depth , LocalOffset& offset ) const { _localDepthAndOffset( node , depth , offset ); }
2144
2145 size_t nodesSize( void ) const { return _sNodes.size(); }
2146 node_index_type nodesBegin( LocalDepth d ) const { return _sNodes.begin( _localToGlobal( d ) ); }
2147 node_index_type nodesEnd ( LocalDepth d ) const { return _sNodes.end ( _localToGlobal( d ) ); }
2148 size_t nodesSize ( LocalDepth d ) const { return _sNodes.size ( _localToGlobal( d ) ); }
2149 node_index_type nodesBegin( LocalDepth d , int slice ) const { return _sNodes.begin( _localToGlobal( d ) , slice + _localInset( d ) ); }
2150 node_index_type nodesEnd ( LocalDepth d , int slice ) const { return _sNodes.end ( _localToGlobal( d ) , slice + _localInset( d ) ); }
2151 size_t nodesSize ( LocalDepth d , int slice ) const { return _sNodes.size ( _localToGlobal( d ) , slice + _localInset( d ) ); }
2152 const FEMTreeNode* node( node_index_type idx ) const { return _sNodes.treeNodes[idx]; }
2153 void centerAndWidth( node_index_type idx , Point< Real , Dim >& center , Real& width ) const { _centerAndWidth( _sNodes.treeNodes[idx] , center , width ); }
2154 void startAndWidth( node_index_type idx , Point< Real , Dim >& center , Real& width ) const { _startAndWidth( _sNodes.treeNodes[idx] , center , width ); }
2155 void centerAndWidth( const FEMTreeNode* node , Point< Real , Dim >& center , Real& width ) const { _centerAndWidth( node , center , width ); }
2156 void startAndWidth( const FEMTreeNode* node , Point< Real , Dim >& start , Real& width ) const { _startAndWidth( node , start , width ); }
2157
2158 protected:
2159 /////////////////////////////////////
2160 // System construction code //
2161 // MultiGridFEMTreeData.System.inl //
2162 /////////////////////////////////////
2163 public:
2164 template< unsigned int ... FEMSigs > void setMultiColorIndices( UIntPack< FEMSigs ... > , int depth , std::vector< std::vector< size_t > >& indices ) const;
2165 protected:
2166 template< unsigned int ... FEMSigs > void _setMultiColorIndices( UIntPack< FEMSigs ... > , node_index_type start , node_index_type end , std::vector< std::vector< size_t > >& indices ) const;
2167
2168 struct _SolverStats
2169 {
2170 double constraintUpdateTime , systemTime , solveTime;
2171 double bNorm2 , inRNorm2 , outRNorm2;
2172 };
2173
2174 // For some reason MSVC has trouble determining the template parameters when using:
2175 // template< unsigned int Idx , unsigned int ... FEMSigs , typename ... InterpolationInfos >
2176 template< unsigned int Idx , typename UIntPackFEMSigs , typename StaticWindow , typename ConstNeighbors , typename PointEvaluator , typename ... InterpolationInfos >
2177 typename std::enable_if< ( Idx==sizeof...(InterpolationInfos) ) >::type _addPointValues( UIntPackFEMSigs , StaticWindow &pointValues , const ConstNeighbors &neighbors , const PointEvaluator &bsData , std::tuple< InterpolationInfos *... > interpolationInfos ) const {}
2178 template< unsigned int Idx , typename UIntPackFEMSigs , typename StaticWindow , typename ConstNeighbors , typename PointEvaluator , typename ... InterpolationInfos >
2179 typename std::enable_if< ( Idx <sizeof...(InterpolationInfos) ) >::type _addPointValues( UIntPackFEMSigs , StaticWindow &pointValues , const ConstNeighbors &neighbors , const PointEvaluator &bsData , std::tuple< InterpolationInfos *... > interpolationInfos ) const
2180 {
2181 _addPointValues( UIntPackFEMSigs() , pointValues , neighbors , bsData , std::get< Idx >( interpolationInfos ) );
2182 _addPointValues< Idx+1 >( UIntPackFEMSigs() , pointValues , neighbors , bsData , interpolationInfos );
2183 }
2184 template< unsigned int ... FEMSigs , typename T , unsigned int PointD >
2185 void _addPointValues( UIntPack< FEMSigs ... > , StaticWindow< Real , UIntPack< BSplineOverlapSizes< FEMSignature< FEMSigs >::Degree >::OverlapSize ... > >& pointValues , const typename FEMTreeNode::template ConstNeighbors< UIntPack< BSplineOverlapSizes< FEMSignature< FEMSigs >::Degree >::OverlapSize ... > >& neighbors , const PointEvaluator< UIntPack< FEMSigs ... > , UIntPack< FEMSignature< FEMSigs >::Degree ... > >& bsData , const InterpolationInfo< T , PointD >* interpolationInfo ) const;
2186
2187 template< unsigned int Idx , typename UIntPackFEMSigs , typename WindowSlice , typename ConstNeighbors , typename PointEvaluator , typename ... InterpolationInfos >
2188 typename std::enable_if< ( Idx==sizeof...(InterpolationInfos) ) >::type _addProlongedPointValues( UIntPackFEMSigs , WindowSlice pointValues , const ConstNeighbors &neighbors , const ConstNeighbors &pNeighbors , const PointEvaluator &bsData , std::tuple< InterpolationInfos *... > interpolationInfos ) const {}
2189 template< unsigned int Idx , typename UIntPackFEMSigs , typename WindowSlice , typename ConstNeighbors , typename PointEvaluator , typename ... InterpolationInfos >
2190 typename std::enable_if< ( Idx <sizeof...(InterpolationInfos) ) >::type _addProlongedPointValues( UIntPackFEMSigs , WindowSlice pointValues , const ConstNeighbors &neighbors , const ConstNeighbors &pNeighbors , const PointEvaluator&bsData , std::tuple< InterpolationInfos *... > interpolationInfos ) const
2191 {
2192 _addProlongedPointValues( UIntPackFEMSigs() , pointValues , neighbors , pNeighbors , bsData , std::get< Idx >( interpolationInfos ) );
2193 _addProlongedPointValues< Idx+1 >( UIntPackFEMSigs() , pointValues , neighbors , pNeighbors , bsData , interpolationInfos );
2194 }
2195 template< unsigned int ... FEMSigs , typename T , unsigned int PointD >
2196 void _addProlongedPointValues( UIntPack< FEMSigs ... > , WindowSlice< Real , UIntPack< BSplineOverlapSizes< FEMSignature< FEMSigs >::Degree >::OverlapSize ... > > pointValues , const typename FEMTreeNode::template ConstNeighbors< UIntPack< BSplineOverlapSizes< FEMSignature< FEMSigs >::Degree >::OverlapSize ... > >& neighbors , const typename FEMTreeNode::template ConstNeighbors< UIntPack< BSplineOverlapSizes< FEMSignature< FEMSigs >::Degree >::OverlapSize ... > >& pNeighbors , const PointEvaluator< UIntPack< FEMSigs ... > , UIntPack< FEMSignature< FEMSigs >::Degree ... > >& bsData , const InterpolationInfo< T , PointD >* iInfo ) const;
2197
2198 template< unsigned int Idx , typename PointEvaluator , typename T , typename ... InterpolationInfos >
2199 typename std::enable_if< ( Idx==sizeof...(InterpolationInfos) ) >::type _setPointValuesFromProlongedSolution( LocalDepth highDepth , const PointEvaluator &bsData , ConstPointer( T ) prolongedSolution , std::tuple< InterpolationInfos *... > interpolationInfos ) const {}
2200 template< unsigned int Idx , typename PointEvaluator , typename T , typename ... InterpolationInfos >
2201 typename std::enable_if< ( Idx <sizeof...(InterpolationInfos) ) >::type _setPointValuesFromProlongedSolution( LocalDepth highDepth , const PointEvaluator &bsData , ConstPointer( T ) prolongedSolution , std::tuple< InterpolationInfos *... > interpolationInfos ) const
2202 {
2203 _setPointValuesFromProlongedSolution( highDepth , bsData , prolongedSolution , std::get< Idx >( interpolationInfos ) );
2204 _setPointValuesFromProlongedSolution< Idx+1 >( highDepth , bsData , prolongedSolution , interpolationInfos );
2205 }
2206 template< unsigned int ... FEMSigs , typename T , unsigned int PointD >
2207 void _setPointValuesFromProlongedSolution( LocalDepth highDepth , const PointEvaluator< UIntPack< FEMSigs ... > , UIntPack< FEMSignature< FEMSigs >::Degree ... > >& bsData , ConstPointer( T ) prolongedSolution , InterpolationInfo< T , PointD >* interpolationInfo ) const;
2208
2209 template< unsigned int Idx , typename ConstNeighbors , typename T , typename PointEvaluator , typename ... InterpolationInfos >
2210 typename std::enable_if< ( Idx==sizeof...(InterpolationInfos) ) , T >::type _getInterpolationConstraintFromProlongedSolution( const ConstNeighbors &neighbors , const FEMTreeNode* node , ConstPointer( T ) prolongedSolution , const PointEvaluator &bsData , std::tuple< InterpolationInfos ... > interpolationInfos ) const { return T{}; }
2211 template< unsigned int Idx , typename ConstNeighbors , typename T , typename PointEvaluator , typename ... InterpolationInfos >
2212 typename std::enable_if< ( Idx <sizeof...(InterpolationInfos) ) , T >::type _getInterpolationConstraintFromProlongedSolution( const ConstNeighbors &neighbors , const FEMTreeNode* node , ConstPointer( T ) prolongedSolution , const PointEvaluator &bsData , std::tuple< InterpolationInfos ... > interpolationInfos ) const
2213 {
2214 return
2215 _getInterpolationConstraintFromProlongedSolution( neighbors , node , prolongedSolution , bsData , std::get< Idx >( interpolationInfos ) ) +
2216 _getInterpolationConstraintFromProlongedSolution< Idx+1 >( neighbors , node , prolongedSolution , bsData , interpolationInfos );
2217 }
2218 template< unsigned int ... FEMSigs , typename T , unsigned int PointD >
2219 T _getInterpolationConstraintFromProlongedSolution( const typename FEMTreeNode::template ConstNeighbors< UIntPack< BSplineOverlapSizes< FEMSignature< FEMSigs >::Degree >::OverlapSize ... > >& neighbors , const FEMTreeNode* node , ConstPointer( T ) prolongedSolution , const PointEvaluator< UIntPack< FEMSigs ... > , UIntPack< FEMSignature< FEMSigs >::Degree ... > >& bsData , const InterpolationInfo< T , PointD >* iInfo ) const;
2220
2221 template< unsigned int Idx , typename PointEvaluator , typename T , typename ... InterpolationInfos >
2222 typename std::enable_if< ( Idx==sizeof...(InterpolationInfos) ) >::type _updateRestrictedInterpolationConstraints( const PointEvaluator &bsData , LocalDepth highDepth , ConstPointer( T ) solution , Pointer( T ) cumulativeConstraints , std::tuple< InterpolationInfos ... > interpolationInfos ) const {}
2223 template< unsigned int Idx , typename PointEvaluator , typename T , typename ... InterpolationInfos >
2224 typename std::enable_if< ( Idx <sizeof...(InterpolationInfos) ) >::type _updateRestrictedInterpolationConstraints( const PointEvaluator &bsData , LocalDepth highDepth , ConstPointer( T ) solution , Pointer( T ) cumulativeConstraints , std::tuple< InterpolationInfos ... > interpolationInfos ) const
2225 {
2226 _updateRestrictedInterpolationConstraints( bsData , highDepth , solution , cumulativeConstraints , std::get< Idx >( interpolationInfos ) );
2227 _updateRestrictedInterpolationConstraints< Idx+1 >( bsData , highDepth , solution , cumulativeConstraints , interpolationInfos );
2228 }
2229 template< unsigned int ... FEMSigs , typename T , unsigned int PointD >
2230 void _updateRestrictedInterpolationConstraints( const PointEvaluator< UIntPack< FEMSigs ... > , UIntPack< FEMSignature< FEMSigs >::Degree ... > >& bsData , LocalDepth highDepth , ConstPointer( T ) solution , Pointer( T ) cumulativeConstraints , const InterpolationInfo< T , PointD >* interpolationInfo ) const;
2231
2232 template< unsigned int FEMDegree1 , unsigned int FEMDegree2 > static void _SetParentOverlapBounds( const FEMTreeNode* node , int start[Dim] , int end[Dim] );
2233 template< unsigned int FEMDegree1 , unsigned int FEMDegree2 > static void _SetParentOverlapBounds( int cIdx , int start[Dim] , int end[Dim] );
2234 template< unsigned int ... FEMDegrees1 , unsigned int ... FEMDegrees2 > static void _SetParentOverlapBounds( UIntPack< FEMDegrees1 ... > , UIntPack< FEMDegrees2 ... > , const FEMTreeNode* node , int start[Dim] , int end[Dim] )
2235 {
2236 if( node )
2237 {
2238 int d , off[Dim] ; node->depthAndOffset( d , off );
2239 BaseFEMIntegrator::ParentOverlapBounds( UIntPack< FEMDegrees1 ... >() , UIntPack< FEMDegrees2 ... >() , d , off , start , end );
2240 }
2241 }
2242 template< unsigned int ... FEMDegrees1 , unsigned int ... FEMDegrees2 > static void _SetParentOverlapBounds( UIntPack< FEMDegrees1 ... > , UIntPack< FEMDegrees2 ... > , int cIdx , int start[Dim] , int end[Dim] )
2243 {
2244 BaseFEMIntegrator::ParentOverlapBounds( UIntPack< FEMDegrees1 ... >() , UIntPack< FEMDegrees2 ... >() , cIdx , start , end );
2245 }
2246
2247 template< unsigned int ... FEMSigs >
2248 int _getProlongedMatrixRowSize( const FEMTreeNode* node , const typename FEMTreeNode::template ConstNeighbors< UIntPack< BSplineOverlapSizes< FEMSignature< FEMSigs >::Degree >::OverlapSize ... > >& pNeighbors ) const;
2249 template< typename T , unsigned int ... FEMSigs , typename ... InterpolationInfos , typename MatrixType >
2250 T _setMatrixRowAndGetConstraintFromProlongation( UIntPack< FEMSigs ... > , const BaseSystem< UIntPack< FEMSignature< FEMSigs >::Degree ... > >& F , const typename FEMTreeNode::template ConstNeighbors< UIntPack< BSplineOverlapSizes< FEMSignature< FEMSigs >::Degree >::OverlapSize ... > >& pNeighbors , const typename FEMTreeNode::template ConstNeighbors< UIntPack< BSplineOverlapSizes< FEMSignature< FEMSigs >::Degree >::OverlapSize ... > >& neighbors , size_t idx , MatrixType &M , node_index_type offset , const PCStencils< UIntPack< FEMSignature< FEMSigs >::Degree ... > >& pcStencils , const CCStencil< UIntPack< FEMSignature< FEMSigs >::Degree ... > >& ccStencil , const PointEvaluator< UIntPack< FEMSigs ... > , UIntPack< FEMSignature< FEMSigs >::Degree ... > >& bsData , ConstPointer( T ) prolongedSolution , std::tuple< InterpolationInfos *... > interpolationInfos ) const;
2251 template< typename T , unsigned int ... FEMSigs , typename ... InterpolationInfos >
2252 int _setProlongedMatrixRow( const typename BaseFEMIntegrator::System< UIntPack< FEMSignature< FEMSigs >::Degree ... > >& F , const typename FEMTreeNode::template ConstNeighbors< UIntPack< BSplineOverlapSizes< FEMSignature< FEMSigs >::Degree >::OverlapSize ... > >& neighbors , const typename FEMTreeNode::template ConstNeighbors< UIntPack< BSplineOverlapSizes< FEMSignature< FEMSigs >::Degree >::OverlapSize ... > >& pNeighbors , Pointer( MatrixEntry< Real , matrix_index_type > ) row , node_index_type offset , const DynamicWindow< double , UIntPack< BSplineOverlapSizes< FEMSignature< FEMSigs >::Degree >::OverlapSize ... > >& stencil , const PointEvaluator< UIntPack< FEMSigs ... > , UIntPack< FEMSignature< FEMSigs >::Degree ... > >& bsData , std::tuple< InterpolationInfos *... > interpolationInfos ) const;
2253
2254 // Updates the constraints @(depth) based on the solution coefficients @(depth-1)
2255 template< unsigned int ... FEMSigs , typename T , typename ... InterpolationInfos >
2256 T _getConstraintFromProlongedSolution( UIntPack< FEMSigs ... > , const BaseSystem< UIntPack< FEMSignature< FEMSigs >::Degree ... > >& F , const typename FEMTreeNode::template ConstNeighbors< UIntPack< BSplineOverlapSizes< FEMSignature< FEMSigs >::Degree >::OverlapSize ... > >& neighbors , const typename FEMTreeNode::template ConstNeighbors< UIntPack< BSplineOverlapSizes< FEMSignature< FEMSigs >::Degree >::OverlapSize ... > >& pNeighbors , const FEMTreeNode* node , ConstPointer( T ) prolongedSolution , const DynamicWindow< double , UIntPack< BSplineOverlapSizes< FEMSignature< FEMSigs >::Degree >::OverlapSize ... > >& stencil , const PointEvaluator< UIntPack< FEMSigs ... > , UIntPack< FEMSignature< FEMSigs >::Degree ... > >& bsData , std::tuple< InterpolationInfos *... > interpolationInfos ) const;
2257
2258 template< unsigned int ... FEMSigs , typename T , typename TDotT , typename SORWeights , typename ... InterpolationInfos >
2259 int _solveFullSystemGS( UIntPack< FEMSigs ... > , const typename BaseFEMIntegrator::System< UIntPack< FEMSignature< FEMSigs >::Degree ... > >& F , const PointEvaluator< UIntPack< FEMSigs ... > , UIntPack< FEMSignature< FEMSigs >::Degree ... > >& bsData , LocalDepth depth , Pointer( T ) solution , ConstPointer( T ) prolongedSolution , ConstPointer( T ) constraints , TDotT Dot , int iters , bool coarseToFine , SORWeights sorWeights , _SolverStats& stats , bool computeNorms , std::tuple< InterpolationInfos *... > interpolationInfos ) const;
2260 template< unsigned int ... FEMSigs , typename T , typename TDotT , typename SORWeights , typename ... InterpolationInfos >
2261 int _solveSlicedSystemGS( UIntPack< FEMSigs ... > , const typename BaseFEMIntegrator::System< UIntPack< FEMSignature< FEMSigs >::Degree ... > >& F , const PointEvaluator< UIntPack< FEMSigs ... > , UIntPack< FEMSignature< FEMSigs >::Degree ... > >& bsData , LocalDepth depth , Pointer( T ) solution , ConstPointer( T ) prolongedSolution , ConstPointer( T ) constraints , TDotT Dot , int iters , bool coarseToFine , unsigned int sliceBlockSize , SORWeights sorWeights , _SolverStats& stats , bool computeNorms , std::tuple< InterpolationInfos *... > interpolationInfos ) const;
2262 template< unsigned int ... FEMSigs , typename T , typename TDotT , typename SORWeights , typename ... InterpolationInfos >
2263 int _solveSystemGS( UIntPack< FEMSigs ... > , bool sliced , const typename BaseFEMIntegrator::System< UIntPack< FEMSignature< FEMSigs >::Degree ... > >& F , const PointEvaluator< UIntPack< FEMSigs ... > , UIntPack< FEMSignature< FEMSigs >::Degree ... > >& bsData , LocalDepth depth , Pointer( T ) solution , ConstPointer( T ) prolongedSolution , ConstPointer( T ) constraints , TDotT Dot , int iters , bool coarseToFine , unsigned int sliceBlockSize , SORWeights sorWeights , _SolverStats& stats , bool computeNorms , std::tuple< InterpolationInfos *... > interpolationInfos ) const
2264 {
2265 if( sliced ) return _solveSlicedSystemGS( UIntPack< FEMSigs ... >() , F , bsData , depth , solution , prolongedSolution , constraints , Dot , iters , coarseToFine , sliceBlockSize , sorWeights , stats , computeNorms , interpolationInfos );
2266 else return _solveFullSystemGS ( UIntPack< FEMSigs ... >() , F , bsData , depth , solution , prolongedSolution , constraints , Dot , iters , coarseToFine , sorWeights , stats , computeNorms , interpolationInfos );
2267 }
2268 template< unsigned int ... FEMSigs , typename T , typename TDotT , typename ... InterpolationInfos >
2269 int _solveSystemCG( UIntPack< FEMSigs ... > , const typename BaseFEMIntegrator::System< UIntPack< FEMSignature< FEMSigs >::Degree ... > >& F , const PointEvaluator< UIntPack< FEMSigs ... > , UIntPack< FEMSignature< FEMSigs >::Degree ... > >& bsData , LocalDepth depth , Pointer( T ) solution , ConstPointer( T ) prolongedSolution , ConstPointer( T ) constraints , TDotT Dot , int iters , bool coarseToFine , _SolverStats& stats , bool computeNorms , double cgAccuracy , std::tuple< InterpolationInfos *... > interpolationInfos ) const;
2270 template< unsigned int ... FEMSigs , typename T , typename TDotT , typename ... InterpolationInfos >
2271 void _solveRegularMG( UIntPack< FEMSigs ... > , typename BaseFEMIntegrator::System< UIntPack< FEMSignature< FEMSigs >::Degree ... > >& F , const PointEvaluator< UIntPack< FEMSigs ... > , UIntPack< FEMSignature< FEMSigs >::Degree ... > >& bsData , LocalDepth depth , Pointer( T ) solution , ConstPointer( T ) constraints , TDotT Dot , int vCycles , int iters , _SolverStats& stats , bool computeNorms , double cgAccuracy , std::tuple< InterpolationInfos *... > interpolationInfos ) const;
2272
2273 // Updates the cumulative integral constraints @(depth-1) based on the change in solution coefficients @(depth)
2274 template< unsigned int ... FEMSigs , typename T >
2275 void _updateRestrictedIntegralConstraints( UIntPack< FEMSigs ... > , const typename BaseFEMIntegrator::System< UIntPack< FEMSignature< FEMSigs >::Degree ... > >& F , LocalDepth highDepth , ConstPointer( T ) solution , Pointer( T ) cumulativeConstraints ) const;
2276
2277 template< unsigned int PointD , typename T , unsigned int ... FEMSigs >
2278 CumulativeDerivativeValues< T , Dim , PointD > _coarserFunctionValues( UIntPack< FEMSigs ... > , Point< Real , Dim > p , const ConstPointSupportKey< UIntPack< FEMSignature< FEMSigs >::Degree ... > >& neighborKey , const FEMTreeNode* node , const PointEvaluator< UIntPack< FEMSigs ... > , UIntPack< FEMSignature< FEMSigs >::Degree ... > >& bsData , ConstPointer( T ) coefficients ) const;
2279 template< unsigned int PointD , typename T , unsigned int ... FEMSigs >
2280 CumulativeDerivativeValues< T , Dim , PointD > _finerFunctionValues( UIntPack< FEMSigs ... > , Point< Real , Dim > p , const ConstPointSupportKey< UIntPack< FEMSignature< FEMSigs >::Degree ... > >& neighborKey , const FEMTreeNode* node , const PointEvaluator< UIntPack< FEMSigs ... > , UIntPack< FEMSignature< FEMSigs >::Degree ... > >& bsData , ConstPointer( T ) coefficients ) const;
2281
2282 template< unsigned int ... FEMSigs , typename T , typename ... InterpolationInfos >
2283 int _getSliceMatrixAndProlongationConstraints( UIntPack< FEMSigs ... > , const BaseSystem< UIntPack< FEMSignature< FEMSigs >::Degree ... > >& F , SystemMatrixType< FEMSigs ... > &matrix , Pointer( Real ) diagonalR , const PointEvaluator< UIntPack< FEMSigs ... > , UIntPack< FEMSignature< FEMSigs >::Degree ... > >& bsData , LocalDepth depth , node_index_type nBegin , node_index_type nEnd , ConstPointer( T ) prolongedSolution , Pointer( T ) constraints , const CCStencil < UIntPack< FEMSignature< FEMSigs >::Degree ... > >& ccStencil , const PCStencils< UIntPack< FEMSignature< FEMSigs >::Degree ... > >& pcStencils , std::tuple< InterpolationInfos *... > interpolationInfos ) const;
2284
2285 // Down samples constraints @(depth) to constraints @(depth-1)
2286 template< class C , typename ArrayWrapper , unsigned ... Degrees , unsigned int ... FEMSigs > void _downSample( UIntPack< FEMSigs ... > , typename BaseFEMIntegrator::template RestrictionProlongation< UIntPack< Degrees ... > >& RP , LocalDepth highDepth , ArrayWrapper finerConstraints , Pointer( C ) coarserConstraints ) const;
2287 // Up samples coefficients @(depth-1) to coefficients @(depth)
2288 template< class C , typename ArrayWrapper , unsigned ... Degrees , unsigned int ... FEMSigs > void _upSample( UIntPack< FEMSigs ... > , typename BaseFEMIntegrator::template RestrictionProlongation< UIntPack< Degrees ... > >& RP , LocalDepth highDepth , ArrayWrapper coarserCoefficients , Pointer( C ) finerCoefficients ) const;
2289
2290 template< unsigned int ... FEMSigs , typename ValidNodeFunctor >
2291 SparseMatrix< Real , matrix_index_type > _downSampleMatrix( UIntPack< FEMSigs ... > , LocalDepth highDepth , ValidNodeFunctor validNodeFunctor ) const;
2292 template< unsigned int ... FEMSigs , typename ValidNodeFunctor >
2293 SparseMatrix< Real , matrix_index_type > _upSampleMatrix( UIntPack< FEMSigs ... > , LocalDepth highDepth , ValidNodeFunctor validNodeFunctor ) const;
2294 template< unsigned int ... FEMSigs , typename ValidNodeFunctor >
2295 SparseMatrix< Real , matrix_index_type > _restrictSystemMatrix( UIntPack< FEMSigs ... > , LocalDepth highDepth , const SparseMatrix< Real , matrix_index_type > &M , ValidNodeFunctor validNodeFunctor ) const;
2296
2297 template< bool XMajor , class C , unsigned int ... FEMSigs > static void _RegularGridUpSample( UIntPack< FEMSigs ... > , LocalDepth highDepth , ConstPointer( C ) lowCoefficients , Pointer( C ) highCoefficients );
2298 template< bool XMajor , class C , unsigned int ... FEMSigs > static void _RegularGridUpSample( UIntPack< FEMSigs ... > , const int lowBegin[] , const int lowEnd[] , const int highBegin[] , const int highEnd[] , LocalDepth highDepth , ConstPointer( C ) lowCoefficients , Pointer( C ) highCoefficients );
2299 public:
2300 template< class C , unsigned int ... FEMSigs > DenseNodeData< C , UIntPack< FEMSigs ... > > coarseCoefficients( const DenseNodeData< C , UIntPack< FEMSigs ... > >& coefficients ) const;
2301 template< class C , unsigned int ... FEMSigs > DenseNodeData< C , UIntPack< FEMSigs ... > > coarseCoefficients( const SparseNodeData< C , UIntPack< FEMSigs ... > >& coefficients ) const;
2302 template< class C , unsigned int ... FEMSigs > DenseNodeData< C , UIntPack< FEMSigs ... > > denseCoefficients( const SparseNodeData< C , UIntPack< FEMSigs ... > >& coefficients ) const;
2303
2304 void trimToDepth( LocalDepth coarseDepth );
2305
2306 template< class C , unsigned int ... FEMSigs > DenseNodeData< C , UIntPack< FEMSigs ... > > trimToDepth( const DenseNodeData< C , UIntPack< FEMSigs ... > >& coefficients , LocalDepth coarseDepth ) const;
2307 template< class C , unsigned int ... FEMSigs > SparseNodeData< C , UIntPack< FEMSigs ... > > trimToDepth( const SparseNodeData< C , UIntPack< FEMSigs ... > >& coefficients , LocalDepth coarseDepth ) const;
2308
2309 template< typename T , unsigned int PointD , typename ConstraintDual , typename SystemDual >
2310 ApproximatePointInterpolationInfo< T , PointD , ConstraintDual , SystemDual > trimToDepth( const ApproximatePointInterpolationInfo< T , PointD , ConstraintDual , SystemDual > &iInfo , LocalDepth coarseDepth ) const;
2311 template< typename T , typename Data , unsigned int PointD , typename ConstraintDual , typename SystemDual >
2312 ApproximatePointAndDataInterpolationInfo< T , Data , PointD , ConstraintDual , SystemDual > trimToDepth( const ApproximatePointAndDataInterpolationInfo< T , Data , PointD , ConstraintDual , SystemDual > &iInfo , LocalDepth coarseDepth ) const;
2313
2314 // For each (valid) fem node, compute the ratio of the sum of active prolongation weights to the sum of total prolongation weights
2315 // If the prolongToChildren flag is set, then these weights are pushed to the children by computing the ratio of the prolongation of the above weights to the prolongation of unity weights
2316
2317 template< unsigned int ... FEMSigs > DenseNodeData< Real , UIntPack< FEMSigs ... > > supportWeights( UIntPack< FEMSigs ... > ) const;
2318 template< unsigned int ... FEMSigs > DenseNodeData< Real , UIntPack< FEMSigs ... > > prolongationWeights( UIntPack< FEMSigs ... > , bool prolongToChildren ) const;
2319
2320 protected:
2321
2322 //////////////////////////////////////////////
2323 // Code for splatting point-sample data //
2324 // MultiGridFEMTreeData.WeightedSamples.inl //
2325 //////////////////////////////////////////////
2326 template< unsigned int CoDim , unsigned int Degree >
2327 Real _GetScaleValue( unsigned int res ) const;
2328 template< unsigned int CoDim , unsigned int Degree >
2329 Real _GetScaleValue( Point< Real , Dim > p ) const;
2330 template< bool ThreadSafe , unsigned int CoDim , unsigned int WeightDegree >
2331 void _addWeightContribution( Allocator< FEMTreeNode > *nodeAllocator , DensityEstimator< WeightDegree >& densityWeights , FEMTreeNode* node , Point< Real , Dim > position , PointSupportKey< IsotropicUIntPack< Dim , WeightDegree > >& weightKey , Real weight=Real(1.0) );
2332 template< unsigned int WeightDegree , class PointSupportKey >
2333 Real _getSamplesPerNode( const DensityEstimator< WeightDegree >& densityWeights , const FEMTreeNode* node , Point< Real , Dim > position , PointSupportKey& weightKey ) const;
2334 template< unsigned int WeightDegree , class WeightKey >
2335 void _getSampleDepthAndWeight( const DensityEstimator< WeightDegree >& densityWeights , const FEMTreeNode* node , Point< Real , Dim > position , WeightKey& weightKey , Real& depth , Real& weight ) const;
2336 template< unsigned int WeightDegree , class WeightKey >
2337 void _getSampleDepthAndWeight( const DensityEstimator< WeightDegree >& densityWeights , Point< Real , Dim > position , WeightKey& weightKey , Real& depth , Real& weight ) const;
2338
2339 template< bool CreateNodes , bool ThreadSafe , class V , unsigned int ... DataSigs > void _splatPointData( V zero , Allocator< FEMTreeNode > *nodeAllocator , FEMTreeNode* node , Point< Real , Dim > point , V v , SparseNodeData< V , UIntPack< DataSigs ... > >& data , PointSupportKey< UIntPack< FEMSignature< DataSigs >::Degree ... > >& dataKey );
2340 template< bool CreateNodes , bool ThreadSafe , unsigned int WeightDegree , class V , unsigned int ... DataSigs > Point< Real , 2 > _splatPointData( V zero , Allocator< FEMTreeNode > *nodeAllocator , const DensityEstimator< WeightDegree >& densityWeights , Real minDepthCutoff , Point< Real , Dim > point , V v , SparseNodeData< V , UIntPack< DataSigs ... > >& data , PointSupportKey< IsotropicUIntPack< Dim , WeightDegree > >& weightKey , PointSupportKey< UIntPack< FEMSignature< DataSigs >::Degree ... > >& dataKey , LocalDepth minDepth , LocalDepth maxDepth , int dim , Real depthBias );
2341 template< bool CreateNodes , bool ThreadSafe , unsigned int WeightDegree , class V , unsigned int ... DataSigs > Point< Real , 2 > _splatPointData( V zero , Allocator< FEMTreeNode > *nodeAllocator , const DensityEstimator< WeightDegree >& densityWeights , Real minDepthCutoff , Point< Real , Dim > point , V v , SparseNodeData< V , UIntPack< DataSigs ... > >& data , PointSupportKey< IsotropicUIntPack< Dim , WeightDegree > >& weightKey , PointSupportKey< UIntPack< FEMSignature< DataSigs >::Degree ... > >& dataKey , LocalDepth minDepth , std::function< int ( Point< Real , Dim > ) > &pointDepthFunctor , int dim , Real depthBias );
2342 template< bool CreateNodes , bool ThreadSafe , unsigned int WeightDegree , class V , unsigned int ... DataSigs > Point< Real , 2 > _multiSplatPointData( V zero , Allocator< FEMTreeNode > *nodeAllocator , const DensityEstimator< WeightDegree >* densityWeights , FEMTreeNode* node , Point< Real , Dim > point , V v , SparseNodeData< V , UIntPack< DataSigs ... > >& data , PointSupportKey< IsotropicUIntPack< Dim , WeightDegree > >& weightKey , PointSupportKey< UIntPack< FEMSignature< DataSigs >::Degree ... > >& dataKey , int dim );
2343 template< unsigned int WeightDegree , class V , unsigned int ... DataSigs > Real _nearestMultiSplatPointData( V zero , const DensityEstimator< WeightDegree >* densityWeights , FEMTreeNode* node , Point< Real , Dim > point , V v , SparseNodeData< V , UIntPack< DataSigs ... > >& data , PointSupportKey< IsotropicUIntPack< Dim , WeightDegree > >& weightKey , int dim=Dim );
2344 template< class V , class Coefficients , unsigned int D , unsigned int ... DataSigs >
2345 void _addEvaluation( const Coefficients& coefficients , Point< Real , Dim > p , const PointEvaluator< UIntPack< DataSigs ... > , IsotropicUIntPack< Dim , D > >& pointEvaluator , const ConstPointSupportKey< UIntPack< FEMSignature< DataSigs >::Degree ... > >& dataKey , V &value ) const;
2346 template< class V , class Coefficients , unsigned int D , unsigned int ... DataSigs >
2347 void _addEvaluation( const Coefficients& coefficients , Point< Real , Dim > p , LocalDepth pointDepth , const PointEvaluator< UIntPack< DataSigs ... > , IsotropicUIntPack< Dim , D > >& pointEvaluator , const ConstPointSupportKey< UIntPack< FEMSignature< DataSigs >::Degree ... > >& dataKey , V &value ) const;
2348 template< typename V , typename Coefficients , unsigned int D , typename AccumulationFunctor /*=std::function< void ( const V & , Real s ) > */ , unsigned int ... DataSigs >
2349 void _accumulate( const Coefficients& coefficients , Point< Real , Dim > p , const PointEvaluator< UIntPack< DataSigs ... > , IsotropicUIntPack< Dim , D > >& pointEvaluator , const ConstPointSupportKey< UIntPack< FEMSignature< DataSigs >::Degree ... > >& dataKey , AccumulationFunctor &AF ) const;
2350 template< typename V , typename Coefficients , unsigned int D , typename AccumulationFunctor /*=std::function< void ( const V & , Real s ) > */ , unsigned int ... DataSigs >
2351 void _accumulate( const Coefficients& coefficients , Point< Real , Dim > p , LocalDepth pointDepth , const PointEvaluator< UIntPack< DataSigs ... > , IsotropicUIntPack< Dim , D > >& pointEvaluator , const ConstPointSupportKey< UIntPack< FEMSignature< DataSigs >::Degree ... > >& dataKey , AccumulationFunctor &AF ) const;
2352
2353 public:
2354
2355 template< bool XMajor , class V , unsigned int ... DataSigs > Pointer( V ) regularGridEvaluate( const DenseNodeData< V , UIntPack< DataSigs ... > >& coefficients , int& res , LocalDepth depth=-1 , bool primal=false ) const;
2356 template< bool XMajor , class V , unsigned int ... DataSigs > Pointer( V ) regularGridEvaluate( const DenseNodeData< V , UIntPack< DataSigs ... > >& coefficients , const unsigned int begin[Dim] , const unsigned int end[Dim] , unsigned int res[Dim] , bool primal=false ) const;
2357 template< bool XMajor , class V , unsigned int ... DataSigs > Pointer( V ) regularGridUpSample( const DenseNodeData< V , UIntPack< DataSigs ... > >& coefficients , LocalDepth depth=-1 ) const;
2358 template< bool XMajor , class V , unsigned int ... DataSigs > Pointer( V ) regularGridUpSample( const DenseNodeData< V , UIntPack< DataSigs ... > >& coefficients , const int begin[Dim] , const int end[Dim] , LocalDepth depth=-1 ) const;
2359 template< class V , unsigned int ... DataSigs > V average( const DenseNodeData< V , UIntPack< DataSigs ... > >& coefficients ) const;
2360 template< class V , unsigned int ... DataSigs > V average( const DenseNodeData< V , UIntPack< DataSigs ... > >& coefficients , const Real begin[Dim] , const Real end[Dim] ) const;
2361 template< typename T > struct HasNormalDataFunctor{};
2362 template< unsigned int ... NormalSigs >
2363 struct HasNormalDataFunctor< UIntPack< NormalSigs ... > >
2364 {
2365 const SparseNodeData< Point< Real , Dim > , UIntPack< NormalSigs ... > >& normalInfo;
2366 HasNormalDataFunctor( const SparseNodeData< Point< Real , Dim > , UIntPack< NormalSigs ... > >& ni ) : normalInfo( ni ){ ; }
2367 bool operator() ( const FEMTreeNode* node ) const
2368 {
2369 const Point< Real , Dim >* n = normalInfo( node );
2370 if( n )
2371 {
2372 const Point< Real , Dim >& normal = *n;
2373 for( int d=0 ; d<Dim ; d++ ) if( normal[d]!=0 ) return true;
2374 }
2375 if( node->children ) for( int c=0 ; c<(1<<Dim) ; c++ ) if( (*this)( node->children + c ) ) return true;
2376 return false;
2377 }
2378 };
2379 struct TrivialHasDataFunctor{ bool operator() ( const FEMTreeNode* node ) const { return true; } };
2380 protected:
2381 // [NOTE] The input/output for this method is pre-scaled by weight
2382 template< typename T > bool _setInterpolationInfoFromChildren( FEMTreeNode* node , SparseNodeData< T , IsotropicUIntPack< Dim , FEMTrivialSignature > >& iInfo ) const;
2383 template< typename T , unsigned int PointD , typename ConstraintDual > SparseNodeData< DualPointInfo < Dim , Real , T , PointD > , IsotropicUIntPack< Dim , FEMTrivialSignature > > _densifyInterpolationInfoAndSetDualConstraints( const std::vector< PointSample >& samples , ConstraintDual constraintDual , LocalDepth maxDepth , int adaptiveExponent ) const;
2384 template< typename T , typename Data , unsigned int PointD , typename ConstraintDual > SparseNodeData< DualPointAndDataInfo< Dim , Real , Data , T , PointD > , IsotropicUIntPack< Dim , FEMTrivialSignature > > _densifyInterpolationInfoAndSetDualConstraints( const std::vector< PointSample >& samples , ConstPointer( Data ) sampleData , ConstraintDual constraintDual , LocalDepth maxDepth , int adaptiveExponent ) const;
2385 template< typename T , unsigned int PointD , typename ConstraintDual > void _densifyInterpolationInfoAndSetDualConstraints( SparseNodeData< DualPointInfo < Dim , Real , T , PointD > , IsotropicUIntPack< Dim , FEMTrivialSignature > > &iInfo , const std::vector< PointSample >& samples , ConstraintDual constraintDual , LocalDepth maxDepth , int adaptiveExponent ) const;
2386 template< typename T , typename Data , unsigned int PointD , typename ConstraintDual > void _densifyInterpolationInfoAndSetDualConstraints( SparseNodeData< DualPointAndDataInfo< Dim , Real , Data , T , PointD > , IsotropicUIntPack< Dim , FEMTrivialSignature > > &iInfo , const std::vector< PointSample >& samples , ConstPointer( Data ) sampleData , ConstraintDual constraintDual , LocalDepth maxDepth , int adaptiveExponent ) const;
2387 template< typename T , unsigned int PointD , typename ConstraintDual > SparseNodeData< DualPointInfoBrood < Dim , Real , T , PointD > , IsotropicUIntPack< Dim , FEMTrivialSignature > > _densifyChildInterpolationInfoAndSetDualConstraints( const std::vector< PointSample >& samples , ConstraintDual constraintDual , bool noRescale ) const;
2388 template< typename T , typename Data , unsigned int PointD , typename ConstraintDual > SparseNodeData< DualPointAndDataInfoBrood< Dim , Real , Data , T , PointD > , IsotropicUIntPack< Dim , FEMTrivialSignature > > _densifyChildInterpolationInfoAndSetDualConstraints( const std::vector< PointSample >& samples , ConstPointer( Data ) sampleData , ConstraintDual constraintDual , bool noRescale ) const;
2389
2390 void _setSpaceValidityFlags( void ) const;
2391 template< unsigned int ... FEMSigs1 > void _setFEM1ValidityFlags( UIntPack< FEMSigs1 ... > ) const;
2392 template< unsigned int ... FEMSigs2 > void _setFEM2ValidityFlags( UIntPack< FEMSigs2 ... > ) const;
2393 template< class HasDataFunctor > void _clipTree( const HasDataFunctor& f , LocalDepth fullDepth );
2394
2395 public:
2396
2397 template< unsigned int PointD , unsigned int ... FEMSigs > SparseNodeData< CumulativeDerivativeValues< Real , Dim , PointD > , IsotropicUIntPack< Dim , FEMTrivialSignature > > leafValues( const DenseNodeData< Real , UIntPack< FEMSigs ... > >& coefficients , int maxDepth=-1 ) const;
2398
2399 protected:
2400
2401 /////////////////////////////////////
2402 // Evaluation Methods //
2403 // MultiGridFEMTreeData.Evaluation //
2404 /////////////////////////////////////
2405 static const unsigned int CHILDREN = 1<<Dim;
2406 template< typename Pack , unsigned int PointD > struct _Evaluator{ };
2407 template< unsigned int ... FEMSigs , unsigned int PointD >
2408 struct _Evaluator< UIntPack< FEMSigs ... > , PointD >
2409 {
2410 static_assert( Dim == sizeof...(FEMSigs) , "[ERROR] Number of signatures doesn't match dimension" );
2411
2412 typedef DynamicWindow< CumulativeDerivativeValues< double , Dim , PointD > , UIntPack< BSplineSupportSizes< FEMSignature< FEMSigs >::Degree >::SupportSize ... > > CenterStencil;
2413 typedef DynamicWindow< CumulativeDerivativeValues< double , Dim , PointD > , UIntPack< BSplineSupportSizes< FEMSignature< FEMSigs >::Degree >::SupportSize ... > > CornerStencil;
2414 typedef DynamicWindow< CumulativeDerivativeValues< double , Dim , PointD > , UIntPack< ( BSplineSupportSizes< FEMSignature< FEMSigs >::Degree >::BCornerSize + 1 ) ... > > BCornerStencil;
2415
2416 typedef std::tuple< typename BSplineEvaluationData< FEMSigs >::template Evaluator< PointD > ... > Evaluators;
2417 typedef std::tuple< typename BSplineEvaluationData< FEMSigs >::template ChildEvaluator< PointD > ... > ChildEvaluators;
2418 struct StencilData
2419 {
2420 CenterStencil ccCenterStencil , pcCenterStencils[CHILDREN];
2421 CornerStencil ccCornerStencil[CHILDREN] , pcCornerStencils[CHILDREN][CHILDREN];
2422 BCornerStencil ccBCornerStencil[CHILDREN] , pcBCornerStencils[CHILDREN][CHILDREN];
2423 };
2424 Pointer( StencilData ) stencilData;
2425 Pointer( Evaluators ) evaluators;
2426 Pointer( ChildEvaluators ) childEvaluators;
2427
2428 void set( LocalDepth depth );
2429 _Evaluator( void ){ _pointEvaluator = NULL ; stencilData = NullPointer( StencilData ) , evaluators = NullPointer( Evaluators ) , childEvaluators = NullPointer( ChildEvaluators ); }
2430 ~_Evaluator( void ){ if( _pointEvaluator ) delete _pointEvaluator , _pointEvaluator = NULL ; if( stencilData ) DeletePointer( stencilData ) ; if( evaluators ) DeletePointer( evaluators ) ; if( childEvaluators ) DeletePointer( childEvaluators ); }
2431 protected:
2432 enum _CenterOffset{ CENTER=-1 , BACK=0 , FRONT=1 };
2433 template< unsigned int _PointD=PointD > CumulativeDerivativeValues< double , Dim , _PointD > _values( unsigned int d , const int fIdx[Dim] , const int idx[Dim] , const _CenterOffset off[Dim] , bool parentChild ) const;
2434 template< unsigned int _PointD=PointD > CumulativeDerivativeValues< double , Dim , _PointD > _centerValues( unsigned int d , const int fIdx[Dim] , const int idx[Dim] , bool parentChild ) const;
2435 template< unsigned int _PointD=PointD > CumulativeDerivativeValues< double , Dim , _PointD > _cornerValues( unsigned int d , const int fIdx[Dim] , const int idx[Dim] , int corner , bool parentChild ) const;
2436 template< unsigned int _PointD=PointD , unsigned int I=0 > typename std::enable_if< I==Dim >::type _setDValues( unsigned int d , const int fIdx[] , const int cIdx[] , const _CenterOffset off[] , bool pc , double dValues[][_PointD+1] ) const{ }
2437 template< unsigned int _PointD=PointD , unsigned int I=0 > typename std::enable_if< (I<Dim) >::type _setDValues( unsigned int d , const int fIdx[] , const int cIdx[] , const _CenterOffset off[] , bool pc , double dValues[][_PointD+1] ) const
2438 {
2439 if( pc ) for( int dd=0 ; dd<=_PointD ; dd++ ) dValues[I][dd] = off[I]==CENTER ? std::get< I >( childEvaluators[d] ).centerValue( fIdx[I] , cIdx[I] , dd ) : std::get< I >( childEvaluators[d] ).cornerValue( fIdx[I] , cIdx[I]+off[I] , dd );
2440 else for( int dd=0 ; dd<=_PointD ; dd++ ) dValues[I][dd] = off[I]==CENTER ? std::get< I >( evaluators[d] ).centerValue( fIdx[I] , cIdx[I] , dd ) : std::get< I >( evaluators[d] ).cornerValue( fIdx[I] , cIdx[I]+off[I] , dd );
2441 _setDValues< _PointD , I+1 >( d , fIdx , cIdx , off , pc , dValues );
2442 }
2443
2444 template< unsigned int I=0 > typename std::enable_if< I==Dim >::type _setEvaluators( unsigned int maxDepth ){ }
2445 template< unsigned int I=0 > typename std::enable_if< (I<Dim) >::type _setEvaluators( unsigned int maxDepth )
2446 {
2447 static const unsigned int FEMSig = UIntPack< FEMSigs ... >::template Get< I >();
2448 for( unsigned int d=0 ; d<=maxDepth ; d++ ) BSplineEvaluationData< FEMSig >:: SetEvaluator( std::template get< I >( evaluators[d] ) , d );
2449 for( unsigned int d=1 ; d<=maxDepth ; d++ ) BSplineEvaluationData< FEMSig >::SetChildEvaluator( std::template get< I >( childEvaluators[d] ) , d-1 );
2450 _setEvaluators< I+1 >( maxDepth );
2451 }
2452 typename FEMIntegrator::template PointEvaluator< UIntPack< FEMSigs ... > , IsotropicUIntPack< Dim , PointD > >* _pointEvaluator;
2453 friend FEMTree;
2454 };
2455
2456 template< class V , unsigned int _PointD , unsigned int ... FEMSigs , unsigned int PointD >
2457 CumulativeDerivativeValues< V , Dim , _PointD > _getCenterValues( const ConstPointSupportKey< UIntPack< FEMSignature< FEMSigs >::Degree ... > >& neighborKey , const FEMTreeNode* node , ConstPointer( V ) solution , ConstPointer( V ) coarseSolution , const _Evaluator< UIntPack< FEMSigs ... > , PointD >& evaluator , int maxDepth , bool isInterior ) const;
2458 template< class V , unsigned int _PointD , unsigned int ... FEMSigs , unsigned int PointD >
2459 CumulativeDerivativeValues< V , Dim , _PointD > _getCornerValues( const ConstPointSupportKey< UIntPack< FEMSignature< FEMSigs >::Degree ... > >& neighborKey , const FEMTreeNode* node , int corner , ConstPointer( V ) solution , ConstPointer( V ) coarseSolution , const _Evaluator< UIntPack< FEMSigs ... > , PointD >& evaluator , int maxDepth , bool isInterior ) const;
2460 template< class V , unsigned int _PointD , unsigned int ... FEMSigs , unsigned int PointD >
2461 CumulativeDerivativeValues< V , Dim , _PointD > _getValues ( const ConstPointSupportKey< UIntPack< FEMSignature< FEMSigs >::Degree ... > >& neighborKey , const FEMTreeNode* node , Point< Real , Dim > p , ConstPointer( V ) solution , ConstPointer( V ) coarseSolution , const _Evaluator< UIntPack< FEMSigs ... > , PointD >& evaluator , int maxDepth ) const;
2462 template< class V , unsigned int _PointD , unsigned int ... FEMSigs , unsigned int PointD >
2463 CumulativeDerivativeValues< V , Dim , _PointD > _getCornerValues( const ConstCornerSupportKey< UIntPack< FEMSignature< FEMSigs >::Degree ... > >& neighborKey , const FEMTreeNode* node , int corner , ConstPointer( V ) solution , ConstPointer( V ) coarseSolution , const _Evaluator< UIntPack< FEMSigs ... > , PointD >& evaluator , int maxDepth , bool isInterior ) const;
2464 template< unsigned int ... SupportSizes >
2465 struct CornerLoopData
2466 {
2467 typedef UIntPack< SupportSizes ... > _SupportSizes;
2468 // static const unsigned int supportSizes[] = { SupportSizes ... };
2469 static const unsigned int supportSizes[];
2470 unsigned int ccSize[1<<Dim] , pcSize[1<<Dim][1<<Dim];
2471 unsigned int ccIndices[1<<Dim] [ WindowSize< _SupportSizes >::Size ];
2472 unsigned int pcIndices[1<<Dim][1<<Dim][ WindowSize< _SupportSizes >::Size ];
2473 CornerLoopData( void )
2474 {
2475 int start[Dim] , end[Dim] , _start[Dim] , _end[Dim];
2476 for( int c=0 ; c<(1<<Dim) ; c++ )
2477 {
2478 ccSize[c] = 0;
2479 for( int dd=0 ; dd<Dim ; dd++ )
2480 {
2481 start[dd] = 0 , end[dd] = supportSizes[dd];
2482 if( (c>>dd) & 1 ) start[dd]++;
2483 else end [dd]--;
2484 }
2485 unsigned int idx[Dim];
2486 WindowLoop< Dim >::Run
2487 (
2488 start , end ,
2489 [&]( int d , int i ){ idx[d] = i; } ,
2490 [&]( void ){ ccIndices[c][ ccSize[c]++ ] = GetWindowIndex( _SupportSizes() , idx ); }
2491 );
2492
2493 for( int _c=0 ; _c<(1<<Dim) ; _c++ )
2494 {
2495 pcSize[c][_c] = 0;
2496 for( int dd=0 ; dd<Dim ; dd++ )
2497 {
2498 if( ( (_c>>dd) & 1 ) != ( (c>>dd) & 1 ) ) _start[dd] = 0 , _end[dd] = supportSizes[dd];
2499 else _start[dd] = start[dd] , _end[dd] = end[dd];
2500 }
2501
2502 unsigned int idx[Dim];
2503 WindowLoop< Dim >::Run
2504 (
2505 _start , _end ,
2506 [&]( int d , int i ){ idx[d] = i; } ,
2507 [&]( void ){ pcIndices[c][_c][ pcSize[c][_c]++ ] = GetWindowIndex( _SupportSizes() , idx ); }
2508 );
2509 }
2510 }
2511 }
2512 };
2513 public:
2514 template< typename Pack , unsigned int PointD , typename T > struct _MultiThreadedEvaluator{ };
2515 template< unsigned int ... FEMSigs , unsigned int PointD , typename T >
2516 struct _MultiThreadedEvaluator< UIntPack< FEMSigs ... > , PointD , T >
2517 {
2518 typedef UIntPack< FEMSigs ... > FEMSignatures;
2519 typedef UIntPack< FEMSignature< FEMSigs >::Degree ... > FEMDegrees;
2520 const FEMTree* _tree;
2521 int _threads;
2522 std::vector< ConstPointSupportKey< FEMDegrees > > _pointNeighborKeys;
2523 std::vector< ConstCornerSupportKey< FEMDegrees > > _cornerNeighborKeys;
2524 _Evaluator< FEMSignatures , PointD > _evaluator;
2525 const DenseNodeData< T , FEMSignatures >& _coefficients;
2526 DenseNodeData< T , FEMSignatures > _coarseCoefficients;
2527 public:
2528 _MultiThreadedEvaluator( const FEMTree* tree , const DenseNodeData< T , FEMSignatures >& coefficients , int threads=ThreadPool::NumThreads() );
2529 template< unsigned int _PointD=PointD > CumulativeDerivativeValues< T , Dim , _PointD > values( Point< Real , Dim > p , int thread=0 , const FEMTreeNode* node=NULL );
2530 template< unsigned int _PointD=PointD > CumulativeDerivativeValues< T , Dim , _PointD > centerValues( const FEMTreeNode* node , int thread=0 );
2531 template< unsigned int _PointD=PointD > CumulativeDerivativeValues< T , Dim , _PointD > cornerValues( const FEMTreeNode* node , int corner , int thread=0 );
2532 };
2533 template< typename Pack , unsigned int PointD , typename T=Real > using MultiThreadedEvaluator = _MultiThreadedEvaluator< Pack , PointD , T >;
2534 template< unsigned int DensityDegree >
2535 struct MultiThreadedWeightEvaluator
2536 {
2537 const FEMTree* _tree;
2538 int _threads;
2539 std::vector< ConstPointSupportKey< IsotropicUIntPack< Dim , DensityDegree > > > _neighborKeys;
2540 const DensityEstimator< DensityDegree >& _density;
2541 public:
2542 MultiThreadedWeightEvaluator( const FEMTree* tree , const DensityEstimator< DensityDegree >& density , int threads=ThreadPool::NumThreads() );
2543 Real weight( Point< Real , Dim > p , int thread=0 );
2544 };
2545
2546 template< typename Pack , typename T > struct MultiThreadedSparseEvaluator{ };
2547 template< unsigned int ... FEMSigs , typename T >
2548 struct MultiThreadedSparseEvaluator< UIntPack< FEMSigs ... > , T >
2549 {
2550 typedef UIntPack< FEMSigs ... > FEMSignatures;
2551 typedef UIntPack< FEMSignature< FEMSigs >::Degree ... > FEMDegrees;
2552 const FEMTree* _tree;
2553 int _threads;
2554 std::vector< ConstPointSupportKey< FEMDegrees > > _pointNeighborKeys;
2555 typename FEMIntegrator::template PointEvaluator< UIntPack< FEMSigs ... > , ZeroUIntPack< Dim > > *_pointEvaluator;
2556 const SparseNodeData< T , FEMSignatures >& _coefficients;
2557 public:
2558 MultiThreadedSparseEvaluator( const FEMTree* tree , const SparseNodeData< T , FEMSignatures >& coefficients , int threads=ThreadPool::NumThreads() );
2559 ~MultiThreadedSparseEvaluator( void ){ if( _pointEvaluator ) delete _pointEvaluator; }
2560 void addValue( Point< Real , Dim > p , T &t , int thread=0 , const FEMTreeNode* node=NULL );
2561 template< typename AccumulationFunctor/*=std::function< void ( const T & , Real s ) > */ >
2562 void accumulate( Point< Real , Dim > p , AccumulationFunctor &Accumulate , int thread=0 , const FEMTreeNode* node=NULL );
2563 };
2564
2565 protected:
2566
2567 template< unsigned int Idx , typename ... DenseOrSparseNodeData >
2568 typename std::enable_if< ( Idx==sizeof...(DenseOrSparseNodeData) ) >::type _reorderDenseOrSparseNodeData( ConstPointer( node_index_type ) map , size_t sz , std::tuple< DenseOrSparseNodeData* ... > data ){}
2569 template< unsigned int Idx , typename ... DenseOrSparseNodeData >
2570 typename std::enable_if< ( Idx <sizeof...(DenseOrSparseNodeData) ) >::type _reorderDenseOrSparseNodeData( ConstPointer( node_index_type ) map , size_t sz , std::tuple< DenseOrSparseNodeData* ... > data )
2571 {
2572 if( std::get<Idx>( data ) ) std::get<Idx>( data )->_remapIndices( map , sz );
2573 _reorderDenseOrSparseNodeData< Idx+1 >( map , sz , data );
2574 }
2575
2576 template< unsigned int Idx , typename ... InterpolationInfos >
2577 typename std::enable_if< ( Idx==sizeof...(InterpolationInfos) ) >::type _reorderInterpolationInfo( ConstPointer( node_index_type ) map , size_t sz , std::tuple< InterpolationInfos* ... > interpolationInfos ){}
2578 template< unsigned int Idx , typename ... InterpolationInfos >
2579 typename std::enable_if< ( Idx <sizeof...(InterpolationInfos) ) >::type _reorderInterpolationInfo( ConstPointer( node_index_type ) map , size_t sz , std::tuple< InterpolationInfos* ... > interpolationInfos )
2580 {
2581 if( std::get<Idx>( interpolationInfos ) ) std::get<Idx>( interpolationInfos )->_remapIndices( map , sz );
2582 _reorderInterpolationInfo< Idx+1 >( map , sz , interpolationInfos );
2583 }
2584
2585 public:
2586
2587 FEMTree( size_t blockSize );
2588 FEMTree( BinaryStream &stream , size_t blockSize );
2589 template< unsigned int CrossDegree , unsigned int Pad >
2590 static FEMTree< Dim , Real > *Slice( const FEMTree< Dim+1 , Real > &tree , unsigned int sliceDepth , unsigned int sliceIndex , bool includeBounds , size_t blockSize );
2591
2592 static FEMTree< Dim , Real > *Merge( const FEMTree< Dim , Real > &tree1 , const FEMTree< Dim , Real > &tree2 , size_t blockSize );
2593 ~FEMTree( void )
2594 {
2595 _tree.cleanChildren( !nodeAllocators.size() );
2596 for( size_t i=0 ; i<nodeAllocators.size() ; i++ ) delete nodeAllocators[i];
2597 }
2598 void write( BinaryStream &stream , bool serialize ) const;
2599 static void WriteParameter( BinaryStream &stream )
2600 {
2601 FEMTreeRealType realType;
2602 if ( typeid( Real )==typeid( float ) ) realType=FEM_TREE_REAL_FLOAT;
2603 else if( typeid( Real )==typeid( double ) ) realType=FEM_TREE_REAL_DOUBLE;
2604 else MK_THROW( "Unrecognized real type" );
2605 stream.write( realType );
2606 int dim = Dim;
2607 stream.write( dim );
2608 }
2609
2610 template< unsigned int LeftRadius , unsigned int RightRadius , bool CreateNodes , typename ProcessingNodeFunctor , typename ... DenseOrSparseNodeData , typename InitializeFunctor > void processNeighbors( ProcessingNodeFunctor processNodes , std::tuple< DenseOrSparseNodeData *... > data , InitializeFunctor initialize );
2611 template< unsigned int LeftRadius , unsigned int RightRadius , bool CreateNodes , typename ... DenseOrSparseNodeData , typename InitializeFunctor > void processNeighbors( FEMTreeNode** nodes , size_t nodeCount , std::tuple< DenseOrSparseNodeData *... > data , InitializeFunctor initialize );
2612 template< unsigned int Radius , bool CreateNodes , typename ProcessingNodeFunctor , typename ... DenseOrSparseNodeData , typename InitializeFunctor > void processNeighbors( ProcessingNodeFunctor processNodes , std::tuple< DenseOrSparseNodeData *... > data , InitializeFunctor initialize ){ processNeighbors< Radius , Radius , CreateNodes >( processNodes , data , initialize ); }
2613 template< unsigned int Radius , bool CreateNodes , typename ... DenseOrSparseNodeData , typename InitializeFunctor > void processNeighbors( FEMTreeNode** nodes , size_t nodeCount , std::tuple< DenseOrSparseNodeData *... > data , InitializeFunctor initialize ){ processNeighbors< Radius , Radius , CreateNodes >( nodes , nodeCount , data , initialize ); }
2614
2615 template< unsigned int LeftRadius , unsigned int RightRadius , bool CreateNodes , typename ProcessingNodeFunctor , typename ... DenseOrSparseNodeData > void processNeighbors( ProcessingNodeFunctor processNodes , std::tuple< DenseOrSparseNodeData *... > data ){ return processNeighbors< LeftRadius , RightRadius , CreateNodes >( processNodes , data , []( const FEMTreeNode * ){} ); }
2616 template< unsigned int LeftRadius , unsigned int RightRadius , bool CreateNodes , typename ... DenseOrSparseNodeData > void processNeighbors( FEMTreeNode** nodes , size_t nodeCount , std::tuple< DenseOrSparseNodeData *... > data ){ return processNeighbors< LeftRadius , RightRadius , CreateNodes >( nodes , nodeCount , data , []( const FEMTreeNode * ){} ); }
2617 template< unsigned int Radius , bool CreateNodes , typename ProcessingNodeFunctor , typename ... DenseOrSparseNodeData > void processNeighbors( ProcessingNodeFunctor processNodes , std::tuple< DenseOrSparseNodeData *... > data ){ return processNeighbors< Radius , CreateNodes >( processNodes , data , []( const FEMTreeNode * ){} ); }
2618 template< unsigned int Radius , bool CreateNodes , typename ... DenseOrSparseNodeData > void processNeighbors( FEMTreeNode** nodes , size_t nodeCount , std::tuple< DenseOrSparseNodeData *... > data ){ return processNeighbors< Radius , CreateNodes >( nodes , nodeCount , data , []( const FEMTreeNode * ){} ) ; }
2619
2620 template< unsigned int LeftRadius , unsigned int RightRadius , typename IsProcessingNodeFunctor , typename ProcessingKernel > void processNeighboringLeaves( IsProcessingNodeFunctor isProcessingNode , ProcessingKernel kernel , bool processSubTree );
2621 template< unsigned int LeftRadius , unsigned int RightRadius , typename ProcessingKernel > void processNeighboringLeaves( FEMTreeNode** nodes , size_t nodeCount , ProcessingKernel kernel , bool processSubTree );
2622 template< unsigned int Radius , typename IsProcessingNodeFunctor , typename ProcessingKernel > void processNeighboringLeaves( IsProcessingNodeFunctor isProcessingNode , ProcessingKernel kernel , bool processSubTree ){ return processNeighboringLeaves< Radius , Radius >( isProcessingNode , kernel , processSubTree ); }
2623 template< unsigned int Radius , typename ProcessingKernel > void processNeighboringLeaves( FEMTreeNode** nodes , size_t nodeCount , ProcessingKernel kernel , bool processSubTree ){ return processNeighboringLeaves< Radius , Radius >( nodes , nodeCount , kernel , processSubTree ); }
2624
2625 template< unsigned int CoDim , unsigned int DensityDegree >
2626 typename FEMTree::template DensityEstimator< DensityDegree > *setDensityEstimator( const std::vector< PointSample >& samples , LocalDepth splatDepth , Real samplesPerNode );
2627 template< unsigned int CoDim , unsigned int DensityDegree >
2628 void updateDensityEstimator( typename FEMTree::template DensityEstimator< DensityDegree > &density , const std::vector< PointSample >& samples , LocalDepth minSplatDepth , LocalDepth maxSplatDepth );
2629 template< unsigned int CoDim , unsigned int DensityDegree >
2630 typename FEMTree::template DensityEstimator< DensityDegree > *setDensityEstimator( const std::vector< PointSample >& samples , LocalDepth splatDepth , std::function< int ( Point< Real , Dim > ) > pointDepthFunctor , Real samplesPerNode );
2631 template< unsigned int CoDim , unsigned int DensityDegree >
2632 void updateDensityEstimator( typename FEMTree::template DensityEstimator< DensityDegree > &density , const std::vector< PointSample >& samples , LocalDepth minSplatDepth , LocalDepth maxSplatDepth , std::function< int ( Point< Real , Dim > ) > pointDepthFunctor );
2633
2634 template< unsigned int ... DataSigs , unsigned int DensityDegree , class InData , class OutData >
2635 SparseNodeData< OutData , UIntPack< DataSigs ... > > setInterpolatedDataField( OutData zero , UIntPack< DataSigs ... > , const std::vector< PointSample >& samples , const std::vector< InData >& data , const DensityEstimator< DensityDegree >* density , LocalDepth minDepth , LocalDepth maxDepth , Real minDepthCutoff , ProjectiveData< Point< Real , 2 > , Real > &pointDepthAndWeight , std::function< bool ( InData , OutData & , Real & ) > ConversionAndBiasFunction );
2636 #if defined(_WIN32) || defined(_WIN64)
2637 template< unsigned int ... DataSigs , unsigned int DensityDegree , class InData , class OutData >
2638 SparseNodeData< OutData , UIntPack< DataSigs ... > > setInterpolatedDataField( OutData zero , UIntPack< DataSigs ... > , const std::vector< PointSample >& samples , const std::vector< InData >& data , const DensityEstimator< DensityDegree >* density , LocalDepth minDepth , LocalDepth maxDepth , Real minDepthCutoff , ProjectiveData< Point< Real , 2 > , Real > &pointDepthAndWeight , std::function< bool ( InData , OutData & ) > ConversionFunction , std::function< Real ( InData ) > BiasFunction = []( InData ){ return 0.f; } );
2639 #else // !_WIN32 && !_WIN64
2640 template< unsigned int ... DataSigs , unsigned int DensityDegree , class InData , class OutData >
2641 SparseNodeData< OutData , UIntPack< DataSigs ... > > setInterpolatedDataField( OutData zero , UIntPack< DataSigs ... > , const std::vector< PointSample >& samples , const std::vector< InData >& data , const DensityEstimator< DensityDegree >* density , LocalDepth minDepth , LocalDepth maxDepth , Real minDepthCutoff , ProjectiveData< Point< Real , 2 > , Real > &pointDepthAndWeight , std::function< bool ( InData , OutData & ) > ConversionFunction , std::function< Real ( InData ) > BiasFunction = []( InData ){ return (Real)0; } );
2642 #endif // _WIN32 || _WIN64
2643 template< unsigned int ... DataSigs , unsigned int DensityDegree , class InData , class OutData >
2644 SparseNodeData< OutData , UIntPack< DataSigs ... > > setInterpolatedDataField( OutData zero , UIntPack< DataSigs ... > , const std::vector< PointSample >& samples , const std::vector< InData >& data , const DensityEstimator< DensityDegree >* density , LocalDepth minDepth , LocalDepth maxDepth , std::function< int ( Point< Real , Dim > ) > pointDepthFunctor , Real minDepthCutoff , ProjectiveData< Point< Real , 2 > , Real > &pointDepthAndWeight , std::function< bool ( InData , OutData & , Real & ) > ConversionAndBiasFunction );
2645 #if defined(_WIN32) || defined(_WIN64)
2646 template< unsigned int ... DataSigs , unsigned int DensityDegree , class InData , class OutData >
2647 SparseNodeData< OutData , UIntPack< DataSigs ... > > setInterpolatedDataField( OutData zero , UIntPack< DataSigs ... > , const std::vector< PointSample >& samples , const std::vector< InData >& data , const DensityEstimator< DensityDegree >* density , LocalDepth minDepth , LocalDepth maxDepth , std::function< int ( Point< Real , Dim > ) > pointDepthFunctor , Real minDepthCutoff , ProjectiveData< Point< Real , 2 > , Real > &pointDepthAndWeight , std::function< bool ( InData , OutData & ) > ConversionFunction , std::function< Real ( InData ) > BiasFunction = []( InData ){ return 0.f; } );
2648 #else // !_WIN32 && !_WIN64
2649 template< unsigned int ... DataSigs , unsigned int DensityDegree , class InData , class OutData >
2650 SparseNodeData< OutData , UIntPack< DataSigs ... > > setInterpolatedDataField( OutData zero , UIntPack< DataSigs ... > , const std::vector< PointSample >& samples , const std::vector< InData >& data , const DensityEstimator< DensityDegree >* density , LocalDepth minDepth , LocalDepth maxDepth , std::function< int ( Point< Real , Dim > ) > pointDepthFunctor , Real minDepthCutoff , ProjectiveData< Point< Real , 2 > , Real > &pointDepthAndWeight , std::function< bool ( InData , OutData & ) > ConversionFunction , std::function< Real ( InData ) > BiasFunction = []( InData ){ return (Real)0; } );
2651 #endif // _WIN32 || _WIN64
2652
2653 template< unsigned int DataSig , bool CreateNodes , unsigned int DensityDegree , class Data >
2654 SparseNodeData< ProjectiveData< Data , Real > , IsotropicUIntPack< Dim , DataSig > > setExtrapolatedDataField( Data zero , const std::vector< PointSample >& samples , const std::vector< Data >& sampleData , const DensityEstimator< DensityDegree >* density , bool nearest=false );
2655 template< unsigned int DataSig , bool CreateNodes , unsigned int DensityDegree , class Data >
2656 void updateExtrapolatedDataField( Data zero , SparseNodeData< ProjectiveData< Data , Real > , IsotropicUIntPack< Dim , DataSig > > &dataField , const std::vector< PointSample >& samples , const std::vector< Data >& sampleData , const DensityEstimator< DensityDegree >* density , bool nearest=false );
2657 template< unsigned int DataSig , bool CreateNodes , unsigned int DensityDegree , class Data , class SampleFunctor /* = std::function< const PointSample & (size_t) >*/ , class SampleDataFunctor /* = std::function< const Data & (size_t) > */ >
2658 SparseNodeData< ProjectiveData< Data , Real > , IsotropicUIntPack< Dim , DataSig > > setExtrapolatedDataField( Data zero , size_t sampleNum , SampleFunctor sampleFunctor , SampleDataFunctor sampleDataFunctor , const DensityEstimator< DensityDegree >* density , bool nearest=false );
2659 template< unsigned int DataSig , bool CreateNodes , unsigned int DensityDegree , class Data , class SampleFunctor /* = std::function< const PointSample & (size_t) >*/ , class SampleDataFunctor /* = std::function< const Data & (size_t) > */ >
2660 void updateExtrapolatedDataField( Data zero , SparseNodeData< ProjectiveData< Data , Real > , IsotropicUIntPack< Dim , DataSig > > &dataField , size_t sampleNum , SampleFunctor sampleFunctor , SampleDataFunctor sampleDataFunctor , const DensityEstimator< DensityDegree >* density , bool nearest=false );
2661
2662 template< unsigned int MaxDegree , unsigned int SystemDegree , typename AddNodeFunctor , typename HasDataFunctor , typename IsDirichletLeafFunctor , typename ... InterpolationInfos , typename ... DenseOrSparseNodeData > std::vector< node_index_type > finalizeForMultigridWithDirichlet( LocalDepth baseDepth , const AddNodeFunctor addNodeFunctor , const HasDataFunctor hasDataFunctor , const IsDirichletLeafFunctor isDirichletLeafFunctor , std::tuple< InterpolationInfos *... > interpolationInfos , std::tuple< DenseOrSparseNodeData *... > data );
2663 template< unsigned int MaxDegree , unsigned int SystemDegree , typename AddNodeFunctor , typename HasDataFunctor , typename IsDirichletLeafFunctor , typename ... InterpolationInfos > std::vector< node_index_type > finalizeForMultigridWithDirichlet( LocalDepth baseDepth , const AddNodeFunctor addNodeFunctor , const HasDataFunctor hasDataFunctor , const IsDirichletLeafFunctor isDirichletLeafFunctor , std::tuple< InterpolationInfos *... > interpolationInfos ){ return finalizeForMultigridWithDirichlet< MaxDegree , SystemDegree , AddNodeFunctor , HasDataFunctor >( baseDepth , addNodeFunctor , hasDataFunctor , isDirichletLeafFunctor , interpolationInfos , std::make_tuple() ); }
2664
2665 template< unsigned int MaxDegree , unsigned int SystemDegree , typename AddNodeFunctor , typename HasDataFunctor , typename ... InterpolationInfos , typename ... DenseOrSparseNodeData > std::vector< node_index_type > finalizeForMultigrid( LocalDepth baseDepth , const AddNodeFunctor addNodeFunctor , const HasDataFunctor hasDataFunctor , std::tuple< InterpolationInfos *... > interpolationInfos , std::tuple< DenseOrSparseNodeData *... > data );
2666 template< unsigned int MaxDegree , unsigned int SystemDegree , typename AddNodeFunctor , typename HasDataFunctor , typename ... InterpolationInfos > std::vector< node_index_type > finalizeForMultigrid( LocalDepth baseDepth , const AddNodeFunctor addNodeFunctor , const HasDataFunctor hasDataFunctor , std::tuple< InterpolationInfos *... > interpolationInfos ){ return finalizeForMultigrid< MaxDegree , SystemDegree , AddNodeFunctor , HasDataFunctor >( baseDepth , addNodeFunctor , hasDataFunctor , interpolationInfos , std::make_tuple() ); }
2667
2668 protected:
2669 template< bool HasDirichlet , unsigned int MaxDegree , unsigned int SystemDegree , typename AddNodeFunctor , typename HasDataFunctor , typename IsDirichletLeafFunctor , typename ... InterpolationInfos , typename ... DenseOrSparseNodeData >
2670 std::vector< node_index_type > _finalizeForMultigrid( LocalDepth baseDepth , const AddNodeFunctor addNodeFunctor , const HasDataFunctor hasDataFunctor , const IsDirichletLeafFunctor isDirichletLeafFunctor , std::tuple< InterpolationInfos *... > interpolationInfos , std::tuple< DenseOrSparseNodeData *... > data );
2671 public:
2672 template< typename ... InterpolationInfos , typename ... DenseOrSparseNodeData > std::vector< node_index_type > setSortedTreeNodes( std::tuple< InterpolationInfos *... > interpolationInfos , std::tuple< DenseOrSparseNodeData *... > data );
2673 template< typename ... InterpolationInfos > std::vector< node_index_type > setSortedTreeNodes( std::tuple< InterpolationInfos *... > interpolationInfos ){ return setSortedTreeNodes( interpolationInfos , std::make_tuple() ); }
2674
2675 template< class ... DenseOrSparseNodeData > void resetIndices( std::tuple< DenseOrSparseNodeData *... > data );
2676
2677 template< typename PruneChildrenFunctor , typename ... InterpolationInfos , typename ... DenseOrSparseNodeData >
2678 void pruneChildren( const PruneChildrenFunctor pruneChildren , std::tuple< InterpolationInfos *... > interpolationInfos , std::tuple< DenseOrSparseNodeData *... > data );
2679
2680 template< unsigned int ... FEMSigs > DenseNodeData< Real , UIntPack< FEMSigs ... > > initDenseNodeData( UIntPack< FEMSigs ... > ) const;
2681 template< class Data , unsigned int ... FEMSigs > DenseNodeData< Data , UIntPack< FEMSigs ... > > initDenseNodeData( UIntPack< FEMSigs ... > ) const;
2682
2683 template< unsigned int Pad , unsigned int FEMSig , unsigned int ... FEMSigs , typename Data >
2684 void slice( const FEMTree< Dim+1 , Real > &tree , unsigned int d , const DenseNodeData< Data , UIntPack< FEMSigs ... , FEMSig > > &coefficients , DenseNodeData< Data , UIntPack< FEMSigs ... > > &sliceCoefficients , unsigned int sliceDepth , unsigned int sliceIndex ) const;
2685
2686 template< unsigned int ... FEMSigs , typename Data >
2687 void merge( const FEMTree< Dim , Real > &tree , const DenseNodeData< Data , UIntPack< FEMSigs ... > > &coefficients , DenseNodeData< Data , UIntPack< FEMSigs ... > > &mergedCoefficients ) const;
2688
2689 // Add multiple-dimensions -> one-dimension constraints
2690 template< typename T , unsigned int ... FEMDegrees , unsigned int ... FEMSigs , unsigned int ... CDegrees , unsigned int ... CSigs , unsigned int CDim >
2691 void addFEMConstraints( typename BaseFEMIntegrator::template Constraint< UIntPack< FEMDegrees ... > , UIntPack< CDegrees ... > , CDim >& F , const _SparseOrDenseNodeData< Point< T , CDim > , UIntPack< CSigs ... > >& coefficients , DenseNodeData< T , UIntPack< FEMSigs ... > >& constraints , LocalDepth maxDepth ) const
2692 {
2693 typedef SparseNodeData< Point< T , CDim > , UIntPack< CSigs ... > > SparseType;
2694 typedef DenseNodeData< Point< T , CDim > , UIntPack< CSigs ... > > DenseType;
2695 static_assert( sizeof...( FEMDegrees )==Dim && sizeof...( FEMSigs )==Dim && sizeof...( CDegrees )==Dim && sizeof...( CSigs )==Dim , "[ERROR] Dimensions don't match" );
2696 static_assert( ParameterPack::Comparison< UIntPack< FEMDegrees ... > , UIntPack< FEMSignature< FEMSigs >::Degree ... > >::Equal , "[ERROR] FEM signature and degrees don't match" );
2697 static_assert( ParameterPack::Comparison< UIntPack< CDegrees ... > , UIntPack< FEMSignature< CSigs >::Degree ... > >::Equal , "[ERROR] Constraint signature and degrees don't match" );
2698 if ( typeid(coefficients)==typeid(SparseType) ) return _addFEMConstraints< T >( UIntPack< FEMSigs ... >() , UIntPack< CSigs ... >() , F , static_cast< const SparseType& >( coefficients ) , constraints() , maxDepth );
2699 else if( typeid(coefficients)==typeid( DenseType) ) return _addFEMConstraints< T >( UIntPack< FEMSigs ... >() , UIntPack< CSigs ... >() , F , static_cast< const DenseType& >( coefficients ) , constraints() , maxDepth );
2700 else return _addFEMConstraints< T >( UIntPack< FEMSigs ... >() , UIntPack< CSigs ... >() , F , coefficients , constraints() , maxDepth );
2701 }
2702 // Add one-dimensions -> one-dimension constraints (with distinct signatures)
2703 template< typename T , unsigned int ... FEMDegrees , unsigned int ... FEMSigs , unsigned int ... CDegrees , unsigned int ... CSigs >
2704 void addFEMConstraints( typename BaseFEMIntegrator::template Constraint< UIntPack< FEMDegrees ... > , UIntPack< CDegrees ... > , 1 >& F , const _SparseOrDenseNodeData< T , UIntPack< CSigs ... > >& coefficients , DenseNodeData< T , UIntPack< FEMSigs ... > >& constraints , LocalDepth maxDepth ) const
2705 {
2706 typedef SparseNodeData< T , UIntPack< CSigs ... > > SparseType;
2707 typedef DenseNodeData< T , UIntPack< CSigs ... > > DenseType;
2708 static_assert( sizeof...( FEMDegrees )==Dim && sizeof...( FEMSigs )==Dim && sizeof...( CDegrees )==Dim && sizeof...( CSigs )==Dim , "[ERROR] Dimensions don't match" );
2709 static_assert( ParameterPack::Comparison< UIntPack< FEMDegrees ... > , UIntPack< FEMSignature< FEMSigs >::Degree ... > >::Equal , "[ERROR] FEM signature and degrees don't match" );
2710 static_assert( ParameterPack::Comparison< UIntPack< CDegrees ... > , UIntPack< FEMSignature< CSigs >::Degree ... > >::Equal , "[ERROR] Constaint signature and degrees don't match" );
2711 if ( typeid(coefficients)==typeid(SparseType) ) return _addFEMConstraints< T >( UIntPack< FEMSigs ... >() , UIntPack< CSigs ... >() , F , static_cast< const SparseType& >( coefficients ) , constraints() , maxDepth );
2712 else if( typeid(coefficients)==typeid( DenseType) ) return _addFEMConstraints< T >( UIntPack< FEMSigs ... >() , UIntPack< CSigs ... >() , F , static_cast< const DenseType& >( coefficients ) , constraints() , maxDepth );
2713 else return _addFEMConstraints< T >( UIntPack< FEMSigs ... >() , UIntPack< CSigs ... >() , F , coefficients , constraints() , maxDepth );
2714 }
2715 // Add one-dimensions -> one-dimension constraints (with the same signatures)
2716 template< typename T , unsigned int ... FEMDegrees , unsigned int ... FEMSigs >
2717 void addFEMConstraints( typename BaseFEMIntegrator::template System< UIntPack< FEMDegrees ... > >& F , const _SparseOrDenseNodeData< T , UIntPack< FEMSigs ... > >& coefficients , DenseNodeData< T , UIntPack< FEMSigs ... > >& constraints , LocalDepth maxDepth ) const
2718 {
2719 typedef SparseNodeData< T , UIntPack< FEMSigs ... > > SparseType;
2720 typedef DenseNodeData< T , UIntPack< FEMSigs ... > > DenseType;
2721 static_assert( sizeof...( FEMDegrees )==Dim && sizeof...( FEMSigs )==Dim , "[ERROR] Dimensions don't match" );
2722 static_assert( ParameterPack::Comparison< UIntPack< FEMDegrees ... > , UIntPack< FEMSignature< FEMSigs >::Degree ... > >::Equal , "[ERROR] FEM signatures and degrees don't match" );
2723 typename BaseFEMIntegrator::template SystemConstraint< UIntPack< FEMDegrees ... > > _F( F );
2724 if ( typeid(coefficients)==typeid(SparseType) ) return _addFEMConstraints< T >( UIntPack< FEMSigs ... >() , UIntPack< FEMSigs ... >() , _F , static_cast< const SparseType& >( coefficients ) , constraints() , maxDepth );
2725 else if( typeid(coefficients)==typeid( DenseType) ) return _addFEMConstraints< T >( UIntPack< FEMSigs ... >() , UIntPack< FEMSigs ... >() , _F , static_cast< const DenseType& >( coefficients ) , constraints() , maxDepth );
2726 else return _addFEMConstraints< T >( UIntPack< FEMSigs ... >() , UIntPack< FEMSigs ... >() , _F , coefficients , constraints() , maxDepth );
2727 }
2728
2729 protected:
2730 template< unsigned int MaxDegree > void _supportApproximateProlongation( void );
2731 template< unsigned int SystemDegree > void _markInexactInterpolationElements( void);
2732 template< unsigned int SystemDegree > void _addAndMarkExactInterpolationElements( void );
2733 template< unsigned int SystemDegree > void _markNonBaseDirichletElements( void );
2734 template< unsigned int SystemDegree > void _markBaseDirichletElements( void );
2735
2736 template< unsigned int Idx , typename T , unsigned int ... FEMSigs , typename ... InterpolationInfos >
2737 typename std::enable_if< ( Idx==sizeof...(InterpolationInfos) ) >::type _addInterpolationConstraints( DenseNodeData< T , UIntPack< FEMSigs ... > >& constraints , LocalDepth maxDepth , std::tuple< InterpolationInfos *... > interpolationInfos ) const {}
2738 template< unsigned int Idx , typename T , unsigned int ... FEMSigs , typename ... InterpolationInfos >
2739 typename std::enable_if< ( Idx <sizeof...(InterpolationInfos) ) >::type _addInterpolationConstraints( DenseNodeData< T , UIntPack< FEMSigs ... > >& constraints , LocalDepth maxDepth , std::tuple< InterpolationInfos *... > interpolationInfos ) const
2740 {
2741 _addInterpolationConstraints( constraints , maxDepth , std::get< Idx >( interpolationInfos ) );
2742 _addInterpolationConstraints< Idx+1 >( constraints , maxDepth , interpolationInfos );
2743 }
2744 template< typename T , unsigned int ... FEMSigs , unsigned int PointD >
2745 void _addInterpolationConstraints( DenseNodeData< T , UIntPack< FEMSigs ... > >& constraints , LocalDepth maxDepth , const InterpolationInfo< T , PointD > *interpolationInfo ) const;
2746 public:
2747 template< typename T , unsigned int ... FEMSigs , typename ... InterpolationInfos >
2748 void addInterpolationConstraints( DenseNodeData< T , UIntPack< FEMSigs ... > >& constraints , LocalDepth maxDepth , std::tuple< InterpolationInfos *... > interpolationInfos ) const { return _addInterpolationConstraints< 0 >( constraints , maxDepth , interpolationInfos ); }
2749
2750 // Real
2751 template< unsigned int ... FEMDegrees1 , unsigned int ... FEMSigs1 , unsigned int ... FEMDegrees2 , unsigned int ... FEMSigs2 >
2752 double dot( typename BaseFEMIntegrator::Constraint< UIntPack< FEMDegrees1 ... > , UIntPack< FEMDegrees2 ... > , 1 >& F , const _SparseOrDenseNodeData< Real , UIntPack< FEMSigs1 ... > >& coefficients1 , const _SparseOrDenseNodeData< Real , UIntPack< FEMSigs2 ... > >& coefficients2 ) const
2753 {
2754 typedef SparseNodeData< Real , UIntPack< FEMSigs1 ... > > SparseType1;
2755 typedef DenseNodeData< Real , UIntPack< FEMSigs1 ... > > DenseType1;
2756 typedef SparseNodeData< Real , UIntPack< FEMSigs2 ... > > SparseType2;
2757 typedef DenseNodeData< Real , UIntPack< FEMSigs2 ... > > DenseType2;
2758 static_assert( sizeof...( FEMDegrees1 )==Dim && sizeof...( FEMSigs1 )==Dim && sizeof...( FEMDegrees2 )==Dim && sizeof...( FEMSigs2 )==Dim , "[ERROR] Dimensions don't match" );
2759 static_assert( ParameterPack::Comparison< UIntPack< FEMDegrees1 ... > , UIntPack< FEMSignature< FEMSigs1 >::Degree ... > >::Equal , "[ERROR] FEM signature and degrees don't match" );
2760 static_assert( ParameterPack::Comparison< UIntPack< FEMDegrees2 ... > , UIntPack< FEMSignature< FEMSigs2 >::Degree ... > >::Equal , "[ERROR] FEM signature and degrees don't match" );
2761 if ( typeid(coefficients1)==typeid(SparseType1) && typeid(coefficients2)==typeid(SparseType2) ) return _dot< Real >( UIntPack< FEMSigs1 ... >() , UIntPack< FEMSigs2 ... >() , F , static_cast< const SparseType1& >( coefficients1 ) , static_cast< const SparseType2& >( coefficients2 ) , []( Real v , Real w ){ return v*w; } );
2762 else if( typeid(coefficients1)==typeid(SparseType1) && typeid(coefficients2)==typeid( DenseType2) ) return _dot< Real >( UIntPack< FEMSigs1 ... >() , UIntPack< FEMSigs2 ... >() , F , static_cast< const SparseType1& >( coefficients1 ) , static_cast< const DenseType2& >( coefficients2 ) , []( Real v , Real w ){ return v*w; } );
2763 else if( typeid(coefficients1)==typeid( DenseType1) && typeid(coefficients2)==typeid( DenseType2) ) return _dot< Real >( UIntPack< FEMSigs1 ... >() , UIntPack< FEMSigs2 ... >() , F , static_cast< const DenseType1& >( coefficients1 ) , static_cast< const DenseType2& >( coefficients2 ) , []( Real v , Real w ){ return v*w; } );
2764 else if( typeid(coefficients1)==typeid( DenseType1) && typeid(coefficients2)==typeid(SparseType2) ) return _dot< Real >( UIntPack< FEMSigs1 ... >() , UIntPack< FEMSigs2 ... >() , F , static_cast< const DenseType1& >( coefficients1 ) , static_cast< const SparseType2& >( coefficients2 ) , []( Real v , Real w ){ return v*w; } );
2765 else return _dot< Real >( UIntPack< FEMSigs1 ... >() , UIntPack< FEMSigs2 ... >() , F , coefficients1 , coefficients2 , []( Real v , Real w ){ return v*w; } );
2766 }
2767 template< unsigned int ... FEMDegrees , unsigned int ... FEMSigs >
2768 double dot( typename BaseFEMIntegrator::System< UIntPack< FEMDegrees ... > >& F , const _SparseOrDenseNodeData< Real , UIntPack< FEMSigs ... > >& coefficients1 , const _SparseOrDenseNodeData< Real , UIntPack< FEMSigs ... > >& coefficients2 ) const
2769 {
2770 typedef SparseNodeData< Real , UIntPack< FEMSigs ... > > SparseType;
2771 typedef DenseNodeData< Real , UIntPack< FEMSigs ... > > DenseType;
2772 static_assert( sizeof...( FEMDegrees )==Dim && sizeof...( FEMSigs )==Dim , "[ERROR] Dimensions don't match" );
2773 static_assert( ParameterPack::Comparison< UIntPack< FEMDegrees ... > , UIntPack< FEMSignature< FEMSigs >::Degree ... > >::Equal , "[ERROR] FEM signatures and degrees don't match" );
2774 typename BaseFEMIntegrator::template SystemConstraint< UIntPack< FEMDegrees ... > > _F( F );
2775 if ( typeid(coefficients1)==typeid(SparseType) && typeid(coefficients2)==typeid(SparseType) ) return _dot< Real >( UIntPack< FEMSigs ... >() , UIntPack< FEMSigs ... >() , _F , static_cast< const SparseType& >( coefficients1 ) , static_cast< const SparseType& >( coefficients2 ) , []( Real v , Real w ){ return v*w; } );
2776 else if( typeid(coefficients1)==typeid(SparseType) && typeid(coefficients2)==typeid( DenseType) ) return _dot< Real >( UIntPack< FEMSigs ... >() , UIntPack< FEMSigs ... >() , _F , static_cast< const SparseType& >( coefficients1 ) , static_cast< const DenseType& >( coefficients2 ) , []( Real v , Real w ){ return v*w; } );
2777 else if( typeid(coefficients1)==typeid( DenseType) && typeid(coefficients2)==typeid( DenseType) ) return _dot< Real >( UIntPack< FEMSigs ... >() , UIntPack< FEMSigs ... >() , _F , static_cast< const DenseType& >( coefficients1 ) , static_cast< const DenseType& >( coefficients2 ) , []( Real v , Real w ){ return v*w; } );
2778 else if( typeid(coefficients1)==typeid( DenseType) && typeid(coefficients2)==typeid(SparseType) ) return _dot< Real >( UIntPack< FEMSigs ... >() , UIntPack< FEMSigs ... >() , _F , static_cast< const DenseType& >( coefficients1 ) , static_cast< const SparseType& >( coefficients2 ) , []( Real v , Real w ){ return v*w; } );
2779 else return _dot< Real >( UIntPack< FEMSigs ... >() , UIntPack< FEMSigs ... >() , _F , coefficients1 , coefficients2 , []( Real v , Real w ){ return v*w; } );
2780 }
2781 template< unsigned int ... FEMDegrees , unsigned int ... FEMSigs >
2782 double squareNorm( typename BaseFEMIntegrator::template System< UIntPack< FEMDegrees ... > >& F , const _SparseOrDenseNodeData< Real , UIntPack< FEMSigs ... > >& coefficients ) const
2783 {
2784 typedef SparseNodeData< Real , UIntPack< FEMSigs ... > > SparseType;
2785 typedef DenseNodeData< Real , UIntPack< FEMSigs ... > > DenseType;
2786 typename BaseFEMIntegrator::template SystemConstraint< UIntPack< FEMDegrees ... > > _F( F );
2787 if ( typeid(coefficients)==typeid(SparseType) ) return _dot< Real >( UIntPack< FEMSigs ... >() , UIntPack< FEMSigs ... >() , _F , static_cast< const SparseType& >( coefficients ) , static_cast< const SparseType& >( coefficients ) , []( Real v , Real w ){ return v*w; } );
2788 else if( typeid(coefficients)==typeid( DenseType) ) return _dot< Real >( UIntPack< FEMSigs ... >() , UIntPack< FEMSigs ... >() , _F , static_cast< const DenseType& >( coefficients ) , static_cast< const DenseType& >( coefficients ) , []( Real v , Real w ){ return v*w; } );
2789 else return _dot< Real >( UIntPack< FEMSigs ... >() , UIntPack< FEMSigs ... >() , _F , coefficients , coefficients , []( Real v , Real w ){ return v*w; } );
2790 }
2791
2792 template< unsigned int ... FEMSigs1 , unsigned int ... FEMSigs2 , typename ... InterpolationInfos >
2793 double interpolationDot( const DenseNodeData< Real , UIntPack< FEMSigs1 ... > >& coefficients1 , const DenseNodeData< Real , UIntPack< FEMSigs2 ... > >& coefficients2 , std::tuple< InterpolationInfos *... > interpolationInfos ) const
2794 {
2795 static_assert( sizeof...( FEMSigs1 )==Dim && sizeof...( FEMSigs2 )==Dim , "[ERROR] Dimensions don't match" );
2796 return _interpolationDot< 0 >( UIntPack< FEMSigs1 ... >() , UIntPack< FEMSigs2 ... >() , coefficients1 , coefficients2 , []( Real v , Real w ){ return v*w; } , interpolationInfos );
2797 }
2798 template< unsigned int ... FEMSigs , typename ... InterpolationInfos >
2799 double interpolationSquareNorm( const DenseNodeData< Real , UIntPack< FEMSigs ... > >& coefficients , std::tuple< InterpolationInfos *... > interpolationInfos ) const
2800 {
2801 static_assert( sizeof...( FEMSigs )==Dim , "[ERROR] Dimensions don't match" );
2802 return _interpolationDot< 0 , Real >( UIntPack< FEMSigs ... >() , UIntPack< FEMSigs ... >() , coefficients , coefficients , []( Real v , Real w ){ return v*w; } , interpolationInfos );
2803 }
2804
2805 // Generic
2806 template< typename T , typename TDotT , unsigned int ... FEMDegrees1 , unsigned int ... FEMSigs1 , unsigned int ... FEMDegrees2 , unsigned int ... FEMSigs2 >
2807 double dot( TDotT Dot , typename BaseFEMIntegrator::Constraint< UIntPack< FEMDegrees1 ... > , UIntPack< FEMDegrees2 ... > , 1 >& F , const _SparseOrDenseNodeData< T , UIntPack< FEMSigs1 ... > >& coefficients1 , const _SparseOrDenseNodeData< T , UIntPack< FEMSigs2 ... > >& coefficients2 ) const
2808 {
2809 typedef SparseNodeData< T , UIntPack< FEMSigs1 ... > > SparseType1;
2810 typedef DenseNodeData< T , UIntPack< FEMSigs1 ... > > DenseType1;
2811 typedef SparseNodeData< T , UIntPack< FEMSigs2 ... > > SparseType2;
2812 typedef DenseNodeData< T , UIntPack< FEMSigs2 ... > > DenseType2;
2813 static_assert( sizeof...( FEMDegrees1 )==Dim && sizeof...( FEMSigs1 )==Dim && sizeof...( FEMDegrees2 )==Dim && sizeof...( FEMSigs2 )==Dim , "[ERROR] Dimensions don't match" );
2814 static_assert( ParameterPack::Comparison< UIntPack< FEMDegrees1 ... > , UIntPack< FEMSignature< FEMSigs1 >::Degree ... > >::Equal , "[ERROR] FEM signature and degrees don't match" );
2815 static_assert( ParameterPack::Comparison< UIntPack< FEMDegrees2 ... > , UIntPack< FEMSignature< FEMSigs2 >::Degree ... > >::Equal , "[ERROR] FEM signature and degrees don't match" );
2816 if ( typeid(coefficients1)==typeid(SparseType1) && typeid(coefficients2)==typeid(SparseType2) ) return _dot< T >( UIntPack< FEMSigs1 ... >() , UIntPack< FEMSigs2 ... >() , F , static_cast< const SparseType1& >( coefficients1 ) , static_cast< const SparseType2& >( coefficients2 ) , Dot );
2817 else if( typeid(coefficients1)==typeid(SparseType1) && typeid(coefficients2)==typeid( DenseType2) ) return _dot< T >( UIntPack< FEMSigs1 ... >() , UIntPack< FEMSigs2 ... >() , F , static_cast< const SparseType1& >( coefficients1 ) , static_cast< const DenseType2& >( coefficients2 ) , Dot );
2818 else if( typeid(coefficients1)==typeid( DenseType1) && typeid(coefficients2)==typeid( DenseType2) ) return _dot< T >( UIntPack< FEMSigs1 ... >() , UIntPack< FEMSigs2 ... >() , F , static_cast< const DenseType1& >( coefficients1 ) , static_cast< const DenseType2& >( coefficients2 ) , Dot );
2819 else if( typeid(coefficients1)==typeid( DenseType1) && typeid(coefficients2)==typeid(SparseType2) ) return _dot< T >( UIntPack< FEMSigs1 ... >() , UIntPack< FEMSigs2 ... >() , F , static_cast< const DenseType1& >( coefficients1 ) , static_cast< const SparseType2& >( coefficients2 ) , Dot );
2820 else return _dot< T >( UIntPack< FEMSigs1 ... >() , UIntPack< FEMSigs2 ... >() , F , coefficients1 , coefficients2 , Dot );
2821 }
2822 template< typename T , typename TDotT , unsigned int ... FEMDegrees , unsigned int ... FEMSigs >
2823 double dot( TDotT Dot , typename BaseFEMIntegrator::System< UIntPack< FEMDegrees ... > >& F , const _SparseOrDenseNodeData< T , UIntPack< FEMSigs ... > >& coefficients1 , const _SparseOrDenseNodeData< T , UIntPack< FEMSigs ... > >& coefficients2 ) const
2824 {
2825 typedef SparseNodeData< T , UIntPack< FEMSigs ... > > SparseType;
2826 typedef DenseNodeData< T , UIntPack< FEMSigs ... > > DenseType;
2827 static_assert( sizeof...( FEMDegrees )==Dim && sizeof...( FEMSigs )==Dim , "[ERROR] Dimensions don't match" );
2828 static_assert( ParameterPack::Comparison< UIntPack< FEMDegrees ... > , UIntPack< FEMSignature< FEMSigs >::Degree ... > >::Equal , "[ERROR] FEM signatures and degrees don't match" );
2829 typename BaseFEMIntegrator::template SystemConstraint< UIntPack< FEMDegrees ... > > _F( F );
2830 if ( typeid(coefficients1)==typeid(SparseType) && typeid(coefficients2)==typeid(SparseType) ) return _dot< T >( UIntPack< FEMSigs ... >() , UIntPack< FEMSigs ... >() , _F , static_cast< const SparseType& >( coefficients1 ) , static_cast< const SparseType& >( coefficients2 ) , Dot );
2831 else if( typeid(coefficients1)==typeid(SparseType) && typeid(coefficients2)==typeid( DenseType) ) return _dot< T >( UIntPack< FEMSigs ... >() , UIntPack< FEMSigs ... >() , _F , static_cast< const SparseType& >( coefficients1 ) , static_cast< const DenseType& >( coefficients2 ) , Dot );
2832 else if( typeid(coefficients1)==typeid( DenseType) && typeid(coefficients2)==typeid( DenseType) ) return _dot< T >( UIntPack< FEMSigs ... >() , UIntPack< FEMSigs ... >() , _F , static_cast< const DenseType& >( coefficients1 ) , static_cast< const DenseType& >( coefficients2 ) , Dot );
2833 else if( typeid(coefficients1)==typeid( DenseType) && typeid(coefficients2)==typeid(SparseType) ) return _dot< T >( UIntPack< FEMSigs ... >() , UIntPack< FEMSigs ... >() , _F , static_cast< const DenseType& >( coefficients1 ) , static_cast< const SparseType& >( coefficients2 ) , Dot );
2834 else return _dot< T >( UIntPack< FEMSigs ... >() , UIntPack< FEMSigs ... >() , _F , coefficients1 , coefficients2 , Dot );
2835 }
2836 template< typename T , typename TDotT , unsigned int ... FEMDegrees , unsigned int ... FEMSigs >
2837 double squareNorm( TDotT Dot , typename BaseFEMIntegrator::template System< UIntPack< FEMDegrees ... > >& F , const _SparseOrDenseNodeData< T , UIntPack< FEMSigs ... > >& coefficients ) const
2838 {
2839 typedef SparseNodeData< T , UIntPack< FEMSigs ... > > SparseType;
2840 typedef DenseNodeData< T , UIntPack< FEMSigs ... > > DenseType;
2841 typename BaseFEMIntegrator::template SystemConstraint< UIntPack< FEMDegrees ... > > _F( F );
2842 if ( typeid(coefficients)==typeid(SparseType) ) return _dot< T >( UIntPack< FEMSigs ... >() , UIntPack< FEMSigs ... >() , _F , static_cast< const SparseType& >( coefficients ) , static_cast< const SparseType& >( coefficients ) , Dot );
2843 else if( typeid(coefficients)==typeid( DenseType) ) return _dot< T >( UIntPack< FEMSigs ... >() , UIntPack< FEMSigs ... >() , _F , static_cast< const DenseType& >( coefficients ) , static_cast< const DenseType& >( coefficients ) , Dot );
2844 else return _dot< T >( UIntPack< FEMSigs ... >() , UIntPack< FEMSigs ... >() , _F , coefficients , coefficients , Dot );
2845 }
2846
2847 template< typename T , typename TDotT , unsigned int ... FEMSigs1 , unsigned int ... FEMSigs2 , typename ... InterpolationInfos >
2848 double interpolationDot( TDotT Dot , const DenseNodeData< T , UIntPack< FEMSigs1 ... > >& coefficients1 , const DenseNodeData< T , UIntPack< FEMSigs2 ... > >& coefficients2 , std::tuple< InterpolationInfos *... > interpolationInfos ) const
2849 {
2850 static_assert( sizeof...( FEMSigs1 )==Dim && sizeof...( FEMSigs2 )==Dim , "[ERROR] Dimensions don't match" );
2851 return _interpolationDot< T >( UIntPack< FEMSigs1 ... >() , UIntPack< FEMSigs2 ... >() , coefficients1 , coefficients2 , Dot , interpolationInfos );
2852 }
2853 template< typename T , typename TDotT , unsigned int ... FEMSigs , typename ... InterpolationInfos >
2854 double interpolationSquareNorm( TDotT Dot , const DenseNodeData< T , UIntPack< FEMSigs ... > >& coefficients , std::tuple< InterpolationInfos *... > interpolationInfos ) const
2855 {
2856 static_assert( sizeof...( FEMSigs )==Dim , "[ERROR] Dimensions don't match" );
2857 return _interpolationDot< T >( UIntPack< FEMSigs ... >() , UIntPack< FEMSigs ... >() , coefficients , coefficients , Dot , interpolationInfos );
2858 }
2859
2860 template< unsigned int ... FEMSigs , typename ... InterpolationInfos >
2861 SparseMatrix< Real , matrix_index_type > systemMatrix( UIntPack< FEMSigs ... > , typename BaseFEMIntegrator::System< UIntPack< FEMSignature< FEMSigs >::Degree ... > >& F , LocalDepth depth , std::tuple< InterpolationInfos *... > interpolationInfos ) const;
2862 template< unsigned int ... FEMSigs , typename ... InterpolationInfos >
2863 SparseMatrix< Real , matrix_index_type > prolongedSystemMatrix( UIntPack< FEMSigs ... > , typename BaseFEMIntegrator::System< UIntPack< FEMSignature< FEMSigs >::Degree ... > >& F , LocalDepth highDepth , std::tuple< InterpolationInfos *... > interpolationInfos ) const;
2864 template< unsigned int ... FEMSigs >
2865 SparseMatrix< Real , matrix_index_type > downSampleMatrix( UIntPack< FEMSigs ... > , LocalDepth highDepth ) const;
2866 template< unsigned int ... FEMSigs >
2867 SparseMatrix< Real , matrix_index_type > upSampleMatrix( UIntPack< FEMSigs ... > , LocalDepth highDepth ) const;
2868 template< unsigned int ... FEMSigs , typename ... InterpolationInfos >
2869 SparseMatrix< Real , matrix_index_type > fullSystemMatrix( UIntPack< FEMSigs ... > , typename BaseFEMIntegrator::System< UIntPack< FEMSignature< FEMSigs >::Degree ... > >& F , LocalDepth depth , std::tuple< InterpolationInfos *... > interpolationInfos ) const;
2870
2871 template< typename T , unsigned int ... FEMSigs >
2872 void pushToBaseDepth( DenseNodeData< T , UIntPack< FEMSigs ... > >& coefficients ) const;
2873
2874 struct SolverInfo
2875 {
2876 protected:
2877 struct _IterFunction
2878 {
2879 _IterFunction( int i ) : _i0(i) , _type(0) {}
2880 _IterFunction( std::function< int ( int ) > iFunction ) : _i1(iFunction) , _type(1) {}
2881 _IterFunction( std::function< int ( bool , int ) > iFunction ) : _i2(iFunction) , _type(2) {}
2882 _IterFunction( std::function< int ( int , bool , int ) > iFunction ) : _i3(iFunction) , _type(3) {}
2883 _IterFunction& operator = ( int i ){ *this = _IterFunction(i) ; return *this; }
2884 _IterFunction& operator = ( std::function< int ( int ) > iFunction ){ *this = _IterFunction(iFunction) ; return *this; }
2885 _IterFunction& operator = ( std::function< int ( bool , int ) > iFunction ){ *this = _IterFunction(iFunction) ; return *this; }
2886 _IterFunction& operator = ( std::function< int ( int , bool , int ) > iFunction ){ *this = _IterFunction(iFunction) ; return *this; }
2887
2888 int operator()( int vCycle , bool restriction , int depth ) const
2889 {
2890 switch( _type )
2891 {
2892 case 0: return _i0;
2893 case 1: return _i1( depth );
2894 case 2: return _i2( restriction , depth );
2895 case 3: return _i3( vCycle , restriction , depth );
2896 default: return 0;
2897 }
2898 }
2899 protected:
2900 int _i0;
2901 std::function< int ( int ) > _i1;
2902 std::function< int ( bool , int ) > _i2;
2903 std::function< int ( int i3 , bool , int ) > _i3;
2904 int _type;
2905 };
2906 public:
2907 // How to solve
2908 bool wCycle;
2909 LocalDepth cgDepth;
2910 bool cascadic;
2911 unsigned int sliceBlockSize;
2912 bool useSupportWeights , useProlongationSupportWeights;
2913 std::function< Real ( Real , Real ) > sorRestrictionFunction;
2914 std::function< Real ( Real , Real ) > sorProlongationFunction;
2915 _IterFunction iters;
2916 int vCycles;
2917 double cgAccuracy;
2918 bool clearSolution;
2919 int baseVCycles;
2920 // What to output
2921 bool verbose , showResidual;
2922 int showGlobalResidual;
2923
2924 SolverInfo( void ) : cgDepth(0) , wCycle(false) , cascadic(true) , iters(1) , vCycles(1) , cgAccuracy(0.) , verbose(false) , showResidual(false) , showGlobalResidual(SHOW_GLOBAL_RESIDUAL_NONE) , sliceBlockSize(1) , sorRestrictionFunction( []( Real , Real ){ return (Real)1; } ) , sorProlongationFunction( []( Real , Real ){ return (Real)1; } ) , useSupportWeights( false ) , useProlongationSupportWeights( false ) , baseVCycles(1) , clearSolution(true) { }
2925 };
2926 // Solve the linear system
2927 // There are several depths playing into the solver:
2928 // 1. maxDepth: The maximum depth of the tree
2929 // 2. solveDepth: The depth up to which we can solve (solveDepth<=maxDepth)
2930 // 3. fullDepth: The depth up to which the octree is completely refined (fullDepth<=maxDepth)
2931 // 4. baseDepth: The depth up to which the system is defined through the regular prolongation operators (baseDepth<=fullDepth)
2932 template< unsigned int ... FEMSigs , typename T , typename TDotT , typename ... InterpolationInfos >
2933 void solveSystem( UIntPack< FEMSigs ... > , typename BaseFEMIntegrator::template System< UIntPack< FEMSignature< FEMSigs >::Degree ... > >& F , const DenseNodeData< T , UIntPack< FEMSigs ... > >& constraints , DenseNodeData< T , UIntPack< FEMSigs ... > >& solution , TDotT Dot , LocalDepth minSolveDepth , LocalDepth maxSolveDepth , const SolverInfo& solverInfo , std::tuple< InterpolationInfos *... > interpolationInfos=std::make_tuple() ) const;
2934 template< unsigned int ... FEMSigs , typename T , typename TDotT , typename ... InterpolationInfos >
2935 DenseNodeData< T , UIntPack< FEMSigs ... > > solveSystem( UIntPack< FEMSigs ... > , typename BaseFEMIntegrator::template System< UIntPack< FEMSignature< FEMSigs >::Degree ... > >& F , const DenseNodeData< T , UIntPack< FEMSigs ... > >& constraints , TDotT Dot , LocalDepth minSolveDepth , LocalDepth maxSolveDepth , const SolverInfo& solverInfo , std::tuple< InterpolationInfos *... > interpolationInfos=std::make_tuple() ) const;
2936 template< unsigned int ... FEMSigs , typename ... InterpolationInfos >
2937 void solveSystem( UIntPack< FEMSigs ... > , typename BaseFEMIntegrator::template System< UIntPack< FEMSignature< FEMSigs >::Degree ... > >& F , const DenseNodeData< Real , UIntPack< FEMSigs ... > >& constraints , DenseNodeData< Real , UIntPack< FEMSigs ... > >& solution , LocalDepth minSolveDepth , LocalDepth maxSolveDepth , const SolverInfo& solverInfo , std::tuple< InterpolationInfos *... > interpolationInfos=std::make_tuple() ) const
2938 {
2939 return solveSystem< FEMSigs ... , Real >( UIntPack< FEMSigs ... >() , F , constraints , solution , []( Real v , Real w ){ return v*w; } , minSolveDepth , maxSolveDepth , solverInfo , interpolationInfos );
2940 }
2941 template< unsigned int ... FEMSigs , typename ... InterpolationInfos >
2942 DenseNodeData< Real , UIntPack< FEMSigs ... > > solveSystem( UIntPack< FEMSigs ... > , typename BaseFEMIntegrator::template System< UIntPack< FEMSignature< FEMSigs >::Degree ... > >& F , const DenseNodeData< Real , UIntPack< FEMSigs ... > >& constraints , LocalDepth minSolveDepth , LocalDepth maxSolveDepth , const SolverInfo& solverInfo , std::tuple< InterpolationInfos *... > interpolationInfos=std::make_tuple() ) const
2943 {
2944 return solveSystem( UIntPack< FEMSigs ... >() , F , constraints , []( Real v , Real w ){ return v*w; } , minSolveDepth , maxSolveDepth , solverInfo , interpolationInfos );
2945 }
2946
2947 FEMTreeNode& spaceRoot( void ){ return *_spaceRoot; }
2948 const FEMTreeNode &spaceRoot( void ) const { return *_spaceRoot; }
2949 const FEMTreeNode& tree( void ) const { return _tree; }
2950 _NodeInitializer &initializer( void ){ return _nodeInitializer; }
2951 size_t leaves( void ) const { return _tree.leaves(); }
2952 size_t allNodes ( void ) const { size_t count = 0 ; _tree.processNodes( [&]( const FEMTreeNode * ){ count++; } ) ; return count; }
2953 size_t activeNodes ( void ) const { size_t count = 0 ; _tree.processNodes( [&]( const FEMTreeNode *n ){ if( IsActiveNode< Dim >( n ) ) count++; } ) ; return count; }
2954 size_t ghostNodes ( void ) const { size_t count = 0 ; _tree.processNodes( [&]( const FEMTreeNode *n ){ if( !IsActiveNode< Dim >( n ) ) count++; } ) ; return count; }
2955 size_t dirichletNodes ( void ) const { size_t count = 0 ; _tree.processNodes( [&]( const FEMTreeNode *n ){ if( n->nodeData.getDirichletNodeFlag() ) count++; } ) ; return count; }
2956 size_t dirichletElements ( void ) const { size_t count = 0 ; _tree.processNodes( [&]( const FEMTreeNode *n ){ if( n->nodeData.getDirichletElementFlag() ) count++; } ) ; return count; }
2957 inline size_t validSpaceNodes( void ) const { size_t count = 0 ; _tree.processNodes( [&]( const FEMTreeNode *n ){ if( isValidSpaceNode( n ) ) count++; } ) ; return count; }
2958 inline size_t validSpaceNodes( LocalDepth d ) const { size_t count = 0 ; _tree.process( [&]( const FEMTreeNode *n ){ if( _localDepth(n)==d && isValidSpaceNode( n ) ) count++; } ) ; return count; }
2959 template< unsigned int ... FEMSigs > size_t validFEMNodes ( UIntPack< FEMSigs ... > ) const { size_t count = 0 ; _tree.processNodes( [&]( const FEMTreeNode *n ){ if( isValidFEMNode( UIntPack< FEMSigs ... >() , n ) ) count++; } ) ; return count; }
2960 template< unsigned int ... FEMSigs > size_t validFEMNodes ( UIntPack< FEMSigs ... > , LocalDepth d ) const { size_t count = 0 ; _tree.processNodes( [&]( const FEMTreeNode *n ){ if( _localDepth(n)==d && isValidFEMNode( UIntPack< FEMSigs ... >() , n ) ) count++; } ) ; return count; }
2961 template< unsigned int ... FEMSigs > size_t validUnlockedFEMNodes( UIntPack< FEMSigs ... > ) const { size_t count = 0 ; _tree.processNodes( [&]( const FEMTreeNode *n ){ if( isValidFEMNode( UIntPack< FEMSigs ... >() , n ) && !n->nodeData.getDirichletSupportedFlag() ) count++; } ) ; return count; }
2962 template< unsigned int ... FEMSigs > size_t validUnlockedFEMNodes( UIntPack< FEMSigs ... > , LocalDepth d ) const { size_t count = 0 ; _tree.processNodes( [&]( const FEMTreeNode *n ){ if( _localDepth(n)==d && isValidFEMNode( UIntPack< FEMSigs ... >() , n ) && !n->nodeData.getDirichletElementFlag() ) count++; } ) ; return count; }
2963 LocalDepth depth( void ) const { return _spaceRoot->maxDepth(); }
2964 LocalDepth maxDepth( void ) const { return _maxDepth; }
2965 template< typename ... DenseOrSparseNodeData >
2966 node_index_type resetNodeIndices( char flagsToClear , std::tuple< DenseOrSparseNodeData *... > data )
2967 {
2968 char mask = ~flagsToClear;
2969 if( sizeof...( DenseOrSparseNodeData ) )
2970 {
2971 std::vector< node_index_type > map( _nodeCount );
2972 _nodeCount = 0;
2973 auto nodeFunctor = [&]( FEMTreeNode *node )
2974 {
2975 node_index_type idx = node->nodeData.nodeIndex;
2976 _nodeInitializer( *node );
2977 if( node->nodeData.nodeIndex!=-1 ) map[ node->nodeData.nodeIndex ] = idx;
2978 node->nodeData.flags &= mask;
2979 };
2980 _tree.processNodes( nodeFunctor );
2981
2982 _reorderDenseOrSparseNodeData< 0 >( GetPointer( map ) , (size_t)_nodeCount , data );
2983 }
2984 else
2985 {
2986 _nodeCount = 0;
2987 auto nodeFunctor = [&]( FEMTreeNode *node )
2988 {
2989 _nodeInitializer( *node );
2990 node->nodeData.flags &= mask;
2991 };
2992 _tree.processNodes( nodeFunctor );
2993 }
2994 return _nodeCount;
2995 }
2996 node_index_type resetNodeIndices( char flagsToClear ){ return resetNodeIndices( flagsToClear , std::make_tuple() ); }
2997
2998 std::vector< node_index_type > merge( FEMTree* tree );
2999 protected:
3000 template< class Real1 , unsigned int _Dim > static bool _IsZero( Point< Real1 , _Dim > p );
3001 template< class Real1 > static bool _IsZero( Real1 p );
3002 template< class SReal , class Data , unsigned int _Dim > static Data _StencilDot( Point< SReal , _Dim > p1 , Point< Data , _Dim > p2 );
3003 template< class SReal , class Data > static Data _StencilDot( Point< SReal , 1 > p1 , Point< Data , 1 > p2 );
3004 template< class SReal , class Data > static Data _StencilDot( SReal p1 , Point< Data , 1 > p2 );
3005 template< class SReal , class Data > static Data _StencilDot( Point< SReal , 1 > p1 , Data p2 );
3006 template< class SReal , class Data > static Data _StencilDot( SReal p1 , Data p2 );
3007
3008 // We need the signatures to test if nodes are valid
3009 template< typename T , unsigned int ... FEMSigs , unsigned int ... CSigs , unsigned int ... FEMDegrees , unsigned int ... CDegrees , unsigned int CDim , class Coefficients >
3010 void _addFEMConstraints( UIntPack< FEMSigs ... > , UIntPack< CSigs ... > , typename BaseFEMIntegrator::Constraint< UIntPack< FEMDegrees ... > , UIntPack< CDegrees ... > , CDim >& F , const Coefficients& coefficients , Pointer( T ) constraints , LocalDepth maxDepth ) const;
3011 template< typename T , typename TDotT , unsigned int ... FEMSigs1 , unsigned int ... FEMSigs2 , unsigned int ... Degrees1 , unsigned int ... Degrees2 , class Coefficients1 , class Coefficients2 >
3012 double _dot( UIntPack< FEMSigs1 ... > , UIntPack< FEMSigs2 ... > , typename BaseFEMIntegrator::Constraint< UIntPack< Degrees1 ... > , UIntPack< Degrees2 ... > , 1 >& F , const Coefficients1& coefficients1 , const Coefficients2& coefficients2 , TDotT Dot ) const;
3013 template< typename T , typename TDotT , unsigned int ... FEMSigs1 , unsigned int ... FEMSigs2 , class Coefficients1 , class Coefficients2 , unsigned int PointD >
3014 double _interpolationDot( UIntPack< FEMSigs1 ... > , UIntPack< FEMSigs2 ... > , const Coefficients1& coefficients1 , const Coefficients2& coefficients2 , TDotT Dot , const InterpolationInfo< T , PointD >* iInfo ) const;
3015 template< unsigned int Idx , typename T , typename TDotT , unsigned int ... FEMSigs1 , unsigned int ... FEMSigs2 , class Coefficients1 , class Coefficients2 , typename ... InterpolationInfos >
3016 typename std::enable_if< ( Idx==sizeof...(InterpolationInfos) ) , double >::type _interpolationDot( UIntPack< FEMSigs1 ... > , UIntPack< FEMSigs2 ... > , const Coefficients1& coefficients1 , const Coefficients2& coefficients2 , TDotT Dot , std::tuple< InterpolationInfos *... > interpolationInfos ) const { return 0.; }
3017 template< unsigned int Idx , typename T , typename TDotT , unsigned int ... FEMSigs1 , unsigned int ... FEMSigs2 , class Coefficients1 , class Coefficients2 , typename ... InterpolationInfos >
3018 typename std::enable_if< ( Idx <sizeof...(InterpolationInfos) ) , double >::type _interpolationDot( UIntPack< FEMSigs1 ... > , UIntPack< FEMSigs2 ... > , const Coefficients1& coefficients1 , const Coefficients2& coefficients2 , TDotT Dot , std::tuple< InterpolationInfos *... > interpolationInfos ) const
3019 {
3020 return _interpolationDot< T >( UIntPack< FEMSigs1 ... >() , UIntPack< FEMSigs2 ... >() , coefficients1 , coefficients2 , Dot , std::get< Idx >( interpolationInfos ) ) + _interpolationDot< Idx+1 , T >( UIntPack< FEMSigs1 ... >() , UIntPack< FEMSigs2 ... >() , coefficients1 , coefficients2 , Dot , interpolationInfos );
3021 }
3022 template< typename T , typename TDotT , unsigned int ... FEMSigs1 , unsigned int ... FEMSigs2 , class Coefficients1 , class Coefficients2 > double _interpolationDot( UIntPack< FEMSigs1 ... > , UIntPack< FEMSigs2 ... > , const Coefficients1& coefficients1 , const Coefficients2& coefficients2 , TDotT Dot ) const{ return 0; }
3023 };
3024
3025
3026 template< unsigned int Dim , class Real >
3027 struct FEMTreeInitializer
3028 {
3029 typedef RegularTreeNode< Dim , FEMTreeNodeData , depth_and_offset_type > FEMTreeNode;
3030 typedef NodeAndPointSample< Dim , Real > PointSample;
3031
3032 template< class Data >
3033 struct DerivativeStream
3034 {
3035 virtual void resolution( unsigned int res[] ) const = 0;
3036 virtual bool nextDerivative( unsigned int idx[] , unsigned int& dir , Data& dValue ) = 0;
3037 };
3038
3039 // Initialize the tree using a refinement avatar
3040 static size_t Initialize( FEMTreeNode& root , int maxDepth , std::function< bool ( int , int[] ) > Refine , Allocator< FEMTreeNode >* nodeAllocator , std::function< void ( FEMTreeNode& ) > NodeInitializer );
3041
3042 // Initialize the tree using a point stream
3043 template< typename Data >
3044 struct InputPointStream
3045 {
3046 typedef Data DataType;
3047 typedef DirectSum< Real , Point< Real , Dim > , Data > PointAndDataType;
3048 typedef InputDataStream< PointAndDataType > StreamType;
3049 static Point< Real , Dim > &GetPoint( PointAndDataType &pd ){ return pd.template get<0>(); }
3050 static const Point< Real , Dim > &GetPoint( const PointAndDataType &pd ){ return pd.template get<0>(); }
3051 static DataType &GetData( PointAndDataType &pd ){ return pd.template get<1>(); }
3052 static const DataType &GetData( const PointAndDataType &pd ){ return pd.template get<1>(); }
3053
3054 static void BoundingBox( StreamType &stream , Data d , Point< Real , Dim >& min , Point< Real , Dim >& max )
3055 {
3056 PointAndDataType p;
3057 p.template get<1>() = d;
3058 for( unsigned int d=0 ; d<Dim ; d++ ) min[d] = std::numeric_limits< Real >::infinity() , max[d] = -std::numeric_limits< Real >::infinity();
3059 while( stream.read( p ) ) for( unsigned int d=0 ; d<Dim ; d++ ) min[d] = std::min< Real >( min[d] , p.template get<0>()[d] ) , max[d] = std::max< Real >( max[d] , p.template get<0>()[d] );
3060 stream.reset();
3061 }
3062 };
3063
3064 template< typename Data >
3065 struct OutputPointStream
3066 {
3067 typedef Data DataType;
3068 typedef DirectSum< Real , Point< Real , Dim > , Data > PointAndDataType;
3069 typedef OutputDataStream< PointAndDataType > StreamType;
3070 static Point< Real , Dim > &GetPoint( PointAndDataType &pd ){ return pd.template get<0>(); }
3071 static const Point< Real , Dim > &GetPoint( const PointAndDataType &pd ){ return pd.template get<0>(); }
3072 static DataType &GetData( PointAndDataType &pd ){ return pd.template get<1>(); }
3073 static const DataType &GetData( const PointAndDataType &pd ){ return pd.template get<1>(); }
3074 };
3075
3076 struct StreamInitializationData
3077 {
3078 friend FEMTreeInitializer;
3079 protected:
3080 std::vector< node_index_type > _nodeToIndexMap;
3081 };
3082
3083 template< typename IsValidFunctor/*=std::function< bool ( const Point< Real , Dim > & , AuxData &... ) >*/ , typename ProcessFunctor/*=std::function< bool ( FEMTreeNode & , const Point< Real , Dim > & , AuxData &... ) >*/ , typename ... AuxData >
3084 static size_t Initialize( FEMTreeNode &root , InputDataStream< Point< Real , Dim > , AuxData ... > &pointStream , AuxData ... zeroData , int maxDepth , Allocator< FEMTreeNode >* nodeAllocator , std::function< void ( FEMTreeNode& ) > NodeInitializer , IsValidFunctor IsValid , ProcessFunctor Process );
3085 template< typename IsValidFunctor/*=std::function< bool ( const Point< Real , Dim > & , AuxData &... ) >*/ , typename ProcessFunctor/*=std::function< bool ( FEMTreeNode & , const Point< Real , Dim > & , AuxData &... ) >*/ , typename ... AuxData >
3086 static size_t Initialize( FEMTreeNode &root , InputDataStream< Point< Real , Dim > , AuxData ... > &pointStream , AuxData ... zeroData , int maxDepth , std::function< int ( Point< Real , Dim > ) > pointDepthFunctor , Allocator< FEMTreeNode >* nodeAllocator , std::function< void ( FEMTreeNode& ) > NodeInitializer , IsValidFunctor IsValid , ProcessFunctor Process );
3087
3088 template< typename AuxData >
3089 static size_t Initialize( struct StreamInitializationData &sid , FEMTreeNode &root , InputDataStream< Point< Real , Dim > , AuxData > &pointStream , AuxData zeroData , int maxDepth , std::vector< PointSample >& samplePoints , std::vector< AuxData > &sampleData , Allocator< FEMTreeNode >* nodeAllocator , std::function< void ( FEMTreeNode& ) > NodeInitializer , std::function< Real ( const Point< Real , Dim > & , AuxData & ) > ProcessData = []( const Point< Real , Dim > & , AuxData & ){ return (Real)1.; } );
3090 template< typename AuxData >
3091 static size_t Initialize( struct StreamInitializationData &sid , FEMTreeNode &root , InputDataStream< Point< Real , Dim > , AuxData > &pointStream , AuxData zeroData , int maxDepth , std::function< int ( Point< Real , Dim > ) > pointDepthFunctor , std::vector< PointSample >& samplePoints , std::vector< AuxData > &sampleData , Allocator< FEMTreeNode >* nodeAllocator , std::function< void ( FEMTreeNode& ) > NodeInitializer , std::function< Real ( const Point< Real , Dim > & , AuxData & ) > ProcessData = []( const Point< Real , Dim > & , AuxData & ){ return (Real)1.; } );
3092 template< typename AuxData >
3093 static size_t Initialize( FEMTreeNode &root , InputDataStream< Point< Real , Dim > , AuxData > &pointStream , AuxData zeroData , int maxDepth , std::vector< PointSample >& samplePoints , std::vector< AuxData > &sampleData , Allocator< FEMTreeNode >* nodeAllocator , std::function< void ( FEMTreeNode& ) > NodeInitializer , std::function< Real ( const Point< Real , Dim > & , AuxData & ) > ProcessData = []( const Point< Real , Dim > & , AuxData & ){ return (Real)1.; } );
3094 template< typename AuxData >
3095 static size_t Initialize( FEMTreeNode &root , InputDataStream< Point< Real , Dim > , AuxData > &pointStream , AuxData zeroData , int maxDepth , std::function< int ( Point< Real , Dim > ) > pointDepthFunctor , std::vector< PointSample >& samplePoints , std::vector< AuxData > &sampleData , Allocator< FEMTreeNode >* nodeAllocator , std::function< void ( FEMTreeNode& ) > NodeInitializer , std::function< Real ( const Point< Real , Dim > & , AuxData & ) > ProcessData = []( const Point< Real , Dim > & , AuxData & ){ return (Real)1.; } );
3096
3097 // Initialize the tree using simplices
3098 static void Initialize( FEMTreeNode& root , const std::vector< Point< Real , Dim > >& vertices , const std::vector< SimplexIndex< Dim-1 , node_index_type > >& simplices , int maxDepth , std::vector< PointSample >& samples , std::vector< Allocator< FEMTreeNode > * > &nodeAllocators , std::function< void ( FEMTreeNode& ) > NodeInitializer );
3099 static void Initialize( FEMTreeNode& root , const std::vector< Point< Real , Dim > >& vertices , const std::vector< SimplexIndex< Dim-1 , node_index_type > >& simplices , unsigned int regularGridDepth , unsigned int maxDepth , std::vector< NodeSimplices< Dim , Real > >& nodeSimplices , std::vector< Allocator< FEMTreeNode > * > &nodeAllocators , std::function< void ( FEMTreeNode& ) > NodeInitializer );
3100
3101 struct GeometryNodeType
3102 {
3103 enum Type : char
3104 {
3105 UNKNOWN ,
3106 INTERIOR ,
3107 BOUNDARY ,
3108 EXTERIOR
3109 };
3110 Type type;
3111 GeometryNodeType( Type t=UNKNOWN ) : type( t ){}
3112 bool operator== ( GeometryNodeType gnt ) const { return type==gnt.type; }
3113 bool operator!= ( GeometryNodeType gnt ) const { return type!=gnt.type; }
3114 bool operator== ( Type type ) const { return this->type==type; }
3115 bool operator!= ( Type type ) const { return this->type!=type; }
3116
3117 friend std::ostream &operator << ( std::ostream &os , const GeometryNodeType &type )
3118 {
3119 if ( type.type==UNKNOWN ) return os << "unknown";
3120 else if( type.type==INTERIOR ) return os << "interior";
3121 else if( type.type==BOUNDARY ) return os << "boundary";
3122 else if( type.type==EXTERIOR ) return os << "exterior";
3123 return os;
3124 }
3125 };
3126
3127 template< unsigned int _Dim=Dim >
3128 static typename std::enable_if< _Dim!=1 , DenseNodeData< GeometryNodeType , IsotropicUIntPack< Dim , FEMTrivialSignature > > >::type GetGeometryNodeDesignators( FEMTreeNode *root , const std::vector< Point< Real , Dim > >& vertices , const std::vector< SimplexIndex< Dim-1 , node_index_type > >& simplices , unsigned int regularGridDepth , unsigned int maxDepth , std::vector< Allocator< FEMTreeNode > * > &nodeAllocators , std::function< void ( FEMTreeNode& ) > NodeInitializer );
3129 template< unsigned int _Dim=Dim >
3130 static typename std::enable_if< _Dim==1 , DenseNodeData< GeometryNodeType , IsotropicUIntPack< Dim , FEMTrivialSignature > > >::type GetGeometryNodeDesignators( FEMTreeNode *root , const std::vector< Point< Real , Dim > >& vertices , const std::vector< SimplexIndex< Dim-1 , node_index_type > >& simplices , unsigned int regularGridDepth , unsigned int maxDepth , std::vector< Allocator< FEMTreeNode > * > &nodeAllocators , std::function< void ( FEMTreeNode& ) > NodeInitializer );
3131 static void TestGeometryNodeDesignators( const FEMTreeNode *root , const DenseNodeData< GeometryNodeType , IsotropicUIntPack< Dim , FEMTrivialSignature > > &geometryNodeDesignators );
3132 static void PushGeometryNodeDesignatorsToFiner( const FEMTreeNode *root , DenseNodeData< GeometryNodeType , IsotropicUIntPack< Dim , FEMTrivialSignature > > &geometryNodeDesignators , unsigned int maxDepth=-1 );
3133 static void PullGeometryNodeDesignatorsFromFiner( const FEMTreeNode *root , DenseNodeData< GeometryNodeType , IsotropicUIntPack< Dim , FEMTrivialSignature > > &geometryNodeDesignators , unsigned int maxDepth=-1 );
3134
3135 // Initialize the tree using weighted points
3136 static void Initialize( FEMTreeNode &root , const std::vector< ProjectiveData< Point< Real , Dim > , Real > > &points , int maxDepth , std::vector< PointSample >& samples , std::vector< Allocator< FEMTreeNode > * > &nodeAllocators , std::function< void ( FEMTreeNode& ) > NodeInitializer );
3137 static void Initialize( FEMTreeNode &root , const std::vector< ProjectiveData< Point< Real , Dim > , Real > > &points , int maxDepth , std::vector< PointSample >& samples );
3138
3139 template< class Data , class _Data , bool Dual=true >
3140 static size_t Initialize( FEMTreeNode& root , ConstPointer( Data ) values , ConstPointer( int ) labels , int resolution[Dim] , std::vector< NodeSample< Dim , _Data > > derivatives[Dim] , Allocator< FEMTreeNode >* nodeAllocator , std::function< void ( FEMTreeNode& ) > NodeInitializer , std::function< _Data ( const Data& ) > DataConverter = []( const Data& d ){ return (_Data)d; } );
3141 template< bool Dual , class Data >
3142 static unsigned int Initialize( FEMTreeNode& root , DerivativeStream< Data >& dStream , Data zeroData , std::vector< NodeSample< Dim , Data > > derivatives[Dim] , Allocator< FEMTreeNode >* nodeAllocator , std::function< void ( FEMTreeNode& ) > NodeInitializer );
3143
3144 protected:
3145 static size_t _Initialize( FEMTreeNode &node , int maxDepth , std::function< bool ( int , int[] ) > Refine , Allocator< FEMTreeNode >* nodeAllocator , std::function< void ( FEMTreeNode& ) > NodeInitializer );
3146 template< bool ThreadSafe > static size_t _AddSimplex( FEMTreeNode& root , Simplex< Real , Dim , Dim-1 >& s , int maxDepth , std::vector< PointSample >& samples , std::vector< node_index_type >* nodeToIndexMap , Allocator< FEMTreeNode >* nodeAllocator , std::function< void ( FEMTreeNode& ) > NodeInitializer );
3147 template< bool ThreadSafe > static size_t _AddSimplex( FEMTreeNode* node , Simplex< Real , Dim , Dim-1 >& s , int maxDepth , std::vector< PointSample >& samples , std::vector< node_index_type >* nodeToIndexMap , Allocator< FEMTreeNode >* nodeAllocator , std::function< void ( FEMTreeNode& ) > NodeInitializer );
3148 template< bool ThreadSafeAllocation , bool ThreadSafeSimplices > static size_t _AddSimplex( FEMTreeNode& root , node_index_type id , Simplex< Real , Dim , Dim-1 >& s , int maxDepth , std::vector< NodeSimplices< Dim , Real > >& simplices , std::vector< node_index_type >& nodeToIndexMap , Allocator< FEMTreeNode >* nodeAllocator , std::function< void ( FEMTreeNode& ) > NodeInitializer );
3149 template< bool ThreadSafeAllocation , bool ThreadSafeSimplices > static size_t _AddSimplex( FEMTreeNode* node , node_index_type id , Simplex< Real , Dim , Dim-1 >& s , int maxDepth , std::vector< NodeSimplices< Dim , Real > >& simplices , std::vector< node_index_type >& nodeToIndexMap , Allocator< FEMTreeNode >* nodeAllocator , std::function< void ( FEMTreeNode& ) > NodeInitializer );
3150 template< bool ThreadSafe > static size_t _AddSample( FEMTreeNode& root , ProjectiveData< Point< Real , Dim > , Real > s , int maxDepth , std::vector< PointSample >& samples , std::vector< node_index_type >* nodeToIndexMap , Allocator< FEMTreeNode >* nodeAllocator , std::function< void ( FEMTreeNode& ) > NodeInitializer );
3151 static size_t _AddSample( FEMTreeNode& root , ProjectiveData< Point< Real , Dim > , Real > s , int maxDepth , std::vector< PointSample >& samples , std::vector< node_index_type >* nodeToIndexMap );
3152 static size_t _AddSample( FEMTreeNode* node , ProjectiveData< Point< Real , Dim > , Real > s , int maxDepth , std::vector< PointSample >& samples , std::vector< node_index_type >* nodeToIndexMap );
3153 static DenseNodeData< GeometryNodeType , IsotropicUIntPack< Dim , FEMTrivialSignature > > _GetGeometryNodeDesignators( FEMTreeNode *root , const std::vector< Point< Real , Dim > >& vertices , const std::vector< SimplexIndex< Dim-1 , node_index_type > >& simplices , const std::vector< Point< Real , Dim > > &normals , unsigned int regularGridDepth , unsigned int maxDepth , std::vector< Allocator< FEMTreeNode > * > &nodeAllocators , std::function< void ( FEMTreeNode& ) > NodeInitializer );
3154 };
3155 template< unsigned int Dim , class Real >
3156 template< unsigned int ... SupportSizes >
3157 const unsigned int FEMTree< Dim , Real >::CornerLoopData< SupportSizes ... >::supportSizes[] = { SupportSizes ... };
3158
3159
3160 #include "FEMTree.inl"
3161 #include "FEMTree.SortedTreeNodes.inl"
3162 #include "FEMTree.WeightedSamples.inl"
3163 #include "FEMTree.System.inl"
3164 #include "FEMTree.Evaluation.inl"
3165 #include "FEMTree.LevelSet.inl"
3166 #include "FEMTree.LevelSet.2D.inl"
3167 #include "FEMTree.LevelSet.3D.inl"
3168 #include "FEMTree.Initialize.inl"
3169 }
3170
3171 #endif // FEM_TREE_INCLUDED