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             cloud->points[i].rgb = src[3]; // use float rgb union
00142             src += 4;
00143         }
00144         if (!m_viewer.wasStopped()){
00145             m_viewer.showCloud(cloud);
00146         }
00147     } else {
00148         pcl::PointCloud<pcl::PointXYZ>::Ptr cloud (new pcl::PointCloud<pcl::PointXYZ>);
00149         cloud->points.resize(m_cloud.width*m_cloud.height);
00150         float *src = (float *)m_cloud.data.get_buffer();
00151         for (unsigned int i=0; i<cloud->points.size(); i++){
00152             cloud->points[i].x = src[0];
00153             cloud->points[i].y = src[1];
00154             cloud->points[i].z = src[2];
00155             src += 4;
00156         }
00157         if (!m_viewer.wasStopped()){
00158             m_viewer.showCloud(cloud);
00159         }        
00160     }
00161   }
00162 
00163   return RTC::RTC_OK;
00164 }
00165 
00166 /*
00167 RTC::ReturnCode_t PointCloudViewer::onAborting(RTC::UniqueId ec_id)
00168 {
00169   return RTC::RTC_OK;
00170 }
00171 */
00172 
00173 /*
00174 RTC::ReturnCode_t PointCloudViewer::onError(RTC::UniqueId ec_id)
00175 {
00176   return RTC::RTC_OK;
00177 }
00178 */
00179 
00180 /*
00181 RTC::ReturnCode_t PointCloudViewer::onReset(RTC::UniqueId ec_id)
00182 {
00183   return RTC::RTC_OK;
00184 }
00185 */
00186 
00187 /*
00188 RTC::ReturnCode_t PointCloudViewer::onStateUpdate(RTC::UniqueId ec_id)
00189 {
00190   return RTC::RTC_OK;
00191 }
00192 */
00193 
00194 /*
00195 RTC::ReturnCode_t PointCloudViewer::onRateChanged(RTC::UniqueId ec_id)
00196 {
00197   return RTC::RTC_OK;
00198 }
00199 */
00200 
00201 
00202 
00203 extern "C"
00204 {
00205 
00206   void PointCloudViewerInit(RTC::Manager* manager)
00207   {
00208     RTC::Properties profile(spec);
00209     manager->registerFactory(profile,
00210                              RTC::Create<PointCloudViewer>,
00211                              RTC::Delete<PointCloudViewer>);
00212   }
00213 
00214 };
00215 
00216 


hrpsys
Author(s): AIST, Fumio Kanehiro
autogenerated on Wed Sep 6 2017 02:35:55