可视化绘图库:Pangolin 使用方式
Refence:
【1】Pangolin官网: https://github.com/stevenlovegrove/Pangolin.
【2】Pangolin官方示例: https://github.com/stevenlovegrove/Pangolin/tree/master/examples.
比较好的相关教程,可能会更清楚一些:
【1】SLAM可视化绘图库——Pangolin教程(三): https://blog.csdn.net/weixin_43991178/article/details/105174734.
std::thread([&]() {
// Create OpenGL window in single line 创建一个新窗口
pangolin::CreateWindowAndBind("imu gyro rate", 640, 480);
// Data logger object
pangolin::DataLog log;
// Optionally add named labels
std::vector<std::string> labels;
labels.push_back(std::string("rx"));
labels.push_back(std::string("ry"));
labels.push_back(std::string("rz"));
log.SetLabels(labels);
const float tinc = 0.02f;
// OpenGL 'view' of data. We might have many views of the same data.
pangolin::Plotter plotter(&log, 0.0f, 4.0f * (float)M_PI / tinc, -4.0f, 4.0f, (float)M_PI / (4.0f * tinc),
0.2f);
plotter.SetBounds(0.0, 1.0, 0.0, 1.0);
plotter.Track("$i");//坐标轴自动滚动
// Add some sample annotations to the plot(为区域着色)
plotter.AddMarker(pangolin::Marker::Vertical, 25 * M_PI, pangolin::Marker::LessThan,
pangolin::Colour::Blue().WithAlpha(0.2f));
plotter.AddMarker(pangolin::Marker::Horizontal, 3, pangolin::Marker::GreaterThan,
pangolin::Colour::Red().WithAlpha(0.2f));
plotter.AddMarker(pangolin::Marker::Horizontal, 3, pangolin::Marker::Equal,
pangolin::Colour::Green().WithAlpha(0.2f));
pangolin::DisplayBase().AddDisplay(plotter);
// Default hooks for exiting (Esc) and fullscreen (tab)
while (!pangolin::ShouldQuit()) {
// Clear entire screen 清除屏幕并激活要渲染到的视图
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
if (show_g_m.flag) {
log.Log(show_g_m.anglerate_x, show_g_m.anglerate_y, show_g_m.anglerate_z);
show_g_m.flag = false;
}
// Swap frames and Process Events
pangolin::FinishFrame();
}
pangolin::QuitAll();
}).detach();
其中 show_g_m
为自己建立的 class, flag 收到更新时变为 true,打印曲线。
代码解析
// Data logger object
pangolin::DataLog log;
// Optionally add named labels
std::vector<std::string> labels;
labels.push_back(std::string("rx"));
labels.push_back(std::string("ry"));
labels.push_back(std::string("rz"));
log.SetLabels(labels);
在 pangolin
中,待可视化的数据全部存储在 pangolin::DataLog
对象中,因此我们首先创建了一个 pangolin::DataLog
对象,并使用对应的成员函数 SetLabels()
设置对应数据的名称标签。
const float tinc = 0.02f;
// OpenGL 'view' of data. We might have many views of the same data.
pangolin::Plotter plotter(&log, 0.0f, 4.0f * (float)M_PI / tinc, -4.0f, 4.0f,
(float)M_PI / (4.0f * tinc), 0.2f);
plotter.SetBounds(0.0, 1.0, 0.0, 1.0);
plotter.Track("$i");//坐标轴自动滚动
数据的可视化是通过对象 pangolin::Plotter
来实现的,该对象的构造参数的第一个参数为需要绘制的 pangolin::DataLog
对象(1);随后
4
4
4 个参数依次为 Plotter
的 左边界(2)、右边界(3)、下边界(4)、上边界(5),即 Plotter
中
x
x
x 轴
y
y
y 轴的范围;最后两个参数依次为
x
x
x 轴(6)和
y
y
y 轴(7)的坐标轴刻度大小。
// Add some sample annotations to the plot(为区域着色)
plotter.AddMarker(pangolin::Marker::Vertical, 25 * M_PI, pangolin::Marker::LessThan,
pangolin::Colour::Blue().WithAlpha(0.2f));
plotter.AddMarker(pangolin::Marker::Horizontal, 3, pangolin::Marker::GreaterThan,
pangolin::Colour::Red().WithAlpha(0.2f));
plotter.AddMarker(pangolin::Marker::Horizontal, 3, pangolin::Marker::Equal,
pangolin::Colour::Green().WithAlpha(0.2f));
随后该程序在 Plotter
中使用 plotter
的成员函数 AddMarker
添加一些标志块的功能,该函数的参数依次为:标志块的方向(1),标志块的数值(2),标志块的判别方式(3),标志块的颜色(4)。
例如,第一个标志块的方向为垂直方向,数值为
25
π
25\pi
25π,判断方式为小于,颜色为带透明度的蓝色,因此在程序的运行结果中会发现
x
x
x 轴坐标小于
50
π
50\pi
50π 的范围都被标记为了透明的蓝色。同理第二个 Marker
将
y
y
y 轴大于
3
3
3 的区域标记为了红色,第三个 Marker
由于是等于,因此其只将
y
=
3
y=3
y=3 这一条线标记为了绿色。图中可以分别看到 蓝色块、红色块 和 绿色线。
pangolin::DisplayBase().AddDisplay(plotter);
这时会将构建好的 plotter
添加到 Display
中:
// Default hooks for exiting (Esc) and fullscreen (tab)
while (!pangolin::ShouldQuit()) {
// Clear entire screen 清除屏幕并激活要渲染到的视图
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
if (show_g_m.flag) {
log.Log(show_g_m.anglerate_x, show_g_m.anglerate_y, show_g_m.anglerate_z);
show_g_m.flag = false;
}
// Swap frames and Process Events
pangolin::FinishFrame();
}
在帧循环中,只需要使用 DataLog::Log()
函数不断更新 DataLog
中的数据,pangolin
就会根据我们之前创建的 plotter
自动在视窗中绘制数据。
如果需要在程序内添加循环的可视化,可将 pangolin::DataLog
,std::vector<std::string>
和 static pangolin::Plotter
改为全局或静态:
static pangolin::DataLog log;
static std::vector<std::string> labels;
static pangolin::Plotter plotter(&log, 0.0f, 4.0f * (float)M_PI / tinc, -4.0f, 0.0f,
(float)M_PI / (4.0f * tinc),0.2f);