MLSFilter.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/surface/mls.h>
13 #include "MLSFilter.h"
14 #include "hrpsys/idl/pointcloud.hh"
15 
16 // Module specification
17 // <rtc-template block="module_spec">
18 static const char* spec[] =
19  {
20  "implementation_id", "MLSFilter",
21  "type_name", "MLSFilter",
22  "description", "Moving Least Squares Filter",
23  "version", HRPSYS_PACKAGE_VERSION,
24  "vendor", "AIST",
25  "category", "example",
26  "activity_type", "DataFlowComponent",
27  "max_instance", "10",
28  "language", "C++",
29  "lang_type", "compile",
30  // Configuration variables
31  "conf.default.radius", "0.03",
32 
33  ""
34  };
35 // </rtc-template>
36 
38  : RTC::DataFlowComponentBase(manager),
39  // <rtc-template block="initializer">
40  m_originalIn("original", m_original),
41  m_filteredOut("filtered", m_filtered),
42  // </rtc-template>
43  dummy(0)
44 {
45 }
46 
48 {
49 }
50 
51 
52 
53 RTC::ReturnCode_t MLSFilter::onInitialize()
54 {
55  //std::cout << m_profile.instance_name << ": onInitialize()" << std::endl;
56  // <rtc-template block="bind_config">
57  // Bind variables and configuration variable
58  bindParameter("radius", m_radius, "0.03");
59 
60  // </rtc-template>
61 
62  // Registration: InPort/OutPort/Service
63  // <rtc-template block="registration">
64  // Set InPort buffers
65  addInPort("originalIn", m_originalIn);
66 
67  // Set OutPort buffer
68  addOutPort("filteredOut", m_filteredOut);
69 
70  // Set service provider to Ports
71 
72  // Set service consumers to Ports
73 
74  // Set CORBA Service Ports
75 
76  // </rtc-template>
77 
79 
80  m_filtered.height = 1;
81  m_filtered.type = "xyz";
82  m_filtered.fields.length(3);
83  m_filtered.fields[0].name = "x";
84  m_filtered.fields[0].offset = 0;
85  m_filtered.fields[0].data_type = PointCloudTypes::FLOAT32;
86  m_filtered.fields[0].count = 4;
87  m_filtered.fields[1].name = "y";
88  m_filtered.fields[1].offset = 4;
89  m_filtered.fields[1].data_type = PointCloudTypes::FLOAT32;
90  m_filtered.fields[1].count = 4;
91  m_filtered.fields[2].name = "z";
92  m_filtered.fields[2].offset = 8;
93  m_filtered.fields[2].data_type = PointCloudTypes::FLOAT32;
94  m_filtered.fields[2].count = 4;
95  m_filtered.is_bigendian = false;
96  m_filtered.point_step = 16;
97  m_filtered.is_dense = true;
98 
99  return RTC::RTC_OK;
100 }
101 
102 
103 
104 /*
105 RTC::ReturnCode_t MLSFilter::onFinalize()
106 {
107  return RTC::RTC_OK;
108 }
109 */
110 
111 /*
112 RTC::ReturnCode_t MLSFilter::onStartup(RTC::UniqueId ec_id)
113 {
114  return RTC::RTC_OK;
115 }
116 */
117 
118 /*
119 RTC::ReturnCode_t MLSFilter::onShutdown(RTC::UniqueId ec_id)
120 {
121  return RTC::RTC_OK;
122 }
123 */
124 
125 RTC::ReturnCode_t MLSFilter::onActivated(RTC::UniqueId ec_id)
126 {
127  std::cout << m_profile.instance_name<< ": onActivated(" << ec_id << ")" << std::endl;
128  return RTC::RTC_OK;
129 }
130 
131 RTC::ReturnCode_t MLSFilter::onDeactivated(RTC::UniqueId ec_id)
132 {
133  std::cout << m_profile.instance_name<< ": onDeactivated(" << ec_id << ")" << std::endl;
134  return RTC::RTC_OK;
135 }
136 
137 RTC::ReturnCode_t MLSFilter::onExecute(RTC::UniqueId ec_id)
138 {
139  //std::cout << m_profile.instance_name<< ": onExecute(" << ec_id << ")" << std::endl;
140 
141  if (m_originalIn.isNew()){
142  m_originalIn.read();
143 
144  pcl::PointCloud<pcl::PointXYZ>::Ptr cloud (new pcl::PointCloud<pcl::PointXYZ>);
145  pcl::PointCloud<pcl::PointXYZ>::Ptr cloud_filtered (new pcl::PointCloud<pcl::PointXYZ>);
146 
147  // RTM -> PCL
148  cloud->points.resize(m_original.width*m_original.height);
149  float *src = (float *)m_original.data.get_buffer();
150  for (unsigned int i=0; i<cloud->points.size(); i++){
151  cloud->points[i].x = src[0];
152  cloud->points[i].y = src[1];
153  cloud->points[i].z = src[2];
154  src += 4;
155  }
156 
157  // PCL Processing
158  pcl::search::KdTree<pcl::PointXYZ>::Ptr tree (new pcl::search::KdTree<pcl::PointXYZ>);
159  pcl::MovingLeastSquares<pcl::PointXYZ, pcl::PointXYZ> mls;
160  mls.setInputCloud (cloud);
161  mls.setPolynomialFit (true);
162  mls.setSearchMethod (tree);
163  mls.setSearchRadius (m_radius);
164  mls.process (*cloud_filtered);
165 
166  // PCL -> RTM
167  m_filtered.width = cloud_filtered->points.size();
168  m_filtered.row_step = m_filtered.point_step*m_filtered.width;
169  m_filtered.data.length(m_filtered.height*m_filtered.row_step);
170  float *dst = (float *)m_filtered.data.get_buffer();
171  for (unsigned int i=0; i<cloud_filtered->points.size(); i++){
172  dst[0] = cloud_filtered->points[i].x;
173  dst[1] = cloud_filtered->points[i].y;
174  dst[2] = cloud_filtered->points[i].z;
175  dst += 4;
176  }
178  }
179 
180  return RTC::RTC_OK;
181 }
182 
183 /*
184 RTC::ReturnCode_t MLSFilter::onAborting(RTC::UniqueId ec_id)
185 {
186  return RTC::RTC_OK;
187 }
188 */
189 
190 /*
191 RTC::ReturnCode_t MLSFilter::onError(RTC::UniqueId ec_id)
192 {
193  return RTC::RTC_OK;
194 }
195 */
196 
197 /*
198 RTC::ReturnCode_t MLSFilter::onReset(RTC::UniqueId ec_id)
199 {
200  return RTC::RTC_OK;
201 }
202 */
203 
204 /*
205 RTC::ReturnCode_t MLSFilter::onStateUpdate(RTC::UniqueId ec_id)
206 {
207  return RTC::RTC_OK;
208 }
209 */
210 
211 /*
212 RTC::ReturnCode_t MLSFilter::onRateChanged(RTC::UniqueId ec_id)
213 {
214  return RTC::RTC_OK;
215 }
216 */
217 
218 
219 
220 extern "C"
221 {
222 
224  {
226  manager->registerFactory(profile,
227  RTC::Create<MLSFilter>,
228  RTC::Delete<MLSFilter>);
229  }
230 
231 };
232 
233 
ComponentProfile m_profile
OutPort< PointCloudTypes::PointCloud > m_filteredOut
Definition: MLSFilter.h:117
png_infop png_charpp int png_charpp profile
virtual RTC::ReturnCode_t onExecute(RTC::UniqueId ec_id)
Definition: MLSFilter.cpp:137
PointCloudTypes::PointCloud m_filtered
Definition: MLSFilter.h:107
virtual ~MLSFilter()
Destructor.
Definition: MLSFilter.cpp:47
png_uint_32 i
coil::Properties & getProperties()
void MLSFilterInit(RTC::Manager *manager)
Definition: MLSFilter.cpp:223
bool addOutPort(const char *name, OutPortBase &outport)
Moving Least Squares Filter.
PointCloudTypes::PointCloud m_original
Definition: MLSFilter.h:106
InPort< PointCloudTypes::PointCloud > m_originalIn
Definition: MLSFilter.h:111
ExecutionContextHandle_t UniqueId
bool bindParameter(const char *param_name, VarType &var, const char *def_val, bool(*trans)(VarType &, const char *)=coil::stringTo)
double m_radius
Definition: MLSFilter.h:138
prop
virtual RTC::ReturnCode_t onActivated(RTC::UniqueId ec_id)
Definition: MLSFilter.cpp:125
static const char * spec[]
Definition: MLSFilter.cpp:18
virtual bool isNew()
virtual RTC::ReturnCode_t onDeactivated(RTC::UniqueId ec_id)
Definition: MLSFilter.cpp:131
virtual RTC::ReturnCode_t onInitialize()
Definition: MLSFilter.cpp:53
virtual bool write(DataType &value)
MLSFilter(RTC::Manager *manager)
Constructor.
Definition: MLSFilter.cpp:37
bool addInPort(const char *name, InPortBase &inport)
bool registerFactory(coil::Properties &profile, RtcNewFunc new_func, RtcDeleteFunc delete_func)


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