61#include "DGtal/base/Common.h"
62#include "DGtal/helpers/StdDefs.h"
63#include "ConfigExamples.h"
67#include "DGtal/io/readers/VolReader.h"
68#include "DGtal/images/ImageSelector.h"
69#include "DGtal/images/ImageContainerBySTLVector.h"
70#include "DGtal/images/SimpleThresholdForegroundPredicate.h"
73#include "DGtal/io/Display3D.h"
74#include "DGtal/io/viewers/Viewer3D.h"
75#include "DGtal/io/DrawWithDisplay3DModifier.h"
76#include "DGtal/images/imagesSetsUtils/SetFromImage.h"
78#include "DGtal/topology/DigitalSurface.h"
79#include "DGtal/topology/helpers/Surfaces.h"
80#include "DGtal/topology/ImplicitDigitalSurface.h"
82#include "DGtal/graph/BreadthFirstVisitor.h"
83#include "DGtal/geometry/surfaces/COBANaivePlaneComputer.h"
84#include "DGtal/geometry/surfaces/ChordNaivePlaneComputer.h"
85#include "DGtal/geometry/surfaces/ChordGenericNaivePlaneComputer.h"
87#include "DGtal/math/linalg/SimpleMatrix.h"
88#include "DGtal/math/linalg/EigenDecomposition.h"
98template <
typename T1,
typename T2>
101 typedef PairSorted2nd<T1,T2> Self;
102 inline PairSorted2nd(
const T1& t1,
const T2& t2 ) : first( t1 ), second( t2 ) {}
103 bool operator<(
const Self& other )
const
105 return second < other.second;
111template <
typename T1,
typename T2,
typename T3>
117 Triple( T1 t1 = T1(), T2 t2 = T2(), T3 t3 = T3() )
118 : first( t1 ), second( t2 ), third( t3 )
134 G +=
RealVector( (*it)[ 0 ], (*it)[ 1 ], (*it)[ 2 ] );
140 RealVector p( (*it)[ 0 ], (*it)[ 1 ], (*it)[ 2 ] );
144 A.setComponent( i, j, A( i, j ) + p[ i ] * p[ j ] );
155 return mu/(double)nb;
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 );
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;
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();
Aim: This class is useful to perform a breadth-first exploration of a graph given a starting point or...
const Node & current() const
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...
FaceSet allClosedFaces() const
DigitalSurfaceContainer::SurfelConstIterator ConstIterator
std::vector< Vertex > VertexRange
The range of vertices is defined as a vector.
ConstIterator begin() const
ConstIterator end() const
VertexRange verticesAroundFace(const Face &f) const
std::set< Face > FaceSet
The set of faces is defined as set.
void writeNeighbors(OutputIterator &it, const Vertex &v) const
static void getEigenDecomposition(const Matrix &matrix, Matrix &eigenVectors, Vector &eigenValues)
Compute both eigen vectors and eigen values from an input matrix.
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....
Aim: Implements basic operations that will be used in Point and Vector classes.
auto dot(const PointVector< dim, OtherComponent, OtherStorage > &v) const -> decltype(DGtal::dotProduct(*this, v))
Dot product with a PointVector.
TEuclideanRing Component
Type for Vector elements.
static Self zero
Static const for zero PointVector.
Aim: implements basic MxN Matrix services (M,N>=1).
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
MyDigitalSurface::ConstIterator ConstIterator
BreadthFirstVisitor< MyDigitalSurface > Visitor
Space::RealVector RealVector
DGtal is the top-level namespace which contains all DGtal functions and types.
bool operator<(PointVector< ptDim, LeftEuclideanRing, LeftContainer > const &lhs, PointVector< ptDim, RightEuclideanRing, RightContainer > const &rhs)
Comparison operator on Points/Vectors (LesserThan).
DGtal::uint32_t Dimension
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())
TriMesh::VertexRange VertexRange