21 template <
typename SCALAR>
23 const bool use_kmeansplusplus_method,
const size_t nPoints,
const size_t k,
24 const size_t dims,
const SCALAR* points,
const size_t attempts,
25 SCALAR* out_center,
int* out_assignments);
29 template <
class LIST_OF_VECTORS1,
class LIST_OF_VECTORS2>
31 const bool use_kmeansplusplus_method,
const size_t k,
32 const LIST_OF_VECTORS1& points, std::vector<int>& assignments,
33 LIST_OF_VECTORS2* out_centers,
const size_t attempts)
38 const size_t N = points.size();
39 assignments.resize(N);
40 if (out_centers) out_centers->clear();
44 const auto it_first = points.begin();
45 const auto it_end = points.end();
46 using TInnerVector =
typename LIST_OF_VECTORS1::value_type;
47 using TInnerVectorCenters =
typename LIST_OF_VECTORS2::value_type;
48 std::vector<typename TInnerVector::value_type> raw_vals;
49 typename TInnerVector::value_type* trg_ptr =
nullptr;
50 for (
typename LIST_OF_VECTORS1::const_iterator it = it_first; it != it_end;
56 ASSERTMSG_(dims > 0,
"Dimensionality of points can't be zero.");
57 raw_vals.resize(N * dims);
58 trg_ptr = &raw_vals[0];
63 size_t(dims) ==
size_t(it->size()),
64 "All points must have the same dimensionality.");
69 dims *
sizeof(
typename TInnerVector::value_type));
73 std::vector<typename TInnerVectorCenters::value_type> centers(dims * k);
75 false, N, k, points.begin()->size(), &raw_vals[0], attempts,
76 ¢ers[0], &assignments[0]);
80 const typename TInnerVectorCenters::value_type* center_ptr =
82 for (
size_t i = 0; i < k; i++)
84 TInnerVectorCenters c;
86 for (
size_t j = 0; j < dims; j++) c[j] = *center_ptr++;
87 out_centers->push_back(c);
122 template <
class LIST_OF_VECTORS1,
class LIST_OF_VECTORS2>
124 const size_t k,
const LIST_OF_VECTORS1& points,
125 std::vector<int>& assignments, LIST_OF_VECTORS2* out_centers =
nullptr,
126 const size_t attempts = 3)
129 false , k, points, assignments, out_centers,
157 template <
class LIST_OF_VECTORS1,
class LIST_OF_VECTORS2 = LIST_OF_VECTORS1>
159 const size_t k,
const LIST_OF_VECTORS1& points,
160 std::vector<int>& assignments, LIST_OF_VECTORS2* out_centers =
nullptr,
161 const size_t attempts = 3)
164 true , k, points, assignments, out_centers,
double kmeans(const size_t k, const LIST_OF_VECTORS1 &points, std::vector< int > &assignments, LIST_OF_VECTORS2 *out_centers=nullptr, const size_t attempts=3)
k-means algorithm to cluster a list of N points of arbitrary dimensionality into exactly K clusters...
#define ASSERT_(f)
Defines an assertion mechanism.
double kmeanspp(const size_t k, const LIST_OF_VECTORS1 &points, std::vector< int > &assignments, LIST_OF_VECTORS2 *out_centers=nullptr, const size_t attempts=3)
k-means++ algorithm to cluster a list of N points of arbitrary dimensionality into exactly K clusters...
#define ASSERTMSG_(f, __ERROR_MSG)
Defines an assertion mechanism.
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
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)
double 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)
void memcpy(void *dest, size_t destSize, const void *src, size_t copyCount) noexcept
An OS and compiler independent version of "memcpy".
#define MRPT_UNUSED_PARAM(a)
Determines whether this is an X86 or AMD64 platform.