Loading [MathJax]/extensions/TeX/AMSsymbols.js
DGtal 2.0.0
polyhedralizer.cpp
Go to the documentation of this file.
1
16
29
44
52
55#include <iostream>
56#include <vector>
57#include <set>
58#include <map>
59#include <queue>
60
61#include "DGtal/base/Common.h"
62#include "DGtal/helpers/StdDefs.h"
63#include "ConfigExamples.h"
65
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"
72
73#include "DGtal/io/viewers/PolyscopeViewer.h"
74#include "DGtal/images/imagesSetsUtils/SetFromImage.h"
75
76#include "DGtal/topology/DigitalSurface.h"
77#include "DGtal/topology/helpers/Surfaces.h"
78#include "DGtal/topology/ImplicitDigitalSurface.h"
79
80#include "DGtal/graph/BreadthFirstVisitor.h"
81#include "DGtal/geometry/surfaces/COBANaivePlaneComputer.h"
82#include "DGtal/geometry/surfaces/ChordNaivePlaneComputer.h"
83#include "DGtal/geometry/surfaces/ChordGenericNaivePlaneComputer.h"
84
85#include "DGtal/math/linalg/SimpleMatrix.h"
86#include "DGtal/math/linalg/EigenDecomposition.h"
87
89
91using namespace std;
92using namespace DGtal;
93using namespace Z3i;
95
96template <typename T1, typename T2>
98{
100 inline PairSorted2nd( const T1& t1, const T2& t2 ) : first( t1 ), second( t2 ) {}
101 bool operator<( const Self& other ) const
102 {
103 return second < other.second;
104 }
107};
108
109template <typename T1, typename T2, typename T3>
110struct Triple
111{
115 Triple( T1 t1 = T1(), T2 t2 = T2(), T3 t3 = T3() )
116 : first( t1 ), second( t2 ), third( t3 )
117 {}
118};
119
120// Least-Square Fit of a plane N.x=mu on points [itB,itE). Returns mu.
121template <typename RealVector,
122 typename ConstIterator>
124{
125 typedef typename RealVector::Component Component;
126 typedef SimpleMatrix<Component,3,3> Matrix;
127 Matrix A; A.clear();
128 unsigned int nb = 0;
129 RealVector G = RealVector::zero; // center of gravity
130 for ( ConstIterator it = itB; it != itE; ++it )
131 {
132 G += RealVector( (*it)[ 0 ], (*it)[ 1 ], (*it)[ 2 ] );
133 ++nb;
134 }
135 G /= nb;
136 for ( ConstIterator it = itB; it != itE; ++it )
137 {
138 RealVector p( (*it)[ 0 ], (*it)[ 1 ], (*it)[ 2 ] );
139 p -= G;
140 for ( Dimension i = 0; i < 3; ++i )
141 for ( Dimension j = 0; j < 3; ++j )
142 A.setComponent( i, j, A( i, j ) + p[ i ] * p[ j ] );
143 }
144 // A is Gram matrix. We look for V such that V^t A V / |V|^2 is
145 // minimal. It is thus the first eigenvalue.
146 Matrix V;
147 RealVector values;
149 N = V.column( 0 ); // first eigenvector;
150 double mu = 0.0;
151 for ( ConstIterator it = itB; it != itE; ++it )
152 mu += N.dot( *it );
153 return mu/(double)nb;
154}
155
156
157int main( int argc, char** argv )
158{
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;
163
165 trace.beginBlock( "Reading vol file into an image." );
167 Image image = VolReader<Image>::importVol(inputFilename);
169 DigitalObject digitalObject( image, threshold );
170 trace.endBlock();
172
174 trace.beginBlock( "Construct the Khalimsky space from the image domain." );
175 KSpace ks;
176 bool space_ok = ks.init( image.domain().lowerBound(), image.domain().upperBound(), true );
177 if (!space_ok)
178 {
179 trace.error() << "Error in the Khamisky space construction."<<endl;
180 return 2;
181 }
182 trace.endBlock();
184
186 typedef SurfelAdjacency<KSpace::dimension> MySurfelAdjacency;
187 MySurfelAdjacency surfAdj( false ); // exterior in all directions.
189
191 trace.beginBlock( "Extracting boundary by tracking the surface. " );
192 typedef KSpace::Surfel Surfel;
193 Surfel start_surfel = Surfaces<KSpace>::findABel( ks, digitalObject, 100000 );
197 MyContainer container( ks, digitalObject, surfAdj, start_surfel );
198 MyDigitalSurface digSurf( container );
199 trace.info() << "Digital surface has " << digSurf.size() << " surfels."
200 << endl;
201 trace.endBlock();
203
205 // First pass to find biggest planes.
206 trace.beginBlock( "Decomposition first pass. Computes all planes so as to sort vertices by the plane size." );
209 map<Surfel,unsigned int> v2size;
210 for ( ConstIterator it = digSurf.begin(), itE= digSurf.end(); it != itE; ++it )
211 v2size[ *it ] = 0;
212 int j = 0;
213 int nb = digSurf.size();
214 NaivePlaneComputer planeComputer;
215 vector<Point> layer;
216 vector<Surfel> layer_surfel;
217 for ( ConstIterator it = digSurf.begin(), itE= digSurf.end(); it != itE; ++it )
218 {
219 if ( ( (++j) % 50 == 0 ) || ( j == nb ) ) trace.progressBar( j, nb );
220 Surfel v = *it;
221 planeComputer.init( widthNum, widthDen );
222 // The visitor takes care of all the breadth-first traversal.
223 Visitor visitor( digSurf, v );
224 layer.clear();
225 layer_surfel.clear();
226 Visitor::Size currentSize = visitor.current().second;
227 while ( ! visitor.finished() )
228 {
229 Visitor::Node node = visitor.current();
230 v = node.first;
231 int axis = ks.sOrthDir( v );
232 Point p = ks.sCoords( ks.sDirectIncident( v, axis ) );
233 if ( node.second != currentSize )
234 {
235 bool isExtended = planeComputer.extend( layer.begin(), layer.end() );
236 if ( isExtended )
237 {
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 )
240 {
241 ++v2size[ *it_layer ];
242 }
243 layer_surfel.clear();
244 layer.clear();
245 currentSize = node.second;
246 }
247 else
248 break;
249 }
250 layer_surfel.push_back( v );
251 layer.push_back( p );
252 visitor.expand();
253 }
254 }
255 // Prepare queue
256 typedef PairSorted2nd<Surfel,int> SurfelWeight;
257 priority_queue<SurfelWeight> Q;
258 for ( ConstIterator it = digSurf.begin(), itE= digSurf.end(); it != itE; ++it )
259 Q.push( SurfelWeight( *it, v2size[ *it ] ) );
260 trace.endBlock();
262
264 // Segmentation into planes
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;
270 j = 0;
271 while ( ! Q.empty() )
272 {
273 if ( ( (++j) % 50 == 0 ) || ( j == nb ) ) trace.progressBar( j, nb );
274 Surfel v = Q.top().first;
275 Q.pop();
276 if ( processedVertices.find( v ) != processedVertices.end() ) // already in set
277 continue; // process to next vertex
278
279 RoundPlane* ptrRoundPlane = new RoundPlane;
280 roundPlanes.push_back( ptrRoundPlane ); // to delete them afterwards.
281 v2plane[ v ] = ptrRoundPlane;
282 ptrRoundPlane->first.init( widthNum, widthDen );
283 ptrRoundPlane->third = make_pair( RealVector::zero, 0.0 );
284 // The visitor takes care of all the breadth-first traversal.
285 Visitor visitor( digSurf, v );
286 layer.clear();
287 layer_surfel.clear();
288 Visitor::Size currentSize = visitor.current().second;
289 while ( ! visitor.finished() )
290 {
291 Visitor::Node node = visitor.current();
292 v = node.first;
293 Dimension axis = ks.sOrthDir( v );
294 Point p = ks.sCoords( ks.sDirectIncident( v, axis ) );
295 if ( node.second != currentSize )
296 {
297 bool isExtended = ptrRoundPlane->first.extend( layer.begin(), layer.end() );
298 if ( isExtended )
299 {
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 )
302 {
303 Surfel s = *it_layer;
304 processedVertices.insert( s );
305 if ( v2plane.find( s ) == v2plane.end() )
306 v2plane[ s ] = ptrRoundPlane;
307 }
308 layer.clear();
309 layer_surfel.clear();
310 currentSize = node.second;
311 }
312 else break;
313 }
314 layer_surfel.push_back( v );
315 layer.push_back( p );
316 if ( processedVertices.find( v ) != processedVertices.end() )
317 // surfel is already in some plane.
318 visitor.ignore();
319 else
320 visitor.expand();
321 }
322 if ( visitor.finished() )
323 {
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 )
326 {
327 Surfel s = *it_layer;
328 processedVertices.insert( s );
329 if ( v2plane.find( s ) == v2plane.end() )
330 v2plane[ s ] = ptrRoundPlane;
331 }
332 }
333 // Assign random color for each plane.
334 ptrRoundPlane->second = Color( rand() % 192 + 64, rand() % 192 + 64, rand() % 192 + 64, 255 );
335 }
336 trace.endBlock();
338
340 for ( vector<RoundPlane*>::iterator
341 it = roundPlanes.begin(), itE = roundPlanes.end();
342 it != itE; ++it )
343 {
344 NaivePlaneComputer& computer = (*it)->first;
345 RealVector normal;
346 double mu = LSF( normal, computer.begin(), computer.end() );
347 (*it)->third = make_pair( normal, mu );
348 }
350
352 map<Surfel, RealPoint> coordinates;
353 for ( map<Surfel,RoundPlane*>::const_iterator
354 it = v2plane.begin(), itE = v2plane.end();
355 it != itE; ++it )
356 {
357 Surfel v = it->first;
358 RoundPlane* rplane = it->second;
359 Point p = ks.sKCoords( v );
360 RealPoint rp( (double)p[ 0 ]/2.0, (double)p[ 1 ]/2.0, (double)p[ 2 ]/2.0 );
361 double mu = rplane->third.second;
362 RealVector normal = rplane->third.first;
363 double lambda = mu - rp.dot( normal );
364 coordinates[ v ] = rp + lambda*normal;
365 }
366 typedef vector<Surfel> SurfelRange;
367 map<Surfel, RealPoint> new_coordinates;
368 for ( ConstIterator it = digSurf.begin(), itE= digSurf.end(); it != itE; ++it )
369 {
370 Surfel s = *it;
371 SurfelRange neighbors;
372 back_insert_iterator<SurfelRange> writeIt = back_inserter( neighbors );
373 digSurf.writeNeighbors( writeIt, *it );
375 for ( SurfelRange::const_iterator its = neighbors.begin(), itsE = neighbors.end();
376 its != itsE; ++its )
377 x += coordinates[ *its ];
378 new_coordinates[ s ] = x / neighbors.size();
379 }
381
383 typedef unsigned int Number;
384 typedef Mesh<RealPoint> MyMesh;
385 typedef MyMesh::MeshFace MeshFace;
386 typedef MyDigitalSurface::FaceSet FaceSet;
388 map<Surfel, Number> index; // Numbers all vertices.
389 Number nbv = 0;
390 MyMesh polyhedron( true );
391 // Insert all projected surfels as vertices of the polyhedral surface.
392 for ( ConstIterator it = digSurf.begin(), itE= digSurf.end(); it != itE; ++it )
393 {
394 polyhedron.addVertex( new_coordinates[ *it ] );
395 index[ *it ] = nbv++;
396 }
397 // Define faces of the mesh. Outputs closed faces.
398 FaceSet faces = digSurf.allClosedFaces();
399 for ( typename FaceSet::const_iterator itf = faces.begin(), itf_end = faces.end();
400 itf != itf_end; ++itf )
401 {
402 MeshFace mface( itf->nbVertices );
403 VertexRange vtcs = digSurf.verticesAroundFace( *itf );
404 int i = 0;
405 for ( typename VertexRange::const_iterator itv = vtcs.begin(), itv_end = vtcs.end();
406 itv != itv_end; ++itv )
407 {
408 mface[ i++ ] = index[ *itv ];
409 }
410 polyhedron.addFace( mface, Color( 255, 243, 150, 255 ) );
411 }
413
415 typedef PolyscopeViewer<Space,KSpace> MyViewer3D;
416 MyViewer3D viewer( ks );
417 // bool isOK = polyhedron >> "test.off";
418 // bool isOK2 = polyhedron >> "test.obj";
419 viewer << polyhedron;
420 viewer.show();
422
424 for ( vector<RoundPlane*>::iterator
425 it = roundPlanes.begin(), itE = roundPlanes.end();
426 it != itE; ++it )
427 delete *it;
429 return true;
430 }
Aim: This class is useful to perform a breadth-first exploration of a graph given a starting point or...
const Node & current() const
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.
Definition Color.h:77
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
ConstIterator begin() const
ConstIterator end() const
VertexRange verticesAroundFace(const Face &f) const
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.
Definition Image.h:70
Aim: A model of CDigitalSurfaceContainer which defines the digital surface as the boundary of an impl...
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....
Definition Mesh.h:92
auto dot(const PointVector< dim, OtherComponent, OtherStorage > &v) const -> decltype(DGtal::dotProduct(*this, v))
Dot product with a PointVector.
static Dimension size()
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...
Aim: Define a simple Foreground predicate thresholding image values given a single thresold....
DigitalSurface< MyDigitalSurfaceContainer > MyDigitalSurface
COBANaivePlaneComputer< Z3, InternalInteger > NaivePlaneComputer
BreadthFirstVisitor< MyDigitalSurface > Visitor
Z3i this namespace gathers the standard of types for 3D imagery.
KhalimskySpaceND< 3, Integer > KSpace
Definition StdDefs.h:146
Space::RealVector RealVector
Definition StdDefs.h:171
Space::RealPoint RealPoint
Definition StdDefs.h:170
DGtal is the top-level namespace which contains all DGtal functions and types.
DGtal::uint32_t Dimension
Definition Common.h:119
Trace trace
STL namespace.
double LSF(RealVector &N, ConstIterator itB, ConstIterator itE)
static ImageContainer importVol(const std::string &filename, const Functor &aFunctor=Functor())
[polyhedralizer-typedefs]
bool operator<(const Self &other) const
PairSorted2nd(const T1 &t1, const T2 &t2)
PairSorted2nd< T1, T2 > Self
Triple(T1 t1=T1(), T2 t2=T2(), T3 t3=T3())
unsigned int index(DGtal::uint32_t n, unsigned int b)
Definition testBits.cpp:44
int main()
Definition testBits.cpp:56
Image image(domain)
TriMesh::VertexRange VertexRange