Skip to content
Merged
Show file tree
Hide file tree
Changes from 6 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
24 changes: 24 additions & 0 deletions filters/include/pcl/filters/farthest_point_sampling.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,12 +79,36 @@ namespace pcl
return (seed_);
}

/** \brief Set the number of threads to use when operating in parallel
* \param nr_threads the number of threads to use
*/
inline void
setNumberOfThreads (unsigned int nr_threads)
{
#ifdef _OPENMP
nr_threads_ = nr_threads == 0 ? omp_get_num_procs() : nr_threads;
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
nr_threads_ = nr_threads == 0 ? omp_get_num_procs() : nr_threads;
nr_threads_ = nr_threads != 0 ? nr_threads : omp_get_num_procs();

To keep it more consistent with existing

Copy link
Contributor

Choose a reason for hiding this comment

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

Missed this one 😄

#else
if (nr_threads_ != 1)
PCL_WARN("OpenMP is not available. Keeping number of threads unchanged at 1\n");
#endif
}

/** \brief Get the value of the internal \a nr_threads_ parameter.
*/
inline unsigned int
getNumberOfThreads () const
{
return nr_threads_;
}

protected:

/** \brief Number of points that will be returned. */
std::size_t sample_size_;
/** \brief Random number seed. */
unsigned int seed_;
/** \brief Number of threads */
unsigned int nr_threads_{1};

/** \brief Sample of point indices
* \param indices indices of the filtered point cloud
Expand Down
35 changes: 24 additions & 11 deletions filters/include/pcl/filters/impl/farthest_point_sampling.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,23 +51,36 @@ pcl::FarthestPointSampling<PointT>::applyFilter (Indices &indices)

for (std::size_t j = 1; j < sample_size_; ++j)
{
index_t next_max_index = 0;

const PointT& max_index_point = (*input_)[toCloudIndex(max_index)];
//recompute distances
for (std::size_t i = 0; i < size; ++i)

#pragma omp parallel \
default(none) \
shared(distances_to_selected_points, max_index, max_index_point, size, toCloudIndex) \
num_threads(nr_threads_)
{
if (distances_to_selected_points[i] == -1.0)
continue;
distances_to_selected_points[i] = std::min(distances_to_selected_points[i], geometry::distance((*input_)[toCloudIndex(i)], max_index_point));
if (distances_to_selected_points[i] > distances_to_selected_points[next_max_index])
next_max_index = i;
}
std::ptrdiff_t local_max_index = max_index;

#pragma omp for
for (std::ptrdiff_t i = 0; i < static_cast<std::ptrdiff_t>(size); ++i)
{
if (distances_to_selected_points[i] == -1.0)
continue;
distances_to_selected_points[i] = std::min(distances_to_selected_points[i], geometry::distance((*input_)[toCloudIndex(i)], max_index_point));
if (distances_to_selected_points[i] > distances_to_selected_points[local_max_index]) {
local_max_index = i;
}
}

#pragma omp critical
{
if (distances_to_selected_points[local_max_index] > distances_to_selected_points[max_index])
max_index = local_max_index;
}
} //pragma omp parallel

//select farthest point based on previously calculated distances
//since distance is set to -1 for all selected elements,previously selected
//elements are guaranteed to not be selected
max_index = next_max_index;
distances_to_selected_points[max_index] = -1.0;
indices.push_back(toCloudIndex(max_index));
//set distance to -1 to ignore during max element search
Expand Down