flann-lib / flann

Fast Library for Approximate Nearest Neighbors
http://people.cs.ubc.ca/~mariusm/flann
Other
2.21k stars 646 forks source link

The knnsearch result seems to be random in C++ version #509

Open Philip035 opened 1 year ago

Philip035 commented 1 year ago

When I use flann to perform KNN search on the same data, the result seems to be random in every test. Here is my code:

vector<vector<int>> Poi_kNN_Idx(Poi_In_Ipl_Num, vector<int>(Ngh_K));
vector<vector<double>> Dis_kNN(Poi_In_Ipl_Num, vector<double>(Ngh_K));
flann::Matrix<double> dataMatrix(Poi_In_Ref_Tmp, Poi_In_Ref_Num, 3);
flann::Index<flann::L2<double>> index(dataMatrix, flann::KDTreeIndexParams(4));
index.buildIndex();
flann::Matrix<double> queryMatrix(Poi_In_Ipl_Tmp, Poi_In_Ipl_Num, 3);
index.knnSearch(queryMatrix, Poi_kNN_Idx, Dis_kNN, Ngh_K, flann::SearchParams(128));

where Poi_In_Ref_Tmp and Poi_In_Ipl_Tmp are defined as double*, and xyz coordinates of each point are stored in turn. However, the result of Poi_kNN_Idx seems to be unfixed.

stephematician commented 11 months ago

The default method is randomised kd-trees therefore the results are random, too. The algorithms are described here: https://www.cs.ubc.ca/~lowe/papers/09muja.pdf