Debian Codegraph @

src/thirdparty/PoissonRecon/Extrapolator.h

raw
1 /*
2 Copyright (c) 2024, 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 #ifndef EXTRAPOLATOR_INCLUDED
30 #define EXTRAPOLATOR_INCLUDED
31
32 #include "PreProcessor.h"
33 #include "MyMiscellany.h"
34 #include "FEMTree.h"
35 #include "PointExtent.h"
36 #include "Reconstructors.streams.h"
37
38 namespace PoissonRecon
39 {
40 namespace Extrapolator
41 {
42 static unsigned int ProfilerMS = 20; // The number of ms at which to poll the performance (set to zero for no polling)
43
44 template< typename Real , unsigned int Dim , typename AuxData >
45 using InputStream = Reconstructor::InputSampleStream< Real , Dim , AuxData >;
46
47 // Specialized solution information without auxiliary data
48 template< typename Real , unsigned int Dim , typename AuxData , unsigned int DataDegree=1 >
49 struct Implicit
50 {
51 struct Parameters
52 {
53 bool verbose = false;
54 Real scale = (Real)1.1;
55 Real width = (Real)0.;
56 Real samplesPerNode = (Real)1.5;
57 Real perLevelScaleFactor = (Real)32;
58 unsigned int baseDepth = -1;
59 unsigned int depth = 8;
60 unsigned int fullDepth = 5;
61 unsigned int alignDir = 0;
62 };
63
64 // The constructor
65 Implicit( InputStream< Real , Dim , AuxData > &pointStream , Parameters params , AuxData zero );
66
67 // The desctructor
68 ~Implicit( void ){ delete auxData ; auxData = nullptr ; delete _auxEvaluator ; _auxEvaluator = nullptr; }
69
70 // The transformation taking points in the unit cube back to world coordinates
71 XForm< Real , Dim+1 > unitCubeToModel;
72
73 // The octree adapted to the points
74 FEMTree< Dim , Real > tree;
75
76 // The signature of the finite-element used for data extrapolation
77 static const unsigned int DataSig = FEMDegreeAndBType< DataDegree , BOUNDARY_FREE >::Signature;
78
79 // The auxiliary information stored with the oriented vertices
80 SparseNodeData< ProjectiveData< AuxData , Real > , IsotropicUIntPack< Dim , DataSig > > *auxData;
81
82 // An instance of "zero" AuxData
83 AuxData zeroAuxData;
84
85 struct OutOfUnitCubeException : public std::exception
86 {
87 OutOfUnitCubeException( Point< Real , Dim > world , Point< Real , Dim > cube )
88 {
89 std::stringstream sStream;
90 sStream << "Out-of-unit-cube input: " << world << " -> " << cube;
91 _message = sStream.str();
92 }
93 const char * what( void ) const noexcept { return _message.c_str(); }
94 protected:
95 std::string _message;
96 };
97
98 XForm< Real , Dim+1 > worldToUnitCubeTransform( void ) const { return _worldToUnitCube; }
99
100 AuxData operator()( unsigned int t , Point< Real , Dim > p ){ return _value(t,p); }
101 AuxData operator()( Point< Real , Dim > p ){ return _value(0,p); }
102
103 void evaluate( unsigned int t , Point< Real , Dim > p , AuxData &data ){ return _evaluate( t , p , data ); }
104 void evaluate( Point< Real , Dim > p , AuxData &data ){ return _evaluate( 0 , p , data ); }
105
106 protected:
107 typename FEMTree< Dim , Real >::template MultiThreadedSparseEvaluator< IsotropicUIntPack< Dim , DataSig > , ProjectiveData< AuxData , Real > > *_auxEvaluator = nullptr;
108 XForm< Real , Dim+1 > _worldToUnitCube;
109
110 AuxData _value( unsigned int t , Point< Real , Dim > p )
111 {
112 Point< Real , Dim > q = _worldToUnitCube * p;
113 for( unsigned int d=0 ; d<Dim ; d++ ) if( q[d]<0 || q[d]>1 ) throw OutOfUnitCubeException(p,q);
114 ProjectiveData< AuxData , Real > pData( zeroAuxData );
115 _auxEvaluator->addValue( q , pData , t );
116 return pData.value();
117 }
118
119 void _evaluate( unsigned int t , Point< Real , Dim > p , AuxData &data )
120 {
121 Point< Real , Dim > q = _worldToUnitCube * p;
122 for( unsigned int d=0 ; d<Dim ; d++ ) if( q[d]<0 || q[d]>1 ) throw OutOfUnitCubeException(p,q);
123
124 Real weight = (Real)0.;
125 data *= 0;
126
127 auto Accumulation = [&weight,&data]( const ProjectiveData< AuxData , Real > &pData , Real scale )
128 {
129 weight += pData.weight * scale;
130 data += pData.data * scale;
131 };
132 _auxEvaluator->accumulate( q , Accumulation , t );
133 if( weight ) data /= weight;
134 }
135 };
136
137 template< typename Real , unsigned int Dim , typename AuxData , unsigned int DataDegree >
138 Implicit< Real , Dim , AuxData , DataDegree >::Implicit( InputStream< Real , Dim , AuxData > &pointStream , Parameters params , AuxData zeroAuxData )
139 : tree(MEMORY_ALLOCATOR_BLOCK_SIZE) , unitCubeToModel( XForm< Real , Dim+1 >::Identity() ) , zeroAuxData(zeroAuxData)
140 {
141 // The finite-element tracking tree node
142 using FEMTreeNode = RegularTreeNode< Dim , FEMTreeNodeData , depth_and_offset_type >;
143
144 // The type describing the sampling density
145 using DensityEstimator = typename FEMTree< Dim , Real >::template DensityEstimator< 0 >;
146
147 // The signature for the finite-elements representing the auxiliary data (if it's there)
148 static const unsigned int DataSig = FEMDegreeAndBType< DataDegree , BOUNDARY_FREE >::Signature;
149
150 XForm< Real , Dim+1 > modelToUnitCube = XForm< Real , Dim+1 >::Identity();
151
152 Profiler profiler( ProfilerMS );
153
154 size_t pointCount;
155
156 ProjectiveData< Point< Real , 2 > , Real > pointDepthAndWeight;
157 std::vector< typename FEMTree< Dim , Real >::PointSample > *samples = new std::vector< typename FEMTree< Dim , Real >::PointSample >();
158 std::vector< AuxData > *sampleAuxData = nullptr;
159
160 // Read in the samples (and auxiliary data)
161 {
162 profiler.reset();
163
164 pointStream.reset();
165 sampleAuxData = new std::vector< AuxData >();
166
167 modelToUnitCube = params.scale>0 ? PointExtent::GetXForm< Real , Dim , true , AuxData >( pointStream , zeroAuxData , params.scale , params.alignDir ) * modelToUnitCube : modelToUnitCube;
168 pointStream.reset();
169
170 if( params.width>0 )
171 {
172 // Assuming the transformation is rigid so that the (max) scale can be pulled from the Frobenius norm
173 Real maxScale = 0;
174 for( unsigned int i=0 ; i<Dim ; i++ ) for( unsigned int j=0 ; j<Dim ; j++ ) maxScale += modelToUnitCube(i,j) * modelToUnitCube(i,j);
175 maxScale = (Real)( 1. / sqrt( maxScale / Dim ) );
176 params.depth = (unsigned int)ceil( std::max< double >( 0. , log( maxScale/params.width )/log(2.) ) );
177 }
178
179 {
180 Reconstructor::TransformedInputSampleStream< Real , Dim , AuxData > _pointStream( modelToUnitCube , pointStream );
181 auto ProcessData = []( const Point< Real , Dim > &p , AuxData &d ){ return (Real)1.; };
182 pointCount = FEMTreeInitializer< Dim , Real >::template Initialize< AuxData >( tree.spaceRoot() , _pointStream , zeroAuxData , params.depth , *samples , *sampleAuxData , tree.nodeAllocators.size() ? tree.nodeAllocators[0] : nullptr , tree.initializer() , ProcessData );
183 }
184
185 if( params.fullDepth>params.depth )
186 {
187 if( params.fullDepth!=-1 ) MK_WARN( "Full depth cannot exceed depth: " , params.fullDepth , " <= " , params.depth );
188 params.fullDepth = params.depth;
189 }
190 if( params.baseDepth>params.fullDepth )
191 {
192 if( params.baseDepth!=-1 ) MK_WARN( "Base depth must be smaller than full depth: " , params.baseDepth , " <= " , params.fullDepth );
193 params.baseDepth = params.fullDepth;
194 }
195
196
197 unitCubeToModel = modelToUnitCube.inverse();
198
199 if( params.verbose )
200 {
201 std::cout << "Input Points / Samples: " << pointCount << " / " << samples->size() << std::endl;
202 std::cout << "# Read input into tree: " << profiler << std::endl;
203 }
204 }
205 tree.resetNodeIndices( 0 , std::make_tuple() );
206 {
207 profiler.reset();
208 auto SampleFunctor = [&]( size_t i ) -> const typename FEMTree< Dim , Real >::PointSample & { return (*samples)[i]; };
209 auto SampleDataFunctor = [&]( size_t i ) -> const AuxData & { return (*sampleAuxData)[i]; };
210 auxData = new SparseNodeData< ProjectiveData< AuxData , Real > , IsotropicUIntPack< Dim , DataSig > >( tree.template setExtrapolatedDataField< DataSig , false , 0 , AuxData >( zeroAuxData , samples->size() , SampleFunctor , SampleDataFunctor , (DensityEstimator*)nullptr ) );
211 delete sampleAuxData;
212 if( params.verbose ) std::cout << "# Got aux data: " << profiler << std::endl;
213 }
214
215 {
216 profiler.reset();
217 auto hasDataFunctor = [&]( const FEMTreeNode *node )
218 {
219 ProjectiveData< AuxData , Real > *data = auxData->operator()( node );
220 return data && data->weight;
221 };
222 auto addNodeFunctor = [&]( int d , const int off[Dim] ){ return d<=(int)params.fullDepth; };
223 tree.template finalizeForMultigrid< 0 , 0 >( 0 , addNodeFunctor , hasDataFunctor , std::make_tuple() , std::make_tuple( auxData ) );
224 }
225
226 if( params.verbose ) std::cout << "# Finalized tree: " << profiler << std::endl;
227 if( params.verbose ) std::cout << "All Nodes / Active Nodes / Ghost Nodes: " << tree.allNodes() << " / " << tree.activeNodes() << " / " << tree.ghostNodes() << std::endl;
228 if( params.verbose ) std::cout << "Memory Usage: " << float( MemoryInfo::Usage())/(1<<20) << " MB" << std::endl;
229 delete samples;
230
231 auto nodeFunctor = [&]( const RegularTreeNode< Dim , FEMTreeNodeData , depth_and_offset_type > *n )
232 {
233 ProjectiveData< AuxData , Real >* clr = (*auxData)( n );
234 if( clr ) (*clr) *= (Real)pow( params.perLevelScaleFactor , tree.depth( n ) );
235 };
236 tree.tree().processNodes( nodeFunctor );
237
238 _worldToUnitCube = unitCubeToModel.inverse();
239 _auxEvaluator = new typename FEMTree< Dim , Real >::template MultiThreadedSparseEvaluator< IsotropicUIntPack< Dim , DataSig > , ProjectiveData< AuxData , Real > >( &tree , *auxData , ThreadPool::NumThreads() );
240
241 }
242 }
243 }
244
245 #endif // EXTRAPOLATOR_INCLUDED