测试环境:
vs2019
pcl==1.12.1
代码:
#include<iostream>
#include <thread>
#include <pcl/common/common_headers.h>
#include <pcl/features/normal_3d.h>
#include <pcl/io/pcd_io.h>
#include <pcl/visualization/pcl_visualizer.h>
#include <pcl/console/parse.h>
#include <boost/current_function.hpp>
using namespace pcl;
int main(int argc, char** argv)
{
// 1. 创建和设置可视化窗口
std::string strWinName = "3D Viewer", strWinTitle = "Point Cloud Viewer";
int scnWidth = 1024, scnHeight = 800;
visualization::PCLVisualizer::Ptr viewer(new visualization::PCLVisualizer(strWinName));
viewer->setWindowName(strWinTitle);
viewer->initCameraParameters(); // set camera before set size
viewer->setPosition(0, 0);
viewer->setSize(scnWidth, scnHeight);
viewer->setShowFPS(false);
// viewports
int vMenu(0), v1(0), v2(0);
viewer->createViewPort(0.0, 0.0, 1.0, 0.2, vMenu); // used as interface
viewer->createViewPort(0.0, 0.2, 0.5, 1.0, v1);
viewer->createViewPort(0.5, 0.2, 1.0, 1.0, v2);
// Background Color
viewer->setBackgroundColor(0.8, 0.8, 0.8, vMenu); // light grey
viewer->setBackgroundColor(0.1, 0.1, 0.1, v1); // dark grey
viewer->setBackgroundColor(0.2, 0.2, 0.2, v2); // dark grey
// cameras
viewer->createViewPortCamera(v1);
viewer->createViewPortCamera(v2);
double pos[3] = { 6,0,0 }; // camera at X-axis
double foc[3] = { 0,0,0 }; // viewpoint at orgin
double up[3] = { 0,0,1 }; // up is Z-axis
viewer->setCameraPosition(pos[0], pos[1], pos[2], foc[0], foc[1], foc[2], up[0], up[1], up[2]);
// check cameras
//std::vector<visualization::Camera> cams;
//viewer->getCameras(cams);
// coordinates
viewer->addCoordinateSystem(1.0, "ref_v1", v1);
viewer->addCoordinateSystem(1.0, "ref_v2", v2);
// 2. 创建点云数据和添加点云。
PointCloud<PointXYZ>::Ptr cloud_ptr(new PointCloud<PointXYZ>);
PointCloud<PointXYZRGB>::Ptr cloud_color_ptr(new PointCloud<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 basic_point;
basic_point.x = 0.5 * std::cos(pcl::deg2rad(angle));
basic_point.y = sinf(pcl::deg2rad(angle));
basic_point.z = z;
cloud_ptr->points.push_back(basic_point); // 将每个点输入点云
pcl::PointXYZRGB point;
point.x = basic_point.x;
point.y = basic_point.y;
point.z = basic_point.z;
std::uint32_t rgb = (static_cast<std::uint32_t>(r) << 16 |
static_cast<std::uint32_t>(g) << 8 | static_cast<std::uint32_t>(b));
point.rgb = *reinterpret_cast<float*>(&rgb);
cloud_color_ptr->points.push_back(point); // 将每个点输入点云
}
if (z < 0.0)
{
r -= 12; // light red at -|z|
g += 12; // light green at 0
}
else
{
g -= 12; // light green at 0
b += 12; // light blue at +|z|
}
}
cloud_ptr->width = cloud_ptr->size(); // 无规则点云的width为点数
cloud_ptr->height = 1;
cloud_color_ptr->width = cloud_color_ptr->size(); // 无规则点云的width为点数
cloud_color_ptr->height = 1;
bool ret = viewer->addPointCloud<PointXYZ>(cloud_ptr, "cloud1"); // 白色点云
if (ret)
{
double clrR = 0, clrG = 0, clrB = 1, szPoint = 3;
viewer->setPointCloudRenderingProperties(visualization::PCL_VISUALIZER_COLOR, clrR, clrG, clrB, "cloud1"); // 设置点云颜色
viewer->setPointCloudRenderingProperties(visualization::PCL_VISUALIZER_POINT_SIZE, szPoint, "cloud1"); // 设置大小
}
else
viewer->updatePointCloud<PointXYZ>(cloud_ptr, "cloud1");
//3. 进入主循环
while (!viewer->wasStopped())
{
// 如果点云不断更新,在这里添加点云
// 如果需要改变视角,在这里设置相机
viewer->spinOnce(100, true);
Sleep(100);
}
}
演示结果: