Point Cloud Library (PCL)  1.11.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Modules Pages
pcl_plotter.h
1 /*
2  * Software License Agreement (BSD License)
3  *
4  * Point Cloud Library (PCL) - www.pointclouds.org
5  * Copyright (c) 2010-2011, Willow Garage, Inc.
6  * Copyright (c) 2012-, Open Perception, Inc.
7  *
8  * All rights reserved.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  *
14  * * Redistributions of source code must retain the above copyright
15  * notice, this list of conditions and the following disclaimer.
16  * * Redistributions in binary form must reproduce the above
17  * copyright notice, this list of conditions and the following
18  * disclaimer in the documentation and/or other materials provided
19  * with the distribution.
20  * * Neither the name of the copyright holder(s) nor the names of its
21  * contributors may be used to endorse or promote products derived
22  * from this software without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
25  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
26  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
27  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
28  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
29  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
30  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
31  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
32  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
34  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35  * POSSIBILITY OF SUCH DAMAGE.
36  *
37  */
38 
39 #pragma once
40 
41 #include <iostream>
42 #include <vector>
43 #include <utility>
44 #include <cfloat>
45 
46 #include <pcl/visualization/common/common.h>
47 #include <pcl/point_types.h>
48 #include <pcl/correspondence.h>
49 #include <pcl/point_cloud.h>
50 #include <pcl/common/io.h>
51 
52 class vtkRenderWindow;
53 class vtkRenderWindowInteractor;
54 class vtkContextView;
55 class vtkChartXY;
56 class vtkColorSeries;
57 
58 #include <vtkSmartPointer.h>
59 #include <vtkCommand.h>
60 #include <vtkChart.h>
61 
62 namespace pcl
63 {
64  namespace visualization
65  {
66  /** \brief PCL Plotter main class. Given point correspondences this class
67  * can be used to plot the data one against the other and display it on the
68  * screen. It also has methods for providing plot for important functions
69  * like histogram etc. Important functions of PCLHistogramVisualizer are
70  * redefined here so that this single class can take responsibility of all
71  * plotting related functionalities.
72  *
73  * \author Kripasindhu Sarkar
74  * \ingroup visualization
75  */
76  class PCL_EXPORTS PCLPlotter
77  {
78  public:
79  using Ptr = shared_ptr<PCLPlotter>;
80  using ConstPtr = shared_ptr<const PCLPlotter>;
81 
82  /**\brief A representation of polynomial function. i'th element of the vector denotes the coefficient of x^i of the polynomial in variable x.
83  */
84  using PolynomialFunction = std::vector<double>;
85 
86  /**\brief A representation of rational function, defined as the ratio of two polynomial functions. pair::first denotes the numerator and pair::second denotes the denominator of the Rational function.
87  */
88  using RationalFunction = std::pair<PolynomialFunction, PolynomialFunction>;
89 
90  /** \brief PCL Plotter constructor.
91  * \param[in] name Name of the window
92  */
93  PCLPlotter (char const * name = "PCL Plotter");
94 
95  /** \brief Destructor. */
96  ~PCLPlotter();
97 
98  /** \brief Adds a plot with correspondences in the arrays arrayX and arrayY
99  * \param[in] array_X X coordinates of point correspondence array
100  * \param[in] array_Y Y coordinates of point correspondence array
101  * \param[in] size length of the array arrayX and arrayY
102  * \param[in] name name of the plot which appears in the legend when toggled on
103  * \param[in] type type of the graph plotted. vtkChart::LINE for line plot, vtkChart::BAR for bar plot, and vtkChart::POINTS for a scattered point plot
104  * \param[in] color a character array of 4 fields denoting the R,G,B and A component of the color of the plot ranging from 0 to 255. If this argument is not passed (or NULL is passed) the plot is colored based on a color scheme
105  */
106  void
107  addPlotData (double const *array_X,
108  double const *array_Y,
109  unsigned long size,
110  char const * name = "Y Axis",
111  int type = vtkChart::LINE ,
112  char const *color=nullptr);
113 
114  /** \brief Adds a plot with correspondences in vectors arrayX and arrayY. This is the vector version of the addPlotData function.
115  * \param[in] array_x X coordinates of point correspondence array
116  * \param[in] array_y Y coordinates of point correspondence array
117  * \param[in] name name of the plot which appears in the legend when toggled on
118  * \param[in] type type of the graph plotted. vtkChart::LINE for line plot, vtkChart::BAR for bar plot, and vtkChart::POINTS for a scattered point plot
119  * \param[in] color a character array of 4 fields denoting the R,G,B and A component of the color of the plot ranging from 0 to 255. If this argument is not passed (or NULL is passed) the plot is colored based on a color scheme
120  */
121  void
122  addPlotData (std::vector<double> const &array_x,
123  std::vector<double>const &array_y,
124  char const * name = "Y Axis",
125  int type = vtkChart::LINE,
126  std::vector<char> const &color = std::vector<char> ());
127 
128  /** \brief Adds a plot with correspondences in vector of pairs. The the first and second field of the pairs of the vector forms the correspondence.
129  * \param plot_data
130  * \param[in] name name of the plot which appears in the legend when toggled on
131  * \param[in] type type of the graph plotted. vtkChart::LINE for line plot, vtkChart::BAR for bar plot, and vtkChart::POINTS for a scattered point plot
132  * \param[in] color a character array of 4 fields denoting the R,G,B and A component of the color of the plot ranging from 0 to 255. If this argument is not passed (or NULL is passed) the plot is colored based on a color scheme
133  */
134  void
135  addPlotData (std::vector<std::pair<double, double> > const &plot_data,
136  char const * name = "Y Axis",
137  int type = vtkChart::LINE,
138  std::vector<char> const &color = std::vector<char>());
139 
140  /** \brief Adds a plot based on the given polynomial function and the range in X axis.
141  * \param[in] p_function A polynomial function which is represented by a vector which stores the coefficients. See description on the typedef.
142  * \param[in] x_min the left boundary of the range for displaying the plot
143  * \param[in] x_max the right boundary of the range for displaying the plot
144  * \param[in] name name of the plot which appears in the legend when toggled on
145  * \param[in] num_points Number of points plotted to show the graph. More this number, more is the resolution.
146  * \param[in] type type of the graph plotted. vtkChart::LINE for line plot, vtkChart::BAR for bar plot, and vtkChart::POINTS for a scattered point plot
147  * \param[in] color a character array of 4 fields denoting the R,G,B and A component of the color of the plot ranging from 0 to 255. If this argument is not passed (or NULL is passed) the plot is colored based on a color scheme
148  */
149  void
150  addPlotData (PolynomialFunction const & p_function,
151  double x_min, double x_max,
152  char const *name = "Y Axis",
153  int num_points = 100,
154  int type = vtkChart::LINE,
155  std::vector<char> const &color = std::vector<char>());
156 
157  /** \brief Adds a plot based on the given rational function and the range in X axis.
158  * \param[in] r_function A rational function which is represented by the ratio of two polynomial functions. See description on the typedef for more details.
159  * \param[in] x_min the left boundary of the range for displaying the plot
160  * \param[in] x_max the right boundary of the range for displaying the plot
161  * \param[in] name name of the plot which appears in the legend when toggled on
162  * \param[in] num_points Number of points plotted to show the graph. More this number, more is the resolution.
163  * \param[in] type type of the graph plotted. vtkChart::LINE for line plot, vtkChart::BAR for bar plot, and vtkChart::POINTS for a scattered point plot
164  * \param[in] color a character array of 4 fields denoting the R,G,B and A component of the color of the plot ranging from 0 to 255. If this argument is not passed (or NULL is passed) the plot is colored based on a color scheme
165  */
166  void
167  addPlotData (RationalFunction const & r_function,
168  double x_min, double x_max,
169  char const *name = "Y Axis",
170  int num_points = 100,
171  int type = vtkChart::LINE,
172  std::vector<char> const &color = std::vector<char>());
173 
174  /** \brief Adds a plot based on a user defined callback function representing the function to plot
175  * \param[in] function a user defined callback function representing the relation y = function(x)
176  * \param[in] x_min the left boundary of the range for displaying the plot
177  * \param[in] x_max the right boundary of the range for displaying the plot
178  * \param[in] name name of the plot which appears in the legend when toggled on
179  * \param[in] num_points Number of points plotted to show the graph. More this number, more is the resolution.
180  * \param[in] type type of the graph plotted. vtkChart::LINE for line plot, vtkChart::BAR for bar plot, and vtkChart::POINTS for a scattered point plot
181  * \param[in] color a character array of 4 fields denoting the R,G,B and A component of the color of the plot ranging from 0 to 255. If this argument is not passed (or NULL is passed) the plot is colored based on a color scheme
182  */
183  void
184  addPlotData (double (*function)(double),
185  double x_min, double x_max,
186  char const *name = "Y Axis",
187  int num_points = 100,
188  int type = vtkChart::LINE,
189  std::vector<char> const &color = std::vector<char>());
190 
191  /** \brief Adds a plot based on a space/tab delimited table provided in a file
192  * \param[in] filename name of the file containing the table. 1st column represents the values of X-Axis. Rest of the columns represent the corresponding values in Y-Axes. First row of the file is considered for naming/labeling of the plot. The plot-names should not contain any space in between.
193  * \param[in] type type of the graph plotted. vtkChart::LINE for line plot, vtkChart::BAR for bar plot, and vtkChart::POINTS for a scattered point plot
194  */
195  void
196  addPlotData (char const * filename,
197  int type = vtkChart::LINE);
198 
199  /** \brief Bins the elements in vector data into nbins equally spaced containers and plots the resulted histogram
200  * \param[in] data the raw data
201  * \param[in] nbins the number of bins for the histogram
202  * \param[in] name name of this histogram which will appear on legends if toggled on
203  * \param[in] color a character array of 4 fields denoting the R,G,B and A component of the color of the plot ranging from 0 to 255. If this argument is not passed (or an empty vector is passed) the histogram is colored based on the current color scheme
204  */
205  void
206  addHistogramData (std::vector<double> const & data,
207  int const nbins = 10,
208  char const * name = "Histogram",
209  std::vector<char> const &color = std::vector<char>());
210 
211  //##PCLHistogramVisulizer methods##
212  /** \brief Add a histogram feature to screen as a separate window, from a cloud containing a single histogram.
213  * \param[in] cloud the PointCloud dataset containing the histogram
214  * \param[in] hsize the length of the histogram
215  * \param[in] id the point cloud object id (default: cloud)
216  * \param[in] win_width the width of the window
217  * \param[in] win_height the height of the window
218  */
219  template <typename PointT> bool
220  addFeatureHistogram (const pcl::PointCloud<PointT> &cloud,
221  int hsize,
222  const std::string &id = "cloud", int win_width = 640, int win_height = 200);
223 
224  /** \brief Add a histogram feature to screen as a separate window from a cloud containing a single histogram.
225  * \param[in] cloud the PointCloud dataset containing the histogram
226  * \param[in] field_name the field name containing the histogram
227  * \param[in] id the point cloud object id (default: cloud)
228  * \param[in] win_width the width of the window
229  * \param[in] win_height the height of the window
230  */
231  bool
232  addFeatureHistogram (const pcl::PCLPointCloud2 &cloud,
233  const std::string &field_name,
234  const std::string &id = "cloud", int win_width = 640, int win_height = 200);
235 
236  /** \brief Add a histogram feature to screen as a separate window.
237  * \param[in] cloud the PointCloud dataset containing the histogram
238  * \param[in] field_name the field name containing the histogram
239  * \param[in] index the point index to extract the histogram from
240  * \param[in] id the point cloud object id (default: cloud)
241  * \param[in] win_width the width of the window
242  * \param[in] win_height the height of the window
243  */
244  template <typename PointT> bool
245  addFeatureHistogram (const pcl::PointCloud<PointT> &cloud,
246  const std::string &field_name,
247  const int index,
248  const std::string &id = "cloud", int win_width = 640, int win_height = 200);
249 
250  /** \brief Add a histogram feature to screen as a separate window.
251  * \param[in] cloud the PointCloud dataset containing the histogram
252  * \param[in] field_name the field name containing the histogram
253  * \param[in] index the point index to extract the histogram from
254  * \param[in] id the point cloud object id (default: cloud)
255  * \param[in] win_width the width of the window
256  * \param[in] win_height the height of the window
257  */
258  bool
259  addFeatureHistogram (const pcl::PCLPointCloud2 &cloud,
260  const std::string &field_name,
261  const int index,
262  const std::string &id = "cloud", int win_width = 640, int win_height = 200);
263 
264  /** \brief Draws all the plots added by addPlotData() or addHistogramData() till now */
265  void
266  plot ();
267 
268  /** \brief Spins (runs the event loop) the interactor for spin_time amount of time. The name is confusing and will be probably obsolete in the future release with a single overloaded spin()/display() function.
269  * \param[in] spin_time - How long (in ms) should the visualization loop be allowed to run.
270  */
271  void
272  spinOnce (const int spin_time = 1);
273 
274  /** \brief Spins (runs the event loop) the interactor indefinitely. Same as plot() - added to retain the similarity between other existing visualization classes. */
275  void
276  spin ();
277 
278  /** \brief Remove all plots from the window. */
279  void
280  clearPlots();
281 
282  /** \brief Set method for the color scheme of the plot. The plots gets autocolored differently based on the color scheme.
283  * \param[in] scheme the color scheme. Possible values are vtkColorSeries::SPECTRUM, vtkColorSeries::WARM, vtkColorSeries::COOL, vtkColorSeries::BLUES, vtkColorSeries::WILD_FLOWER, vtkColorSeries::CITRUS
284  */
285  void
286  setColorScheme (int scheme);
287 
288  /** \brief get the currently used color scheme
289  * \return[out] the currently used color scheme. Values include WARM, COOL, BLUES, WILD_FLOWER, CITRUS, CUSTOM
290  */
291  int
292  getColorScheme ();
293 
294  /** \brief set/get method for the viewport's background color.
295  * \param[in] r the red component of the RGB color
296  * \param[in] g the green component of the RGB color
297  * \param[in] b the blue component of the RGB color
298  */
299  void
300  setBackgroundColor (const double r, const double g, const double b);
301 
302  /** \brief set/get method for the viewport's background color.
303  * \param [in] color the array containing the 3 component of the RGB color
304  */
305  void
306  setBackgroundColor (const double color[3]);
307 
308  /** \brief set/get method for the viewport's background color.
309  * \return [out] color the array containing the 3 component of the RGB color
310  */
311  double *
312  getBackgroundColor ();
313 
314  /** \brief Set logical range of the X-Axis in plot coordinates
315  * \param[in] min the left boundary of the range
316  * \param[in] max the right boundary of the range
317  */
318  void
319  setXRange (double min, double max);
320 
321  /** \brief Set logical range of the Y-Axis in plot coordinates
322  * \param[in] min the left boundary of the range
323  * \param[in] max the right boundary of the range
324  */
325  void
326  setYRange (double min, double max);
327 
328  /** \brief Set the main title of the plot
329  * \param[in] title the title to set
330  */
331  void
332  setTitle (const char *title);
333 
334  /** \brief Set the title of the X-Axis
335  * \param[in] title the title to set
336  */
337  void
338  setXTitle (const char *title);
339 
340  /** \brief Set the title of the Y-Axis
341  * \param[in] title the title to set
342  */
343  void
344  setYTitle (const char *title);
345 
346  /** \brief Shows the legend of the graph
347  * \param[in] flag pass flag = true for the display of the legend of the graph
348  */
349  void
350  setShowLegend (bool flag);
351 
352  /** \brief set/get method for the window size.
353  * \param[in] w the width of the window
354  * \param[in] h the height of the window
355  */
356  void
357  setWindowSize (int w, int h);
358 
359  /** \brief Set the position in screen coordinates.
360  * \param[in] x where to move the window to (X)
361  * \param[in] y where to move the window to (Y)
362  */
363  void
364  setWindowPosition (int x, int y);
365 
366  /** \brief Set the visualizer window name.
367  * \param[in] name the name of the window
368  */
369  void
370  setWindowName (const std::string &name);
371 
372  /** \brief set/get method for the window size.
373  * \return[in] array containing the width and height of the window
374  */
375  int*
376  getWindowSize () const;
377 
378  /** \brief Return a pointer to the underlying VTK RenderWindow used. */
380  getRenderWindow ();
381 
382  /** \brief Set the view's interactor. */
383  void
384  setViewInteractor (vtkSmartPointer<vtkRenderWindowInteractor> interactor);
385 
386  /** \brief Initialize and Start the view's interactor. */
387  void
388  startInteractor ();
389 
390  /** \brief Render the vtkWindow once. */
391  void renderOnce();
392 
393  /** \brief Returns true when the user tried to close the window */
394  bool
395  wasStopped () const;
396 
397  /** \brief Stop the interaction and close the visualizaton window. */
398  void
399  close ();
400 
401  private:
404  vtkSmartPointer<vtkColorSeries> color_series_; //for automatic coloring
405 
406  //extra state variables
407  int current_plot_; //stores the id of the current (most recent) plot, used in automatic coloring and other state change schemes
408  int win_width_, win_height_;
409  int win_x_, win_y_; //window position according to screen coordinate
410  double bkg_color_[3];
411  std::string win_name_;
412 
413  //####event callback class####
414  struct ExitMainLoopTimerCallback : public vtkCommand
415  {
416  static ExitMainLoopTimerCallback* New ()
417  {
418  return (new ExitMainLoopTimerCallback);
419  }
420  void
421  Execute (vtkObject*, unsigned long event_id, void* call_data) override;
422 
423  int right_timer_id;
424  vtkRenderWindowInteractor *interactor;
425  };
426 
427  struct ExitCallback : public vtkCommand
428  {
429  static ExitCallback* New ()
430  {
431  return new ExitCallback;
432  }
433  void
434  Execute (vtkObject*, unsigned long event_id, void*) override;
435 
436  PCLPlotter *plotter;
437  };
438 
439  /** \brief Set to false if the interaction loop is running. */
440  bool stopped_;
441 
442  /** \brief Callback object enabling us to leave the main loop, when a timer fires. */
444  vtkSmartPointer<ExitCallback> exit_callback_;
445 
446  ////////////////////////////////////IMPORTANT PRIVATE COMPUTING FUNCTIONS////////////////////////////////////////////////////
447  /** \brief computes the value of the polynomial function at val
448  * \param[in] p_function polynomial function
449  * \param[in] value the value at which the function is to be computed
450  */
451  double
452  compute (PolynomialFunction const & p_function, double val);
453 
454  /** \brief computes the value of the rational function at val
455  * \param[in] r_function the rational function
456  * \param[in] value the value at which the function is to be computed
457  */
458  double
459  compute (RationalFunction const & r_function, double val);
460 
461  /** \brief bins the elements in vector data into nbins equally spaced containers and returns the histogram form, ie, computes the histogram for 'data'
462  * \param[in] data data who's frequency distribution is to be found
463  * \param[in] nbins number of bins for the histogram
464  * \param[out] histogram vector of pairs containing the histogram. The first field of the pair represent the middle value of the corresponding bin. The second field denotes the frequency of data in that bin.
465  * \note NaN values will be ignored!
466  */
467  void
468  computeHistogram (std::vector<double> const & data, int const nbins, std::vector<std::pair<double, double> > &histogram);
469  };
470  }
471 }
472 
473 #include <pcl/visualization/impl/pcl_plotter.hpp>
PCL Plotter main class.
Definition: pcl_plotter.h:76
shared_ptr< const PCLPlotter > ConstPtr
Definition: pcl_plotter.h:80
PointCloud represents the base class in PCL for storing collections of 3D points. ...
std::vector< double > PolynomialFunction
A representation of polynomial function.
Definition: pcl_plotter.h:84
shared_ptr< PCLPlotter > Ptr
Definition: pcl_plotter.h:79
std::pair< PolynomialFunction, PolynomialFunction > RationalFunction
A representation of rational function, defined as the ratio of two polynomial functions.
Definition: pcl_plotter.h:88