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     if (!m_original.data.length()){
00147         m_filtered.width = m_filtered.row_step = 0;
00148         m_filtered.data.length(0);
00149         return RTC::RTC_OK;
00150     }
00151 
00152     pcl::PointCloud<pcl::PointXYZ>::Ptr cloud (new pcl::PointCloud<pcl::PointXYZ>);
00153     pcl::PointCloud<pcl::PointXYZ>::Ptr cloud_filtered (new pcl::PointCloud<pcl::PointXYZ>);
00154 
00155     cloud->points.resize(m_original.width*m_original.height);
00156     float *src = (float *)m_original.data.get_buffer();
00157     for (unsigned int i=0; i<cloud->points.size(); i++){
00158       cloud->points[i].x = src[0];
00159       cloud->points[i].y = src[1];
00160       cloud->points[i].z = src[2];
00161       src += 4;
00162     }
00163     
00164     pcl::StatisticalOutlierRemoval<pcl::PointXYZ> sor;
00165     sor.setInputCloud (cloud);
00166     sor.setMeanK (m_meanK);
00167     sor.setStddevMulThresh (m_stddevMulThresh);
00168     sor.filter (*cloud_filtered);
00169 
00170     m_filtered.width = cloud_filtered->points.size();
00171     m_filtered.row_step = m_filtered.point_step*m_filtered.width;
00172     m_filtered.data.length(m_filtered.height*m_filtered.row_step);
00173     float *dst = (float *)m_filtered.data.get_buffer();
00174     for (unsigned int i=0; i<cloud_filtered->points.size(); i++){
00175       dst[0] = cloud_filtered->points[i].x;
00176       dst[1] = cloud_filtered->points[i].y;
00177       dst[2] = cloud_filtered->points[i].z;
00178       dst += 4;
00179     }
00180     m_filteredOut.write();
00181   }
00182 
00183   return RTC::RTC_OK;
00184 }
00185 
00186 /*
00187 RTC::ReturnCode_t SORFilter::onAborting(RTC::UniqueId ec_id)
00188 {
00189   return RTC::RTC_OK;
00190 }
00191 */
00192 
00193 /*
00194 RTC::ReturnCode_t SORFilter::onError(RTC::UniqueId ec_id)
00195 {
00196   return RTC::RTC_OK;
00197 }
00198 */
00199 
00200 /*
00201 RTC::ReturnCode_t SORFilter::onReset(RTC::UniqueId ec_id)
00202 {
00203   return RTC::RTC_OK;
00204 }
00205 */
00206 
00207 /*
00208 RTC::ReturnCode_t SORFilter::onStateUpdate(RTC::UniqueId ec_id)
00209 {
00210   return RTC::RTC_OK;
00211 }
00212 */
00213 
00214 /*
00215 RTC::ReturnCode_t SORFilter::onRateChanged(RTC::UniqueId ec_id)
00216 {
00217   return RTC::RTC_OK;
00218 }
00219 */
00220 
00221 
00222 
00223 extern "C"
00224 {
00225 
00226   void SORFilterInit(RTC::Manager* manager)
00227   {
00228     RTC::Properties profile(spec);
00229     manager->registerFactory(profile,
00230                              RTC::Create<SORFilter>,
00231                              RTC::Delete<SORFilter>);
00232   }
00233 
00234 };
00235 
00236 


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