31 #ifndef RTABMAP_FLANN_INDEX_TESTING_H_ 32 #define RTABMAP_FLANN_INDEX_TESTING_H_ 51 for (
int i=0; i<n; ++i) {
52 for (
int k=0; k<n; ++k) {
53 if (neighbors[i]==groundTruth[k]) {
63 template <
typename Distance>
65 size_t* neighbors,
size_t* groundTruth,
int veclen,
int n,
const Distance&
distance)
67 typedef typename Distance::ResultType DistanceType;
70 for (
int i=0; i<n; ++i) {
71 DistanceType den =
distance(inputData[groundTruth[i]], target, veclen);
72 DistanceType num =
distance(inputData[neighbors[i]], target, veclen);
74 if ((den==0)&&(num==0)) {
85 template <
typename Index,
typename Distance>
88 float& time,
typename Distance::ResultType& dist,
const Distance&
distance,
int skipMatches)
90 typedef typename Distance::ElementType ElementType;
91 typedef typename Distance::ResultType DistanceType;
93 if (matches.
cols<
size_t(nn)) {
94 Logger::info(
"matches.cols=%d, nn=%d\n",matches.
cols,nn);
95 throw FLANNException(
"Ground truth is not computed for as many neighbors as requested");
100 size_t* indices =
new size_t[nn+skipMatches];
101 DistanceType* dists =
new DistanceType[nn+skipMatches];
106 size_t* neighbors = indices + skipMatches;
109 DistanceType distR = 0;
112 while (t.
value<0.2) {
117 for (
size_t i = 0; i < testData.
rows; i++) {
121 distR += computeDistanceRaport<Distance>(inputData, testData[i], neighbors, matches[i], testData.
cols, nn,
distance);
125 time = float(t.
value/repeats);
130 float precicion = (float)correct/(nn*testData.
rows);
132 dist = distR/(testData.
rows*nn);
134 Logger::info(
"%8d %10.4g %10.5g %10.5g %10.5g\n",
135 checks, precicion, time, 1000.0 * time / testData.
rows, dist);
141 template <
typename Index,
typename Distance>
144 int checks,
float&
precision,
const Distance&
distance,
int nn = 1,
int skipMatches = 0)
146 typedef typename Distance::ResultType DistanceType;
148 Logger::info(
" Nodes Precision(%) Time(s) Time/vec(ms) Mean dist\n");
149 Logger::info(
"---------------------------------------------------------\n");
152 DistanceType
dist = 0;
153 precision =
search_with_ground_truth(index, inputData, testData, matches, nn, checks, time, dist, distance, skipMatches);
158 template <
typename Index,
typename Distance>
161 float precision,
int& checks,
const Distance&
distance,
int nn = 1,
int skipMatches = 0)
163 typedef typename Distance::ResultType DistanceType;
164 const float SEARCH_EPS = 0.001f;
166 Logger::info(
" Nodes Precision(%) Time(s) Time/vec(ms) Mean dist\n");
167 Logger::info(
"---------------------------------------------------------\n");
179 Logger::info(
"Got as close as I can\n");
184 while (p2<precision) {
193 if (fabs(p2-precision)>SEARCH_EPS) {
194 Logger::info(
"Start linear estimation\n");
199 realPrecision =
search_with_ground_truth(index, inputData, testData, matches, nn, cx, time, dist, distance, skipMatches);
200 while (fabs(realPrecision-precision)>SEARCH_EPS) {
202 if (realPrecision<precision) {
210 Logger::info(
"Got as close as I can\n");
213 realPrecision =
search_with_ground_truth(index, inputData, testData, matches, nn, cx, time, dist, distance, skipMatches);
221 Logger::info(
"No need for linear estimation\n");
231 template <
typename Index,
typename Distance>
234 float* precisions,
int precisions_length,
const Distance&
distance,
int nn = 1,
int skipMatches = 0,
float maxTime = 0)
236 typedef typename Distance::ResultType DistanceType;
238 const float SEARCH_EPS = 0.001;
241 std::sort(precisions, precisions+precisions_length);
246 Logger::info(
" Nodes Precision(%) Time(s) Time/vec(ms) Mean dist\n");
247 Logger::info(
"---------------------------------------------------------\n");
263 while (precisions[pindex]<p2 && pindex<precisions_length) {
267 if (pindex==precisions_length) {
268 Logger::info(
"Got as close as I can\n");
272 for (
int i=pindex; i<precisions_length; ++i) {
274 precision = precisions[i];
275 while (p2<precision) {
280 if ((maxTime> 0)&&(time > maxTime)&&(p2<precision))
return;
285 if (fabs(p2-precision)>SEARCH_EPS) {
286 Logger::info(
"Start linear estimation\n");
291 realPrecision =
search_with_ground_truth(index, inputData, testData, matches, nn, cx, time, dist, distance, skipMatches);
292 while (fabs(realPrecision-precision)>SEARCH_EPS) {
294 if (realPrecision<precision) {
302 Logger::info(
"Got as close as I can\n");
305 realPrecision =
search_with_ground_truth(index, inputData, testData, matches, nn, cx, time, dist, distance, skipMatches);
313 Logger::info(
"No need for linear estimation\n");
323 #endif //FLANN_INDEX_TESTING_H_ float test_index_checks(Index &index, const Matrix< typename Distance::ElementType > &inputData, const Matrix< typename Distance::ElementType > &testData, const Matrix< size_t > &matches, int checks, float &precision, const Distance &distance, int nn=1, int skipMatches=0)
GLM_FUNC_DECL genType::value_type distance(genType const &p0, genType const &p1)
int countCorrectMatches(size_t *neighbors, size_t *groundTruth, int n)
float test_index_precision(Index &index, const Matrix< typename Distance::ElementType > &inputData, const Matrix< typename Distance::ElementType > &testData, const Matrix< size_t > &matches, float precision, int &checks, const Distance &distance, int nn=1, int skipMatches=0)
float search_with_ground_truth(Index &index, const Matrix< typename Distance::ElementType > &inputData, const Matrix< typename Distance::ElementType > &testData, const Matrix< size_t > &matches, int nn, int checks, float &time, typename Distance::ResultType &dist, const Distance &distance, int skipMatches)
Distance::ResultType computeDistanceRaport(const Matrix< typename Distance::ElementType > &inputData, typename Distance::ElementType *target, size_t *neighbors, size_t *groundTruth, int veclen, int n, const Distance &distance)
int knnSearch(const Matrix< ElementType > &queries, Matrix< size_t > &indices, Matrix< DistanceType > &dists, size_t knn, const SearchParams ¶ms) const
Perform k-nearest neighbor search.
void test_index_precisions(Index &index, const Matrix< typename Distance::ElementType > &inputData, const Matrix< typename Distance::ElementType > &testData, const Matrix< int > &matches, float *precisions, int precisions_length, const Distance &distance, int nn=1, int skipMatches=0, float maxTime=0)