Skip to content

Commit f088f31

Browse files
committed
support melodic
1 parent 799ff8e commit f088f31

File tree

2 files changed

+53
-27
lines changed

2 files changed

+53
-27
lines changed

jsk_pcl_ros/include/jsk_pcl_ros/organized_statistical_outlier_removal.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,11 @@ namespace jsk_pcl_ros
6565

6666
virtual void filter(const sensor_msgs::PointCloud2::ConstPtr& msg);
6767
virtual void filterCloud(const pcl::PCLPointCloud2::Ptr pcl_cloud,
68-
pcl::PointIndices::Ptr pcl_indices_filtered,
68+
pcl::PCLPointCloud2::Ptr pcl_cloud_filtered,
6969
bool keep_organized);
70+
virtual void filterIndices(const pcl::PCLPointCloud2::Ptr pcl_cloud,
71+
pcl::PointIndices::Ptr pcl_indices_filtered,
72+
bool keep_organized);
7073

7174
virtual void updateDiagnostic(
7275
diagnostic_updater::DiagnosticStatusWrapper &stat);

jsk_pcl_ros/src/organized_statistical_outlier_removal_nodelet.cpp

Lines changed: 49 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -82,21 +82,19 @@ namespace jsk_pcl_ros
8282

8383
void OrganizedStatisticalOutlierRemoval::filterCloud(
8484
const pcl::PCLPointCloud2::Ptr pcl_cloud,
85-
pcl::PointIndices::Ptr pcl_indices_filtered,
85+
pcl::PCLPointCloud2::Ptr pcl_cloud_filtered,
8686
bool keep_organized)
8787
{
88-
#if PCL_VERSION_COMPARE (<, 1, 9, 0)
89-
pcl::PointCloud<pcl::PointXYZ>::Ptr cloud (new pcl::PointCloud<pcl::PointXYZ>);
90-
pcl::fromPCLPointCloud2(*pcl_cloud, *cloud);
9188
if (keep_organized) {
89+
pcl::PointCloud<pcl::PointXYZ>::Ptr cloud (new pcl::PointCloud<pcl::PointXYZ>);
90+
pcl::fromPCLPointCloud2(*pcl_cloud, *cloud);
9291
// Initialize the spatial locator
9392
pcl::search::Search<pcl::PointXYZ>::Ptr tree;
9493
tree.reset (new pcl::search::OrganizedNeighbor<pcl::PointXYZ> ());
9594
tree->setInputCloud (cloud);
9695

9796
// Allocate enough space to hold the results
9897
std::vector<int> indices (cloud->size());
99-
std::vector<int> indices_filtered;
10098
std::vector<int> nn_indices (mean_k_);
10199
std::vector<float> nn_dists (mean_k_);
102100
std::vector<float> distances (indices.size ());
@@ -142,6 +140,9 @@ namespace jsk_pcl_ros
142140
double distance_threshold = mean + std_mul_ * stddev; // a distance that is bigger than this signals an outlier
143141

144142
// Build a new cloud by neglecting outliers
143+
int nr_p = 0;
144+
int nr_removed_p = 0;
145+
bool remove_point = false;
145146
for (int cp = 0; cp < static_cast<int> (indices.size ()); ++cp)
146147
{
147148
bool remove_point = false;
@@ -153,12 +154,22 @@ namespace jsk_pcl_ros
153154
{
154155
remove_point = (distances[cp] > distance_threshold);
155156
}
156-
if (!remove_point)
157+
158+
if (remove_point)
157159
{
158-
indices_filtered.push_back(indices[cp]);
160+
/* Set the current point to NaN. */
161+
*(reinterpret_cast<float*>(&pcl_cloud_filtered->data[nr_p * pcl_cloud_filtered->point_step])+0) = std::numeric_limits<float>::quiet_NaN();
162+
*(reinterpret_cast<float*>(&pcl_cloud_filtered->data[nr_p * pcl_cloud_filtered->point_step])+1) = std::numeric_limits<float>::quiet_NaN();
163+
*(reinterpret_cast<float*>(&pcl_cloud_filtered->data[nr_p * pcl_cloud_filtered->point_step])+2) = std::numeric_limits<float>::quiet_NaN();
159164
}
165+
else
166+
{
167+
memcpy (&pcl_cloud_filtered->data[nr_p * pcl_cloud_filtered->point_step],
168+
&pcl_cloud->data[indices[cp] * pcl_cloud_filtered->point_step],
169+
pcl_cloud_filtered->point_step);
170+
}
171+
nr_p++;
160172
}
161-
pcl_indices_filtered->indices = indices_filtered;
162173
}
163174
else
164175
{
@@ -167,9 +178,16 @@ namespace jsk_pcl_ros
167178
sor.setMeanK(mean_k_);
168179
sor.setStddevMulThresh(std_mul_);
169180
sor.setNegative(negative_);
170-
sor.filter(pcl_indices_filtered->indices);
181+
sor.filter(*pcl_cloud_filtered);
171182
}
172-
#else
183+
}
184+
185+
void OrganizedStatisticalOutlierRemoval::filterIndices(
186+
const pcl::PCLPointCloud2::Ptr pcl_cloud,
187+
pcl::PointIndices::Ptr pcl_indices_filtered,
188+
bool keep_organized)
189+
{
190+
#if PCL_VERSION_COMPARE (>=, 1, 9, 0)
173191
pcl::StatisticalOutlierRemoval<pcl::PCLPointCloud2> sor;
174192
sor.setInputCloud(pcl_cloud);
175193
sor.setMeanK(mean_k_);
@@ -190,15 +208,15 @@ namespace jsk_pcl_ros
190208
NODELET_ERROR("keep_organized parameter is true, but input pointcloud is not organized.");
191209
}
192210
bool keep_organized = keep_organized_ && !msg->is_dense;
211+
// filter
212+
#if PCL_VERSION_COMPARE (>=, 1, 9, 0)
193213
pcl::PCLPointCloud2::Ptr pcl_cloud (new pcl::PCLPointCloud2);
194214
pcl_conversions::toPCL(*msg, *pcl_cloud);
195-
196-
// filter
197215
pcl::PointIndices::Ptr pcl_indices_filtered (new pcl::PointIndices());
198216
pcl_indices_filtered->indices.resize(pcl_cloud->data.size());
199-
OrganizedStatisticalOutlierRemoval::filterCloud(pcl_cloud,
200-
pcl_indices_filtered,
201-
keep_organized);
217+
OrganizedStatisticalOutlierRemoval::filterIndices(pcl_cloud,
218+
pcl_indices_filtered,
219+
keep_organized);
202220
pcl::PCLPointCloud2::Ptr pcl_cloud_filtered (new pcl::PCLPointCloud2);
203221
pcl::ExtractIndices<pcl::PCLPointCloud2> ex;
204222
ex.setInputCloud(pcl_cloud);
@@ -207,17 +225,22 @@ namespace jsk_pcl_ros
207225
ex.setIndices(pcl_indices_filtered);
208226
ex.filter(*pcl_cloud_filtered);
209227
pcl_conversions::fromPCL(*pcl_cloud_filtered, output);
210-
#if PCL_VERSION_COMPARE (<, 1, 9, 0)
211-
if (keep_organized) {
212-
// Copy the common fields
213-
output.is_bigendian = msg->is_bigendian;
214-
output.fields = msg->fields;
215-
output.point_step = msg->point_step;
216-
output.data.resize (msg->data.size ());
217-
output.width = msg->width;
218-
output.height = msg->height;
219-
output.row_step = msg->point_step * msg->width;
220-
}
228+
#else
229+
pcl::PCLPointCloud2::Ptr pcl_cloud (new pcl::PCLPointCloud2);
230+
pcl_conversions::toPCL(*msg, *pcl_cloud);
231+
pcl::PCLPointCloud2::Ptr pcl_cloud_filtered (new pcl::PCLPointCloud2);
232+
// Copy the common fields
233+
pcl_cloud_filtered->is_bigendian = msg->is_bigendian;
234+
pcl_cloud_filtered->fields = msg->fields;
235+
pcl_cloud_filtered->point_step = msg->point_step;
236+
pcl_cloud_filtered->data.resize (msg->data.size ());
237+
pcl_cloud_filtered->width = msg->width;
238+
pcl_cloud_filtered->height = msg->height;
239+
pcl_cloud_filtered->row_step = msg->point_step * msg->width;
240+
OrganizedStatisticalOutlierRemoval::filterCloud(pcl_cloud,
241+
pcl_cloud_filtered,
242+
keep_organized);
243+
pcl_conversions::fromPCL(*pcl_cloud_filtered, output);
221244
#endif
222245
output.header = msg->header;
223246
output.is_dense = !keep_organized;

0 commit comments

Comments
 (0)