/* Copyright (c) 2016, Michael Kazhdan All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. Neither the name of the Johns Hopkins University nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ //////////////////////// // FEMTreeInitializer // //////////////////////// template< unsigned int Dim , class Real > int FEMTreeInitializer< Dim , Real >::Initialize( FEMTreeNode& node , int maxDepth , std::function< bool ( int , int[] ) > Refine , Allocator< FEMTreeNode >* nodeAllocator , std::function< void ( FEMTreeNode& ) > NodeInitializer ) { int count = 0; int d , off[3]; node.depthAndOffset( d , off ); if( node.depth() int FEMTreeInitializer< Dim , Real >::Initialize( FEMTreeNode& root , InputPointStream< Real , Dim >& pointStream , int maxDepth , std::vector< PointSample >& samplePoints , Allocator< FEMTreeNode >* nodeAllocator , std::function< void ( FEMTreeNode& ) > NodeInitializer ) { auto Leaf = [&]( FEMTreeNode& root , Point< Real , Dim > p , int maxDepth ) { for( int d=0 ; d1 ) return (FEMTreeNode*)NULL; Point< Real , Dim > center; for( int d=0 ; dchildren ) node->initChildren( nodeAllocator , NodeInitializer ); int cIndex = FEMTreeNode::ChildIndex( center , p ); node = node->children + cIndex; d++; width /= 2; for( int dd=0 ; dd>dd) & 1 ) center[dd] += width/2; else center[dd] -= width/2; } return node; }; // Add the point data int outOfBoundPoints = 0 , pointCount = 0; { std::vector< int > nodeToIndexMap; Point< Real , Dim > p; while( pointStream.nextPoint( p ) ) { Real weight = (Real)1.; FEMTreeNode* temp = Leaf( root , p , maxDepth ); if( !temp ){ outOfBoundPoints++ ; continue; } int nodeIndex = temp->nodeData.nodeIndex; if( nodeIndex>=nodeToIndexMap.size() ) nodeToIndexMap.resize( nodeIndex+1 , -1 ); int idx = nodeToIndexMap[ nodeIndex ]; if( idx==-1 ) { idx = (int)samplePoints.size(); nodeToIndexMap[ nodeIndex ] = idx; samplePoints.resize( idx+1 ) , samplePoints[idx].node = temp; } samplePoints[idx].sample += ProjectiveData< Point< Real , Dim > , Real >( p*weight , weight ); pointCount++; } pointStream.reset(); } if( outOfBoundPoints ) WARN( "Found out-of-bound points: %d" , outOfBoundPoints ); FEMTree< Dim , Real >::MemoryUsage(); return pointCount; } template< unsigned int Dim , class Real > template< class Data > int FEMTreeInitializer< Dim , Real >::Initialize( FEMTreeNode& root , InputPointStreamWithData< Real , Dim , Data >& pointStream , int maxDepth , std::vector< PointSample >& samplePoints , std::vector< Data >& sampleData , bool mergeNodeSamples , Allocator< FEMTreeNode >* nodeAllocator , std::function< void ( FEMTreeNode& ) > NodeInitializer , std::function< Real ( const Point< Real , Dim >& , Data& ) > ProcessData ) { auto Leaf = [&]( FEMTreeNode& root , Point< Real , Dim > p , int maxDepth ) { for( int d=0 ; d1 ) return (FEMTreeNode*)NULL; Point< Real , Dim > center; for( int d=0 ; dchildren ) node->initChildren( nodeAllocator , NodeInitializer ); int cIndex = FEMTreeNode::ChildIndex( center , p ); node = node->children + cIndex; d++; width /= 2; for( int dd=0 ; dd>dd) & 1 ) center[dd] += width/2; else center[dd] -= width/2; } return node; }; // Add the point data int outOfBoundPoints = 0 , badData = 0 , pointCount = 0; { std::vector< int > nodeToIndexMap; Point< Real , Dim > p; Data d; while( pointStream.nextPoint( p , d ) ) { Real weight = ProcessData( p , d ); if( weight<=0 ){ badData++ ; continue; } FEMTreeNode* temp = Leaf( root , p , maxDepth ); if( !temp ){ outOfBoundPoints++ ; continue; } int nodeIndex = temp->nodeData.nodeIndex; if( mergeNodeSamples ) { if( nodeIndex>=nodeToIndexMap.size() ) nodeToIndexMap.resize( nodeIndex+1 , -1 ); int idx = nodeToIndexMap[ nodeIndex ]; if( idx==-1 ) { idx = (int)samplePoints.size(); nodeToIndexMap[ nodeIndex ] = idx; samplePoints.resize( idx+1 ) , samplePoints[idx].node = temp; sampleData.resize( idx+1 ); } samplePoints[idx].sample += ProjectiveData< Point< Real , Dim > , Real >( p*weight , weight ); sampleData[ idx ] += d*weight; } else { int idx = (int)samplePoints.size(); samplePoints.resize( idx+1 ) , sampleData.resize( idx+1 ); samplePoints[idx].node = temp; samplePoints[idx].sample = ProjectiveData< Point< Real , Dim > , Real >( p*weight , weight ); sampleData[ idx ] = d*weight; } pointCount++; } pointStream.reset(); } if( outOfBoundPoints ) WARN( "Found out-of-bound points: %d" , outOfBoundPoints ); if( badData ) WARN( "Found bad data: %d" , badData ); FEMTree< Dim , Real >::MemoryUsage(); return pointCount; } template< unsigned int Dim , class Real > void FEMTreeInitializer< Dim , Real >::Initialize( FEMTreeNode& root , const std::vector< Point< Real , Dim > >& vertices , const std::vector< SimplexIndex< Dim-1 > >& simplices , int maxDepth , std::vector< PointSample >& samples , bool mergeNodeSamples , Allocator< FEMTreeNode >* nodeAllocator , std::function< void ( FEMTreeNode& ) > NodeInitializer ) { std::vector< int > nodeToIndexMap; #pragma omp parallel for for( int i=0 ; i s; for( int k=0 ; k::MemoryUsage(); } template< unsigned int Dim , class Real > int FEMTreeInitializer< Dim , Real >::_AddSimplex( FEMTreeNode& root , Simplex< Real , Dim , Dim-1 >& s , int maxDepth , std::vector< PointSample >& samples , std::vector< int >* nodeToIndexMap , Allocator< FEMTreeNode >* nodeAllocator , std::function< void ( FEMTreeNode& ) > NodeInitializer ) { std::vector< Simplex< Real , Dim , Dim-1 > > subSimplices; subSimplices.push_back( s ); // Clip the simplex to the unit cube { for( int d=0 ; d n; n[d] = 1; { std::vector< Simplex< Real , Dim , Dim-1 > > back , front; for( int i=0 ; i > back , front; for( int i=0 ; i p , int maxDepth ) { for( int d=0 ; d1 ) return (FEMTreeNode*)NULL; Point< Real , Dim > center; for( int d=0 ; dchildren ) node->initChildren( nodeAllocator , NodeInitializer ); int cIndex = FEMTreeNode::ChildIndex( center , p ); node = node->children + cIndex; d++; width /= 2; for( int d=0 ; d>d) & 1 ) center[d] += width/2; else center[d] -= width/2; } return node; }; int sCount = 0; for( int i=0 ; i int FEMTreeInitializer< Dim , Real >::_AddSimplex( FEMTreeNode* node , Simplex< Real , Dim , Dim-1 >& s , int maxDepth , std::vector< PointSample >& samples , std::vector< int >* nodeToIndexMap , Allocator< FEMTreeNode >* nodeAllocator , std::function< void ( FEMTreeNode& ) > NodeInitializer ) { int d = node->depth(); if( d==maxDepth ) { Real weight = s.measure(); Point< Real , Dim > position = s.center() , normal; { Point< Real , Dim > v[Dim-1]; for( int k=0 ; k::CrossProduct( v ); } if( weight && weight==weight ) { if( nodeToIndexMap ) { int nodeIndex = node->nodeData.nodeIndex; #pragma omp critical { if( nodeIndex>=nodeToIndexMap->size() ) nodeToIndexMap->resize( nodeIndex+1 , -1 ); int idx = (*nodeToIndexMap)[ nodeIndex ]; if( idx==-1 ) { idx = (int)samples.size(); (*nodeToIndexMap)[ nodeIndex ] = idx; samples.resize( idx+1 ); samples[idx].node = node; } samples[idx].sample += ProjectiveData< Point< Real , Dim > , Real >( position*weight , weight ); } } else { #pragma omp critical { int idx = (int)samples.size(); samples.resize( idx+1 ); samples[idx].node = node; samples[idx].sample = ProjectiveData< Point< Real , Dim > , Real >( position*weight , weight ); } } } return 1; } else { int sCount = 0; #pragma omp critical if( !node->children ) node->initChildren( nodeAllocator , NodeInitializer ); // Split up the simplex and pass the parts on to the children Point< Real , Dim > center; Real width; node->centerAndWidth( center , width ); std::vector< std::vector< Simplex< Real , Dim , Dim-1 > > > childSimplices( 1 ); childSimplices[0].push_back( s ); for( int d=0 ; d n ; n[Dim-d-1] = 1; std::vector< std::vector< Simplex< Real , Dim , Dim-1 > > > temp( (int)( 1<<(d+1) ) ); for( int c=0 ; c<(1<children+c , childSimplices[c][i] , maxDepth , samples , nodeToIndexMap , nodeAllocator , NodeInitializer ); return sCount; } } template< unsigned int Dim , class Real > void FEMTreeInitializer< Dim , Real >::Initialize( FEMTreeNode& root , const std::vector< Point< Real , Dim > >& vertices , const std::vector< SimplexIndex< Dim-1 > >& simplices , int maxDepth , std::vector< NodeSimplices< Dim , Real > >& nodeSimplices , Allocator< FEMTreeNode >* nodeAllocator , std::function< void ( FEMTreeNode& ) > NodeInitializer ) { std::vector< int > nodeToIndexMap; for( int i=0 ; i s; for( int k=0 ; k::MemoryUsage(); } template< unsigned int Dim , class Real > int FEMTreeInitializer< Dim , Real >::_AddSimplex( FEMTreeNode& root , Simplex< Real , Dim , Dim-1 >& s , int maxDepth , std::vector< NodeSimplices< Dim , Real > >& simplices , std::vector< int >& nodeToIndexMap , Allocator< FEMTreeNode >* nodeAllocator , std::function< void ( FEMTreeNode& ) > NodeInitializer ) { std::vector< Simplex< Real , Dim , Dim-1 > > subSimplices; subSimplices.push_back( s ); // Clip the simplex to the unit cube { for( int d=0 ; d n; n[d] = 1; { std::vector< Simplex< Real , Dim , Dim-1 > > back , front; for( int i=0 ; i > back , front; for( int i=0 ; i p , int maxDepth ) { for( int d=0 ; d1 ) return (FEMTreeNode*)NULL; Point< Real , Dim > center; for( int d=0 ; dchildren ) node->initChildren( nodeAllocator , NodeInitializer ); int cIndex = FEMTreeNode::ChildIndex( center , p ); node = node->children + cIndex; d++; width /= 2; for( int d=0 ; d>d) & 1 ) center[d] += width/2; else center[d] -= width/2; } return node; }; int sCount = 0; for( int i=0 ; i int FEMTreeInitializer< Dim , Real >::_AddSimplex( FEMTreeNode* node , Simplex< Real , Dim , Dim-1 >& s , int maxDepth , std::vector< NodeSimplices< Dim , Real > >& simplices , std::vector< int >& nodeToIndexMap , Allocator< FEMTreeNode >* nodeAllocator , std::function< void ( FEMTreeNode& ) > NodeInitializer ) { int d = node->depth(); if( d==maxDepth ) { // If the simplex has non-zero size, add it to the list Real weight = s.measure(); if( weight && weight==weight ) { int nodeIndex = node->nodeData.nodeIndex; if( nodeIndex>=nodeToIndexMap.size() ) nodeToIndexMap.resize( nodeIndex+1 , -1 ); int idx = nodeToIndexMap[ nodeIndex ]; if( idx==-1 ) { idx = (int)simplices.size(); nodeToIndexMap[ nodeIndex ] = idx; simplices.resize( idx+1 ); simplices[idx].node = node; } simplices[idx].data.push_back( s ); } return 1; } else { int sCount = 0; if( !node->children ) node->initChildren( nodeAllocator , NodeInitializer ); // Split up the simplex and pass the parts on to the children Point< Real , Dim > center; Real width; node->centerAndWidth( center , width ); std::vector< std::vector< Simplex< Real , Dim , Dim-1 > > > childSimplices( 1 ); childSimplices[0].push_back( s ); for( int d=0 ; d n ; n[Dim-d-1] = 1; std::vector< std::vector< Simplex< Real , Dim , Dim-1 > > > temp( (int)( 1<<(d+1) ) ); for( int c=0 ; c<(1<children+c , childSimplices[c][i] , maxDepth , simplices , nodeToIndexMap , nodeAllocator , NodeInitializer ); return sCount; } } template< unsigned int Dim , class Real > template< class Data , class _Data , bool Dual > int 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 ) { auto Leaf = [&]( FEMTreeNode& root , const int idx[Dim] , int maxDepth ) { for( int d=0 ; d=(1<children ) node->initChildren( nodeAllocator , NodeInitializer ); int cIndex = 0; for( int dd=0 ; ddchildren + cIndex; } return node; }; auto FactorIndex = []( size_t i , const int resolution[Dim] , int idx[Dim] ) { size_t ii = i; for( int d=0 ; d( maxResolution , resolution[d] ); int maxDepth = 0; while( ( (1<=0 && labels[ii]>=0 ) { if( !Dual ) idx[d]--; NodeSample< Dim , _Data > nodeSample; nodeSample.node = Leaf( root , idx , maxDepth ); nodeSample.data = DataConverter( values[ii] ) - DataConverter( values[i] ); if( nodeSample.node ) derivatives[d].push_back( nodeSample ); } } } return maxDepth; } template< unsigned int Dim , class Real > template< bool Dual , class Data > unsigned int FEMTreeInitializer< Dim , Real >::Initialize( FEMTreeNode& root , DerivativeStream< Data >& dStream , std::vector< NodeSample< Dim , Data > > derivatives[Dim] , Allocator< FEMTreeNode >* nodeAllocator , std::function< void ( FEMTreeNode& ) > NodeInitializer ) { // Note: // -- Dual: The difference between [i] and [i+1] is stored at cell [i+1] // -- Primal: The difference between [i] and [i+1] is stored at cell [i] // Find the leaf containing the specified cell index auto Leaf = [&]( FEMTreeNode& root , const unsigned int idx[Dim] , unsigned int maxDepth ) { for( int d=0 ; d=(unsigned int)(1<children ) node->initChildren( nodeAllocator , NodeInitializer ); int cIndex = 0; for( int dd=0 ; ddchildren + cIndex; } return node; }; unsigned int resolution[Dim]; dStream.resolution( resolution ); unsigned int maxResolution = resolution[0]; for( int d=1 ; d( maxResolution , resolution[d] ); unsigned int maxDepth = 0; // If we are using a dual formulation, we need at least maxResolution cells. // Otherwise, we need at least maxResolution-1 cells. while( (unsigned int)( (1< nodeSample; nodeSample.node = Leaf( root , idx , maxDepth ); nodeSample.data = dValue; if( nodeSample.node ) derivatives[dir].push_back( nodeSample ); } return maxDepth; }