| 1 |
/* |
| 2 |
Copyright (c) 2022, 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 |
#ifndef RECONSTRUCTORS_INCLUDED |
| 30 |
#define RECONSTRUCTORS_INCLUDED |
| 31 |
|
| 32 |
#include "PreProcessor.h" |
| 33 |
#include "MyMiscellany.h" |
| 34 |
#include "DataStream.imp.h" |
| 35 |
#include "FEMTree.h" |
| 36 |
#include "PointExtent.h" |
| 37 |
#include "Reconstructors.streams.h" |
| 38 |
|
| 39 |
namespace PoissonRecon |
| 40 |
{ |
| 41 |
|
| 42 |
namespace Reconstructor |
| 43 |
{ |
| 44 |
static unsigned int ProfilerMS = 20; // The number of ms at which to poll the performance (set to zero for no polling) |
| 45 |
static const unsigned int DataDegree = 0; // The order of the B-Spline used to splat in data for auxiliary data interpolation |
| 46 |
static const unsigned int WeightDegree = 2; // The order of the B-Spline used to splat in the weights for density estimation |
| 47 |
|
| 48 |
// Declare a type for storing the solution information |
| 49 |
template< typename Real , unsigned int Dim , typename FEMSigPack /* = UIntPack< FEMSigs... >*/ , typename ... AuxData > struct Implicit; |
| 50 |
|
| 51 |
// Parameters for mesh extraction |
| 52 |
struct LevelSetExtractionParameters |
| 53 |
{ |
| 54 |
bool linearFit; |
| 55 |
bool outputGradients; |
| 56 |
bool forceManifold; |
| 57 |
bool polygonMesh; |
| 58 |
bool gridCoordinates; |
| 59 |
bool outputDensity; |
| 60 |
bool verbose; |
| 61 |
LevelSetExtractionParameters( void ) : linearFit(false) , outputGradients(false) , forceManifold(true) , polygonMesh(false) , gridCoordinates(false) , verbose(false) , outputDensity(false) {} |
| 62 |
}; |
| 63 |
|
| 64 |
// General solver parameters |
| 65 |
template< typename Real > |
| 66 |
struct SolutionParameters |
| 67 |
{ |
| 68 |
bool verbose; |
| 69 |
bool exactInterpolation; |
| 70 |
bool showResidual; |
| 71 |
bool confidence; |
| 72 |
Real scale; |
| 73 |
Real lowDepthCutOff; |
| 74 |
Real width; |
| 75 |
Real samplesPerNode; |
| 76 |
Real cgSolverAccuracy; |
| 77 |
Real perLevelDataScaleFactor; |
| 78 |
unsigned int depth; |
| 79 |
unsigned int solveDepth; |
| 80 |
unsigned int baseDepth; |
| 81 |
unsigned int fullDepth; |
| 82 |
unsigned int kernelDepth; |
| 83 |
unsigned int baseVCycles; |
| 84 |
unsigned int iters; |
| 85 |
unsigned int alignDir; |
| 86 |
|
| 87 |
SolutionParameters( void ) : |
| 88 |
verbose(false) , exactInterpolation(false) , showResidual(false) , confidence(false) , |
| 89 |
scale((Real)1.1) , |
| 90 |
lowDepthCutOff((Real)0.) , width((Real)0.) , |
| 91 |
samplesPerNode((Real)1.5) , cgSolverAccuracy((Real)1e-3 ) , perLevelDataScaleFactor((Real)32.) , |
| 92 |
depth((unsigned int)8) , solveDepth((unsigned int)-1) , baseDepth((unsigned int)-1) , fullDepth((unsigned int)5) , kernelDepth((unsigned int)-1) , |
| 93 |
baseVCycles((unsigned int)1) , iters((unsigned int)8) , alignDir(0) |
| 94 |
{} |
| 95 |
|
| 96 |
template< unsigned int Dim > |
| 97 |
void testAndSet( XForm< Real , Dim+1 > unitCubeToModel ) |
| 98 |
{ |
| 99 |
if( width>0 ) |
| 100 |
{ |
| 101 |
Real maxScale = 0; |
| 102 |
for( unsigned int i=0 ; i<Dim ; i++ ) |
| 103 |
{ |
| 104 |
Real l2 = 0; |
| 105 |
for( unsigned int j=0 ; j<Dim ; j++ ) l2 += unitCubeToModel(i,j) * unitCubeToModel(i,j); |
| 106 |
if( l2>maxScale ) maxScale = l2; |
| 107 |
} |
| 108 |
maxScale = sqrt( maxScale ); |
| 109 |
depth = (unsigned int)ceil( std::max< double >( 0. , log( maxScale/width )/log(2.) ) ); |
| 110 |
} |
| 111 |
|
| 112 |
if( solveDepth>depth ) |
| 113 |
{ |
| 114 |
if( solveDepth!=-1 ) MK_WARN( "Solution depth cannot exceed system depth: " , solveDepth , " <= " , depth ); |
| 115 |
solveDepth = depth; |
| 116 |
} |
| 117 |
if( fullDepth>solveDepth ) |
| 118 |
{ |
| 119 |
if( fullDepth!=-1 ) MK_WARN( "Full depth cannot exceed system depth: " , fullDepth , " <= " , solveDepth ); |
| 120 |
fullDepth = solveDepth; |
| 121 |
} |
| 122 |
if( baseDepth>fullDepth ) |
| 123 |
{ |
| 124 |
if( baseDepth!=-1 ) MK_WARN( "Base depth must be smaller than full depth: " , baseDepth , " <= " , fullDepth ); |
| 125 |
baseDepth = fullDepth; |
| 126 |
} |
| 127 |
if( kernelDepth==-1 ) kernelDepth = depth>2 ? depth-2 : 0; |
| 128 |
if( kernelDepth>depth ) |
| 129 |
{ |
| 130 |
if( kernelDepth!=-1 ) MK_WARN( "Kernel depth cannot exceed system depth: " , kernelDepth , " <= " , depth ); |
| 131 |
kernelDepth = depth; |
| 132 |
} |
| 133 |
} |
| 134 |
}; |
| 135 |
struct Poisson; |
| 136 |
struct SSD; |
| 137 |
|
| 138 |
// Specialized solution information without auxiliary data |
| 139 |
template< typename Real , unsigned int Dim , unsigned int ... FEMSigs , typename ... AuxData > |
| 140 |
struct Implicit< Real , Dim , UIntPack< FEMSigs ... > , AuxData ... > |
| 141 |
{ |
| 142 |
static_assert( sizeof...(FEMSigs)==Dim , "[ERROR] Number of FEM signatures doesn't match dimension" ); |
| 143 |
|
| 144 |
// The internal type representing the auxiliary data |
| 145 |
using InternalAuxData = DirectSum< Real , AuxData... >; |
| 146 |
|
| 147 |
// The internal type representing the normal and auxiliary data |
| 148 |
using InternalNormalAndAuxData = DirectSum< Real , Normal< Real , Dim > , InternalAuxData >; |
| 149 |
|
| 150 |
// The signature pack |
| 151 |
using Sigs = UIntPack< FEMSigs ... >; |
| 152 |
|
| 153 |
// The type representing the point sampling density |
| 154 |
typedef typename FEMTree< Dim , Real >::template DensityEstimator< Reconstructor::WeightDegree > DensityEstimator; |
| 155 |
|
| 156 |
// The signature of the finite-element used for data extrapolation |
| 157 |
static const unsigned int DataSig = FEMDegreeAndBType< Reconstructor::DataDegree , BOUNDARY_FREE >::Signature; |
| 158 |
|
| 159 |
// The constructor |
| 160 |
Implicit( AuxData ... zeroAuxData ) : density(nullptr) , isoValue(0) , tree(MEMORY_ALLOCATOR_BLOCK_SIZE) , unitCubeToModel( XForm< Real , Dim+1 >::Identity() ) , _auxData(nullptr) , _zeroAuxData(zeroAuxData...) { _zeroNormalAndAuxData = InternalNormalAndAuxData( Normal< Real , Dim >() , _zeroAuxData ); } |
| 161 |
|
| 162 |
// The desctructor |
| 163 |
~Implicit( void ){ delete density ; density = nullptr ; delete _auxData ; _auxData = nullptr; } |
| 164 |
|
| 165 |
// Write out to file |
| 166 |
void write( BinaryStream &stream ) const |
| 167 |
{ |
| 168 |
tree.write( stream , false ); |
| 169 |
stream.write( isoValue ); |
| 170 |
stream.write( unitCubeToModel ); |
| 171 |
solution.write( stream ); |
| 172 |
density->write( stream ); |
| 173 |
if constexpr( sizeof...(AuxData) ) _auxData->write( stream ); |
| 174 |
} |
| 175 |
|
| 176 |
// The transformation taking points in the unit cube back to world coordinates |
| 177 |
XForm< Real , Dim+1 > unitCubeToModel; |
| 178 |
|
| 179 |
// The octree adapted to the points |
| 180 |
FEMTree< Dim , Real > tree; |
| 181 |
|
| 182 |
// The solution coefficients |
| 183 |
DenseNodeData< Real , Sigs > solution; |
| 184 |
|
| 185 |
// The average value at the sample positions |
| 186 |
Real isoValue; |
| 187 |
|
| 188 |
// The density estimator computed from the samples |
| 189 |
DensityEstimator *density; |
| 190 |
|
| 191 |
// A method that writes the extracted mesh to the streams |
| 192 |
void extractLevelSet( OutputLevelSetVertexStream< Real , Dim , AuxData... > &vertexStream , OutputFaceStream< Dim-1 > &faceStream , LevelSetExtractionParameters params ) const; |
| 193 |
|
| 194 |
struct Evaluator |
| 195 |
{ |
| 196 |
struct OutOfUnitCubeException : public std::exception |
| 197 |
{ |
| 198 |
OutOfUnitCubeException( Point< Real , Dim > world , Point< Real , Dim > cube ) |
| 199 |
{ |
| 200 |
std::stringstream sStream; |
| 201 |
sStream << "Out-of-unit-cube input: " << world << " -> " << cube; |
| 202 |
_message = sStream.str(); |
| 203 |
} |
| 204 |
const char * what( void ) const noexcept { return _message.c_str(); } |
| 205 |
protected: |
| 206 |
std::string _message; |
| 207 |
}; |
| 208 |
|
| 209 |
Evaluator( const FEMTree< Dim , Real > *tree , const DenseNodeData< Real , Sigs > &coefficients , XForm< Real , Dim+1 > worldToUnitCube ) |
| 210 |
: _evaluator( tree , coefficients , ThreadPool::NumThreads() ) , _xForm(worldToUnitCube) { _gxForm = XForm< Real , Dim >(_xForm).transpose(); } |
| 211 |
|
| 212 |
Point< Real , Dim > grad( unsigned int t , Point< Real , Dim > p ) |
| 213 |
{ |
| 214 |
CumulativeDerivativeValues< Real , Dim , 1 > v = _values( t , p ); |
| 215 |
Point< Real , Dim > g; |
| 216 |
for( unsigned int d=0 ; d<Dim ; d++ ) g[d] = v[d+1]; |
| 217 |
return _gxForm * g; |
| 218 |
} |
| 219 |
Real operator()( unsigned int t , Point< Real , Dim > p ){ return _values(t,p)[0]; } |
| 220 |
|
| 221 |
Real operator()( Point< Real , Dim > p ){ return operator()( 0 , p ); } |
| 222 |
Point< Real , Dim > grad( Point< Real , Dim > p ){ return grad( 0 , p ); } |
| 223 |
protected: |
| 224 |
CumulativeDerivativeValues< Real , Dim , 1 > _values( unsigned int t , Point< Real , Dim > p ) |
| 225 |
{ |
| 226 |
Point< Real , Dim > q = _xForm * p; |
| 227 |
for( unsigned int d=0 ; d<Dim ; d++ ) if( q[d]<0 || q[d]>1 ) throw OutOfUnitCubeException(p,q); |
| 228 |
return _evaluator.values( q , t ); |
| 229 |
} |
| 230 |
|
| 231 |
typename FEMTree< Dim , Real >::template MultiThreadedEvaluator< Sigs , 1 > _evaluator; |
| 232 |
XForm< Real , Dim+1 > _xForm; |
| 233 |
XForm< Real , Dim > _gxForm; |
| 234 |
}; |
| 235 |
Evaluator evaluator( void ) const { return Evaluator( &tree , solution , unitCubeToModel.inverse() ); } |
| 236 |
|
| 237 |
struct AuxEvaluator |
| 238 |
{ |
| 239 |
struct OutOfUnitCubeException : public std::exception |
| 240 |
{ |
| 241 |
OutOfUnitCubeException( Point< Real , Dim > world , Point< Real , Dim > cube ) |
| 242 |
{ |
| 243 |
std::stringstream sStream; |
| 244 |
sStream << "Out-of-unit-cube input: " << world << " -> " << cube; |
| 245 |
_message = sStream.str(); |
| 246 |
} |
| 247 |
const char * what( void ) const noexcept { return _message.c_str(); } |
| 248 |
protected: |
| 249 |
std::string _message; |
| 250 |
}; |
| 251 |
|
| 252 |
AuxEvaluator( const FEMTree< Dim , Real > *tree , const SparseNodeData< ProjectiveData< InternalAuxData , Real > , IsotropicUIntPack< Dim , DataSig > > &coefficients , XForm< Real , Dim+1 > worldToUnitCube , InternalAuxData zero ) |
| 253 |
: _auxEvaluator( tree , coefficients , ThreadPool::NumThreads() ) , _xForm(worldToUnitCube) , _zero(zero) {} |
| 254 |
|
| 255 |
void set( unsigned int t , Point< Real , Dim > p , AuxData&...d ){ _SetFromInternal( _value(t,p) , d... ); } |
| 256 |
|
| 257 |
void set( Point< Real , Dim > p , AuxData&...d ){ return operator()( 0 , p , d... ); } |
| 258 |
|
| 259 |
XForm< Real , Dim+1 > worldToUnitCubeTransform( void ) const { return worldToUnitCubeTransform(); } |
| 260 |
protected: |
| 261 |
InternalAuxData _value( unsigned int t , Point< Real , Dim > p ) |
| 262 |
{ |
| 263 |
Point< Real , Dim > q = _xForm * p; |
| 264 |
for( unsigned int d=0 ; d<Dim ; d++ ) if( q[d]<0 || q[d]>1 ) throw OutOfUnitCubeException(p,q); |
| 265 |
ProjectiveData< InternalAuxData , Real > pData( _zero ); |
| 266 |
_auxEvaluator.addValue( q , pData ); |
| 267 |
return pData.value(); |
| 268 |
} |
| 269 |
|
| 270 |
template< typename D , typename ... Ds > |
| 271 |
static void _SetFromInternal( InternalAuxData iData , D &d , Ds&... ds ) |
| 272 |
{ |
| 273 |
d = iData.template get< sizeof...(AuxData) - sizeof...(Ds) - 1 >(); |
| 274 |
if constexpr( sizeof...(Ds) ) _SetFromInternal( iData , ds... ); |
| 275 |
} |
| 276 |
|
| 277 |
typename FEMTree< Dim , Real >::template MultiThreadedSparseEvaluator< IsotropicUIntPack< Dim , DataSig > , ProjectiveData< InternalAuxData , Real > > _auxEvaluator; |
| 278 |
XForm< Real , Dim+1 > _xForm; |
| 279 |
InternalAuxData _zero; |
| 280 |
}; |
| 281 |
|
| 282 |
AuxEvaluator auxEvaluator( void ) const |
| 283 |
{ |
| 284 |
static_assert( sizeof...(AuxData) , "No auxiliary data" ); |
| 285 |
return AuxEvaluator( &tree , *_auxData , unitCubeToModel.inverse() , _zeroAuxData ); |
| 286 |
} |
| 287 |
|
| 288 |
protected: |
| 289 |
// The auxiliary information stored with the oriented vertices |
| 290 |
SparseNodeData< ProjectiveData< InternalAuxData , Real > , IsotropicUIntPack< Dim , DataSig > > *_auxData; |
| 291 |
|
| 292 |
// An instance of "zero" AuxData |
| 293 |
InternalAuxData _zeroAuxData; |
| 294 |
|
| 295 |
InternalNormalAndAuxData _zeroNormalAndAuxData; |
| 296 |
|
| 297 |
struct _VertexTypeConverter |
| 298 |
{ |
| 299 |
using ExternalVertexType = std::tuple< Position< Real , Dim > , Gradient< Real , Dim > , Weight< Real > , InternalAuxData >; |
| 300 |
using InternalVertexType = std::tuple< Position< Real , Dim > , Gradient< Real , Dim > , Weight< Real > , AuxData... >; |
| 301 |
|
| 302 |
static ExternalVertexType ConvertI2X( const InternalVertexType &iType ) |
| 303 |
{ |
| 304 |
ExternalVertexType xType; |
| 305 |
|
| 306 |
// Copy the position information |
| 307 |
std::get< 0 >( xType ) = std::get< 0 >( iType ); |
| 308 |
std::get< 1 >( xType ) = std::get< 1 >( iType ); |
| 309 |
std::get< 2 >( xType ) = std::get< 2 >( iType ); |
| 310 |
// Copy the remainder of the information |
| 311 |
_SetAuxData< 0 >( iType , xType ); |
| 312 |
return xType; |
| 313 |
} |
| 314 |
static InternalVertexType ConvertX2I( const ExternalVertexType &xType ) |
| 315 |
{ |
| 316 |
InternalVertexType iType; |
| 317 |
|
| 318 |
// Copy the position information |
| 319 |
std::get< 0 >( iType ) = std::get< 0 >( xType ); |
| 320 |
std::get< 1 >( iType ) = std::get< 1 >( xType ); |
| 321 |
std::get< 2 >( iType ) = std::get< 2 >( xType ); |
| 322 |
// Copy the remainder of the information |
| 323 |
_SetAuxData< 0 >( xType , iType ); |
| 324 |
return iType; |
| 325 |
} |
| 326 |
protected: |
| 327 |
template< unsigned int I > |
| 328 |
static void _SetAuxData( const InternalVertexType &iType , ExternalVertexType &xType ) |
| 329 |
{ |
| 330 |
std::get< 3 >( xType ).template get< I >() = std::get< I+3 >( iType ); |
| 331 |
if constexpr( I+1<sizeof...(AuxData) ) _SetAuxData< I+1 >( iType , xType ); |
| 332 |
} |
| 333 |
template< unsigned int I > |
| 334 |
static void _SetAuxData( const ExternalVertexType &xType , InternalVertexType &iType ) |
| 335 |
{ |
| 336 |
std::get< I+3 >( iType ) = std::get< 3 >( xType ).template get< I >(); |
| 337 |
if constexpr( I+1<sizeof...(AuxData) ) _SetAuxData< I+1 >( xType , iType ); |
| 338 |
} |
| 339 |
}; |
| 340 |
|
| 341 |
struct _SampleTypeConverter |
| 342 |
{ |
| 343 |
using ExternalSampleType = std::tuple< Position< Real , Dim > , InternalNormalAndAuxData >; |
| 344 |
using InternalSampleType = std::tuple< Position< Real , Dim > , Normal< Real , Dim > , AuxData... >; |
| 345 |
|
| 346 |
static ExternalSampleType ConvertI2X( const InternalSampleType &iType ) |
| 347 |
{ |
| 348 |
ExternalSampleType xType; |
| 349 |
|
| 350 |
// Copy the position information |
| 351 |
std::get< 0 >( xType ) = std::get< 0 >( iType ); |
| 352 |
// Copy the remainder of the information |
| 353 |
_SetNormalAndAuxData< 0 >( iType , xType ); |
| 354 |
return xType; |
| 355 |
} |
| 356 |
static InternalSampleType ConvertX2I( const ExternalSampleType &xType ) |
| 357 |
{ |
| 358 |
InternalSampleType iType; |
| 359 |
|
| 360 |
// Copy the position information |
| 361 |
std::get< 0 >( iType ) = std::get< 0 >( xType ); |
| 362 |
// Copy the remainder of the information |
| 363 |
_SetNormalAndAuxData< 0 >( xType , iType ); |
| 364 |
return iType; |
| 365 |
} |
| 366 |
protected: |
| 367 |
template< unsigned int I > |
| 368 |
static void _SetNormalAndAuxData( const InternalSampleType &iType , ExternalSampleType &xType ) |
| 369 |
{ |
| 370 |
std::get< 1 >( xType ).template get< I >() = std::get< I+1 >( iType ); |
| 371 |
if constexpr( I<sizeof...(AuxData) ) _SetNormalAndAuxData< I+1 >( iType , xType ); |
| 372 |
} |
| 373 |
template< unsigned int I > |
| 374 |
static void _SetNormalAndAuxData( const ExternalSampleType &xType , InternalSampleType &iType ) |
| 375 |
{ |
| 376 |
std::get< I+1 >( iType ) = std::get< 1 >( xType ).template get< I >(); |
| 377 |
if constexpr( I<sizeof...(AuxData) ) _SetNormalAndAuxData< I+1 >( xType , iType ); |
| 378 |
} |
| 379 |
}; |
| 380 |
|
| 381 |
friend Poisson; |
| 382 |
friend SSD; |
| 383 |
}; |
| 384 |
|
| 385 |
|
| 386 |
struct Poisson |
| 387 |
{ |
| 388 |
static const unsigned int NormalDegree = 2; // The order of the B-Spline used to splat in the normals for constructing the Laplacian constraints |
| 389 |
static const unsigned int DefaultFEMDegree = 1; // The default finite-element degree (has to be at least 1) |
| 390 |
static const BoundaryType DefaultFEMBoundary = BOUNDARY_NEUMANN; // The default finite-element boundary type {BOUNDARY_FREE, BOUNDARY_DIRICHLET, BOUNDARY_NEUMANN} |
| 391 |
inline static const float WeightMultiplier = 2.f; // The default degree-to-point-weight scaling |
| 392 |
|
| 393 |
template< unsigned int Dim , typename Real > |
| 394 |
struct ConstraintDual |
| 395 |
{ |
| 396 |
Real target , weight; |
| 397 |
ConstraintDual( void ) : target(0) , weight(0) {} |
| 398 |
ConstraintDual( Real t , Real w ) : target(t) , weight(w) {} |
| 399 |
CumulativeDerivativeValues< Real , Dim , 0 > operator()( const Point< Real , Dim >& p ) const { return CumulativeDerivativeValues< Real , Dim , 0 >( target*weight ); }; |
| 400 |
}; |
| 401 |
|
| 402 |
template< unsigned int Dim , typename Real > |
| 403 |
struct SystemDual |
| 404 |
{ |
| 405 |
Real weight; |
| 406 |
SystemDual( void ) : weight(0) {} |
| 407 |
SystemDual( Real w ) : weight(w) {} |
| 408 |
CumulativeDerivativeValues< Real , Dim , 0 > operator()( const Point< Real , Dim >& p , const CumulativeDerivativeValues< Real , Dim , 0 >& dValues ) const { return dValues * weight; }; |
| 409 |
CumulativeDerivativeValues< double , Dim , 0 > operator()( const Point< Real , Dim >& p , const CumulativeDerivativeValues< double , Dim , 0 >& dValues ) const { return dValues * weight; }; |
| 410 |
}; |
| 411 |
|
| 412 |
template< unsigned int Dim > |
| 413 |
struct SystemDual< Dim , double > |
| 414 |
{ |
| 415 |
typedef double Real; |
| 416 |
Real weight; |
| 417 |
SystemDual( void ) : weight(0) {} |
| 418 |
SystemDual( Real w ) : weight(w) {} |
| 419 |
CumulativeDerivativeValues< Real , Dim , 0 > operator()( const Point< Real , Dim >& p , const CumulativeDerivativeValues< Real , Dim , 0 >& dValues ) const { return dValues * weight; }; |
| 420 |
}; |
| 421 |
|
| 422 |
template< unsigned int Dim , typename Real > |
| 423 |
struct ValueInterpolationConstraintDual |
| 424 |
{ |
| 425 |
typedef DirectSum< Real , Real > PointSampleData; |
| 426 |
Real vWeight; |
| 427 |
ValueInterpolationConstraintDual( Real v ) : vWeight(v){ } |
| 428 |
CumulativeDerivativeValues< Real , Dim , 0 > operator()( const Point< Real , Dim > &p , const DirectSum< Real , Real >& data ) const |
| 429 |
{ |
| 430 |
Real value = data.template get<0>(); |
| 431 |
CumulativeDerivativeValues< Real , Dim , 0 > cdv; |
| 432 |
cdv[0] = value*vWeight; |
| 433 |
return cdv; |
| 434 |
} |
| 435 |
}; |
| 436 |
|
| 437 |
template< unsigned int Dim , typename Real > |
| 438 |
struct ValueInterpolationSystemDual |
| 439 |
{ |
| 440 |
CumulativeDerivativeValues< Real , Dim , 0 > weight; |
| 441 |
ValueInterpolationSystemDual( Real v ){ weight[0] = v; } |
| 442 |
CumulativeDerivativeValues< Real , Dim , 0 > operator()( Point< Real , Dim > p , const DirectSum< Real , Real > &data , const CumulativeDerivativeValues< Real , Dim , 0 > &dValues ) const |
| 443 |
{ |
| 444 |
return dValues * weight; |
| 445 |
} |
| 446 |
CumulativeDerivativeValues< double , Dim , 0 > operator()( Point< Real , Dim > p , const DirectSum< Real , Real > &data , const CumulativeDerivativeValues< double , Dim , 0 > &dValues ) const |
| 447 |
{ |
| 448 |
return dValues * weight; |
| 449 |
} |
| 450 |
}; |
| 451 |
|
| 452 |
template< unsigned int Dim > |
| 453 |
struct ValueInterpolationSystemDual< Dim , double > |
| 454 |
{ |
| 455 |
typedef double Real; |
| 456 |
Real weight; |
| 457 |
ValueInterpolationSystemDual( void ) : weight(0) {} |
| 458 |
ValueInterpolationSystemDual( Real v ) : weight(v) {} |
| 459 |
CumulativeDerivativeValues< Real , Dim , 0 > operator()( Point< Real , Dim > p , const DirectSum< Real , Real > &data , const CumulativeDerivativeValues< Real , Dim , 0 > &dValues ) const |
| 460 |
{ |
| 461 |
return dValues * weight; |
| 462 |
} |
| 463 |
}; |
| 464 |
|
| 465 |
template< typename Real > |
| 466 |
struct SolutionParameters : public Reconstructor::SolutionParameters< Real > |
| 467 |
{ |
| 468 |
bool dirichletErode; |
| 469 |
Real pointWeight; |
| 470 |
Real valueInterpolationWeight; |
| 471 |
unsigned int envelopeDepth; |
| 472 |
|
| 473 |
SolutionParameters( void ) : |
| 474 |
dirichletErode(false) , |
| 475 |
pointWeight((Real)( WeightMultiplier * DefaultFEMDegree ) ) , valueInterpolationWeight((Real)0.) , |
| 476 |
envelopeDepth((unsigned int)-1) |
| 477 |
{} |
| 478 |
|
| 479 |
template< unsigned int Dim > |
| 480 |
void testAndSet( XForm< Real , Dim+1 > unitCubeToModel ) |
| 481 |
{ |
| 482 |
Reconstructor::SolutionParameters< Real >::template testAndSet< Dim >( unitCubeToModel ); |
| 483 |
if( envelopeDepth==-1 ) envelopeDepth = Reconstructor::SolutionParameters< Real >::baseDepth; |
| 484 |
if( envelopeDepth>Reconstructor::SolutionParameters< Real >::depth ) |
| 485 |
{ |
| 486 |
if( envelopeDepth!=-1 ) MK_WARN( "Envelope depth cannot exceed system depth: " , envelopeDepth , " <= " , Reconstructor::SolutionParameters< Real >::depth ); |
| 487 |
envelopeDepth = Reconstructor::SolutionParameters< Real >::depth; |
| 488 |
} |
| 489 |
if( envelopeDepth<Reconstructor::SolutionParameters< Real >::baseDepth ) |
| 490 |
{ |
| 491 |
MK_WARN( "Envelope depth cannot be less than base depth: " , envelopeDepth , " >= " , Reconstructor::SolutionParameters< Real >::baseDepth ); |
| 492 |
envelopeDepth = Reconstructor::SolutionParameters< Real >::baseDepth; |
| 493 |
} |
| 494 |
} |
| 495 |
}; |
| 496 |
|
| 497 |
template< typename Real , unsigned int Dim > |
| 498 |
struct EnvelopeMesh |
| 499 |
{ |
| 500 |
std::vector< Point< Real , Dim > > vertices; |
| 501 |
std::vector< SimplexIndex< Dim-1 , node_index_type > > simplices; |
| 502 |
}; |
| 503 |
|
| 504 |
template< typename Real , unsigned int Dim , typename FEMSigPack , typename ... AuxData > struct Solver; |
| 505 |
|
| 506 |
template< typename Real , unsigned int Dim , unsigned int ... FEMSigs , typename ... AuxData > |
| 507 |
struct Solver< Real , Dim , UIntPack< FEMSigs... > , AuxData... > |
| 508 |
{ |
| 509 |
static Implicit< Real , Dim , UIntPack< FEMSigs... > , AuxData... > *Solve( InputOrientedSampleStream< Real , Dim , AuxData... > &pointStream , SolutionParameters< Real > params , AuxData ... zero , const EnvelopeMesh< Real , Dim > *envelopeMesh=nullptr , InputValuedSampleStream< Real , Dim > *valueInterpolationStream=nullptr ); |
| 510 |
}; |
| 511 |
}; |
| 512 |
|
| 513 |
struct SSD |
| 514 |
{ |
| 515 |
static const unsigned int NormalDegree = 2; // The order of the B-Spline used to splat in the normals for constructing the Laplacian constraints |
| 516 |
static const unsigned int DefaultFEMDegree = 2; // The default finite-element degree (has to be at least 2) |
| 517 |
static const BoundaryType DefaultFEMBoundary = BOUNDARY_NEUMANN; // The default finite-element boundary type {BOUNDARY_FREE, BOUNDARY_DIRICHLET, BOUNDARY_NEUMANN} |
| 518 |
inline static const double WeightMultipliers[] = { 5e+1f , 5e-4f , 1e-5f }; // The default weights for balancing the value, gradient, and laplacian energy terms |
| 519 |
|
| 520 |
template< typename Real , unsigned int Dim , typename ... AuxData > |
| 521 |
using NormalAndAuxData = DirectSum< Real , Normal< Real , Dim > , DirectSum< Real , AuxData... > >; |
| 522 |
|
| 523 |
template< unsigned int Dim , typename Real , typename ... AuxData > |
| 524 |
struct ConstraintDual |
| 525 |
{ |
| 526 |
Real target , vWeight , gWeight; |
| 527 |
ConstraintDual( Real t , Real v , Real g ) : target(t) , vWeight(v) , gWeight(g) { } |
| 528 |
CumulativeDerivativeValues< Real , Dim , 1 > operator()( const Point< Real , Dim >& p , const NormalAndAuxData< Real , Dim , AuxData... > &normalAndAuxData ) const |
| 529 |
{ |
| 530 |
Point< Real , Dim > n = normalAndAuxData.template get<0>(); |
| 531 |
CumulativeDerivativeValues< Real , Dim , 1 > cdv; |
| 532 |
cdv[0] = target*vWeight; |
| 533 |
for( int d=0 ; d<Dim ; d++ ) cdv[1+d] = -n[d]*gWeight; |
| 534 |
return cdv; |
| 535 |
} |
| 536 |
}; |
| 537 |
|
| 538 |
template< unsigned int Dim , typename Real , typename ... AuxData > |
| 539 |
struct SystemDual |
| 540 |
{ |
| 541 |
CumulativeDerivativeValues< Real , Dim , 1 > weight; |
| 542 |
SystemDual( Real v , Real g ) |
| 543 |
{ |
| 544 |
weight[0] = v; |
| 545 |
for( int d=0 ; d<Dim ; d++ ) weight[d+1] = g; |
| 546 |
} |
| 547 |
CumulativeDerivativeValues< Real , Dim , 1 > operator()( Point< Real , Dim > p , const NormalAndAuxData< Real , Dim , AuxData... > & , const CumulativeDerivativeValues< Real , Dim , 1 >& dValues ) const |
| 548 |
{ |
| 549 |
return dValues * weight; |
| 550 |
} |
| 551 |
CumulativeDerivativeValues< double , Dim , 1 > operator()( Point< Real , Dim > p , const NormalAndAuxData< Real , Dim , AuxData... > & , const CumulativeDerivativeValues< double , Dim , 1 >& dValues ) const |
| 552 |
{ |
| 553 |
return dValues * weight; |
| 554 |
}; |
| 555 |
}; |
| 556 |
|
| 557 |
template< unsigned int Dim , typename ... AuxData > |
| 558 |
struct SystemDual< Dim , double , AuxData... > |
| 559 |
{ |
| 560 |
typedef double Real; |
| 561 |
CumulativeDerivativeValues< Real , Dim , 1 > weight; |
| 562 |
SystemDual( Real v , Real g ) : weight( v , g , g , g ) { } |
| 563 |
CumulativeDerivativeValues< Real , Dim , 1 > operator()( Point< Real , Dim > p , const NormalAndAuxData< Real , Dim , AuxData... > & , const CumulativeDerivativeValues< Real , Dim , 1 >& dValues ) const |
| 564 |
{ |
| 565 |
return dValues * weight; |
| 566 |
} |
| 567 |
}; |
| 568 |
|
| 569 |
template< typename Real > |
| 570 |
struct SolutionParameters : public Reconstructor::SolutionParameters< Real > |
| 571 |
{ |
| 572 |
Real pointWeight; |
| 573 |
Real gradientWeight; |
| 574 |
Real biLapWeight; |
| 575 |
|
| 576 |
SolutionParameters( void ) : |
| 577 |
pointWeight((Real)WeightMultipliers[0]) , gradientWeight((Real)WeightMultipliers[1]) , biLapWeight((Real)WeightMultipliers[2]) |
| 578 |
{} |
| 579 |
}; |
| 580 |
|
| 581 |
template< typename Real , unsigned int Dim , typename FEMSigPack , typename ... AuxData > struct Solver; |
| 582 |
|
| 583 |
template< typename Real , unsigned int Dim , unsigned int ... FEMSigs , typename ... AuxData > |
| 584 |
struct Solver< Real , Dim , UIntPack< FEMSigs... > , AuxData... > |
| 585 |
{ |
| 586 |
static Implicit< Real , Dim , UIntPack< FEMSigs... > , AuxData... > *Solve( InputOrientedSampleStream< Real , Dim , AuxData... > &pointStream , SolutionParameters< Real > params , AuxData ... zero ); |
| 587 |
}; |
| 588 |
}; |
| 589 |
|
| 590 |
// Implementation of the base Implicit class's level-set extraction |
| 591 |
template< typename Real , unsigned int Dim , unsigned int ... FEMSigs , typename ... AuxData > |
| 592 |
void Implicit< Real , Dim , UIntPack< FEMSigs ... > , AuxData ... >::extractLevelSet( OutputLevelSetVertexStream< Real , Dim , AuxData... > &vertexStream , OutputFaceStream< Dim-1 > &faceStream , LevelSetExtractionParameters params ) const |
| 593 |
{ |
| 594 |
XForm< Real , Dim+1 > unitCubeToModel; |
| 595 |
if( params.gridCoordinates ) |
| 596 |
{ |
| 597 |
unitCubeToModel = XForm< Real , Dim+1 >::Identity(); |
| 598 |
unsigned int res = 1<<tree.depth(); |
| 599 |
for( unsigned int d=0 ; d<Dim ; d++ ) unitCubeToModel(d,d) = (Real)res; |
| 600 |
} |
| 601 |
else unitCubeToModel = this->unitCubeToModel; |
| 602 |
|
| 603 |
DensityEstimator *density = params.outputDensity ? this->density : nullptr; |
| 604 |
|
| 605 |
if constexpr( Dim==2 || Dim==3 ) |
| 606 |
{ |
| 607 |
Profiler profiler( ProfilerMS ); |
| 608 |
|
| 609 |
std::string statsString; |
| 610 |
|
| 611 |
TransformedOutputLevelSetVertexStream< Real , Dim , AuxData... > _vertexStream( unitCubeToModel , vertexStream ); |
| 612 |
if constexpr( sizeof...(AuxData) ) |
| 613 |
{ |
| 614 |
// Convert stream: |
| 615 |
// OutputDataStream< Position< Real , Dim > , Gradient< Real , Dim > , Weight< Real > , AuxData... > |
| 616 |
// -> OutputDataStream< Position< Real , Dim > , Gradient< Real , Dim > , Weight< Real > , InternalAuxData > |
| 617 |
OutputDataStreamConverter< typename _VertexTypeConverter::InternalVertexType , typename _VertexTypeConverter::ExternalVertexType > __vertexStream( _vertexStream , _VertexTypeConverter::ConvertX2I ); |
| 618 |
typename LevelSetExtractor< Real , Dim , InternalAuxData >::Stats stats; |
| 619 |
if constexpr( Dim==3 ) |
| 620 |
stats = LevelSetExtractor< Real , Dim , InternalAuxData >::Extract( Sigs() , UIntPack< Reconstructor::WeightDegree >() , UIntPack< DataSig >() , tree , density , _auxData , solution , isoValue , __vertexStream , faceStream , _zeroAuxData , !params.linearFit , params.outputGradients , params.forceManifold , params.polygonMesh , false ); |
| 621 |
else if constexpr( Dim==2 ) |
| 622 |
stats = LevelSetExtractor< Real , Dim , InternalAuxData >::Extract( Sigs() , UIntPack< Reconstructor::WeightDegree >() , UIntPack< DataSig >() , tree , density , _auxData , solution , isoValue , __vertexStream , faceStream , _zeroAuxData , !params.linearFit , params.outputGradients , false ); |
| 623 |
statsString = stats.toString(); |
| 624 |
} |
| 625 |
else |
| 626 |
{ |
| 627 |
typename LevelSetExtractor< Real , Dim >::Stats stats; |
| 628 |
if constexpr( Dim==3 ) |
| 629 |
stats = LevelSetExtractor< Real , Dim >::Extract( Sigs() , UIntPack< Reconstructor::WeightDegree >() , tree , density , solution , isoValue , _vertexStream , faceStream , !params.linearFit , params.outputGradients , params.forceManifold , params.polygonMesh , false ); |
| 630 |
else if constexpr( Dim==2 ) |
| 631 |
stats = LevelSetExtractor< Real , Dim >::Extract( Sigs() , UIntPack< Reconstructor::WeightDegree >() , tree , density , solution , isoValue , _vertexStream , faceStream , !params.linearFit , params.outputGradients , false ); |
| 632 |
statsString = stats.toString(); |
| 633 |
} |
| 634 |
|
| 635 |
if( params.verbose ) |
| 636 |
{ |
| 637 |
std::cout << "Vertices / Faces: " << vertexStream.size() << " / " << faceStream.size() << std::endl; |
| 638 |
std::cout << statsString << std::endl; |
| 639 |
std::cout << "# Got Faces: " << profiler << std::endl; |
| 640 |
} |
| 641 |
} |
| 642 |
else MK_WARN( "Extraction only supported for dimensions 2 and 3" ); |
| 643 |
} |
| 644 |
|
| 645 |
// Implementation of the derived Poisson::Implicit's constructor |
| 646 |
template< typename Real , unsigned int Dim , unsigned int ... FEMSigs , typename ... AuxData > |
| 647 |
Implicit< Real , Dim , UIntPack< FEMSigs... > , AuxData... > *Poisson::Solver< Real , Dim , UIntPack< FEMSigs... > , AuxData... >::Solve( InputOrientedSampleStream< Real , Dim , AuxData... > &pointStream , SolutionParameters< Real > params , AuxData ... zero , const EnvelopeMesh< Real , Dim > *envelopeMesh , InputValuedSampleStream< Real , Dim > *valueInterpolationStream ) |
| 648 |
{ |
| 649 |
Implicit< Real , Dim , UIntPack< FEMSigs... > , AuxData... > *implicitPtr = new Implicit< Real , Dim , UIntPack< FEMSigs... > , AuxData... >( zero... ); |
| 650 |
Implicit< Real , Dim , UIntPack< FEMSigs... > , AuxData... > &implicit = *implicitPtr; |
| 651 |
|
| 652 |
if( params.valueInterpolationWeight<0 ) |
| 653 |
{ |
| 654 |
MK_WARN( "Negative value interpolation weight clamped to zero" ); |
| 655 |
params.valueInterpolationWeight = 0; |
| 656 |
} |
| 657 |
if( valueInterpolationStream && !params.valueInterpolationWeight ) MK_WARN( "Value interpolation stream provided but interpolation weight is zero" ); |
| 658 |
|
| 659 |
// The signature for the finite-elements representing the auxiliary data (if it's there) |
| 660 |
static const unsigned int DataSig = FEMDegreeAndBType< Reconstructor::DataDegree , BOUNDARY_FREE >::Signature; |
| 661 |
|
| 662 |
/////////////// |
| 663 |
// Types --> // |
| 664 |
using InternalAuxData = typename Reconstructor::Implicit< Real , Dim , UIntPack< FEMSigs... > , AuxData... >::InternalAuxData; |
| 665 |
using InternalNormalAndAuxData = typename Reconstructor::Implicit< Real , Dim , UIntPack< FEMSigs... > , AuxData... >::InternalNormalAndAuxData; |
| 666 |
using _SampleTypeConverter = typename Reconstructor::Implicit< Real , Dim , UIntPack< FEMSigs... > , AuxData... >::_SampleTypeConverter; |
| 667 |
using Sigs = typename Reconstructor::Implicit< Real , Dim , UIntPack< FEMSigs... > , AuxData... >::Sigs; |
| 668 |
|
| 669 |
// The degrees of the finite elements across the different axes |
| 670 |
typedef UIntPack< FEMSignature< FEMSigs >::Degree ... > Degrees; |
| 671 |
|
| 672 |
// The signature describing the normals elements |
| 673 |
typedef UIntPack< FEMDegreeAndBType< Poisson::NormalDegree , DerivativeBoundary< FEMSignature< FEMSigs >::BType , 1 >::BType >::Signature ... > NormalSigs; |
| 674 |
|
| 675 |
// Type for tracking sample interpolation |
| 676 |
typedef typename FEMTree< Dim , Real >::template InterpolationInfo< Real , 0 > InterpolationInfo; |
| 677 |
|
| 678 |
// The finite-element tracking tree node |
| 679 |
typedef RegularTreeNode< Dim , FEMTreeNodeData , depth_and_offset_type > FEMTreeNode; |
| 680 |
|
| 681 |
typedef typename FEMTreeInitializer< Dim , Real >::GeometryNodeType GeometryNodeType; |
| 682 |
// <-- Types // |
| 683 |
/////////////// |
| 684 |
|
| 685 |
XForm< Real , Dim+1 > modelToUnitCube = XForm< Real , Dim+1 >::Identity(); |
| 686 |
|
| 687 |
Profiler profiler( ProfilerMS ); |
| 688 |
|
| 689 |
size_t pointCount; |
| 690 |
|
| 691 |
ProjectiveData< Point< Real , 2 > , Real > pointDepthAndWeight; |
| 692 |
std::vector< typename FEMTree< Dim , Real >::PointSample > *valueInterpolationSamples = nullptr; |
| 693 |
std::vector< Real > *valueInterpolationSampleData = nullptr; |
| 694 |
DenseNodeData< GeometryNodeType , IsotropicUIntPack< Dim , FEMTrivialSignature > > geometryNodeDesignators; |
| 695 |
SparseNodeData< Point< Real , Dim > , NormalSigs > *normalInfo = nullptr; |
| 696 |
std::vector< typename FEMTree< Dim , Real >::PointSample > *samples = new std::vector< typename FEMTree< Dim , Real >::PointSample >(); |
| 697 |
std::vector< InternalNormalAndAuxData > *sampleNormalAndAuxData = new std::vector< InternalNormalAndAuxData >(); |
| 698 |
|
| 699 |
Real targetValue = (Real)0.5; |
| 700 |
|
| 701 |
// Read in the samples (and auxiliary data) |
| 702 |
{ |
| 703 |
profiler.reset(); |
| 704 |
|
| 705 |
pointStream.reset(); |
| 706 |
modelToUnitCube = params.scale>0 ? PointExtent::GetXForm< Real , Dim , true , Normal< Real , Dim > , AuxData... >( pointStream , Normal< Real , Dim >() , zero... , params.scale , params.alignDir ) * modelToUnitCube : modelToUnitCube; |
| 707 |
implicit.unitCubeToModel = modelToUnitCube.inverse(); |
| 708 |
pointStream.reset(); |
| 709 |
|
| 710 |
params.template testAndSet< Dim >( implicit.unitCubeToModel ); |
| 711 |
|
| 712 |
{ |
| 713 |
// Apply the transformation |
| 714 |
TransformedInputOrientedSampleStream< Real , Dim , AuxData... > _pointStream( modelToUnitCube , pointStream ); |
| 715 |
|
| 716 |
std::vector< node_index_type > nodeToIndexMap; |
| 717 |
|
| 718 |
auto IsValid = [&]( const Point< Real , Dim > &p , const Normal< Real , Dim > &n , AuxData ... d ) |
| 719 |
{ |
| 720 |
Real l = Point< Real , Dim >::SquareNorm( n ); |
| 721 |
return l>0 && std::isfinite(l); |
| 722 |
}; |
| 723 |
|
| 724 |
auto Process = [&]( FEMTreeNode &node , const Point< Real , Dim > &p , Normal< Real , Dim > &n , AuxData ... d ) |
| 725 |
{ |
| 726 |
Real l = (Real)Length( n ); |
| 727 |
Real weight = params.confidence ? l : (Real)1.; |
| 728 |
n /= l; |
| 729 |
|
| 730 |
node_index_type nodeIndex = node.nodeData.nodeIndex; |
| 731 |
// If the node's index exceeds what's stored in the node-to-index map, grow the node-to-index map |
| 732 |
if( nodeIndex>=(node_index_type)nodeToIndexMap.size() ) nodeToIndexMap.resize( nodeIndex+1 , -1 ); |
| 733 |
|
| 734 |
node_index_type idx = nodeToIndexMap[ nodeIndex ]; |
| 735 |
if( idx==-1 ) |
| 736 |
{ |
| 737 |
idx = (node_index_type)samples->size(); |
| 738 |
nodeToIndexMap[ nodeIndex ] = idx; |
| 739 |
samples->resize( idx+1 ) , (*samples)[idx].node = &node; |
| 740 |
sampleNormalAndAuxData->resize( idx+1 ); |
| 741 |
(*samples)[idx].sample = ProjectiveData< Point< Real , Dim > , Real >( p*weight , weight ); |
| 742 |
(*sampleNormalAndAuxData)[idx] = InternalNormalAndAuxData( n , DirectSum< Real , AuxData... >( d... ) ) * weight; |
| 743 |
} |
| 744 |
else |
| 745 |
{ |
| 746 |
(*samples)[idx].sample += ProjectiveData< Point< Real , Dim > , Real >( p*weight , weight ); |
| 747 |
(*sampleNormalAndAuxData)[idx] += InternalNormalAndAuxData( n , DirectSum< Real , AuxData... >( d... ) ) * weight; |
| 748 |
} |
| 749 |
return true; |
| 750 |
}; |
| 751 |
|
| 752 |
auto F = [&]( AuxData ... zeroAuxData ) |
| 753 |
{ |
| 754 |
pointCount = FEMTreeInitializer< Dim , Real >::template Initialize< decltype(IsValid) , decltype(Process) , Normal< Real , Dim > , AuxData... >( implicit.tree.spaceRoot() , _pointStream , Normal< Real , Dim >() , zeroAuxData... , params.depth , implicit.tree.nodeAllocators.size() ? implicit.tree.nodeAllocators[0] : nullptr , implicit.tree.initializer() , IsValid , Process ); |
| 755 |
}; |
| 756 |
implicit._zeroAuxData.process( F ); |
| 757 |
} |
| 758 |
|
| 759 |
|
| 760 |
if( params.verbose ) |
| 761 |
{ |
| 762 |
std::cout << "Input Points / Samples: " << pointCount << " / " << samples->size() << std::endl; |
| 763 |
std::cout << "# Read input into tree: " << profiler << std::endl; |
| 764 |
} |
| 765 |
} |
| 766 |
|
| 767 |
if( valueInterpolationStream && params.valueInterpolationWeight ) |
| 768 |
{ |
| 769 |
valueInterpolationSamples = new std::vector< typename FEMTree< Dim , Real >::PointSample >(); |
| 770 |
valueInterpolationSampleData = new std::vector< Real >(); |
| 771 |
// Wrap the point stream in a transforming stream |
| 772 |
TransformedInputValuedSampleStream< Real , Dim > _valueInterpolationStream( modelToUnitCube , *valueInterpolationStream ); |
| 773 |
|
| 774 |
// Assign each sample a weight of 1. |
| 775 |
auto ProcessData = []( const Point< Real , Dim > &p , Real &d ){ return (Real)1.; }; |
| 776 |
Real zeroValue = (Real)0.; |
| 777 |
size_t count = FEMTreeInitializer< Dim , Real >::template Initialize< Real >( implicit.tree.spaceRoot() , _valueInterpolationStream , zeroValue , params.depth , *valueInterpolationSamples , *valueInterpolationSampleData , implicit.tree.nodeAllocators.size() ? implicit.tree.nodeAllocators[0] : NULL , implicit.tree.initializer() , ProcessData ); |
| 778 |
if( params.verbose ) std::cout << "Input Interpolation Points / Samples: " << count << " / " << valueInterpolationSamples->size() << std::endl; |
| 779 |
} |
| 780 |
|
| 781 |
{ |
| 782 |
InterpolationInfo *valueInterpolationInfo = NULL; |
| 783 |
DenseNodeData< Real , Sigs > constraints; |
| 784 |
InterpolationInfo *iInfo = NULL; |
| 785 |
int solveDepth = params.depth; |
| 786 |
|
| 787 |
implicit.tree.resetNodeIndices( 0 , std::make_tuple() ); |
| 788 |
|
| 789 |
// Get the kernel density estimator |
| 790 |
{ |
| 791 |
profiler.reset(); |
| 792 |
implicit.density = implicit.tree.template setDensityEstimator< 1 , Reconstructor::WeightDegree >( *samples , params.kernelDepth , params.samplesPerNode ); |
| 793 |
if( params.verbose ) std::cout << "# Got kernel density: " << profiler << std::endl; |
| 794 |
} |
| 795 |
|
| 796 |
// Transform the Hermite samples into a vector field |
| 797 |
{ |
| 798 |
profiler.reset(); |
| 799 |
normalInfo = new SparseNodeData< Point< Real , Dim > , NormalSigs >(); |
| 800 |
|
| 801 |
std::function< bool ( InternalNormalAndAuxData , Point< Real , Dim > & ) > ConversionFunction; |
| 802 |
std::function< bool ( InternalNormalAndAuxData , Point< Real , Dim > & , Real & ) > ConversionAndBiasFunction; |
| 803 |
ConversionFunction = []( InternalNormalAndAuxData in , Normal< Real , Dim > &out ) |
| 804 |
{ |
| 805 |
Normal< Real , Dim > n = in.template get<0>(); |
| 806 |
Real l = (Real)Length( n ); |
| 807 |
// It is possible that the samples have non-zero normals but there are two co-located samples with negative normals... |
| 808 |
if( !l ) return false; |
| 809 |
out = n / l; |
| 810 |
return true; |
| 811 |
}; |
| 812 |
*normalInfo = implicit.tree.setInterpolatedDataField( Point< Real , Dim >() , NormalSigs() , *samples , *sampleNormalAndAuxData , implicit.density , params.baseDepth , params.depth , params.lowDepthCutOff , pointDepthAndWeight , ConversionFunction ); |
| 813 |
|
| 814 |
ThreadPool::ParallelFor( 0 , normalInfo->size() , [&]( unsigned int , size_t i ){ (*normalInfo)[i] *= (Real)-1.; } ); |
| 815 |
if( params.verbose ) |
| 816 |
{ |
| 817 |
std::cout << "# Got normal field: " << profiler << std::endl; |
| 818 |
std::cout << "Point depth / Point weight / Estimated measure: " << pointDepthAndWeight.value()[0] << " / " << pointDepthAndWeight.value()[1] << " / " << pointCount*pointDepthAndWeight.value()[1] << std::endl; |
| 819 |
} |
| 820 |
} |
| 821 |
|
| 822 |
// Get the geometry designators indicating if the space node are interior to, exterior to, or contain the envelope boundary |
| 823 |
if( envelopeMesh ) |
| 824 |
{ |
| 825 |
profiler.reset(); |
| 826 |
{ |
| 827 |
// Make the octree complete up to the base depth |
| 828 |
FEMTreeInitializer< Dim , Real >::Initialize( implicit.tree.spaceRoot() , params.baseDepth , []( int , int[] ){ return true; } , implicit.tree.nodeAllocators.size() ? implicit.tree.nodeAllocators[0] : NULL , implicit.tree.initializer() ); |
| 829 |
|
| 830 |
std::vector< Point< Real , Dim > > vertices( envelopeMesh->vertices.size() ); |
| 831 |
for( unsigned int i=0 ; i<vertices.size() ; i++ ) vertices[i] = modelToUnitCube * envelopeMesh->vertices[i]; |
| 832 |
geometryNodeDesignators = FEMTreeInitializer< Dim , Real >::GetGeometryNodeDesignators( &implicit.tree.spaceRoot() , vertices , envelopeMesh->simplices , params.baseDepth , params.envelopeDepth , implicit.tree.nodeAllocators , implicit.tree.initializer() ); |
| 833 |
|
| 834 |
// Make nodes in the support of the vector field @{ExactDepth} interior |
| 835 |
if( params.dirichletErode ) |
| 836 |
{ |
| 837 |
// What to do if we find a node in the support of the vector field |
| 838 |
auto SetScratchFlag = [&]( FEMTreeNode *node ) |
| 839 |
{ |
| 840 |
if( node ) |
| 841 |
{ |
| 842 |
while( node->depth()>(int)params.baseDepth ) node = node->parent; |
| 843 |
node->nodeData.setScratchFlag( true ); |
| 844 |
} |
| 845 |
}; |
| 846 |
|
| 847 |
std::function< void ( FEMTreeNode * ) > PropagateToLeaves = [&]( const FEMTreeNode *node ) |
| 848 |
{ |
| 849 |
geometryNodeDesignators[ node ] = GeometryNodeType::INTERIOR; |
| 850 |
if( node->children ) for( int c=0 ; c<(1<<Dim) ; c++ ) PropagateToLeaves( node->children+c ); |
| 851 |
}; |
| 852 |
|
| 853 |
// Flags indicating if a node contains a non-zero vector field coefficient |
| 854 |
std::vector< bool > isVectorFieldElement( implicit.tree.nodeCount() , false ); |
| 855 |
|
| 856 |
// Get the set of base nodes |
| 857 |
std::vector< FEMTreeNode * > baseNodes; |
| 858 |
auto nodeFunctor = [&]( FEMTreeNode *node ) |
| 859 |
{ |
| 860 |
if( node->depth()==params.baseDepth ) baseNodes.push_back( node ); |
| 861 |
return node->depth()<(int)params.baseDepth; |
| 862 |
}; |
| 863 |
implicit.tree.spaceRoot().processNodes( nodeFunctor ); |
| 864 |
|
| 865 |
std::vector< node_index_type > vectorFieldElementCounts( baseNodes.size() ); |
| 866 |
for( int i=0 ; i<vectorFieldElementCounts.size() ; i++ ) vectorFieldElementCounts[i] = 0; |
| 867 |
|
| 868 |
// In parallel, iterate over the base nodes and mark the nodes containing non-zero vector field coefficients |
| 869 |
ThreadPool::ParallelFor( 0 , baseNodes.size() , [&]( unsigned int t , size_t i ) |
| 870 |
{ |
| 871 |
auto nodeFunctor = [&]( FEMTreeNode *node ) |
| 872 |
{ |
| 873 |
Point< Real , Dim > *n = (*normalInfo)( node ); |
| 874 |
if( n && Point< Real , Dim >::SquareNorm( *n ) ) isVectorFieldElement[ node->nodeData.nodeIndex ] = true , vectorFieldElementCounts[i]++; |
| 875 |
}; |
| 876 |
baseNodes[i]->processNodes( nodeFunctor ); |
| 877 |
} ); |
| 878 |
size_t vectorFieldElementCount = 0; |
| 879 |
for( int i=0 ; i<vectorFieldElementCounts.size() ; i++ ) vectorFieldElementCount += vectorFieldElementCounts[i]; |
| 880 |
|
| 881 |
// Get the subset of nodes containing non-zero vector field coefficients and disable the "scratch" flag |
| 882 |
std::vector< FEMTreeNode * > vectorFieldElements; |
| 883 |
vectorFieldElements.reserve( vectorFieldElementCount ); |
| 884 |
{ |
| 885 |
std::vector< std::vector< FEMTreeNode * > > _vectorFieldElements( baseNodes.size() ); |
| 886 |
for( int i=0 ; i<_vectorFieldElements.size() ; i++ ) _vectorFieldElements[i].reserve( vectorFieldElementCounts[i] ); |
| 887 |
ThreadPool::ParallelFor( 0 , baseNodes.size() , [&]( unsigned int t , size_t i ) |
| 888 |
{ |
| 889 |
auto nodeFunctor = [&]( FEMTreeNode *node ) |
| 890 |
{ |
| 891 |
if( isVectorFieldElement[ node->nodeData.nodeIndex ] ) _vectorFieldElements[i].push_back( node ); |
| 892 |
node->nodeData.setScratchFlag( false ); |
| 893 |
}; |
| 894 |
baseNodes[i]->processNodes( nodeFunctor ); |
| 895 |
} ); |
| 896 |
for( int i=0 ; i<_vectorFieldElements.size() ; i++ ) vectorFieldElements.insert( vectorFieldElements.end() , _vectorFieldElements[i].begin() , _vectorFieldElements[i].end() ); |
| 897 |
} |
| 898 |
|
| 899 |
// Set the scratch flag for the base nodes on which the vector field is supported |
| 900 |
#ifdef SHOW_WARNINGS |
| 901 |
#pragma message( "[WARNING] In principal, we should unlock finite elements whose support overlaps the vector field" ) |
| 902 |
#endif // SHOW_WARNINGS |
| 903 |
implicit.tree.template processNeighboringLeaves< -BSplineSupportSizes< Poisson::NormalDegree >::SupportStart , BSplineSupportSizes< Poisson::NormalDegree >::SupportEnd >( &vectorFieldElements[0] , vectorFieldElements.size() , SetScratchFlag , false ); |
| 904 |
|
| 905 |
// Set sub-trees rooted at interior nodes @ ExactDepth to interior |
| 906 |
ThreadPool::ParallelFor( 0 , baseNodes.size() , [&]( unsigned int , size_t i ){ if( baseNodes[i]->nodeData.getScratchFlag() ) PropagateToLeaves( baseNodes[i] ); } ); |
| 907 |
|
| 908 |
// Adjust the coarser node designators in case exterior nodes have become boundary. |
| 909 |
ThreadPool::ParallelFor( 0 , baseNodes.size() , [&]( unsigned int , size_t i ){ FEMTreeInitializer< Dim , Real >::PullGeometryNodeDesignatorsFromFiner( baseNodes[i] , geometryNodeDesignators ); } ); |
| 910 |
FEMTreeInitializer< Dim , Real >::PullGeometryNodeDesignatorsFromFiner( &implicit.tree.spaceRoot() , geometryNodeDesignators , params.baseDepth ); |
| 911 |
} |
| 912 |
} |
| 913 |
if( params.verbose ) std::cout << "# Initialized envelope constraints: " << profiler << std::endl; |
| 914 |
} |
| 915 |
|
| 916 |
if constexpr( sizeof...(AuxData) ) |
| 917 |
{ |
| 918 |
profiler.reset(); |
| 919 |
auto PointSampleFunctor = [&]( size_t i ) -> const typename FEMTree< Dim , Real >::PointSample & { return (*samples)[i]; }; |
| 920 |
auto AuxDataSampleFunctor = [&]( size_t i ) -> const typename Reconstructor::Implicit< Real , Dim , UIntPack< FEMSigs... > , AuxData... >::InternalAuxData & { return (*sampleNormalAndAuxData)[i].template get<1>(); }; |
| 921 |
implicit._auxData = new SparseNodeData< ProjectiveData< InternalAuxData , Real > , IsotropicUIntPack< Dim , DataSig > > |
| 922 |
( |
| 923 |
implicit.tree.template setExtrapolatedDataField< DataSig , false , Reconstructor::WeightDegree , InternalAuxData > |
| 924 |
( |
| 925 |
InternalAuxData( zero... ) , |
| 926 |
samples->size() , |
| 927 |
PointSampleFunctor , |
| 928 |
AuxDataSampleFunctor , |
| 929 |
(typename Reconstructor::Implicit< Real , Dim , UIntPack< FEMSigs... > , AuxData... >::DensityEstimator*)nullptr |
| 930 |
) |
| 931 |
); |
| 932 |
auto nodeFunctor = [&]( const RegularTreeNode< Dim , FEMTreeNodeData , depth_and_offset_type > *n ) |
| 933 |
{ |
| 934 |
ProjectiveData< InternalAuxData , Real >* clr = (*implicit._auxData)( n ); |
| 935 |
if( clr ) (*clr) *= (Real)pow( (Real)params.perLevelDataScaleFactor , implicit.tree.depth( n ) ); |
| 936 |
}; |
| 937 |
implicit.tree.tree().processNodes( nodeFunctor ); |
| 938 |
if( params.verbose ) std::cout << "# Got aux data: " << profiler << std::endl; |
| 939 |
} |
| 940 |
|
| 941 |
delete sampleNormalAndAuxData; |
| 942 |
|
| 943 |
// Add the interpolation constraints |
| 944 |
if( params.pointWeight>0 ) |
| 945 |
{ |
| 946 |
profiler.reset(); |
| 947 |
if( params.exactInterpolation ) iInfo = FEMTree< Dim , Real >::template InitializeExactPointInterpolationInfo< Real , 0 > ( implicit.tree , *samples , Poisson::ConstraintDual< Dim , Real >( targetValue , params.pointWeight * pointDepthAndWeight.value()[1] ) , Poisson::SystemDual< Dim , Real >( params.pointWeight * pointDepthAndWeight.value()[1] ) , true , false ); |
| 948 |
else iInfo = FEMTree< Dim , Real >::template InitializeApproximatePointInterpolationInfo< Real , 0 > ( implicit.tree , *samples , Poisson::ConstraintDual< Dim , Real >( targetValue , params.pointWeight * pointDepthAndWeight.value()[1] ) , Poisson::SystemDual< Dim , Real >( params.pointWeight * pointDepthAndWeight.value()[1] ) , true , params.depth , 1 ); |
| 949 |
if( params.verbose ) std::cout << "#Initialized point interpolation constraints: " << profiler << std::endl; |
| 950 |
} |
| 951 |
|
| 952 |
// Trim the tree and prepare for multigrid |
| 953 |
{ |
| 954 |
profiler.reset(); |
| 955 |
constexpr int MaxDegree = Poisson::NormalDegree > Degrees::Max() ? Poisson::NormalDegree : Degrees::Max(); |
| 956 |
typename FEMTree< Dim , Real >::template HasNormalDataFunctor< NormalSigs > hasNormalDataFunctor( *normalInfo ); |
| 957 |
auto hasDataFunctor = [&]( const FEMTreeNode *node ){ return hasNormalDataFunctor( node ); }; |
| 958 |
auto addNodeFunctor = [&]( int d , const int off[Dim] ){ return d<=(int)params.fullDepth; }; |
| 959 |
if constexpr( sizeof...(AuxData) ) |
| 960 |
{ |
| 961 |
if( geometryNodeDesignators.size() ) implicit.tree.template finalizeForMultigridWithDirichlet< MaxDegree , Degrees::Max() >( params.baseDepth , addNodeFunctor , hasDataFunctor , [&]( const FEMTreeNode *node ){ return node->nodeData.nodeIndex<(node_index_type)geometryNodeDesignators.size() && geometryNodeDesignators[node]==GeometryNodeType::EXTERIOR; } , std::make_tuple( iInfo ) , std::make_tuple( normalInfo , implicit.density , implicit._auxData , &geometryNodeDesignators ) ); |
| 962 |
else implicit.tree.template finalizeForMultigrid < MaxDegree , Degrees::Max() >( params.baseDepth , addNodeFunctor , hasDataFunctor , std::make_tuple( iInfo ) , std::make_tuple( normalInfo , implicit.density , implicit._auxData ) ); |
| 963 |
} |
| 964 |
else |
| 965 |
{ |
| 966 |
if( geometryNodeDesignators.size() ) implicit.tree.template finalizeForMultigridWithDirichlet< MaxDegree , Degrees::Max() >( params.baseDepth , addNodeFunctor , hasDataFunctor , [&]( const FEMTreeNode *node ){ return node->nodeData.nodeIndex<(node_index_type)geometryNodeDesignators.size() && geometryNodeDesignators[node]==GeometryNodeType::EXTERIOR; } , std::make_tuple( iInfo ) , std::make_tuple( normalInfo , implicit.density , &geometryNodeDesignators ) ); |
| 967 |
else implicit.tree.template finalizeForMultigrid < MaxDegree , Degrees::Max() >( params.baseDepth , addNodeFunctor , hasDataFunctor , std::make_tuple( iInfo ) , std::make_tuple( normalInfo , implicit.density ) ); |
| 968 |
} |
| 969 |
|
| 970 |
if( params.verbose ) std::cout << "# Finalized tree: " << profiler << std::endl; |
| 971 |
} |
| 972 |
|
| 973 |
// Add the FEM constraints |
| 974 |
{ |
| 975 |
profiler.reset(); |
| 976 |
constraints = implicit.tree.initDenseNodeData( Sigs() ); |
| 977 |
|
| 978 |
// Add Poisson constraints |
| 979 |
{ |
| 980 |
typename FEMIntegrator::template Constraint< Sigs , IsotropicUIntPack< Dim , 1 > , NormalSigs , IsotropicUIntPack< Dim , 0 > , Dim > F; |
| 981 |
unsigned int derivatives2[Dim]; |
| 982 |
for( int d=0 ; d<Dim ; d++ ) derivatives2[d] = 0; |
| 983 |
typedef IsotropicUIntPack< Dim , 1 > Derivatives1; |
| 984 |
typedef IsotropicUIntPack< Dim , 0 > Derivatives2; |
| 985 |
for( int d=0 ; d<Dim ; d++ ) |
| 986 |
{ |
| 987 |
unsigned int derivatives1[Dim]; |
| 988 |
for( int dd=0 ; dd<Dim ; dd++ ) derivatives1[dd] = dd==d ? 1 : 0; |
| 989 |
F.weights[d][ TensorDerivatives< Derivatives1 >::Index( derivatives1 ) ][ TensorDerivatives< Derivatives2 >::Index( derivatives2 ) ] = 1; |
| 990 |
} |
| 991 |
implicit.tree.addFEMConstraints( F , *normalInfo , constraints , solveDepth ); |
| 992 |
} |
| 993 |
if( params.verbose ) std::cout << "# Set FEM constraints: " << profiler << std::endl; |
| 994 |
} |
| 995 |
|
| 996 |
// Free up the normal info |
| 997 |
delete normalInfo , normalInfo = NULL; |
| 998 |
|
| 999 |
if( params.pointWeight>0 ) |
| 1000 |
{ |
| 1001 |
profiler.reset(); |
| 1002 |
implicit.tree.addInterpolationConstraints( constraints , solveDepth , std::make_tuple( iInfo ) ); |
| 1003 |
if( params.verbose ) std::cout << "#Set point constraints: " << profiler << std::endl; |
| 1004 |
} |
| 1005 |
|
| 1006 |
if( valueInterpolationSamples && params.valueInterpolationWeight ) |
| 1007 |
{ |
| 1008 |
profiler.reset(); |
| 1009 |
if( params.exactInterpolation ) valueInterpolationInfo = FEMTree< Dim , Real >::template InitializeExactPointAndDataInterpolationInfo< Real , Real , 0 > ( implicit.tree , *valueInterpolationSamples , GetPointer( *valueInterpolationSampleData ) , Poisson::ValueInterpolationConstraintDual< Dim , Real >( params.valueInterpolationWeight ) , Poisson::ValueInterpolationSystemDual< Dim , Real >( params.valueInterpolationWeight ) , true , false ); |
| 1010 |
else valueInterpolationInfo = FEMTree< Dim , Real >::template InitializeApproximatePointAndDataInterpolationInfo< Real , Real , 0 > ( implicit.tree , *valueInterpolationSamples , GetPointer( *valueInterpolationSampleData ) , Poisson::ValueInterpolationConstraintDual< Dim , Real >( params.valueInterpolationWeight ) , Poisson::ValueInterpolationSystemDual< Dim , Real >( params.valueInterpolationWeight ) , true , params.depth , 1 ); |
| 1011 |
delete valueInterpolationSamples , valueInterpolationSamples = NULL; |
| 1012 |
delete valueInterpolationSampleData , valueInterpolationSampleData = NULL; |
| 1013 |
|
| 1014 |
implicit.tree.addInterpolationConstraints( constraints , solveDepth , std::make_tuple( valueInterpolationInfo ) ); |
| 1015 |
if( params.verbose ) std::cout << "#Set value interpolation constraints: " << profiler << std::endl; |
| 1016 |
} |
| 1017 |
|
| 1018 |
if( params.verbose ) std::cout << "All Nodes / Active Nodes / Ghost Nodes / Dirichlet Supported Nodes: " << implicit.tree.allNodes() << " / " << implicit.tree.activeNodes() << " / " << implicit.tree.ghostNodes() << " / " << implicit.tree.dirichletElements() << std::endl; |
| 1019 |
if( params.verbose ) std::cout << "Memory Usage: " << float( MemoryInfo::Usage())/(1<<20) << " MB" << std::endl; |
| 1020 |
|
| 1021 |
// Solve the linear system |
| 1022 |
{ |
| 1023 |
profiler.reset(); |
| 1024 |
typename FEMTree< Dim , Real >::SolverInfo _sInfo; |
| 1025 |
_sInfo.cgDepth = 0 , _sInfo.cascadic = true , _sInfo.vCycles = 1 , _sInfo.iters = params.iters , _sInfo.cgAccuracy = params.cgSolverAccuracy , _sInfo.verbose = params.verbose , _sInfo.showResidual = params.showResidual , _sInfo.showGlobalResidual = SHOW_GLOBAL_RESIDUAL_NONE , _sInfo.sliceBlockSize = 1; |
| 1026 |
_sInfo.baseVCycles = params.baseVCycles; |
| 1027 |
typename FEMIntegrator::template System< Sigs , IsotropicUIntPack< Dim , 1 > > F( { 0. , 1. } ); |
| 1028 |
if( valueInterpolationInfo ) implicit.solution = implicit.tree.solveSystem( Sigs() , F , constraints , params.baseDepth , params.solveDepth , _sInfo , std::make_tuple( iInfo , valueInterpolationInfo ) ); |
| 1029 |
else implicit.solution = implicit.tree.solveSystem( Sigs() , F , constraints , params.baseDepth , params.solveDepth , _sInfo , std::make_tuple( iInfo ) ); |
| 1030 |
if( params.verbose ) std::cout << "# Linear system solved: " << profiler << std::endl; |
| 1031 |
if( iInfo ) delete iInfo , iInfo = NULL; |
| 1032 |
if( valueInterpolationInfo ) delete valueInterpolationInfo , valueInterpolationInfo = NULL; |
| 1033 |
} |
| 1034 |
} |
| 1035 |
|
| 1036 |
// Get the iso-value |
| 1037 |
{ |
| 1038 |
profiler.reset(); |
| 1039 |
double valueSum = 0 , weightSum = 0; |
| 1040 |
typename FEMTree< Dim , Real >::template MultiThreadedEvaluator< Sigs , 0 > evaluator( &implicit.tree , implicit.solution ); |
| 1041 |
std::vector< double > valueSums( ThreadPool::NumThreads() , 0 ) , weightSums( ThreadPool::NumThreads() , 0 ); |
| 1042 |
ThreadPool::ParallelFor( 0 , samples->size() , [&]( unsigned int thread , size_t j ) |
| 1043 |
{ |
| 1044 |
ProjectiveData< Point< Real , Dim > , Real >& sample = (*samples)[j].sample; |
| 1045 |
Real w = sample.weight; |
| 1046 |
if( w>0 ) weightSums[thread] += w , valueSums[thread] += evaluator.values( sample.data / sample.weight , thread , (*samples)[j].node )[0] * w; |
| 1047 |
} ); |
| 1048 |
for( size_t t=0 ; t<valueSums.size() ; t++ ) valueSum += valueSums[t] , weightSum += weightSums[t]; |
| 1049 |
implicit.isoValue = (Real)( valueSum / weightSum ); |
| 1050 |
if( params.verbose ) |
| 1051 |
{ |
| 1052 |
std::cout << "Got average: " << profiler << std::endl; |
| 1053 |
std::cout << "Iso-Value: " << implicit.isoValue << " = " << valueSum << " / " << weightSum << std::endl; |
| 1054 |
} |
| 1055 |
} |
| 1056 |
delete samples; |
| 1057 |
return implicitPtr; |
| 1058 |
} |
| 1059 |
|
| 1060 |
// Implementation of the derived Poisson::Implicit's constructor |
| 1061 |
template< typename Real , unsigned int Dim , unsigned int ... FEMSigs , typename ... AuxData > |
| 1062 |
Implicit< Real , Dim , UIntPack< FEMSigs... > , AuxData... > *SSD::Solver< Real , Dim , UIntPack< FEMSigs... > , AuxData... >::Solve( InputOrientedSampleStream< Real , Dim , AuxData... > &pointStream , SolutionParameters< Real > params , AuxData ... zero ) |
| 1063 |
{ |
| 1064 |
Implicit< Real , Dim , UIntPack< FEMSigs... > , AuxData... > *implicitPtr = new Implicit< Real , Dim , UIntPack< FEMSigs... > , AuxData... >( zero... ); |
| 1065 |
Implicit< Real , Dim , UIntPack< FEMSigs... > , AuxData... > &implicit = *implicitPtr; |
| 1066 |
|
| 1067 |
// The signature for the finite-elements representing the auxiliary data (if it's there) |
| 1068 |
static const unsigned int DataSig = FEMDegreeAndBType< Reconstructor::DataDegree , BOUNDARY_FREE >::Signature; |
| 1069 |
|
| 1070 |
/////////////// |
| 1071 |
// Types --> // |
| 1072 |
using InternalAuxData = typename Reconstructor::Implicit< Real , Dim , UIntPack< FEMSigs... > , AuxData... >::InternalAuxData; |
| 1073 |
using InternalNormalAndAuxData = typename Reconstructor::Implicit< Real , Dim , UIntPack< FEMSigs... > , AuxData... >::InternalNormalAndAuxData; |
| 1074 |
using _SampleTypeConverter = typename Reconstructor::Implicit< Real , Dim , UIntPack< FEMSigs... > , AuxData... >::_SampleTypeConverter; |
| 1075 |
using Sigs = typename Reconstructor::Implicit< Real , Dim , UIntPack< FEMSigs... > , AuxData... >::Sigs; |
| 1076 |
|
| 1077 |
// The degrees of the finite elements across the different axes |
| 1078 |
typedef UIntPack< FEMSignature< FEMSigs >::Degree ... > Degrees; |
| 1079 |
|
| 1080 |
// The signature describing the normals elements |
| 1081 |
typedef UIntPack< FEMDegreeAndBType< SSD::NormalDegree , DerivativeBoundary< FEMSignature< FEMSigs >::BType , 1 >::BType >::Signature ... > NormalSigs; |
| 1082 |
|
| 1083 |
// Type for tracking sample interpolation |
| 1084 |
typedef typename FEMTree< Dim , Real >::template InterpolationInfo< Real , 1 > InterpolationInfo; |
| 1085 |
|
| 1086 |
// The finite-element tracking tree node |
| 1087 |
typedef RegularTreeNode< Dim , FEMTreeNodeData , depth_and_offset_type > FEMTreeNode; |
| 1088 |
// <-- Types // |
| 1089 |
/////////////// |
| 1090 |
|
| 1091 |
XForm< Real , Dim+1 > modelToUnitCube = XForm< Real , Dim+1 >::Identity(); |
| 1092 |
|
| 1093 |
Profiler profiler( ProfilerMS ); |
| 1094 |
|
| 1095 |
size_t pointCount; |
| 1096 |
|
| 1097 |
ProjectiveData< Point< Real , 2 > , Real > pointDepthAndWeight; |
| 1098 |
SparseNodeData< Point< Real , Dim > , NormalSigs > *normalInfo = nullptr; |
| 1099 |
std::vector< typename FEMTree< Dim , Real >::PointSample > *samples = new std::vector< typename FEMTree< Dim , Real >::PointSample >(); |
| 1100 |
std::vector< InternalNormalAndAuxData > *sampleNormalAndAuxData = new std::vector< InternalNormalAndAuxData >();; |
| 1101 |
|
| 1102 |
Real targetValue = (Real)0.0; |
| 1103 |
|
| 1104 |
// Read in the samples (and auxiliary data) |
| 1105 |
{ |
| 1106 |
profiler.reset(); |
| 1107 |
|
| 1108 |
pointStream.reset(); |
| 1109 |
modelToUnitCube = params.scale>0 ? PointExtent::GetXForm< Real , Dim , true , Normal< Real , Dim > , AuxData... >( pointStream , Normal< Real , Dim >() , zero... , params.scale , params.alignDir ) * modelToUnitCube : modelToUnitCube; |
| 1110 |
implicit.unitCubeToModel = modelToUnitCube.inverse(); |
| 1111 |
pointStream.reset(); |
| 1112 |
|
| 1113 |
params.template testAndSet< Dim >( implicit.unitCubeToModel ); |
| 1114 |
|
| 1115 |
{ |
| 1116 |
// Apply the transformation |
| 1117 |
TransformedInputOrientedSampleStream< Real , Dim , AuxData... > _pointStream( modelToUnitCube , pointStream ); |
| 1118 |
|
| 1119 |
// Convert: |
| 1120 |
// InputDataStream< Position< Real , Dim > , Normal< Real , Dim > , AuxData ... > |
| 1121 |
// -> InputDataStream< Position< Real , Dim > , InternalNormalAndAuxData > |
| 1122 |
InputDataStreamConverter< typename _SampleTypeConverter::InternalSampleType , typename _SampleTypeConverter::ExternalSampleType > __pointStream( _pointStream , _SampleTypeConverter::ConvertI2X , Position< Real , Dim >() , Normal< Real , Dim >() , zero... ); |
| 1123 |
|
| 1124 |
std::vector< node_index_type > nodeToIndexMap; |
| 1125 |
|
| 1126 |
auto IsValid = [&]( const Point< Real , Dim > &p , const Normal< Real , Dim > &n , AuxData ... d ) |
| 1127 |
{ |
| 1128 |
Real l = Point< Real , Dim >::SquareNorm( n ); |
| 1129 |
return l>0 && std::isfinite(l); |
| 1130 |
}; |
| 1131 |
|
| 1132 |
auto Process = [&]( FEMTreeNode &node , const Point< Real , Dim > &p , Normal< Real , Dim > &n , AuxData ... d ) |
| 1133 |
{ |
| 1134 |
Real l = (Real)Length( n ); |
| 1135 |
Real weight = params.confidence ? l : (Real)1.; |
| 1136 |
n /= l; |
| 1137 |
|
| 1138 |
node_index_type nodeIndex = node.nodeData.nodeIndex; |
| 1139 |
// If the node's index exceeds what's stored in the node-to-index map, grow the node-to-index map |
| 1140 |
if( nodeIndex>=(node_index_type)nodeToIndexMap.size() ) nodeToIndexMap.resize( nodeIndex+1 , -1 ); |
| 1141 |
|
| 1142 |
node_index_type idx = nodeToIndexMap[ nodeIndex ]; |
| 1143 |
if( idx==-1 ) |
| 1144 |
{ |
| 1145 |
idx = (node_index_type)samples->size(); |
| 1146 |
nodeToIndexMap[ nodeIndex ] = idx; |
| 1147 |
samples->resize( idx+1 ) , (*samples)[idx].node = &node; |
| 1148 |
sampleNormalAndAuxData->resize( idx+1 ); |
| 1149 |
(*samples)[idx].sample = ProjectiveData< Point< Real , Dim > , Real >( p*weight , weight ); |
| 1150 |
(*sampleNormalAndAuxData)[idx] = InternalNormalAndAuxData( n , DirectSum< Real , AuxData... >( d... ) ) * weight; |
| 1151 |
} |
| 1152 |
else |
| 1153 |
{ |
| 1154 |
(*samples)[idx].sample += ProjectiveData< Point< Real , Dim > , Real >( p*weight , weight ); |
| 1155 |
(*sampleNormalAndAuxData)[idx] += InternalNormalAndAuxData( n , DirectSum< Real , AuxData... >( d... ) ) * weight; |
| 1156 |
} |
| 1157 |
return true; |
| 1158 |
}; |
| 1159 |
auto F = [&]( AuxData ... zeroAuxData ) |
| 1160 |
{ |
| 1161 |
pointCount = FEMTreeInitializer< Dim , Real >::template Initialize< decltype(IsValid) , decltype(Process) , Normal< Real , Dim > , AuxData... >( implicit.tree.spaceRoot() , _pointStream , Normal< Real , Dim >() , zeroAuxData... , params.depth , implicit.tree.nodeAllocators.size() ? implicit.tree.nodeAllocators[0] : nullptr , implicit.tree.initializer() , IsValid , Process ); |
| 1162 |
}; |
| 1163 |
implicit._zeroAuxData.process( F ); |
| 1164 |
} |
| 1165 |
|
| 1166 |
if( params.verbose ) |
| 1167 |
{ |
| 1168 |
std::cout << "Input Points / Samples: " << pointCount << " / " << samples->size() << std::endl; |
| 1169 |
std::cout << "# Read input into tree: " << profiler << std::endl; |
| 1170 |
} |
| 1171 |
} |
| 1172 |
{ |
| 1173 |
DenseNodeData< Real , Sigs > constraints; |
| 1174 |
InterpolationInfo *iInfo = NULL; |
| 1175 |
int solveDepth = params.depth; |
| 1176 |
|
| 1177 |
implicit.tree.resetNodeIndices( 0 , std::make_tuple() ); |
| 1178 |
|
| 1179 |
// Get the kernel density estimator |
| 1180 |
{ |
| 1181 |
profiler.reset(); |
| 1182 |
implicit.density = implicit.tree.template setDensityEstimator< 1 , Reconstructor::WeightDegree >( *samples , params.kernelDepth , params.samplesPerNode ); |
| 1183 |
if( params.verbose ) std::cout << "# Got kernel density: " << profiler << std::endl; |
| 1184 |
} |
| 1185 |
|
| 1186 |
// Transform the Hermite samples into a vector field |
| 1187 |
{ |
| 1188 |
profiler.reset(); |
| 1189 |
normalInfo = new SparseNodeData< Point< Real , Dim > , NormalSigs >(); |
| 1190 |
|
| 1191 |
std::function< bool ( InternalNormalAndAuxData , Point< Real , Dim > & ) > ConversionFunction; |
| 1192 |
std::function< bool ( InternalNormalAndAuxData , Point< Real , Dim > & , Real & ) > ConversionAndBiasFunction; |
| 1193 |
ConversionFunction = []( InternalNormalAndAuxData in , Normal< Real , Dim > &out ) |
| 1194 |
{ |
| 1195 |
Normal< Real , Dim > n = in.template get<0>(); |
| 1196 |
Real l = (Real)Length( n ); |
| 1197 |
// It is possible that the samples have non-zero normals but there are two co-located samples with negative normals... |
| 1198 |
if( !l ) return false; |
| 1199 |
out = n / l; |
| 1200 |
return true; |
| 1201 |
}; |
| 1202 |
*normalInfo = implicit.tree.setInterpolatedDataField( Point< Real , Dim >() , NormalSigs() , *samples , *sampleNormalAndAuxData , implicit.density , params.baseDepth , params.depth , params.lowDepthCutOff , pointDepthAndWeight , ConversionFunction ); |
| 1203 |
|
| 1204 |
if( params.verbose ) |
| 1205 |
{ |
| 1206 |
std::cout << "# Got normal field: " << profiler << std::endl; |
| 1207 |
std::cout << "Point depth / Point weight / Estimated measure: " << pointDepthAndWeight.value()[0] << " / " << pointDepthAndWeight.value()[1] << " / " << pointCount*pointDepthAndWeight.value()[1] << std::endl; |
| 1208 |
} |
| 1209 |
} |
| 1210 |
|
| 1211 |
if constexpr( sizeof...(AuxData) ) |
| 1212 |
{ |
| 1213 |
profiler.reset(); |
| 1214 |
auto PointSampleFunctor = [&]( size_t i ) -> const typename FEMTree< Dim , Real >::PointSample & { return (*samples)[i]; }; |
| 1215 |
auto AuxDataSampleFunctor = [&]( size_t i ) -> const typename Reconstructor::Implicit< Real , Dim , UIntPack< FEMSigs... > , AuxData... >::InternalAuxData & { return (*sampleNormalAndAuxData)[i].template get<1>(); }; |
| 1216 |
implicit._auxData = new SparseNodeData< ProjectiveData< InternalAuxData , Real > , IsotropicUIntPack< Dim , DataSig > > |
| 1217 |
( |
| 1218 |
implicit.tree.template setExtrapolatedDataField< DataSig , false , Reconstructor::WeightDegree , InternalAuxData > |
| 1219 |
( |
| 1220 |
InternalAuxData( zero... ) , |
| 1221 |
samples->size() , |
| 1222 |
PointSampleFunctor , |
| 1223 |
AuxDataSampleFunctor , |
| 1224 |
(typename Reconstructor::Implicit< Real , Dim , UIntPack< FEMSigs... > , AuxData... >::DensityEstimator*)nullptr |
| 1225 |
) |
| 1226 |
); |
| 1227 |
auto nodeFunctor = [&]( const RegularTreeNode< Dim , FEMTreeNodeData , depth_and_offset_type > *n ) |
| 1228 |
{ |
| 1229 |
ProjectiveData< InternalAuxData , Real >* clr = (*implicit._auxData)( n ); |
| 1230 |
if( clr ) (*clr) *= (Real)pow( (Real)params.perLevelDataScaleFactor , implicit.tree.depth( n ) ); |
| 1231 |
}; |
| 1232 |
implicit.tree.tree().processNodes( nodeFunctor ); |
| 1233 |
if( params.verbose ) std::cout << "# Got aux data: " << profiler << std::endl; |
| 1234 |
} |
| 1235 |
// Add the interpolation constraints |
| 1236 |
if( params.pointWeight>0 || params.gradientWeight>0 ) |
| 1237 |
{ |
| 1238 |
profiler.reset(); |
| 1239 |
if( params.exactInterpolation ) iInfo = FEMTree< Dim , Real >::template InitializeExactPointAndDataInterpolationInfo< Real , InternalNormalAndAuxData , 1 >( implicit.tree , *samples , GetPointer( *sampleNormalAndAuxData ) , SSD::ConstraintDual< Dim , Real , AuxData... >( targetValue , params.pointWeight * pointDepthAndWeight.value()[1] , params.gradientWeight * pointDepthAndWeight.value()[1] ) , SSD::SystemDual< Dim , Real , AuxData... >( params.pointWeight * pointDepthAndWeight.value()[1] , params.gradientWeight * pointDepthAndWeight.value()[1] ) , true , false ); |
| 1240 |
else iInfo = FEMTree< Dim , Real >::template InitializeApproximatePointAndDataInterpolationInfo< Real , InternalNormalAndAuxData , 1 >( implicit.tree , *samples , GetPointer( *sampleNormalAndAuxData ) , SSD::ConstraintDual< Dim , Real , AuxData... >( targetValue , params.pointWeight * pointDepthAndWeight.value()[1] , params.gradientWeight * pointDepthAndWeight.value()[1] ) , SSD::SystemDual< Dim , Real , AuxData... >( params.pointWeight * pointDepthAndWeight.value()[1] , params.gradientWeight * pointDepthAndWeight.value()[1] ) , true , params.depth , 1 ); |
| 1241 |
if( params.verbose ) std::cout << "#Initialized point interpolation constraints: " << profiler << std::endl; |
| 1242 |
} |
| 1243 |
|
| 1244 |
delete sampleNormalAndAuxData; |
| 1245 |
|
| 1246 |
|
| 1247 |
// Trim the tree and prepare for multigrid |
| 1248 |
{ |
| 1249 |
profiler.reset(); |
| 1250 |
constexpr int MaxDegree = SSD::NormalDegree > Degrees::Max() ? SSD::NormalDegree : Degrees::Max(); |
| 1251 |
typename FEMTree< Dim , Real >::template HasNormalDataFunctor< NormalSigs > hasNormalDataFunctor( *normalInfo ); |
| 1252 |
auto hasDataFunctor = [&]( const FEMTreeNode *node ){ return hasNormalDataFunctor( node ); }; |
| 1253 |
auto addNodeFunctor = [&]( int d , const int off[Dim] ){ return d<=(int)params.fullDepth; }; |
| 1254 |
if constexpr( sizeof...(AuxData) ) implicit.tree.template finalizeForMultigrid< MaxDegree , Degrees::Max() >( params.baseDepth , addNodeFunctor , hasDataFunctor , std::make_tuple( iInfo ) , std::make_tuple( normalInfo , implicit.density , implicit._auxData ) ); |
| 1255 |
else implicit.tree.template finalizeForMultigrid< MaxDegree , Degrees::Max() >( params.baseDepth , addNodeFunctor , hasDataFunctor , std::make_tuple( iInfo ) , std::make_tuple( normalInfo , implicit.density ) ); |
| 1256 |
|
| 1257 |
if( params.verbose ) std::cout << "# Finalized tree: " << profiler << std::endl; |
| 1258 |
} |
| 1259 |
|
| 1260 |
// Free up the normal info |
| 1261 |
delete normalInfo , normalInfo = NULL; |
| 1262 |
|
| 1263 |
if( params.pointWeight>0 || params.gradientWeight>0 ) |
| 1264 |
{ |
| 1265 |
profiler.reset(); |
| 1266 |
constraints = implicit.tree.initDenseNodeData( Sigs() ); |
| 1267 |
implicit.tree.addInterpolationConstraints( constraints , solveDepth , std::make_tuple( iInfo ) ); |
| 1268 |
if( params.verbose ) std::cout << "#Set point constraints: " << profiler << std::endl; |
| 1269 |
} |
| 1270 |
|
| 1271 |
if( params.verbose ) std::cout << "All Nodes / Active Nodes / Ghost Nodes: " << implicit.tree.allNodes() << " / " << implicit.tree.activeNodes() << " / " << implicit.tree.ghostNodes() << std::endl; |
| 1272 |
if( params.verbose ) std::cout << "Memory Usage: " << float( MemoryInfo::Usage())/(1<<20) << " MB" << std::endl; |
| 1273 |
|
| 1274 |
// Solve the linear system |
| 1275 |
{ |
| 1276 |
profiler.reset(); |
| 1277 |
typename FEMTree< Dim , Real >::SolverInfo _sInfo; |
| 1278 |
_sInfo.cgDepth = 0 , _sInfo.cascadic = true , _sInfo.vCycles = 1 , _sInfo.iters = params.iters , _sInfo.cgAccuracy = params.cgSolverAccuracy , _sInfo.verbose = params.verbose , _sInfo.showResidual = params.showResidual , _sInfo.showGlobalResidual = SHOW_GLOBAL_RESIDUAL_NONE , _sInfo.sliceBlockSize = 1; |
| 1279 |
_sInfo.baseVCycles = params.baseVCycles; |
| 1280 |
typename FEMIntegrator::template System< Sigs , IsotropicUIntPack< Dim , 2 > > F( { 0. , 0. , (double)params.biLapWeight } ); |
| 1281 |
implicit.solution = implicit.tree.solveSystem( Sigs() , F , constraints , params.baseDepth , params.solveDepth , _sInfo , std::make_tuple( iInfo ) ); |
| 1282 |
if( params.verbose ) std::cout << "# Linear system solved: " << profiler << std::endl; |
| 1283 |
if( iInfo ) delete iInfo , iInfo = NULL; |
| 1284 |
} |
| 1285 |
} |
| 1286 |
|
| 1287 |
// Get the iso-value |
| 1288 |
{ |
| 1289 |
profiler.reset(); |
| 1290 |
double valueSum = 0 , weightSum = 0; |
| 1291 |
typename FEMTree< Dim , Real >::template MultiThreadedEvaluator< Sigs , 0 > evaluator( &implicit.tree , implicit.solution ); |
| 1292 |
std::vector< double > valueSums( ThreadPool::NumThreads() , 0 ) , weightSums( ThreadPool::NumThreads() , 0 ); |
| 1293 |
ThreadPool::ParallelFor( 0 , samples->size() , [&]( unsigned int thread , size_t j ) |
| 1294 |
{ |
| 1295 |
ProjectiveData< Point< Real , Dim > , Real >& sample = (*samples)[j].sample; |
| 1296 |
Real w = sample.weight; |
| 1297 |
if( w>0 ) weightSums[thread] += w , valueSums[thread] += evaluator.values( sample.data / sample.weight , thread , (*samples)[j].node )[0] * w; |
| 1298 |
} ); |
| 1299 |
for( size_t t=0 ; t<valueSums.size() ; t++ ) valueSum += valueSums[t] , weightSum += weightSums[t]; |
| 1300 |
implicit.isoValue = (Real)( valueSum / weightSum ); |
| 1301 |
if( params.verbose ) |
| 1302 |
{ |
| 1303 |
std::cout << "Got average: " << profiler << std::endl; |
| 1304 |
std::cout << "Iso-Value: " << implicit.isoValue << " = " << valueSum << " / " << weightSum << std::endl; |
| 1305 |
} |
| 1306 |
} |
| 1307 |
delete samples; |
| 1308 |
return implicitPtr; |
| 1309 |
} |
| 1310 |
} |
| 1311 |
} |
| 1312 |
|
| 1313 |
|
| 1314 |
#endif // RECONSTRUCTORS_INCLUDED |