31
#ifndef OPENCV_FLANN_DIST_H_
32
#define OPENCV_FLANN_DIST_H_
40
typedef
unsigned
__int32 uint32_t;
41
typedef
unsigned
__int64 uint64_t;
48
#if defined _WIN32 && (defined(_M_ARM) || defined(_M_ARM64))
52
#if defined(__ARM_NEON__) && !defined(__CUDACC__)
53
# include "arm_neon.h"
60
inline
T
abs(T x) {
return
(x<0) ? -x : x; }
66
inline
float
abs<float>(
float
x) {
return
fabsf(x); }
69
inline
double
abs<double>(
double
x) {
return
fabs(x); }
72
template<
typename
TargetType>
73
inline
TargetType round(
float
x) {
return
static_cast<TargetType
>(x); }
76
inline
unsigned
int
round<unsigned int>(
float
x) {
return
static_cast<
unsigned
int
>(x + 0.5f); }
79
inline
unsigned
short
round<unsigned short>(
float
x) {
return
static_cast<
unsigned
short
>(x + 0.5f); }
82
inline
unsigned
char
round<unsigned char>(
float
x) {
return
static_cast<
unsigned
char
>(x + 0.5f); }
85
inline
long
long
round<long long>(
float
x) {
return
static_cast<
long
long
>(x + 0.5f); }
88
inline
long
round<long>(
float
x) {
return
static_cast<
long
>(x + 0.5f); }
91
inline
int
round<int>(
float
x) {
return
static_cast<
int
>(x + 0.5f) - (x<0); }
94
inline
short
round<short>(
float
x) {
return
static_cast<
short
>(x + 0.5f) - (x<0); }
97
inline
char
round<char>(
float
x) {
return
static_cast<
char
>(x + 0.5f) - (x<0); }
100
template<
typename
TargetType>
101
inline
TargetType round(
double
x) {
return
static_cast<TargetType
>(x); }
104
inline
unsigned
int
round<unsigned int>(
double
x) {
return
static_cast<
unsigned
int
>(x + 0.5); }
107
inline
unsigned
short
round<unsigned short>(
double
x) {
return
static_cast<
unsigned
short
>(x + 0.5); }
110
inline
unsigned
char
round<unsigned char>(
double
x) {
return
static_cast<
unsigned
char
>(x + 0.5); }
113
inline
long
long
round<long long>(
double
x) {
return
static_cast<
long
long
>(x + 0.5); }
116
inline
long
round<long>(
double
x) {
return
static_cast<
long
>(x + 0.5); }
119
inline
int
round<int>(
double
x) {
return
static_cast<
int
>(x + 0.5) - (x<0); }
122
inline
short
round<short>(
double
x) {
return
static_cast<
short
>(x + 0.5) - (x<0); }
125
inline
char
round<char>(
double
x) {
return
static_cast<
char
>(x + 0.5) - (x<0); }
129
struct
Accumulator {
typedef
T Type; };
131
struct
Accumulator<unsigned char> {
typedef
float
Type; };
133
struct
Accumulator<unsigned short> {
typedef
float
Type; };
135
struct
Accumulator<unsigned int> {
typedef
float
Type; };
137
struct
Accumulator<char> {
typedef
float
Type; };
139
struct
Accumulator<short> {
typedef
float
Type; };
141
struct
Accumulator<int> {
typedef
float
Type; };
149
static
const
bool
val =
true;
155
static
const
bool
val =
false;
166
template
<
typename
T>
180
const
ZeroIterator<T>& operator ++()
185
ZeroIterator<T> operator ++(
int)
190
ZeroIterator<T>& operator+=(
int)
208
typedef
True is_kdtree_distance;
209
typedef
True is_vector_space_distance;
211
typedef
T ElementType;
212
typedef
typename
Accumulator<T>::Type ResultType;
213
typedef
ResultType CentersType;
215
template
<
typename
Iterator1,
typename
Iterator2>
216
ResultType operator()(Iterator1 a, Iterator2 b,
size_t
size, ResultType
= -1)
const
218
ResultType result = ResultType();
220
for(
size_t
i = 0; i < size; ++i ) {
221
diff = (ResultType)(*a++ - *b++);
227
template
<
typename
U,
typename
V>
228
inline
ResultType accum_dist(
const
U& a,
const
V& b,
int)
const
242
typedef
True is_kdtree_distance;
243
typedef
True is_vector_space_distance;
245
typedef
T ElementType;
246
typedef
typename
Accumulator<T>::Type ResultType;
247
typedef
ResultType CentersType;
258
template
<
typename
Iterator1,
typename
Iterator2>
259
ResultType operator()(Iterator1 a, Iterator2 b,
size_t
size, ResultType worst_dist = -1)
const
261
ResultType result = ResultType();
262
ResultType diff0, diff1, diff2, diff3;
263
Iterator1 last = a + size;
264
Iterator1 lastgroup = last - 3;
267
while
(a < lastgroup) {
268
diff0 = (ResultType)(a[0] - b[0]);
269
diff1 = (ResultType)(a[1] - b[1]);
270
diff2 = (ResultType)(a[2] - b[2]);
271
diff3 = (ResultType)(a[3] - b[3]);
272
result += diff0 * diff0 + diff1 * diff1 + diff2 * diff2 + diff3 * diff3;
276
if
((worst_dist>0)&&(result>worst_dist)) {
282
diff0 = (ResultType)(*a++ - *b++);
283
result += diff0 * diff0;
294
template
<
typename
U,
typename
V>
295
inline
ResultType accum_dist(
const
U& a,
const
V& b,
int)
const
308
typedef
True is_kdtree_distance;
309
typedef
True is_vector_space_distance;
311
typedef
T ElementType;
312
typedef
typename
Accumulator<T>::Type ResultType;
313
typedef
ResultType CentersType;
321
template
<
typename
Iterator1,
typename
Iterator2>
322
ResultType operator()(Iterator1 a, Iterator2 b,
size_t
size, ResultType worst_dist = -1)
const
324
ResultType result = ResultType();
325
ResultType diff0, diff1, diff2, diff3;
326
Iterator1 last = a + size;
327
Iterator1 lastgroup = last - 3;
330
while
(a < lastgroup) {
331
diff0 = (ResultType)
abs(a[0] - b[0]);
332
diff1 = (ResultType)
abs(a[1] - b[1]);
333
diff2 = (ResultType)
abs(a[2] - b[2]);
334
diff3 = (ResultType)
abs(a[3] - b[3]);
335
result += diff0 + diff1 + diff2 + diff3;
339
if
((worst_dist>0)&&(result>worst_dist)) {
345
diff0 = (ResultType)
abs(*a++ - *b++);
354
template
<
typename
U,
typename
V>
355
inline
ResultType accum_dist(
const
U& a,
const
V& b,
int)
const
364
struct
MinkowskiDistance
366
typedef
True is_kdtree_distance;
367
typedef
True is_vector_space_distance;
369
typedef
T ElementType;
370
typedef
typename
Accumulator<T>::Type ResultType;
371
typedef
ResultType CentersType;
375
MinkowskiDistance(
int
order_) : order(order_) {}
386
template
<
typename
Iterator1,
typename
Iterator2>
387
ResultType operator()(Iterator1 a, Iterator2 b,
size_t
size, ResultType worst_dist = -1)
const
389
ResultType result = ResultType();
390
ResultType diff0, diff1, diff2, diff3;
391
Iterator1 last = a + size;
392
Iterator1 lastgroup = last - 3;
395
while
(a < lastgroup) {
396
diff0 = (ResultType)
abs(a[0] - b[0]);
397
diff1 = (ResultType)
abs(a[1] - b[1]);
398
diff2 = (ResultType)
abs(a[2] - b[2]);
399
diff3 = (ResultType)
abs(a[3] - b[3]);
400
result +=
pow(diff0,order) +
pow(diff1,order) +
pow(diff2,order) +
pow(diff3,order);
404
if
((worst_dist>0)&&(result>worst_dist)) {
410
diff0 = (ResultType)
abs(*a++ - *b++);
411
result +=
pow(diff0,order);
419
template
<
typename
U,
typename
V>
420
inline
ResultType accum_dist(
const
U& a,
const
V& b,
int)
const
422
return
pow(
static_cast<ResultType
>(
abs(a-b)),order);
431
typedef
False is_kdtree_distance;
432
typedef
True is_vector_space_distance;
434
typedef
T ElementType;
435
typedef
typename
Accumulator<T>::Type ResultType;
436
typedef
ResultType CentersType;
443
template
<
typename
Iterator1,
typename
Iterator2>
444
ResultType operator()(Iterator1 a, Iterator2 b,
size_t
size, ResultType worst_dist = -1)
const
446
ResultType result = ResultType();
447
ResultType diff0, diff1, diff2, diff3;
448
Iterator1 last = a + size;
449
Iterator1 lastgroup = last - 3;
452
while
(a < lastgroup) {
453
diff0 =
abs(a[0] - b[0]);
454
diff1 =
abs(a[1] - b[1]);
455
diff2 =
abs(a[2] - b[2]);
456
diff3 =
abs(a[3] - b[3]);
457
if
(diff0>result) {result = diff0; }
458
if
(diff1>result) {result = diff1; }
459
if
(diff2>result) {result = diff2; }
460
if
(diff3>result) {result = diff3; }
464
if
((worst_dist>0)&&(result>worst_dist)) {
470
diff0 =
abs(*a++ - *b++);
471
result = (diff0>result) ? diff0 : result;
489
typedef
False is_kdtree_distance;
490
typedef
False is_vector_space_distance;
492
typedef
unsigned
char
ElementType;
493
typedef
int
ResultType;
494
typedef
ElementType CentersType;
498
template<
typename
Iterator2>
499
ResultType operator()(
const
unsigned
char* a,
const
Iterator2 b,
size_t
size)
const
501
static
const
uchar popCountTable[] =
503
0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4, 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
504
1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5, 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
505
1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5, 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
506
2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
507
1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5, 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
508
2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
509
2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
510
3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7, 4, 5, 5, 6, 5, 6, 6, 7, 5, 6, 6, 7, 6, 7, 7, 8
512
ResultType result = 0;
513
const
unsigned
char* b2 =
reinterpret_cast<
const
unsigned
char*
>
(b);
514
for
(
size_t
i = 0; i < size; i++) {
515
result += popCountTable[a[i] ^ b2[i]];
521
ResultType operator()(
const
unsigned
char* a,
const
ZeroIterator<unsigned char> b,
size_t
size)
const
524
static
const
uchar popCountTable[] =
526
0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4, 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
527
1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5, 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
528
1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5, 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
529
2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
530
1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5, 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
531
2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
532
2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
533
3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7, 4, 5, 5, 6, 5, 6, 6, 7, 5, 6, 6, 7, 6, 7, 7, 8
535
ResultType result = 0;
536
for
(
size_t
i = 0; i < size; i++) {
537
result += popCountTable[a[i]];
550
typedef
False is_kdtree_distance;
551
typedef
False is_vector_space_distance;
554
typedef
T ElementType;
555
typedef
int
ResultType;
556
typedef
ElementType CentersType;
558
template<
typename
Iterator1,
typename
Iterator2>
559
ResultType operator()(
const
Iterator1 a,
const
Iterator2 b,
size_t
size, ResultType
= -1)
const
561
ResultType result = 0;
562
#if defined(__ARM_NEON__) && !defined(__CUDACC__)
564
const
unsigned
char* a2 =
reinterpret_cast<
const
unsigned
char*
>
(a);
565
const
unsigned
char* b2 =
reinterpret_cast<
const
unsigned
char*
>
(b);
566
uint32x4_t bits = vmovq_n_u32(0);
567
for
(
size_t
i = 0; i < size; i += 16) {
568
uint8x16_t A_vec = vld1q_u8 (a2 + i);
569
uint8x16_t B_vec = vld1q_u8 (b2 + i);
570
uint8x16_t AxorB = veorq_u8 (A_vec, B_vec);
571
uint8x16_t bitsSet = vcntq_u8 (AxorB);
572
uint16x8_t bitSet8 = vpaddlq_u8 (bitsSet);
573
uint32x4_t bitSet4 = vpaddlq_u16 (bitSet8);
574
bits = vaddq_u32(bits, bitSet4);
576
uint64x2_t bitSet2 = vpaddlq_u32 (bits);
577
result = vgetq_lane_s32 (vreinterpretq_s32_u64(bitSet2),0);
578
result += vgetq_lane_s32 (vreinterpretq_s32_u64(bitSet2),2);
580
#elif defined(__GNUC__)
583
typedef
unsigned
long
long
pop_t;
584
const
size_t
modulo = size %
sizeof(pop_t);
585
const
pop_t* a2 =
reinterpret_cast<
const
pop_t*
>
(a);
586
const
pop_t* b2 =
reinterpret_cast<
const
pop_t*
>
(b);
587
const
pop_t* a2_end = a2 + (size /
sizeof(pop_t));
589
for
(; a2 != a2_end; ++a2, ++b2) result += __builtin_popcountll((*a2) ^ (*b2));
594
pop_t a_final = 0, b_final = 0;
595
memcpy(&a_final, a2, modulo);
596
memcpy(&b_final, b2, modulo);
597
result += __builtin_popcountll(a_final ^ b_final);
602
result = lut(
reinterpret_cast<
const
unsigned
char*
>
(a),
603
reinterpret_cast<
const
unsigned
char*
>
(b), size);
609
template<
typename
Iterator1>
610
ResultType operator()(
const
Iterator1 a, ZeroIterator<unsigned char> b,
size_t
size, ResultType
= -1)
const
613
ResultType result = 0;
614
#if defined(__ARM_NEON__) && !defined(__CUDACC__)
616
const
unsigned
char* a2 =
reinterpret_cast<
const
unsigned
char*
>
(a);
617
uint32x4_t bits = vmovq_n_u32(0);
618
for
(
size_t
i = 0; i < size; i += 16) {
619
uint8x16_t A_vec = vld1q_u8 (a2 + i);
620
uint8x16_t bitsSet = vcntq_u8 (A_vec);
621
uint16x8_t bitSet8 = vpaddlq_u8 (bitsSet);
622
uint32x4_t bitSet4 = vpaddlq_u16 (bitSet8);
623
bits = vaddq_u32(bits, bitSet4);
625
uint64x2_t bitSet2 = vpaddlq_u32 (bits);
626
result = vgetq_lane_s32 (vreinterpretq_s32_u64(bitSet2),0);
627
result += vgetq_lane_s32 (vreinterpretq_s32_u64(bitSet2),2);
629
#elif defined(__GNUC__)
632
typedef
unsigned
long
long
pop_t;
633
const
size_t
modulo = size %
sizeof(pop_t);
634
const
pop_t* a2 =
reinterpret_cast<
const
pop_t*
>
(a);
635
const
pop_t* a2_end = a2 + (size /
sizeof(pop_t));
637
for
(; a2 != a2_end; ++a2) result += __builtin_popcountll(*a2);
643
memcpy(&a_final, a2, modulo);
644
result += __builtin_popcountll(a_final);
649
result = lut(
reinterpret_cast<
const
unsigned
char*
>
(a), b, size);
658
typedef
False is_kdtree_distance;
659
typedef
False is_vector_space_distance;
661
typedef
T ElementType;
662
typedef
int
ResultType;
663
typedef
ElementType CentersType;
667
unsigned
int
popcnt32(uint32_t n)
const
669
n -= ((n >> 1) & 0x55555555);
670
n = (n & 0x33333333) + ((n >> 2) & 0x33333333);
671
return
(((n + (n >> 4))& 0xF0F0F0F)* 0x1010101) >> 24;
674
#ifdef FLANN_PLATFORM_64_BIT
675
unsigned
int
popcnt64(uint64_t n)
const
677
n -= ((n >> 1) & 0x5555555555555555);
678
n = (n & 0x3333333333333333) + ((n >> 2) & 0x3333333333333333);
679
return
(((n + (n >> 4))& 0x0f0f0f0f0f0f0f0f)* 0x0101010101010101) >> 56;
683
template
<
typename
Iterator1,
typename
Iterator2>
684
ResultType operator()(
const
Iterator1 a,
const
Iterator2 b,
size_t
size, ResultType
= -1)
const
686
CV_DbgAssert(!(size % long_word_size_) &&
"vectors size must be multiple of long words size (i.e. 8)");
688
#ifdef FLANN_PLATFORM_64_BIT
689
const
uint64_t* pa =
reinterpret_cast<
const
uint64_t*
>(a);
690
const
uint64_t* pb =
reinterpret_cast<
const
uint64_t*
>(b);
691
ResultType result = 0;
692
size /= long_word_size_;
693
for(
size_t
i = 0; i < size; ++i ) {
694
result += popcnt64(*pa ^ *pb);
699
const
uint32_t* pa =
reinterpret_cast<
const
uint32_t*
>(a);
700
const
uint32_t* pb =
reinterpret_cast<
const
uint32_t*
>(b);
701
ResultType result = 0;
702
size /= long_word_size_;
703
for(
size_t
i = 0; i < size; ++i ) {
704
result += popcnt32(*pa ^ *pb);
713
template
<
typename
Iterator1>
714
ResultType operator()(
const
Iterator1 a, ZeroIterator<unsigned char> b,
size_t
size, ResultType
= -1)
const
716
CV_DbgAssert(!(size % long_word_size_) &&
"vectors size must be multiple of long words size (i.e. 8)");
719
#ifdef FLANN_PLATFORM_64_BIT
720
const
uint64_t* pa =
reinterpret_cast<
const
uint64_t*
>(a);
721
ResultType result = 0;
722
size /= long_word_size_;
723
for(
size_t
i = 0; i < size; ++i ) {
724
result += popcnt64(*pa);
728
const
uint32_t* pa =
reinterpret_cast<
const
uint32_t*
>(a);
729
ResultType result = 0;
730
size /= long_word_size_;
731
for(
size_t
i = 0; i < size; ++i ) {
732
result += popcnt32(*pa);
740
#ifdef FLANN_PLATFORM_64_BIT
741
static
const
size_t
long_word_size_ =
sizeof(uint64_t)/
sizeof(
unsigned
char);
743
static
const
size_t
long_word_size_ =
sizeof(uint32_t)/
sizeof(
unsigned
char);
753
typedef
False is_kdtree_distance;
754
typedef
False is_vector_space_distance;
756
typedef
unsigned
char
ElementType;
757
typedef
int
ResultType;
758
typedef
ElementType CentersType;
762
template<
typename
Iterator2>
763
ResultType operator()(
const
unsigned
char* a,
const
Iterator2 b,
size_t
size)
const
765
static
const
uchar popCountTable[] =
767
0, 1, 1, 1, 1, 2, 2, 2, 1, 2, 2, 2, 1, 2, 2, 2, 1, 2, 2, 2, 2, 3, 3, 3, 2, 3, 3, 3, 2, 3, 3, 3,
768
1, 2, 2, 2, 2, 3, 3, 3, 2, 3, 3, 3, 2, 3, 3, 3, 1, 2, 2, 2, 2, 3, 3, 3, 2, 3, 3, 3, 2, 3, 3, 3,
769
1, 2, 2, 2, 2, 3, 3, 3, 2, 3, 3, 3, 2, 3, 3, 3, 2, 3, 3, 3, 3, 4, 4, 4, 3, 4, 4, 4, 3, 4, 4, 4,
770
2, 3, 3, 3, 3, 4, 4, 4, 3, 4, 4, 4, 3, 4, 4, 4, 2, 3, 3, 3, 3, 4, 4, 4, 3, 4, 4, 4, 3, 4, 4, 4,
771
1, 2, 2, 2, 2, 3, 3, 3, 2, 3, 3, 3, 2, 3, 3, 3, 2, 3, 3, 3, 3, 4, 4, 4, 3, 4, 4, 4, 3, 4, 4, 4,
772
2, 3, 3, 3, 3, 4, 4, 4, 3, 4, 4, 4, 3, 4, 4, 4, 2, 3, 3, 3, 3, 4, 4, 4, 3, 4, 4, 4, 3, 4, 4, 4,
773
1, 2, 2, 2, 2, 3, 3, 3, 2, 3, 3, 3, 2, 3, 3, 3, 2, 3, 3, 3, 3, 4, 4, 4, 3, 4, 4, 4, 3, 4, 4, 4,
774
2, 3, 3, 3, 3, 4, 4, 4, 3, 4, 4, 4, 3, 4, 4, 4, 2, 3, 3, 3, 3, 4, 4, 4, 3, 4, 4, 4, 3, 4, 4, 4
776
ResultType result = 0;
777
const
unsigned
char* b2 =
reinterpret_cast<
const
unsigned
char*
>
(b);
778
for
(
size_t
i = 0; i < size; i++) {
779
result += popCountTable[a[i] ^ b2[i]];
785
ResultType operator()(
const
unsigned
char* a,
const
ZeroIterator<unsigned char> b,
size_t
size)
const
788
static
const
uchar popCountTable[] =
790
0, 1, 1, 1, 1, 2, 2, 2, 1, 2, 2, 2, 1, 2, 2, 2, 1, 2, 2, 2, 2, 3, 3, 3, 2, 3, 3, 3, 2, 3, 3, 3,
791
1, 2, 2, 2, 2, 3, 3, 3, 2, 3, 3, 3, 2, 3, 3, 3, 1, 2, 2, 2, 2, 3, 3, 3, 2, 3, 3, 3, 2, 3, 3, 3,
792
1, 2, 2, 2, 2, 3, 3, 3, 2, 3, 3, 3, 2, 3, 3, 3, 2, 3, 3, 3, 3, 4, 4, 4, 3, 4, 4, 4, 3, 4, 4, 4,
793
2, 3, 3, 3, 3, 4, 4, 4, 3, 4, 4, 4, 3, 4, 4, 4, 2, 3, 3, 3, 3, 4, 4, 4, 3, 4, 4, 4, 3, 4, 4, 4,
794
1, 2, 2, 2, 2, 3, 3, 3, 2, 3, 3, 3, 2, 3, 3, 3, 2, 3, 3, 3, 3, 4, 4, 4, 3, 4, 4, 4, 3, 4, 4, 4,
795
2, 3, 3, 3, 3, 4, 4, 4, 3, 4, 4, 4, 3, 4, 4, 4, 2, 3, 3, 3, 3, 4, 4, 4, 3, 4, 4, 4, 3, 4, 4, 4,
796
1, 2, 2, 2, 2, 3, 3, 3, 2, 3, 3, 3, 2, 3, 3, 3, 2, 3, 3, 3, 3, 4, 4, 4, 3, 4, 4, 4, 3, 4, 4, 4,
797
2, 3, 3, 3, 3, 4, 4, 4, 3, 4, 4, 4, 3, 4, 4, 4, 2, 3, 3, 3, 3, 4, 4, 4, 3, 4, 4, 4, 3, 4, 4, 4
799
ResultType result = 0;
800
for
(
size_t
i = 0; i < size; i++) {
801
result += popCountTable[a[i]];
811
typedef
False is_kdtree_distance;
812
typedef
False is_vector_space_distance;
814
typedef
T ElementType;
815
typedef
int
ResultType;
816
typedef
ElementType CentersType;
820
unsigned
int
popcnt32(uint32_t n)
const
822
n = ((n >> 1) | n) & 0x55555555;
823
n = (n & 0x33333333) + ((n >> 2) & 0x33333333);
824
return
(((n + (n >> 4))& 0x0F0F0F0F)* 0x01010101) >> 24;
827
#ifdef FLANN_PLATFORM_64_BIT
828
unsigned
int
popcnt64(uint64_t n)
const
830
n = ((n >> 1) | n) & 0x5555555555555555;
831
n = (n & 0x3333333333333333) + ((n >> 2) & 0x3333333333333333);
832
return
(((n + (n >> 4))& 0x0f0f0f0f0f0f0f0f)* 0x0101010101010101) >> 56;
836
template
<
typename
Iterator1,
typename
Iterator2>
837
ResultType operator()(
const
Iterator1 a,
const
Iterator2 b,
size_t
size, ResultType
= -1)
const
839
CV_DbgAssert(!(size % long_word_size_) &&
"vectors size must be multiple of long words size (i.e. 8)");
841
#ifdef FLANN_PLATFORM_64_BIT
842
const
uint64_t* pa =
reinterpret_cast<
const
uint64_t*
>(a);
843
const
uint64_t* pb =
reinterpret_cast<
const
uint64_t*
>(b);
844
ResultType result = 0;
845
size /= long_word_size_;
846
for(
size_t
i = 0; i < size; ++i ) {
847
result += popcnt64(*pa ^ *pb);
852
const
uint32_t* pa =
reinterpret_cast<
const
uint32_t*
>(a);
853
const
uint32_t* pb =
reinterpret_cast<
const
uint32_t*
>(b);
854
ResultType result = 0;
855
size /= long_word_size_;
856
for(
size_t
i = 0; i < size; ++i ) {
857
result += popcnt32(*pa ^ *pb);
866
template
<
typename
Iterator1>
867
ResultType operator()(
const
Iterator1 a, ZeroIterator<unsigned char> b,
size_t
size, ResultType
= -1)
const
869
CV_DbgAssert(!(size % long_word_size_) &&
"vectors size must be multiple of long words size (i.e. 8)");
872
#ifdef FLANN_PLATFORM_64_BIT
873
const
uint64_t* pa =
reinterpret_cast<
const
uint64_t*
>(a);
874
ResultType result = 0;
875
size /= long_word_size_;
876
for(
size_t
i = 0; i < size; ++i ) {
877
result += popcnt64(*pa);
881
const
uint32_t* pa =
reinterpret_cast<
const
uint32_t*
>(a);
882
ResultType result = 0;
883
size /= long_word_size_;
884
for(
size_t
i = 0; i < size; ++i ) {
885
result += popcnt32(*pa);
893
#ifdef FLANN_PLATFORM_64_BIT
894
static
const
size_t
long_word_size_=
sizeof(uint64_t)/
sizeof(
unsigned
char);
896
static
const
size_t
long_word_size_=
sizeof(uint32_t)/
sizeof(
unsigned
char);
903
struct
HistIntersectionDistance
905
typedef
True is_kdtree_distance;
906
typedef
True is_vector_space_distance;
908
typedef
T ElementType;
909
typedef
typename
Accumulator<T>::Type ResultType;
910
typedef
ResultType CentersType;
915
template
<
typename
Iterator1,
typename
Iterator2>
916
ResultType operator()(Iterator1 a, Iterator2 b,
size_t
size, ResultType worst_dist = -1)
const
918
ResultType result = ResultType();
919
ResultType min0, min1, min2, min3;
920
Iterator1 last = a + size;
921
Iterator1 lastgroup = last - 3;
924
while
(a < lastgroup) {
925
min0 = (ResultType)(a[0] < b[0] ? a[0] : b[0]);
926
min1 = (ResultType)(a[1] < b[1] ? a[1] : b[1]);
927
min2 = (ResultType)(a[2] < b[2] ? a[2] : b[2]);
928
min3 = (ResultType)(a[3] < b[3] ? a[3] : b[3]);
929
result += min0 + min1 + min2 + min3;
932
if
((worst_dist>0)&&(result>worst_dist)) {
938
min0 = (ResultType)(*a < *b ? *a : *b);
949
template
<
typename
U,
typename
V>
950
inline
ResultType accum_dist(
const
U& a,
const
V& b,
int)
const
959
struct
HellingerDistance
961
typedef
True is_kdtree_distance;
962
typedef
True is_vector_space_distance;
964
typedef
T ElementType;
965
typedef
typename
Accumulator<T>::Type ResultType;
966
typedef
ResultType CentersType;
971
template
<
typename
Iterator1,
typename
Iterator2>
972
ResultType operator()(Iterator1 a, Iterator2 b,
size_t
size, ResultType
= -1)
const
974
ResultType result = ResultType();
975
ResultType diff0, diff1, diff2, diff3;
976
Iterator1 last = a + size;
977
Iterator1 lastgroup = last - 3;
980
while
(a < lastgroup) {
981
diff0 =
sqrt(
static_cast<ResultType
>(a[0])) -
sqrt(
static_cast<ResultType
>(b[0]));
982
diff1 =
sqrt(
static_cast<ResultType
>(a[1])) -
sqrt(
static_cast<ResultType
>(b[1]));
983
diff2 =
sqrt(
static_cast<ResultType
>(a[2])) -
sqrt(
static_cast<ResultType
>(b[2]));
984
diff3 =
sqrt(
static_cast<ResultType
>(a[3])) -
sqrt(
static_cast<ResultType
>(b[3]));
985
result += diff0 * diff0 + diff1 * diff1 + diff2 * diff2 + diff3 * diff3;
990
diff0 =
sqrt(
static_cast<ResultType
>(*a++)) -
sqrt(
static_cast<ResultType
>(*b++));
991
result += diff0 * diff0;
999
template
<
typename
U,
typename
V>
1000
inline
ResultType accum_dist(
const
U& a,
const
V& b,
int)
const
1002
ResultType diff =
sqrt(
static_cast<ResultType
>(a)) -
sqrt(
static_cast<ResultType
>(b));
1009
struct
ChiSquareDistance
1011
typedef
True is_kdtree_distance;
1012
typedef
True is_vector_space_distance;
1014
typedef
T ElementType;
1015
typedef
typename
Accumulator<T>::Type ResultType;
1016
typedef
ResultType CentersType;
1021
template
<
typename
Iterator1,
typename
Iterator2>
1022
ResultType operator()(Iterator1 a, Iterator2 b,
size_t
size, ResultType worst_dist = -1)
const
1024
ResultType result = ResultType();
1025
ResultType sum, diff;
1026
Iterator1 last = a + size;
1029
sum = (ResultType)(*a + *b);
1031
diff = (ResultType)(*a - *b);
1032
result += diff*diff/sum;
1037
if
((worst_dist>0)&&(result>worst_dist)) {
1047
template
<
typename
U,
typename
V>
1048
inline
ResultType accum_dist(
const
U& a,
const
V& b,
int)
const
1050
ResultType result = ResultType();
1051
ResultType sum, diff;
1053
sum = (ResultType)(a+b);
1055
diff = (ResultType)(a-b);
1056
result = diff*diff/sum;
1064
struct
KL_Divergence
1066
typedef
True is_kdtree_distance;
1067
typedef
True is_vector_space_distance;
1069
typedef
T ElementType;
1070
typedef
typename
Accumulator<T>::Type ResultType;
1071
typedef
ResultType CentersType;
1076
template
<
typename
Iterator1,
typename
Iterator2>
1077
ResultType operator()(Iterator1 a, Iterator2 b,
size_t
size, ResultType worst_dist = -1)
const
1079
ResultType result = ResultType();
1080
Iterator1 last = a + size;
1083
if
( *a != 0 && *b != 0 ) {
1084
ResultType ratio = (ResultType)(*a / *b);
1086
result += *a *
log(ratio);
1092
if
((worst_dist>0)&&(result>worst_dist)) {
1102
template
<
typename
U,
typename
V>
1103
inline
ResultType accum_dist(
const
U& a,
const
V& b,
int)
const
1105
ResultType result = ResultType();
1106
if( a != 0 && b != 0 ) {
1107
ResultType ratio = (ResultType)(a / b);
1109
result = a *
log(ratio);
1122
template
<
typename
Distance,
typename
ElementType>
1123
struct
squareDistance
1125
typedef
typename
Distance::ResultType ResultType;
1126
ResultType operator()( ResultType dist ) {
return
dist*dist; }
1130
template
<
typename
ElementType>
1131
struct
squareDistance<L2_Simple<ElementType>, ElementType>
1133
typedef
typename
L2_Simple<ElementType>::ResultType ResultType;
1134
ResultType operator()( ResultType dist ) {
return
dist; }
1137
template
<
typename
ElementType>
1138
struct
squareDistance<L2<ElementType>, ElementType>
1140
typedef
typename
L2<ElementType>::ResultType ResultType;
1141
ResultType operator()( ResultType dist ) {
return
dist; }
1145
template
<
typename
ElementType>
1146
struct
squareDistance<MinkowskiDistance<ElementType>, ElementType>
1148
typedef
typename
MinkowskiDistance<ElementType>::ResultType ResultType;
1149
ResultType operator()( ResultType dist ) {
return
dist; }
1152
template
<
typename
ElementType>
1153
struct
squareDistance<HellingerDistance<ElementType>, ElementType>
1155
typedef
typename
HellingerDistance<ElementType>::ResultType ResultType;
1156
ResultType operator()( ResultType dist ) {
return
dist; }
1159
template
<
typename
ElementType>
1160
struct
squareDistance<ChiSquareDistance<ElementType>, ElementType>
1162
typedef
typename
ChiSquareDistance<ElementType>::ResultType ResultType;
1163
ResultType operator()( ResultType dist ) {
return
dist; }
1167
template
<
typename
Distance>
1168
typename
Distance::ResultType ensureSquareDistance(
typename
Distance::ResultType dist )
1170
typedef
typename
Distance::ElementType ElementType;
1172
squareDistance<Distance, ElementType> dummy;
1173
return
dummy( dist );
1181
template
<
typename
Distance,
typename
ElementType>
1184
bool
operator()() {
return
false; }
1188
template
<
typename
ElementType>
1189
struct
isSquareDist<L2_Simple<ElementType>, ElementType>
1191
bool
operator()() {
return
true; }
1194
template
<
typename
ElementType>
1195
struct
isSquareDist<L2<ElementType>, ElementType>
1197
bool
operator()() {
return
true; }
1201
template
<
typename
ElementType>
1202
struct
isSquareDist<MinkowskiDistance<ElementType>, ElementType>
1204
bool
operator()() {
return
true; }
1207
template
<
typename
ElementType>
1208
struct
isSquareDist<HellingerDistance<ElementType>, ElementType>
1210
bool
operator()() {
return
true; }
1213
template
<
typename
ElementType>
1214
struct
isSquareDist<ChiSquareDistance<ElementType>, ElementType>
1216
bool
operator()() {
return
true; }
1220
template
<
typename
Distance>
1221
bool
isSquareDistance()
1223
typedef
typename
Distance::ElementType ElementType;
1225
isSquareDist<Distance, ElementType> dummy;
1234
template
<
typename
Distance,
typename
ElementType>
1235
struct
simpleDistance
1237
typedef
typename
Distance::ResultType ResultType;
1238
ResultType operator()( ResultType dist ) {
return
dist; }
1242
template
<
typename
ElementType>
1243
struct
simpleDistance<L2_Simple<ElementType>, ElementType>
1245
typedef
typename
L2_Simple<ElementType>::ResultType ResultType;
1246
ResultType operator()( ResultType dist ) {
return
sqrt(dist); }
1249
template
<
typename
ElementType>
1250
struct
simpleDistance<L2<ElementType>, ElementType>
1252
typedef
typename
L2<ElementType>::ResultType ResultType;
1253
ResultType operator()( ResultType dist ) {
return
sqrt(dist); }
1257
template
<
typename
ElementType>
1258
struct
simpleDistance<MinkowskiDistance<ElementType>, ElementType>
1260
typedef
typename
MinkowskiDistance<ElementType>::ResultType ResultType;
1261
ResultType operator()( ResultType dist ) {
return
sqrt(dist); }
1264
template
<
typename
ElementType>
1265
struct
simpleDistance<HellingerDistance<ElementType>, ElementType>
1267
typedef
typename
HellingerDistance<ElementType>::ResultType ResultType;
1268
ResultType operator()( ResultType dist ) {
return
sqrt(dist); }
1271
template
<
typename
ElementType>
1272
struct
simpleDistance<ChiSquareDistance<ElementType>, ElementType>
1274
typedef
typename
ChiSquareDistance<ElementType>::ResultType ResultType;
1275
ResultType operator()( ResultType dist ) {
return
sqrt(dist); }
1279
template
<
typename
Distance>
1280
typename
Distance::ResultType ensureSimpleDistance(
typename
Distance::ResultType dist )
1282
typedef
typename
Distance::ElementType ElementType;
1284
simpleDistance<Distance, ElementType> dummy;
1285
return
dummy( dist );
CV_EXPORTS_W void sqrt(InputArray src, OutputArray dst)
Calculates a square root of array elements.
CV_EXPORTS_W void pow(InputArray src, double power, OutputArray dst)
Raises every array element to a power.
CV_EXPORTS_W void log(InputArray src, OutputArray dst)
Calculates the natural logarithm of every array element.
CV_INLINE v_reg< _Tp, n > operator*(const v_reg< _Tp, n > &a, const v_reg< _Tp, n > &b)
Multiply values
softfloat abs(softfloat a)
Absolute value
Definition:
softfloat.hpp:444
#define CV_DbgAssert(expr)
Definition:
base.hpp:375