DGtalTools  1.2.0
3dParametricCurveDigitizer.cpp
1 
30 #include <iostream>
31 #include <vector>
32 #include <string>
33 #include <iterator>
34 
35 #include "CLI11.hpp"
36 
37 #include "DGtal/base/Common.h"
38 #include "DGtal/helpers/StdDefs.h"
39 #include "DGtal/kernel/BasicPointFunctors.h"
40 
41 #include "DGtal/geometry/curves/parametric/EllipticHelix.h"
42 #include "DGtal/geometry/curves/parametric/Knot_3_1.h"
43 #include "DGtal/geometry/curves/parametric/Knot_3_2.h"
44 #include "DGtal/geometry/curves/parametric/Knot_4_1.h"
45 #include "DGtal/geometry/curves/parametric/Knot_4_3.h"
46 #include "DGtal/geometry/curves/parametric/Knot_5_1.h"
47 #include "DGtal/geometry/curves/parametric/Knot_5_2.h"
48 #include "DGtal/geometry/curves/parametric/Knot_6_2.h"
49 #include "DGtal/geometry/curves/parametric/Knot_7_4.h"
50 #include "DGtal/geometry/curves/parametric/NaiveParametricCurveDigitizer3D.h"
51 #include "DGtal/geometry/curves/parametric/DecoratorParametricCurveTransformation.h"
52 #include "DGtal/images/RigidTransformation3D.h"
53 
54 using namespace DGtal;
55 
56 
127 std::vector<std::string> curves3D;
128 std::vector<std::string> curvesDesc;
129 
134 void createList()
135 {
136  curves3D.push_back("EllipticHelix");
137  curvesDesc.push_back("A helix with two axes.");
138 
139  curves3D.push_back("Knot_3_1");
140  curvesDesc.push_back("A parametric polynomial knot.");
141 
142  curves3D.push_back("Knot_3_2");
143  curvesDesc.push_back("A parametric knot having period 2 * Pi.");
144 
145  curves3D.push_back("Knot_4_1");
146  curvesDesc.push_back("A parametric polynomial knot.");
147 
148  curves3D.push_back("Knot_4_3");
149  curvesDesc.push_back("A parametric knot having period 2 * Pi.");
150 
151  curves3D.push_back("Knot_5_1");
152  curvesDesc.push_back("A parametric polynomial knot.");
153 
154  curves3D.push_back("Knot_5_2");
155  curvesDesc.push_back("A parametric knot having period 2 * Pi.");
156 
157  curves3D.push_back("Knot_6_2");
158  curvesDesc.push_back("A parametric polynomial knot.");
159 
160  curves3D.push_back("Knot_7_4");
161  curvesDesc.push_back("A parametric polynomial knot.");
162 }
163 
168 void displayList()
169 {
170  trace.emphase()<<"3D Curves:"<<std::endl;
171  for ( unsigned int i = 0; i < curves3D.size(); ++i )
172  trace.info()<<"\t"<<curves3D[i]<<"\t" <<curvesDesc[i]<<std::endl;
173 }
174 
175 
184 unsigned int checkAndRetrunIndex(const std::string &curveName)
185 {
186  unsigned int pos = 0;
187 
188  while ((pos < curves3D.size()) && (curves3D[pos] != curveName))
189  pos++;
190 
191  if ( pos == curves3D.size ( ) )
192  {
193  trace.error ( ) << "The specified shape has not found.";
194  trace.info ( ) << std::endl;
195  exit ( 1 );
196  }
197 
198  return pos;
199 }
200 
201 
209 template <typename Iterator>
210 struct Exporter
211 {
212 
221  static
222  void save ( Iterator begin, Iterator end, const std::string outputName )
223  {
224  std::ofstream outfile;
225  outfile.open ( outputName + ".dat", std::ofstream::out );
226  for ( auto it = begin; it != end; ++it )
227  outfile << (*it)[0] << " " << (*it)[1] << " " << (*it)[2] << std::endl;
228  }
229 };
230 
236 void missingParam(std::string param)
237 {
238  trace.error() <<" Parameter: "<<param<<" is required..";
239  trace.info()<<std::endl;
240  exit(1);
241 }
242 
244 
245 int main( int argc, char** argv )
246 {
247  // parse command line CLI ----------------------------------------------
248  CLI::App app;
249  std::string curveName;
250  std::string outputName;
251  double param1 {1};
252  double param2 {1};
253  double param3 {1};
254  double tstart;
255  double tend;
256  double step;
257  unsigned int knext {5};
258  double angle;
259  double ox {0}, oy {0}, oz {0};
260  double ax {1}, ay {0}, az {0};
261 
262  app.description("Digitizes 3D parametric curves using DGtal library.\n Typical use example:\n \t 3dParametricCurveDigitizer [options] --curve <curve> --param1 <double> --param2 <double> --param3 <double> --tstart <double> --tend <double> --step <double> --output <basename>\n");
263  app.add_option("--param1",param1,"a radius or a scaling factor (default 0)",true);
264  app.add_option("--param2",param2,"a radius or a scaling factor (default 0)",true);
265  app.add_option("--param3",param3,"a radius or a scaling factor (default 0)",true);
266  auto tstartOpt = app.add_option("--tstart",tstart,"start time");
267  auto tendOpt = app.add_option("--tend",tend,"end time");
268  auto stepOpt = app.add_option("--step, -s",step,"step");
269  app.add_option("--knext, -k",knext,"K_NEXT value (default 5)",true);
270  auto listOpt = app.add_flag("--list,-l","List all available shapes");
271  auto curveNameOpt = app.add_option("--curve,-c",curveName,"Shape name");
272  app.add_option("--angle,-a",angle,"Rotation angle in radians(default 0)",true);
273  app.add_option("--ox",ox,"X coordinate of origin (default 0)",true);
274  app.add_option("--oy",oy,"Y coordinate of origin (default 0)",true);
275  app.add_option("--oz",oz,"Z coordinate of origin (default 0)",true);
276  app.add_option("--ax",ax,"X component of rotation axis (default 1)",true);
277  app.add_option("--ay",ay,"Y component of rotation axis (default 0)",true);
278  app.add_option("--az",az,"Z component of rotation axis (default 0)",true);
279  auto outputNameOpt = app.add_option("--output,-o",outputName,"Basename of the output file");
280 
281  app.get_formatter()->column_width(40);
282  CLI11_PARSE(app, argc, argv);
283  // END parse command line using CLI ----------------------------------------------
284 
285  //List creation
286  createList();
287 
288  if ( listOpt->count() > 0 )
289  {
290  displayList();
291  return 0;
292  }
293 
294  if ( curveNameOpt->count() == 0) missingParam("--curve");
295  if ( outputNameOpt->count() == 0) missingParam("--output");
296  if ( tstartOpt->count() == 0) missingParam("--tstart");
297  if ( tendOpt->count() == 0) missingParam("--tend");
298  if ( stepOpt->count() == 0) missingParam("--step");
299 
300  typedef functors::ForwardRigidTransformation3D < Z3i::Space, Z3i::RealPoint, Z3i::RealPoint, functors::Identity > ForwardTrans;
301  ForwardTrans trans ( Z3i::RealPoint ( ox, oy, oz ), Z3i::RealPoint ( ax, ay, az ), angle, Z3i::RealVector ( 0, 0, 0 ) );
302  //We check that the shape is known
303  unsigned int id = checkAndRetrunIndex ( curveName );
304 
305  if ( id == 0 )
306  {
307  typedef EllipticHelix < Z3i::Space > MyHelix;
308  typedef DecoratorParametricCurveTransformation < MyHelix, ForwardTrans > MyRotatedCurve;
309  typedef NaiveParametricCurveDigitizer3D < MyRotatedCurve > Digitizer;
310  typedef typename NaiveParametricCurveDigitizer3D < MyRotatedCurve >::DigitalCurve MyDigitalCurve;
311  typedef NaiveParametricCurveDigitizer3D < MyRotatedCurve >::MetaData MyMetaData;
312 
313  MyDigitalCurve digitalCurve;
314  MyHelix helix ( param1, param2, param3 );
315  MyRotatedCurve rotCurve ( helix, trans );
316  Digitizer digitize;
317  digitize.init ( tstart, tend, step );
318  digitize.setKNext ( knext );
319  digitize.attach ( &rotCurve );
320  digitize.digitize ( std::back_insert_iterator < MyDigitalCurve> ( digitalCurve ) );
321  Exporter < MyDigitalCurve::const_iterator >::save ( digitalCurve.begin ( ), digitalCurve.end ( ), outputName );
322  }
323 
324  if ( id == 1 )
325  {
326  typedef Knot_3_1 < Z3i::Space > MyKnot;
327  typedef DecoratorParametricCurveTransformation < MyKnot, ForwardTrans > MyRotatedCurve;
328  typedef NaiveParametricCurveDigitizer3D < MyRotatedCurve > Digitizer;
329  typedef NaiveParametricCurveDigitizer3D < MyRotatedCurve >::DigitalCurve MyDigitalCurve;
330  typedef NaiveParametricCurveDigitizer3D < MyRotatedCurve >::MetaData MyMetaData;
331 
332  MyDigitalCurve digitalCurve;
333  MyKnot knot ( param1, param2, param3 );
334  MyRotatedCurve rotCurve ( knot, trans );
335  Digitizer digitize;
336  digitize.init ( tstart, tend, step );
337  digitize.setKNext ( knext );
338  digitize.attach ( &rotCurve );
339  digitize.digitize ( std::back_insert_iterator < MyDigitalCurve> ( digitalCurve ) );
340  Exporter < MyDigitalCurve::const_iterator >::save ( digitalCurve.begin ( ), digitalCurve.end ( ), outputName );
341  }
342 
343  if ( id == 2 )
344  {
345  typedef Knot_3_2 < Z3i::Space > MyKnot;
346  typedef DecoratorParametricCurveTransformation < MyKnot, ForwardTrans > MyRotatedCurve;
347  typedef NaiveParametricCurveDigitizer3D < MyRotatedCurve > Digitizer;
348  typedef NaiveParametricCurveDigitizer3D < MyRotatedCurve >::DigitalCurve MyDigitalCurve;
349  typedef NaiveParametricCurveDigitizer3D < MyRotatedCurve >::MetaData MyMetaData;
350 
351  MyDigitalCurve digitalCurve;
352  MyKnot knot ( param1, param2, param3 );
353  MyRotatedCurve rotCurve ( knot, trans );
354  Digitizer digitize;
355  digitize.init ( tstart, tend, step );
356  digitize.setKNext ( knext );
357  digitize.attach ( &rotCurve );
358  digitize.digitize ( std::back_insert_iterator < MyDigitalCurve> ( digitalCurve ) );
359  Exporter < MyDigitalCurve::const_iterator >::save ( digitalCurve.begin ( ), digitalCurve.end ( ), outputName );
360  }
361 
362  if ( id == 3 )
363  {
364  typedef Knot_4_1 < Z3i::Space > MyKnot;
365  typedef DecoratorParametricCurveTransformation < MyKnot, ForwardTrans > MyRotatedCurve;
366  typedef NaiveParametricCurveDigitizer3D < MyRotatedCurve > Digitizer;
367  typedef NaiveParametricCurveDigitizer3D < MyRotatedCurve >::DigitalCurve MyDigitalCurve;
368  typedef NaiveParametricCurveDigitizer3D < MyRotatedCurve >::MetaData MyMetaData;
369 
370  MyDigitalCurve digitalCurve;
371  MyKnot knot ( param1, param2, param3 );
372  MyRotatedCurve rotCurve ( knot, trans );
373  Digitizer digitize;
374  digitize.init ( tstart, tend, step );
375  digitize.setKNext ( knext );
376  digitize.attach ( &rotCurve );
377  digitize.digitize ( std::back_insert_iterator < MyDigitalCurve> ( digitalCurve ) );
378  Exporter < MyDigitalCurve::const_iterator >::save ( digitalCurve.begin ( ), digitalCurve.end ( ), outputName );
379  }
380 
381  if ( id == 4 )
382  {
383  typedef Knot_4_3 < Z3i::Space > MyKnot;
384  typedef DecoratorParametricCurveTransformation < MyKnot, ForwardTrans > MyRotatedCurve;
385  typedef NaiveParametricCurveDigitizer3D < MyRotatedCurve > Digitizer;
386  typedef NaiveParametricCurveDigitizer3D < MyRotatedCurve >::DigitalCurve MyDigitalCurve;
387  typedef NaiveParametricCurveDigitizer3D < MyRotatedCurve >::MetaData MyMetaData;
388 
389  MyDigitalCurve digitalCurve;
390  MyKnot knot ( param1, param2, param3 );
391  MyRotatedCurve rotCurve ( knot, trans );
392  Digitizer digitize;
393  digitize.init ( tstart, tend, step );
394  digitize.setKNext ( knext );
395  digitize.attach ( &rotCurve );
396  digitize.digitize ( std::back_insert_iterator < MyDigitalCurve> ( digitalCurve ) );
397  Exporter < MyDigitalCurve::const_iterator >::save ( digitalCurve.begin ( ), digitalCurve.end ( ), outputName );
398  }
399 
400  if ( id == 5 )
401  {
402  typedef Knot_5_1 < Z3i::Space > MyKnot;
403  typedef DecoratorParametricCurveTransformation < MyKnot, ForwardTrans > MyRotatedCurve;
404  typedef NaiveParametricCurveDigitizer3D < MyRotatedCurve > Digitizer;
405  typedef NaiveParametricCurveDigitizer3D < MyRotatedCurve >::DigitalCurve MyDigitalCurve;
406  typedef NaiveParametricCurveDigitizer3D < MyRotatedCurve >::MetaData MyMetaData;
407 
408  MyDigitalCurve digitalCurve;
409  MyKnot knot ( param1, param2, param3 );
410  MyRotatedCurve rotCurve ( knot, trans );
411  Digitizer digitize;
412  digitize.init ( tstart, tend, step );
413  digitize.setKNext ( knext );
414  digitize.attach ( &rotCurve );
415  digitize.digitize ( std::back_insert_iterator < MyDigitalCurve> ( digitalCurve ) );
416  Exporter < MyDigitalCurve::const_iterator >::save ( digitalCurve.begin ( ), digitalCurve.end ( ), outputName );
417  }
418 
419  if ( id == 6 )
420  {
421  typedef Knot_5_2 < Z3i::Space > MyKnot;
422  typedef DecoratorParametricCurveTransformation < MyKnot, ForwardTrans > MyRotatedCurve;
423  typedef NaiveParametricCurveDigitizer3D < MyRotatedCurve > Digitizer;
424  typedef NaiveParametricCurveDigitizer3D < MyRotatedCurve >::DigitalCurve MyDigitalCurve;
425  typedef NaiveParametricCurveDigitizer3D < MyRotatedCurve >::MetaData MyMetaData;
426 
427  MyDigitalCurve digitalCurve;
428  MyKnot knot ( param1, param2, param3 );
429  MyRotatedCurve rotCurve ( knot, trans );
430  Digitizer digitize;
431  digitize.init ( tstart, tend, step );
432  digitize.setKNext ( knext );
433  digitize.attach ( &rotCurve );
434  digitize.digitize ( std::back_insert_iterator < MyDigitalCurve> ( digitalCurve ) );
435  Exporter < MyDigitalCurve::const_iterator >::save ( digitalCurve.begin ( ), digitalCurve.end ( ), outputName );
436  }
437 
438  if ( id == 7 )
439  {
440  typedef Knot_6_2 < Z3i::Space > MyKnot;
441  typedef DecoratorParametricCurveTransformation < MyKnot, ForwardTrans > MyRotatedCurve;
442  typedef NaiveParametricCurveDigitizer3D < MyRotatedCurve > Digitizer;
443  typedef NaiveParametricCurveDigitizer3D < MyRotatedCurve >::DigitalCurve MyDigitalCurve;
444  typedef NaiveParametricCurveDigitizer3D < MyRotatedCurve >::MetaData MyMetaData;
445 
446  MyDigitalCurve digitalCurve;
447  MyKnot knot ( param1, param2, param3 );
448  MyRotatedCurve rotCurve ( knot, trans );
449  Digitizer digitize;
450  digitize.init ( tstart, tend, step );
451  digitize.setKNext ( knext );
452  digitize.attach ( &rotCurve );
453  digitize.digitize ( std::back_insert_iterator < MyDigitalCurve> ( digitalCurve ) );
454  Exporter < MyDigitalCurve::const_iterator >::save ( digitalCurve.begin ( ), digitalCurve.end ( ), outputName );
455  }
456 
457  if ( id == 8 )
458  {
459  typedef Knot_7_4 < Z3i::Space > MyKnot;
460  typedef DecoratorParametricCurveTransformation < MyKnot, ForwardTrans > MyRotatedCurve;
461  typedef NaiveParametricCurveDigitizer3D < MyRotatedCurve > Digitizer;
462  typedef NaiveParametricCurveDigitizer3D < MyRotatedCurve >::DigitalCurve MyDigitalCurve;
463  typedef NaiveParametricCurveDigitizer3D < MyRotatedCurve >::MetaData MyMetaData;
464 
465  MyDigitalCurve digitalCurve;
466  MyKnot knot ( param1, param2, param3 );
467  MyRotatedCurve rotCurve ( knot, trans );
468  Digitizer digitize;
469  digitize.init ( tstart, tend, step );
470  digitize.setKNext ( knext );
471  digitize.attach ( &rotCurve );
472  digitize.digitize ( std::back_insert_iterator < MyDigitalCurve> ( digitalCurve ) );
473  Exporter < MyDigitalCurve::const_iterator >::save ( digitalCurve.begin ( ), digitalCurve.end ( ), outputName );
474  }
475 
476  return 0;
477 }
Definition: ATu0v1.h:57