Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use range-based for & std::find to modernize codes in segmentation #4738

Merged
merged 4 commits into from
May 11, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions segmentation/include/pcl/segmentation/extract_clusters.h
Original file line number Diff line number Diff line change
Expand Up @@ -251,16 +251,16 @@ namespace pcl
Indices nn_indices;
std::vector<float> nn_distances;
// Process all points in the indices vector
for (std::size_t i = 0; i < indices.size (); ++i)
for (const auto& point : indices)
ueqri marked this conversation as resolved.
Show resolved Hide resolved
{
if (processed[indices[i]])
if (processed[point])
continue;

Indices seed_queue;
int sq_idx = 0;
seed_queue.push_back (indices[i]);
seed_queue.push_back (point);

processed[indices[i]] = true;
processed[point] = true;

while (sq_idx < static_cast<int> (seed_queue.size ()))
{
Expand Down
19 changes: 6 additions & 13 deletions segmentation/include/pcl/segmentation/impl/region_growing.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -611,20 +611,13 @@ pcl::RegionGrowing<PointT, NormalT>::getSegmentFromPoint (pcl::index_t index, pc
// to which this point belongs
for (const auto& i_segment : clusters_)
{
bool segment_was_found = false;
for (const auto& i_point : (i_segment.indices))
{
if (i_point == index)
{
segment_was_found = true;
cluster.indices.clear ();
cluster.indices.reserve (i_segment.indices.size ());
std::copy (i_segment.indices.begin (), i_segment.indices.end (), std::back_inserter (cluster.indices));
break;
}
}
if (segment_was_found)
const auto it = std::find (i_segment.indices.cbegin (), i_segment.indices.cend (), index);
if (it != i_segment.indices.cend())
{
// if segment was found
cluster.indices.clear ();
cluster.indices.reserve (i_segment.indices.size ());
std::copy (i_segment.indices.begin (), i_segment.indices.end (), std::back_inserter (cluster.indices));
break;
}
}// next segment
Expand Down
47 changes: 16 additions & 31 deletions segmentation/include/pcl/segmentation/impl/region_growing_rgb.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -482,24 +482,19 @@ pcl::RegionGrowingRGB<PointT, NormalT>::applyRegionMergingAlgorithm ()
num_seg_in_homogeneous_region[i_reg] = 0;
final_segment_number -= 1;

int nghbr_number = static_cast<int> (region_neighbours[reg_index].size ());
for (int i_nghbr = 0; i_nghbr < nghbr_number; i_nghbr++)
for (auto& nghbr : region_neighbours[reg_index])
{
if ( segment_labels_[ region_neighbours[reg_index][i_nghbr].second ] == reg_index )
if ( segment_labels_[ nghbr.second ] == reg_index )
{
region_neighbours[reg_index][i_nghbr].first = std::numeric_limits<float>::max ();
region_neighbours[reg_index][i_nghbr].second = 0;
nghbr.first = std::numeric_limits<float>::max ();
nghbr.second = 0;
}
}
nghbr_number = static_cast<int> (region_neighbours[i_reg].size ());
for (int i_nghbr = 0; i_nghbr < nghbr_number; i_nghbr++)
for (const auto& nghbr : region_neighbours[i_reg])
{
if ( segment_labels_[ region_neighbours[i_reg][i_nghbr].second ] != reg_index )
if ( segment_labels_[ nghbr.second ] != reg_index )
{
std::pair<float, int> pair;
pair.first = region_neighbours[i_reg][i_nghbr].first;
pair.second = region_neighbours[i_reg][i_nghbr].second;
region_neighbours[reg_index].push_back (pair);
region_neighbours[reg_index].push_back (nghbr);
}
}
region_neighbours[i_reg].clear ();
Expand Down Expand Up @@ -535,9 +530,8 @@ pcl::RegionGrowingRGB<PointT, NormalT>::findRegionNeighbours (std::vector< std::
{
int segment_num = static_cast<int> (regions_in[i_reg].size ());
neighbours_out[i_reg].reserve (segment_num * region_neighbour_number_);
mvieth marked this conversation as resolved.
Show resolved Hide resolved
for (int i_seg = 0; i_seg < segment_num; i_seg++)
for (const auto& curr_segment : regions_in[i_reg])
{
int curr_segment = regions_in[i_reg][i_seg];
int nghbr_number = static_cast<int> (segment_neighbours_[curr_segment].size ());
std::pair<float, int> pair;
for (int i_nghbr = 0; i_nghbr < nghbr_number; i_nghbr++)
Expand Down Expand Up @@ -571,10 +565,8 @@ pcl::RegionGrowingRGB<PointT, NormalT>::assembleRegions (std::vector<unsigned in

std::vector<int> counter;
counter.resize (num_regions, 0);
int point_number = static_cast<int> (indices_->size ());
for (int i_point = 0; i_point < point_number; i_point++)
for (const auto& point_index : (*indices_))
{
int point_index = (*indices_)[i_point];
int index = point_labels_[point_index];
index = segment_labels_[index];
clusters_[index].indices[ counter[index] ] = point_index;
Expand Down Expand Up @@ -735,22 +727,15 @@ pcl::RegionGrowingRGB<PointT, NormalT>::getSegmentFromPoint (pcl::index_t index,
}
// if we have already made the segmentation, then find the segment
// to which this point belongs
for (auto i_segment = clusters_.cbegin (); i_segment != clusters_.cend (); i_segment++)
for (const auto& i_segment : clusters_)
{
bool segment_was_found = false;
for (std::size_t i_point = 0; i_point < i_segment->indices.size (); i_point++)
{
if (i_segment->indices[i_point] == index)
{
segment_was_found = true;
cluster.indices.clear ();
cluster.indices.reserve (i_segment->indices.size ());
std::copy (i_segment->indices.begin (), i_segment->indices.end (), std::back_inserter (cluster.indices));
break;
}
}
if (segment_was_found)
const auto it = std::find (i_segment.indices.cbegin (), i_segment.indices.cend (), index);
if (it != i_segment.indices.cend())
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps, we can rewrite the condition to exit early

for(loop) {
    const auto it = find();
    if (it == cend) { continue; }
    // rest of logic
    break;
}

Copy link
Contributor Author

@ueqri ueqri Apr 30, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using if (it == cend) { continue; } is more explicit, but could we just stay on the current to make it concise:blush:?

{
// if segment was found
cluster.indices.clear ();
cluster.indices.reserve (i_segment.indices.size ());
std::copy (i_segment.indices.begin (), i_segment.indices.end (), std::back_inserter (cluster.indices));
break;
}
}// next segment
Expand Down
15 changes: 7 additions & 8 deletions segmentation/include/pcl/segmentation/impl/unary_classifier.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -174,25 +174,24 @@ pcl::UnaryClassifier<PointT>::getCloudWithLabel (typename pcl::PointCloud<PointT
// find the 'label' field index
std::vector <pcl::PCLPointField> fields;
int label_idx = -1;
pcl::PointCloud <PointT> point;
label_idx = pcl::getFieldIndex<PointT> ("label", fields);

if (label_idx != -1)
{
for (std::size_t i = 0; i < in->size (); i++)
for (auto& point : (*in))
ueqri marked this conversation as resolved.
Show resolved Hide resolved
{
// get the 'label' field
std::uint32_t label;
memcpy (&label, reinterpret_cast<char*> (&(*in)[i]) + fields[label_idx].offset, sizeof(std::uint32_t));
memcpy (&label, reinterpret_cast<char*> (&point) + fields[label_idx].offset, sizeof(std::uint32_t));

if (static_cast<int> (label) == label_num)
{
pcl::PointXYZ point;
pcl::PointXYZ tmp;
// X Y Z
point.x = (*in)[i].x;
point.y = (*in)[i].y;
point.z = (*in)[i].z;
out->points.push_back (point);
tmp.x = point.x;
tmp.y = point.y;
tmp.z = point.z;
out->points.push_back (tmp);
ueqri marked this conversation as resolved.
Show resolved Hide resolved
}
}
out->width = out->size ();
Expand Down