# PCLPlotter

PCLPlotter provides a very straightforward and easy interface for plotting graphs. One can visualize all sort of important plots - from polynomial functions to histograms - inside the library without going to any other software (like MATLAB). Please go through the documentation when some specific concepts are introduced in this tutorial to know the exact method signatures.

The code for the visualization of a plot are usually as simple as the following snippet.

#include<vector>
#include<iostream>
#include<utility>

#include<pcl/visualization/pcl_plotter.h>
//...

using namespace std;

int
main ()
{
//defining a plotter
pcl::visualization::PCLPlotter * plotter = new PCLPlotter ();

//defining the polynomial function, y = x^2. Index of x^2 is 1, rest is 0
vector<double> func1 (3,0);
func1[2] = 1;

//adding the polynomial func1 to the plotter with [-10, 10] as the range in X axis and "y = x^2" as title
plotter->addPlotData (func1, -10, 10, "y = x^2");

//display the plot, DONE!
plotter->plot ();

return 0;
}


If this program is compiled and run, you will get the following output

## Basic code structure

The following snippet shows the basic structure of code for using PCLPlotter

...
//1. define a plotter. Change the colorscheme if you want some different colorscheme in auto-coloring.
pcl::visualization::PCLPlotter *plotter = new PCLPlotter ("My Plotter");

...

...
//3. add some properties if required
plotter->setWindowSize (900, 600);
plotter->setYTitle ("this is my own function");

...
//4. display the plot
plotter->plot ()


All the subsequent sections will elaborate the above concept in detail.

# Auto-coloring

You have the choice to add your own color to the plot in addPlotData*() functions. But if left empty, the plotter will auto-color depending upon a color-scheme. The default color-scheme is vtkColorSeries::SPECTRUM which contains 7 different (normal) hues over the entire spectrum. The other values are vtkColorSeries::WARM, vtkColorSeries::COOL, vtkColorSeries::BLUES, vtkColorSeries::WILD_FLOWER, vtkColorSeries::CITRUS. You can change the colorscheme by setColorScheme () function. To reflect the effect of the color-scheme to all the plots call this function before calling any addPlotData*() functions.

# Different types of plot input

Have a look at the addPlotData() functions in the documentation for their detailed signatures. The prototypes pretty much tell about their functionalities. The following subsections contains some of the important input method of the plot.

## Point-Correspondences

This the most fundamental way of providing input. Provide the point correspondences, that is (x,y) coordinates, for the plot using a vector<pair> in addPlotData

...
vector<pair<double, double> > data;
populateData (data);
...


The other ways of input for point correspondences are two arrays of same length denoting the X and Y values of the correspondences.

## Table

This is same as the previous one except the fact that the user stores the correspondences in a text file in the form of an space delimited table. This forms a substitute for the plotting using MS Excel. A very simple executable (without decoration) which performs the functionalities of MS Excel Plotter will be the following.

#include<pcl/visualization/pcl_plotter.h>

int
main (int argc, char ** argv)
{
pcl::visualization::PCLPlotter * plotter = new PCLPlotter ();
plotter->plot ();

return 0;
}


## Polynomial and Rational Functions

Polynomial are defined in terms of vector of coefficients and Rational functions are defined in terms of pair of polynomial (pair of numerator and denominator) . See the definition in the documentation. The following snippet plots the function y = 1/x

...
vector<double> func1 (1,0);
func1[0] = 1;
vector<double> func2 (2,0);
func1[1] = 1;

plotter->addPlotData (std::make_pair (func1, func2),-10, 10, "y = 1/x");
...


## A custom explicit function

User can specify a custom function, f depicting the relation: Y = f(X) in the form of a callback

...
double
identity (double val)
{
return val;
}
...

...
...


# Adding other properties and decorations

One can add other properties of the plot like title, legends, background colours etc. You can call these functions at any time before any display (plot()/spin*()) function call.

...
plotter->setTitle ("My plot"); //global title
plotter->setXTitle ("degrees");
plotter->setYTitle ("cos");
plotter->setShowLegend (true); //show legends
...
plotter->plot ();
...


# Other Functionalities

PCLPlotter provides few other important functionalities other than plotting given a well defined plots and correspondences. These includes a histogram plotting functions and all functionalities of the legacy class PCLHistogramVisualizer.

## ‘Plotting’ Histogram

PCLPlotter provides a very convenient MATLAB like histogram plotting function (hist() in MATLAB). It takes raw data and bins them according to their frequency and plot them as bar chart.

...

vector<double> freqdata = generateNomalDistData ();

plotter->addHistogramData (freqdata,10); //number of bins are 10

plotter->plot ();
...


## PCLHistogramVisualizer functions

All functionalities of PCLHistogramVisualizer has been rewritten and added to the plotter with their signature retained. See the documentation for method details.

# Display

To display all the plots added use the simple function - plot(). PCLPlotter is also provided with the legacy spin*() functions which can be used for animations or updating the plots with time. The following snippet shows the functionality.

...

//data and callback defined here
...

plotter->addPlotData (func1, -10, 10, "y = x^2");
plotter->spinOnce (2000);    //display the plot for 2 seconds

plotter->clearPlots ();
plotter->spinOnce (2000);

plotter->clearPlots ();
plotter->spinOnce (2000);
...


# Demo

Following is a complete example depicting many usage of the Plotter. Copy it into a file named pcl_plotter_demo.cpp.

  1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 /* \author Kripasindhu Sarkar */ #include #include #include #include #include //for abs() using namespace std; using namespace pcl::visualization; void generateData (double *ax, double *acos, double *asin, int numPoints) { double inc = 7.5 / (numPoints - 1); for (int i = 0; i < numPoints; ++i) { ax[i] = i*inc; acos[i] = cos (i * inc); asin[i] = sin (i * inc); } } //.....................callback functions defining Y= f(X).................... double step (double val) { if (val > 0) return (double) (int) val; else return (double) ((int) val - 1); } double identity (double val) { return val; } //............................................................................ int main (int argc, char * argv []) { //defining a plotter PCLPlotter *plotter = new PCLPlotter ("My Plotter"); //setting some properties plotter->setShowLegend (true); //generating point correspondances int numPoints = 69; double ax[100], acos[100], asin[100]; generateData (ax, acos, asin, numPoints); //adding plot data plotter->addPlotData (ax, acos, numPoints, "cos"); plotter->addPlotData (ax, asin, numPoints, "sin"); //display for 2 seconds plotter->spinOnce (3000); plotter->clearPlots (); //...................plotting implicit functions and custom callbacks.................... //make a fixed axis plotter->setYRange (-10, 10); //defining polynomials vector func1 (1, 0); func1[0] = 1; //y = 1 vector func2 (3, 0); func2[2] = 1; //y = x^2 plotter->addPlotData (std::make_pair (func1, func2), -10, 10, "y = 1/x^2", 100, vtkChart::POINTS); plotter->spinOnce (2000); plotter->addPlotData (func2, -10, 10, "y = x^2"); plotter->spinOnce (2000); //callbacks plotter->addPlotData (identity, -10, 10, "identity"); plotter->spinOnce (2000); plotter->addPlotData (abs, -10, 10, "abs"); plotter->spinOnce (2000); plotter->addPlotData (step, -10, 10, "step", 100, vtkChart::POINTS); plotter->spinOnce (2000); plotter->clearPlots (); //........................A simple animation.............................. vector fsq (3, 0); fsq[2] = -100; //y = x^2 while (!plotter->wasStopped ()) { if (fsq[2] == 100) fsq[2] = -100; fsq[2]++; char str[50]; sprintf (str, "y = %dx^2", (int) fsq[2]); plotter->addPlotData (fsq, -10, 10, str); plotter->spinOnce (100); plotter->clearPlots (); } return 1; } 

## Compiling and running the program

 1 2 3 4 5 6 7 8 cmake_minimum_required(VERSION 2.8 FATAL_ERROR) project(pcl_plotter) find_package(PCL 1.7 REQUIRED COMPONENTS common visualization) include_directories(${PCL_INCLUDE_DIRS}) link_directories(${PCL_LIBRARY_DIRS}) add_definitions(${PCL_DEFINITIONS}) add_executable(pcl_plotter pcl_plotter_demo.cpp) target_link_libraries(pcl_plotter${PCL_LIBRARIES}) 
$cmake .$ make