8 #ifndef RTABMAP_CENTER_CHOOSER_H_ 9 #define RTABMAP_CENTER_CHOOSER_H_ 16 template <
typename Distance,
typename ElementType>
20 ResultType
operator()( ResultType dist ) {
return dist*dist; }
24 template <
typename ElementType>
28 ResultType
operator()( ResultType dist ) {
return dist; }
31 template <
typename ElementType>
35 ResultType
operator()( ResultType dist ) {
return dist; }
38 template <
typename ElementType>
42 ResultType
operator()( ResultType dist ) {
return dist; }
46 template <
typename ElementType>
50 ResultType
operator()( ResultType dist ) {
return dist; }
54 template <
typename ElementType>
58 ResultType
operator()( ResultType dist ) {
return dist; }
62 template <
typename Distance>
65 typedef typename Distance::ElementType ElementType;
73 template <
typename Distance>
80 CenterChooser(
const Distance&
distance,
const std::vector<ElementType*>& points) : distance_(distance), points_(points) {};
95 virtual void operator()(
int k,
int* indices,
int indices_length,
int* centers,
int& centers_length) = 0;
104 template <
typename Distance>
117 void operator()(
int k,
int* indices,
int indices_length,
int* centers,
int& centers_length)
122 for (index=0; index<k; ++index) {
123 bool duplicate =
true;
129 centers_length = index;
133 centers[index] = indices[rnd];
135 for (
int j=0; j<index; ++j) {
136 DistanceType sq = distance_(points_[centers[index]], points_[centers[j]], cols_);
144 centers_length = index;
153 template <
typename Distance>
167 void operator()(
int k,
int* indices,
int indices_length,
int* centers,
int& centers_length)
169 int n = indices_length;
172 assert(rnd >=0 && rnd < n);
174 centers[0] = indices[rnd];
177 for (index=1; index<k; ++index) {
180 DistanceType best_val = 0;
181 for (
int j=0; j<n; ++j) {
182 DistanceType
dist = distance_(points_[centers[0]],points_[indices[j]],cols_);
183 for (
int i=1; i<index; ++i) {
184 DistanceType tmp_dist = distance_(points_[centers[i]],points_[indices[j]],cols_);
194 if (best_index!=-1) {
195 centers[index] = indices[best_index];
201 centers_length = index;
210 template <
typename Distance>
224 void operator()(
int k,
int* indices,
int indices_length,
int* centers,
int& centers_length)
226 int n = indices_length;
228 double currentPot = 0;
229 DistanceType* closestDistSq =
new DistanceType[n];
233 assert(index >=0 && index < n);
234 centers[0] = indices[index];
238 for (
int i = 0; i < n; i++) {
239 closestDistSq[i] = distance_(points_[indices[i]], points_[indices[index]], cols_);
240 closestDistSq[i] = ensureSquareDistance<Distance>( closestDistSq[i] );
241 currentPot += closestDistSq[i];
245 const int numLocalTries = 1;
249 for (centerCount = 1; centerCount < k; centerCount++) {
252 double bestNewPot = -1;
253 int bestNewIndex = 0;
254 for (
int localTrial = 0; localTrial < numLocalTries; localTrial++) {
259 for (index = 0; index < n-1; index++) {
260 if (randVal <= closestDistSq[index])
break;
261 else randVal -= closestDistSq[index];
266 for (
int i = 0; i < n; i++) {
267 DistanceType
dist = distance_(points_[indices[i]], points_[indices[index]], cols_);
268 newPot +=
std::min( ensureSquareDistance<Distance>(dist), closestDistSq[i] );
272 if ((bestNewPot < 0)||(newPot < bestNewPot)) {
274 bestNewIndex = index;
279 centers[centerCount] = indices[bestNewIndex];
280 currentPot = bestNewPot;
281 for (
int i = 0; i < n; i++) {
282 DistanceType
dist = distance_(points_[indices[i]], points_[indices[bestNewIndex]], cols_);
283 closestDistSq[i] =
std::min( ensureSquareDistance<Distance>(dist), closestDistSq[i] );
287 centers_length = centerCount;
289 delete[] closestDistSq;
306 template <
typename Distance>
320 void operator()(
int k,
int* indices,
int indices_length,
int* centers,
int& centers_length)
322 const float kSpeedUpFactor = 1.3f;
324 int n = indices_length;
326 DistanceType* closestDistSq =
new DistanceType[n];
330 assert(index >=0 && index < n);
331 centers[0] = indices[index];
333 for (
int i = 0; i < n; i++) {
334 closestDistSq[i] = distance_(points_[indices[i]], points_[indices[index]], cols_);
340 for (centerCount = 1; centerCount < k; centerCount++) {
343 double bestNewPot = -1;
344 int bestNewIndex = 0;
345 DistanceType furthest = 0;
346 for (index = 0; index < n; index++) {
349 if( closestDistSq[index] > kSpeedUpFactor * (
float)furthest ) {
353 for (
int i = 0; i < n; i++) {
354 newPot +=
std::min( distance_(points_[indices[i]], points_[indices[index]], cols_)
355 , closestDistSq[i] );
359 if ((bestNewPot < 0)||(newPot <= bestNewPot)) {
361 bestNewIndex = index;
362 furthest = closestDistSq[index];
368 centers[centerCount] = indices[bestNewIndex];
369 for (
int i = 0; i < n; i++) {
370 closestDistSq[i] =
std::min( distance_(points_[indices[i]], points_[indices[bestNewIndex]], cols_)
371 , closestDistSq[i] );
375 centers_length = centerCount;
377 delete[] closestDistSq;
Distance::ElementType ElementType
Accumulator< T >::Type ResultType
ResultType operator()(ResultType dist)
GLM_FUNC_DECL genType min(genType const &x, genType const &y)
Distance::ResultType ResultType
GLM_FUNC_DECL genType e()
RandomCenterChooser(const Distance &distance, const std::vector< ElementType *> &points)
GLM_FUNC_DECL genType::value_type distance(genType const &p0, genType const &p1)
void operator()(int k, int *indices, int indices_length, int *centers, int ¢ers_length)
Accumulator< T >::Type ResultType
Distance::ElementType ElementType
ResultType operator()(ResultType dist)
int rand_int(int high=RAND_MAX, int low=0)
Accumulator< T >::Type ResultType
Distance::ResultType DistanceType
Distance::ResultType ensureSquareDistance(typename Distance::ResultType dist)
void operator()(int k, int *indices, int indices_length, int *centers, int ¢ers_length)
Distance::ResultType DistanceType
CenterChooser(const Distance &distance, const std::vector< ElementType *> &points)
GroupWiseCenterChooser(const Distance &distance, const std::vector< ElementType *> &points)
ChiSquareDistance< ElementType >::ResultType ResultType
Distance::ElementType ElementType
const std::vector< ElementType * > & points_
Distance::ElementType ElementType
Distance::ResultType DistanceType
void setDataSize(size_t cols)
L2_3D< ElementType >::ResultType ResultType
Distance::ResultType DistanceType
ResultType operator()(ResultType dist)
ResultType operator()(ResultType dist)
void operator()(int k, int *indices, int indices_length, int *centers, int ¢ers_length)
L2< ElementType >::ResultType ResultType
Distance::ResultType DistanceType
ResultType operator()(ResultType dist)
void operator()(int k, int *indices, int indices_length, int *centers, int ¢ers_length)
L2_Simple< ElementType >::ResultType ResultType
double rand_double(double high=1.0, double low=0)
Distance::ElementType ElementType
Accumulator< T >::Type ResultType
ResultType operator()(ResultType dist)
HellingerDistance< ElementType >::ResultType ResultType
Accumulator< T >::Type ResultType
KMeansppCenterChooser(const Distance &distance, const std::vector< ElementType *> &points)
GonzalesCenterChooser(const Distance &distance, const std::vector< ElementType *> &points)