DGtalTools 2.1.0
Loading...
Searching...
No Matches
vol2normalField.cpp
1
32#include <iostream>
33#include <iterator>
34#include "DGtal/base/Common.h"
35
36#include "DGtal/topology/CanonicDigitalSurfaceEmbedder.h"
37#include "DGtal/topology/DigitalSurface.h"
38#include "DGtal/topology/DigitalSetBoundary.h"
39#include "DGtal/topology/ImplicitDigitalSurface.h"
40#include "DGtal/topology/LightImplicitDigitalSurface.h"
41#include "DGtal/topology/ExplicitDigitalSurface.h"
42#include "DGtal/topology/LightExplicitDigitalSurface.h"
43#include "DGtal/graph/BreadthFirstVisitor.h"
44#include "DGtal/topology/helpers/FrontierPredicate.h"
45#include "DGtal/topology/helpers/BoundaryPredicate.h"
46#include "DGtal/graph/CUndirectedSimpleLocalGraph.h"
47#include "DGtal/graph/CUndirectedSimpleGraph.h"
48
49#include "DGtal/io/readers/VolReader.h"
50#include "DGtal/images/imagesSetsUtils/SetFromImage.h"
51#include "DGtal/images/SimpleThresholdForegroundPredicate.h"
52#include "DGtal/images/ImageSelector.h"
53#include "DGtal/shapes/Shapes.h"
54#include "DGtal/helpers/StdDefs.h"
55#include "DGtal/kernel/CanonicEmbedder.h"
56
57#include "DGtal/geometry/surfaces/estimation/DigitalSurfaceEmbedderWithNormalVectorEstimator.h"
58
59#include "DGtal/geometry/surfaces/estimation/LocalEstimatorFromSurfelFunctorAdapter.h"
60#include "DGtal/geometry/surfaces/estimation/estimationFunctors/ElementaryConvolutionNormalVectorEstimator.h"
61#include "DGtal/geometry/volumes/distance/LpMetric.h"
62#include "CLI11.hpp"
63
65
66using namespace std;
67using namespace DGtal;
68using namespace Z3i;
69
129void missingParam ( std::string param )
130{
131 trace.error() <<" Parameter: "<<param<<" is required..";
132 trace.info() <<std::endl;
133 exit ( 1 );
134}
135
136
137int main ( int argc, char**argv )
138{
139
140 // parse command line CLI ----------------------------------------------
141 CLI::App app;
142 std::string filename;
143 std::string outputFileName;
144 unsigned int level {0};
145 double sigma {5.0};
146 unsigned int neighborhood {10};
147 double normExport {1.0};
148
149 app.description("Generates normal vector field from a vol file using DGtal library.\n Typical use example:\n \t vol2normalField[options] --input <volFileName> --o <outputFileName>\n");
150 app.add_option("-i,--input,1",filename,"Input vol file.")->required()->check(CLI::ExistingFile);
151 app.add_option("-o,--output,2",outputFileName,"Output file.")->required();
152 app.add_option("--level,-l",level,"Iso-level for the surface construction (default 0).");
153 app.add_option("--sigma,-s", sigma,"Sigma parameter of the Gaussian kernel (default 5.0).");
154 auto expOpt = app.add_flag("--exportOriginAndExtremity", "exports the origin and extremity of the vector fields when exporting the vector field in TXT format (useful to be displayed in other viewer like meshViewer).");
155 app.add_option("--vectorsNorm,-N", normExport, "set the norm of the exported vectors in TXT format (when the extremity points are exported with --exportOriginAndExtremity). By using a negative value you will invert the direction of the vectors (default 1.0).");
156 app.add_option("--neighborhood,-n", neighborhood,"Size of the neighborhood for the convolution (distance on surfel graph, default 10).");
157
158 app.get_formatter()->column_width(40);
159 CLI11_PARSE(app, argc, argv);
160 // END parse command line using CLI ----------------------------------------------
161
162 typedef ImageSelector < Z3i::Domain, unsigned char>::Type Image;
163 Image image = VolReader<Image>::importVol ( filename );
164
165 trace.info() <<image<<std::endl;
166
167 functors::SimpleThresholdForegroundPredicate<Image> simplePredicate ( image, level );
168
169 KSpace ks;
170 bool space_ok = ks.init ( image.domain().lowerBound(),
171 image.domain().upperBound(), true );
172 if ( !space_ok )
173 {
174 trace.error() << "Error in the Khamisky space construction."<<std::endl;
175 return 2;
176 }
177
178 typedef SurfelAdjacency<KSpace::dimension> MySurfelAdjacency;
179 MySurfelAdjacency surfAdj ( true ); // interior in all directions.
180
181 //Set up digital surface.
182 typedef LightImplicitDigitalSurface<KSpace, functors::SimpleThresholdForegroundPredicate<Image> > MyDigitalSurfaceContainer;
183 typedef DigitalSurface<MyDigitalSurfaceContainer> MyDigitalSurface;
184 SCell bel = Surfaces<KSpace>::findABel ( ks, simplePredicate );
185
186 MyDigitalSurfaceContainer* ptrSurfContainer =
187 new MyDigitalSurfaceContainer ( ks, simplePredicate, surfAdj, bel );
188 MyDigitalSurface digSurf ( ptrSurfContainer ); // acquired
189 MyDigitalSurface::ConstIterator it = digSurf.begin();
190
191 // Embedder definition
192 typedef CanonicDigitalSurfaceEmbedder<MyDigitalSurface> SurfaceEmbedder;
193 SurfaceEmbedder surfaceEmbedder ( digSurf );
194
195 //Convolution kernel
196 typedef typename MyDigitalSurface::Surfel Surfel;
197 typedef DGtal::functors::GaussianKernel GaussianFunctor;
198 typedef DGtal::functors::ElementaryConvolutionNormalVectorEstimator<Surfel, CanonicSCellEmbedder<KSpace>> Functor;
199 typedef LocalEstimatorFromSurfelFunctorAdapter<
200 MyDigitalSurfaceContainer,
201 LpMetric<Z3i::Space>,
202 Functor, GaussianFunctor> MyGaussianEstimator;
203
204 //Estimator definition
205
206 GaussianFunctor Gkernel(sigma);
207 LpMetric<Z3i::Space> l1(1.0);
208 CanonicSCellEmbedder<KSpace> embedder(digSurf.container().space());
209 Functor estimator(embedder, 1.0);
210
211 MyGaussianEstimator myNormalEstimatorG;
212 myNormalEstimatorG.attach(digSurf);
213 myNormalEstimatorG.setParams(l1, estimator, Gkernel, neighborhood);
214
215 // Embedder definition
216 typedef DigitalSurfaceEmbedderWithNormalVectorEstimator<SurfaceEmbedder,MyGaussianEstimator> SurfaceEmbedderWithGaussianNormal;
217 SurfaceEmbedderWithGaussianNormal mySurfelEmbedderG ( surfaceEmbedder, myNormalEstimatorG );
218
219 // Compute normal vector field and displays it.
220 myNormalEstimatorG.init ( 1.0, digSurf.begin(), digSurf.end() );
221
222 trace.info() << "Generating the NOFF surface "<< std::endl;
223 ofstream out2 ( ( outputFileName + ".off" ).c_str() );
224 if ( out2.good() )
225 digSurf.exportAs3DNOFF ( out2 ,mySurfelEmbedderG );
226 out2.close();
227
228 trace.info() << "Generating the polar coordinates file"<< std::endl;
229 ofstream out3 ( ( outputFileName + ".txt" ).c_str() );
230 if ( out3.good() )
231 {
232 MyGaussianEstimator::Quantity res;
233 for ( MyDigitalSurface::ConstIterator it =digSurf.begin(),
234 itend = digSurf.end(); it != itend; ++it )
235 {
236 res = myNormalEstimatorG.eval ( it );
237 //We output Theta - Phi
238 out3<< acos ( res [2] ) *180.0/M_PI <<" " << ( atan2 ( res [1], res [0] ) + M_PI ) *180.0/M_PI;
239
240 if (expOpt->count()>0)
241 {
242 res *= normExport;
243 out3 << " " << mySurfelEmbedderG(*it)[0]
244 << " " << mySurfelEmbedderG(*it)[1]
245 << " " << mySurfelEmbedderG(*it)[2] << " "
246 << mySurfelEmbedderG(*it)[0]+res[0] << " "
247 << mySurfelEmbedderG(*it)[1]+res[1] << " "
248 << mySurfelEmbedderG(*it)[2]+res[2];
249
250 }
251
252 out3 <<std::endl;
253 }
254 }
255 out3.close();
256
257 return 0;
258}
259
260
261// //
263
Definition ATu0v1.h:57