From 642aea115aa0b3944f06291c0103c29fc589f87f Mon Sep 17 00:00:00 2001 From: Simon Giraudot Date: Thu, 31 May 2018 14:05:53 +0200 Subject: [PATCH] Add variant of classify() to get detailed output --- .../include/CGAL/Classification/classify.h | 85 +++++++++++++++++++ 1 file changed, 85 insertions(+) diff --git a/Classification/include/CGAL/Classification/classify.h b/Classification/include/CGAL/Classification/classify.h index bf491b89c71..59bd19cfe2f 100644 --- a/Classification/include/CGAL/Classification/classify.h +++ b/Classification/include/CGAL/Classification/classify.h @@ -87,6 +87,53 @@ namespace internal { }; + template + class Classify_detailed_output_functor + { + const Label_set& m_labels; + const Classifier& m_classifier; + LabelIndexRange& m_out; + ProbabilitiesRanges& m_prob; + + public: + + Classify_detailed_output_functor (const Label_set& labels, + const Classifier& classifier, + LabelIndexRange& out, + ProbabilitiesRanges& prob) + : m_labels (labels), m_classifier (classifier), m_out (out), m_prob (prob) + { } + +#ifdef CGAL_LINKED_WITH_TBB + void operator()(const tbb::blocked_range& r) const + { + for (std::size_t s = r.begin(); s != r.end(); ++ s) + apply(s); + } +#endif // CGAL_LINKED_WITH_TBB + + inline void apply (std::size_t s) const + { + std::size_t nb_class_best=0; + std::vector values; + m_classifier (s, values); + + float val_class_best = 0.f; + for(std::size_t k = 0; k < m_labels.size(); ++ k) + { + m_prob[k][s] = values[k]; + if(val_class_best < values[k]) + { + val_class_best = values[k]; + nb_class_best = k; + } + } + + m_out[s] = static_cast(nb_class_best); + } + + }; + template class Classify_functor_local_smoothing_preprocessing { @@ -344,6 +391,44 @@ namespace internal { } } + /// \cond SKIP_IN_MANUAL + // variant to get a detailed output (not documented yet) + template + void classify (const ItemRange& input, + const Label_set& labels, + const Classifier& classifier, + LabelIndexRange& output, + ProbabilitiesRanges& probabilities) + { + output.resize (input.size()); + probabilities.resize (labels.size()); + for (std::size_t i = 0; i < probabilities.size(); ++ i) + probabilities[i].resize (input.size()); + + internal::Classify_detailed_output_functor + f (labels, classifier, output, probabilities); + +#ifndef CGAL_LINKED_WITH_TBB + CGAL_static_assertion_msg (!(boost::is_convertible::value), + "Parallel_tag is enabled but TBB is unavailable."); +#else + if (boost::is_convertible::value) + { + tbb::parallel_for(tbb::blocked_range(0, input.size ()), f); + } + else +#endif + { + for (std::size_t i = 0; i < input.size(); ++ i) + f.apply(i); + } + } + /// \endcond + /*! \ingroup PkgClassificationMain