SORFilter.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 "SORFilter.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", "SORFilter",
00021     "type_name",         "SORFilter",
00022     "description",       "Statistical Outlier Removal Filter",
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     "conf.default.meanK", "50",
00032     "conf.default.stddevMulThresh", "1.0",
00033 
00034     ""
00035   };
00036 // </rtc-template>
00037 
00038 SORFilter::SORFilter(RTC::Manager* manager)
00039   : RTC::DataFlowComponentBase(manager),
00040     // <rtc-template block="initializer">
00041     m_originalIn("original", m_original),
00042     m_filteredOut("filtered", m_filtered),
00043     // </rtc-template>
00044     dummy(0)
00045 {
00046 }
00047 
00048 SORFilter::~SORFilter()
00049 {
00050 }
00051 
00052 
00053 
00054 RTC::ReturnCode_t SORFilter::onInitialize()
00055 {
00056   //std::cout << m_profile.instance_name << ": onInitialize()" << std::endl;
00057   // <rtc-template block="bind_config">
00058   // Bind variables and configuration variable
00059   bindParameter("meanK", m_meanK, "50");
00060   bindParameter("stddevMulThresh", m_stddevMulThresh, "1.0");
00061   
00062   // </rtc-template>
00063 
00064   // Registration: InPort/OutPort/Service
00065   // <rtc-template block="registration">
00066   // Set InPort buffers
00067   addInPort("originalIn", m_originalIn);
00068 
00069   // Set OutPort buffer
00070   addOutPort("filteredOut", m_filteredOut);
00071   
00072   // Set service provider to Ports
00073   
00074   // Set service consumers to Ports
00075   
00076   // Set CORBA Service Ports
00077   
00078   // </rtc-template>
00079 
00080   RTC::Properties& prop = getProperties();
00081 
00082   m_filtered.height = 1;
00083   m_filtered.type = "xyz";
00084   m_filtered.fields.length(3);
00085   m_filtered.fields[0].name = "x";
00086   m_filtered.fields[0].offset = 0;
00087   m_filtered.fields[0].data_type = PointCloudTypes::FLOAT32;
00088   m_filtered.fields[0].count = 4;
00089   m_filtered.fields[1].name = "y";
00090   m_filtered.fields[1].offset = 4;
00091   m_filtered.fields[1].data_type = PointCloudTypes::FLOAT32;
00092   m_filtered.fields[1].count = 4;
00093   m_filtered.fields[2].name = "z";
00094   m_filtered.fields[2].offset = 8;
00095   m_filtered.fields[2].data_type = PointCloudTypes::FLOAT32;
00096   m_filtered.fields[2].count = 4;
00097   m_filtered.is_bigendian = false;
00098   m_filtered.point_step = 16;
00099   m_filtered.is_dense = true;
00100 
00101   return RTC::RTC_OK;
00102 }
00103 
00104 
00105 
00106 /*
00107 RTC::ReturnCode_t SORFilter::onFinalize()
00108 {
00109   return RTC::RTC_OK;
00110 }
00111 */
00112 
00113 /*
00114 RTC::ReturnCode_t SORFilter::onStartup(RTC::UniqueId ec_id)
00115 {
00116   return RTC::RTC_OK;
00117 }
00118 */
00119 
00120 /*
00121 RTC::ReturnCode_t SORFilter::onShutdown(RTC::UniqueId ec_id)
00122 {
00123   return RTC::RTC_OK;
00124 }
00125 */
00126 
00127 RTC::ReturnCode_t SORFilter::onActivated(RTC::UniqueId ec_id)
00128 {
00129   std::cout << m_profile.instance_name<< ": onActivated(" << ec_id << ")" << std::endl;
00130   return RTC::RTC_OK;
00131 }
00132 
00133 RTC::ReturnCode_t SORFilter::onDeactivated(RTC::UniqueId ec_id)
00134 {
00135   std::cout << m_profile.instance_name<< ": onDeactivated(" << ec_id << ")" << std::endl;
00136   return RTC::RTC_OK;
00137 }
00138 
00139 RTC::ReturnCode_t SORFilter::onExecute(RTC::UniqueId ec_id)
00140 {
00141   //std::cout << m_profile.instance_name<< ": onExecute(" << ec_id << ")" << std::endl;
00142 
00143   if (m_originalIn.isNew()){
00144     m_originalIn.read();
00145 
00146     pcl::PointCloud<pcl::PointXYZ>::Ptr cloud (new pcl::PointCloud<pcl::PointXYZ>);
00147     pcl::PointCloud<pcl::PointXYZ>::Ptr cloud_filtered (new pcl::PointCloud<pcl::PointXYZ>);
00148 
00149     cloud->points.resize(m_original.width*m_original.height);
00150     float *src = (float *)m_original.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     
00158     pcl::StatisticalOutlierRemoval<pcl::PointXYZ> sor;
00159     sor.setInputCloud (cloud);
00160     sor.setMeanK (m_meanK);
00161     sor.setStddevMulThresh (m_stddevMulThresh);
00162     sor.filter (*cloud_filtered);
00163 
00164     m_filtered.width = cloud_filtered->points.size();
00165     m_filtered.row_step = m_filtered.point_step*m_filtered.width;
00166     m_filtered.data.length(m_filtered.height*m_filtered.row_step);
00167     float *dst = (float *)m_filtered.data.get_buffer();
00168     for (unsigned int i=0; i<cloud_filtered->points.size(); i++){
00169       dst[0] = cloud_filtered->points[i].x;
00170       dst[1] = cloud_filtered->points[i].y;
00171       dst[2] = cloud_filtered->points[i].z;
00172       dst += 4;
00173     }
00174     m_filteredOut.write();
00175   }
00176 
00177   return RTC::RTC_OK;
00178 }
00179 
00180 /*
00181 RTC::ReturnCode_t SORFilter::onAborting(RTC::UniqueId ec_id)
00182 {
00183   return RTC::RTC_OK;
00184 }
00185 */
00186 
00187 /*
00188 RTC::ReturnCode_t SORFilter::onError(RTC::UniqueId ec_id)
00189 {
00190   return RTC::RTC_OK;
00191 }
00192 */
00193 
00194 /*
00195 RTC::ReturnCode_t SORFilter::onReset(RTC::UniqueId ec_id)
00196 {
00197   return RTC::RTC_OK;
00198 }
00199 */
00200 
00201 /*
00202 RTC::ReturnCode_t SORFilter::onStateUpdate(RTC::UniqueId ec_id)
00203 {
00204   return RTC::RTC_OK;
00205 }
00206 */
00207 
00208 /*
00209 RTC::ReturnCode_t SORFilter::onRateChanged(RTC::UniqueId ec_id)
00210 {
00211   return RTC::RTC_OK;
00212 }
00213 */
00214 
00215 
00216 
00217 extern "C"
00218 {
00219 
00220   void SORFilterInit(RTC::Manager* manager)
00221   {
00222     RTC::Properties profile(spec);
00223     manager->registerFactory(profile,
00224                              RTC::Create<SORFilter>,
00225                              RTC::Delete<SORFilter>);
00226   }
00227 
00228 };
00229 
00230 


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