Add tpram concurrency to Neighbor_search::get_close_curves()

This commit is contained in:
Andreas Fabri 2025-05-07 14:09:18 +01:00
parent 00b5174181
commit 69d0c792e6
2 changed files with 48 additions and 4 deletions

View File

@ -37,14 +37,14 @@ int main()
curves.pop_back();
using Neighbor_search = CGAL::Frechet_distance::Neighbor_search<Curve, Traits>;
Neighbor_search ds(curves, CGAL::Parallel_tag());
Neighbor_search ds(curves, CGAL::Parallel_if_available_tag());
for(const Curve& c : curves){
std::pair<double, double> res = CGAL::bounded_error_Frechet_distance(c, query, 0.000001);
std::cout << "The Frechet distance between the polylines is between " << res.first << " and " << res.second << std::endl;
}
double distance = 16;
std::vector<std::size_t> result = ds.get_close_curves(query, distance);
std::vector<std::size_t> result = ds.get_close_curves<CGAL::Parallel_if_available_tag>(query, distance);
std::cout << result.size() << " curves at Frechet distance closer than " << distance << std::endl;
}

View File

@ -67,10 +67,22 @@ public:
using Point = Traits::Point_d;
#endif
std::vector<std::size_t> get_close_curves(const PointRange& query, double distance, Sequential_tag);
std::vector<std::size_t> get_close_curves(const PointRange& query, double distance, Parallel_tag);
/*! returns the indices of the inserted polylines that are closer than `distance` to the polyline `query`.
* \param query the query polyline
* \param distance the distance bound
* \tparam ConcurrencyTag enables sequential versus parallel computation.
* Possible values are `Sequential_tag`, `Parallel_tag`, and `Parallel_if_available_tag`.
* \return a vector of indices of the inserted polylines that are closer than `distance` to the polyline `query`.
*/
std::vector<std::size_t> get_close_curves(const PointRange& query, double distance);
template <typename ConcurrencyTag = Sequential_tag>
std::vector<std::size_t> get_close_curves(const PointRange& query, double distance)
{
return get_close_curves(query, distance, ConcurrencyTag());
}
private:
Polylines curves;
@ -85,7 +97,7 @@ std::vector<std::size_t>
auto
#endif
Neighbor_search<PointRange, Traits>::get_close_curves(
const PointRange& curve, double distance) -> std::vector<std::size_t>
const PointRange& curve, double distance, Sequential_tag) -> std::vector<std::size_t>
{
auto result = kd_tree.search(curve, distance);
@ -103,6 +115,38 @@ Neighbor_search<PointRange, Traits>::get_close_curves(
return result;
}
template <class PointRange, class Traits>
#ifdef DOXYGEN_RUNNING
std::vector<std::size_t>
#else
auto
#endif
Neighbor_search<PointRange, Traits>::get_close_curves(
const PointRange& curve, double distance, Parallel_tag) -> std::vector<std::size_t>
{
std::vector<std::size_t> result;
#ifdef CGAL_LINKED_WITH_TBB
result = kd_tree.search(curve, distance);
std::vector<int> to_remove(result.size(), 0);
tbb::parallel_for(tbb::blocked_range<std::size_t>(0, result.size()), [&](const tbb::blocked_range<std::size_t>& r) {
for (std::size_t i = r.begin(); i != r.end(); ++i) {
to_remove[i] = is_Frechet_distance_larger(curve, curves[result[i]], distance, parameters::geom_traits(Traits()));
}
});
int compact = 0;
for(int i = 0; i < to_remove.size(); ++i) {
if(! to_remove[i]) {
result[compact] = result[i];
++compact;
}
}
result.resize(compact);
#endif
return result;
}
}
} // end of namespace CGAL