Open3D (C++ API)  0.11.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"
32 
33 // Forward declarations.
34 namespace nanoflann {
35 
36 template <class T, class DataSource, typename _DistanceType>
37 struct L2_Adaptor;
38 
39 template <class T, class DataSource, typename _DistanceType>
40 struct L1_Adaptor;
41 
42 template <typename Distance, class DatasetAdaptor, int DIM, typename IndexType>
44 
45 struct SearchParams;
46 }; // namespace nanoflann
47 
48 namespace open3d {
49 namespace core {
50 namespace nns {
51 
53 enum Metric { L1, L2, Linf };
54 
58 };
59 
61 template <int METRIC, class T>
64  struct DataAdaptor {
65  DataAdaptor(size_t dataset_size, int dimension, const T *const data_ptr)
66  : dataset_size_(dataset_size),
67  dimension_(dimension),
68  data_ptr_(data_ptr) {}
69 
70  inline size_t kdtree_get_point_count() const { return dataset_size_; }
71 
72  inline T kdtree_get_pt(const size_t idx, const size_t dim) const {
73  return data_ptr_[idx * dimension_ + dim];
74  }
75 
76  template <class BBOX>
77  bool kdtree_get_bbox(BBOX &) const {
78  return false;
79  }
80 
81  size_t dataset_size_ = 0;
82  int dimension_ = 0;
83  const T *const data_ptr_;
84  };
85 
87  template <int M, typename fake = void>
89 
90  template <typename fake>
91  struct SelectNanoflannAdaptor<L2, fake> {
93  };
94 
95  template <typename fake>
96  struct SelectNanoflannAdaptor<L1, fake> {
98  };
99 
103  DataAdaptor,
104  -1,
105  int64_t>
107 
108  NanoFlannIndexHolder(size_t dataset_size,
109  int dimension,
110  const T *data_ptr) {
111  adaptor_.reset(new DataAdaptor(dataset_size, dimension, data_ptr));
112  index_.reset(new KDTree_t(dimension, *adaptor_.get()));
113  index_->buildIndex();
114  }
115 
116  std::unique_ptr<KDTree_t> index_;
117  std::unique_ptr<DataAdaptor> adaptor_;
118 };
119 
124 public:
126  NanoFlannIndex();
127 
132  NanoFlannIndex(const Tensor &dataset_points);
133  ~NanoFlannIndex();
134  NanoFlannIndex(const NanoFlannIndex &) = delete;
135  NanoFlannIndex &operator=(const NanoFlannIndex &) = delete;
136 
137 public:
143  bool SetTensorData(const Tensor &dataset_points);
144 
153  std::pair<Tensor, Tensor> SearchKnn(const Tensor &query_points, int knn);
154 
165  std::tuple<Tensor, Tensor, Tensor> SearchRadius(const Tensor &query_points,
166  const Tensor &radii);
167 
178  std::tuple<Tensor, Tensor, Tensor> SearchRadius(const Tensor &query_points,
179  double radius);
180 
183  int GetDimension() const;
184 
187  size_t GetDatasetSize() const;
188 
191  Dtype GetDtype() const;
192 
193 protected:
195  std::unique_ptr<NanoFlannIndexHolderBase> holder_;
196 };
197 } // namespace nns
198 } // namespace core
199 } // namespace open3d
const T *const data_ptr_
Definition: NanoFlannIndex.h:83
NanoFlannIndexHolder(size_t dataset_size, int dimension, const T *data_ptr)
Definition: NanoFlannIndex.h:108
Definition: NanoFlannIndex.h:40
size_t kdtree_get_point_count() const
Definition: NanoFlannIndex.h:70
virtual ~NanoFlannIndexHolderBase()
Definition: NanoFlannIndex.h:57
nanoflann::L1_Adaptor< T, DataAdaptor, T > adaptor_t
Definition: NanoFlannIndex.h:97
Definition: NanoFlannIndex.h:53
Base struct for Index holder.
Definition: NanoFlannIndex.h:56
Definition: Dtype.h:39
Metric
Distance metric enum.
Definition: NanoFlannIndex.h:53
Definition: NanoFlannIndex.h:123
NanoFlann Index Holder.
Definition: NanoFlannIndex.h:62
Definition: NanoFlannIndex.h:53
T kdtree_get_pt(const size_t idx, const size_t dim) const
Definition: NanoFlannIndex.h:72
Adaptor Selector.
Definition: NanoFlannIndex.h:88
This class is the Adaptor for connecting Open3D Tensor and NanoFlann.
Definition: NanoFlannIndex.h:64
bool kdtree_get_bbox(BBOX &) const
Definition: NanoFlannIndex.h:77
Tensor dataset_points_
Definition: NanoFlannIndex.h:194
std::unique_ptr< DataAdaptor > adaptor_
Definition: NanoFlannIndex.h:117
std::unique_ptr< NanoFlannIndexHolderBase > holder_
Definition: NanoFlannIndex.h:195
Definition: NanoFlannIndex.h:43
nanoflann::KDTreeSingleIndexAdaptor< typename SelectNanoflannAdaptor< METRIC >::adaptor_t, DataAdaptor, -1, int64_t > KDTree_t
typedef for KDtree.
Definition: NanoFlannIndex.h:106
Definition: PinholeCameraIntrinsic.cpp:35
Definition: NanoFlannIndex.h:37
Definition: Tensor.h:48
DataAdaptor(size_t dataset_size, int dimension, const T *const data_ptr)
Definition: NanoFlannIndex.h:65
Definition: NanoFlannIndex.h:53
std::unique_ptr< KDTree_t > index_
Definition: NanoFlannIndex.h:116
nanoflann::L2_Adaptor< T, DataAdaptor, T > adaptor_t
Definition: NanoFlannIndex.h:92
Definition: NanoFlannIndex.h:34