1 // Copyright (c) 2018 GeometryFactory Sarl (France).
2 // All rights reserved.
3 //
4 // This file is part of CGAL (www.cgal.org).
5 //
6 // $URL: https://github.com/CGAL/cgal/blob/v5.3/Classification/include/CGAL/Classification/Feature/Cluster_mean_of_feature.h $
7 // $Id: Cluster_mean_of_feature.h 0e934b1 2020-08-04T13:16:13+02:00 Simon Giraudot
8 // SPDX-License-Identifier: GPL-3.0-or-later OR LicenseRef-Commercial
9 //
10 // Author(s)     : Simon Giraudot
11 
12 #ifndef CGAL_CLASSIFICATION_FEATURE_CLUSTER_MEAN_FEATURE_H
13 #define CGAL_CLASSIFICATION_FEATURE_CLUSTER_MEAN_FEATURE_H
14 
15 #include <CGAL/license/Classification.h>
16 
17 #include <vector>
18 #include <sstream>
19 
20 #include <CGAL/Classification/Feature_base.h>
21 
22 namespace CGAL {
23 
24 namespace Classification {
25 
26 namespace Feature {
27 
28   /*!
29     \ingroup PkgClassificationCluster
30 
31     \brief %Feature that computes the mean values of an itemwise
32     feature over the respective items of clusters.
33 
34     Its default name is "mean_" + the name of the itemwise feature.
35   */
36 class Cluster_mean_of_feature : public CGAL::Classification::Feature_base
37 {
38   std::vector<float> m_values;
39 
40 public:
41 
42   /*!
43     \brief constructs the feature.
44 
45     \tparam ClusterRange model of `ConstRange`. Its iterator type
46     is `RandomAccessIterator` and its value type is the key type of
47     `Cluster`.
48 
49     \param clusters input range.
50     \param itemwise_feature feature that takes values on the range of
51     items from which `clusters` is a subset.
52   */
53   template <typename ClusterRange>
Cluster_mean_of_feature(ClusterRange & clusters,Feature_handle itemwise_feature)54   Cluster_mean_of_feature (ClusterRange& clusters,
55                            Feature_handle itemwise_feature)
56   {
57     std::ostringstream oss;
58     oss << "mean_" << itemwise_feature->name();
59     this->set_name (oss.str());
60 
61     m_values.reserve (clusters.size());
62     for (std::size_t i = 0; i < clusters.size(); ++ i)
63     {
64       double mean = 0.;
65 
66       for (std::size_t j = 0; j < clusters[i].size(); ++ j)
67         mean += double(itemwise_feature->value (clusters[i].index(j)));
68       mean /= clusters[i].size();
69       m_values.push_back (float(mean));
70     }
71   }
72 
73   /// \cond SKIP_IN_MANUAL
value(std::size_t cluster_index)74   virtual float value (std::size_t cluster_index)
75   {
76     return m_values[cluster_index];
77   }
78   /// \endcond
79 };
80 
81 } // namespace Feature
82 
83 } // namespace Classification
84 
85 } // namespace CGAL
86 
87 #endif // CGAL_CLASSIFICATION_FEATURE_CLUSTER_MEAN_FEATURE_H
88