Debian Codegraph @

src/thirdparty/PoissonRecon/RegularTree.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 // RegularTreeNode //
31 /////////////////////
32 template< unsigned int Dim , class NodeData , class DepthAndOffsetType >
33 RegularTreeNode< Dim , NodeData , DepthAndOffsetType >::RegularTreeNode( void )
34 {
35 parent = children = NULL;
36 _depth = 0;
37 memset( _offset , 0 , sizeof(_offset ) );
38 }
39 template< unsigned int Dim , class NodeData , class DepthAndOffsetType >
40 template< typename Initializer >
41 RegularTreeNode< Dim , NodeData , DepthAndOffsetType >::RegularTreeNode( Initializer &initializer ) : RegularTreeNode() { initializer( *this ); }
42
43 template< unsigned int Dim , class NodeData , class DepthAndOffsetType >
44 void RegularTreeNode< Dim , NodeData , DepthAndOffsetType >::cleanChildren( bool deleteChildren )
45 {
46 if( children )
47 {
48 for( int c=0 ; c<(1<<Dim) ; c++ ) children[c].cleanChildren( deleteChildren );
49 if( deleteChildren ) delete[] children;
50 }
51 children = NULL;
52 }
53
54 template< unsigned int Dim , class NodeData , class DepthAndOffsetType >
55 RegularTreeNode< Dim , NodeData , DepthAndOffsetType >::~RegularTreeNode(void)
56 {
57 #ifdef SHOW_WARNINGS
58 #pragma message( "[WARNING] Deallocation of children is your responsibility" )
59 #endif // SHOW_WARNINGS
60 parent = children = NULL;
61 }
62 template< unsigned int Dim , class NodeData , class DepthAndOffsetType >
63 template< typename Initializer >
64 RegularTreeNode< Dim , NodeData , DepthAndOffsetType >* RegularTreeNode< Dim , NodeData , DepthAndOffsetType >::NewBrood( Allocator< RegularTreeNode >* nodeAllocator , Initializer &initializer )
65 {
66 RegularTreeNode< Dim , NodeData , DepthAndOffsetType >* brood;
67 if( nodeAllocator ) brood = PointerAddress( nodeAllocator->newElements( 1<<Dim ) );
68 else brood = new RegularTreeNode[ 1<<Dim ];
69 for( int idx=0 ; idx<(1<<Dim) ; idx++ )
70 {
71 initializer( brood[idx] );
72 brood[idx]._depth = 0;
73 for( int d=0 ; d<Dim ; d++ ) brood[idx]._offset[d] = (idx>>d) & 1;
74 }
75 return brood;
76 }
77 template< unsigned int Dim , class NodeData , class DepthAndOffsetType >
78 void RegularTreeNode< Dim , NodeData , DepthAndOffsetType >::ResetDepthAndOffset( RegularTreeNode* root , int depth , int offset[Dim] )
79 {
80 root->_depth = depth;
81 for( unsigned int d=0 ; d<Dim ; d++ ) root->_offset[d] = offset[d];
82 if( root->children )
83 {
84 int _offset[Dim];
85 for( unsigned int d=0 ; d<Dim ; d++ ) _offset[d] = offset[d]<<1;
86 for( unsigned int c=0 ; c<(1<<Dim) ; c++ )
87 {
88 for( unsigned int d=0 ; d<Dim ; d++ ) _offset[d] = ( offset[d]<<1 ) | ( (c>>d) & 1 );
89 ResetDepthAndOffset( root->children + c , depth+1 , _offset );
90 }
91 }
92 }
93
94 template< unsigned int Dim , class NodeData , class DepthAndOffsetType >
95 template< typename Initializer >
96 void RegularTreeNode< Dim , NodeData , DepthAndOffsetType >::setFullDepth( int maxDepth , Allocator< RegularTreeNode >* nodeAllocator , Initializer &initializer )
97 {
98 if( maxDepth>0 )
99 {
100 if( !children ) initChildren< false >( nodeAllocator , initializer );
101 for( int i=0 ; i<(1<<Dim) ; i++ ) children[i].setFullDepth( maxDepth-1 , nodeAllocator , initializer );
102 }
103 }
104
105 template< unsigned int Dim , class NodeData , class DepthAndOffsetType >
106 template< typename PruneChildrenFunctor >
107 void RegularTreeNode< Dim , NodeData , DepthAndOffsetType >::pruneChildren( PruneChildrenFunctor pruneFunctor , bool deleteChildren )
108 {
109 if( children )
110 {
111 if( pruneFunctor( this ) ) cleanChildren( deleteChildren );
112 else for( int i=0 ; i<(1<<Dim) ; i++ ) children[i].pruneChildren( pruneFunctor , deleteChildren );
113 }
114 }
115
116 template< unsigned int Dim , class NodeData , class DepthAndOffsetType >
117 template< typename Initializer >
118 bool RegularTreeNode< Dim , NodeData , DepthAndOffsetType >::_initChildren( Allocator< RegularTreeNode >* nodeAllocator , Initializer &initializer )
119 {
120 if( nodeAllocator ) children = PointerAddress( nodeAllocator->newElements( 1<<Dim ) );
121 else
122 {
123 if( children ) delete[] children;
124 children = new RegularTreeNode[ 1<<Dim ];
125 }
126 if( !children ) MK_THROW( "Failed to initialize children" );
127 for( int idx=0 ; idx<(1<<Dim) ; idx++ )
128 {
129 children[idx].parent = this;
130 children[idx].children = NULL;
131 initializer( children[idx] );
132 children[idx]._depth = _depth+1;
133 for( int d=0 ; d<Dim ; d++ ) children[idx]._offset[d] = (_offset[d]<<1) | ( (idx>>d) & 1 );
134 }
135 return true;
136 }
137 template< unsigned int Dim , class NodeData , class DepthAndOffsetType >
138 template< typename Initializer >
139 bool RegularTreeNode< Dim , NodeData , DepthAndOffsetType >::_initChildren_s( Allocator< RegularTreeNode >* nodeAllocator , Initializer &initializer )
140 {
141 RegularTreeNode * volatile & children = this->children;
142 RegularTreeNode *_children;
143
144 // Allocate the children
145 if( nodeAllocator ) _children = PointerAddress( nodeAllocator->newElements( 1<<Dim ) );
146 else _children = new RegularTreeNode[ 1<<Dim ];
147 if( !_children ) MK_THROW( "Failed to initialize children" );
148 for( int idx=0 ; idx<(1<<Dim) ; idx++ )
149 {
150 _children[idx].parent = this;
151 _children[idx].children = NULL;
152 _children[idx]._depth = _depth+1;
153 for( int d=0 ; d<Dim ; d++ ) _children[idx]._offset[d] = (_offset[d]<<1) | ( (idx>>d) & 1 );
154 // [WARNING] We are assuming that it's OK to initialize nodes that may not be used.
155 for( int idx=0 ; idx<(1<<Dim) ; idx++ ) initializer( _children[idx] );
156 }
157
158 // If we are the first to set the child, initialize
159 if( SetAtomic( children , _children , (RegularTreeNode *)NULL ) ) return true;
160 // Otherwise clean up
161 else
162 {
163 if( nodeAllocator ) ;
164 else delete[] _children;
165 return false;
166 }
167 }
168
169 template< unsigned int Dim , class NodeData , class DepthAndOffsetType >
170 template< class MergeFunctor >
171 void RegularTreeNode< Dim , NodeData , DepthAndOffsetType >::merge( RegularTreeNode* node , MergeFunctor& f )
172 {
173 if( node )
174 {
175 nodeData = f( nodeData , node->nodeData );
176 if( children && node->children ) for( int c=0 ; c<(1<<Dim) ; c++ ) children[c].merge( node->children[c] , f );
177 else if( node->children )
178 {
179 children = node->children;
180 for( int c=0 ; c<(1<<Dim) ; c++ ) children[c].parent = this;
181 node->children = NULL;
182 }
183 }
184 }
185
186 template< unsigned int Dim , class NodeData , class DepthAndOffsetType >
187 inline typename RegularTreeNode< Dim , NodeData , DepthAndOffsetType >::DepthAndOffset RegularTreeNode< Dim , NodeData , DepthAndOffsetType >::depthAndOffset( void ) const
188 {
189 DepthAndOffset doff;
190 doff.depth = _depth;
191 for( int d=0 ; d<Dim ; d++ ) doff.offset[d] = _offset[d];
192 return doff;
193 }
194
195 template< unsigned int Dim , class NodeData , class DepthAndOffsetType >
196 inline void RegularTreeNode< Dim , NodeData , DepthAndOffsetType >::depthAndOffset( int& depth , int offset[Dim] ) const
197 {
198 depth = _depth;
199 for( int d=0 ; d<Dim ; d++ ) offset[d] = _offset[d];
200 }
201 template< unsigned int Dim , class NodeData , class DepthAndOffsetType >
202 inline void RegularTreeNode< Dim , NodeData , DepthAndOffsetType >::centerIndex( int index[Dim] ) const
203 {
204 for( int i=0 ; i<Dim ; i++ ) index[i] = BinaryNode::CenterIndex( _depth , _offset[i] );
205 }
206 template< unsigned int Dim , class NodeData , class DepthAndOffsetType >
207 inline int RegularTreeNode< Dim , NodeData , DepthAndOffsetType >::depth( void ) const { return _depth; }
208
209 template< unsigned int Dim , class NodeData , class DepthAndOffsetType >
210 template< class Real >
211 void RegularTreeNode< Dim , NodeData , DepthAndOffsetType >::centerAndWidth( Point< Real , Dim >& center , Real& width ) const
212 {
213 width = Real( 1.0 / (1<<_depth) );
214 for( int d=0 ; d<Dim ; d++ ) center[d] = Real( 0.5+_offset[d] ) * width;
215 }
216 template< unsigned int Dim , class NodeData , class DepthAndOffsetType >
217 template< class Real >
218 void RegularTreeNode< Dim , NodeData , DepthAndOffsetType >::startAndWidth( Point< Real , Dim >& start , Real& width ) const
219 {
220 width = Real( 1.0 / (1<<_depth) );
221 for( int d=0 ; d<Dim ; d++ ) start[d] = Real( _offset[d] ) * width;
222 }
223 template< unsigned int Dim , class NodeData , class DepthAndOffsetType >
224 template< class Real >
225 bool RegularTreeNode< Dim , NodeData , DepthAndOffsetType >::isInside( Point< Real , Dim > p ) const
226 {
227 Point< Real , Dim > c ; Real w;
228 centerAndWidth( c , w ) , w /= 2;
229 for( int d=0 ; d<Dim ; d++ ) if( p[d]<=(c[d]-w) || p[d]>(c[d]+w) ) return false;
230 return true;
231 }
232
233 template< unsigned int Dim , class NodeData , class DepthAndOffsetType >
234 int RegularTreeNode< Dim , NodeData , DepthAndOffsetType >::maxDepth(void) const
235 {
236 if( !children ) return 0;
237 else
238 {
239 int c , d;
240 for( int i=0 ; i<(1<<Dim) ; i++ )
241 {
242 d = children[i].maxDepth();
243 if( !i || d>c ) c=d;
244 }
245 return c+1;
246 }
247 }
248 template< unsigned int Dim , class NodeData , class DepthAndOffsetType >
249 size_t RegularTreeNode< Dim , NodeData , DepthAndOffsetType >::nodes( void ) const
250 {
251 if( !children ) return 1;
252 else
253 {
254 size_t c=0;
255 for( int i=0 ; i<(1<<Dim) ; i++ ) c += children[i].nodes();
256 return c+1;
257 }
258 }
259 template< unsigned int Dim , class NodeData , class DepthAndOffsetType >
260 size_t RegularTreeNode< Dim , NodeData , DepthAndOffsetType >::leaves( void ) const
261 {
262 if( !children ) return 1;
263 else
264 {
265 size_t c=0;
266 for( int i=0 ; i<(1<<Dim) ; i++ ) c += children[i].leaves();
267 return c;
268 }
269 }
270 template< unsigned int Dim , class NodeData , class DepthAndOffsetType >
271 size_t RegularTreeNode< Dim , NodeData , DepthAndOffsetType >::maxDepthLeaves( int maxDepth ) const
272 {
273 if( depth()>maxDepth ) return 0;
274 if( !children ) return 1;
275 else
276 {
277 size_t c=0;
278 for( int i=0 ; i<(1<<Dim) ; i++ ) c += children[i].maxDepthLeaves(maxDepth);
279 return c;
280 }
281 }
282 template< unsigned int Dim , class NodeData , class DepthAndOffsetType >
283 const RegularTreeNode< Dim , NodeData , DepthAndOffsetType >* RegularTreeNode< Dim , NodeData , DepthAndOffsetType >::root( void ) const
284 {
285 const RegularTreeNode* temp = this;
286 while( temp->parent ) temp = temp->parent;
287 return temp;
288 }
289
290 template< unsigned int Dim , class NodeData , class DepthAndOffsetType >
291 template< typename NodeFunctor /* = std::function< bool/void ( RegularTreeNode * ) > */ >
292 void RegularTreeNode< Dim , NodeData , DepthAndOffsetType >::processNodes( NodeFunctor nodeFunctor )
293 {
294 if constexpr( std::is_same< bool , typename std::invoke_result< NodeFunctor , RegularTreeNode * >::type >::value )
295 {
296 if( nodeFunctor( this ) && children )
297 for( int c=0 ; c<(1<<Dim) ; c++ )
298 if( nodeFunctor( children + c ) && children[c].children )
299 children[c]._processChildNodes( nodeFunctor );
300 }
301 else
302 {
303 nodeFunctor( this );
304 if( children ) for( int c=0 ; c<(1<<Dim) ; c++ )
305 {
306 nodeFunctor( children + c );
307 if( children[c].children ) children[c]._processChildNodes( nodeFunctor );
308 }
309 }
310 }
311
312 template< unsigned int Dim , class NodeData , class DepthAndOffsetType >
313 template< typename NodeFunctor /* = std::function< bool/void ( const RegularTreeNode * ) > */ >
314 void RegularTreeNode< Dim , NodeData , DepthAndOffsetType >::processNodes( NodeFunctor nodeFunctor ) const
315 {
316 if constexpr( std::is_same< bool , typename std::invoke_result< NodeFunctor , RegularTreeNode * >::type >::value )
317 {
318 if( nodeFunctor( this ) && children )
319 for( int c=0 ; c<(1<<Dim) ; c++ )
320 if( nodeFunctor( children + c ) && children[c].children )
321 children[c]._processChildNodes( nodeFunctor );
322 }
323 else
324 {
325 nodeFunctor( this );
326 if( children ) for( int c=0 ; c<(1<<Dim) ; c++ )
327 {
328 nodeFunctor( children + c );
329 if( children[c].children ) children[c]._processChildNodes( nodeFunctor );
330 }
331 }
332 }
333
334 template< unsigned int Dim , class NodeData , class DepthAndOffsetType >
335 template< typename NodeFunctor /* = std::function< void ( RegularTreeNode * ) > */ >
336 void RegularTreeNode< Dim , NodeData , DepthAndOffsetType >::processLeaves( NodeFunctor nodeFunctor )
337 {
338 if( children )
339 {
340 for( int c=0 ; c<(1<<Dim) ; c++ )
341 if( children[c].children ) children[c]._processChildLeaves( nodeFunctor );
342 else nodeFunctor( children+c );
343 }
344 else nodeFunctor( this );
345 }
346
347 template< unsigned int Dim , class NodeData , class DepthAndOffsetType >
348 template< typename NodeFunctor /* = std::function< void ( const RegularTreeNode * ) > */ >
349 void RegularTreeNode< Dim , NodeData , DepthAndOffsetType >::processLeaves( NodeFunctor nodeFunctor ) const
350 {
351 if( children )
352 {
353 for( int c=0 ; c<(1<<Dim) ; c++ )
354 if( children[c].children ) children[c]._processChildLeaves( nodeFunctor );
355 else nodeFunctor( children+c );
356 }
357 else nodeFunctor( this );
358 }
359
360 template< unsigned int Dim , class NodeData , class DepthAndOffsetType >
361 template< typename NodeFunctor /* = std::function< bool/void ( RegularTreeNode * ) > */ >
362 void RegularTreeNode< Dim , NodeData , DepthAndOffsetType >::_processChildNodes( NodeFunctor &nodeFunctor )
363 {
364 if constexpr( std::is_same< bool , typename std::invoke_result< NodeFunctor , RegularTreeNode * >::type >::value )
365 {
366 for( int c=0 ; c<(1<<Dim) ; c++ )
367 if( nodeFunctor( children + c ) && children[c].children )
368 children[c]._processChildNodes( nodeFunctor );
369 }
370 else
371 {
372 for( int c=0 ; c<(1<<Dim) ; c++ )
373 {
374 nodeFunctor( children + c );
375 if( children[c].children ) children[c]._processChildNodes( nodeFunctor );
376 }
377 }
378 }
379
380 template< unsigned int Dim , class NodeData , class DepthAndOffsetType >
381 template< typename NodeFunctor /* = std::function< bool/void ( const RegularTreeNode * ) > */ >
382 void RegularTreeNode< Dim , NodeData , DepthAndOffsetType >::_processChildNodes( NodeFunctor &nodeFunctor ) const
383 {
384 if constexpr( std::is_same< bool , typename std::invoke_result< NodeFunctor , RegularTreeNode * >::type >::value )
385 {
386 for( int c=0 ; c<(1<<Dim) ; c++ )
387 if( nodeFunctor( children + c ) && children[c].children )
388 children[c]._processChildNodes( nodeFunctor );
389 }
390 else
391 {
392 for( int c=0 ; c<(1<<Dim) ; c++ )
393 {
394 nodeFunctor( children + c );
395 if( children[c].children ) children[c]._processChildNodes( nodeFunctor );
396 }
397 }
398 }
399
400 template< unsigned int Dim , class NodeData , class DepthAndOffsetType >
401 template< typename NodeFunctor /* = std::function< void ( RegularTreeNode * ) > */ >
402 void RegularTreeNode< Dim , NodeData , DepthAndOffsetType >::_processChildLeaves( NodeFunctor &nodeFunctor )
403 {
404 for( int c=0 ; c<(1<<Dim) ; c++ )
405 if( children[c].children ) children[c]._processChildLeaves( nodeFunctor );
406 else nodeFunctor( children+c );
407 }
408
409 template< unsigned int Dim , class NodeData , class DepthAndOffsetType >
410 template< typename NodeFunctor /* = std::function< void ( const RegularTreeNode * ) > */ >
411 void RegularTreeNode< Dim , NodeData , DepthAndOffsetType >::_processChildLeaves( NodeFunctor &nodeFunctor ) const
412 {
413 for( int c=0 ; c<(1<<Dim) ; c++ )
414 if( children[c].children ) children[c]._processChildLeaves( nodeFunctor );
415 else nodeFunctor( children+c );
416 }
417
418 template< unsigned int Dim , class NodeData , class DepthAndOffsetType >
419 void RegularTreeNode< Dim , NodeData , DepthAndOffsetType >::printRange(void) const
420 {
421 Point< float , Dim > center;
422 float width;
423 centerAndWidth( center , width );
424 for( int d=0 ; d<Dim ; d++ )
425 {
426 printf( "[%f,%f]" , center[d]-width/2 , center[d]+width/2 );
427 if( d<Dim-1 ) printf( " x " );
428 else printf("\n");
429 }
430 }
431
432 template< unsigned int Dim , class NodeData , class DepthAndOffsetType >
433 template< class Real >
434 int RegularTreeNode< Dim , NodeData , DepthAndOffsetType >::ChildIndex( const Point< Real , Dim >& center , const Point< Real , Dim >& p )
435 {
436 int cIndex=0;
437 for( int d=0 ; d<Dim ; d++ ) if( p[d]>center[d] ) cIndex |= (1<<d);
438 return cIndex;
439 }
440
441 template< unsigned int Dim , class NodeData , class DepthAndOffsetType >
442 template< typename KeepNodeFunctor >
443 void RegularTreeNode< Dim , NodeData , DepthAndOffsetType >::copySubTree( RegularTreeNode< Dim , NodeData , DepthAndOffsetType > &subTree , const KeepNodeFunctor &keepNodeFunctor , Allocator< RegularTreeNode > *nodeAllocator ) const
444 {
445 bool copyChildren = false;
446 if( children ) for( unsigned int c=0 ; c<(1<<Dim) ; c++ ) copyChildren |= keepNodeFunctor( children+c );
447 if( copyChildren )
448 {
449 if( nodeAllocator ) subTree.children = PointerAddress( nodeAllocator->newElements( 1<<Dim ) );
450 else subTree.children = new RegularTreeNode[ 1<<Dim ];
451 for( unsigned int c=0 ; c<(1<<Dim) ; c++ )
452 {
453 subTree.children[c] = children[c];
454 subTree.children[c].children = NULL;
455 subTree.children[c].parent = &subTree;
456 children[c].copySubTree( subTree.children[c] , keepNodeFunctor );
457 }
458 }
459 }
460
461 // KeepNodeFunctor looks like std::function< bool ( const RegularTreeNode * ) >
462 template< unsigned int Dim , class NodeData , class DepthAndOffsetType >
463 template< typename KeepNodeFunctor >
464 Pointer( RegularTreeNode< Dim , NodeData , DepthAndOffsetType > ) RegularTreeNode< Dim , NodeData , DepthAndOffsetType >::serializeSubTree( const KeepNodeFunctor &keepNodeFunctor , size_t &nodeCount ) const
465 {
466 std::function< size_t ( const RegularTreeNode * ) > ChildNodeCount = [&]( const RegularTreeNode *node )
467 {
468 size_t count = 0;
469 bool keepChildren = false;
470 if( node->children ) for( unsigned int c=0 ; c<(1<<Dim) ; c++ ) keepChildren |= keepNodeFunctor( node->children+c );
471 if( keepChildren )
472 {
473 count += 1<<Dim;
474 for( unsigned int c=0 ; c<(1<<Dim) ; c++ ) count += ChildNodeCount( node->children+c );
475 }
476 return count;
477 };
478
479 nodeCount = 1 + ChildNodeCount( this );
480 Pointer( RegularTreeNode ) nodes = NewPointer< RegularTreeNode >( nodeCount );
481
482 std::function< Pointer( RegularTreeNode ) ( const RegularTreeNode * , RegularTreeNode & , Pointer( RegularTreeNode ) ) > SetChildNodes = [&]( const RegularTreeNode *node , RegularTreeNode &subNode , Pointer( RegularTreeNode ) buffer )
483 {
484 bool keepChildren = false;
485 if( node->children ) for( unsigned int c=0 ; c<(1<<Dim) ; c++ ) keepChildren |= keepNodeFunctor( node->children+c );
486 if( keepChildren )
487 {
488 subNode.children = PointerAddress( buffer );
489 for( unsigned int c=0 ; c<(1<<Dim) ; c++ )
490 {
491 buffer[c] = node->children[c];
492 buffer[c].parent = &subNode;
493 buffer[c].children = NULL;
494 }
495 buffer += 1<<Dim;
496 for( unsigned int c=0 ; c<(1<<Dim) ; c++ ) buffer = SetChildNodes( node->children+c , subNode.children[c] , buffer );
497 }
498 return buffer;
499 };
500
501 nodes[0] = *this;
502 nodes[0].parent = NULL;
503 nodes[0].children = NULL;
504
505 SetChildNodes( this , nodes[0] , nodes+1 );
506
507 return nodes;
508 }
509
510 template< unsigned int Dim , class NodeData , class DepthAndOffsetType >
511 template< typename WriteNodeFunctor >
512 bool RegularTreeNode< Dim , NodeData , DepthAndOffsetType >::write( BinaryStream &stream , bool serialize , const WriteNodeFunctor &writeNodeFunctor ) const
513 {
514 if( serialize )
515 {
516 size_t nodeCount;
517 Pointer( RegularTreeNode ) nodes = serializeSubTree( writeNodeFunctor , nodeCount );
518 stream.write( nodes , nodeCount );
519 DeletePointer( nodes );
520 }
521 else
522 {
523 std::function< void ( const RegularTreeNode *node ) > WriteChildren = [&]( const RegularTreeNode *node )
524 {
525 bool writeChildren = false;
526 if( node->children ) for( unsigned int c=0 ; c<(1<<Dim) ; c++ ) writeChildren |= writeNodeFunctor( node->children+c );
527 if( writeChildren )
528 {
529 stream.write( GetPointer( node->children , 1<<Dim ) , 1<<Dim );
530 for( unsigned int c=0 ; c<(1<<Dim) ; c++ ) WriteChildren( node->children+c );
531 }
532 };
533 stream.write( *this );
534 WriteChildren( this );
535 }
536 return true;
537 }
538
539 template< unsigned int Dim , class NodeData , class DepthAndOffsetType >
540 template< typename Initializer >
541 bool RegularTreeNode< Dim , NodeData , DepthAndOffsetType >::read( BinaryStream &stream , Allocator< RegularTreeNode >* nodeAllocator , Initializer &initializer )
542 {
543 std::function< void ( RegularTreeNode *node ) > ReadChildren = [&]( RegularTreeNode *node )
544 {
545 if( node->children )
546 {
547 node->children = NULL;
548 node->initChildren< false >( nodeAllocator , initializer );
549 if( !stream.read( GetPointer( node->children , 1<<Dim ) , 1<<Dim ) ) MK_THROW( "Failed to read children" );
550 for( int i=0 ; i<(1<<Dim) ; i++ )
551 {
552 node->children[i].parent = node;
553 ReadChildren( node->children+i );
554 }
555 }
556 };
557 if( !stream.read( *this ) ) MK_THROW( "Failed to read root" );
558 ReadChildren( this );
559 return true;
560 }
561
562 template< unsigned int Dim , class NodeData , class DepthAndOffsetType >
563 int RegularTreeNode< Dim , NodeData , DepthAndOffsetType >::width( int maxDepth ) const
564 {
565 int d=depth();
566 return 1<<(maxDepth-d);
567 }
568
569 ////////////////////////////////
570 // RegularTreeNode::Neighbors //
571 ////////////////////////////////
572 template< unsigned int Dim , class NodeData , class DepthAndOffsetType >
573 template< unsigned int ... Widths >
574 RegularTreeNode< Dim , NodeData , DepthAndOffsetType >::Neighbors< ParameterPack::UIntPack< Widths ... > >::Neighbors( void ){ static_assert( sizeof...(Widths)==Dim , "[ERROR] Window and tree dimensions don't match" ) ; clear(); }
575 template< unsigned int Dim , class NodeData , class DepthAndOffsetType >
576 template< unsigned int ... Widths >
577 void RegularTreeNode< Dim , NodeData , DepthAndOffsetType >::Neighbors< ParameterPack::UIntPack< Widths ... > >::clear( void ){ for( unsigned int i=0 ; i<Window::Size< Widths... >() ; i++ ) neighbors.data[i] = NULL; }
578
579 /////////////////////////////////////
580 // RegularTreeNode::ConstNeighbors //
581 /////////////////////////////////////
582 template< unsigned int Dim , class NodeData , class DepthAndOffsetType >
583 template< unsigned int ... Widths >
584 RegularTreeNode< Dim , NodeData , DepthAndOffsetType >::ConstNeighbors< ParameterPack::UIntPack< Widths ... > >::ConstNeighbors( void ){ static_assert( sizeof...(Widths)==Dim , "[ERROR] Window and tree dimensions don't match" ) ; clear(); }
585 template< unsigned int Dim , class NodeData , class DepthAndOffsetType >
586 template< unsigned int ... Widths >
587 void RegularTreeNode< Dim , NodeData , DepthAndOffsetType >::ConstNeighbors< ParameterPack::UIntPack< Widths ... > >::clear( void ){ for( unsigned int i=0 ; i<Window::Size< Widths... >() ; i++ ) neighbors.data[i] = NULL; }
588
589 //////////////////////////////////
590 // RegularTreeNode::NeighborKey //
591 //////////////////////////////////
592 template< unsigned int Dim , class NodeData , class DepthAndOffsetType >
593 template< unsigned int ... LeftRadii , unsigned int ... RightRadii >
594 RegularTreeNode< Dim , NodeData , DepthAndOffsetType >::NeighborKey< ParameterPack::UIntPack< LeftRadii ... > , ParameterPack::UIntPack< RightRadii ... > >::NeighborKey( void ){ _depth=-1 , neighbors=NULL; }
595 template< unsigned int Dim , class NodeData , class DepthAndOffsetType >
596 template< unsigned int ... LeftRadii , unsigned int ... RightRadii >
597 RegularTreeNode< Dim , NodeData , DepthAndOffsetType >::NeighborKey< ParameterPack::UIntPack< LeftRadii ... > , ParameterPack::UIntPack< RightRadii ... > >::NeighborKey( const NeighborKey& key )
598 {
599 _depth = 0 , neighbors = NULL;
600 set( key._depth );
601 for( int d=0 ; d<=_depth ; d++ ) memcpy( &neighbors[d] , &key.neighbors[d] , sizeof( Neighbors< ParameterPack::UIntPack< ( LeftRadii + RightRadii + 1 ) ... > > ) );
602 }
603 template< unsigned int Dim , class NodeData , class DepthAndOffsetType >
604 template< unsigned int ... LeftRadii , unsigned int ... RightRadii >
605 RegularTreeNode< Dim , NodeData , DepthAndOffsetType >::NeighborKey< ParameterPack::UIntPack< LeftRadii ... > , ParameterPack::UIntPack< RightRadii ... > >::~NeighborKey( void )
606 {
607 if( neighbors ) delete[] neighbors;
608 neighbors=NULL;
609 }
610 template< unsigned int Dim , class NodeData , class DepthAndOffsetType >
611 template< unsigned int ... LeftRadii , unsigned int ... RightRadii >
612 void RegularTreeNode< Dim , NodeData , DepthAndOffsetType >::NeighborKey< ParameterPack::UIntPack< LeftRadii ... > , ParameterPack::UIntPack< RightRadii ... > >::set( int d )
613 {
614 if( neighbors ) delete[] neighbors;
615 neighbors = NULL;
616 _depth = d;
617 if( d<0 ) return;
618 neighbors = new NeighborType[d+1];
619 }
620
621 template< unsigned int Dim , class NodeData , class DepthAndOffsetType >
622 template< unsigned int ... LeftRadii , unsigned int ... RightRadii >
623 template< bool CreateNodes , bool ThreadSafe , typename NodeInitializer , unsigned int ... _PLeftRadii , unsigned int ... _PRightRadii , unsigned int ... _CLeftRadii , unsigned int ... _CRightRadii >
624 unsigned int RegularTreeNode< Dim , NodeData , DepthAndOffsetType >::NeighborKey< ParameterPack::UIntPack< LeftRadii ... > , ParameterPack::UIntPack< RightRadii ... > >::_NeighborsLoop( ParameterPack::UIntPack< _PLeftRadii ... > pLeftRadii , ParameterPack::UIntPack< _PRightRadii ... > pRightRadii , ParameterPack::UIntPack< _CLeftRadii ... > cLeftRadii , ParameterPack::UIntPack< _CRightRadii ... > cRightRadii , Window::ConstSlice< RegularTreeNode* , ( _PLeftRadii + _PRightRadii + 1 ) ... > pNeighbors , Window::Slice< RegularTreeNode* , ( _CLeftRadii + _CRightRadii + 1 ) ... > cNeighbors , int cIdx , Allocator< RegularTreeNode >* nodeAllocator , NodeInitializer &initializer )
625 {
626 static_assert( Dim==sizeof ... ( _PLeftRadii ) && Dim==sizeof ... ( _PRightRadii ) && Dim==sizeof ... ( _CLeftRadii ) && Dim==sizeof ... ( _CRightRadii ) , "[ERROR] Dimensions don't match" );
627 int c[Dim];
628 for( int d=0 ; d<Dim ; d++ ) c[d] = ( cIdx>>d ) & 1;
629 return _Run< CreateNodes , ThreadSafe , NodeInitializer , ParameterPack::UIntPack< _PLeftRadii ... > , ParameterPack::UIntPack< _PRightRadii ... > , ParameterPack::UIntPack< _CLeftRadii ... > , ParameterPack::UIntPack< _CRightRadii ... > >::Run( pNeighbors , cNeighbors , c , 0 , nodeAllocator , initializer );
630 }
631
632 template< unsigned int Dim , class NodeData , class DepthAndOffsetType >
633 template< unsigned int ... LeftRadii , unsigned int ... RightRadii >
634 template< bool CreateNodes , bool ThreadSafe , typename NodeInitializer , unsigned int ... _PLeftRadii , unsigned int ... _PRightRadii , unsigned int ... _CLeftRadii , unsigned int ... _CRightRadii >
635 unsigned int RegularTreeNode< Dim , NodeData , DepthAndOffsetType >::NeighborKey< ParameterPack::UIntPack< LeftRadii ... > , ParameterPack::UIntPack< RightRadii ... > >::_NeighborsLoop( ParameterPack::UIntPack< _PLeftRadii ... > pLeftRadii , ParameterPack::UIntPack< _PRightRadii ... > pRightRadii , ParameterPack::UIntPack< _CLeftRadii ... > cLeftRadii , ParameterPack::UIntPack< _CRightRadii ... > cRightRadii , Window::Slice< RegularTreeNode* , ( _PLeftRadii + _PRightRadii + 1 ) ... > pNeighbors , Window::Slice< RegularTreeNode* , ( _CLeftRadii + _CRightRadii + 1 ) ... > cNeighbors , int cIdx , Allocator< RegularTreeNode >* nodeAllocator , NodeInitializer &initializer )
636 {
637 return _NeighborsLoop< CreateNodes , ThreadSafe , NodeInitializer >( ParameterPack::UIntPack< _PLeftRadii ... >() , ParameterPack::UIntPack< _PRightRadii ... >() , ParameterPack::UIntPack< _CLeftRadii ... >() , ParameterPack::UIntPack< _CRightRadii ... >() , ( Window::ConstSlice< RegularTreeNode* , ( _PLeftRadii + _PRightRadii + 1 ) ... > )pNeighbors , cNeighbors , cIdx , nodeAllocator , initializer );
638 }
639
640 template< unsigned int Dim , class NodeData , class DepthAndOffsetType >
641 template< unsigned int ... LeftRadii , unsigned int ... RightRadii >
642 template< bool CreateNodes , bool ThreadSafe , typename NodeInitializer , unsigned int _PLeftRadius , unsigned int ... _PLeftRadii , unsigned int _PRightRadius , unsigned int ... _PRightRadii , unsigned int _CLeftRadius , unsigned int ... _CLeftRadii , unsigned int _CRightRadius , unsigned int ... _CRightRadii >
643 unsigned int RegularTreeNode< Dim , NodeData , DepthAndOffsetType >::NeighborKey< ParameterPack::UIntPack< LeftRadii ... > , ParameterPack::UIntPack< RightRadii ... > >::_Run< CreateNodes , ThreadSafe , NodeInitializer , ParameterPack::UIntPack< _PLeftRadius , _PLeftRadii ... > , ParameterPack::UIntPack< _PRightRadius , _PRightRadii ... > , ParameterPack::UIntPack< _CLeftRadius , _CLeftRadii ... > , ParameterPack::UIntPack< _CRightRadius , _CRightRadii ... > >::Run( Window::ConstSlice< RegularTreeNode* , _PLeftRadius + _PRightRadius + 1 , ( _PLeftRadii + _PRightRadii + 1 ) ... > pNeighbors , Window::Slice< RegularTreeNode* , _CLeftRadius + _CRightRadius + 1 , ( _CLeftRadii + _CRightRadii + 1 ) ... > cNeighbors , int* c , int cornerIndex , Allocator< RegularTreeNode >* nodeAllocator , NodeInitializer &initializer )
644 {
645 static const int D = sizeof ... ( _PLeftRadii ) + 1;
646 unsigned int count=0;
647 for( int i=-(int)_CLeftRadius ; i<=(int)_CRightRadius ; i++ )
648 {
649 int _i = (i+c[Dim-D]) + ( _CLeftRadius<<1 ) , pi = ( _i>>1 ) - _CLeftRadius + _PLeftRadius , ci = i + _CLeftRadius;
650 count += _Run< CreateNodes , ThreadSafe , NodeInitializer , ParameterPack::UIntPack< _PLeftRadii ... > , ParameterPack::UIntPack< _PRightRadii ... > , ParameterPack::UIntPack< _CLeftRadii ... > , ParameterPack::UIntPack< _CRightRadii ... > >::Run( pNeighbors[pi] , cNeighbors[ci] , c , cornerIndex | ( ( _i&1)<<(Dim-D) ) , nodeAllocator , initializer );
651 }
652 return count;
653 }
654
655
656 template< unsigned int Dim , class NodeData , class DepthAndOffsetType >
657 template< unsigned int ... LeftRadii , unsigned int ... RightRadii >
658 template< bool CreateNodes , bool ThreadSafe , typename NodeInitializer , unsigned int _PLeftRadius , unsigned int _PRightRadius , unsigned int _CLeftRadius , unsigned int _CRightRadius >
659 unsigned int RegularTreeNode< Dim , NodeData , DepthAndOffsetType >::NeighborKey< ParameterPack::UIntPack< LeftRadii ... > , ParameterPack::UIntPack< RightRadii ... > >::_Run< CreateNodes , ThreadSafe , NodeInitializer , ParameterPack::UIntPack< _PLeftRadius > , ParameterPack::UIntPack< _PRightRadius > , ParameterPack::UIntPack< _CLeftRadius > , ParameterPack::UIntPack< _CRightRadius > >::Run( Window::ConstSlice< RegularTreeNode* , _PLeftRadius+_PRightRadius+1 > pNeighbors , Window::Slice< RegularTreeNode* , _CLeftRadius+_CRightRadius+1 > cNeighbors , int* c , int cornerIndex , Allocator< RegularTreeNode >* nodeAllocator , NodeInitializer &initializer )
660 {
661 static const int D = 1;
662 unsigned int count=0;
663 for( int i=-(int)_CLeftRadius ; i<=(int)_CRightRadius ; i++ )
664 {
665 int _i = (i+c[Dim-1]) + ( _CLeftRadius<<1 ) , pi = ( _i>>1 ) - _CLeftRadius + _PLeftRadius , ci = i + _CLeftRadius;
666 if( CreateNodes )
667 {
668 if( pNeighbors[pi] )
669 {
670 #ifdef SANITIZED_PR
671 //#ifdef NEW_CODE
672 RegularTreeNode * children = ReadAtomic( pNeighbors[pi]->children );
673 if( !children )
674 {
675 pNeighbors[pi]->template initChildren< ThreadSafe >( nodeAllocator , initializer );
676 children = ReadAtomic( pNeighbors[pi]->children );
677 }
678 cNeighbors[ci] = children + ( cornerIndex | ( ( _i&1)<<(Dim-1) ) );
679 #else // !SANITIZED_PR
680 if( !pNeighbors[pi]->children ) pNeighbors[pi]->template initChildren< ThreadSafe >( nodeAllocator , initializer );
681 cNeighbors[ci] = pNeighbors[pi]->children + ( cornerIndex | ( ( _i&1)<<(Dim-1) ) );
682 #endif // SANITIZED_PR
683 count++;
684 }
685 else cNeighbors[ci] = NULL;
686 }
687 else
688 {
689 if( pNeighbors[pi] && pNeighbors[pi]->children ) cNeighbors[ci] = pNeighbors[pi]->children + ( cornerIndex | ( ( _i&1)<<(Dim-1) ) ) , count++;
690 else cNeighbors[ci] = NULL;
691 }
692 }
693 return count;
694 }
695
696 template< unsigned int Dim , class NodeData , class DepthAndOffsetType >
697 template< unsigned int ... LeftRadii , unsigned int ... RightRadii >
698 template< bool CreateNodes , bool ThreadSafe , typename NodeInitializer >
699 unsigned int RegularTreeNode< Dim , NodeData , DepthAndOffsetType >::NeighborKey< ParameterPack::UIntPack< LeftRadii ... > , ParameterPack::UIntPack< RightRadii ... > >::getChildNeighbors( int cIdx , int d , NeighborType& cNeighbors , Allocator< RegularTreeNode >* nodeAllocator , NodeInitializer &initializer ) const
700 {
701 NeighborType& pNeighbors = neighbors[d];
702 // Check that we actually have a center node
703 if( !pNeighbors.neighbors.data[ CenterIndex ] ) return 0;
704 return _NeighborsLoop< CreateNodes , ThreadSafe >( ParameterPack::UIntPack< LeftRadii ... >() , ParameterPack::UIntPack< RightRadii ... >() , ParameterPack::UIntPack< LeftRadii ... >() , ParameterPack::UIntPack< RightRadii ... >() , pNeighbors.neighbors() , cNeighbors.neighbors() , cIdx , nodeAllocator , initializer );
705 }
706
707 template< unsigned int Dim , class NodeData , class DepthAndOffsetType >
708 template< unsigned int ... LeftRadii , unsigned int ... RightRadii >
709 template< bool CreateNodes , bool ThreadSafe , typename NodeInitializer , class Real >
710 unsigned int RegularTreeNode< Dim , NodeData , DepthAndOffsetType >::NeighborKey< ParameterPack::UIntPack< LeftRadii ... > , ParameterPack::UIntPack< RightRadii ... > >::getChildNeighbors( Point< Real , Dim > p , int d , NeighborType& cNeighbors , Allocator< RegularTreeNode >* nodeAllocator , NodeInitializer &initializer ) const
711 {
712 NeighborType& pNeighbors = neighbors[d];
713 // Check that we actually have a center node
714 if( !pNeighbors.neighbors.data[ CenterIndex ] ) return 0;
715 Point< Real , Dim > c;
716 Real w;
717 pNeighbors.neighbors.data[ CenterIndex ]->centerAndWidth( c , w );
718 return getChildNeighbors< CreateNodes , ThreadSafe >( CornerIndex( c , p ) , d , cNeighbors , nodeAllocator , initializer );
719 }
720
721 template< unsigned int Dim , class NodeData , class DepthAndOffsetType >
722 template< unsigned int ... LeftRadii , unsigned int ... RightRadii >
723 template< bool CreateNodes , bool ThreadSafe , typename NodeInitializer >
724 typename RegularTreeNode< Dim , NodeData , DepthAndOffsetType >::template Neighbors< ParameterPack::UIntPack< ( LeftRadii + RightRadii + 1 ) ... > >& RegularTreeNode< Dim , NodeData , DepthAndOffsetType >::NeighborKey< ParameterPack::UIntPack< LeftRadii ... > , ParameterPack::UIntPack< RightRadii ... > >::getNeighbors( RegularTreeNode< Dim , NodeData , DepthAndOffsetType >* node , Allocator< RegularTreeNode >* nodeAllocator , NodeInitializer &initializer )
725 {
726 NeighborType& neighbors = this->neighbors[node->depth()];
727 // This is required in case the neighbors have been constructed between the last call to getNeighbors and this one
728 if( node==neighbors.neighbors.data[ CenterIndex ] )
729 {
730 bool reset = false;
731 for( unsigned int i=0 ; i<Window::Size< ( LeftRadii+RightRadii+1 ) ... >() ; i++ ) if( !neighbors.neighbors.data[i] ) reset = true;
732 if( reset ) neighbors.neighbors.data[ CenterIndex ] = NULL;
733 }
734 if( node!=neighbors.neighbors.data[ CenterIndex ] )
735 {
736 for( int d=node->depth()+1 ; d<=_depth && this->neighbors[d].neighbors.data[ CenterIndex ] ; d++ ) this->neighbors[d].neighbors.data[ CenterIndex ] = NULL;
737 neighbors.clear();
738 if( !node->parent ) neighbors.neighbors.data[ CenterIndex ] = node;
739 else _NeighborsLoop< CreateNodes , ThreadSafe >( ParameterPack::UIntPack< LeftRadii ... >() , ParameterPack::UIntPack< RightRadii ... >() , ParameterPack::UIntPack< LeftRadii ... >() , ParameterPack::UIntPack< RightRadii ... >() , getNeighbors< CreateNodes , ThreadSafe >( node->parent , nodeAllocator , initializer ).neighbors() , neighbors.neighbors() , (int)( node - node->parent->children ) , nodeAllocator , initializer );
740 }
741 return neighbors;
742 }
743
744 template< unsigned int Dim , class NodeData , class DepthAndOffsetType >
745 template< unsigned int ... LeftRadii , unsigned int ... RightRadii >
746 template< bool CreateNodes , bool ThreadSafe , typename NodeInitializer , unsigned int ... _LeftRadii , unsigned int ... _RightRadii >
747 void RegularTreeNode< Dim , NodeData , DepthAndOffsetType >::NeighborKey< ParameterPack::UIntPack< LeftRadii ... > , ParameterPack::UIntPack< RightRadii ... > >::getNeighbors( ParameterPack::UIntPack< _LeftRadii ... > , ParameterPack::UIntPack< _RightRadii ... > , RegularTreeNode< Dim , NodeData , DepthAndOffsetType >* node , Neighbors< ParameterPack::UIntPack< ( _LeftRadii + _RightRadii + 1 ) ... > >& neighbors , Allocator< RegularTreeNode >* nodeAllocator , NodeInitializer &initializer )
748 {
749 static const unsigned int _CenterIndex = Window::Index( ParameterPack::UIntPack< ( _LeftRadii + _RightRadii + 1 ) ... >() , ParameterPack::UIntPack< _LeftRadii ... >() );
750 neighbors.clear();
751 if( !node ) return;
752
753 // [WARNING] This estimate of the required radius is somewhat conservative if the readius is odd (depending on where the node is relative to its parent)
754 ParameterPack::UIntPack< LeftRadii ... > leftRadii;
755 ParameterPack::UIntPack< RightRadii ... > rightRadii;
756 ParameterPack::UIntPack< ( _LeftRadii+1 )/2 ... > pLeftRadii;
757 ParameterPack::UIntPack< ( _RightRadii+1 )/2 ... > pRightRadii;
758 ParameterPack::UIntPack< _LeftRadii ... > cLeftRadii;
759 ParameterPack::UIntPack< _RightRadii ... > cRightRadii;
760
761 // If we are at the root of the tree, we are done
762 if( !node->parent ) neighbors.neighbors.data[ _CenterIndex ] = node;
763 // If we can get the data from the the key for the parent node, do that
764 else if( pLeftRadii<=leftRadii && pRightRadii<=rightRadii )
765 {
766 getNeighbors< CreateNodes , ThreadSafe >( node->parent , nodeAllocator , initializer );
767 const Neighbors< ParameterPack::UIntPack< ( LeftRadii + RightRadii + 1 ) ... > >& pNeighbors = this->neighbors[ node->depth()-1 ];
768 _NeighborsLoop< CreateNodes , ThreadSafe >( leftRadii , rightRadii , cLeftRadii , cRightRadii , pNeighbors.neighbors() , neighbors.neighbors() , (int)( node - node->parent->children ) , nodeAllocator , initializer );
769 }
770 // Otherwise recurse
771 else
772 {
773 Neighbors< ParameterPack::UIntPack< ( ( _LeftRadii+1 )/2 + ( _RightRadii+1 )/2 + 1 ) ... > > pNeighbors;
774 getNeighbors< CreateNodes , ThreadSafe >( pLeftRadii , pRightRadii , node->parent , pNeighbors , nodeAllocator , initializer );
775 _NeighborsLoop< CreateNodes , ThreadSafe >( pLeftRadii , pRightRadii , cLeftRadii , cRightRadii , pNeighbors.neighbors() , neighbors.neighbors() , (int)( node - node->parent->children ) , nodeAllocator , initializer );
776 }
777 }
778
779 template< unsigned int Dim , class NodeData , class DepthAndOffsetType >
780 template< unsigned int ... LeftRadii , unsigned int ... RightRadii >
781 template< bool CreateNodes , bool ThreadSafe , typename NodeInitializer , unsigned int ... _LeftRadii , unsigned int ... _RightRadii >
782 void RegularTreeNode< Dim , NodeData , DepthAndOffsetType >::NeighborKey< ParameterPack::UIntPack< LeftRadii ... > , ParameterPack::UIntPack< RightRadii ... > >::getNeighbors( ParameterPack::UIntPack< _LeftRadii ... > , ParameterPack::UIntPack< _RightRadii ... > , RegularTreeNode< Dim , NodeData , DepthAndOffsetType >* node , Neighbors< ParameterPack::UIntPack< ( _LeftRadii + _RightRadii + 1 ) ... > >& pNeighbors , Neighbors< ParameterPack::UIntPack< ( _LeftRadii + _RightRadii + 1 ) ... > >& neighbors , Allocator< RegularTreeNode >* nodeAllocator , NodeInitializer &initializer )
783 {
784 ParameterPack::UIntPack< _LeftRadii ... > leftRadii;
785 ParameterPack::UIntPack< _RightRadii ... > rightRadii;
786 if( !node->parent ) getNeighbors< CreateNodes , ThreadSafe >( leftRadii , rightRadii , node , neighbors , nodeAllocator , initializer );
787 else
788 {
789 getNeighbors< CreateNodes , ThreadSafe >( leftRadii , rightRadii , node->parent , pNeighbors , nodeAllocator , initializer );
790 _NeighborsLoop< CreateNodes , ThreadSafe >( leftRadii , rightRadii , leftRadii , rightRadii , pNeighbors.neighbors() , neighbors.neighbors() , (int)( node - node->parent->children ) , nodeAllocator , initializer );
791 }
792 }
793
794 template< unsigned int Dim , class NodeData , class DepthAndOffsetType >
795 template< unsigned int ... LeftRadii , unsigned int ... RightRadii >
796 void RegularTreeNode< Dim , NodeData , DepthAndOffsetType >::NeighborKey< ParameterPack::UIntPack< LeftRadii ... > , ParameterPack::UIntPack< RightRadii ... > >::setLeafNeighbors( RegularTreeNode *node , Window::StaticWindow< RegularTreeNode * , ( LeftRadii + RightRadii + 1 ) ... > &leaves )
797 {
798 // Suppose that we have a node at index I and we want the leaf nodes supported on the (possibly virtual) node K away
799 // Case 1: The K-th neighbor exists
800 // ---> Iterate over the leaf nodes of the sub-tree rooted at the K-th neighbor
801 // Case 2: The K-th neighbor does not exist
802 // ---> The index of the K-th neighbor is I+K
803 // ---> The index of the parent is floor( I/2 )
804 // ---> The index of the K-th neighbors parent is floor( (I+K)/2 )
805 // ---> The parent of the K-th neighbor is the [ floor( (I+k)/2 ) - floor( I/2 ) ]-th neighbor of the parent
806
807 static const unsigned int _LeftRadii[] = { LeftRadii ... };
808 auto GetNeighborLeaf = [&]( unsigned int depth , const int index[Dim] , const int offset[Dim] )
809 {
810 // unsigned int _index[Dim] , _offset[Dim] , __offset[Dim];
811 int _index[Dim] , _offset[Dim] , __offset[Dim];
812 for( int dim=0 ; dim<Dim ; dim++ ) _index[dim] = index[dim] , _offset[dim] = offset[dim];
813 for( int d=depth ; d>=0 ; d-- )
814 {
815 for( int dim=0 ; dim<Dim ; dim++ ) __offset[dim] = _offset[dim] + _LeftRadii[dim];
816 if( neighbors[d].neighbors( __offset ) ) return neighbors[d].neighbors( __offset );
817 else
818 {
819 for( int dim=0 ; dim<Dim ; dim++ )
820 {
821 _offset[dim] = ( ( _index[dim] + _offset[dim] ) >> 1 ) - ( _index[dim] >> 1 );
822 _index[dim] >>= 1;
823 }
824 }
825 }
826 return ( RegularTreeNode * )NULL;
827 };
828 getNeighbors( node );
829 int depth , index[Dim] , offset[Dim] , _offset[Dim];
830 node->depthAndOffset( depth , index );
831
832 Window::Loop< Dim >::Run
833 (
834 ParameterPack::IsotropicUIntPack< Dim , 0 >() , ParameterPack::UIntPack< ( LeftRadii + RightRadii + 1 ) ... >() ,
835 [&]( int d , int i ){ offset[d] = i , _offset[d] = i-_LeftRadii[d]; } ,
836 [&]( void ){ leaves( offset ) = GetNeighborLeaf( depth , index , _offset ); }
837 );
838 }
839
840 ///////////////////////////////////////
841 // RegularTreeNode::ConstNeighborKey //
842 ///////////////////////////////////////
843 template< unsigned int Dim , class NodeData , class DepthAndOffsetType >
844 template< unsigned int ... LeftRadii , unsigned int ... RightRadii >
845 RegularTreeNode< Dim , NodeData , DepthAndOffsetType >::ConstNeighborKey< ParameterPack::UIntPack< LeftRadii ... > , ParameterPack::UIntPack< RightRadii ... > >::ConstNeighborKey( void ){ _depth=-1 , neighbors=NULL; }
846 template< unsigned int Dim , class NodeData , class DepthAndOffsetType >
847 template< unsigned int ... LeftRadii , unsigned int ... RightRadii >
848 RegularTreeNode< Dim , NodeData , DepthAndOffsetType >::ConstNeighborKey< ParameterPack::UIntPack< LeftRadii ... > , ParameterPack::UIntPack< RightRadii ... > >::ConstNeighborKey( const ConstNeighborKey& key )
849 {
850 _depth = 0 , neighbors = NULL;
851 set( key._depth );
852 for( int d=0 ; d<=_depth ; d++ ) memcpy( &neighbors[d] , &key.neighbors[d] , sizeof( ConstNeighbors< ParameterPack::UIntPack< ( LeftRadii + RightRadii + 1 ) ... > > ) );
853 }
854 template< unsigned int Dim , class NodeData , class DepthAndOffsetType >
855 template< unsigned int ... LeftRadii , unsigned int ... RightRadii >
856 RegularTreeNode< Dim , NodeData , DepthAndOffsetType >::ConstNeighborKey< ParameterPack::UIntPack< LeftRadii ... > , ParameterPack::UIntPack< RightRadii ... > >::~ConstNeighborKey( void )
857 {
858 if( neighbors ) delete[] neighbors;
859 neighbors=NULL;
860 }
861 template< unsigned int Dim , class NodeData , class DepthAndOffsetType >
862 template< unsigned int ... LeftRadii , unsigned int ... RightRadii >
863 typename RegularTreeNode< Dim , NodeData , DepthAndOffsetType >::template ConstNeighborKey< ParameterPack::UIntPack< LeftRadii ... > , ParameterPack::UIntPack< RightRadii ... > >& RegularTreeNode< Dim , NodeData , DepthAndOffsetType >::ConstNeighborKey< ParameterPack::UIntPack< LeftRadii ... > , ParameterPack::UIntPack< RightRadii ... > >::operator = ( const ConstNeighborKey& key )
864 {
865 set( key._depth );
866 for( int d=0 ; d<=_depth ; d++ ) memcpy( &neighbors[d] , &key.neighbors[d] , sizeof( ConstNeighbors< ParameterPack::UIntPack< ( LeftRadii + RightRadii + 1 ) ... > > ) );
867 }
868 template< unsigned int Dim , class NodeData , class DepthAndOffsetType >
869 template< unsigned int ... LeftRadii , unsigned int ... RightRadii >
870 void RegularTreeNode< Dim , NodeData , DepthAndOffsetType >::ConstNeighborKey< ParameterPack::UIntPack< LeftRadii ... > , ParameterPack::UIntPack< RightRadii ... > >::set( int d )
871 {
872 if( neighbors ) delete[] neighbors;
873 neighbors = NULL;
874 _depth = d;
875 if( d<0 ) return;
876 neighbors = new NeighborType[d+1];
877 }
878
879 template< unsigned int Dim , class NodeData , class DepthAndOffsetType >
880 template< unsigned int ... LeftRadii , unsigned int ... RightRadii >
881 template< unsigned int ... _PLeftRadii , unsigned int ... _PRightRadii , unsigned int ... _CLeftRadii , unsigned int ... _CRightRadii >
882 unsigned int RegularTreeNode< Dim , NodeData , DepthAndOffsetType >::ConstNeighborKey< ParameterPack::UIntPack< LeftRadii ... > , ParameterPack::UIntPack< RightRadii ... > >::_NeighborsLoop( ParameterPack::UIntPack< _PLeftRadii ... > pLeftRadii , ParameterPack::UIntPack< _PRightRadii ... > pRightRadii , ParameterPack::UIntPack< _CLeftRadii ... > cLeftRadii , ParameterPack::UIntPack< _CRightRadii ... > cRightRadii , Window::ConstSlice< const RegularTreeNode * , ( _PLeftRadii + _PRightRadii + 1 ) ... > pNeighbors , Window::Slice< const RegularTreeNode * , ( _CLeftRadii + _CRightRadii + 1 ) ... > cNeighbors , int cIdx )
883 {
884 static_assert( Dim==sizeof ... ( _PLeftRadii ) && Dim==sizeof ... ( _PRightRadii ) && Dim==sizeof ... ( _CLeftRadii ) && Dim==sizeof ... ( _CRightRadii ) , "[ERROR] Dimensions don't match" );
885 int c[Dim];
886 for( int d=0 ; d<Dim ; d++ ) c[d] = ( cIdx>>d ) & 1;
887 return _Run< ParameterPack::UIntPack< _PLeftRadii ... > , ParameterPack::UIntPack< _PRightRadii ... > , ParameterPack::UIntPack< _CLeftRadii ... > , ParameterPack::UIntPack< _CRightRadii ... > >::Run( pNeighbors , cNeighbors , c , 0 );
888 }
889 template< unsigned int Dim , class NodeData , class DepthAndOffsetType >
890
891 template< unsigned int ... LeftRadii , unsigned int ... RightRadii >
892 template< unsigned int ... _PLeftRadii , unsigned int ... _PRightRadii , unsigned int ... _CLeftRadii , unsigned int ... _CRightRadii >
893 unsigned int RegularTreeNode< Dim , NodeData , DepthAndOffsetType >::ConstNeighborKey< ParameterPack::UIntPack< LeftRadii ... > , ParameterPack::UIntPack< RightRadii ... > >::_NeighborsLoop( ParameterPack::UIntPack< _PLeftRadii ... > pLeftRadii , ParameterPack::UIntPack< _PRightRadii ... > pRightRadii , ParameterPack::UIntPack< _CLeftRadii ... > cLeftRadii , ParameterPack::UIntPack< _CRightRadii ... > cRightRadii , Window::Slice< const RegularTreeNode* , ( _PLeftRadii + _PRightRadii + 1 ) ... > pNeighbors , Window::Slice< const RegularTreeNode* , ( _CLeftRadii + _CRightRadii + 1 ) ... > cNeighbors , int cIdx )
894 {
895 return _NeighborsLoop( ParameterPack::UIntPack< _PLeftRadii ... >() , ParameterPack::UIntPack< _PRightRadii ... >() , ParameterPack::UIntPack< _CLeftRadii ... >() , ParameterPack::UIntPack< _CRightRadii ... >() , ( Window::ConstSlice< const RegularTreeNode* , ( _PLeftRadii + _PRightRadii + 1 ) ... > )pNeighbors , cNeighbors , cIdx );
896 }
897
898 template< unsigned int Dim , class NodeData , class DepthAndOffsetType >
899 template< unsigned int ... LeftRadii , unsigned int ... RightRadii >
900 template< unsigned int _PLeftRadius , unsigned int ... _PLeftRadii , unsigned int _PRightRadius , unsigned int ... _PRightRadii , unsigned int _CLeftRadius , unsigned int ... _CLeftRadii , unsigned int _CRightRadius , unsigned int ... _CRightRadii >
901 unsigned int RegularTreeNode< Dim , NodeData , DepthAndOffsetType >::ConstNeighborKey< ParameterPack::UIntPack< LeftRadii ... > , ParameterPack::UIntPack< RightRadii ... > >::_Run< ParameterPack::UIntPack< _PLeftRadius , _PLeftRadii ... > , ParameterPack::UIntPack< _PRightRadius , _PRightRadii ... > , ParameterPack::UIntPack< _CLeftRadius , _CLeftRadii ... > , ParameterPack::UIntPack< _CRightRadius , _CRightRadii ... > >::Run( Window::ConstSlice< const RegularTreeNode* , _PLeftRadius + _PRightRadius + 1 , ( _PLeftRadii + _PRightRadii + 1 ) ... > pNeighbors , Window::Slice< const RegularTreeNode* , _CLeftRadius + _CRightRadius + 1 , ( _CLeftRadii + _CRightRadii + 1 ) ... > cNeighbors , int* c , int cornerIndex )
902 {
903 static const int D = sizeof ... ( _PLeftRadii ) + 1;
904 unsigned int count=0;
905 for( int i=-(int)_CLeftRadius ; i<=(int)_CRightRadius ; i++ )
906 {
907 int _i = (i+c[Dim-D]) + ( _CLeftRadius<<1 ) , pi = ( _i>>1 ) - _CLeftRadius + _PLeftRadius , ci = i + _CLeftRadius;
908 count += _Run< ParameterPack::UIntPack< _PLeftRadii ... > , ParameterPack::UIntPack< _PRightRadii ... > , ParameterPack::UIntPack< _CLeftRadii ... > , ParameterPack::UIntPack< _CRightRadii ... > >::Run( pNeighbors[pi] , cNeighbors[ci] , c , cornerIndex | ( ( _i&1)<<(Dim-D) ) );
909 }
910 return count;
911 }
912
913 template< unsigned int Dim , class NodeData , class DepthAndOffsetType >
914 template< unsigned int ... LeftRadii , unsigned int ... RightRadii >
915 template< unsigned int _PLeftRadius , unsigned int _PRightRadius , unsigned int _CLeftRadius , unsigned int _CRightRadius >
916 unsigned int RegularTreeNode< Dim , NodeData , DepthAndOffsetType >::ConstNeighborKey< ParameterPack::UIntPack< LeftRadii ... > , ParameterPack::UIntPack< RightRadii ... > >::_Run< ParameterPack::UIntPack< _PLeftRadius > , ParameterPack::UIntPack< _PRightRadius > , ParameterPack::UIntPack< _CLeftRadius > , ParameterPack::UIntPack< _CRightRadius > >::Run( Window::ConstSlice< const RegularTreeNode* , _PLeftRadius+_PRightRadius+1 > pNeighbors , Window::Slice< const RegularTreeNode* , _CLeftRadius+_CRightRadius+1 > cNeighbors , int* c , int cornerIndex )
917 {
918 static const int D = 1;
919 unsigned int count=0;
920 for( int i=-(int)_CLeftRadius ; i<=(int)_CRightRadius ; i++ )
921 {
922 int _i = (i+c[Dim-D]) + ( _CLeftRadius<<1 ) , pi = ( _i>>1 ) - _CLeftRadius + _PLeftRadius , ci = i + _CLeftRadius;
923 if( pNeighbors[pi] && pNeighbors[pi]->children ) cNeighbors[ci] = pNeighbors[pi]->children + ( cornerIndex | ( ( _i&1)<<(Dim-1) ) ) , count++;
924 else cNeighbors[ci] = NULL;
925 }
926 return count;
927 }
928
929 template< unsigned int Dim , class NodeData , class DepthAndOffsetType >
930 template< unsigned int ... LeftRadii , unsigned int ... RightRadii >
931 unsigned int RegularTreeNode< Dim , NodeData , DepthAndOffsetType >::ConstNeighborKey< ParameterPack::UIntPack< LeftRadii ... > , ParameterPack::UIntPack< RightRadii ... > >::getChildNeighbors( int cIdx , int d , ConstNeighbors< ParameterPack::UIntPack< ( LeftRadii + RightRadii + 1 ) ... > >& cNeighbors ) const
932 {
933 const ConstNeighbors< ParameterPack::UIntPack< ( LeftRadii + RightRadii + 1 ) ... > >& pNeighbors = neighbors[d];
934 // Check that we actually have a center node
935 if( !pNeighbors.neighbors.data[ CenterIndex ] ) return 0;
936
937 return _NeighborsLoop( ParameterPack::UIntPack< LeftRadii ... >() , ParameterPack::UIntPack< RightRadii ... >() , ParameterPack::UIntPack< LeftRadii ... >() , ParameterPack::UIntPack< RightRadii ... >() , pNeighbors.neighbors() , cNeighbors.neighbors() , cIdx );
938 }
939
940 template< unsigned int Dim , class NodeData , class DepthAndOffsetType >
941 template< unsigned int ... LeftRadii , unsigned int ... RightRadii >
942 typename RegularTreeNode< Dim , NodeData , DepthAndOffsetType >::template ConstNeighbors< ParameterPack::UIntPack< ( LeftRadii + RightRadii + 1 ) ... > >& RegularTreeNode< Dim , NodeData , DepthAndOffsetType >::ConstNeighborKey< ParameterPack::UIntPack< LeftRadii ... > , ParameterPack::UIntPack< RightRadii ... > >::getNeighbors( const RegularTreeNode* node )
943 {
944 ConstNeighbors< ParameterPack::UIntPack< ( LeftRadii + RightRadii + 1 ) ... > >& neighbors = this->neighbors[ node->depth() ];
945 if( node!=neighbors.neighbors.data[ CenterIndex ] )
946 {
947 for( int d=node->depth()+1 ; d<=_depth && this->neighbors[d].neighbors.data[ CenterIndex ] ; d++ ) this->neighbors[d].neighbors.data[ CenterIndex ] = NULL;
948 neighbors.clear();
949 if( !node->parent ) neighbors.neighbors.data[ CenterIndex ] = node;
950 else _NeighborsLoop( ParameterPack::UIntPack< LeftRadii ... >() , ParameterPack::UIntPack< RightRadii ... >() , ParameterPack::UIntPack< LeftRadii ... >() , ParameterPack::UIntPack< RightRadii ... >() , getNeighbors( node->parent ).neighbors() , neighbors.neighbors() , (int)( node - node->parent->children ) );
951 }
952 return neighbors;
953 }
954
955 template< unsigned int Dim , class NodeData , class DepthAndOffsetType >
956 template< unsigned int ... LeftRadii , unsigned int ... RightRadii >
957 template< unsigned int ... _LeftRadii , unsigned int ... _RightRadii >
958 void RegularTreeNode< Dim , NodeData , DepthAndOffsetType >::ConstNeighborKey< ParameterPack::UIntPack< LeftRadii ... > , ParameterPack::UIntPack< RightRadii ... > >::getNeighbors( ParameterPack::UIntPack< _LeftRadii ... > , ParameterPack::UIntPack< _RightRadii ... > , const RegularTreeNode< Dim , NodeData , DepthAndOffsetType >* node , ConstNeighbors< ParameterPack::UIntPack< ( _LeftRadii + _RightRadii + 1 ) ... > >& neighbors )
959 {
960 static const unsigned int _CenterIndex = Window::Index< ( _LeftRadii + _RightRadii + 1 ) ... >::template I< _LeftRadii ... >();
961
962 neighbors.clear();
963 if( !node ) return;
964
965 ParameterPack::UIntPack< LeftRadii ... > leftRadii;
966 ParameterPack::UIntPack< RightRadii ... > rightRadii;
967 ParameterPack::UIntPack< ( _LeftRadii+1 )/2 ... > pLeftRadii;
968 ParameterPack::UIntPack< ( _RightRadii+1 )/2 ... > pRightRadii;
969 ParameterPack::UIntPack< _LeftRadii ... > cLeftRadii;
970 ParameterPack::UIntPack< _RightRadii ... > cRightRadii;
971 // If we are at the root of the tree, we are done
972 if( !node->parent ) neighbors.neighbors.data[ _CenterIndex ] = node;
973 // If we can get the data from the the key for the parent node, do that
974 else if( pLeftRadii<=leftRadii && pRightRadii<=rightRadii )
975 {
976 getNeighbors( node->parent );
977 const ConstNeighbors< ParameterPack::UIntPack< ( LeftRadii + RightRadii + 1 ) ... > >& pNeighbors = this->neighbors[ node->depth()-1 ];
978 _NeighborsLoop( leftRadii , rightRadii , cLeftRadii , cRightRadii , pNeighbors.neighbors() , neighbors.neighbors() , (int)( node - node->parent->children ) );
979 }
980 // Otherwise recurse
981 else
982 {
983 ConstNeighbors< ParameterPack::UIntPack< ( ( _LeftRadii+1 )/2 + ( _RightRadii+1 )/2 + 1 ) ... > > pNeighbors;
984 getNeighbors( pLeftRadii , pRightRadii , node->parent , pNeighbors );
985 _NeighborsLoop( pLeftRadii , pRightRadii , cLeftRadii , cRightRadii , pNeighbors.neighbors() , neighbors.neighbors() , (int)( node - node->parent->children ) );
986 }
987 return;
988 }
989
990 template< unsigned int Dim , class NodeData , class DepthAndOffsetType >
991 template< unsigned int ... LeftRadii , unsigned int ... RightRadii >
992 template< unsigned int ... _LeftRadii , unsigned int ... _RightRadii >
993 void RegularTreeNode< Dim , NodeData , DepthAndOffsetType >::ConstNeighborKey< ParameterPack::UIntPack< LeftRadii ... > , ParameterPack::UIntPack< RightRadii ... > >::getNeighbors( ParameterPack::UIntPack< _LeftRadii ... > , ParameterPack::UIntPack< _RightRadii ... > , const RegularTreeNode< Dim , NodeData , DepthAndOffsetType >* node , ConstNeighbors< ParameterPack::UIntPack< ( _LeftRadii + _RightRadii + 1 ) ... > >& pNeighbors , ConstNeighbors< ParameterPack::UIntPack< ( _LeftRadii + _RightRadii + 1 ) ... > >& neighbors )
994 {
995 ParameterPack::UIntPack< _LeftRadii ... > leftRadii;
996 ParameterPack::UIntPack< _RightRadii ... > rightRadii;
997 if( !node->parent ) return getNeighbors( leftRadii , rightRadii , node , neighbors );
998 else
999 {
1000 getNeighbors( leftRadii , rightRadii , node->parent , pNeighbors );
1001 _NeighborsLoop( leftRadii , rightRadii , leftRadii , rightRadii , pNeighbors.neighbors() , neighbors.neighbors() , (int)( node - node->parent->children ) );
1002 }
1003 }
1004
1005 template< unsigned int Dim , class NodeData , class DepthAndOffsetType >
1006 template< unsigned int ... LeftRadii , unsigned int ... RightRadii >
1007 template< class Real >
1008 unsigned int RegularTreeNode< Dim , NodeData , DepthAndOffsetType >::ConstNeighborKey< ParameterPack::UIntPack< LeftRadii ... > , ParameterPack::UIntPack< RightRadii ... > >::getChildNeighbors( Point< Real , Dim > p , int d , ConstNeighbors< ParameterPack::UIntPack< ( LeftRadii + RightRadii + 1 ) ... > >& cNeighbors ) const
1009 {
1010 ConstNeighbors< ParameterPack::UIntPack< ( LeftRadii + RightRadii + 1 ) ... > >& pNeighbors = neighbors[d];
1011 // Check that we actually have a center node
1012 if( !pNeighbors.neighbors.data[ CenterIndex ] ) return 0;
1013 Point< Real , Dim > c;
1014 Real w;
1015 pNeighbors.neighbors.data[ CenterIndex ]->centerAndWidth( c , w );
1016 int cIdx = 0;
1017 for( int d=0 ; d<Dim ; d++ ) if( p[d]>c[d] ) cIdx |= (1<<d);
1018 return getChildNeighbors( cIdx , d , cNeighbors );
1019 }
1020
1021 template< unsigned int Dim , class NodeData , class DepthAndOffsetType >
1022 template< unsigned int ... LeftRadii , unsigned int ... RightRadii >
1023 void RegularTreeNode< Dim , NodeData , DepthAndOffsetType >::ConstNeighborKey< ParameterPack::UIntPack< LeftRadii ... > , ParameterPack::UIntPack< RightRadii ... > >::setLeafNeighbors( const RegularTreeNode *node , Window::StaticWindow< RegularTreeNode * , ( LeftRadii + RightRadii + 1 ) ... > &leaves )
1024 {
1025 // Suppose that we have a node at index I and we want the leaf nodes supported on the (possibly virtual) node K away
1026 // Case 1: The K-th neighbor exists
1027 // ---> Iterate over the leaf nodes of the sub-tree rooted at the K-th neighbor
1028 // Case 2: The K-th neighbor does not exist
1029 // ---> The index of the K-th neighbor is I+K
1030 // ---> The index of the parent is floor( I/2 )
1031 // ---> The index of the K-th neighbors parent is floor( (I+K)/2 )
1032 // ---> The parent of the K-th neighbor is the [ floor( (I+k)/2 ) - floor( I/2 ) ]-th neighbor of the parent
1033
1034 static const unsigned int _LeftRadii[] = { LeftRadii ... };
1035 auto GetNeighborLeaf = [&]( unsigned int depth , const int index[Dim] , const int offset[Dim] )
1036 {
1037 // unsigned int _index[Dim] , _offset[Dim] , __offset[Dim];
1038 int _index[Dim] , _offset[Dim] , __offset[Dim];
1039 for( int dim=0 ; dim<Dim ; dim++ ) _index[dim] = index[dim] , _offset[dim] = offset[dim];
1040 for( int d=depth ; d>=0 ; d-- )
1041 {
1042 for( int dim=0 ; dim<Dim ; dim++ ) __offset[dim] = _offset[dim] + _LeftRadii[dim];
1043 if( neighbors[d].neighbors( __offset ) ) return neighbors[d].neighbors( __offset );
1044 else
1045 {
1046 for( int dim=0 ; dim<Dim ; dim++ )
1047 {
1048 _offset[dim] = ( ( _index[dim] + _offset[dim] ) >> 1 ) - ( _index[dim] >> 1 );
1049 _index[dim] >>= 1;
1050 }
1051 }
1052 }
1053 return ( RegularTreeNode * )NULL;
1054 };
1055 getNeighbors( node );
1056 int depth , index[Dim] , offset[Dim] , _offset[Dim];
1057 node->depthAndOffset( depth , index );
1058
1059 Window::Loop< Dim >::Run
1060 (
1061 ParameterPack::IsotropicUIntPack< Dim , 0 >() , ParameterPack::UIntPack< ( LeftRadii + RightRadii + 1 ) ... >() ,
1062 [&]( int d , int i ){ offset[d] = i , _offset[d] = i-_LeftRadii[d]; } ,
1063 [&]( void ){ leaves( offset ) = GetNeighborLeaf( depth , index , _offset ); }
1064 );
1065 }