@@ -270,25 +270,39 @@ void RayGroundFilterComponent::initializePointCloud2(
270
270
}
271
271
272
272
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,
274
274
PointCloud2::SharedPtr ground_cloud_msg_ptr, PointCloud2::SharedPtr no_ground_cloud_msg_ptr)
275
275
{
276
276
initializePointCloud2 (in_cloud_ptr, ground_cloud_msg_ptr);
277
277
initializePointCloud2 (in_cloud_ptr, no_ground_cloud_msg_ptr);
278
278
size_t ground_count = 0 ;
279
279
int point_step = in_cloud_ptr->point_step ;
280
- size_t prev_ground_count = 0 ;
280
+ size_t prev_ground_idx = 0 ;
281
281
size_t no_ground_count = 0 ;
282
+ // sort indices
283
+ sort (in_indices.indices .begin (), in_indices.indices .end ());
282
284
for (const auto & idx : in_indices.indices ) {
283
285
std::memcpy (
284
286
&ground_cloud_msg_ptr->data [ground_count * point_step], &in_cloud_ptr->data [idx * point_step],
285
287
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;
290
288
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 ;
292
306
}
293
307
ground_cloud_msg_ptr->data .resize (ground_count * point_step);
294
308
no_ground_cloud_msg_ptr->data .resize (no_ground_count * point_step);
@@ -329,10 +343,6 @@ void RayGroundFilterComponent::filter(
329
343
sensor_msgs::msg::PointCloud2::SharedPtr ground_cloud_msg_ptr (new sensor_msgs::msg::PointCloud2);
330
344
331
345
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
-
336
346
output = *no_ground_cloud_msg_ptr;
337
347
}
338
348
0 commit comments