| 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 |
|
| 31 |
//#undef USE_DOUBLE // If enabled, double-precesion is used |
| 32 |
#define USE_DOUBLE // If enabled, double-precesion is used |
| 33 |
#define WEIGHT_DEGREE 2 // The order of the B-Spline used to splat in the weights for density estimation |
| 34 |
#define DEFAULT_FEM_DEGREE 2 // The default finite-element degree |
| 35 |
#define DEFAULT_FEM_BOUNDARY BOUNDARY_FREE // The default finite-element boundary type |
| 36 |
#define DEFAULT_DIMENSION 2 // The dimension of the system |
| 37 |
|
| 38 |
#include <stdio.h> |
| 39 |
#include <stdlib.h> |
| 40 |
#include <math.h> |
| 41 |
#include <float.h> |
| 42 |
#include "MyMiscellany.h" |
| 43 |
#include "CmdLineParser.h" |
| 44 |
#include "PPolynomial.h" |
| 45 |
#include "FEMTree.h" |
| 46 |
#include "Ply.h" |
| 47 |
#include "VertexFactory.h" |
| 48 |
#include "Image.h" |
| 49 |
#include "RegularGrid.h" |
| 50 |
#include "DataStream.imp.h" |
| 51 |
#include "Reconstructors.h" |
| 52 |
|
| 53 |
using namespace PoissonRecon; |
| 54 |
|
| 55 |
CmdLineParameter< char* > |
| 56 |
InValues( "inValues" ) , |
| 57 |
InGradients( "inGradients" ) , |
| 58 |
Out( "out" ) , |
| 59 |
TempDir( "tempDir" ) , |
| 60 |
Grid( "grid" ) , |
| 61 |
Tree( "tree" ) , |
| 62 |
Transform( "xForm" ); |
| 63 |
|
| 64 |
CmdLineReadable |
| 65 |
Performance( "performance" ) , |
| 66 |
ShowResidual( "showResidual" ) , |
| 67 |
PrimalGrid( "primalGrid" ) , |
| 68 |
ExactInterpolation( "exact" ) , |
| 69 |
InCore( "inCore" ) , |
| 70 |
PolygonMesh( "polygonMesh" ) , |
| 71 |
NonManifold( "nonManifold" ) , |
| 72 |
NonLinearFit( "nonLinearFit" ) , |
| 73 |
ASCII( "ascii" ) , |
| 74 |
Verbose( "verbose" ); |
| 75 |
|
| 76 |
CmdLineParameter< int > |
| 77 |
#ifndef FAST_COMPILE |
| 78 |
Degree( "degree" , DEFAULT_FEM_DEGREE ) , |
| 79 |
#endif // !FAST_COMPILE |
| 80 |
Depth( "depth" , 8 ) , |
| 81 |
SolveDepth( "solveDepth" , -1 ) , |
| 82 |
Iters( "iters" , 8 ) , |
| 83 |
FullDepth( "fullDepth" , 5 ) , |
| 84 |
BaseDepth( "baseDepth" ) , |
| 85 |
BaseVCycles( "baseVCycles" , 4 ) , |
| 86 |
#ifndef FAST_COMPILE |
| 87 |
BType( "bType" , DEFAULT_FEM_BOUNDARY+1 ) , |
| 88 |
Dimension( "dim" , DEFAULT_DIMENSION ) , |
| 89 |
#endif // !FAST_COMPILE |
| 90 |
MaxMemoryGB( "maxMemory" , 0 ) , |
| 91 |
ParallelType( "parallel" , 0 ) , |
| 92 |
ScheduleType( "schedule" , (int)ThreadPool::Schedule ) , |
| 93 |
ThreadChunkSize( "chunkSize" , (int)ThreadPool::ChunkSize ); |
| 94 |
|
| 95 |
CmdLineParameter< float > |
| 96 |
Scale( "scale" , 1.1f ) , |
| 97 |
Width( "width" , 0.f ) , |
| 98 |
CGSolverAccuracy( "cgAccuracy" , 1e-3f ) , |
| 99 |
IsoValue( "iso" , 0.f ) , |
| 100 |
ValueWeight ( "valueWeight" , 1000.f ) , |
| 101 |
GradientWeight( "gradientWeight" , 1.f ) , |
| 102 |
LapWeight ( "lapWeight" , 0.f ) , |
| 103 |
BiLapWeight ( "biLapWeight" , 1.f ); |
| 104 |
|
| 105 |
CmdLineReadable* params[] = |
| 106 |
{ |
| 107 |
#ifndef FAST_COMPILE |
| 108 |
&Degree , &BType , &Dimension , |
| 109 |
#endif // !FAST_COMPILE |
| 110 |
&SolveDepth , |
| 111 |
&InValues , &InGradients , |
| 112 |
&Out , &Depth , &Transform , |
| 113 |
&Width , |
| 114 |
&Scale , &Verbose , &CGSolverAccuracy , |
| 115 |
&NonManifold , &PolygonMesh , &ASCII , &ShowResidual , |
| 116 |
&ValueWeight , &GradientWeight , |
| 117 |
&LapWeight , &BiLapWeight , |
| 118 |
&Grid , |
| 119 |
&Tree , |
| 120 |
&FullDepth , |
| 121 |
&BaseDepth , &BaseVCycles , |
| 122 |
&Iters , |
| 123 |
&IsoValue , |
| 124 |
&PrimalGrid , |
| 125 |
&ExactInterpolation , |
| 126 |
&Performance , |
| 127 |
&MaxMemoryGB , |
| 128 |
&InCore , |
| 129 |
&ParallelType , |
| 130 |
&ScheduleType , |
| 131 |
&ThreadChunkSize , |
| 132 |
&NonLinearFit , |
| 133 |
NULL |
| 134 |
}; |
| 135 |
|
| 136 |
void ShowUsage(char* ex) |
| 137 |
{ |
| 138 |
printf( "Usage: %s\n" , ex ); |
| 139 |
printf( "\t[--%s <input point values>]\n" , InValues.name ); |
| 140 |
printf( "\t[--%s <input point gradients>]\n" , InGradients.name ); |
| 141 |
printf( "\t[--%s <ouput mesh>]\n" , Out.name ); |
| 142 |
printf( "\t[--%s <ouput grid>]\n" , Grid.name ); |
| 143 |
printf( "\t[--%s <ouput fem tree>]\n" , Tree.name ); |
| 144 |
#ifndef FAST_COMPILE |
| 145 |
printf( "\t[--%s <dimension>=%d]\n" , Dimension.name , Dimension.value ); |
| 146 |
printf( "\t[--%s <b-spline degree>=%d]\n" , Degree.name , Degree.value ); |
| 147 |
printf( "\t[--%s <boundary type>=%d]\n" , BType.name , BType.value ); |
| 148 |
for( int i=0 ; i<BOUNDARY_COUNT ; i++ ) printf( "\t\t%d] %s\n" , i+1 , BoundaryNames[i] ); |
| 149 |
#endif // !FAST_COMPILE |
| 150 |
printf( "\t[--%s <maximum reconstruction depth>=%d]\n" , Depth.name , Depth.value ); |
| 151 |
printf( "\t[--%s <maximum solution depth>=%d]\n" , SolveDepth.name , SolveDepth.value ); |
| 152 |
printf( "\t[--%s <grid width>]\n" , Width.name ); |
| 153 |
printf( "\t[--%s <full depth>=%d]\n" , FullDepth.name , FullDepth.value ); |
| 154 |
printf( "\t[--%s <coarse MG solver depth>]\n" , BaseDepth.name ); |
| 155 |
printf( "\t[--%s <coarse MG solver v-cycles>=%d]\n" , BaseVCycles.name , BaseVCycles.value ); |
| 156 |
printf( "\t[--%s <scale factor>=%f]\n" , Scale.name , Scale.value ); |
| 157 |
printf( "\t[--%s <zero-crossing weight>=%.3e]\n" , ValueWeight.name , ValueWeight.value ); |
| 158 |
printf( "\t[--%s <gradient weight>=%.3e]\n" , GradientWeight.name , GradientWeight.value ); |
| 159 |
printf( "\t[--%s <laplacian weight>=%.3e]\n" , LapWeight.name , LapWeight.value ); |
| 160 |
printf( "\t[--%s <bi-laplacian weight>=%.3e]\n" , BiLapWeight.name , BiLapWeight.value ); |
| 161 |
printf( "\t[--%s <iterations>=%d]\n" , Iters.name , Iters.value ); |
| 162 |
printf( "\t[--%s]\n" , ExactInterpolation.name ); |
| 163 |
printf( "\t[--%s <parallel type>=%d]\n" , ParallelType.name , ParallelType.value ); |
| 164 |
for( size_t i=0 ; i<ThreadPool::ParallelNames.size() ; i++ ) printf( "\t\t%d] %s\n" , (int)i , ThreadPool::ParallelNames[i].c_str() ); |
| 165 |
printf( "\t[--%s <schedue type>=%d]\n" , ScheduleType.name , ScheduleType.value ); |
| 166 |
for( size_t i=0 ; i<ThreadPool::ScheduleNames.size() ; i++ ) printf( "\t\t%d] %s\n" , (int)i , ThreadPool::ScheduleNames[i].c_str() ); |
| 167 |
printf( "\t[--%s <thread chunk size>=%d]\n" , ThreadChunkSize.name , ThreadChunkSize.value ); |
| 168 |
printf( "\t[--%s <cg solver accuracy>=%g]\n" , CGSolverAccuracy.name , CGSolverAccuracy.value ); |
| 169 |
printf( "\t[--%s <maximum memory (in GB)>=%d]\n" , MaxMemoryGB.name , MaxMemoryGB.value ); |
| 170 |
printf( "\t[--%s <iso-value>=%f]\n" , IsoValue.name , IsoValue.value ); |
| 171 |
printf( "\t[--%s]\n" , Performance.name ); |
| 172 |
printf( "\t[--%s]\n" , PrimalGrid.name ); |
| 173 |
printf( "\t[--%s]\n" , NonManifold.name ); |
| 174 |
printf( "\t[--%s]\n" , NonLinearFit.name ); |
| 175 |
printf( "\t[--%s]\n" , ASCII.name ); |
| 176 |
printf( "\t[--%s]\n" , InCore.name ); |
| 177 |
printf( "\t[--%s]\n" , Verbose.name ); |
| 178 |
} |
| 179 |
|
| 180 |
|
| 181 |
template< class Real , unsigned int Dim > |
| 182 |
XForm< Real , Dim+1 > GetBoundingBoxXForm( Point< Real , Dim > min , Point< Real , Dim > max , Real scaleFactor ) |
| 183 |
{ |
| 184 |
Point< Real , Dim > center = ( max + min ) / 2; |
| 185 |
Real scale = max[0] - min[0]; |
| 186 |
for( int d=1 ; d<Dim ; d++ ) scale = std::max< Real >( scale , max[d]-min[d] ); |
| 187 |
scale *= scaleFactor; |
| 188 |
for( int i=0 ; i<Dim ; i++ ) center[i] -= scale/2; |
| 189 |
XForm< Real , Dim+1 > tXForm = XForm< Real , Dim+1 >::Identity() , sXForm = XForm< Real , Dim+1 >::Identity(); |
| 190 |
for( int i=0 ; i<Dim ; i++ ) sXForm(i,i) = (Real)(1./scale ) , tXForm(Dim,i) = -center[i]; |
| 191 |
return sXForm * tXForm; |
| 192 |
} |
| 193 |
template< class Real , unsigned int Dim > |
| 194 |
XForm< Real , Dim+1 > GetBoundingBoxXForm( Point< Real , Dim > min , Point< Real , Dim > max , Real width , Real scaleFactor , int& depth ) |
| 195 |
{ |
| 196 |
// Get the target resolution (along the largest dimension) |
| 197 |
Real resolution = ( max[0]-min[0] ) / width; |
| 198 |
for( int d=1 ; d<Dim ; d++ ) resolution = std::max< Real >( resolution , ( max[d]-min[d] ) / width ); |
| 199 |
resolution *= scaleFactor; |
| 200 |
depth = 0; |
| 201 |
while( (1<<depth)<resolution ) depth++; |
| 202 |
|
| 203 |
Point< Real , Dim > center = ( max + min ) / 2; |
| 204 |
Real scale = (1<<depth) * width; |
| 205 |
|
| 206 |
for( int i=0 ; i<Dim ; i++ ) center[i] -= scale/2; |
| 207 |
XForm< Real , Dim+1 > tXForm = XForm< Real , Dim+1 >::Identity() , sXForm = XForm< Real , Dim+1 >::Identity(); |
| 208 |
for( int i=0 ; i<Dim ; i++ ) sXForm(i,i) = (Real)(1./scale ) , tXForm(Dim,i) = -center[i]; |
| 209 |
return sXForm * tXForm; |
| 210 |
} |
| 211 |
|
| 212 |
template< typename Real , unsigned int Dim , typename FunctionValueType > |
| 213 |
using InputPointStreamInfo = typename FEMTreeInitializer< Dim , Real >::template InputPointStream< FunctionValueType >; |
| 214 |
|
| 215 |
template< typename Real , unsigned int Dim , typename FunctionValueType > |
| 216 |
using InputPointStream = typename InputPointStreamInfo< Real , Dim , FunctionValueType >::StreamType; |
| 217 |
|
| 218 |
template< class Real , unsigned int Dim , typename FunctionValueType > |
| 219 |
XForm< Real , Dim+1 > GetPointXForm( InputPointStream< Real , Dim , FunctionValueType > &stream , Real width , Real scaleFactor , int& depth ) |
| 220 |
{ |
| 221 |
Point< Real , Dim > min , max; |
| 222 |
InputPointStreamInfo< Real , Dim , FunctionValueType >::BoundingBox( stream , min , max ); |
| 223 |
return GetBoundingBoxXForm( min , max , width , scaleFactor , depth ); |
| 224 |
} |
| 225 |
template< class Real , unsigned int Dim , typename FunctionValueType > |
| 226 |
XForm< Real , Dim+1 > GetPointXForm( InputPointStream< Real , Dim , FunctionValueType > &stream , Real scaleFactor ) |
| 227 |
{ |
| 228 |
Point< Real , Dim > min , max; |
| 229 |
InputPointStreamInfo< Real , Dim , FunctionValueType >::BoundingBox( stream , min , max ); |
| 230 |
return GetBoundingBoxXForm( min , max , scaleFactor ); |
| 231 |
} |
| 232 |
|
| 233 |
template< unsigned int Dim , typename Real , typename PointSampleData > struct ConstraintDual; |
| 234 |
|
| 235 |
template< unsigned int Dim , typename Real > |
| 236 |
struct ConstraintDual< Dim , Real , DirectSum< Real , Real > > |
| 237 |
{ |
| 238 |
typedef DirectSum< Real , Real > PointSampleData; |
| 239 |
Real vWeight; |
| 240 |
ConstraintDual( Real v) : vWeight(v){ } |
| 241 |
CumulativeDerivativeValues< Real , Dim , 0 > operator()( const Point< Real , Dim > &p , const PointSampleData& data ) const |
| 242 |
{ |
| 243 |
Real value = data.template get<0>(); |
| 244 |
CumulativeDerivativeValues< Real , Dim , 0 > cdv; |
| 245 |
cdv[0] = value*vWeight; |
| 246 |
return cdv; |
| 247 |
} |
| 248 |
}; |
| 249 |
|
| 250 |
template< unsigned int Dim , typename Real > |
| 251 |
struct ConstraintDual< Dim , Real , DirectSum< Real , Point< Real , Dim > > > |
| 252 |
{ |
| 253 |
typedef DirectSum< Real , Point< Real , Dim > > PointSampleData; |
| 254 |
Real gWeight; |
| 255 |
ConstraintDual( Real g ) : gWeight(g) { } |
| 256 |
CumulativeDerivativeValues< Real , Dim , 1 > operator()( const Point< Real , Dim >& p , const PointSampleData& data ) const |
| 257 |
{ |
| 258 |
Point< Real , Dim > gradient = data.template get<0>(); |
| 259 |
CumulativeDerivativeValues< Real , Dim , 1 > cdv; |
| 260 |
for( int d=0 ; d<Dim ; d++ ) cdv[1+d] = gradient[d]*gWeight; |
| 261 |
return cdv; |
| 262 |
} |
| 263 |
}; |
| 264 |
|
| 265 |
template< unsigned int Dim , typename Real , typename TotalPointSampleData > struct SystemDual; |
| 266 |
|
| 267 |
template< unsigned int Dim , typename Real > |
| 268 |
struct SystemDual< Dim , Real , DirectSum< Real , Real > > |
| 269 |
{ |
| 270 |
CumulativeDerivativeValues< Real , Dim , 0 > weight; |
| 271 |
SystemDual( Real v ){ weight[0] = v; } |
| 272 |
CumulativeDerivativeValues< Real , Dim , 0 > operator()( Point< Real , Dim > p , const DirectSum< Real , Real > &data , const CumulativeDerivativeValues< Real , Dim , 0 > &dValues ) const |
| 273 |
{ |
| 274 |
return dValues * weight; |
| 275 |
} |
| 276 |
CumulativeDerivativeValues< double , Dim , 0 > operator()( Point< Real , Dim > p , const DirectSum< Real , Real > &data , const CumulativeDerivativeValues< double , Dim , 0 > &dValues ) const |
| 277 |
{ |
| 278 |
return dValues * weight; |
| 279 |
}; |
| 280 |
}; |
| 281 |
template< unsigned int Dim > |
| 282 |
struct SystemDual< Dim , double , DirectSum< double , double > > |
| 283 |
{ |
| 284 |
typedef double Real; |
| 285 |
CumulativeDerivativeValues< Real , Dim , 0 > weight; |
| 286 |
SystemDual( Real v ){ weight[0] = v; } |
| 287 |
CumulativeDerivativeValues< Real , Dim , 0 > operator()( Point< Real , Dim > p , const DirectSum< double , double > &data , const CumulativeDerivativeValues< Real , Dim , 0 > &dValues ) const |
| 288 |
{ |
| 289 |
return dValues * weight; |
| 290 |
} |
| 291 |
}; |
| 292 |
|
| 293 |
template< unsigned int Dim , typename Real > |
| 294 |
struct SystemDual< Dim , Real , DirectSum< Real , Point< Real , Dim > > > |
| 295 |
{ |
| 296 |
CumulativeDerivativeValues< Real , Dim , 1 > weight; |
| 297 |
SystemDual( Real g ) |
| 298 |
{ |
| 299 |
weight[0] = 0; |
| 300 |
for( int d=0 ; d<Dim ; d++ ) weight[d+1] = g; |
| 301 |
} |
| 302 |
CumulativeDerivativeValues< Real , Dim , 1 > operator()( Point< Real , Dim > p , const DirectSum< Real , Point< Real , Dim > > &data , const CumulativeDerivativeValues< Real , Dim , 1 > &dValues ) const |
| 303 |
{ |
| 304 |
return dValues * weight; |
| 305 |
} |
| 306 |
CumulativeDerivativeValues< double , Dim , 1 > operator()( Point< Real , Dim > p , const DirectSum< Real , Point< Real , Dim > > &data , const CumulativeDerivativeValues< double , Dim , 1 > &dValues ) const |
| 307 |
{ |
| 308 |
return dValues * weight; |
| 309 |
}; |
| 310 |
}; |
| 311 |
template< unsigned int Dim > |
| 312 |
struct SystemDual< Dim , double , DirectSum< double , Point< double , Dim > > > |
| 313 |
{ |
| 314 |
typedef double Real; |
| 315 |
CumulativeDerivativeValues< Real , Dim , 1 > weight; |
| 316 |
SystemDual( Real g ) |
| 317 |
{ |
| 318 |
weight[0] = 0; |
| 319 |
for( int d=0 ; d<Dim ; d++) weight[1+d] = g; |
| 320 |
} |
| 321 |
CumulativeDerivativeValues< Real , Dim , 1 > operator()( Point< Real , Dim > p , const DirectSum< Real , Point< Real , Dim > > &data , const CumulativeDerivativeValues< Real , Dim , 1 > &dValues ) const |
| 322 |
{ |
| 323 |
return dValues * weight; |
| 324 |
} |
| 325 |
}; |
| 326 |
|
| 327 |
template< typename Real , unsigned int ... FEMSigs > |
| 328 |
void ExtractLevelSet |
| 329 |
( |
| 330 |
UIntPack< FEMSigs ... > , |
| 331 |
FEMTree< sizeof ... ( FEMSigs ) , Real >& tree , |
| 332 |
const DenseNodeData< Real , UIntPack< FEMSigs ... > >& solution , |
| 333 |
Real isoValue , |
| 334 |
XForm< Real , sizeof...(FEMSigs)+1 > unitCubeToModel , |
| 335 |
std::vector< std::string > &comments |
| 336 |
) |
| 337 |
{ |
| 338 |
static const int Dim = sizeof ... ( FEMSigs ); |
| 339 |
typedef UIntPack< FEMSigs ... > Sigs; |
| 340 |
static const unsigned int DataSig = FEMDegreeAndBType< WEIGHT_DEGREE , BOUNDARY_FREE >::Signature; |
| 341 |
|
| 342 |
Profiler profiler(20); |
| 343 |
|
| 344 |
char tempHeader[2048]; |
| 345 |
{ |
| 346 |
char tempPath[1024]; |
| 347 |
tempPath[0] = 0; |
| 348 |
if( TempDir.set ) strcpy( tempPath , TempDir.value ); |
| 349 |
else SetTempDirectory( tempPath , sizeof(tempPath) ); |
| 350 |
if( strlen(tempPath)==0 ) sprintf( tempPath , ".%c" , FileSeparator ); |
| 351 |
if( tempPath[ strlen( tempPath )-1 ]==FileSeparator ) sprintf( tempHeader , "%sPR_" , tempPath ); |
| 352 |
else sprintf( tempHeader , "%s%cPR_" , tempPath , FileSeparator ); |
| 353 |
} |
| 354 |
|
| 355 |
// A description of the output vertex information |
| 356 |
using VInfo = Reconstructor::OutputVertexInfo< Real , Dim , false , false >; |
| 357 |
|
| 358 |
// A factory generating the output vertices |
| 359 |
using Factory = typename VInfo::Factory; |
| 360 |
Factory factory = VInfo::GetFactory(); |
| 361 |
|
| 362 |
// A backing stream for the vertices |
| 363 |
Reconstructor::OutputInputFactoryTypeStream< Real , Dim , Factory , false , true > vertexStream( factory , VInfo::Convert ); |
| 364 |
Reconstructor::OutputInputFaceStream< Dim-1 , false , true > faceStream; |
| 365 |
typename LevelSetExtractor< Real , Dim >::Stats stats; |
| 366 |
|
| 367 |
Reconstructor::TransformedOutputLevelSetVertexStream< Real , Dim > _vertexStream( unitCubeToModel , vertexStream ); |
| 368 |
|
| 369 |
// Extract the mesh |
| 370 |
stats = LevelSetExtractor< Real , Dim >::Extract( Sigs() , UIntPack< 0 >() , tree , ( typename FEMTree< Dim , Real >::template DensityEstimator< 0 >* )NULL , solution , isoValue , _vertexStream , faceStream , NonLinearFit.set , false , !NonManifold.set , PolygonMesh.set , false ); |
| 371 |
|
| 372 |
if( Verbose.set ) |
| 373 |
{ |
| 374 |
std::cout << "Vertices / Faces: " << vertexStream.size() << " / " << faceStream.size() << std::endl; |
| 375 |
std::cout << stats.toString() << std::endl; |
| 376 |
std::cout << "# Got faces: " << profiler << std::endl; |
| 377 |
} |
| 378 |
|
| 379 |
// Write the mesh to a .ply file |
| 380 |
std::vector< std::string > noComments; |
| 381 |
PLY::Write< Factory , node_index_type , Real , Dim >( Out.value , factory , vertexStream.size() , faceStream.size() , vertexStream , faceStream , ASCII.set ? PLY_ASCII : PLY_BINARY_NATIVE , noComments ); |
| 382 |
} |
| 383 |
|
| 384 |
template< typename Real , unsigned int Dim > |
| 385 |
void WriteGrid( const char *fileName , ConstPointer( Real ) values , unsigned int res , XForm< Real , Dim+1 > voxelToModel , bool verbose ) |
| 386 |
{ |
| 387 |
char *ext = GetFileExtension( fileName ); |
| 388 |
|
| 389 |
if( Dim==2 && ImageWriter::ValidExtension( ext ) ) |
| 390 |
{ |
| 391 |
unsigned int totalResolution = 1; |
| 392 |
for( int d=0 ; d<Dim ; d++ ) totalResolution *= res; |
| 393 |
|
| 394 |
// Compute average |
| 395 |
Real avg = 0; |
| 396 |
std::vector< Real > avgs( ThreadPool::NumThreads() , 0 ); |
| 397 |
ThreadPool::ParallelFor( 0 , totalResolution , [&]( unsigned int thread , size_t i ){ avgs[thread] += values[i]; } ); |
| 398 |
for( unsigned int t=0 ; t<ThreadPool::NumThreads() ; t++ ) avg += avgs[t]; |
| 399 |
avg /= (Real)totalResolution; |
| 400 |
|
| 401 |
// Compute standard deviation |
| 402 |
Real std = 0; |
| 403 |
std::vector< Real > stds( ThreadPool::NumThreads() , 0 ); |
| 404 |
ThreadPool::ParallelFor( 0 , totalResolution , [&]( unsigned int thread , size_t i ){ stds[thread] += ( values[i] - avg ) * ( values[i] - avg ); } ); |
| 405 |
for( unsigned int t=0 ; t<ThreadPool::NumThreads() ; t++ ) std += stds[t]; |
| 406 |
std = (Real)sqrt( std / totalResolution ); |
| 407 |
|
| 408 |
if( verbose ) |
| 409 |
{ |
| 410 |
printf( "Grid to image: [%.2f,%.2f] -> [0,255]\n" , avg - 2*std , avg + 2*std ); |
| 411 |
printf( "Transform:\n" ); |
| 412 |
for( int i=0 ; i<Dim+1 ; i++ ) |
| 413 |
{ |
| 414 |
printf( "\t" ); |
| 415 |
for( int j=0 ; j<Dim+1 ; j++ ) printf( " %f" , voxelToModel(j,i) ); |
| 416 |
printf( "\n" ); |
| 417 |
} |
| 418 |
} |
| 419 |
|
| 420 |
unsigned char *pixels = new unsigned char[ totalResolution*3 ]; |
| 421 |
ThreadPool::ParallelFor( 0 , totalResolution , [&]( unsigned int , size_t i ) |
| 422 |
{ |
| 423 |
Real v = (Real)std::min< Real >( (Real)1. , std::max< Real >( (Real)-1. , ( values[i] - avg ) / (2*std ) ) ); |
| 424 |
v = (Real)( ( v + 1. ) / 2. * 256. ); |
| 425 |
unsigned char color = (unsigned char )std::min< Real >( (Real)255. , std::max< Real >( (Real)0. , v ) ); |
| 426 |
for( int c=0 ; c<3 ; c++ ) pixels[i*3+c ] = color; |
| 427 |
} |
| 428 |
); |
| 429 |
ImageWriter::Write( fileName , pixels , res , res , 3 ); |
| 430 |
delete[] pixels; |
| 431 |
} |
| 432 |
else if( !strcasecmp( ext , "iso" ) ) |
| 433 |
{ |
| 434 |
FILE *fp = fopen( fileName , "wb" ); |
| 435 |
if( !fp ) MK_THROW( "Failed to open file for writing: " , fileName ); |
| 436 |
int r = (int)res; |
| 437 |
fwrite( &r , sizeof(int) , 1 , fp ); |
| 438 |
size_t count = 1; |
| 439 |
for( unsigned int d=0 ; d<Dim ; d++ ) count *= res; |
| 440 |
fwrite( values , sizeof(Real) , count , fp ); |
| 441 |
fclose( fp ); |
| 442 |
} |
| 443 |
else |
| 444 |
{ |
| 445 |
unsigned int _res[Dim]; |
| 446 |
for( int d=0 ; d<Dim ; d++ ) _res[d] = res; |
| 447 |
RegularGrid< Real , Dim >::Write( fileName , _res , values , voxelToModel ); |
| 448 |
} |
| 449 |
delete[] ext; |
| 450 |
} |
| 451 |
|
| 452 |
template< class Real , unsigned int ... FEMSigs > |
| 453 |
void Execute( UIntPack< FEMSigs ... > ) |
| 454 |
{ |
| 455 |
static const int Dim = sizeof ... ( FEMSigs ); |
| 456 |
typedef UIntPack< FEMSigs ... > Sigs; |
| 457 |
typedef UIntPack< FEMSignature< FEMSigs >::Degree ... > Degrees; |
| 458 |
typedef UIntPack< FEMDegreeAndBType< WEIGHT_DEGREE , DerivativeBoundary< FEMSignature< FEMSigs >::BType , 1 >::BType >::Signature ... > DataSigs; |
| 459 |
typedef typename FEMTree< Dim , Real >::template DensityEstimator< WEIGHT_DEGREE > DensityEstimator; |
| 460 |
|
| 461 |
typedef typename FEMTree< Dim , Real >::template InterpolationInfo< Real , 0 > ValueInterpolationInfo; |
| 462 |
typedef typename FEMTree< Dim , Real >::template InterpolationInfo< Real , 1 > GradientInterpolationInfo; |
| 463 |
|
| 464 |
// The factory for constructing the function's values |
| 465 |
typedef VertexFactory::Factory< Real , VertexFactory::ValueFactory< Real > > FunctionValueFactory; |
| 466 |
// The factory for constructing the function's gradients |
| 467 |
typedef VertexFactory::Factory< Real , VertexFactory::NormalFactory< Real , Dim > > FunctionGradientFactory; |
| 468 |
|
| 469 |
// The factory for constructing the value data |
| 470 |
typedef VertexFactory::Factory< Real , VertexFactory::PositionFactory< Real , Dim > , FunctionValueFactory > InputSampleValueFactory; |
| 471 |
// The factory for constructing the gradient data |
| 472 |
typedef VertexFactory::Factory< Real , VertexFactory::PositionFactory< Real , Dim > , FunctionGradientFactory > InputSampleGradientFactory; |
| 473 |
|
| 474 |
// The type of the function values |
| 475 |
typedef typename FunctionValueFactory::VertexType FunctionValueType; |
| 476 |
// The type of the function gradients |
| 477 |
typedef typename FunctionGradientFactory::VertexType FunctionGradientType; |
| 478 |
|
| 479 |
// The type of the input value |
| 480 |
typedef typename InputSampleValueFactory::VertexType InputSampleValueType; |
| 481 |
// The type of the input gradient |
| 482 |
typedef typename InputSampleGradientFactory::VertexType InputSampleGradientType; |
| 483 |
|
| 484 |
typedef InputDataStream< InputSampleValueType > InputPointValueStream; |
| 485 |
typedef InputDataStream< InputSampleGradientType > InputPointGradientStream; |
| 486 |
|
| 487 |
struct XInputPointValueStream : public InputDataStream< InputSampleValueType > |
| 488 |
{ |
| 489 |
InputDataStream< InputSampleValueType > &stream; |
| 490 |
XForm< Real , Dim+1 > pointTransform; |
| 491 |
XInputPointValueStream( InputDataStream< InputSampleValueType > &stream , XForm< Real , Dim+1 > modelToUnitCube ) : stream(stream) , pointTransform( modelToUnitCube ){} |
| 492 |
bool read( InputSampleValueType &s ) |
| 493 |
{ |
| 494 |
if( stream.read( s ) ){ s.template get<0>() = pointTransform * s.template get<0>() ; return true; } |
| 495 |
else return false; |
| 496 |
} |
| 497 |
void reset( void ){ return stream.reset(); } |
| 498 |
}; |
| 499 |
struct XInputPointGradientStream : public InputDataStream< InputSampleGradientType > |
| 500 |
{ |
| 501 |
// G(p) = F( A*p ) |
| 502 |
// F(p+d) = F(p) + < \nabla F(p) , d > |
| 503 |
// G(p+d) = F( A*p ) + < \nabla F(A*p) , A*d > |
| 504 |
// = G(p) + < A^t * \nabla F(A*p) , d > |
| 505 |
// => \nabla G(p) = A^t * \nabla F(A*p) |
| 506 |
|
| 507 |
InputDataStream< InputSampleGradientType > &stream; |
| 508 |
XForm< Real , Dim+1 > pointTransform; |
| 509 |
XForm< Real , Dim > gradientTransform; |
| 510 |
XInputPointGradientStream( InputDataStream< InputSampleGradientType > &stream , XForm< Real , Dim+1 > modelToUnitCube ) : stream(stream) |
| 511 |
{ |
| 512 |
pointTransform = modelToUnitCube; |
| 513 |
gradientTransform = XForm< Real , Dim >( pointTransform ).inverse().transpose(); |
| 514 |
} |
| 515 |
bool read( InputSampleGradientType &s ) |
| 516 |
{ |
| 517 |
if( stream.read( s ) ) |
| 518 |
{ |
| 519 |
s.template get<0>() = pointTransform * s.template get<0>(); |
| 520 |
s.template get<1>() = gradientTransform * s.template get<1>().template get<0>(); |
| 521 |
return true; |
| 522 |
} |
| 523 |
else return false; |
| 524 |
} |
| 525 |
void reset( void ){ return stream.reset(); } |
| 526 |
}; |
| 527 |
FunctionValueFactory functionValueFactory; |
| 528 |
FunctionGradientFactory functionGradientFactory; |
| 529 |
|
| 530 |
InputSampleValueFactory inputSampleValueFactory( VertexFactory::PositionFactory< Real , Dim >() , functionValueFactory ); |
| 531 |
InputSampleGradientFactory inputSampleGradientFactory( VertexFactory::PositionFactory< Real , Dim >() , functionGradientFactory ); |
| 532 |
|
| 533 |
typedef RegularTreeNode< Dim , FEMTreeNodeData , depth_and_offset_type > FEMTreeNode; |
| 534 |
std::vector< std::string > comments; |
| 535 |
if( Verbose.set ) |
| 536 |
{ |
| 537 |
std::cout << "***********************************************" << std::endl; |
| 538 |
std::cout << "***********************************************" << std::endl; |
| 539 |
std::cout << "** Running Point Interpolant (Version " << ADAPTIVE_SOLVERS_VERSION << ") **" << std::endl; |
| 540 |
std::cout << "***********************************************" << std::endl; |
| 541 |
std::cout << "***********************************************" << std::endl; |
| 542 |
} |
| 543 |
|
| 544 |
ThreadPool::ParallelizationType= (ThreadPool::ParallelType)ParallelType.value; |
| 545 |
|
| 546 |
XForm< Real , Dim+1 > modelToUnitCube , unitCubeToModel; |
| 547 |
if( Transform.set ) |
| 548 |
{ |
| 549 |
FILE* fp = fopen( Transform.value , "r" ); |
| 550 |
if( !fp ) |
| 551 |
{ |
| 552 |
MK_WARN( "Could not read x-form from: " , Transform.value ); |
| 553 |
modelToUnitCube = XForm< Real , Dim+1 >::Identity(); |
| 554 |
} |
| 555 |
else |
| 556 |
{ |
| 557 |
for( int i=0 ; i<Dim+1 ; i++ ) for( int j=0 ; j<Dim+1 ; j++ ) |
| 558 |
{ |
| 559 |
float f; |
| 560 |
if( fscanf( fp , " %f " , &f )!=1 ) MK_THROW( "Failed to read xform" ); |
| 561 |
modelToUnitCube(i,j) = (Real)f; |
| 562 |
} |
| 563 |
fclose( fp ); |
| 564 |
} |
| 565 |
} |
| 566 |
else modelToUnitCube = XForm< Real , Dim+1 >::Identity(); |
| 567 |
|
| 568 |
char str[1024]; |
| 569 |
for( int i=0 ; params[i] ; i++ ) |
| 570 |
if( params[i]->set ) |
| 571 |
{ |
| 572 |
params[i]->writeValue( str ); |
| 573 |
if( Verbose.set ) |
| 574 |
{ |
| 575 |
if( strlen( str ) ) std::cout << "\t--" << params[i]->name << " " << str << std::endl; |
| 576 |
else std::cout << "\t--" << params[i]->name << std::endl; |
| 577 |
} |
| 578 |
} |
| 579 |
|
| 580 |
double startTime = Time(); |
| 581 |
|
| 582 |
FEMTree< Dim , Real > tree( MEMORY_ALLOCATOR_BLOCK_SIZE ); |
| 583 |
Profiler profiler(20); |
| 584 |
|
| 585 |
if( Depth.set && Width.value>0 ) |
| 586 |
{ |
| 587 |
MK_WARN( "Both --" , Depth.name , " and --" , Width.name , " set, ignoring --" , Width.name ); |
| 588 |
Width.value = 0; |
| 589 |
} |
| 590 |
|
| 591 |
size_t pointValueCount , pointGradientCount; |
| 592 |
|
| 593 |
std::vector< typename FEMTree< Dim , Real >::PointSample > *valueSamples = NULL; |
| 594 |
std::vector< FunctionValueType > *valueSampleData = NULL; |
| 595 |
std::vector< typename FEMTree< Dim , Real >::PointSample > *gradientSamples = NULL; |
| 596 |
std::vector< FunctionGradientType > *gradientSampleData = NULL; |
| 597 |
|
| 598 |
// Read in the samples |
| 599 |
{ |
| 600 |
profiler.reset(); |
| 601 |
InputPointValueStream *pointValueStream = NULL; |
| 602 |
InputPointGradientStream *pointGradientStream = NULL; |
| 603 |
Point< Real , Dim > valueMin , valueMax , gradientMin , gradientMax; |
| 604 |
std::vector< InputSampleValueType > inCorePointsAndValues; |
| 605 |
std::vector< InputSampleGradientType > inCorePointsAndGradients; |
| 606 |
|
| 607 |
if( ValueWeight.value>0 ) |
| 608 |
{ |
| 609 |
char* ext = GetFileExtension( InValues.value ); |
| 610 |
valueSampleData = new std::vector< FunctionValueType >(); |
| 611 |
if( InCore.set ) |
| 612 |
{ |
| 613 |
InputPointValueStream *_pointValueStream; |
| 614 |
if ( !strcasecmp( ext , "bnpts" ) ) _pointValueStream = new BinaryInputDataStream< InputSampleValueFactory>( InValues.value , inputSampleValueFactory ); |
| 615 |
else if( !strcasecmp( ext , "ply" ) ) _pointValueStream = new PLYInputDataStream< InputSampleValueFactory>( InValues.value , inputSampleValueFactory ); |
| 616 |
else _pointValueStream = new ASCIIInputDataStream< InputSampleValueFactory>( InValues.value , inputSampleValueFactory ); |
| 617 |
InputSampleValueType s = inputSampleValueFactory(); |
| 618 |
while( _pointValueStream->read( s ) ) inCorePointsAndValues.push_back( s ); |
| 619 |
delete _pointValueStream; |
| 620 |
|
| 621 |
pointValueStream = new VectorBackedInputDataStream< InputSampleValueType >( inCorePointsAndValues ); |
| 622 |
} |
| 623 |
else |
| 624 |
{ |
| 625 |
if ( !strcasecmp( ext , "bnpts" ) ) pointValueStream = new BinaryInputDataStream< InputSampleValueFactory>( InValues.value , inputSampleValueFactory ); |
| 626 |
else if( !strcasecmp( ext , "ply" ) ) pointValueStream = new PLYInputDataStream< InputSampleValueFactory>( InValues.value , inputSampleValueFactory ); |
| 627 |
else pointValueStream = new ASCIIInputDataStream< InputSampleValueFactory>( InValues.value , inputSampleValueFactory ); |
| 628 |
} |
| 629 |
delete[] ext; |
| 630 |
|
| 631 |
XInputPointValueStream _pointStream( *pointValueStream , modelToUnitCube ); |
| 632 |
FunctionValueType s = functionValueFactory(); |
| 633 |
InputPointStreamInfo< Real , Dim , FunctionValueType >::BoundingBox( _pointStream , s , valueMin , valueMax ); |
| 634 |
} |
| 635 |
if( GradientWeight.value>0 ) |
| 636 |
{ |
| 637 |
char* ext = GetFileExtension( InGradients.value ); |
| 638 |
gradientSampleData = new std::vector< FunctionGradientType >(); |
| 639 |
if( InCore.set ) |
| 640 |
{ |
| 641 |
InputPointGradientStream *_pointGradientStream; |
| 642 |
if ( !strcasecmp( ext , "bnpts" ) ) _pointGradientStream = new BinaryInputDataStream< InputSampleGradientFactory>( InGradients.value , inputSampleGradientFactory ); |
| 643 |
else if( !strcasecmp( ext , "ply" ) ) _pointGradientStream = new PLYInputDataStream< InputSampleGradientFactory>( InGradients.value , inputSampleGradientFactory ); |
| 644 |
else _pointGradientStream = new ASCIIInputDataStream< InputSampleGradientFactory>( InGradients.value , inputSampleGradientFactory ); |
| 645 |
InputSampleGradientType s = inputSampleGradientFactory(); |
| 646 |
while( _pointGradientStream->read( s ) ) inCorePointsAndGradients.push_back( s ); |
| 647 |
delete _pointGradientStream; |
| 648 |
|
| 649 |
pointGradientStream = new VectorBackedInputDataStream< InputSampleGradientType >( inCorePointsAndGradients ); |
| 650 |
} |
| 651 |
else |
| 652 |
{ |
| 653 |
if ( !strcasecmp( ext , "bnpts" ) ) pointGradientStream = new BinaryInputDataStream< InputSampleGradientFactory>( InGradients.value , inputSampleGradientFactory ); |
| 654 |
else if( !strcasecmp( ext , "ply" ) ) pointGradientStream = new PLYInputDataStream< InputSampleGradientFactory>( InGradients.value , inputSampleGradientFactory ); |
| 655 |
else pointGradientStream = new ASCIIInputDataStream< InputSampleGradientFactory>( InGradients.value , inputSampleGradientFactory ); |
| 656 |
} |
| 657 |
delete[] ext; |
| 658 |
|
| 659 |
XInputPointGradientStream _pointStream( *pointGradientStream , modelToUnitCube ); |
| 660 |
FunctionGradientType s = functionGradientFactory(); |
| 661 |
InputPointStreamInfo< Real , Dim , FunctionGradientType >::BoundingBox( _pointStream , s , gradientMin , gradientMax ); |
| 662 |
} |
| 663 |
|
| 664 |
{ |
| 665 |
Point< Real , Dim > min , max; |
| 666 |
if( ValueWeight.value>0 && GradientWeight.value>0 ) for( int d=0 ; d<Dim ; d++ ) min[d] = std::min< Real >( valueMin[d] , gradientMin[d] ) , max[d] = std::max< Real >( valueMax[d] , gradientMax[d] ); |
| 667 |
else if( ValueWeight.value>0 ) min = valueMin , max = valueMax; |
| 668 |
else if( GradientWeight.value>0 ) min = gradientMin , max = gradientMax; |
| 669 |
|
| 670 |
if( Width.value>0 ) |
| 671 |
{ |
| 672 |
modelToUnitCube = GetBoundingBoxXForm( min , max , (Real)Width.value , (Real)( Scale.value>0 ? Scale.value : 1. ) , Depth.value ) * modelToUnitCube; |
| 673 |
if( !SolveDepth.set || SolveDepth.value==-1 ) SolveDepth.value = Depth.value; |
| 674 |
if( SolveDepth.value>Depth.value ) |
| 675 |
{ |
| 676 |
MK_WARN( "Solution depth cannot exceed system depth: " , SolveDepth.value , " <= " , Depth.value ); |
| 677 |
SolveDepth.value = Depth.value; |
| 678 |
} |
| 679 |
if( FullDepth.value>Depth.value ) |
| 680 |
{ |
| 681 |
MK_WARN( "Full depth cannot exceed system depth: " , FullDepth.value , " <= " , Depth.value ); |
| 682 |
FullDepth.value = Depth.value; |
| 683 |
} |
| 684 |
if( BaseDepth.value>FullDepth.value ) |
| 685 |
{ |
| 686 |
if( BaseDepth.set ) MK_WARN( "Base depth must be smaller than full depth: " , BaseDepth.value , " <= " , FullDepth.value ); |
| 687 |
BaseDepth.value = FullDepth.value; |
| 688 |
} |
| 689 |
} |
| 690 |
else modelToUnitCube = Scale.value>0 ? GetBoundingBoxXForm( min , max , (Real)Scale.value ) * modelToUnitCube : modelToUnitCube; |
| 691 |
} |
| 692 |
|
| 693 |
if( ValueWeight.value>0 ) |
| 694 |
{ |
| 695 |
valueSamples = new std::vector< typename FEMTree< Dim , Real >::PointSample >(); |
| 696 |
XInputPointValueStream _pointStream( *pointValueStream , modelToUnitCube ); |
| 697 |
auto ProcessData = []( const Point< Real , Dim > &p , FunctionValueType &d ){ return (Real)1.; }; |
| 698 |
FunctionValueType zeroValue = functionValueFactory(); |
| 699 |
typename FEMTreeInitializer< Dim , Real >::StreamInitializationData sid; |
| 700 |
{ |
| 701 |
using ExternalType = std::tuple< Point< Real , Dim > , FunctionValueType >; |
| 702 |
using InternalType = std::tuple< InputSampleValueType >; |
| 703 |
auto converter = []( const InternalType &iType ) |
| 704 |
{ |
| 705 |
ExternalType xType; |
| 706 |
std::get< 0 >( xType ) = std::get< 0 >( iType ).template get<0>(); |
| 707 |
std::get< 1 >( xType ) = std::get< 0 >( iType ).template get<1>(); |
| 708 |
return xType; |
| 709 |
}; |
| 710 |
InputDataStreamConverter< InternalType , ExternalType > __pointStream( _pointStream , converter , InputSampleValueType( Point< Real , Dim >() , zeroValue ) ); |
| 711 |
pointValueCount = FEMTreeInitializer< Dim , Real >::template Initialize< FunctionValueType >( sid , tree.spaceRoot() , __pointStream , zeroValue , Depth.value , *valueSamples , *valueSampleData , tree.nodeAllocators.size() ? tree.nodeAllocators[0] : NULL , tree.initializer() , ProcessData ); |
| 712 |
} |
| 713 |
delete pointValueStream; |
| 714 |
} |
| 715 |
else pointValueCount = 0; |
| 716 |
|
| 717 |
if( GradientWeight.value>0 ) |
| 718 |
{ |
| 719 |
gradientSamples = new std::vector< typename FEMTree< Dim , Real >::PointSample >(); |
| 720 |
XInputPointGradientStream _pointStream( *pointGradientStream , modelToUnitCube ); |
| 721 |
auto ProcessData = []( const Point< Real , Dim > &p , FunctionGradientType &d ){ return (Real)1.; }; |
| 722 |
FunctionGradientType zeroGradient = functionGradientFactory(); |
| 723 |
typename FEMTreeInitializer< Dim , Real >::StreamInitializationData sid; |
| 724 |
{ |
| 725 |
using ExternalType = std::tuple< Point< Real , Dim > , FunctionGradientType >; |
| 726 |
using InternalType = std::tuple< InputSampleGradientType >; |
| 727 |
auto converter = []( const InternalType &iType ) |
| 728 |
{ |
| 729 |
ExternalType xType; |
| 730 |
std::get< 0 >( xType ) = std::get< 0 >( iType ).template get<0>(); |
| 731 |
std::get< 1 >( xType ) = std::get< 0 >( iType ).template get<1>(); |
| 732 |
return xType; |
| 733 |
}; |
| 734 |
InputDataStreamConverter< InternalType , ExternalType > __pointGradientStream( _pointStream , converter , InputSampleGradientType( Point< Real , Dim >() , zeroGradient ) ); |
| 735 |
pointGradientCount = FEMTreeInitializer< Dim , Real >::template Initialize< FunctionGradientType >( sid , tree.spaceRoot() , __pointGradientStream , zeroGradient , Depth.value , *gradientSamples , *gradientSampleData , tree.nodeAllocators.size() ? tree.nodeAllocators[0] : NULL , tree.initializer() , ProcessData ); |
| 736 |
} |
| 737 |
delete pointGradientStream; |
| 738 |
} |
| 739 |
else pointGradientCount = 0; |
| 740 |
|
| 741 |
unitCubeToModel = modelToUnitCube.inverse(); |
| 742 |
|
| 743 |
if( Verbose.set ) |
| 744 |
{ |
| 745 |
if( valueSamples ) std::cout << "Input Value Points / Value Samples: " << pointValueCount << " / " << valueSamples->size() << std::endl; |
| 746 |
if( gradientSamples ) std::cout << "Input Gradient Points / Gradient Samples: " << pointGradientCount << " / " << gradientSamples->size() << std::endl; |
| 747 |
std::cout << "# Read input into tree: " << profiler << std::endl; |
| 748 |
} |
| 749 |
} |
| 750 |
|
| 751 |
DenseNodeData< Real , Sigs > solution; |
| 752 |
{ |
| 753 |
DenseNodeData< Real , Sigs > constraints; |
| 754 |
ValueInterpolationInfo *valueInterpolationInfo = NULL; |
| 755 |
GradientInterpolationInfo *gradientInterpolationInfo = NULL; |
| 756 |
int solveDepth = Depth.value; |
| 757 |
|
| 758 |
tree.resetNodeIndices( 0 ); |
| 759 |
|
| 760 |
if( ValueWeight.value>0 ) |
| 761 |
{ |
| 762 |
if( ExactInterpolation.set ) valueInterpolationInfo = FEMTree< Dim , Real >::template InitializeExactPointAndDataInterpolationInfo< Real , FunctionValueType , 0 >( tree , *valueSamples , GetPointer( *valueSampleData ) , ConstraintDual< Dim , Real , FunctionValueType >( (Real)ValueWeight.value ) , SystemDual< Dim , Real , FunctionValueType >( (Real)ValueWeight.value ) , true , false ); |
| 763 |
else valueInterpolationInfo = FEMTree< Dim , Real >::template InitializeApproximatePointAndDataInterpolationInfo< Real , FunctionValueType , 0 >( tree , *valueSamples , GetPointer( *valueSampleData ) , ConstraintDual< Dim , Real , FunctionValueType >( (Real)ValueWeight.value ) , SystemDual< Dim , Real , FunctionValueType >( (Real)ValueWeight.value ) , true , Depth.value , 1 ); |
| 764 |
} |
| 765 |
if( GradientWeight.value>0 ) |
| 766 |
{ |
| 767 |
if( ExactInterpolation.set ) gradientInterpolationInfo = FEMTree< Dim , Real >::template InitializeExactPointAndDataInterpolationInfo< Real , FunctionGradientType , 1 >( tree , *gradientSamples , GetPointer( *gradientSampleData ) , ConstraintDual< Dim , Real , FunctionGradientType >( (Real)GradientWeight.value ) , SystemDual< Dim , Real , FunctionGradientType >( (Real)GradientWeight.value ) , true , false ); |
| 768 |
else gradientInterpolationInfo = FEMTree< Dim , Real >::template InitializeApproximatePointAndDataInterpolationInfo< Real , FunctionGradientType , 1 >( tree , *gradientSamples , GetPointer( *gradientSampleData ) , ConstraintDual< Dim , Real , FunctionGradientType >( (Real)GradientWeight.value ) , SystemDual< Dim , Real , FunctionGradientType >( (Real)GradientWeight.value ) , true , Depth.value , 1 ); |
| 769 |
} |
| 770 |
|
| 771 |
// Prepare for multigrid |
| 772 |
{ |
| 773 |
profiler.reset(); |
| 774 |
|
| 775 |
auto addNodeFunctor = [&]( int d , const int off[Dim] ){ return d<=FullDepth.value; }; |
| 776 |
if( ValueWeight.value>0 && GradientWeight.value>0 ) |
| 777 |
tree.template finalizeForMultigrid< Degrees::Max() , Degrees::Max() >( BaseDepth.value , addNodeFunctor , []( const FEMTreeNode * ){ return true; } , std::make_tuple( valueInterpolationInfo , gradientInterpolationInfo ) ); |
| 778 |
else if( ValueWeight.value>0 ) |
| 779 |
tree.template finalizeForMultigrid< Degrees::Max() , Degrees::Max() >( BaseDepth.value , addNodeFunctor , []( const FEMTreeNode * ){ return true; } , std::make_tuple( valueInterpolationInfo ) ); |
| 780 |
else if( GradientWeight.value>0 ) |
| 781 |
tree.template finalizeForMultigrid< Degrees::Max() , Degrees::Max() >( BaseDepth.value , addNodeFunctor , []( const FEMTreeNode * ){ return true; } , std::make_tuple( gradientInterpolationInfo ) ); |
| 782 |
|
| 783 |
if( Verbose.set ) std::cout << "# Finalized tree: " << profiler << std::endl; |
| 784 |
} |
| 785 |
|
| 786 |
// Add the interpolation constraints |
| 787 |
{ |
| 788 |
profiler.reset(); |
| 789 |
constraints = tree.initDenseNodeData( Sigs() ); |
| 790 |
if( ValueWeight.value>0 && GradientWeight.value>0 ) tree.addInterpolationConstraints( constraints , solveDepth , std::make_tuple( valueInterpolationInfo , gradientInterpolationInfo ) ); |
| 791 |
else if( ValueWeight.value>0 ) tree.addInterpolationConstraints( constraints , solveDepth , std::make_tuple( valueInterpolationInfo ) ); |
| 792 |
else if( GradientWeight.value>0 ) tree.addInterpolationConstraints( constraints , solveDepth , std::make_tuple( gradientInterpolationInfo ) ); |
| 793 |
if( Verbose.set ) std::cout << "#Set point constraints: " << profiler << std::endl; |
| 794 |
} |
| 795 |
|
| 796 |
if( Verbose.set ) |
| 797 |
{ |
| 798 |
std::cout << "All Nodes / Active Nodes / Ghost Nodes: " << tree.allNodes() << " / " << tree.activeNodes() << " / " << tree.ghostNodes() << std::endl; |
| 799 |
std::cout << "Memory Usage: " << float( MemoryInfo::Usage() )/(1<<20) << " MB" << std::endl; |
| 800 |
} |
| 801 |
|
| 802 |
// Solve the linear system |
| 803 |
{ |
| 804 |
profiler.reset(); |
| 805 |
typename FEMTree< Dim , Real >::SolverInfo sInfo; |
| 806 |
sInfo.cgDepth = 0 , sInfo.cascadic = true , sInfo.vCycles = 1 , sInfo.iters = Iters.value , sInfo.cgAccuracy = CGSolverAccuracy.value , sInfo.verbose = Verbose.set , sInfo.showResidual = ShowResidual.set , sInfo.showGlobalResidual = SHOW_GLOBAL_RESIDUAL_NONE , sInfo.sliceBlockSize = 1; |
| 807 |
sInfo.baseVCycles = BaseVCycles.value; |
| 808 |
typename FEMIntegrator::template System< Sigs , IsotropicUIntPack< Dim , 2 > > F( { 0. , (double)LapWeight.value , (double)BiLapWeight.value } ); |
| 809 |
if( ValueWeight.value>0 && GradientWeight.value>0 ) solution = tree.solveSystem( Sigs() , F , constraints , BaseDepth.value , SolveDepth.value , sInfo , std::make_tuple( valueInterpolationInfo , gradientInterpolationInfo ) ); |
| 810 |
else if( ValueWeight.value>0 ) solution = tree.solveSystem( Sigs() , F , constraints , BaseDepth.value , SolveDepth.value , sInfo , std::make_tuple( valueInterpolationInfo ) ); |
| 811 |
else if( GradientWeight.value>0 ) solution = tree.solveSystem( Sigs() , F , constraints , BaseDepth.value , SolveDepth.value , sInfo , std::make_tuple( gradientInterpolationInfo ) ); |
| 812 |
if( Verbose.set ) std::cout << "# Linear system solved: " << profiler << std::endl; |
| 813 |
delete valueInterpolationInfo , valueInterpolationInfo = NULL; |
| 814 |
delete gradientInterpolationInfo , gradientInterpolationInfo = NULL; |
| 815 |
} |
| 816 |
} |
| 817 |
|
| 818 |
if( Verbose.set ) |
| 819 |
{ |
| 820 |
if( valueSamples ) |
| 821 |
{ |
| 822 |
typename FEMTree< Dim , Real >::template MultiThreadedEvaluator< Sigs , 0 > evaluator( &tree , solution ); |
| 823 |
std::pair< double , double > valueStat(0,0); |
| 824 |
std::vector< std::pair< double , double > > valueStats( ThreadPool::NumThreads() , std::pair< double , double >(0,0) ); |
| 825 |
ThreadPool::ParallelFor( 0 , valueSamples->size() , [&]( unsigned int thread , size_t j ) |
| 826 |
{ |
| 827 |
ProjectiveData< Point< Real , Dim > , Real >& sample = (*valueSamples)[j].sample; |
| 828 |
Real w = sample.weight; |
| 829 |
if( w>0 ) |
| 830 |
{ |
| 831 |
CumulativeDerivativeValues< Real , Dim , 0 > values = evaluator.values( sample.data / sample.weight , thread , (*valueSamples)[j].node ); |
| 832 |
Real v1 = values[0]; |
| 833 |
Real v2 = (*valueSampleData)[j].template get<0>() / w; |
| 834 |
valueStats[ thread ].first += ( v1 - v2 ) * ( v1 - v2 ) * w; |
| 835 |
valueStats[ thread ].second += ( v1 * v1 + v2 * v2 ) * w; |
| 836 |
} |
| 837 |
} |
| 838 |
); |
| 839 |
for( unsigned int t=0 ; t<ThreadPool::NumThreads() ; t++ ) valueStat.first += valueStats[t].first , valueStat.second += valueStats[t].second; |
| 840 |
if( Verbose.set ) std::cout << "Value Error: " << sqrt( valueStat.first / valueStat.second ) << std::endl; |
| 841 |
} |
| 842 |
if( gradientSamples ) |
| 843 |
{ |
| 844 |
typename FEMTree< Dim , Real >::template MultiThreadedEvaluator< Sigs , 1 > evaluator( &tree , solution ); |
| 845 |
std::pair< double , double > gradientStat(0,0); |
| 846 |
std::vector< std::pair< double , double > > gradientStats( ThreadPool::NumThreads() , std::pair< double , double >(0,0) ); |
| 847 |
ThreadPool::ParallelFor( 0 , gradientSamples->size() , [&]( unsigned int thread , size_t j ) |
| 848 |
{ |
| 849 |
ProjectiveData< Point< Real , Dim > , Real >& sample = (*gradientSamples)[j].sample; |
| 850 |
Real w = sample.weight; |
| 851 |
if( w>0 ) |
| 852 |
{ |
| 853 |
CumulativeDerivativeValues< Real , Dim , 1 > values = evaluator.values( sample.data / sample.weight , thread , (*gradientSamples)[j].node ); |
| 854 |
Point< Real , Dim > g1; |
| 855 |
for( int d=0 ; d<Dim ; d++ ) g1[d] = values[d+1]; |
| 856 |
Point< Real , Dim > g2 = (*gradientSampleData)[j].template get<0>() / w; |
| 857 |
gradientStats[ thread ].first += Point< Real , Dim >::SquareNorm( g1 - g2 ) * w; |
| 858 |
gradientStats[ thread ].second += ( Point< Real , Dim >::SquareNorm( g1 ) + Point< Real , Dim >::SquareNorm( g2 ) ) * w; |
| 859 |
} |
| 860 |
} |
| 861 |
); |
| 862 |
for( unsigned int t=0 ; t<ThreadPool::NumThreads() ; t++ ) gradientStat.first += gradientStats[t].first , gradientStat.second += gradientStats[t].second; |
| 863 |
if( Verbose.set ) std::cout << "Gradient Error: " << sqrt( gradientStat.first / gradientStat.second ) << std::endl; |
| 864 |
} |
| 865 |
} |
| 866 |
|
| 867 |
delete valueSamples , valueSamples = NULL; |
| 868 |
delete gradientSamples , gradientSamples = NULL; |
| 869 |
delete valueSampleData , valueSampleData = NULL; |
| 870 |
delete gradientSampleData , gradientSampleData = NULL; |
| 871 |
|
| 872 |
if( Tree.set ) |
| 873 |
{ |
| 874 |
FILE* fp = fopen( Tree.value , "wb" ); |
| 875 |
if( !fp ) MK_THROW( "Failed to open file for writing: " , Tree.value ); |
| 876 |
FileStream fs( fp ); |
| 877 |
FEMTree< Dim , Real >::WriteParameter( fs ); |
| 878 |
DenseNodeData< Real , Sigs >::WriteSignatures( fs ); |
| 879 |
tree.write( fs , false ); |
| 880 |
fs.write( modelToUnitCube ); |
| 881 |
solution.write( fs ); |
| 882 |
fclose( fp ); |
| 883 |
} |
| 884 |
|
| 885 |
if( Grid.set ) |
| 886 |
{ |
| 887 |
int res = 0; |
| 888 |
profiler.reset(); |
| 889 |
Pointer( Real ) values = tree.template regularGridEvaluate< true >( solution , res , -1 , PrimalGrid.set ); |
| 890 |
size_t resolution = 1; |
| 891 |
for( int d=0 ; d<Dim ; d++ ) resolution *= res; |
| 892 |
if( Verbose.set ) std::cout << "Got grid: " << profiler << std::endl; |
| 893 |
XForm< Real , Dim+1 > voxelToUnitCube = XForm< Real , Dim+1 >::Identity(); |
| 894 |
if( PrimalGrid.set ) for( int d=0 ; d<Dim ; d++ ) voxelToUnitCube( d , d ) = (Real)( 1. / (res-1) ); |
| 895 |
else for( int d=0 ; d<Dim ; d++ ) voxelToUnitCube( d , d ) = (Real)( 1. / res ) , voxelToUnitCube( Dim , d ) = (Real)( 0.5 / res ); |
| 896 |
WriteGrid< Real , Dim >( Grid.value , values , res , unitCubeToModel * voxelToUnitCube , Verbose.set ); |
| 897 |
DeletePointer( values ); |
| 898 |
} |
| 899 |
|
| 900 |
if( Out.set ) |
| 901 |
{ |
| 902 |
if constexpr ( Dim==3 ) |
| 903 |
{ |
| 904 |
ExtractLevelSet( UIntPack< FEMSigs ... >() , tree , solution , (Real)IsoValue.value , unitCubeToModel , comments ); |
| 905 |
} |
| 906 |
else if constexpr ( Dim==2 ) |
| 907 |
{ |
| 908 |
typedef VertexFactory::PositionFactory< Real , 3 > VertexFactory; |
| 909 |
int res = 0; |
| 910 |
Pointer( Real ) values = tree.template regularGridEvaluate< true >( solution , res , -1 , true ); |
| 911 |
res--; |
| 912 |
std::vector< std::vector< int > > polygons( res * res ); |
| 913 |
std::vector< typename VertexFactory::VertexType > vertices( (res+1) * (res+1) ); |
| 914 |
|
| 915 |
for( int i=0 ; i<res ; i++ ) for( int j=0 ; j<res ; j++ ) |
| 916 |
{ |
| 917 |
std::vector< int > &poly = polygons[ j*res+i ]; |
| 918 |
poly.resize( 4 ); |
| 919 |
poly[0] = (j+0)*(res+1)+(i+0); |
| 920 |
poly[1] = (j+0)*(res+1)+(i+1); |
| 921 |
poly[2] = (j+1)*(res+1)+(i+1); |
| 922 |
poly[3] = (j+1)*(res+1)+(i+0); |
| 923 |
} |
| 924 |
for( int i=0 ; i<=res ; i++ ) for( int j=0 ; j<=res ; j++ ) |
| 925 |
{ |
| 926 |
Point< Real , Dim > p; |
| 927 |
p[0] = (Real)i/res; |
| 928 |
p[1] = (Real)j/res; |
| 929 |
p = unitCubeToModel * p; |
| 930 |
vertices[ j*(res+1)+i ] = Point< float , 3 >( (float)p[0] , (float)p[1] , values[ j*(res+1)+i ] ); |
| 931 |
} |
| 932 |
DeletePointer( values ); |
| 933 |
|
| 934 |
std::vector< std::string > noComments; |
| 935 |
PLY::WritePolygons( Out.value , VertexFactory() , vertices , polygons , ASCII.set ? PLY_ASCII : PLY_BINARY_NATIVE , noComments ); |
| 936 |
} |
| 937 |
} |
| 938 |
|
| 939 |
if( Verbose.set ) std::cout <<"# Total Solve: " << Time()-startTime << " (s), " << MemoryInfo::PeakMemoryUsageMB() << " (MB)" << std::endl; |
| 940 |
} |
| 941 |
|
| 942 |
#ifndef FAST_COMPILE |
| 943 |
template< unsigned int Dim , class Real , BoundaryType BType > |
| 944 |
void Execute( void ) |
| 945 |
{ |
| 946 |
switch( Degree.value ) |
| 947 |
{ |
| 948 |
// case 1: return Execute< Real >( IsotropicUIntPack< Dim , FEMDegreeAndBType< 1 , BType >::Signature >() ); |
| 949 |
case 2: return Execute< Real >( IsotropicUIntPack< Dim , FEMDegreeAndBType< 2 , BType >::Signature >() ); |
| 950 |
case 3: return Execute< Real >( IsotropicUIntPack< Dim , FEMDegreeAndBType< 3 , BType >::Signature >() ); |
| 951 |
// case 4: return Execute< Real >( IsotropicUIntPack< Dim , FEMDegreeAndBType< 4 , BType >::Signature >() ); |
| 952 |
default: MK_THROW( "Only B-Splines of degree 1 - 3 are supported" ); |
| 953 |
} |
| 954 |
} |
| 955 |
|
| 956 |
template< unsigned int Dim , class Real > |
| 957 |
void Execute( void ) |
| 958 |
{ |
| 959 |
switch( BType.value ) |
| 960 |
{ |
| 961 |
case BOUNDARY_FREE+1: return Execute< Dim , Real , BOUNDARY_FREE >(); |
| 962 |
case BOUNDARY_NEUMANN+1: return Execute< Dim , Real , BOUNDARY_NEUMANN >(); |
| 963 |
case BOUNDARY_DIRICHLET+1: return Execute< Dim , Real , BOUNDARY_DIRICHLET >(); |
| 964 |
default: MK_THROW( "Not a valid boundary type: " , BType.value ); |
| 965 |
} |
| 966 |
} |
| 967 |
#endif // !FAST_COMPILE |
| 968 |
|
| 969 |
int main( int argc , char* argv[] ) |
| 970 |
{ |
| 971 |
Timer timer; |
| 972 |
#ifdef ARRAY_DEBUG |
| 973 |
MK_WARN( "Array debugging enabled" ); |
| 974 |
#endif // ARRAY_DEBUG |
| 975 |
|
| 976 |
CmdLineParse( argc-1 , &argv[1] , params ); |
| 977 |
|
| 978 |
if( MaxMemoryGB.value>0 ) SetPeakMemoryMB( MaxMemoryGB.value<<10 ); |
| 979 |
ThreadPool::ChunkSize = ThreadChunkSize.value; |
| 980 |
ThreadPool::Schedule = (ThreadPool::ScheduleType)ScheduleType.value; |
| 981 |
|
| 982 |
if( !InValues.set && !InGradients.set ) |
| 983 |
{ |
| 984 |
ShowUsage( argv[0] ); |
| 985 |
MK_THROW( "Either values or gradients need to be specified" ); |
| 986 |
return 0; |
| 987 |
} |
| 988 |
if( !InValues.set ) ValueWeight.value = 0; |
| 989 |
if( !InGradients.set ) GradientWeight.value = 0; |
| 990 |
|
| 991 |
if( ValueWeight.value<0 ) MK_THROW( "Value weight must be non-negative: " , ValueWeight.value , "> 0" ); |
| 992 |
if( GradientWeight.value<0 ) MK_THROW( "Gradient weight must be non-negative: " , GradientWeight.value , "> 0" ); |
| 993 |
if( !ValueWeight.value && !GradientWeight.value ) MK_THROW( "Either value or gradient weight must be positive" ); |
| 994 |
|
| 995 |
if( LapWeight.value<0 ) MK_THROW( "Laplacian weight must be non-negative: " , LapWeight.value , " > 0" ); |
| 996 |
if( BiLapWeight.value<0 ) MK_THROW( "Bi-Laplacian weight must be non-negative: " , BiLapWeight.value , " > 0" ); |
| 997 |
if( !LapWeight.value && !BiLapWeight.value ) MK_THROW( "Eiter Laplacian or bi-Laplacian weight must be positive" ); |
| 998 |
|
| 999 |
if( !BaseDepth.set ) BaseDepth.value = FullDepth.value; |
| 1000 |
if( BaseDepth.value>FullDepth.value ) |
| 1001 |
{ |
| 1002 |
if( BaseDepth.set ) MK_WARN( "Base depth must be smaller than full depth: " , BaseDepth.value , " <= " , FullDepth.value ); |
| 1003 |
BaseDepth.value = FullDepth.value; |
| 1004 |
} |
| 1005 |
if( !SolveDepth.set || SolveDepth.value==-1 ) SolveDepth.value = Depth.value; |
| 1006 |
if( SolveDepth.value>Depth.value ) |
| 1007 |
{ |
| 1008 |
MK_WARN( "Solution depth cannot exceed system depth: " , SolveDepth.value , " <= " , Depth.value ); |
| 1009 |
SolveDepth.value = Depth.value; |
| 1010 |
} |
| 1011 |
|
| 1012 |
#ifdef USE_DOUBLE |
| 1013 |
typedef double Real; |
| 1014 |
#else // !USE_DOUBLE |
| 1015 |
typedef float Real; |
| 1016 |
#endif // USE_DOUBLE |
| 1017 |
|
| 1018 |
#ifdef FAST_COMPILE |
| 1019 |
static const int Dimension = DEFAULT_DIMENSION; |
| 1020 |
static const int Degree = DEFAULT_FEM_DEGREE; |
| 1021 |
static const BoundaryType BType = DEFAULT_FEM_BOUNDARY; |
| 1022 |
typedef IsotropicUIntPack< Dimension , FEMDegreeAndBType< Degree , BType >::Signature > FEMSigs; |
| 1023 |
MK_WARN( "Compiled for degree-" , Degree , ", boundary-" , BoundaryNames[ BType ] , ", " , sizeof(Real)==4 ? "single" : "double" , "-precision _only_" ); |
| 1024 |
Execute< Real >( FEMSigs() ); |
| 1025 |
#else // !FAST_COMPILE |
| 1026 |
if ( Dimension.value==2 ) Execute< 2 , Real >(); |
| 1027 |
else if( Dimension.value==3 ) Execute< 3 , Real >(); |
| 1028 |
else MK_THROW( "Only Degrees 2 and 3 are supported" ); |
| 1029 |
#endif // FAST_COMPILE |
| 1030 |
if( Performance.set ) |
| 1031 |
{ |
| 1032 |
printf( "Time (Wall/CPU): %.2f / %.2f\n" , timer.wallTime() , timer.cpuTime() ); |
| 1033 |
printf( "Peak Memory (MB): %d\n" , MemoryInfo::PeakMemoryUsageMB() ); |
| 1034 |
} |
| 1035 |
return EXIT_SUCCESS; |
| 1036 |
} |