PCLPlotter可视化特征直方图
1. PCLPlotter
- PCL官方文档: https://pcl.readthedocs.io/projects/tutorials/en/master/pcl_plotter.html#basic-code-structure
- PCLPlotter 提供了一个直接简单的绘图接口,可以绘制许多类型的二维图形,包括多项式函数和特征直方图,比如 FPFH (快速特征直方图)。
- 利用 PCLPlotter 绘制图形分以下4步:
- 第一步:声明绘图对象 PCLPlotter;
- 第二步:利用 addPlotData() 函数,添加绘图所需的函数或数据;
- 第三步:添加窗口特性,可以调整窗口大小和标题;(非必须步骤)
- 第四步:显示绘图。
2. 绘制函数图形示例代码
/* author Kripasindhu Sarkar */
#include <pcl/visualization/pcl_plotter.h>
#include <iostream>
#include <vector>
#include <utility>
#include <cmath> //for std::abs()
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()
{
//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); //Calls the interactor and updates the screen once.
plotter->clearPlots(); //Remove all plots from the window.
//...................plotting implicit functions and custom callbacks....................
//make a fixed axis
plotter->setYRange(-10, 10);
//defining polynomials
std::vector<double> func1(1, 0);
func1[0] = 1; //y = 1
std::vector<double> 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(std::abs, -10, 10, "abs");
plotter->spinOnce(2000);
plotter->addPlotData(step, -10, 10, "step", 100, vtkChart::POINTS);
plotter->spinOnce(2000);
plotter->clearPlots();
//........................A simple animation..............................
std::vector<double> 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;
}