用pcl读ply文件_PCL之轨迹绘制(二)

之前学习点云库做一些简单的应用都是直接复制demo的代码,然后把导入文件改一下,今天尝试自己写一些程序,结果错漏百出,难受的早上,不过坚持了下来,求夸~~~

这个主要是一个简单的绘制轨迹的教程,绘制轨迹只需要两个东西,旋转R和平移T,只要我们能够得到这两个东西,再结合初始坐标点,利用点云库里面的Visualization模块中的addLine函数就可以实现轨迹的绘制了。

这里为了简单,我们直接采用模拟的数据以及模拟的旋转矩阵。先一步一步讲解流程,最后再附上源码~~

1.生成点云数据

```c++  //为了方便,我们在这里只生成一个点

pcl::PointCloud::Ptr cloud1(new pcl::PointCloud);    
cloud1->width  = 1;     
cloud1->height = 1;     
cloud1->points.resize(cloud1->width * cloud1->height);
cloud1->points[0].x = 1;
cloud1->points[0].y = 1;
cloud1->points[0].z = 1;

cout << "cloud1 size " << cloud1->points.size()<<endl;

2.模拟生成旋转矩阵

2.模拟生成旋转矩阵

```c++
//模拟一个旋转矩阵,为了简单,这里只是用一个,每次迭代都使用这个矩阵,效果是x方向一次增加1个单位长度
//我们知道旋转矩阵形式如下[ R t
                        0 1] 下一篇文章会讲解一下这些的由来
     Eigen::Matrix4f transform_1 ;
     transform_1 << 1 , 0 , 0 , 1 ,
                    0 , 1 , 0 , 0 ,
                    0 , 0 , 1 , 0 ,
                    0 , 0 , 0 , 1 ;
    cout << transform_1 <<endl;

3.接下来会用来 Register模块中的transforms函数来实现点的转变

```c++     //将矩阵进行变化    
  pcl::PointCloud::Ptr cloud2(new pcl::PointCloud);    
  pcl::transformPointCloud (cloud1, cloud2, transform_1);  
  cout << "original cloud1 points "<< cloud1->points[0].x<<" "<points[0].y <<"    "<points[0].z<points[0].x<<" "<points[0].y <<"    "<points[0].z<<endl;    
  cout<<"----------------------------------------"<<endl;

吐槽:不知道为啥B乎每次都把我们格式弄坏了。。。

4.最后会用到Visualization模块中的PCLVisualizer类显示窗口

pcl::visualization::PCLVisualizer::Ptr viewer(new pcl::visualization::PCLVisualizer ("3D Viewer"));
 viewer->setBackgroundColor (0, 0, 0);
 viewer->addText("Trajector", 10, 10, "v1 text");
 viewer->addCoordinateSystem (1.0);
 viewer->initCameraParameters ();
 int line_numeber = 0;
 char str[25];//这一个的作用是为了下面给每条线段 1 个唯一的id符,不然的画只能画出一条线


 while(!viewer->wasStopped()){

 viewer->spinOnce (1000);//这一句很重要,一开始没有写这一句,导致窗口一直没有显示,用于更新屏幕
 line_numeber++;//依次增加,从而实现id号的不同
 sprintf(str, "%d", line_numeber);//将数字转化为字符串,addLine需要,addLine函数定义在下面
 viewer->addLine<pcl::PointXYZ> (cloud1->points[0], cloud2->points[0], str);
 *cloud1 = *cloud2;//将上一步的点云给另外一个
 pcl::transformPointCloud (*cloud1, *cloud2, transform_1);  
 //cout语句用于测试
 cout << "cloud1 points "<< cloud1->points[0].x<<" "<<cloud1->points[0].y <<" "<<cloud1->points[0].z<<endl;
 cout << "cloud1 points "<< cloud2->points[0].x<<" "<<cloud2->points[0].y <<" "<<cloud2->points[0].z<<endl;  
     cout<<"----------------------------------------"<<endl;    
}

5.最后把头文件给你就组成完整的程序了

#include <iostream>
#include<pcl/visualization/pcl_visualizer.h> 
#include<pcl/registration/transforms.h> #include<stdio.h> //这两个std文件没有作用,因为一开始想用到itoa(),将整形转化为字符的函数,后来发现这 
#include <stdlib.h>//这个函数只有在window下面才存在 
using namespace std;

注意点:

1.

void pcl::visualization::PCLVisualizer::spinOnce(int time =1 ; bool force_redraw = false )
Spin once method.
Calls the interactor and updates the screen once.
  • Parameters

[in] time- How long (in ms) should the visualization loop be allowed to run.
[in] force_redraw- if false it might return without doing anything if the interactor's framerate does not require a redraw yet.

while

viewer->spinOnce (100);一定要加这一句,否则什么现象也没有

2.

bool pcl::visualization::PCLVisualizer::addLine(const P1 & pt1, const P2 & ptr2,const std::string& id ="line",int viewpoint = 0 )
Add a line segment from two points.
  • Parameters

[in]pt1the first (start) point on the line
[in]pt2the second (end) point on the line
[in]idthe line id/name (default: "line")
[in]viewport(optional) the id of the new viewport (default: 0)
Note:每一条线都需要有自己的id号


总结:还是要多多自己敲代码,复制粘贴一时爽,真定编码火葬场~~~B乎的格式和Typora很不一样....,还是CSDN好。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值