Skip to content

Commit e77389b

Browse files
committed
fix(ray_ground_filter): indices extract bug fix
Signed-off-by: badai-nguyen <dai.nguyen@tier4.jp>
1 parent 3414c05 commit e77389b

File tree

2 files changed

+22
-12
lines changed

2 files changed

+22
-12
lines changed

perception/ground_segmentation/include/ground_segmentation/ray_ground_filter_nodelet.hpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ class RayGroundFilterComponent : public pointcloud_preprocessor::Filter
186186
* @param out_removed_indices_cloud_ptr Resulting PointCloud with the indices removed
187187
*/
188188
void ExtractPointsIndices(
189-
const PointCloud2::ConstSharedPtr in_cloud_ptr, const pcl::PointIndices & in_indices,
189+
const PointCloud2::ConstSharedPtr in_cloud_ptr, pcl::PointIndices & in_indices,
190190
PointCloud2::SharedPtr out_only_indices_cloud_ptr,
191191
PointCloud2::SharedPtr out_removed_indices_cloud_ptr);
192192

perception/ground_segmentation/src/ray_ground_filter_nodelet.cpp

+21-11
Original file line numberDiff line numberDiff line change
@@ -270,25 +270,39 @@ void RayGroundFilterComponent::initializePointCloud2(
270270
}
271271

272272
void RayGroundFilterComponent::ExtractPointsIndices(
273-
const PointCloud2::ConstSharedPtr in_cloud_ptr, const pcl::PointIndices & in_indices,
273+
const PointCloud2::ConstSharedPtr in_cloud_ptr, pcl::PointIndices & in_indices,
274274
PointCloud2::SharedPtr ground_cloud_msg_ptr, PointCloud2::SharedPtr no_ground_cloud_msg_ptr)
275275
{
276276
initializePointCloud2(in_cloud_ptr, ground_cloud_msg_ptr);
277277
initializePointCloud2(in_cloud_ptr, no_ground_cloud_msg_ptr);
278278
size_t ground_count = 0;
279279
int point_step = in_cloud_ptr->point_step;
280-
size_t prev_ground_count = 0;
280+
size_t prev_ground_idx = 0;
281281
size_t no_ground_count = 0;
282+
// sort indices
283+
sort(in_indices.indices.begin(), in_indices.indices.end());
282284
for (const auto & idx : in_indices.indices) {
283285
std::memcpy(
284286
&ground_cloud_msg_ptr->data[ground_count * point_step], &in_cloud_ptr->data[idx * point_step],
285287
point_step);
286-
std::memcpy(
287-
&no_ground_cloud_msg_ptr->data[prev_ground_count * point_step],
288-
&in_cloud_ptr->data[idx * point_step], point_step * (idx - prev_ground_count));
289-
prev_ground_count = idx - prev_ground_count;
290288
ground_count++;
291-
prev_ground_count = idx;
289+
if (idx - prev_ground_idx > 1) {
290+
std::memcpy(
291+
&no_ground_cloud_msg_ptr->data[no_ground_count * point_step],
292+
&in_cloud_ptr->data[(prev_ground_idx + 1) * point_step],
293+
point_step * (idx - prev_ground_idx - 1));
294+
no_ground_count += idx - prev_ground_idx - 1;
295+
}
296+
prev_ground_idx = idx;
297+
}
298+
299+
// Check no_ground_points after last idx
300+
if (prev_ground_idx < in_cloud_ptr->width - 1) {
301+
std::memcpy(
302+
&no_ground_cloud_msg_ptr->data[no_ground_count * point_step],
303+
&in_cloud_ptr->data[(prev_ground_idx + 1) * point_step],
304+
point_step * (in_cloud_ptr->width - prev_ground_idx - 1));
305+
no_ground_count += in_cloud_ptr->width - prev_ground_idx - 1;
292306
}
293307
ground_cloud_msg_ptr->data.resize(ground_count * point_step);
294308
no_ground_cloud_msg_ptr->data.resize(no_ground_count * point_step);
@@ -329,10 +343,6 @@ void RayGroundFilterComponent::filter(
329343
sensor_msgs::msg::PointCloud2::SharedPtr ground_cloud_msg_ptr(new sensor_msgs::msg::PointCloud2);
330344

331345
ExtractPointsIndices(input, ground_indices, ground_cloud_msg_ptr, no_ground_cloud_msg_ptr);
332-
333-
pcl::toROSMsg(*no_ground_cloud_ptr, *no_ground_cloud_msg_ptr);
334-
no_ground_cloud_msg_ptr->header = input->header;
335-
336346
output = *no_ground_cloud_msg_ptr;
337347
}
338348

0 commit comments

Comments
 (0)