Point Cloud Library (PCL)  1.14.1-dev
kmeans.h
1 /*
2  * Software License Agreement (BSD License)
3  *
4  * Point Cloud Library (PCL) - www.pointclouds.org
5  *
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * * Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  * * Redistributions in binary form must reproduce the above
15  * copyright notice, this list of conditions and the following
16  * disclaimer in the documentation and/or other materials provided
17  * with the distribution.
18  * * Neither the name of Willow Garage, Inc. nor the names of its
19  * contributors may be used to endorse or promote products derived
20  * from this software without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
25  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
26  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
27  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
28  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
29  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
30  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
32  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33  * POSSIBILITY OF SUCH DAMAGE.
34  *
35  * Author : Christian Potthast
36  * Email : potthast@usc.edu
37  *
38  */
39 
40 #pragma once
41 
42 #include <pcl/memory.h>
43 #include <pcl/pcl_macros.h>
44 
45 #include <set>
46 #include <vector> // for vector
47 
48 namespace pcl {
49 
50 /** K-means clustering.
51  *
52  * \author Christian Potthast
53  * \ingroup ML
54  */
56 public:
57  using PointId = unsigned int; // the id of this point
58  using ClusterId = unsigned int; // the id of this cluster
59 
60  // using Point = std::vector<Coord>; // a point (a centroid)
61 
62  using SetPoints = std::set<PointId>; // set of points
63 
64  using Point = std::vector<float>;
65 
66  // ClusterId -> (PointId, PointId, PointId, .... )
67  using ClustersToPoints = std::vector<SetPoints>;
68  // PointId -> ClusterId
69  using PointsToClusters = std::vector<ClusterId>;
70  // coll of centroids
71  using Centroids = std::vector<Point>;
72 
73  /** Empty constructor. */
74  Kmeans(unsigned int num_points, unsigned int num_dimensions);
75 
76  /** This method sets the k-means cluster size.
77  *
78  * \param[in] k number of clusters
79  */
80  void
81  setClusterSize(unsigned int k)
82  {
83  num_clusters_ = k;
84  };
85 
86  /*
87  void
88  setClusterField (std::string field_name)
89  {
90  cluster_field_name_ = field_name;
91  };
92  */
93 
94  // void
95  // getClusterCentroids (PointT &out);
96 
97  // void
98  // cluster (std::vector<PointIndices> &clusters);
99 
100  void
102 
103  void
104  setInputData(std::vector<Point>& data)
105  {
106  if (num_points_ != data.size())
107  std::cout << "Data vector not the same" << std::endl;
108 
109  data_ = data;
110  }
111 
112  void
113  addDataPoint(Point& data_point)
114  {
115  if (num_dimensions_ != data_point.size())
116  std::cout << "Dimensions not the same" << std::endl;
117 
118  data_.push_back(data_point);
119  }
120 
121  // Initial partition points among available clusters
122  void
124 
125  void
127 
128  // distance between two points
129  float
130  distance(const Point& x, const Point& y)
131  {
132  float total = 0.0;
133  float diff;
134 
135  auto cpy = y.cbegin();
136  for (auto cpx = x.cbegin(), cpx_end = x.cend(); cpx != cpx_end; ++cpx, ++cpy) {
137  diff = *cpx - *cpy;
138  total += (diff * diff);
139  }
140  return total; // no need to take sqrt, which is monotonic
141  }
142 
143  Centroids
145  {
146  return centroids_;
147  }
148 
149 protected:
150  // Members derived from the base class
151  /*
152  using BasePCLBase::input_;
153  using BasePCLBase::indices_;
154  using BasePCLBase::initCompute;
155  using BasePCLBase::deinitCompute;
156  */
157 
158  unsigned int num_points_;
159  unsigned int num_dimensions_;
160 
161  /** The number of clusters. */
162  unsigned int num_clusters_;
163 
164  /** The cluster centroids. */
165  // std::vector
166 
167  // std::string cluster_field_name_;
168 
169  // one data point
170 
171  // all data points
172  std::vector<Point> data_;
173 
177 
178 public:
180 };
181 
182 } // namespace pcl
K-means clustering.
Definition: kmeans.h:55
unsigned int ClusterId
Definition: kmeans.h:58
ClustersToPoints clusters_to_points_
Definition: kmeans.h:174
unsigned int num_clusters_
The number of clusters.
Definition: kmeans.h:162
void setInputData(std::vector< Point > &data)
Definition: kmeans.h:104
unsigned int num_dimensions_
Definition: kmeans.h:159
Centroids get_centroids()
Definition: kmeans.h:144
std::vector< float > Point
Definition: kmeans.h:64
unsigned int PointId
Definition: kmeans.h:57
void initialClusterPoints()
void addDataPoint(Point &data_point)
Definition: kmeans.h:113
void setClusterSize(unsigned int k)
This method sets the k-means cluster size.
Definition: kmeans.h:81
std::vector< ClusterId > PointsToClusters
Definition: kmeans.h:69
std::vector< Point > Centroids
Definition: kmeans.h:71
PointsToClusters points_to_clusters_
Definition: kmeans.h:175
std::vector< SetPoints > ClustersToPoints
Definition: kmeans.h:67
float distance(const Point &x, const Point &y)
Definition: kmeans.h:130
void computeCentroids()
std::set< PointId > SetPoints
Definition: kmeans.h:62
void kMeans()
std::vector< Point > data_
The cluster centroids.
Definition: kmeans.h:172
Centroids centroids_
Definition: kmeans.h:176
unsigned int num_points_
Definition: kmeans.h:158
#define PCL_MAKE_ALIGNED_OPERATOR_NEW
Macro to signal a class requires a custom allocator.
Definition: memory.h:86
Defines functions, macros and traits for allocating and using memory.
Defines all the PCL and non-PCL macros used.
#define PCL_EXPORTS
Definition: pcl_macros.h:325