可视化绘图库:Pangolin 使用方式

可视化绘图库: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::DataLogstd::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);
  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

泠山

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值