Open3D (C++ API)  0.19.0
KnnSearchOpKernel.h
Go to the documentation of this file.
1 // ----------------------------------------------------------------------------
2 // - Open3D: www.open3d.org -
3 // ----------------------------------------------------------------------------
4 // Copyright (c) 2018-2024 www.open3d.org
5 // SPDX-License-Identifier: MIT
6 // ----------------------------------------------------------------------------
7 
8 #pragma once
9 
10 #include <cstdint>
11 
12 #include "../TensorFlowHelper.h"
13 #include "absl/status/status.h"
15 #include "tensorflow/core/framework/op.h"
16 #include "tensorflow/core/framework/op_kernel.h"
17 #include "tensorflow/core/lib/core/errors.h"
18 
20 // namespace for code that is common for all kernels
21 namespace knn_search_opkernel {
22 
23 class KnnSearchOpKernel : public tensorflow::OpKernel {
24 public:
25  explicit KnnSearchOpKernel(tensorflow::OpKernelConstruction* construction)
26  : OpKernel(construction) {
27  using namespace open3d::core::nns;
28  using namespace tensorflow;
29  std::string metric_str;
30  OP_REQUIRES_OK(construction,
31  construction->GetAttr("metric", &metric_str));
32  if (metric_str == "L1")
33  metric = L1;
34  else
35  metric = L2;
36 
37  OP_REQUIRES_OK(construction,
38  construction->GetAttr("ignore_query_point",
39  &ignore_query_point));
40 
41  OP_REQUIRES_OK(construction, construction->GetAttr("return_distances",
42  &return_distances));
43  }
44 
45  void Compute(tensorflow::OpKernelContext* context) override {
46  using namespace tensorflow;
47  static_assert(sizeof(int64_t) == sizeof(int64_t),
48  "int64_t type is not compatible");
49 
50  const Tensor& points = context->input(0);
51  const Tensor& queries = context->input(1);
52  const Tensor& k_tensor = context->input(2);
53  const TensorShape k_shape(k_tensor.shape());
54  OP_REQUIRES(context, k_shape.dims() == 0,
55  absl::InvalidArgumentError("k must be a rank 0 tensor"));
56  const int k = k_tensor.scalar<int32_t>()();
57  const Tensor& points_row_splits = context->input(3);
58  const Tensor& queries_row_splits = context->input(4);
59  {
60  using namespace open3d::ml::op_util;
61 
62  Dim num_points("num_points");
63  Dim num_queries("num_queries");
64  Dim batch_size("batch_size");
65  CHECK_SHAPE(context, points, num_points, 3);
66  CHECK_SHAPE(context, queries, num_queries, 3);
67  CHECK_SHAPE(context, points_row_splits, batch_size + 1);
68  CHECK_SHAPE(context, queries_row_splits, batch_size + 1);
69  }
70 
71  Tensor* query_neighbors_row_splits = 0;
72  TensorShape query_neighbors_row_splits_shape(
73  {queries.shape().dim_size(0) + 1});
74  OP_REQUIRES_OK(context, context->allocate_output(
75  1, query_neighbors_row_splits_shape,
76  &query_neighbors_row_splits));
77 
78  Kernel(context, points, queries, k, points_row_splits,
79  queries_row_splits, *query_neighbors_row_splits);
80  }
81 
82  virtual void Kernel(tensorflow::OpKernelContext* context,
83  const tensorflow::Tensor& points,
84  const tensorflow::Tensor& queries,
85  const int k,
86  const tensorflow::Tensor& points_row_splits,
87  const tensorflow::Tensor& queries_row_splits,
88  tensorflow::Tensor& query_neighbors_row_splits) = 0;
89 
90 protected:
92  bool ignore_query_point;
93  bool return_distances;
94 };
95 
96 } // namespace knn_search_opkernel
#define CHECK_SHAPE(tensor,...)
Definition: TorchHelper.h:190
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