Visual Servoing Platform  version 3.6.1 under development (2024-11-15)
tutorial-pcl-viewer.cpp
1 #include <visp3/core/vpConfig.h>
3 
4 // System include
5 #include <iostream>
6 
7 #if defined(VISP_HAVE_PCL) && defined(VISP_HAVE_PCL_VISUALIZATION)
8 
9 // ViSP include
10 #include <visp3/core/vpIoTools.h>
11 
13 // Tutorial include
14 #include "ClassUsingPclViewer.h"
16 
17 #ifdef ENABLE_VISP_NAMESPACE
18 using namespace VISP_NAMESPACE_NAME;
19 #endif
20 
22 
26 typedef enum DisplayMode
27 {
28  BLOCKING = 0,
29  THREADED = 1,
30  BOTH = 2,
31  MODE_COUNT = 3
32 } DisplayMode;
33 
40 std::string displayModeToString(const DisplayMode &mode)
41 {
42  switch (mode) {
43  case BLOCKING:
44  return "blocking";
45  case THREADED:
46  return "threaded";
47  case BOTH:
48  return "both";
49  default:
50  break;
51  }
52  return "unknown";
53 }
54 
62 DisplayMode displayModeFromString(const std::string &name)
63 {
64  DisplayMode res = DisplayMode::MODE_COUNT;
65  bool wasFound = false;
66  std::string lowerCaseName = vpIoTools::toLowerCase(name);
67  for (unsigned int i = 0; i < DisplayMode::MODE_COUNT && !wasFound; i++) {
68  DisplayMode candidate = (DisplayMode)i;
69  if (lowerCaseName == displayModeToString(candidate)) {
70  res = candidate;
71  wasFound = true;
72  }
73  }
74  return res;
75 }
76 
85 std::string getAvailableDisplayMode(const std::string &prefix = "< ", const std::string &sep = " , ", const std::string &suffix = " >")
86 {
87  std::string modes(prefix);
88  for (unsigned int i = 0; i < DisplayMode::MODE_COUNT - 1; i++) {
89  DisplayMode candidate = (DisplayMode)i;
90  modes += displayModeToString(candidate) + sep;
91  }
92  DisplayMode candidate = (DisplayMode)(DisplayMode::MODE_COUNT - 1);
93  modes += displayModeToString(candidate) + suffix;
94  return modes;
95 }
97 
98 int main(int argc, char *argv[])
99 {
101  const double def_addedNoise = 0.; // Standard deviation of the noise added to the points.
102  const unsigned int def_order = 2; // Order of the polynomial surface used for the example.
103  const std::pair<double, double> def_xlim = std::pair<double, double>(-2.5, 2.5); // Min and max X-axis coordinates.
104  const std::pair<double, double> def_ylim = std::pair<double, double>(-2.5, 2.5); // Min and max Y-axis coordinates.
105  const std::pair<unsigned int, unsigned int> def_reso = std::pair<unsigned int, unsigned int>(50, 50); // Number of points along the X-axis and Y-axis reciprocally.
106  const DisplayMode def_mode = DisplayMode::BLOCKING; // Display mode that should be used.
108 
110  double opt_addedNoise = def_addedNoise;
111  unsigned int opt_order = def_order;
112  std::pair<double, double> opt_xlim = def_xlim;
113  std::pair<double, double> opt_ylim = def_ylim;
114  std::pair<unsigned int, unsigned int> opt_reso = def_reso;
115  DisplayMode opt_mode = def_mode;
116 
117  for (int i = 1; i < argc; i++) {
118  if (std::string(argv[i]) == "--noise" && i + 1 < argc) {
119  opt_addedNoise = atof(argv[i + 1]);
120  i++;
121  }
122  else if (std::string(argv[i]) == "--order" && i + 1 < argc) {
123  opt_order = atoi(argv[i + 1]);
124  i++;
125  }
126  else if (std::string(argv[i]) == "--x-lim" && i + 2 < argc) {
127  opt_xlim.first = atof(argv[i + 1]);
128  opt_xlim.second = atof(argv[i + 2]);
129  i += 2;
130  }
131  else if (std::string(argv[i]) == "--y-lim" && i + 2 < argc) {
132  opt_ylim.first = atof(argv[i + 1]);
133  opt_ylim.second = atof(argv[i + 2]);
134  i += 2;
135  }
136  else if (std::string(argv[i]) == "--reso" && i + 2 < argc) {
137  opt_reso.first = atoi(argv[i + 1]);
138  opt_reso.second = atoi(argv[i + 2]);
139  i += 2;
140  }
141  else if (std::string(argv[i]) == "--display-mode" && i + 1 < argc) {
142  opt_mode = displayModeFromString(std::string(argv[i + 1]));
143  i++;
144  }
145  else if (std::string(argv[i]) == "--help" || std::string(argv[i]) == "-h") {
147  std::cout << "NAME" << std::endl;
148  std::cout << "\t" << argv[0] << " Test programm for the PCL-based point-cloud visualizer." << std::endl
149  << std::endl;
150  std::cout << "SYNOPSIS" << std::endl;
151  std::cout << "\t" << argv[0]
152  << "\t[--noise <stdev_noise>] (default: " + std::to_string(def_addedNoise) << ")\n"
153  << "\t[--order <surface-order>](default: " + std::to_string(def_order) << ")\n"
154  << "\t[--x-lim <xmin xmax>](default: [" + std::to_string(def_xlim.first) + ";" + std::to_string(def_xlim.second) << "])\n"
155  << "\t[--y-lim <ymin ymax>](default: [" + std::to_string(def_ylim.first) + ";" + std::to_string(def_ylim.second) << "])\n"
156  << "\t[--reso <x_resolution y_resolution>](default: [" + std::to_string(def_reso.first) + ";" + std::to_string(def_reso.second) << "])\n"
157  << "\t[--display-mode " << getAvailableDisplayMode() << "](default: " << displayModeToString(def_mode) << ")\n"
158  << "\t[--help] [-h]" << std::endl
159  << std::endl;
161  return EXIT_SUCCESS;
162  }
163  }
165 
166  std::cout << "Parameters:" << std::endl;
167  std::cout << "\tSurface order: " << opt_order << std::endl;
168  std::cout << "\tX-axis limits: [" << opt_xlim.first << " ; " << opt_xlim.first << "]" << std::endl;
169  std::cout << "\tY-axis limits: [" << opt_ylim.first << " ; " << opt_ylim.first << "]" << std::endl;
170  std::cout << "\tGrid resolution: [" << opt_reso.first << " x " << opt_reso.first << "]" << std::endl;
171  std::cout << "\tNoise standard deviation: " << opt_addedNoise << std::endl;
172  std::cout << "\tDisplay mode: " << displayModeToString(opt_mode) << std::endl;
173 
175  if (opt_mode == DisplayMode::BLOCKING || opt_mode == DisplayMode::BOTH) {
176  ClassUsingPclViewer demo(opt_xlim, opt_ylim, opt_reso);
177  demo.blockingMode(opt_addedNoise, opt_order);
178  }
180 
182  if (opt_mode == DisplayMode::THREADED || opt_mode == DisplayMode::BOTH) {
183  ClassUsingPclViewer demo(opt_xlim, opt_ylim, opt_reso);
184  demo.threadedMode(opt_addedNoise, opt_order);
185  }
187 
188  return 0;
189 }
190 #else
191 
192 int main()
193 {
194  std::cout << "ViSP seems to have been compiled without PCL visualization module." << std::endl;
195  std::cout << "Please install PCL visualization module and recompile ViSP." << std::endl;
196  return EXIT_SUCCESS;
197 }
198 #endif
static std::string toLowerCase(const std::string &input)
Return a lower-case version of the string input . Numbers and special characters stay the same.
Definition: vpIoTools.cpp:1339