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