159int main(
int argc,
char** argv )
161 QApplication application(argc,argv);
162 string inputFilename = argc > 1 ? argv[ 1 ] : examplesPath+
"/samples/Al.100.vol";
163 int threshold = argc > 2 ? atoi( argv[ 2 ] ) : 0;
164 int widthNum = argc > 3 ? atoi( argv[ 3 ] ) : 2;
165 int widthDen = argc > 4 ? atoi( argv[ 4 ] ) : 1;
172 DigitalObject digitalObject( image, threshold );
177 trace.
beginBlock(
"Construct the Khalimsky space from the image domain." );
179 bool space_ok = ks.
init( image.domain().lowerBound(), image.domain().upperBound(),
true );
182 trace.
error() <<
"Error in the Khamisky space construction."<<endl;
190 MySurfelAdjacency surfAdj(
false );
200 MyContainer container( ks, digitalObject, surfAdj, start_surfel );
202 trace.
info() <<
"Digital surface has " << digSurf.
size() <<
" surfels."
209 trace.
beginBlock(
"Decomposition first pass. Computes all planes so as to sort vertices by the plane size." );
212 map<Surfel,unsigned int> v2size;
216 int nb = digSurf.
size();
219 vector<Surfel> layer_surfel;
224 planeComputer.
init( widthNum, widthDen );
228 layer_surfel.clear();
236 if ( node.second != currentSize )
238 bool isExtended = planeComputer.
extend( layer.begin(), layer.end() );
241 for ( vector<Surfel>::const_iterator it_layer = layer_surfel.begin(),
242 it_layer_end = layer_surfel.end(); it_layer != it_layer_end; ++it_layer )
244 ++v2size[ *it_layer ];
246 layer_surfel.clear();
248 currentSize = node.second;
253 layer_surfel.push_back( v );
254 layer.push_back( p );
259 typedef PairSorted2nd<Surfel,int> SurfelWeight;
260 priority_queue<SurfelWeight> Q;
262 Q.push( SurfelWeight( *it, v2size[ *it ] ) );
268 trace.
beginBlock(
"Decomposition second pass. Visits vertices from the one with biggest plane to the one with smallest plane." );
269 typedef Triple<NaivePlaneComputer, Color, pair<RealVector,double> > RoundPlane;
270 set<Surfel> processedVertices;
271 vector<RoundPlane*> roundPlanes;
272 map<Surfel,RoundPlane*> v2plane;
274 while ( ! Q.empty() )
279 if ( processedVertices.find( v ) != processedVertices.end() )
282 RoundPlane* ptrRoundPlane =
new RoundPlane;
283 roundPlanes.push_back( ptrRoundPlane );
284 v2plane[ v ] = ptrRoundPlane;
285 ptrRoundPlane->first.init( widthNum, widthDen );
286 ptrRoundPlane->third = make_pair( RealVector::zero, 0.0 );
290 layer_surfel.clear();
298 if ( node.second != currentSize )
300 bool isExtended = ptrRoundPlane->first.extend( layer.begin(), layer.end() );
303 for ( vector<Surfel>::const_iterator it_layer = layer_surfel.begin(),
304 it_layer_end = layer_surfel.end(); it_layer != it_layer_end; ++it_layer )
307 processedVertices.insert( s );
308 if ( v2plane.find( s ) == v2plane.end() )
309 v2plane[ s ] = ptrRoundPlane;
312 layer_surfel.clear();
313 currentSize = node.second;
317 layer_surfel.push_back( v );
318 layer.push_back( p );
319 if ( processedVertices.find( v ) != processedVertices.end() )
327 for ( vector<Surfel>::const_iterator it_layer = layer_surfel.begin(),
328 it_layer_end = layer_surfel.end(); it_layer != it_layer_end; ++it_layer )
331 processedVertices.insert( s );
332 if ( v2plane.find( s ) == v2plane.end() )
333 v2plane[ s ] = ptrRoundPlane;
337 ptrRoundPlane->second =
Color( rand() % 192 + 64, rand() % 192 + 64, rand() % 192 + 64, 255 );
343 for ( vector<RoundPlane*>::iterator
344 it = roundPlanes.begin(), itE = roundPlanes.end();
349 double mu =
LSF( normal, computer.
begin(), computer.
end() );
350 (*it)->third = make_pair( normal, mu );
355 map<Surfel, RealPoint> coordinates;
356 for ( map<Surfel,RoundPlane*>::const_iterator
357 it = v2plane.begin(), itE = v2plane.end();
361 RoundPlane* rplane = it->second;
363 RealPoint rp( (
double)p[ 0 ]/2.0, (
double)p[ 1 ]/2.0, (
double)p[ 2 ]/2.0 );
364 double mu = rplane->third.second;
365 RealVector normal = rplane->third.first;
366 double lambda = mu - rp.
dot( normal );
367 coordinates[ v ] = rp + lambda*normal;
369 typedef vector<Surfel> SurfelRange;
370 map<Surfel, RealPoint> new_coordinates;
374 SurfelRange neighbors;
375 back_insert_iterator<SurfelRange> writeIt = back_inserter( neighbors );
378 for ( SurfelRange::const_iterator its = neighbors.begin(), itsE = neighbors.end();
380 x += coordinates[ *its ];
381 new_coordinates[ s ] = x / neighbors.size();
386 typedef unsigned int Number;
388 typedef MyMesh::MeshFace MeshFace;
391 map<Surfel, Number> index;
393 MyMesh polyhedron(
true );
397 polyhedron.addVertex( new_coordinates[ *it ] );
398 index[ *it ] = nbv++;
402 for (
typename FaceSet::const_iterator itf = faces.begin(), itf_end = faces.end();
403 itf != itf_end; ++itf )
405 MeshFace mface( itf->nbVertices );
408 for (
typename VertexRange::const_iterator itv = vtcs.begin(), itv_end = vtcs.end();
409 itv != itv_end; ++itv )
411 mface[ i++ ] = index[ *itv ];
413 polyhedron.addFace( mface,
Color( 255, 243, 150, 255 ) );
419 MyViewer3D viewer( ks );
421 bool isOK = polyhedron >>
"test.off";
422 bool isOK2 = polyhedron >>
"test.obj";
423 viewer << polyhedron;
424 viewer << MyViewer3D::updateDisplay;
429 for ( vector<RoundPlane*>::iterator
430 it = roundPlanes.begin(), itE = roundPlanes.end();