Repository navigation
Expand file tree
/
Copy pathfeature_search.cpp
More file actions
56 lines (47 loc) · 1.69 KB
/
Copy pathfeature_search.cpp
File metadata and controls
56 lines (47 loc) · 1.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include "feature_search.hpp"
#include <algorithm>
#include <functional>
#include <vector>
namespace slam {
class FeatureSearchImplementation : public FeatureSearch {
private:
struct Node {
float x;
float y;
std::size_t keypointIndex;
};
// first implementation: keypoints in an array sorted by Y coordinate
// (the Y size of the image is assumed to be larger than X size)
std::vector<Node> indexByY;
std::function<bool(const Node&, const Node&)> comparator;
public:
FeatureSearchImplementation(const KeyPointVector &keypoints) {
for (std::size_t i = 0; i < keypoints.size(); ++i) {
const auto &kp = keypoints[i];
indexByY.push_back({ kp.pt.x, kp.pt.y, i });
}
comparator = [](const Node &a, const Node &b) -> bool {
return a.y < b.y;
};
std::sort(indexByY.begin(), indexByY.end(), comparator);
}
void getFeaturesAround(float x, float y, float r, std::vector<size_t> &output) const final {
output.clear();
// find Y range begin using binary search
const Node lb { x, y - r, 0 };
for (auto itr = std::lower_bound(indexByY.begin(), indexByY.end(), lb, comparator);
itr != indexByY.end() && itr->y <= y + r;
++itr)
{
// pick points within correct X bounds
const float dx = x - itr->x;
const float dy = y - itr->y;
if (dx*dx + dy*dy < r*r)
output.push_back(itr->keypointIndex);
}
}
};
std::unique_ptr<FeatureSearch> FeatureSearch::create(const KeyPointVector &kps) {
return std::unique_ptr<FeatureSearch>(new FeatureSearchImplementation(kps));
}
}