Point Cloud Library (PCL)
1.12.1
Toggle main menu visibility
Loading...
Searching...
No Matches
pcl
ml
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
*/
55
class
PCL_EXPORTS
Kmeans
{
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 destructor destroys. */
77
~Kmeans
();
78
79
/** This method sets the k-means cluster size.
80
*
81
* \param[in] k number of clusters
82
*/
83
void
84
setClusterSize
(
unsigned
int
k)
85
{
86
num_clusters_
= k;
87
};
88
89
/*
90
void
91
setClusterField (std::string field_name)
92
{
93
cluster_field_name_ = field_name;
94
};
95
*/
96
97
// void
98
// getClusterCentroids (PointT &out);
99
100
// void
101
// cluster (std::vector<PointIndices> &clusters);
102
103
void
104
kMeans
();
105
106
void
107
setInputData
(std::vector<Point>& data)
108
{
109
if
(
num_points_
!= data.size())
110
std::cout <<
"Data vector not the same"
<< std::endl;
111
112
data_
= data;
113
}
114
115
void
116
addDataPoint
(
Point
& data_point)
117
{
118
if
(
num_dimensions_
!= data_point.size())
119
std::cout <<
"Dimensions not the same"
<< std::endl;
120
121
data_
.push_back(data_point);
122
}
123
124
// Initial partition points among available clusters
125
void
126
initialClusterPoints
();
127
128
void
129
computeCentroids
();
130
131
// distance between two points
132
float
133
distance
(
const
Point
& x,
const
Point
& y)
134
{
135
float
total = 0.0;
136
float
diff;
137
138
auto
cpy = y.cbegin();
139
for
(
auto
cpx = x.cbegin(), cpx_end = x.cend(); cpx != cpx_end; ++cpx, ++cpy) {
140
diff = *cpx - *cpy;
141
total += (diff * diff);
142
}
143
return
total;
// no need to take sqrt, which is monotonic
144
}
145
146
Centroids
147
get_centroids
()
148
{
149
return
centroids_
;
150
}
151
152
protected
:
153
// Members derived from the base class
154
/*
155
using BasePCLBase::input_;
156
using BasePCLBase::indices_;
157
using BasePCLBase::initCompute;
158
using BasePCLBase::deinitCompute;
159
*/
160
161
unsigned
int
num_points_
;
162
unsigned
int
num_dimensions_
;
163
164
/** The number of clusters. */
165
unsigned
int
num_clusters_
;
166
167
/** The cluster centroids. */
168
// std::vector
169
170
// std::string cluster_field_name_;
171
172
// one data point
173
174
// all data points
175
std::vector<Point>
data_
;
176
177
ClustersToPoints
clusters_to_points_
;
178
PointsToClusters
points_to_clusters_
;
179
Centroids
centroids_
;
180
181
public
:
182
PCL_MAKE_ALIGNED_OPERATOR_NEW
183
};
184
185
}
// namespace pcl
pcl::Kmeans::ClusterId
unsigned int ClusterId
Definition
kmeans.h:58
pcl::Kmeans::clusters_to_points_
ClustersToPoints clusters_to_points_
Definition
kmeans.h:177
pcl::Kmeans::num_clusters_
unsigned int num_clusters_
The number of clusters.
Definition
kmeans.h:165
pcl::Kmeans::setInputData
void setInputData(std::vector< Point > &data)
Definition
kmeans.h:107
pcl::Kmeans::num_dimensions_
unsigned int num_dimensions_
Definition
kmeans.h:162
pcl::Kmeans::get_centroids
Centroids get_centroids()
Definition
kmeans.h:147
pcl::Kmeans::Point
std::vector< float > Point
Definition
kmeans.h:64
pcl::Kmeans::PointId
unsigned int PointId
Definition
kmeans.h:57
pcl::Kmeans::initialClusterPoints
void initialClusterPoints()
pcl::Kmeans::addDataPoint
void addDataPoint(Point &data_point)
Definition
kmeans.h:116
pcl::Kmeans::Kmeans
Kmeans(unsigned int num_points, unsigned int num_dimensions)
Empty constructor.
Definition
kmeans.hpp:52
pcl::Kmeans::setClusterSize
void setClusterSize(unsigned int k)
This method sets the k-means cluster size.
Definition
kmeans.h:84
pcl::Kmeans::PointsToClusters
std::vector< ClusterId > PointsToClusters
Definition
kmeans.h:69
pcl::Kmeans::Centroids
std::vector< Point > Centroids
Definition
kmeans.h:71
pcl::Kmeans::points_to_clusters_
PointsToClusters points_to_clusters_
Definition
kmeans.h:178
pcl::Kmeans::ClustersToPoints
std::vector< SetPoints > ClustersToPoints
Definition
kmeans.h:67
pcl::Kmeans::distance
float distance(const Point &x, const Point &y)
Definition
kmeans.h:133
pcl::Kmeans::computeCentroids
void computeCentroids()
pcl::Kmeans::SetPoints
std::set< PointId > SetPoints
Definition
kmeans.h:62
pcl::Kmeans::kMeans
void kMeans()
pcl::Kmeans::data_
std::vector< Point > data_
The cluster centroids.
Definition
kmeans.h:175
pcl::Kmeans::centroids_
Centroids centroids_
Definition
kmeans.h:179
pcl::Kmeans::num_points_
unsigned int num_points_
Definition
kmeans.h:161
PCL_MAKE_ALIGNED_OPERATOR_NEW
#define PCL_MAKE_ALIGNED_OPERATOR_NEW
Macro to signal a class requires a custom allocator.
Definition
memory.h:63
memory.h
Defines functions, macros and traits for allocating and using memory.
pcl
Definition
convolution.h:46
pcl_macros.h
Defines all the PCL and non-PCL macros used.