Debian Codegraph @

src/thirdparty/PoissonRecon/FEMTree.LevelSet.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 // Level-set extraction data
30 namespace LevelSetExtraction
31 {
32 /////////
33 // Key //
34 /////////
35 template< unsigned int Dim >
36 struct Key
37 {
38 protected:
39 template< unsigned int _Dim > friend struct KeyGenerator;
40 public:
41 unsigned int idx[Dim];
42
43 Key( void ){ for( unsigned int d=0 ; d<Dim ; d++ ) idx[d] = -1; }
44
45 unsigned int &operator[]( int i ){ return idx[i]; }
46 const unsigned int &operator[]( int i ) const { return idx[i]; }
47
48 bool operator == ( const Key &key ) const
49 {
50 for( unsigned int d=0 ; d<Dim ; d++ ) if( idx[d]!=key[d] ) return false;
51 return true;
52 }
53 bool operator != ( const Key &key ) const { return !operator==( key ); }
54
55 std::string to_string( void ) const
56 {
57 std::stringstream stream;
58 stream << "(";
59 for( unsigned int d=0 ; d<Dim ; d++ )
60 {
61 if( d ) stream << ",";
62 stream << idx[d];
63 }
64 stream << ")";
65 return stream.str();
66 }
67
68 friend std::ostream &operator << ( std::ostream &os , const Key &key ){ return os << key.to_string(); }
69
70 friend Key SetAtomic( volatile Key & value , Key newValue )
71 {
72 Key oldValue;
73 for( unsigned int d=0 ; d<Dim ; d++ ) oldValue.idx[d] = PoissonRecon::SetAtomic( value.idx[d] , newValue.idx[d] );
74 return oldValue;
75 }
76
77 #ifdef SHOW_WARNINGS
78 #pragma message( "[WARNING] Could we hash better?" )
79 #endif // SHOW_WARNINGS
80 struct Hasher
81 {
82 size_t operator()( const Key &i ) const
83 {
84 size_t hash = 0;
85 for( unsigned int d=0 ; d<Dim ; d++ ) hash ^= i.idx[d];
86 return hash;
87 }
88 };
89 };
90
91 template< unsigned int Dim , typename Data > using KeyMap = std::unordered_map< Key< Dim > , Data , typename Key< Dim >::Hasher >;
92
93 /////////////
94 // IsoEdge //
95 /////////////
96 template< unsigned int Dim >
97 struct IsoEdge
98 {
99 Key< Dim > vertices[2];
100 IsoEdge( void ) {}
101 IsoEdge( Key< Dim > v1 , Key< Dim > v2 ){ vertices[0] = v1 , vertices[1] = v2; }
102 Key< Dim > &operator[]( int idx ){ return vertices[idx]; }
103 const Key< Dim > &operator[]( int idx ) const { return vertices[idx]; }
104
105 friend IsoEdge SetAtomic( volatile IsoEdge & value , IsoEdge newValue )
106 {
107 IsoEdge oldValue;
108 oldValue.vertices[0] = SetAtomic( value.vertices[0] , newValue.vertices[0] );
109 oldValue.vertices[1] = SetAtomic( value.vertices[1] , newValue.vertices[1] );
110 return oldValue;
111 }
112 };
113
114 /////////////////////
115 // HyperCubeTables //
116 /////////////////////
117 template< unsigned int D , unsigned int ... Ks > struct HyperCubeTables{};
118
119 template< unsigned int D , unsigned int K1 , unsigned int K2 >
120 struct HyperCubeTables< D , K1 , K2 >
121 {
122 // The number of K1-dimensional elements in a D-dimensional cube
123 static constexpr unsigned int ElementNum1 = HyperCube::Cube< D >::template ElementNum< K1 >();
124 // The number of K2-dimensional elements in a D-dimensional cube
125 static constexpr unsigned int ElementNum2 = HyperCube::Cube< D >::template ElementNum< K2 >();
126 // The number of K2-dimensional elements overlapping the K1-dimensional element
127 static constexpr unsigned int OverlapElementNum = HyperCube::Cube< D >::template OverlapElementNum< K1 , K2 >();
128 // The list of K2-dimensional elements overlapping the K1-dimensional element
129 static typename HyperCube::Cube< D >::template Element< K2 > OverlapElements[ ElementNum1 ][ OverlapElementNum ];
130 // Indicates if the K2-dimensional element overlaps the K1-dimensional element
131 static bool Overlap[ ElementNum1 ][ ElementNum2 ];
132
133 static void SetTables( void )
134 {
135 for( typename HyperCube::Cube< D >::template Element< K1 > e ; e<HyperCube::Cube< D >::template ElementNum< K1 >() ; e++ )
136 {
137 for( typename HyperCube::Cube< D >::template Element< K2 > _e ; _e<HyperCube::Cube< D >::template ElementNum< K2 >() ; _e++ )
138 Overlap[e.index][_e.index] = HyperCube::Cube< D >::Overlap( e , _e );
139 HyperCube::Cube< D >::OverlapElements( e , OverlapElements[e.index] );
140 }
141 if( !K2 ) HyperCubeTables< D , K1 >::SetTables();
142 }
143 };
144
145 template< unsigned int D , unsigned int K >
146 struct HyperCubeTables< D , K >
147 {
148 static constexpr unsigned int IncidentCubeNum = HyperCube::Cube< D >::template IncidentCubeNum< K >();
149 static constexpr unsigned int ElementNum = HyperCube::Cube< D >::template ElementNum< K >();
150 static unsigned int CellOffset[ ElementNum ][ IncidentCubeNum ];
151 static unsigned int IncidentElementCoIndex[ ElementNum ][ IncidentCubeNum ];
152 static unsigned int IncidentElementIndex[ ElementNum ][ IncidentCubeNum ];
153 static unsigned int CellOffsetAntipodal[ ElementNum ];
154 static typename HyperCube::Cube< D >::template IncidentCubeIndex< K > IncidentCube[ ElementNum ];
155 static typename HyperCube::Direction Directions[ ElementNum ][ D ];
156
157 static void SetTables( void )
158 {
159 for( typename HyperCube::Cube< D >::template Element< K > e ; e<HyperCube::Cube< D >::template ElementNum< K >() ; e++ )
160 {
161 for( typename HyperCube::Cube< D >::template IncidentCubeIndex< K > i ; i<HyperCube::Cube< D >::template IncidentCubeNum< K >() ; i++ )
162 {
163 CellOffset[e.index][i.index] = HyperCube::Cube< D >::CellOffset( e , i );
164 IncidentElementCoIndex[e.index][i.index] = HyperCube::Cube< D >::IncidentElement( e , i ).coIndex();
165 IncidentElementIndex[e.index][i.index] = HyperCube::Cube< D >::IncidentElement( e , i ).index;
166 }
167 CellOffsetAntipodal[e.index] = HyperCube::Cube< D >::CellOffset( e , HyperCube::Cube< D >::IncidentCube( e ).antipodal() );
168 IncidentCube[ e.index ] = HyperCube::Cube< D >::IncidentCube( e );
169 e.directions( Directions[e.index] );
170 }
171 }
172 };
173
174 template< unsigned int D , unsigned int K1=D , unsigned int K2=D >
175 static void SetHyperCubeTables( void )
176 {
177 if constexpr( K2!=0 )
178 {
179 HyperCubeTables< D , K1 , K2 >::SetTables();
180 SetHyperCubeTables< D , K1 , K2-1 >();
181 }
182 else if constexpr( K1!=0 && K2==0 )
183 {
184 HyperCubeTables< D , K1 , K2 >::SetTables();
185 SetHyperCubeTables< D , K1-1 , D >();
186 }
187 else if constexpr( D!=1 && K1==0 && K2==0 )
188 {
189 HyperCubeTables< D , K1 , K2 >::SetTables();
190 SetHyperCubeTables< D-1 , D-1 , D-1 >();
191 }
192 else HyperCubeTables< D , K1 , K2 >::SetTables();
193 }
194
195 // A helper class for storing a static array
196 template< unsigned int Indices >
197 struct _Indices
198 {
199 node_index_type idx[Indices];
200 _Indices( void ){ for( unsigned int i=0 ; i<Indices ; i++ ) idx[i] = -1; }
201 node_index_type& operator[] ( int i ) { return idx[i]; }
202 const node_index_type& operator[] ( int i ) const { return idx[i]; }
203 };
204
205 template< unsigned int Dim , unsigned int CellDim >
206 struct _CellIndices
207 {
208 static const unsigned int ElementNum = HyperCube::Cube< Dim >::template ElementNum< CellDim >();
209 using Type = _Indices< ElementNum >;
210 };
211
212 // A helper struct for storing arrays of different types of indices
213 template< unsigned int Dim , typename T > struct _Tables{};
214 template< unsigned int Dim , unsigned int ... CellDimensions > struct _Tables< Dim , std::integer_sequence< unsigned int , CellDimensions ... > >
215 {
216 typedef std::tuple< Pointer( typename _CellIndices< Dim , CellDimensions >::Type ) ... > Type;
217 };
218
219 // Temporary data used for tracking ownership of cells shared by different nodes
220 template< unsigned int Dim , unsigned int MaxCellDim >
221 struct _Scratch
222 {
223 Pointer( node_index_type ) maps[MaxCellDim+1];
224 _Scratch( void ) : _reserved(0) { for( unsigned int i=0 ; i<=MaxCellDim ; i++ ) maps[i] = NullPointer( node_index_type ); }
225 ~_Scratch( void ){ clear(); }
226 void clear( void ){ for( unsigned int i=0 ; i<=MaxCellDim ; i++ ) DeletePointer( maps[i] ); }
227 void resize( size_t sz )
228 {
229 if( sz>_reserved )
230 {
231 clear();
232 _allocate<0>( sz );
233 _reserved = sz;
234 }
235 _zeroOut<0>( sz );
236 }
237 protected:
238
239 template< unsigned int CellDim >
240 void _allocate( size_t sz )
241 {
242 maps[ CellDim ] = NewPointer< node_index_type >( sz * HyperCube::Cube< Dim >::template ElementNum< CellDim >() );
243
244 if constexpr( CellDim==MaxCellDim ) return;
245 else _allocate< CellDim+1 >( sz );
246 }
247
248 template< unsigned int CellDim >
249 void _zeroOut( size_t sz )
250 {
251 if( sz && maps[CellDim] ) memset( maps[CellDim] , 0 , sizeof(node_index_type) * sz * HyperCube::Cube< Dim >::template ElementNum< CellDim >() );
252 else if( sz ) MK_THROW( "Traying to zero out null pointer" );
253
254 if constexpr( CellDim==MaxCellDim ) return;
255 else _zeroOut< CellDim+1 >( sz );
256 }
257 size_t _reserved;
258 };
259
260 ///////////////////
261 // CellIndexData //
262 ///////////////////
263 // A data-struture that allows us to take a node together with a local cell index and returns the global cell's index
264 template< unsigned int Dim , unsigned int _MaxCellDim >
265 struct CellIndexData
266 {
267 static const unsigned int MaxCellDim = _MaxCellDim;
268 using TreeNode = RegularTreeNode< Dim , FEMTreeNodeData , depth_and_offset_type >;
269
270 // Static arrays for holding specific dimension cell indices
271 template< unsigned int CellDim > using CellIndices = typename _CellIndices< Dim , CellDim >::Type;
272
273 // A tuple of pointers to cell indices of dimensions {0,...,MaxCellDim}
274 using TablesType = typename _Tables< Dim , std::make_integer_sequence< unsigned int , MaxCellDim+1 > >::Type;
275
276 // The tables holding the cell indices of different dimensions
277 TablesType tables;
278
279 // The count of the different number of cells
280 size_t counts[MaxCellDim+1];
281
282 CellIndexData( void ) : _size(0) , _capacity(0) { _init<0>(); }
283 ~CellIndexData( void ){ clear(); }
284
285 void clear( void ){ _clear<0>(); }
286 void resize( size_t sz )
287 {
288 if( sz>_capacity )
289 {
290 _clear<0>();
291 _allocate<0>( sz );
292 _capacity = sz;
293 }
294 else for( unsigned int d=0 ; d<=MaxCellDim ; d++ ) counts[d] = 0;
295 _size = sz;
296 }
297 size_t size( void ) const { return _size; }
298
299 void read( BinaryStream &stream )
300 {
301 if( !stream.read( _size ) ) MK_THROW( "Failed to read node count" );
302 resize( _size );
303 if( _size ) _read<0>( stream );
304 }
305
306 void write( BinaryStream &stream ) const
307 {
308 stream.write( _size );
309 if( _size ) _write<0>( stream );
310 }
311
312 protected:
313 size_t _size , _capacity;
314
315 template< unsigned int CellDim >
316 void _init( void )
317 {
318 counts[CellDim] = 0;
319 std::get< CellDim >( tables ) = NullPointer( CellIndices< CellDim > );
320
321 if constexpr( CellDim==MaxCellDim ) return;
322 else _init< CellDim+1 >();
323 }
324
325 template< unsigned int CellDim >
326 void _clear( void )
327 {
328 counts[CellDim] = 0;
329 DeletePointer( std::get< CellDim >( tables ) );
330
331 if constexpr( CellDim==MaxCellDim ) return;
332 else _clear< CellDim+1 >();
333 }
334
335 template< unsigned int CellDim >
336 void _allocate( size_t sz )
337 {
338 std::get< CellDim >( tables ) = NewPointer< CellIndices< CellDim > >( sz );
339
340 if constexpr( CellDim==MaxCellDim ) return;
341 else _allocate< CellDim+1 >( sz );
342 }
343
344 template< unsigned int CellDim >
345 void _read( BinaryStream &stream )
346 {
347 if( !stream.read( counts[CellDim] ) ) MK_THROW( "Failed to read count at dimension: " , CellDim );
348 if( !stream.read( std::get< CellDim >( tables ) , _size ) ) MK_THROW( "Failed to read table at dimension: " , CellDim );
349
350 if constexpr( CellDim==MaxCellDim ) return;
351 else _read< CellDim+1 >( stream );
352 }
353
354 template< unsigned int CellDim >
355 void _write( BinaryStream &stream ) const
356 {
357 stream.write( counts[CellDim] );
358 stream.write( std::get< CellDim >( tables ) , _size );
359
360 if constexpr( CellDim==MaxCellDim ) return;
361 else _write< CellDim+1 >( stream );
362 }
363 };
364
365 ///////////////////////
366 // FullCellIndexData //
367 ///////////////////////
368 // A data-struture that allows us to take a node together with a local cell index and returns the global cell's index
369 // [WARNING] Do we want to go up to Dim or up to Dim-1?
370 template< unsigned int Dim >
371 struct FullCellIndexData : public CellIndexData< Dim , Dim >
372 {
373 static const unsigned int MaxCellDim = CellIndexData< Dim , Dim >::MaxCellDim;
374 template< unsigned int CellDim > using CellIndices = typename CellIndexData< Dim , Dim >::template CellIndices< CellDim >;
375 using TreeNode = RegularTreeNode< Dim , FEMTreeNodeData , depth_and_offset_type >;
376 using ConstOneRingNeighborKey = typename TreeNode::template ConstNeighborKey< IsotropicUIntPack< Dim , 1 > , IsotropicUIntPack< Dim , 1 > >;
377 using ConstNeighbors = typename TreeNode::template ConstNeighbors< IsotropicUIntPack< Dim , 3 > >;
378 using CellIndexData< Dim , Dim >::tables;
379 using CellIndexData< Dim , Dim >::size;
380 using CellIndexData< Dim , Dim >::resize;
381 using CellIndexData< Dim , Dim >::counts;
382
383 node_index_type nodeOffset;
384
385 FullCellIndexData( void ) : nodeOffset(0) {}
386
387 void read( BinaryStream &stream )
388 {
389 if( !stream.read( nodeOffset ) ) MK_THROW( "Failed to read node ofset" );
390 CellIndexData< Dim , MaxCellDim >::read( stream );
391 }
392
393 void write( BinaryStream &stream ) const
394 {
395 stream.write( nodeOffset );
396 CellIndexData< Dim , MaxCellDim >::write( stream );
397 }
398
399 void set( const SortedTreeNodes< Dim > &sNodes , int depth )
400 {
401 std::pair< node_index_type , node_index_type > span( sNodes.begin( depth ) , sNodes.end( depth ) );
402 nodeOffset = (size_t)span.first;
403 resize( (size_t)( span.second-span.first ) );
404 _scratch.resize( size() );
405
406 std::vector< ConstOneRingNeighborKey > neighborKeys( ThreadPool::NumThreads() );
407 for( size_t i=0 ; i<neighborKeys.size() ; i++ ) neighborKeys[i].set( depth );
408
409 // Try and get at the nodes outside of the slab through the neighbor key
410 ThreadPool::ParallelFor( sNodes.begin(depth) , sNodes.end(depth) , [&]( unsigned int thread , size_t i )
411 {
412 ConstOneRingNeighborKey& neighborKey = neighborKeys[ thread ];
413 const TreeNode *node = sNodes.treeNodes[i];
414 ConstNeighbors &neighbors = neighborKey.getNeighbors( node );
415 _setProcess<0>( neighbors , _scratch.maps );
416 }
417 );
418
419 _setCounts<0>( _scratch.maps );
420 ThreadPool::ParallelFor( 0 , size() , [&]( unsigned int , size_t i ){ _setTables<0>( (unsigned int)i , _scratch.maps ); } );
421 }
422
423 // Maps from tree nodes (and their associated indices) to the associated indices for the cell indices
424 template< unsigned int CellDim > CellIndices< CellDim > &indices( const TreeNode *node ) { return std::get< CellDim >( tables )[ node->nodeData.nodeIndex - nodeOffset ]; }
425 template< unsigned int CellDim > CellIndices< CellDim > &indices( node_index_type nodeIndex ) { return std::get< CellDim >( tables )[ nodeIndex - nodeOffset ]; }
426 template< unsigned int CellDim > const CellIndices< CellDim > &indices( const TreeNode *node ) const { return std::get< CellDim >( tables )[ node->nodeData.nodeIndex - nodeOffset ]; }
427 template< unsigned int CellDim > const CellIndices< CellDim > &indices( node_index_type nodeIndex ) const { return std::get< CellDim >( tables )[ nodeIndex - nodeOffset ]; }
428
429 protected:
430 _Scratch< Dim , MaxCellDim > _scratch;
431
432 template< unsigned int CellDim >
433 void _setProcess( const ConstNeighbors& neighbors , Pointer( node_index_type ) maps[MaxCellDim+1] )
434 {
435 const TreeNode *node = neighbors.neighbors.data[ WindowIndex< IsotropicUIntPack< Dim , 3 > , IsotropicUIntPack< Dim , 1 > >::Index ];
436 node_index_type i = node->nodeData.nodeIndex;
437
438 // Iterate over the cells in the node
439 for( typename HyperCube::Cube< Dim >::template Element< CellDim > c ; c<HyperCube::Cube< Dim >::template ElementNum< CellDim >() ; c++ )
440 {
441 bool owner = true;
442
443 // The index of the node relative to the cell
444 typename HyperCube::Cube< Dim >::template IncidentCubeIndex< CellDim > my_ic = HyperCubeTables< Dim , CellDim >::IncidentCube[c.index];
445
446 // Iterate over the nodes adjacent to the cell
447 for( typename HyperCube::Cube< Dim >::template IncidentCubeIndex< CellDim > ic ; ic<HyperCube::Cube< Dim >::template IncidentCubeNum< CellDim >() ; ic++ )
448 {
449 // Get the index of node relative to the cell neighbors
450 unsigned int xx = HyperCubeTables< Dim , CellDim >::CellOffset[c.index][ic.index];
451 // If the neighbor exists and comes before, they own the corner
452 if( IsActiveNode< Dim >( neighbors.neighbors.data[xx] ) && ic<my_ic ){ owner = false ; break; }
453 }
454 if( owner )
455 {
456 node_index_type myCount = ( i - nodeOffset ) * HyperCube::Cube< Dim >::template ElementNum< CellDim >() + c.index;
457 maps[CellDim][ myCount ] = 1;
458 // Set the cell index for all nodes incident on the cell
459 for( typename HyperCube::Cube< Dim >::template IncidentCubeIndex< CellDim > ic ; ic<HyperCube::Cube< Dim >::template IncidentCubeNum< CellDim >() ; ic++ ) // Iterate over the nodes adjacent to the cell
460 {
461 unsigned int xx = HyperCubeTables< Dim , CellDim >::CellOffset[c.index][ic.index];
462 // If the neighbor exits, sets its corner
463 if( IsActiveNode< Dim >( neighbors.neighbors.data[xx] ) ) this->template indices< CellDim >( neighbors.neighbors.data[xx] )[ HyperCubeTables< Dim , CellDim >::IncidentElementIndex[c.index][ic.index] ] = myCount;
464 }
465 }
466 }
467
468 if constexpr( CellDim==MaxCellDim ) return;
469 else _setProcess< CellDim+1 >( neighbors , maps );
470 }
471
472 template< unsigned int CellDim >
473 void _setCounts( Pointer( node_index_type ) maps[MaxCellDim+1] )
474 {
475 node_index_type count = 0;
476 for( node_index_type i=0 ; i<(node_index_type)size() * (node_index_type)HyperCube::Cube< Dim >::template ElementNum< CellDim >() ; i++ )
477 if( maps[CellDim][i] ) maps[CellDim][i] = count++;
478 counts[ CellDim ] = count;
479
480 if constexpr( CellDim==MaxCellDim ) return;
481 else _setCounts< CellDim+1 >( maps );
482 }
483
484 template< unsigned int CellDim >
485 void _setTables( unsigned int i , Pointer( node_index_type ) maps[MaxCellDim+1] )
486 {
487 for( unsigned int j=0 ; j<HyperCube::Cube< Dim >::template ElementNum< CellDim >() ; j++ )
488 std::get< CellDim >( tables )[i][j] = maps[CellDim][ std::get< CellDim >( tables )[i][j] ];
489
490 if constexpr( CellDim==MaxCellDim ) return;
491 else _setTables< CellDim+1 >( i , maps );
492 }
493 };
494
495 ////////////////////////
496 // SliceCellIndexData //
497 ////////////////////////
498 // A data-struture that allows us to take a node adjacent to a slice together with a local cell index and returns the global cell's index
499 template< unsigned int Dim >
500 struct SliceCellIndexData : public CellIndexData< Dim-1 , Dim-1 >
501 {
502 static const unsigned int _Dim = Dim-1;
503 static const unsigned int MaxCellDim = CellIndexData< _Dim , _Dim >::MaxCellDim;
504 template< unsigned int CellDim > using CellIndices = typename CellIndexData< _Dim , _Dim >::template CellIndices< CellDim >;
505 using TreeNode = RegularTreeNode< Dim , FEMTreeNodeData , depth_and_offset_type >;
506 using ConstOneRingNeighborKey = typename TreeNode::template ConstNeighborKey< IsotropicUIntPack< Dim , 1 > , IsotropicUIntPack< Dim , 1 > >;
507 using ConstNeighbors = typename TreeNode::template ConstNeighbors< IsotropicUIntPack< Dim , 3 > >;
508 using CellIndexData< _Dim , _Dim >::tables;
509 using CellIndexData< _Dim , _Dim >::size;
510 using CellIndexData< _Dim , _Dim >::resize;
511 using CellIndexData< _Dim , _Dim >::counts;
512
513 node_index_type nodeOffset;
514
515 SliceCellIndexData( void ) : nodeOffset(0) {}
516
517 void read( BinaryStream &stream )
518 {
519 if( !stream.read( nodeOffset ) ) MK_THROW( "Failed to read node ofset" );
520 CellIndexData< _Dim , _Dim >::read( stream );
521 }
522
523 void write( BinaryStream &stream ) const
524 {
525 stream.write( nodeOffset );
526 CellIndexData< _Dim , _Dim >::write( stream );
527 }
528
529 void set( const SortedTreeNodes< Dim > &sNodes , int depth , int slice )
530 {
531 std::pair< node_index_type , node_index_type > span( sNodes.begin( depth , slice-1 ) , sNodes.end( depth , slice ) );
532 nodeOffset = (size_t)span.first;
533 resize( (size_t)( span.second - span.first ) );
534 _scratch.resize( size() );
535
536 std::vector< ConstOneRingNeighborKey > neighborKeys( ThreadPool::NumThreads() );
537 for( size_t i=0 ; i<neighborKeys.size() ; i++ ) neighborKeys[i].set( depth );
538
539 // Try and get at the nodes outside of the slab through the neighbor key
540 ThreadPool::ParallelFor( sNodes.begin( depth , slice-1 ) , sNodes.end( depth , slice ) , [&]( unsigned int thread , size_t i )
541 {
542 ConstOneRingNeighborKey &neighborKey = neighborKeys[ thread ];
543 const TreeNode *node = sNodes.treeNodes[i];
544 ConstNeighbors &neighbors = neighborKey.getNeighbors( node );
545 _setProcess<0>( neighbors , i<(size_t)sNodes.end( depth , slice-1 ) , _scratch.maps );
546 }
547 );
548
549 _setCounts<0>( _scratch.maps );
550 ThreadPool::ParallelFor( 0 , size() , [&]( unsigned int , size_t i ){ _setTables<0>( (unsigned int)i , _scratch.maps ); } );
551 }
552
553 // Maps from tree nodes (and their associated indices) to the associated indices for the cell indices
554 template< unsigned int CellDim > CellIndices< CellDim > &indices( const TreeNode *node ) { return std::get< CellDim >( tables )[ node->nodeData.nodeIndex - nodeOffset ]; }
555 template< unsigned int CellDim > CellIndices< CellDim > &indices( node_index_type nodeIndex ) { return std::get< CellDim >( tables )[ nodeIndex - nodeOffset ]; }
556 template< unsigned int CellDim > const CellIndices< CellDim > &indices( const TreeNode *node ) const { return std::get< CellDim >( tables )[ node->nodeData.nodeIndex - nodeOffset ]; }
557 template< unsigned int CellDim > const CellIndices< CellDim > &indices( node_index_type nodeIndex ) const { return std::get< CellDim >( tables )[ nodeIndex - nodeOffset ]; }
558
559 protected:
560 _Scratch< _Dim , MaxCellDim > _scratch;
561
562 template< unsigned int CellDim >
563 void _setProcess( const ConstNeighbors& neighbors , bool fromBehind , Pointer( node_index_type ) maps[MaxCellDim+1] )
564 {
565 HyperCube::Direction dir = fromBehind ? HyperCube::FRONT : HyperCube::BACK;
566 const TreeNode *node = neighbors.neighbors.data[ WindowIndex< IsotropicUIntPack< Dim , 3 > , IsotropicUIntPack< Dim , 1 > >::Index ];
567 node_index_type i = node->nodeData.nodeIndex;
568
569 // Iterate over the cells in the face
570 for( typename HyperCube::Cube< _Dim >::template Element< CellDim > _c ; _c<HyperCube::Cube< _Dim >::template ElementNum< CellDim >() ; _c++ )
571 {
572 // Get the corresponding index of the cell in the node
573 typename HyperCube::Cube< Dim >::template Element< CellDim > c( dir , _c.index );
574
575 bool owner = true;
576
577 // The index of the node relative to the cell
578 typename HyperCube::Cube< Dim >::template IncidentCubeIndex< CellDim > my_ic = HyperCubeTables< Dim , CellDim >::IncidentCube[c.index];
579
580 // Iterate over the nodes adjacent to the cell
581 for( typename HyperCube::Cube< Dim >::template IncidentCubeIndex< CellDim > ic ; ic<HyperCube::Cube< Dim >::template IncidentCubeNum< CellDim >() ; ic++ )
582 {
583 // Get the index of node relative to the cell neighbors
584 unsigned int xx = HyperCubeTables< Dim , CellDim >::CellOffset[c.index][ic.index];
585 // If the neighbor exists and comes before, they own the corner
586 if( IsActiveNode< Dim >( neighbors.neighbors.data[xx] ) && ic<my_ic ){ owner = false ; break; }
587 }
588 if( owner )
589 {
590 node_index_type myCount = ( i - nodeOffset ) * HyperCube::Cube< _Dim >::template ElementNum< CellDim >() + _c.index;
591 maps[CellDim][ myCount ] = 1;
592 // Set the cell index for all nodes incident on the cell
593 for( typename HyperCube::Cube< Dim >::template IncidentCubeIndex< CellDim > ic ; ic<HyperCube::Cube< Dim >::template IncidentCubeNum< CellDim >() ; ic++ ) // Iterate over the nodes adjacent to the cell
594 {
595 unsigned int xx = HyperCubeTables< Dim , CellDim >::CellOffset[c.index][ic.index];
596 // If the neighbor exits, sets its cell
597 if( IsActiveNode< Dim >( neighbors.neighbors.data[xx] ) ) this->template indices< CellDim >( neighbors.neighbors.data[xx] )[ HyperCubeTables< Dim , CellDim >::IncidentElementCoIndex[c.index][ic.index] ] = myCount;
598 }
599 }
600 }
601
602 if constexpr( CellDim==MaxCellDim ) return;
603 else _setProcess< CellDim+1 >( neighbors , fromBehind , maps );
604 }
605
606 template< unsigned int CellDim >
607 void _setCounts( Pointer( node_index_type ) maps[MaxCellDim+1] )
608 {
609 node_index_type count = 0;
610 for( node_index_type i=0 ; i<(node_index_type)size() * (node_index_type)HyperCube::Cube< _Dim >::template ElementNum< CellDim >() ; i++ )
611 if( maps[CellDim][i] ) maps[CellDim][i] = count++;
612 counts[ CellDim ] = count;
613
614 if constexpr( CellDim==MaxCellDim ) return;
615 else _setCounts< CellDim+1 >( maps );
616 }
617
618 template< unsigned int CellDim >
619 void _setTables( unsigned int i , Pointer( node_index_type ) maps[MaxCellDim+1] )
620 {
621 for( unsigned int j=0 ; j<HyperCube::Cube< _Dim >::template ElementNum< CellDim >() ; j++ )
622 std::get< CellDim >( tables )[i][j] = maps[CellDim][ std::get< CellDim >( tables )[i][j] ];
623
624 if constexpr( CellDim==MaxCellDim ) return;
625 else _setTables< CellDim+1 >( i , maps );
626 }
627 };
628
629 ///////////////////////
630 // SlabCellIndexData //
631 ///////////////////////
632 // A data-struture that allows us to take a node adjacent to together with a local cell index and returns the global cell's index
633 template< unsigned int Dim >
634 struct SlabCellIndexData : public CellIndexData< Dim-1 , Dim-1 >
635 {
636 static const unsigned int _Dim = Dim-1;
637 static const unsigned int _MaxCellDim = CellIndexData< _Dim , _Dim >::MaxCellDim;
638 template< unsigned int _CellDim > using CellIndices = typename CellIndexData< _Dim , _Dim >::template CellIndices< _CellDim >;
639 using TreeNode = RegularTreeNode< Dim , FEMTreeNodeData , depth_and_offset_type >;
640 using ConstOneRingNeighborKey = typename TreeNode::template ConstNeighborKey< IsotropicUIntPack< Dim , 1 > , IsotropicUIntPack< Dim , 1 > >;
641 using ConstNeighbors = typename TreeNode::template ConstNeighbors< IsotropicUIntPack< Dim , 3 > >;
642 using CellIndexData< _Dim , _Dim >::tables;
643 using CellIndexData< _Dim , _Dim >::size;
644 using CellIndexData< _Dim , _Dim >::resize;
645 using CellIndexData< _Dim , _Dim >::counts;
646
647 node_index_type nodeOffset;
648
649 SlabCellIndexData( void ) : nodeOffset(0) {}
650
651 void read( BinaryStream &stream )
652 {
653 if( !stream.read( nodeOffset ) ) MK_THROW( "Failed to read node ofset" );
654 CellIndexData< _Dim , _Dim >::read( stream );
655 }
656
657 void write( BinaryStream &stream ) const
658 {
659 stream.write( nodeOffset );
660 CellIndexData< _Dim , _Dim >::write( stream );
661 }
662
663 void set( const SortedTreeNodes< Dim > &sNodes , int depth , int slab )
664 {
665 std::pair< node_index_type , node_index_type > span( sNodes.begin( depth , slab ) , sNodes.end( depth , slab ) );
666 nodeOffset = (size_t)span.first;
667 resize( (size_t)( span.second - span.first ) );
668 _scratch.resize( size() );
669
670 std::vector< ConstOneRingNeighborKey > neighborKeys( ThreadPool::NumThreads() );
671 for( size_t i=0 ; i<neighborKeys.size() ; i++ ) neighborKeys[i].set( depth );
672
673 // Try and get at the nodes outside of the slab through the neighbor key
674 ThreadPool::ParallelFor( sNodes.begin( depth , slab ) , sNodes.end( depth , slab ) , [&]( unsigned int thread , size_t i )
675 {
676 ConstOneRingNeighborKey &neighborKey = neighborKeys[ thread ];
677 const TreeNode *node = sNodes.treeNodes[i];
678 ConstNeighbors &neighbors = neighborKey.getNeighbors( node );
679 _setProcess<0>( neighbors , _scratch.maps );
680 }
681 );
682
683 _setCounts<0>( _scratch.maps );
684 ThreadPool::ParallelFor( 0 , size() , [&]( unsigned int , size_t i ){ _setTables<0>( (unsigned int)i , _scratch.maps ); } );
685 }
686
687 // Maps from tree nodes (and their associated indices) to the associated indices for the cell indices
688 template< unsigned int CellDim > CellIndices< CellDim > &indices( const TreeNode *node ) { return std::get< CellDim >( tables )[ node->nodeData.nodeIndex - nodeOffset ]; }
689 template< unsigned int CellDim > CellIndices< CellDim > &indices( node_index_type nodeIndex ) { return std::get< CellDim >( tables )[ nodeIndex - nodeOffset ]; }
690 template< unsigned int CellDim > const CellIndices< CellDim > &indices( const TreeNode *node ) const { return std::get< CellDim >( tables )[ node->nodeData.nodeIndex - nodeOffset ]; }
691 template< unsigned int CellDim > const CellIndices< CellDim > &indices( node_index_type nodeIndex ) const { return std::get< CellDim >( tables )[ nodeIndex - nodeOffset ]; }
692
693 protected:
694 _Scratch< _Dim , _MaxCellDim > _scratch;
695
696
697 template< unsigned int _CellDim >
698 void _setProcess( const ConstNeighbors& neighbors , Pointer( node_index_type ) maps[_MaxCellDim+1] )
699 {
700 static const unsigned int CellDim = _CellDim+1;
701 const TreeNode *node = neighbors.neighbors.data[ WindowIndex< IsotropicUIntPack< Dim , 3 > , IsotropicUIntPack< Dim , 1 > >::Index ];
702 node_index_type i = node->nodeData.nodeIndex;
703
704 // Iterate over the cells in the face
705 for( typename HyperCube::Cube< _Dim >::template Element< _CellDim > _c ; _c<HyperCube::Cube< _Dim >::template ElementNum< _CellDim >() ; _c++ )
706 {
707 // Get the index of the extruded cell
708 typename HyperCube::Cube< Dim >::template Element< CellDim > c( HyperCube::CROSS , _c.index );
709 bool owner = true;
710
711 // The index of the node relative to the extruded cell
712 typename HyperCube::Cube< Dim >::template IncidentCubeIndex< CellDim > my_ic = HyperCubeTables< Dim , CellDim >::IncidentCube[c.index];
713
714 // Iterate over the nodes adjacent to the cell
715 for( typename HyperCube::Cube< Dim >::template IncidentCubeIndex< CellDim > ic ; ic<HyperCube::Cube< Dim >::template IncidentCubeNum< CellDim >() ; ic++ )
716 {
717 // Get the index of node relative to the cell neighbors
718 unsigned int xx = HyperCubeTables< Dim , CellDim >::CellOffset[c.index][ic.index];
719 // If the neighbor exists and comes before, they own the corner
720 if( IsActiveNode< Dim >( neighbors.neighbors.data[xx] ) && ic<my_ic ){ owner = false ; break; }
721 }
722 if( owner )
723 {
724 node_index_type myCount = ( i - nodeOffset ) * HyperCube::Cube< _Dim >::template ElementNum< _CellDim >() + _c.index;
725 maps[_CellDim][ myCount ] = 1;
726 // Set the cell index for all nodes incident on the cell
727 for( typename HyperCube::Cube< Dim >::template IncidentCubeIndex< CellDim > ic ; ic<HyperCube::Cube< Dim >::template IncidentCubeNum< CellDim >() ; ic++ ) // Iterate over the nodes adjacent to the cell
728 {
729 unsigned int xx = HyperCubeTables< Dim , CellDim >::CellOffset[c.index][ic.index];
730 // If the neighbor exits, sets its cell
731 if( IsActiveNode< Dim >( neighbors.neighbors.data[xx] ) )
732 this->template indices< _CellDim >( neighbors.neighbors.data[xx] )[ HyperCubeTables< Dim , CellDim >::IncidentElementCoIndex[c.index][ic.index] ] = myCount;
733 }
734 }
735 }
736
737 if constexpr( _CellDim==_MaxCellDim ) return;
738 else _setProcess< _CellDim+1 >( neighbors , maps );
739 }
740
741 template< unsigned int _CellDim >
742 void _setCounts( Pointer( node_index_type ) maps[_MaxCellDim+1] )
743 {
744 node_index_type count = 0;
745 for( node_index_type i=0 ; i<(node_index_type)size() * (node_index_type)HyperCube::Cube< _Dim >::template ElementNum< _CellDim >() ; i++ )
746 if( maps[_CellDim][i] ) maps[_CellDim][i] = count++;
747 counts[ _CellDim ] = count;
748
749 if constexpr( _CellDim==_MaxCellDim ) return;
750 else _setCounts< _CellDim+1 >( maps );
751 }
752
753 template< unsigned int _CellDim >
754 void _setTables( unsigned int i , Pointer( node_index_type ) maps[_MaxCellDim+1] )
755 {
756 for( unsigned int j=0 ; j<HyperCube::Cube< _Dim >::template ElementNum< _CellDim >() ; j++ )
757 std::get< _CellDim >( tables )[i][j] = maps[_CellDim][ std::get< _CellDim >( tables )[i][j] ];
758
759 if constexpr( _CellDim==_MaxCellDim ) return;
760 else _setTables< _CellDim+1 >( i , maps );
761 }
762 };
763
764 //////////////////
765 // KeyGenerator //
766 //////////////////
767 template< unsigned int Dim >
768 struct KeyGenerator
769 {
770 KeyGenerator( int maxDepth ) : _maxDepth(maxDepth){}
771
772 template< unsigned int CellDim >
773 Key< Dim > operator()( int depth , const int offset[Dim] , typename HyperCube::Cube< Dim >::template Element< CellDim > e ) const
774 {
775 static_assert( ( CellDim<=Dim ) , "[ERROR] Cell dimension cannot exceed total dimension" );
776 Key< Dim > key;
777 const HyperCube::Direction* x = HyperCubeTables< Dim , CellDim >::Directions[ e.index ];
778 for( int dd=0 ; dd<Dim ; dd++ ) key[dd] = index( depth , offset[dd] , x[dd] );
779 return key;
780 }
781
782 Key< Dim > operator()( int depth , int offset , Key< Dim-1 > key ) const
783 {
784 Key< Dim > pKey;
785 if( depth>_maxDepth ) MK_THROW( "Depth cannot exceed max depth: " , depth , " <= " , _maxDepth );
786 for( unsigned int d=0 ; d<Dim-1 ; d++ ) pKey[d] = key[d];
787 pKey[Dim-1] = cornerIndex( depth , offset );
788 return pKey;
789 }
790
791 // The corner indices are divisible by 4
792 unsigned int cornerIndex( unsigned int depth , unsigned int offset ) const { return offset<<( _maxDepth+2-depth); }
793 // The edge indices are odd
794 unsigned int edgeIndex( unsigned int depth , unsigned int offset ) const { return ( cornerIndex(depth,offset) + cornerIndex(depth,offset+1) )/2+1; }
795 unsigned int index( unsigned int depth , unsigned int offset , HyperCube::Direction dir ) const
796 {
797 return dir==HyperCube::CROSS ? edgeIndex( depth , offset ) : cornerIndex( depth , offset + ( dir==HyperCube::BACK ? 0 : 1 ) );
798 }
799
800 std::string to_string( Key< Dim > key ) const
801 {
802 std::stringstream stream;
803 stream << "(";
804 for( unsigned int d=0 ; d<Dim ; d++ )
805 {
806 if( d ) stream << ",";
807 if( key[d]&1 ) // If it's odd, it will be an edge
808 {
809 unsigned int depth = _maxDepth;
810 key[d]>>=1;
811 while( !(key[d]&1) ) depth-- , key[d]>>=1;
812 stream << "[" << (((key[d]-1)/2)<<(_maxDepth-depth)) << "," << (((key[d]+1)/2)<<(_maxDepth-depth)) << "]";
813 }
814 else stream << (key[d]>>2);
815 }
816 stream << ")/" << (1<<_maxDepth);
817 return stream.str();
818 }
819
820 int maxDepth( void ) const { return _maxDepth; }
821
822 protected:
823 int _maxDepth;
824 };
825
826 template< unsigned int D , unsigned int K >
827 unsigned int HyperCubeTables< D , K >::CellOffset[ ElementNum ][ IncidentCubeNum ];
828 template< unsigned int D , unsigned int K >
829 unsigned int HyperCubeTables< D , K >::IncidentElementCoIndex[ ElementNum ][ IncidentCubeNum ];
830 template< unsigned int D , unsigned int K >
831 unsigned int HyperCubeTables< D , K >::IncidentElementIndex[ ElementNum ][ IncidentCubeNum ];
832 template< unsigned int D , unsigned int K >
833 unsigned int HyperCubeTables< D , K >::CellOffsetAntipodal[ ElementNum ];
834 template< unsigned int D , unsigned int K >
835 typename HyperCube::Cube< D >::template IncidentCubeIndex < K > HyperCubeTables< D , K >::IncidentCube[ ElementNum ];
836 template< unsigned int D , unsigned int K >
837 typename HyperCube::Direction HyperCubeTables< D , K >::Directions[ ElementNum ][ D ];
838 template< unsigned int D , unsigned int K1 , unsigned int K2 >
839 typename HyperCube::Cube< D >::template Element< K2 > HyperCubeTables< D , K1 , K2 >::OverlapElements[ ElementNum1 ][ OverlapElementNum ];
840 template< unsigned int D , unsigned int K1 , unsigned int K2 >
841 bool HyperCubeTables< D , K1 , K2 >::Overlap[ ElementNum1 ][ ElementNum2 ];
842 }