1. 相关说明
2. CMakeLists.txt
# 指定最低的CMAKE版本
CMAKE_MINIMUM_REQUIRED(VERSION 3.22 FATAL_ERROR)
# 创建项目
PROJECT(PCLSamples LANGUAGES CXX)
# 指定CPLUSPLUS标准
SET(CMAKE_CXX_STANDARD_REQUIRED ON)
SET(CMAKE_CXX_STANDARD 14)
SET(CMAKE_C_STANDARD 14)
SET(CMAKE_C_STANDARD_REQUIRED ON)
# 查找第三方依赖库
FIND_PACKAGE(PCL 1.11 REQUIRED)
# 添加可执行文件
ADD_EXECUTABLE(PCLSample src/main.cpp)
# 添加头文件目录
TARGET_INCLUDE_DIRECTORIES(PCLSample PRIVATE ${PCL_INCLUDE_DIRS})
# 添加库目录
TARGET_LINK_DIRECTORIES(PCLSample PRIVATE ${PCL_LIBRARY_DIRS})
# 添加编译器定义
TARGET_COMPILE_DEFINITIONS(PCLSample PRIVATE ${PCL_DEFINITIONS})
# 取消已有的编译器定义
#SET_TARGET_PROPERTIES(PCLSample PROPERTIES COMPILE_FLAGS "/UBOOST_ALL_NO_LIB-DBOOST_ALL_NO_LIB")
# 添加依赖库文件
target_link_libraries(PCLSample PRIVATE ${PCL_LIBRARIES})
3. main.cpp
#include <iostream>
#include <thread>
#include <pcl/common/angles.h>
#include <pcl/features/normal_3d.h>
#include <pcl/visualization/pcl_visualizer.h>
#include <pcl/console/parse.h>
using namespace std::chrono_literals;
void printHelper(const char *programName)
{
std::cout << "\n\nUsage: " << programName << " [options]\n\n"
<< "Options:\n"
<< "-------------------------------------------\n"
<< "-h this help\n"
<< "-s Simple visualisation example\n"
<< "-r RGB colour visualisation example\n"
<< "-c Custom colour visualisation example\n"
<< "-n Normals visualisation example\n"
<< "-a Shapes visualisation example\n"
<< "-v Viewports example\n"
<< "-i Interaction Customization example\n"
<< "\n\n";
}
pcl::visualization::PCLVisualizer::Ptr viewXyz(const pcl::PointCloud<pcl::PointXYZ>::ConstPtr cloud)
{
pcl::visualization::PCLVisualizer::Ptr viewer(new pcl::visualization::PCLVisualizer("3D Viewer"));
viewer->setBackgroundColor(0, 0, 0);
viewer->addPointCloud<pcl::PointXYZ>(cloud, "sample cloud");
viewer->setPointCloudRenderingProperties(pcl::visualization::PCL_VISUALIZER_POINT_SIZE, 1, "sample cloud");
viewer->addCoordinateSystem(1.0);
viewer->initCameraParameters();
return (viewer);
}
pcl::visualization::PCLVisualizer::Ptr viewRgb(const pcl::PointCloud<pcl::PointXYZRGB>::ConstPtr cloud)
{
pcl::visualization::PCLVisualizer::Ptr viewer(new pcl::visualization::PCLVisualizer("3D Viewer"));
viewer->setBackgroundColor(0, 0, 0);
pcl::visualization::PointCloudColorHandlerRGBField<pcl::PointXYZRGB> rgb(cloud);
viewer->addPointCloud<pcl::PointXYZRGB>(cloud, rgb, "sample cloud");
viewer->setPointCloudRenderingProperties(pcl::visualization::PCL_VISUALIZER_POINT_SIZE, 3, "sample cloud");
viewer->addCoordinateSystem(1.0);
viewer->initCameraParameters();
return (viewer);
}
pcl::visualization::PCLVisualizer::Ptr viewCustomColor(const pcl::PointCloud<pcl::PointXYZ>::ConstPtr cloud)
{
pcl::visualization::PCLVisualizer::Ptr viewer(new pcl::visualization::PCLVisualizer("3D Viewer"));
viewer->setBackgroundColor(0, 0, 0);
pcl::visualization::PointCloudColorHandlerCustom<pcl::PointXYZ> single_color(cloud, 0, 255, 0);
viewer->addPointCloud<pcl::PointXYZ>(cloud, single_color, "sample cloud");
viewer->setPointCloudRenderingProperties(pcl::visualization::PCL_VISUALIZER_POINT_SIZE, 3, "sample cloud");
viewer->addCoordinateSystem(1.0);
viewer->initCameraParameters();
return (viewer);
}
pcl::visualization::PCLVisualizer::Ptr viewNormal(const pcl::PointCloud<pcl::PointXYZRGB>::ConstPtr cloud,
const pcl::PointCloud<pcl::Normal>::ConstPtr normals)
{
pcl::visualization::PCLVisualizer::Ptr viewer(new pcl::visualization::PCLVisualizer("3D Viewer"));
viewer->setBackgroundColor(0, 0, 0);
pcl::visualization::PointCloudColorHandlerRGBField<pcl::PointXYZRGB> rgb(cloud);
viewer->addPointCloud<pcl::PointXYZRGB>(cloud, rgb, "sample cloud");
viewer->setPointCloudRenderingProperties(pcl::visualization::PCL_VISUALIZER_POINT_SIZE, 3, "sample cloud");
viewer->addPointCloudNormals<pcl::PointXYZRGB, pcl::Normal>(cloud, normals, 10, 0.05, "normals");
viewer->addCoordinateSystem(1.0);
viewer->initCameraParameters();
return (viewer);
}
pcl::visualization::PCLVisualizer::Ptr viewShape(const pcl::PointCloud<pcl::PointXYZRGB>::ConstPtr cloud)
{
pcl::visualization::PCLVisualizer::Ptr viewer(new pcl::visualization::PCLVisualizer("3D Viewer"));
viewer->setBackgroundColor(0, 0, 0);
pcl::visualization::PointCloudColorHandlerRGBField<pcl::PointXYZRGB> rgb(cloud);
viewer->addPointCloud<pcl::PointXYZRGB>(cloud, rgb, "sample cloud");
viewer->setPointCloudRenderingProperties(pcl::visualization::PCL_VISUALIZER_POINT_SIZE, 3, "sample cloud");
viewer->addCoordinateSystem(1.0);
viewer->initCameraParameters();
viewer->addLine<pcl::PointXYZRGB>((*cloud)[0], (*cloud)[cloud->size() - 1], "line");
viewer->addSphere((*cloud)[0], 0.2, 0.5, 0.5, 0.0, "sphere");
pcl::ModelCoefficients coefficients;
coefficients.values.push_back(0.0);
coefficients.values.push_back(0.0);
coefficients.values.push_back(1.0);
coefficients.values.push_back(0.0);
viewer->addPlane(coefficients, "plane");
coefficients.values.clear();
coefficients.values.push_back(0.3);
coefficients.values.push_back(0.3);
coefficients.values.push_back(0.0);
coefficients.values.push_back(0.0);
coefficients.values.push_back(1.0);
coefficients.values.push_back(0.0);
coefficients.values.push_back(5.0);
viewer->addCone(coefficients, "cone");
return (viewer);
}
pcl::visualization::PCLVisualizer::Ptr multiViews(const pcl::PointCloud<pcl::PointXYZRGB>::ConstPtr cloud,
const pcl::PointCloud<pcl::Normal>::ConstPtr normals1,
const pcl::PointCloud<pcl::Normal>::ConstPtr normals2)
{
pcl::visualization::PCLVisualizer::Ptr viewer(new pcl::visualization::PCLVisualizer("3D Viewer"));
viewer->initCameraParameters();
int v1(0);
viewer->createViewPort(0.0, 0.0, 0.5, 1.0, v1);
viewer->setBackgroundColor(0, 0, 0, v1);
viewer->addText("Radius: 0.01", 10, 10, "v1 text", v1);
pcl::visualization::PointCloudColorHandlerRGBField<pcl::PointXYZRGB> rgb(cloud);
viewer->addPointCloud<pcl::PointXYZRGB>(cloud, rgb, "sample cloud1", v1);
int v2(0);
viewer->createViewPort(0.5, 0.0, 1.0, 1.0, v2);
viewer->setBackgroundColor(0.3, 0.3, 0.3, v2);
viewer->addText("Radius: 0.1", 10, 10, "v2 text", v2);
pcl::visualization::PointCloudColorHandlerCustom<pcl::PointXYZRGB> single_color(cloud, 0, 255, 0);
viewer->addPointCloud<pcl::PointXYZRGB>(cloud, single_color, "sample cloud2", v2);
viewer->setPointCloudRenderingProperties(pcl::visualization::PCL_VISUALIZER_POINT_SIZE, 3, "sample cloud1");
viewer->setPointCloudRenderingProperties(pcl::visualization::PCL_VISUALIZER_POINT_SIZE, 3, "sample cloud2");
viewer->addCoordinateSystem(1.0);
viewer->addPointCloudNormals<pcl::PointXYZRGB, pcl::Normal>(cloud, normals1, 10, 0.05, "normals1", v1);
viewer->addPointCloudNormals<pcl::PointXYZRGB, pcl::Normal>(cloud, normals2, 10, 0.05, "normals2", v2);
return (viewer);
}
unsigned int text_id = 0;
void keyboardEventOccurred(const pcl::visualization::KeyboardEvent &event, void *viewer_void)
{
auto *viewer = static_cast<pcl::visualization::PCLVisualizer *>(viewer_void);
if (event.getKeySym() == "r" && event.keyDown())
{
std::cout << "r was pressed => removing all text" << std::endl;
char str[512];
for (unsigned int i = 0; i < text_id; ++i)
{
sprintf(str, "text#%03d", i);
viewer->removeShape(str);
}
text_id = 0;
}
}
void mouseEventOccurred(const pcl::visualization::MouseEvent &event, void *viewer_void)
{
auto *viewer = static_cast<pcl::visualization::PCLVisualizer *>(viewer_void);
if (event.getButton() == pcl::visualization::MouseEvent::LeftButton &&
event.getType() == pcl::visualization::MouseEvent::MouseButtonRelease)
{
std::cout << "Left mouse button released at position (" << event.getX() << ", " << event.getY() << ")" << std::endl;
char str[512];
sprintf(str, "text#%03d", text_id++);
viewer->addText("clicked here", event.getX(), event.getY(), str);
}
}
pcl::visualization::PCLVisualizer::Ptr interactionCustomizationVis()
{
pcl::visualization::PCLVisualizer::Ptr viewer(new pcl::visualization::PCLVisualizer("3D Viewer"));
viewer->setBackgroundColor(0, 0, 0);
viewer->addCoordinateSystem(1.0);
viewer->registerKeyboardCallback(keyboardEventOccurred, (void *)viewer.get());
viewer->registerMouseCallback(mouseEventOccurred, (void *)viewer.get());
return (viewer);
}
int main(int argc, char **argv)
{
bool simple(false);
bool rgb(false);
bool custom_c(false);
bool normals(false);
bool shapes(false);
bool viewports(false);
bool interaction_customization(false);
if (pcl::console::find_argument(argc, argv, "-h") >= 0)
{
printHelper(argv[0]);
return EXIT_FAILURE;
}
else if (pcl::console::find_argument(argc, argv, "-s") >= 0)
{
simple = true;
std::cout << "Simple visualisation example\n";
}
else if (pcl::console::find_argument(argc, argv, "-c") >= 0)
{
custom_c = true;
std::cout << "Custom colour visualisation example\n";
}
else if (pcl::console::find_argument(argc, argv, "-r") >= 0)
{
rgb = true;
std::cout << "RGB colour visualisation example\n";
}
else if (pcl::console::find_argument(argc, argv, "-n") >= 0)
{
normals = true;
std::cout << "Normals visualisation example\n";
}
else if (pcl::console::find_argument(argc, argv, "-a") >= 0)
{
shapes = true;
std::cout << "Shapes visualisation example\n";
}
else if (pcl::console::find_argument(argc, argv, "-v") >= 0)
{
viewports = true;
std::cout << "Viewports example\n";
}
else if (pcl::console::find_argument(argc, argv, "-i") >= 0)
{
interaction_customization = true;
std::cout << "Interaction Customization example\n";
}
else
{
printHelper(argv[0]);
return 0;
}
pcl::PointCloud<pcl::PointXYZ>::Ptr xyzCloudPtr(new pcl::PointCloud<pcl::PointXYZ>);
pcl::PointCloud<pcl::PointXYZRGB>::Ptr rgbCloudPtr(new pcl::PointCloud<pcl::PointXYZRGB>);
std::uint8_t r(255), g(15), b(15);
for (float z(-1.0); z <= 1.0; z += 0.05)
{
for (float angle(0.0); angle <= 360.0; angle += 5.0)
{
pcl::PointXYZ xyzPoint;
xyzPoint.x = 0.5f * std::cosf(pcl::deg2rad(angle));
xyzPoint.y = 1.0f * std::sinf(pcl::deg2rad(angle));
xyzPoint.z = z;
xyzCloudPtr->points.push_back(xyzPoint);
pcl::PointXYZRGB point(xyzPoint.x, xyzPoint.y, xyzPoint.z, r, g, b);
rgbCloudPtr->points.push_back(point);
}
if (z < 0.0)
{
r -= 12;
g += 12;
}
else
{
g -= 12;
b += 12;
}
}
xyzCloudPtr->width = xyzCloudPtr->size();
xyzCloudPtr->height = 1;
rgbCloudPtr->width = rgbCloudPtr->size();
rgbCloudPtr->height = 1;
pcl::NormalEstimation<pcl::PointXYZRGB, pcl::Normal> ne;
ne.setInputCloud(rgbCloudPtr);
pcl::search::KdTree<pcl::PointXYZRGB>::Ptr tree(new pcl::search::KdTree<pcl::PointXYZRGB>());
ne.setSearchMethod(tree);
pcl::PointCloud<pcl::Normal>::Ptr rgbCloudNormal(new pcl::PointCloud<pcl::Normal>);
ne.setRadiusSearch(0.05);
ne.compute(*rgbCloudNormal);
pcl::PointCloud<pcl::Normal>::Ptr xyzCloudNormal(new pcl::PointCloud<pcl::Normal>);
ne.setRadiusSearch(0.1);
ne.compute(*xyzCloudNormal);
pcl::visualization::PCLVisualizer::Ptr viewer;
if (simple)
viewer = viewXyz(xyzCloudPtr);
else if (rgb)
viewer = viewRgb(rgbCloudPtr);
else if (custom_c)
viewer = viewCustomColor(xyzCloudPtr);
else if (normals)
viewer = viewNormal(rgbCloudPtr, rgbCloudNormal);
else if (shapes)
viewer = viewShape(rgbCloudPtr);
else if (viewports)
viewer = multiViews(rgbCloudPtr, rgbCloudNormal, xyzCloudNormal);
else if (interaction_customization)
viewer = interactionCustomizationVis();
while (!viewer->wasStopped())
{
viewer->spinOnce(100);
std::this_thread::sleep_for(100ms);
}
}
4. 效果图
4.1 PointXYZ
4.2 PointXYZRGB 自带颜色数据
4.3 PointXYZ 附加颜色
4.4 法线
4.5 显示形状
4.6 多视图
4.7 鼠标键盘事件
- 鼠标事件
- 键盘事件(按下R键,清空当前显示)