9 #include <tbb/parallel_for.h>
13 #include <nanoflann.hpp>
25 template <
int METRIC,
class TReal,
class TIndex>
31 const TReal *
const data_ptr)
53 template <
int M,
typename fake =
void>
56 template <
typename fake>
58 typedef nanoflann::L2_Adaptor<TReal, DataAdaptor, TReal>
adaptor_t;
61 template <
typename fake>
63 typedef nanoflann::L1_Adaptor<TReal, DataAdaptor, TReal>
adaptor_t;
67 typedef nanoflann::KDTreeSingleIndexAdaptor<
76 const TReal *data_ptr) {
88 template <
class T,
class TIndex,
int METRIC>
89 void _BuildKdTree(
size_t num_points,
97 template <
class T,
class TIndex,
class OUTPUT_ALLOCATOR,
int METRIC>
99 int64_t *query_neighbors_row_splits,
103 const T *
const queries,
104 const size_t dimension,
106 bool ignore_query_point,
107 bool return_distances,
108 OUTPUT_ALLOCATOR &output_allocator) {
110 if (num_queries == 0 || num_points == 0 || holder ==
nullptr) {
111 std::fill(query_neighbors_row_splits,
112 query_neighbors_row_splits + num_queries + 1, 0);
114 output_allocator.AllocIndices(&indices_ptr, 0);
117 output_allocator.AllocDistances(&distances_ptr, 0);
121 auto points_equal = [](
const T *
const p1,
const T *
const p2,
123 std::vector<T> p1_vec(p1, p1 + dimension);
124 std::vector<T> p2_vec(p2, p2 + dimension);
125 return p1_vec == p2_vec;
128 std::vector<std::vector<TIndex>> neighbors_indices(num_queries);
129 std::vector<std::vector<T>> neighbors_distances(num_queries);
130 std::vector<uint32_t> neighbors_count(num_queries, 0);
137 tbb::blocked_range<size_t>(0, num_queries),
138 [&](
const tbb::blocked_range<size_t> &r) {
139 std::vector<TIndex> result_indices(knn);
140 std::vector<T> result_distances(knn);
141 for (
size_t i = r.begin(); i != r.end(); ++i) {
142 size_t num_valid = holder_->index_->knnSearch(
143 &queries[i * dimension], knn, result_indices.data(),
144 result_distances.data());
146 int num_neighbors = 0;
147 for (
size_t valid_i = 0; valid_i < num_valid; ++valid_i) {
148 TIndex idx = result_indices[valid_i];
149 if (ignore_query_point &&
150 points_equal(&queries[i * dimension],
151 &
points[idx * dimension], dimension)) {
154 neighbors_indices[i].push_back(idx);
155 if (return_distances) {
156 T dist = result_distances[valid_i];
157 neighbors_distances[i].push_back(dist);
161 neighbors_count[i] = num_neighbors;
165 query_neighbors_row_splits[0] = 0;
167 neighbors_count.data() + neighbors_count.size(),
168 query_neighbors_row_splits + 1);
170 int64_t num_indices = query_neighbors_row_splits[num_queries];
173 output_allocator.AllocIndices(&indices_ptr, num_indices);
175 if (return_distances)
176 output_allocator.AllocDistances(&distances_ptr, num_indices);
178 output_allocator.AllocDistances(&distances_ptr, 0);
180 std::fill(neighbors_count.begin(), neighbors_count.end(), 0);
183 tbb::parallel_for(tbb::blocked_range<size_t>(0, num_queries),
184 [&](
const tbb::blocked_range<size_t> &r) {
185 for (
size_t i = r.begin(); i != r.end(); ++i) {
186 int64_t start_idx = query_neighbors_row_splits[i];
187 std::copy(neighbors_indices[i].begin(),
188 neighbors_indices[i].end(),
189 &indices_ptr[start_idx]);
191 if (return_distances) {
192 std::copy(neighbors_distances[i].begin(),
193 neighbors_distances[i].end(),
194 &distances_ptr[start_idx]);
200 template <
class T,
class TIndex,
class OUTPUT_ALLOCATOR,
int METRIC>
201 void _RadiusSearchCPU(NanoFlannIndexHolderBase *holder,
202 int64_t *query_neighbors_row_splits,
206 const T *
const queries,
207 const size_t dimension,
208 const T *
const radii,
209 bool ignore_query_point,
210 bool return_distances,
211 bool normalize_distances,
213 OUTPUT_ALLOCATOR &output_allocator) {
214 if (num_queries == 0 || num_points == 0 || holder ==
nullptr) {
215 std::fill(query_neighbors_row_splits,
216 query_neighbors_row_splits + num_queries + 1, 0);
218 output_allocator.AllocIndices(&indices_ptr, 0);
221 output_allocator.AllocDistances(&distances_ptr, 0);
225 auto points_equal = [](
const T *
const p1,
const T *
const p2,
227 std::vector<T> p1_vec(p1, p1 + dimension);
228 std::vector<T> p2_vec(p2, p2 + dimension);
229 return p1_vec == p2_vec;
232 std::vector<std::vector<TIndex>> neighbors_indices(num_queries);
233 std::vector<std::vector<T>> neighbors_distances(num_queries);
234 std::vector<uint32_t> neighbors_count(num_queries, 0);
236 nanoflann::SearchParams params;
237 params.sorted = sort;
240 static_cast<NanoFlannIndexHolder<METRIC, T, TIndex> *
>(holder);
242 tbb::blocked_range<size_t>(0, num_queries),
243 [&](
const tbb::blocked_range<size_t> &r) {
244 std::vector<std::pair<TIndex, T>> search_result;
245 for (
size_t i = r.begin(); i != r.end(); ++i) {
248 radius = radius * radius;
251 holder_->index_->radiusSearch(&queries[i * dimension],
252 radius, search_result,
255 int num_neighbors = 0;
256 for (
const auto &idx_dist : search_result) {
257 if (ignore_query_point &&
258 points_equal(&queries[i * dimension],
259 &
points[idx_dist.first * dimension],
263 neighbors_indices[i].push_back(idx_dist.first);
264 if (return_distances) {
265 neighbors_distances[i].push_back(idx_dist.second);
269 neighbors_count[i] = num_neighbors;
273 query_neighbors_row_splits[0] = 0;
275 neighbors_count.data() + neighbors_count.size(),
276 query_neighbors_row_splits + 1);
278 int64_t num_indices = query_neighbors_row_splits[num_queries];
281 output_allocator.AllocIndices(&indices_ptr, num_indices);
283 if (return_distances)
284 output_allocator.AllocDistances(&distances_ptr, num_indices);
286 output_allocator.AllocDistances(&distances_ptr, 0);
288 std::fill(neighbors_count.begin(), neighbors_count.end(), 0);
292 tbb::blocked_range<size_t>(0, num_queries),
293 [&](
const tbb::blocked_range<size_t> &r) {
294 for (
size_t i = r.begin(); i != r.end(); ++i) {
295 int64_t start_idx = query_neighbors_row_splits[i];
296 std::copy(neighbors_indices[i].begin(),
297 neighbors_indices[i].end(),
298 &indices_ptr[start_idx]);
299 if (return_distances) {
300 std::transform(neighbors_distances[i].begin(),
301 neighbors_distances[i].end(),
302 &distances_ptr[start_idx], [&](T dist) {
304 if (normalize_distances) {
306 d /= (radii[i] * radii[i]);
318 template <
class T,
class TIndex,
class OUTPUT_ALLOCATOR,
int METRIC>
319 void _HybridSearchCPU(NanoFlannIndexHolderBase *holder,
323 const T *
const queries,
324 const size_t dimension,
327 bool ignore_query_point,
328 bool return_distances,
329 OUTPUT_ALLOCATOR &output_allocator) {
330 if (num_queries == 0 || num_points == 0 || holder ==
nullptr) {
331 TIndex *indices_ptr, *counts_ptr;
332 output_allocator.AllocIndices(&indices_ptr, 0);
333 output_allocator.AllocCounts(&counts_ptr, 0);
336 output_allocator.AllocDistances(&distances_ptr, 0);
340 T radius_squared = radius * radius;
341 TIndex *indices_ptr, *counts_ptr;
344 size_t num_indices =
static_cast<size_t>(max_knn) * num_queries;
345 output_allocator.AllocIndices(&indices_ptr, num_indices);
346 output_allocator.AllocDistances(&distances_ptr, num_indices);
347 output_allocator.AllocCounts(&counts_ptr, num_queries);
349 nanoflann::SearchParams params;
350 params.sorted =
true;
353 static_cast<NanoFlannIndexHolder<METRIC, T, TIndex> *
>(holder);
355 tbb::blocked_range<size_t>(0, num_queries),
356 [&](
const tbb::blocked_range<size_t> &r) {
357 std::vector<std::pair<TIndex, T>> ret_matches;
358 for (
size_t i = r.begin(); i != r.end(); ++i) {
359 size_t num_results = holder_->index_->radiusSearch(
360 &queries[i * dimension], radius_squared,
361 ret_matches, params);
362 ret_matches.resize(num_results);
364 TIndex count_i = static_cast<TIndex>(num_results);
365 count_i = count_i < max_knn ? count_i : max_knn;
366 counts_ptr[i] = count_i;
368 int neighbor_idx = 0;
369 for (auto it = ret_matches.begin();
370 it < ret_matches.end() && neighbor_idx < max_knn;
371 it++, neighbor_idx++) {
372 indices_ptr[i * max_knn + neighbor_idx] = it->first;
373 distances_ptr[i * max_knn + neighbor_idx] = it->second;
376 while (neighbor_idx < max_knn) {
377 indices_ptr[i * max_knn + neighbor_idx] = -1;
378 distances_ptr[i * max_knn + neighbor_idx] = 0;
401 template <
class T,
class TIndex>
402 std::unique_ptr<NanoFlannIndexHolderBase>
BuildKdTree(
size_t num_points,
407 #define FN_PARAMETERS num_points, points, dimension, &holder
409 #define CALL_TEMPLATE(METRIC) \
410 if (METRIC == metric) { \
411 _BuildKdTree<T, TIndex, METRIC>(FN_PARAMETERS); \
414 #define CALL_TEMPLATE2 \
421 #undef CALL_TEMPLATE2
424 return std::unique_ptr<NanoFlannIndexHolderBase>(holder);
479 template <
class T,
class TIndex,
class OUTPUT_ALLOCATOR>
481 int64_t *query_neighbors_row_splits,
485 const T *
const queries,
486 const size_t dimension,
489 bool ignore_query_point,
490 bool return_distances,
491 OUTPUT_ALLOCATOR &output_allocator) {
492 #define FN_PARAMETERS \
493 holder, query_neighbors_row_splits, num_points, points, num_queries, \
494 queries, dimension, knn, ignore_query_point, return_distances, \
497 #define CALL_TEMPLATE(METRIC) \
498 if (METRIC == metric) { \
499 _KnnSearchCPU<T, TIndex, OUTPUT_ALLOCATOR, METRIC>(FN_PARAMETERS); \
502 #define CALL_TEMPLATE2 \
509 #undef CALL_TEMPLATE2
573 template <
class T,
class TIndex,
class OUTPUT_ALLOCATOR>
575 int64_t *query_neighbors_row_splits,
579 const T *
const queries,
580 const size_t dimension,
581 const T *
const radii,
583 bool ignore_query_point,
584 bool return_distances,
585 bool normalize_distances,
587 OUTPUT_ALLOCATOR &output_allocator) {
588 #define FN_PARAMETERS \
589 holder, query_neighbors_row_splits, num_points, points, num_queries, \
590 queries, dimension, radii, ignore_query_point, return_distances, \
591 normalize_distances, sort, output_allocator
593 #define CALL_TEMPLATE(METRIC) \
594 if (METRIC == metric) { \
595 _RadiusSearchCPU<T, TIndex, OUTPUT_ALLOCATOR, METRIC>(FN_PARAMETERS); \
598 #define CALL_TEMPLATE2 \
605 #undef CALL_TEMPLATE2
661 template <
class T,
class TIndex,
class OUTPUT_ALLOCATOR>
666 const T *
const queries,
667 const size_t dimension,
671 bool ignore_query_point,
672 bool return_distances,
673 OUTPUT_ALLOCATOR &output_allocator) {
674 #define FN_PARAMETERS \
675 holder, num_points, points, num_queries, queries, dimension, radius, \
676 max_knn, ignore_query_point, return_distances, output_allocator
678 #define CALL_TEMPLATE(METRIC) \
679 if (METRIC == metric) { \
680 _HybridSearchCPU<T, TIndex, OUTPUT_ALLOCATOR, METRIC>(FN_PARAMETERS); \
683 #define CALL_TEMPLATE2 \
690 #undef CALL_TEMPLATE2
std::unique_ptr< NanoFlannIndexHolderBase > BuildKdTree(size_t num_points, const T *const points, size_t dimension, const Metric metric)
Definition: NanoFlannImpl.h:402
void RadiusSearchCPU(NanoFlannIndexHolderBase *holder, int64_t *query_neighbors_row_splits, size_t num_points, const T *const points, size_t num_queries, const T *const queries, const size_t dimension, const T *const radii, const Metric metric, bool ignore_query_point, bool return_distances, bool normalize_distances, bool sort, OUTPUT_ALLOCATOR &output_allocator)
Definition: NanoFlannImpl.h:574
void HybridSearchCPU(NanoFlannIndexHolderBase *holder, size_t num_points, const T *const points, size_t num_queries, const T *const queries, const size_t dimension, const T radius, const int max_knn, const Metric metric, bool ignore_query_point, bool return_distances, OUTPUT_ALLOCATOR &output_allocator)
Definition: NanoFlannImpl.h:662
void KnnSearchCPU(NanoFlannIndexHolderBase *holder, int64_t *query_neighbors_row_splits, size_t num_points, const T *const points, size_t num_queries, const T *const queries, const size_t dimension, int knn, const Metric metric, bool ignore_query_point, bool return_distances, OUTPUT_ALLOCATOR &output_allocator)
Definition: NanoFlannImpl.h:480
Metric
Supported metrics.
Definition: NeighborSearchCommon.h:19
@ L1
Definition: NeighborSearchCommon.h:19
@ L2
Definition: NeighborSearchCommon.h:19
void InclusivePrefixSum(const Tin *first, const Tin *last, Tout *out)
Definition: ParallelScan.h:71
Definition: PinholeCameraIntrinsic.cpp:16
This class is the Adaptor for connecting Open3D Tensor and NanoFlann.
Definition: NanoFlannImpl.h:28
const TReal *const data_ptr_
Definition: NanoFlannImpl.h:49
size_t dataset_size_
Definition: NanoFlannImpl.h:47
int dimension_
Definition: NanoFlannImpl.h:48
TReal kdtree_get_pt(const size_t idx, const size_t dim) const
Definition: NanoFlannImpl.h:38
DataAdaptor(size_t dataset_size, int dimension, const TReal *const data_ptr)
Definition: NanoFlannImpl.h:29
bool kdtree_get_bbox(BBOX &) const
Definition: NanoFlannImpl.h:43
size_t kdtree_get_point_count() const
Definition: NanoFlannImpl.h:36
nanoflann::L1_Adaptor< TReal, DataAdaptor, TReal > adaptor_t
Definition: NanoFlannImpl.h:63
nanoflann::L2_Adaptor< TReal, DataAdaptor, TReal > adaptor_t
Definition: NanoFlannImpl.h:58
Adaptor Selector.
Definition: NanoFlannImpl.h:54
Base struct for NanoFlann index holder.
Definition: NeighborSearchCommon.h:53
NanoFlann Index Holder.
Definition: NanoFlannImpl.h:26
std::unique_ptr< KDTree_t > index_
Definition: NanoFlannImpl.h:82
nanoflann::KDTreeSingleIndexAdaptor< typename SelectNanoflannAdaptor< METRIC >::adaptor_t, DataAdaptor, -1, TIndex > KDTree_t
typedef for KDtree.
Definition: NanoFlannImpl.h:72
std::unique_ptr< DataAdaptor > adaptor_
Definition: NanoFlannImpl.h:83
NanoFlannIndexHolder(size_t dataset_size, int dimension, const TReal *data_ptr)
Definition: NanoFlannImpl.h:74