Point Cloud Library (PCL)  1.9.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 <set>
43 
44 #include <pcl/common/io.h>
45 #include <pcl/console/parse.h>
46 #include <pcl/console/print.h>
47 #include <pcl/point_cloud.h>
48 #include <pcl/point_types.h>
49 
50 #include <pcl/pcl_base.h>
51 #include <pcl/pcl_macros.h>
52 
53 namespace pcl {
54 
55 /** K-means clustering.
56  *
57  * \author Christian Potthast
58  * \ingroup ML
59  */
61 public:
62  using PointId = unsigned int; // the id of this point
63  using ClusterId = unsigned int; // the id of this cluster
64 
65  // using Point = std::vector<Coord>; // a point (a centroid)
66 
67  using SetPoints = std::set<PointId>; // set of points
68 
69  using Point = std::vector<float>;
70 
71  // ClusterId -> (PointId, PointId, PointId, .... )
72  using ClustersToPoints = std::vector<SetPoints>;
73  // PointId -> ClusterId
74  using PointsToClusters = std::vector<ClusterId>;
75  // coll of centroids
76  using Centroids = std::vector<Point>;
77 
78  /** Empty constructor. */
79  Kmeans(unsigned int num_points, unsigned int num_dimensions);
80 
81  /** This destructor destroys. */
82  ~Kmeans();
83 
84  /** This method sets the k-means cluster size.
85  *
86  * \param[in] k number of clusters
87  */
88  void
89  setClusterSize(unsigned int k)
90  {
91  num_clusters_ = k;
92  };
93 
94  /*
95  void
96  setClusterField (std::string field_name)
97  {
98  cluster_field_name_ = field_name;
99  };
100  */
101 
102  // void
103  // getClusterCentroids (PointT &out);
104 
105  // void
106  // cluster (std::vector<PointIndices> &clusters);
107 
108  void
109  kMeans();
110 
111  void
112  setInputData(std::vector<Point>& data)
113  {
114  if (num_points_ != data.size())
115  std::cout << "Data vector not the same" << std::endl;
116 
117  data_ = data;
118  }
119 
120  void
121  addDataPoint(Point& data_point)
122  {
123  if (num_dimensions_ != data_point.size())
124  std::cout << "Dimensions not the same" << std::endl;
125 
126  data_.push_back(data_point);
127  }
128 
129  // Initial partition points among available clusters
130  void
131  initialClusterPoints();
132 
133  void
134  computeCentroids();
135 
136  // distance between two points
137  float
138  distance(const Point& x, const Point& y)
139  {
140  float total = 0.0;
141  float diff;
142 
143  auto cpy = y.cbegin();
144  for (auto cpx = x.cbegin(), cpx_end = x.cend(); cpx != cpx_end; ++cpx, ++cpy) {
145  diff = *cpx - *cpy;
146  total += (diff * diff);
147  }
148  return total; // no need to take sqrt, which is monotonic
149  }
150 
151  Centroids
153  {
154  return centroids_;
155  }
156 
157 protected:
158  // Members derived from the base class
159  /*
160  using BasePCLBase::input_;
161  using BasePCLBase::indices_;
162  using BasePCLBase::initCompute;
163  using BasePCLBase::deinitCompute;
164  */
165 
166  unsigned int num_points_;
167  unsigned int num_dimensions_;
168 
169  /** The number of clusters. */
170  unsigned int num_clusters_;
171 
172  /** The cluster centroids. */
173  // std::vector
174 
175  // std::string cluster_field_name_;
176 
177  // one data point
178 
179  // all data points
180  std::vector<Point> data_;
181 
185 
186 public:
188 };
189 
190 } // namespace pcl
std::vector< ClusterId > PointsToClusters
Definition: kmeans.h:74
void addDataPoint(Point &data_point)
Definition: kmeans.h:121
float distance(const Point &x, const Point &y)
Definition: kmeans.h:138
std::vector< Point > Centroids
Definition: kmeans.h:76
ClustersToPoints clusters_to_points_
Definition: kmeans.h:182
This file defines compatibility wrappers for low level I/O functions.
Definition: convolution.h:45
std::set< PointId > SetPoints
Definition: kmeans.h:67
K-means clustering.
Definition: kmeans.h:60
#define PCL_MAKE_ALIGNED_OPERATOR_NEW
Macro to signal a class requires a custom allocator.
Definition: pcl_macros.h:345
unsigned int ClusterId
Definition: kmeans.h:63
Centroids centroids_
Definition: kmeans.h:184
void setClusterSize(unsigned int k)
This method sets the k-means cluster size.
Definition: kmeans.h:89
PointsToClusters points_to_clusters_
Definition: kmeans.h:183
unsigned int num_points_
Definition: kmeans.h:166
Centroids get_centroids()
Definition: kmeans.h:152
Defines all the PCL implemented PointT point type structures.
unsigned int num_dimensions_
Definition: kmeans.h:167
std::vector< Point > data_
The cluster centroids.
Definition: kmeans.h:180
unsigned int num_clusters_
The number of clusters.
Definition: kmeans.h:170
std::vector< SetPoints > ClustersToPoints
Definition: kmeans.h:72
unsigned int PointId
Definition: kmeans.h:62
std::vector< float > Point
Definition: kmeans.h:69
void setInputData(std::vector< Point > &data)
Definition: kmeans.h:112
#define PCL_EXPORTS
Definition: pcl_macros.h:227
Defines all the PCL and non-PCL macros used.