Open3D (C++ API)  0.17.0
KnnSearchOpKernel.h
Go to the documentation of this file.
1 // ----------------------------------------------------------------------------
2 // - Open3D: www.open3d.org -
3 // ----------------------------------------------------------------------------
4 // Copyright (c) 2018-2023 www.open3d.org
5 // SPDX-License-Identifier: MIT
6 // ----------------------------------------------------------------------------
7 
8 #pragma once
9 
10 #include "../TensorFlowHelper.h"
12 #include "tensorflow/core/framework/op.h"
13 #include "tensorflow/core/framework/op_kernel.h"
14 #include "tensorflow/core/lib/core/errors.h"
15 
17 // namespace for code that is common for all kernels
18 namespace knn_search_opkernel {
19 
20 class KnnSearchOpKernel : public tensorflow::OpKernel {
21 public:
22  explicit KnnSearchOpKernel(tensorflow::OpKernelConstruction* construction)
23  : OpKernel(construction) {
24  using namespace open3d::core::nns;
25  using namespace tensorflow;
26  std::string metric_str;
27  OP_REQUIRES_OK(construction,
28  construction->GetAttr("metric", &metric_str));
29  if (metric_str == "L1")
30  metric = L1;
31  else
32  metric = L2;
33 
34  OP_REQUIRES_OK(construction,
35  construction->GetAttr("ignore_query_point",
36  &ignore_query_point));
37 
38  OP_REQUIRES_OK(construction, construction->GetAttr("return_distances",
39  &return_distances));
40  }
41 
42  void Compute(tensorflow::OpKernelContext* context) override {
43  using namespace tensorflow;
44  static_assert(sizeof(int64) == sizeof(int64_t),
45  "int64 type is not compatible");
46 
47  const Tensor& points = context->input(0);
48  const Tensor& queries = context->input(1);
49  const Tensor& k_tensor = context->input(2);
50  const TensorShape k_shape(k_tensor.shape());
51  OP_REQUIRES(context, k_shape.dims() == 0,
52  errors::InvalidArgument("k must be a rank 0 tensor"));
53  const int k = k_tensor.scalar<int32_t>()();
54  const Tensor& points_row_splits = context->input(3);
55  const Tensor& queries_row_splits = context->input(4);
56  {
57  using namespace open3d::ml::op_util;
58 
59  Dim num_points("num_points");
60  Dim num_queries("num_queries");
61  Dim batch_size("batch_size");
62  CHECK_SHAPE(context, points, num_points, 3);
63  CHECK_SHAPE(context, queries, num_queries, 3);
64  CHECK_SHAPE(context, points_row_splits, batch_size + 1);
65  CHECK_SHAPE(context, queries_row_splits, batch_size + 1);
66  }
67 
68  Tensor* query_neighbors_row_splits = 0;
69  TensorShape query_neighbors_row_splits_shape(
70  {queries.shape().dim_size(0) + 1});
71  OP_REQUIRES_OK(context, context->allocate_output(
72  1, query_neighbors_row_splits_shape,
73  &query_neighbors_row_splits));
74 
75  Kernel(context, points, queries, k, points_row_splits,
76  queries_row_splits, *query_neighbors_row_splits);
77  }
78 
79  virtual void Kernel(tensorflow::OpKernelContext* context,
80  const tensorflow::Tensor& points,
81  const tensorflow::Tensor& queries,
82  const int k,
83  const tensorflow::Tensor& points_row_splits,
84  const tensorflow::Tensor& queries_row_splits,
85  tensorflow::Tensor& query_neighbors_row_splits) = 0;
86 
87 protected:
89  bool ignore_query_point;
90  bool return_distances;
91 };
92 
93 } // namespace knn_search_opkernel
#define CHECK_SHAPE(tensor,...)
Definition: TorchHelper.h:186
ImGuiContext * context
Definition: Window.cpp:76
Class for dimensions for which the value should be inferred.
Definition: ShapeChecking.h:50
int points
Definition: FilePCD.cpp:54
Definition: FixedRadiusIndex.cpp:16
Metric
Supported metrics.
Definition: NeighborSearchCommon.h:19
@ L1
Definition: NeighborSearchCommon.h:19
@ L2
Definition: NeighborSearchCommon.h:19
const char const char value recording_handle imu_sample recording_handle uint8_t size_t data_size k4a_record_configuration_t config target_format k4a_capture_t capture_handle k4a_imu_sample_t imu_sample playback_handle k4a_logging_message_cb_t void min_level device_handle k4a_imu_sample_t int32_t
Definition: K4aPlugin.cpp:395
Definition: ShapeChecking.h:16