PlaneRemover.cpp
Go to the documentation of this file.
1 // -*- C++ -*-
10 #include <pcl/io/pcd_io.h>
11 #include <pcl/point_types.h>
12 #include <pcl/segmentation/sac_segmentation.h>
13 #include <pcl/filters/extract_indices.h>
14 #include "PlaneRemover.h"
15 #include "hrpsys/idl/pointcloud.hh"
16 
17 // Module specification
18 // <rtc-template block="module_spec">
19 static const char* spec[] =
20  {
21  "implementation_id", "PlaneRemover",
22  "type_name", "PlaneRemover",
23  "description", "Plane Remover",
24  "version", HRPSYS_PACKAGE_VERSION,
25  "vendor", "AIST",
26  "category", "example",
27  "activity_type", "DataFlowComponent",
28  "max_instance", "10",
29  "language", "C++",
30  "lang_type", "compile",
31  // Configuration variables
32  "conf.default.distanceThd", "0.02",
33  "conf.default.pointNumThd", "500",
34 
35  ""
36  };
37 // </rtc-template>
38 
40  : RTC::DataFlowComponentBase(manager),
41  // <rtc-template block="initializer">
42  m_originalIn("original", m_original),
43  m_filteredOut("filtered", m_filtered),
44  // </rtc-template>
45  dummy(0)
46 {
47 }
48 
50 {
51 }
52 
53 
54 
55 RTC::ReturnCode_t PlaneRemover::onInitialize()
56 {
57  //std::cout << m_profile.instance_name << ": onInitialize()" << std::endl;
58  // <rtc-template block="bind_config">
59  // Bind variables and configuration variable
60  bindParameter("distanceThd", m_distThd, "0.02");
61  bindParameter("pointNumThd", m_pointNumThd, "500");
62 
63  // </rtc-template>
64 
65  // Registration: InPort/OutPort/Service
66  // <rtc-template block="registration">
67  // Set InPort buffers
68  addInPort("originalIn", m_originalIn);
69 
70  // Set OutPort buffer
71  addOutPort("filteredOut", m_filteredOut);
72 
73  // Set service provider to Ports
74 
75  // Set service consumers to Ports
76 
77  // Set CORBA Service Ports
78 
79  // </rtc-template>
80 
82 
83  m_filtered.height = 1;
84  m_filtered.type = "xyz";
85  m_filtered.fields.length(3);
86  m_filtered.fields[0].name = "x";
87  m_filtered.fields[0].offset = 0;
88  m_filtered.fields[0].data_type = PointCloudTypes::FLOAT32;
89  m_filtered.fields[0].count = 4;
90  m_filtered.fields[1].name = "y";
91  m_filtered.fields[1].offset = 4;
92  m_filtered.fields[1].data_type = PointCloudTypes::FLOAT32;
93  m_filtered.fields[1].count = 4;
94  m_filtered.fields[2].name = "z";
95  m_filtered.fields[2].offset = 8;
96  m_filtered.fields[2].data_type = PointCloudTypes::FLOAT32;
97  m_filtered.fields[2].count = 4;
98  m_filtered.is_bigendian = false;
99  m_filtered.point_step = 16;
100  m_filtered.is_dense = true;
101 
102  return RTC::RTC_OK;
103 }
104 
105 
106 
107 /*
108 RTC::ReturnCode_t PlaneRemover::onFinalize()
109 {
110  return RTC::RTC_OK;
111 }
112 */
113 
114 /*
115 RTC::ReturnCode_t PlaneRemover::onStartup(RTC::UniqueId ec_id)
116 {
117  return RTC::RTC_OK;
118 }
119 */
120 
121 /*
122 RTC::ReturnCode_t PlaneRemover::onShutdown(RTC::UniqueId ec_id)
123 {
124  return RTC::RTC_OK;
125 }
126 */
127 
128 RTC::ReturnCode_t PlaneRemover::onActivated(RTC::UniqueId ec_id)
129 {
130  std::cout << m_profile.instance_name<< ": onActivated(" << ec_id << ")" << std::endl;
131  return RTC::RTC_OK;
132 }
133 
135 {
136  std::cout << m_profile.instance_name<< ": onDeactivated(" << ec_id << ")" << std::endl;
137  return RTC::RTC_OK;
138 }
139 
140 RTC::ReturnCode_t PlaneRemover::onExecute(RTC::UniqueId ec_id)
141 {
142  //std::cout << m_profile.instance_name<< ": onExecute(" << ec_id << ")" << std::endl;
143 
144  if (m_originalIn.isNew()){
145  m_originalIn.read();
146 
147  // CORBA -> PCL
148  pcl::PointCloud<pcl::PointXYZ>::Ptr original (new pcl::PointCloud<pcl::PointXYZ>);
149  original->points.resize(m_original.width*m_original.height);
150  float *src = (float *)m_original.data.get_buffer();
151  for (unsigned int i=0; i<original->points.size(); i++){
152  original->points[i].x = src[0];
153  original->points[i].y = src[1];
154  original->points[i].z = src[2];
155  src += 4;
156  }
157 
158  // PROCESSING
159 
160  pcl::ModelCoefficients::Ptr coefficients (new pcl::ModelCoefficients);
161  pcl::PointIndices::Ptr inliers (new pcl::PointIndices);
162  // Create the segmentation object
163  pcl::SACSegmentation<pcl::PointXYZ> seg;
164  // Optional
165  seg.setOptimizeCoefficients (true);
166  // Mandatory
167  seg.setModelType (pcl::SACMODEL_PLANE);
168  seg.setMethodType (pcl::SAC_RANSAC);
169  seg.setDistanceThreshold (m_distThd);
170 
171  pcl::PointCloud<pcl::PointXYZ>::Ptr cloud = original;
172  pcl::PointCloud<pcl::PointXYZ>::Ptr cloud_f(new pcl::PointCloud<pcl::PointXYZ>);
173 
174  pcl::ExtractIndices<pcl::PointXYZ> extract;
175 
176  while(1){
177  seg.setInputCloud (cloud);
178  seg.segment (*inliers, *coefficients);
179 
180  if (inliers->indices.size () < m_pointNumThd) break;
181 
182  extract.setInputCloud( cloud );
183  extract.setIndices( inliers );
184  extract.setNegative( true );
185  extract.filter( *cloud_f );
186  cloud = cloud_f;
187  }
188 
189  //std::cout << "PLaneRemover: original = " << original->points.size() << ", filtered = " << cloud->points.size() << ", thd=" << m_distThd << std::endl;
190 
191  // PCL -> CORBA
192  m_filtered.width = cloud->points.size();
193  m_filtered.row_step = m_filtered.point_step*m_filtered.width;
194  m_filtered.data.length(m_filtered.height*m_filtered.row_step);
195  float *dst = (float *)m_filtered.data.get_buffer();
196  for (unsigned int i=0; i<cloud->points.size(); i++){
197  dst[0] = cloud->points[i].x;
198  dst[1] = cloud->points[i].y;
199  dst[2] = cloud->points[i].z;
200  dst += 4;
201  }
203  }
204 
205  return RTC::RTC_OK;
206 }
207 
208 /*
209 RTC::ReturnCode_t PlaneRemover::onAborting(RTC::UniqueId ec_id)
210 {
211  return RTC::RTC_OK;
212 }
213 */
214 
215 /*
216 RTC::ReturnCode_t PlaneRemover::onError(RTC::UniqueId ec_id)
217 {
218  return RTC::RTC_OK;
219 }
220 */
221 
222 /*
223 RTC::ReturnCode_t PlaneRemover::onReset(RTC::UniqueId ec_id)
224 {
225  return RTC::RTC_OK;
226 }
227 */
228 
229 /*
230 RTC::ReturnCode_t PlaneRemover::onStateUpdate(RTC::UniqueId ec_id)
231 {
232  return RTC::RTC_OK;
233 }
234 */
235 
236 /*
237 RTC::ReturnCode_t PlaneRemover::onRateChanged(RTC::UniqueId ec_id)
238 {
239  return RTC::RTC_OK;
240 }
241 */
242 
243 
244 
245 extern "C"
246 {
247 
249  {
251  manager->registerFactory(profile,
252  RTC::Create<PlaneRemover>,
253  RTC::Delete<PlaneRemover>);
254  }
255 
256 };
257 
258 
ComponentProfile m_profile
png_infop png_charpp int png_charpp profile
virtual RTC::ReturnCode_t onInitialize()
virtual RTC::ReturnCode_t onActivated(RTC::UniqueId ec_id)
png_uint_32 i
coil::Properties & getProperties()
bool addOutPort(const char *name, OutPortBase &outport)
virtual RTC::ReturnCode_t onExecute(RTC::UniqueId ec_id)
void PlaneRemoverInit(RTC::Manager *manager)
ExecutionContextHandle_t UniqueId
bool bindParameter(const char *param_name, VarType &var, const char *def_val, bool(*trans)(VarType &, const char *)=coil::stringTo)
PointCloudTypes::PointCloud m_filtered
Definition: PlaneRemover.h:107
static const char * spec[]
Statistical Outlier Removal Filter.
virtual RTC::ReturnCode_t onDeactivated(RTC::UniqueId ec_id)
InPort< PointCloudTypes::PointCloud > m_originalIn
Definition: PlaneRemover.h:111
PlaneRemover(RTC::Manager *manager)
Constructor.
prop
double m_pointNumThd
Definition: PlaneRemover.h:138
virtual bool isNew()
virtual bool write(DataType &value)
PointCloudTypes::PointCloud m_original
Definition: PlaneRemover.h:106
OutPort< PointCloudTypes::PointCloud > m_filteredOut
Definition: PlaneRemover.h:117
virtual ~PlaneRemover()
Destructor.
bool addInPort(const char *name, InPortBase &inport)
bool registerFactory(coil::Properties &profile, RtcNewFunc new_func, RtcDeleteFunc delete_func)
double m_distThd
Definition: PlaneRemover.h:137


hrpsys
Author(s): AIST, Fumio Kanehiro
autogenerated on Thu May 6 2021 02:41:50