Skip to content

Commit 627020c

Browse files
committed
fix: add loop for parallel
Signed-off-by: badai-nguyen <dai.nguyen@tier4.jp>
1 parent b1680c5 commit 627020c

File tree

2 files changed

+37
-11
lines changed

2 files changed

+37
-11
lines changed

perception/ground_segmentation/include/ground_segmentation/scan_ground_filter_nodelet.hpp

+1
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,7 @@ class ScanGroundFilterComponent : public pointcloud_preprocessor::Filter
174174
bool use_virtual_ground_point_;
175175
bool use_recheck_ground_cluster_; // to enable recheck ground cluster
176176
size_t radial_dividers_num_;
177+
int omp_num_threads_{1};
177178
VehicleInfo vehicle_info_;
178179

179180
/*!

perception/ground_segmentation/src/scan_ground_filter_nodelet.cpp

+36-11
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include <memory>
2323
#include <string>
2424
#include <vector>
25+
#include <omp.h>
2526

2627
namespace ground_segmentation
2728
{
@@ -55,6 +56,7 @@ ScanGroundFilterComponent::ScanGroundFilterComponent(const rclcpp::NodeOptions &
5556
split_height_distance_ = declare_parameter("split_height_distance", 0.2);
5657
use_virtual_ground_point_ = declare_parameter("use_virtual_ground_point", true);
5758
use_recheck_ground_cluster_ = declare_parameter("use_recheck_ground_cluster", true);
59+
omp_num_threads_ = declare_parameter("omp_num_threads", 1);
5860
radial_dividers_num_ = std::ceil(2.0 * M_PI / radial_divider_angle_rad_);
5961
vehicle_info_ = VehicleInfoUtil(*this).getVehicleInfo();
6062

@@ -288,7 +290,12 @@ void ScanGroundFilterComponent::classifyPointCloudGridScan(
288290
pcl::PointIndices & out_no_ground_indices)
289291
{
290292
out_no_ground_indices.indices.clear();
293+
std::vector<pcl::PointIndices> ground_indices;
294+
ground_indices.resize(in_radial_ordered_clouds.size());
295+
omp_set_num_threads(omp_num_threads_);
296+
#pragma omp parallel for
291297
for (size_t i = 0; i < in_radial_ordered_clouds.size(); ++i) {
298+
auto & ground_indices_private = ground_indices[i];
292299
PointsCentroid ground_cluster;
293300
ground_cluster.initialize();
294301
std::vector<GridCenter> gnd_grids;
@@ -299,10 +306,9 @@ void ScanGroundFilterComponent::classifyPointCloudGridScan(
299306
continue;
300307
}
301308

302-
// check the first point in ray
303-
auto * p = &in_radial_ordered_clouds[i][0];
304-
PointRef * prev_p;
305-
prev_p = &in_radial_ordered_clouds[i][0]; // for checking the distance to prev point
309+
// // check the first point in ray
310+
auto p = &in_radial_ordered_clouds[i][0];
311+
auto prev_p = &in_radial_ordered_clouds[i][0]; // for checking the distance to prev point
306312

307313
bool initialized_first_gnd_grid = false;
308314
bool prev_list_init = false;
@@ -319,7 +325,10 @@ void ScanGroundFilterComponent::classifyPointCloudGridScan(
319325
if (
320326
!initialized_first_gnd_grid && global_slope_p >= global_slope_max_angle_rad_ &&
321327
p->orig_point->z > non_ground_height_threshold_local) {
322-
out_no_ground_indices.indices.push_back(p->orig_index);
328+
// #pragma omp critical
329+
{
330+
ground_indices_private.indices.push_back(p->orig_index);
331+
}
323332
p->point_state = PointLabel::NON_GROUND;
324333
prev_p = p;
325334
continue;
@@ -358,7 +367,7 @@ void ScanGroundFilterComponent::classifyPointCloudGridScan(
358367
if (p->grid_id > prev_p->grid_id && ground_cluster.getAverageRadius() > 0.0) {
359368
// check if the prev grid have ground point cloud
360369
if (use_recheck_ground_cluster_) {
361-
recheckGroundCluster(ground_cluster, non_ground_height_threshold_, out_no_ground_indices);
370+
recheckGroundCluster(ground_cluster, non_ground_height_threshold_, ground_indices_private);
362371
}
363372
curr_gnd_grid.radius = ground_cluster.getAverageRadius();
364373
curr_gnd_grid.avg_height = ground_cluster.getAverageHeight();
@@ -380,13 +389,13 @@ void ScanGroundFilterComponent::classifyPointCloudGridScan(
380389
points_xy_distance < split_points_distance_tolerance_ &&
381390
p->orig_point->z > prev_p->orig_point->z) {
382391
p->point_state = PointLabel::NON_GROUND;
383-
out_no_ground_indices.indices.push_back(p->orig_index);
392+
ground_indices_private.indices.push_back(p->orig_index);
384393
prev_p = p;
385394
continue;
386395
}
387396

388397
if (global_slope_p > global_slope_max_angle_rad_) {
389-
out_no_ground_indices.indices.push_back(p->orig_index);
398+
ground_indices_private.indices.push_back(p->orig_index);
390399
prev_p = p;
391400
continue;
392401
}
@@ -401,14 +410,30 @@ void ScanGroundFilterComponent::classifyPointCloudGridScan(
401410
} else if (p->radius - gnd_grids.back().radius < gnd_grid_continual_thresh_ * p->grid_size) {
402411
checkDiscontinuousGndGrid(*p, gnd_grids);
403412
} else {
404-
checkBreakGndGrid(*p, gnd_grids);
413+
// checkBreakGndGrid(*p, gnd_grids);
414+
{
415+
float tmp_delta_avg_z = p->orig_point->z - gnd_grids.back().avg_height;
416+
float tmp_delta_radius = p->radius - gnd_grids.back().radius;
417+
float local_slope = std::atan(tmp_delta_avg_z / tmp_delta_radius);
418+
if (abs(local_slope) < global_slope_max_angle_rad_) {
419+
p->point_state = PointLabel::GROUND;
420+
} else if (local_slope > global_slope_max_angle_rad_) {
421+
p->point_state = PointLabel::NON_GROUND;
422+
}
423+
}
405424
}
406425
if (p->point_state == PointLabel::NON_GROUND) {
407-
out_no_ground_indices.indices.push_back(p->orig_index);
426+
ground_indices_private.indices.push_back(p->orig_index);
408427
} else if (p->point_state == PointLabel::GROUND) {
409428
ground_cluster.addPoint(p->radius, p->orig_point->z, p->orig_index);
410429
}
411-
prev_p = p;
430+
prev_p = p;
431+
}
432+
}
433+
434+
for(size_t i = 0; i < in_radial_ordered_clouds.size();++i){
435+
if(ground_indices.at(i).indices.size() > 0){
436+
out_no_ground_indices.indices.insert(out_no_ground_indices.indices.end(),ground_indices.at(i).indices.begin(),ground_indices.at(i).indices.end());
412437
}
413438
}
414439
}

0 commit comments

Comments
 (0)