Debian Codegraph @

src/thirdparty/PoissonRecon/PoissonRecon.cpp

raw
1 /*
2 Copyright (c) 2006, Michael Kazhdan and Matthew Bolitho
3 All rights reserved.
4
5 Redistribution and use in source and binary forms, with or without modification,
6 are permitted provided that the following conditions are met:
7
8 Redistributions of source code must retain the above copyright notice, this list of
9 conditions and the following disclaimer. Redistributions in binary form must reproduce
10 the above copyright notice, this list of conditions and the following disclaimer
11 in the documentation and/or other materials provided with the distribution.
12
13 Neither the name of the Johns Hopkins University nor the names of its contributors
14 may be used to endorse or promote products derived from this software without specific
15 prior written permission.
16
17 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
18 EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO THE IMPLIED WARRANTIES
19 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
20 SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
21 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
22 TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
23 BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
25 ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
26 DAMAGE.
27 */
28
29 #include "PreProcessor.h"
30 #include "Reconstructors.h"
31
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <math.h>
35 #include <float.h>
36 #include "MyMiscellany.h"
37 #include "CmdLineParser.h"
38 #include "PPolynomial.h"
39 #include "FEMTree.h"
40 #include "Ply.h"
41 #include "VertexFactory.h"
42 #include "RegularGrid.h"
43 #include "DataStream.imp.h"
44
45 #define DEFAULT_DIMENSION 3
46
47 using namespace PoissonRecon;
48
49 namespace {
50
51 CmdLineParameter< char* >
52 In( "in" ) ,
53 Out( "out" ) ,
54 TempDir( "tempDir" ) ,
55 Grid( "grid" ) ,
56 Tree( "tree" ) ,
57 Envelope( "envelope" ) ,
58 Transform( "xForm" );
59
60 CmdLineReadable
61 Performance( "performance" ) ,
62 ShowResidual( "showResidual" ) ,
63 PolygonMesh( "polygonMesh" ) ,
64 NonManifold( "nonManifold" ) ,
65 ASCII( "ascii" ) ,
66 Density( "density" ) ,
67 LinearFit( "linearFit" ) ,
68 PrimalGrid( "primalGrid" ) ,
69 ExactInterpolation( "exact" ) ,
70 Colors( "colors" ) ,
71 InCore( "inCore" ) ,
72 NoDirichletErode( "noErode" ) ,
73 Gradients( "gradients" ) ,
74 GridCoordinates( "gridCoordinates" ) ,
75 Confidence( "confidence" ) ,
76 Verbose( "verbose" );
77
78 CmdLineParameter< int >
79 #ifndef FAST_COMPILE
80 Degree( "degree" , Reconstructor::Poisson::DefaultFEMDegree ) ,
81 #endif // !FAST_COMPILE
82 Depth( "depth" , 8 ) ,
83 KernelDepth( "kernelDepth" , -1 ) ,
84 SolveDepth( "solveDepth" , -1 ) ,
85 EnvelopeDepth( "envelopeDepth" , -1 ) ,
86 FullDepth( "fullDepth" , 5 ) ,
87 BaseDepth( "baseDepth" , -1 ) ,
88 Iters( "iters" , 8 ) ,
89 BaseVCycles( "baseVCycles" , 1 ) ,
90 #ifndef FAST_COMPILE
91 BType( "bType" , Reconstructor::Poisson::DefaultFEMBoundary+1 ) ,
92 #endif // !FAST_COMPILE
93 MaxMemoryGB( "maxMemory" , 0 ) ,
94 ParallelType( "parallel" , 0 ) ,
95 AlignmentDir( "alignDir" , DEFAULT_DIMENSION-1 ) ,
96 ScheduleType( "schedule" , (int)ThreadPool::Schedule ) ,
97 ThreadChunkSize( "chunkSize" , (int)ThreadPool::ChunkSize );
98
99 CmdLineParameter< float >
100 DataX( "data" , 32.f ) ,
101 SamplesPerNode( "samplesPerNode" , 1.5f ) ,
102 Scale( "scale" , 1.1f ) ,
103 Width( "width" , 0.f ) ,
104 CGSolverAccuracy( "cgAccuracy" , 1e-3f ) ,
105 LowDepthCutOff( "lowDepthCutOff" , 0.f ) ,
106 PointWeight( "pointWeight" );
107
108 CmdLineReadable* params[] =
109 {
110 #ifndef FAST_COMPILE
111 &Degree , &BType ,
112 #endif // !FAST_COMPILE
113 &In , &Depth , &Out , &Transform ,
114 &SolveDepth ,
115 &Envelope ,
116 &Width ,
117 &Scale , &Verbose , &CGSolverAccuracy ,
118 &KernelDepth , &SamplesPerNode , &Confidence , &NonManifold , &PolygonMesh , &ASCII , &ShowResidual ,
119 &EnvelopeDepth ,
120 &NoDirichletErode ,
121 &BaseDepth , &BaseVCycles ,
122 &PointWeight ,
123 &Grid ,
124 &Tree ,
125 &Density ,
126 &FullDepth ,
127 &Iters ,
128 &DataX ,
129 &Colors ,
130 &Gradients ,
131 &LinearFit ,
132 &PrimalGrid ,
133 &TempDir ,
134 &ExactInterpolation ,
135 &Performance ,
136 &MaxMemoryGB ,
137 &InCore ,
138 &ParallelType ,
139 &ScheduleType ,
140 &ThreadChunkSize ,
141 &LowDepthCutOff ,
142 &AlignmentDir ,
143 &GridCoordinates ,
144 NULL
145 };
146
147 void ShowUsage(char* ex)
148 {
149 printf( "Usage: %s\n" , ex );
150 printf( "\t --%s <input points>\n" , In.name );
151 printf( "\t[--%s <input envelope>\n" , Envelope.name );
152 printf( "\t[--%s <ouput triangle mesh>]\n" , Out.name );
153 printf( "\t[--%s <ouput grid>]\n" , Grid.name );
154 printf( "\t[--%s <ouput fem tree>]\n" , Tree.name );
155 #ifndef FAST_COMPILE
156 printf( "\t[--%s <b-spline degree>=%d]\n" , Degree.name , Degree.value );
157 printf( "\t[--%s <boundary type>=%d]\n" , BType.name , BType.value );
158 for( int i=0 ; i<BOUNDARY_COUNT ; i++ ) printf( "\t\t%d] %s\n" , i+1 , BoundaryNames[i] );
159 #endif // !FAST_COMPILE
160 printf( "\t[--%s <maximum reconstruction depth>=%d]\n" , Depth.name , Depth.value );
161 printf( "\t[--%s <maximum solution depth>=%d]\n" , SolveDepth.name , SolveDepth.value );
162 printf( "\t[--%s <grid width>]\n" , Width.name );
163 printf( "\t[--%s <full depth>=%d]\n" , FullDepth.name , FullDepth.value );
164 printf( "\t[--%s <envelope depth>=%d]\n" , EnvelopeDepth.name , EnvelopeDepth.value );
165 printf( "\t[--%s <coarse MG solver depth>]\n" , BaseDepth.name );
166 printf( "\t[--%s <coarse MG solver v-cycles>=%d]\n" , BaseVCycles.name , BaseVCycles.value );
167 printf( "\t[--%s <scale factor>=%f]\n" , Scale.name , Scale.value );
168 printf( "\t[--%s <minimum number of samples per node>=%f]\n" , SamplesPerNode.name, SamplesPerNode.value );
169 printf( "\t[--%s <interpolation weight>=%.3e * <b-spline degree>]\n" , PointWeight.name , Reconstructor::Poisson::WeightMultiplier * Reconstructor::Poisson::DefaultFEMDegree );
170 printf( "\t[--%s <iterations>=%d]\n" , Iters.name , Iters.value );
171 printf( "\t[--%s]\n" , ExactInterpolation.name );
172 printf( "\t[--%s <pull factor>=%f]\n" , DataX.name , DataX.value );
173 printf( "\t[--%s]\n" , Colors.name );
174 printf( "\t[--%s]\n" , Gradients.name );
175 printf( "\t[--%s <parallel type>=%d]\n" , ParallelType.name , ParallelType.value );
176 for( size_t i=0 ; i<ThreadPool::ParallelNames.size() ; i++ ) printf( "\t\t%d] %s\n" , (int)i , ThreadPool::ParallelNames[i].c_str() );
177 printf( "\t[--%s <schedue type>=%d]\n" , ScheduleType.name , ScheduleType.value );
178 for( size_t i=0 ; i<ThreadPool::ScheduleNames.size() ; i++ ) printf( "\t\t%d] %s\n" , (int)i , ThreadPool::ScheduleNames[i].c_str() );
179 printf( "\t[--%s <thread chunk size>=%d]\n" , ThreadChunkSize.name , ThreadChunkSize.value );
180 printf( "\t[--%s <low depth cut-off>=%f]\n" , LowDepthCutOff.name , LowDepthCutOff.value );
181 printf( "\t[--%s <alignment direction>=%d]\n" , AlignmentDir.name , AlignmentDir.value );
182 printf( "\t[--%s]\n" , Confidence.name );
183 printf( "\t[--%s]\n" , NonManifold.name );
184 printf( "\t[--%s]\n" , PolygonMesh.name );
185 printf( "\t[--%s <cg solver accuracy>=%g]\n" , CGSolverAccuracy.name , CGSolverAccuracy.value );
186 printf( "\t[--%s <maximum memory (in GB)>=%d]\n" , MaxMemoryGB.name , MaxMemoryGB.value );
187 printf( "\t[--%s]\n" , NoDirichletErode.name );
188 printf( "\t[--%s]\n" , GridCoordinates.name );
189 printf( "\t[--%s]\n" , Performance.name );
190 printf( "\t[--%s]\n" , Density.name );
191 printf( "\t[--%s]\n" , LinearFit.name );
192 printf( "\t[--%s]\n" , PrimalGrid.name );
193 printf( "\t[--%s]\n" , ASCII.name );
194 printf( "\t[--%s]\n" , TempDir.name );
195 printf( "\t[--%s]\n" , InCore.name );
196 printf( "\t[--%s]\n" , Verbose.name );
197 }
198
199 template< typename Real , unsigned int Dim , unsigned int FEMSig , bool HasGradients , bool HasDensity , bool InCore , typename ... AuxDataFactories >
200 void WriteMesh
201 (
202 Reconstructor::Implicit< Real , Dim , IsotropicUIntPack< Dim , FEMSig > , typename AuxDataFactories::VertexType ... > &implicit ,
203 const Reconstructor::LevelSetExtractionParameters &meParams ,
204 std::string fileName ,
205 bool ascii ,
206 const AuxDataFactories& ... factories
207 )
208 {
209 // A description of the output vertex information
210 using VInfo = Reconstructor::OutputVertexInfo< Real , Dim , HasGradients , HasDensity , AuxDataFactories ... >;
211
212 // A factory generating the output vertices
213 using Factory = typename VInfo::Factory;
214 Factory factory = VInfo::GetFactory( factories... );
215
216 Reconstructor::OutputInputFactoryTypeStream< Real , Dim , Factory , InCore , true , typename AuxDataFactories::VertexType... > vertexStream( factory , VInfo::Convert );
217 Reconstructor::OutputInputFaceStream< Dim-1 , InCore , true > faceStream;
218
219 implicit.extractLevelSet( vertexStream , faceStream , meParams );
220
221 // Write the mesh to a .ply file
222 std::vector< std::string > noComments;
223 vertexStream.reset();
224 PLY::Write< Factory , node_index_type , Real , Dim >( fileName , factory , vertexStream.size() , faceStream.size() , vertexStream , faceStream , ascii ? PLY_ASCII : PLY_BINARY_NATIVE , noComments );
225 }
226
227 template< typename Real , unsigned int Dim , unsigned int FEMSig , bool HasDensity , bool InCore , typename ... AuxDataFactories >
228 void WriteMesh
229 (
230 bool hasGradients ,
231 Reconstructor::Implicit< Real , Dim , IsotropicUIntPack< Dim , FEMSig > , typename AuxDataFactories::VertexType ... > &implicit ,
232 const Reconstructor::LevelSetExtractionParameters &meParams ,
233 std::string fileName ,
234 bool ascii ,
235 const AuxDataFactories& ... factories
236 )
237 {
238 if( hasGradients ) return WriteMesh< Real , Dim, FEMSig , true , HasDensity , InCore , AuxDataFactories ... >( implicit , meParams , fileName , ascii , factories... );
239 else return WriteMesh< Real , Dim, FEMSig , false , HasDensity , InCore , AuxDataFactories ... >( implicit , meParams , fileName , ascii , factories... );
240 }
241
242 template< typename Real , unsigned int Dim , unsigned int FEMSig , bool InCore , typename ... AuxDataFactories >
243 void WriteMesh
244 (
245 bool hasGradients , bool hasDensity ,
246 Reconstructor::Implicit< Real , Dim , IsotropicUIntPack< Dim , FEMSig > , typename AuxDataFactories::VertexType ... > &implicit ,
247 const Reconstructor::LevelSetExtractionParameters &meParams ,
248 std::string fileName ,
249 bool ascii ,
250 const AuxDataFactories& ... factories
251 )
252 {
253 if( hasDensity ) return WriteMesh< Real , Dim, FEMSig , true , InCore , AuxDataFactories ... >( hasGradients , implicit , meParams , fileName , ascii , factories... );
254 else return WriteMesh< Real , Dim, FEMSig , false , InCore , AuxDataFactories ... >( hasGradients , implicit , meParams , fileName , ascii , factories... );
255 }
256
257 template< typename Real , unsigned int Dim , unsigned int FEMSig , typename ... AuxDataFactories >
258 void WriteMesh
259 (
260 bool hasGradients , bool hasDensity , bool inCore ,
261 Reconstructor::Implicit< Real , Dim , IsotropicUIntPack< Dim , FEMSig > , typename AuxDataFactories::VertexType ... > &implicit ,
262 const Reconstructor::LevelSetExtractionParameters &meParams ,
263 std::string fileName ,
264 bool ascii ,
265 const AuxDataFactories& ... factories
266 )
267 {
268 if( inCore ) return WriteMesh< Real , Dim, FEMSig , true , AuxDataFactories ... >( hasGradients , hasDensity , implicit , meParams , fileName , ascii , factories... );
269 else return WriteMesh< Real , Dim, FEMSig , false , AuxDataFactories ... >( hasGradients , hasDensity , implicit , meParams , fileName , ascii , factories... );
270 }
271
272 template< class Real , unsigned int Dim , unsigned int FEMSig , typename AuxDataFactory >
273 void Execute( const AuxDataFactory &auxDataFactory )
274 {
275 static const bool HasAuxData = !std::is_same< AuxDataFactory , VertexFactory::EmptyFactory< Real > >::value;
276
277 ///////////////
278 // Types --> //
279 typedef IsotropicUIntPack< Dim , FEMSig > Sigs;
280 using namespace VertexFactory;
281
282 // The factory for constructing an input sample's data
283 typedef std::conditional_t< HasAuxData , Factory< Real , NormalFactory< Real , Dim > , AuxDataFactory > , NormalFactory< Real , Dim > > InputSampleDataFactory;
284
285 // The factory for constructing an input sample
286 typedef Factory< Real , PositionFactory< Real , Dim > , InputSampleDataFactory > InputSampleFactory;
287
288 typedef InputDataStream< typename InputSampleFactory::VertexType > InputPointStream;
289
290 using Implicit = std::conditional_t< HasAuxData , Reconstructor::Implicit< Real , Dim , IsotropicUIntPack< Dim , FEMSig > , typename AuxDataFactory::VertexType > , Reconstructor::Implicit< Real , Dim , IsotropicUIntPack< Dim , FEMSig > > >;
291 using Solver = std::conditional_t< HasAuxData , Reconstructor::Poisson::Solver< Real , Dim , IsotropicUIntPack< Dim , FEMSig > , typename AuxDataFactory::VertexType > , Reconstructor::Poisson::Solver< Real , Dim , IsotropicUIntPack< Dim , FEMSig > > >;
292 // <-- Types //
293 ///////////////
294
295 if( Verbose.set )
296 {
297 std::cout << "*************************************************************" << std::endl;
298 std::cout << "*************************************************************" << std::endl;
299 std::cout << "** Running Screened Poisson Reconstruction (Version " << ADAPTIVE_SOLVERS_VERSION << ") **" << std::endl;
300 std::cout << "*************************************************************" << std::endl;
301 std::cout << "*************************************************************" << std::endl;
302
303 char str[1024];
304 for( int i=0 ; params[i] ; i++ ) if( params[i]->set )
305 {
306 params[i]->writeValue( str );
307 if( strlen( str ) ) std::cout << "\t--" << params[i]->name << " " << str << std::endl;
308 else std::cout << "\t--" << params[i]->name << std::endl;
309 }
310 }
311
312 Profiler profiler(20);
313 Implicit *implicit = NULL;
314 typename Reconstructor::Poisson::SolutionParameters< Real > sParams;
315 Reconstructor::LevelSetExtractionParameters meParams;
316
317 sParams.verbose = Verbose.set;
318 sParams.dirichletErode = !NoDirichletErode.set;
319 sParams.exactInterpolation = ExactInterpolation.set;
320 sParams.showResidual = ShowResidual.set;
321 sParams.confidence = Confidence.set;
322 sParams.scale = (Real)Scale.value;
323 sParams.lowDepthCutOff = (Real)LowDepthCutOff.value;
324 sParams.width = (Real)Width.value;
325 sParams.pointWeight = (Real)PointWeight.value;
326 sParams.samplesPerNode = (Real)SamplesPerNode.value;
327 sParams.cgSolverAccuracy = (Real)CGSolverAccuracy.value;
328 sParams.perLevelDataScaleFactor = (Real)DataX.value;
329 sParams.depth = (unsigned int)Depth.value;
330 sParams.baseDepth = (unsigned int)BaseDepth.value;
331 sParams.solveDepth = (unsigned int)SolveDepth.value;
332 sParams.fullDepth = (unsigned int)FullDepth.value;
333 sParams.kernelDepth = (unsigned int)KernelDepth.value;
334 sParams.envelopeDepth = (unsigned int)EnvelopeDepth.value;
335 sParams.baseVCycles = (unsigned int)BaseVCycles.value;
336 sParams.iters = (unsigned int)Iters.value;
337 sParams.alignDir = (unsigned int)AlignmentDir.value;
338
339 meParams.linearFit = LinearFit.set;
340 meParams.outputGradients = Gradients.set;
341 meParams.forceManifold = !NonManifold.set;
342 meParams.polygonMesh = PolygonMesh.set;
343 meParams.gridCoordinates = GridCoordinates.set;
344 meParams.outputDensity = Density.set;
345 meParams.verbose = Verbose.set;
346
347 double startTime = Time();
348
349 InputSampleFactory *_inputSampleFactory;
350 if constexpr( HasAuxData ) _inputSampleFactory = new InputSampleFactory( VertexFactory::PositionFactory< Real , Dim >() , InputSampleDataFactory( VertexFactory::NormalFactory< Real , Dim >() , auxDataFactory ) );
351 else _inputSampleFactory = new InputSampleFactory( VertexFactory::PositionFactory< Real , Dim >() , VertexFactory::NormalFactory< Real , Dim >() );
352 InputSampleFactory &inputSampleFactory = *_inputSampleFactory;
353 XForm< Real , Dim+1 > toModel = XForm< Real , Dim+1 >::Identity();
354
355 // Read in the transform, if we want to apply one to the points before processing
356 if( Transform.set )
357 {
358 FILE* fp = fopen( Transform.value , "r" );
359 if( !fp ) MK_WARN( "Could not read x-form from: " , Transform.value );
360 else
361 {
362 for( int i=0 ; i<Dim+1 ; i++ ) for( int j=0 ; j<Dim+1 ; j++ )
363 {
364 float f;
365 if( fscanf( fp , " %f " , &f )!=1 ) MK_THROW( "Failed to read xform" );
366 toModel(i,j) = (Real)f;
367 }
368 fclose( fp );
369 }
370 }
371 std::vector< typename InputSampleFactory::VertexType > inCorePoints;
372 InputPointStream *pointStream;
373
374 // Get the point stream
375 {
376 profiler.reset();
377 char *ext = GetFileExtension( In.value );
378
379 if( InCore.set )
380 {
381 InputPointStream *_pointStream;
382 if ( !strcasecmp( ext , "bnpts" ) ) _pointStream = new BinaryInputDataStream< InputSampleFactory >( In.value , inputSampleFactory );
383 else if( !strcasecmp( ext , "ply" ) ) _pointStream = new PLYInputDataStream< InputSampleFactory >( In.value , inputSampleFactory );
384 else _pointStream = new ASCIIInputDataStream< InputSampleFactory >( In.value , inputSampleFactory );
385 typename InputSampleFactory::VertexType p = inputSampleFactory();
386 while( _pointStream->read( p ) ) inCorePoints.push_back( p );
387 delete _pointStream;
388
389 pointStream = new VectorBackedInputDataStream< typename InputSampleFactory::VertexType >( inCorePoints );
390 }
391 else
392 {
393 if ( !strcasecmp( ext , "bnpts" ) ) pointStream = new BinaryInputDataStream< InputSampleFactory >( In.value , inputSampleFactory );
394 else if( !strcasecmp( ext , "ply" ) ) pointStream = new PLYInputDataStream< InputSampleFactory >( In.value , inputSampleFactory );
395 else pointStream = new ASCIIInputDataStream< InputSampleFactory >( In.value , inputSampleFactory );
396 }
397 delete[] ext;
398 }
399
400 typename Reconstructor::Poisson::EnvelopeMesh< Real , Dim > *envelopeMesh = NULL;
401 if( Envelope.set )
402 {
403 envelopeMesh = new typename Reconstructor::Poisson::EnvelopeMesh< Real , Dim >();
404 {
405 std::vector< std::vector< int > > polygons;
406 std::vector< std::string > comments;
407 int file_type;
408 PLY::ReadPolygons( Envelope.value , PositionFactory< Real , Dim >() , envelopeMesh->vertices , polygons , file_type , comments );
409 envelopeMesh->simplices.resize( polygons.size() );
410 for( int i=0 ; i<polygons.size() ; i++ )
411 if( polygons[i].size()!=Dim ) MK_THROW( "Not a simplex" );
412 else for( int j=0 ; j<Dim ; j++ ) envelopeMesh->simplices[i][j] = polygons[i][j];
413 }
414 }
415
416 // A wrapper class to realize InputDataStream< SampleType > as an InputSampleStream
417 struct _InputOrientedSampleStream : public Reconstructor::InputOrientedSampleStream< Real , Dim >
418 {
419 typedef Reconstructor::Normal< Real , Dim > DataType;
420 typedef DirectSum< Real , Reconstructor::Position< Real , Dim > , DataType > SampleType;
421 typedef InputDataStream< SampleType > _InputPointStream;
422 _InputPointStream &pointStream;
423 SampleType scratch;
424
425 _InputOrientedSampleStream( _InputPointStream &pointStream )
426 : pointStream( pointStream ) , scratch( Reconstructor::Position< Real , Dim >() , Reconstructor::Normal< Real , Dim >() )
427 {}
428
429 void reset( void ){ pointStream.reset(); }
430
431 bool read( Reconstructor::Position< Real , Dim > &p , Reconstructor::Normal< Real , Dim > &n )
432 {
433 bool ret = pointStream.read( scratch );
434 if( ret ) p = scratch.template get<0>() , n = scratch.template get<1>();
435 return ret;
436 }
437
438 bool read( unsigned int thread , Reconstructor::Position< Real , Dim > &p , Reconstructor::Normal< Real , Dim > &n )
439 {
440 bool ret = pointStream.read( thread , scratch );
441 if( ret ) p = scratch.template get<0>() , n = scratch.template get<1>();
442 return ret;
443 }
444 };
445
446 // A wrapper class to realize InputDataStream< SampleType > as an InputSampleWithDataStream
447 struct _InputOrientedSampleWithDataStream : public Reconstructor::InputOrientedSampleStream< Real , Dim , typename AuxDataFactory::VertexType >
448 {
449 typedef DirectSum< Real , Reconstructor::Normal< Real , Dim > , typename AuxDataFactory::VertexType > DataType;
450 typedef DirectSum< Real , Reconstructor::Position< Real , Dim > , DataType > SampleType;
451 typedef InputDataStream< SampleType > _InputPointStream;
452 _InputPointStream &pointStream;
453 SampleType scratch;
454
455 _InputOrientedSampleWithDataStream( _InputPointStream &pointStream , typename AuxDataFactory::VertexType zero )
456 : pointStream( pointStream ) , scratch( Reconstructor::Position< Real , Dim >() , DataType( Reconstructor::Normal< Real , Dim >() , zero ) )
457 {}
458
459 void reset( void ){ pointStream.reset(); }
460
461 bool read( Reconstructor::Position< Real , Dim > &p , Reconstructor::Normal< Real , Dim > &n , typename AuxDataFactory::VertexType &d )
462 {
463 bool ret = pointStream.read( scratch );
464 if( ret ) p = scratch.template get<0>() , n = scratch.template get<1>().template get<0>() , d = scratch.template get<1>().template get<1>();
465 return ret;
466 }
467
468 bool read( unsigned int thread , Reconstructor::Position< Real , Dim > &p , Reconstructor::Normal< Real , Dim > &n , typename AuxDataFactory::VertexType &d )
469 {
470 bool ret = pointStream.read( thread , scratch );
471 if( ret ) p = scratch.template get<0>() , n = scratch.template get<1>().template get<0>() , d = scratch.template get<1>().template get<1>();
472 return ret;
473 }
474 };
475
476 if( Transform.set && envelopeMesh ) for( unsigned int i=0 ; i<envelopeMesh->vertices.size() ; i++ ) envelopeMesh->vertices[i] = toModel * envelopeMesh->vertices[i];
477 if constexpr( HasAuxData )
478 {
479 _InputOrientedSampleWithDataStream sampleStream( *pointStream , auxDataFactory() );
480
481 if( Transform.set )
482 {
483 Reconstructor::TransformedInputOrientedSampleStream< Real , Dim , typename AuxDataFactory::VertexType > _sampleStream( toModel , sampleStream );
484 implicit = Solver::Solve( _sampleStream , sParams , auxDataFactory() , envelopeMesh );
485 implicit->unitCubeToModel = toModel.inverse() * implicit->unitCubeToModel;
486 }
487 else implicit = Solver::Solve( sampleStream , sParams , auxDataFactory() , envelopeMesh );
488 }
489 else
490 {
491 _InputOrientedSampleStream sampleStream( *pointStream );
492
493 if( Transform.set )
494 {
495 Reconstructor::TransformedInputOrientedSampleStream< Real , Dim > _sampleStream( toModel , sampleStream );
496 implicit = Solver::Solve( _sampleStream , sParams , envelopeMesh );
497 implicit->unitCubeToModel = toModel.inverse() * implicit->unitCubeToModel;
498 }
499 else implicit = Solver::Solve( sampleStream , sParams , envelopeMesh );
500 }
501
502 delete pointStream;
503 delete _inputSampleFactory;
504 delete envelopeMesh;
505
506 if( Tree.set )
507 {
508 FILE* fp = fopen( Tree.value , "wb" );
509 if( !fp ) MK_THROW( "Failed to open file for writing: " , Tree.value );
510 FileStream fs(fp);
511 FEMTree< Dim , Real >::WriteParameter( fs );
512 DenseNodeData< Real , Sigs >::WriteSignatures( fs );
513 implicit->tree.write( fs , false );
514 fs.write( implicit->unitCubeToModel.inverse() );
515 implicit->solution.write( fs );
516 fclose( fp );
517 }
518
519 if( Grid.set )
520 {
521 int res = 0;
522 profiler.reset();
523 Pointer( Real ) values = implicit->tree.template regularGridEvaluate< true >( implicit->solution , res , -1 , PrimalGrid.set );
524 if( Verbose.set ) std::cout << "Got grid: " << profiler << std::endl;
525 XForm< Real , Dim+1 > voxelToUnitCube = XForm< Real , Dim+1 >::Identity();
526 if( PrimalGrid.set ) for( int d=0 ; d<Dim ; d++ ) voxelToUnitCube( d , d ) = (Real)( 1. / (res-1) );
527 else for( int d=0 ; d<Dim ; d++ ) voxelToUnitCube( d , d ) = (Real)( 1. / res ) , voxelToUnitCube( Dim , d ) = (Real)( 0.5 / res );
528
529 unsigned int _res[Dim];
530 for( int d=0 ; d<Dim ; d++ ) _res[d] = res;
531 RegularGrid< Real , Dim >::Write( Grid.value , _res , values , implicit->unitCubeToModel * voxelToUnitCube );
532
533 DeletePointer( values );
534 }
535
536
537 if( Out.set )
538 if( Dim==2 || Dim==3 )
539 {
540 // Create the output mesh
541 char tempHeader[2048];
542 {
543 char tempPath[1024];
544 tempPath[0] = 0;
545 if( TempDir.set ) strcpy( tempPath , TempDir.value );
546 else SetTempDirectory( tempPath , sizeof(tempPath) );
547 if( strlen(tempPath)==0 ) sprintf( tempPath , ".%c" , FileSeparator );
548 if( tempPath[ strlen( tempPath )-1 ]==FileSeparator ) sprintf( tempHeader , "%sPR_" , tempPath );
549 else sprintf( tempHeader , "%s%cPR_" , tempPath , FileSeparator );
550 }
551
552 XForm< Real , Dim+1 > pXForm = implicit->unitCubeToModel;
553 XForm< Real , Dim > nXForm = XForm< Real , Dim >( pXForm ).inverse().transpose();
554
555 if constexpr( HasAuxData ) WriteMesh< Real , Dim , FEMSig >( Gradients.set , Density.set , InCore.set , *implicit , meParams , Out.value , ASCII.set , auxDataFactory );
556 else WriteMesh< Real , Dim , FEMSig >( Gradients.set , Density.set , InCore.set , *implicit , meParams , Out.value , ASCII.set );
557 }
558 else MK_WARN( "Mesh extraction is only supported in dimensions 2 and 3" );
559
560 if( Verbose.set ) std::cout << "# Total Solve: " << Time()-startTime << " (s), " << MemoryInfo::PeakMemoryUsageMB() << " (MB)" << std::endl;
561 delete implicit;
562 }
563
564 #ifndef FAST_COMPILE
565 template< unsigned int Dim , class Real , BoundaryType BType , typename AuxDataFactory >
566 void Execute( const AuxDataFactory &auxDataFactory )
567 {
568 switch( Degree.value )
569 {
570 case 1: return Execute< Real , Dim , FEMDegreeAndBType< 1 , BType >::Signature >( auxDataFactory );
571 case 2: return Execute< Real , Dim , FEMDegreeAndBType< 2 , BType >::Signature >( auxDataFactory );
572 // case 3: return Execute< Real , Dim , FEMDegreeAndBType< 3 , BType >::Signature >( auxDataFactory );
573 // case 4: return Execute< Real , Dim , FEMDegreeAndBType< 4 , BType >::Signature >( auxDataFactory );
574 default: MK_THROW( "Only B-Splines of degree 1 - 2 are supported" );
575 }
576 }
577
578 template< unsigned int Dim , class Real , typename AuxDataFactory >
579 void Execute( const AuxDataFactory &auxDataFactory )
580 {
581 switch( BType.value )
582 {
583 case BOUNDARY_FREE+1: return Execute< Dim , Real , BOUNDARY_FREE >( auxDataFactory );
584 case BOUNDARY_NEUMANN+1: return Execute< Dim , Real , BOUNDARY_NEUMANN >( auxDataFactory );
585 case BOUNDARY_DIRICHLET+1: return Execute< Dim , Real , BOUNDARY_DIRICHLET >( auxDataFactory );
586 default: MK_THROW( "Not a valid boundary type: " , BType.value );
587 }
588 }
589 #endif // !FAST_COMPILE
590
591 } // namespace
592
593 int RunPoissonRecon( int argc , char* argv[] )
594 {
595 Timer timer;
596 #ifdef ARRAY_DEBUG
597 MK_WARN( "Array debugging enabled" );
598 #endif // ARRAY_DEBUG
599 CmdLineParse( argc-1 , &argv[1] , params );
600 if( MaxMemoryGB.value>0 ) SetPeakMemoryMB( MaxMemoryGB.value<<10 );
601 ThreadPool::ChunkSize = ThreadChunkSize.value;
602 ThreadPool::Schedule = (ThreadPool::ScheduleType)ScheduleType.value;
603 ThreadPool::ParallelizationType= (ThreadPool::ParallelType)ParallelType.value;
604
605 if( !In.set )
606 {
607 ShowUsage( argv[0] );
608 return 0;
609 }
610
611 #ifdef USE_DOUBLE
612 typedef double Real;
613 #else // !USE_DOUBLE
614 typedef float Real;
615 #endif // USE_DOUBLE
616
617 #ifdef FAST_COMPILE
618 static const int Degree = Reconstructor::Poisson::DefaultFEMDegree;
619 static const BoundaryType BType = Reconstructor::Poisson::DefaultFEMBoundary;
620 static const unsigned int Dim = DEFAULT_DIMENSION;
621 static const unsigned int FEMSig = FEMDegreeAndBType< Degree , BType >::Signature;
622 MK_WARN( "Compiled for degree-" , Degree , ", boundary-" , BoundaryNames[ BType ] , ", " , sizeof(Real)==4 ? "single" : "double" , "-precision _only_" );
623 if( !PointWeight.set ) PointWeight.value = Reconstructor::Poisson::WeightMultiplier*Degree;
624 char *ext = GetFileExtension( In.value );
625 if( !strcasecmp( ext , "ply" ) )
626 {
627 typedef VertexFactory::Factory< Real , typename VertexFactory::PositionFactory< Real , DEFAULT_DIMENSION > , typename VertexFactory::NormalFactory< Real , DEFAULT_DIMENSION > > Factory;
628 Factory factory;
629 bool *readFlags = new bool[ factory.plyReadNum() ];
630 std::vector< PlyProperty > unprocessedProperties;
631 PLY::ReadVertexHeader( In.value , factory , readFlags , unprocessedProperties );
632 if( !factory.template plyValidReadProperties<0>( readFlags ) ) MK_THROW( "Ply file does not contain positions" );
633 if( !factory.template plyValidReadProperties<1>( readFlags ) ) MK_THROW( "Ply file does not contain normals" );
634 delete[] readFlags;
635
636 if( unprocessedProperties.size() ) Execute< Real , Dim , FEMSig >( VertexFactory::DynamicFactory< Real >( unprocessedProperties ) );
637 else Execute< Real , Dim , FEMSig >( VertexFactory::EmptyFactory< Real >() );
638 }
639 else
640 {
641 if( Colors.set ) Execute< Real , Dim , FEMSig >( VertexFactory::RGBColorFactory< Real >() );
642 else Execute< Real , Dim , FEMSig >( VertexFactory::EmptyFactory< Real >() );
643 }
644 delete[] ext;
645 #else // !FAST_COMPILE
646 if( !PointWeight.set ) PointWeight.value = Reconstructor::Poisson::WeightMultiplier * Degree.value;
647 char *ext = GetFileExtension( In.value );
648 if( !strcasecmp( ext , "ply" ) )
649 {
650 typedef VertexFactory::Factory< Real , typename VertexFactory::PositionFactory< Real , DEFAULT_DIMENSION > , typename VertexFactory::NormalFactory< Real , DEFAULT_DIMENSION > > Factory;
651 Factory factory;
652 bool *readFlags = new bool[ factory.plyReadNum() ];
653 std::vector< PlyProperty > unprocessedProperties;
654 PLY::ReadVertexHeader( In.value , factory , readFlags , unprocessedProperties );
655 if( !factory.template plyValidReadProperties<0>( readFlags ) ) MK_THROW( "Ply file does not contain positions" );
656 if( !factory.template plyValidReadProperties<1>( readFlags ) ) MK_THROW( "Ply file does not contain normals" );
657 delete[] readFlags;
658
659 if( unprocessedProperties.size() ) Execute< DEFAULT_DIMENSION , Real >( VertexFactory::DynamicFactory< Real >( unprocessedProperties ) );
660 else Execute< DEFAULT_DIMENSION , Real >( VertexFactory::EmptyFactory< Real >() );
661 }
662 else
663 {
664 if( Colors.set ) Execute< DEFAULT_DIMENSION , Real >( VertexFactory::RGBColorFactory< Real >() );
665 else Execute< DEFAULT_DIMENSION , Real >( VertexFactory::EmptyFactory< Real >() );
666 }
667 delete[] ext;
668 #endif // FAST_COMPILE
669 if( Performance.set )
670 {
671 printf( "Time (Wall/CPU): %.2f / %.2f\n" , timer.wallTime() , timer.cpuTime() );
672 printf( "Peak Memory (MB): %d\n" , MemoryInfo::PeakMemoryUsageMB() );
673 }
674
675 return EXIT_SUCCESS;
676 }