Debian Codegraph @

src/thirdparty/PoissonRecon/FEMTree.inl

raw
1 /*
2 Copyright (c) 2006, Michael Kazhdan and Matthew Bolitho
3 All rights reserved.
4
5 Redistribution and use in source and binary forms, with or without modification,
6 are permitted provided that the following conditions are met:
7
8 Redistributions of source code must retain the above copyright notice, this list of
9 conditions and the following disclaimer. Redistributions in binary form must reproduce
10 the above copyright notice, this list of conditions and the following disclaimer
11 in the documentation and/or other materials provided with the distribution.
12
13 Neither the name of the Johns Hopkins University nor the names of its contributors
14 may be used to endorse or promote products derived from this software without specific
15 prior written permission.
16
17 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
18 EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO THE IMPLIED WARRANTIES
19 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
20 SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
21 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
22 TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
23 BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
25 ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
26 DAMAGE.
27 */
28
29 /////////////////////
30 // FEMTreeNodeData //
31 /////////////////////
32 #ifdef SANITIZED_PR
33 FEMTreeNodeData::FEMTreeNodeData( void ) : flags(0){}
34 FEMTreeNodeData &FEMTreeNodeData::operator = ( const FEMTreeNodeData &data )
35 {
36 nodeIndex = data.nodeIndex;
37 flags = data.flags.load();
38 return *this;
39 }
40 #else // !SANITIZED_PR
41 FEMTreeNodeData::FEMTreeNodeData( void ){ flags = 0; }
42 #endif // SANITIZED_PR
43 FEMTreeNodeData::~FEMTreeNodeData( void ) { }
44
45
46 /////////////
47 // FEMTree //
48 /////////////
49
50 template< unsigned int Dim , class Real >
51 void FEMTree< Dim , Real >::_init( void )
52 {
53 // Reset the depths and offsets
54 int offset[Dim];
55 for( int d=0 ; d<Dim ; d++ ) offset[d] = 0;
56 RegularTreeNode< Dim , FEMTreeNodeData , depth_and_offset_type >::ResetDepthAndOffset( &_tree , 0 , offset );
57
58 // Set the _spaceRoot
59 _spaceRoot = &_tree;
60 for( int d=0 ; d<_depthOffset ; d++ )
61 {
62 if( !_spaceRoot->children ) MK_THROW( "Expected child node: " , d , " / " , _depthOffset );
63 else if( d==0 ) _spaceRoot = _spaceRoot->children + (1<<Dim)-1;
64 else _spaceRoot = _spaceRoot->children;
65 }
66 }
67
68 template< unsigned int Dim , class Real >
69 FEMTree< Dim , Real > *FEMTree< Dim , Real >::Merge( const FEMTree< Dim , Real > &tree1 , const FEMTree< Dim , Real > &tree2 , size_t blockSize )
70 {
71 if( tree1._baseDepth != tree2._baseDepth ) MK_THROW( "Base depths differ: " , tree1._baseDepth , " != " , tree2._baseDepth );
72 if( tree1._depthOffset != tree2._depthOffset ) MK_THROW( "Depth offsets differ: " , tree1._depthOffset , " != " , tree2._depthOffset );
73 FEMTree< Dim , Real > *mergeTree = new FEMTree( blockSize );
74
75 // have support overlapping the slice.
76 std::function< void ( const FEMTreeNode *node1 , const FEMTreeNode *node2 , FEMTreeNode *mergeNode ) > mergeTrees =
77 [&]( const FEMTreeNode *node1 , const FEMTreeNode *node2 , FEMTreeNode *mergeNode )
78 {
79 if( ( node1 && node1->children ) || ( node2 && node2->children ) )
80 {
81 if( !mergeNode->children ) mergeNode->template initChildren< false >( mergeTree->nodeAllocators.size() ? mergeTree->nodeAllocators[0] : NULL , mergeTree->_nodeInitializer );
82
83 for( unsigned int c=0 ; c<(1<<Dim) ; c++ )
84 if ( node1 && node1->children && node2 && node2->children ) mergeTrees( node1->children+c , node2->children+c , mergeNode->children+c );
85 else if( node1 && node1->children ) mergeTrees( node1->children+c , NULL , mergeNode->children+c );
86 else if( node2 && node2->children ) mergeTrees( NULL , node2->children+c , mergeNode->children+c );
87 }
88 };
89 mergeTrees( &tree1._tree , &tree2._tree , &mergeTree->_tree );
90
91 int d=0 , off[Dim];
92 for( int d=0 ; d<Dim ; d++ ) off[d] = 0;
93 FEMTreeNode::ResetDepthAndOffset( &mergeTree->_tree , d , off );
94 mergeTree->_depthOffset = tree1._depthOffset;
95 mergeTree->_baseDepth = tree1._baseDepth;
96
97 mergeTree->_init();
98 mergeTree->_maxDepth = mergeTree->_spaceRoot->maxDepth();
99
100 std::vector< node_index_type > map;
101 mergeTree->_sNodes.reset( mergeTree->_tree , map );
102 mergeTree->_setSpaceValidityFlags();
103
104 return mergeTree;
105 }
106
107 template< unsigned int Dim , class Real >
108 template< unsigned int CrossDegree , unsigned int Pad >
109 FEMTree< Dim , Real > *FEMTree< Dim , Real >::Slice( const FEMTree< Dim+1 , Real > &tree , unsigned int sliceDepth , unsigned int sliceIndex , bool includeBounds , size_t blockSize )
110 {
111 if( sliceIndex>(unsigned int)(1<<sliceDepth) ) MK_THROW( "Slice index out of bounds: 0 <= " , sliceIndex , " <= " , (1<<sliceDepth) );
112 FEMTree< Dim , Real > *sliceTree = new FEMTree( blockSize );
113
114 unsigned int maxDepth = tree.maxDepth();
115 if( sliceDepth<maxDepth+1 )
116 {
117 sliceIndex <<= ( maxDepth+1-sliceDepth );
118 sliceDepth = maxDepth+1;
119 }
120 const int StartOffset = BSplineSupportSizes< CrossDegree >::SupportStart-(int)Pad;
121 const int EndOffset = BSplineSupportSizes< CrossDegree >::SupportEnd+1+(int)Pad;
122
123 // A function returning true if the function indexed by the node has support overlapping the slice
124 auto OverlapsSlice = [&]( const typename FEMTree< Dim+1 , Real >::FEMTreeNode *node )
125 {
126 typename FEMTree< Dim+1 , Real >::LocalDepth d ; typename FEMTree< Dim+1 , Real >::LocalOffset off;
127 tree.depthAndOffset( node , d , off );
128 if( d<0 ) return true;
129 else
130 {
131 int start = ( off[Dim] + StartOffset )<<(sliceDepth-d);
132 int end = ( off[Dim] + EndOffset )<<(sliceDepth-d);
133 if( includeBounds ) return (int)sliceIndex>=start && (int)sliceIndex<=end;
134 else return (int)sliceIndex> start && (int)sliceIndex< end;
135 }
136 };
137
138 // Walk through the two trees in tandem, adding children to the slice-tree if the associated children in the full tree
139 // have support overlapping the slice.
140 std::function< void ( const typename FEMTree< Dim+1 , Real >::FEMTreeNode *node , typename FEMTree< Dim , Real >::FEMTreeNode *sliceNode ) > refineSliceTree =
141 [&]( const typename FEMTree< Dim+1 , Real >::FEMTreeNode *node , typename FEMTree< Dim , Real >::FEMTreeNode *sliceNode )
142 {
143 if( !GetGhostFlag( node->children ) )
144 {
145 bool overlaps = false;
146 for( unsigned int c=0 ; c<(1<<(Dim+1)) ; c++ ) overlaps |= OverlapsSlice( node->children+c );
147 if( overlaps )
148 {
149 if( !sliceNode->children ) sliceNode->template initChildren< false >( sliceTree->nodeAllocators.size() ? sliceTree->nodeAllocators[0] : NULL , sliceTree->_nodeInitializer );
150 for( unsigned int c=0 ; c<(1<<Dim) ; c++ )
151 {
152 refineSliceTree( node->children+( c|(1<<Dim) ) , sliceNode->children+c );
153 refineSliceTree( node->children+( c ) , sliceNode->children+c );
154 }
155 }
156 }
157 };
158
159 refineSliceTree( &tree._tree , &sliceTree->_tree );
160
161 int d=0 , off[Dim];
162 for( int d=0 ; d<Dim ; d++ ) off[d] = 0;
163 FEMTreeNode::ResetDepthAndOffset( &sliceTree->_tree , d , off );
164 sliceTree->_depthOffset = tree._depthOffset;
165 sliceTree->_baseDepth = tree._baseDepth;
166
167 sliceTree->_init();
168 sliceTree->_maxDepth = sliceTree->_spaceRoot->maxDepth();
169
170 std::vector< node_index_type > map;
171 sliceTree->_sNodes.reset( sliceTree->_tree , map );
172 sliceTree->_setSpaceValidityFlags();
173 return sliceTree;
174 }
175
176 template< unsigned int Dim , class Real >
177 template< unsigned int ... FEMSigs , typename Data >
178 void FEMTree< Dim , Real >::merge( const FEMTree< Dim , Real > &tree , const DenseNodeData< Data , UIntPack< FEMSigs ... > > &coefficients , DenseNodeData< Data , UIntPack< FEMSigs ... > > &mergeCoefficients ) const
179 {
180 static_assert( sizeof ... ( FEMSigs )==Dim , "[ERROR] Signature count and dimension don't match" );
181
182 std::function< void ( const FEMTreeNode * , const FEMTreeNode * ) > accumulateCoefficients =
183 [&]( const FEMTreeNode *node , const FEMTreeNode *mergeNode )
184 {
185 if( node && node->nodeData.nodeIndex!=-1 )
186 {
187 if( !mergeNode || mergeNode->nodeData.nodeIndex==-1 ) MK_THROW( "Merge node not set" );
188 LocalDepth d ; LocalOffset off;
189 tree.depthAndOffset( node , d , off );
190 mergeCoefficients[ mergeNode->nodeData.nodeIndex ] += coefficients[ node->nodeData.nodeIndex ];
191 }
192 if( node && node->children ) for( unsigned int c=0 ; c<(1<<Dim) ; c++ ) accumulateCoefficients( node->children+c , mergeNode->children+c );
193 };
194 accumulateCoefficients( &tree._tree , &_tree );
195 }
196
197 template< unsigned int Dim , class Real , unsigned int Pad , unsigned int FEMSig >
198 struct SliceEvaluator
199 {
200 struct _SliceEvaluator
201 {
202 const int StartOffset = BSplineSupportSizes< FEMSignature< FEMSig >::Degree >::SupportStart-(int)Pad;
203 const int EndOffset = BSplineSupportSizes< FEMSignature< FEMSig >::Degree >::SupportEnd+1+(int)Pad;
204 int start;
205 Real values[ BSplineSupportSizes< FEMSignature< FEMSig >::Degree >::SupportSize+2*Pad ];
206 void init( unsigned int depth , double x , unsigned int d )
207 {
208 // off @ depthsupports the slice if
209 // x>(off+StartOffset)/(1<<depth) && x<(off+EndOffset)/(1<<depth)
210 // <=> x*(1<<depth)-StartOffset>off && x*(1<<depth)-EndOfset<off
211 // <=> off \in ( x*(1<<depth)-EndOffset , x*(1<<depth)-StartOffset )
212 // <= off \in [ ceil( x*(1<<depth) )-EndOffset , ceil( x*(1<<depth) ) - StartOffset )
213 start = (int)ceil( x*(1<<depth)-EndOffset );
214
215 // The derivative is lower than the degree, the derivative will be continuous so we can evaluate it directly
216 if( d<FEMSignature< FEMSig >::Degree ) for( int i=0 ; i<EndOffset-StartOffset ; i++ ) values[i] = (Real)BSplineEvaluationData< FEMSig >::Value( depth , start+i , x , d );
217 else if( d==FEMSignature< FEMSig >::Degree )
218 {
219 double eps = 1e-4/(1<<depth);
220 #ifdef SHOW_WARNINGS
221 MK_WARN_ONCE( "Using discrete derivative: " , eps );
222 #endif // SHOW_WARNINGS
223 // If we are dealing with a degree-zero polynomial, we offset
224 if( d==0 )
225 {
226 for( int i=0 ; i<EndOffset-StartOffset ; i++ )
227 {
228 double value = 0;
229 value += BSplineEvaluationData< FEMSig >::Value( depth , start+i , x-eps , d );
230 value += BSplineEvaluationData< FEMSig >::Value( depth , start+i , x+eps , d );
231 values[i] = (Real)(value/2);
232 }
233 }
234 else // Otherwise we compute the discrete derivative
235 {
236 for( int i=0 ; i<EndOffset-StartOffset ; i++ )
237 {
238 double value = 0;
239 value += BSplineEvaluationData< FEMSig >::Value( depth , start+i , x , d-1 ) - BSplineEvaluationData< FEMSig >::Value( depth , start+i , x-eps , d-1 );
240 value += BSplineEvaluationData< FEMSig >::Value( depth , start+i , x+eps , d-1 ) - BSplineEvaluationData< FEMSig >::Value( depth , start+i , x , d-1 );
241 values[i] = (Real)(value/(2*eps) );
242 }
243 }
244
245 }
246 else MK_THROW( "Derivative exceeds degree: " , d , " > " , FEMSignature< FEMSig >::Degree );
247 }
248 Real operator()( int off ) const
249 {
250 if( off<start || off>=start+(EndOffset-StartOffset ) ) return 0;
251 else return values[off-start];
252 }
253 };
254
255 std::vector< _SliceEvaluator > evaluators;
256 void init( unsigned int maxDepth , double x , unsigned int d )
257 {
258 evaluators.resize( maxDepth+1 );
259 for( unsigned int depth=0 ; depth<=maxDepth ; depth++ ) evaluators[depth].init( depth , x , d );
260 }
261 Real operator()( int d , int off ) const
262 {
263 if( d<0 || d>=(int)evaluators.size() ) return 0;
264 else return evaluators[d]( off );
265 }
266 };
267
268 template< unsigned int Dim , class Real >
269 template< unsigned int Pad , unsigned int FEMSig , unsigned int ... FEMSigs , typename Data >
270 void FEMTree< Dim , Real >::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
271 {
272 static_assert( sizeof ... ( FEMSigs )==Dim , "[ERROR] Signature count and dimension don't match" );
273 #ifdef __GNUC__
274 #ifdef SHOW_WARNINGS
275 #warning "you've got me gcc version<5"
276 #endif // SHOW_WARNINGS
277 #else // !__GNUC__
278 static const unsigned int CrossDegree = FEMSignature< FEMSig >::Degree;
279 static const unsigned int _Pad = Pad;
280 #endif // __GNUC__
281
282 unsigned int maxDepth = tree.maxDepth();
283 if( sliceDepth<maxDepth+1 )
284 {
285 sliceIndex <<= ( maxDepth+1-sliceDepth );
286 sliceDepth = maxDepth+1;
287 }
288
289 double s = (double)sliceIndex/(1<<sliceDepth);
290
291 #ifdef __GNUC__
292 const int StartOffset = BSplineSupportSizes< FEMSignature< FEMSig >::Degree >::SupportStart-(int)Pad;
293 const int EndOffset = BSplineSupportSizes< FEMSignature< FEMSig >::Degree >::SupportEnd+1+(int)Pad;
294 #else // !__GNUC__
295 const int StartOffset = BSplineSupportSizes< CrossDegree >::SupportStart-(int)Pad;
296 const int EndOffset = BSplineSupportSizes< CrossDegree >::SupportEnd+1+(int)Pad;
297 #endif // __GNUC__
298
299 SliceEvaluator< Dim , Real , Pad , FEMSig > sliceEvaluator;
300 sliceEvaluator.init( maxDepth , s , d );
301
302 // A function return true if the function indexed by the node has support overlapping the slice
303 auto OverlapsSlice = [&]( const typename FEMTree< Dim+1 , Real >::FEMTreeNode *node )
304 {
305 typename FEMTree< Dim+1 , Real >::LocalDepth d ; typename FEMTree< Dim+1 , Real >::LocalOffset off;
306 tree.depthAndOffset( node , d , off );
307 if( d<0 ) return true;
308 else
309 {
310 int start = ( off[Dim] + StartOffset )<<(sliceDepth-d);
311 int end = ( off[Dim] + EndOffset )<<(sliceDepth-d);
312 return (int)sliceIndex>start && (int)sliceIndex<end;
313 }
314 };
315
316 std::function< void ( const typename FEMTree< Dim+1 , Real >::FEMTreeNode * , const typename FEMTree< Dim , Real >::FEMTreeNode * ) > accumulateSliceCoefficients =
317 [&]( const typename FEMTree< Dim+1 , Real >::FEMTreeNode *node , const typename FEMTree< Dim , Real >::FEMTreeNode *sliceNode )
318 {
319 if( node->nodeData.nodeIndex!=-1 )
320 {
321 if( sliceNode->nodeData.nodeIndex==-1 ) MK_THROW( "Slice node not set" );
322 typename FEMTree< Dim+1 , Real >::LocalDepth d ; typename FEMTree< Dim+1 , Real >::LocalOffset off;
323 tree.depthAndOffset( node , d , off );
324 sliceCoefficients[ sliceNode->nodeData.nodeIndex ] += coefficients[ node->nodeData.nodeIndex ] * sliceEvaluator( d , off[Dim] );
325 }
326 if( !GetGhostFlag( node->children ) )
327 {
328 if( OverlapsSlice( node->children ) ) for( unsigned int c=0 ; c<(1<<Dim) ; c++ ) accumulateSliceCoefficients( node->children+( c ) , sliceNode->children+c );
329 if( OverlapsSlice( node->children+(1<<Dim) ) ) for( unsigned int c=0 ; c<(1<<Dim) ; c++ ) accumulateSliceCoefficients( node->children+( c|(1<<Dim) ) , sliceNode->children+c );
330 }
331 };
332 accumulateSliceCoefficients( &tree._tree , &_tree );
333 }
334
335 template< unsigned int Dim , class Real >
336 FEMTree< Dim , Real >::FEMTree( size_t blockSize ) : _nodeInitializer( *this ) , _depthOffset(1)
337 {
338 if( blockSize )
339 {
340 nodeAllocators.resize( ThreadPool::NumThreads() );
341 for( size_t i=0 ; i<nodeAllocators.size() ; i++ )
342 {
343 nodeAllocators[i] = new Allocator< FEMTreeNode >();
344 nodeAllocators[i]->set( blockSize );
345 }
346 }
347 _nodeCount = 0;
348 // Initialize the root
349 _nodeInitializer( _tree );
350 _tree.template initChildren< false >( nodeAllocators.size() ? nodeAllocators[0] : NULL , _nodeInitializer );
351 _init();
352 memset( _femSigs1 , -1 , sizeof( _femSigs1 ) );
353 memset( _femSigs2 , -1 , sizeof( _femSigs2 ) );
354 }
355
356 template< unsigned int Dim , class Real >
357 FEMTree< Dim , Real >::FEMTree( BinaryStream &stream , size_t blockSize ) : FEMTree( blockSize )
358 {
359 Allocator< FEMTreeNode > *nodeAllocator = nodeAllocators.size() ? nodeAllocators[0] : NULL;
360 node_index_type nodeCount;
361 if( !stream.read( nodeCount ) ) MK_THROW( "Failed to read nodeCount" );
362 _nodeCount = nodeCount;
363 if( !stream.read( _maxDepth ) ) MK_THROW( "Failed to read _maxDepth" );
364 if( !stream.read( _depthOffset ) ) MK_THROW( "Failed to read _depthOffset" );
365 if( !stream.read( _baseDepth ) ) MK_THROW( "Failed to read _baseDepth" );
366 _tree.read( stream , nodeAllocator );
367 _init();
368 _sNodes.read( stream , _tree );
369 }
370
371 template< unsigned int Dim , class Real > void FEMTree< Dim , Real >::write( BinaryStream &stream , bool serialize ) const
372 {
373 node_index_type nodeCount = _nodeCount;
374 stream.write( nodeCount );
375 stream.write( _maxDepth );
376 stream.write( _depthOffset );
377 stream.write( _baseDepth );
378 _tree.write( stream , serialize );
379 _sNodes.write( stream );
380 }
381
382 template< unsigned int Dim , class Real >
383 const RegularTreeNode< Dim , FEMTreeNodeData , depth_and_offset_type >* FEMTree< Dim , Real >::leaf( Point< Real , Dim > p ) const
384 {
385 if( !_InBounds( p ) ) return NULL;
386 Point< Real , Dim > center;
387 for( int d=0 ; d<Dim ; d++ ) center[d] = (Real)0.5;
388 Real width = Real(1.0);
389 FEMTreeNode* node = _spaceRoot;
390 while( node->children )
391 {
392 int cIndex = FEMTreeNode::ChildIndex( center , p );
393 node = node->children + cIndex;
394 width /= 2;
395 for( int d=0 ; d<Dim ; d++ )
396 if( (cIndex>>d) & 1 ) center[d] += width/2;
397 else center[d] -= width/2;
398 }
399 return node;
400 }
401 template< unsigned int Dim , class Real >
402 template< bool ThreadSafe >
403 RegularTreeNode< Dim , FEMTreeNodeData , depth_and_offset_type >* FEMTree< Dim , Real >::_leaf( Allocator< FEMTreeNode > *nodeAllocator , Point< Real , Dim > p , LocalDepth maxDepth )
404 {
405 if( !_InBounds( p ) ) return NULL;
406 Point< Real , Dim > center;
407 for( int d=0 ; d<Dim ; d++ ) center[d] = (Real)0.5;
408 Real width = Real(1.0);
409 FEMTreeNode* node = _spaceRoot;
410 LocalDepth d = _localDepth( node );
411 while( ( d<0 && node->children ) || ( d>=0 && d<maxDepth ) )
412 {
413 if( !node->children ) node->template initChildren< ThreadSafe >( nodeAllocator , _nodeInitializer );
414 int cIndex = FEMTreeNode::ChildIndex( center , p );
415 node = node->children + cIndex;
416 d++;
417 width /= 2;
418 for( int d=0 ; d<Dim ; d++ )
419 if( (cIndex>>d) & 1 ) center[d] += width/2;
420 else center[d] -= width/2;
421 }
422 return node;
423 }
424
425 template< unsigned int Dim , class Real >
426 template< bool ThreadSafe >
427 RegularTreeNode< Dim , FEMTreeNodeData , depth_and_offset_type >* FEMTree< Dim , Real >::_leaf( Allocator< FEMTreeNode > *nodeAllocator , Point< Real , Dim > p , std::function< int ( Point< Real , Dim > ) > &pointDepthFunctor )
428 {
429 if( !_InBounds( p ) ) return NULL;
430 int maxDepth = pointDepthFunctor( p );
431 Point< Real , Dim > center;
432 for( int d=0 ; d<Dim ; d++ ) center[d] = (Real)0.5;
433 Real width = Real(1.0);
434 FEMTreeNode* node = _spaceRoot;
435 LocalDepth depth = 0;
436 int cIndex = FEMTreeNode::ChildIndex( center , p );
437
438 while( depth<maxDepth )
439 {
440 if( !node->children ) node->template initChildren< ThreadSafe >( nodeAllocator , _nodeInitializer );
441 int cIndex = FEMTreeNode::ChildIndex( center , p );
442 node = node->children + cIndex;
443
444 depth++;
445 width /= 2;
446 for( int d=0 ; d<Dim ; d++ )
447 if( (cIndex>>d) & 1 ) center[d] += width/2;
448 else center[d] -= width/2;
449 }
450 return node;
451 }
452
453 template< unsigned int Dim , class Real > bool FEMTree< Dim , Real >::_InBounds( Point< Real , Dim > p ){ for( int d=0 ; d<Dim ; d++ ) if( p[d]<0 || p[d]>1 ) return false ; return true; }
454 template< unsigned int Dim , class Real >
455 template< unsigned int ... FEMSignatures >
456 bool FEMTree< Dim , Real >::isValidFEMNode( UIntPack< FEMSignatures ... > , const FEMTreeNode* node ) const
457 {
458 if( GetGhostFlag< Dim >( node ) ) return false;
459 LocalDepth d ; LocalOffset off ; _localDepthAndOffset( node , d , off );
460 if( d<0 ) return false;
461 return FEMIntegrator::IsValidFEMNode( UIntPack< FEMSignatures ... >() , d , off );
462 }
463 template< unsigned int Dim , class Real >
464 bool FEMTree< Dim , Real >::isValidSpaceNode( const FEMTreeNode* node ) const
465 {
466 if( !node ) return false;
467 LocalDepth d ; LocalOffset off ; _localDepthAndOffset( node , d , off );
468 if( d<0 ) return false;
469 int res = 1<<d;
470 for( int dd=0 ; dd<Dim ; dd++ ) if( off[dd]<0 || off[dd]>=res ) return false;
471 return true;
472 }
473
474 template< unsigned int Dim , class Real >
475 template< bool ThreadSafe , typename AddNodeFunctor , unsigned int ... Degrees >
476 void FEMTree< Dim , Real >::_refine( UIntPack< Degrees ... > , Allocator< FEMTreeNode > *nodeAllocator , const AddNodeFunctor &addNodeFunctor , FEMTreeNode *node )
477 {
478 LocalDepth d , _d ; LocalOffset off , _off;
479 _localDepthAndOffset( node , d , off );
480 _d = d+1;
481
482 bool refine = d<0;
483 for( int c=0 ; c<(1<<Dim) ; c++ )
484 {
485 for( int d=0 ; d<Dim ; d++ )
486 if( c&(1<<d) ) _off[d] = off[d]*2+1;
487 else _off[d] = off[d]*2+0;
488 refine |= !FEMIntegrator::IsOutOfBounds( UIntPack< FEMDegreeAndBType< Degrees , BOUNDARY_FREE >::Signature ... >() , _d , _off ) && addNodeFunctor( _d , _off );
489 }
490 if( refine )
491 {
492 if( !node->children ) node->template initChildren< ThreadSafe >( nodeAllocator , _nodeInitializer );
493 for( int c=0 ; c<(1<<Dim) ; c++ ) _refine< ThreadSafe >( UIntPack< Degrees ... >() , nodeAllocator , addNodeFunctor , node->children+c );
494 }
495 }
496
497 template< unsigned int Dim , class Real >
498 template< bool ThreadSafe , typename AddNodeFunctor , unsigned int ... Degrees >
499 void FEMTree< Dim , Real >::_refine( UIntPack< Degrees ... > , Allocator< FEMTreeNode > *nodeAllocator , const AddNodeFunctor &addNodeFunctor )
500 {
501 if( !_tree.children ) _tree.template initChildren< ThreadSafe >( nodeAllocator , _nodeInitializer );
502 for( int c=0 ; c<(1<<Dim) ; c++ ) _refine< ThreadSafe >( UIntPack< Degrees ... >() , nodeAllocator , addNodeFunctor , _tree.children+c );
503 }
504
505 template< unsigned int Dim , class Real >
506 template< bool ThreadSafe , unsigned int ... Degrees >
507 void FEMTree< Dim , Real >::_setFullDepth( UIntPack< Degrees ... > , Allocator< FEMTreeNode > *nodeAllocator , FEMTreeNode* node , LocalDepth depth )
508 {
509 LocalDepth d ; LocalOffset off;
510 _localDepthAndOffset( node , d , off );
511 bool refine = d<depth && ( d<0 || !FEMIntegrator::IsOutOfBounds( UIntPack< FEMDegreeAndBType< Degrees , BOUNDARY_FREE >::Signature ... >() , d , off ) );
512 if( refine )
513 {
514 if( !node->children ) node->template initChildren< ThreadSafe >( nodeAllocator , _nodeInitializer );
515 for( int c=0 ; c<(1<<Dim) ; c++ ) _setFullDepth< ThreadSafe >( UIntPack< Degrees ... >() , nodeAllocator , node->children+c , depth );
516 }
517 }
518
519 template< unsigned int Dim , class Real >
520 template< bool ThreadSafe , unsigned int ... Degrees >
521 void FEMTree< Dim , Real >::_setFullDepth( UIntPack< Degrees ... > , Allocator< FEMTreeNode > *nodeAllocator , LocalDepth depth )
522 {
523 if( !_tree.children ) _tree.template initChildren< ThreadSafe >( nodeAllocator , _nodeInitializer );
524 for( int c=0 ; c<(1<<Dim) ; c++ ) _setFullDepth< ThreadSafe >( UIntPack< Degrees ... >() , nodeAllocator , _tree.children+c , depth );
525 }
526
527 template< unsigned int Dim , class Real >
528 template< unsigned int ... Degrees >
529 typename FEMTree< Dim , Real >::LocalDepth FEMTree< Dim , Real >::_getFullDepth( UIntPack< Degrees ... > , const FEMTreeNode* node ) const
530 {
531 LocalDepth d ; LocalOffset off;
532 _localDepthAndOffset( node , d , off );
533 bool refine = d<0 || !FEMIntegrator::IsOutOfBounds( UIntPack< FEMDegreeAndBType< Degrees , BOUNDARY_FREE >::Signature ... >() , d , off );
534
535 if( refine )
536 {
537 if( !node->children ) return d;
538 else
539 {
540 LocalDepth depth = INT_MAX;
541 for( int c=0 ; c<(1<<Dim) ; c++ )
542 {
543 LocalDepth d = _getFullDepth( UIntPack< Degrees ... >() , node->children+c );
544 if( d<depth ) depth = d;
545 }
546 return depth;
547 }
548 }
549 else return INT_MAX;
550 }
551
552 template< unsigned int Dim , class Real >
553 template< unsigned int ... Degrees >
554 typename FEMTree< Dim , Real >::LocalDepth FEMTree< Dim , Real >::_getFullDepth( UIntPack< Degrees ... > , const LocalDepth depth , const LocalOffset begin , const LocalOffset end , const FEMTreeNode* node ) const
555 {
556 LocalDepth d ; LocalOffset off;
557 _localDepthAndOffset( node , d , off );
558
559 // [NOTE]: Changing closed interval to half open interval
560 const int StartOffsets[] = { BSplineSupportSizes< Degrees >::SupportStart ... };
561 const int EndOffsets[] = { BSplineSupportSizes< Degrees >::SupportEnd + 1 ... };
562
563 auto IsSupported = [&]( LocalDepth d , LocalOffset off )
564 {
565 if( d>depth ) return false;
566 LocalOffset supportStart , supportEnd;
567 for( unsigned int dim=0 ; dim<Dim ; dim++ )
568 {
569 supportStart[dim] = ( off[dim] + StartOffsets[dim] )*(1<<(depth-d));
570 supportEnd [dim] = ( off[dim] + EndOffsets[dim] )*(1<<(depth-d));
571 }
572 if( d>=0 ) for( unsigned int dim=0 ; dim<Dim ; dim++ ) if( supportStart[dim]>=end[dim] || supportEnd[dim]<=begin[dim] ) return false;
573 return true;
574 };
575
576 {
577 if( !node->children )
578 {
579 LocalDepth _d=d+1;
580 LocalOffset _off;
581 bool childrenSupported = false;
582 for( unsigned int c=0 ; c<(1<<Dim) ; c++ )
583 {
584 for( int dim=0 ; dim<Dim ; dim++ )
585 #ifdef SANITIZED_PR
586 if( c&(1<<dim) ) _off[dim] = off[dim]*2 + 1;
587 else _off[dim] = off[dim]*2 + 0;
588 #else // !SANITIZED_PR
589 if( c&(1<<dim) ) _off[dim] = (off[dim]<<1) | 1;
590 else _off[dim] = (off[dim]<<1);
591 #endif // SANITIZED_PR
592 childrenSupported |= IsSupported( _d , _off );
593 }
594 if( childrenSupported ) return d;
595 else return INT_MAX;
596 }
597 else
598 {
599 LocalDepth minDepth = INT_MAX;
600 for( int c=0 ; c<(1<<Dim) ; c++ )
601 {
602 LocalDepth d = _getFullDepth( UIntPack< Degrees ... >() , depth , begin , end , node->children+c );
603 if( d<minDepth ) minDepth = d;
604 }
605 return minDepth;
606 }
607 }
608 }
609
610 template< unsigned int Dim , class Real >
611 template< unsigned int ... Degrees >
612 typename FEMTree< Dim , Real >::LocalDepth FEMTree< Dim , Real >::getFullDepth( UIntPack< Degrees ... > ) const
613 {
614 if( !_tree.children ) return -1;
615 LocalDepth depth = INT_MAX;
616 for( int c=0 ; c<(1<<Dim) ; c++ )
617 {
618 LocalDepth d = _getFullDepth( UIntPack< Degrees ... >() , _tree.children+c );
619 if( d<depth ) depth = d;
620 }
621 return depth;
622 }
623
624 template< unsigned int Dim , class Real >
625 template< unsigned int ... Degrees >
626 typename FEMTree< Dim , Real >::LocalDepth FEMTree< Dim , Real >::getFullDepth( UIntPack< Degrees ... > , const LocalDepth depth , const LocalOffset begin , const LocalOffset end ) const
627 {
628 // [NOTE] Need "+1" because _getFullDepth will test children of leaves
629 LocalDepth maxDepth = this->maxDepth() + 1;
630 LocalDepth _depth ; LocalOffset _begin , _end;
631 for( unsigned int d=0 ; d<Dim ; d++ )
632 {
633 if( begin[d]>end[d] ) MK_THROW( "Bad bounds [" , d , "]: " , begin[d] , " <= " , end[d] );
634 if( begin[d]<0 ) MK_THROW( "Start bound cannot be negative [" , d , "]: 0 <= " , begin[d] );
635 if( end[d]>(1<<depth) ) MK_THROW( "End bound cannot exceed resolution [" , d , "]: " , end[d] , " <=" , (1<<depth) );
636
637 // Push to max depth
638 if( depth<maxDepth )
639 {
640 _begin[d] = begin[d]<<(maxDepth-depth);
641 _end [d] = end [d]<<(maxDepth-depth);
642 }
643 else
644 {
645 _begin[d] = begin[d];
646 _end [d] = end [d];
647 }
648 }
649 if( depth<maxDepth ) _depth = maxDepth;
650 else _depth = depth;
651
652 if( !_tree.children ) return -1;
653
654 LocalDepth minDepth = INT_MAX;
655 for( int c=0 ; c<(1<<Dim) ; c++ )
656 {
657 LocalDepth d = _getFullDepth( UIntPack< Degrees ... >() , _depth , _begin , _end , _tree.children+c );
658 if( d<minDepth ) minDepth = d;
659 }
660
661 return minDepth;
662 }
663
664 template< unsigned int Dim , class Real >
665 template< unsigned int LeftRadius , unsigned int RightRadius , bool CreateNodes , typename ProcessingNodeFunctor , typename ... DenseOrSparseNodeData , typename InitializeFunctor >
666 void FEMTree< Dim , Real >::processNeighbors( ProcessingNodeFunctor processingNode , std::tuple< DenseOrSparseNodeData *... > data , InitializeFunctor initialize )
667 {
668 std::vector< FEMTreeNode * > nodes;
669 nodes.reserve( _spaceRoot->nodes() );
670 _spaceRoot->processNodes( [&]( FEMTreeNode *node ){ if( processingNode(node) ) nodes.push_back( node ); } );
671 processNeighbors< LeftRadius , RightRadius , CreateNodes >( &nodes[0] , nodes.size() , data , initialize );
672 }
673
674 template< unsigned int Dim , class Real >
675 template< unsigned int LeftRadius , unsigned int RightRadius , bool CreateNodes , typename ... DenseOrSparseNodeData , typename InitializeFunctor >
676 void FEMTree< Dim , Real >::processNeighbors( FEMTreeNode **nodes , size_t nodeCount, std::tuple< DenseOrSparseNodeData *... > data , InitializeFunctor initialize )
677 {
678 int maxDepth = 0;
679 for( size_t i=0 ; i<nodeCount ; i++ ) maxDepth = std::max( maxDepth , nodes[i]->depth() );
680 std::vector< node_index_type > map( _nodeCount );
681 for( node_index_type i=0 ; i<_nodeCount ; i++ ) map[i] = i;
682 typedef typename RegularTreeNode< Dim , FEMTreeNodeData , depth_and_offset_type >::template NeighborKey< IsotropicUIntPack< Dim , LeftRadius > , IsotropicUIntPack< Dim , RightRadius > > NeighborKey;
683
684 Allocator< FEMTreeNode > *nodeAllocator = nodeAllocators.size() ? nodeAllocators[0] : NULL;
685 // typename RegularTreeNode< Dim , FEMTreeNodeData , depth_and_offset_type >::template NeighborKey< IsotropicUIntPack< Dim , LeftRadius > , IsotropicUIntPack< Dim , RightRadius > > neighborKey;
686 NeighborKey neighborKey;
687 neighborKey.set( maxDepth );
688 for( size_t i=0 ; i<nodeCount ; i++ )
689 {
690 auto neighbors = neighborKey.template getNeighbors< CreateNodes , false >( nodes[i] , nodeAllocator , _nodeInitializer );
691 for( unsigned int j=0 ; j<neighbors.neighbors.Size() ; j++ ) if( neighbors.neighbors.data[j] ) initialize( neighbors.neighbors.data[j] );
692 }
693
694 _reorderDenseOrSparseNodeData< 0 >( GetPointer( map ) , _nodeCount , data );
695 }
696
697 template< unsigned int Dim , class Real >
698 template< unsigned int LeftRadius , unsigned int RightRadius , typename IsProcessingNodeFunctor , typename ProcessingKernel >
699 void FEMTree< Dim , Real >::processNeighboringLeaves( IsProcessingNodeFunctor isProcessingNode , ProcessingKernel kernel , bool processSubTree )
700 {
701 std::vector< FEMTreeNode * > nodes;
702 nodes.reserve( _spaceRoot->nodes() );
703 _spaceRoot->processNodes( [&]( FEMTreeNode *node ){ if( isProcessingNode(node) ) nodes.push_back( node ); } );
704 processNeighboringLeaves< LeftRadius , RightRadius >( &nodes[0] , nodes.size() , kernel , processSubTree );
705 }
706
707 template< unsigned int Dim , class Real >
708 template< unsigned int LeftRadius , unsigned int RightRadius , typename ProcessingKernel >
709 void FEMTree< Dim , Real >::processNeighboringLeaves( FEMTreeNode **nodes , size_t nodeCount , ProcessingKernel kernel , bool processSubTree )
710 {
711 #ifdef SHOW_WARNINGS
712 #pragma message( "[WARNING] may process the same leaf multiple times, if it is coarser" )
713 #endif // SHOW_WARNINGS
714 typedef typename RegularTreeNode< Dim , FEMTreeNodeData , depth_and_offset_type >::template NeighborKey< IsotropicUIntPack< Dim , LeftRadius > , IsotropicUIntPack< Dim , RightRadius > > NeighborKey;
715 // Suppose that we have a node at index I and we want the leaf nodes supported on the (possibly virtual) node K away
716 // Case 1: The K-th neighbor exists
717 // ---> Iterate over the leaf nodes of the sub-tree rooted at the K-th neighbor
718 // Case 2: The K-th neighbor does not exist
719 // ---> The index of the K-th neighbor is I+K
720 // ---> The index of the parent is floor( I/2 )
721 // ---> The index of the K-th neighbors parent is floor( (I+K)/2 )
722 // ---> The parent of the K-th neighbor is the [ floor( (I+k)/2 ) - floor( I/2 ) ]-th neighbor of the parent
723
724 std::function< void ( FEMTreeNode * ) > ProcessSubTree = [&]( FEMTreeNode *node )
725 {
726 if( node->children ) for( int c=0 ; c<(1<<Dim) ; c++ ) ProcessSubTree( node->children+c );
727 kernel( node );
728 };
729
730 unsigned int maxDepth=0;
731 for( size_t i=0 ; i<nodeCount ; i++ ) maxDepth = std::max( maxDepth , (unsigned int)nodes[i]->depth() );
732
733 std::vector< NeighborKey > neighborKeys( ThreadPool::NumThreads() );
734 for( int i=0 ; i<neighborKeys.size() ; i++ ) neighborKeys[i].set( maxDepth );
735
736 ThreadPool::ParallelFor( 0 , nodeCount , [&]( unsigned int t , size_t i )
737 {
738 typedef StaticWindow< FEMTreeNode * , IsotropicUIntPack< Dim , LeftRadius+RightRadius+1 > > NeighborLeafNodes;
739 NeighborLeafNodes neighborLeafNodes;
740 neighborKeys[t].setLeafNeighbors( nodes[i] , neighborLeafNodes );
741 for( unsigned int i=0 ; i<NeighborLeafNodes::Size() ; i++ ) if( neighborLeafNodes.data[i] )
742 if( processSubTree ) ProcessSubTree( neighborLeafNodes.data[i] );
743 else kernel( neighborLeafNodes.data[i] );
744 } );
745 }
746
747 template< unsigned int Dim , class Real >
748 template< class C , unsigned int ... FEMSigs >
749 DenseNodeData< C , UIntPack< FEMSigs ... > > FEMTree< Dim , Real >::trimToDepth( const DenseNodeData< C , UIntPack< FEMSigs ... > >& data , LocalDepth coarseDepth ) const
750 {
751 if( coarseDepth>_maxDepth ) return data;
752 return data._trim( _sNodesEnd( coarseDepth ) );
753 }
754
755 template< unsigned int Dim , class Real >
756 template< class C , unsigned int ... FEMSigs >
757 SparseNodeData< C , UIntPack< FEMSigs ... > > FEMTree< Dim , Real >::trimToDepth( const SparseNodeData< C , UIntPack< FEMSigs ... > >& data , LocalDepth coarseDepth ) const
758 {
759 if( coarseDepth>_maxDepth ) return data;
760 return data._trim( _sNodesEnd( coarseDepth ) );
761 }
762
763 template< unsigned int Dim , class Real >
764 template< typename T , unsigned int PointD , typename ConstraintDual , typename SystemDual >
765 typename FEMTree< Dim , Real >::template ApproximatePointInterpolationInfo< T , PointD , ConstraintDual , SystemDual > FEMTree< Dim , Real >::trimToDepth( const ApproximatePointInterpolationInfo< T , PointD , ConstraintDual , SystemDual > &iInfo , LocalDepth coarseDepth ) const
766 {
767 ApproximatePointInterpolationInfo< T , PointD , ConstraintDual , SystemDual > _iInfo;
768 if( coarseDepth<_maxDepth ) _iInfo.iData = iInfo.iData._trim( _sNodesEnd( coarseDepth ) );
769 else _iInfo.iData = iInfo.iData;
770 _iInfo._constrainsDCTerm = iInfo._constrainsDCTerm;
771 _iInfo._constraintDual = iInfo._constraintDual;
772 _iInfo._systemDual = iInfo._systemDual;
773 return _iInfo;
774 }
775
776 template< unsigned int Dim , class Real >
777 template< typename T , typename Data , unsigned int PointD , typename ConstraintDual , typename SystemDual >
778 typename FEMTree< Dim , Real >::template ApproximatePointAndDataInterpolationInfo< T , Data , PointD , ConstraintDual , SystemDual > FEMTree< Dim , Real >::trimToDepth( const ApproximatePointAndDataInterpolationInfo< T , Data , PointD , ConstraintDual , SystemDual > &iInfo , LocalDepth coarseDepth ) const
779 {
780 ApproximatePointAndDataInterpolationInfo< T , Data , PointD , ConstraintDual , SystemDual > _iInfo;
781 if( coarseDepth<_maxDepth ) _iInfo.iData = iInfo.iData._trim( _sNodesEnd( coarseDepth ) );
782 else _iInfo.iData = iInfo.iData;
783 _iInfo._constrainsDCTerm = iInfo._constrainsDCTerm;
784 _iInfo._constraintDual = iInfo._constraintDual;
785 _iInfo._systemDual = iInfo._systemDual;
786 return _iInfo;
787 }
788
789 template< unsigned int Dim , class Real >
790 template< unsigned int CoDim , unsigned int DensityDegree >
791 void FEMTree< Dim , Real >::updateDensityEstimator( typename FEMTree< Dim , Real >::template DensityEstimator< DensityDegree > &density , const std::vector< PointSample >& samples , LocalDepth minSplatDepth , LocalDepth maxSplatDepth )
792 {
793 // typename FEMTreeNode::SubTreeExtractor subtreeExtractor( _spaceRoot );
794 SubTreeExtractor subtreeExtractor( _spaceRoot , _depthOffset );
795 Allocator< FEMTreeNode > *nodeAllocator = nodeAllocators.size() ? nodeAllocators[0] : NULL;
796 LocalDepth maxDepth = _spaceRoot->maxDepth();
797 maxSplatDepth = std::max< LocalDepth >( 0 , std::min< LocalDepth >( maxSplatDepth , maxDepth ) );
798 minSplatDepth = std::max< LocalDepth >( 0 , std::min< LocalDepth >( minSplatDepth , maxDepth ) );
799 if( minSplatDepth>maxSplatDepth ) MK_THROW( "Minimum splat depth exceeds maximum splat depth" );
800 PointSupportKey< IsotropicUIntPack< Dim , DensityDegree > > densityKey;
801 densityKey.set( maxSplatDepth );
802
803 std::vector< node_index_type > sampleMap( nodeCount() , -1 );
804
805 // Initialize the map from node indices to samples
806 ThreadPool::ParallelFor( 0 , samples.size() , [&]( unsigned int , size_t i ){ if( samples[i].sample.weight>0 ) sampleMap[ samples[i].node->nodeData.nodeIndex ] = (node_index_type)i; } );
807
808 std::function< ProjectiveData< Point< Real , Dim > , Real > ( FEMTreeNode* ) > SetDensity = [&] ( FEMTreeNode* node )
809 {
810 ProjectiveData< Point< Real , Dim > , Real > sample;
811 LocalDepth d = node->depth();
812 node_index_type idx = node->nodeData.nodeIndex;
813 if( node->children ) for( int c=0 ; c<(1<<Dim) ; c++ ) sample += SetDensity( node->children + c );
814 if( idx<(node_index_type)sampleMap.size() && sampleMap[idx]!=-1 ) sample += samples[ sampleMap[ idx ] ].sample;
815 if( d>=minSplatDepth && d<=maxSplatDepth && sample.weight>0 ) _addWeightContribution< true , CoDim >( nodeAllocator , density , node , sample.data / sample.weight , densityKey , sample.weight );
816 return sample;
817 };
818 SetDensity( _spaceRoot );
819 }
820
821 template< unsigned int Dim , class Real >
822 template< unsigned int CoDim , unsigned int DensityDegree >
823 void FEMTree< Dim , Real >::updateDensityEstimator( typename FEMTree< Dim , Real >::template DensityEstimator< DensityDegree > &density , const std::vector< PointSample >& samples , LocalDepth minSplatDepth , LocalDepth maxSplatDepth , std::function< int ( Point< Real , Dim > ) > pointDepthFunctor )
824 {
825 // typename FEMTreeNode::SubTreeExtractor subtreeExtractor( _spaceRoot );
826 SubTreeExtractor subtreeExtractor( _spaceRoot , _depthOffset );
827 Allocator< FEMTreeNode > *nodeAllocator = nodeAllocators.size() ? nodeAllocators[0] : NULL;
828 LocalDepth maxDepth = _spaceRoot->maxDepth();
829 maxSplatDepth = std::max< LocalDepth >( 0 , std::min< LocalDepth >( maxSplatDepth , maxDepth ) );
830 minSplatDepth = std::max< LocalDepth >( 0 , std::min< LocalDepth >( minSplatDepth , maxDepth ) );
831 if( minSplatDepth>maxSplatDepth ) MK_THROW( "Minimum splat depth exceeds maximum splat depth" );
832 PointSupportKey< IsotropicUIntPack< Dim , DensityDegree > > densityKey;
833 densityKey.set( maxSplatDepth );
834
835 std::vector< node_index_type > sampleMap( nodeCount() , -1 );
836
837 // Initialize the map from node indices to samples
838 ThreadPool::ParallelFor( 0 , samples.size() , [&]( unsigned int , size_t i ){ if( samples[i].sample.weight>0 ) sampleMap[ samples[i].node->nodeData.nodeIndex ] = (node_index_type)i; } );
839
840 std::function< ProjectiveData< Point< Real , Dim > , Real > ( FEMTreeNode* ) > SetDensity = [&] ( FEMTreeNode* node )
841 {
842 ProjectiveData< Point< Real , Dim > , Real > sample;
843 LocalDepth d = node->depth();
844 node_index_type idx = node->nodeData.nodeIndex;
845 if( node->children ) for( int c=0 ; c<(1<<Dim) ; c++ ) sample += SetDensity( node->children + c );
846 if( idx<(node_index_type)sampleMap.size() && sampleMap[idx]!=-1 ) sample += samples[ sampleMap[ idx ] ].sample;
847
848 if( d>=minSplatDepth && d<=maxSplatDepth && sample.weight>0 )
849 {
850 // The average position of samples accumulated in the node
851 Point< Real , Dim > p = sample.data / sample.weight;
852 if( d<=pointDepthFunctor( p ) ) _addWeightContribution< true , CoDim >( nodeAllocator , density , node , p , densityKey , sample.weight );
853 }
854 return sample;
855 };
856 SetDensity( _spaceRoot );
857 }
858
859 template< unsigned int Dim , class Real >
860 template< unsigned int CoDim , unsigned int DensityDegree >
861 typename FEMTree< Dim , Real >::template DensityEstimator< DensityDegree >* FEMTree< Dim , Real >::setDensityEstimator( const std::vector< PointSample >& samples , LocalDepth splatDepth , Real samplesPerNode )
862 {
863 LocalDepth maxDepth = _spaceRoot->maxDepth();
864 splatDepth = std::max< LocalDepth >( 0 , std::min< LocalDepth >( splatDepth , maxDepth ) );
865 DensityEstimator< DensityDegree > *density = new DensityEstimator< DensityDegree >( splatDepth , CoDim , samplesPerNode );
866 this->template updateDensityEstimator< CoDim , DensityDegree >( *density , samples , 0 , splatDepth );
867
868 return density;
869 }
870
871 template< unsigned int Dim , class Real >
872 template< unsigned int CoDim , unsigned int DensityDegree >
873 typename FEMTree< Dim , Real >::template DensityEstimator< DensityDegree >* FEMTree< Dim , Real >::setDensityEstimator( const std::vector< PointSample >& samples , LocalDepth splatDepth , std::function< int ( Point< Real , Dim > ) > pointDepthFunctor , Real samplesPerNode )
874 {
875 LocalDepth maxDepth = _spaceRoot->maxDepth();
876 splatDepth = std::max< LocalDepth >( 0 , std::min< LocalDepth >( splatDepth , maxDepth ) );
877 DensityEstimator< DensityDegree > *density = new DensityEstimator< DensityDegree >( splatDepth , CoDim , samplesPerNode );
878 this->template updateDensityEstimator< CoDim , DensityDegree >( *density , samples , 0 , splatDepth , pointDepthFunctor );
879
880 return density;
881 }
882
883 template< unsigned int Dim , class Real >
884 template< unsigned int ... DataSigs , unsigned int DensityDegree , class InData , class OutData >
885 SparseNodeData< OutData , UIntPack< DataSigs ... > > FEMTree< Dim , Real >::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 )
886 {
887 std::function< bool ( InData , OutData & , Real & ) > ConversionAndBiasFunction = [&]( InData in , OutData &out , Real &bias )
888 {
889 if( ConversionFunction( in , out ) )
890 {
891 bias = BiasFunction( in );
892 return true;
893 }
894 else return false;
895 };
896 return setInterpolatedDataField( zero , UIntPack< DataSigs ... >() , samples , data , density , minDepth , maxDepth , minDepthCutoff , pointDepthAndWeight , ConversionAndBiasFunction );
897 }
898
899 template< unsigned int Dim , class Real >
900 template< unsigned int ... DataSigs , unsigned int DensityDegree , class InData , class OutData >
901 SparseNodeData< OutData , UIntPack< DataSigs ... > > FEMTree< Dim , Real >::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 )
902 {
903 // typename FEMTreeNode::SubTreeExtractor subtreeExtractor( _spaceRoot );
904 SubTreeExtractor subtreeExtractor( _spaceRoot , _depthOffset );
905
906 typedef PointSupportKey< IsotropicUIntPack< Dim , DensityDegree > > DensityKey;
907 typedef UIntPack< FEMSignature< DataSigs >::Degree ... > DataDegrees;
908 typedef PointSupportKey< UIntPack< FEMSignature< DataSigs >::Degree ... > > DataKey;
909 std::vector< DensityKey > densityKeys( ThreadPool::NumThreads() );
910 std::vector< DataKey > dataKeys( ThreadPool::NumThreads() );
911 bool oneKey = DensityDegree==DataDegrees::Min() && DensityDegree==DataDegrees::Max();
912 for( size_t i=0 ; i<densityKeys.size() ; i++ ) densityKeys[i].set( _localToGlobal( maxDepth ) );
913 if( !oneKey ) for( size_t i=0 ; i<dataKeys.size() ; i++ ) dataKeys[i].set( _localToGlobal( maxDepth ) );
914 std::vector< Real > depthAndWeightSums( ThreadPool::NumThreads() , 0 );
915 pointDepthAndWeight.data = Point< Real , 2 >();
916 pointDepthAndWeight.weight = 0;
917 SparseNodeData< OutData , UIntPack< DataSigs ... > > dataField;
918 std::vector< Point< Real , 2 > > pointDepthAndWeightSums( ThreadPool::NumThreads() , Point< Real , 2 >() );
919 ThreadPool::ParallelFor( 0 , samples.size() , [&]( unsigned int thread , size_t i )
920 {
921 DensityKey& densityKey = densityKeys[ thread ];
922 DataKey& dataKey = dataKeys[ thread ];
923 const ProjectiveData< Point< Real , Dim > , Real >& sample = samples[i].sample;
924 if( sample.weight>0 )
925 {
926 Point< Real , Dim > p = sample.data / sample.weight;
927 InData in = data[i] / sample.weight;
928 OutData out;
929
930 Real depthBias;
931 if( !_InBounds(p) ) MK_WARN( "Point sample is out of bounds" );
932 else if( ConversionAndBiasFunction( in , out , depthBias ) )
933 {
934 depthAndWeightSums[thread] += sample.weight;
935 #ifdef SHOW_WARNINGS
936 #pragma message( "[WARNING] Should the next line be commented out?" )
937 #endif // SHOW_WARNINGS
938 out *= sample.weight;
939 Allocator< FEMTreeNode > *nodeAllocator = nodeAllocators.size() ? nodeAllocators[ thread ] : NULL;
940 #if defined( __GNUC__ ) && __GNUC__ < 5
941 #ifdef SHOW_WARNINGS
942 #warning "you've got me gcc version<5"
943 #endif // SHOW_WARNINGS
944 if( density ) pointDepthAndWeightSums[thread] += _splatPointData< true , true , DensityDegree , OutData >( zero , nodeAllocator , *density , minDepthCutoff , p , out , dataField , densityKey , oneKey ? *( (DataKey*)&densityKey ) : dataKey , minDepth , maxDepth , Dim , depthBias ) * sample.weight;
945 #else // !__GNUC__ || __GNUC__ >=5
946 if( density ) pointDepthAndWeightSums[thread] += _splatPointData< true , true , DensityDegree , OutData , DataSigs ... >( zero , nodeAllocator , *density , minDepthCutoff , p , out , dataField , densityKey , oneKey ? *( (DataKey*)&densityKey ) : dataKey , minDepth , maxDepth , Dim , depthBias ) * sample.weight;
947 #endif // __GNUC__ && __GNUC__ < 5
948 else
949 {
950 Real width = (Real)( 1.0 / ( 1<<maxDepth ) );
951 #if defined( __GNUC__ ) && __GNUC__ < 5
952 #ifdef SHOW_WARNINGS
953 #warning "you've got me gcc version<5"
954 #endif // SHOW_WARNINGS
955 _splatPointData< true , true , OutData >( zero , nodeAllocator , _leaf< true >( nodeAllocator , p , maxDepth ) , p , out / (Real)pow( width , Dim ) , dataField , oneKey ? *( (DataKey*)&densityKey ) : dataKey );
956 #else // !__GNUC__ || __GNUC__ >=5
957 _splatPointData< true , true , OutData , DataSigs ... >( zero , nodeAllocator , _leaf< true >( nodeAllocator , p , maxDepth ) , p , out / (Real)pow( width , Dim ) , dataField , oneKey ? *( (DataKey*)&densityKey ) : dataKey );
958 #endif // __GNUC__ && __GNUC__ < 5
959 pointDepthAndWeightSums[thread] += Point< Real , 2 >( (Real)1. , (Real)maxDepth ) * sample.weight;
960 }
961 }
962 }
963 }
964 );
965 pointDepthAndWeight.data = Point< Real , 2 >();
966 for( unsigned int i=0 ; i<depthAndWeightSums.size() ; i++ ) pointDepthAndWeight.data += pointDepthAndWeightSums[i];
967 pointDepthAndWeight.weight = 0;
968 for( unsigned int i=0 ; i<depthAndWeightSums.size() ; i++ ) pointDepthAndWeight.weight += depthAndWeightSums[i];
969 return dataField;
970 }
971
972 template< unsigned int Dim , class Real >
973 template< unsigned int ... DataSigs , unsigned int DensityDegree , class InData , class OutData >
974 SparseNodeData< OutData , UIntPack< DataSigs ... > > FEMTree< Dim , Real >::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 )
975 {
976 std::function< bool ( InData , OutData & , Real & ) > ConversionAndBiasFunction = [&]( InData in , OutData &out , Real &bias )
977 {
978 if( ConversionFunction( in , out ) )
979 {
980 bias = BiasFunction( in );
981 return true;
982 }
983 else return false;
984 };
985 return setInterpolatedDataField( zero , UIntPack< DataSigs ... >() , samples , data , density , minDepth , maxDepth , pointDepthFunctor , minDepthCutoff , pointDepthAndWeight , ConversionAndBiasFunction );
986 }
987
988 template< unsigned int Dim , class Real >
989 template< unsigned int ... DataSigs , unsigned int DensityDegree , class InData , class OutData >
990 SparseNodeData< OutData , UIntPack< DataSigs ... > > FEMTree< Dim , Real >::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 )
991 {
992 // typename FEMTreeNode::SubTreeExtractor subtreeExtractor( _spaceRoot );
993 SubTreeExtractor subtreeExtractor( _spaceRoot , _depthOffset );
994
995 typedef PointSupportKey< IsotropicUIntPack< Dim , DensityDegree > > DensityKey;
996 typedef UIntPack< FEMSignature< DataSigs >::Degree ... > DataDegrees;
997 typedef PointSupportKey< UIntPack< FEMSignature< DataSigs >::Degree ... > > DataKey;
998 std::vector< DensityKey > densityKeys( ThreadPool::NumThreads() );
999 std::vector< DataKey > dataKeys( ThreadPool::NumThreads() );
1000 bool oneKey = DensityDegree==DataDegrees::Min() && DensityDegree==DataDegrees::Max();
1001 for( size_t i=0 ; i<densityKeys.size() ; i++ ) densityKeys[i].set( _localToGlobal( maxDepth ) );
1002 if( !oneKey ) for( size_t i=0 ; i<dataKeys.size() ; i++ ) dataKeys[i].set( _localToGlobal( maxDepth ) );
1003 std::vector< Real > depthAndWeightSums( ThreadPool::NumThreads() , 0 );
1004 pointDepthAndWeight.data = Point< Real , 2 >();
1005 pointDepthAndWeight.weight = 0;
1006 SparseNodeData< OutData , UIntPack< DataSigs ... > > dataField;
1007 std::vector< Point< Real , 2 > > pointDepthAndWeightSums( ThreadPool::NumThreads() , Point< Real , 2 >() );
1008 ThreadPool::ParallelFor( 0 , samples.size() , [&]( unsigned int thread , size_t i )
1009 {
1010 DensityKey& densityKey = densityKeys[ thread ];
1011 DataKey& dataKey = dataKeys[ thread ];
1012 const ProjectiveData< Point< Real , Dim > , Real >& sample = samples[i].sample;
1013 if( sample.weight>0 )
1014 {
1015 Point< Real , Dim > p = sample.data / sample.weight;
1016 InData in = data[i] / sample.weight;
1017 OutData out;
1018
1019 Real depthBias;
1020 if( !_InBounds(p) ) MK_WARN( "Point sample is out of bounds" );
1021 else if( ConversionAndBiasFunction( in , out , depthBias ) )
1022 {
1023 depthAndWeightSums[thread] += sample.weight;
1024 #ifdef SHOW_WARNINGS
1025 #pragma message( "[WARNING] Should the next line be commented out?" )
1026 #endif // SHOW_WARNINGS
1027 out *= sample.weight;
1028 Allocator< FEMTreeNode > *nodeAllocator = nodeAllocators.size() ? nodeAllocators[ thread ] : NULL;
1029 #if defined( __GNUC__ ) && __GNUC__ < 5
1030 #ifdef SHOW_WARNINGS
1031 #warning "you've got me gcc version<5"
1032 #endif // SHOW_WARNINGS
1033 if( density ) pointDepthAndWeightSums[thread] += _splatPointData< true , true , DensityDegree , OutData >( zero , nodeAllocator , *density , minDepthCutoff , p , out , dataField , densityKey , oneKey ? *( (DataKey*)&densityKey ) : dataKey , minDepth , pointDepthFunctor , Dim , depthBias ) * sample.weight;
1034 #else // !__GNUC__ || __GNUC__ >=5
1035 if( density ) pointDepthAndWeightSums[thread] += _splatPointData< true , true , DensityDegree , OutData , DataSigs ... >( zero , nodeAllocator , *density , minDepthCutoff , p , out , dataField , densityKey , oneKey ? *( (DataKey*)&densityKey ) : dataKey , minDepth , pointDepthFunctor , Dim , depthBias ) * sample.weight;
1036 #endif // __GNUC__ && __GNUC__ < 5
1037 else
1038 {
1039 Real width = (Real)( 1.0 / ( 1<<maxDepth ) );
1040 #if defined( __GNUC__ ) && __GNUC__ < 5
1041 #ifdef SHOW_WARNINGS
1042 #warning "you've got me gcc version<5"
1043 #endif // SHOW_WARNINGS
1044 _splatPointData< true , true , OutData >( zero , nodeAllocator , _leaf< true >( nodeAllocator , p , pointDepthFunctor ) , p , out / (Real)pow( width , Dim ) , dataField , oneKey ? *( (DataKey*)&densityKey ) : dataKey );
1045 #else // !__GNUC__ || __GNUC__ >=5
1046 _splatPointData< true , true , OutData , DataSigs ... >( zero , nodeAllocator , _leaf< true >( nodeAllocator , p , pointDepthFunctor ) , p , out / (Real)pow( width , Dim ) , dataField , oneKey ? *( (DataKey*)&densityKey ) : dataKey );
1047 #endif // __GNUC__ && __GNUC__ < 5
1048 pointDepthAndWeightSums[thread] += Point< Real , 2 >( (Real)1 , (Real)maxDepth ) * sample.weight;
1049 }
1050 }
1051 }
1052 }
1053 );
1054 pointDepthAndWeight.data = Point< Real , 2 >();
1055 for( unsigned int i=0 ; i<pointDepthAndWeightSums.size() ; i++ ) pointDepthAndWeight.data += pointDepthAndWeightSums[i];
1056 pointDepthAndWeight.weight = 0;
1057 for( unsigned int i=0 ; i<depthAndWeightSums.size() ; i++ ) pointDepthAndWeight.weight += depthAndWeightSums[i];
1058 return dataField;
1059 }
1060
1061 template< unsigned int Dim , class Real >
1062 template< unsigned int DataSig , bool CreateNodes , unsigned int DensityDegree , class Data >
1063 SparseNodeData< ProjectiveData< Data , Real > , IsotropicUIntPack< Dim , DataSig > > FEMTree< Dim , Real >::setExtrapolatedDataField( Data zero , const std::vector< PointSample >& samples , const std::vector< Data >& sampleData , const DensityEstimator< DensityDegree >* density , bool nearest )
1064 {
1065 return this->template setExtrapolatedDataField< DataSig , CreateNodes , DensityDegree , Data >( zero , samples.size() , [&]( size_t i ) -> const PointSample & { return samples[i]; } , [&]( size_t i ) -> const Data & { return sampleData[i]; } , density , nearest );
1066 }
1067
1068 template< unsigned int Dim , class Real >
1069 template< unsigned int DataSig , bool CreateNodes , unsigned int DensityDegree , class Data >
1070 void FEMTree< Dim , Real >::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 )
1071 {
1072 return this->template updateExtrapolatedDataField< DataSig , CreateNodes , DensityDegree , Data >( zero , dataField , samples.size() , [&]( size_t i ) -> const PointSample & { return samples[i]; } , [&]( size_t i ) -> const Data & { return sampleData[i]; } , density , nearest );
1073 }
1074
1075 template< unsigned int Dim , class Real >
1076 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) > */ >
1077 SparseNodeData< ProjectiveData< Data , Real > , IsotropicUIntPack< Dim , DataSig > > FEMTree< Dim , Real >::setExtrapolatedDataField( Data zero , size_t sampleNum , SampleFunctor sampleFunctor , SampleDataFunctor sampleDataFunctor , const DensityEstimator< DensityDegree >* density , bool nearest )
1078 {
1079 SparseNodeData< ProjectiveData< Data , Real > , IsotropicUIntPack< Dim , DataSig > > dataField;
1080 this->template updateExtrapolatedDataField< DataSig , CreateNodes , DensityDegree , Data >( zero , dataField , sampleNum , sampleFunctor , sampleDataFunctor , density , nearest );
1081 return dataField;
1082 }
1083
1084 template< unsigned int Dim , class Real >
1085 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) > */ >
1086 void FEMTree< Dim , Real >::updateExtrapolatedDataField( Data zero , SparseNodeData< ProjectiveData< Data , Real > , IsotropicUIntPack< Dim , DataSig > > &dataField , size_t sampleNum , SampleFunctor sampleFunctor , SampleDataFunctor sampleDataFunctor , const DensityEstimator< DensityDegree >* density , bool nearest )
1087 {
1088 Allocator< FEMTreeNode > *nodeAllocator = nodeAllocators.size() ? nodeAllocators[0] : NULL;
1089 LocalDepth maxDepth = _spaceRoot->maxDepth();
1090
1091 if constexpr( CreateNodes )
1092 {
1093 PointSupportKey< IsotropicUIntPack< Dim , DensityDegree > > densityKey;
1094 PointSupportKey< IsotropicUIntPack< Dim , FEMSignature< DataSig >::Degree > > dataKey;
1095 densityKey.set( _localToGlobal( maxDepth ) ) , dataKey.set( _localToGlobal( maxDepth ) );
1096 for( node_index_type i=0 ; i<(node_index_type)sampleNum ; i++ )
1097 {
1098 const PointSample &sampleAndNode = sampleFunctor(i);
1099 const ProjectiveData< Point< Real , Dim > , Real >& sample = sampleAndNode.sample;
1100 const Data& data = sampleDataFunctor(i);
1101 Point< Real , Dim > p = sample.weight==0 ? sample.data : sample.data / sample.weight;
1102 if( !_InBounds(p) )
1103 {
1104 MK_WARN( "Point is out of bounds" );
1105 continue;
1106 }
1107 if( nearest ) _nearestMultiSplatPointData< DensityDegree >( ProjectiveData< Data , Real >( zero ) , density , (FEMTreeNode*)sampleAndNode.node , p , ProjectiveData< Data , Real >( data , sample.weight ) , dataField , densityKey , 2 );
1108 else _multiSplatPointData< CreateNodes , false , DensityDegree >( ProjectiveData< Data , Real >( zero ) , nodeAllocator , density , (FEMTreeNode*)sampleAndNode.node , p , ProjectiveData< Data , Real >( data , sample.weight ) , dataField , densityKey , dataKey , 2 );
1109 }
1110 }
1111 else
1112 {
1113 std::vector< PointSupportKey< IsotropicUIntPack< Dim , DensityDegree > > > densityKeys( ThreadPool::NumThreads() );
1114 std::vector< PointSupportKey< IsotropicUIntPack< Dim , FEMSignature< DataSig >::Degree > > > dataKeys( ThreadPool::NumThreads() );
1115 for( unsigned int i=0 ; i<ThreadPool::NumThreads() ; i++ ) densityKeys[i].set( _localToGlobal( maxDepth ) ) , dataKeys[i].set( _localToGlobal( maxDepth ) );
1116
1117 ThreadPool::ParallelFor( 0 , sampleNum , [&]( unsigned int thread , size_t i ) {
1118 PointSupportKey< IsotropicUIntPack< Dim , DensityDegree > > &densityKey = densityKeys[thread];
1119 PointSupportKey< IsotropicUIntPack< Dim , FEMSignature< DataSig >::Degree > > &dataKey = dataKeys[thread];
1120
1121 const PointSample &sampleAndNode = sampleFunctor(i);
1122 const ProjectiveData< Point< Real , Dim > , Real >& sample = sampleAndNode.sample;
1123 const Data& data = sampleDataFunctor(i);
1124 Point< Real , Dim > p = sample.weight==0 ? sample.data : sample.data / sample.weight;
1125 if( !_InBounds(p) ) MK_WARN( "Point is out of bounds" );
1126 else
1127 {
1128 if( nearest ) _nearestMultiSplatPointData< DensityDegree >( ProjectiveData< Data , Real >( zero ) , density , (FEMTreeNode*)sampleAndNode.node , p , ProjectiveData< Data , Real >( data , sample.weight ) , dataField , densityKey , 2 );
1129 else _multiSplatPointData< CreateNodes , false , DensityDegree >( ProjectiveData< Data , Real >( zero ) , nodeAllocator , density , (FEMTreeNode*)sampleAndNode.node , p , ProjectiveData< Data , Real >( data , sample.weight ) , dataField , densityKey , dataKey , 2 );
1130 }
1131 } );
1132 }
1133 }
1134
1135 template< unsigned int Dim , class Real >
1136 template< unsigned int MaxDegree >
1137 void FEMTree< Dim , Real >::_supportApproximateProlongation( void )
1138 {
1139 // Refine the tree so that if an active element exists @{d}, all supporting elements exist @{d-1}
1140 const int OverlapRadius = -BSplineOverlapSizes< MaxDegree , MaxDegree >::OverlapStart;
1141 typedef typename FEMTreeNode::template NeighborKey< IsotropicUIntPack< Dim , OverlapRadius > , IsotropicUIntPack< Dim , OverlapRadius > > NeighborKey;
1142
1143 std::vector< NeighborKey > neighborKeys( ThreadPool::NumThreads() );
1144 for( int i=0 ; i<neighborKeys.size() ; i++ ) neighborKeys[i].set( _localToGlobal( _maxDepth-1 ) );
1145
1146 for( LocalDepth d=_maxDepth-1 ; d>_baseDepth ; d-- )
1147 {
1148 // Compute the set of nodes at depth d that have (non-ghost) children at depth d+1.
1149 std::vector< FEMTreeNode* > nodes;
1150 _tree.processNodes( [&]( FEMTreeNode *node ){ if( _localDepth( node )==d && IsActiveNode( node->children ) ) nodes.push_back( node ) ; return _localDepth(node)<d; } );
1151
1152 // Make sure that all finite elements whose support overlaps the support of the finite elements indexed by those nodes are in the tree.
1153 #ifdef SHOW_WARNINGS
1154 #pragma message( "[WARNING] This may be overkill as we only need to check if the support overlaps the support of the children" )
1155 #endif // SHOW_WARNINGS
1156 ThreadPool::ParallelFor( 0 , nodes.size() , [&]( unsigned int thread , size_t i )
1157 {
1158 NeighborKey& neighborKey = neighborKeys[ thread ];
1159 FEMTreeNode *node = nodes[i];
1160
1161 // Create the neighbors if they are not already in the tree
1162 neighborKey.template getNeighbors< true , true >( node , nodeAllocators.size() ? nodeAllocators[ thread ] : NULL , _nodeInitializer );
1163
1164 // Mark the neighbors as active
1165 Pointer( FEMTreeNode* ) nodes = neighborKey.neighbors[ _localToGlobal(d) ].neighbors().data;
1166 unsigned int size = neighborKey.neighbors[ _localToGlobal(d) ].neighbors.Size();
1167 for( unsigned int i=0 ; i<size ; i++ ) SetGhostFlag( nodes[i] , false );
1168 }
1169 );
1170 }
1171 }
1172
1173 template< unsigned int Dim , typename Real >
1174 template< unsigned int SystemDegree >
1175 void FEMTree< Dim , Real >::_markNonBaseDirichletElements( void )
1176 {
1177 const int LeftSupportRadius = -BSplineSupportSizes< SystemDegree >::SupportStart;
1178 const int RightSupportRadius = BSplineSupportSizes< SystemDegree >::SupportEnd;
1179 typedef typename FEMTreeNode::template NeighborKey< IsotropicUIntPack< Dim , LeftSupportRadius > , IsotropicUIntPack< Dim , RightSupportRadius > > SupportKey;
1180 typedef StaticWindow< FEMTreeNode * , IsotropicUIntPack< Dim , LeftSupportRadius + RightSupportRadius + 1 > > NeighborLeaves;
1181
1182 std::vector< NeighborLeaves > neighborLeaves( ThreadPool::NumThreads() );
1183 std::vector< SupportKey > supportKeys( ThreadPool::NumThreads() );
1184 for( int i=0 ; i<supportKeys.size() ; i++ ) supportKeys[i].set( _localToGlobal( _maxDepth ) );
1185
1186 // Get the list of nodes @{_baseDepth)
1187 std::vector< FEMTreeNode * > baseNodes;
1188 _tree.processNodes( [&]( FEMTreeNode *node ){ if( _localDepth( node )==_baseDepth ) baseNodes.push_back( node ); } );
1189
1190 // Process the sub-tree rooted at the node:
1191 // -- For each non-ghost node in the sub-tree check if the finite element associated to the node is supported on a Dirichlet node.
1192 // If it is, mark the node as a Dirichlet element node.
1193 std::function< void ( FEMTreeNode * , SupportKey & , NeighborLeaves & ) > ProcessSubTree = [&]( FEMTreeNode *node , SupportKey &supportKey , NeighborLeaves &neighborLeaves )
1194 {
1195 if( !node->nodeData.getGhostFlag() )
1196 {
1197 supportKey.setLeafNeighbors( node , neighborLeaves );
1198 bool hasDirichletNeighbor = false;
1199 for( unsigned int i=0 ; i<NeighborLeaves::Size() ; i++ ) if( neighborLeaves.data[i] && neighborLeaves.data[i]->nodeData.getDirichletNodeFlag() ) hasDirichletNeighbor = true;
1200 node->nodeData.setDirichletElementFlag( hasDirichletNeighbor );
1201
1202 if( node->children ) for( int c=0 ; c<(1<<Dim) ; c++ ) ProcessSubTree( node->children+c , supportKey , neighborLeaves );
1203 }
1204 };
1205
1206 ThreadPool::ParallelFor( 0 , baseNodes.size() , [&]( unsigned int t , size_t i ){ ProcessSubTree( baseNodes[i] , supportKeys[t] , neighborLeaves[t] ); } );
1207 }
1208
1209 template< unsigned int Dim , typename Real >
1210 template< unsigned int SystemDegree >
1211 void FEMTree< Dim , Real >::_markBaseDirichletElements( void )
1212 {
1213 const int LeftSupportRadius = BSplineSupportSizes< SystemDegree >::SupportEnd;
1214 const int RightSupportRadius = -BSplineSupportSizes< SystemDegree >::SupportStart;
1215
1216 typedef typename FEMTreeNode::template NeighborKey< IsotropicUIntPack< Dim , LeftSupportRadius > , IsotropicUIntPack< Dim , RightSupportRadius > > SupportKey;
1217 std::vector< SupportKey > supportKeys( ThreadPool::NumThreads() );
1218 for( int i=0 ; i<supportKeys.size() ; i++ ) supportKeys[i].set( _localToGlobal( _baseDepth ) );
1219
1220 std::vector< FEMTreeNode* > nodes;
1221 _tree.processNodes( [&]( FEMTreeNode *node ){ if( _localDepth( node )==_baseDepth && node->nodeData.getDirichletNodeFlag() ) nodes.push_back( node ) ; return _localDepth(node)<_baseDepth; } );
1222
1223 ThreadPool::ParallelFor( 0 , nodes.size() , [&]( unsigned int thread , size_t i )
1224 {
1225 SupportKey &supportKey = supportKeys[ thread ];
1226 FEMTreeNode *node = nodes[i];
1227 supportKey.getNeighbors( node );
1228 for( LocalDepth d=0 ; d<=_baseDepth ; d++ )
1229 {
1230 Pointer( FEMTreeNode* ) _nodes = supportKey.neighbors[ _localToGlobal(d) ].neighbors().data;
1231 unsigned int size = supportKey.neighbors[ _localToGlobal(d) ].neighbors.Size();
1232 for( unsigned int i=0 ; i<size ; i++ ) if( _nodes[i] ) SetGhostFlag( _nodes[i] , false ) , _nodes[i]->nodeData.setDirichletElementFlag( true );
1233 }
1234 } );
1235 }
1236
1237 template< unsigned int Dim , class Real >
1238 template< unsigned int MaxDegree , unsigned int SystemDegree , typename AddNodeFunctor , typename HasDataFunctor , typename ... InterpolationInfos , typename ... DenseOrSparseNodeData >
1239 std::vector< node_index_type > FEMTree< Dim , Real >::finalizeForMultigrid( LocalDepth baseDepth , const AddNodeFunctor addNodeFunctor , const HasDataFunctor hasDataFunctor , std::tuple< InterpolationInfos *... > interpolationInfos , std::tuple< DenseOrSparseNodeData *... > data )
1240 {
1241 auto isDirichletLeafFunctor = []( const FEMTreeNode * ){ return false; };
1242 return _finalizeForMultigrid< false , MaxDegree , SystemDegree >( baseDepth , addNodeFunctor , hasDataFunctor , isDirichletLeafFunctor , interpolationInfos , data );
1243 }
1244
1245 template< unsigned int Dim , class Real >
1246 template< unsigned int MaxDegree , unsigned int SystemDegree , typename AddNodeFunctor , typename HasDataFunctor , typename IsDirichletLeafFunctor , typename ... InterpolationInfos , typename ... DenseOrSparseNodeData >
1247 std::vector< node_index_type > FEMTree< Dim , Real >::finalizeForMultigridWithDirichlet( LocalDepth baseDepth , const AddNodeFunctor addNodeFunctor , const HasDataFunctor hasDataFunctor , const IsDirichletLeafFunctor isDirichletLeafFunctor , std::tuple< InterpolationInfos *... > interpolationInfos , std::tuple< DenseOrSparseNodeData *... > data )
1248 {
1249 return _finalizeForMultigrid< true , MaxDegree , SystemDegree >( baseDepth , addNodeFunctor , hasDataFunctor , isDirichletLeafFunctor , interpolationInfos , data );
1250 }
1251
1252
1253 template< unsigned int Dim , class Real >
1254 template< bool HasDirichlet , unsigned int MaxDegree , unsigned int SystemDegree , typename AddNodeFunctor , typename HasDataFunctor , typename IsDirichletLeafFunctor , typename ... InterpolationInfos , typename ... DenseOrSparseNodeData >
1255 std::vector< node_index_type > FEMTree< Dim , Real >::_finalizeForMultigrid( LocalDepth baseDepth , const AddNodeFunctor addNodeFunctor , const HasDataFunctor hasDataFunctor , const IsDirichletLeafFunctor isDirichletLeafFunctor , std::tuple< InterpolationInfos *... > interpolationInfos , std::tuple< DenseOrSparseNodeData *... > data )
1256 {
1257 std::function< void ( FEMTreeNode * ) > pushFullDirichletFlag = [&]( FEMTreeNode *node )
1258 {
1259 if( node->children )
1260 {
1261 if( node->nodeData.getDirichletNodeFlag() )
1262 {
1263 for( int c=0 ; c<(1<<Dim) ; c++ ) node->children[c].nodeData.setDirichletNodeFlag( true );
1264 node->nodeData.setDirichletNodeFlag( false );
1265 }
1266 for( int c=0 ; c<(1<<Dim) ; c++ ) pushFullDirichletFlag( node->children + c );
1267 }
1268 };
1269
1270 std::function< bool ( FEMTreeNode * ) > pullPartialDirichletFlag = [&]( FEMTreeNode *node )
1271 {
1272 if( node->children )
1273 {
1274 bool childDirichlet = false;
1275 for( int c=0 ; c<(1<<Dim) ; c++ ) childDirichlet |= pullPartialDirichletFlag( node->children + c );
1276 node->nodeData.setDirichletNodeFlag( childDirichlet );
1277 }
1278 return node->nodeData.getDirichletNodeFlag();
1279 };
1280
1281 std::function< void ( FEMTreeNode * , node_index_type , bool ) > pushDirichletFlag = [&]( FEMTreeNode *node , node_index_type newNodeIndex , bool isDirichletNode )
1282 {
1283 if( node->nodeData.nodeIndex>=newNodeIndex ) node->nodeData.setDirichletNodeFlag( isDirichletNode );
1284 isDirichletNode = node->nodeData.getDirichletNodeFlag();
1285 if( node->children ) for( int c=0 ; c<(1<<Dim) ; c++ ) pushDirichletFlag( node->children+c , newNodeIndex , isDirichletNode );
1286 };
1287
1288 _baseDepth = baseDepth;
1289 // Expand the tree to ensure supporting finite elements can be indexed by octree nodes
1290
1291 Allocator< FEMTreeNode > *nodeAllocator = nodeAllocators.size() ? nodeAllocators[0] : NULL;
1292
1293 while( _localInset( 0 ) + BSplineEvaluationData< FEMDegreeAndBType< MaxDegree >::Signature >::Begin( 0 )<0 || _localInset( 0 ) + BSplineEvaluationData< FEMDegreeAndBType< MaxDegree >::Signature >::End( 0 )>(1<<_depthOffset) )
1294 {
1295 // +-+-+-+-+-+-+-+-+
1296 // | | | | | | | | |
1297 // +-+-+-+-+-+-+-+-+
1298 // | | | | | | | | |
1299 // +-+-+-+-+ +-+-+-+-+-+-+-+-+
1300 // | | | | | | | | | | | | | |
1301 // +-+-+ +-+-+-+-+ +-+-+-+-+-+-+-+-+
1302 // |*| | | | | | | | | | | | | | | |
1303 // +-o-+ -> +-+-o-+-+ -> +-+-+-+-o-+-+-+-+
1304 // | | | | | |*| | | | | | |*| | | |
1305 // +-+-+ +-+-+-+-+ +-+-+-+-+-+-+-+-+
1306 // | | | | | | | | | | | | | |
1307 // +-+-+-+-+ +-+-+-+-+-+-+-+-+
1308 // | | | | | | | | |
1309 // +-+-+-+-+-+-+-+-+
1310 // | | | | | | | | |
1311 // +-+-+-+-+-+-+-+-+
1312
1313 // Insert a new brood between the root of the tree and its (old) children:
1314 // -- Make the old children sit off the last node of the brood
1315 // -- Swap the children sitting off the last node of the old children to the first node of the old children
1316 FEMTreeNode *oldChildren = _tree.children;
1317 FEMTreeNode *newChildren = FEMTreeNode::NewBrood( nodeAllocator , _nodeInitializer );
1318 if( !oldChildren ) MK_THROW( "Expected children" );
1319 {
1320 if( oldChildren[(1<<Dim)-1].children )
1321 {
1322 for( int c=0 ; c<(1<<Dim) ; c++ ) oldChildren[(1<<Dim)-1].children[c].parent = oldChildren;
1323 oldChildren[0].children = oldChildren[(1<<Dim)-1].children;
1324 oldChildren[(1<<Dim)-1].children = NULL;
1325 }
1326 for( int c=0 ; c<(1<<Dim) ; c++ ) oldChildren[c].parent = newChildren+(1<<Dim)-1;
1327 newChildren[(1<<Dim)-1].children = oldChildren;
1328 }
1329
1330 for( int c=0 ; c<(1<<Dim) ; c++ ) newChildren[c].parent = &_tree;
1331 _tree.children = newChildren;
1332
1333 _depthOffset++;
1334 }
1335 _init();
1336
1337 _maxDepth = _spaceRoot->maxDepth();
1338
1339 // Mark leaf nodes that are Dirichlet constraints so they do not get clipped out.
1340 // Need to do this before introducing new nodes into the tree (since isDirichletLeaf depends on the structure at input).
1341 if constexpr( HasDirichlet ) _spaceRoot->processLeaves( [&]( FEMTreeNode *leaf ){ leaf->nodeData.setDirichletNodeFlag( isDirichletLeafFunctor( leaf ) ); } );
1342
1343 // Make the low-resolution part of the tree be complete
1344 _setFullDepth< false >( IsotropicUIntPack< Dim , MaxDegree >() , nodeAllocator , _baseDepth );
1345 _refine< false >( IsotropicUIntPack< Dim , MaxDegree >() , nodeAllocator , addNodeFunctor );
1346
1347 if constexpr( HasDirichlet )
1348 {
1349 // Mark new leaf nodes
1350 pushFullDirichletFlag( _spaceRoot );
1351
1352 // Pull the Dirichlet designator from the leaves so that nodes are now marked if they contain (possibly partial Dirichlet constraints
1353 pullPartialDirichletFlag( _spaceRoot );
1354
1355 // Use the node Dirichlet designators to set the coarser finite element Dirichlet designators
1356 _markBaseDirichletElements< SystemDegree >();
1357 }
1358
1359 // Clear all the flags and make everything that is not low-res a ghost node, and clip the tree
1360 auto _addNodeFunctor = [&]( const FEMTreeNode *node )
1361 {
1362 LocalDepth d ; LocalOffset off;
1363 _localDepthAndOffset( node , d , off );
1364 return addNodeFunctor( d , off );
1365 };
1366
1367 auto nodeFunctor = [&]( FEMTreeNode *node )
1368 {
1369 if constexpr( HasDirichlet ) node->nodeData.flags &= ( FEMTreeNodeData::SCRATCH_FLAG | FEMTreeNodeData::DIRICHLET_NODE_FLAG | FEMTreeNodeData::DIRICHLET_ELEMENT_FLAG );
1370 else node->nodeData.flags &= ( FEMTreeNodeData::SCRATCH_FLAG );
1371 SetGhostFlag( node , !_addNodeFunctor( node ) && _localDepth( node )>_baseDepth );
1372 };
1373 _tree.processNodes( nodeFunctor );
1374
1375 // Clip off nodes that not have data and do not contain geometry or Dirichlet constraints below the exactDepth
1376 if constexpr( HasDirichlet ) _clipTree( [&]( const FEMTreeNode *node ){ return _addNodeFunctor(node) || hasDataFunctor(node) || ( ( node->nodeData.getDirichletNodeFlag() || node->nodeData.getDirichletElementFlag() ) && _localDepth(node)<=_baseDepth ); } , _baseDepth );
1377 else _clipTree( [&]( const FEMTreeNode *node ){ return _addNodeFunctor(node) || hasDataFunctor(node); } , _baseDepth );
1378
1379 // It is possible for the tree to have become shallower after clipping
1380 _maxDepth = _tree.maxDepth() - _depthOffset;
1381
1382 node_index_type oldNodeCount = _nodeCount;
1383
1384 // Refine the node so that finite elements @{depth-1} whose support overlaps finite elements @{depth} are in the tree
1385 _supportApproximateProlongation< MaxDegree >();
1386
1387 // Mark new leaf nodes
1388 if constexpr( HasDirichlet )
1389 {
1390 pushDirichletFlag( _spaceRoot , oldNodeCount , _spaceRoot->nodeData.getDirichletNodeFlag() );
1391 _markNonBaseDirichletElements< SystemDegree >();
1392 }
1393 _markNonBaseDirichletElements< SystemDegree >();
1394
1395 return setSortedTreeNodes( interpolationInfos , data );
1396 }
1397
1398 template< unsigned int Dim , class Real >
1399 template< typename ... InterpolationInfos , typename ... DenseOrSparseNodeData >
1400 std::vector< node_index_type > FEMTree< Dim , Real >::setSortedTreeNodes( std::tuple< InterpolationInfos *... > interpolationInfos , std::tuple< DenseOrSparseNodeData *... > data )
1401 {
1402 _maxDepth = _tree.maxDepth() - _depthOffset;
1403 std::vector< node_index_type > map;
1404 _sNodes.reset( _tree , map );
1405 _setSpaceValidityFlags();
1406 _tree.processNodes( [&]( FEMTreeNode *node ){ if( !IsActiveNode( node ) ) node->nodeData.nodeIndex = -1; } );
1407 _reorderDenseOrSparseNodeData< 0 >( GetPointer( map ) , _sNodes.size() , data );
1408 _reorderInterpolationInfo< 0 >( GetPointer( map ) , _sNodes.size() , interpolationInfos );
1409 memset( _femSigs1 , -1 , sizeof( _femSigs1 ) );
1410 memset( _femSigs2 , -1 , sizeof( _femSigs2 ) );
1411 return map;
1412 }
1413
1414 template< unsigned int Dim , class Real >
1415 template< class ... DenseOrSparseNodeData >
1416 void FEMTree< Dim , Real >::resetIndices( std::tuple< DenseOrSparseNodeData *... > data )
1417 {
1418 std::vector< node_index_type > map;
1419 _sNodes.reset( _tree , map );
1420 _setSpaceValidityFlags();
1421 _tree.processNodes( [&]( FEMTreeNode *node ){ if( !IsActiveNode< Dim >( node ) ) node->nodeData.nodeIndex = -1; } );
1422 _reorderDenseOrSparseNodeData< 0 >( GetPointer( map ) , _sNodes.size() , data );
1423 }
1424
1425 template< unsigned int Dim , class Real >
1426 template< typename PruneChildrenFunctor , typename ... InterpolationInfos , typename ... DenseOrSparseNodeData >
1427 void FEMTree< Dim , Real >::pruneChildren( const PruneChildrenFunctor pruneChildren , std::tuple< InterpolationInfos *... > interpolationInfos , std::tuple< DenseOrSparseNodeData *... > data )
1428 {
1429 _tree.pruneChildren( pruneChildren , false );
1430 std::vector< node_index_type > map;
1431 _sNodes.reset( _tree , map );
1432 _setSpaceValidityFlags();
1433 _tree.processNodes( [&]( FEMTreeNode *node ){ if( !IsActiveNode< Dim >( node ) ) node->nodeData.nodeIndex = -1; } );
1434 _reorderDenseOrSparseNodeData< 0 >( GetPointer( map ) , _sNodes.size() , data );
1435 _reorderInterpolationInfo< 0 >( GetPointer( map ) , _sNodes.size() , interpolationInfos );
1436 }
1437
1438 template< unsigned int Dim , class Real >
1439 void FEMTree< Dim , Real >::_setSpaceValidityFlags( void ) const
1440 {
1441 const unsigned char MASK = ~( FEMTreeNodeData::SPACE_FLAG );
1442 ThreadPool::ParallelFor( 0 , _sNodes.size() , [&]( unsigned int , size_t i )
1443 {
1444 _sNodes.treeNodes[i]->nodeData.flags &= MASK;
1445 if( isValidSpaceNode( _sNodes.treeNodes[i] ) ) _sNodes.treeNodes[i]->nodeData.flags |= FEMTreeNodeData::SPACE_FLAG;
1446 }
1447 );
1448 }
1449 template< unsigned int Dim , class Real >
1450 template< unsigned int ... FEMSigs1 >
1451 void FEMTree< Dim , Real >::_setFEM1ValidityFlags( UIntPack< FEMSigs1 ... > ) const
1452 {
1453 bool needToReset;
1454 unsigned int femSigs1[] = { FEMSigs1 ... };
1455 {
1456 static std::mutex m;
1457 std::lock_guard< std::mutex > lock( m );
1458 needToReset = memcmp( femSigs1 , _femSigs1 , sizeof( _femSigs1 ) )!=0;
1459 if( needToReset ) memcpy( _femSigs1 , femSigs1 , sizeof( _femSigs1 ) );
1460 }
1461 if( needToReset )
1462 for( node_index_type i=0 ; i<(node_index_type)_sNodes.size() ; i++ )
1463 {
1464 const unsigned char MASK = ~( FEMTreeNodeData::FEM_FLAG_1 );
1465 _sNodes.treeNodes[i]->nodeData.flags &= MASK;
1466 if( isValidFEMNode( UIntPack< FEMSigs1 ... >() , _sNodes.treeNodes[i] ) ) _sNodes.treeNodes[i]->nodeData.flags |= FEMTreeNodeData::FEM_FLAG_1;
1467 }
1468
1469 }
1470 template< unsigned int Dim , class Real >
1471 template< unsigned int ... FEMSigs2 >
1472 void FEMTree< Dim , Real >::_setFEM2ValidityFlags( UIntPack< FEMSigs2 ... > ) const
1473 {
1474 bool needToReset;
1475 unsigned int femSigs2[] = { FEMSigs2 ... };
1476 {
1477 static std::mutex m;
1478 std::lock_guard< std::mutex > lock(m);
1479 needToReset = memcmp( femSigs2 , _femSigs2 , sizeof( _femSigs2 ) )!=0;
1480 if( needToReset ) memcpy( _femSigs2 , femSigs2 , sizeof( _femSigs2 ) );
1481 }
1482 if( needToReset )
1483 for( node_index_type i=0 ; i<(node_index_type)_sNodes.size() ; i++ )
1484 {
1485 const unsigned char MASK = ~( FEMTreeNodeData::FEM_FLAG_2 );
1486 _sNodes.treeNodes[i]->nodeData.flags &= MASK;
1487 if( isValidFEMNode( UIntPack< FEMSigs2 ... >() , _sNodes.treeNodes[i] ) ) _sNodes.treeNodes[i]->nodeData.flags |= FEMTreeNodeData::FEM_FLAG_2;
1488 }
1489 }
1490
1491 template< unsigned int Dim , class Real >
1492 template< class HasDataFunctor >
1493 void FEMTree< Dim , Real >::_clipTree( const HasDataFunctor& f , LocalDepth fullDepth )
1494 {
1495 std::vector< FEMTreeNode * > regularNodes;
1496 _tree.processNodes( [&]( FEMTreeNode *node ){ if( _localDepth( node )==fullDepth ) regularNodes.push_back( node ) ; return _localDepth( node )<fullDepth; } );
1497
1498 // Get the data status of each node
1499 // [NOTE] Have to use an array of chars instead of bools because the latter is not thread safe
1500 node_index_type sz = nodeCount();
1501 Pointer( char ) nodeHasData = NewPointer< char >( sz );
1502 for( node_index_type i=0 ; i<sz ; i++ ) nodeHasData[i] = 0;
1503 ThreadPool::ParallelFor( 0 , regularNodes.size() , [&]( unsigned int , size_t i )
1504 {
1505 regularNodes[i]->processNodes( [&]( FEMTreeNode *node ){ if( node->nodeData.nodeIndex!=-1 ) nodeHasData[node->nodeData.nodeIndex] = f( node ) ? 1 : 0; } );
1506 } );
1507
1508 // Pull the data status from the leaves
1509 std::function< char ( const FEMTreeNode * ) > PullHasDataFromChildren = [&]( const FEMTreeNode *node )
1510 {
1511 if( node->nodeData.nodeIndex==-1 ) return (char)0;
1512 char hasData = nodeHasData[node->nodeData.nodeIndex];
1513 if( node->children ) for( int c=0 ; c<(1<<Dim) ; c++ ) hasData |= PullHasDataFromChildren( node->children+c );
1514 nodeHasData[node->nodeData.nodeIndex] = hasData;
1515 return hasData;
1516 };
1517
1518 ThreadPool::ParallelFor( 0 , regularNodes.size() , [&]( unsigned int , size_t i ){ PullHasDataFromChildren( regularNodes[i] ); } );
1519
1520 // Mark all children of a node as ghost if none of them have data
1521 ThreadPool::ParallelFor( 0 , regularNodes.size() , [&]( unsigned int , size_t i )
1522 {
1523 auto nodeFunctor = [&]( FEMTreeNode *node )
1524 {
1525 if( node->children )
1526 {
1527 char childHasData = 0;
1528 for( int c=0 ; c<(1<<Dim) ; c++ ) if( node->children[c].nodeData.nodeIndex!=-1 ) childHasData |= nodeHasData[node->children[c].nodeData.nodeIndex];
1529 for( int c=0 ; c<(1<<Dim) ; c++ ) SetGhostFlag< Dim >( node->children+c , !childHasData );
1530 }
1531 };
1532 regularNodes[i]->processNodes( nodeFunctor );
1533 } );
1534 DeletePointer( nodeHasData );
1535 }
1536
1537 template< unsigned int Dim , class Real >
1538 template< typename T , typename Data , unsigned int PointD , typename ConstraintDual , typename SystemDual >
1539 void FEMTree< Dim , Real >::_ExactPointAndDataInterpolationInfo< T , Data , PointD , ConstraintDual , SystemDual >::_init( const class FEMTree< Dim , Real >& tree , const std::vector< PointSample >& samples , ConstPointer( Data ) sampleData , bool noRescale )
1540 {
1541 _sampleSpan.resize( tree.nodesSize() );
1542 ThreadPool::ParallelFor( 0 , tree.nodesSize() , [&]( unsigned int , size_t i ){ _sampleSpan[i] = std::pair< node_index_type , node_index_type >( 0 , 0 ); } );
1543 for( node_index_type i=0 ; i<(node_index_type)samples.size() ; i++ )
1544 {
1545 const FEMTreeNode* leaf = samples[i].node;
1546 while( leaf && !tree._isValidSpaceNode( leaf ) ) leaf = leaf->parent;
1547 if( leaf && tree._isValidSpaceNode( leaf ) ) _sampleSpan[ leaf->nodeData.nodeIndex ].second++;
1548 }
1549 _iData.resize( samples.size() );
1550
1551 std::function< void ( FEMTreeNode* , node_index_type & ) > SetRange = [&] ( FEMTreeNode* node , node_index_type &start )
1552 {
1553 std::pair< node_index_type , node_index_type >& span = _sampleSpan[ node->nodeData.nodeIndex ];
1554 if( tree._isValidSpaceNode( node->children ) )
1555 {
1556 for( int c=0 ; c<(1<<Dim) ; c++ ) SetRange( node->children + c , start );
1557 span.first = _sampleSpan[ node->children[0 ].nodeData.nodeIndex ].first;
1558 span.second = _sampleSpan[ node->children[ (1<<Dim)-1 ].nodeData.nodeIndex ].second;
1559 }
1560 else
1561 {
1562 span.second = start + span.second - span.first;
1563 span.first = start;
1564 start += span.second - span.first;
1565 }
1566 };
1567
1568 node_index_type start = 0;
1569 SetRange( tree._spaceRoot , start );
1570 auto nodeFunctor = [&]( FEMTreeNode *node )
1571 {
1572 if( tree._isValidSpaceNode( node ) && !tree._isValidSpaceNode( node->children ) ) _sampleSpan[ node->nodeData.nodeIndex ].second = _sampleSpan[ node->nodeData.nodeIndex ].first;
1573 };
1574 tree._spaceRoot->processNodes( nodeFunctor );
1575
1576 for( node_index_type i=0 ; i<(node_index_type)samples.size() ; i++ )
1577 {
1578 const FEMTreeNode* leaf = samples[i].node;
1579 while( leaf && !tree._isValidSpaceNode( leaf ) ) leaf = leaf->parent;
1580 if( leaf && tree._isValidSpaceNode( leaf ) )
1581 {
1582 const ProjectiveData< Point< Real , Dim > , Real >& pData = samples[i].sample;
1583 DualPointAndDataInfo< Dim , Real , Data , T , PointD >& _pData = _iData[ _sampleSpan[ leaf->nodeData.nodeIndex ].second++ ];
1584 _pData.pointInfo.position = pData.data;
1585 _pData.pointInfo.weight = pData.weight;
1586 _pData.pointInfo.dualValues = _constraintDual( pData.data/pData.weight , sampleData[i]/pData.weight ) * pData.weight;
1587 _pData.data = sampleData[i];
1588 }
1589 }
1590
1591 ThreadPool::ParallelFor( 0 , _iData.size() , [&]( unsigned int , size_t i )
1592 {
1593 Real w = _iData[i].pointInfo.weight;
1594 _iData[i] /= w;
1595 if( noRescale ) _iData[i].pointInfo.weight = w;
1596 else _iData[i].pointInfo.weight = w * ( 1<<tree._maxDepth );
1597 _iData[i].pointInfo.dualValues *= _iData[i].pointInfo.weight;
1598 } );
1599 }
1600
1601 template< unsigned int Dim , class Real >
1602 template< typename T , unsigned int PointD , typename ConstraintDual , typename SystemDual >
1603 void FEMTree< Dim , Real >::ExactPointInterpolationInfo< T , PointD , ConstraintDual , SystemDual >::_init( const class FEMTree< Dim , Real >& tree , const std::vector< PointSample >& samples , bool noRescale )
1604 {
1605 _sampleSpan.resize( tree._nodeCount );
1606 ThreadPool::ParallelFor( 0 , tree.nodesSize() , [&]( unsigned int , size_t i ){ _sampleSpan[i] = std::pair< node_index_type , node_index_type >( 0 , 0 ); } );
1607 for( node_index_type i=0 ; i<(node_index_type)samples.size() ; i++ )
1608 {
1609 const FEMTreeNode* leaf = samples[i].node;
1610 while( leaf && !tree._isValidSpaceNode( leaf ) ) leaf = leaf->parent;
1611 if( leaf && tree._isValidSpaceNode( leaf ) ) _sampleSpan[ leaf->nodeData.nodeIndex ].second++;
1612 }
1613 _iData.resize( samples.size() );
1614
1615 std::function< void ( FEMTreeNode* , node_index_type & ) > SetRange = [&] ( FEMTreeNode* node , node_index_type &start )
1616 {
1617 std::pair< node_index_type , node_index_type >& span = _sampleSpan[ node->nodeData.nodeIndex ];
1618 if( tree._isValidSpaceNode( node->children ) )
1619 {
1620 for( int c=0 ; c<(1<<Dim) ; c++ ) SetRange( node->children + c , start );
1621 span.first = _sampleSpan[ node->children[0 ].nodeData.nodeIndex ].first;
1622 span.second = _sampleSpan[ node->children[ (1<<Dim)-1 ].nodeData.nodeIndex ].second;
1623 }
1624 else
1625 {
1626 span.second = start + span.second - span.first;
1627 span.first = start;
1628 start += span.second - span.first;
1629 }
1630 };
1631
1632 node_index_type start=0;
1633 SetRange( tree._spaceRoot , start );
1634
1635 auto nodeFunctor = [&]( FEMTreeNode *node )
1636 {
1637 if( tree._isValidSpaceNode( node ) && !tree._isValidSpaceNode( node->children ) ) _sampleSpan[ node->nodeData.nodeIndex ].second = _sampleSpan[ node->nodeData.nodeIndex ].first;
1638 };
1639 tree._spaceRoot->processNodes( nodeFunctor );
1640
1641 for( node_index_type i=0 ; i<(node_index_type)samples.size() ; i++ )
1642 {
1643 const FEMTreeNode* leaf = samples[i].node;
1644 while( leaf && !tree._isValidSpaceNode( leaf ) ) leaf = leaf->parent;
1645 if( leaf && tree._isValidSpaceNode( leaf ) )
1646 {
1647 const ProjectiveData< Point< Real , Dim > , Real >& pData = samples[i].sample;
1648 DualPointInfo< Dim , Real , T , PointD >& _pData = _iData[ _sampleSpan[ leaf->nodeData.nodeIndex ].second++ ];
1649 _pData.position = pData.data;
1650 _pData.dualValues = _constraintDual( pData.data/pData.weight ) * pData.weight;
1651 _pData.weight = pData.weight;
1652 }
1653 }
1654
1655 ThreadPool::ParallelFor( 0 , _iData.size() , [&]( unsigned int , size_t i )
1656 {
1657 Real w = _iData[i].weight;
1658 _iData[i] /= w;
1659 if( noRescale ) _iData[i].weight = w;
1660 else _iData[i].weight = w * ( 1<<tree._maxDepth );
1661 _iData[i].dualValues *= _iData[i].weight;
1662 } );
1663 }
1664
1665 template< unsigned int Dim , class Real >
1666 template< unsigned int PointD , typename ConstraintDual , typename SystemDual >
1667 void FEMTree< Dim , Real >::ExactPointInterpolationInfo< double , PointD , ConstraintDual , SystemDual >::_init( const class FEMTree< Dim , Real >& tree , const std::vector< PointSample >& samples , bool noRescale )
1668 {
1669 _sampleSpan.resize( tree._nodeCount );
1670 ThreadPool::ParallelFor( 0 , tree.nodesSize() , [&]( unsigned int , size_t i ){ _sampleSpan[i] = std::pair< node_index_type , node_index_type >( 0 , 0 ); } );
1671 for( node_index_type i=0 ; i<(node_index_type)samples.size() ; i++ )
1672 {
1673 const FEMTreeNode* leaf = samples[i].node;
1674 while( leaf && !tree._isValidSpaceNode( leaf ) ) leaf = leaf->parent;
1675 if( leaf && tree._isValidSpaceNode( leaf ) ) _sampleSpan[ leaf->nodeData.nodeIndex ].second++;
1676 }
1677 _iData.resize( samples.size() );
1678
1679 std::function< void ( FEMTreeNode* , node_index_type & ) > SetRange = [&] ( FEMTreeNode *node , node_index_type &start )
1680 {
1681 std::pair< node_index_type , node_index_type >& span = _sampleSpan[ node->nodeData.nodeIndex ];
1682 if( tree._isValidSpaceNode( node->children ) )
1683 {
1684 for( int c=0 ; c<(1<<Dim) ; c++ ) SetRange( node->children + c , start );
1685 span.first = _sampleSpan[ node->children[0 ].nodeData.nodeIndex ].first;
1686 span.second = _sampleSpan[ node->children[ (1<<Dim)-1 ].nodeData.nodeIndex ].second;
1687 }
1688 else
1689 {
1690 span.second = start + span.second - span.first;
1691 span.first = start;
1692 start += span.second - span.first;
1693 }
1694 };
1695
1696 node_index_type start = 0;
1697 SetRange( tree._spaceRoot , start );
1698 auto nodeFunctor = [&]( FEMTreeNode *node )
1699 {
1700 if( tree._isValidSpaceNode( node ) && !tree._isValidSpaceNode( node->children ) ) _sampleSpan[ node->nodeData.nodeIndex ].second = _sampleSpan[ node->nodeData.nodeIndex ].first;
1701 };
1702 tree._spaceRoot->processNodes( nodeFunctor );
1703
1704 for( node_index_type i=0 ; i<(node_index_type)samples.size() ; i++ )
1705 {
1706 const FEMTreeNode* leaf = samples[i].node;
1707 while( leaf && !tree._isValidSpaceNode( leaf ) ) leaf = leaf->parent;
1708 if( leaf && tree._isValidSpaceNode( leaf ) )
1709 {
1710 const ProjectiveData< Point< Real , Dim > , Real >& pData = samples[i].sample;
1711 DualPointInfo< Dim , Real , T , PointD >& _pData = _iData[ _sampleSpan[ leaf->nodeData.nodeIndex ].second++ ];
1712 _pData.position = pData.data;
1713 _pData.dualValues = _constraintDual( pData.data/pData.weight ) * pData.weight;
1714 _pData.weight = pData.weight;
1715 }
1716 }
1717
1718 ThreadPool::ParallelFor( 0 , _iData.size() , [&]( unsigned int , size_t i )
1719 {
1720 Real w = _iData[i].weight;
1721 _iData[i] /= w;
1722 if( noRescale ) _iData[i].weight = w;
1723 else _iData[i].weight = w * ( 1<<tree._maxDepth );
1724 _iData[i].dualValues *= _iData[i].weight;
1725 }
1726 );
1727 }
1728
1729 template< unsigned int Dim , class Real >
1730 template< typename T >
1731 bool FEMTree< Dim , Real >::_setInterpolationInfoFromChildren( FEMTreeNode* node , SparseNodeData< T , IsotropicUIntPack< Dim , FEMTrivialSignature > >& interpolationInfo ) const
1732 {
1733 if( IsActiveNode< Dim >( node->children ) )
1734 {
1735 bool hasChildData = false;
1736 T t = {};
1737 for( int c=0 ; c<(1<<Dim) ; c++ )
1738 if( _setInterpolationInfoFromChildren( node->children + c , interpolationInfo ) )
1739 {
1740 t += interpolationInfo[ node->children + c ];
1741 hasChildData = true;
1742 }
1743 if( hasChildData && IsActiveNode< Dim >( node ) ) interpolationInfo[ node ] += t;
1744 return hasChildData;
1745 }
1746 else return interpolationInfo( node )!=NULL;
1747 }
1748
1749 template< unsigned int Dim , class Real >
1750 template< typename T , unsigned int PointD , typename ConstraintDual >
1751 void FEMTree< Dim , Real >::_densifyInterpolationInfoAndSetDualConstraints( SparseNodeData< DualPointInfo< Dim , Real , T , PointD > , IsotropicUIntPack< Dim , FEMTrivialSignature > > & iInfo , const std::vector< PointSample >& samples , ConstraintDual constraintDual , LocalDepth maxDepth , int adaptiveExponent ) const
1752 {
1753 for( node_index_type i=0 ; i<(node_index_type)samples.size() ; i++ )
1754 {
1755 const FEMTreeNode* node = samples[i].node;
1756 const ProjectiveData< Point< Real , Dim > , Real >& pData = samples[i].sample;
1757 while( !IsActiveNode< Dim >( node ) ) node = node->parent;
1758 if( pData.weight )
1759 {
1760 DualPointInfo< Dim , Real , T , PointD >& _pData = iInfo[node];
1761 _pData.position += pData.data;
1762 _pData.weight += pData.weight;
1763 _pData.dualValues += constraintDual( pData.data/pData.weight ) * pData.weight;
1764 }
1765 }
1766
1767 // Set the interior values
1768 _setInterpolationInfoFromChildren( _spaceRoot , iInfo );
1769
1770 ThreadPool::ParallelFor( 0 , iInfo.size() , [&]( unsigned int , size_t i )
1771 {
1772 Real w = iInfo[i].weight;
1773 iInfo[i] /= w ; iInfo[i].weight = w;
1774 } );
1775
1776 // Set the average position and scale the weights
1777 auto nodeFunctor = [&]( const FEMTreeNode *node )
1778 {
1779 if( IsActiveNode< Dim >( node ) )
1780 {
1781 DualPointInfo< Dim , Real , T , PointD >* pData = iInfo( node );
1782 if( pData )
1783 {
1784 int e = _localDepth( node ) * adaptiveExponent - ( maxDepth ) * (adaptiveExponent-1);
1785 if( e<0 ) pData->weight /= Real( 1<<(-e) );
1786 else pData->weight *= Real( 1<< e );
1787 pData->dualValues *= pData->weight;
1788 }
1789 }
1790 };
1791 _tree.processNodes( nodeFunctor );
1792 }
1793 template< unsigned int Dim , class Real >
1794 template< typename T , typename Data , unsigned int PointD , typename ConstraintDual >
1795 void FEMTree< Dim , Real >::_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
1796 {
1797 for( node_index_type i=0 ; i<(node_index_type)samples.size() ; i++ )
1798 {
1799 const FEMTreeNode* node = samples[i].node;
1800 const ProjectiveData< Point< Real , Dim > , Real >& pData = samples[i].sample;
1801 while( !IsActiveNode< Dim >( node ) ) node = node->parent;
1802 if( pData.weight )
1803 {
1804 DualPointAndDataInfo< Dim , Real , Data , T , PointD >& _pData = iInfo[node];
1805 _pData.pointInfo.position += pData.data;
1806 _pData.pointInfo.dualValues += constraintDual( pData.data/pData.weight , sampleData[i]/pData.weight ) * pData.weight;
1807 _pData.pointInfo.weight += pData.weight;
1808 _pData.data += sampleData[i];
1809 }
1810 }
1811
1812 // Set the interior values
1813 _setInterpolationInfoFromChildren( _spaceRoot , iInfo );
1814
1815 ThreadPool::ParallelFor( 0 , iInfo.size() , [&]( unsigned int , size_t i )
1816 {
1817 Real w = iInfo[i].pointInfo.weight;
1818 iInfo[i] /= w ; iInfo[i].pointInfo.weight = w;
1819 }
1820 );
1821
1822 // Set the average position and scale the weights
1823 auto nodeFunctor = [&]( const FEMTreeNode *node )
1824 {
1825 if( IsActiveNode< Dim >( node ) )
1826 {
1827 DualPointAndDataInfo< Dim , Real , Data , T , PointD >* pData = iInfo( node );
1828 if( pData )
1829 {
1830 int e = _localDepth( node ) * adaptiveExponent - ( maxDepth ) * (adaptiveExponent-1);
1831 if( e<0 ) pData->pointInfo.weight /= Real( 1<<(-e) );
1832 else pData->pointInfo.weight *= Real( 1<< e );
1833 pData->pointInfo.dualValues *= pData->pointInfo.weight;
1834 }
1835 }
1836 };
1837 _tree.processNodes( nodeFunctor );
1838 }
1839 template< unsigned int Dim , class Real >
1840 template< typename T , unsigned int PointD , typename ConstraintDual >
1841 SparseNodeData< DualPointInfo< Dim , Real , T , PointD > , IsotropicUIntPack< Dim , FEMTrivialSignature > > FEMTree< Dim , Real >::_densifyInterpolationInfoAndSetDualConstraints( const std::vector< PointSample >& samples , ConstraintDual constraintDual , LocalDepth maxDepth , int adaptiveExponent ) const
1842 {
1843 SparseNodeData< DualPointInfo< Dim , Real , T , PointD > , IsotropicUIntPack< Dim , FEMTrivialSignature > > iInfo;
1844 _densifyInterpolationInfoAndSetDualConstraints( iInfo , samples , constraintDual , maxDepth , adaptiveExponent );
1845 return iInfo;
1846 }
1847 template< unsigned int Dim , class Real >
1848 template< typename T , typename Data , unsigned int PointD , typename ConstraintDual >
1849 SparseNodeData< DualPointAndDataInfo< Dim , Real , Data , T , PointD > , IsotropicUIntPack< Dim , FEMTrivialSignature > > FEMTree< Dim , Real >::_densifyInterpolationInfoAndSetDualConstraints( const std::vector< PointSample >& samples , ConstPointer( Data ) sampleData , ConstraintDual constraintDual , LocalDepth maxDepth , int adaptiveExponent ) const
1850 {
1851 SparseNodeData< DualPointAndDataInfo< Dim , Real , Data , T , PointD > , IsotropicUIntPack< Dim , FEMTrivialSignature > > iInfo;
1852 _densifyInterpolationInfoAndSetDualConstraints( iInfo , samples , sampleData , constraintDual , maxDepth , adaptiveExponent );
1853 return iInfo;
1854 }
1855
1856
1857 template< unsigned int Dim , class Real >
1858 template< typename T , unsigned int PointD , typename ConstraintDual >
1859 SparseNodeData< DualPointInfoBrood< Dim , Real , T , PointD > , IsotropicUIntPack< Dim , FEMTrivialSignature > > FEMTree< Dim , Real >::_densifyChildInterpolationInfoAndSetDualConstraints( const std::vector< PointSample >& samples , ConstraintDual constraintDual , bool noRescale ) const
1860 {
1861 SparseNodeData< DualPointInfoBrood< Dim , Real , T , PointD > , IsotropicUIntPack< Dim , FEMTrivialSignature > > iInfo;
1862 for( node_index_type i=0 ; i<samples.size() ; i++ )
1863 {
1864 const FEMTreeNode* node = samples[i].node;
1865 const ProjectiveData< Point< Real , Dim > , Real >& pData = samples[i].sample;
1866 while( !IsActiveNode< Dim >( node ) ) node = node->parent;
1867 if( pData.weight )
1868 {
1869 DualPointInfoBrood< Dim , Real , T , PointD >& _pData = iInfo[node];
1870 Point< Real , Dim > p = pData.data/pData.weight;
1871 int cIdx = _childIndex( node , p );
1872 _pData[cIdx].position += pData.data;
1873 _pData[cIdx].weight += pData.weight;
1874 _pData[cIdx].dualValues += constraintDual( p ) * pData.weight;
1875 }
1876 }
1877
1878 // Set the interior values
1879 _setInterpolationInfoFromChildren( _spaceRoot , iInfo );
1880
1881 ThreadPool::ParallelFor( 0 , iInfo.size() , [&]( unsigned int , size_t i )
1882 {
1883 iInfo[i].finalize();
1884 for( size_t c=0 ; c<iInfo[i].size() ; c++ )
1885 {
1886 iInfo[i][c].position /= iInfo[i][c].weight;
1887 if( !noRescale )
1888 {
1889 iInfo[i][c].weight *= ( 1<<_maxDepth );
1890 iInfo[i][c].dualValues *= ( 1<<_maxDepth );
1891 }
1892 }
1893 }
1894 );
1895 return iInfo;
1896 }
1897 template< unsigned int Dim , class Real >
1898 template< typename T , typename Data , unsigned int PointD , typename ConstraintDual >
1899 SparseNodeData< DualPointAndDataInfoBrood< Dim , Real , Data , T , PointD > , IsotropicUIntPack< Dim , FEMTrivialSignature > > FEMTree< Dim , Real >::_densifyChildInterpolationInfoAndSetDualConstraints( const std::vector< PointSample >& samples , ConstPointer( Data ) sampleData , ConstraintDual constraintDual , bool noRescale ) const
1900 {
1901 SparseNodeData< DualPointAndDataInfoBrood< Dim , Real , Data , T , PointD > , IsotropicUIntPack< Dim , FEMTrivialSignature > > iInfo;
1902 for( node_index_type i=0 ; i<samples.size() ; i++ )
1903 {
1904 const FEMTreeNode* node = samples[i].node;
1905 const ProjectiveData< Point< Real , Dim > , Real >& pData = samples[i].sample;
1906 while( !IsActiveNode< Dim >( node ) ) node = node->parent;
1907 if( pData.weight )
1908 {
1909 DualPointAndDataInfoBrood< Dim , Real , Data , T , PointD >& _pData = iInfo[node];
1910 Point< Real , Dim > p = pData.data/pData.weight;
1911 int cIdx = _childIndex( node , p );
1912 _pData[cIdx].pointInfo.position += pData.data;
1913 _pData[cIdx].pointInfo.dualValues += constraintDual( p , sampleData[i]/pData.weight ) * pData.weight;
1914 _pData[cIdx].pointInfo.weight += pData.weight;
1915 _pData[cIdx].data += sampleData[i];
1916 }
1917 }
1918
1919 // Set the interior values
1920 _setInterpolationInfoFromChildren( _spaceRoot , iInfo );
1921
1922 ThreadPool::ParallelFor( 0 , iInfo.size() , [&]( unsigned int , size_t i )
1923 {
1924 iInfo[i].finalize();
1925 for( size_t c=0 ; c<iInfo[i].size() ; c++ )
1926 {
1927 iInfo[i][c].pointInfo.position /= iInfo[i][c].pointInfo.weight;
1928 iInfo[i][c].data /= iInfo[i][c].pointInfo.weight;
1929 if( !noRescale )
1930 {
1931 iInfo[i][c].pointInfo.weight *= ( 1<<_maxDepth );
1932 iInfo[i][c].pointInfo.dualValues *= ( 1<<_maxDepth );
1933 iInfo[i][c].data *= ( 1<<_maxDepth );
1934 }
1935 }
1936 }
1937 );
1938 return iInfo;
1939 }
1940
1941
1942
1943 template< unsigned int Dim , class Real >
1944 std::vector< node_index_type > FEMTree< Dim , Real >::merge( FEMTree* tree )
1945 {
1946 std::vector< node_index_type > map;
1947 if( _depthOffset!=tree->_depthOffset ) MK_THROW( "depthOffsets don't match: %d != %d" , _depthOffset , tree->_depthOffset );
1948
1949 // Compute the next available index
1950 node_index_type nextIndex = 0;
1951 _tree.processNodes( [&]( const FEMTreeNode *node ){ nextIndex = std::max< node_index_type >( nextIndex , node->nodeData.nodeIndex+1 ); } );
1952
1953 // Set the size of the map
1954 {
1955 node_index_type mapSize = 0;
1956 tree->_tree.processNodes( [&]( const FEMTreeNode *node ){ mapSize = std::max< node_index_type >( mapSize , node->nodeData.nodeIndex+1 ); } );
1957 map.resize( mapSize );
1958 }
1959
1960 std::function< void ( FEMTreeNode* , FEMTreeNode* , std::vector< node_index_type > & , node_index_type & ) > MergeNodes = [&]( FEMTreeNode* node1 , FEMTreeNode* node2 , std::vector< node_index_type > &map , node_index_type &nextIndex )
1961 {
1962 if( node1 && node2 )
1963 {
1964 if( node2->nodeData.nodeIndex>=0 )
1965 {
1966 if( node1->nodeData.nodeIndex<0 ) node1->nodeData.nodeIndex = nextIndex++;
1967 map[ node2->nodeData.nodeIndex ] = node1->nodeData.nodeIndex;
1968 }
1969 if( node1->children && node2->children ) for( int c=0 ; c<(1<<Dim) ; c++ ) MergeNodes( node1->children+c , node2->children+c , map , nextIndex );
1970 else if( node2->children )
1971 {
1972 for( int c=0 ; c<(1<<Dim) ; c++ ) MergeNodes( NULL , node2->children+c , map , nextIndex );
1973 node1->children = node2->children;
1974 node2->children = NULL;
1975 for( int c=0 ; c<(1<<Dim) ; c++ ) node1->children[c].parent = node1;
1976 }
1977 }
1978 else if( node2 )
1979 {
1980 if( node2->nodeData.nodeIndex>=0 ){ map[ node2->nodeData.nodeIndex ] = nextIndex ; node2->nodeData.nodeIndex = nextIndex++; }
1981 if( node2->children ) for( int c=0 ; c<(1<<Dim) ; c++ ) MergeNodes( NULL , node2->children+c , map , nextIndex );
1982 }
1983 };
1984
1985 MergeNodes( _tree , tree->_tree , map , nextIndex );
1986 return map;
1987 }