PCDLoader.cpp
Go to the documentation of this file.
00001 // -*- C++ -*-
00010 #include <pcl/point_types.h>
00011 #include <pcl/filters/statistical_outlier_removal.h>
00012 #include "PCDLoader.h"
00013 #include "hrpsys/idl/pointcloud.hh"
00014 
00015 // Module specification
00016 // <rtc-template block="module_spec">
00017 static const char* spec[] =
00018   {
00019     "implementation_id", "PCDLoader",
00020     "type_name",         "PCDLoader",
00021     "description",       "PCD file loader",
00022     "version",           HRPSYS_PACKAGE_VERSION,
00023     "vendor",            "AIST",
00024     "category",          "example",
00025     "activity_type",     "DataFlowComponent",
00026     "max_instance",      "10",
00027     "language",          "C++",
00028     "lang_type",         "compile",
00029     // Configuration variables
00030     "conf.default.path",  "",
00031     "conf.default.fields","XYZ",
00032 
00033     ""
00034   };
00035 // </rtc-template>
00036 
00037 PCDLoader::PCDLoader(RTC::Manager* manager)
00038   : RTC::DataFlowComponentBase(manager),
00039     // <rtc-template block="initializer">
00040     m_offsetIn("offset", m_offset),
00041     m_isOutputOut("isOutput", m_isOutput),
00042     m_cloudOut("cloud", m_cloud),
00043     // </rtc-template>
00044     m_PCDLoaderServicePort("PCDLoaderService"),
00045     dummy(0)
00046 {
00047 }
00048 
00049 PCDLoader::~PCDLoader()
00050 {
00051 }
00052 
00053 
00054 
00055 RTC::ReturnCode_t PCDLoader::onInitialize()
00056 {
00057     //std::cout << m_profile.instance_name << ": onInitialize()" << std::endl;
00058     // <rtc-template block="bind_config">
00059     // Bind variables and configuration variable
00060     bindParameter("path", m_path, "");
00061     bindParameter("fields", m_fields, "XYZ");
00062     
00063     // </rtc-template>
00064     
00065     // Registration: InPort/OutPort/Service
00066     // <rtc-template block="registration">
00067     // Set InPort buffers
00068     addInPort("offsetIn", m_offsetIn);
00069     
00070     // Set OutPort buffer
00071     addOutPort("isOutputOut", m_isOutputOut);
00072     addOutPort("cloudOut", m_cloudOut);
00073     
00074     // Set service provider to Ports
00075     m_PCDLoaderServicePort.registerProvider("service0", "PCDLoaderService", m_service0);
00076     addPort(m_PCDLoaderServicePort);
00077     m_service0.setComp(this);
00078     m_isOutput.data = true;
00079     
00080     // Set service consumers to Ports
00081     
00082     // Set CORBA Service Ports
00083     
00084     // </rtc-template>
00085     
00086     RTC::Properties& prop = getProperties();
00087     
00088     return RTC::RTC_OK;
00089 }
00090 
00091 void PCDLoader::setCloudXYZ(PointCloudTypes::PointCloud& cloud, const pcl::PointCloud<pcl::PointXYZ>::Ptr cloud_raw)
00092 {
00093     int npoint = cloud_raw->points.size();
00094     
00095     cloud.type = "xyz";
00096     cloud.fields.length(3);
00097     cloud.fields[0].name = "x";
00098     cloud.fields[0].offset = 0;
00099     cloud.fields[0].data_type = PointCloudTypes::FLOAT32;
00100     cloud.fields[0].count = 4;
00101     cloud.fields[1].name = "y";
00102     cloud.fields[1].offset = 4;
00103     cloud.fields[1].data_type = PointCloudTypes::FLOAT32;
00104     cloud.fields[1].count = 4;
00105     cloud.fields[2].name = "z";
00106     cloud.fields[2].offset = 8;
00107     cloud.fields[2].data_type = PointCloudTypes::FLOAT32;
00108     cloud.fields[2].count = 4;
00109     cloud.is_bigendian = false;
00110     cloud.point_step = 16;
00111     cloud.width = cloud_raw->width;
00112     cloud.height = cloud_raw->height;
00113     cloud.data.length(npoint*cloud.point_step);
00114     cloud.row_step = cloud.width*cloud.point_step;
00115     cloud.is_dense = cloud_raw->is_dense;
00116     float *ptr = (float *)cloud.data.get_buffer();
00117     std::cout << "npoint = " << npoint << std::endl;
00118     for (int i=0; i<npoint; i++){
00119         ptr[0] = cloud_raw->points[i].x;
00120         ptr[1] = cloud_raw->points[i].y;
00121         ptr[2] = cloud_raw->points[i].z;
00122         ptr += 4;
00123     }
00124 }
00125 
00126 void PCDLoader::setCloudXYZRGB(PointCloudTypes::PointCloud& cloud, const pcl::PointCloud<pcl::PointXYZRGB>::Ptr cloud_raw)
00127 {
00128     int npoint = cloud_raw->points.size();
00129     
00130     cloud.type = "xyzrgb";
00131     cloud.fields.length(6);
00132     cloud.fields[0].name = "x";
00133     cloud.fields[0].offset = 0;
00134     cloud.fields[0].data_type = PointCloudTypes::FLOAT32;
00135     cloud.fields[0].count = 4;
00136     cloud.fields[1].name = "y";
00137     cloud.fields[1].offset = 4;
00138     cloud.fields[1].data_type = PointCloudTypes::FLOAT32;
00139     cloud.fields[1].count = 4;
00140     cloud.fields[2].name = "z";
00141     cloud.fields[2].offset = 8;
00142     cloud.fields[2].data_type = PointCloudTypes::FLOAT32;
00143     cloud.fields[2].count = 4;
00144     cloud.fields[3].name = "r";
00145     cloud.fields[3].offset = 12;
00146     cloud.fields[3].data_type = PointCloudTypes::UINT8;
00147     cloud.fields[3].count = 1;
00148     cloud.fields[4].name = "g";
00149     cloud.fields[4].offset = 13;
00150     cloud.fields[4].data_type = PointCloudTypes::UINT8;
00151     cloud.fields[4].count = 1;
00152     cloud.fields[5].name = "b";
00153     cloud.fields[5].offset = 14;
00154     cloud.fields[5].data_type = PointCloudTypes::UINT8;
00155     cloud.fields[5].count = 1;
00156     cloud.is_bigendian = false;
00157     cloud.point_step = 16;
00158     cloud.width = cloud_raw->width;
00159     cloud.height = cloud_raw->height;
00160     cloud.data.length(npoint*cloud.point_step);
00161     cloud.row_step = cloud.width*cloud.point_step;
00162     cloud.is_dense = cloud_raw->is_dense;
00163     float *ptr = (float *)cloud.data.get_buffer();
00164     std::cout << "npoint = " << npoint << std::endl;
00165     for (int i=0; i<npoint; i++){
00166         ptr[0] = cloud_raw->points[i].x;
00167         ptr[1] = cloud_raw->points[i].y;
00168         ptr[2] = cloud_raw->points[i].z;
00169         unsigned char *rgb = (unsigned char *)(ptr+3);
00170         rgb[0] = cloud_raw->points[i].r;
00171         rgb[1] = cloud_raw->points[i].g;
00172         rgb[2] = cloud_raw->points[i].b;
00173         ptr += 4;
00174     }
00175 }
00176 
00177 void PCDLoader::updateOffsetToCloudXYZ(void)
00178 {
00179     pcl::PointCloud<pcl::PointXYZ>::Ptr clouds(new pcl::PointCloud<pcl::PointXYZ>);
00180     for (unsigned int i=0; i<m_offset.length(); i++){
00181         const OpenHRP::PCDOffset& offset = m_offset[i];
00182         const std::string label(offset.label);
00183         if( m_clouds_xyz.find(label) != m_clouds_xyz.end() ){
00184             const hrp::Vector3 center(offset.center.x, offset.center.y, offset.center.z);
00185             const hrp::Vector3 offsetP(offset.data.position.x, offset.data.position.y, offset.data.position.z);
00186             const hrp::Matrix33 offsetR(hrp::rotFromRpy(offset.data.orientation.r,
00187                                                         offset.data.orientation.p,
00188                                                         offset.data.orientation.y));
00189             
00190             pcl::PointCloud<pcl::PointXYZ>::Ptr cloud_raw = m_clouds_xyz[label];
00191             pcl::PointCloud<pcl::PointXYZ>::Ptr cloud_new(new pcl::PointCloud<pcl::PointXYZ>);
00192             int npoint = cloud_raw->points.size();
00193             cloud_new->points.resize(npoint);
00194             cloud_new->width = cloud_raw->width;
00195             cloud_new->height = cloud_raw->height;
00196             cloud_new->is_dense = cloud_raw->is_dense;
00197             hrp::Vector3 point, point_new;
00198             for(int j = 0 ; j < npoint ; j++){
00199                 point << cloud_raw->points[j].x, cloud_raw->points[j].y, cloud_raw->points[j].z;
00200                 point_new = offsetR * (point - center) + center + offsetP;
00201                 cloud_new->points[j].x = point_new[0];
00202                 cloud_new->points[j].y = point_new[1];
00203                 cloud_new->points[j].z = point_new[2];
00204             }
00205             *clouds += *cloud_new;
00206         }
00207     }
00208     setCloudXYZ(m_cloud, clouds);
00209 }
00210 
00211 void PCDLoader::updateOffsetToCloudXYZRGB(void)
00212 {
00213     pcl::PointCloud<pcl::PointXYZRGB>::Ptr clouds(new pcl::PointCloud<pcl::PointXYZRGB>);
00214     for (unsigned int i=0; i<m_offset.length(); i++){
00215         const OpenHRP::PCDOffset& offset = m_offset[i];
00216         const std::string label(offset.label);
00217         if( m_clouds_xyzrgb.find(label) != m_clouds_xyzrgb.end() ){
00218             const hrp::Vector3 center(offset.center.x, offset.center.y, offset.center.z);
00219             const hrp::Vector3 offsetP(offset.data.position.x, offset.data.position.y, offset.data.position.z);
00220             const hrp::Matrix33 offsetR(hrp::rotFromRpy(offset.data.orientation.r,
00221                                                         offset.data.orientation.p,
00222                                                         offset.data.orientation.y));
00223             
00224             pcl::PointCloud<pcl::PointXYZRGB>::Ptr cloud_raw = m_clouds_xyzrgb[label];
00225             pcl::PointCloud<pcl::PointXYZRGB>::Ptr cloud_new(new pcl::PointCloud<pcl::PointXYZRGB>);
00226             int npoint = cloud_raw->points.size();
00227             cloud_new->points.resize(npoint);
00228             cloud_new->width = cloud_raw->width;
00229             cloud_new->height = cloud_raw->height;
00230             cloud_new->is_dense = cloud_raw->is_dense;
00231             hrp::Vector3 point, point_new;
00232             for(int j = 0 ; j < npoint ; j++){
00233                 point << cloud_raw->points[j].x, cloud_raw->points[j].y, cloud_raw->points[j].z;
00234                 point_new = offsetR * (point - center) + center + offsetP;
00235                 cloud_new->points[j].x = point_new[0];
00236                 cloud_new->points[j].y = point_new[1];
00237                 cloud_new->points[j].z = point_new[2];
00238             }
00239             *clouds += *cloud_new;
00240         }
00241     }
00242     setCloudXYZRGB(m_cloud, clouds);
00243 }
00244 
00245 /*
00246 RTC::ReturnCode_t PCDLoader::onFinalize()
00247 {
00248   return RTC::RTC_OK;
00249 }
00250 */
00251 
00252 /*
00253 RTC::ReturnCode_t PCDLoader::onStartup(RTC::UniqueId ec_id)
00254 {
00255   return RTC::RTC_OK;
00256 }
00257 */
00258 
00259 /*
00260 RTC::ReturnCode_t PCDLoader::onShutdown(RTC::UniqueId ec_id)
00261 {
00262   return RTC::RTC_OK;
00263 }
00264 */
00265 
00266 RTC::ReturnCode_t PCDLoader::onActivated(RTC::UniqueId ec_id)
00267 {
00268     std::cout << m_profile.instance_name<< ": onActivated(" << ec_id << ")" << std::endl;
00269     return RTC::RTC_OK;
00270 }
00271 
00272 RTC::ReturnCode_t PCDLoader::onDeactivated(RTC::UniqueId ec_id)
00273 {
00274     std::cout << m_profile.instance_name<< ": onDeactivated(" << ec_id << ")" << std::endl;
00275     return RTC::RTC_OK;
00276 }
00277 
00278 RTC::ReturnCode_t PCDLoader::onExecute(RTC::UniqueId ec_id)
00279 {
00280     //std::cout << m_profile.instance_name<< ": onExecute(" << ec_id << ")" << std::endl;
00281     if ( !m_path.empty()){
00282         pcl::PCDReader reader;
00283         
00284         if (m_fields=="XYZ"){
00285             pcl::PointCloud<pcl::PointXYZ>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZ>);
00286             if (reader.read (m_path, *cloud)){
00287                 std::cerr << m_profile.instance_name << ": failed to load("
00288                           << m_path << ")" << std::endl;
00289                 m_path = "";
00290                 return RTC::RTC_OK;
00291             }
00292             setCloudXYZ(m_cloud, cloud);
00293         }else if(m_fields=="XYZRGB"){
00294             pcl::PointCloud<pcl::PointXYZRGB>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZRGB>);
00295             if (reader.read (m_path, *cloud)){
00296                 std::cerr << m_profile.instance_name << ": failed to load("
00297                           << m_path << ")" << std::endl;
00298                 m_path = "";
00299                 return RTC::RTC_OK;
00300             }
00301             setCloudXYZRGB(m_cloud, cloud);
00302         }else{
00303             std::cerr << "fields[" << m_fields << "] is not supported" << std::endl;
00304         }
00305         m_cloudOut.write();
00306         m_isOutputOut.write();
00307         m_path = "";
00308     }
00309     
00310     if( m_offsetIn.isNew() ){
00311         m_offsetIn.read();
00312         if( !m_clouds_xyz.empty() ){
00313             updateOffsetToCloudXYZ();
00314             m_cloudOut.write();
00315             m_isOutputOut.write();
00316         }
00317         else if( !m_clouds_xyzrgb.empty() ){
00318             updateOffsetToCloudXYZRGB();
00319             m_cloudOut.write();
00320             m_isOutputOut.write();
00321         }
00322     }
00323     
00324     return RTC::RTC_OK;
00325 }
00326 
00327 /*
00328 RTC::ReturnCode_t PCDLoader::onAborting(RTC::UniqueId ec_id)
00329 {
00330   return RTC::RTC_OK;
00331 }
00332 */
00333 
00334 /*
00335 RTC::ReturnCode_t PCDLoader::onError(RTC::UniqueId ec_id)
00336 {
00337   return RTC::RTC_OK;
00338 }
00339 */
00340 
00341 /*
00342 RTC::ReturnCode_t PCDLoader::onReset(RTC::UniqueId ec_id)
00343 {
00344   return RTC::RTC_OK;
00345 }
00346 */
00347 
00348 /*
00349 RTC::ReturnCode_t PCDLoader::onStateUpdate(RTC::UniqueId ec_id)
00350 {
00351   return RTC::RTC_OK;
00352 }
00353 */
00354 
00355 /*
00356 RTC::ReturnCode_t PCDLoader::onRateChanged(RTC::UniqueId ec_id)
00357 {
00358   return RTC::RTC_OK;
00359 }
00360 */
00361 
00362 
00363 bool PCDLoader::load(const std::string& filename, const std::string& label)
00364 {
00365     pcl::PCDReader reader;
00366     if( m_fields == "XYZ" ){
00367         pcl::PointCloud<pcl::PointXYZ>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZ>);
00368         if (reader.read (filename, *cloud)){
00369             std::cerr << m_profile.instance_name << ": failed to load("
00370                       << filename << ")" << std::endl;
00371             return RTC::RTC_OK;
00372         }
00373         else
00374             std::cout << "Loading " << filename << " with XYZ fields." << std::endl;
00375         m_clouds_xyz[label] = cloud;
00376     }
00377     else if( m_fields == "XYZRGB" ){
00378         pcl::PointCloud<pcl::PointXYZRGB>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZRGB>);
00379         if (reader.read (filename, *cloud)){
00380             std::cerr << m_profile.instance_name << ": failed to load("
00381                       << filename << ")" << std::endl;
00382             return RTC::RTC_OK;
00383         }
00384         else
00385             std::cout << "Loading " << filename << " with XYZRGB fields." << std::endl;
00386         m_clouds_xyzrgb[label] = cloud;
00387     }else{
00388         std::cerr << "fields[" << m_fields << "] is not supported" << std::endl;
00389     }
00390 }
00391 
00392 extern "C"
00393 {
00394     
00395     void PCDLoaderInit(RTC::Manager* manager)
00396     {
00397         RTC::Properties profile(spec);
00398         manager->registerFactory(profile,
00399                                  RTC::Create<PCDLoader>,
00400                                  RTC::Delete<PCDLoader>);
00401     }
00402     
00403 };
00404 
00405 


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