Skip to content

Commit 729eec0

Browse files
fix(ground_segmentation): add intensity field (#6791)
* fix(grond_segmentation): add intensity Signed-off-by: badai-nguyen <dai.nguyen@tier4.jp> * fix(ray_ground_filter): change to pointXYZI Signed-off-by: badai-nguyen <dai.nguyen@tier4.jp> * fix(ransac_ground_filter): change to pointXYZI Signed-off-by: badai-nguyen <dai.nguyen@tier4.jp> * fix(scan_ground_filter): intensity bug fix Signed-off-by: badai-nguyen <dai.nguyen@tier4.jp> * fix(ground_segmentation): add field select option Signed-off-by: badai-nguyen <dai.nguyen@tier4.jp> * Revert "fix(ground_segmentation): add field select option" This reverts commit 36fc554. * fix: copy input field to output Signed-off-by: badai-nguyen <dai.nguyen@tier4.jp> * fix(ray_ground_filter): copy input fields Signed-off-by: badai-nguyen <dai.nguyen@tier4.jp> * fix(ransac_ground_filter): copy input fields Signed-off-by: badai-nguyen <dai.nguyen@tier4.jp> * fix(ray_ground_filter): indices extract bug fix Signed-off-by: badai-nguyen <dai.nguyen@tier4.jp> * refactor: ray_ground_filter Signed-off-by: badai-nguyen <dai.nguyen@tier4.jp> * fix: indices max Signed-off-by: badai-nguyen <dai.nguyen@tier4.jp> * style(pre-commit): autofix --------- Signed-off-by: badai-nguyen <dai.nguyen@tier4.jp> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent 569d56c commit 729eec0

File tree

4 files changed

+78
-32
lines changed

4 files changed

+78
-32
lines changed

perception/ground_segmentation/include/ground_segmentation/ray_ground_filter_nodelet.hpp

+5-4
Original file line numberDiff line numberDiff line change
@@ -186,15 +186,16 @@ 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 pcl::PointCloud<PointType_>::Ptr in_cloud_ptr, const pcl::PointIndices & in_indices,
190-
pcl::PointCloud<PointType_>::Ptr out_only_indices_cloud_ptr,
191-
pcl::PointCloud<PointType_>::Ptr out_removed_indices_cloud_ptr);
189+
const PointCloud2::ConstSharedPtr in_cloud_ptr, pcl::PointIndices & in_indices,
190+
PointCloud2::SharedPtr out_only_indices_cloud_ptr,
191+
PointCloud2::SharedPtr out_removed_indices_cloud_ptr);
192192

193193
boost::optional<float> calcPointVehicleIntersection(const Point & point);
194194

195195
void setVehicleFootprint(
196196
const double min_x, const double max_x, const double min_y, const double max_y);
197-
197+
void initializePointCloud2(
198+
const PointCloud2::ConstSharedPtr & in_cloud_ptr, PointCloud2::SharedPtr & out_cloud_msg_ptr);
198199
/** \brief Parameter service callback result : needed to be hold */
199200
OnSetParametersCallbackHandle::SharedPtr set_param_res_;
200201

perception/ground_segmentation/src/ransac_ground_filter_nodelet.cpp

+25-8
Original file line numberDiff line numberDiff line change
@@ -284,19 +284,36 @@ void RANSACGroundFilterComponent::filter(
284284
const Eigen::Affine3d plane_affine = getPlaneAffine(*segment_ground_cloud_ptr, plane_normal);
285285
pcl::PointCloud<PointType>::Ptr no_ground_cloud_ptr(new pcl::PointCloud<PointType>);
286286

287+
int x_offset = input->fields[pcl::getFieldIndex(*input, "x")].offset;
288+
int y_offset = input->fields[pcl::getFieldIndex(*input, "y")].offset;
289+
int z_offset = input->fields[pcl::getFieldIndex(*input, "z")].offset;
290+
int point_step = input->point_step;
291+
292+
sensor_msgs::msg::PointCloud2::SharedPtr no_ground_cloud_msg_ptr(
293+
new sensor_msgs::msg::PointCloud2);
294+
no_ground_cloud_msg_ptr->header = input->header;
295+
no_ground_cloud_msg_ptr->fields = input->fields;
296+
no_ground_cloud_msg_ptr->point_step = point_step;
297+
size_t output_size = 0;
287298
// use not downsampled pointcloud for extract pointcloud that higher than height threshold
288-
for (const auto & p : current_sensor_cloud_ptr->points) {
289-
const Eigen::Vector3d transformed_point =
290-
plane_affine.inverse() * Eigen::Vector3d(p.x, p.y, p.z);
299+
for (size_t global_size = 0; global_size < input->data.size(); global_size += point_step) {
300+
float x = *reinterpret_cast<float *>(input->data[global_size + x_offset]);
301+
float y = *reinterpret_cast<float *>(input->data[global_size + y_offset]);
302+
float z = *reinterpret_cast<float *>(input->data[global_size + z_offset]);
303+
const Eigen::Vector3d transformed_point = plane_affine.inverse() * Eigen::Vector3d(x, y, z);
291304
if (std::abs(transformed_point.z()) > height_threshold_) {
292-
no_ground_cloud_ptr->points.push_back(p);
305+
std::memcpy(
306+
&no_ground_cloud_msg_ptr->data[output_size], &input->data[global_size], point_step);
307+
output_size += point_step;
293308
}
294309
}
310+
no_ground_cloud_msg_ptr->data.resize(output_size);
311+
no_ground_cloud_msg_ptr->height = input->height;
312+
no_ground_cloud_msg_ptr->width = output_size / point_step / input->height;
313+
no_ground_cloud_msg_ptr->row_step = output_size / input->height;
314+
no_ground_cloud_msg_ptr->is_dense = input->is_dense;
315+
no_ground_cloud_msg_ptr->is_bigendian = input->is_bigendian;
295316

296-
sensor_msgs::msg::PointCloud2::SharedPtr no_ground_cloud_msg_ptr(
297-
new sensor_msgs::msg::PointCloud2);
298-
pcl::toROSMsg(*no_ground_cloud_ptr, *no_ground_cloud_msg_ptr);
299-
no_ground_cloud_msg_ptr->header = input->header;
300317
sensor_msgs::msg::PointCloud2::SharedPtr no_ground_cloud_transformed_msg_ptr(
301318
new sensor_msgs::msg::PointCloud2);
302319
if (!transformPointCloud(

perception/ground_segmentation/src/ray_ground_filter_nodelet.cpp

+47-17
Original file line numberDiff line numberDiff line change
@@ -257,20 +257,53 @@ void RayGroundFilterComponent::ClassifyPointCloud(
257257
// return (true);
258258
// }
259259

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)
264262
{
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+
}
271271

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;
274307
}
275308

276309
void RayGroundFilterComponent::filter(
@@ -299,14 +332,11 @@ void RayGroundFilterComponent::filter(
299332
pcl::PointCloud<PointType_>::Ptr no_ground_cloud_ptr(new pcl::PointCloud<PointType_>);
300333
pcl::PointCloud<pcl::PointXYZRGB>::Ptr radials_cloud_ptr(new pcl::PointCloud<pcl::PointXYZRGB>);
301334

302-
ExtractPointsIndices(
303-
current_sensor_cloud_ptr, ground_indices, ground_cloud_ptr, no_ground_cloud_ptr);
304-
305335
sensor_msgs::msg::PointCloud2::SharedPtr no_ground_cloud_msg_ptr(
306336
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);
309338

339+
ExtractPointsIndices(input, ground_indices, ground_cloud_msg_ptr, no_ground_cloud_msg_ptr);
310340
output = *no_ground_cloud_msg_ptr;
311341
}
312342

perception/ground_segmentation/src/scan_ground_filter_nodelet.cpp

+1-3
Original file line numberDiff line numberDiff line change
@@ -575,8 +575,6 @@ void ScanGroundFilterComponent::extractObjectPoints(
575575
std::memcpy(
576576
&out_object_cloud.data[output_data_size], &in_cloud_ptr->data[i * in_cloud_ptr->point_step],
577577
in_cloud_ptr->point_step * sizeof(uint8_t));
578-
*reinterpret_cast<float *>(&out_object_cloud.data[output_data_size + intensity_offset_]) =
579-
1; // set intensity to 1
580578
output_data_size += in_cloud_ptr->point_step;
581579
}
582580
}
@@ -606,7 +604,7 @@ void ScanGroundFilterComponent::faster_filter(
606604
output.row_step = no_ground_indices.indices.size() * input->point_step;
607605
output.data.resize(output.row_step);
608606
output.width = no_ground_indices.indices.size();
609-
output.fields.assign(input->fields.begin(), input->fields.begin() + 3);
607+
output.fields = input->fields;
610608
output.is_dense = true;
611609
output.height = input->height;
612610
output.is_bigendian = input->is_bigendian;

0 commit comments

Comments
 (0)