DGtalTools 2.1.0
Loading...
Searching...
No Matches
contourGenerator.cpp
1
32#include <cmath>
33#include <iostream>
34#include <iomanip>
35#include <vector>
36#include <string>
37
38#include "CLI11.hpp"
39
40#include "DGtal/base/Common.h"
41#include "DGtal/kernel/domains/HyperRectDomain.h"
42
43#include "DGtal/shapes/ShapeFactory.h"
44#include "DGtal/shapes/Shapes.h"
45#include "DGtal/helpers/StdDefs.h"
46
47
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"
53
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"
61
62#include "DGtal/topology/helpers/Surfaces.h"
63
64
65using namespace DGtal;
66
148std::vector<std::string> shapes2D;
149std::vector<std::string> shapesDesc;
150std::vector<std::string> shapesParam1;
151std::vector<std::string> shapesParam2;
152std::vector<std::string> shapesParam3;
153std::vector<std::string> shapesParam4;
154
155
160void createList()
161{
162 shapes2D.push_back("ball");
163 shapesDesc.push_back("Ball for the Euclidean metric.");
164 shapesParam1.push_back("--radius [-R]");
165 shapesParam2.push_back("");
166 shapesParam3.push_back("");
167 shapesParam4.push_back("");
168
169 shapes2D.push_back("square");
170 shapesDesc.push_back("square (no signature).");
171 shapesParam1.push_back("--width [-w]");
172 shapesParam2.push_back("");
173 shapesParam3.push_back("");
174 shapesParam4.push_back("");
175
176 shapes2D.push_back("lpball");
177 shapesDesc.push_back("Ball for the l_power metric (no signature).");
178 shapesParam1.push_back("--radius [-R],");
179 shapesParam2.push_back("--power [-p]");
180 shapesParam3.push_back("");
181 shapesParam4.push_back("");
182
183 shapes2D.push_back("flower");
184 shapesDesc.push_back("Flower with k petals with radius ranging from R+/-v.");
185 shapesParam1.push_back("--radius [-R],");
186 shapesParam2.push_back("--varsmallradius [-v],");
187 shapesParam3.push_back("--k [-k],");
188 shapesParam4.push_back("--phi");
189
190 shapes2D.push_back("ngon");
191 shapesDesc.push_back("Regular k-gon.");
192 shapesParam1.push_back("--radius [-R],");
193 shapesParam2.push_back("--k [-k],");
194 shapesParam3.push_back("--phi");
195 shapesParam4.push_back("");
196
197 shapes2D.push_back("accflower");
198 shapesDesc.push_back("Accelerated Flower with k petals.");
199 shapesParam1.push_back("--radius [-R],");
200 shapesParam2.push_back("--varsmallradius [-v],");
201 shapesParam3.push_back("--k [-k],");
202 shapesParam4.push_back("--phi");
203
204 shapes2D.push_back("ellipse");
205 shapesDesc.push_back("Ellipse.");
206 shapesParam1.push_back("--axis1 [-A],");
207 shapesParam2.push_back("--axis2 [-a],");
208 shapesParam3.push_back("--phi");
209 shapesParam4.push_back("");
210}
211
216void displayList()
217{
218 trace.emphase()<<"2D Shapes:"<<std::endl;
219 for(unsigned int i=0; i<shapes2D.size(); ++i)
220 trace.info()<<"\t"<<shapes2D[i]<<"\t"
221 << shapesDesc[i]<<std::endl
222 <<"\t\tRequired parameter(s): "
223 << shapesParam1[i]<<" "
224 << shapesParam2[i]<<" "
225 << shapesParam3[i]<<" "
226 << shapesParam4[i]<<std::endl;
227}
228
229
238unsigned int checkAndReturnIndex(const std::string &shapeName)
239{
240 unsigned int pos=0;
241
242 while ((pos < shapes2D.size()) && (shapes2D[pos] != shapeName))
243 pos++;
244
245 if (pos == shapes2D.size())
246 {
247 trace.error() << "The specified shape has not found.";
248 trace.info()<<std::endl;
249 exit(1);
250 }
251
252 return pos;
253}
254
255
267template <typename Shape, typename Range, typename Point, typename Quantity>
268void
269estimateGeometry(Shape& s,
270 const double& h,
271 const Range& r,
272 std::vector<Point>& points,
273 std::vector<Point>& tangents,
274 std::vector<Quantity>& curvatures) {
275
276 typedef typename Range::ConstIterator ConstIterator;
277 for (ConstIterator i = r.begin(); i != r.end(); ++i)
278 {
279 Point p( *i );
280 p *= h;
281 points.push_back(p);
282 }
283
284 typedef typename Range::ConstCirculator ConstCirculator;
285
286 typedef ParametricShapeTangentFunctor< Shape > TangentFunctor;
287 TrueLocalEstimatorOnPoints< ConstCirculator, Shape, TangentFunctor >
288 trueTangentEstimator;
289 trueTangentEstimator.attach( s );
290 trueTangentEstimator.eval( r.c(), r.c(), std::back_inserter(tangents), h );
291
292 typedef ParametricShapeCurvatureFunctor< Shape > CurvatureFunctor;
293 TrueLocalEstimatorOnPoints< ConstCirculator, Shape, CurvatureFunctor >
294 trueCurvatureEstimator;
295 trueCurvatureEstimator.attach( s );
296 trueCurvatureEstimator.eval( r.c(), r.c(), std::back_inserter(curvatures), h );
297}
298
299template <typename Space, typename Shape>
300bool
301generateContour(
302 Shape & aShape,
303 double h,
304 const std::string & outputFormat,
305 bool withGeom,
306 const std::string & outputFileName )
307{
308 // Types
309 typedef typename Space::Point Point;
310 typedef typename Space::Vector Vector;
311 typedef typename Space::RealPoint RealPoint;
312 typedef typename Space::Integer Integer;
313 typedef HyperRectDomain<Space> Domain;
314 typedef KhalimskySpaceND<Space::dimension,Integer> KSpace;
315 typedef typename KSpace::SCell SCell;
316 typedef typename GridCurve<KSpace>::PointsRange Range;
317 typedef typename Range::ConstIterator ConstIteratorOnPoints;
318 typedef typename GridCurve<KSpace>::MidPointsRange MidPointsRange;
319
320 // Digitizer
321 GaussDigitizer<Space,Shape> dig;
322 dig.attach( aShape ); // attaches the shape.
323 Vector vlow(-1,-1); Vector vup(1,1);
324 dig.init( aShape.getLowerBound()+vlow, aShape.getUpperBound()+vup, h );
325 Domain domain = dig.getDomain();
326 // Create cellular space
327 KSpace K;
328 bool ok = K.init( dig.getLowerBound(), dig.getUpperBound(), true );
329 if ( ! ok )
330 {
331 std::cerr << "[generateContour]"
332 << " error in creating KSpace." << std::endl;
333 return false;
334 }
335 try
336 {
337 // Extracts shape boundary
338 SurfelAdjacency<KSpace::dimension> SAdj( true );
339 SCell bel = Surfaces<KSpace>::findABel( K, dig, 10000 );
340 // Getting the consecutive surfels of the 2D boundary
341 std::vector<Point> points;
342 Surfaces<KSpace>::track2DBoundaryPoints( points, K, SAdj, dig, bel );
343 // Create GridCurve
344 GridCurve<KSpace> gridcurve;
345 gridcurve.initFromPointsVector( points );
346 // gridcurve contains the digital boundary to analyze.
347 Range r = gridcurve.getPointsRange(); //building range
348
349 if ( outputFormat == "pts" )
350 {
351 for ( ConstIteratorOnPoints it = r.begin(), it_end = r.end();
352 it != it_end; ++it )
353 {
354 Point p = *it;
355 std::cout << p[ 0 ] << " " << p[ 1 ] << std::endl;
356 }
357 }
358 else if ( outputFormat == "fc" )
359 {
360 ConstIteratorOnPoints it = r.begin();
361 Point p = *it++;
362 std::cout << p[ 0 ] << " " << p[ 1 ] << " ";
363 for ( ConstIteratorOnPoints it_end = r.end(); it != it_end; ++it )
364 {
365 Point p2 = *it;
366 Vector v = p2 - p;
367 if ( v[0 ]== 1 ) std::cout << '0';
368 if ( v[ 1 ] == 1 ) std::cout << '1';
369 if ( v[ 0 ] == -1 ) std::cout << '2';
370 if ( v[ 1 ] == -1 ) std::cout << '3';
371 p = p2;
372 }
373 // close freemanchain if necessary.
374 Point p2= *(r.begin());
375 Vector v = p2 - p;
376 if ( v.norm1() == 1 )
377 {
378 if ( v[ 0 ] == 1 ) std::cout << '0';
379 if ( v[ 1 ] == 1 ) std::cout << '1';
380 if ( v[ 0 ] == -1 ) std::cout << '2';
381 if ( v[ 1 ] == -1 ) std::cout << '3';
382 }
383 std::cout << std::endl;
384 }
385
386 if (withGeom)
387 {
388 // write geometry of the shape
389 std::stringstream s;
390 s << outputFileName << ".geom";
391 std::ofstream outstream(s.str().c_str()); //output stream
392 if (!outstream.is_open()) return false;
393 else
394 {
395 outstream << "# " << outputFileName << std::endl;
396 outstream << "# Pointel (x,y), Midpoint of the following linel (x',y')" << std::endl;
397 outstream << "# id x y tangentx tangenty curvaturexy"
398 << " x' y' tangentx' tangenty' curvaturex'y'" << std::endl;
399
400 std::vector<RealPoint> truePoints, truePoints2;
401 std::vector<RealPoint> trueTangents, trueTangents2;
402 std::vector<double> trueCurvatures, trueCurvatures2;
403
404 estimateGeometry<Shape, Range, RealPoint, double>
405 (aShape, h, r, truePoints, trueTangents, trueCurvatures);
406
407 estimateGeometry<Shape, MidPointsRange, RealPoint, double>
408 (aShape, h, gridcurve.getMidPointsRange(), truePoints2, trueTangents2, trueCurvatures2);
409
410
411 unsigned int n = (unsigned int)r.size();
412 for (unsigned int i = 0; i < n; ++i )
413 {
414 outstream << std::setprecision( 15 ) << i
415 << " " << truePoints[ i ][ 0 ]
416 << " " << truePoints[ i ][ 1 ]
417 << " " << trueTangents[ i ][ 0 ]
418 << " " << trueTangents[ i ][ 1 ]
419 << " " << trueCurvatures[ i ]
420 << " " << truePoints2[ i ][ 0 ]
421 << " " << truePoints2[ i ][ 1 ]
422 << " " << trueTangents2[ i ][ 0 ]
423 << " " << trueTangents2[ i ][ 1 ]
424 << " " << trueCurvatures2[ i ]
425 << std::endl;
426 }
427 }
428 outstream.close();
429 }
430
432
433 }
434 catch ( InputException e )
435 {
436 std::cerr << "[generateContour]"
437 << " error in finding a bel." << std::endl;
438 return false;
439 }
440 return true;
441}
442
448void missingParam(std::string param)
449{
450 trace.error() <<" Parameter: "<<param<<" is required..";
451 trace.info()<<std::endl;
452 exit(1);
453}
454
456
457int main( int argc, char** argv )
458{
459 // parse command line CLI ----------------------------------------------
460 CLI::App app;
461 std::string shapeName;
462 std::string outputFileName;
463 std::string outputFormat {"pts"};
464 double radius;
465 double power {2.0};
466 double smallradius {5};
467 double varsmallradius {5};
468 double cx {0.0}, cy {0.0};
469 double h {1.0};
470 unsigned int k {3};
471 double phi {0.0};
472 double width {10.0};
473 double axis1, axis2;
474
475 app.description("Generates multigrid contours of 2d digital shapes using DGtal library.\n Typical use example:\n \t contourGenerator --shape <shapeName> [requiredParam] [otherOptions]\n");
476 auto listOpt = app.add_flag("--list,-l","List all available shapes");
477 auto shapeNameOpt = app.add_option("--shape,-s", shapeName, "Shape name");
478 auto radiusOpt = app.add_option("--radius,-R", radius, "Radius of the shape" );
479 auto axis1Opt = app.add_option("--axis1,-A", axis1, "Half big axis of the shape (ellipse)" );
480 auto axis2Opt = app.add_option("--axis2,-a", axis2, "Half small axis of the shape (ellipse)" );
481 auto smallradiusOpt = app.add_option("--smallradius,-r", smallradius, "Small radius of the shape (default 5)");
482 auto varsmallradiusOpt = app.add_option("--varsmallradius,-v", varsmallradius, "Variable small radius of the shape (default 5)" );
483 auto kOpt = app.add_option("-k", k, "Number of branches or corners the shape (default 3)" );
484 auto phiOpt = app.add_option("--phi", phi, "Phase of the shape (in radian, default 0.0)" );
485 auto widthOpt = app.add_option("--width,-w", width, "Width of the shape (default 10.0)" );
486 auto powerOpt = app.add_option("--power,-p", power, "Power of the metric (default 2.0)" );
487 app.add_option("--center_x,-x", cx, "x-coordinate of the shape center (default 0.0)" );
488 app.add_option("--center_y,-y", cy, "y-coordinate of the shape center (default 0.0)" );
489 app.add_option("--gridstep,-g", h, "Gridstep for the digitization (default 1.0)" );
490 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)");
491 auto outputFileNameOpt = app.add_option("--outputGeometry,-o", outputFileName, "Base name of the file containing the shape geometry (points, tangents, curvature)" );
492
493 app.get_formatter()->column_width(40);
494 CLI11_PARSE(app, argc, argv);
495 // END parse command line using CLI ----------------------------------------------
496
497 //List creation
498 createList();
499
500 if ( listOpt->count() > 0 )
501 {
502 displayList();
503 return 0;
504 }
505
506 if(shapeNameOpt->count()==0) missingParam("--shape");
507 bool withGeom = true;
508 if (outputFileNameOpt->count()==0) withGeom = false;
509
510 //We check that the shape is known
511 unsigned int id = checkAndReturnIndex(shapeName);
512
513 // standard types
514 typedef Z2i::Space Space;
515 typedef Space::RealPoint RealPoint;
516
517 RealPoint center( cx, cy );
518
519 if (id ==0)
520 {
521 if (radiusOpt->count()==0) missingParam("--radius");
522 Ball2D<Space> ball(Z2i::Point(0,0), radius);
523 generateContour<Space>( ball, h, outputFormat, withGeom, outputFileName );
524 }
525 else if (id == 1)
526 {
527 //if (widthOpt->count()==0) missingParam("--width");
528 ImplicitHyperCube<Space> object(Z2i::Point(0,0), width/2);
529 trace.error()<< "Not available.";
530 trace.info()<<std::endl;
531 }
532 else if (id == 2)
533 {
534 //if (powerOpt->count()==0) missingParam("--power");
535 if (radiusOpt->count()==0) missingParam("--radius");
536 ImplicitRoundedHyperCube<Space> ball(Z2i::Point(0,0), radius, power);
537 trace.error()<< "Not available.";
538 trace.info()<<std::endl;
539 }
540 else if (id == 3)
541 {
542 //if (varsmallradiusOpt->count()==0) missingParam("--varsmallradius");
543 if (radiusOpt->count()==0) missingParam("--radius");
544 //if (kOpt->count()==0) missingParam("--k");
545 //if (phiOpt->count()==0) missingParam("--phi");
546 Flower2D<Space> flower( center, radius, varsmallradius, k, phi );
547 generateContour<Space>( flower, h, outputFormat, withGeom, outputFileName );
548 }
549 else if (id == 4)
550 {
551 if (radiusOpt->count()==0) missingParam("--radius");
552 //if (kOpt->count()==0) missingParam("--k");
553 //if (phiOpt->count()==0) missingParam("--phi");
554 NGon2D<Space> object( center, radius, k, phi );
555 generateContour<Space>( object, h, outputFormat, withGeom, outputFileName );
556 }
557 else if (id == 5)
558 {
559 //if (varsmallradiusOpt->count()==0) missingParam("--varsmallradius");
560 if (radiusOpt->count()==0) missingParam("--radius");
561 //if (kOpt->count()==0) missingParam("--k");
562 //if (phiOpt->count()==0) missingParam("--phi");
563 AccFlower2D<Space> accflower( center, radius, varsmallradius, k, phi );
564 generateContour<Space>( accflower, h, outputFormat, withGeom, outputFileName );
565 }
566 else if (id == 6)
567 {
568 if (axis1Opt->count()==0) missingParam("--axis1");
569 if (axis2Opt->count()==0) missingParam("--axis2");
570 //if (phiOpt->count()==0) missingParam("--phi");
571 Ellipse2D<Space> ellipse( center, axis1, axis2, phi );
572 generateContour<Space>( ellipse, h, outputFormat, withGeom, outputFileName );
573 }
574}
Definition ATu0v1.h:57