PointCloudViewer.cpp
Go to the documentation of this file.
00001 // -*- C++ -*-
00010 #include <pcl/io/pcd_io.h>
00011 #include <pcl/point_types.h>
00012 #include <pcl/filters/voxel_grid.h>
00013 #include "PointCloudViewer.h"
00014 #include "hrpsys/idl/pointcloud.hh"
00015 #include <string>
00016 
00017 // Module specification
00018 // <rtc-template block="module_spec">
00019 static const char* spec[] =
00020   {
00021     "implementation_id", "PointCloudViewer",
00022     "type_name",         "PointCloudViewer",
00023     "description",       "Point Cloud Viewer",
00024     "version",           HRPSYS_PACKAGE_VERSION,
00025     "vendor",            "AIST",
00026     "category",          "example",
00027     "activity_type",     "DataFlowComponent",
00028     "max_instance",      "10",
00029     "language",          "C++",
00030     "lang_type",         "compile",
00031     // Configuration variables
00032 
00033     ""
00034   };
00035 // </rtc-template>
00036 
00037 PointCloudViewer::PointCloudViewer(RTC::Manager* manager)
00038   : RTC::DataFlowComponentBase(manager),
00039     // <rtc-template block="initializer">
00040     m_cloudIn("cloud", m_cloud),
00041     // </rtc-template>
00042     m_viewer("Point Cloud Viewer"),
00043     dummy(0)
00044 {
00045 }
00046 
00047 PointCloudViewer::~PointCloudViewer()
00048 {
00049 }
00050 
00051 
00052 
00053 RTC::ReturnCode_t PointCloudViewer::onInitialize()
00054 {
00055   //std::cout << m_profile.instance_name << ": onInitialize()" << std::endl;
00056   // <rtc-template block="bind_config">
00057   // Bind variables and configuration variable
00058   
00059   // </rtc-template>
00060 
00061   // Registration: InPort/OutPort/Service
00062   // <rtc-template block="registration">
00063   // Set InPort buffers
00064   addInPort("cloudIn", m_cloudIn);
00065 
00066   // Set OutPort buffer
00067   
00068   // Set service provider to Ports
00069   
00070   // Set service consumers to Ports
00071   
00072   // Set CORBA Service Ports
00073   
00074   // </rtc-template>
00075 
00076   RTC::Properties& prop = getProperties();
00077 
00078   return RTC::RTC_OK;
00079 }
00080 
00081 
00082 
00083 /*
00084 RTC::ReturnCode_t PointCloudViewer::onFinalize()
00085 {
00086   return RTC::RTC_OK;
00087 }
00088 */
00089 
00090 /*
00091 RTC::ReturnCode_t PointCloudViewer::onStartup(RTC::UniqueId ec_id)
00092 {
00093   return RTC::RTC_OK;
00094 }
00095 */
00096 
00097 /*
00098 RTC::ReturnCode_t PointCloudViewer::onShutdown(RTC::UniqueId ec_id)
00099 {
00100   return RTC::RTC_OK;
00101 }
00102 */
00103 
00104 RTC::ReturnCode_t PointCloudViewer::onActivated(RTC::UniqueId ec_id)
00105 {
00106   std::cout << m_profile.instance_name<< ": onActivated(" << ec_id << ")" << std::endl;
00107   return RTC::RTC_OK;
00108 }
00109 
00110 RTC::ReturnCode_t PointCloudViewer::onDeactivated(RTC::UniqueId ec_id)
00111 {
00112   std::cout << m_profile.instance_name<< ": onDeactivated(" << ec_id << ")" << std::endl;
00113   return RTC::RTC_OK;
00114 }
00115 
00116 RTC::ReturnCode_t PointCloudViewer::onExecute(RTC::UniqueId ec_id)
00117 {
00118   //std::cout << m_profile.instance_name<< ": onExecute(" << ec_id << ")" << std::endl;
00119 
00120   if (m_cloudIn.isNew()){
00121     m_cloudIn.read();
00122 
00123     bool is_color_points = false;
00124     for (int i = 0; i < m_cloud.fields.length(); i++) {
00125         std::string tmp_name(m_cloud.fields[i].name);
00126         if (tmp_name.find("r") != std::string::npos || tmp_name.find("g") != std::string::npos || tmp_name.find("b") != std::string::npos) {
00127             is_color_points = true; // color pointcloud should have rgb field 
00128         }
00129     }
00130 
00131     // currently only support PointXYZ and PointXYZRGB
00132     if (is_color_points) { 
00133         pcl::PointCloud<pcl::PointXYZRGB>::Ptr cloud (new pcl::PointCloud<pcl::PointXYZRGB>);
00134         cloud->is_dense = m_cloud.is_dense; // need to handle pointcloud which has invalid points (is_dense = false)
00135         cloud->points.resize(m_cloud.width*m_cloud.height);
00136         float *src = reinterpret_cast<float*>(m_cloud.data.get_buffer());
00137         for (unsigned int i = 0; i< cloud->points.size(); i++) {
00138             cloud->points[i].x = src[0];
00139             cloud->points[i].y = src[1];
00140             cloud->points[i].z = src[2];
00141             unsigned char *rgb = (unsigned char *)(src+3);
00142             cloud->points[i].r = rgb[0];
00143             cloud->points[i].g = rgb[1];
00144             cloud->points[i].b = rgb[2];
00145             src += 4;
00146         }
00147         if (!m_viewer.wasStopped()){
00148             m_viewer.showCloud(cloud);
00149         }
00150     } else {
00151         pcl::PointCloud<pcl::PointXYZ>::Ptr cloud (new pcl::PointCloud<pcl::PointXYZ>);
00152         cloud->points.resize(m_cloud.width*m_cloud.height);
00153         float *src = (float *)m_cloud.data.get_buffer();
00154         for (unsigned int i=0; i<cloud->points.size(); i++){
00155             cloud->points[i].x = src[0];
00156             cloud->points[i].y = src[1];
00157             cloud->points[i].z = src[2];
00158             src += 4;
00159         }
00160         if (!m_viewer.wasStopped()){
00161             m_viewer.showCloud(cloud);
00162         }        
00163     }
00164   }
00165 
00166   return RTC::RTC_OK;
00167 }
00168 
00169 /*
00170 RTC::ReturnCode_t PointCloudViewer::onAborting(RTC::UniqueId ec_id)
00171 {
00172   return RTC::RTC_OK;
00173 }
00174 */
00175 
00176 /*
00177 RTC::ReturnCode_t PointCloudViewer::onError(RTC::UniqueId ec_id)
00178 {
00179   return RTC::RTC_OK;
00180 }
00181 */
00182 
00183 /*
00184 RTC::ReturnCode_t PointCloudViewer::onReset(RTC::UniqueId ec_id)
00185 {
00186   return RTC::RTC_OK;
00187 }
00188 */
00189 
00190 /*
00191 RTC::ReturnCode_t PointCloudViewer::onStateUpdate(RTC::UniqueId ec_id)
00192 {
00193   return RTC::RTC_OK;
00194 }
00195 */
00196 
00197 /*
00198 RTC::ReturnCode_t PointCloudViewer::onRateChanged(RTC::UniqueId ec_id)
00199 {
00200   return RTC::RTC_OK;
00201 }
00202 */
00203 
00204 
00205 
00206 extern "C"
00207 {
00208 
00209   void PointCloudViewerInit(RTC::Manager* manager)
00210   {
00211     RTC::Properties profile(spec);
00212     manager->registerFactory(profile,
00213                              RTC::Create<PointCloudViewer>,
00214                              RTC::Delete<PointCloudViewer>);
00215   }
00216 
00217 };
00218 
00219 


hrpsys
Author(s): AIST, Fumio Kanehiro
autogenerated on Wed May 15 2019 05:02:18