环境配置以及项目CMakeLists.txt文件参见
安装 PCL + OpenGL + GLFW
https://blog.csdn.net/u012180635/article/details/85232036
安装 RealSense2 + CMake + OpenCV
https://blog.csdn.net/u012180635/article/details/82143340
源码来自RealSense源码包 librealsense/wrappers/pcl/pcl/rs-pcl.cpp
rs-pcl.cpp内容如下:
// License: Apache 2.0. See LICENSE file in root directory.
// Copyright(c) 2015-2017 Intel Corporation. All Rights Reserved.
#include // Include RealSense Cross Platform API
//#include "example.hpp" // Include short list of convenience functions for rendering
#include
#include
#include
// Struct for managing rotation of pointcloud view
struct state {
state() : yaw(0.0), pitch(0.0), last_x(0.0), last_y(0.0),
ml(false), offset_x(0.0f), offset_y(0.0f) {}
double yaw, pitch, last_x, last_y; bool ml; float offset_x, offset_y;
};
using pcl_ptr = pcl::PointCloud::Ptr;
// Helper functions
void register_glfw_callbacks(window& app, state& app_state);
void draw_pointcloud(window& app, state& app_state, const std::vector& points);
pcl_ptr points_to_pcl(const rs2::points& points)
{
pcl_ptr cloud(new pcl::PointCloud);
auto sp = points.get_profile().as();
cloud->width = sp.width();
cloud->height = sp.height();
cloud->is_dense = false;
cloud->points.resize(points.size());
auto ptr = points.get_vertices();
for (auto& p : cloud->points)
{
p.x = ptr->x;
p.y = ptr->y;
p.z = ptr->z;
ptr++;
}
return cloud;
}
float3 colors[] { { 0.8f, 0.1f, 0.3f },
{ 0.1f, 0.9f, 0.5f },
};
int main(int argc, char * argv[]) try
{
// Create a simple OpenGL window for rendering:
window app(1280, 720, "RealSense PCL Pointcloud Example");
// Construct an object to manage view state
state app_state;
// register callbacks to allow manipulation of the pointcloud
register_glfw_callbacks(app, app_state);
// Declare pointcloud object, for calculating pointclouds and texture mappings
rs2::pointcloud pc;
// We want the points object to be persistent so we can display the last cloud when a frame drops
rs2::points points;
// Declare RealSense pipeline, encapsulating the actual device and sensors
rs2::pipeline pipe;
// Start streaming with default recommended configuration
pipe.start();
// Wait for the next set of frames from the camera
auto frames = pipe.wait_for_frames();
auto depth = frames.get_depth_frame();
// Generate the pointcloud and texture mappings
points = pc.calculate(depth);
auto pcl_points = points_to_pcl(points);
pcl_ptr cloud_filtered(new pcl::PointCloud);
pcl::PassThrough pass;
pass.setInputCloud(pcl_points);
pass.setFilterFieldName("z");
pass.setFilterLimits(0.0, 1.0);
pass.filter(*cloud_filtered);
std::vector layers;
layers.push_back(pcl_points);
layers.push_back(cloud_filtered);
while (app) // Application still alive?
{
draw_pointcloud(app, app_state, layers);
}
return EXIT_SUCCESS;
}
catch (const rs2::error & e)
{
std::cerr << "RealSense error calling " << e.get_failed_function() << "(" << e.get_failed_args() << "):\n " << e.what() << std::endl;
return EXIT_FAILURE;
}
catch (const std::exception & e)
{
std::cerr << e.what() << std::endl;
return EXIT_FAILURE;
}
// Registers the state variable and callbacks to allow mouse control of the pointcloud
void register_glfw_callbacks(window& app, state& app_state)
{
app.on_left_mouse = [&](bool pressed)
{
app_state.ml = pressed;
};
app.on_mouse_scroll = [&](double xoffset, double yoffset)
{
app_state.offset_x += static_cast(xoffset);
app_state.offset_y += static_cast(yoffset);
};
app.on_mouse_move = [&](double x, double y)
{
if (app_state.ml)
{
app_state.yaw -= (x - app_state.last_x);
app_state.yaw = std::max(app_state.yaw, -120.0);
app_state.yaw = std::min(app_state.yaw, +120.0);
app_state.pitch += (y - app_state.last_y);
app_state.pitch = std::max(app_state.pitch, -80.0);
app_state.pitch = std::min(app_state.pitch, +80.0);
}
app_state.last_x = x;
app_state.last_y = y;
};
app.on_key_release = [&](int key)
{
if (key == 32) // Escape
{
app_state.yaw = app_state.pitch = 0; app_state.offset_x = app_state.offset_y = 0.0;
}
};
}
// Handles all the OpenGL calls needed to display the point cloud
void draw_pointcloud(window& app, state& app_state, const std::vector& points)
{
// OpenGL commands that prep screen for the pointcloud
glPopMatrix();
glPushAttrib(GL_ALL_ATTRIB_BITS);
float width = app.width(), height = app.height();
glClearColor(153.f / 255, 153.f / 255, 153.f / 255, 1);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_PROJECTION);
glPushMatrix();
gluPerspective(60, width / height, 0.01f, 10.0f);
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
gluLookAt(0, 0, 0, 0, 0, 1, 0, -1, 0);
glTranslatef(0, 0, +0.5f + app_state.offset_y*0.05f);
glRotated(app_state.pitch, 1, 0, 0);
glRotated(app_state.yaw, 0, 1, 0);
glTranslatef(0, 0, -0.5f);
glPointSize(width / 640);
glEnable(GL_TEXTURE_2D);
int color = 0;
for (auto&& pc : points)
{
auto c = colors[(color++) % (sizeof(colors) / sizeof(float3))];
glBegin(GL_POINTS);
glColor3f(c.x, c.y, c.z);
/* this segment actually prints the pointcloud */
for (int i = 0; i < pc->points.size(); i++)
{
auto&& p = pc->points[i];
if (p.z)
{
// upload the point and texture coordinates only for points we have depth data for
glVertex3f(p.x, p.y, p.z);
}
}
glEnd();
}
// OpenGL cleanup
glPopMatrix();
glMatrixMode(GL_PROJECTION);
glPopMatrix();
glPopAttrib();
glPushMatrix();
}
CMakeLists.txt文件如下
# minimum required cmake version: 3.1.0
cmake_minimum_required(VERSION 3.1.0)
project(rs-pcl)
# Save the command line compile commands in the build output
set(CMAKE_EXPORT_COMPILE_COMMANDS 1)
include(CheckCXXCompilerFlag)
CHECK_CXX_COMPILER_FLAG("-std=c++11" COMPILER_SUPPORTS_CXX11)
CHECK_CXX_COMPILER_FLAG("-std=c++0x" COMPILER_SUPPORTS_CXX0X)
if(COMPILER_SUPPORTS_CXX11)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
elseif(COMPILER_SUPPORTS_CXX0X)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++0x")
endif()
add_executable(rs-pcl rs-pcl.cpp)
target_link_libraries(rs-pcl ${DEPENDENCIES})
set_target_properties (rs-pcl PROPERTIES FOLDER "rs-pcl")