DGtal  1.5.beta
fullConvexityThinning3D.cpp
Go to the documentation of this file.
1 
39 #include <iostream>
40 #include <queue>
41 #include "DGtal/base/Common.h"
42 #include "DGtal/io/viewers/Viewer3D.h"
43 #include "DGtal/io/DrawWithDisplay3DModifier.h"
44 #include "DGtal/io/Color.h"
45 #include "DGtal/shapes/Shapes.h"
46 #include "DGtal/helpers/StdDefs.h"
47 #include "DGtal/helpers/Shortcuts.h"
48 #include "DGtal/images/ImageContainerBySTLVector.h"
49 #include "DGtal/geometry/volumes/NeighborhoodConvexityAnalyzer.h"
50 
52 
53 using namespace std;
54 using namespace DGtal;
55 using namespace Z3i;
57 
59 
61 
62 int main( int argc, char** argv )
63 {
64  trace.info() << "Usage: " << argv[ 0 ] << " <thickness> <convexity> <input.vol> <m> <M>" << std::endl;
65  trace.info() << " - convexity in {0,1}: 0=0-convexity, 1=full-convexity"<< std::endl;
66 
67  int thickness = argc > 1 ? atoi( argv[ 1 ] ) : 2;
68  bool full_cvx = argc > 2 ? atoi( argv[ 2 ] ) == 1 : false;
69  std::string fn= argc > 3 ? argv[ 3 ] : "";
70  int m = argc > 4 ? atoi( argv[ 4 ] ) : 0;
71  int M = argc > 5 ? atoi( argv[ 5 ] ) : 255;
72  trace.beginBlock ( "Example of 3D shape thinning with full convexity properties" );
73 
74  QApplication application(argc,argv);
75  Viewer3D<> viewer;
76  viewer.setWindowTitle("fullConvexityThinning3D");
77  viewer.show();
78 
79  auto params = SH3::defaultParameters();
80 
81  // Domain creation from two bounding points.
82  trace.info() << "Building set or importing vol ... ";
83  Point c( 0, 0, 0 );
84  Point p1( -50, -50, -50 );
85  Point p2( 50, 50, 50 );
86  Domain domain( p1, p2 );
87  KSpace K;
88  std::set< Point > shape_set;
90  if ( fn == "" )
91  {
92  K.init( p1, p2, true );
93  for (Domain::ConstIterator it = domain.begin(); it != domain.end(); ++it )
94  {
95  Point p = *it;
96  if ( ((p - c ).norm() <= 22+thickness ) && ((p - c ).norm() >= 20-thickness)
97  && ( ((p[0] <= thickness)&& (p[0] >= -thickness))
98  || ((p[1] <= thickness)&& (p[1] >= -thickness))))
99  {
100  shape_set.insert( p );
101  bimage->setValue( p, true );
102  }
103  else
104  bimage->setValue( p, false );
105  }
106  }
107  else
108  {
109  params( "thresholdMin", m );
110  params( "thresholdMax", M );
111  bimage = SH3::makeBinaryImage( fn, params );
112  K = SH3::getKSpace( bimage );
113  p1 = K.lowerBound();
114  p2 = K.upperBound();
115  domain = Domain( p1, p2 );
116  for ( auto p : domain )
117  if ( (*bimage)( p ) ) shape_set.insert( p );
118  }
119  std::set< Point > origin_set( shape_set );
120  trace.info() << " [Done]" << std::endl;
121 
122  {
123  params( "surfaceComponents" , "All" );
124  auto surface = SH3::makeDigitalSurface( bimage, K, params );
125  bool ok = SH3::saveOBJ( surface, "source.obj" );
126  }
127 
128  trace.beginBlock ( "Thinning" );
129  SH3::BinaryImage& image = *bimage;
130  NCA nca( p1, p2, 10000 );
131  int nb_simple=0;
132  std::set< Point >::iterator it, itE;
133  std::set< Point > to_process( shape_set );
134  do
135  {
136  std::set< Point > next_to_process;
137  nb_simple = 0;
138  trace.info() << "Pass #S=" << shape_set.size()
139  << " #Q=" << to_process.size() << std::endl;
140  for ( auto it = to_process.begin(), itE = to_process.end(); it != itE; ++it )
141  {
142  Point p = *it;
143  if ( ! image( p ) ) continue; // already removed
144  nca.setCenter( p, image );
145  if ( full_cvx
147  : nca.is0ConvexCollapsible() )
148  {
149  std::vector< Point > neighbors;
150  nca.getLocalX( neighbors, false );
151  for ( auto q : neighbors ) next_to_process.insert( q );
152  shape_set.erase( p );
153  image.setValue( p, false );
154  ++nb_simple;
155  }
156  }
157  trace.info() << " => nb_removed=" << nb_simple<< std::endl;
158  if ( nb_simple != 0 )
159  std::swap( to_process, next_to_process );
160  }
161  while ( nb_simple != 0 );
162  trace.endBlock();
163 
164  {
165  params( "surfaceComponents" , "All" );
166  auto surface = SH3::makeDigitalSurface( bimage, K, params );
167  SH3::saveOBJ( surface, "geom-thinned.obj" );
168  }
169 
170  // Display by using two different list to manage OpenGL transparency.
171  DigitalSet origin( domain );
172  DigitalSet output( domain );
173  for ( auto p : origin_set ) origin.insert( p );
174  for ( auto p : shape_set ) output.insert( p );
175  viewer << SetMode3D( output.className(), "Paving" );
176  viewer << CustomColors3D(Color(25,25,255, 255), Color(25,25,255, 255));
177  viewer << output;
178 
179  viewer << SetMode3D( origin.className(), "PavingTransp" );
180  viewer << CustomColors3D(Color(250, 0,0, 25), Color(250, 0,0, 5));
181  viewer << origin;
182 
183  viewer<< Viewer3D<>::updateDisplay;
184 
185 
186  trace.endBlock();
187  return application.exec();
188 
189 }
190 // //
192 
Structure representing an RGB triple with alpha component.
Definition: Color.h:68
Aim: Smart pointer based on reference counts.
Definition: CountedPtr.h:80
Aim: A wrapper class around a STL associative container for storing sets of digital points within som...
Iterator for HyperRectDomain.
const ConstIterator & end() const
const ConstIterator & begin() const
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.
const Point & lowerBound() const
Return the lower bound for digital points in this space.
const Point & upperBound() const
Return the upper bound for digital points in this space.
Aim: A class that models a neighborhood and that provides services to analyse the convexity properti...
void setCenter(Point c, const PointPredicate &X)
void getLocalX(std::vector< Point > &localX, bool with_center) const
Aim: This class is used to simplify shape and surface creation. With it, you can create new shapes an...
Definition: Shortcuts.h:105
void beginBlock(const std::string &keyword="")
std::ostream & info()
double endBlock()
virtual void show()
Overload QWidget method in order to add a call to updateList() method (to ensure that the lists are w...
CountedPtr< SH3::DigitalSurface > surface
int main(int argc, char **argv)
Shortcuts< KSpace > SH3
NeighborhoodConvexityAnalyzer< KSpace, 1 > NCA
DGtal is the top-level namespace which contains all DGtal functions and types.
Trace trace
Definition: Common.h:153
Modifier class in a Display3D stream. Useful to choose your own mode for a given class....
KSpace K
Domain domain
Image image(domain)
HyperRectDomain< Space > Domain