31 #ifndef RTABMAP_FLANN_DIST_H_ 32 #define RTABMAP_FLANN_DIST_H_ 81 template <
typename Iterator1,
typename Iterator2>
82 ResultType
operator()(Iterator1 a, Iterator2 b,
size_t size, ResultType = -1)
const 84 ResultType result = ResultType();
86 for(
size_t i = 0; i < size; ++i ) {
93 template <
typename U,
typename V>
94 inline ResultType
accum_dist(
const U& a,
const V& b,
int)
const 108 template <
typename Iterator1,
typename Iterator2>
109 ResultType
operator()(Iterator1 a, Iterator2 b,
size_t size, ResultType = -1)
const 111 ResultType result = ResultType();
122 template <
typename U,
typename V>
123 inline ResultType
accum_dist(
const U& a,
const V& b,
int)
const 149 template <
typename Iterator1,
typename Iterator2>
150 ResultType
operator()(Iterator1 a, Iterator2 b,
size_t size, ResultType worst_dist = -1)
const 152 ResultType result = ResultType();
153 ResultType diff0, diff1, diff2, diff3;
154 Iterator1 last = a + size;
155 Iterator1 lastgroup = last - 3;
158 while (a < lastgroup) {
159 diff0 = (ResultType)(a[0] - b[0]);
160 diff1 = (ResultType)(a[1] - b[1]);
161 diff2 = (ResultType)(a[2] - b[2]);
162 diff3 = (ResultType)(a[3] - b[3]);
163 result += diff0 * diff0 + diff1 * diff1 + diff2 * diff2 + diff3 * diff3;
167 if ((worst_dist>0)&&(result>worst_dist)) {
173 diff0 = (ResultType)(*a++ - *b++);
174 result += diff0 * diff0;
185 template <
typename U,
typename V>
186 inline ResultType
accum_dist(
const U& a,
const V& b,
int)
const 210 template <
typename Iterator1,
typename Iterator2>
211 ResultType
operator()(Iterator1 a, Iterator2 b,
size_t size, ResultType worst_dist = -1)
const 213 ResultType result = ResultType();
214 ResultType diff0, diff1, diff2, diff3;
215 Iterator1 last = a + size;
216 Iterator1 lastgroup = last - 3;
219 while (a < lastgroup) {
220 diff0 = (ResultType)
std::abs(a[0] - b[0]);
221 diff1 = (ResultType)
std::abs(a[1] - b[1]);
222 diff2 = (ResultType)
std::abs(a[2] - b[2]);
223 diff3 = (ResultType)
std::abs(a[3] - b[3]);
224 result += diff0 + diff1 + diff2 + diff3;
228 if ((worst_dist>0)&&(result>worst_dist)) {
234 diff0 = (ResultType)
std::abs(*a++ - *b++);
243 template <
typename U,
typename V>
244 inline ResultType
accum_dist(
const U& a,
const V& b,
int)
const 273 template <
typename Iterator1,
typename Iterator2>
274 ResultType
operator()(Iterator1 a, Iterator2 b,
size_t size, ResultType worst_dist = -1)
const 276 ResultType result = ResultType();
277 ResultType diff0, diff1, diff2, diff3;
278 Iterator1 last = a + size;
279 Iterator1 lastgroup = last - 3;
282 while (a < lastgroup) {
283 diff0 = (ResultType)
std::abs(a[0] - b[0]);
284 diff1 = (ResultType)
std::abs(a[1] - b[1]);
285 diff2 = (ResultType)
std::abs(a[2] - b[2]);
286 diff3 = (ResultType)
std::abs(a[3] - b[3]);
287 result +=
pow(diff0,order) +
pow(diff1,order) +
pow(diff2,order) +
pow(diff3,order);
291 if ((worst_dist>0)&&(result>worst_dist)) {
297 diff0 = (ResultType)
std::abs(*a++ - *b++);
298 result +=
pow(diff0,order);
306 template <
typename U,
typename V>
307 inline ResultType
accum_dist(
const U& a,
const V& b,
int)
const 309 return pow(static_cast<ResultType>(
std::abs(a-b)),order);
328 template <
typename Iterator1,
typename Iterator2>
329 ResultType
operator()(Iterator1 a, Iterator2 b,
size_t size, ResultType worst_dist = -1)
const 331 ResultType result = ResultType();
332 ResultType diff0, diff1, diff2, diff3;
333 Iterator1 last = a + size;
334 Iterator1 lastgroup = last - 3;
337 while (a < lastgroup) {
342 if (diff0>result) {result = diff0; }
343 if (diff1>result) {result = diff1; }
344 if (diff2>result) {result = diff2; }
345 if (diff3>result) {result = diff3; }
349 if ((worst_dist>0)&&(result>worst_dist)) {
356 result = (diff0>result) ? diff0 : result;
379 ResultType
operator()(
const unsigned char* a,
const unsigned char* b,
int size)
const 381 ResultType result = 0;
382 for (
int i = 0; i < size; i++) {
383 result += byteBitsLookUp(a[i] ^ b[i]);
396 static const unsigned char table[256] = {
476 template<
typename Iterator1,
typename Iterator2>
477 ResultType
operator()(Iterator1 a, Iterator2 b,
size_t size, ResultType = -1)
const 479 ResultType result = 0;
481 #if ANDROID && HAVE_NEON 482 static uint64_t features = android_getCpuFeatures();
483 if ((features& ANDROID_CPU_ARM_FEATURE_NEON)) {
484 for (
size_t i = 0; i < size; i += 16) {
485 uint8x16_t A_vec = vld1q_u8 (a + i);
486 uint8x16_t B_vec = vld1q_u8 (b + i);
488 uint8x16_t AxorB = veorq_u8 (A_vec, B_vec);
490 uint8x16_t bitsSet += vcntq_u8 (AxorB);
492 uint16x8_t bitSet8 = vpaddlq_u8 (bitsSet);
493 uint32x4_t bitSet4 = vpaddlq_u16 (bitSet8);
495 uint64x2_t bitSet2 = vpaddlq_u32 (bitSet4);
496 result += vgetq_lane_u64 (bitSet2,0);
497 result += vgetq_lane_u64 (bitSet2,1);
503 typedef unsigned long long pop_t;
504 const size_t modulo = size %
sizeof(pop_t);
505 const pop_t* a2 =
reinterpret_cast<const pop_t*
> (a);
506 const pop_t* b2 =
reinterpret_cast<const pop_t*
> (b);
507 const pop_t* a2_end = a2 + (size /
sizeof(pop_t));
509 for (; a2 != a2_end; ++a2, ++b2) result += __builtin_popcountll((*a2) ^ (*b2));
514 pop_t a_final = 0, b_final = 0;
515 memcpy(&a_final, a2, modulo);
516 memcpy(&b_final, b2, modulo);
517 result += __builtin_popcountll(a_final ^ b_final);
521 result = lut(reinterpret_cast<const unsigned char*> (a),
522 reinterpret_cast<const unsigned char*> (b), size *
sizeof(pop_t));
538 n -= ((n >> 1) & 0x55555555);
539 n = (n & 0x33333333) + ((n >> 2) & 0x33333333);
540 return (((n + (n >> 4))& 0xF0F0F0F)* 0x1010101) >> 24;
545 n -= ((n >> 1) & 0x5555555555555555LL);
546 n = (n & 0x3333333333333333LL) + ((n >> 2) & 0x3333333333333333LL);
547 return (((n + (n >> 4))& 0x0f0f0f0f0f0f0f0fLL)* 0x0101010101010101LL) >> 56;
550 template <
typename Iterator1,
typename Iterator2>
551 ResultType
operator()(Iterator1 a, Iterator2 b,
size_t size, ResultType = 0)
const 553 #ifdef FLANN_PLATFORM_64_BIT 556 ResultType result = 0;
557 size /= (
sizeof(
uint64_t)/
sizeof(
unsigned char));
558 for(
size_t i = 0; i < size; ++i ) {
559 result += popcnt64(*pa ^ *pb);
566 ResultType result = 0;
567 size /= (
sizeof(
uint32_t)/
sizeof(
unsigned char));
568 for(
size_t i = 0; i < size; ++i ) {
569 result += popcnt32(*pa ^ *pb);
593 template <
typename Iterator1,
typename Iterator2>
594 ResultType
operator()(Iterator1 a, Iterator2 b,
size_t size, ResultType worst_dist = -1)
const 596 ResultType result = ResultType();
597 ResultType min0, min1, min2,
min3;
598 Iterator1 last = a + size;
599 Iterator1 lastgroup = last - 3;
602 while (a < lastgroup) {
603 min0 = (ResultType)(a[0] < b[0] ? a[0] : b[0]);
604 min1 = (ResultType)(a[1] < b[1] ? a[1] : b[1]);
605 min2 = (ResultType)(a[2] < b[2] ? a[2] : b[2]);
606 min3 = (ResultType)(a[3] < b[3] ? a[3] : b[3]);
607 result += min0 + min1 + min2 +
min3;
610 if ((worst_dist>0)&&(result>worst_dist)) {
616 min0 = (ResultType)(*a < *b ? *a : *b);
627 template <
typename U,
typename V>
628 inline ResultType
accum_dist(
const U& a,
const V& b,
int)
const 647 template <
typename Iterator1,
typename Iterator2>
648 ResultType
operator()(Iterator1 a, Iterator2 b,
size_t size, ResultType = -1)
const 650 ResultType result = ResultType();
651 ResultType diff0, diff1, diff2, diff3;
652 Iterator1 last = a + size;
653 Iterator1 lastgroup = last - 3;
656 while (a < lastgroup) {
657 diff0 =
sqrt(static_cast<ResultType>(a[0])) -
sqrt(static_cast<ResultType>(b[0]));
658 diff1 =
sqrt(static_cast<ResultType>(a[1])) -
sqrt(static_cast<ResultType>(b[1]));
659 diff2 =
sqrt(static_cast<ResultType>(a[2])) -
sqrt(static_cast<ResultType>(b[2]));
660 diff3 =
sqrt(static_cast<ResultType>(a[3])) -
sqrt(static_cast<ResultType>(b[3]));
661 result += diff0 * diff0 + diff1 * diff1 + diff2 * diff2 + diff3 * diff3;
666 diff0 =
sqrt(static_cast<ResultType>(*a++)) -
sqrt(static_cast<ResultType>(*b++));
667 result += diff0 * diff0;
675 template <
typename U,
typename V>
676 inline ResultType
accum_dist(
const U& a,
const V& b,
int)
const 678 ResultType dist =
sqrt(static_cast<ResultType>(a)) -
sqrt(static_cast<ResultType>(b));
695 template <
typename Iterator1,
typename Iterator2>
696 ResultType
operator()(Iterator1 a, Iterator2 b,
size_t size, ResultType worst_dist = -1)
const 698 ResultType result = ResultType();
699 ResultType sum, diff;
700 Iterator1 last = a + size;
703 sum = (ResultType)(*a + *b);
705 diff = (ResultType)(*a - *b);
706 result += diff*diff/sum;
711 if ((worst_dist>0)&&(result>worst_dist)) {
721 template <
typename U,
typename V>
722 inline ResultType
accum_dist(
const U& a,
const V& b,
int)
const 724 ResultType result = ResultType();
725 ResultType sum, diff;
727 sum = (ResultType)(a+b);
729 diff = (ResultType)(a-b);
730 result = diff*diff/sum;
748 template <
typename Iterator1,
typename Iterator2>
749 ResultType
operator()(Iterator1 a, Iterator2 b,
size_t size, ResultType worst_dist = -1)
const 751 ResultType result = ResultType();
752 Iterator1 last = a + size;
755 if ( *a != 0 && *b != 0 ) {
756 ResultType ratio = (ResultType)(*a / *b);
758 result += *a *
log(ratio);
764 if ((worst_dist>0)&&(result>worst_dist)) {
774 template <
typename U,
typename V>
775 inline ResultType
accum_dist(
const U& a,
const V& b,
int)
const 777 ResultType result = ResultType();
778 if( a != 0 && b != 0 ) {
779 ResultType ratio = (ResultType)(a / b);
781 result = a *
log(ratio);
790 #endif //FLANN_DIST_H_ GLM_FUNC_DECL genType log(genType const &x)
ResultType accum_dist(const U &a, const V &b, int) const
Accumulator< T >::Type ResultType
ResultType operator()(Iterator1 a, Iterator2 b, size_t size, ResultType worst_dist=-1) const
Accumulator< T >::Type ResultType
ResultType accum_dist(const U &a, const V &b, int) const
ResultType accum_dist(const U &a, const V &b, int) const
ResultType operator()(Iterator1 a, Iterator2 b, size_t size, ResultType worst_dist=-1) const
Accumulator< T >::Type ResultType
GLM_FUNC_DECL vecType< T, P > sqrt(vecType< T, P > const &x)
ResultType operator()(Iterator1 a, Iterator2 b, size_t size, ResultType=-1) const
ResultType operator()(Iterator1 a, Iterator2 b, size_t size, ResultType worst_dist=-1) const
Accumulator< T >::Type ResultType
bool is_vector_space_distance
ResultType operator()(Iterator1 a, Iterator2 b, size_t size, ResultType=0) const
unsigned char ElementType
Accumulator< T >::Type ResultType
ResultType accum_dist(const U &a, const V &b, int) const
Accumulator< T >::Type ResultType
ResultType operator()(Iterator1 a, Iterator2 b, size_t size, ResultType=-1) const
ResultType operator()(Iterator1 a, Iterator2 b, size_t size, ResultType worst_dist=-1) const
Accumulator< T >::Type ResultType
ResultType operator()(Iterator1 a, Iterator2 b, size_t size, ResultType=-1) const
GLM_FUNC_DECL genType abs(genType const &x)
ResultType operator()(Iterator1 a, Iterator2 b, size_t size, ResultType worst_dist=-1) const
ResultType operator()(Iterator1 a, Iterator2 b, size_t size, ResultType worst_dist=-1) const
ResultType operator()(Iterator1 a, Iterator2 b, size_t size, ResultType=-1) const
ResultType accum_dist(const U &a, const V &b, int) const
static unsigned char byteBitsLookUp(unsigned char b)
given a byte, count the bits using a look up table
ResultType accum_dist(const U &a, const V &b, int) const
GLM_FUNC_DECL genType pow(genType const &base, genType const &exponent)
MinkowskiDistance(int order_)
ResultType accum_dist(const U &a, const V &b, int) const
ResultType accum_dist(const U &a, const V &b, int) const
Accumulator< T >::Type ResultType
Accumulator< T >::Type ResultType
ResultType operator()(const unsigned char *a, const unsigned char *b, int size) const
unsigned int popcnt32(uint32_t n) const
Accumulator< T >::Type ResultType
ResultType accum_dist(const U &a, const V &b, int) const
unsigned int popcnt64(uint64_t n) const
double min3(const double &a, const double &b, const double &c)
ResultType operator()(Iterator1 a, Iterator2 b, size_t size, ResultType worst_dist=-1) const