DGtal  0.6.devel
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
testNormalVectorEstimatorEmbedder.cpp
1 
32 
33 #include <iostream>
34 #include <iterator>
35 #include "DGtal/base/Common.h"
36 #include "ConfigTest.h"
37 #include "DGtal/kernel/CanonicDigitalSurfaceEmbedder.h"
38 #include "DGtal/topology/DigitalSurface.h"
39 #include "DGtal/topology/DigitalSetBoundary.h"
40 #include "DGtal/topology/ImplicitDigitalSurface.h"
41 #include "DGtal/topology/LightImplicitDigitalSurface.h"
42 #include "DGtal/topology/ExplicitDigitalSurface.h"
43 #include "DGtal/topology/LightExplicitDigitalSurface.h"
44 #include "DGtal/topology/BreadthFirstVisitor.h"
45 #include "DGtal/topology/helpers/FrontierPredicate.h"
46 #include "DGtal/topology/helpers/BoundaryPredicate.h"
47 #include "DGtal/topology/CUndirectedSimpleLocalGraph.h"
48 #include "DGtal/topology/CUndirectedSimpleGraph.h"
49 
50 #include "DGtal/io/readers/VolReader.h"
51 #include "DGtal/images/imagesSetsUtils/SetFromImage.h"
52 
53 #include "DGtal/images/ImageSelector.h"
54 #include "DGtal/shapes/Shapes.h"
55 #include "DGtal/helpers/StdDefs.h"
56 #include "DGtal/kernel/CanonicEmbedder.h"
57 
58 #include "DGtal/geometry/surfaces/estimation/CNormalVectorEstimator.h"
59 #include "DGtal/geometry/surfaces/estimation/BasicConvolutionWeights.h"
60 #include "DGtal/geometry/surfaces/estimation/LocalConvolutionNormalVectorEstimator.h"
61 #include "DGtal/geometry/surfaces/estimation/DigitalSurfaceEmbedderWithNormalVectorEstimator.h"
63 
64 using namespace std;
65 using namespace DGtal;
66 using namespace Z3i;
67 
69 // Functions for testing class LocalConvolutionNormalVectorEstimator.
71 
75 bool testLocalConvolutionNormalVectorEstimator ( int /*argc*/, char**/*argv*/ )
76 {
77  unsigned int nbok = 0;
78  unsigned int nb = 0;
79 
80  trace.beginBlock ( "Testing convolution neighborhood ..." );
81 
82  std::string filename = testPath + "samples/cat10.vol";
83 
85  Image image = VolReader<Image>::importVol ( filename );
86  trace.info() <<image<<std::endl;
87  DigitalSet set3d ( image.domain() );
88  SetPredicate<DigitalSet> set3dPredicate ( set3d );
89  SetFromImage<DigitalSet>::append<Image> ( set3d, image,
90  0,256 );
91 
92  KSpace ks;
93  bool space_ok = ks.init ( image.domain().lowerBound(),
94  image.domain().upperBound(), true );
95  if ( !space_ok )
96  {
97  trace.error() << "Error in the Khamisky space construction."<<std::endl;
98  return 2;
99  }
100  trace.endBlock();
101  typedef SurfelAdjacency<KSpace::dimension> MySurfelAdjacency;
102  MySurfelAdjacency surfAdj ( true ); // interior in all directions.
103 
104  trace.beginBlock ( "Set up digital surface." );
108  SCell bel = Surfaces<KSpace>::findABel ( ks, set3dPredicate, 100000 );
109  MyDigitalSurfaceContainer* ptrSurfContainer =
110  new MyDigitalSurfaceContainer ( ks, set3dPredicate, surfAdj, bel );
111  MyDigitalSurface digSurf ( ptrSurfContainer ); // acquired
112  MyDigitalSurface::ConstIterator it = digSurf.begin();
113  trace.endBlock();
114 
115  trace.beginBlock ( "Compute and output surface <cat10-constant.off> with trivial normals." );
116  //Convolution kernel
118 
119  //Estimator definition
121  < MyDigitalSurface,
123  BOOST_CONCEPT_ASSERT ( ( CNormalVectorEstimator< MyConstantEstimator > ) );
124  MyConstantEstimator myNormalEstimator ( digSurf, kernel );
125 
126  // Embedder definition
127  typedef CanonicDigitalSurfaceEmbedder<MyDigitalSurface> SurfaceEmbedder;
128  SurfaceEmbedder surfaceEmbedder ( digSurf );
130  < SurfaceEmbedder, MyConstantEstimator > SurfaceEmbedderWithTrivialNormal;
131  SurfaceEmbedderWithTrivialNormal mySurfelEmbedder ( surfaceEmbedder,
132  myNormalEstimator );
133 
134  // Compute normal vector field and displays it.
135  myNormalEstimator.init ( 1.0, 2 );
136 
137  MyConstantEstimator::Quantity res = myNormalEstimator.eval ( it );
138  trace.info() << "Normal vector at begin() : "<< res << std::endl;
139 
140  ofstream out ( "cat10-constant.off" );
141  if ( out.good() )
142  digSurf.exportAs3DNOFF ( out,mySurfelEmbedder );
143  out.close();
144  trace.endBlock();
145 
146  trace.beginBlock ( "Compute and output surface <cat10-gaussian.off> with gaussian convoluted normals." );
147 
148  //Convolution kernel
150 
151  //Estimator definition
152  typedef LocalConvolutionNormalVectorEstimator < MyDigitalSurface,
154  BOOST_CONCEPT_ASSERT ( ( CNormalVectorEstimator< MyGaussianEstimator > ) );
155  MyGaussianEstimator myNormalEstimatorG ( digSurf, Gkernel );
156 
157  // Embedder definition
159  SurfaceEmbedderWithGaussianNormal mySurfelEmbedderG ( surfaceEmbedder, myNormalEstimatorG );
160 
161  // Compute normal vector field and displays it.
162  myNormalEstimatorG.init ( 1.0, 5 );
163 
164  MyGaussianEstimator::Quantity res2 = myNormalEstimatorG.eval ( it );
165  trace.info() << "Normal vector at begin() : "<< res2 << std::endl;
166  std::vector<MyGaussianEstimator::Quantity> allNormals;
167  myNormalEstimatorG.evalAll ( std::back_inserter ( allNormals ) );
168  trace.info() << "Normal vector field of size "<< allNormals.size() << std::endl;
169 
170  ofstream out2 ( "cat10-gaussian.off" );
171  if ( out2.good() )
172  digSurf.exportAs3DNOFF ( out2 ,mySurfelEmbedderG );
173  out2.close();
174 
175  nbok += true ? 1 : 0;
176  nb++;
177  trace.info() << "(" << nbok << "/" << nb << ") "
178  << "true == true" << std::endl;
179  trace.endBlock();
180 
181  return true;
182 }
183 
185 // Standard services - public :
186 
187 int main ( int argc, char** argv )
188 {
189  trace.beginBlock ( "Testing class LocalConvolutionNormalVectorEstimator" );
190  trace.info() << "Args:";
191  for ( int i = 0; i < argc; ++i )
192  trace.info() << " " << argv[ i ];
193  trace.info() << endl;
194 
195  bool res = testLocalConvolutionNormalVectorEstimator ( argc,argv ); // && ... other tests
196  trace.emphase() << ( res ? "Passed." : "Error." ) << endl;
197  trace.endBlock();
198  return true;
199 }
200 // //