@@ -257,20 +257,53 @@ void RayGroundFilterComponent::ClassifyPointCloud(
257
257
// return (true);
258
258
// }
259
259
260
- void RayGroundFilterComponent::ExtractPointsIndices (
261
- const pcl::PointCloud<PointType_>::Ptr in_cloud_ptr, const pcl::PointIndices & in_indices,
262
- pcl::PointCloud<PointType_>::Ptr out_only_indices_cloud_ptr,
263
- pcl::PointCloud<PointType_>::Ptr out_removed_indices_cloud_ptr)
260
+ void RayGroundFilterComponent::initializePointCloud2 (
261
+ const PointCloud2::ConstSharedPtr & in_cloud_ptr, PointCloud2::SharedPtr & out_cloud_msg_ptr)
264
262
{
265
- pcl::ExtractIndices<PointType_> extract_ground;
266
- extract_ground.setInputCloud (in_cloud_ptr);
267
- extract_ground.setIndices (pcl::make_shared<pcl::PointIndices>(in_indices));
268
-
269
- extract_ground.setNegative (false ); // true removes the indices, false leaves only the indices
270
- extract_ground.filter (*out_only_indices_cloud_ptr);
263
+ out_cloud_msg_ptr->header = in_cloud_ptr->header ;
264
+ out_cloud_msg_ptr->height = in_cloud_ptr->height ;
265
+ out_cloud_msg_ptr->fields = in_cloud_ptr->fields ;
266
+ out_cloud_msg_ptr->is_bigendian = in_cloud_ptr->is_bigendian ;
267
+ out_cloud_msg_ptr->point_step = in_cloud_ptr->point_step ;
268
+ out_cloud_msg_ptr->is_dense = in_cloud_ptr->is_dense ;
269
+ out_cloud_msg_ptr->data .resize (in_cloud_ptr->data .size ());
270
+ }
271
271
272
- extract_ground.setNegative (true ); // true removes the indices, false leaves only the indices
273
- extract_ground.filter (*out_removed_indices_cloud_ptr);
272
+ void RayGroundFilterComponent::ExtractPointsIndices (
273
+ const PointCloud2::ConstSharedPtr in_cloud_ptr, pcl::PointIndices & in_indices,
274
+ PointCloud2::SharedPtr ground_cloud_msg_ptr, PointCloud2::SharedPtr no_ground_cloud_msg_ptr)
275
+ {
276
+ initializePointCloud2 (in_cloud_ptr, ground_cloud_msg_ptr);
277
+ initializePointCloud2 (in_cloud_ptr, no_ground_cloud_msg_ptr);
278
+ int point_step = in_cloud_ptr->point_step ;
279
+ size_t ground_count = 0 ;
280
+ size_t no_ground_count = 0 ;
281
+ std::vector<bool > is_ground_idx (in_cloud_ptr->width , false );
282
+ for (const auto & idx : in_indices.indices ) {
283
+ if (std::size_t (idx) >= is_ground_idx.size ()) {
284
+ continue ;
285
+ }
286
+ is_ground_idx[idx] = true ;
287
+ }
288
+ for (size_t i = 0 ; i < is_ground_idx.size (); ++i) {
289
+ if (is_ground_idx[i]) {
290
+ std::memcpy (
291
+ &ground_cloud_msg_ptr->data [ground_count * point_step], &in_cloud_ptr->data [i * point_step],
292
+ point_step);
293
+ ground_count++;
294
+ } else {
295
+ std::memcpy (
296
+ &no_ground_cloud_msg_ptr->data [no_ground_count * point_step],
297
+ &in_cloud_ptr->data [i * point_step], point_step);
298
+ no_ground_count++;
299
+ }
300
+ }
301
+ ground_cloud_msg_ptr->data .resize (ground_count * point_step);
302
+ no_ground_cloud_msg_ptr->data .resize (no_ground_count * point_step);
303
+ ground_cloud_msg_ptr->width = ground_count;
304
+ no_ground_cloud_msg_ptr->width = no_ground_count;
305
+ ground_cloud_msg_ptr->row_step = ground_count * point_step;
306
+ no_ground_cloud_msg_ptr->row_step = no_ground_count * point_step;
274
307
}
275
308
276
309
void RayGroundFilterComponent::filter (
@@ -299,14 +332,11 @@ void RayGroundFilterComponent::filter(
299
332
pcl::PointCloud<PointType_>::Ptr no_ground_cloud_ptr (new pcl::PointCloud<PointType_>);
300
333
pcl::PointCloud<pcl::PointXYZRGB>::Ptr radials_cloud_ptr (new pcl::PointCloud<pcl::PointXYZRGB>);
301
334
302
- ExtractPointsIndices (
303
- current_sensor_cloud_ptr, ground_indices, ground_cloud_ptr, no_ground_cloud_ptr);
304
-
305
335
sensor_msgs::msg::PointCloud2::SharedPtr no_ground_cloud_msg_ptr (
306
336
new sensor_msgs::msg::PointCloud2);
307
- pcl::toROSMsg (*no_ground_cloud_ptr, *no_ground_cloud_msg_ptr);
308
- no_ground_cloud_msg_ptr->header = input->header ;
337
+ sensor_msgs::msg::PointCloud2::SharedPtr ground_cloud_msg_ptr (new sensor_msgs::msg::PointCloud2);
309
338
339
+ ExtractPointsIndices (input, ground_indices, ground_cloud_msg_ptr, no_ground_cloud_msg_ptr);
310
340
output = *no_ground_cloud_msg_ptr;
311
341
}
312
342
0 commit comments