win10环境下PCL安装和配置回顾(一)

2020年博主有写过几篇关于PCL的博客,这边想先回顾下,再增加点新内容。之前写的博客专栏如下链接。

https://blog.csdn.net/jiugeshao/category_11993239.html?spm=1001.2014.3001.5482https://blog.csdn.net/jiugeshao/category_11993239.html?spm=1001.2014.3001.5482

一.PCL官网资料

之前多篇博客中都提过,要善于利用官方去学习一个新的东西,好的东西,其官网大多必然讲些详细。现在官网的一些资源都会放到github上,比如之前博主讲到的OpenVINO。

OpenVINO使用介绍(一)_竹叶青lvye的博客-CSDN博客_openvino

OpenVINO示例介绍_竹叶青lvye的博客-CSDN博客_openvino 示例

PLC的官网链接如下:

Point Cloud Library | The Point Cloud Library (PCL) is a standalone, large scale, open project for 2D/3D image and point cloud processing.

 Docs入口是API的介绍文档,PCL里模块分的很清楚,若想直接看对应模块的API,也可以直接从点击大方对应图标进入。

Tutorials是详细的手册介绍,里面还会有一些示例,方便整体知道如何去使用,了解一些算子原理

github网址如下

 GitHub - PointCloudLibrary/pcl: Point Cloud Library (PCL)

 可看到最新的版本是1.12.1

tags可以下载到常用的平台下源码编译出来的安装文件,如果没有满足自己平台的,那么可以自己去编译下。(源码编译和交叉编译方法可以参考如下两篇博客),这里不再赘叙。

Ubuntu下源码编译OpenCV静态库及使用(勾选WITH_QT)_竹叶青lvye的博客-CSDN博客_opencv静态库使用

Ubuntu下交叉编译OpenCV(WITH_QT)_竹叶青lvye的博客-CSDN博客_opencv编译with_qt

二. PCL安装和配置

这一步主要可参考之前的博客:

vs2017配置PCL1.8.1 QT5.12.1(一)- 配置步骤_竹叶青lvye的博客-CSDN博客_vs2017配置pcl

 这里博主选择了新的版本,vs2019 + PCL1.12.1

 这篇博客需要补充的地方是,中间在输入目标链接库名字时,可以用python脚本批量去获得lib库的名字。

 脚本如下:

# author:"jiugeshao"
# data:2022/9/3 2:16


import os
def ListFilesToTxt(Currentdir, outfile, nameExtentisons, isIncludeSubDir, isDebug):
    """
    :param Currentdir: 需要统计的目录
    :param outfile: 需要输出到的txt文件
    :param nameExtentisons: 后缀列表
    :param isIncludeSubDir: 是否需要统计子目录
    :return:
    """
    exts = nameExtentisons.split(" ")
    files = os.listdir(Currentdir)
    for name in files:
        fullname = os.path.join(Currentdir, name)
        if (os.path.isdir(fullname) & isIncludeSubDir):
            ListFilesToTxt(fullname, outfile, nameExtentisons, isIncludeSubDir, isDebug)
        else:
            for ext in exts:
                if (name.endswith(ext)):
                    if(isDebug == True):
                        if("gd" in name or "d.lib" in name):
                            outfile.write(name + "\n")
                            break
                    else:
                        if("gd" not in name or "d.lib" in name):
                            outfile.write(name + "\n")
                            break



def Read(Currentdir, outfile, isDebug):
    """
    :param Currentdir:需要统计的目录
    :param outfile:需要输出到的txt文本
    :return:
    """
    nameExtensons = ".lib"  #若要支持多种后缀统计,可以空格隔开,再加其它后缀名
    file = open(outfile,"w")
    if not file:
        print("cannot open the fule %s" % outfile)

    ListFilesToTxt(Currentdir, file, nameExtensons, 1, isDebug)

    file.close()


dir = "C:\\Program Files\\PCL 1.12.1\\3rdParty"

debug_txt = "D:\\PCL\\debug_3rdparty.txt"
release_txt = "D:\\PCL\\release_3rdparty.txt"

Read(dir, debug_txt, isDebug=True)
Read(dir, release_txt, isDebug=False)


如上是去获取3rdParty目录及其子目录下满足条件设置的lib(debug版本和release版本也都做了区分)。生成的debug_3rdparty.txt和release_3rdparty.txt文本如下:

              

 大家可在此脚本基础上做其它演变。

测试工程执行效果如下(工程也上传百度网盘):

链接:https://pan.baidu.com/s/1RFLs3zvOIVR3ao_wR0lVfA 
提取码:u4l9 
c++代码如下:

// test_vtk63.cpp : 定义控制台应用程序的入口点。
//
#include <stdio.h>
#include <tchar.h>

#include "vtkAutoInit.h" 
//VTK_MODULE_INIT(vtkRenderingOpenGL2);
//VTK_MODULE_INIT(vtkInteractionStyle);
//#define vtkRenderingCore_AUTOINIT 2(vtkRenderingOpenGL2, vtkInteractionStyle)



#include <boost/thread/thread.hpp> 
#include <pcl/visualization/cloud_viewer.h>
#include <pcl/io/io.h>
#include <pcl/io/pcd_io.h>//pcd 读写类相关的头文件。
#include <pcl/PCLPointCloud2.h>
#include <pcl/visualization/range_image_visualizer.h>
#include <pcl/visualization/pcl_visualizer.h>
#include <iostream>

using namespace pcl;
using namespace pcl::io;
using namespace std;

int testpointcloudToPcd()
{
	vtkObject::GlobalWarningDisplayOff();
	pcl::PointCloud<pcl::PointXYZ>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZ>);
	char strfilepath[256] = "D:\\PCL\\rabbit.pcd";

	//第一种读入方法较多场合如此
	if (-1 == pcl::io::loadPCDFile(strfilepath, *cloud)) {
		cout << "error input!" << endl;
		return -1;
	}

	boost::shared_ptr<pcl::visualization::PCLVisualizer> viewer(new pcl::visualization::PCLVisualizer("PointsCloud Recognition"));
	viewer->addPointCloud(cloud, "model_cloud");
	viewer->addCoordinateSystem(1.0);
	viewer->initCameraParameters();
	while (!viewer->wasStopped())
	{
		viewer->spinOnce(100);
		boost::this_thread::sleep(boost::posix_time::microseconds(100000));
	}

}

int _tmain(int argc, _TCHAR* argv[])
{
	testpointcloudToPcd();
	return 0;
}

运行效果如下: 

此版本对应的VTK版本是9.1

三.在VTK控件中显示PCL点云

1.直接用vtk去显示自定义的一个锥体

// test_vtk63.cpp : 定义控制台应用程序的入口点。
//

#include <map>
#include <tchar.h>
#include "vtkConeSource.h"
#include "vtkConeSource.h"
#include "vtkCommand.h"
#include "vtkCamera.h"
#include "vtkRenderWindowInteractor.h"
#include "vtkTransform.h"
#include "vtkInteractorStyleTrackballCamera.h"
#include <vtkSmartPointer.h>
#include <vtkDoubleArray.h>
#include <vtkCellArray.h>
#include <vtkPolyData.h>
#include <vtkDataSet.h>
#include <vtkLookupTable.h>
#include <vtkPointData.h>
#include <vtkPolyDataMapper.h>
#include <vtkActor.h>
#include <vtkProperty.h>
#include <vtkRenderer.h>
#include <vtkRenderWindow.h>
#include <vtkInteractorStyleTrackballCamera.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkArcSource.h>
#include <vtkAppendPolyData.h>
#include <vtkScalarBarActor.h>
#include <vtkKdTree.h>
#include <vtkLODActor.h>
#include <vtkMath.h>
#include <vtkWindowToImageFilter.h>
#include <vtkBMPWriter.h>
#include <vtkAutoInit.h>
#include <iostream>
#include <windows.h>
#include <stdio.h>
#include <windef.h>

#include <vtkAutoInit.h> 
VTK_MODULE_INIT(vtkRenderingOpenGL);
VTK_MODULE_INIT(vtkRenderingFreeType);
VTK_MODULE_INIT(vtkInteractionStyle);

void testvtk2()
{
	//创建数据源:创建一个圆锥,并设置其参数:高度、底面半径和分辨率
	vtkConeSource* cone = vtkConeSource::New();
	cone->SetHeight(3.0);
	cone->SetRadius(1.0);
	cone->SetResolution(100);

	//映射器:创建一个多边形映射器,用于把多边形数据映射为可以被计算机渲染的图元
	vtkPolyDataMapper* coneMapper = vtkPolyDataMapper::New();
	coneMapper->SetInputConnection(cone->GetOutputPort());

	//创建一个Actor,并关联一个映射器,从而确定Actor的形状
	vtkActor* coneActor = vtkActor::New();
	coneActor->SetMapper(coneMapper);

	//方法1:设置演员颜色
	coneActor->GetProperty()->SetColor(1.0, 0.0, 0.0);//设置对象颜色为红色
													  //方法2:设置演员颜色
													  //vtkProperty* coneProperty = vtkProperty::New();//设置一个Property对象
													  //coneProperty->SetColor(1.0, 0.0, 0.0);
													  //coneActor->SetProperty(coneProperty);


													  //渲染器:创建一个渲染器,添加要渲染的演员
	vtkRenderer* ren1 = vtkRenderer::New();
	ren1->AddActor(coneActor);
	ren1->SetBackground(0.1, 0.2, 0.4);

	//渲染窗口:添加一个渲染窗口,供渲染器使用

	vtkRenderWindow* renWin = vtkRenderWindow::New();

	renWin->AddRenderer(ren1);

	renWin->SetSize(300, 300);


	//实例化一个交互对象
	vtkRenderWindowInteractor* interactor = vtkRenderWindowInteractor::New();//交互对象加入renWin窗口中
	interactor->SetRenderWindow(renWin);

	renWin->Render();//绘制舞台上的东西

	interactor->Initialize();//交互对象初始化
	interactor->Start();//开始交互事件

						//释放内存
	cone->Delete();
	coneActor->Delete();
	coneMapper->Delete();
	ren1->Delete();
	renWin->Delete();
	interactor->Delete();
}

int _tmain(int argc, _TCHAR* argv[])
{
	testvtk2();
	return 0;
}

运行出现如下报错:

严重性    代码    说明    项目    文件    行    禁止显示状态
错误    LNK2019    无法解析的外部符号 "void __cdecl vtkRenderingOpenGL_AutoInit_Construct(void)" (?vtkRenderingOpenGL_AutoInit_Construct@@YAXXZ),函数 "public: __cdecl `anonymous namespace'::vtkRenderingOpenGL_ModuleInit::vtkRenderingOpenGL_ModuleInit(void)" (??0vtkRenderingOpenGL_ModuleInit@?A0xfeadf992@@QEAA@XZ) 中引用了该符号    PCL_config_console    D:\PCL\code\PCL_config_console\PCL_config_console\PCL_config_console.obj    1    
 

将代码中的VTK_MODULE_INIT(vtkRenderingOpenGL)修改为VTK_MODULE_INIT(vtkRenderingOpenGL2),即可!

 2.pcl读入点云,在vtk控件中显示

代码如下:

// test_vtk63.cpp : 定义控制台应用程序的入口点。
//


// test_mfc_showDlg.h: 头文件
//

#include <tchar.h>
#include "vtkAutoInit.h" 
VTK_MODULE_INIT(vtkRenderingOpenGL2);
VTK_MODULE_INIT(vtkInteractionStyle);

#include <boost/thread/thread.hpp> //多线程
#include <pcl/visualization/cloud_viewer.h>
#include <pcl/io/io.h>
#include <pcl/io/pcd_io.h>//pcd 读写类相关的头文件。
#include <pcl/io/ply_io.h>
#include <pcl/PCLPointCloud2.h>
#include <pcl/point_types.h> //PCL中支持的点类型头文件。
#include <pcl/common/common_headers.h>
#include <pcl/console/parse.h>
#include <pcl/point_types.h>  
#include <pcl/range_image/range_image.h>
#include <pcl/visualization/range_image_visualizer.h>
#include <pcl/visualization/pcl_visualizer.h>

#include "vtkCommand.h"
#include "vtkRenderer.h"
#include "vtkCellPicker.h"
#include "vtkProperty.h"
#include "vtkPlaneSource.h"
#include "vtkMetaImageReader.h"

#include "vtkImagePlaneWidget.h"
#include "vtkPlaneSource.h" 
#include "vtkPlane.h" 

#include "vtkSmartPointer.h"
#include "vtkResliceCursorActor.h" 
#include "vtkResliceCursorPolyDataAlgorithm.h" 
#include "vtkResliceCursor.h" 
#include "vtkResliceCursorWidget.h" 
#include "vtkResliceCursorLineRepresentation.h" 
#include "vtkResliceCursorThickLineRepresentation.h" 

#include <vtkVertexGlyphFilter.h>

#include "vtkImageReader2Factory.h"
#include "vtkImageReader2.h"

using namespace pcl;
using namespace pcl::io;
using namespace std;


using pcl::visualization::PointCloudColorHandlerGenericField;
using pcl::visualization::PointCloudColorHandlerCustom;

void testvtk2()
{
	pcl::PointCloud<pcl::PointXYZ>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZ>);
	char strfilepath[256] = "D:\\PCL\\rabbit.pcd";
	std::cout << "start to read" << std::endl;
	//第一种读入方法j较多场合如此
		if (-1 == pcl::io::loadPCDFile(strfilepath, *cloud)) {
			cout << "error input!" << endl;
		}

	cout << cloud->points.size() << endl;

	vtkSmartPointer<vtkPoints> points = vtkSmartPointer<vtkPoints>::New();//key code
	vtkIdType size = cloud->points.size();
	int n = 0;

	for (vtkIdType rowId = 0; rowId < size; rowId++)
	{
		auto dp = cloud->points.at(rowId);
		if (dp.z == 0) {
			continue;
		}
		points->InsertNextPoint(dp.x, dp.y, dp.z);//key code
		n++;
	}

	vtkSmartPointer<vtkPolyData> polydata = vtkSmartPointer<vtkPolyData>::New();
	polydata->SetPoints(points);

	vtkSmartPointer<vtkVertexGlyphFilter> glyphFilter = vtkSmartPointer<vtkVertexGlyphFilter>::New();
	glyphFilter->SetInputData(polydata);
	glyphFilter->Update();

	vtkPolyDataMapper* dataMapper = vtkPolyDataMapper::New();
	dataMapper->SetInputConnection(glyphFilter->GetOutputPort());

	vtkActor* coneActor = vtkActor::New();
	coneActor->SetMapper(dataMapper);
	coneActor->GetProperty()->SetColor(1.0, 0.0, 0.0);//设置对象颜色为红色
													  //方法2:设置演员颜色
													  //vtkProperty* coneProperty = vtkProperty::New();//设置一个Property对象
													  //coneProperty->SetColor(1.0, 0.0, 0.0);
													  //coneActor->SetProperty(coneProperty);


													  //渲染器:创建一个渲染器,添加要渲染的演员

	vtkSmartPointer<vtkRenderer>     m_Renderer;
	vtkSmartPointer<vtkRenderWindow> m_RenderWindow;

	m_Renderer = vtkSmartPointer<vtkRenderer>::New();
	m_Renderer->AddActor(coneActor);
	m_Renderer->SetBackground(0.1, 0.2, 0.4);

	m_RenderWindow = vtkSmartPointer<vtkRenderWindow>::New();
	m_RenderWindow->SetSize(500, 500);
	m_RenderWindow->AddRenderer(m_Renderer);


    vtkRenderWindowInteractor * interactor = vtkRenderWindowInteractor::New();//交互对象加入renWin窗口中
	interactor->SetRenderWindow(m_RenderWindow);

	m_RenderWindow->Render();//绘制舞台上的东西

	interactor->Initialize();//交互对象初始化
	interactor->Start();//开始交互事件

	polydata->Delete();
	glyphFilter->Delete();
	dataMapper->Delete();

	coneActor->Delete();
	m_Renderer->Delete();
	m_RenderWindow->Delete();
	interactor->Delete();

	//if (m_RenderWindow->GetInteractor() == NULL)
	//{
	//	vtkSmartPointer<vtkRenderWindowInteractor> RenderWindowInteractor =
	//		vtkSmartPointer<vtkRenderWindowInteractor>::New();
	//	RenderWindowInteractor->SetRenderWindow(m_RenderWindow);
	//	RenderWindowInteractor->Initialize();
	//}
}

int _tmain(int argc, _TCHAR* argv[])
{
	testvtk2();
	return 0;
}

vtk中显示的效果如下:

3.VTK保存数据为vtk文件

在上面代码的基础上,增加头文件#include <vtkPolyDataWriter.h>

在增加如下代码(polydata->SetPoints(points)语句下面),即可以保存数据到vtk文件中

4.VTK API读取本地vtk文件,并在自己控件里显示

代码如下:

// test_vtk63.cpp : 定义控制台应用程序的入口点。
//


// test_mfc_showDlg.h: 头文件
//

#include <tchar.h>
#include "vtkAutoInit.h" 
VTK_MODULE_INIT(vtkRenderingOpenGL2);
VTK_MODULE_INIT(vtkInteractionStyle);

#include <boost/thread/thread.hpp> //多线程
#include <pcl/visualization/cloud_viewer.h>
#include <pcl/io/io.h>
#include <pcl/io/pcd_io.h>//pcd 读写类相关的头文件。
#include <pcl/io/ply_io.h>
#include <pcl/PCLPointCloud2.h>
#include <pcl/point_types.h> //PCL中支持的点类型头文件。
#include <pcl/common/common_headers.h>
#include <pcl/console/parse.h>
#include <pcl/point_types.h>  
#include <pcl/range_image/range_image.h>
#include <pcl/visualization/range_image_visualizer.h>

#include <pcl/visualization/pcl_visualizer.h>
#include <vtkSimplePointsReader.h>
#include "vtkCommand.h"
#include "vtkRenderer.h"
#include "vtkCellPicker.h"
#include "vtkProperty.h"
#include "vtkPlaneSource.h"
#include "vtkMetaImageReader.h"

#include "vtkImagePlaneWidget.h"
#include "vtkPlaneSource.h" 
#include "vtkPlane.h" 

#include "vtkSimplePointsReader.h"

#include "vtkSmartPointer.h"
#include "vtkResliceCursorActor.h" 
#include "vtkResliceCursorPolyDataAlgorithm.h" 
#include "vtkResliceCursor.h" 
#include "vtkResliceCursorWidget.h" 
#include "vtkResliceCursorLineRepresentation.h" 
#include "vtkResliceCursorThickLineRepresentation.h" 

#include <vtkVertexGlyphFilter.h>

#include "vtkImageReader2Factory.h"
#include "vtkImageReader2.h"

#include <vtkPolyDataReader.h>

using namespace pcl;
using namespace pcl::io;
using namespace std;


using pcl::visualization::PointCloudColorHandlerGenericField;
using pcl::visualization::PointCloudColorHandlerCustom;

void testvtk2()
{
	vtkNew<vtkPolyDataReader> reader;
	reader->SetFileName("D:\\PCL\\rabbit.vtk");
	reader->Update();


	vtkSmartPointer<vtkPolyData> polydata = vtkSmartPointer<vtkPolyData>::New();
	polydata->SetPoints(reader->GetOutput()->GetPoints());

	vtkSmartPointer<vtkVertexGlyphFilter> glyphFilter = vtkSmartPointer<vtkVertexGlyphFilter>::New();
	glyphFilter->SetInputData(polydata);
	glyphFilter->Update();

	vtkPolyDataMapper* dataMapper = vtkPolyDataMapper::New();
	dataMapper->SetInputConnection(glyphFilter->GetOutputPort());

	vtkActor* coneActor = vtkActor::New();
	coneActor->SetMapper(dataMapper);
	coneActor->GetProperty()->SetColor(1.0, 0.0, 0.0);//设置对象颜色为红色
													  //方法2:设置演员颜色
													  //vtkProperty* coneProperty = vtkProperty::New();//设置一个Property对象
													  //coneProperty->SetColor(1.0, 0.0, 0.0);
													  //coneActor->SetProperty(coneProperty);


													  //渲染器:创建一个渲染器,添加要渲染的演员

	vtkSmartPointer<vtkRenderer>     m_Renderer;
	vtkSmartPointer<vtkRenderWindow> m_RenderWindow;

	m_Renderer = vtkSmartPointer<vtkRenderer>::New();
	m_Renderer->AddActor(coneActor);
	m_Renderer->SetBackground(0.1, 0.2, 0.4);

	m_RenderWindow = vtkSmartPointer<vtkRenderWindow>::New();
	m_RenderWindow->SetSize(500, 500);
	m_RenderWindow->AddRenderer(m_Renderer);


	vtkRenderWindowInteractor* interactor = vtkRenderWindowInteractor::New();//交互对象加入renWin窗口中
	interactor->SetRenderWindow(m_RenderWindow);

	m_RenderWindow->Render();//绘制舞台上的东西

	interactor->Initialize();//交互对象初始化
	interactor->Start();//开始交互事件

	polydata->Delete();
	glyphFilter->Delete();
	dataMapper->Delete();

	coneActor->Delete();
	m_Renderer->Delete();
	m_RenderWindow->Delete();
	interactor->Delete();

	//if (m_RenderWindow->GetInteractor() == NULL)
	//{
	//	vtkSmartPointer<vtkRenderWindowInteractor> RenderWindowInteractor =
	//		vtkSmartPointer<vtkRenderWindowInteractor>::New();
	//	RenderWindowInteractor->SetRenderWindow(m_RenderWindow);
	//	RenderWindowInteractor->Initialize();
	//}
}

int _tmain(int argc, _TCHAR* argv[])
{
	testvtk2();
	return 0;
}

执行效果如下: 

 5.VTK保存数据到txt文本中

在上面代码的基础上增加如下代码(polydata->SetPoints(reader->GetOutput()->GetPoints()):

6.VTK读txt数据并转化为vtk数据

代码如下,读入一个txt文本,然后进行数据的转换

// test_vtk63.cpp : 定义控制台应用程序的入口点。
//


// test_mfc_showDlg.h: 头文件
//

#include <tchar.h>
#include "vtkAutoInit.h" 
VTK_MODULE_INIT(vtkRenderingOpenGL2);
VTK_MODULE_INIT(vtkInteractionStyle);

#include <boost/thread/thread.hpp> //多线程
#include <pcl/visualization/cloud_viewer.h>
#include <pcl/io/io.h>
#include <pcl/io/pcd_io.h>//pcd 读写类相关的头文件。
#include <pcl/io/ply_io.h>
#include <pcl/PCLPointCloud2.h>
#include <pcl/point_types.h> //PCL中支持的点类型头文件。
#include <pcl/common/common_headers.h>
#include <pcl/console/parse.h>
#include <pcl/point_types.h>  
#include <pcl/range_image/range_image.h>
#include <pcl/visualization/range_image_visualizer.h>
#include <pcl/visualization/pcl_visualizer.h>

#include "vtkCommand.h"
#include "vtkRenderer.h"
#include "vtkCellPicker.h"
#include "vtkProperty.h"
#include "vtkPlaneSource.h"
#include "vtkMetaImageReader.h"

#include "vtkImagePlaneWidget.h"
#include "vtkPlaneSource.h" 
#include "vtkPlane.h" 

#include "vtkSmartPointer.h"
#include "vtkResliceCursorActor.h" 
#include "vtkResliceCursorPolyDataAlgorithm.h" 
#include "vtkResliceCursor.h" 
#include "vtkResliceCursorWidget.h" 
#include "vtkResliceCursorLineRepresentation.h" 
#include "vtkResliceCursorThickLineRepresentation.h" 

#include <vtkVertexGlyphFilter.h>

#include "vtkImageReader2Factory.h"
#include "vtkImageReader2.h"

using namespace pcl;
using namespace pcl::io;
using namespace std;


using pcl::visualization::PointCloudColorHandlerGenericField;
using pcl::visualization::PointCloudColorHandlerCustom;

void testvtk2()
{
	//istringstream istr;
	//string line, str;
	//while (getline(cin, line))//从终端接收一行字符串,并放入字符串line中   
	//{
	//	istr.str(line);//把line中的字符串存入字符串流中   
	//	while (istr >> str)//每次读取一个单词(以空格为界),存入str中   
	//	{
	//		cout << str << endl;
	//	}
	//}

    //std::string filename = "Points.txt";
 // std::ifstream filestream(filename.c_str()); //文件流
 // std::string line;
 
//  vtkSmartPointer<vtkPoints> points = vtkSmartPointer<vtkPoints>::New();
 
 // while(std::getline(filestream, line))  //整行读取文件
  //{
   // double x, y, z;
   // std::stringstream linestream;
   // linestream << line;
   // linestream >> x >> y >> z;
   // points->InsertNextPoint(x, y, z); //新读取的数据赋予点的几何结构
 // }
 // filestream.close();  //关闭文件流操作



	vtkSmartPointer<vtkPoints> points = vtkSmartPointer<vtkPoints>::New();//key code

	ifstream inFile;
	inFile.open("D:\\PCL\\rabbit.txt");

	double x, y, z;
	while (inFile >> x >> y >> z)
	{
		points->InsertNextPoint(x, y, z);
	}

	vtkSmartPointer<vtkPolyData> polydata = vtkSmartPointer<vtkPolyData>::New();
	polydata->SetPoints(points);

	vtkSmartPointer<vtkVertexGlyphFilter> glyphFilter = vtkSmartPointer<vtkVertexGlyphFilter>::New();
	glyphFilter->SetInputData(polydata);
	glyphFilter->Update();

	vtkPolyDataMapper* dataMapper = vtkPolyDataMapper::New();
	dataMapper->SetInputConnection(glyphFilter->GetOutputPort());

	vtkActor* coneActor = vtkActor::New();
	coneActor->SetMapper(dataMapper);
	coneActor->GetProperty()->SetColor(1.0, 0.0, 0.0);//设置对象颜色为红色
													  //方法2:设置演员颜色
													  //vtkProperty* coneProperty = vtkProperty::New();//设置一个Property对象
													  //coneProperty->SetColor(1.0, 0.0, 0.0);
													  //coneActor->SetProperty(coneProperty);


													  //渲染器:创建一个渲染器,添加要渲染的演员

	vtkSmartPointer<vtkRenderer>     m_Renderer;
	vtkSmartPointer<vtkRenderWindow> m_RenderWindow;

	m_Renderer = vtkSmartPointer<vtkRenderer>::New();
	m_Renderer->AddActor(coneActor);
	m_Renderer->SetBackground(0.1, 0.2, 0.4);

	m_RenderWindow = vtkSmartPointer<vtkRenderWindow>::New();
	m_RenderWindow->SetSize(500, 500);
	m_RenderWindow->AddRenderer(m_Renderer);


	vtkRenderWindowInteractor* interactor = vtkRenderWindowInteractor::New();//交互对象加入renWin窗口中
	interactor->SetRenderWindow(m_RenderWindow);

	m_RenderWindow->Render();//绘制舞台上的东西

	interactor->Initialize();//交互对象初始化
	interactor->Start();//开始交互事件

	polydata->Delete();
	glyphFilter->Delete();
	dataMapper->Delete();

	coneActor->Delete();
	m_Renderer->Delete();
	m_RenderWindow->Delete();
	interactor->Delete();

	//if (m_RenderWindow->GetInteractor() == NULL)
	//{
	//	vtkSmartPointer<vtkRenderWindowInteractor> RenderWindowInteractor =
	//		vtkSmartPointer<vtkRenderWindowInteractor>::New();
	//	RenderWindowInteractor->SetRenderWindow(m_RenderWindow);
	//	RenderWindowInteractor->Initialize();
	//}
}

int _tmain(int argc, _TCHAR* argv[])
{
	testvtk2();
	return 0;
}

显示效果如下:

7.PCL读入txt数据显示

代码如下:

// test_vtk63.cpp : 定义控制台应用程序的入口点。
//
#include <stdio.h>
#include <tchar.h>

#include "vtkAutoInit.h" 
//VTK_MODULE_INIT(vtkRenderingOpenGL2);
//VTK_MODULE_INIT(vtkInteractionStyle);
//#define vtkRenderingCore_AUTOINIT 2(vtkRenderingOpenGL2, vtkInteractionStyle)



#include <boost/thread/thread.hpp> 
#include <pcl/visualization/cloud_viewer.h>
#include <pcl/io/io.h>
#include <pcl/io/pcd_io.h>//pcd 读写类相关的头文件。
#include <pcl/PCLPointCloud2.h>
#include <pcl/visualization/range_image_visualizer.h>
#include <pcl/visualization/pcl_visualizer.h>
#include <iostream>

using namespace pcl;
using namespace pcl::io;
using namespace std;

int testpointcloudToPcd()
{

	pcl::PointCloud<pcl::PointXYZ>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZ>);

	ifstream in0;
	in0.open("D:\\PCL\\rabbit.txt");
	if (!in0.is_open())
	{
		cout << "error open!" << endl;
		return -1;
	}

	//a,b为符号
	int count = 0;
	double x, y, z;
	while (in0 >> x >> y >> z)
	{
		pcl::PointXYZ cltmp(x, y, z);		
		cloud->points.push_back(cltmp);
		count++;
	}

	std::cout << "start to save pointcloud...";
	cloud->width = count; //设置点云宽度
	cloud->height = 1; //设置点云高度
	cloud->is_dense = false;//判断points中的数据是否是有限的(有限为true)或者说是判断点云中的点是否包含 Inf/NaN这种值(包含为false)。
							//为false或者true并不影响结果,是否为密度点云
							//pcl::io::savePCDFileASCII("D:\\mycode\\0-PCL_VTK\\pryamid_yidong10.pcd", *cloud);
							/*pcl::io::savePCDFileBinary("D:\\mycode\\0-PCL_VTK\\pryamid1.pcd", *cloud);
							pcl::io::savePCDFile("D:\\mycode\\0-PCL_VTK\\pryamid2.pcd", *cloud);*/

	std::cout << "save completed!start to show" << std::endl;

	boost::shared_ptr<pcl::visualization::PCLVisualizer> viewer(new pcl::visualization::PCLVisualizer("PointsCloud Recognition"));
	viewer->addPointCloud(cloud, "model_cloud");
	viewer->addCoordinateSystem(1.0);
	viewer->initCameraParameters();
	while (!viewer->wasStopped())
	{
		viewer->spinOnce(100);
		boost::this_thread::sleep(boost::posix_time::microseconds(100000));
	}


}

int _tmain(int argc, _TCHAR* argv[])
{
	testpointcloudToPcd();
	return 0;
}

8.常见点云文件格式介绍

这边列举了一些博客用于参考

(1)PCD

点云库PCL入门1:存储点云数据的pcd文件简介 - 知乎

PCL:点云数据(*.pcd)文件格式详解_孙 悟 空的博客-CSDN博客_pcd格式

PCL中PCD(Point Cloud Data)文件格式解析_SOC罗三炮的博客-CSDN博客_pcd文件

(2) PLY

PLY文件格式 - 慕尘 - 博客园

(3) Step

一篇文章了解STEP文件格式 - 知乎

https://www.jianshu.com/p/ccfc77d0ee45

9.PCL读写点云文件

这边列举了一些博客用于参考

PCL读写pcd文件 - 知乎

PCL:读写pcd点云的两种方式_孙 悟 空的博客-CSDN博客_pcl读取pcd

ply las pcd等一些常见的基本点云文件格式的读写方法_qq_46084757的博客-CSDN博客_ply点云

PCL入门系列三——PCL进行数据读写 - 知乎

点云学习----PCL读写点云_刚上路DE小白的博客-CSDN博客_pcl读取点云

10.VTK数据处理

VTK图像处理(二)--vtkPolyData数据处理 - 知乎

11. 八叉树

如下的一些帖子,博主觉得可参考

百度安全验证

PCL学习笔记(六)-- k-d tree_看到我请叫我学C++的博客-CSDN博客

PCL学习笔记(七)-- 八叉树OcTree_八叉树写法_看到我请叫我学C++的博客-CSDN博客

https://www.cnblogs.com/z-web-2017/p/14111071.html

【图形学】基于点云生成树木模型_树木点云_wk_119的博客-CSDN博客

一文搞懂k近邻(k-NN)算法(一) - 知乎

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

竹叶青lvye

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

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

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

打赏作者

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

抵扣说明:

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

余额充值