00001 /* 00002 * Software License Agreement (BSD License) 00003 * 00004 * Point Cloud Library (PCL) - www.pointclouds.org 00005 * Copyright (c) 2010-2011, Willow Garage, Inc. 00006 * Copyright (c) 2012-, Open Perception, Inc. 00007 * 00008 * All rights reserved. 00009 * 00010 * Redistribution and use in source and binary forms, with or without 00011 * modification, are permitted provided that the following conditions 00012 * are met: 00013 * 00014 * * Redistributions of source code must retain the above copyright 00015 * notice, this list of conditions and the following disclaimer. 00016 * * Redistributions in binary form must reproduce the above 00017 * copyright notice, this list of conditions and the following 00018 * disclaimer in the documentation and/or other materials provided 00019 * with the distribution. 00020 * * Neither the name of the copyright holder(s) nor the names of its 00021 * contributors may be used to endorse or promote products derived 00022 * from this software without specific prior written permission. 00023 * 00024 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 00025 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 00026 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 00027 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 00028 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 00029 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 00030 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 00031 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 00032 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 00033 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 00034 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 00035 * POSSIBILITY OF SUCH DAMAGE. 00036 * 00037 */ 00038 #ifndef PCL_VISUALUALIZATION_PCL_PLOTTER_H_ 00039 #define PCL_VISUALUALIZATION_PCL_PLOTTER_H_ 00040 00041 #include <iostream> 00042 #include <vector> 00043 #include <utility> 00044 #include <cfloat> 00045 00046 #include <pcl/visualization/common/common.h> 00047 #include <pcl/point_types.h> 00048 #include <pcl/correspondence.h> 00049 #include <pcl/point_cloud.h> 00050 #include <pcl/common/io.h> 00051 00052 class PCLVisualizerInteractor; 00053 class vtkRenderWindow; 00054 class vtkRenderWindowInteractor; 00055 class vtkContextView; 00056 class vtkChartXY; 00057 class vtkColorSeries; 00058 00059 #include <vtkSmartPointer.h> 00060 #include <vtkCommand.h> 00061 #include <vtkChart.h> 00062 00063 namespace pcl 00064 { 00065 namespace visualization 00066 { 00067 /** \brief PCL Plotter main class. Given point correspondences this class 00068 * can be used to plot the data one against the other and display it on the 00069 * screen. It also has methods for providing plot for important functions 00070 * like histogram etc. Important functions of PCLHistogramVisualizer are 00071 * redefined here so that this single class can take responsibility of all 00072 * plotting related functionalities. 00073 * 00074 * \author Kripasindhu Sarkar 00075 * \ingroup visualization 00076 */ 00077 class PCL_EXPORTS PCLPlotter 00078 { 00079 public: 00080 00081 /**\brief A representation of polynomial function. i'th element of the vector denotes the coefficient of x^i of the polynomial in variable x. 00082 */ 00083 typedef std::vector<double> PolynomialFunction; 00084 00085 /**\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. 00086 */ 00087 typedef std::pair<PolynomialFunction, PolynomialFunction> RationalFunction; 00088 00089 /** \brief PCL Plotter constructor. 00090 * \param[in] name Name of the window 00091 */ 00092 PCLPlotter (char const * name = "PCL Plotter"); 00093 00094 /** \brief Destructor. */ 00095 ~PCLPlotter(); 00096 00097 /** \brief Adds a plot with correspondences in the arrays arrayX and arrayY 00098 * \param[in] array_X X coordinates of point correspondence array 00099 * \param[in] array_Y Y coordinates of point correspondence array 00100 * \param[in] size length of the array arrayX and arrayY 00101 * \param[in] name name of the plot which appears in the legend when toggled on 00102 * \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 00103 * \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 00104 */ 00105 void 00106 addPlotData (double const *array_X, 00107 double const *array_Y, 00108 unsigned long size, 00109 char const * name = "Y Axis", 00110 int type = vtkChart::LINE , 00111 char const *color=NULL); 00112 00113 /** \brief Adds a plot with correspondences in vectors arrayX and arrayY. This is the vector version of the addPlotData function. 00114 * \param[in] array_X X coordinates of point correspondence array 00115 * \param[in] array_Y Y coordinates of point correspondence array 00116 * \param[in] size length of the array arrayX and arrayY 00117 * \param[in] name name of the plot which appears in the legend when toggled on 00118 * \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 00119 * \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 00120 */ 00121 void 00122 addPlotData (std::vector<double> const &array_X, 00123 std::vector<double>const &array_Y, 00124 char const * name = "Y Axis", 00125 int type = vtkChart::LINE, 00126 std::vector<char> const &color = std::vector<char> ()); 00127 00128 /** \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. 00129 * \param[in] name name of the plot which appears in the legend when toggled on 00130 * \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 00131 * \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 00132 */ 00133 void 00134 addPlotData (std::vector<std::pair<double, double> > const &plot_data, 00135 char const * name = "Y Axis", 00136 int type = vtkChart::LINE, 00137 std::vector<char> const &color = std::vector<char>()); 00138 00139 /** \brief Adds a plot based on the given polynomial function and the range in X axis. 00140 * \param[in] p_function A polynomial function which is represented by a vector which stores the coefficients. See description on the typedef. 00141 * \param[in] x_min the left boundary of the range for displaying the plot 00142 * \param[in] x_max the right boundary of the range for displaying the plot 00143 * \param[in] name name of the plot which appears in the legend when toggled on 00144 * \param[in] num_points Number of points plotted to show the graph. More this number, more is the resolution. 00145 * \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 00146 * \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 00147 */ 00148 void 00149 addPlotData (PolynomialFunction const & p_function, 00150 double x_min, double x_max, 00151 char const *name = "Y Axis", 00152 int num_points = 100, 00153 int type = vtkChart::LINE, 00154 std::vector<char> const &color = std::vector<char>()); 00155 00156 /** \brief Adds a plot based on the given rational function and the range in X axis. 00157 * \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. 00158 * \param[in] x_min the left boundary of the range for displaying the plot 00159 * \param[in] x_max the right boundary of the range for displaying the plot 00160 * \param[in] name name of the plot which appears in the legend when toggled on 00161 * \param[in] num_points Number of points plotted to show the graph. More this number, more is the resolution. 00162 * \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 00163 * \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 00164 */ 00165 void 00166 addPlotData (RationalFunction const & r_function, 00167 double x_min, double x_max, 00168 char const *name = "Y Axis", 00169 int num_points = 100, 00170 int type = vtkChart::LINE, 00171 std::vector<char> const &color = std::vector<char>()); 00172 00173 /** \brief Adds a plot based on a user defined callback function representing the function to plot 00174 * \param[in] function a user defined callback function representing the relation y = function(x) 00175 * \param[in] x_min the left boundary of the range for displaying the plot 00176 * \param[in] x_max the right boundary of the range for displaying the plot 00177 * \param[in] name name of the plot which appears in the legend when toggled on 00178 * \param[in] num_points Number of points plotted to show the graph. More this number, more is the resolution. 00179 * \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 00180 * \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 00181 */ 00182 void 00183 addPlotData (double (*function)(double), 00184 double x_min, double x_max, 00185 char const *name = "Y Axis", 00186 int num_points = 100, 00187 int type = vtkChart::LINE, 00188 std::vector<char> const &color = std::vector<char>()); 00189 00190 /** \brief Adds a plot based on a space/tab delimited table provided in a file 00191 * \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 concidered for naming/labling of the plot. The plot-names should not contain any space in between. 00192 * \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 00193 */ 00194 void 00195 addPlotData (char const * filename, 00196 int type = vtkChart::LINE); 00197 00198 /** \brief Bins the elements in vector data into nbins equally spaced containers and plots the resulted histogram 00199 * \param[in] data the raw data 00200 * \param[in] nbins the number of bins for the histogram 00201 * \param[in] name name of this histogram which will appear on legends if toggled on 00202 * \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 00203 */ 00204 void 00205 addHistogramData (std::vector<double> const & data, 00206 int const nbins = 10, 00207 char const * name = "Histogram", 00208 std::vector<char> const &color = std::vector<char>()); 00209 00210 //##PCLHistogramVisulizer methods## 00211 /** \brief Add a histogram feature to screen as a separate window, from a cloud containing a single histogram. 00212 * \param[in] cloud the PointCloud dataset containing the histogram 00213 * \param[in] hsize the length of the histogram 00214 * \param[in] id the point cloud object id (default: cloud) 00215 * \param[in] win_width the width of the window 00216 * \param[in] win_height the height of the window 00217 */ 00218 template <typename PointT> bool 00219 addFeatureHistogram (const pcl::PointCloud<PointT> &cloud, 00220 int hsize, 00221 const std::string &id = "cloud", int win_width = 640, int win_height = 200); 00222 00223 /** \brief Add a histogram feature to screen as a separate window from a cloud containing a single histogram. 00224 * \param[in] cloud the PointCloud dataset containing the histogram 00225 * \param[in] field_name the field name containing the histogram 00226 * \param[in] id the point cloud object id (default: cloud) 00227 * \param[in] win_width the width of the window 00228 * \param[in] win_height the height of the window 00229 */ 00230 bool 00231 addFeatureHistogram (const pcl::PCLPointCloud2 &cloud, 00232 const std::string &field_name, 00233 const std::string &id = "cloud", int win_width = 640, int win_height = 200); 00234 00235 /** \brief Add a histogram feature to screen as a separate window. 00236 * \param[in] cloud the PointCloud dataset containing the histogram 00237 * \param[in] field_name the field name containing the histogram 00238 * \param[in] index the point index to extract the histogram from 00239 * \param[in] id the point cloud object id (default: cloud) 00240 * \param[in] win_width the width of the window 00241 * \param[in] win_height the height of the window 00242 */ 00243 template <typename PointT> bool 00244 addFeatureHistogram (const pcl::PointCloud<PointT> &cloud, 00245 const std::string &field_name, 00246 const int index, 00247 const std::string &id = "cloud", int win_width = 640, int win_height = 200); 00248 00249 /** \brief Add a histogram feature to screen as a separate window. 00250 * \param[in] cloud the PointCloud dataset containing the histogram 00251 * \param[in] field_name the field name containing the histogram 00252 * \param[in] index the point index to extract the histogram from 00253 * \param[in] id the point cloud object id (default: cloud) 00254 * \param[in] win_width the width of the window 00255 * \param[in] win_height the height of the window 00256 */ 00257 bool 00258 addFeatureHistogram (const pcl::PCLPointCloud2 &cloud, 00259 const std::string &field_name, 00260 const int index, 00261 const std::string &id = "cloud", int win_width = 640, int win_height = 200); 00262 00263 /** \brief Draws all the plots added by addPlotData() or addHistogramData() till now */ 00264 void 00265 plot (); 00266 00267 /** \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. 00268 * \param[in] spin_time - How long (in ms) should the visualization loop be allowed to run. 00269 */ 00270 void 00271 spinOnce (const int spin_time = 1); 00272 00273 /** \brief Spins (runs the event loop) the interactor indefinitely. Same as plot() - added to retain the similarity between other existing visualization classes. */ 00274 void 00275 spin (); 00276 00277 /** \brief Remove all plots from the window. */ 00278 void 00279 clearPlots(); 00280 00281 /** \brief Set method for the color scheme of the plot. The plots gets autocolored differently based on the color scheme. 00282 * \param[in] scheme the color scheme. Possible values are vtkColorSeries::SPECTRUM, vtkColorSeries::WARM, vtkColorSeries::COOL, vtkColorSeries::BLUES, vtkColorSeries::WILD_FLOWER, vtkColorSeries::CITRUS 00283 */ 00284 void 00285 setColorScheme (int scheme); 00286 00287 /** \brief get the currently used color scheme 00288 * \return[out] the currently used color scheme. Values include WARM, COOL, BLUES, WILD_FLOWER, CITRUS, CUSTOM 00289 */ 00290 int 00291 getColorScheme (); 00292 00293 /** \brief set/get method for the viewport's background color. 00294 * \param[in] r the red component of the RGB color 00295 * \param[in] g the green component of the RGB color 00296 * \param[in] b the blue component of the RGB color 00297 */ 00298 void 00299 setBackgroundColor (const double r, const double g, const double b); 00300 00301 /** \brief set/get method for the viewport's background color. 00302 * \param [in] color the array containing the 3 component of the RGB color 00303 */ 00304 void 00305 setBackgroundColor (const double color[3]); 00306 00307 /** \brief set/get method for the viewport's background color. 00308 * \return [out] color the array containing the 3 component of the RGB color 00309 */ 00310 double * 00311 getBackgroundColor (); 00312 00313 /** \brief Set logical range of the X-Axis in plot coordinates 00314 * \param[in] min the left boundary of the range 00315 * \param[in] max the right boundary of the range 00316 */ 00317 void 00318 setXRange (double min, double max); 00319 00320 /** \brief Set logical range of the Y-Axis in plot coordinates 00321 * \param[in] min the left boundary of the range 00322 * \param[in] max the right boundary of the range 00323 */ 00324 void 00325 setYRange (double min, double max); 00326 00327 /** \brief Set the main title of the plot 00328 * \param[in] title the title to set 00329 */ 00330 void 00331 setTitle (const char *title); 00332 00333 /** \brief Set the title of the X-Axis 00334 * \param[in] title the title to set 00335 */ 00336 void 00337 setXTitle (const char *title); 00338 00339 /** \brief Set the title of the Y-Axis 00340 * \param[in] title the title to set 00341 */ 00342 void 00343 setYTitle (const char *title); 00344 00345 /** \brief Shows the legend of the graph 00346 * \param[in] flag pass flag = true for the display of the legend of the graph 00347 */ 00348 void 00349 setShowLegend (bool flag); 00350 00351 /** \brief set/get method for the window size. 00352 * \param[in] w the width of the window 00353 * \param[in] h the height of the window 00354 */ 00355 void 00356 setWindowSize (int w, int h); 00357 00358 /** \brief set/get method for the window size. 00359 * \return[in] array containing the width and height of the window 00360 */ 00361 int* 00362 getWindowSize (); 00363 00364 /** \brief Return a pointer to the underlying VTK RenderWindow used. */ 00365 vtkSmartPointer<vtkRenderWindow> 00366 getRenderWindow (); 00367 00368 /** \brief Set the view's interactor. */ 00369 void 00370 setViewInteractor (vtkSmartPointer<vtkRenderWindowInteractor> interactor); 00371 00372 /** \brief Initialize and Start the view's interactor. */ 00373 void 00374 startInteractor (); 00375 00376 /** \brief Render the vtkWindow once. */ 00377 void renderOnce(); 00378 00379 /** \brief Returns true when the user tried to close the window */ 00380 bool 00381 wasStopped () const; 00382 00383 /** \brief Stop the interaction and close the visualizaton window. */ 00384 void 00385 close (); 00386 00387 private: 00388 vtkSmartPointer<vtkContextView> view_; 00389 vtkSmartPointer<vtkChartXY> chart_; 00390 vtkSmartPointer<vtkColorSeries> color_series_; //for automatic coloring 00391 00392 //extra state variables 00393 int current_plot_; //stores the id of the current (most recent) plot, used in automatic coloring and other state change schemes 00394 int win_width_, win_height_; 00395 double bkg_color_[3]; 00396 00397 //####event callback class#### 00398 struct ExitMainLoopTimerCallback : public vtkCommand 00399 { 00400 static ExitMainLoopTimerCallback* New () 00401 { 00402 return (new ExitMainLoopTimerCallback); 00403 } 00404 virtual void 00405 Execute (vtkObject*, unsigned long event_id, void* call_data); 00406 00407 int right_timer_id; 00408 #if ((VTK_MAJOR_VERSION == 5) && (VTK_MINOR_VERSION <= 4)) 00409 PCLVisualizerInteractor *interactor; 00410 #else 00411 vtkRenderWindowInteractor *interactor; 00412 #endif 00413 }; 00414 00415 struct ExitCallback : public vtkCommand 00416 { 00417 static ExitCallback* New () 00418 { 00419 return new ExitCallback; 00420 } 00421 virtual void 00422 Execute (vtkObject*, unsigned long event_id, void*); 00423 00424 PCLPlotter *plotter; 00425 }; 00426 00427 /** \brief Set to false if the interaction loop is running. */ 00428 bool stopped_; 00429 00430 /** \brief Callback object enabling us to leave the main loop, when a timer fires. */ 00431 vtkSmartPointer<ExitMainLoopTimerCallback> exit_loop_timer_; 00432 vtkSmartPointer<ExitCallback> exit_callback_; 00433 00434 ////////////////////////////////////IMPORTANT PRIVATE COMPUTING FUNCTIONS//////////////////////////////////////////////////// 00435 /** \brief computes the value of the polynomial function at val 00436 * \param[in] p_function polynomial function 00437 * \param[in] value the value at which the function is to be computed 00438 */ 00439 double 00440 compute (PolynomialFunction const & p_function, double val); 00441 00442 /** \brief computes the value of the rational function at val 00443 * \param[in] r_function the rational function 00444 * \param[in] value the value at which the function is to be computed 00445 */ 00446 double 00447 compute (RationalFunction const & r_function, double val); 00448 00449 /** \brief bins the elements in vector data into nbins equally spaced containers and returns the histogram form, ie, computes the histogram for 'data' 00450 * \param[in] data data who's frequency distribution is to be found 00451 * \param[in] nbins number of bins for the histogram 00452 * \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. 00453 */ 00454 void 00455 computeHistogram (std::vector<double> const & data, int const nbins, std::vector<std::pair<double, double> > &histogram); 00456 }; 00457 } 00458 } 00459 00460 #include <pcl/visualization/impl/pcl_plotter.hpp> 00461 00462 #endif /* PCL_VISUALUALIZATION_PCL_PLOTTER_H_ */ 00463
Except where otherwise noted, the PointClouds.org web pages are licensed under Creative Commons Attribution 3.0.
Pages generated on Wed Mar 25 00:26:36 2015