160{
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;
166
172 DigitalObject digitalObject( image, threshold );
175
177 trace.
beginBlock(
"Construct the Khalimsky space from the image domain." );
179 bool space_ok = ks.
init( image.domain().lowerBound(), image.domain().upperBound(),
true );
180 if (!space_ok)
181 {
182 trace.
error() <<
"Error in the Khamisky space construction."<<endl;
183 return 2;
184 }
187
190 MySurfelAdjacency surfAdj( false );
192
200 MyContainer container( ks, digitalObject, surfAdj, start_surfel );
202 trace.
info() <<
"Digital surface has " << digSurf.size() <<
" surfels."
203 << endl;
206
208
209 trace.
beginBlock(
"Decomposition first pass. Computes all planes so as to sort vertices by the plane size." );
212 map<Surfel,unsigned int> v2size;
213 for (
ConstIterator it = digSurf.begin(), itE= digSurf.end(); it != itE; ++it )
214 v2size[ *it ] = 0;
215 int j = 0;
216 int nb = digSurf.size();
218 vector<Point> layer;
219 vector<Surfel> layer_surfel;
220 for (
ConstIterator it = digSurf.begin(), itE= digSurf.end(); it != itE; ++it )
221 {
224 planeComputer.
init( widthNum, widthDen );
225
227 layer.clear();
228 layer_surfel.clear();
230 while ( ! visitor.finished() )
231 {
233 v = node.first;
236 if ( node.second != currentSize )
237 {
238 bool isExtended = planeComputer.
extend( layer.begin(), layer.end() );
239 if ( isExtended )
240 {
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 )
243 {
244 ++v2size[ *it_layer ];
245 }
246 layer_surfel.clear();
247 layer.clear();
248 currentSize = node.second;
249 }
250 else
251 break;
252 }
253 layer_surfel.push_back( v );
254 layer.push_back( p );
255 visitor.expand();
256 }
257 }
258
259 typedef PairSorted2nd<Surfel,int> SurfelWeight;
260 priority_queue<SurfelWeight> Q;
261 for (
ConstIterator it = digSurf.begin(), itE= digSurf.end(); it != itE; ++it )
262 Q.push( SurfelWeight( *it, v2size[ *it ] ) );
265
267
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;
273 j = 0;
274 while ( ! Q.empty() )
275 {
278 Q.pop();
279 if ( processedVertices.find( v ) != processedVertices.end() )
280 continue;
281
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 );
287
289 layer.clear();
290 layer_surfel.clear();
292 while ( ! visitor.finished() )
293 {
295 v = node.first;
298 if ( node.second != currentSize )
299 {
300 bool isExtended = ptrRoundPlane->first.extend( layer.begin(), layer.end() );
301 if ( isExtended )
302 {
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 )
305 {
307 processedVertices.insert( s );
308 if ( v2plane.find( s ) == v2plane.end() )
309 v2plane[ s ] = ptrRoundPlane;
310 }
311 layer.clear();
312 layer_surfel.clear();
313 currentSize = node.second;
314 }
315 else break;
316 }
317 layer_surfel.push_back( v );
318 layer.push_back( p );
319 if ( processedVertices.find( v ) != processedVertices.end() )
320
321 visitor.ignore();
322 else
323 visitor.expand();
324 }
325 if ( visitor.finished() )
326 {
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 )
329 {
331 processedVertices.insert( s );
332 if ( v2plane.find( s ) == v2plane.end() )
333 v2plane[ s ] = ptrRoundPlane;
334 }
335 }
336
337 ptrRoundPlane->second =
Color( rand() % 192 + 64, rand() % 192 + 64, rand() % 192 + 64, 255 );
338 }
341
343 for ( vector<RoundPlane*>::iterator
344 it = roundPlanes.begin(), itE = roundPlanes.end();
345 it != itE; ++it )
346 {
348 RealVector normal;
349 double mu =
LSF( normal, computer.
begin(), computer.
end() );
350 (*it)->third = make_pair( normal, mu );
351 }
353
355 map<Surfel, RealPoint> coordinates;
356 for ( map<Surfel,RoundPlane*>::const_iterator
357 it = v2plane.begin(), itE = v2plane.end();
358 it != itE; ++it )
359 {
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;
368 }
369 typedef vector<Surfel> SurfelRange;
370 map<Surfel, RealPoint> new_coordinates;
371 for (
ConstIterator it = digSurf.begin(), itE= digSurf.end(); it != itE; ++it )
372 {
374 SurfelRange neighbors;
375 back_insert_iterator<SurfelRange> writeIt = back_inserter( neighbors );
376 digSurf.writeNeighbors( writeIt, *it );
378 for ( SurfelRange::const_iterator its = neighbors.begin(), itsE = neighbors.end();
379 its != itsE; ++its )
380 x += coordinates[ *its ];
381 new_coordinates[ s ] = x / neighbors.size();
382 }
384
386 typedef unsigned int Number;
388 typedef MyMesh::MeshFace MeshFace;
391 map<Surfel, Number> index;
392 Number nbv = 0;
393 MyMesh polyhedron( true );
394
395 for (
ConstIterator it = digSurf.begin(), itE= digSurf.end(); it != itE; ++it )
396 {
397 polyhedron.addVertex( new_coordinates[ *it ] );
398 index[ *it ] = nbv++;
399 }
400
401 FaceSet faces = digSurf.allClosedFaces();
402 for ( typename FaceSet::const_iterator itf = faces.begin(), itf_end = faces.end();
403 itf != itf_end; ++itf )
404 {
405 MeshFace mface( itf->nbVertices );
406 VertexRange vtcs = digSurf.verticesAroundFace( *itf );
407 int i = 0;
408 for ( typename VertexRange::const_iterator itv = vtcs.begin(), itv_end = vtcs.end();
409 itv != itv_end; ++itv )
410 {
411 mface[ i++ ] = index[ *itv ];
412 }
413 polyhedron.addFace( mface,
Color( 255, 243, 150, 255 ) );
414 }
416
419 MyViewer3D viewer( ks );
420 viewer.show();
421 bool isOK = polyhedron >> "test.off";
422 bool isOK2 = polyhedron >> "test.obj";
423 viewer << polyhedron;
424 viewer << MyViewer3D::updateDisplay;
425 application.exec();
427
429 for ( vector<RoundPlane*>::iterator
430 it = roundPlanes.begin(), itE = roundPlanes.end();
431 it != itE; ++it )
432 delete *it;
434
435 if (isOK && isOK2)
436 return 0;
437 else
438 return 1;
439}
Aim: This class is useful to perform a breadth-first exploration of a graph given a starting point or...
std::pair< Vertex, Data > Node
FIXME.
void init(Dimension axis, InternalInteger diameter, InternalInteger widthNumerator=NumberTraits< InternalInteger >::ONE, InternalInteger widthDenominator=NumberTraits< InternalInteger >::ONE)
ConstIterator end() const
ConstIterator begin() const
bool extend(const Point &p)
Aim: A class that recognizes pieces of digital planes of given axis width. When the width is 1,...
Structure representing an RGB triple with alpha component.
Aim: Represents a set of n-1-cells in a nD space, together with adjacency relation between these cell...
std::vector< Vertex > VertexRange
The range of vertices is defined as a vector.
std::set< Face > FaceSet
The set of faces is defined as set.
DigitalSurfaceContainer::SurfelConstIterator ConstIterator
Aim: implements association bewteen points lying in a digital domain and values.
Aim: A model of CDigitalSurfaceContainer which defines the digital surface as the boundary of an impl...
Aim: This class is a model of CCellularGridSpaceND. It represents the cubical grid as a cell complex,...
bool init(const Point &lower, const Point &upper, bool isClosed)
Specifies the upper and lower bounds for the maximal cells in this space.
Dimension sOrthDir(const SCell &s) const
Given a signed surfel [s], returns its orthogonal direction (ie, the coordinate where the surfel is c...
Point sCoords(const SCell &c) const
Return its digital coordinates.
const Point & sKCoords(const SCell &c) const
Return its Khalimsky coordinates.
SCell sDirectIncident(const SCell &p, Dimension k) const
Return the direct incident cell of [p] along [k] (the incident cell along [k])
Aim: This class is defined to represent a surface mesh through a set of vertices and faces....
static SCell findABel(const KSpace &K, const PointPredicate &pp, unsigned int nbtries=1000)
Aim: Represent adjacencies between surfel elements, telling if it follows an interior to exterior ord...
void beginBlock(const std::string &keyword="")
void progressBar(const double currentValue, const double maximalValue)
Aim: Define a simple Foreground predicate thresholding image values given a single thresold....
DigitalSurface< MyDigitalSurfaceContainer > MyDigitalSurface
COBANaivePlaneComputer< Z3, InternalInteger > NaivePlaneComputer
BreadthFirstVisitor< MyDigitalSurface > Visitor
double LSF(RealVector &N, ConstIterator itB, ConstIterator itE)
Represents a signed cell in a cellular grid space by its Khalimsky coordinates and a boolean value.
static ImageContainer importVol(const std::string &filename, const Functor &aFunctor=Functor())
ImageContainerBySTLVector< Domain, Value > Image
TriMesh::VertexRange VertexRange