Skip to content

Commit 147f3eb

Browse files
committed
flann: use OpenCV theRNG()
std::rand() has no thread-safe guarantee.
1 parent 53e6854 commit 147f3eb

File tree

3 files changed

+34
-3
lines changed

3 files changed

+34
-3
lines changed

modules/flann/include/opencv2/flann/kdtree_index.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,12 @@ class KDTreeIndex : public NNIndex<Distance>
125125
/* Construct the randomized trees. */
126126
for (int i = 0; i < trees_; i++) {
127127
/* Randomize the order of vectors to allow for unbiased sampling. */
128+
#ifndef OPENCV_FLANN_USE_STD_RAND
129+
cv::randShuffle(vind_);
130+
#else
128131
std::random_shuffle(vind_.begin(), vind_.end());
132+
#endif
133+
129134
tree_roots_[i] = divideTree(&vind_[0], int(size_) );
130135
}
131136
}

modules/flann/include/opencv2/flann/lsh_table.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -350,7 +350,11 @@ inline LshTable<unsigned char>::LshTable(unsigned int feature_size, unsigned int
350350
// A bit brutal but fast to code
351351
std::vector<size_t> indices(feature_size * CHAR_BIT);
352352
for (size_t i = 0; i < feature_size * CHAR_BIT; ++i) indices[i] = i;
353+
#ifndef OPENCV_FLANN_USE_STD_RAND
354+
cv::randShuffle(indices);
355+
#else
353356
std::random_shuffle(indices.begin(), indices.end());
357+
#endif
354358

355359
// Generate a random set of order of subsignature_size_ bits
356360
for (unsigned int i = 0; i < key_size_; ++i) {

modules/flann/include/opencv2/flann/random.h

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,31 @@
4040
namespace cvflann
4141
{
4242

43+
inline int rand()
44+
{
45+
#ifndef OPENCV_FLANN_USE_STD_RAND
46+
# if INT_MAX == RAND_MAX
47+
int v = cv::theRNG().next() & INT_MAX;
48+
# else
49+
int v = cv::theRNG().uniform(0, RAND_MAX + 1);
50+
# endif
51+
#else
52+
int v = std::rand();
53+
#endif // OPENCV_FLANN_USE_STD_RAND
54+
return v;
55+
}
56+
4357
/**
4458
* Seeds the random number generator
4559
* @param seed Random seed
4660
*/
4761
inline void seed_random(unsigned int seed)
4862
{
49-
srand(seed);
63+
#ifndef OPENCV_FLANN_USE_STD_RAND
64+
cv::theRNG() = cv::RNG(seed);
65+
#else
66+
std::srand(seed);
67+
#endif
5068
}
5169

5270
/*
@@ -60,7 +78,7 @@ inline void seed_random(unsigned int seed)
6078
*/
6179
inline double rand_double(double high = 1.0, double low = 0)
6280
{
63-
return low + ((high-low) * (std::rand() / (RAND_MAX + 1.0)));
81+
return low + ((high-low) * (rand() / (RAND_MAX + 1.0)));
6482
}
6583

6684
/**
@@ -71,7 +89,7 @@ inline double rand_double(double high = 1.0, double low = 0)
7189
*/
7290
inline int rand_int(int high = RAND_MAX, int low = 0)
7391
{
74-
return low + (int) ( double(high-low) * (std::rand() / (RAND_MAX + 1.0)));
92+
return low + (int) ( double(high-low) * (rand() / (RAND_MAX + 1.0)));
7593
}
7694

7795
/**
@@ -107,7 +125,11 @@ class UniqueRandom
107125
for (int i = 0; i < size_; ++i) vals_[i] = i;
108126

109127
// shuffle the elements in the array
128+
#ifndef OPENCV_FLANN_USE_STD_RAND
129+
cv::randShuffle(vals_);
130+
#else
110131
std::random_shuffle(vals_.begin(), vals_.end());
132+
#endif
111133

112134
counter_ = 0;
113135
}

0 commit comments

Comments
 (0)