9 #ifndef mrpt_math_kmeans_H 10 #define mrpt_math_kmeans_H 21 template <
typename SCALAR>
23 const bool use_kmeansplusplus_method,
28 const size_t attempts,
34 template <
class LIST_OF_VECTORS1,
class LIST_OF_VECTORS2>
36 const bool use_kmeansplusplus_method,
38 const LIST_OF_VECTORS1 &
points,
39 std::vector<int> &assignments,
40 LIST_OF_VECTORS2 *out_centers,
47 const size_t N =
points.size();
48 assignments.resize(N);
49 if (out_centers) out_centers->clear();
56 typedef typename LIST_OF_VECTORS1::value_type TInnerVector;
57 typedef typename LIST_OF_VECTORS2::value_type TInnerVectorCenters;
58 std::vector<typename TInnerVector::value_type> raw_vals;
59 typename TInnerVector::value_type *trg_ptr=NULL;
65 ASSERTMSG_(dims>0,
"Dimensionality of points can't be zero.")
66 raw_vals.resize(N*dims);
67 trg_ptr = &raw_vals[0];
71 ASSERTMSG_(
size_t(dims)==
size_t(it->size()),
"All points must have the same dimensionality.")
74 ::memcpy(trg_ptr, &(*it)[0], dims*
sizeof(
typename TInnerVector::value_type));
78 std::vector<typename TInnerVectorCenters::value_type> centers(dims*k);
83 const typename TInnerVectorCenters::value_type *center_ptr = ¢ers[0];
84 for (
size_t i=0;i<k;i++)
86 TInnerVectorCenters
c;
88 for (
size_t j=0;j<dims;j++)
c[j]= *center_ptr++;
89 out_centers->push_back(
c);
117 template <
class LIST_OF_VECTORS1,
class LIST_OF_VECTORS2>
120 const LIST_OF_VECTORS1 &
points,
121 std::vector<int> &assignments,
122 LIST_OF_VECTORS2 *out_centers = NULL,
123 const size_t attempts = 3
145 template <
class LIST_OF_VECTORS1,
class LIST_OF_VECTORS2>
148 const LIST_OF_VECTORS1 &
points,
149 std::vector<int> &assignments,
150 LIST_OF_VECTORS2 *out_centers = NULL,
151 const size_t attempts = 3
void BASE_IMPEXP memcpy(void *dest, size_t destSize, const void *src, size_t copyCount) MRPT_NO_THROWS
An OS and compiler independent version of "memcpy".
const Scalar * const_iterator
GLsizei const GLfloat * points
double kmeanspp(const size_t k, const LIST_OF_VECTORS1 &points, std::vector< int > &assignments, LIST_OF_VECTORS2 *out_centers=NULL, const size_t attempts=3)
k-means++ algorithm to cluster a list of N points of arbitrary dimensionality into exactly K clusters...
#define MRPT_UNUSED_PARAM(a)
Can be used to avoid "not used parameters" warnings from the compiler.
double kmeans(const size_t k, const LIST_OF_VECTORS1 &points, std::vector< int > &assignments, LIST_OF_VECTORS2 *out_centers=NULL, const size_t attempts=3)
k-means algorithm to cluster a list of N points of arbitrary dimensionality into exactly K clusters...
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
double BASE_IMPEXP internal_kmeans(const bool use_kmeansplusplus_method, const size_t nPoints, const size_t k, const size_t dims, const SCALAR *points, const size_t attempts, SCALAR *out_center, int *out_assignments)
double stub_kmeans(const bool use_kmeansplusplus_method, const size_t k, const LIST_OF_VECTORS1 &points, std::vector< int > &assignments, LIST_OF_VECTORS2 *out_centers, const size_t attempts)
#define ASSERTMSG_(f, __ERROR_MSG)