ACloudViewer  3.9.4
A Modern Library for 3D Data Processing
KnnIndex.h
Go to the documentation of this file.
1 // ----------------------------------------------------------------------------
2 // - CloudViewer: www.cloudViewer.org -
3 // ----------------------------------------------------------------------------
4 // Copyright (c) 2018-2024 www.cloudViewer.org
5 // SPDX-License-Identifier: MIT
6 // ----------------------------------------------------------------------------
7 
8 #pragma once
9 
10 #include <Logging.h>
11 
12 #include "cloudViewer/core/Dtype.h"
16 
17 namespace cloudViewer {
18 namespace core {
19 namespace nns {
20 
21 #ifdef BUILD_CUDA_MODULE
22 template <class T, class TIndex>
23 void KnnSearchCUDA(const Tensor& points,
24  const Tensor& points_row_splits,
25  const Tensor& queries,
26  const Tensor& queries_row_splits,
27  int knn,
28  Tensor& neighbors_index,
29  Tensor& neighbors_row_splits,
30  Tensor& neighbors_distance);
31 #endif
32 
33 class KnnIndex : public NNSIndex {
34 public:
35  KnnIndex();
36 
41  KnnIndex(const Tensor& dataset_points);
42  KnnIndex(const Tensor& dataset_points, const Dtype& index_dtype);
43  ~KnnIndex();
44  KnnIndex(const KnnIndex&) = delete;
45  KnnIndex& operator=(const KnnIndex&) = delete;
46 
47 public:
48  bool SetTensorData(const Tensor& dataset_points,
49  const Dtype& index_dtype = core::Int64) override;
50  bool SetTensorData(const Tensor& dataset_points,
51  const Tensor& points_row_splits,
52  const Dtype& index_dtype = core::Int64);
53  bool SetTensorData(const Tensor& dataset_points,
54  double radius,
55  const Dtype& index_dtype = core::Int64) override {
57  "[KnnIndex::SetTensorData with radius not implemented.");
58  }
59 
60  std::pair<Tensor, Tensor> SearchKnn(const Tensor& query_points,
61  int knn) const override;
62 
63  std::pair<Tensor, Tensor> SearchKnn(const Tensor& query_points,
64  const Tensor& queries_row_splits,
65  int knn) const;
66 
67  std::tuple<Tensor, Tensor, Tensor> SearchRadius(const Tensor& query_points,
68  const Tensor& radii,
69  bool sort) const override {
70  utility::LogError("KnnIndex::SearchRadius not implemented.");
71  }
72 
73  std::tuple<Tensor, Tensor, Tensor> SearchRadius(const Tensor& query_points,
74  const double radius,
75  bool sort) const override {
76  utility::LogError("KnnIndex::SearchRadius not implemented.");
77  }
78 
79  std::tuple<Tensor, Tensor, Tensor> SearchHybrid(
80  const Tensor& query_points,
81  const double radius,
82  const int max_knn) const override {
83  utility::LogError("KnnIndex::SearchHybrid not implemented.");
84  }
85 
86 protected:
88 };
89 
90 } // namespace nns
91 } // namespace core
92 } // namespace cloudViewer
cloudViewer
Generic file read and write utility for python interface.
Definition: CloudViewerConfig.cpp:12
cloudViewer::core::nns::KnnIndex::SetTensorData
bool SetTensorData(const Tensor &dataset_points, double radius, const Dtype &index_dtype=core::Int64) override
Definition: KnnIndex.h:53
cloudViewer::core::nns::NeighborSearchAllocator::NeighborsDistance
const Tensor & NeighborsDistance() const
Definition: NeighborSearchAllocator.h:67
Device.h
cloudViewer::core::make_pair
CLOUDVIEWER_HOST_DEVICE Pair< First, Second > make_pair(const First &_first, const Second &_second)
Definition: SlabTraits.h:49
INSTANTIATE_HYBRID
#define INSTANTIATE_HYBRID(T, TIndex)
Definition: FixedRadiusSearchOps.cpp:107
cloudViewer::core::Tensor::GetShape
SizeVector GetShape() const
Definition: Tensor.h:1127
FixedRadiusSearchImpl.h
INSTANTIATE_BUILD
#define INSTANTIATE_BUILD(T)
Definition: FixedRadiusSearchOps.cpp:91
cloudViewer::core::nns::KnnIndex::SetTensorData
bool SetTensorData(const Tensor &dataset_points, const Dtype &index_dtype=core::Int64) override
Definition: KnnIndex.cpp:32
cloudViewer::core::IsDevice::IsCUDA
bool IsCUDA() const
Definition: Device.h:99
INSTANTIATE_RADIUS
#define INSTANTIATE_RADIUS(T, TIndex)
Definition: FixedRadiusSearchOps.cpp:97
AssertTensorDevice
#define AssertTensorDevice(tensor,...)
Definition: TensorCheck.h:45
cloudViewer::core::Tensor::Contiguous
Tensor Contiguous() const
Definition: Tensor.cpp:772
cloudViewer::core::nns::NNSIndex::GetDevice
Device GetDevice() const
Definition: NNSIndex.cpp:26
NeighborSearchAllocator.h
cloudViewer::core::nns::NNSIndex::GetDtype
Dtype GetDtype() const
Definition: NNSIndex.cpp:24
NeighborSearchCommon.h
cloudViewer::core::Tensor
Definition: Tensor.h:32
cloudViewer::core::Dtype
Definition: Dtype.h:21
KNN_PARAMETERS
#define KNN_PARAMETERS
NNSIndex.h
cloudViewer::core::nns::KnnIndex::~KnnIndex
~KnnIndex()
Definition: KnnIndex.cpp:30
cloudViewer::core::Device::IsCUDA
bool IsCUDA() const
Returns true iff device type is CUDA.
Definition: Device.h:49
cloudViewer::core::Tensor::NumDims
int64_t NumDims() const
Definition: Tensor.h:1172
cloudViewer::core::nns::KnnIndex::SearchHybrid
std::tuple< Tensor, Tensor, Tensor > SearchHybrid(const Tensor &query_points, const double radius, const int max_knn) const override
Definition: KnnIndex.h:79
cloudViewer::core::nns::KnnIndex::SearchRadius
std::tuple< Tensor, Tensor, Tensor > SearchRadius(const Tensor &query_points, const Tensor &radii, bool sort) const override
Definition: KnnIndex.h:67
cloudViewer::core::nns::NNSIndex::GetDimension
int GetDimension() const
Definition: NNSIndex.cpp:14
cloudViewer::core::nns::KnnIndex
Definition: KnnIndex.h:33
cloudViewer::core::nns::NeighborSearchAllocator::NeighborsIndex
const Tensor & NeighborsIndex() const
Definition: NeighborSearchAllocator.h:65
cloudViewer::core::Int32
const Dtype Int32
Definition: Dtype.cpp:46
Dispatch.h
cloudViewer::core::Device
Definition: Device.h:18
cloudViewer::core::nns::NNSIndex
Definition: NNSIndex.h:21
TensorCheck.h
core
cloudViewer::core::nns::Metric
Metric
Supported metrics.
Definition: NeighborSearchCommon.h:19
cloudViewer::core::Float64
const Dtype Float64
Definition: Dtype.cpp:43
points
int points
Definition: FileIOFactory.cpp:144
cloudViewer::core::Float32
const Dtype Float32
Definition: Dtype.cpp:42
cloudViewer::core::nns::FixedRadiusSearchCPU
void FixedRadiusSearchCPU(const Tensor &points, const Tensor &queries, double radius, const Tensor &points_row_splits, const Tensor &queries_row_splits, const Tensor &hash_table_splits, const Tensor &hash_table_index, const Tensor &hash_table_cell_splits, const Metric metric, const bool ignore_query_point, const bool return_distances, const bool sort, Tensor &neighbors_index, Tensor &neighbors_row_splits, Tensor &neighbors_distance)
Definition: FixedRadiusSearchOps.cpp:39
cloudViewer::core::nns::KnnIndex::SearchKnn
std::pair< Tensor, Tensor > SearchKnn(const Tensor &query_points, int knn) const override
Definition: KnnIndex.cpp:80
LogError
#define LogError(...)
Definition: Logging.h:60
KnnIndex.h
cloudViewer::core::nns::BuildSpatialHashTableCPU
void BuildSpatialHashTableCPU(const Tensor &points, double radius, const Tensor &points_row_splits, const Tensor &hash_table_splits, Tensor &hash_table_index, Tensor &hash_table_cell_splits)
Definition: FixedRadiusSearchOps.cpp:22
cloudViewer::core::Tensor::Item
T Item() const
Definition: Tensor.h:618
AssertTensorDtype
#define AssertTensorDtype(tensor,...)
Definition: TensorCheck.h:21
cloudViewer::core::nns::NNSIndex::GetIndexDtype
Dtype GetIndexDtype() const
Definition: NNSIndex.cpp:28
cloudViewer::core::Tensor::Empty
static Tensor Empty(const SizeVector &shape, Dtype dtype, const Device &device=Device("CPU:0"))
Create a tensor with uninitialized values.
Definition: Tensor.cpp:400
cloudViewer::utility::nullopt
constexpr nullopt_t nullopt
Definition: Optional.h:136
Tensor.h
FixedRadiusIndex.h
cloudViewer::core::nns::KnnIndex::KnnIndex
KnnIndex()
Definition: KnnIndex.cpp:20
AssertTensorShape
#define AssertTensorShape(tensor,...)
Definition: TensorCheck.h:61
cloudViewer::core::nns::NNSIndex::index_dtype_
Dtype index_dtype_
Definition: NNSIndex.h:121
cloudViewer::core::nns::HybridSearchCPU
void HybridSearchCPU(const Tensor &points, const Tensor &queries, double radius, int max_knn, const Tensor &points_row_splits, const Tensor &queries_row_splits, const Tensor &hash_table_splits, const Tensor &hash_table_index, const Tensor &hash_table_cell_splits, const Metric metric, Tensor &neighbors_index, Tensor &neighbors_count, Tensor &neighbors_distance)
Definition: FixedRadiusSearchOps.cpp:75
AssertTensorDtypes
#define AssertTensorDtypes(tensor,...)
Definition: TensorCheck.h:33
cloudViewer::core::nns::NeighborSearchAllocator
Definition: NeighborSearchAllocator.h:19
radii
std::vector< PointCoordinateType > radii
Definition: qM3C2Tools.cpp:42
cloudViewer::core::Int64
const Dtype Int64
Definition: Dtype.cpp:47
Dtype.h
cloudViewer::core::nns::KnnIndex::operator=
KnnIndex & operator=(const KnnIndex &)=delete
cloudViewer::core::nns::KnnIndex::points_row_splits_
Tensor points_row_splits_
Definition: KnnIndex.h:87
DISPATCH_FLOAT_INT_DTYPE_TO_TEMPLATE
#define DISPATCH_FLOAT_INT_DTYPE_TO_TEMPLATE(FDTYPE, IDTYPE,...)
Definition: Dispatch.h:91
cloudViewer::core::Tensor::GetDataPtr
T * GetDataPtr()
Definition: Tensor.h:1144
cloudViewer::core::nns::KnnIndex::SearchRadius
std::tuple< Tensor, Tensor, Tensor > SearchRadius(const Tensor &query_points, const double radius, bool sort) const override
Definition: KnnIndex.h:73
cloudViewer::core::nns::NNSIndex::dataset_points_
Tensor dataset_points_
Definition: NNSIndex.h:120