Open3D (C++ API)  0.12.0
NanoFlannIndex.h
Go to the documentation of this file.
1 // ----------------------------------------------------------------------------
2 // - Open3D: www.open3d.org -
3 // ----------------------------------------------------------------------------
4 // The MIT License (MIT)
5 //
6 // Copyright (c) 2018 www.open3d.org
7 //
8 // Permission is hereby granted, free of charge, to any person obtaining a copy
9 // of this software and associated documentation files (the "Software"), to deal
10 // in the Software without restriction, including without limitation the rights
11 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 // copies of the Software, and to permit persons to whom the Software is
13 // furnished to do so, subject to the following conditions:
14 //
15 // The above copyright notice and this permission notice shall be included in
16 // all copies or substantial portions of the Software.
17 //
18 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
24 // IN THE SOFTWARE.
25 // ----------------------------------------------------------------------------
26 
27 #pragma once
28 
29 #include <vector>
30 
31 #include "open3d/core/Tensor.h"
33 #include "open3d/utility/Console.h"
34 
35 // Forward declarations.
36 namespace nanoflann {
37 
38 template <class T, class DataSource, typename _DistanceType>
39 struct L2_Adaptor;
40 
41 template <class T, class DataSource, typename _DistanceType>
42 struct L1_Adaptor;
43 
44 template <typename Distance, class DatasetAdaptor, int DIM, typename IndexType>
46 
47 struct SearchParams;
48 }; // namespace nanoflann
49 
50 namespace open3d {
51 namespace core {
52 namespace nns {
53 
55 enum Metric { L1, L2, Linf };
56 
60 };
61 
63 template <int METRIC, class T>
66  struct DataAdaptor {
67  DataAdaptor(size_t dataset_size, int dimension, const T *const data_ptr)
68  : dataset_size_(dataset_size),
69  dimension_(dimension),
70  data_ptr_(data_ptr) {}
71 
72  inline size_t kdtree_get_point_count() const { return dataset_size_; }
73 
74  inline T kdtree_get_pt(const size_t idx, const size_t dim) const {
75  return data_ptr_[idx * dimension_ + dim];
76  }
77 
78  template <class BBOX>
79  bool kdtree_get_bbox(BBOX &) const {
80  return false;
81  }
82 
83  size_t dataset_size_ = 0;
84  int dimension_ = 0;
85  const T *const data_ptr_;
86  };
87 
89  template <int M, typename fake = void>
91 
92  template <typename fake>
93  struct SelectNanoflannAdaptor<L2, fake> {
95  };
96 
97  template <typename fake>
98  struct SelectNanoflannAdaptor<L1, fake> {
100  };
101 
105  DataAdaptor,
106  -1,
107  int64_t>
109 
110  NanoFlannIndexHolder(size_t dataset_size,
111  int dimension,
112  const T *data_ptr) {
113  adaptor_.reset(new DataAdaptor(dataset_size, dimension, data_ptr));
114  index_.reset(new KDTree_t(dimension, *adaptor_.get()));
115  index_->buildIndex();
116  }
117 
118  std::unique_ptr<KDTree_t> index_;
119  std::unique_ptr<DataAdaptor> adaptor_;
120 };
121 
125 class NanoFlannIndex : public NNSIndex {
126 public:
128  NanoFlannIndex();
129 
134  NanoFlannIndex(const Tensor &dataset_points);
135  ~NanoFlannIndex();
136  NanoFlannIndex(const NanoFlannIndex &) = delete;
137  NanoFlannIndex &operator=(const NanoFlannIndex &) = delete;
138 
139 public:
140  bool SetTensorData(const Tensor &dataset_points) override;
141 
142  bool SetTensorData(const Tensor &dataset_points, double radius) override {
144  "NanoFlannIndex::SetTensorData with radius not implemented.");
145  }
146 
147  std::pair<Tensor, Tensor> SearchKnn(const Tensor &query_points,
148  int knn) const override;
149 
150  std::tuple<Tensor, Tensor, Tensor> SearchRadius(
151  const Tensor &query_points, const Tensor &radii) const override;
152 
153  std::tuple<Tensor, Tensor, Tensor> SearchRadius(
154  const Tensor &query_points, double radius) const override;
155 
156  std::pair<Tensor, Tensor> SearchHybrid(const Tensor &query_points,
157  float radius,
158  int max_knn) const override;
159 
160 protected:
161  // Tensor dataset_points_;
162  std::unique_ptr<NanoFlannIndexHolderBase> holder_;
163 };
164 } // namespace nns
165 } // namespace core
166 } // namespace open3d
const T *const data_ptr_
Definition: NanoFlannIndex.h:85
NanoFlannIndexHolder(size_t dataset_size, int dimension, const T *data_ptr)
Definition: NanoFlannIndex.h:110
Definition: NanoFlannIndex.h:42
size_t kdtree_get_point_count() const
Definition: NanoFlannIndex.h:72
virtual ~NanoFlannIndexHolderBase()
Definition: NanoFlannIndex.h:59
nanoflann::L1_Adaptor< T, DataAdaptor, T > adaptor_t
Definition: NanoFlannIndex.h:99
Base struct for Index holder.
Definition: NanoFlannIndex.h:58
bool SetTensorData(const Tensor &dataset_points, double radius) override
Definition: NanoFlannIndex.h:142
Definition: NNSIndex.h:40
void LogError(const char *format, const Args &... args)
Definition: Console.h:176
Metric
Distance metric enum.
Definition: NanoFlannIndex.h:55
Definition: NanoFlannIndex.h:125
Definition: NanoFlannIndex.h:55
Definition: NanoFlannIndex.h:55
NanoFlann Index Holder.
Definition: NanoFlannIndex.h:64
T kdtree_get_pt(const size_t idx, const size_t dim) const
Definition: NanoFlannIndex.h:74
Adaptor Selector.
Definition: NanoFlannIndex.h:90
This class is the Adaptor for connecting Open3D Tensor and NanoFlann.
Definition: NanoFlannIndex.h:66
bool kdtree_get_bbox(BBOX &) const
Definition: NanoFlannIndex.h:79
std::unique_ptr< DataAdaptor > adaptor_
Definition: NanoFlannIndex.h:119
std::unique_ptr< NanoFlannIndexHolderBase > holder_
Definition: NanoFlannIndex.h:162
Definition: NanoFlannIndex.h:55
Definition: NanoFlannIndex.h:45
nanoflann::KDTreeSingleIndexAdaptor< typename SelectNanoflannAdaptor< METRIC >::adaptor_t, DataAdaptor, -1, int64_t > KDTree_t
typedef for KDtree.
Definition: NanoFlannIndex.h:108
Definition: PinholeCameraIntrinsic.cpp:35
Definition: NanoFlannIndex.h:39
Definition: Tensor.h:48
DataAdaptor(size_t dataset_size, int dimension, const T *const data_ptr)
Definition: NanoFlannIndex.h:67
std::unique_ptr< KDTree_t > index_
Definition: NanoFlannIndex.h:118
nanoflann::L2_Adaptor< T, DataAdaptor, T > adaptor_t
Definition: NanoFlannIndex.h:94
Definition: NanoFlannIndex.h:36