From 7fba36b8ddfd0f8d769ff566c8bda8ec1fb6f365 Mon Sep 17 00:00:00 2001 From: Simon Giraudot Date: Fri, 9 Mar 2018 13:56:43 +0100 Subject: [PATCH] Move Parallel_callback out of function + documentation --- .../include/CGAL/compute_average_spacing.h | 53 +++----------- .../include/CGAL/internal/Parallel_callback.h | 73 +++++++++++++++++++ 2 files changed, 84 insertions(+), 42 deletions(-) create mode 100644 Point_set_processing_3/include/CGAL/internal/Parallel_callback.h diff --git a/Point_set_processing_3/include/CGAL/compute_average_spacing.h b/Point_set_processing_3/include/CGAL/compute_average_spacing.h index a96f8e0ef09..647af926dbb 100644 --- a/Point_set_processing_3/include/CGAL/compute_average_spacing.h +++ b/Point_set_processing_3/include/CGAL/compute_average_spacing.h @@ -40,6 +40,7 @@ #include #ifdef CGAL_LINKED_WITH_TBB +#include #include #include #include @@ -139,42 +140,6 @@ compute_average_spacing(const typename Kernel::Point_3& query, ///< 3D point who } }; - - class Callback_caller - { - const cpp11::function& callback; - tbb::atomic& advancement; - tbb::atomic& interrupted; - std::size_t size; - - public: - Callback_caller (const cpp11::function& callback, - tbb::atomic& interrupted, - tbb::atomic& advancement, - std::size_t size) - : callback (callback) - , advancement (advancement) - , interrupted (interrupted) - , size (size) - { } - - void operator()() - { - tbb::tick_count::interval_t sleeping_time(0.00001); - - while (advancement != size) - { - if (!callback (advancement / double(size))) - { - interrupted = true; - return; - } - std::this_thread::sleep_for(sleeping_time); - } - callback (1.); - } - }; - #endif // CGAL_LINKED_WITH_TBB } /* namespace internal */ @@ -205,7 +170,13 @@ compute_average_spacing(const typename Kernel::Point_3& query, ///< 3D point who \cgalNamedParamsBegin \cgalParamBegin{point_map} a model of `ReadablePropertyMap` with value type `geom_traits::Point_3`. If this parameter is omitted, `CGAL::Identity_property_map` is used.\cgalParamEnd - \cgalParamBegin{callback} an instance of `cpp11::function`\cgalParamEnd + \cgalParamBegin{callback} an instance of + `cpp11::function`. It is called regularly when the + algorithm is running: the current advancement (between 0. and + 1.) is passed as parameter. If it returns `true`, then the + algorithm continues its execution normally; if it returns + `false`, the algorithm is stopped and the average spacing value + estimated on the processed subset is returned.\cgalParamEnd \cgalParamBegin{geom_traits} an instance of a geometric traits class, model of `Kernel`\cgalParamEnd \cgalNamedParamsEnd @@ -278,11 +249,9 @@ compute_average_spacing( tbb::atomic interrupted; interrupted = false; - internal::Callback_caller callback_caller (callback, interrupted, advancement, kd_tree_points.size()); - std::thread* callback_thread; - - if (callback) - callback_thread = new std::thread (callback_caller); + internal::Point_set_processing_3::Parallel_callback + parallel_callback (callback, interrupted, advancement, kd_tree_points.size()); + std::thread* callback_thread = (callback ? new std::thread (parallel_callback) : NULL); std::vector spacings (kd_tree_points.size (), -1); CGAL::internal::Compute_average_spacings diff --git a/Point_set_processing_3/include/CGAL/internal/Parallel_callback.h b/Point_set_processing_3/include/CGAL/internal/Parallel_callback.h new file mode 100644 index 00000000000..b6185059b23 --- /dev/null +++ b/Point_set_processing_3/include/CGAL/internal/Parallel_callback.h @@ -0,0 +1,73 @@ +// Copyright (c) 2018 GeometryFactory (France). +// All rights reserved. +// +// This file is part of CGAL (www.cgal.org). +// You can redistribute it and/or modify it under the terms of the GNU +// General Public License as published by the Free Software Foundation, +// either version 3 of the License, or (at your option) any later version. +// +// Licensees holding a valid commercial license may use this file in +// accordance with the commercial license agreement provided with the software. +// +// This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE +// WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. +// +// $URL$ +// $Id$ +// SPDX-License-Identifier: GPL-3.0+ +// +// Author(s) : Simon Giraudot + +#ifndef CGAL_INTERNAL_PSP_PARALLEL_CALLBACK_H +#define CGAL_INTERNAL_PSP_PARALLEL_CALLBACK_H + +#include + +#include +#define TBB_IMPLEMENT_CPP0X 1 +#include + +namespace CGAL { +namespace internal { +namespace Point_set_processing_3 { + +class Parallel_callback +{ + const cpp11::function& callback; + tbb::atomic& advancement; + tbb::atomic& interrupted; + std::size_t size; + +public: + Parallel_callback (const cpp11::function& callback, + tbb::atomic& interrupted, + tbb::atomic& advancement, + std::size_t size) + : callback (callback) + , advancement (advancement) + , interrupted (interrupted) + , size (size) + { } + + void operator()() + { + tbb::tick_count::interval_t sleeping_time(0.00001); + + while (advancement != size) + { + if (!callback (advancement / double(size))) + { + interrupted = true; + return; + } + std::this_thread::sleep_for(sleeping_time); + } + callback (1.); + } +}; + +} // namespace Point_set_processing_3 +} // namespace internal +} // namespace CGAL + +#endif // CGAL_INTERNAL_PSP_PARALLEL_CALLBACK_H