| 1 |
/* |
| 2 |
Copyright (c) 2013, 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 |
#define BIG_DATA |
| 30 |
#include "PreProcessor.h" |
| 31 |
|
| 32 |
#undef USE_DOUBLE |
| 33 |
#define DEFAULT_DIMENSION 2 |
| 34 |
#define ROW_BLOCK_SIZE 16 |
| 35 |
#define DEFAULT_FEM_DEGREE 1 |
| 36 |
|
| 37 |
#ifndef USE_DEEP_TREE_NODES |
| 38 |
#define USE_DEEP_TREE_NODES |
| 39 |
#endif // USE_DEEP_TREE_NODES |
| 40 |
|
| 41 |
#include <stdio.h> |
| 42 |
#include <stdlib.h> |
| 43 |
#include <float.h> |
| 44 |
#include <algorithm> |
| 45 |
#include "Image.h" |
| 46 |
#include "MyMiscellany.h" |
| 47 |
#include "Array.h" |
| 48 |
#include "CmdLineParser.h" |
| 49 |
#include "Geometry.h" |
| 50 |
#include "FEMTree.h" |
| 51 |
|
| 52 |
using namespace PoissonRecon; |
| 53 |
|
| 54 |
CmdLineParameterArray< char* , 2 > |
| 55 |
In( "in" ); |
| 56 |
CmdLineParameter< char* > |
| 57 |
Out( "out" ); |
| 58 |
CmdLineParameter< int > |
| 59 |
#ifdef FAST_COMPILE |
| 60 |
#else // !FAST_COMPILE |
| 61 |
Degree( "degree" , DEFAULT_FEM_DEGREE ) , |
| 62 |
#endif // FAST_COMPILE |
| 63 |
ParallelType( "parallel" , 0 ) , |
| 64 |
ScheduleType( "schedule" , (int)ThreadPool::Schedule ) , |
| 65 |
ThreadChunkSize( "chunkSize" , (int)ThreadPool::ChunkSize ) , |
| 66 |
MaxMemoryGB( "maxMemory" , 0 ) , |
| 67 |
GSIterations( "iters" , 8 ) , |
| 68 |
FullDepth( "fullDepth" , 6 ) , |
| 69 |
BaseDepth( "baseDepth" ) , |
| 70 |
BaseVCycles( "baseVCycles" , 4 ); |
| 71 |
CmdLineReadable |
| 72 |
Verbose( "verbose" ) , |
| 73 |
ShowResidual( "showResidual" ) , |
| 74 |
Performance( "performance" ); |
| 75 |
CmdLineParameter< float > |
| 76 |
WeightScale ( "wScl", 0.125f ) , |
| 77 |
WeightExponent( "wExp" , 6.f ); |
| 78 |
|
| 79 |
CmdLineReadable* params[] = |
| 80 |
{ |
| 81 |
&In , &Out , &Verbose , &ShowResidual , &GSIterations , &FullDepth , |
| 82 |
&BaseDepth , &BaseVCycles , |
| 83 |
&WeightScale , &WeightExponent , |
| 84 |
&Performance , |
| 85 |
&MaxMemoryGB , |
| 86 |
#if !defined( FAST_COMPILE ) |
| 87 |
&Degree , |
| 88 |
#endif // !FAST_COMPILE |
| 89 |
&ParallelType , |
| 90 |
&ScheduleType , |
| 91 |
&ThreadChunkSize , |
| 92 |
NULL |
| 93 |
}; |
| 94 |
|
| 95 |
void ShowUsage( char* ex ) |
| 96 |
{ |
| 97 |
printf( "Usage: %s\n" , ex ); |
| 98 |
printf( "\t --%s <input color / labels>\n" , In.name ); |
| 99 |
printf( "\t[--%s <ouput stitched image>]\n" , Out.name ); |
| 100 |
#if !defined( FAST_COMPILE ) |
| 101 |
printf( "\t[--%s <b-spline degree>=%d]\n" , Degree.name , Degree.value ); |
| 102 |
#endif // !FAST_COMPILE |
| 103 |
printf( "\t[--%s <GS iterations>=%d]\n" , GSIterations.name , GSIterations.value ); |
| 104 |
printf( "\t[--%s <full depth>=%d]\n" , FullDepth.name , FullDepth.value ); |
| 105 |
printf( "\t[--%s <parallel type>=%d]\n" , ParallelType.name , ParallelType.value ); |
| 106 |
for( size_t i=0 ; i<ThreadPool::ParallelNames.size() ; i++ ) printf( "\t\t%d] %s\n" , (int)i , ThreadPool::ParallelNames[i].c_str() ); |
| 107 |
printf( "\t[--%s <schedue type>=%d]\n" , ScheduleType.name , ScheduleType.value ); |
| 108 |
for( size_t i=0 ; i<ThreadPool::ScheduleNames.size() ; i++ ) printf( "\t\t%d] %s\n" , (int)i , ThreadPool::ScheduleNames[i].c_str() ); |
| 109 |
printf( "\t[--%s <thread chunk size>=%d]\n" , ThreadChunkSize.name , ThreadChunkSize.value ); |
| 110 |
printf( "\t[--%s <successive under-relaxation scale>=%f]\n", WeightScale.name , WeightScale.value ); |
| 111 |
printf( "\t[--%s <successive under-relaxation exponent>=%f]\n", WeightExponent.name , WeightExponent.value ); |
| 112 |
printf( "\t[--%s <maximum memory (in GB)>=%d]\n" , MaxMemoryGB.name , MaxMemoryGB.value ); |
| 113 |
printf( "\t[--%s]\n" , Performance.name ); |
| 114 |
printf( "\t[--%s <coarse MG solver depth>]\n" , BaseDepth.name ); |
| 115 |
printf( "\t[--%s <coarse MG solver v-cycles>=%d]\n" , BaseVCycles.name , BaseVCycles.value ); |
| 116 |
printf( "\t[--%s]\n" , ShowResidual.name ); |
| 117 |
printf( "\t[--%s]\n" , Verbose.name ); |
| 118 |
} |
| 119 |
|
| 120 |
struct RGBPixel |
| 121 |
{ |
| 122 |
unsigned char rgb[3]; |
| 123 |
unsigned char& operator[]( int idx ){ return rgb[idx]; } |
| 124 |
const unsigned char& operator[]( int idx ) const { return rgb[idx]; } |
| 125 |
int mask( void ) const |
| 126 |
{ |
| 127 |
if( rgb[0]==255 && rgb[1]==255 && rgb[2]==255 ) return -1; |
| 128 |
else return ( (int)rgb[0] )<<16 | ( (int)rgb[1] )<<8 | ( (int)rgb[2] ); |
| 129 |
} |
| 130 |
|
| 131 |
RGBPixel( void ){ rgb[0] = rgb[1] = rgb[2] = 0; } |
| 132 |
RGBPixel( double r , double g , double b ) |
| 133 |
{ |
| 134 |
rgb[0] = (unsigned char)( std::max< int >( 0 , std::min< int >( 255 , (int)( r*255 ) ) ) ); |
| 135 |
rgb[1] = (unsigned char)( std::max< int >( 0 , std::min< int >( 255 , (int)( g*255 ) ) ) ); |
| 136 |
rgb[2] = (unsigned char)( std::max< int >( 0 , std::min< int >( 255 , (int)( b*255 ) ) ) ); |
| 137 |
} |
| 138 |
RGBPixel( float r , float g , float b ) |
| 139 |
{ |
| 140 |
rgb[0] = (unsigned char)( std::max< int >( 0 , std::min< int >( 255 , (int)( r*255 ) ) ) ); |
| 141 |
rgb[1] = (unsigned char)( std::max< int >( 0 , std::min< int >( 255 , (int)( g*255 ) ) ) ); |
| 142 |
rgb[2] = (unsigned char)( std::max< int >( 0 , std::min< int >( 255 , (int)( b*255 ) ) ) ); |
| 143 |
} |
| 144 |
|
| 145 |
template< class Real > |
| 146 |
static Point< Real , 3 > ToPoint( RGBPixel rgb ) |
| 147 |
{ |
| 148 |
Point< Real , 3 > p; |
| 149 |
for( int c=0 ; c<3 ; c++ ) p[c] = (Real)( ( (double)rgb[c] ) / 255. ); |
| 150 |
return p; |
| 151 |
} |
| 152 |
}; |
| 153 |
|
| 154 |
void WriteImage( char* fileName , RGBPixel* pixels , int w , int h ) |
| 155 |
{ |
| 156 |
unsigned int _w = w , _h = h , _c = 3; |
| 157 |
ImageWriter::Write( fileName , (const unsigned char*)pixels , _w , _h , _c ); |
| 158 |
} |
| 159 |
|
| 160 |
template< unsigned int Colors > |
| 161 |
void ReadAndWrite( ImageReader* pixels , ImageReader* labels , ImageWriter* output ) |
| 162 |
{ |
| 163 |
RGBPixel* pixelRow = new RGBPixel[ pixels->width() ]; |
| 164 |
RGBPixel* labelRow = new RGBPixel[ labels->width() ]; |
| 165 |
for( unsigned int r=0 ; r<pixels->height() ; r++ ) |
| 166 |
{ |
| 167 |
if( Verbose.set ) printf( "%d / %d \r" , r ,pixels->height() ); |
| 168 |
pixels->nextRow( (unsigned char*)pixelRow ); |
| 169 |
labels->nextRow( (unsigned char*)labelRow ); |
| 170 |
output->nextRow( (unsigned char*)pixelRow ); |
| 171 |
} |
| 172 |
if( Verbose.set ) printf( "\n" ); |
| 173 |
delete[] pixelRow; |
| 174 |
delete[] labelRow; |
| 175 |
} |
| 176 |
|
| 177 |
template< class Real , unsigned int Colors > |
| 178 |
struct BufferedImageDerivativeStream : public FEMTreeInitializer< DEFAULT_DIMENSION , Real >::template DerivativeStream< Point< Real , Colors > > |
| 179 |
{ |
| 180 |
BufferedImageDerivativeStream( const unsigned int resolution[] , ImageReader* pixels , ImageReader* labels ) : _pixels( pixels ) , _labels( labels ) |
| 181 |
{ |
| 182 |
memcpy( _resolution , resolution , sizeof( unsigned int ) * DEFAULT_DIMENSION ); |
| 183 |
for( int i=0 ; i<3 ; i++ ) |
| 184 |
{ |
| 185 |
_pixelRows[i] = new RGBPixel[ _resolution[0] ]; |
| 186 |
_labelRows[i] = new RGBPixel[ _resolution[0] ]; |
| 187 |
_maskRows [i] = new int[ _resolution[0] ]; |
| 188 |
} |
| 189 |
if( pixels->channels()!=3 && pixels->channels()!=1 ) MK_THROW( "Pixel input must have 1 or 3 channels: " , pixels->channels() ); |
| 190 |
if( labels->channels()!=3 && labels->channels()!=1 ) MK_THROW( "Label input must have 1 or 3 channels: " , labels->channels() ); |
| 191 |
__pixelRow = pixels->channels()==3 ? NULL : new unsigned char[ _resolution[0] ]; |
| 192 |
__labelRow = labels->channels()==3 ? NULL : new unsigned char[ _resolution[0] ]; |
| 193 |
_r = -2 ; prefetch(); |
| 194 |
_r = -1 ; prefetch(); |
| 195 |
_c = _r = _dir = 0; |
| 196 |
} |
| 197 |
~BufferedImageDerivativeStream( void ) |
| 198 |
{ |
| 199 |
for( int i=0 ; i<3 ; i++ ) delete[] _pixelRows[i] , delete[] _labelRows[i] , delete[] _maskRows[i]; |
| 200 |
if( __pixelRow ) delete[] __pixelRow; |
| 201 |
if( __labelRow ) delete[] __labelRow; |
| 202 |
} |
| 203 |
void resolution( unsigned int res[] ) const { memcpy( res , _resolution , sizeof(_resolution) ); } |
| 204 |
|
| 205 |
void advance( void ){ _c = _dir = 0 , _r++; } |
| 206 |
void prefetch( void ) |
| 207 |
{ |
| 208 |
if( _r+2<(int)_resolution[1] ) |
| 209 |
{ |
| 210 |
int j = (_r+2)%3; |
| 211 |
RGBPixel *pixelRow = _pixelRows[j] , *labelRow = _labelRows[j]; |
| 212 |
int *maskRow = _maskRows[j]; |
| 213 |
if( _pixels->channels()==3 ) _pixels->nextRow( (unsigned char*)pixelRow ); |
| 214 |
else |
| 215 |
{ |
| 216 |
_pixels->nextRow( __pixelRow ); |
| 217 |
for( int i=0 ; i<(int)_resolution[0] ; i++ ) pixelRow[i][0] = pixelRow[i][1] = pixelRow[i][2] = __pixelRow[i]; |
| 218 |
} |
| 219 |
if( _labels->channels()==3 ) _labels->nextRow( (unsigned char*)labelRow ); |
| 220 |
else |
| 221 |
{ |
| 222 |
_labels->nextRow( __labelRow ); |
| 223 |
for( int i=0 ; i<(int)_resolution[0] ; i++ ) labelRow[i][0] = labelRow[i][1] = labelRow[i][2] = __labelRow[i]; |
| 224 |
} |
| 225 |
ThreadPool::ParallelFor( 0 , _resolution[0] , [&]( unsigned int , size_t i ){ maskRow[i] = labelRow[i].mask(); } ); |
| 226 |
} |
| 227 |
} |
| 228 |
|
| 229 |
bool nextDerivative( unsigned int idx[] , unsigned int& dir , Point< Real , Colors >& dValue ) |
| 230 |
{ |
| 231 |
const RGBPixel *pixelRow1 = _pixelRows[_r%3] , *pixelRow2 = _pixelRows[(_r+1)%3]; |
| 232 |
const int *maskRow1 = _maskRows[_r%3] , *maskRow2 = _maskRows[(_r+1)%3]; |
| 233 |
if( _dir==0 ) |
| 234 |
{ |
| 235 |
for( ; _c<(int)_resolution[0]-1 ; _c++ ) |
| 236 |
{ |
| 237 |
if( maskRow1[_c]!=maskRow1[_c+1] && maskRow1[_c]>=0 && maskRow1[_c+1]>=0 ) |
| 238 |
{ |
| 239 |
idx[0] = _c , idx[1] = _r , dir = _dir; |
| 240 |
dValue = RGBPixel::ToPoint< Real >( pixelRow1[_c+1] ) - RGBPixel::ToPoint< Real >( pixelRow1[_c] ); |
| 241 |
_c++; |
| 242 |
return true; |
| 243 |
} |
| 244 |
} |
| 245 |
_dir = 1 , _c = 0; |
| 246 |
} |
| 247 |
if( _dir==1 ) |
| 248 |
{ |
| 249 |
if( _r+1<(int)_resolution[1] ) |
| 250 |
{ |
| 251 |
for( ; _c<(int)_resolution[0] ; _c++ ) |
| 252 |
{ |
| 253 |
if( maskRow1[_c]!=maskRow2[_c] && maskRow1[_c]>=0 && maskRow2[_c]>=0 ) |
| 254 |
{ |
| 255 |
idx[0] = _c , idx[1] = _r , dir = _dir; |
| 256 |
dValue = RGBPixel::ToPoint< Real >( pixelRow2[_c] ) - RGBPixel::ToPoint< Real >( pixelRow1[_c] ); |
| 257 |
_c++; |
| 258 |
return true; |
| 259 |
} |
| 260 |
} |
| 261 |
} |
| 262 |
} |
| 263 |
return false; |
| 264 |
} |
| 265 |
protected: |
| 266 |
int _r , _c , _dir; |
| 267 |
unsigned int _resolution[DEFAULT_DIMENSION]; |
| 268 |
ImageReader *_pixels , *_labels; |
| 269 |
RGBPixel *_pixelRows[3] , *_labelRows[3]; |
| 270 |
unsigned char *__pixelRow , *__labelRow; |
| 271 |
int* _maskRows[3]; |
| 272 |
}; |
| 273 |
|
| 274 |
template< typename Real , unsigned int Degree > |
| 275 |
void _Execute( void ) |
| 276 |
{ |
| 277 |
ThreadPool::ParallelizationType= (ThreadPool::ParallelType)ParallelType.value; |
| 278 |
int w , h; |
| 279 |
{ |
| 280 |
unsigned int _w , _h , _c; |
| 281 |
ImageReader::GetInfo( In.values[0] , _w , _h , _c ); |
| 282 |
w = _w , h = _h; |
| 283 |
ImageReader::GetInfo( In.values[1] , _w , _h , _c ); |
| 284 |
if( w!=_w || h!=_h ) MK_THROW( "Pixel and label dimensions don't match: " , _w , " x " , _h , " != " , w , " x " , h ); |
| 285 |
} |
| 286 |
if( Verbose.set ) printf( "Resolution: %d x %d\n" , w , h ); |
| 287 |
|
| 288 |
static const unsigned int Dim = DEFAULT_DIMENSION; |
| 289 |
static const unsigned int Colors = 3; |
| 290 |
static const unsigned int FEMSig = FEMDegreeAndBType< Degree , BOUNDARY_NEUMANN >::Signature; |
| 291 |
typedef typename FEMTree< Dim , Real >::FEMTreeNode FEMTreeNode; |
| 292 |
|
| 293 |
FEMTree< Dim , Real > tree( MEMORY_ALLOCATOR_BLOCK_SIZE ); |
| 294 |
std::vector< NodeSample< Dim , Point< Real , Colors > > > derivatives[Dim]; |
| 295 |
int maxDepth; |
| 296 |
DenseNodeData< Point< Real , Colors > , IsotropicUIntPack< Dim , FEMSig > > constraints; |
| 297 |
DenseNodeData< Point< Real , Colors > , IsotropicUIntPack< Dim , FEMSig > > solution; |
| 298 |
{ |
| 299 |
Profiler p; |
| 300 |
ImageReader* pixels = ImageReader::Get( In.values[0] ); |
| 301 |
ImageReader* labels = ImageReader::Get( In.values[1] ); |
| 302 |
unsigned int resolution[] = { (unsigned int )w , (unsigned int )h }; |
| 303 |
BufferedImageDerivativeStream< Real , Colors > dStream( resolution , pixels , labels ); |
| 304 |
for( int j=0 ; j<h ; j++ ) |
| 305 |
{ |
| 306 |
Point< Real , Colors > zeroData; |
| 307 |
ThreadPool::ParallelSections |
| 308 |
( |
| 309 |
[&]( void ){ dStream.prefetch(); } , |
| 310 |
[&]( void ){ maxDepth = FEMTreeInitializer< Dim , Real >::template Initialize< (Degree&1)==0 , Point< Real , Colors > >( tree.spaceRoot() , dStream , zeroData , derivatives , tree.nodeAllocators.size() ? tree.nodeAllocators[0] : NULL , tree.initializer() ); } |
| 311 |
); |
| 312 |
dStream.advance(); |
| 313 |
} |
| 314 |
delete pixels; |
| 315 |
delete labels; |
| 316 |
{ |
| 317 |
std::vector< typename FEMTree< Dim , Real >::FEMTreeNode* > nodes; |
| 318 |
nodes.reserve( derivatives[0].size() + derivatives[1].size() ); |
| 319 |
for( int i=0 ; i<derivatives[0].size() ; i++ ) nodes.push_back( derivatives[0][i].node ); |
| 320 |
for( int i=0 ; i<derivatives[1].size() ; i++ ) nodes.push_back( derivatives[1][i].node ); |
| 321 |
tree.template processNeighbors< 1 , 0 , true >( &nodes[0] , (int)nodes.size() , std::make_tuple() ); |
| 322 |
} |
| 323 |
|
| 324 |
auto addNodeFunctor = [&]( int d , const int off[Dim] ){ return d<=FullDepth.value; }; |
| 325 |
tree.template finalizeForMultigrid< Degree , Degree >( BaseDepth.value , addNodeFunctor , []( const FEMTreeNode * ){ return true; } , std::make_tuple() ); |
| 326 |
|
| 327 |
if( Verbose.set ) |
| 328 |
{ |
| 329 |
printf( "Valid FEM Nodes / Edges: %llu %llu\n" , (unsigned long long)tree.validFEMNodes( IsotropicUIntPack< Dim , FEMSig >() ) , (unsigned long long)( derivatives[0].size() + derivatives[1].size() ) ); |
| 330 |
std::string str = p(); |
| 331 |
printf( "Set tree [%d]: %s\n" , maxDepth , str.c_str() ); |
| 332 |
} |
| 333 |
} |
| 334 |
|
| 335 |
{ |
| 336 |
Profiler p; |
| 337 |
constraints = tree.template initDenseNodeData< Point< Real , Colors > >( IsotropicUIntPack< Dim , FEMSig >() ); |
| 338 |
static const unsigned int DFEMSig = FEMSignature< FEMSig >::DSignature(); |
| 339 |
// Generate the partial-x constraints |
| 340 |
{ |
| 341 |
typedef UIntPack< DFEMSig , FEMSig > CSignature; |
| 342 |
typedef IsotropicUIntPack< 2 , 0 > CDerivative; |
| 343 |
typedef UIntPack< FEMSig , FEMSig > FEMSignature; |
| 344 |
typedef UIntPack< 1 , 0 > FEMDerivative; |
| 345 |
SparseNodeData< Point< Real , Colors > , CSignature > partialX; |
| 346 |
for( int i=0 ; i<derivatives[0].size() ; i++ ) partialX[ derivatives[0][i].node ] = -derivatives[0][i].data * (1<<maxDepth); |
| 347 |
|
| 348 |
unsigned int derivatives1[] = { 1 , 0 } , derivatives2[] = { 0 , 0 }; |
| 349 |
typename FEMIntegrator::template Constraint< FEMSignature , FEMDerivative , CSignature , CDerivative , 1 > F; |
| 350 |
F.weights[0][ TensorDerivatives< FEMDerivative >::Index( derivatives1 ) ][ TensorDerivatives< CDerivative >::Index( derivatives2 ) ] = 1; |
| 351 |
tree.addFEMConstraints( F , partialX , constraints , maxDepth ); |
| 352 |
} |
| 353 |
// Generate the partial-y constraints |
| 354 |
{ |
| 355 |
typedef UIntPack< FEMSig , DFEMSig > CSignature; |
| 356 |
typedef IsotropicUIntPack< 2 , 0 > CDerivative; |
| 357 |
typedef UIntPack< FEMSig , FEMSig > FEMSignature; |
| 358 |
typedef UIntPack< 0 , 1 > FEMDerivative; |
| 359 |
SparseNodeData< Point< Real , Colors > , CSignature > partialY; |
| 360 |
for( int i=0 ; i<derivatives[1].size() ; i++ ) partialY[ derivatives[1][i].node ] = -derivatives[1][i].data * (1<<maxDepth); |
| 361 |
|
| 362 |
unsigned int derivatives1[] = { 0 , 1 } , derivatives2[] = { 0 , 0 }; |
| 363 |
typename FEMIntegrator::template Constraint< FEMSignature , FEMDerivative , CSignature , CDerivative , 1 > F; |
| 364 |
F.weights[0][ TensorDerivatives< FEMDerivative >::Index( derivatives1 ) ][ TensorDerivatives< CDerivative >::Index( derivatives2 ) ] = 1; |
| 365 |
tree.addFEMConstraints( F , partialY , constraints , maxDepth ); |
| 366 |
} |
| 367 |
if( Verbose.set ) |
| 368 |
{ |
| 369 |
std::string str = p(); |
| 370 |
printf( "Set constraints: %s\n" , str.c_str() ); |
| 371 |
} |
| 372 |
} |
| 373 |
// Solve the system |
| 374 |
{ |
| 375 |
Profiler p; |
| 376 |
double t = Time(); |
| 377 |
solution = tree.template initDenseNodeData< Point< Real , Colors > >( IsotropicUIntPack< Dim , FEMSig >() ); |
| 378 |
typename FEMTree< Dim , Real >::SolverInfo sInfo; |
| 379 |
sInfo.cgDepth = 0 , sInfo.cascadic = false , sInfo.vCycles = 1 , sInfo.cgAccuracy = 0 , sInfo.verbose = Verbose.set , sInfo.showResidual = ShowResidual.set , sInfo.showGlobalResidual = false , sInfo.sliceBlockSize = ROW_BLOCK_SIZE; |
| 380 |
sInfo.baseVCycles = BaseVCycles.value; |
| 381 |
sInfo.iters = GSIterations.value; |
| 382 |
sInfo.useSupportWeights = true; |
| 383 |
sInfo.sorRestrictionFunction = [&] ( Real w , Real ){ return (Real)( WeightScale.value * pow( w , WeightExponent.value ) ); }; |
| 384 |
sInfo.wCycle = false; |
| 385 |
typename FEMIntegrator::template System< IsotropicUIntPack< Dim , FEMSig > , IsotropicUIntPack< Dim , 1 > > F( { 0. , 1. } ); |
| 386 |
DenseNodeData< Point< Real , Colors > , IsotropicUIntPack< Dim , FEMSig > > _constraints = tree.template initDenseNodeData< Point< Real , Colors > >( IsotropicUIntPack< Dim , FEMSig >() ); |
| 387 |
tree.solveSystem( IsotropicUIntPack< Dim , FEMSig >() , F , constraints , solution , Point< Real , Colors >::Dot , BaseDepth.value , maxDepth , sInfo ); |
| 388 |
if( Verbose.set ) |
| 389 |
{ |
| 390 |
std::string str = p(); |
| 391 |
printf( "Solved system: %s\n" , str.c_str() ); |
| 392 |
} |
| 393 |
} |
| 394 |
|
| 395 |
Point< Real , Colors > average; |
| 396 |
{ |
| 397 |
Profiler p; |
| 398 |
Real begin[] = { 0 , 0 } , end[] = { (Real)w/(1<<maxDepth) , (Real)h/(1<<maxDepth) }; |
| 399 |
average = tree.average( solution , begin , end ); |
| 400 |
if( Verbose.set ) |
| 401 |
{ |
| 402 |
std::string str = p(); |
| 403 |
printf( "Got average: %s\n" , str.c_str() ); |
| 404 |
} |
| 405 |
} |
| 406 |
// Stitch the image |
| 407 |
if( Out.set ) |
| 408 |
{ |
| 409 |
Profiler p; |
| 410 |
int begin[2] , end[2]; |
| 411 |
ImageReader* in = ImageReader::Get( In.values[0] ); |
| 412 |
ImageWriter* out = ImageWriter::Get( Out.value , w , h , 3 ); |
| 413 |
|
| 414 |
RGBPixel *inRows[2] , *outRows[2]; |
| 415 |
unsigned char* inRow = NULL; |
| 416 |
for( int i=0 ; i<2 ; i++ ) inRows[i] = new RGBPixel[w*ROW_BLOCK_SIZE] , outRows[i] = new RGBPixel[w*ROW_BLOCK_SIZE]; |
| 417 |
if( in->channels()==1 ) inRow = new unsigned char[w]; |
| 418 |
|
| 419 |
auto FetchInput = [&]( unsigned int block ) |
| 420 |
{ |
| 421 |
int rStart = block*ROW_BLOCK_SIZE; |
| 422 |
int rEnd = rStart + ROW_BLOCK_SIZE < h ? rStart + ROW_BLOCK_SIZE : h; |
| 423 |
for( int r=rStart , rr=0 ; r<rEnd ; r++ , rr++ ) |
| 424 |
{ |
| 425 |
if( in->channels()==3 ) in->nextRow( (unsigned char*)( inRows[block&1] + rr*w ) ); |
| 426 |
else |
| 427 |
{ |
| 428 |
in->nextRow( inRow ); |
| 429 |
RGBPixel *_inRow = inRows[block&1] + rr*w; |
| 430 |
ThreadPool::ParallelFor( 0 , w , [&]( unsigned int , size_t i ){ _inRow[i][0] = _inRow[i][1] = _inRow[i][2] = inRow[i]; } ); |
| 431 |
} |
| 432 |
} |
| 433 |
}; |
| 434 |
auto SetOutput = [&]( unsigned int block ) |
| 435 |
{ |
| 436 |
int rStart = block*ROW_BLOCK_SIZE; |
| 437 |
int rEnd = rStart + ROW_BLOCK_SIZE < h ? rStart + ROW_BLOCK_SIZE : h; |
| 438 |
out->nextRows( (unsigned char*)outRows[block&1] , rEnd-rStart ); |
| 439 |
}; |
| 440 |
int blockNum = ( h + ROW_BLOCK_SIZE - 1 ) / ROW_BLOCK_SIZE; |
| 441 |
|
| 442 |
// Prefetch the first block |
| 443 |
FetchInput( 0 ); |
| 444 |
for( int rStart=0 , block=0 ; rStart<h ; rStart+=ROW_BLOCK_SIZE , block++ ) |
| 445 |
{ |
| 446 |
ThreadPool::ParallelSections |
| 447 |
( |
| 448 |
[&]( void ){ if( block<blockNum ) FetchInput( block+1 ); } , |
| 449 |
[&]( void ){ if( block>0 ) SetOutput( block-1 ); } , |
| 450 |
[&]( void ) |
| 451 |
{ |
| 452 |
RGBPixel *_inRows = inRows[block&1] , *_outRows = outRows[block&1]; |
| 453 |
int rEnd = rStart + ROW_BLOCK_SIZE < h ? rStart + ROW_BLOCK_SIZE : h; |
| 454 |
|
| 455 |
// Expand the next block of values |
| 456 |
begin[0] = 0 , begin[1] = rStart , end[0] = w , end[1] = rEnd; |
| 457 |
Pointer( Point< Real , Colors > ) outBlock = tree.template regularGridUpSample< true >( solution , begin , end ); |
| 458 |
int size = (rEnd-rStart)*w; |
| 459 |
ThreadPool::ParallelFor( 0 , size , [&]( unsigned int , size_t ii ) |
| 460 |
{ |
| 461 |
Point< Real , Colors > c = Point< Real , Colors >( _inRows[ii][0] , _inRows[ii][1] , _inRows[ii][2] ) / 255; |
| 462 |
c += outBlock[ii] - average; |
| 463 |
_outRows[ii] = RGBPixel( c[0] , c[1] , c[2] ); |
| 464 |
} |
| 465 |
); |
| 466 |
DeletePointer( outBlock ); |
| 467 |
} |
| 468 |
); |
| 469 |
} |
| 470 |
// Write out the last block |
| 471 |
SetOutput( blockNum-1 ); |
| 472 |
if( Verbose.set ) |
| 473 |
{ |
| 474 |
std::string str = p(); |
| 475 |
printf( "Wrote output: %s\n" , str.c_str() ); |
| 476 |
} |
| 477 |
delete[] inRows[0]; |
| 478 |
delete[] outRows[0]; |
| 479 |
delete[] inRows[1]; |
| 480 |
delete[] outRows[1]; |
| 481 |
if( inRow ) delete[] inRow; |
| 482 |
delete in; |
| 483 |
delete out; |
| 484 |
} |
| 485 |
} |
| 486 |
|
| 487 |
#ifdef FAST_COMPILE |
| 488 |
#else // !FAST_COMPILE |
| 489 |
template< typename Real > |
| 490 |
void _Execute( void ) |
| 491 |
{ |
| 492 |
switch( Degree.value ) |
| 493 |
{ |
| 494 |
case 1: _Execute< Real , 1 >() ; break; |
| 495 |
case 2: _Execute< Real , 2 >() ; break; |
| 496 |
// case 3: _Execute< Real , 3 >() ; break; |
| 497 |
// case 4: _Execute< Real , 4 >() ; break; |
| 498 |
default: MK_THROW( "Only B-Splines of degree 1 - 2 are supported" ); |
| 499 |
} |
| 500 |
} |
| 501 |
#endif // FAST_COMPILE |
| 502 |
|
| 503 |
int main( int argc , char* argv[] ) |
| 504 |
{ |
| 505 |
Timer timer; |
| 506 |
#ifdef ARRAY_DEBUG |
| 507 |
MK_WARN( "Array debugging enabled" ); |
| 508 |
#endif // ARRAY_DEBUG |
| 509 |
CmdLineParse( argc-1 , &argv[1] , params ); |
| 510 |
if( MaxMemoryGB.value>0 ) SetPeakMemoryMB( MaxMemoryGB.value<<10 ); |
| 511 |
ThreadPool::ChunkSize = ThreadChunkSize.value; |
| 512 |
ThreadPool::Schedule = (ThreadPool::ScheduleType)ScheduleType.value; |
| 513 |
if( Verbose.set ) |
| 514 |
{ |
| 515 |
printf( "*********************************************\n" ); |
| 516 |
printf( "*********************************************\n" ); |
| 517 |
printf( "** Running Image Stitching (Version %s) **\n" , ADAPTIVE_SOLVERS_VERSION ); |
| 518 |
printf( "*********************************************\n" ); |
| 519 |
printf( "*********************************************\n" ); |
| 520 |
} |
| 521 |
|
| 522 |
if( !In.set ) |
| 523 |
{ |
| 524 |
ShowUsage( argv[0] ); |
| 525 |
return EXIT_FAILURE; |
| 526 |
} |
| 527 |
if( !BaseDepth.set ) BaseDepth.value = FullDepth.value; |
| 528 |
if( BaseDepth.value>FullDepth.value ) |
| 529 |
{ |
| 530 |
if( BaseDepth.set ) MK_WARN( "Base depth must be smaller than full depth: " , BaseDepth.value , " <= " , FullDepth.value ); |
| 531 |
BaseDepth.value = FullDepth.value; |
| 532 |
} |
| 533 |
|
| 534 |
#ifdef USE_DOUBLE |
| 535 |
typedef double Real; |
| 536 |
#else // !USE_DOUBLE |
| 537 |
typedef float Real; |
| 538 |
#endif // USE_DOUBLE |
| 539 |
|
| 540 |
#ifdef FAST_COMPILE |
| 541 |
static const int Degree = DEFAULT_FEM_DEGREE; |
| 542 |
MK_WARN( "Compiled for degree-" , Degree , ", " , sizeof(Real)==4 ? "single" : "double" , "-precision _only_" ); |
| 543 |
_Execute< Real , Degree >(); |
| 544 |
#else // !FAST_COMPILE |
| 545 |
_Execute< Real >(); |
| 546 |
#endif // FAST_COMPILE |
| 547 |
|
| 548 |
if( Performance.set ) |
| 549 |
{ |
| 550 |
printf( "Time (Wall/CPU): %.2f / %.2f\n" , timer.wallTime() , timer.cpuTime() ); |
| 551 |
printf( "Peak Memory (MB): %d\n" , MemoryInfo::PeakMemoryUsageMB() ); |
| 552 |
} |
| 553 |
return EXIT_SUCCESS; |
| 554 |
} |