PCL库使用addLine实现轨迹绘制

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

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

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

1.生成点云数据

 //为了方便,我们在这里只生成一个点
 
  pcl::PointCloud<pcl::PointXYZ>::Ptr cloud1(new pcl::PointCloud<pcl::PointXYZ>);
    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.模拟生成旋转矩阵

//模拟一个旋转矩阵,为了简单,这里只是用一个,每次迭代都使用这个矩阵,效果是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函数来实现点的转变

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

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->wasStopped()){    
    // viewer->spinOnce (100);
 }

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号


总结:还是要多多自己敲代码,复制粘贴一时爽,真正编码火葬场~~~

  • 11
    点赞
  • 28
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
在Ubuntu上使用pcl,你可以按照以下步骤进行操作: 1. 首先,使用命令`dpkg -S pcl`查看pcl包文件的安装位置,包括头文件和文件。你可以进入文件路径下查看具体安装的版本。 2. 下载点云python包的GitHub链接,并按照该repo给出的命令从源码生成python binding文件并安装包。例如,使用命令`python setup.py build_ext -i python setup.py install`。 3. 在安装过程中可能会遇到ld找不到响应版本的链接的错误。如果你的系统自动安装了pcl1.8.1和vtk7.1,而setup.py中指定的是vtk7.0,你需要将setup.py中的vtk_version改为7.1,然后再执行以上命令。 4. 安装完成后,你可以在指定的安装路径下找到生成的python binding文件和其他相关文件。 另外,如果你需要在C++项目中使用pcl,你可以使用CMake来指定pcl的路径。以下是一个示例的CMakeLists.txt文件: ``` cmake_minimum_required(VERSION 2.6) project(pcl_test) find_package(PCL 1.2 REQUIRED) include_directories(${PCL_INCLUDE_DIRS}) link_directories(${PCL_LIBRARY_DIRS}) add_definitions(${PCL_DEFINITIONS}) add_executable(pcl_test pcl_test.cpp) target_link_libraries(pcl_test ${PCL_LIBRARIES}) install(TARGETS pcl_test RUNTIME DESTINATION bin) ``` 你可以根据需要修改PCL的版本和路径,然后使用cmake命令进行编译和安装。 #### 引用[.reference_title] - *1* *2* [ubuntu18.04上点云PCL 使用初探](https://blog.csdn.net/richard_m_yang/article/details/128225927)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down28v1,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [ubuntu pcl 点云使用](https://blog.csdn.net/qiqiqiqi0000/article/details/114378491)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down28v1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值