8namespace gtsam_points {
14 template <
typename Result>
16 return result.worst_distance() <
epsilon || result.num_found() >=
max_nn;
20 int max_nn = std::numeric_limits<int>::max();
28 T operator()(
const T& x)
const {
35template <
int N,
typename IndexTransform =
identity_transform>
38 static constexpr size_t INVALID = std::numeric_limits<size_t>::max();
48 int num_neighbors = -1,
49 const IndexTransform& index_transform = IndexTransform(),
50 double max_sq_dist = std::numeric_limits<double>::max())
51 : index_transform(index_transform),
56 if constexpr (N > 0) {
57 if (num_neighbors >= 0) {
58 std::cerr <<
"warning: Specifying dynamic num_neighbors=" << num_neighbors <<
" for a static KNN result container (N=" << N <<
")"
63 if (num_neighbors <= 0) {
64 std::cerr <<
"error: Specifying invalid num_neighbors=" << num_neighbors <<
" for a dynamic KNN result container" << std::endl;
69 std::fill(this->indices, this->indices +
buffer_size(), INVALID);
70 std::fill(this->distances, this->distances +
buffer_size(), max_sq_dist);
75 if constexpr (N > 0) {
89 void push(
size_t index,
double distance) {
94 if constexpr (N == 1) {
95 indices[0] = index_transform(index);
99 for (; insert_loc > 0 && distance <
distances[insert_loc - 1]; insert_loc--) {
104 indices[insert_loc] = index_transform(index);
112 const IndexTransform& index_transform;
120template <
typename IndexTransform =
identity_transform>
125 explicit RadiusSearchResult(
const IndexTransform& index_transform = IndexTransform()) : index_transform(index_transform) {
neighbors.reserve(32); }
131 constexpr double worst_distance()
const {
return std::numeric_limits<double>::max(); }
135 std::sort(
neighbors.begin(),
neighbors.end(), [](
const auto& a,
const auto& b) { return a.second < b.second; });
139 void push(
size_t index,
double distance) {
neighbors.emplace_back(index_transform(index), distance); }
142 const IndexTransform& index_transform;
K-nearest neighbor search result container.
Definition knn_result.hpp:36
void push(size_t index, double distance)
Push a pair of point index and distance to the result.
Definition knn_result.hpp:89
double worst_distance() const
Worst distance in the result.
Definition knn_result.hpp:86
size_t buffer_size() const
Buffer size (i.e., Maximum number of neighbors)
Definition knn_result.hpp:74
const int capacity
Maximum number of neighbors to search.
Definition knn_result.hpp:113
size_t num_found() const
Number of found neighbors.
Definition knn_result.hpp:83
KnnResult(size_t *indices, double *distances, int num_neighbors=-1, const IndexTransform &index_transform=IndexTransform(), double max_sq_dist=std::numeric_limits< double >::max())
Constructor.
Definition knn_result.hpp:45
size_t * indices
Indices of neighbors.
Definition knn_result.hpp:115
double * distances
Distances to neighbors.
Definition knn_result.hpp:116
int num_found_neighbors
Number of found neighbors.
Definition knn_result.hpp:114
K-nearest neighbor search setting.
Definition knn_result.hpp:11
double epsilon
Early termination threshold.
Definition knn_result.hpp:22
int max_nn
Maximum number of neighbors to search.
Definition knn_result.hpp:20
bool fulfilled(const Result &result) const
Check if the result satisfies the early termination condition.
Definition knn_result.hpp:15
double max_sq_dist
Maximum squared distance to search.
Definition knn_result.hpp:21
Radius neighbor search result container.
Definition knn_result.hpp:121
void sort()
Sort neighbors by distance.
Definition knn_result.hpp:134
std::vector< std::pair< size_t, double > > neighbors
Pairs of point index and distance.
Definition knn_result.hpp:143
void push(size_t index, double distance)
Push a pair of point index and distance to the result.
Definition knn_result.hpp:139
constexpr double worst_distance() const
Worst distance in the result.
Definition knn_result.hpp:131
size_t num_found() const
Number of found neighbors.
Definition knn_result.hpp:128
RadiusSearchResult(const IndexTransform &index_transform=IndexTransform())
Constructor.
Definition knn_result.hpp:125