157int main(
int argc,
char** argv )
159 string inputFilename = argc > 1 ? argv[ 1 ] : examplesPath+
"/samples/Al.100.vol";
160 int threshold = argc > 2 ? atoi( argv[ 2 ] ) : 0;
161 int widthNum = argc > 3 ? atoi( argv[ 3 ] ) : 2;
162 int widthDen = argc > 4 ? atoi( argv[ 4 ] ) : 1;
165 trace.beginBlock(
"Reading vol file into an image." );
169 DigitalObject digitalObject(
image, threshold );
174 trace.beginBlock(
"Construct the Khalimsky space from the image domain." );
176 bool space_ok = ks.
init(
image.domain().lowerBound(),
image.domain().upperBound(),
true );
179 trace.error() <<
"Error in the Khamisky space construction."<<endl;
187 MySurfelAdjacency surfAdj(
false );
191 trace.beginBlock(
"Extracting boundary by tracking the surface. " );
197 MyContainer container( ks, digitalObject, surfAdj, start_surfel );
199 trace.info() <<
"Digital surface has " << digSurf.
size() <<
" surfels."
206 trace.beginBlock(
"Decomposition first pass. Computes all planes so as to sort vertices by the plane size." );
209 map<Surfel,unsigned int> v2size;
213 int nb = digSurf.
size();
216 vector<Surfel> layer_surfel;
219 if ( ( (++j) % 50 == 0 ) || ( j == nb ) )
trace.progressBar( j, nb );
221 planeComputer.
init( widthNum, widthDen );
225 layer_surfel.clear();
233 if ( node.second != currentSize )
235 bool isExtended = planeComputer.
extend( layer.begin(), layer.end() );
238 for ( vector<Surfel>::const_iterator it_layer = layer_surfel.begin(),
239 it_layer_end = layer_surfel.end(); it_layer != it_layer_end; ++it_layer )
241 ++v2size[ *it_layer ];
243 layer_surfel.clear();
245 currentSize = node.second;
250 layer_surfel.push_back( v );
251 layer.push_back( p );
257 priority_queue<SurfelWeight> Q;
259 Q.push( SurfelWeight( *it, v2size[ *it ] ) );
265 trace.beginBlock(
"Decomposition second pass. Visits vertices from the one with biggest plane to the one with smallest plane." );
267 set<Surfel> processedVertices;
268 vector<RoundPlane*> roundPlanes;
269 map<Surfel,RoundPlane*> v2plane;
271 while ( ! Q.empty() )
273 if ( ( (++j) % 50 == 0 ) || ( j == nb ) )
trace.progressBar( j, nb );
276 if ( processedVertices.find( v ) != processedVertices.end() )
279 RoundPlane* ptrRoundPlane =
new RoundPlane;
280 roundPlanes.push_back( ptrRoundPlane );
281 v2plane[ v ] = ptrRoundPlane;
282 ptrRoundPlane->first.init( widthNum, widthDen );
287 layer_surfel.clear();
295 if ( node.second != currentSize )
297 bool isExtended = ptrRoundPlane->first.extend( layer.begin(), layer.end() );
300 for ( vector<Surfel>::const_iterator it_layer = layer_surfel.begin(),
301 it_layer_end = layer_surfel.end(); it_layer != it_layer_end; ++it_layer )
304 processedVertices.insert( s );
305 if ( v2plane.find( s ) == v2plane.end() )
306 v2plane[ s ] = ptrRoundPlane;
309 layer_surfel.clear();
310 currentSize = node.second;
314 layer_surfel.push_back( v );
315 layer.push_back( p );
316 if ( processedVertices.find( v ) != processedVertices.end() )
324 for ( vector<Surfel>::const_iterator it_layer = layer_surfel.begin(),
325 it_layer_end = layer_surfel.end(); it_layer != it_layer_end; ++it_layer )
328 processedVertices.insert( s );
329 if ( v2plane.find( s ) == v2plane.end() )
330 v2plane[ s ] = ptrRoundPlane;
334 ptrRoundPlane->second =
Color( rand() % 192 + 64, rand() % 192 + 64, rand() % 192 + 64, 255 );
340 for ( vector<RoundPlane*>::iterator
341 it = roundPlanes.begin(), itE = roundPlanes.end();
346 double mu =
LSF( normal, computer.
begin(), computer.
end() );
347 (*it)->third = make_pair( normal, mu );
352 map<Surfel, RealPoint> coordinates;
353 for ( map<Surfel,RoundPlane*>::const_iterator
354 it = v2plane.begin(), itE = v2plane.end();
358 RoundPlane* rplane = it->second;
360 RealPoint rp( (
double)p[ 0 ]/2.0, (
double)p[ 1 ]/2.0, (
double)p[ 2 ]/2.0 );
361 double mu = rplane->third.second;
363 double lambda = mu - rp.
dot( normal );
364 coordinates[ v ] = rp + lambda*normal;
366 typedef vector<Surfel> SurfelRange;
367 map<Surfel, RealPoint> new_coordinates;
371 SurfelRange neighbors;
372 back_insert_iterator<SurfelRange> writeIt = back_inserter( neighbors );
375 for ( SurfelRange::const_iterator its = neighbors.begin(), itsE = neighbors.end();
377 x += coordinates[ *its ];
378 new_coordinates[ s ] = x / neighbors.
size();
383 typedef unsigned int Number;
385 typedef MyMesh::MeshFace MeshFace;
388 map<Surfel, Number>
index;
390 MyMesh polyhedron(
true );
394 polyhedron.addVertex( new_coordinates[ *it ] );
395 index[ *it ] = nbv++;
399 for (
typename FaceSet::const_iterator itf = faces.begin(), itf_end = faces.end();
400 itf != itf_end; ++itf )
402 MeshFace mface( itf->nbVertices );
405 for (
typename VertexRange::const_iterator itv = vtcs.begin(), itv_end = vtcs.end();
406 itv != itv_end; ++itv )
408 mface[ i++ ] =
index[ *itv ];
410 polyhedron.addFace( mface,
Color( 255, 243, 150, 255 ) );
416 MyViewer3D viewer( ks );
419 viewer << polyhedron;
424 for ( vector<RoundPlane*>::iterator
425 it = roundPlanes.begin(), itE = roundPlanes.end();