Debian Codegraph @

src/thirdparty/PoissonRecon/SSDRecon.cpp

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