Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[PointCloudViewer] Support color pointcloud view #1144

Merged
merged 1 commit into from
May 25, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 42 additions & 10 deletions rtc/PointCloudViewer/PointCloudViewer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include <pcl/filters/voxel_grid.h>
#include "PointCloudViewer.h"
#include "hrpsys/idl/pointcloud.hh"
#include <string>

// Module specification
// <rtc-template block="module_spec">
Expand Down Expand Up @@ -119,18 +120,49 @@ RTC::ReturnCode_t PointCloudViewer::onExecute(RTC::UniqueId ec_id)
if (m_cloudIn.isNew()){
m_cloudIn.read();

pcl::PointCloud<pcl::PointXYZ>::Ptr cloud (new pcl::PointCloud<pcl::PointXYZ>);
cloud->points.resize(m_cloud.width*m_cloud.height);
float *src = (float *)m_cloud.data.get_buffer();
for (unsigned int i=0; i<cloud->points.size(); i++){
cloud->points[i].x = src[0];
cloud->points[i].y = src[1];
cloud->points[i].z = src[2];
src += 4;
bool is_color_points = false;
for (int i = 0; i < m_cloud.fields.length(); i++) {
std::string tmp_name(m_cloud.fields[i].name);
if (tmp_name.find("r") != std::string::npos || tmp_name.find("g") != std::string::npos || tmp_name.find("b") != std::string::npos) {
is_color_points = true; // color pointcloud should have rgb field
}
}

if (!m_viewer.wasStopped()){
m_viewer.showCloud(cloud);
// currently only support PointXYZ and PointXYZRGB
if (is_color_points) {
pcl::PointCloud<pcl::PointXYZRGB>::Ptr cloud (new pcl::PointCloud<pcl::PointXYZRGB>);
cloud->is_dense = m_cloud.is_dense;
float *src = reinterpret_cast<float*>(m_cloud.data.get_buffer());
for (unsigned int i = 0; i< (m_cloud.width * m_cloud.height); i++){
pcl::PointXYZRGB tmp_point;
tmp_point.x = src[0];
tmp_point.y = src[1];
tmp_point.z = src[2];
uint32_t rgb = *reinterpret_cast<uint32_t*>(&(src[3])); // http:https://docs.pointclouds.org/1.7.2/a01059.html#a1678cfbe6e832aa61ec0de773cab15ae
tmp_point.r = (uint8_t)((rgb >> 16) & 0x0000ff);
tmp_point.g = (uint8_t)((rgb >> 8) & 0x0000ff);
tmp_point.b = (uint8_t)((rgb) & 0x0000ff);
if (m_cloud.is_dense || rgb != 0) { // rgb == 0 means invalid point
cloud->push_back(tmp_point);
}
src += 4;
}
if (!m_viewer.wasStopped()){
m_viewer.showCloud(cloud);
}
} else {
pcl::PointCloud<pcl::PointXYZ>::Ptr cloud (new pcl::PointCloud<pcl::PointXYZ>);
cloud->points.resize(m_cloud.width*m_cloud.height);
float *src = (float *)m_cloud.data.get_buffer();
for (unsigned int i=0; i<cloud->points.size(); i++){
cloud->points[i].x = src[0];
cloud->points[i].y = src[1];
cloud->points[i].z = src[2];
src += 4;
}
if (!m_viewer.wasStopped()){
m_viewer.showCloud(cloud);
}
}
}

Expand Down