PCDLoader.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/statistical_outlier_removal.h>
00013 #include "PCDLoader.h"
00014 #include "hrpsys/idl/pointcloud.hh"
00015 
00016 // Module specification
00017 // <rtc-template block="module_spec">
00018 static const char* spec[] =
00019   {
00020     "implementation_id", "PCDLoader",
00021     "type_name",         "PCDLoader",
00022     "description",       "PCD file loader",
00023     "version",           HRPSYS_PACKAGE_VERSION,
00024     "vendor",            "AIST",
00025     "category",          "example",
00026     "activity_type",     "DataFlowComponent",
00027     "max_instance",      "10",
00028     "language",          "C++",
00029     "lang_type",         "compile",
00030     // Configuration variables
00031 
00032     ""
00033   };
00034 // </rtc-template>
00035 
00036 PCDLoader::PCDLoader(RTC::Manager* manager)
00037   : RTC::DataFlowComponentBase(manager),
00038     // <rtc-template block="initializer">
00039     m_cloudOut("cloud", m_cloud),
00040     // </rtc-template>
00041     dummy(0)
00042 {
00043 }
00044 
00045 PCDLoader::~PCDLoader()
00046 {
00047 }
00048 
00049 
00050 
00051 RTC::ReturnCode_t PCDLoader::onInitialize()
00052 {
00053   //std::cout << m_profile.instance_name << ": onInitialize()" << std::endl;
00054   // <rtc-template block="bind_config">
00055   // Bind variables and configuration variable
00056   
00057   // </rtc-template>
00058 
00059   // Registration: InPort/OutPort/Service
00060   // <rtc-template block="registration">
00061   // Set InPort buffers
00062 
00063   // Set OutPort buffer
00064   addOutPort("cloudOut", m_cloudOut);
00065   
00066   // Set service provider to Ports
00067   
00068   // Set service consumers to Ports
00069   
00070   // Set CORBA Service Ports
00071   
00072   // </rtc-template>
00073 
00074   RTC::Properties& prop = getProperties();
00075 
00076   return RTC::RTC_OK;
00077 }
00078 
00079 
00080 
00081 /*
00082 RTC::ReturnCode_t PCDLoader::onFinalize()
00083 {
00084   return RTC::RTC_OK;
00085 }
00086 */
00087 
00088 /*
00089 RTC::ReturnCode_t PCDLoader::onStartup(RTC::UniqueId ec_id)
00090 {
00091   return RTC::RTC_OK;
00092 }
00093 */
00094 
00095 /*
00096 RTC::ReturnCode_t PCDLoader::onShutdown(RTC::UniqueId ec_id)
00097 {
00098   return RTC::RTC_OK;
00099 }
00100 */
00101 
00102 RTC::ReturnCode_t PCDLoader::onActivated(RTC::UniqueId ec_id)
00103 {
00104   std::cout << m_profile.instance_name<< ": onActivated(" << ec_id << ")" << std::endl;
00105   return RTC::RTC_OK;
00106 }
00107 
00108 RTC::ReturnCode_t PCDLoader::onDeactivated(RTC::UniqueId ec_id)
00109 {
00110   std::cout << m_profile.instance_name<< ": onDeactivated(" << ec_id << ")" << std::endl;
00111   return RTC::RTC_OK;
00112 }
00113 
00114 RTC::ReturnCode_t PCDLoader::onExecute(RTC::UniqueId ec_id)
00115 {
00116   //std::cout << m_profile.instance_name<< ": onExecute(" << ec_id << ")" << std::endl;
00117   std::cerr << "PCD filename and fields: " << std::flush;
00118   std::string filename, fields="XYZ";
00119   std::cin >> filename >> fields;
00120   
00121   pcl::PCDReader reader;
00122 
00123   if (fields=="XYZ"){
00124       pcl::PointCloud<pcl::PointXYZ>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZ>);
00125       reader.read (filename, *cloud);
00126       int npoint = cloud->points.size();
00127 
00128       m_cloud.type = "xyz";
00129       m_cloud.fields.length(3);
00130       m_cloud.fields[0].name = "x";
00131       m_cloud.fields[0].offset = 0;
00132       m_cloud.fields[0].data_type = PointCloudTypes::FLOAT32;
00133       m_cloud.fields[0].count = 4;
00134       m_cloud.fields[1].name = "y";
00135       m_cloud.fields[1].offset = 4;
00136       m_cloud.fields[1].data_type = PointCloudTypes::FLOAT32;
00137       m_cloud.fields[1].count = 4;
00138       m_cloud.fields[2].name = "z";
00139       m_cloud.fields[2].offset = 8;
00140       m_cloud.fields[2].data_type = PointCloudTypes::FLOAT32;
00141       m_cloud.fields[2].count = 4;
00142       m_cloud.is_bigendian = false;
00143       m_cloud.point_step = 16;
00144       m_cloud.width = cloud->width;
00145       m_cloud.height = cloud->height;
00146       m_cloud.data.length(npoint*m_cloud.point_step);
00147       m_cloud.row_step = m_cloud.width*m_cloud.point_step;
00148       m_cloud.is_dense = cloud->is_dense;
00149       float *ptr = (float *)m_cloud.data.get_buffer();
00150       std::cout << "npoint = " << npoint << std::endl;
00151       for (int i=0; i<npoint; i++){
00152           ptr[0] = cloud->points[i].x;
00153           ptr[1] = cloud->points[i].y;
00154           ptr[2] = cloud->points[i].z;
00155           ptr += 4;
00156       }
00157   }else if(fields=="XYZRGB"){
00158       pcl::PointCloud<pcl::PointXYZRGB>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZRGB>);
00159       reader.read (filename, *cloud);
00160       int npoint = cloud->points.size();
00161 
00162       m_cloud.type = "xyzrgb";
00163       m_cloud.fields.length(6);
00164       m_cloud.fields[0].name = "x";
00165       m_cloud.fields[0].offset = 0;
00166       m_cloud.fields[0].data_type = PointCloudTypes::FLOAT32;
00167       m_cloud.fields[0].count = 4;
00168       m_cloud.fields[1].name = "y";
00169       m_cloud.fields[1].offset = 4;
00170       m_cloud.fields[1].data_type = PointCloudTypes::FLOAT32;
00171       m_cloud.fields[1].count = 4;
00172       m_cloud.fields[2].name = "z";
00173       m_cloud.fields[2].offset = 8;
00174       m_cloud.fields[2].data_type = PointCloudTypes::FLOAT32;
00175       m_cloud.fields[2].count = 4;
00176       m_cloud.fields[2].name = "r";
00177       m_cloud.fields[2].offset = 12;
00178       m_cloud.fields[2].data_type = PointCloudTypes::UINT8;
00179       m_cloud.fields[2].count = 1;
00180       m_cloud.fields[2].name = "g";
00181       m_cloud.fields[2].offset = 13;
00182       m_cloud.fields[2].data_type = PointCloudTypes::UINT8;
00183       m_cloud.fields[2].count = 1;
00184       m_cloud.fields[2].name = "b";
00185       m_cloud.fields[2].offset = 14;
00186       m_cloud.fields[2].data_type = PointCloudTypes::UINT8;
00187       m_cloud.fields[2].count = 1;
00188       m_cloud.is_bigendian = false;
00189       m_cloud.point_step = 16;
00190       m_cloud.width = cloud->width;
00191       m_cloud.height = cloud->height;
00192       m_cloud.data.length(npoint*m_cloud.point_step);
00193       m_cloud.row_step = m_cloud.width*m_cloud.point_step;
00194       m_cloud.is_dense = cloud->is_dense;
00195       float *ptr = (float *)m_cloud.data.get_buffer();
00196       std::cout << "npoint = " << npoint << std::endl;
00197       for (int i=0; i<npoint; i++){
00198           ptr[0] = cloud->points[i].x;
00199           ptr[1] = cloud->points[i].y;
00200           ptr[2] = cloud->points[i].z;
00201           unsigned char *rgb = (unsigned char *)(ptr+3);
00202           rgb[0] = cloud->points[i].r;
00203           rgb[1] = cloud->points[i].g;
00204           rgb[2] = cloud->points[i].b;
00205           ptr += 4;
00206       }
00207   }else{
00208       std::cerr << "fields[" << fields << "] is not supported" << std::endl;
00209   }
00210 
00211   m_cloudOut.write();
00212 
00213   return RTC::RTC_OK;
00214 }
00215 
00216 /*
00217 RTC::ReturnCode_t PCDLoader::onAborting(RTC::UniqueId ec_id)
00218 {
00219   return RTC::RTC_OK;
00220 }
00221 */
00222 
00223 /*
00224 RTC::ReturnCode_t PCDLoader::onError(RTC::UniqueId ec_id)
00225 {
00226   return RTC::RTC_OK;
00227 }
00228 */
00229 
00230 /*
00231 RTC::ReturnCode_t PCDLoader::onReset(RTC::UniqueId ec_id)
00232 {
00233   return RTC::RTC_OK;
00234 }
00235 */
00236 
00237 /*
00238 RTC::ReturnCode_t PCDLoader::onStateUpdate(RTC::UniqueId ec_id)
00239 {
00240   return RTC::RTC_OK;
00241 }
00242 */
00243 
00244 /*
00245 RTC::ReturnCode_t PCDLoader::onRateChanged(RTC::UniqueId ec_id)
00246 {
00247   return RTC::RTC_OK;
00248 }
00249 */
00250 
00251 
00252 
00253 extern "C"
00254 {
00255 
00256   void PCDLoaderInit(RTC::Manager* manager)
00257   {
00258     RTC::Properties profile(spec);
00259     manager->registerFactory(profile,
00260                              RTC::Create<PCDLoader>,
00261                              RTC::Delete<PCDLoader>);
00262   }
00263 
00264 };
00265 
00266 


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