40#include "DGtal/base/Common.h"
41#include "DGtal/kernel/domains/HyperRectDomain.h"
43#include "DGtal/shapes/ShapeFactory.h"
44#include "DGtal/shapes/Shapes.h"
45#include "DGtal/helpers/StdDefs.h"
48#include "DGtal/io/colormaps/GrayscaleColorMap.h"
49#include "DGtal/images/imagesSetsUtils/ImageFromSet.h"
50#include "DGtal/images/imagesSetsUtils/SetFromImage.h"
51#include "DGtal/images/ImageContainerBySTLVector.h"
52#include "DGtal/io/boards/Board2D.h"
54#include "DGtal/shapes/GaussDigitizer.h"
55#include "DGtal/geometry/curves/GridCurve.h"
56#include "DGtal/geometry/curves/estimation/TrueLocalEstimatorOnPoints.h"
57#include "DGtal/geometry/curves/estimation/TrueGlobalEstimatorOnPoints.h"
58#include "DGtal/geometry/curves/estimation/ParametricShapeCurvatureFunctor.h"
59#include "DGtal/geometry/curves/estimation/ParametricShapeTangentFunctor.h"
60#include "DGtal/geometry/curves/estimation/ParametricShapeArcLengthFunctor.h"
62#include "DGtal/topology/helpers/Surfaces.h"
147std::vector<std::string> shapes2D;
148std::vector<std::string> shapesDesc;
149std::vector<std::string> shapesParam1;
150std::vector<std::string> shapesParam2;
151std::vector<std::string> shapesParam3;
152std::vector<std::string> shapesParam4;
161 shapes2D.push_back(
"ball");
162 shapesDesc.push_back(
"Ball for the Euclidean metric.");
163 shapesParam1.push_back(
"--radius [-R]");
164 shapesParam2.push_back(
"");
165 shapesParam3.push_back(
"");
166 shapesParam4.push_back(
"");
168 shapes2D.push_back(
"square");
169 shapesDesc.push_back(
"square (no signature).");
170 shapesParam1.push_back(
"--width [-w]");
171 shapesParam2.push_back(
"");
172 shapesParam3.push_back(
"");
173 shapesParam4.push_back(
"");
175 shapes2D.push_back(
"lpball");
176 shapesDesc.push_back(
"Ball for the l_power metric (no signature).");
177 shapesParam1.push_back(
"--radius [-R],");
178 shapesParam2.push_back(
"--power [-p]");
179 shapesParam3.push_back(
"");
180 shapesParam4.push_back(
"");
182 shapes2D.push_back(
"flower");
183 shapesDesc.push_back(
"Flower with k petals with radius ranging from R+/-v.");
184 shapesParam1.push_back(
"--radius [-R],");
185 shapesParam2.push_back(
"--varsmallradius [-v],");
186 shapesParam3.push_back(
"--k [-k],");
187 shapesParam4.push_back(
"--phi");
189 shapes2D.push_back(
"ngon");
190 shapesDesc.push_back(
"Regular k-gon.");
191 shapesParam1.push_back(
"--radius [-R],");
192 shapesParam2.push_back(
"--k [-k],");
193 shapesParam3.push_back(
"--phi");
194 shapesParam4.push_back(
"");
196 shapes2D.push_back(
"accflower");
197 shapesDesc.push_back(
"Accelerated Flower with k petals.");
198 shapesParam1.push_back(
"--radius [-R],");
199 shapesParam2.push_back(
"--varsmallradius [-v],");
200 shapesParam3.push_back(
"--k [-k],");
201 shapesParam4.push_back(
"--phi");
203 shapes2D.push_back(
"ellipse");
204 shapesDesc.push_back(
"Ellipse.");
205 shapesParam1.push_back(
"--axis1 [-A],");
206 shapesParam2.push_back(
"--axis2 [-a],");
207 shapesParam3.push_back(
"--phi");
208 shapesParam4.push_back(
"");
217 trace.emphase()<<
"2D Shapes:"<<std::endl;
218 for(
unsigned int i=0; i<shapes2D.size(); ++i)
219 trace.info()<<
"\t"<<shapes2D[i]<<
"\t"
220 << shapesDesc[i]<<std::endl
221 <<
"\t\tRequired parameter(s): "
222 << shapesParam1[i]<<
" "
223 << shapesParam2[i]<<
" "
224 << shapesParam3[i]<<
" "
225 << shapesParam4[i]<<std::endl;
237unsigned int checkAndReturnIndex(
const std::string &shapeName)
241 while ((pos < shapes2D.size()) && (shapes2D[pos] != shapeName))
244 if (pos == shapes2D.size())
246 trace.error() <<
"The specified shape has not found.";
247 trace.info()<<std::endl;
266template <
typename Shape,
typename Range,
typename Po
int,
typename Quantity>
268estimateGeometry(Shape& s,
271 std::vector<Point>& points,
272 std::vector<Point>& tangents,
273 std::vector<Quantity>& curvatures) {
275 typedef typename Range::ConstIterator ConstIterator;
276 for (ConstIterator i = r.begin(); i != r.end(); ++i)
283 typedef typename Range::ConstCirculator ConstCirculator;
285 typedef ParametricShapeTangentFunctor< Shape > TangentFunctor;
286 TrueLocalEstimatorOnPoints< ConstCirculator, Shape, TangentFunctor >
287 trueTangentEstimator;
288 trueTangentEstimator.attach( s );
289 trueTangentEstimator.eval( r.c(), r.c(), std::back_inserter(tangents), h );
291 typedef ParametricShapeCurvatureFunctor< Shape > CurvatureFunctor;
292 TrueLocalEstimatorOnPoints< ConstCirculator, Shape, CurvatureFunctor >
293 trueCurvatureEstimator;
294 trueCurvatureEstimator.attach( s );
295 trueCurvatureEstimator.eval( r.c(), r.c(), std::back_inserter(curvatures), h );
298template <
typename Space,
typename Shape>
303 const std::string & outputFormat,
305 const std::string & outputFileName )
308 typedef typename Space::Point Point;
309 typedef typename Space::Vector Vector;
310 typedef typename Space::RealPoint RealPoint;
311 typedef typename Space::Integer Integer;
312 typedef HyperRectDomain<Space> Domain;
313 typedef KhalimskySpaceND<Space::dimension,Integer> KSpace;
314 typedef typename KSpace::SCell SCell;
315 typedef typename GridCurve<KSpace>::PointsRange Range;
316 typedef typename Range::ConstIterator ConstIteratorOnPoints;
317 typedef typename GridCurve<KSpace>::MidPointsRange MidPointsRange;
320 GaussDigitizer<Space,Shape> dig;
321 dig.attach( aShape );
322 Vector vlow(-1,-1); Vector vup(1,1);
323 dig.init( aShape.getLowerBound()+vlow, aShape.getUpperBound()+vup, h );
324 Domain domain = dig.getDomain();
327 bool ok = K.init( dig.getLowerBound(), dig.getUpperBound(),
true );
330 std::cerr <<
"[generateContour]"
331 <<
" error in creating KSpace." << std::endl;
337 SurfelAdjacency<KSpace::dimension> SAdj(
true );
338 SCell bel = Surfaces<KSpace>::findABel( K, dig, 10000 );
340 std::vector<Point> points;
341 Surfaces<KSpace>::track2DBoundaryPoints( points, K, SAdj, dig, bel );
343 GridCurve<KSpace> gridcurve;
344 gridcurve.initFromPointsVector( points );
346 Range r = gridcurve.getPointsRange();
348 if ( outputFormat ==
"pts" )
350 for ( ConstIteratorOnPoints it = r.begin(), it_end = r.end();
354 std::cout << p[ 0 ] <<
" " << p[ 1 ] << std::endl;
357 else if ( outputFormat ==
"fc" )
359 ConstIteratorOnPoints it = r.begin();
361 std::cout << p[ 0 ] <<
" " << p[ 1 ] <<
" ";
362 for ( ConstIteratorOnPoints it_end = r.end(); it != it_end; ++it )
366 if ( v[0 ]== 1 ) std::cout <<
'0';
367 if ( v[ 1 ] == 1 ) std::cout <<
'1';
368 if ( v[ 0 ] == -1 ) std::cout <<
'2';
369 if ( v[ 1 ] == -1 ) std::cout <<
'3';
373 Point p2= *(r.begin());
375 if ( v.norm1() == 1 )
377 if ( v[ 0 ] == 1 ) std::cout <<
'0';
378 if ( v[ 1 ] == 1 ) std::cout <<
'1';
379 if ( v[ 0 ] == -1 ) std::cout <<
'2';
380 if ( v[ 1 ] == -1 ) std::cout <<
'3';
382 std::cout << std::endl;
389 s << outputFileName <<
".geom";
390 std::ofstream outstream(s.str().c_str());
391 if (!outstream.is_open())
return false;
394 outstream <<
"# " << outputFileName << std::endl;
395 outstream <<
"# Pointel (x,y), Midpoint of the following linel (x',y')" << std::endl;
396 outstream <<
"# id x y tangentx tangenty curvaturexy"
397 <<
" x' y' tangentx' tangenty' curvaturex'y'" << std::endl;
399 std::vector<RealPoint> truePoints, truePoints2;
400 std::vector<RealPoint> trueTangents, trueTangents2;
401 std::vector<double> trueCurvatures, trueCurvatures2;
403 estimateGeometry<Shape, Range, RealPoint, double>
404 (aShape, h, r, truePoints, trueTangents, trueCurvatures);
406 estimateGeometry<Shape, MidPointsRange, RealPoint, double>
407 (aShape, h, gridcurve.getMidPointsRange(), truePoints2, trueTangents2, trueCurvatures2);
410 unsigned int n = (
unsigned int)r.size();
411 for (
unsigned int i = 0; i < n; ++i )
413 outstream << std::setprecision( 15 ) << i
414 <<
" " << truePoints[ i ][ 0 ]
415 <<
" " << truePoints[ i ][ 1 ]
416 <<
" " << trueTangents[ i ][ 0 ]
417 <<
" " << trueTangents[ i ][ 1 ]
418 <<
" " << trueCurvatures[ i ]
419 <<
" " << truePoints2[ i ][ 0 ]
420 <<
" " << truePoints2[ i ][ 1 ]
421 <<
" " << trueTangents2[ i ][ 0 ]
422 <<
" " << trueTangents2[ i ][ 1 ]
423 <<
" " << trueCurvatures2[ i ]
433 catch ( InputException e )
435 std::cerr <<
"[generateContour]"
436 <<
" error in finding a bel." << std::endl;
447void missingParam(std::string param)
449 trace.error() <<
" Parameter: "<<param<<
" is required..";
450 trace.info()<<std::endl;
456int main(
int argc,
char** argv )
460 std::string shapeName;
461 std::string outputFileName;
462 std::string outputFormat {
"pts"};
465 double smallradius {5};
466 double varsmallradius {5};
467 double cx {0.0}, cy {0.0};
474 app.description(
"Generates multigrid contours of 2d digital shapes using DGtal library.\n Typical use example:\n \t contourGenerator --shape <shapeName> [requiredParam] [otherOptions]\n");
475 auto listOpt = app.add_flag(
"--list,-l",
"List all available shapes");
476 auto shapeNameOpt = app.add_option(
"--shape,-s", shapeName,
"Shape name");
477 auto radiusOpt = app.add_option(
"--radius,-R", radius,
"Radius of the shape" );
478 auto axis1Opt = app.add_option(
"--axis1,-A", axis1,
"Half big axis of the shape (ellipse)" );
479 auto axis2Opt = app.add_option(
"--axis2,-a", axis2,
"Half small axis of the shape (ellipse)" );
480 auto smallradiusOpt = app.add_option(
"--smallradius,-r", smallradius,
"Small radius of the shape (default 5)");
481 auto varsmallradiusOpt = app.add_option(
"--varsmallradius,-v", varsmallradius,
"Variable small radius of the shape (default 5)" );
482 auto kOpt = app.add_option(
"-k", k,
"Number of branches or corners the shape (default 3)" );
483 auto phiOpt = app.add_option(
"--phi", phi,
"Phase of the shape (in radian, default 0.0)" );
484 auto widthOpt = app.add_option(
"--width,-w", width,
"Width of the shape (default 10.0)" );
485 auto powerOpt = app.add_option(
"--power,-p", power,
"Power of the metric (default 2.0)" );
486 app.add_option(
"--center_x,-x", cx,
"x-coordinate of the shape center (default 0.0)" );
487 app.add_option(
"--center_y,-y", cy,
"y-coordinate of the shape center (default 0.0)" );
488 app.add_option(
"--gridstep,-g", h,
"Gridstep for the digitization (default 1.0)" );
489 auto outputFormatOpt = app.add_option(
"--format,-f", outputFormat,
"Output format:\n\t List of pointel coordinates {pts}\n\t Freeman chaincode Vector {fc} (default pts)");
490 auto outputFileNameOpt = app.add_option(
"--outputGeometry,-o", outputFileName,
"Base name of the file containing the shape geometry (points, tangents, curvature)" );
492 app.get_formatter()->column_width(40);
493 CLI11_PARSE(app, argc, argv);
499 if ( listOpt->count() > 0 )
505 if(shapeNameOpt->count()==0) missingParam(
"--shape");
506 bool withGeom =
true;
507 if (outputFileNameOpt->count()==0) withGeom =
false;
510 unsigned int id = checkAndReturnIndex(shapeName);
513 typedef Z2i::Space Space;
514 typedef Space::RealPoint RealPoint;
516 RealPoint center( cx, cy );
520 if (radiusOpt->count()==0) missingParam(
"--radius");
521 Ball2D<Space> ball(Z2i::Point(0,0), radius);
522 generateContour<Space>( ball, h, outputFormat, withGeom, outputFileName );
527 ImplicitHyperCube<Space> object(Z2i::Point(0,0), width/2);
528 trace.error()<<
"Not available.";
529 trace.info()<<std::endl;
534 if (radiusOpt->count()==0) missingParam(
"--radius");
535 ImplicitRoundedHyperCube<Space> ball(Z2i::Point(0,0), radius, power);
536 trace.error()<<
"Not available.";
537 trace.info()<<std::endl;
542 if (radiusOpt->count()==0) missingParam(
"--radius");
545 Flower2D<Space> flower( center, radius, varsmallradius, k, phi );
546 generateContour<Space>( flower, h, outputFormat, withGeom, outputFileName );
550 if (radiusOpt->count()==0) missingParam(
"--radius");
553 NGon2D<Space> object( center, radius, k, phi );
554 generateContour<Space>(
object, h, outputFormat, withGeom, outputFileName );
559 if (radiusOpt->count()==0) missingParam(
"--radius");
562 AccFlower2D<Space> accflower( center, radius, varsmallradius, k, phi );
563 generateContour<Space>( accflower, h, outputFormat, withGeom, outputFileName );
567 if (axis1Opt->count()==0) missingParam(
"--axis1");
568 if (axis2Opt->count()==0) missingParam(
"--axis2");
570 Ellipse2D<Space> ellipse( center, axis1, axis2, phi );
571 generateContour<Space>( ellipse, h, outputFormat, withGeom, outputFileName );