Debian Codegraph @

src/thirdparty/PoissonRecon/FEMTree.LevelSet.3D.inl

raw
1 /*
2 Copyright (c) 2022, Michael Kazhdan
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 // Specialized level-set surface extraction
30 template< bool HasData , typename Real , typename Data >
31 struct _LevelSetExtractor< HasData , Real , 3 , Data >
32 {
33 static const unsigned int Dim = 3;
34 // Store the position, the (interpolated) gradient, the weight, and possibly data
35 typedef std::conditional_t
36 <
37 HasData ,
38 DirectSum< Real , Point< Real , Dim > , Point< Real , Dim > , Real , Data > ,
39 DirectSum< Real , Point< Real , Dim > , Point< Real , Dim > , Real >
40 > Vertex;
41
42 using OutputVertexStream = std::conditional_t
43 <
44 HasData ,
45 OutputDataStream< Point< Real , Dim > , Point< Real , Dim > , Real , Data > ,
46 OutputDataStream< Point< Real , Dim > , Point< Real , Dim > , Real >
47 >;
48
49 protected:
50 static std::atomic< size_t > _BadRootCount;
51
52 public:
53 typedef typename FEMTree< Dim , Real >::LocalDepth LocalDepth;
54 typedef typename FEMTree< Dim , Real >::LocalOffset LocalOffset;
55 typedef typename FEMTree< Dim , Real >::ConstOneRingNeighborKey ConstOneRingNeighborKey;
56 typedef typename FEMTree< Dim , Real >::ConstOneRingNeighbors ConstOneRingNeighbors;
57 typedef RegularTreeNode< Dim , FEMTreeNodeData , depth_and_offset_type > TreeNode;
58 template< unsigned int WeightDegree > using DensityEstimator = typename FEMTree< Dim , Real >::template DensityEstimator< WeightDegree >;
59 template< typename FEMSigPack , unsigned int PointD > using _Evaluator = typename FEMTree< Dim , Real >::template _Evaluator< FEMSigPack , PointD >;
60
61 using Key = LevelSetExtraction::Key< Dim >;
62 using IsoEdge = LevelSetExtraction::IsoEdge< Dim >;
63 template< unsigned int D , unsigned int ... Ks >
64 using HyperCubeTables = LevelSetExtraction::HyperCubeTables< D , Ks ... >;
65
66 ////////////////
67 // FaceEdges //
68 ////////////////
69 struct FaceEdges
70 {
71 IsoEdge edges[2];
72 int count;
73 FaceEdges( void ) : count(-1){}
74
75 friend FaceEdges SetAtomic( volatile FaceEdges & value , FaceEdges newValue )
76 {
77 FaceEdges oldEdge;
78 oldEdge.edges[0] = SetAtomic( value.edges[0] , newValue.edges[0] );
79 oldEdge.edges[1] = SetAtomic( value.edges[1] , newValue.edges[1] );
80 oldEdge.count = SetAtomic( value.count , newValue.count );
81 return oldEdge;
82 }
83 };
84
85 ///////////////
86 // SliceData //
87 ///////////////
88 class SliceData
89 {
90 public:
91 template< unsigned int Indices >
92 struct _Indices
93 {
94 node_index_type idx[Indices];
95 _Indices( void ){ for( unsigned int i=0 ; i<Indices ; i++ ) idx[i] = -1; }
96 node_index_type& operator[] ( int i ) { return idx[i]; }
97 const node_index_type& operator[] ( int i ) const { return idx[i]; }
98 };
99 // The four corner indices associated with a square face
100 using SquareCornerIndices = _Indices< HyperCube::Cube< Dim-1 >::template ElementNum< 0 >() >;
101 // The four edge indices associated with a square face
102 using SquareEdgeIndices = _Indices< HyperCube::Cube< Dim-1 >::template ElementNum< 1 >() >;
103 // The one face index associated with a square face
104 using SquareFaceIndices = _Indices< HyperCube::Cube< Dim-1 >::template ElementNum< 2 >() >;
105 };
106
107 /////////////////
108 // SliceValues //
109 /////////////////
110 struct SliceValues
111 {
112 struct Scratch
113 {
114 using FKeyValues = std::vector< std::vector< std::pair< Key , std::vector< IsoEdge > > > >;
115 using EKeyValues = std::vector< std::vector< std::pair< Key , std::pair< node_index_type , Vertex > > > >;
116 using VKeyValues = std::vector< std::vector< std::pair< Key , Key > > >;
117
118 FKeyValues fKeyValues;
119 EKeyValues eKeyValues;
120 VKeyValues vKeyValues;
121
122 #ifdef SANITIZED_PR
123 Pointer( std::atomic< char > ) cSet;
124 Pointer( std::atomic< char > ) eSet;
125 Pointer( std::atomic< char > ) fSet;
126 #else // !SANITIZED_PR
127 Pointer( char ) cSet;
128 Pointer( char ) eSet;
129 Pointer( char ) fSet;
130 #endif // SANITIZED_PR
131
132 Scratch( void )
133 {
134 vKeyValues.resize( ThreadPool::NumThreads() );
135 eKeyValues.resize( ThreadPool::NumThreads() );
136 fKeyValues.resize( ThreadPool::NumThreads() );
137 #ifdef SANITIZED_PR
138 cSet = NullPointer( std::atomic< char > );
139 eSet = NullPointer( std::atomic< char > );
140 fSet = NullPointer( std::atomic< char > );
141 #else // !SANITIZED_PR
142 cSet = NullPointer( char );
143 eSet = NullPointer( char );
144 fSet = NullPointer( char );
145 #endif // SANITIZED_PR
146 }
147
148 ~Scratch( void )
149 {
150 #ifdef SANITIZED_PR
151 DeletePointer( cSet );
152 DeletePointer( eSet );
153 DeletePointer( fSet );
154 #else // !SANITIZED_PR
155 FreePointer( cSet );
156 FreePointer( eSet );
157 FreePointer( fSet );
158 #endif // SANITIZED_PR
159 }
160
161 void reset( const LevelSetExtraction::SliceCellIndexData< Dim > &cellIndices )
162 {
163 for( size_t i=0 ; i<vKeyValues.size() ; i++ ) vKeyValues[i].clear();
164 for( size_t i=0 ; i<eKeyValues.size() ; i++ ) eKeyValues[i].clear();
165 for( size_t i=0 ; i<fKeyValues.size() ; i++ ) fKeyValues[i].clear();
166 #ifdef SANITIZED_PR
167 DeletePointer( cSet );
168 DeletePointer( eSet );
169 DeletePointer( fSet );
170 if( cellIndices.counts[0] )
171 {
172 cSet = NewPointer< std::atomic< char > >( cellIndices.counts[0] );
173 for( unsigned int i=0 ; i<cellIndices.counts[0] ; i++ ) cSet[i] = 0;
174 }
175 if( cellIndices.counts[1] )
176 {
177 eSet = NewPointer< std::atomic< char > >( cellIndices.counts[1] );
178 for( unsigned int i=0 ; i<cellIndices.counts[1] ; i++ ) eSet[i] = 0;
179 }
180 if( cellIndices.counts[2] )
181 {
182 fSet = NewPointer< std::atomic< char > >( cellIndices.counts[2] );
183 for( unsigned int i=0 ; i<cellIndices.counts[2] ; i++ ) fSet[i] = 0;
184 }
185 #else // !SANITIZED_PR
186 FreePointer( cSet );
187 FreePointer( eSet );
188 FreePointer( fSet );
189 if( cellIndices.counts[0] )
190 {
191 cSet = AllocPointer< char >( cellIndices.counts[0] );
192 memset( cSet , 0 , sizeof( char ) * cellIndices.counts[0] );
193 }
194 if( cellIndices.counts[1] )
195 {
196 eSet = AllocPointer< char >( cellIndices.counts[1] );
197 memset( eSet , 0 , sizeof( char ) * cellIndices.counts[1] );
198 }
199 if( cellIndices.counts[2] )
200 {
201 fSet = AllocPointer< char >( cellIndices.counts[2] );
202 memset( fSet , 0 , sizeof( char ) * cellIndices.counts[2] );
203 }
204 #endif // SANITIZED_PR
205 }
206 };
207
208 LevelSetExtraction::SliceCellIndexData< Dim > cellIndices;
209 Pointer( Real ) cornerValues ; Pointer( Point< Real , Dim > ) cornerGradients;
210 Pointer( Key ) edgeKeys;
211 Pointer( FaceEdges ) faceEdges;
212 Pointer( char ) mcIndices;
213 LevelSetExtraction::KeyMap< Dim , std::vector< IsoEdge > > faceEdgeMap;
214 LevelSetExtraction::KeyMap< Dim , std::pair< node_index_type , Vertex > > edgeVertexMap;
215 LevelSetExtraction::KeyMap< Dim , Key > vertexPairMap;
216
217 SliceValues( void )
218 {
219 _slice = -1;
220 _oldCCount = _oldECount = _oldFCount = 0;
221 _oldNCount = 0;
222 cornerValues = NullPointer( Real ) ; cornerGradients = NullPointer( Point< Real , Dim > );
223 edgeKeys = NullPointer( Key );
224 faceEdges = NullPointer( FaceEdges );
225 mcIndices = NullPointer( char );
226 }
227 ~SliceValues( void )
228 {
229 _oldCCount = _oldECount = _oldFCount = 0;
230 _oldNCount = 0;
231 FreePointer( cornerValues ) ; FreePointer( cornerGradients );
232 DeletePointer( edgeKeys );
233 DeletePointer( faceEdges );
234 FreePointer( mcIndices );
235 }
236
237 void setFromScratch( typename Scratch::VKeyValues &scratch )
238 {
239 for( unsigned int t=0 ; t<scratch.size() ; t++ )
240 {
241 for( int i=0 ; i<scratch[t].size() ; i++ )
242 {
243 vertexPairMap[ scratch[t][i].first ] = scratch[t][i].second;
244 vertexPairMap[ scratch[t][i].second ] = scratch[t][i].first;
245 }
246 scratch[t].clear();
247 }
248 }
249
250 void setFromScratch( typename Scratch::EKeyValues &scratch )
251 {
252 for( unsigned int t=0 ; t<scratch.size() ; t++ )
253 {
254 for( int i=0 ; i<scratch[t].size() ; i++ ) edgeVertexMap[ scratch[t][i].first ] = scratch[t][i].second;
255 scratch[t].clear();
256 }
257 }
258
259 void setFromScratch( typename Scratch::FKeyValues &scratch )
260 {
261 for( unsigned int t=0 ; t<scratch.size() ; t++ )
262 {
263 for( int i=0 ; i<scratch[t].size() ; i++ )
264 {
265 std::vector< IsoEdge > &faceEdges = faceEdgeMap[ scratch[t][i].first ];
266 faceEdges.insert( faceEdges.end() , scratch[t][i].second.begin() , scratch[t][i].second.end() );
267 }
268 scratch[t].clear();
269 }
270 }
271
272 unsigned int slice( void ) const { return _slice; }
273 void reset( unsigned int slice , bool computeGradients )
274 {
275 _slice = slice;
276 faceEdgeMap.clear() , edgeVertexMap.clear() , vertexPairMap.clear();
277
278 if( _oldNCount<(node_index_type)cellIndices.size() )
279 {
280 _oldNCount = (node_index_type)cellIndices.size();
281 FreePointer( mcIndices );
282 if( _oldNCount>0 ) mcIndices = AllocPointer< char >( _oldNCount );
283 }
284 if( _oldCCount<(node_index_type)cellIndices.counts[0] )
285 {
286 _oldCCount = (node_index_type)cellIndices.counts[0];
287 FreePointer( cornerValues ) ; FreePointer( cornerGradients );
288 if( cellIndices.counts[0]>0 )
289 {
290 cornerValues = AllocPointer< Real >( _oldCCount );
291 if( computeGradients ) cornerGradients = AllocPointer< Point< Real , Dim > >( _oldCCount );
292 }
293 }
294 if( _oldECount<(node_index_type)cellIndices.counts[1] )
295 {
296 _oldECount = (node_index_type)cellIndices.counts[1];
297 DeletePointer( edgeKeys );
298 edgeKeys = NewPointer< Key >( _oldECount );
299 }
300 if( _oldFCount<(node_index_type)cellIndices.counts[2] )
301 {
302 _oldFCount = (node_index_type)cellIndices.counts[2];
303 DeletePointer( faceEdges );
304 faceEdges = NewPointer< FaceEdges >( _oldFCount );
305 }
306 }
307
308 template< typename FaceIndexFunctor /* = std::function< LevelSetExtraction::Key< Dim > ( const TreeNode * , typename HyperCube::Cube< Dim >::template Element< 2 > ) */ >
309 void addIsoEdges( const LevelSetExtraction::KeyGenerator< Dim > &keyGenerator , FaceIndexFunctor &faceIndexFunctor , const FEMTree< Dim , Real > &tree , const Scratch &scratch , const TreeNode *leaf , unsigned int sliceIndex , std::vector< IsoEdge > &edges , bool isOriented ) const
310 {
311 auto _FaceIndex = [&]( const TreeNode *node )
312 {
313 int depth , offset[Dim];
314 tree.depthAndOffset( node , depth , offset );
315 typename HyperCube::Cube< Dim >::template Element< 2 > f;
316 if ( offset[Dim-1]+0==sliceIndex ) f = typename HyperCube::Cube< Dim >::template Element< 2 >( HyperCube::BACK , 0 );
317 else if( offset[Dim-1]+1==sliceIndex ) f = typename HyperCube::Cube< Dim >::template Element< 2 >( HyperCube::FRONT , 0 );
318 else MK_THROW( "Node/slice-index mismatch: " , offset[Dim-1] , " <-> " , sliceIndex );
319 return faceIndexFunctor( node , f );
320 };
321
322 int flip = isOriented ? 0 : 1;
323
324 node_index_type fIdx = cellIndices.template indices<2>(leaf->nodeData.nodeIndex)[0];
325 if( scratch.fSet[fIdx] )
326 {
327 const FaceEdges &fe = faceEdges[ fIdx ];
328 for( int i=0 ; i<fe.count ; i++ ) edges.push_back( IsoEdge( fe.edges[i][flip] , fe.edges[i][1-flip] ) );
329 }
330 else
331 {
332 Key key = _FaceIndex( leaf );
333 typename LevelSetExtraction::KeyMap< Dim , std::vector< IsoEdge > >::const_iterator iter = faceEdgeMap.find( key );
334 if( iter!=faceEdgeMap.end() )
335 {
336 const std::vector< IsoEdge >& _edges = iter->second;
337 for( size_t i=0 ; i<_edges.size() ; i++ ) edges.push_back( IsoEdge( _edges[i][flip] , _edges[i][1-flip] ) );
338 }
339 else
340 {
341 LocalDepth d ; LocalOffset off;
342 tree.depthAndOffset( leaf , d , off );
343 // [WARNING] Is this right? If the face isn't set, wouldn't it inherit?
344 MK_WARN( "Invalid face: [" , off[0] , " " , off[1] , " " , off[2] , " @ " , d , " | " , sliceIndex , " : " , leaf->nodeData.nodeIndex , " ( " , keyGenerator.to_string(key) , " | " , key.to_string() , " )" );
345 }
346 }
347 }
348
349 bool setVertexPair( Key current , Key &pair ) const
350 {
351 typename LevelSetExtraction::KeyMap< Dim , Key >::const_iterator iter;
352
353 if( (iter=vertexPairMap.find(current))!=vertexPairMap.end() )
354 {
355 pair = iter->second;
356 return true;
357 }
358 else return false;
359 }
360
361 bool setEdgeVertex( Key key , std::pair< node_index_type , Vertex > &edgeVertex ) const
362 {
363 typename LevelSetExtraction::KeyMap< Dim , std::pair< node_index_type , Vertex > >::const_iterator iter;
364 if( ( iter=edgeVertexMap.find( key ) )!=edgeVertexMap.end() )
365 {
366 edgeVertex = iter->second;
367 return true;
368 }
369 else return false;
370 }
371 protected:
372 node_index_type _oldCCount , _oldECount , _oldFCount;
373 node_index_type _oldNCount;
374 unsigned int _slice;
375 };
376
377 //////////////////
378 // XSliceValues //
379 //////////////////
380 struct XSliceValues
381 {
382 struct Scratch
383 {
384 using FKeyValues = std::vector< std::vector< std::pair< Key , std::vector< IsoEdge > > > >;
385 using EKeyValues = std::vector< std::vector< std::pair< Key , std::pair< node_index_type , Vertex > > > >;
386 using VKeyValues = std::vector< std::vector< std::pair< Key , Key > > >;
387
388 FKeyValues fKeyValues;
389 EKeyValues eKeyValues;
390 VKeyValues vKeyValues;
391
392 #ifdef SANITIZED_PR
393 Pointer( std::atomic< char > ) eSet;
394 Pointer( std::atomic< char > ) fSet;
395 #else // !SANITIZED_PR
396 Pointer( char ) eSet;
397 Pointer( char ) fSet;
398 #endif // SANITIZED_PR
399
400 Scratch( void )
401 {
402 vKeyValues.resize( ThreadPool::NumThreads() );
403 eKeyValues.resize( ThreadPool::NumThreads() );
404 fKeyValues.resize( ThreadPool::NumThreads() );
405 #ifdef SANITIZED_PR
406 eSet = NullPointer( std::atomic< char > );
407 fSet = NullPointer( std::atomic< char > );
408 #else // !SANITIZED_PR
409 eSet = NullPointer( char );
410 fSet = NullPointer( char );
411 #endif // SANITIZED_PR
412 }
413
414 ~Scratch( void )
415 {
416 #ifdef SANITIZED_PR
417 DeletePointer( eSet );
418 DeletePointer( fSet );
419 #else // !SANITIZED_PR
420 FreePointer( eSet );
421 FreePointer( fSet );
422 #endif // SANITIZED_PR
423 }
424
425 void reset( const LevelSetExtraction::SlabCellIndexData< Dim > &cellIndices )
426 {
427 for( size_t i=0 ; i<vKeyValues.size() ; i++ ) vKeyValues[i].clear();
428 for( size_t i=0 ; i<eKeyValues.size() ; i++ ) eKeyValues[i].clear();
429 for( size_t i=0 ; i<fKeyValues.size() ; i++ ) fKeyValues[i].clear();
430 #ifdef SANITIZED_PR
431 DeletePointer( eSet );
432 DeletePointer( fSet );
433 if( cellIndices.counts[0] )
434 {
435 eSet = NewPointer< std::atomic< char > >( cellIndices.counts[0] );
436 for( unsigned int i=0 ; i<cellIndices.counts[0] ; i++ ) eSet[i] = 0;
437 }
438 if( cellIndices.counts[1] )
439 {
440 fSet = NewPointer< std::atomic< char > >( cellIndices.counts[1] );
441 for( unsigned int i=0 ; i<cellIndices.counts[1] ; i++ ) fSet[i] = 0;
442 }
443 #else // !SANITIZED_PR
444 FreePointer( eSet );
445 FreePointer( fSet );
446 if( cellIndices.counts[0] )
447 {
448 eSet = AllocPointer< char >( cellIndices.counts[0] );
449 memset( eSet , 0 , sizeof( char ) * cellIndices.counts[0] );
450 }
451 if( cellIndices.counts[1] )
452 {
453 fSet = AllocPointer< char >( cellIndices.counts[1] );
454 memset( fSet , 0 , sizeof( char ) * cellIndices.counts[1] );
455 }
456 #endif // SANITIZED_PR
457 }
458 };
459
460 LevelSetExtraction::SlabCellIndexData< Dim > cellIndices;
461 Pointer( Key ) edgeKeys;
462 Pointer( FaceEdges ) faceEdges;
463 LevelSetExtraction::KeyMap< Dim , std::vector< IsoEdge > > faceEdgeMap;
464 LevelSetExtraction::KeyMap< Dim , std::pair< node_index_type , Vertex > > edgeVertexMap;
465 LevelSetExtraction::KeyMap< Dim , Key > vertexPairMap;
466
467 XSliceValues( void )
468 {
469 _slab = -1;
470 _oldECount = _oldFCount = 0;
471 edgeKeys = NullPointer( Key );
472 faceEdges = NullPointer( FaceEdges );
473 }
474
475 ~XSliceValues( void )
476 {
477 _oldECount = _oldFCount = 0;
478 DeletePointer( edgeKeys );
479 DeletePointer( faceEdges );
480 }
481
482 void setFromScratch( typename Scratch::VKeyValues &scratch )
483 {
484 for( unsigned int t=0 ; t<scratch.size() ; t++ )
485 {
486 for( int i=0 ; i<scratch[t].size() ; i++ )
487 {
488 vertexPairMap[ scratch[t][i].first ] = scratch[t][i].second;
489 vertexPairMap[ scratch[t][i].second ] = scratch[t][i].first;
490 }
491 scratch[t].clear();
492 }
493 }
494
495 void setFromScratch( typename Scratch::EKeyValues &scratch )
496 {
497 for( unsigned int t=0 ; t<scratch.size() ; t++ )
498 {
499 for( int i=0 ; i<scratch[t].size() ; i++ ) edgeVertexMap[ scratch[t][i].first ] = scratch[t][i].second;
500 scratch[t].clear();
501 }
502 }
503
504 void setFromScratch( typename Scratch::FKeyValues &scratch )
505 {
506 for( unsigned int t=0 ; t<scratch.size() ; t++ )
507 {
508 for( int i=0 ; i<scratch[t].size() ; i++ )
509 {
510 auto iter = faceEdgeMap.find( scratch[t][i].first );
511 if( iter==faceEdgeMap.end() ) faceEdgeMap[ scratch[t][i].first ] = scratch[t][i].second;
512 else for( int j=0 ; j<scratch[t][i].second.size() ; j++ ) iter->second.push_back( scratch[t][i].second[j] );
513 }
514 scratch[t].clear();
515 }
516 }
517
518 unsigned int slab( void ) const { return _slab; }
519 void reset( unsigned int slab )
520 {
521 _slab = slab;
522 faceEdgeMap.clear() , edgeVertexMap.clear() , vertexPairMap.clear();
523
524 if( _oldECount<(node_index_type)cellIndices.counts[0] )
525 {
526 _oldECount = (node_index_type)cellIndices.counts[0];
527 DeletePointer( edgeKeys );
528 edgeKeys = NewPointer< Key >( _oldECount );
529 }
530 if( _oldFCount<(node_index_type)cellIndices.counts[1] )
531 {
532 _oldFCount = (node_index_type)cellIndices.counts[1];
533 DeletePointer( faceEdges );
534 faceEdges = NewPointer< FaceEdges >( _oldFCount );
535 }
536 }
537
538 protected:
539 node_index_type _oldECount , _oldFCount;
540 unsigned int _slab;
541 };
542
543 ////////////////
544 // SlabValues //
545 ////////////////
546 struct SlabValues
547 {
548 protected:
549 XSliceValues _xSliceValues[2];
550 SliceValues _sliceValues[2];
551 typename SliceValues::Scratch _sliceScratch[2];
552 typename XSliceValues::Scratch _xSliceScratch[2];
553 public:
554 SliceValues& sliceValues( int idx ){ return _sliceValues[idx&1]; }
555 const SliceValues& sliceValues( int idx ) const { return _sliceValues[idx&1]; }
556 XSliceValues& xSliceValues( int idx ){ return _xSliceValues[idx&1]; }
557 const XSliceValues& xSliceValues( int idx ) const { return _xSliceValues[idx&1]; }
558 typename SliceValues::Scratch &sliceScratch ( int idx ) { return _sliceScratch[idx&1]; }
559 const typename SliceValues::Scratch &sliceScratch ( int idx ) const { return _sliceScratch[idx&1]; }
560 typename XSliceValues::Scratch &xSliceScratch( int idx ) { return _xSliceScratch[idx&1]; }
561 const typename XSliceValues::Scratch &xSliceScratch( int idx ) const { return _xSliceScratch[idx&1]; }
562
563 bool validSlice( int slice ) const { return _sliceValues[slice&1].slice()==slice; }
564 bool validXSlice( int slab ) const { return _xSliceValues[slab&1].slab()==slab; }
565 };
566
567 static std::vector< std::pair< node_index_type , node_index_type > > SetIncidence( const FEMTree< Dim , Real > &tree , const FEMTree< Dim-1 , Real > &sliceTree , LocalDepth fullDepth , unsigned int sliceAtMaxDepth , unsigned int maxDepth )
568 {
569 using SliceTreeNode = typename LevelSetExtractor< Real , Dim-1 , Point< Real , Dim-1 > >::TreeNode;
570 using SliceLocalDepth = typename FEMTree< Dim-1 , Real >::LocalDepth;
571 using SliceLocalOffset = typename FEMTree< Dim-1 , Real >::LocalOffset;
572
573 std::vector< std::pair< node_index_type , node_index_type > > incidence( sliceTree.nodesSize() , std::pair< node_index_type , node_index_type >( -1 , -1 ) );
574
575 // Recursively set the incidence
576 std::function< void ( const TreeNode * , const SliceTreeNode * ) > SetIncidenceFunctor = [&]( const TreeNode *node , const SliceTreeNode *sliceNode )
577 {
578 LocalDepth depth ; LocalOffset offset;
579 SliceLocalDepth sliceDepth ; SliceLocalOffset sliceOffset;
580 tree.depthAndOffset( node , depth , offset );
581 sliceTree.depthAndOffset( sliceNode , sliceDepth , sliceOffset );
582 if( depth!=sliceDepth ) MK_THROW( "Depths do not match: " , depth , " != " , sliceDepth );
583 for( unsigned int i=0 ; i<Dim-1 ; i++ ) if( offset[i]!=sliceOffset[i] ) MK_THROW( "Offsets do not match[ " , i , "]: " , offset[i] , " != " , sliceOffset[i] );
584
585 unsigned int beginAtMaxDepth = ( offset[Dim-1] + 0 )<<( maxDepth - depth );
586 unsigned int endAtMaxDepth = ( offset[Dim-1] + 1 )<<( maxDepth - depth );
587 unsigned int midAtMaxDepth = ( beginAtMaxDepth + endAtMaxDepth ) / 2;
588
589 if( node->nodeData.nodeIndex==-1 ) return;
590 else if( sliceNode->nodeData.nodeIndex==-1 ) MK_THROW( "Expected valid slice node" );
591
592 if( sliceAtMaxDepth<beginAtMaxDepth || sliceAtMaxDepth>endAtMaxDepth ) MK_THROW( "Bad slice: " , sliceAtMaxDepth , " in [ " , beginAtMaxDepth , " , " , endAtMaxDepth , " ]" );
593 if( depth>=fullDepth )
594 {
595 // Set the incidence
596 if ( sliceAtMaxDepth==beginAtMaxDepth ) incidence[ sliceNode->nodeData.nodeIndex ].second = node->nodeData.nodeIndex;
597 else if( sliceAtMaxDepth==endAtMaxDepth ) incidence[ sliceNode->nodeData.nodeIndex ].first = node->nodeData.nodeIndex;
598 else
599 {
600 incidence[ sliceNode->nodeData.nodeIndex ].second = node->nodeData.nodeIndex;
601 incidence[ sliceNode->nodeData.nodeIndex ].first = node->nodeData.nodeIndex;
602 }
603 }
604
605 if( !GetGhostFlag( node->children ) )
606 {
607 if( !sliceNode->children )
608 {
609 int d , off[Dim] , _d , _off[Dim-1];
610 Point< int , Dim > p;
611 Point< int , Dim-1 > _p;
612 for( unsigned int d=0 ; d<Dim ; d++ ) p[d] = off[d];
613 for( unsigned int d=0 ; d<Dim-1 ; d++ ) _p[d] = _off[d];
614 tree.depthAndOffset( node , d , off );
615 sliceTree.depthAndOffset( sliceNode , _d , _off );
616 MK_THROW( "Expected slice children: " , p , " @ " , d , " <-> " , _p , " @ " , _d , " : ", node->nodeData.nodeIndex , " <-> " , sliceNode->nodeData.nodeIndex );
617 }
618 if( sliceAtMaxDepth<=midAtMaxDepth ) for( int c=0 ; c<(1<<(Dim-1)) ; c++ ) SetIncidenceFunctor( node->children+(c ) , sliceNode->children + c );
619 if( sliceAtMaxDepth>=midAtMaxDepth ) for( int c=0 ; c<(1<<(Dim-1)) ; c++ ) SetIncidenceFunctor( node->children+(c|(1<<(Dim-1))) , sliceNode->children + c );
620 }
621 };
622 SetIncidenceFunctor( &tree.spaceRoot() , &sliceTree.spaceRoot() );
623 return incidence;
624 }
625
626 template< unsigned int WeightDegree , unsigned int DataSig , typename VertexStream , typename SliceFunctor /* = std::function< SliceValues & ( unsigned int ) > */ , typename ScratchFunctor /* = std::function< typename SliceValues::Scratch & ( unsigned int ) */ >
627 static void CopyIsoStructure
628 (
629 const LevelSetExtraction::KeyGenerator< Dim > &keyGenerator ,
630 const typename LevelSetExtractor< Real , Dim-1 , Point< Real , Dim-1 > >::TreeSliceValuesAndVertexPositions &boundaryInfo ,
631 const FEMTree< Dim , Real > &tree ,
632 LocalDepth fullDepth ,
633 unsigned int sliceAtMaxDepth ,
634 unsigned int maxDepth ,
635 SliceFunctor sliceFunctor ,
636 ScratchFunctor scratchFunctor ,
637 const std::vector< std::pair< node_index_type , node_index_type > > &incidence ,
638 VertexStream &vertexStream ,
639 bool gradientNormals ,
640 typename FEMIntegrator::template PointEvaluator< IsotropicUIntPack< Dim , DataSig > , ZeroUIntPack< Dim > > *pointEvaluator ,
641 const DensityEstimator< WeightDegree >* densityWeights ,
642 const SparseNodeData< ProjectiveData< Data , Real > , IsotropicUIntPack< Dim , DataSig > >* data ,
643 const Data &zeroData
644 )
645 {
646 using SliceTreeNode = typename LevelSetExtractor< Real , Dim-1 , Point< Real , Dim-1 > >::TreeNode;
647 using SliceSliceValues = typename LevelSetExtractor< Real , Dim-1 , Point< Real , Dim-1 > >::SliceValues;
648 using SliceKey = LevelSetExtraction::Key< Dim-1 >;
649 using SliceIsoEdge = LevelSetExtraction::IsoEdge< Dim-1 >;
650
651 auto PromoteIsoVertex = [&]( SliceKey key )
652 {
653 return keyGenerator( maxDepth , sliceAtMaxDepth , key );
654 };
655
656 auto PromoteIsoEdge = [&]( SliceIsoEdge edge )
657 {
658 IsoEdge pEdge;
659 for( unsigned int i=0 ; i<2 ; i++ ) pEdge[i] = keyGenerator( maxDepth , sliceAtMaxDepth , edge[i] );
660 return pEdge;
661 };
662
663 std::vector< node_index_type > vertexIndices( boundaryInfo.vertexPositions.size() );
664 std::vector< Vertex > vertices( boundaryInfo.vertexPositions.size() );
665 // Add the iso-vertices
666 {
667 static const unsigned int DataDegree = FEMSignature< DataSig >::Degree;
668
669 // A functor returning the finest leaf node containing the iso-vertex
670 std::function< const TreeNode * ( const TreeNode * , Key ) > FinestLeaf = [&]( const TreeNode *node , Key key )
671 {
672 const TreeNode *candidate = node;
673 if( node->children )
674 {
675 LocalDepth depth ; LocalOffset offset;
676 int start[Dim] , mid[Dim] , end[Dim];
677 tree.depthAndOffset( node , depth , offset );
678 for( unsigned int d=0 ; d<Dim ; d++ )
679 {
680 start[d] = (offset[d]+0)<<(maxDepth+2-depth);
681 end [d] = (offset[d]+1)<<(maxDepth+2-depth);
682 mid [d] = (start[d]+end[d])>>1;
683 }
684 for( unsigned int c=0 ; c<(1<<Dim) ; c++ )
685 {
686 bool containsKey = true;
687 for( unsigned int d=0 ; d<Dim ; d++ )
688 if( c&(1<<d) ) containsKey &= ((int)key[d]>=mid [d] && (int)key[d]<=end[d]);
689 else containsKey &= ((int)key[d]>=start[d] && (int)key[d]<=mid[d]);
690 if( containsKey )
691 {
692 const TreeNode *_candidate = FinestLeaf( node->children+c , key );
693 if( tree.depth(_candidate)>tree.depth(candidate) ) candidate = _candidate;
694 }
695 }
696 }
697 return candidate;
698 };
699
700 std::vector< Key > keys;
701 {
702 std::vector< LevelSetExtraction::Key< Dim-1 > > _keys = boundaryInfo.vertexKeys();
703 keys.resize( _keys.size() );
704 for( unsigned int i=0 ; i<_keys.size() ; i++ ) keys[i] = PromoteIsoVertex( _keys[i] );
705 }
706
707 for( unsigned int i=0 ; i<boundaryInfo.vertexPositions.size(); i++ )
708 {
709 ConstPointSupportKey< IsotropicUIntPack< Dim , WeightDegree > > weightKey;
710 ConstPointSupportKey< IsotropicUIntPack< Dim , DataDegree > > dataKey;
711 Point< Real , Dim > p;
712 for( unsigned int d=0 ; d<Dim-1 ; d++ ) p[d] = boundaryInfo.vertexPositions[i][d];
713 p[Dim-1] = (Real)sliceAtMaxDepth/(Real)(1<<maxDepth);
714 #ifdef SHOW_WARNINGS
715 #pragma message( "[WARNING] Not setting normal gradients" )
716 #endif // SHOW_WARNINGS
717 Real depth = (Real)1.;
718 Data dataValue;
719 const TreeNode *node = NULL;
720 if( densityWeights || data ) node = FinestLeaf( &tree.spaceRoot() , keys[i] );
721 if( densityWeights )
722 {
723 weightKey.set( node->depth() );
724 weightKey.getNeighbors( node );
725 Real weight;
726 tree._getSampleDepthAndWeight( *densityWeights , node , p , weightKey , depth , weight );
727 }
728 if constexpr( HasData ) if( data )
729 {
730 dataKey.set( node->depth() );
731 dataKey.getNeighbors( node );
732 Point< Real , Dim > start;
733 Real width;
734 tree.startAndWidth( node , start , width );
735 if( DataDegree==0 )
736 {
737 Point< Real , Dim > center( start[0] + width/2 , start[1] + width/2 , start[2] + width/2 );
738 ProjectiveData< Data , Real > pValue( zeroData );
739 tree.template _addEvaluation< ProjectiveData< Data , Real > , SparseNodeData< ProjectiveData< Data , Real > , IsotropicUIntPack< Dim , DataSig > > , 0 >( *data , center , *pointEvaluator , dataKey , pValue );
740 dataValue = pValue.weight ? pValue.value() : zeroData;
741 }
742 else
743 {
744 ProjectiveData< Data , Real > pValue( zeroData );
745 tree.template _addEvaluation< ProjectiveData< Data , Real > , SparseNodeData< ProjectiveData< Data , Real > , IsotropicUIntPack< Dim , DataSig > > , 0 >( *data , p , *pointEvaluator , dataKey , pValue );
746 dataValue = pValue.weight ? pValue.value() : zeroData;
747 }
748 }
749 vertices[i].template get<0>() = p;
750 vertices[i].template get<1>() = Point< Real , Dim >();
751 vertices[i].template get<2>() = depth;
752 if constexpr( HasData ) vertices[i].template get<3>() = dataValue;
753 vertexIndices[i] = (node_index_type)vertexStream.write( 0 , vertices[i] );
754 }
755 }
756
757 // Copy over the edge/face -> key tables
758 for( unsigned int depth=fullDepth ; depth<=std::min< unsigned int >( tree._maxDepth , boundaryInfo.sliceTree.depth() ) ; depth++ )
759 {
760 SliceValues &sValues = sliceFunctor( depth );
761 typename SliceValues::Scratch &sScratch = scratchFunctor( depth );
762 const SliceSliceValues &ssValues = boundaryInfo.sliceValues[depth];
763
764 auto CopyEdgeAndFaceInfo = [&]( node_index_type sliceIndex , node_index_type index )
765 {
766 if( index==-1 ) return;
767 const TreeNode *node = tree._sNodes.treeNodes[ index ];
768 const SliceTreeNode *sliceNode = boundaryInfo.sliceTree._sNodes.treeNodes[ sliceIndex ];
769
770 const typename LevelSetExtraction::SliceCellIndexData< Dim >::template CellIndices<1> &eIndices = sValues.cellIndices.template indices<1>( node );
771 const typename LevelSetExtraction::FullCellIndexData< Dim-1 >::template CellIndices<1> &sliceEIndices = ssValues.cellIndices.template indices< 1 >( sliceNode );
772 for( typename HyperCube::Cube< Dim-1 >::template Element< 1 > _e ; _e<HyperCube::Cube< Dim-1 >::template ElementNum< 1 >() ; _e++ )
773 {
774 node_index_type eIndex = eIndices[_e.index];
775 node_index_type sliceEIndex = sliceEIndices[_e.index];
776 if( ssValues.edgeKeys[sliceEIndex].idx[0]!=-1 )
777 {
778 sValues.edgeKeys[eIndex] = keyGenerator( maxDepth , sliceAtMaxDepth , ssValues.edgeKeys[sliceEIndex] );
779 sScratch.eSet[eIndex] = 1;
780 }
781 else sScratch.eSet[eIndex] = 0;
782 }
783
784 const typename LevelSetExtraction::SliceCellIndexData< Dim >::template CellIndices<2> &fIndices = sValues.cellIndices.template indices<2>( node );
785 const typename LevelSetExtraction::FullCellIndexData< Dim-1 >::template CellIndices< 2 > &sliceFIndices = ssValues.cellIndices.template indices<2>( sliceNode );
786 for( typename HyperCube::Cube< Dim-1 >::template Element< 2 > _f ; _f<HyperCube::Cube< Dim-1 >::template ElementNum< 2 >() ; _f++ )
787 {
788 node_index_type fIndex = fIndices[_f.index];
789 node_index_type sliceFIndex = sliceFIndices[_f.index];
790 sValues.faceEdges[fIndex].count = ssValues.faceEdges[sliceFIndex].count;
791 for( int c=0 ; c<ssValues.faceEdges[sliceFIndex].count ; c++ ) sValues.faceEdges[fIndex].edges[c] = PromoteIsoEdge( ssValues.faceEdges[sliceFIndex].edges[c] );
792 sScratch.fSet[fIndex] = sValues.faceEdges[fIndex].count!=-1;
793 }
794 };
795 for( node_index_type i=boundaryInfo.sliceTree.nodesBegin(depth) ; i<boundaryInfo.sliceTree.nodesEnd(depth) ; i++ ) CopyEdgeAndFaceInfo( i , incidence[i].first ) , CopyEdgeAndFaceInfo( i , incidence[i].second );
796 }
797
798 // Copy over the iso-information
799 for( unsigned int depth=fullDepth ; depth<=std::min< unsigned int >( tree._maxDepth , boundaryInfo.sliceTree.depth() ) ; depth++ )
800 {
801 SliceValues &sValues = sliceFunctor( depth );
802 const SliceSliceValues &ssValues = boundaryInfo.sliceValues[depth];
803
804 // Copy the face->edge map
805 for( auto iter=ssValues.faceEdgeMap.cbegin() ; iter!=ssValues.faceEdgeMap.cend() ; iter++ )
806 {
807 Key key = keyGenerator( maxDepth , sliceAtMaxDepth , iter->first );
808 std::vector< IsoEdge > edges( iter->second.size() );
809 for( unsigned int i=0 ; i<iter->second.size() ; i++ ) edges[i] = PromoteIsoEdge( iter->second[i] );
810 sValues.faceEdgeMap[key] = edges;
811 }
812
813 // Copy the edge-vertex map
814 for( auto iter=ssValues.edgeVertexMap.cbegin() ; iter!=ssValues.edgeVertexMap.cend() ; iter++ )
815 {
816 Key key = keyGenerator( maxDepth , sliceAtMaxDepth , iter->first );
817 node_index_type idx = iter->second;
818 sValues.edgeVertexMap[key] = std::pair< node_index_type , Vertex >( vertexIndices[idx] , vertices[idx] );
819 }
820
821 // Copy the vertex-vertex map
822 for( auto iter=ssValues.vertexPairMap.cbegin() ; iter!=ssValues.vertexPairMap.cend() ; iter++ )
823 {
824 Key key = keyGenerator( maxDepth , sliceAtMaxDepth , iter->first );
825 sValues.vertexPairMap[key] = keyGenerator( maxDepth , sliceAtMaxDepth , iter->second );
826 }
827 }
828 }
829
830 static void OverwriteCornerValues( const typename LevelSetExtractor< Real , Dim-1 , Point< Real , Dim-1 > >::TreeSliceValuesAndVertexPositions &boundaryInfo , const std::vector< Real > &dValues , const FEMTree< Dim , Real > &tree , LocalDepth depth , unsigned int sliceAtMaxDepth , unsigned int maxDepth , bool isBack , std::vector< SlabValues > &slabValues , const std::vector< std::pair< node_index_type , node_index_type > > &incidence )
831 {
832 using SliceTreeNode = typename LevelSetExtractor< Real , Dim-1 , Point< Real , Dim-1 > >::TreeNode;
833 using SliceSliceValues = typename LevelSetExtractor< Real , Dim-1 , Point< Real , Dim-1 > >::SliceValues;
834
835 unsigned int slice = sliceAtMaxDepth>>( maxDepth - depth );
836 if( !isBack && sliceAtMaxDepth!=( slice<<(maxDepth-depth ) ) ) slice++;
837 if( !slabValues[depth].validSlice( slice ) ) MK_THROW( "Invalid slice: " , slice , " @ " , depth , " : " , slabValues[depth].sliceValues(slice).slice() );
838 SliceValues &sValues = slabValues[depth].sliceValues( slice );
839 const SliceSliceValues &ssValues = boundaryInfo.sliceValues[depth];
840
841 if( sValues.cornerGradients && !ssValues.cornerGradients ) MK_THROW( "Epxected slice gradients" );
842
843 auto CopyCornerInfo = [&]( node_index_type sliceIndex , node_index_type index )
844 {
845
846 if( index==-1 ) return;
847 const TreeNode *node = tree._sNodes.treeNodes[ index ];
848 const SliceTreeNode *sliceNode = boundaryInfo.sliceTree._sNodes.treeNodes[ sliceIndex ];
849
850 const typename LevelSetExtraction::SliceCellIndexData< Dim >::template CellIndices< 0 > &cIndices = sValues.cellIndices.template indices<0>( node );
851 const typename LevelSetExtraction::FullCellIndexData< Dim-1 >::template CellIndices< 0 > &sliceCIndices = ssValues.cellIndices.template indices< 0 >( sliceNode );
852 for( typename HyperCube::Cube< Dim-1 >::template Element< 0 > _c ; _c<HyperCube::Cube< Dim-1 >::template ElementNum< 0 >() ; _c++ )
853 {
854 node_index_type vIndex = cIndices[_c.index];
855 node_index_type sliceVIndex = sliceCIndices[_c.index];
856 sValues.cornerValues[vIndex] = ssValues.cornerValues[sliceVIndex];
857 if( sValues.cornerGradients )
858 {
859 for( unsigned int i=0 ; i<Dim-1 ; i++ ) sValues.cornerGradients[vIndex][i] = ssValues.cornerGradients[sliceVIndex][i];
860 sValues.cornerGradients[vIndex][Dim-1] = dValues[sliceVIndex];
861 }
862 }
863 };
864 for( node_index_type i=boundaryInfo.sliceTree.nodesBegin(depth) ; i<boundaryInfo.sliceTree.nodesEnd(depth) ; i++ ) CopyCornerInfo( i , incidence[i].first ) , CopyCornerInfo( i , incidence[i].second );
865 }
866
867 template< unsigned int ... FEMSigs >
868 static void SetSliceCornerValuesAndMCIndices( const FEMTree< Dim , Real >& tree , ConstPointer( Real ) coefficients , ConstPointer( Real ) coarseCoefficients , Real isoValue , LocalDepth depth , LocalDepth fullDepth , int slice , std::vector< SlabValues >& slabValues , const _Evaluator< UIntPack< FEMSigs ... > , 1 >& evaluator )
869 {
870 if( slice>0 ) SetSliceCornerValuesAndMCIndices< FEMSigs ... >( tree , coefficients , coarseCoefficients , isoValue , depth , fullDepth , slice , HyperCube::FRONT , slabValues , evaluator );
871 if( slice<(1<<depth) ) SetSliceCornerValuesAndMCIndices< FEMSigs ... >( tree , coefficients , coarseCoefficients , isoValue , depth , fullDepth , slice , HyperCube::BACK , slabValues , evaluator );
872 }
873
874 template< unsigned int ... FEMSigs >
875 static void SetSliceCornerValuesAndMCIndices( const FEMTree< Dim , Real >& tree , ConstPointer( Real ) coefficients , ConstPointer( Real ) coarseCoefficients , Real isoValue , LocalDepth depth , LocalDepth fullDepth , int slice , HyperCube::Direction zDir , std::vector< SlabValues >& slabValues , const _Evaluator< UIntPack< FEMSigs ... > , 1 >& evaluator )
876 {
877 static const unsigned int FEMDegrees[] = { FEMSignature< FEMSigs >::Degree ... };
878 SliceValues& sValues = slabValues[depth].sliceValues( slice );
879 typename SliceValues::Scratch &sScratch = slabValues[depth].sliceScratch( slice );
880 bool useBoundaryEvaluation = false;
881 for( int d=0 ; d<Dim ; d++ ) if( FEMDegrees[d]==0 || ( FEMDegrees[d]==1 && sValues.cornerGradients ) ) useBoundaryEvaluation = true;
882 std::vector< ConstPointSupportKey< UIntPack< FEMSignature< FEMSigs >::Degree ... > > > neighborKeys( ThreadPool::NumThreads() );
883 std::vector< ConstCornerSupportKey< UIntPack< FEMSignature< FEMSigs >::Degree ... > > > bNeighborKeys( ThreadPool::NumThreads() );
884 if( useBoundaryEvaluation ) for( size_t i=0 ; i<neighborKeys.size() ; i++ ) bNeighborKeys[i].set( tree._localToGlobal( depth ) );
885 else for( size_t i=0 ; i<neighborKeys.size() ; i++ ) neighborKeys[i].set( tree._localToGlobal( depth ) );
886 ThreadPool::ParallelFor( tree._sNodesBegin(depth,slice-(zDir==HyperCube::BACK ? 0 : 1)) , tree._sNodesEnd(depth,slice-(zDir==HyperCube::BACK ? 0 : 1)) , [&]( unsigned int thread , size_t i )
887 {
888 if( tree._isValidSpaceNode( tree._sNodes.treeNodes[i] ) )
889 {
890 Real squareValues[ HyperCube::Cube< Dim-1 >::template ElementNum< 0 >() ];
891 ConstPointSupportKey< UIntPack< FEMSignature< FEMSigs >::Degree ... > >& neighborKey = neighborKeys[ thread ];
892 ConstCornerSupportKey< UIntPack< FEMSignature< FEMSigs >::Degree ... > >& bNeighborKey = bNeighborKeys[ thread ];
893 TreeNode* leaf = tree._sNodes.treeNodes[i];
894 if( !IsActiveNode< Dim >( leaf->children ) )
895 {
896 const typename LevelSetExtraction::SliceCellIndexData< Dim >::template CellIndices<0> &cIndices = sValues.cellIndices.template indices<0>( leaf );
897
898 bool isInterior = tree._isInteriorlySupported( UIntPack< FEMSignature< FEMSigs >::Degree ... >() , leaf->parent );
899 if( useBoundaryEvaluation ) bNeighborKey.getNeighbors( leaf );
900 else neighborKey.getNeighbors( leaf );
901
902 for( typename HyperCube::Cube< Dim-1 >::template Element< 0 > _c ; _c<HyperCube::Cube< Dim-1 >::template ElementNum< 0 >() ; _c++ )
903 {
904 typename HyperCube::Cube< Dim >::template Element< 0 > c( zDir , _c.index );
905 #ifdef SANITIZED_PR
906 node_index_type vIndex = ReadAtomic( cIndices[_c.index] );
907 #else // !SANITIZED_PR
908 node_index_type vIndex = cIndices[_c.index];
909 #endif // SANITIZED_PR
910 if( !sScratch.cSet[vIndex] )
911 {
912 if( sValues.cornerGradients )
913 {
914 CumulativeDerivativeValues< Real , Dim , 1 > p;
915 if( useBoundaryEvaluation ) p = tree.template _getCornerValues< Real , 1 >( bNeighborKey , leaf , c.index , coefficients , coarseCoefficients , evaluator , tree._maxDepth , isInterior );
916 else p = tree.template _getCornerValues< Real , 1 >( neighborKey , leaf , c.index , coefficients , coarseCoefficients , evaluator , tree._maxDepth , isInterior );
917 #ifdef SANITIZED_PR
918 SetAtomic( sValues.cornerValues[vIndex] , p[0] );
919 SetAtomic( sValues.cornerGradients[vIndex] , Point< Real , Dim >( p[1] , p[2] , p[3] ) );
920 #else // !SANITIZED_PR
921 sValues.cornerValues[vIndex] = p[0] , sValues.cornerGradients[vIndex] = Point< Real , Dim >( p[1] , p[2] , p[3] );
922 #endif // SANITIZED_PR
923 }
924 else
925 {
926 #ifdef SANITIZED_PR
927 if( useBoundaryEvaluation ) SetAtomic( sValues.cornerValues[vIndex] , tree.template _getCornerValues< Real , 0 >( bNeighborKey , leaf , c.index , coefficients , coarseCoefficients , evaluator , tree._maxDepth , isInterior )[0] );
928 else SetAtomic( sValues.cornerValues[vIndex] , tree.template _getCornerValues< Real , 0 >( neighborKey , leaf , c.index , coefficients , coarseCoefficients , evaluator , tree._maxDepth , isInterior )[0] );
929 #else // !SANITIZED_PR
930 if( useBoundaryEvaluation ) sValues.cornerValues[vIndex] = tree.template _getCornerValues< Real , 0 >( bNeighborKey , leaf , c.index , coefficients , coarseCoefficients , evaluator , tree._maxDepth , isInterior )[0];
931 else sValues.cornerValues[vIndex] = tree.template _getCornerValues< Real , 0 >( neighborKey , leaf , c.index , coefficients , coarseCoefficients , evaluator , tree._maxDepth , isInterior )[0];
932 #endif // SANITIZED_PR
933 }
934 sScratch.cSet[vIndex] = 1;
935 }
936 #ifdef SANITIZED_PR
937 squareValues[_c.index] = ReadAtomic( sValues.cornerValues[ vIndex ] );
938 #else // !SANITIZED_PR
939 squareValues[_c.index] = sValues.cornerValues[ vIndex ];
940 #endif // SANITIZED_PR
941 TreeNode* node = leaf;
942 LocalDepth _depth = depth;
943 int _slice = slice;
944 while( _depth>fullDepth && tree._isValidSpaceNode( node->parent ) && (node-node->parent->children)==c.index )
945 {
946 node = node->parent , _depth-- , _slice >>= 1;
947 SliceValues &_sValues = slabValues[_depth].sliceValues( _slice );
948 typename SliceValues::Scratch &_sScratch = slabValues[_depth].sliceScratch( _slice );
949 const typename LevelSetExtraction::SliceCellIndexData< Dim >::template CellIndices<0> &_cIndices = _sValues.cellIndices.template indices<0>( node );
950 node_index_type _vIndex = _cIndices[_c.index];
951 #ifdef SANITIZED_PR
952 SetAtomic( _sValues.cornerValues[_vIndex] , ReadAtomic( sValues.cornerValues[vIndex] ) );
953 if( _sValues.cornerGradients ) SetAtomic( _sValues.cornerGradients[_vIndex] , ReadAtomic( sValues.cornerGradients[vIndex] ) );
954 #else // !SANITIZED_PR
955 _sValues.cornerValues[_vIndex] = sValues.cornerValues[vIndex];
956 if( _sValues.cornerGradients ) _sValues.cornerGradients[_vIndex] = sValues.cornerGradients[vIndex];
957 #endif // SANITIZED_PR
958 _sScratch.cSet[_vIndex] = 1;
959 }
960 }
961 sValues.mcIndices[ i - sValues.cellIndices.nodeOffset ] = HyperCube::Cube< Dim-1 >::MCIndex( squareValues , isoValue );
962 }
963 }
964 }
965 );
966 }
967
968 static void SetMCIndices( const FEMTree< Dim , Real >& tree , Real isoValue , LocalDepth depth , LocalDepth fullDepth , int slice , std::vector< SlabValues >& slabValues )
969 {
970 if( slice>0 ) SetMCIndices( tree , isoValue , depth , fullDepth , slice , HyperCube::FRONT , slabValues );
971 if( slice<(1<<depth) ) SetMCIndices( tree , isoValue , depth , fullDepth , slice , HyperCube::BACK , slabValues );
972 }
973
974 static void SetMCIndices( const FEMTree< Dim , Real >& tree , Real isoValue , LocalDepth depth , LocalDepth fullDepth , int slice , HyperCube::Direction zDir , std::vector< SlabValues >& slabValues )
975 {
976 SliceValues& sValues = slabValues[depth].sliceValues( slice );
977 bool useBoundaryEvaluation = false;
978 ThreadPool::ParallelFor( tree._sNodesBegin(depth,slice-(zDir==HyperCube::BACK ? 0 : 1)) , tree._sNodesEnd(depth,slice-(zDir==HyperCube::BACK ? 0 : 1)) , [&]( unsigned int thread , size_t i )
979 {
980 Real squareValues[ HyperCube::Cube< Dim-1 >::template ElementNum< 0 >() ];
981 TreeNode* leaf = tree._sNodes.treeNodes[i];
982
983 if( tree._isValidSpaceNode( leaf ) && !IsActiveNode< Dim >( leaf->children ) )
984 {
985 const typename LevelSetExtraction::SliceCellIndexData< Dim >::template CellIndices<0> &cIndices = sValues.cellIndices.template indices<0>( leaf );
986
987 for( typename HyperCube::Cube< Dim-1 >::template Element< 0 > _c ; _c<HyperCube::Cube< Dim-1 >::template ElementNum< 0 >() ; _c++ )
988 {
989 typename HyperCube::Cube< Dim >::template Element< 0 > c( zDir , _c.index );
990 node_index_type vIndex = cIndices[_c.index];
991 squareValues[_c.index] = sValues.cornerValues[ vIndex ];
992 sValues.mcIndices[ i - sValues.cellIndices.nodeOffset ] = HyperCube::Cube< Dim-1 >::MCIndex( squareValues , isoValue );
993 }
994 }
995 }
996 );
997 }
998
999 template< unsigned int WeightDegree , unsigned int DataSig , typename VertexStream >
1000 static void SetSliceIsoVertices( const LevelSetExtraction::KeyGenerator< Dim > &keyGenerator , const FEMTree< Dim , Real >& tree , bool nonLinearFit , bool gradientNormals , typename FEMIntegrator::template PointEvaluator< IsotropicUIntPack< Dim , DataSig > , ZeroUIntPack< Dim > >* pointEvaluator , const DensityEstimator< WeightDegree >* densityWeights , const SparseNodeData< ProjectiveData< Data , Real > , IsotropicUIntPack< Dim , DataSig > >* data , Real isoValue , LocalDepth depth , LocalDepth fullDepth , int slice , VertexStream &vertexStream , std::vector< SlabValues >& slabValues , const Data &zeroData )
1001 {
1002 if( slice>0 ) SetSliceIsoVertices< WeightDegree , DataSig >( keyGenerator , tree , nonLinearFit , gradientNormals , pointEvaluator , densityWeights , data , isoValue , depth , fullDepth , slice , HyperCube::FRONT , vertexStream , slabValues , zeroData );
1003 if( slice<(1<<depth) ) SetSliceIsoVertices< WeightDegree , DataSig >( keyGenerator , tree , nonLinearFit , gradientNormals , pointEvaluator , densityWeights , data , isoValue , depth , fullDepth , slice , HyperCube::BACK , vertexStream , slabValues , zeroData );
1004 }
1005
1006 template< unsigned int WeightDegree , unsigned int DataSig , typename VertexStream >
1007 static void SetSliceIsoVertices( const LevelSetExtraction::KeyGenerator< Dim > &keyGenerator , const FEMTree< Dim , Real >& tree , bool nonLinearFit , bool gradientNormals , typename FEMIntegrator::template PointEvaluator< IsotropicUIntPack< Dim , DataSig > , ZeroUIntPack< Dim > >* pointEvaluator , const DensityEstimator< WeightDegree >* densityWeights , const SparseNodeData< ProjectiveData< Data , Real > , IsotropicUIntPack< Dim , DataSig > >* data , Real isoValue , LocalDepth depth , LocalDepth fullDepth , int slice , HyperCube::Direction zDir , VertexStream &vertexStream , std::vector< SlabValues >& slabValues , const Data &zeroData )
1008 {
1009 auto _EdgeIndex = [&]( const TreeNode *node , typename HyperCube::Cube< Dim >::template Element< 1 > e )
1010 {
1011 int depth , offset[Dim];
1012 tree.depthAndOffset( node , depth , offset );
1013 return keyGenerator( depth , offset , e );
1014 };
1015
1016 static const unsigned int DataDegree = FEMSignature< DataSig >::Degree;
1017 SliceValues &sValues = slabValues[depth].sliceValues( slice );
1018 typename SliceValues::Scratch &sScratch = slabValues[depth].sliceScratch( slice );
1019 // [WARNING] In the case Degree=2, these two keys are the same, so we don't have to maintain them separately.
1020 std::vector< ConstOneRingNeighborKey > neighborKeys( ThreadPool::NumThreads() );
1021 std::vector< ConstPointSupportKey< IsotropicUIntPack< Dim , WeightDegree > > > weightKeys( ThreadPool::NumThreads() );
1022 std::vector< ConstPointSupportKey< IsotropicUIntPack< Dim , DataDegree > > > dataKeys( ThreadPool::NumThreads() );
1023 for( size_t i=0 ; i<neighborKeys.size() ; i++ ) neighborKeys[i].set( tree._localToGlobal( depth ) ) , weightKeys[i].set( tree._localToGlobal( depth ) ) , dataKeys[i].set( tree._localToGlobal( depth ) );
1024 ThreadPool::ParallelFor( tree._sNodesBegin(depth,slice-(zDir==HyperCube::BACK ? 0 : 1)) , tree._sNodesEnd(depth,slice-(zDir==HyperCube::BACK ? 0 : 1)) , [&]( unsigned int thread , size_t i )
1025 {
1026 if( tree._isValidSpaceNode( tree._sNodes.treeNodes[i] ) )
1027 {
1028 ConstOneRingNeighborKey& neighborKey = neighborKeys[ thread ];
1029 ConstPointSupportKey< IsotropicUIntPack< Dim , WeightDegree > >& weightKey = weightKeys[ thread ];
1030 ConstPointSupportKey< IsotropicUIntPack< Dim , DataDegree > >& dataKey = dataKeys[ thread ];
1031 TreeNode* leaf = tree._sNodes.treeNodes[i];
1032 if( !IsActiveNode< Dim >( leaf->children ) )
1033 {
1034 node_index_type idx = (node_index_type)i - sValues.cellIndices.nodeOffset;
1035 const typename LevelSetExtraction::SliceCellIndexData< Dim >::template CellIndices<1> &eIndices = sValues.cellIndices.template indices<1>( leaf );
1036 if( HyperCube::Cube< Dim-1 >::HasMCRoots( sValues.mcIndices[idx] ) )
1037 {
1038 neighborKey.getNeighbors( leaf );
1039 if( densityWeights ) weightKey.getNeighbors( leaf );
1040 if constexpr( HasData ) if( data ) dataKey.getNeighbors( leaf );
1041
1042 for( typename HyperCube::Cube< Dim-1 >::template Element< 1 > _e ; _e<HyperCube::Cube< Dim-1 >::template ElementNum< 1 >() ; _e++ )
1043 if( HyperCube::Cube< 1 >::HasMCRoots( HyperCube::Cube< Dim-1 >::ElementMCIndex( _e , sValues.mcIndices[idx] ) ) )
1044 {
1045 typename HyperCube::Cube< Dim >::template Element< 1 > e( zDir , _e.index );
1046 node_index_type vIndex = eIndices[_e.index];
1047 #ifdef SANITIZED_PR
1048 std::atomic< char > &edgeSet = sScratch.eSet[vIndex];
1049 #else // !SANITIZED_PR
1050 volatile char &edgeSet = sScratch.eSet[vIndex];
1051 #endif // SANITIZED_PR
1052 if( !edgeSet )
1053 {
1054 Vertex vertex;
1055 Key key = _EdgeIndex( leaf , e );
1056 GetIsoVertex< WeightDegree , DataSig >( tree , nonLinearFit , gradientNormals , pointEvaluator , densityWeights , data , isoValue , weightKey , dataKey , leaf , _e , zDir , sValues , vertex , zeroData );
1057 bool stillOwner = false;
1058 std::pair< node_index_type , Vertex > hashed_vertex;
1059
1060 {
1061 char desired = 1 , expected = 0;
1062 #ifdef SANITIZED_PR
1063 if( edgeSet.compare_exchange_weak( expected , desired ) )
1064 #else // !SANITIZED_PR
1065 if( SetAtomic( edgeSet , desired , expected ) )
1066 #endif // SANITIZED_PR
1067 {
1068 hashed_vertex = std::pair< node_index_type , Vertex >( (node_index_type)vertexStream.write( thread , vertex ) , vertex );
1069 stillOwner = true;
1070 }
1071 }
1072
1073 if( stillOwner )
1074 {
1075 sValues.edgeKeys[ vIndex ] = key;
1076 sScratch.eKeyValues[ thread ].push_back( std::pair< Key , std::pair< node_index_type , Vertex > >( key , hashed_vertex ) );
1077 // We only need to pass the iso-vertex down if the edge it lies on is adjacent to a coarser leaf
1078 auto IsNeeded = [&]( unsigned int depth )
1079 {
1080 bool isNeeded = false;
1081 typename HyperCube::Cube< Dim >::template IncidentCubeIndex< 1 > my_ic = HyperCubeTables< Dim , 1 >::IncidentCube[e.index];
1082 for( typename HyperCube::Cube< Dim >::template IncidentCubeIndex< 1 > ic ; ic<HyperCube::Cube< Dim >::template IncidentCubeNum< 1 >() ; ic++ ) if( ic!=my_ic )
1083 {
1084 unsigned int xx = HyperCubeTables< Dim , 1 >::CellOffset[e.index][ic.index];
1085 isNeeded |= !tree._isValidSpaceNode( neighborKey.neighbors[ tree._localToGlobal( depth ) ].neighbors.data[xx] );
1086 }
1087 return isNeeded;
1088 };
1089 if( IsNeeded( depth ) )
1090 {
1091 const typename HyperCube::Cube< Dim >::template Element< Dim-1 > *f = HyperCubeTables< Dim , 1 , Dim-1 >::OverlapElements[e.index];
1092 for( int k=0 ; k<HyperCubeTables< Dim , 1 , Dim-1 >::OverlapElementNum ; k++ )
1093 {
1094 TreeNode* node = leaf;
1095 LocalDepth _depth = depth;
1096 int _slice = slice;
1097 bool _cross = false;
1098 while( tree._isValidSpaceNode( node->parent ) && HyperCubeTables< Dim , Dim-1 , 0 >::Overlap[f[k].index][(unsigned int)(node-node->parent->children) ] )
1099 {
1100 if( _slice&1 ) _cross = true;
1101 node = node->parent , _depth-- , _slice >>= 1;
1102 SliceValues &_sValues = slabValues[_depth].sliceValues( _slice );
1103 typename SliceValues::Scratch &_sScratch = slabValues[_depth].sliceScratch( _slice );
1104
1105 if( _depth>=fullDepth && !_cross )
1106 {
1107 const typename LevelSetExtraction::SliceCellIndexData< Dim >::template CellIndices<1> &_eIndices = _sValues.cellIndices.template indices<1>( node );
1108 node_index_type _vIndex = _eIndices[_e.index];
1109 _sScratch.eSet[_vIndex] = 1;
1110 }
1111
1112 if( _cross )
1113 {
1114 XSliceValues& _xValues = slabValues[_depth].xSliceValues( _slice );
1115 typename XSliceValues::Scratch &_xScratch = slabValues[_depth].xSliceScratch( _slice );
1116 _xScratch.eKeyValues[ thread ].push_back( std::pair< Key , std::pair< node_index_type , Vertex > >( key , hashed_vertex ) );
1117 }
1118 else
1119 {
1120 SliceValues& _sValues = slabValues[_depth].sliceValues( _slice );
1121 typename SliceValues::Scratch &_sScratch = slabValues[_depth].sliceScratch( _slice );
1122 _sScratch.eKeyValues[ thread ].push_back( std::pair< Key , std::pair< node_index_type , Vertex > >( key , hashed_vertex ) );
1123 }
1124 if( !IsNeeded( _depth ) ) break;
1125 }
1126 }
1127 }
1128 }
1129 }
1130 }
1131 }
1132 }
1133 }
1134 }
1135 );
1136 }
1137
1138 ////////////////////
1139 // Iso-Extraction //
1140 ////////////////////
1141 template< unsigned int WeightDegree , unsigned int DataSig , typename VertexStream >
1142 static void SetXSliceIsoVertices( const LevelSetExtraction::KeyGenerator< Dim > &keyGenerator , const FEMTree< Dim , Real >& tree , bool nonLinearFit , bool gradientNormals , typename FEMIntegrator::template PointEvaluator< IsotropicUIntPack< Dim , DataSig > , ZeroUIntPack< Dim > >* pointEvaluator , const DensityEstimator< WeightDegree >* densityWeights , const SparseNodeData< ProjectiveData< Data , Real > , IsotropicUIntPack< Dim , DataSig > >* data , Real isoValue , LocalDepth depth , LocalDepth fullDepth , int slab , Real bCoordinate , Real fCoordinate , VertexStream &vertexStream , std::vector< SlabValues >& slabValues , const Data &zeroData )
1143 {
1144 auto _EdgeIndex = [&]( const TreeNode *node , typename HyperCube::Cube< Dim >::template Element< 1 > e )
1145 {
1146 int depth , offset[Dim];
1147 tree.depthAndOffset( node , depth , offset );
1148 return keyGenerator( depth , offset , e );
1149 };
1150
1151 static const unsigned int DataDegree = FEMSignature< DataSig >::Degree;
1152 SliceValues& bValues = slabValues[depth].sliceValues ( slab );
1153 SliceValues& fValues = slabValues[depth].sliceValues ( slab+1 );
1154 XSliceValues& xValues = slabValues[depth].xSliceValues( slab );
1155 typename SliceValues::Scratch &bScratch = slabValues[depth]. sliceScratch( slab );
1156 typename SliceValues::Scratch &fScratch = slabValues[depth]. sliceScratch( slab+1 );
1157 typename XSliceValues::Scratch &xScratch = slabValues[depth].xSliceScratch( slab );
1158
1159 // [WARNING] In the case Degree=2, these two keys are the same, so we don't have to maintain them separately.
1160 std::vector< ConstOneRingNeighborKey > neighborKeys( ThreadPool::NumThreads() );
1161 std::vector< ConstPointSupportKey< IsotropicUIntPack< Dim , WeightDegree > > > weightKeys( ThreadPool::NumThreads() );
1162 std::vector< ConstPointSupportKey< IsotropicUIntPack< Dim , DataDegree > > > dataKeys( ThreadPool::NumThreads() );
1163 for( size_t i=0 ; i<neighborKeys.size() ; i++ ) neighborKeys[i].set( tree._localToGlobal( depth ) ) , weightKeys[i].set( tree._localToGlobal( depth ) ) , dataKeys[i].set( tree._localToGlobal( depth ) );
1164 ThreadPool::ParallelFor( tree._sNodesBegin(depth,slab) , tree._sNodesEnd(depth,slab) , [&]( unsigned int thread , size_t i )
1165 {
1166 if( tree._isValidSpaceNode( tree._sNodes.treeNodes[i] ) )
1167 {
1168 ConstOneRingNeighborKey& neighborKey = neighborKeys[ thread ];
1169 ConstPointSupportKey< IsotropicUIntPack< Dim , WeightDegree > >& weightKey = weightKeys[ thread ];
1170 ConstPointSupportKey< IsotropicUIntPack< Dim , DataDegree > >& dataKey = dataKeys[ thread ];
1171 TreeNode* leaf = tree._sNodes.treeNodes[i];
1172 if( !IsActiveNode< Dim >( leaf->children ) )
1173 {
1174 unsigned char mcIndex = ( bValues.mcIndices[ i - bValues.cellIndices.nodeOffset ] ) | ( fValues.mcIndices[ i - fValues.cellIndices.nodeOffset ] )<<4;
1175 const typename LevelSetExtraction::SlabCellIndexData< Dim >::template CellIndices<0> &eIndices = xValues.cellIndices.template indices<0>( leaf );
1176 if( HyperCube::Cube< Dim >::HasMCRoots( mcIndex ) )
1177 {
1178 neighborKey.getNeighbors( leaf );
1179 if( densityWeights ) weightKey.getNeighbors( leaf );
1180 if constexpr( HasData ) if( data ) dataKey.getNeighbors( leaf );
1181 for( typename HyperCube::Cube< Dim-1 >::template Element< 0 > _c ; _c<HyperCube::Cube< Dim-1 >::template ElementNum< 0 >() ; _c++ )
1182 {
1183 typename HyperCube::Cube< Dim >::template Element< 1 > e( HyperCube::CROSS , _c.index );
1184 unsigned int _mcIndex = HyperCube::Cube< Dim >::ElementMCIndex( e , mcIndex );
1185 if( HyperCube::Cube< 1 >::HasMCRoots( _mcIndex ) )
1186 {
1187 node_index_type vIndex = eIndices[_c.index];
1188 #ifdef SANITIZED_PR
1189 std::atomic< char > &edgeSet = xScratch.eSet[vIndex];
1190 #else // !SANITIZED_PR
1191 volatile char &edgeSet = xScratch.eSet[vIndex];
1192 #endif // SANITIZED_PR
1193 if( !edgeSet )
1194 {
1195 Vertex vertex;
1196 Key key = _EdgeIndex( leaf , e.index );
1197 GetIsoVertex< WeightDegree , DataSig >( tree , nonLinearFit , gradientNormals , pointEvaluator , densityWeights , data , isoValue , weightKey , dataKey , leaf , _c , bCoordinate , fCoordinate , bValues , fValues , vertex , zeroData );
1198 bool stillOwner = false;
1199 std::pair< node_index_type , Vertex > hashed_vertex;
1200
1201 {
1202 char desired = 1 , expected = 0;
1203 #ifdef SANITIZED_PR
1204 if( edgeSet.compare_exchange_weak( expected , desired ) )
1205 #else // !SANITIZED_PR
1206 if( SetAtomic( edgeSet , desired , expected ) )
1207 #endif // SANITIZED_PR
1208 {
1209 hashed_vertex = std::pair< node_index_type , Vertex >( (node_index_type)vertexStream.write( thread , vertex ) , vertex );
1210 stillOwner = true;
1211 }
1212 }
1213
1214 if( stillOwner )
1215 {
1216 xValues.edgeKeys[ vIndex ] = key;
1217 xScratch.eKeyValues[ thread ].push_back( std::pair< Key , std::pair< node_index_type , Vertex > >( key , hashed_vertex ) );
1218
1219 // We only need to pass the iso-vertex down if the edge it lies on is adjacent to a coarser leaf
1220 auto IsNeeded = [&]( unsigned int depth )
1221 {
1222 bool isNeeded = false;
1223 typename HyperCube::Cube< Dim >::template IncidentCubeIndex< 1 > my_ic = HyperCubeTables< Dim , 1 >::IncidentCube[e.index];
1224 for( typename HyperCube::Cube< Dim >::template IncidentCubeIndex< 1 > ic ; ic<HyperCube::Cube< Dim >::template IncidentCubeNum< 1 >() ; ic++ ) if( ic!=my_ic )
1225 {
1226 unsigned int xx = HyperCubeTables< Dim , 1 >::CellOffset[e.index][ic.index];
1227 isNeeded |= !tree._isValidSpaceNode( neighborKey.neighbors[ tree._localToGlobal( depth ) ].neighbors.data[xx] );
1228 }
1229 return isNeeded;
1230 };
1231 if( IsNeeded( depth ) )
1232 {
1233 const typename HyperCube::Cube< Dim >::template Element< Dim-1 > *f = HyperCubeTables< Dim , 1 , Dim-1 >::OverlapElements[e.index];
1234 for( int k=0 ; k<2 ; k++ )
1235 {
1236 TreeNode* node = leaf;
1237 LocalDepth _depth = depth;
1238 int _slab = slab;
1239 // As long as we are still in the tree and the parent is also adjacent to the node
1240 while( tree._isValidSpaceNode( node->parent ) && HyperCubeTables< Dim , Dim-1 , 0 >::Overlap[f[k].index][(unsigned int)(node-node->parent->children) ] )
1241 {
1242 node = node->parent , _depth-- , _slab >>= 1;
1243 XSliceValues& _xValues = slabValues[_depth].xSliceValues( _slab );
1244 typename XSliceValues::Scratch &_xScratch = slabValues[_depth].xSliceScratch( _slab );
1245 _xScratch.eKeyValues[ thread ].push_back( std::pair< Key , std::pair< node_index_type , Vertex > >( key , hashed_vertex ) );
1246
1247 if( _depth>=fullDepth )
1248 {
1249 const typename LevelSetExtraction::SlabCellIndexData< Dim >::template CellIndices<0> &_eIndices = _xValues.cellIndices.template indices<0>( node );
1250 node_index_type _vIndex = _eIndices[_c.index];
1251 _xScratch.eSet[_vIndex] = 1;
1252 }
1253
1254 if( !IsNeeded( _depth ) ) break;
1255 }
1256 }
1257 }
1258 }
1259 }
1260 }
1261 }
1262 }
1263 }
1264 }
1265 }
1266 );
1267 }
1268
1269 static void CopyFinerSliceIsoEdgeKeys( const FEMTree< Dim , Real >& tree , LocalDepth depth , LocalDepth fullDepth , int slice , std::vector< SlabValues >& slabValues )
1270 {
1271 if( slice>0 ) CopyFinerSliceIsoEdgeKeys( tree , depth , fullDepth , slice , HyperCube::FRONT , slabValues );
1272 if( slice<(1<<depth) ) CopyFinerSliceIsoEdgeKeys( tree , depth , fullDepth , slice , HyperCube::BACK , slabValues );
1273 }
1274
1275 static void CopyFinerSliceIsoEdgeKeys( const FEMTree< Dim , Real >& tree , LocalDepth depth , LocalDepth fullDepth , int slice , HyperCube::Direction zDir , std::vector< SlabValues >& slabValues )
1276 {
1277 SliceValues& pSliceValues = slabValues[depth ].sliceValues(slice );
1278 SliceValues& cSliceValues = slabValues[depth+1].sliceValues(slice<<1);
1279 typename SliceValues::Scratch &pSliceScratch = slabValues[depth ].sliceScratch(slice );
1280 typename SliceValues::Scratch &cSliceScratch = slabValues[depth+1].sliceScratch(slice<<1);
1281 LevelSetExtraction::SliceCellIndexData< Dim > &pCellIndices = pSliceValues.cellIndices;
1282 LevelSetExtraction::SliceCellIndexData< Dim > &cCellIndices = cSliceValues.cellIndices;
1283 ThreadPool::ParallelFor( tree._sNodesBegin(depth,slice-(zDir==HyperCube::BACK ? 0 : 1)) , tree._sNodesEnd(depth,slice-(zDir==HyperCube::BACK ? 0 : 1)) , [&]( unsigned int thread , size_t i )
1284 {
1285 if( tree._isValidSpaceNode( tree._sNodes.treeNodes[i] ) ) if( IsActiveNode< Dim >( tree._sNodes.treeNodes[i]->children ) )
1286 {
1287 typename LevelSetExtraction::SliceCellIndexData< Dim >::template CellIndices<1> &pIndices = pCellIndices.template indices<1>( (node_index_type)i );
1288 // Copy the edges that overlap the coarser edges
1289 for( typename HyperCube::Cube< Dim-1 >::template Element< 1 > _e ; _e<HyperCube::Cube< Dim-1 >::template ElementNum< 1 >() ; _e++ )
1290 {
1291 node_index_type pIndex = pIndices[_e.index];
1292 {
1293 typename HyperCube::Cube< Dim >::template Element< 1 > e( zDir , _e.index );
1294 const typename HyperCube::Cube< Dim >::template Element< 0 > *c = HyperCubeTables< Dim , 1 , 0 >::OverlapElements[e.index];
1295 // [SANITY CHECK]
1296 // if( tree._isValidSpaceNode( tree._sNodes.treeNodes[i]->children + c[0].index )!=tree._isValidSpaceNode( tree._sNodes.treeNodes[i]->children + c[1].index ) ) MK_THROW( "Finer edges should both be valid or invalid" );
1297 if( !tree._isValidSpaceNode( tree._sNodes.treeNodes[i]->children + c[0].index ) || !tree._isValidSpaceNode( tree._sNodes.treeNodes[i]->children + c[1].index ) ) continue;
1298
1299 node_index_type cIndex1 = cCellIndices.template indices<1>( tree._sNodes.treeNodes[i]->children + c[0].index )[_e.index];
1300 node_index_type cIndex2 = cCellIndices.template indices<1>( tree._sNodes.treeNodes[i]->children + c[1].index )[_e.index];
1301 if( cSliceScratch.eSet[cIndex1] != cSliceScratch.eSet[cIndex2] )
1302 {
1303 Key key;
1304 if( cSliceScratch.eSet[cIndex1] ) key = cSliceValues.edgeKeys[cIndex1];
1305 else key = cSliceValues.edgeKeys[cIndex2];
1306 #ifdef SANITIZED_PR
1307 SetAtomic( pSliceValues.edgeKeys[pIndex] , key );
1308 #else // !SANITIZED_PR
1309 pSliceValues.edgeKeys[pIndex] = key;
1310 #endif // SANITIZED_PR
1311 pSliceScratch.eSet[pIndex] = 1;
1312 }
1313 else if( cSliceScratch.eSet[cIndex1] && cSliceScratch.eSet[cIndex2] )
1314 {
1315 Key key1 = cSliceValues.edgeKeys[cIndex1] , key2 = cSliceValues.edgeKeys[cIndex2];
1316 pSliceScratch.vKeyValues[ thread ].push_back( std::pair< Key , Key >( key1 , key2 ) );
1317
1318 const TreeNode* node = tree._sNodes.treeNodes[i];
1319 LocalDepth _depth = depth;
1320 int _slice = slice;
1321 while( _depth>fullDepth && tree._isValidSpaceNode( node->parent ) && HyperCubeTables< Dim , 1 , 0 >::Overlap[e.index][(unsigned int)(node-node->parent->children) ] )
1322 {
1323 node = node->parent , _depth-- , _slice >>= 1;
1324 SliceValues& _pSliceValues = slabValues[_depth].sliceValues(_slice);
1325 typename SliceValues::Scratch &_pSliceScratch = slabValues[_depth].sliceScratch(_slice);
1326 _pSliceScratch.vKeyValues[ thread ].push_back( std::pair< Key , Key >( key1 , key2 ) );
1327 }
1328 }
1329 }
1330 }
1331 }
1332 }
1333 );
1334 }
1335
1336 static void CopyFinerXSliceIsoEdgeKeys( const FEMTree< Dim , Real >& tree , LocalDepth depth , LocalDepth fullDepth , int slab , std::vector< SlabValues>& slabValues )
1337 {
1338 XSliceValues& pSliceValues = slabValues[depth ].xSliceValues(slab);
1339 XSliceValues& cSliceValues0 = slabValues[depth+1].xSliceValues( (slab<<1)|0 );
1340 XSliceValues& cSliceValues1 = slabValues[depth+1].xSliceValues( (slab<<1)|1 );
1341 typename XSliceValues::Scratch &pSliceScratch = slabValues[depth ].xSliceScratch(slab);
1342 typename XSliceValues::Scratch &cSliceScratch0 = slabValues[depth+1].xSliceScratch( (slab<<1)|0 );
1343 typename XSliceValues::Scratch &cSliceScratch1 = slabValues[depth+1].xSliceScratch( (slab<<1)|1 );
1344 LevelSetExtraction::SlabCellIndexData< Dim > &pCellIndices = pSliceValues. cellIndices;
1345 LevelSetExtraction::SlabCellIndexData< Dim > &cCellIndices0 = cSliceValues0.cellIndices;
1346 LevelSetExtraction::SlabCellIndexData< Dim > &cCellIndices1 = cSliceValues1.cellIndices;
1347
1348 bool has0 = cSliceValues0.slab()==((slab<<1)|0);
1349 bool has1 = cSliceValues1.slab()==((slab<<1)|1);
1350
1351 ThreadPool::ParallelFor( tree._sNodesBegin(depth,slab) , tree._sNodesEnd(depth,slab) , [&]( unsigned int thread , size_t i )
1352 {
1353 // If the node is not a leaf, inherit iso-edges from children
1354 if( tree._isValidSpaceNode( tree._sNodes.treeNodes[i] ) && IsActiveNode< Dim >( tree._sNodes.treeNodes[i]->children ) )
1355 {
1356 // Get the mapping from node + local face-corner -> global corner
1357 typename LevelSetExtraction::SlabCellIndexData< Dim >::template CellIndices<0> &pIndices = pCellIndices.template indices<0>( (node_index_type)i );
1358 for( typename HyperCube::Cube< Dim-1 >::template Element< 0 > _c ; _c<HyperCube::Cube< Dim-1 >::template ElementNum< 0 >() ; _c++ )
1359 {
1360 // Transform the face-corner index to a cross-edge index
1361 typename HyperCube::Cube< Dim >::template Element< 1 > e( HyperCube::CROSS , _c.index );
1362 node_index_type pIndex = pIndices[ _c.index ];
1363
1364 {
1365 typename HyperCube::Cube< Dim >::template Element< 0 > c0( HyperCube::BACK , _c.index ) , c1( HyperCube::FRONT , _c.index );
1366
1367 // [SANITY CHECK]
1368 // if( tree._isValidSpaceNode( tree._sNodes.treeNodes[i]->children + c0 )!=tree._isValidSpaceNode( tree._sNodes.treeNodes[i]->children + c1 ) ) MK_THROW( "Finer edges should both be valid or invalid" );
1369 if( !tree._isValidSpaceNode( tree._sNodes.treeNodes[i]->children + c0.index ) || !tree._isValidSpaceNode( tree._sNodes.treeNodes[i]->children + c1.index ) ) continue;
1370
1371 node_index_type cIndex0 , cIndex1;
1372 if( has0 ) cIndex0 = cCellIndices0.template indices<0>( tree._sNodes.treeNodes[i]->children + c0.index )[_c.index];
1373 if( has1 ) cIndex1 = cCellIndices1.template indices<0>( tree._sNodes.treeNodes[i]->children + c1.index )[_c.index];
1374 char eSet0 = has0 && cSliceScratch0.eSet[cIndex0] , eSet1 = has1 && cSliceScratch1.eSet[cIndex1];
1375
1376 // If there's one zero-crossing along the edge
1377 if( eSet0 != eSet1 )
1378 {
1379 Key key;
1380 if( eSet0 ) key = cSliceValues0.edgeKeys[cIndex0]; //, vPair = cSliceValues0.edgeVertexMap.find( key )->second;
1381 else key = cSliceValues1.edgeKeys[cIndex1]; //, vPair = cSliceValues1.edgeVertexMap.find( key )->second;
1382 #ifdef SANITIZED_PR
1383 SetAtomic( pSliceValues.edgeKeys[ pIndex ] , key );
1384 #else // !SANITIZED_PR
1385 pSliceValues.edgeKeys[ pIndex ] = key;
1386 #endif // SANITIZED_PR
1387 pSliceScratch.eSet[ pIndex ] = 1;
1388 }
1389 // If there's are two zero-crossings along the edge
1390 else if( eSet0 && eSet1 )
1391 {
1392 Key key0 = cSliceValues0.edgeKeys[cIndex0] , key1 = cSliceValues1.edgeKeys[cIndex1];
1393 pSliceScratch.vKeyValues[ thread ].push_back( std::pair< Key , Key >( key0 , key1 ) );
1394 const TreeNode* node = tree._sNodes.treeNodes[i];
1395 LocalDepth _depth = depth;
1396 int _slab = slab;
1397 while( _depth>fullDepth && tree._isValidSpaceNode( node->parent ) && HyperCubeTables< Dim , 1 , 0 >::Overlap[e.index][(unsigned int)(node-node->parent->children) ] )
1398 {
1399 node = node->parent , _depth-- , _slab>>= 1;
1400 SliceValues& _pSliceValues = slabValues[_depth].sliceValues(_slab);
1401 typename SliceValues::Scratch &_pSliceScratch = slabValues[_depth].sliceScratch(_slab);
1402 _pSliceScratch.vKeyValues[ thread ].push_back( std::pair< Key , Key >( key0 , key1 ) );
1403 }
1404 }
1405 }
1406 }
1407 }
1408 }
1409 );
1410 }
1411
1412 static void SetSliceIsoEdges( const LevelSetExtraction::KeyGenerator< Dim > &keyGenerator , const FEMTree< Dim , Real >& tree , LocalDepth depth , int slice , std::vector< SlabValues >& slabValues )
1413 {
1414 if( slice>0 ) SetSliceIsoEdges( keyGenerator , tree , depth , slice , HyperCube::FRONT , slabValues );
1415 if( slice<(1<<depth) ) SetSliceIsoEdges( keyGenerator , tree , depth , slice , HyperCube::BACK , slabValues );
1416 }
1417
1418 static void SetSliceIsoEdges( const LevelSetExtraction::KeyGenerator< Dim > &keyGenerator , const FEMTree< Dim , Real >& tree , LocalDepth depth , int slice , HyperCube::Direction zDir , std::vector< SlabValues >& slabValues )
1419 {
1420 auto _FaceIndex = [&]( const TreeNode *node , typename HyperCube::Cube< Dim >::template Element< 2 > f )
1421 {
1422 int depth , offset[Dim];
1423 tree.depthAndOffset( node , depth , offset );
1424 return keyGenerator( depth , offset , f );
1425 };
1426
1427 SliceValues& sValues = slabValues[depth].sliceValues( slice );
1428 typename SliceValues::Scratch &sScratch = slabValues[depth].sliceScratch( slice );
1429 std::vector< ConstOneRingNeighborKey > neighborKeys( ThreadPool::NumThreads() );
1430 for( size_t i=0 ; i<neighborKeys.size() ; i++ ) neighborKeys[i].set( tree._localToGlobal( depth ) );
1431 ThreadPool::ParallelFor( tree._sNodesBegin(depth, slice-(zDir==HyperCube::BACK ? 0 : 1)) , tree._sNodesEnd(depth,slice-(zDir==HyperCube::BACK ? 0 : 1)) , [&]( unsigned int thread , size_t i )
1432 {
1433 if( tree._isValidSpaceNode( tree._sNodes.treeNodes[i] ) )
1434 {
1435 int isoEdges[ 2 * HyperCube::MarchingSquares::MAX_EDGES ];
1436 ConstOneRingNeighborKey& neighborKey = neighborKeys[ thread ];
1437 TreeNode* leaf = tree._sNodes.treeNodes[i];
1438 if( !IsActiveNode< Dim >( leaf->children ) )
1439 {
1440 node_index_type idx = (node_index_type)i - sValues.cellIndices.nodeOffset;
1441 const typename LevelSetExtraction::SliceCellIndexData< Dim >::template CellIndices<1> &eIndices = sValues.cellIndices.template indices<1>( leaf );
1442 const typename LevelSetExtraction::SliceCellIndexData< Dim >::template CellIndices<2> &fIndices = sValues.cellIndices.template indices<2>( leaf );
1443 unsigned char mcIndex = sValues.mcIndices[idx];
1444 if( !sScratch.fSet[ fIndices[0] ] )
1445 {
1446 neighborKey.getNeighbors( leaf );
1447 unsigned int xx = WindowIndex< IsotropicUIntPack< Dim , 3 > , IsotropicUIntPack< Dim , 1 > >::Index + (zDir==HyperCube::BACK ? -1 : 1);
1448 if( !IsActiveNode< Dim >( neighborKey.neighbors[ tree._localToGlobal( depth ) ].neighbors.data[xx] ) || !IsActiveNode< Dim >( neighborKey.neighbors[ tree._localToGlobal( depth ) ].neighbors.data[xx]->children ) )
1449 {
1450 FaceEdges fe;
1451 fe.count = HyperCube::MarchingSquares::AddEdgeIndices( mcIndex , isoEdges );
1452 for( int j=0 ; j<fe.count ; j++ ) for( int k=0 ; k<2 ; k++ )
1453 {
1454 if( !sScratch.eSet[ eIndices[ isoEdges[2*j+k] ] ] ) MK_THROW( "Edge not set: " , slice-(zDir==HyperCube::BACK ? 0 : 1) , " / " , 1<<depth );
1455 fe.edges[j][k] = sValues.edgeKeys[ eIndices[ isoEdges[2*j+k] ] ];
1456 }
1457 sScratch.fSet[ fIndices[0] ] = 1;
1458 sValues.faceEdges[ fIndices[0] ] = fe;
1459
1460 TreeNode* node = leaf;
1461 LocalDepth _depth = depth;
1462 int _slice = slice;
1463 typename HyperCube::Cube< Dim >::template Element< Dim-1 > f( zDir , 0 );
1464 std::vector< IsoEdge > edges;
1465 edges.resize( fe.count );
1466 for( int j=0 ; j<fe.count ; j++ ) edges[j] = fe.edges[j];
1467 while( tree._isValidSpaceNode( node->parent ) && HyperCubeTables< Dim , 2 , 0 >::Overlap[f.index][(unsigned int)(node-node->parent->children) ] )
1468 {
1469 node = node->parent , _depth-- , _slice >>= 1;
1470 if( IsActiveNode< Dim >( neighborKey.neighbors[ tree._localToGlobal( _depth ) ].neighbors.data[xx] ) && IsActiveNode< Dim >( neighborKey.neighbors[ tree._localToGlobal( _depth ) ].neighbors.data[xx]->children ) ) break;
1471 Key key = _FaceIndex( node , f );
1472 SliceValues& _sValues = slabValues[_depth].sliceValues( _slice );
1473 typename SliceValues::Scratch &_sScratch = slabValues[_depth].sliceScratch( _slice );
1474 _sScratch.fKeyValues[ thread ].push_back( std::pair< Key , std::vector< IsoEdge > >( key , edges ) );
1475 }
1476 }
1477 }
1478 }
1479 }
1480 }
1481 );
1482 }
1483
1484 static void SetXSliceIsoEdges( const LevelSetExtraction::KeyGenerator< Dim > &keyGenerator , const FEMTree< Dim , Real >& tree , LocalDepth depth , int slab , std::vector< SlabValues >& slabValues )
1485 {
1486 auto _FaceIndex = [&]( const TreeNode *node , typename HyperCube::Cube< Dim >::template Element< 2 > f )
1487 {
1488 int depth , offset[Dim];
1489 tree.depthAndOffset( node , depth , offset );
1490 return keyGenerator( depth , offset , f );
1491 };
1492
1493 SliceValues& bValues = slabValues[depth].sliceValues ( slab );
1494 SliceValues& fValues = slabValues[depth].sliceValues ( slab+1 );
1495 XSliceValues& xValues = slabValues[depth].xSliceValues( slab );
1496 typename SliceValues::Scratch &bScratch = slabValues[depth]. sliceScratch( slab );
1497 typename SliceValues::Scratch &fScratch = slabValues[depth]. sliceScratch( slab+1 );
1498 typename XSliceValues::Scratch &xScratch = slabValues[depth].xSliceScratch( slab );
1499
1500 std::vector< ConstOneRingNeighborKey > neighborKeys( ThreadPool::NumThreads() );
1501 for( size_t i=0 ; i<neighborKeys.size() ; i++ ) neighborKeys[i].set( tree._localToGlobal( depth ) );
1502 ThreadPool::ParallelFor( tree._sNodesBegin(depth,slab) , tree._sNodesEnd(depth,slab) , [&]( unsigned int thread , size_t i )
1503 {
1504 if( tree._isValidSpaceNode( tree._sNodes.treeNodes[i] ) )
1505 {
1506 int isoEdges[ 2 * HyperCube::MarchingSquares::MAX_EDGES ];
1507 ConstOneRingNeighborKey& neighborKey = neighborKeys[ thread ];
1508 TreeNode* leaf = tree._sNodes.treeNodes[i];
1509 if( !IsActiveNode< Dim >( leaf->children ) )
1510 {
1511 const typename LevelSetExtraction::SlabCellIndexData< Dim >::template CellIndices<0> &cIndices = xValues.cellIndices.template indices<0>( leaf );
1512 const typename LevelSetExtraction::SlabCellIndexData< Dim >::template CellIndices<1> &eIndices = xValues.cellIndices.template indices<1>( leaf );
1513 unsigned char mcIndex = ( bValues.mcIndices[ i - bValues.cellIndices.nodeOffset ] ) | ( fValues.mcIndices[ i - fValues.cellIndices.nodeOffset ]<<4 );
1514 {
1515 neighborKey.getNeighbors( leaf );
1516 // Iterate over the edges on the back
1517 for( typename HyperCube::Cube< Dim-1 >::template Element< 1 > _e ; _e<HyperCube::Cube< Dim-1 >::template ElementNum< 1 >() ; _e++ )
1518 {
1519 typename HyperCube::Cube< Dim >::template Element< 2 > f( HyperCube::CROSS , _e.index );
1520 unsigned char _mcIndex = HyperCube::Cube< Dim >::template ElementMCIndex< 2 >( f , mcIndex );
1521
1522 unsigned int xx = HyperCubeTables< Dim , 2 >::CellOffsetAntipodal[f.index];
1523 if( !xScratch.fSet[ eIndices[_e.index] ] && ( !IsActiveNode< Dim >( neighborKey.neighbors[ tree._localToGlobal( depth ) ].neighbors.data[xx] ) || !IsActiveNode< Dim >( neighborKey.neighbors[ tree._localToGlobal( depth ) ].neighbors.data[xx]->children ) ) )
1524 {
1525 FaceEdges fe;
1526 fe.count = HyperCube::MarchingSquares::AddEdgeIndices( _mcIndex , isoEdges );
1527 for( int j=0 ; j<fe.count ; j++ ) for( int k=0 ; k<2 ; k++ )
1528 {
1529 typename HyperCube::Cube< Dim >::template Element< 1 > e( f , typename HyperCube::Cube< Dim-1 >::template Element< 1 >( isoEdges[2*j+k] ) );
1530 HyperCube::Direction dir ; unsigned int coIndex;
1531 e.factor( dir , coIndex );
1532 if( dir==HyperCube::CROSS ) // Cross-edge
1533 {
1534 node_index_type idx = cIndices[ coIndex ];
1535 if( !xScratch.eSet[ idx ] ) MK_THROW( "Edge not set: " , slab , " / " , 1<<depth );
1536 fe.edges[j][k] = xValues.edgeKeys[ idx ];
1537 }
1538 else
1539 {
1540 const SliceValues& sValues = dir==HyperCube::BACK ? bValues : fValues;
1541 const typename SliceValues::Scratch &sScratch = dir==HyperCube::BACK ? bScratch : fScratch;
1542 node_index_type idx = sValues.cellIndices.template indices<1>((node_index_type)i)[ coIndex ];
1543 if( !sScratch.eSet[ idx ] ) MK_THROW( "Edge not set: " , slab , " / " , 1<<depth );
1544 fe.edges[j][k] = sValues.edgeKeys[ idx ];
1545 }
1546 }
1547 xScratch.fSet[ eIndices[_e.index] ] = 1;
1548 #ifdef SANITIZED_PR
1549 SetAtomic( xValues.faceEdges[ eIndices[_e.index] ] , fe );
1550 #else // !SANITIZED_PR
1551 xValues.faceEdges[ eIndices[_e.index] ] = fe;
1552 #endif // SANITIZED_PR
1553
1554 TreeNode* node = leaf;
1555 LocalDepth _depth = depth;
1556 int _slab = slab;
1557 std::vector< IsoEdge > edges;
1558 edges.resize( fe.count );
1559 for( int j=0 ; j<fe.count ; j++ ) edges[j] = fe.edges[j];
1560 while( tree._isValidSpaceNode( node->parent ) && HyperCubeTables< Dim , 2 , 0 >::Overlap[f.index][(unsigned int)(node-node->parent->children) ] )
1561 {
1562 node = node->parent , _depth-- , _slab >>= 1;
1563 if( IsActiveNode< Dim >( neighborKey.neighbors[ tree._localToGlobal( _depth ) ].neighbors.data[xx] ) && IsActiveNode< Dim >( neighborKey.neighbors[ tree._localToGlobal( _depth ) ].neighbors.data[xx]->children ) ) break;
1564 Key key = _FaceIndex( node , f );
1565 XSliceValues& _xValues = slabValues[_depth].xSliceValues( _slab );
1566 typename XSliceValues::Scratch &_xScratch = slabValues[_depth].xSliceScratch( _slab );
1567 _xScratch.fKeyValues[ thread ].push_back( std::pair< Key , std::vector< IsoEdge > >( key , edges ) );
1568 }
1569 }
1570 }
1571 }
1572 }
1573 }
1574 } );
1575 }
1576
1577 template< typename VertexStream , typename FaceIndexFunctor /* = std::function< LevelSetExtraction::Key< Dim > ( const TreeNode * , typename HyperCube::Cube< Dim >::template Element< 2 > ) */ >
1578 static void SetLevelSet( const LevelSetExtraction::KeyGenerator< Dim > &keyGenerator , FaceIndexFunctor faceIndexFunctor , const FEMTree< Dim , Real >& tree , LocalDepth depth , int offset , const SliceValues& bValues , const SliceValues& fValues , const XSliceValues& xValues , const typename SliceValues::Scratch &bScratch , const typename SliceValues::Scratch &fScratch , const typename XSliceValues::Scratch &xScratch , VertexStream &vertexStream , OutputDataStream< std::vector< node_index_type > > &polygonStream , bool polygonMesh , bool addBarycenter , bool flipOrientation )
1579 {
1580 std::vector< std::pair< node_index_type , Vertex > > polygon;
1581 std::vector< std::vector< IsoEdge > > edgess( ThreadPool::NumThreads() );
1582 ThreadPool::ParallelFor( tree._sNodesBegin(depth,offset) , tree._sNodesEnd(depth,offset) , [&]( unsigned int thread , size_t i )
1583 {
1584 if( tree._isValidSpaceNode( tree._sNodes.treeNodes[i] ) )
1585 {
1586 std::vector< IsoEdge >& edges = edgess[ thread ];
1587 TreeNode* leaf = tree._sNodes.treeNodes[i];
1588 int res = 1<<depth;
1589 LocalDepth d ; LocalOffset off;
1590 tree._localDepthAndOffset( leaf , d , off );
1591 bool inBounds = off[0]>=0 && off[0]<res && off[1]>=0 && off[1]<res && off[2]>=0 && off[2]<res;
1592 if( inBounds && !IsActiveNode< Dim >( leaf->children ) )
1593 {
1594 edges.clear();
1595 {
1596 // Gather the edges from the faces (with the correct orientation)
1597 for( typename HyperCube::Cube< Dim >::template Element< Dim-1 > f ; f<HyperCube::Cube< Dim >::template ElementNum< Dim-1 >() ; f++ )
1598 {
1599 bool isOriented = HyperCube::Cube< Dim >::IsOriented( f );
1600 int flip = isOriented ? 0 : 1;
1601 HyperCube::Direction fDir = f.direction();
1602
1603 if ( fDir==HyperCube::BACK ) bValues.addIsoEdges( keyGenerator , faceIndexFunctor , tree , bScratch , leaf , offset+0 , edges , isOriented );
1604 else if( fDir==HyperCube::FRONT ) fValues.addIsoEdges( keyGenerator , faceIndexFunctor , tree , fScratch , leaf , offset+1 , edges , isOriented );
1605 else
1606 {
1607 node_index_type fIdx = xValues.cellIndices.template indices<1>((node_index_type)i)[ f.coIndex() ];
1608 if( xScratch.fSet[fIdx] )
1609 {
1610 const FaceEdges& fe = xValues.faceEdges[ fIdx ];
1611 for( int j=0 ; j<fe.count ; j++ ) edges.push_back( IsoEdge( fe.edges[j][flip] , fe.edges[j][1-flip] ) );
1612 }
1613 else
1614 {
1615 Key key = faceIndexFunctor( leaf , f );
1616 typename LevelSetExtraction::KeyMap< Dim , std::vector< IsoEdge > >::const_iterator iter = xValues.faceEdgeMap.find(key);
1617 if( iter!=xValues.faceEdgeMap.end() )
1618 {
1619 const std::vector< IsoEdge >& _edges = iter->second;
1620 for( size_t j=0 ; j<_edges.size() ; j++ ) edges.push_back( IsoEdge( _edges[j][flip] , _edges[j][1-flip] ) );
1621 }
1622 else MK_THROW( "Invalid faces: " , i , " " , fDir==HyperCube::BACK ? "back" : ( fDir==HyperCube::FRONT ? "front" : ( fDir==HyperCube::CROSS ? "cross" : "unknown" ) ) );
1623 }
1624 }
1625 }
1626 // Get the edge loops
1627 std::vector< std::vector< Key > > loops;
1628 while( edges.size() )
1629 {
1630 loops.resize( loops.size()+1 );
1631 IsoEdge edge = edges.back();
1632 edges.pop_back();
1633 Key start = edge[0] , current = edge[1];
1634 while( current!=start )
1635 {
1636 int idx;
1637 for( idx=0 ; idx<(int)edges.size() ; idx++ ) if( edges[idx][0]==current ) break;
1638 if( idx==edges.size() )
1639 {
1640 typename LevelSetExtraction::KeyMap< Dim , Key >::const_iterator iter;
1641 Key pair;
1642 if ( bValues.setVertexPair(current,pair) ) loops.back().push_back( current ) , current = pair;
1643 else if( fValues.setVertexPair(current,pair) ) loops.back().push_back( current ) , current = pair;
1644 else if( (iter=xValues.vertexPairMap.find(current))!=xValues.vertexPairMap.end() ) loops.back().push_back( current ) , current = iter->second;
1645 else MK_THROW( "Failed to close loop for node[" , i , "]: [" , off[0] , " " , off[1] , " " , off[2] , " @ " , d , "] | " , keyGenerator.to_string( current ) , " -- " , keyGenerator.to_string( start ) , " | " , current.to_string() , " -- " , start.to_string() );
1646 }
1647 else
1648 {
1649 loops.back().push_back( current );
1650 current = edges[idx][1];
1651 edges[idx] = edges.back() , edges.pop_back();
1652 }
1653 }
1654 loops.back().push_back( start );
1655 }
1656 // Add the loops to the mesh
1657 for( size_t j=0 ; j<loops.size() ; j++ )
1658 {
1659 std::vector< std::pair< node_index_type , Vertex > > polygon( loops[j].size() );
1660 for( size_t k=0 ; k<loops[j].size() ; k++ )
1661 {
1662 Key key = loops[j][k];
1663 typename LevelSetExtraction::KeyMap< Dim , std::pair< node_index_type , Vertex > >::const_iterator iter;
1664 size_t kk = flipOrientation ? loops[j].size()-1-k : k;
1665 if ( bValues.setEdgeVertex( key , polygon[kk] ) );
1666 else if( fValues.setEdgeVertex( key , polygon[kk] ) );
1667 else if( ( iter=xValues.edgeVertexMap.find( key ) )!=xValues.edgeVertexMap.end() ) polygon[kk] = iter->second;
1668 else MK_THROW( "Couldn't find vertex in edge map: " , off[0] , " , " , off[1] , " , " , off[2] , " @ " , depth , " : " , keyGenerator.to_string( key ) , " | " , key.to_string() );
1669 }
1670 AddIsoPolygons( thread , vertexStream , polygonStream , polygon , polygonMesh , addBarycenter );
1671 }
1672 }
1673 }
1674 }
1675 } );
1676 }
1677
1678 template< unsigned int WeightDegree , unsigned int DataSig >
1679 static bool GetIsoVertex
1680 (
1681 const FEMTree< Dim , Real >& tree ,
1682 bool nonLinearFit ,
1683 bool gradientNormals ,
1684 typename FEMIntegrator::template PointEvaluator< IsotropicUIntPack< Dim , DataSig > , ZeroUIntPack< Dim > > *pointEvaluator ,
1685 const DensityEstimator< WeightDegree > *densityWeights ,
1686 const SparseNodeData< ProjectiveData< Data , Real > , IsotropicUIntPack< Dim , DataSig > > *data ,
1687 Real isoValue ,
1688 ConstPointSupportKey< IsotropicUIntPack< Dim , WeightDegree > >& weightKey ,
1689 ConstPointSupportKey< IsotropicUIntPack< Dim , FEMSignature< DataSig >::Degree > > &dataKey ,
1690 const TreeNode *node ,
1691 typename HyperCube::template Cube< Dim-1 >::template Element< 1 > _e ,
1692 HyperCube::Direction zDir ,
1693 const SliceValues& sValues ,
1694 Vertex& vertex ,
1695 const Data &zeroData
1696 )
1697 {
1698 static const unsigned int DataDegree = FEMSignature< DataSig >::Degree;
1699 Point< Real , Dim > position , gradient;
1700 int c0 , c1;
1701 const typename HyperCube::Cube< Dim-1 >::template Element< 0 > *_c = HyperCubeTables< Dim-1 , 1 , 0 >::OverlapElements[_e.index];
1702 c0 = _c[0].index , c1 = _c[1].index;
1703
1704 const typename LevelSetExtraction::SliceCellIndexData< Dim >::template CellIndices<0> &idx = sValues.cellIndices.template indices<0>( node );
1705 Real x0 = sValues.cornerValues[idx[c0]] , x1 = sValues.cornerValues[idx[c1]];
1706 Point< Real , 3 > dx0 , dx1;
1707 if( gradientNormals ) dx0 = sValues.cornerGradients[idx[c0]] , dx1 = sValues.cornerGradients[idx[c1]];
1708 Point< Real , Dim > s;
1709 Real start , width;
1710 tree._startAndWidth( node , s , width );
1711 int o;
1712 {
1713 const HyperCube::Direction* dirs = HyperCubeTables< Dim-1 , 1 >::Directions[ _e.index ];
1714 for( int d=0 ; d<Dim-1 ; d++ ) if( dirs[d]==HyperCube::CROSS )
1715 {
1716 o = d;
1717 start = s[d];
1718 for( int dd=1 ; dd<Dim-1 ; dd++ ) position[(d+dd)%(Dim-1)] = s[(d+dd)%(Dim-1)] + width * ( dirs[(d+dd)%(Dim-1)]==HyperCube::BACK ? 0 : 1 );
1719 }
1720 }
1721 position[ Dim-1 ] = s[Dim-1] + width * ( zDir==HyperCube::BACK ? 0 : 1 );
1722
1723 double averageRoot;
1724 bool rootFound = false;
1725 if( nonLinearFit )
1726 {
1727 double dx0 = sValues.cornerGradients[idx[c0]][o] * width , dx1 = sValues.cornerGradients[idx[c1]][o] * width;
1728
1729 // The scaling will turn the Hermite Spline into a quadratic
1730 double scl = (x1-x0) / ( (dx1+dx0 ) / 2 );
1731 dx0 *= scl , dx1 *= scl;
1732
1733 // Hermite Spline
1734 Polynomial< 2 > P;
1735 P.coefficients[0] = x0;
1736 P.coefficients[1] = dx0;
1737 P.coefficients[2] = 3*(x1-x0)-dx1-2*dx0;
1738
1739 double roots[2];
1740 int rCount = 0 , rootCount = P.getSolutions( isoValue , roots , 0 );
1741 averageRoot = 0;
1742 for( int i=0 ; i<rootCount ; i++ ) if( roots[i]>=0 && roots[i]<=1 ) averageRoot += roots[i] , rCount++;
1743 if( rCount ) rootFound = true;
1744 averageRoot /= rCount;
1745 }
1746 if( !rootFound )
1747 {
1748 // We have a linear function L, with L(0) = x0 and L(1) = x1
1749 // => L(t) = x0 + t * (x1-x0)
1750 // => L(t) = isoValue <=> t = ( isoValue - x0 ) / ( x1 - x0 )
1751 if( x0==x1 ) MK_THROW( "Not a zero-crossing root: " , x0 , " " , x1 );
1752 averageRoot = ( isoValue - x0 ) / ( x1 - x0 );
1753 }
1754 if( averageRoot<=0 || averageRoot>=1 )
1755 {
1756 _BadRootCount++;
1757 if( averageRoot<0 ) averageRoot = 0;
1758 if( averageRoot>1 ) averageRoot = 1;
1759 }
1760 position[o] = Real( start + width*averageRoot );
1761 gradient = dx0 * (Real)( 1.-averageRoot ) + dx1 * (Real)averageRoot;
1762 Real depth = (Real)1.;
1763 Data dataValue;
1764 if( densityWeights )
1765 {
1766 Real weight;
1767 tree._getSampleDepthAndWeight( *densityWeights , node , position , weightKey , depth , weight );
1768 }
1769 if constexpr( HasData ) if( data )
1770 {
1771 if( DataDegree==0 )
1772 {
1773 Point< Real , 3 > center( s[0] + width/2 , s[1] + width/2 , s[2] + width/2 );
1774 ProjectiveData< Data , Real > pValue( zeroData );
1775 tree.template _addEvaluation< ProjectiveData< Data , Real > , SparseNodeData< ProjectiveData< Data , Real > , IsotropicUIntPack< Dim , DataSig > > , 0 >( *data , center , *pointEvaluator , dataKey , pValue );
1776 dataValue = pValue.weight ? pValue.value() : zeroData;
1777 }
1778 else
1779 {
1780 ProjectiveData< Data , Real > pValue( zeroData );
1781 tree.template _addEvaluation< ProjectiveData< Data , Real > , SparseNodeData< ProjectiveData< Data , Real > , IsotropicUIntPack< Dim , DataSig > > , 0 >( *data , position , *pointEvaluator , dataKey , pValue );
1782 dataValue = pValue.weight ? pValue.value() : zeroData;
1783 }
1784 }
1785 vertex.template get<0>() = position;
1786 vertex.template get<1>() = gradient;
1787 vertex.template get<2>() = depth;
1788 if constexpr( HasData ) vertex.template get<3>() = dataValue;
1789 return true;
1790 }
1791
1792 template< unsigned int WeightDegree , unsigned int DataSig >
1793 static bool GetIsoVertex
1794 (
1795 const FEMTree< Dim , Real > &tree ,
1796 bool nonLinearFit ,
1797 bool gradientNormals ,
1798 typename FEMIntegrator::template PointEvaluator< IsotropicUIntPack< Dim , DataSig > , ZeroUIntPack< Dim > > *pointEvaluator ,
1799 const DensityEstimator< WeightDegree > *densityWeights ,
1800 const SparseNodeData< ProjectiveData< Data , Real > , IsotropicUIntPack< Dim , DataSig > > *data ,
1801 Real isoValue ,
1802 ConstPointSupportKey< IsotropicUIntPack< Dim , WeightDegree > > &weightKey ,
1803 ConstPointSupportKey< IsotropicUIntPack< Dim , FEMSignature< DataSig >::Degree > > &dataKey ,
1804 const TreeNode *node ,
1805 typename HyperCube::template Cube< Dim-1 >::template Element< 0 > _c ,
1806 Real bCoordinate ,
1807 Real fCoordinate ,
1808 const SliceValues &bValues ,
1809 const SliceValues &fValues ,
1810 Vertex &vertex ,
1811 const Data &zeroData
1812 )
1813 {
1814 static const unsigned int DataDegree = FEMSignature< DataSig >::Degree;
1815 Point< Real , Dim > position , gradient;
1816
1817 const typename LevelSetExtraction::SliceCellIndexData< Dim >::template CellIndices<0> &idx0 = bValues.cellIndices.template indices<0>( node );
1818 const typename LevelSetExtraction::SliceCellIndexData< Dim >::template CellIndices<0> &idx1 = fValues.cellIndices.template indices<0>( node );
1819 Real x0 = bValues.cornerValues[ idx0[_c.index] ] , x1 = fValues.cornerValues[ idx1[_c.index] ];
1820 Point< Real , 3 > dx0 , dx1;
1821 if( gradientNormals ) dx0 = bValues.cornerGradients[ idx0[_c.index] ] , dx1 = fValues.cornerGradients[ idx1[_c.index] ];
1822 Point< Real , Dim > s;
1823 Real w;
1824 tree._startAndWidth( node , s , w );
1825 int x , y;
1826 {
1827 const HyperCube::Direction* xx = HyperCubeTables< Dim-1 , 0 >::Directions[ _c.index ];
1828 x = xx[0]==HyperCube::BACK ? 0 : 1 , y = xx[1]==HyperCube::BACK ? 0 : 1;
1829 }
1830
1831 position[0] = s[0] + w*x;
1832 position[1] = s[1] + w*y;
1833
1834 double averageRoot;
1835 bool rootFound = false;
1836
1837 if( nonLinearFit )
1838 {
1839 double dx0 = bValues.cornerGradients[ idx0[_c.index] ][2] * (fCoordinate-bCoordinate) , dx1 = fValues.cornerGradients[ idx1[_c.index] ][2] * (fCoordinate-bCoordinate);
1840 // The scaling will turn the Hermite Spline into a quadratic
1841 double scl = (x1-x0) / ( (dx1+dx0 ) / 2 );
1842 dx0 *= scl , dx1 *= scl;
1843
1844 // Hermite Spline
1845 Polynomial< 2 > P;
1846 P.coefficients[0] = x0;
1847 P.coefficients[1] = dx0;
1848 P.coefficients[2] = 3*(x1-x0)-dx1-2*dx0;
1849
1850 double roots[2];
1851 int rCount = 0 , rootCount = P.getSolutions( isoValue , roots , 0 );
1852 averageRoot = 0;
1853 for( int i=0 ; i<rootCount ; i++ ) if( roots[i]>=0 && roots[i]<=1 ) averageRoot += roots[i] , rCount++;
1854 if( rCount ) rootFound = true;
1855 averageRoot /= rCount;
1856 }
1857 if( !rootFound )
1858 {
1859 // We have a linear function L, with L(0) = x0 and L(1) = x1
1860 // => L(t) = x0 + t * (x1-x0)
1861 // => L(t) = isoValue <=> t = ( isoValue - x0 ) / ( x1 - x0 )
1862 if( x0==x1 ) MK_THROW( "Not a zero-crossing root: " , x0 , " " , x1 );
1863 averageRoot = ( isoValue - x0 ) / ( x1 - x0 );
1864 }
1865 if( averageRoot<=0 || averageRoot>=1 )
1866 {
1867 _BadRootCount++;
1868 if( averageRoot<0 ) averageRoot = 0;
1869 if( averageRoot>1 ) averageRoot = 1;
1870 }
1871 position[2] = Real( bCoordinate + (fCoordinate-bCoordinate)*averageRoot );
1872 gradient = dx0 * (Real)( 1.-averageRoot ) + dx1 * (Real)averageRoot;
1873 Real depth = (Real)1.;
1874 Data dataValue;
1875 if( densityWeights )
1876 {
1877 Real weight;
1878 tree._getSampleDepthAndWeight( *densityWeights , node , position , weightKey , depth , weight );
1879 }
1880 if constexpr( HasData ) if( data )
1881 {
1882 if( DataDegree==0 )
1883 {
1884 Point< Real , 3 > center( s[0] + w/2 , s[1] + w/2 , (bCoordinate+fCoordinate)/2 );
1885 ProjectiveData< Data , Real > pValue( zeroData );
1886 tree.template _addEvaluation< ProjectiveData< Data , Real > , SparseNodeData< ProjectiveData< Data , Real > , IsotropicUIntPack< Dim , DataSig > > , 0 >( *data , center , *pointEvaluator , dataKey , pValue );
1887 dataValue = pValue.weight ? pValue.value() : zeroData;
1888 }
1889 else
1890 {
1891 ProjectiveData< Data , Real > pValue( zeroData );
1892 tree.template _addEvaluation< ProjectiveData< Data , Real > , SparseNodeData< ProjectiveData< Data , Real > , IsotropicUIntPack< Dim , DataSig > > , 0 >( *data , position , *pointEvaluator , dataKey , pValue );
1893 dataValue = pValue.weight ? pValue.value() : zeroData;
1894 }
1895 }
1896 vertex.template get<0>() = position;
1897 vertex.template get<1>() = gradient;
1898 vertex.template get<2>() = depth;
1899 if constexpr( HasData ) vertex.template get<3>() = dataValue;
1900 return true;
1901 }
1902
1903 template< typename VertexStream >
1904 static unsigned int AddIsoPolygons( unsigned int thread , VertexStream &vertexStream , OutputDataStream< std::vector< node_index_type > > &polygonStream , std::vector< std::pair< node_index_type , Vertex > >& polygon , bool polygonMesh , bool addBarycenter )
1905 {
1906 if( polygonMesh )
1907 {
1908 std::vector< node_index_type > vertices( polygon.size() );
1909 for( unsigned int i=0 ; i<polygon.size() ; i++ ) vertices[i] = polygon[polygon.size()-1-i].first;
1910 polygonStream.write( thread , vertices );
1911 return 1;
1912 }
1913 if( polygon.size()>3 )
1914 {
1915 bool isCoplanar = false;
1916 std::vector< node_index_type > triangle( 3 );
1917
1918 if( addBarycenter )
1919 for( unsigned int i=0 ; i<polygon.size() ; i++ ) for( unsigned int j=0 ; j<i ; j++ )
1920 if( (i+1)%polygon.size()!=j && (j+1)%polygon.size()!=i )
1921 {
1922 Vertex v1 = polygon[i].second , v2 = polygon[j].second;
1923 for( int k=0 ; k<3 ; k++ ) if( v1.template get<0>()[k]==v2.template get<0>()[k] ) isCoplanar = true;
1924 }
1925 if( isCoplanar )
1926 {
1927 Vertex c;
1928 c *= 0;
1929 for( unsigned int i=0 ; i<polygon.size() ; i++ ) c += polygon[i].second;
1930 c /= ( typename Vertex::Real )polygon.size();
1931
1932 node_index_type cIdx = (node_index_type)vertexStream.write( thread , c );
1933
1934 for( unsigned i=0 ; i<polygon.size() ; i++ )
1935 {
1936 triangle[0] = polygon[ i ].first;
1937 triangle[1] = cIdx;
1938 triangle[2] = polygon[(i+1)%polygon.size()].first;
1939 polygonStream.write( thread , triangle );
1940 }
1941 return (unsigned int)polygon.size();
1942 }
1943 else
1944 {
1945 std::vector< Point< Real , Dim > > vertices( polygon.size() );
1946 for( unsigned int i=0 ; i<polygon.size() ; i++ ) vertices[i] = polygon[i].second.template get<0>();
1947 std::vector< TriangleIndex< node_index_type > > triangles = MinimalAreaTriangulation< node_index_type , Real , Dim >( ( ConstPointer( Point< Real , Dim > ) )GetPointer( vertices ) , (node_index_type)vertices.size() );
1948 if( triangles.size()!=polygon.size()-2 ) MK_THROW( "Minimal area triangulation failed:" , triangles.size() , " != " , polygon.size()-2 );
1949 for( unsigned int i=0 ; i<triangles.size() ; i++ )
1950 {
1951 for( int j=0 ; j<3 ; j++ ) triangle[2-j] = polygon[ triangles[i].idx[j] ].first;
1952 polygonStream.write( thread , triangle );
1953 }
1954 }
1955 }
1956 else if( polygon.size()==3 )
1957 {
1958 std::vector< node_index_type > vertices( 3 );
1959 for( int i=0 ; i<3 ; i++ ) vertices[2-i] = polygon[i].first;
1960 polygonStream.write( thread , vertices );
1961 }
1962 return (unsigned int)polygon.size()-2;
1963 }
1964
1965 struct Stats
1966 {
1967 double cornersTime , verticesTime , edgesTime , surfaceTime;
1968 double setTableTime;
1969 Stats( void ) : cornersTime(0) , verticesTime(0) , edgesTime(0) , surfaceTime(0) , setTableTime(0) {;}
1970 std::string toString( void ) const
1971 {
1972 std::stringstream stream;
1973 stream << "Corners / Vertices / Edges / Surface / Set Table: ";
1974 stream << std::fixed << std::setprecision(1) << cornersTime << " / " << verticesTime << " / " << edgesTime << " / " << surfaceTime << " / " << setTableTime;
1975 stream << " (s)";
1976 return stream.str();
1977 }
1978 };
1979
1980
1981 template< unsigned int WeightDegree , unsigned int DataSig , typename OutputVertexStream , unsigned int ... FEMSigs >
1982 static Stats Extract
1983 (
1984 UIntPack< FEMSigs ... > ,
1985 UIntPack< WeightDegree > ,
1986 UIntPack< DataSig > ,
1987 const FEMTree< Dim , Real > &tree ,
1988 int maxKeyDepth ,
1989 const DensityEstimator< WeightDegree > *densityWeights ,
1990 const SparseNodeData< ProjectiveData< Data , Real > , IsotropicUIntPack< Dim , DataSig > > *data ,
1991 const DenseNodeData< Real , UIntPack< FEMSigs ... > > &coefficients ,
1992 Real isoValue ,
1993 unsigned int slabDepth ,
1994 unsigned int slabStart ,
1995 unsigned int slabEnd ,
1996 OutputVertexStream &vertexStream ,
1997 OutputDataStream< std::vector< node_index_type > > &polygonStream ,
1998 const Data &zeroData ,
1999 bool nonLinearFit ,
2000 bool gradientNormals ,
2001 bool addBarycenter ,
2002 bool polygonMesh ,
2003 bool flipOrientation ,
2004 const typename LevelSetExtractor< Real , Dim-1 , Point< Real , Dim-1 > >::TreeSliceValuesAndVertexPositions * backBoundary ,
2005 const typename LevelSetExtractor< Real , Dim-1 , Point< Real , Dim-1 > >::TreeSliceValuesAndVertexPositions *frontBoundary ,
2006 const std::vector< std::vector< Real > > &backDValues ,
2007 const std::vector< std::vector< Real > > &frontDValues ,
2008 bool copyTopology
2009 )
2010 {
2011 if( maxKeyDepth<tree._maxDepth ) MK_THROW( "Max key depth has to be at least tree depth: " , tree._maxDepth , " <= " , maxKeyDepth );
2012 if( slabStart>=slabEnd ) MK_THROW( "Slab start cannot excceed slab end: " , slabStart , " < " , slabEnd );
2013 if( slabEnd>(1u<<slabDepth) ) MK_THROW( "Slab end cannot exceed slab num: " , slabEnd , " <= " , 1<<slabDepth );
2014
2015 LevelSetExtraction::KeyGenerator< Dim > keyGenerator( maxKeyDepth );
2016 LocalOffset start , end;
2017 for( unsigned int d=0 ; d<Dim-1 ; d++ ) start[d] = 0 , end[d] = (1<<slabDepth);
2018 start[Dim-1] = slabStart , end[Dim-1] = slabEnd;
2019 LocalDepth fullDepth = tree.getFullDepth( UIntPack< FEMSignature< FEMSigs >::Degree ... >() , slabDepth , start , end );
2020 unsigned int maxDepth = std::max< unsigned int >( tree._maxDepth , slabDepth );
2021 unsigned int slabStartAtMaxDepth = slabStart << ( maxDepth - slabDepth );
2022 unsigned int slabEndAtMaxDepth = slabEnd << ( maxDepth - slabDepth );
2023 #ifdef SHOW_WARNINGS
2024 if( slabDepth>(unsigned int)fullDepth && ( ( slabStart!=0 && !backBoundary ) || ( slabEnd+1!=1<<(slabDepth) && !frontBoundary ) ) ) MK_WARN( "Slab depth exceeds full depth, reconstruction may not be water-tight: " , slabDepth , " <= " , fullDepth , " [ " , slabStart , " , " , slabEnd , " )" );
2025 #endif // SHOW_WARNINGS
2026
2027 _BadRootCount = 0u;
2028 Stats stats;
2029 static_assert( sizeof...(FEMSigs)==Dim , "[ERROR] Number of signatures should match dimension" );
2030 tree._setFEM1ValidityFlags( UIntPack< FEMSigs ... >() );
2031 static const unsigned int DataDegree = FEMSignature< DataSig >::Degree;
2032 static const int FEMDegrees[] = { FEMSignature< FEMSigs >::Degree ... };
2033 for( int d=0 ; d<Dim ; d++ ) if( FEMDegrees[d]==0 && ( nonLinearFit || gradientNormals ) ) MK_THROW( "Constant B-Splines do not support gradient estimation" );
2034
2035 LevelSetExtraction::SetHyperCubeTables< Dim >();
2036 LevelSetExtraction::SetHyperCubeTables< Dim-1 >();
2037
2038 typename FEMIntegrator::template PointEvaluator< IsotropicUIntPack< Dim , DataSig > , ZeroUIntPack< Dim > >* pointEvaluator = NULL;
2039 if constexpr( HasData ) if( data ) pointEvaluator = new typename FEMIntegrator::template PointEvaluator< IsotropicUIntPack< Dim , DataSig > , ZeroUIntPack< Dim > >( tree._maxDepth );
2040 DenseNodeData< Real , UIntPack< FEMSigs ... > > coarseCoefficients( tree._sNodesEnd( tree._maxDepth-1 ) );
2041 memset( coarseCoefficients() , 0 , sizeof(Real)*tree._sNodesEnd( tree._maxDepth-1 ) );
2042 ThreadPool::ParallelFor( tree._sNodesBegin(0) , tree._sNodesEnd( tree._maxDepth-1 ) , [&]( unsigned int, size_t i ){ coarseCoefficients[i] = coefficients[i]; } );
2043 typename FEMIntegrator::template RestrictionProlongation< UIntPack< FEMSigs ... > > rp;
2044 for( LocalDepth d=1 ; d<tree._maxDepth ; d++ ) tree._upSample( UIntPack< FEMSigs ... >() , rp , d , ( ConstPointer(Real) )coarseCoefficients()+tree._sNodesBegin(d-1) , coarseCoefficients()+tree._sNodesBegin(d) );
2045
2046 std::vector< _Evaluator< UIntPack< FEMSigs ... > , 1 > > evaluators( tree._maxDepth+1 );
2047 for( LocalDepth d=0 ; d<=tree._maxDepth ; d++ ) evaluators[d].set( tree._maxDepth );
2048
2049 std::vector< SlabValues > slabValues( tree._maxDepth+1 );
2050 std::vector< std::pair< node_index_type , node_index_type > > backIncidence , frontIncidence;
2051 if( backBoundary ) backIncidence = SetIncidence( tree , backBoundary->sliceTree , fullDepth , slabStartAtMaxDepth , maxDepth );
2052 if( frontBoundary ) frontIncidence = SetIncidence( tree , frontBoundary->sliceTree , fullDepth , slabEndAtMaxDepth , maxDepth );
2053
2054 enum BoundarySliceType { BACK , FRONT , NEITHER };
2055
2056 auto BoundarySlice = [&]( unsigned int sliceAtMaxDepth )
2057 {
2058 if ( backBoundary && sliceAtMaxDepth==slabStartAtMaxDepth ) return BoundarySliceType::BACK;
2059 else if( frontBoundary && sliceAtMaxDepth== slabEndAtMaxDepth ) return BoundarySliceType::FRONT;
2060 else return BoundarySliceType::NEITHER;
2061 };
2062
2063
2064 auto SetCoarseSlice = [&]( unsigned int sliceAtMaxDepth , unsigned int coarseDepth , unsigned int &coarseSlice )
2065 {
2066 BoundarySliceType bst = BoundarySlice( sliceAtMaxDepth );
2067 unsigned dOff = maxDepth - coarseDepth;
2068 coarseSlice = sliceAtMaxDepth>>dOff;
2069 if( sliceAtMaxDepth!=( coarseSlice<<dOff ) )
2070 {
2071 if ( bst==BoundarySliceType::BACK ) ;
2072 else if( bst==BoundarySliceType::FRONT ) coarseSlice++;
2073 else return false;
2074 }
2075 return true;
2076 };
2077
2078 auto InteriorSlab = [&]( unsigned int d , unsigned int o )
2079 {
2080 unsigned int startAtMaxDepth = (o+0)<<(maxDepth-d);
2081 unsigned int endAtMaxDepth = (o+1)<<(maxDepth-d);
2082 if( endAtMaxDepth<=slabStartAtMaxDepth ) return false;
2083 if( startAtMaxDepth>= slabEndAtMaxDepth ) return false;
2084 if( startAtMaxDepth<slabStartAtMaxDepth && ! backBoundary ) return false;
2085 if( endAtMaxDepth> slabEndAtMaxDepth && !frontBoundary ) return false;
2086 return true;
2087 };
2088
2089 auto SetSlabBounds = [&]( unsigned int d , unsigned int o , Real &bCoordinate , Real &fCoordinate )
2090 {
2091 unsigned int start = std::max< unsigned int >( (o+0)<<(maxDepth-d) , slabStartAtMaxDepth );
2092 unsigned int end = std::min< unsigned int >( (o+1)<<(maxDepth-d) , slabEndAtMaxDepth );
2093 bCoordinate = start/(Real)(1<<maxDepth);
2094 fCoordinate = end/(Real)(1<<maxDepth);
2095 };
2096
2097 // Initializes the indexing tables
2098 auto InitSlice = [&]( unsigned int sliceAtMaxDepth )
2099 {
2100
2101 BoundarySliceType bst = BoundarySlice( sliceAtMaxDepth );
2102
2103 for( LocalDepth d=maxDepth ; d>=fullDepth ; d-- )
2104 {
2105 unsigned int slice;
2106 if( !SetCoarseSlice( sliceAtMaxDepth , d , slice ) ) break;
2107 if( d<=tree._maxDepth )
2108 {
2109 double t = Time();
2110 slabValues[d].sliceValues(slice).cellIndices.set( tree._sNodes , tree._localToGlobal( d ) , slice + tree._localInset( d ) );
2111 stats.setTableTime += Time()-t;
2112
2113 slabValues[d].sliceValues(slice).reset( slice , nonLinearFit || gradientNormals );
2114
2115 slabValues[d].sliceScratch(slice).reset( slabValues[d].sliceValues(slice).cellIndices );
2116 }
2117 }
2118 };
2119
2120 auto InitSlab = [&]( unsigned int slabAtMaxDepth , bool first )
2121 {
2122 unsigned int slab = slabAtMaxDepth;
2123 for( LocalDepth d=maxDepth ; d>=fullDepth ; d-- , slab>>=1 )
2124 {
2125 if( d<=tree._maxDepth )
2126 {
2127 double t = Time();
2128 slabValues[d].xSliceValues(slab).cellIndices.set( tree._sNodes , tree._localToGlobal( d ) , slab + tree._localInset( d ) );
2129 stats.setTableTime += Time()-t;
2130
2131 slabValues[d].xSliceValues(slab).reset(slab);
2132
2133 slabValues[d].xSliceScratch(slab).reset( slabValues[d].xSliceValues(slab).cellIndices );
2134 }
2135 if( (slab&1) && !first ) break;
2136 }
2137 };
2138
2139 auto FinalizeSlice = [&]( unsigned int sliceAtMaxDepth )
2140 {
2141
2142 bool useBoundary = ( ( sliceAtMaxDepth==slabStartAtMaxDepth && backBoundary ) || ( sliceAtMaxDepth==slabEndAtMaxDepth && frontBoundary ) ) && copyTopology;
2143 if( !useBoundary )
2144 {
2145 LocalDepth d ; unsigned int o;
2146 for( d=maxDepth , o=sliceAtMaxDepth ; d>=fullDepth ; d-- , o>>=1 )
2147 {
2148 if( d<=tree._maxDepth )
2149 {
2150 ThreadPool::ParallelSections
2151 (
2152 [ &slabValues , d , o ]( void ){ slabValues[d].sliceValues(o).setFromScratch( slabValues[d].sliceScratch(o).vKeyValues ); } ,
2153 [ &slabValues , d , o ]( void ){ slabValues[d].sliceValues(o).setFromScratch( slabValues[d].sliceScratch(o).eKeyValues ); } ,
2154 [ &slabValues , d , o ]( void ){ slabValues[d].sliceValues(o).setFromScratch( slabValues[d].sliceScratch(o).fKeyValues ); }
2155 );
2156 }
2157 if( o&1 ) break;
2158 }
2159 }
2160 };
2161
2162 auto FinalizeSlab = [&]( unsigned int slabAtMaxDepth )
2163 {
2164 LocalDepth d ; unsigned int o;
2165 bool boundary = (slabAtMaxDepth+1)==slabEndAtMaxDepth && frontBoundary;
2166 for( d=maxDepth , o=slabAtMaxDepth ; d>=fullDepth ; d-- , o>>=1 )
2167 {
2168 if( d<=tree._maxDepth )
2169 {
2170 ThreadPool::ParallelSections
2171 (
2172 [ &slabValues , d , o ]( void ){ slabValues[d].xSliceValues(o).setFromScratch( slabValues[d].xSliceScratch(o).vKeyValues ); } ,
2173 [ &slabValues , d , o ]( void ){ slabValues[d].xSliceValues(o).setFromScratch( slabValues[d].xSliceScratch(o).eKeyValues ); } ,
2174 [ &slabValues , d , o ]( void ){ slabValues[d].xSliceValues(o).setFromScratch( slabValues[d].xSliceScratch(o).fKeyValues ); }
2175 );
2176 }
2177 if( !(o&1) && !boundary ) break;
2178 }
2179 };
2180
2181 auto SetSliceValues = [&]( unsigned int sliceAtMaxDepth )
2182 {
2183
2184 LocalDepth d ; unsigned int o;
2185 const typename LevelSetExtractor< Real , Dim-1 , Point< Real , Dim-1 > >::TreeSliceValuesAndVertexPositions *boundary = NULL;
2186 const std::vector< std::pair< node_index_type , node_index_type > > *incidence = NULL;
2187 const std::vector< std::vector< Real > > *dValues = NULL;
2188 if ( sliceAtMaxDepth==slabStartAtMaxDepth ) boundary = backBoundary , incidence = &backIncidence , dValues = & backDValues;
2189 else if( sliceAtMaxDepth== slabEndAtMaxDepth ) boundary = frontBoundary , incidence = &frontIncidence , dValues = &frontDValues;
2190
2191 if( boundary && copyTopology )
2192 {
2193 double t = Time();
2194 for( LocalDepth d=maxDepth ; d>=fullDepth ; d-- ) if( d<=boundary->sliceTree.depth() && d<=tree._maxDepth )
2195 {
2196 unsigned int slice;
2197 if( !SetCoarseSlice( sliceAtMaxDepth , d , slice ) ) MK_THROW( "Could not set coarse slice" );
2198 OverwriteCornerValues( *boundary , (*dValues)[d] , tree , d , sliceAtMaxDepth , maxDepth , sliceAtMaxDepth==slabStartAtMaxDepth , slabValues , *incidence );
2199 SetMCIndices( tree , isoValue , d , fullDepth , slice , slabValues );
2200 }
2201 stats.cornersTime += Time()-t , t = Time();
2202 }
2203 else
2204 {
2205 double t;
2206
2207 t = Time();
2208 for( d=maxDepth , o=sliceAtMaxDepth ; d>=fullDepth ; d-- , o>>=1 )
2209 {
2210 if( d<=tree._maxDepth )
2211 {
2212 if( boundary )
2213 {
2214 if( d<=boundary->sliceTree.depth() )
2215 {
2216 OverwriteCornerValues( *boundary , (*dValues)[d] , tree , d , sliceAtMaxDepth , maxDepth , sliceAtMaxDepth==slabStartAtMaxDepth , slabValues , backIncidence );
2217 SetMCIndices( tree , isoValue , d , fullDepth , o , slabValues );
2218 }
2219 }
2220 else SetSliceCornerValuesAndMCIndices< FEMSigs ... >( tree , coefficients() , coarseCoefficients() , isoValue , d , fullDepth , o , slabValues , evaluators[d] );
2221 }
2222 if( o&1 ) break;
2223 }
2224 stats.cornersTime += Time()-t;
2225 }
2226 };
2227
2228 auto SetSliceIso = [&]( unsigned int sliceAtMaxDepth )
2229 {
2230 LocalDepth d ; unsigned int o;
2231
2232 const typename LevelSetExtractor< Real , Dim-1 , Point< Real , Dim-1 > >::TreeSliceValuesAndVertexPositions *boundary = NULL;
2233 const std::vector< std::pair< node_index_type , node_index_type > > *incidence = NULL;
2234 const std::vector< std::vector< Real > > *dValues = NULL;
2235 if ( sliceAtMaxDepth==slabStartAtMaxDepth ) boundary = backBoundary , incidence = &backIncidence , dValues = & backDValues;
2236 else if( sliceAtMaxDepth== slabEndAtMaxDepth ) boundary = frontBoundary , incidence = &frontIncidence , dValues = &frontDValues;
2237
2238 if( boundary && copyTopology )
2239 {
2240 auto sliceFunctor = [&]( unsigned int depth ) -> SliceValues &
2241 {
2242 unsigned int slice;
2243 if( !SetCoarseSlice( sliceAtMaxDepth , depth , slice ) ) MK_THROW( "Could not set coarse slice" );
2244 return slabValues[depth].sliceValues( slice );
2245 };
2246 auto scratchFunctor = [&]( unsigned int depth ) -> typename SliceValues::Scratch &
2247 {
2248 unsigned int slice;
2249 if( !SetCoarseSlice( sliceAtMaxDepth , depth , slice ) ) MK_THROW( "Could not set coarse slice" );
2250 return slabValues[depth].sliceScratch( slice );
2251 };
2252 CopyIsoStructure< WeightDegree , DataSig >( keyGenerator , *boundary , tree , fullDepth , sliceAtMaxDepth , maxDepth , sliceFunctor , scratchFunctor , *incidence , vertexStream , gradientNormals , pointEvaluator , densityWeights , data , zeroData );
2253 }
2254 else
2255 {
2256 double t;
2257 // Set the iso-vertices
2258 t = Time();
2259 for( d=maxDepth , o=sliceAtMaxDepth ; d>=fullDepth ; d-- , o>>=1 )
2260 {
2261 if( d<=tree._maxDepth )
2262 {
2263 SetSliceIsoVertices< WeightDegree , DataSig >( keyGenerator , tree , nonLinearFit , gradientNormals , pointEvaluator , densityWeights , data , isoValue , d , fullDepth , o , vertexStream , slabValues , zeroData );
2264 }
2265 if( o&1 ) break;
2266 }
2267 stats.verticesTime += Time()-t;
2268
2269 // Set the iso-edges
2270 t = Time();
2271 for( d=maxDepth , o=sliceAtMaxDepth ; d>=fullDepth ; d-- , o>>=1 )
2272 {
2273 if( d<=tree._maxDepth )
2274 {
2275 if( d<tree._maxDepth ) CopyFinerSliceIsoEdgeKeys( tree , d , fullDepth , o , slabValues );
2276 SetSliceIsoEdges( keyGenerator , tree , d , o , slabValues );
2277 }
2278 if( o&1 ) break;
2279 }
2280 stats.edgesTime += Time()-t;
2281 }
2282 };
2283
2284
2285 auto SetSlabIsoVertices = [&]( unsigned int slabAtMaxDepth )
2286 {
2287 LocalDepth d ; unsigned int o;
2288 double t = Time();
2289
2290 bool boundary = (slabAtMaxDepth+1)==slabEndAtMaxDepth && frontBoundary;
2291
2292 // Set the iso-edges
2293 for( d=maxDepth , o=slabAtMaxDepth ; d>=fullDepth ; d-- , o>>=1 )
2294 {
2295 if( d<=tree._maxDepth )
2296 {
2297 if( InteriorSlab( d , o ) )
2298 {
2299 // Set the iso-vertices
2300 Real bCoordinate , fCoordinate;
2301 SetSlabBounds( d , o , bCoordinate , fCoordinate );
2302 SetXSliceIsoVertices< WeightDegree , DataSig >( keyGenerator , tree , nonLinearFit , gradientNormals , pointEvaluator , densityWeights , data , isoValue , d , std::max< unsigned int >( slabDepth , fullDepth ) , o , bCoordinate , fCoordinate , vertexStream , slabValues , zeroData );
2303 }
2304 }
2305
2306 if( !(o&1) && !boundary ) break;
2307 }
2308 stats.edgesTime += Time()-t;
2309 };
2310
2311 auto SetSlabIsoEdges = [&]( unsigned int slabAtMaxDepth )
2312 {
2313 LocalDepth d ; unsigned int o;
2314 double t = Time();
2315
2316 bool boundary = (slabAtMaxDepth+1)==slabEndAtMaxDepth && frontBoundary;
2317
2318 // Set the iso-edges
2319 for( d=maxDepth , o=slabAtMaxDepth ; d>=fullDepth ; d-- , o>>=1 )
2320 {
2321 if( d<=tree._maxDepth )
2322 {
2323 if( InteriorSlab( d , o ) )
2324 {
2325 // Copy the edges from finer
2326 if( d<tree._maxDepth )
2327 {
2328 CopyFinerXSliceIsoEdgeKeys( tree , d , fullDepth , o , slabValues );
2329 }
2330 SetXSliceIsoEdges( keyGenerator , tree , d , o , slabValues );
2331 }
2332
2333 }
2334 if( !(o&1) && !boundary ) break;
2335 }
2336 stats.edgesTime += Time()-t;
2337 };
2338
2339
2340 auto IsoSurface = [&]( unsigned int slabAtMaxDepth )
2341 {
2342 double t = Time();
2343 bool boundary = (slabAtMaxDepth+1)==slabEndAtMaxDepth && frontBoundary;
2344 LocalDepth d ; int o;
2345 for( d=maxDepth , o=slabAtMaxDepth ; d>=fullDepth ; d-- , o>>=1 )
2346 {
2347 if( d<=tree._maxDepth )
2348 {
2349 auto faceIndexFunctor = [&]( const TreeNode *node , typename HyperCube::Cube< Dim >::template Element< 2 > f )
2350 {
2351 HyperCube::Direction dir ; unsigned int coIndex;
2352 f.factor( dir , coIndex );
2353 int depth , offset[Dim];
2354 tree.depthAndOffset( node , depth , offset );
2355 LevelSetExtraction::Key< Dim > key = keyGenerator( depth , offset , f );
2356 if ( dir==HyperCube::BACK && ( (((unsigned int)offset[Dim-1]+0)<<(maxDepth-depth))<slabStartAtMaxDepth ) ) key[Dim-1] = keyGenerator.cornerIndex( maxDepth , slabStartAtMaxDepth );
2357 else if( dir==HyperCube::FRONT && ( (((unsigned int)offset[Dim-1]+1)<<(maxDepth-depth))> slabEndAtMaxDepth ) ) key[Dim-1] = keyGenerator.cornerIndex( maxDepth , slabEndAtMaxDepth );
2358 return key;
2359 };
2360 if( InteriorSlab( d , o ) ) SetLevelSet( keyGenerator , faceIndexFunctor , tree , d , o , slabValues[d].sliceValues(o) , slabValues[d].sliceValues(o+1) , slabValues[d].xSliceValues(o) , slabValues[d].sliceScratch(o) , slabValues[d].sliceScratch(o+1) , slabValues[d].xSliceScratch(o) , vertexStream , polygonStream , polygonMesh , addBarycenter , flipOrientation );
2361 }
2362 if( !(o&1) && !boundary ) break;
2363 }
2364 stats.surfaceTime += Time()-t;
2365 };
2366
2367 InitSlice( slabStartAtMaxDepth );
2368 InitSlab( slabStartAtMaxDepth , true ); // This needs to be done in case the slice wants to push iso-vertices down to the slab
2369 SetSliceValues( slabStartAtMaxDepth );
2370 SetSliceIso( slabStartAtMaxDepth );
2371 FinalizeSlice( slabStartAtMaxDepth );
2372
2373 // Iterate over the slabs at the finest level
2374 for( unsigned int slab=slabStartAtMaxDepth ; slab<slabEndAtMaxDepth ; slab++)
2375 {
2376 // Need to init the slab before setting the slice (in case things need to be pushed down from the slice to the slab)
2377 InitSlice( slab+1 );
2378 if( slab!=slabStartAtMaxDepth ) InitSlab( slab , false );
2379
2380 // Need to set the front slice corner values before computing iso-vertices
2381 SetSliceValues( slab+1 );
2382
2383 // Compute the iso-vertices first, so that slice's iso-vertices are set last
2384 SetSlabIsoVertices( slab );
2385 SetSliceIso( slab+1 );
2386
2387 // Now compute the iso-edges
2388 SetSlabIsoEdges( slab );
2389
2390 FinalizeSlice( slab+1 );
2391 FinalizeSlab( slab );
2392
2393 IsoSurface( slab );
2394 }
2395
2396 if( pointEvaluator ) delete pointEvaluator;
2397 size_t badRootCount = _BadRootCount;
2398 if( badRootCount!=0 ) MK_WARN( "bad average roots: " , badRootCount );
2399 return stats;
2400 }
2401 };
2402
2403 template< bool HasData , typename Real , typename Data > std::atomic< size_t > _LevelSetExtractor< HasData , Real , 3 , Data >::_BadRootCount;
2404
2405 template< typename Real >
2406 struct LevelSetExtractor< Real , 3 >
2407 {
2408 static const unsigned int Dim = 3;
2409 static const bool HasData = false;
2410 typedef unsigned char Data;
2411 typedef typename _LevelSetExtractor< HasData , Real , Dim , Data >::Stats Stats;
2412 typedef typename _LevelSetExtractor< HasData , Real , Dim , Data >::Vertex Vertex;
2413 typedef typename _LevelSetExtractor< HasData , Real , Dim , Data >::TreeNode TreeNode;
2414 using OutputVertexStream = typename _LevelSetExtractor< HasData , Real , Dim , Data >::OutputVertexStream;
2415 template< unsigned int WeightDegree > using DensityEstimator = typename _LevelSetExtractor< HasData , Real , Dim , Data >::template DensityEstimator< WeightDegree >;
2416
2417 template< unsigned int WeightDegree , unsigned int ... FEMSigs >
2418 static Stats Extract( UIntPack< FEMSigs ... > , UIntPack< WeightDegree > , const FEMTree< Dim , Real >& tree , const DensityEstimator< WeightDegree > *densityWeights , const DenseNodeData< Real , UIntPack< FEMSigs ... > > &coefficients , Real isoValue , OutputVertexStream &vertexStream , OutputDataStream< std::vector< node_index_type > > &polygonStream , bool nonLinearFit , bool outputGradients , bool addBarycenter , bool polygonMesh , bool flipOrientation )
2419 {
2420 return Extract( UIntPack< FEMSigs ... >() , UIntPack< WeightDegree >() , tree , densityWeights , coefficients , isoValue , 0 , 0 , 1 , vertexStream , polygonStream , nonLinearFit , outputGradients , addBarycenter , polygonMesh , flipOrientation );
2421 }
2422
2423 template< unsigned int WeightDegree , unsigned int ... FEMSigs >
2424 static Stats Extract( UIntPack< FEMSigs ... > , UIntPack< WeightDegree > , const FEMTree< Dim , Real >& tree , const DensityEstimator< WeightDegree >* densityWeights , const DenseNodeData< Real , UIntPack< FEMSigs ... > >& coefficients , Real isoValue , unsigned int slabDepth , unsigned int slabStart , unsigned int slabEnd , OutputVertexStream &vertexStream , OutputDataStream< std::vector< node_index_type > > &polygonStream , bool nonLinearFit , bool outputGradients , bool addBarycenter , bool polygonMesh , bool flipOrientation )
2425 {
2426 std::vector< std::vector< Real > > dValues;
2427 return Extract( UIntPack< FEMSigs ... >() , UIntPack< WeightDegree >() , tree , tree._maxDepth , densityWeights , coefficients , isoValue , slabDepth , slabStart , slabEnd , vertexStream , polygonStream , nonLinearFit , outputGradients , addBarycenter , polygonMesh , flipOrientation , NULL , NULL , dValues , dValues , false );
2428 }
2429
2430 template< unsigned int WeightDegree , unsigned int ... FEMSigs >
2431 static Stats Extract( UIntPack< FEMSigs ... > , UIntPack< WeightDegree > , const FEMTree< Dim , Real >& tree , int maxKeyDepth , const DensityEstimator< WeightDegree >* densityWeights , const DenseNodeData< Real , UIntPack< FEMSigs ... > >& coefficients , Real isoValue , unsigned int slabDepth , unsigned int slabStart , unsigned int slabEnd , OutputVertexStream &vertexStream , OutputDataStream< std::vector< node_index_type > > &polygonStream , bool nonLinearFit , bool outputGradients , bool addBarycenter , bool polygonMesh , bool flipOrientation , const typename LevelSetExtractor< Real , Dim-1 , Point< Real , Dim-1 > >::TreeSliceValuesAndVertexPositions *backBoundary , const typename LevelSetExtractor< Real , Dim-1 , Point< Real , Dim-1 > >::TreeSliceValuesAndVertexPositions *frontBoundary , const std::vector< std::vector< Real > > &backDValues , const std::vector< std::vector< Real > > &frontDValues , bool copyTopology )
2432 {
2433 static const unsigned int DataSig = FEMDegreeAndBType< 0 , BOUNDARY_FREE >::Signature;
2434 const SparseNodeData< ProjectiveData< Data , Real > , IsotropicUIntPack< Dim , DataSig > > *data = NULL;
2435 Data zeroData = 0;
2436 return _LevelSetExtractor< HasData , Real , Dim , Data >::template Extract< WeightDegree , DataSig , OutputVertexStream , FEMSigs ... >( UIntPack< FEMSigs ... >() , UIntPack< WeightDegree >() , UIntPack< DataSig >() , tree , maxKeyDepth , densityWeights , data , coefficients , isoValue , slabDepth , slabStart , slabEnd , vertexStream , polygonStream , zeroData , nonLinearFit , outputGradients , addBarycenter , polygonMesh , flipOrientation , backBoundary , frontBoundary , backDValues , frontDValues , copyTopology );
2437 }
2438 };
2439
2440 template< typename Real , typename Data >
2441 struct LevelSetExtractor< Real , 3 , Data >
2442 {
2443 static const unsigned int Dim = 3;
2444 static const bool HasData = true;
2445 typedef typename _LevelSetExtractor< HasData , Real , Dim , Data >::Stats Stats;
2446 typedef typename _LevelSetExtractor< HasData , Real , Dim , Data >::Vertex Vertex;
2447 typedef typename _LevelSetExtractor< HasData , Real , Dim , Data >::TreeNode TreeNode;
2448 using OutputVertexStream = typename _LevelSetExtractor< HasData , Real , Dim , Data >::OutputVertexStream;
2449 template< unsigned int WeightDegree > using DensityEstimator = typename _LevelSetExtractor< HasData , Real , Dim , Data >::template DensityEstimator< WeightDegree >;
2450
2451 template< unsigned int WeightDegree , unsigned int DataSig , unsigned int ... FEMSigs >
2452 static Stats Extract( UIntPack< FEMSigs ... > , UIntPack< WeightDegree > , UIntPack< DataSig > , const FEMTree< Dim , Real >& tree , const DensityEstimator< WeightDegree > *densityWeights , const SparseNodeData< ProjectiveData< Data , Real > , IsotropicUIntPack< Dim , DataSig > > *data , const DenseNodeData< Real , UIntPack< FEMSigs ... > > &coefficients , Real isoValue , OutputVertexStream &vertexStream , OutputDataStream< std::vector< node_index_type > > &polygonStream , const Data &zeroData , bool nonLinearFit , bool outputGradients , bool addBarycenter , bool polygonMesh , bool flipOrientation )
2453 {
2454 return Extract( UIntPack< FEMSigs ... >() , UIntPack< WeightDegree >() , UIntPack< DataSig >() , tree , densityWeights , data , coefficients , isoValue , 0 , 0 , 1 , vertexStream , polygonStream , zeroData , nonLinearFit , outputGradients , addBarycenter , polygonMesh , flipOrientation );
2455 }
2456
2457 template< unsigned int WeightDegree , unsigned int DataSig , unsigned int ... FEMSigs >
2458 static Stats Extract( UIntPack< FEMSigs ... > , UIntPack< WeightDegree > , UIntPack< DataSig > , const FEMTree< Dim , Real >& tree , const DensityEstimator< WeightDegree >* densityWeights , const SparseNodeData< ProjectiveData< Data , Real > , IsotropicUIntPack< Dim , DataSig > >* data , const DenseNodeData< Real , UIntPack< FEMSigs ... > >& coefficients , Real isoValue , unsigned int slabDepth , unsigned int slabStart , unsigned int slabEnd , OutputVertexStream &vertexStream , OutputDataStream< std::vector< node_index_type > > &polygonStream , const Data &zeroData , bool nonLinearFit , bool outputGradients , bool addBarycenter , bool polygonMesh , bool flipOrientation )
2459 {
2460 std::vector< std::vector< Real > > dValues;
2461 return Extract( UIntPack< FEMSigs ... >() , UIntPack< WeightDegree >() , UIntPack< DataSig >() , tree , tree._maxDepth , densityWeights , data , coefficients , isoValue , slabDepth , slabStart , slabEnd , vertexStream , polygonStream , zeroData , nonLinearFit , outputGradients , addBarycenter , polygonMesh , flipOrientation , NULL , NULL , dValues , dValues , false );
2462 }
2463
2464 template< unsigned int WeightDegree , unsigned int DataSig , unsigned int ... FEMSigs >
2465 static Stats Extract( UIntPack< FEMSigs ... > , UIntPack< WeightDegree > , UIntPack< DataSig > , const FEMTree< Dim , Real >& tree , int maxKeyDepth , const DensityEstimator< WeightDegree >* densityWeights , const SparseNodeData< ProjectiveData< Data , Real > , IsotropicUIntPack< Dim , DataSig > >* data , const DenseNodeData< Real , UIntPack< FEMSigs ... > >& coefficients , Real isoValue , unsigned int slabDepth , unsigned int slabStart , unsigned int slabEnd , OutputVertexStream &vertexStream , OutputDataStream< std::vector< node_index_type > > &polygonStream , const Data &zeroData , bool nonLinearFit , bool outputGradients , bool addBarycenter , bool polygonMesh , bool flipOrientation , const typename LevelSetExtractor< Real , Dim-1 , Point< Real , Dim-1 > >::TreeSliceValuesAndVertexPositions *backBoundary , const typename LevelSetExtractor< Real , Dim-1 , Point< Real , Dim-1 > >::TreeSliceValuesAndVertexPositions *frontBoundary , const std::vector< std::vector< Real > > &backDValues , const std::vector< std::vector< Real > > &frontDValues , bool copyTopology )
2466 {
2467 return _LevelSetExtractor< HasData , Real , Dim , Data >::template Extract< WeightDegree , DataSig , OutputVertexStream , FEMSigs ... >( UIntPack< FEMSigs ... >() , UIntPack< WeightDegree >() , UIntPack< DataSig >() , tree , maxKeyDepth , densityWeights , data , coefficients , isoValue , slabDepth , slabStart , slabEnd , vertexStream , polygonStream , zeroData , nonLinearFit , outputGradients , addBarycenter , polygonMesh , flipOrientation , backBoundary , frontBoundary , backDValues , frontDValues , copyTopology );
2468 }
2469 };