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