DGtal  0.6.devel
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
testLocalConvolutionNormalVectorEstimator.cpp
1 
30 
31 #include <iostream>
32 #include "ConfigTest.h"
33 #include "DGtal/base/Common.h"
34 #include "DGtal/topology/DigitalSurface.h"
35 #include "DGtal/topology/DigitalSetBoundary.h"
36 #include "DGtal/topology/ImplicitDigitalSurface.h"
37 #include "DGtal/topology/LightImplicitDigitalSurface.h"
38 #include "DGtal/topology/ExplicitDigitalSurface.h"
39 #include "DGtal/topology/LightExplicitDigitalSurface.h"
40 #include "DGtal/topology/BreadthFirstVisitor.h"
41 #include "DGtal/topology/helpers/FrontierPredicate.h"
42 #include "DGtal/topology/helpers/BoundaryPredicate.h"
43 #include "DGtal/topology/CUndirectedSimpleLocalGraph.h"
44 #include "DGtal/topology/CUndirectedSimpleGraph.h"
45 
46 #include "DGtal/io/readers/VolReader.h"
47 #include "DGtal/images/imagesSetsUtils/SetFromImage.h"
48 
49 #include "DGtal/images/ImageSelector.h"
50 #include "DGtal/shapes/Shapes.h"
51 #include "DGtal/helpers/StdDefs.h"
52 #include <QtGui/qapplication.h>
53 
54 #include "DGtal/io/viewers/Viewer3D.h"
55 #include "DGtal/geometry/surfaces/estimation/BasicConvolutionWeights.h"
56 
57 #include "DGtal/geometry/surfaces/estimation/LocalConvolutionNormalVectorEstimator.h"
59 
60 using namespace std;
61 using namespace DGtal;
62 using namespace Z3i;
63 
65 // Functions for testing class LocalConvolutionNormalVectorEstimator.
67 
71 bool testLocalConvolutionNormalVectorEstimator ( int argc, char**argv )
72 {
73  unsigned int nbok = 0;
74  unsigned int nb = 0;
75 
76  trace.beginBlock ( "Testing convolution neighborhood ..." );
77 
78  QApplication application ( argc,argv );
79  Viewer3D viewer;
80 
81  std::string filename = testPath + "samples/cat10.vol";
82 
84  Image image = VolReader<Image>::importVol ( filename );
85  trace.info() <<image<<std::endl;
86  DigitalSet set3d ( image.domain() );
87  SetPredicate<DigitalSet> set3dPredicate ( set3d );
88  SetFromImage<DigitalSet>::append<Image> ( set3d, image,
89  0,256 );
90 
91  KSpace ks;
92  bool space_ok = ks.init ( image.domain().lowerBound(),
93  image.domain().upperBound(), true );
94  if ( !space_ok )
95  {
96  trace.error() << "Error in the Khamisky space construction."<<std::endl;
97  return true; //2; (return a bool !!!)
98  }
99  trace.endBlock();
100  typedef SurfelAdjacency<KSpace::dimension> MySurfelAdjacency;
101  MySurfelAdjacency surfAdj ( true ); // interior in all directions.
102 
103  trace.beginBlock ( "Set up digital surface." );
107  SCell bel = Surfaces<KSpace>::findABel ( ks, set3dPredicate, 100000 );
108  MyDigitalSurfaceContainer* ptrSurfContainer =
109  new MyDigitalSurfaceContainer ( ks, set3dPredicate, surfAdj, bel );
110  MyDigitalSurface digSurf ( ptrSurfContainer ); // acquired
111 
112  MyDigitalSurface::ConstIterator it = digSurf.begin();
113 
114 
115  //Convolution kernel
117 
118  //Estimator definition
119  typedef LocalConvolutionNormalVectorEstimator<MyDigitalSurface,
121  MyEstimator myNormalEstimator ( digSurf, kernel );
122 
123  myNormalEstimator.init ( 1.0, 5 );
124 
125  MyEstimator::Quantity res = myNormalEstimator.eval ( it );
126  trace.info() << "Normal vector at begin() : "<< res << std::endl;
127 
128  viewer.show();
129 
130  for ( MyDigitalSurface::ConstIterator itbis = digSurf.begin(),itend=digSurf.end();
131  itbis!=itend; ++itbis )
132  {
133  viewer << ks.unsigns ( *itbis );
134 
135  Point center = ks.sCoords ( *itbis );
136  MyEstimator::Quantity normal = myNormalEstimator.eval ( itbis );
137  viewer.addLine ( center[0],center[1],center[2],
138  center[0]-3*normal[0],center[1]-3*normal[1],center[2]-3*normal[2],
139  DGtal::Color ( 200,20,20 ), 1.0 );
140  }
141  viewer<< Viewer3D::updateDisplay;
142 
143  //Convolution kernel
145 
146  //Estimator definition
147  typedef LocalConvolutionNormalVectorEstimator<MyDigitalSurface,
149  MyEstimatorGaussian myNormalEstimatorG ( digSurf, Gkernel );
150 
151  myNormalEstimatorG.init ( 1.0, 15 );
152 
153  MyEstimatorGaussian::Quantity res2 = myNormalEstimatorG.eval ( it );
154  trace.info() << "Normal vector at begin() : "<< res2 << std::endl;
155 
156  viewer<< CustomColors3D ( Color ( 200, 0, 0 ),Color ( 200, 0,0 ) );
157  for ( MyDigitalSurface::ConstIterator itbis = digSurf.begin(),itend=digSurf.end();
158  itbis!=itend; ++itbis )
159  {
160  viewer << ks.unsigns ( *itbis );
161 
162  Point center = ks.sCoords ( *itbis );
163  MyEstimatorGaussian::Quantity normal = myNormalEstimatorG.eval ( itbis );
164  viewer.addLine ( center[0],center[1],center[2],
165  center[0]-3*normal[0],center[1]-3*normal[1],center[2]-3*normal[2],
166  DGtal::Color ( 20,200,20 ), 1.0 );
167  }
168  viewer<< Viewer3D::updateDisplay;
169 
170 
171  nbok += true ? 1 : 0;
172  nb++;
173  trace.info() << "(" << nbok << "/" << nb << ") "
174  << "true == true" << std::endl;
175  trace.endBlock();
176 
177  return application.exec();
178 }
179 
181 // Standard services - public :
182 
183 int main ( int argc, char** argv )
184 {
185  trace.beginBlock ( "Testing class LocalConvolutionNormalVectorEstimator" );
186  trace.info() << "Args:";
187  for ( int i = 0; i < argc; ++i )
188  trace.info() << " " << argv[ i ];
189  trace.info() << endl;
190 
191  bool res = testLocalConvolutionNormalVectorEstimator ( argc,argv ); // && ... other tests
192  trace.emphase() << ( res ? "Passed." : "Error." ) << endl;
193  trace.endBlock();
194  return true;
195 }
196 // //