| 1 |
/* |
| 2 |
Copyright (c) 2016, 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 |
//////////////////////// |
| 30 |
// FEMTreeInitializer // |
| 31 |
//////////////////////// |
| 32 |
template< unsigned int Dim , class Real > |
| 33 |
size_t FEMTreeInitializer< Dim , Real >::Initialize( FEMTreeNode &root , int maxDepth , std::function< bool ( int , int[] ) > Refine , Allocator< FEMTreeNode >* nodeAllocator , std::function< void ( FEMTreeNode& ) > NodeInitializer ) |
| 34 |
{ |
| 35 |
typename FEMTreeNode::SubTreeExtractor subtreeExtractor( root ); |
| 36 |
return _Initialize( root , maxDepth , Refine , nodeAllocator , NodeInitializer ); |
| 37 |
} |
| 38 |
|
| 39 |
template< unsigned int Dim , class Real > |
| 40 |
size_t FEMTreeInitializer< Dim , Real >::_Initialize( FEMTreeNode &node , int maxDepth , std::function< bool ( int , int[] ) > Refine , Allocator< FEMTreeNode > *nodeAllocator , std::function< void ( FEMTreeNode& ) > NodeInitializer ) |
| 41 |
{ |
| 42 |
size_t count = 0; |
| 43 |
int d , off[3]; |
| 44 |
node.depthAndOffset( d , off ); |
| 45 |
if( d<maxDepth && Refine( d , off ) ) |
| 46 |
{ |
| 47 |
if( !node.children ) node.template initChildren< false >( nodeAllocator , NodeInitializer ) , count += 1<<Dim; |
| 48 |
for( int c=0 ; c<(1<<Dim) ; c++ ) count += _Initialize( node.children[c] , maxDepth , Refine , nodeAllocator , NodeInitializer ); |
| 49 |
} |
| 50 |
return count; |
| 51 |
} |
| 52 |
|
| 53 |
template< unsigned int Dim , class Real > |
| 54 |
template< typename IsValidFunctor /*=std::function< bool ( const Point< Real , Dim > & , const AuxData &... ) >*/ , typename ProcessFunctor/*=std::function< bool ( FEMTreeNode & , const Point< Real , Dim > & , const AuxData &... ) >*/ , typename ... AuxData > |
| 55 |
size_t FEMTreeInitializer< Dim , Real >::Initialize( FEMTreeNode &root , InputDataStream< Point< Real , Dim > , AuxData ... > &pointStream , AuxData ... d , int maxDepth , Allocator< FEMTreeNode >* nodeAllocator , std::function< void ( FEMTreeNode& ) > NodeInitializer , IsValidFunctor IsValid , ProcessFunctor Process ) |
| 56 |
{ |
| 57 |
return Initialize< IsValidFunctor , ProcessFunctor , AuxData ... >( root , pointStream , d... , maxDepth , [&]( Point< Real , Dim > ){ return maxDepth; } , nodeAllocator , NodeInitializer , IsValid , Process ); |
| 58 |
} |
| 59 |
|
| 60 |
template< unsigned int Dim , class Real > |
| 61 |
template< typename IsValidFunctor/*=std::function< bool ( const Point< Real , Dim > & , const AuxData &... ) >*/ , typename ProcessFunctor/*=std::function< bool ( FEMTreeNode & , const Point< Real , Dim > & , const AuxData &... ) >*/ , typename ... AuxData > |
| 62 |
size_t FEMTreeInitializer< Dim , Real >::Initialize( FEMTreeNode &root , InputDataStream< Point< Real , Dim > , AuxData ... > &pointStream , AuxData ... d , int maxDepth , std::function< int ( Point< Real , Dim > ) > pointDepthFunctor , Allocator< FEMTreeNode >* nodeAllocator , std::function< void ( FEMTreeNode& ) > NodeInitializer , IsValidFunctor IsValid , ProcessFunctor Process ) |
| 63 |
{ |
| 64 |
typename FEMTreeNode::SubTreeExtractor subtreeExtractor( root ); |
| 65 |
auto Leaf = [&]( FEMTreeNode& root , Point< Real , Dim > p , unsigned int maxDepth ) |
| 66 |
{ |
| 67 |
for( int d=0 ; d<Dim ; d++ ) if( p[d]<0 || p[d]>1 ) return (FEMTreeNode*)NULL; |
| 68 |
Point< Real , Dim > center; |
| 69 |
Real width; |
| 70 |
typename FEMTree< Dim , Real >::LocalDepth depth; |
| 71 |
typename FEMTree< Dim , Real >::LocalOffset offset; |
| 72 |
root.centerAndWidth( center , width ); |
| 73 |
root.depthAndOffset( depth , offset ); |
| 74 |
|
| 75 |
FEMTreeNode* node = &root; |
| 76 |
|
| 77 |
while( depth<(int)maxDepth ) |
| 78 |
{ |
| 79 |
if( !node->children ) node->template initChildren< false >( nodeAllocator , NodeInitializer ); |
| 80 |
int cIndex = FEMTreeNode::ChildIndex( center , p ); |
| 81 |
node = node->children + cIndex; |
| 82 |
width /= 2; |
| 83 |
|
| 84 |
depth++; |
| 85 |
for( int dd=0 ; dd<Dim ; dd++ ) |
| 86 |
if( (cIndex>>dd) & 1 ) center[dd] += width/2 , offset[dd] = (offset[dd]<<1) | 1; |
| 87 |
else center[dd] -= width/2 , offset[dd] = (offset[dd]<<1) | 0; |
| 88 |
} |
| 89 |
return node; |
| 90 |
}; |
| 91 |
|
| 92 |
// Add the point data |
| 93 |
size_t outOfBoundPoints = 0 , badDataCount = 0 , pointCount = 0; |
| 94 |
Point< Real , Dim > p; |
| 95 |
while( pointStream.read( p , d... ) ) |
| 96 |
{ |
| 97 |
// Check if the data is good |
| 98 |
if( !IsValid( p , d... ) ){ badDataCount++ ; continue; } |
| 99 |
|
| 100 |
// Check that the position is in-range |
| 101 |
FEMTreeNode *leaf = Leaf( root , p , pointDepthFunctor(p) ); |
| 102 |
if( !leaf ){ outOfBoundPoints++ ; continue; } |
| 103 |
|
| 104 |
// Process the data |
| 105 |
if( Process( *leaf , p , d ... ) ) pointCount++; |
| 106 |
} |
| 107 |
pointStream.reset(); |
| 108 |
return pointCount; |
| 109 |
} |
| 110 |
|
| 111 |
template< unsigned int Dim , class Real > |
| 112 |
template< typename AuxData > |
| 113 |
size_t FEMTreeInitializer< Dim , Real >::Initialize( FEMTreeNode &root , InputDataStream< Point< Real , Dim > , AuxData > &pointStream , AuxData zeroData , int maxDepth , std::vector< PointSample >& samplePoints , std::vector< AuxData > &sampleData , Allocator< FEMTreeNode >* nodeAllocator , std::function< void ( FEMTreeNode& ) > NodeInitializer , std::function< Real ( const Point< Real , Dim > & , AuxData & ) > ProcessData ) |
| 114 |
{ |
| 115 |
struct StreamInitializationData sid; |
| 116 |
return Initialize< AuxData >( sid , root , pointStream , zeroData , maxDepth , samplePoints , sampleData , nodeAllocator , NodeInitializer , ProcessData ); |
| 117 |
} |
| 118 |
|
| 119 |
template< unsigned int Dim , class Real > |
| 120 |
template< typename AuxData > |
| 121 |
size_t FEMTreeInitializer< Dim , Real >::Initialize( FEMTreeNode &root , InputDataStream< Point< Real , Dim > , AuxData > &pointStream , AuxData zeroData , int maxDepth , std::function< int ( Point< Real , Dim > ) > pointDepthFunctor , std::vector< PointSample >& samplePoints , std::vector< AuxData > &sampleData , Allocator< FEMTreeNode >* nodeAllocator , std::function< void ( FEMTreeNode& ) > NodeInitializer , std::function< Real ( const Point< Real , Dim > & , AuxData & ) > ProcessData ) |
| 122 |
{ |
| 123 |
struct StreamInitializationData sid; |
| 124 |
return Initialize< AuxData >( sid , root , pointStream , zeroData , maxDepth , pointDepthFunctor , samplePoints , sampleData , nodeAllocator , NodeInitializer , ProcessData ); |
| 125 |
} |
| 126 |
|
| 127 |
template< unsigned int Dim , class Real > |
| 128 |
template< typename AuxData > |
| 129 |
size_t FEMTreeInitializer< Dim , Real >::Initialize( struct StreamInitializationData &sid , FEMTreeNode &root , InputDataStream< Point< Real , Dim > , AuxData > &pointStream , AuxData zeroData , int maxDepth , std::vector< PointSample >& samplePoints , std::vector< AuxData > &sampleData , Allocator< FEMTreeNode >* nodeAllocator , std::function< void ( FEMTreeNode& ) > NodeInitializer , std::function< Real ( const Point< Real , Dim > & , AuxData & ) > ProcessData ) |
| 130 |
{ |
| 131 |
return Initialize< AuxData >( sid , root , pointStream , zeroData , maxDepth , [&]( Point< Real , Dim > ){ return maxDepth; } , samplePoints , sampleData , nodeAllocator , NodeInitializer , ProcessData ); |
| 132 |
} |
| 133 |
|
| 134 |
template< unsigned int Dim , class Real > |
| 135 |
template< typename AuxData > |
| 136 |
size_t FEMTreeInitializer< Dim , Real >::Initialize( struct StreamInitializationData &sid , FEMTreeNode &root , InputDataStream< Point< Real , Dim > , AuxData > &pointStream , AuxData zeroData , int maxDepth , std::function< int ( Point< Real , Dim > ) > pointDepthFunctor , std::vector< PointSample >& samplePoints , std::vector< AuxData > &sampleData , Allocator< FEMTreeNode >* nodeAllocator , std::function< void ( FEMTreeNode& ) > NodeInitializer , std::function< Real ( const Point< Real , Dim > & , AuxData & ) > ProcessData ) |
| 137 |
{ |
| 138 |
Real weight; |
| 139 |
std::vector< node_index_type > &nodeToIndexMap = sid._nodeToIndexMap; |
| 140 |
|
| 141 |
auto IsValid = [&]( const Point< Real , Dim > &p , AuxData & d ) |
| 142 |
{ |
| 143 |
weight = ProcessData( p , d ); |
| 144 |
return weight>0; |
| 145 |
}; |
| 146 |
auto Process = [&]( FEMTreeNode &node , const Point< Real , Dim > &p , AuxData &d ) |
| 147 |
{ |
| 148 |
node_index_type nodeIndex = node.nodeData.nodeIndex; |
| 149 |
// If the node's index exceeds what's stored in the node-to-index map, grow the node-to-index map |
| 150 |
if( nodeIndex>=(node_index_type)nodeToIndexMap.size() ) nodeToIndexMap.resize( nodeIndex+1 , -1 ); |
| 151 |
|
| 152 |
node_index_type idx = nodeToIndexMap[ nodeIndex ]; |
| 153 |
if( idx==-1 ) |
| 154 |
{ |
| 155 |
idx = (node_index_type)samplePoints.size(); |
| 156 |
nodeToIndexMap[ nodeIndex ] = idx; |
| 157 |
samplePoints.resize( idx+1 ) , samplePoints[idx].node = &node; |
| 158 |
sampleData.resize( idx+1 ); |
| 159 |
samplePoints[idx].sample = ProjectiveData< Point< Real , Dim > , Real >( p*weight , weight ); |
| 160 |
sampleData[idx] = d*weight; |
| 161 |
} |
| 162 |
else |
| 163 |
{ |
| 164 |
samplePoints[idx].sample += ProjectiveData< Point< Real , Dim > , Real >( p*weight , weight ); |
| 165 |
sampleData[ idx ] += d*weight; |
| 166 |
} |
| 167 |
return true; |
| 168 |
}; |
| 169 |
return Initialize< decltype(IsValid) , decltype(Process) , AuxData >( root , pointStream , zeroData , maxDepth , pointDepthFunctor , nodeAllocator , NodeInitializer , IsValid , Process ); |
| 170 |
} |
| 171 |
|
| 172 |
template< unsigned int Dim , class Real > |
| 173 |
void FEMTreeInitializer< Dim , Real >::Initialize( FEMTreeNode &root , const std::vector< Point< Real , Dim > >& vertices , const std::vector< SimplexIndex< Dim-1 , node_index_type > >& simplices , int maxDepth , std::vector< PointSample >& samples , std::vector< Allocator< FEMTreeNode > * > &nodeAllocators , std::function< void ( FEMTreeNode& ) > NodeInitializer ) |
| 174 |
{ |
| 175 |
typename FEMTreeNode::SubTreeExtractor subtreeExtractor( root ); |
| 176 |
|
| 177 |
std::vector< node_index_type > nodeToIndexMap; |
| 178 |
ThreadPool::ParallelFor( 0 , simplices.size() , [&]( unsigned int t , size_t i ) |
| 179 |
{ |
| 180 |
Simplex< Real , Dim , Dim-1 > s; |
| 181 |
for( int k=0 ; k<Dim ; k++ ) s[k] = vertices[ simplices[i][k] ]; |
| 182 |
_AddSimplex< true >( root , s , maxDepth , samples , &nodeToIndexMap , nodeAllocators.size() ? nodeAllocators[t] : NULL , NodeInitializer ); |
| 183 |
} ); |
| 184 |
} |
| 185 |
|
| 186 |
template< unsigned int Dim , class Real > |
| 187 |
void FEMTreeInitializer< Dim , Real >::Initialize( FEMTreeNode &root , const std::vector< ProjectiveData< Point< Real , Dim > , Real > > &points , int maxDepth , std::vector< PointSample >& samples , std::vector< Allocator< FEMTreeNode > * > &nodeAllocators , std::function< void ( FEMTreeNode& ) > NodeInitializer ) |
| 188 |
{ |
| 189 |
typename FEMTreeNode::SubTreeExtractor subtreeExtractor( root ); |
| 190 |
|
| 191 |
std::vector< node_index_type > nodeToIndexMap; |
| 192 |
ThreadPool::ParallelFor( 0 , points.size() , [&]( unsigned int t , size_t i ) |
| 193 |
{ |
| 194 |
_AddSample< true >( root , points[i] , maxDepth , samples , &nodeToIndexMap , nodeAllocators.size() ? nodeAllocators[t] : NULL , NodeInitializer ); |
| 195 |
} ); |
| 196 |
} |
| 197 |
|
| 198 |
template< unsigned int Dim , class Real > |
| 199 |
void FEMTreeInitializer< Dim , Real >::Initialize( FEMTreeNode &root , const std::vector< ProjectiveData< Point< Real , Dim > , Real > > &points , int maxDepth , std::vector< PointSample >& samples ) |
| 200 |
{ |
| 201 |
typename FEMTreeNode::SubTreeExtractor subtreeExtractor( root ); |
| 202 |
|
| 203 |
std::vector< node_index_type > nodeToIndexMap; |
| 204 |
ThreadPool::ParallelFor( 0 , points.size() , [&]( unsigned int t , size_t i ) |
| 205 |
{ |
| 206 |
_AddSample( root , points[i] , maxDepth , samples , &nodeToIndexMap ); |
| 207 |
} ); |
| 208 |
} |
| 209 |
|
| 210 |
template< unsigned int Dim , class Real > |
| 211 |
template< bool ThreadSafe > |
| 212 |
size_t FEMTreeInitializer< Dim , Real >::_AddSample( FEMTreeNode& root , ProjectiveData< Point< Real , Dim > , Real > s , int maxDepth , std::vector< PointSample >& samples , std::vector< node_index_type >* nodeToIndexMap , Allocator< FEMTreeNode >* nodeAllocator , std::function< void ( FEMTreeNode& ) > NodeInitializer ) |
| 213 |
{ |
| 214 |
auto Leaf = [&]( Point< Real , Dim > p , int maxDepth ) |
| 215 |
{ |
| 216 |
for( int d=0 ; d<Dim ; d++ ) if( p[d]<0 || p[d]>1 ) return (FEMTreeNode*)NULL; |
| 217 |
Point< Real , Dim > center; |
| 218 |
for( int d=0 ; d<Dim ; d++ ) center[d] = (Real)0.5; |
| 219 |
Real width = Real(1.0); |
| 220 |
FEMTreeNode* node = &root; |
| 221 |
int d=0; |
| 222 |
while( d<maxDepth ) |
| 223 |
{ |
| 224 |
if( !node->children ) node->template initChildren< ThreadSafe >( nodeAllocator , NodeInitializer ); |
| 225 |
int cIndex = FEMTreeNode::ChildIndex( center , p ); |
| 226 |
node = node->children + cIndex; |
| 227 |
d++; |
| 228 |
width /= 2; |
| 229 |
for( int d=0 ; d<Dim ; d++ ) |
| 230 |
if( (cIndex>>d) & 1 ) center[d] += width/2; |
| 231 |
else center[d] -= width/2; |
| 232 |
} |
| 233 |
return node; |
| 234 |
}; |
| 235 |
FEMTreeNode *node = Leaf( s.value() , maxDepth ); |
| 236 |
if( !node ) return 0; |
| 237 |
else return _AddSample( node , s , maxDepth , samples , nodeToIndexMap ); |
| 238 |
} |
| 239 |
|
| 240 |
template< unsigned int Dim , class Real > |
| 241 |
size_t FEMTreeInitializer< Dim , Real >::_AddSample( FEMTreeNode& root , ProjectiveData< Point< Real , Dim > , Real > s , int maxDepth , std::vector< PointSample >& samples , std::vector< node_index_type >* nodeToIndexMap ) |
| 242 |
{ |
| 243 |
auto Leaf = [&]( Point< Real , Dim > p , int maxDepth ) |
| 244 |
{ |
| 245 |
for( int d=0 ; d<Dim ; d++ ) if( p[d]<0 || p[d]>1 ) return (FEMTreeNode*)NULL; |
| 246 |
Point< Real , Dim > center; |
| 247 |
for( int d=0 ; d<Dim ; d++ ) center[d] = (Real)0.5; |
| 248 |
Real width = Real(1.0); |
| 249 |
FEMTreeNode* node = &root; |
| 250 |
int d=0; |
| 251 |
while( d<maxDepth && node->children ) |
| 252 |
{ |
| 253 |
int cIndex = FEMTreeNode::ChildIndex( center , p ); |
| 254 |
node = node->children + cIndex; |
| 255 |
d++; |
| 256 |
width /= 2; |
| 257 |
for( int d=0 ; d<Dim ; d++ ) |
| 258 |
if( (cIndex>>d) & 1 ) center[d] += width/2; |
| 259 |
else center[d] -= width/2; |
| 260 |
} |
| 261 |
return node; |
| 262 |
}; |
| 263 |
FEMTreeNode *node = Leaf( s.value() , maxDepth ); |
| 264 |
if( !node ) return 0; |
| 265 |
else return _AddSample( node , s , maxDepth , samples , nodeToIndexMap ); |
| 266 |
} |
| 267 |
|
| 268 |
template< unsigned int Dim , class Real > |
| 269 |
size_t FEMTreeInitializer< Dim , Real >::_AddSample( FEMTreeNode* node , ProjectiveData< Point< Real , Dim > , Real > s , int maxDepth , std::vector< PointSample >& samples , std::vector< node_index_type >* nodeToIndexMap ) |
| 270 |
{ |
| 271 |
if( nodeToIndexMap ) |
| 272 |
{ |
| 273 |
node_index_type nodeIndex = node->nodeData.nodeIndex; |
| 274 |
{ |
| 275 |
static std::mutex m; |
| 276 |
std::lock_guard< std::mutex > lock(m); |
| 277 |
if( nodeIndex>=(node_index_type)nodeToIndexMap->size() ) nodeToIndexMap->resize( nodeIndex+1 , -1 ); |
| 278 |
node_index_type idx = (*nodeToIndexMap)[ nodeIndex ]; |
| 279 |
if( idx==-1 ) |
| 280 |
{ |
| 281 |
idx = (node_index_type)samples.size(); |
| 282 |
(*nodeToIndexMap)[ nodeIndex ] = idx; |
| 283 |
samples.resize( idx+1 ); |
| 284 |
samples[idx].node = node; |
| 285 |
} |
| 286 |
samples[idx].sample += s; |
| 287 |
} |
| 288 |
} |
| 289 |
else |
| 290 |
{ |
| 291 |
{ |
| 292 |
static std::mutex m; |
| 293 |
std::lock_guard< std::mutex > lock(m); |
| 294 |
node_index_type idx = (node_index_type)samples.size(); |
| 295 |
samples.resize( idx+1 ); |
| 296 |
samples[idx].node = node; |
| 297 |
samples[idx].sample = s; |
| 298 |
} |
| 299 |
} |
| 300 |
return 1; |
| 301 |
} |
| 302 |
|
| 303 |
template< unsigned int Dim , class Real > |
| 304 |
template< bool ThreadSafe > |
| 305 |
size_t FEMTreeInitializer< Dim , Real >::_AddSimplex( FEMTreeNode& root , Simplex< Real , Dim , Dim-1 >& s , int maxDepth , std::vector< PointSample >& samples , std::vector< node_index_type >* nodeToIndexMap , Allocator< FEMTreeNode >* nodeAllocator , std::function< void ( FEMTreeNode& ) > NodeInitializer ) |
| 306 |
{ |
| 307 |
std::vector< Simplex< Real , Dim , Dim-1 > > subSimplices; |
| 308 |
subSimplices.push_back( s ); |
| 309 |
|
| 310 |
// Clip the simplex to the unit cube |
| 311 |
{ |
| 312 |
for( int d=0 ; d<Dim ; d++ ) |
| 313 |
{ |
| 314 |
Point< Real , Dim > n; |
| 315 |
n[d] = 1; |
| 316 |
{ |
| 317 |
std::vector< Simplex< Real , Dim , Dim-1 > > back , front; |
| 318 |
for( int i=0 ; i<subSimplices.size() ; i++ ) subSimplices[i].split( n , 0 , back , front ); |
| 319 |
subSimplices = front; |
| 320 |
} |
| 321 |
{ |
| 322 |
std::vector< Simplex< Real , Dim , Dim-1 > > back , front; |
| 323 |
for( int i=0 ; i<subSimplices.size() ; i++ ) subSimplices[i].split( n , 1 , back , front ); |
| 324 |
subSimplices = back; |
| 325 |
} |
| 326 |
} |
| 327 |
} |
| 328 |
|
| 329 |
struct RegularGridIndex |
| 330 |
{ |
| 331 |
int idx[Dim]; |
| 332 |
bool operator != ( const RegularGridIndex& i ) const |
| 333 |
{ |
| 334 |
for( int d=0 ; d<Dim ; d++ ) if( idx[d]!=i.idx[d] ) return true; |
| 335 |
return false; |
| 336 |
} |
| 337 |
}; |
| 338 |
|
| 339 |
auto Leaf = [&]( Point< Real , Dim > p , int maxDepth ) |
| 340 |
{ |
| 341 |
for( int d=0 ; d<Dim ; d++ ) if( p[d]<0 || p[d]>1 ) return (FEMTreeNode*)NULL; |
| 342 |
Point< Real , Dim > center; |
| 343 |
for( int d=0 ; d<Dim ; d++ ) center[d] = (Real)0.5; |
| 344 |
Real width = Real(1.0); |
| 345 |
FEMTreeNode* node = &root; |
| 346 |
int d=0; |
| 347 |
while( d<maxDepth ) |
| 348 |
{ |
| 349 |
if( !node->children ) node->template initChildren< ThreadSafe >( nodeAllocator , NodeInitializer ); |
| 350 |
int cIndex = FEMTreeNode::ChildIndex( center , p ); |
| 351 |
node = node->children + cIndex; |
| 352 |
d++; |
| 353 |
width /= 2; |
| 354 |
for( int d=0 ; d<Dim ; d++ ) |
| 355 |
if( (cIndex>>d) & 1 ) center[d] += width/2; |
| 356 |
else center[d] -= width/2; |
| 357 |
} |
| 358 |
return node; |
| 359 |
}; |
| 360 |
|
| 361 |
size_t sCount = 0; |
| 362 |
for( int i=0 ; i<subSimplices.size() ; i++ ) |
| 363 |
{ |
| 364 |
// Find the finest depth at which the simplex is entirely within a node |
| 365 |
int tDepth; |
| 366 |
RegularGridIndex idx0 , idx; |
| 367 |
for( tDepth=0 ; tDepth<maxDepth ; tDepth++ ) |
| 368 |
{ |
| 369 |
// Get the grid index of the first vertex of the simplex |
| 370 |
for( int d=0 ; d<Dim ; d++ ) idx0.idx[d] = idx.idx[d] = (int)( subSimplices[i][0][d] * (1<<(tDepth+1)) ); |
| 371 |
bool done = false; |
| 372 |
for( int k=0 ; k<Dim && !done ; k++ ) |
| 373 |
{ |
| 374 |
for( int d=0 ; d<Dim ; d++ ) idx.idx[d] = (int)( subSimplices[i][k][d] * (1<<(tDepth+1)) ); |
| 375 |
if( idx!=idx0 ) done = true; |
| 376 |
} |
| 377 |
if( done ) break; |
| 378 |
} |
| 379 |
|
| 380 |
// Generate a point in the middle of the simplex |
| 381 |
sCount += _AddSimplex< ThreadSafe >( Leaf( subSimplices[i].center() , tDepth ) , subSimplices[i] , maxDepth , samples , nodeToIndexMap , nodeAllocator , NodeInitializer ); |
| 382 |
} |
| 383 |
return sCount; |
| 384 |
} |
| 385 |
|
| 386 |
template< unsigned int Dim , class Real > |
| 387 |
template< bool ThreadSafe > |
| 388 |
size_t FEMTreeInitializer< Dim , Real >::_AddSimplex( FEMTreeNode* node , Simplex< Real , Dim , Dim-1 >& s , int maxDepth , std::vector< PointSample >& samples , std::vector< node_index_type >* nodeToIndexMap , Allocator< FEMTreeNode >* nodeAllocator , std::function< void ( FEMTreeNode& ) > NodeInitializer ) |
| 389 |
{ |
| 390 |
int d = node->depth(); |
| 391 |
if( d==maxDepth ) |
| 392 |
{ |
| 393 |
Real weight = s.measure(); |
| 394 |
Point< Real , Dim > position = s.center(); |
| 395 |
if( weight && weight==weight ) |
| 396 |
{ |
| 397 |
if( nodeToIndexMap ) |
| 398 |
{ |
| 399 |
node_index_type nodeIndex = node->nodeData.nodeIndex; |
| 400 |
{ |
| 401 |
static std::mutex m; |
| 402 |
std::lock_guard< std::mutex > lock(m); |
| 403 |
if( nodeIndex>=(node_index_type)nodeToIndexMap->size() ) nodeToIndexMap->resize( nodeIndex+1 , -1 ); |
| 404 |
node_index_type idx = (*nodeToIndexMap)[ nodeIndex ]; |
| 405 |
if( idx==-1 ) |
| 406 |
{ |
| 407 |
idx = (node_index_type)samples.size(); |
| 408 |
(*nodeToIndexMap)[ nodeIndex ] = idx; |
| 409 |
samples.resize( idx+1 ); |
| 410 |
samples[idx].node = node; |
| 411 |
} |
| 412 |
samples[idx].sample += ProjectiveData< Point< Real , Dim > , Real >( position*weight , weight ); |
| 413 |
} |
| 414 |
} |
| 415 |
else |
| 416 |
{ |
| 417 |
{ |
| 418 |
static std::mutex m; |
| 419 |
std::lock_guard< std::mutex > lock(m); |
| 420 |
node_index_type idx = (node_index_type)samples.size(); |
| 421 |
samples.resize( idx+1 ); |
| 422 |
samples[idx].node = node; |
| 423 |
samples[idx].sample = ProjectiveData< Point< Real , Dim > , Real >( position*weight , weight ); |
| 424 |
} |
| 425 |
} |
| 426 |
} |
| 427 |
return 1; |
| 428 |
} |
| 429 |
else |
| 430 |
{ |
| 431 |
size_t sCount = 0; |
| 432 |
if( !node->children ) node->template initChildren< ThreadSafe >( nodeAllocator , NodeInitializer ); |
| 433 |
|
| 434 |
// Split up the simplex and pass the parts on to the children |
| 435 |
Point< Real , Dim > center; |
| 436 |
Real width; |
| 437 |
node->centerAndWidth( center , width ); |
| 438 |
|
| 439 |
std::vector< std::vector< Simplex< Real , Dim , Dim-1 > > > childSimplices( 1 ); |
| 440 |
childSimplices[0].push_back( s ); |
| 441 |
for( int d=0 ; d<Dim ; d++ ) |
| 442 |
{ |
| 443 |
Point< Real , Dim > n ; n[Dim-d-1] = 1; |
| 444 |
std::vector< std::vector< Simplex< Real , Dim , Dim-1 > > > temp( (int)( 1<<(d+1) ) ); |
| 445 |
for( int c=0 ; c<(1<<d) ; c++ ) for( size_t i=0 ; i<childSimplices[c].size() ; i++ ) childSimplices[c][i].split( n , center[Dim-d-1] , temp[2*c] , temp[2*c+1] ); |
| 446 |
childSimplices = temp; |
| 447 |
} |
| 448 |
for( int c=0 ; c<(1<<Dim) ; c++ ) for( size_t i=0 ; i<childSimplices[c].size() ; i++ ) if( childSimplices[c][i].measure() ) sCount += _AddSimplex< ThreadSafe >( node->children+c , childSimplices[c][i] , maxDepth , samples , nodeToIndexMap , nodeAllocator , NodeInitializer ); |
| 449 |
return sCount; |
| 450 |
} |
| 451 |
} |
| 452 |
|
| 453 |
template< unsigned int Dim , class Real > |
| 454 |
void FEMTreeInitializer< Dim , Real >::Initialize( FEMTreeNode& root , const std::vector< Point< Real , Dim > >& vertices , const std::vector< SimplexIndex< Dim-1 , node_index_type > >& simplices , unsigned int regularGridDepth , unsigned int maxDepth , std::vector< NodeSimplices< Dim , Real > >& nodeSimplices , std::vector< Allocator< FEMTreeNode > * > &nodeAllocators , std::function< void ( FEMTreeNode& ) > NodeInitializer ) |
| 455 |
{ |
| 456 |
if( regularGridDepth>maxDepth ) MK_THROW( "Regular grid depth cannot excceed maximum depth: " , regularGridDepth , " <= " , maxDepth ); |
| 457 |
|
| 458 |
// Allocate the tree up to the prescribed depth |
| 459 |
const Real RegularGridWidth = (Real)( 1./(1<<regularGridDepth) ); |
| 460 |
|
| 461 |
auto Leaf = [&]( FEMTreeNode *root , Point< Real , Dim > p , int depth ) |
| 462 |
{ |
| 463 |
for( int d=0 ; d<Dim ; d++ ) if( p[d]<0 || p[d]>1 ) return (FEMTreeNode*)NULL; |
| 464 |
Point< Real , Dim > center; |
| 465 |
Real width; |
| 466 |
root->centerAndWidth( center , width ); |
| 467 |
FEMTreeNode *node = root; |
| 468 |
int d=node->depth(); |
| 469 |
while( d<depth ) |
| 470 |
{ |
| 471 |
if( !node->children ) node->template initChildren< false >( nodeAllocators.size() ? nodeAllocators[0] : NULL , NodeInitializer ); |
| 472 |
int cIndex = FEMTreeNode::ChildIndex( center , p ); |
| 473 |
node = node->children + cIndex; |
| 474 |
d++; |
| 475 |
width /= 2; |
| 476 |
for( int d=0 ; d<Dim ; d++ ) |
| 477 |
if( (cIndex>>d) & 1 ) center[d] += width/2; |
| 478 |
else center[d] -= width/2; |
| 479 |
} |
| 480 |
return node; |
| 481 |
}; |
| 482 |
|
| 483 |
IndexedSimplicialComplex< Real , Dim , Dim-1 , node_index_type > sComplex( vertices , simplices ); |
| 484 |
typename Rasterizer< Real , Dim >::template SimplexRasterizationGrid< node_index_type , Dim-1 > raster = Rasterizer< Real , Dim >::template Rasterize< node_index_type >( sComplex , regularGridDepth , typename Rasterizer< Real , Dim >::ThreadSafety( Rasterizer< Real , Dim >::ThreadSafety::MUTEXES , regularGridDepth ) ); |
| 485 |
// typename Rasterizer< Real , Dim >::template SimplexRasterizationGrid< node_index_type , Dim-1 > raster = Rasterizer< Real , Dim >::template Rasterize< node_index_type >( sComplex , regularGridDepth , typename Rasterizer< Real , Dim >::ThreadSafety( Rasterizer< Real , Dim >::ThreadSafety::SINGLE_THREADED ) ); |
| 486 |
// typename Rasterizer< Real , Dim >::template SimplexRasterizationGrid< node_index_type , Dim-1 > raster = Rasterizer< Real , Dim >::template Rasterize< node_index_type >( sComplex , regularGridDepth , typename Rasterizer< Real , Dim >::ThreadSafety( Rasterizer< Real , Dim >::ThreadSafety::MAP_REDUCE ) ); |
| 487 |
|
| 488 |
size_t geometricCellCount = 0; |
| 489 |
for( size_t i=0 ; i<raster.resolution() ; i++ ) if( raster[i].size() ) geometricCellCount++; |
| 490 |
|
| 491 |
if( maxDepth==regularGridDepth ) |
| 492 |
{ |
| 493 |
nodeSimplices.resize( geometricCellCount ); |
| 494 |
|
| 495 |
geometricCellCount = 0; |
| 496 |
for( size_t i=0 ; i<raster.resolution() ; i++ ) if( raster[i].size() ) |
| 497 |
{ |
| 498 |
int idx[Dim]; |
| 499 |
raster.setIndex( i , idx ); |
| 500 |
Point< Real , Dim > p; |
| 501 |
for( int d=0 ; d<Dim ; d++ ) p[d] = (Real)( idx[d] + 0.5 ) * RegularGridWidth; |
| 502 |
NodeSimplices< Dim , Real > &nSimplices = nodeSimplices[ geometricCellCount++ ]; |
| 503 |
nSimplices.node = Leaf( &root , p , maxDepth ); |
| 504 |
nSimplices.data = raster[i]; |
| 505 |
} |
| 506 |
} |
| 507 |
else |
| 508 |
{ |
| 509 |
// The indices of the grid cells containing geometry |
| 510 |
std::vector< size_t > cellIndices( geometricCellCount ); |
| 511 |
// The list of nodes @{regularGridDepth} containing geometry |
| 512 |
std::vector< FEMTreeNode * > roots( geometricCellCount ); |
| 513 |
std::vector< node_index_type > nodeToIndexMap; |
| 514 |
|
| 515 |
// Get the list of the indices of the regular grid containing geometry |
| 516 |
geometricCellCount = 0; |
| 517 |
// [WARNING] In principal, this could be done in parallel but then Leaf would need to be thread-safe. |
| 518 |
for( size_t i=0 ; i<raster.resolution() ; i++ ) if( raster[i].size() ) |
| 519 |
{ |
| 520 |
cellIndices[ geometricCellCount ] = i; |
| 521 |
int idx[Dim]; |
| 522 |
raster.setIndex( i , idx ); |
| 523 |
Point< Real , Dim > p; |
| 524 |
for( int d=0 ; d<Dim ; d++ ) p[d] = (Real)( idx[d] + 0.5 ) * RegularGridWidth; |
| 525 |
roots[ geometricCellCount ] = Leaf( &root , p , regularGridDepth ); |
| 526 |
geometricCellCount++; |
| 527 |
} |
| 528 |
|
| 529 |
std::vector< Allocator< FEMTreeNode > * > _nodeAllocators( ThreadPool::NumThreads() ); |
| 530 |
for( int i=0 ; i<_nodeAllocators.size() ; i++ ) _nodeAllocators[i] = nodeAllocators.size() ? nodeAllocators[i] : NULL; |
| 531 |
ThreadPool::ParallelFor( 0 , geometricCellCount , [&]( unsigned int t , size_t i ) |
| 532 |
{ |
| 533 |
auto &cellSimplices = raster[ cellIndices[i] ]; |
| 534 |
for( int j=0 ; j<cellSimplices.size() ; j++ ) _AddSimplex< false , true >( *roots[i] , cellSimplices[j].first , cellSimplices[j].second , maxDepth , nodeSimplices , nodeToIndexMap , _nodeAllocators[t] , NodeInitializer ); |
| 535 |
} ); |
| 536 |
} |
| 537 |
} |
| 538 |
|
| 539 |
template< unsigned int Dim , class Real > |
| 540 |
template< bool ThreadSafeAllocation , bool ThreadSafeSimplices > |
| 541 |
size_t FEMTreeInitializer< Dim , Real >::_AddSimplex( FEMTreeNode& root , node_index_type id , Simplex< Real , Dim , Dim-1 >& s , int maxDepth , std::vector< NodeSimplices< Dim , Real > >& simplices , std::vector< node_index_type >& nodeToIndexMap , Allocator< FEMTreeNode >* nodeAllocator , std::function< void ( FEMTreeNode& ) > NodeInitializer ) |
| 542 |
{ |
| 543 |
struct RegularGridIndex |
| 544 |
{ |
| 545 |
int idx[Dim]; |
| 546 |
bool operator != ( const RegularGridIndex& i ) const |
| 547 |
{ |
| 548 |
for( int d=0 ; d<Dim ; d++ ) if( idx[d]!=i.idx[d] ) return true; |
| 549 |
return false; |
| 550 |
} |
| 551 |
}; |
| 552 |
|
| 553 |
auto Leaf = [&]( Point< Real , Dim > p , int maxDepth ) |
| 554 |
{ |
| 555 |
for( int d=0 ; d<Dim ; d++ ) if( p[d]<0 || p[d]>1 ) return (FEMTreeNode*)NULL; |
| 556 |
Point< Real , Dim > center; |
| 557 |
Real width; |
| 558 |
root.centerAndWidth( center , width ); |
| 559 |
int d = root.depth(); |
| 560 |
FEMTreeNode* node = &root; |
| 561 |
while( d<maxDepth ) |
| 562 |
{ |
| 563 |
if( !node->children ) node->template initChildren< ThreadSafeAllocation >( nodeAllocator , NodeInitializer ); |
| 564 |
int cIndex = FEMTreeNode::ChildIndex( center , p ); |
| 565 |
node = node->children + cIndex; |
| 566 |
d++; |
| 567 |
width /= 2; |
| 568 |
for( int d=0 ; d<Dim ; d++ ) |
| 569 |
if( (cIndex>>d) & 1 ) center[d] += width/2; |
| 570 |
else center[d] -= width/2; |
| 571 |
} |
| 572 |
return node; |
| 573 |
}; |
| 574 |
|
| 575 |
// Find the finest depth at which the simplex is entirely within a node |
| 576 |
int tDepth; |
| 577 |
RegularGridIndex idx0 , idx; |
| 578 |
for( tDepth=0 ; tDepth<maxDepth ; tDepth++ ) |
| 579 |
{ |
| 580 |
// Get the grid index of the first vertex of the simplex |
| 581 |
for( int d=0 ; d<Dim ; d++ ) idx0.idx[d] = (int)( s[0][d] * (1<<(tDepth+1)) ); |
| 582 |
bool done = false; |
| 583 |
for( int k=1 ; k<=Dim && !done ; k++ ) |
| 584 |
{ |
| 585 |
for( int d=0 ; d<Dim ; d++ ) idx.idx[d] = (int)( s[k][d] * (1<<(tDepth+1)) ); |
| 586 |
if( idx!=idx0 ) done = true; |
| 587 |
} |
| 588 |
if( done ) break; |
| 589 |
} |
| 590 |
// Add the simplex to the node |
| 591 |
return _AddSimplex< ThreadSafeAllocation , ThreadSafeSimplices >( Leaf( s.center() , tDepth ) , id , s , maxDepth , simplices , nodeToIndexMap , nodeAllocator , NodeInitializer ); |
| 592 |
} |
| 593 |
|
| 594 |
template< unsigned int Dim , class Real > |
| 595 |
template< bool ThreadSafeAllocation , bool ThreadSafeSimplices > |
| 596 |
size_t FEMTreeInitializer< Dim , Real >::_AddSimplex( FEMTreeNode* node , node_index_type id , Simplex< Real , Dim , Dim-1 >& s , int maxDepth , std::vector< NodeSimplices< Dim , Real > >& simplices , std::vector< node_index_type >& nodeToIndexMap , Allocator< FEMTreeNode >* nodeAllocator , std::function< void ( FEMTreeNode& ) > NodeInitializer ) |
| 597 |
{ |
| 598 |
int d = node->depth(); |
| 599 |
if( d==maxDepth ) |
| 600 |
{ |
| 601 |
// If the simplex has non-zero size, add it to the list |
| 602 |
Real weight = s.measure(); |
| 603 |
if( weight && weight==weight ) |
| 604 |
{ |
| 605 |
node_index_type nodeIndex = node->nodeData.nodeIndex; |
| 606 |
if( ThreadSafeSimplices ) |
| 607 |
{ |
| 608 |
static std::mutex m; |
| 609 |
std::lock_guard< std::mutex > lock(m); |
| 610 |
if( nodeIndex>=(node_index_type)nodeToIndexMap.size() ) nodeToIndexMap.resize( nodeIndex+1 , -1 ); |
| 611 |
node_index_type idx = nodeToIndexMap[ nodeIndex ]; |
| 612 |
if( idx==-1 ) |
| 613 |
{ |
| 614 |
idx = (node_index_type)simplices.size(); |
| 615 |
nodeToIndexMap[ nodeIndex ] = idx; |
| 616 |
simplices.resize( idx+1 ); |
| 617 |
simplices[idx].node = node; |
| 618 |
} |
| 619 |
simplices[idx].data.push_back( std::pair< node_index_type , Simplex< Real , Dim , Dim-1 > >( id , s ) ); |
| 620 |
} |
| 621 |
else |
| 622 |
{ |
| 623 |
if( nodeIndex>=(node_index_type)nodeToIndexMap.size() ) nodeToIndexMap.resize( nodeIndex+1 , -1 ); |
| 624 |
node_index_type idx = nodeToIndexMap[ nodeIndex ]; |
| 625 |
if( idx==-1 ) |
| 626 |
{ |
| 627 |
idx = (node_index_type)simplices.size(); |
| 628 |
nodeToIndexMap[ nodeIndex ] = idx; |
| 629 |
simplices.resize( idx+1 ); |
| 630 |
simplices[idx].node = node; |
| 631 |
} |
| 632 |
simplices[idx].data.push_back( std::pair< node_index_type , Simplex< Real , Dim , Dim-1 > >( id , s ) ); |
| 633 |
} |
| 634 |
} |
| 635 |
return 1; |
| 636 |
} |
| 637 |
else |
| 638 |
{ |
| 639 |
size_t sCount = 0; |
| 640 |
if( !node->children ) node->template initChildren< ThreadSafeAllocation >( nodeAllocator , NodeInitializer ); |
| 641 |
|
| 642 |
// Split up the simplex and pass the parts on to the children |
| 643 |
Point< Real , Dim > center; |
| 644 |
Real width; |
| 645 |
node->centerAndWidth( center , width ); |
| 646 |
|
| 647 |
std::vector< std::vector< Simplex< Real , Dim , Dim-1 > > > childSimplices( 1 ); |
| 648 |
childSimplices[0].push_back( s ); |
| 649 |
for( int d=0 ; d<Dim ; d++ ) |
| 650 |
{ |
| 651 |
Point< Real , Dim > n ; n[Dim-d-1] = 1; |
| 652 |
std::vector< std::vector< Simplex< Real , Dim , Dim-1 > > > temp( (int)( 1<<(d+1) ) ); |
| 653 |
for( int c=0 ; c<(1<<d) ; c++ ) for( int i=0 ; i<childSimplices[c].size() ; i++ ) childSimplices[c][i].split( n , center[Dim-d-1] , temp[2*c] , temp[2*c+1] ); |
| 654 |
childSimplices = temp; |
| 655 |
} |
| 656 |
for( int c=0 ; c<(1<<Dim) ; c++ ) for( int i=0 ; i<childSimplices[c].size() ; i++ ) sCount += _AddSimplex< ThreadSafeAllocation , ThreadSafeSimplices >( node->children+c , id , childSimplices[c][i] , maxDepth , simplices , nodeToIndexMap , nodeAllocator , NodeInitializer ); |
| 657 |
return sCount; |
| 658 |
} |
| 659 |
} |
| 660 |
|
| 661 |
template< unsigned int Dim , class Real > |
| 662 |
template< class Data , class _Data , bool Dual > |
| 663 |
size_t FEMTreeInitializer< Dim , Real >::Initialize( FEMTreeNode& root , ConstPointer( Data ) values , ConstPointer( int ) labels , int resolution[Dim] , std::vector< NodeSample< Dim , _Data > > derivatives[Dim] , Allocator< FEMTreeNode >* nodeAllocator , std::function< void ( FEMTreeNode& ) > NodeInitializer , std::function< _Data ( const Data& ) > DataConverter ) |
| 664 |
{ |
| 665 |
auto Leaf = [&]( FEMTreeNode& root , const int idx[Dim] , int maxDepth ) |
| 666 |
{ |
| 667 |
for( int d=0 ; d<Dim ; d++ ) if( idx[d]<0 || idx[d]>=(1<<maxDepth) ) return (FEMTreeNode*)NULL; |
| 668 |
FEMTreeNode* node = &root; |
| 669 |
for( int d=0 ; d<maxDepth ; d++ ) |
| 670 |
{ |
| 671 |
if( !node->children ) node->template initChildren< false >( nodeAllocator , NodeInitializer ); |
| 672 |
int cIndex = 0; |
| 673 |
for( int dd=0 ; dd<Dim ; dd++ ) if( idx[dd]&(1<<(maxDepth-d-1)) ) cIndex |= 1<<dd; |
| 674 |
node = node->children + cIndex; |
| 675 |
} |
| 676 |
return node; |
| 677 |
}; |
| 678 |
auto FactorIndex = []( size_t i , const int resolution[Dim] , int idx[Dim] ) |
| 679 |
{ |
| 680 |
size_t ii = i; |
| 681 |
for( int d=0 ; d<Dim ; d++ ) idx[d] = ii % resolution[d] , ii /= resolution[d]; |
| 682 |
}; |
| 683 |
auto MakeIndex = [] ( const int idx[Dim] , const int resolution[Dim] ) |
| 684 |
{ |
| 685 |
size_t i = 0; |
| 686 |
for( int d=0 ; d<Dim ; d++ ) i = i * resolution[Dim-1-d] + idx[Dim-1-d]; |
| 687 |
return i; |
| 688 |
}; |
| 689 |
|
| 690 |
|
| 691 |
int maxResolution = resolution[0]; |
| 692 |
for( int d=1 ; d<Dim ; d++ ) maxResolution = std::max< int >( maxResolution , resolution[d] ); |
| 693 |
int maxDepth = 0; |
| 694 |
while( ( (1<<maxDepth) + ( Dual ? 0 : 1 ) )<maxResolution ) maxDepth++; |
| 695 |
|
| 696 |
size_t totalRes = 1; |
| 697 |
for( int d=0 ; d<Dim ; d++ ) totalRes *= resolution[d]; |
| 698 |
|
| 699 |
// Iterate over each direction |
| 700 |
for( int d=0 ; d<Dim ; d++ ) for( size_t i=0 ; i<totalRes ; i++ ) |
| 701 |
{ |
| 702 |
// Factor the index into directional components and get the index of the next cell |
| 703 |
int idx[Dim] ; FactorIndex( i , resolution , idx ) ; idx[d]++; |
| 704 |
|
| 705 |
if( idx[d]<resolution[d] ) |
| 706 |
{ |
| 707 |
// Get the index of the next cell |
| 708 |
size_t ii = MakeIndex( idx , resolution ); |
| 709 |
|
| 710 |
// [NOTE] There are no derivatives across negative labels |
| 711 |
if( labels[i]!=labels[ii] && labels[i]>=0 && labels[ii]>=0 ) |
| 712 |
{ |
| 713 |
if( !Dual ) idx[d]--; |
| 714 |
NodeSample< Dim , _Data > nodeSample; |
| 715 |
nodeSample.node = Leaf( root , idx , maxDepth ); |
| 716 |
nodeSample.data = DataConverter( values[ii] ) - DataConverter( values[i] ); |
| 717 |
if( nodeSample.node ) derivatives[d].push_back( nodeSample ); |
| 718 |
} |
| 719 |
} |
| 720 |
} |
| 721 |
return maxDepth; |
| 722 |
} |
| 723 |
|
| 724 |
template< unsigned int Dim , class Real > |
| 725 |
template< bool Dual , class Data > |
| 726 |
unsigned int FEMTreeInitializer< Dim , Real >::Initialize( FEMTreeNode& root , DerivativeStream< Data >& dStream , Data zeroData , std::vector< NodeSample< Dim , Data > > derivatives[Dim] , Allocator< FEMTreeNode >* nodeAllocator , std::function< void ( FEMTreeNode& ) > NodeInitializer ) |
| 727 |
{ |
| 728 |
// Note: |
| 729 |
// -- Dual: The difference between [i] and [i+1] is stored at cell [i+1] |
| 730 |
// -- Primal: The difference between [i] and [i+1] is stored at cell [i] |
| 731 |
|
| 732 |
// Find the leaf containing the specified cell index |
| 733 |
auto Leaf = [&]( FEMTreeNode& root , const unsigned int idx[Dim] , unsigned int maxDepth ) |
| 734 |
{ |
| 735 |
for( int d=0 ; d<Dim ; d++ ) if( idx[d]<0 || idx[d]>=(unsigned int)(1<<maxDepth) ) return (FEMTreeNode*)NULL; |
| 736 |
FEMTreeNode* node = &root; |
| 737 |
for( unsigned int d=0 ; d<maxDepth ; d++ ) |
| 738 |
{ |
| 739 |
if( !node->children ) node->template initChildren< false >( nodeAllocator , NodeInitializer ); |
| 740 |
int cIndex = 0; |
| 741 |
for( int dd=0 ; dd<Dim ; dd++ ) if( idx[dd]&(1<<(maxDepth-d-1)) ) cIndex |= 1<<dd; |
| 742 |
node = node->children + cIndex; |
| 743 |
} |
| 744 |
return node; |
| 745 |
}; |
| 746 |
|
| 747 |
unsigned int resolution[Dim]; |
| 748 |
dStream.resolution( resolution ); |
| 749 |
unsigned int maxResolution = resolution[0]; |
| 750 |
for( int d=1 ; d<Dim ; d++ ) maxResolution = std::max< unsigned int >( maxResolution , resolution[d] ); |
| 751 |
unsigned int maxDepth = 0; |
| 752 |
|
| 753 |
// If we are using a dual formulation, we need at least maxResolution cells. |
| 754 |
// Otherwise, we need at least maxResolution-1 cells. |
| 755 |
while( (unsigned int)( (1<<maxDepth) + ( Dual ? 0 : 1 ) )<maxResolution ) maxDepth++; |
| 756 |
|
| 757 |
unsigned int idx[Dim] , dir; |
| 758 |
Data dValue = zeroData; |
| 759 |
while( dStream.nextDerivative( idx , dir , dValue ) ) |
| 760 |
{ |
| 761 |
if( Dual ) idx[dir]++; |
| 762 |
NodeSample< Dim , Data > nodeSample; |
| 763 |
nodeSample.node = Leaf( root , idx , maxDepth ); |
| 764 |
nodeSample.data = dValue; |
| 765 |
if( nodeSample.node ) derivatives[dir].push_back( nodeSample ); |
| 766 |
} |
| 767 |
return maxDepth; |
| 768 |
} |
| 769 |
|
| 770 |
template< unsigned int Dim , class Real > |
| 771 |
template< unsigned int _Dim > |
| 772 |
typename std::enable_if< _Dim!=1 , DenseNodeData< typename FEMTreeInitializer< Dim , Real >::GeometryNodeType , IsotropicUIntPack< Dim , FEMTrivialSignature > > >::type FEMTreeInitializer< Dim , Real >::GetGeometryNodeDesignators( FEMTreeNode *root , const std::vector< Point< Real , Dim > >& vertices , const std::vector< SimplexIndex< Dim-1 , node_index_type > >& simplices , unsigned int regularGridDepth , unsigned int maxDepth , std::vector< Allocator< FEMTreeNode > * > &nodeAllocators , std::function< void ( FEMTreeNode& ) > NodeInitializer ) |
| 773 |
{ |
| 774 |
static_assert( Dim==_Dim , "[ERROR] Dimensions don't match" ); |
| 775 |
std::vector< Point< Real , Dim > > normals( simplices.size() ); |
| 776 |
ThreadPool::ParallelFor |
| 777 |
( |
| 778 |
0 , simplices.size() , |
| 779 |
[&]( unsigned int , size_t i ) |
| 780 |
{ |
| 781 |
Simplex< Real , Dim , Dim-1 > s; |
| 782 |
for( int j=0 ; j<Dim ; j++ ) s[j] = vertices[ simplices[i][j] ]; |
| 783 |
normals[i] = s.normal(); |
| 784 |
} |
| 785 |
); |
| 786 |
return _GetGeometryNodeDesignators( root , vertices , simplices , normals , regularGridDepth , maxDepth , nodeAllocators , NodeInitializer ); |
| 787 |
} |
| 788 |
template< unsigned int Dim , class Real > |
| 789 |
template< unsigned int _Dim > |
| 790 |
typename std::enable_if< _Dim==1 , DenseNodeData< typename FEMTreeInitializer< Dim , Real >::GeometryNodeType , IsotropicUIntPack< Dim , FEMTrivialSignature > > >::type FEMTreeInitializer< Dim , Real >::GetGeometryNodeDesignators( FEMTreeNode *root , const std::vector< Point< Real , Dim > >& vertices , const std::vector< SimplexIndex< Dim-1 , node_index_type > >& simplices , unsigned int regularGridDepth , unsigned int maxDepth , std::vector< Allocator< FEMTreeNode > * > &nodeAllocators , std::function< void ( FEMTreeNode& ) > NodeInitializer ) |
| 791 |
{ |
| 792 |
static_assert( Dim==_Dim , "[ERROR] Dimensions don't match" ); |
| 793 |
if( simplices.size()%2 ) MK_THROW( "Expected even number of hull points: " , simplices.size() ); |
| 794 |
struct HullPoint |
| 795 |
{ |
| 796 |
Real x; |
| 797 |
size_t idx; |
| 798 |
}; |
| 799 |
std::vector< HullPoint > hullPoints( simplices.size() ); |
| 800 |
for( size_t i=0 ; i<simplices.size() ; i++ ) hullPoints[i].x = vertices[ simplices[i][0] ][0] , hullPoints[i].idx = i; |
| 801 |
std::sort( hullPoints.begin() , hullPoints.end() , []( const HullPoint &hp1 , const HullPoint &hp2 ){ return hp1.x<hp2.x; } ); |
| 802 |
std::vector< Point< Real , Dim > > normals( simplices.size() ); |
| 803 |
for( int i=0 ; i<hullPoints.size() ; i++ ) normals[ hullPoints[i].idx ][0] = (i%2) ? (Real)1. : (Real)-1.; |
| 804 |
return _GetGeometryNodeDesignators( root , vertices , simplices , normals , regularGridDepth , maxDepth , nodeAllocators , NodeInitializer ); |
| 805 |
} |
| 806 |
|
| 807 |
template< unsigned int Dim , class Real > |
| 808 |
DenseNodeData< typename FEMTreeInitializer< Dim , Real >::GeometryNodeType , IsotropicUIntPack< Dim , FEMTrivialSignature > > FEMTreeInitializer< Dim , Real >::_GetGeometryNodeDesignators( FEMTreeNode *root , const std::vector< Point< Real , Dim > >& vertices , const std::vector< SimplexIndex< Dim-1 , node_index_type > >& simplices , const std::vector< Point< Real , Dim > > &normals , unsigned int regularGridDepth , unsigned int maxDepth , std::vector< Allocator< FEMTreeNode > * > &nodeAllocators , std::function< void ( FEMTreeNode& ) > NodeInitializer ) |
| 809 |
{ |
| 810 |
typename FEMTreeNode::SubTreeExtractor subtreeExtractor( root ); |
| 811 |
|
| 812 |
typedef typename FEMTreeNode::template ConstNeighborKey< IsotropicUIntPack< Dim , 1 > , IsotropicUIntPack< Dim , 1 > > NeighborKey; |
| 813 |
typedef typename FEMTreeNode::template ConstNeighbors< IsotropicUIntPack< Dim , 3 > > Neighbors; |
| 814 |
DenseNodeData< typename FEMTreeInitializer< Dim , Real >::GeometryNodeType , IsotropicUIntPack< Dim , FEMTrivialSignature > > geometryNodeDesignators; |
| 815 |
|
| 816 |
// Rasterize the geometry into the tree |
| 817 |
std::vector< NodeSimplices< Dim , Real > > nodeSimplices; |
| 818 |
FEMTreeInitializer< Dim , Real >::Initialize( *root , vertices , simplices , regularGridDepth , maxDepth , nodeSimplices , nodeAllocators , NodeInitializer ); |
| 819 |
|
| 820 |
// Mark all the nodes containing geometry |
| 821 |
node_index_type nodeCount = 0; |
| 822 |
root->processNodes( [&]( FEMTreeNode *node ){ nodeCount = std::max< node_index_type >( nodeCount , node->nodeData.nodeIndex ); } ); |
| 823 |
nodeCount++; |
| 824 |
|
| 825 |
geometryNodeDesignators.resize( nodeCount ); |
| 826 |
|
| 827 |
ThreadPool::ParallelFor( 0 , nodeSimplices.size() , [&]( unsigned int , size_t i ){ for( FEMTreeNode *node=nodeSimplices[i].node ; node ; node=node->parent ) geometryNodeDesignators[node] = GeometryNodeType::BOUNDARY; } ); |
| 828 |
|
| 829 |
// Propagate out from the boundary nodes |
| 830 |
std::vector< const FEMTreeNode * > interiorNodes , exteriorNodes; |
| 831 |
std::vector< std::vector< const FEMTreeNode * > > _interiorNodes( ThreadPool::NumThreads() ) , _exteriorNodes( ThreadPool::NumThreads() ); |
| 832 |
|
| 833 |
std::vector< NeighborKey > neighborKeys( ThreadPool::NumThreads() ); |
| 834 |
for( int i=0 ; i<neighborKeys.size() ; i++ ) neighborKeys[i].set( maxDepth ); |
| 835 |
|
| 836 |
// In the first pass, flood-fill from the geometry-containing nodes |
| 837 |
ThreadPool::ParallelFor( 0 , nodeSimplices.size() , [&]( unsigned int thread , size_t i ) |
| 838 |
{ |
| 839 |
std::vector< const FEMTreeNode * > &interiorNodes = _interiorNodes[thread]; |
| 840 |
std::vector< const FEMTreeNode * > &exteriorNodes = _exteriorNodes[thread]; |
| 841 |
NeighborKey &neighborKey = neighborKeys[thread]; |
| 842 |
Point< Real , Dim > center ; Real width; |
| 843 |
nodeSimplices[i].node->centerAndWidth( center , width ); |
| 844 |
Neighbors &neighbors = neighborKey.getNeighbors( nodeSimplices[i].node ); |
| 845 |
|
| 846 |
// Iterate over the faces |
| 847 |
for( unsigned int d=0 ; d<Dim ; d++ ) for( int dir=0 ; dir<=2 ; dir+=2 ) |
| 848 |
{ |
| 849 |
unsigned int idx[Dim]; |
| 850 |
for( unsigned int d=0 ; d<Dim ; d++ ) idx[d] = 1; |
| 851 |
idx[d] = dir; |
| 852 |
|
| 853 |
// Terminate early if the neighbor node exists but contains geometry |
| 854 |
const FEMTreeNode *node = neighbors.neighbors( idx ); |
| 855 |
if( node && geometryNodeDesignators[node]==GeometryNodeType::BOUNDARY ) continue; |
| 856 |
|
| 857 |
// Compute the center of the face |
| 858 |
Point< Real , Dim > p = center; |
| 859 |
p[d] += (Real)(dir-1) * width / 2; |
| 860 |
|
| 861 |
// If the center of the face is outside and the face-adjacent node does not contain geometry, add the face-adjacent neighbor to the list of exterior nodes |
| 862 |
std::vector< Simplex< Real , Dim , Dim-1 > > _simplices( nodeSimplices[i].data.size() ); |
| 863 |
std::vector< Point< Real , Dim > > _normals( nodeSimplices[i].data.size() ); |
| 864 |
for( int j=0 ; j<nodeSimplices[i].data.size() ; j++ ) |
| 865 |
{ |
| 866 |
_simplices[j] = nodeSimplices[i].data[j].second; |
| 867 |
_normals[j] = normals[ nodeSimplices[i].data[j].first ]; |
| 868 |
} |
| 869 |
|
| 870 |
bool interior = Simplex< Real , Dim , Dim-1 >::IsInterior( p , _simplices , _normals ); |
| 871 |
{ |
| 872 |
const FEMTreeNode *node = neighbors.neighbors( idx ); |
| 873 |
|
| 874 |
// If the face-adjacent node exists and does not contain geometry, add it to the list of exterior nodes |
| 875 |
if( node ) |
| 876 |
{ |
| 877 |
if( geometryNodeDesignators[node]==GeometryNodeType::UNKNOWN ) |
| 878 |
if( interior ) interiorNodes.push_back( node ); |
| 879 |
else exteriorNodes.push_back( node ); |
| 880 |
} |
| 881 |
// Otherwise, try the parents' face-adjacent neighbors |
| 882 |
else |
| 883 |
{ |
| 884 |
for( int depth=maxDepth-1 ; depth>=0 ; depth-- ) |
| 885 |
{ |
| 886 |
node = neighborKey.neighbors[depth].neighbors( idx ); |
| 887 |
if( node ) |
| 888 |
{ |
| 889 |
if( geometryNodeDesignators[node]==GeometryNodeType::UNKNOWN ) |
| 890 |
if( interior ) interiorNodes.push_back( node ); |
| 891 |
else exteriorNodes.push_back( node ); |
| 892 |
break; |
| 893 |
} |
| 894 |
} |
| 895 |
} |
| 896 |
} |
| 897 |
} |
| 898 |
} ); |
| 899 |
|
| 900 |
// Merge the exterior nodes computed by the different threads and mark them exterior |
| 901 |
{ |
| 902 |
size_t interiorCount = 0 , exteriorCount = 0;; |
| 903 |
for( int i=0 ; i<_interiorNodes.size() ; i++ ) interiorCount += _interiorNodes[i].size(); |
| 904 |
for( int i=0 ; i<_exteriorNodes.size() ; i++ ) exteriorCount += _exteriorNodes[i].size(); |
| 905 |
interiorNodes.reserve( interiorCount ) , exteriorNodes.reserve( exteriorCount ); |
| 906 |
for( int i=0 ; i<_interiorNodes.size() ; i++ ) for( int j=0 ; j<_interiorNodes[i].size() ; j++ ) |
| 907 |
{ |
| 908 |
if( geometryNodeDesignators[ _interiorNodes[i][j] ]==GeometryNodeType::BOUNDARY ) MK_THROW( "Interior node has geometry" ); |
| 909 |
else if( geometryNodeDesignators[ _interiorNodes[i][j] ]==GeometryNodeType::UNKNOWN ) |
| 910 |
{ |
| 911 |
geometryNodeDesignators[ _interiorNodes[i][j] ] = GeometryNodeType::INTERIOR; |
| 912 |
interiorNodes.push_back( _interiorNodes[i][j] ); |
| 913 |
} |
| 914 |
} |
| 915 |
for( int i=0 ; i<_exteriorNodes.size() ; i++ ) for( int j=0 ; j<_exteriorNodes[i].size() ; j++ ) |
| 916 |
{ |
| 917 |
if( geometryNodeDesignators[ _exteriorNodes[i][j] ]==GeometryNodeType::BOUNDARY ) MK_THROW( "Exterior node has geometry" ); |
| 918 |
else if( geometryNodeDesignators[ _exteriorNodes[i][j] ]==GeometryNodeType::UNKNOWN ) |
| 919 |
{ |
| 920 |
geometryNodeDesignators[ _exteriorNodes[i][j] ] = GeometryNodeType::EXTERIOR; |
| 921 |
exteriorNodes.push_back( _exteriorNodes[i][j] ); |
| 922 |
} |
| 923 |
} |
| 924 |
} |
| 925 |
|
| 926 |
// In subsequent passes, propagate from nodes marked as interior/exterior |
| 927 |
while( interiorNodes.size() || exteriorNodes.size() ) |
| 928 |
{ |
| 929 |
for( int i=0 ; i<_interiorNodes.size() ; i++ ) _interiorNodes[i].resize( 0 ); |
| 930 |
for( int i=0 ; i<_exteriorNodes.size() ; i++ ) _exteriorNodes[i].resize( 0 ); |
| 931 |
|
| 932 |
ThreadPool::ParallelFor( 0 , interiorNodes.size() , [&]( unsigned int thread , size_t i ) |
| 933 |
{ |
| 934 |
std::vector< const FEMTreeNode * > &__interiorNodes = _interiorNodes[thread]; |
| 935 |
NeighborKey &neighborKey = neighborKeys[thread]; |
| 936 |
Neighbors &neighbors = neighborKey.getNeighbors( interiorNodes[i] ); |
| 937 |
|
| 938 |
// Iterate over the faces |
| 939 |
for( unsigned int d=0 ; d<Dim ; d++ ) for( int dir=0 ; dir<=2 ; dir+=2 ) |
| 940 |
{ |
| 941 |
unsigned int idx[Dim]; |
| 942 |
for( unsigned int _d=0 ; _d<Dim ; _d++ ) idx[_d] = 1; |
| 943 |
idx[d] = dir; |
| 944 |
|
| 945 |
for( int depth=interiorNodes[i]->depth() ; depth>=0 ; depth-- ) |
| 946 |
{ |
| 947 |
const FEMTreeNode *node = neighborKey.neighbors[depth].neighbors( idx ); |
| 948 |
if( node ) |
| 949 |
{ |
| 950 |
if( geometryNodeDesignators[node]==GeometryNodeType::UNKNOWN ) __interiorNodes.push_back( node ); |
| 951 |
break; |
| 952 |
} |
| 953 |
} |
| 954 |
} |
| 955 |
} ); |
| 956 |
|
| 957 |
ThreadPool::ParallelFor( 0 , exteriorNodes.size() , [&]( unsigned int thread , size_t i ) |
| 958 |
{ |
| 959 |
std::vector< const FEMTreeNode * > &__exteriorNodes = _exteriorNodes[thread]; |
| 960 |
NeighborKey &neighborKey = neighborKeys[thread]; |
| 961 |
Neighbors &neighbors = neighborKey.getNeighbors( exteriorNodes[i] ); |
| 962 |
|
| 963 |
// Iterate over the faces |
| 964 |
for( unsigned int d=0 ; d<Dim ; d++ ) for( int dir=0 ; dir<=2 ; dir+=2 ) |
| 965 |
{ |
| 966 |
unsigned int idx[Dim]; |
| 967 |
for( unsigned int _d=0 ; _d<Dim ; _d++ ) idx[_d] = 1; |
| 968 |
idx[d] = dir; |
| 969 |
|
| 970 |
for( int depth=exteriorNodes[i]->depth() ; depth>=0 ; depth-- ) |
| 971 |
{ |
| 972 |
const FEMTreeNode *node = neighborKey.neighbors[depth].neighbors( idx ); |
| 973 |
if( node ) |
| 974 |
{ |
| 975 |
if( geometryNodeDesignators[node]==GeometryNodeType::UNKNOWN ) __exteriorNodes.push_back( node ); |
| 976 |
break; |
| 977 |
} |
| 978 |
} |
| 979 |
} |
| 980 |
} ); |
| 981 |
|
| 982 |
// Merge the interior/exterior nodes computed by the different threads |
| 983 |
{ |
| 984 |
size_t interiorCount = 0 , exteriorCount = 0; |
| 985 |
for( int i=0 ; i<_interiorNodes.size() ; i++ ) interiorCount += _interiorNodes[i].size(); |
| 986 |
for( int i=0 ; i<_exteriorNodes.size() ; i++ ) exteriorCount += _exteriorNodes[i].size(); |
| 987 |
if( !interiorCount && !exteriorCount ) break; |
| 988 |
interiorNodes.resize( 0 ) , exteriorNodes.resize( 0 ); |
| 989 |
interiorNodes.reserve( interiorCount ) , exteriorNodes.reserve( exteriorCount ); |
| 990 |
|
| 991 |
for( int i=0 ; i<_interiorNodes.size() ; i++ ) for( int j=0 ; j<_interiorNodes[i].size() ; j++ ) |
| 992 |
{ |
| 993 |
if( geometryNodeDesignators[ _interiorNodes[i][j] ]==GeometryNodeType::BOUNDARY ) MK_THROW( "Interior node has geometry" ); |
| 994 |
else if( geometryNodeDesignators[ _interiorNodes[i][j] ]==GeometryNodeType::UNKNOWN ) |
| 995 |
{ |
| 996 |
geometryNodeDesignators[ _interiorNodes[i][j] ] = GeometryNodeType::INTERIOR; |
| 997 |
interiorNodes.push_back( _interiorNodes[i][j] ); |
| 998 |
} |
| 999 |
} |
| 1000 |
for( int i=0 ; i<_exteriorNodes.size() ; i++ ) for( int j=0 ; j<_exteriorNodes[i].size() ; j++ ) |
| 1001 |
{ |
| 1002 |
if( geometryNodeDesignators[ _exteriorNodes[i][j] ]==GeometryNodeType::BOUNDARY ) MK_THROW( "Exterior node has geometry" ); |
| 1003 |
else if( geometryNodeDesignators[ _exteriorNodes[i][j] ]==GeometryNodeType::UNKNOWN ) |
| 1004 |
{ |
| 1005 |
geometryNodeDesignators[ _exteriorNodes[i][j] ] = GeometryNodeType::EXTERIOR; |
| 1006 |
exteriorNodes.push_back( _exteriorNodes[i][j] ); |
| 1007 |
} |
| 1008 |
} |
| 1009 |
} |
| 1010 |
} |
| 1011 |
|
| 1012 |
size_t correctionCount=0; |
| 1013 |
std::function< void ( FEMTreeNode * ) > CorrectDesignatorsFromChildren = [&]( const FEMTreeNode *node ) |
| 1014 |
{ |
| 1015 |
if( node->children ) |
| 1016 |
{ |
| 1017 |
int interiorCount=0 , exteriorCount=0 , boundaryCount=0; |
| 1018 |
for( int c=0 ; c<(1<<Dim) ; c++ ) |
| 1019 |
{ |
| 1020 |
CorrectDesignatorsFromChildren( node->children+c ); |
| 1021 |
if ( geometryNodeDesignators[node->children+c]==GeometryNodeType::INTERIOR ) interiorCount++; |
| 1022 |
else if( geometryNodeDesignators[node->children+c]==GeometryNodeType::EXTERIOR ) exteriorCount++; |
| 1023 |
else if( geometryNodeDesignators[node->children+c]==GeometryNodeType::BOUNDARY ) boundaryCount++; |
| 1024 |
} |
| 1025 |
if( boundaryCount || ( exteriorCount && interiorCount ) ) |
| 1026 |
{ |
| 1027 |
if( geometryNodeDesignators[node]!=GeometryNodeType::UNKNOWN && geometryNodeDesignators[node]!=GeometryNodeType::BOUNDARY ) correctionCount++; |
| 1028 |
geometryNodeDesignators[node] = GeometryNodeType::BOUNDARY; |
| 1029 |
} |
| 1030 |
else if( interiorCount==(1<<Dim) ) |
| 1031 |
{ |
| 1032 |
if( geometryNodeDesignators[node]!=GeometryNodeType::UNKNOWN && geometryNodeDesignators[node]!=GeometryNodeType::INTERIOR ) correctionCount++; |
| 1033 |
geometryNodeDesignators[node] = GeometryNodeType::INTERIOR; |
| 1034 |
} |
| 1035 |
else if( exteriorCount==(1<<Dim) ) |
| 1036 |
{ |
| 1037 |
if( geometryNodeDesignators[node]!=GeometryNodeType::UNKNOWN && geometryNodeDesignators[node]!=GeometryNodeType::EXTERIOR ) correctionCount++; |
| 1038 |
geometryNodeDesignators[node] = GeometryNodeType::EXTERIOR; |
| 1039 |
} |
| 1040 |
else if( interiorCount ) |
| 1041 |
{ |
| 1042 |
if( geometryNodeDesignators[node]!=GeometryNodeType::UNKNOWN && geometryNodeDesignators[node]!=GeometryNodeType::INTERIOR && geometryNodeDesignators[node]!=GeometryNodeType::BOUNDARY ) correctionCount++; |
| 1043 |
geometryNodeDesignators[node] = GeometryNodeType::BOUNDARY; |
| 1044 |
} |
| 1045 |
else if( exteriorCount ) |
| 1046 |
{ |
| 1047 |
if( geometryNodeDesignators[node]!=GeometryNodeType::UNKNOWN && geometryNodeDesignators[node]!=GeometryNodeType::EXTERIOR && geometryNodeDesignators[node]!=GeometryNodeType::BOUNDARY ) correctionCount++; |
| 1048 |
geometryNodeDesignators[node] = GeometryNodeType::BOUNDARY; |
| 1049 |
} |
| 1050 |
} |
| 1051 |
}; |
| 1052 |
CorrectDesignatorsFromChildren( root ); |
| 1053 |
if( correctionCount ) MK_WARN( "Adjusted designator inconsistencies: " , correctionCount ); |
| 1054 |
|
| 1055 |
std::function< void ( FEMTreeNode * ) > SetUnknownDesignatorsFromParents = [&]( FEMTreeNode *node ) |
| 1056 |
{ |
| 1057 |
if( geometryNodeDesignators[node]==GeometryNodeType::UNKNOWN && node->parent ) geometryNodeDesignators[node] = geometryNodeDesignators[node->parent]; |
| 1058 |
if( node->children ) for( int c=0 ; c<(1<<Dim) ; c++ ) SetUnknownDesignatorsFromParents( node->children + c ); |
| 1059 |
}; |
| 1060 |
std::function< void ( FEMTreeNode * ) > SetUnknownDesignatorsFromChildren = [&]( FEMTreeNode *node ) |
| 1061 |
{ |
| 1062 |
if( node->children ) for( int c=0 ; c<(1<<Dim) ; c++ ) SetUnknownDesignatorsFromChildren( node->children + c ); |
| 1063 |
if( geometryNodeDesignators[node]==GeometryNodeType::UNKNOWN ) |
| 1064 |
if( node->children ) |
| 1065 |
{ |
| 1066 |
int interiorCount = 0 , exteriorCount = 0 , boundaryCount = 0; |
| 1067 |
for( int c=0 ; c<(1<<Dim) ; c++ ) |
| 1068 |
{ |
| 1069 |
if ( geometryNodeDesignators[node->children+c]==GeometryNodeType::INTERIOR ) interiorCount++; |
| 1070 |
else if( geometryNodeDesignators[node->children+c]==GeometryNodeType::EXTERIOR ) exteriorCount++; |
| 1071 |
else if( geometryNodeDesignators[node->children+c]==GeometryNodeType::BOUNDARY ) boundaryCount++; |
| 1072 |
} |
| 1073 |
if( interiorCount+exteriorCount+boundaryCount!=(1<<Dim) ) MK_THROW( "Children are unknown" ); |
| 1074 |
else if( boundaryCount==0 && interiorCount!=0 && exteriorCount!=0 ) MK_THROW( "Expected boundary between interior/exterior" ); |
| 1075 |
else if( boundaryCount!=0 ) geometryNodeDesignators[node] = GeometryNodeType::BOUNDARY; |
| 1076 |
else if( interiorCount!=0 ) geometryNodeDesignators[node] = GeometryNodeType::INTERIOR; |
| 1077 |
else if( exteriorCount!=0 ) geometryNodeDesignators[node] = GeometryNodeType::INTERIOR; |
| 1078 |
} |
| 1079 |
else if( geometryNodeDesignators[node]==GeometryNodeType::UNKNOWN ) MK_THROW( "Leaf node is unknown" ); |
| 1080 |
}; |
| 1081 |
SetUnknownDesignatorsFromParents( root ); |
| 1082 |
SetUnknownDesignatorsFromChildren( root ); |
| 1083 |
|
| 1084 |
return geometryNodeDesignators; |
| 1085 |
} |
| 1086 |
|
| 1087 |
template< unsigned int Dim , class Real > |
| 1088 |
void FEMTreeInitializer< Dim , Real >::TestGeometryNodeDesignators( const FEMTreeNode *root , const DenseNodeData< typename FEMTreeInitializer< Dim , Real >::GeometryNodeType , IsotropicUIntPack< Dim , FEMTrivialSignature > > &geometryNodeDesignators ) |
| 1089 |
{ |
| 1090 |
std::function< void ( const FEMTreeNode * ) > Test = [&]( const FEMTreeNode *node ) |
| 1091 |
{ |
| 1092 |
if( node->children ) |
| 1093 |
{ |
| 1094 |
if( node->nodeData.nodeIndex>=0 && node->nodeData.nodeIndex<(node_index_type)geometryNodeDesignators.size() && geometryNodeDesignators[node->nodeData.nodeIndex]!=GeometryNodeType::UNKNOWN ) |
| 1095 |
{ |
| 1096 |
GeometryNodeType type = geometryNodeDesignators[node->nodeData.nodeIndex]; |
| 1097 |
int interiorCount=0 , exteriorCount=0 , boundaryCount=0 , unknownCount=0; |
| 1098 |
for( int c=0 ; c<(1<<Dim) ; c++ ) |
| 1099 |
{ |
| 1100 |
if( node->children[c].nodeData.nodeIndex>=0 && node->children[c].nodeData.nodeIndex<(node_index_type)geometryNodeDesignators.size() ) |
| 1101 |
{ |
| 1102 |
if( geometryNodeDesignators[ node->children[c].nodeData.nodeIndex ]==GeometryNodeType::UNKNOWN ) unknownCount++; |
| 1103 |
if( geometryNodeDesignators[ node->children[c].nodeData.nodeIndex ]==GeometryNodeType::INTERIOR ) interiorCount++; |
| 1104 |
if( geometryNodeDesignators[ node->children[c].nodeData.nodeIndex ]==GeometryNodeType::EXTERIOR ) exteriorCount++; |
| 1105 |
if( geometryNodeDesignators[ node->children[c].nodeData.nodeIndex ]==GeometryNodeType::BOUNDARY ) boundaryCount++; |
| 1106 |
} |
| 1107 |
} |
| 1108 |
if( boundaryCount || ( interiorCount && exteriorCount ) ) |
| 1109 |
{ |
| 1110 |
if( type!=GeometryNodeType::UNKNOWN && type!=GeometryNodeType::BOUNDARY ) MK_THROW( "Expected unknown or boundary, got: " , type , " | " , node->depthAndOffset() ); |
| 1111 |
} |
| 1112 |
else if( interiorCount==(1<<Dim) ) |
| 1113 |
{ |
| 1114 |
if( type!=GeometryNodeType::UNKNOWN && type!=GeometryNodeType::INTERIOR ) MK_THROW( "Expected unknown or interior, got: " , type , " | " , node->depthAndOffset() ); |
| 1115 |
} |
| 1116 |
else if( exteriorCount==(1<<Dim) ) |
| 1117 |
{ |
| 1118 |
if( type!=GeometryNodeType::UNKNOWN && type!=GeometryNodeType::EXTERIOR ) MK_THROW( "Expected unknown or exterior, got: " , type , " | " , node->depthAndOffset() ); |
| 1119 |
} |
| 1120 |
else if( interiorCount ) |
| 1121 |
{ |
| 1122 |
if( type!=GeometryNodeType::UNKNOWN && type!=GeometryNodeType::INTERIOR && type!=GeometryNodeType::BOUNDARY ) MK_THROW( "Expected unknown, interior , or boundary, got: " , type , " | " , node->depthAndOffset() ); |
| 1123 |
} |
| 1124 |
else if( exteriorCount==(1<<Dim) ) |
| 1125 |
{ |
| 1126 |
if( type!=GeometryNodeType::UNKNOWN && type!=GeometryNodeType::EXTERIOR && type!=GeometryNodeType::BOUNDARY ) MK_THROW( "Expected unknown, exterior, or boundary, got: " , type , " | " , node->depthAndOffset() ); |
| 1127 |
} |
| 1128 |
} |
| 1129 |
|
| 1130 |
for( int c=0 ; c<(1<<Dim) ; c++ ) Test( node->children+c ); |
| 1131 |
} |
| 1132 |
}; |
| 1133 |
|
| 1134 |
Test( root ); |
| 1135 |
} |
| 1136 |
|
| 1137 |
template< unsigned int Dim , class Real > |
| 1138 |
void FEMTreeInitializer< Dim , Real >::PushGeometryNodeDesignatorsToFiner( const FEMTreeNode *root , DenseNodeData< typename FEMTreeInitializer< Dim , Real >::GeometryNodeType , IsotropicUIntPack< Dim , FEMTrivialSignature > > &geometryNodeDesignators , unsigned int maxDepth ) |
| 1139 |
{ |
| 1140 |
std::function< void ( const FEMTreeNode * ) > Push = [&]( const FEMTreeNode *node ) |
| 1141 |
{ |
| 1142 |
if( node->nodeData.nodeIndex>=0 && node->nodeData.nodeIndex<(node_index_type)geometryNodeDesignators.size() ) |
| 1143 |
{ |
| 1144 |
if( geometryNodeDesignators[node]==GeometryNodeType::UNKNOWN ) |
| 1145 |
if( node!=root ) geometryNodeDesignators[node] = geometryNodeDesignators[node->parent]; |
| 1146 |
else MK_THROW( "Root node should not be unknown" ); |
| 1147 |
else if( node!=root && geometryNodeDesignators[node]!=geometryNodeDesignators[node->parent] && geometryNodeDesignators[node->parent]!=GeometryNodeType::BOUNDARY ) |
| 1148 |
{ |
| 1149 |
int d , off[Dim]; |
| 1150 |
node->depthAndOffset( d , off ); |
| 1151 |
MK_THROW( "Child designator does not match parent: " , geometryNodeDesignators[node] , " != " , geometryNodeDesignators[node->parent] , " | " , d , " @ ( " , off[0] , " , " , off[1] , " , " , off[2] , " ) " ); |
| 1152 |
} |
| 1153 |
if( node->depth()<(long long)maxDepth && node->children ) for( int c=0 ; c<(1<<Dim) ; c++ ) Push( node->children+c ); |
| 1154 |
} |
| 1155 |
}; |
| 1156 |
|
| 1157 |
Push( root ); |
| 1158 |
} |
| 1159 |
|
| 1160 |
template< unsigned int Dim , class Real > |
| 1161 |
void FEMTreeInitializer< Dim , Real >::PullGeometryNodeDesignatorsFromFiner( const FEMTreeNode *root , DenseNodeData< typename FEMTreeInitializer< Dim , Real >::GeometryNodeType , IsotropicUIntPack< Dim , FEMTrivialSignature > > &geometryNodeDesignators , unsigned int maxDepth ) |
| 1162 |
{ |
| 1163 |
std::function< void ( const FEMTreeNode * ) > Pull = [&]( const FEMTreeNode *node ) |
| 1164 |
{ |
| 1165 |
if( node->nodeData.nodeIndex>=0 && node->nodeData.nodeIndex<(node_index_type)geometryNodeDesignators.size() ) |
| 1166 |
{ |
| 1167 |
if( node->depth()<(long long)maxDepth && node->children && node->children->nodeData.nodeIndex>=0 && node->children->nodeData.nodeIndex<(node_index_type)geometryNodeDesignators.size() ) |
| 1168 |
{ |
| 1169 |
size_t interiorCount = 0 , exteriorCount = 0; |
| 1170 |
for( int c=0 ; c<(1<<Dim) ; c++ ) |
| 1171 |
{ |
| 1172 |
Pull( node->children+c ); |
| 1173 |
if ( geometryNodeDesignators[ node->children+c ]==GeometryNodeType::EXTERIOR ) exteriorCount++; |
| 1174 |
else if( geometryNodeDesignators[ node->children+c ]==GeometryNodeType::INTERIOR ) interiorCount++; |
| 1175 |
} |
| 1176 |
if ( interiorCount==(1<<Dim) ) geometryNodeDesignators[node] = GeometryNodeType::INTERIOR; |
| 1177 |
else if( exteriorCount==(1<<Dim) ) geometryNodeDesignators[node] = GeometryNodeType::EXTERIOR; |
| 1178 |
else geometryNodeDesignators[node] = GeometryNodeType::BOUNDARY; |
| 1179 |
} |
| 1180 |
else if( geometryNodeDesignators[node]==GeometryNodeType::UNKNOWN ) MK_THROW( "Should not have unknown nodes" ); |
| 1181 |
} |
| 1182 |
}; |
| 1183 |
Pull( root ); |
| 1184 |
} |