Open3D (C++ API)  0.19.0
FixedRadiusSearchOpKernel.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"
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 fixed_radius_search_opkernel {
22 
23 // class for the allocator object
24 template <class T, class TIndex>
25 class OutputAllocator {
26 public:
27  OutputAllocator(tensorflow::OpKernelContext* context) : context(context) {}
28 
29  void AllocIndices(TIndex** ptr, size_t num) {
30  using namespace tensorflow;
31  *ptr = nullptr;
32  Tensor* tensor = 0;
33  TensorShape shape({int64_t(num)});
34  OP_REQUIRES_OK(context, context->allocate_output(0, shape, &tensor));
35 
36  auto flat_tensor = tensor->flat<TIndex>();
37  *ptr = (TIndex*)flat_tensor.data();
38  }
39 
40  void AllocDistances(T** ptr, size_t num) {
41  using namespace tensorflow;
42  *ptr = nullptr;
43  Tensor* tensor = 0;
44  TensorShape shape({int64_t(num)});
45  OP_REQUIRES_OK(context, context->allocate_output(2, shape, &tensor));
46  auto flat_tensor = tensor->flat<T>();
47  *ptr = flat_tensor.data();
48  }
49 
50 private:
51  tensorflow::OpKernelContext* context;
52 };
53 
54 class FixedRadiusSearchOpKernel : public tensorflow::OpKernel {
55 public:
56  explicit FixedRadiusSearchOpKernel(
57  tensorflow::OpKernelConstruction* construction)
58  : OpKernel(construction) {
59  using namespace tensorflow;
60  using namespace open3d::core::nns;
61 
62  std::string metric_str;
63  OP_REQUIRES_OK(construction,
64  construction->GetAttr("metric", &metric_str));
65  if (metric_str == "L1")
66  metric = L1;
67  else if (metric_str == "L2")
68  metric = L2;
69  else
70  metric = Linf;
71 
72  OP_REQUIRES_OK(construction,
73  construction->GetAttr("ignore_query_point",
74  &ignore_query_point));
75 
76  OP_REQUIRES_OK(construction, construction->GetAttr("return_distances",
77  &return_distances));
78  }
79 
80  void Compute(tensorflow::OpKernelContext* context) override {
81  using namespace tensorflow;
82  static_assert(sizeof(int64_t) == sizeof(int64_t),
83  "int64_t type is not compatible");
84 
85  const Tensor& points = context->input(0);
86  const Tensor& queries = context->input(1);
87 
88  const Tensor& radius = context->input(2);
89  OP_REQUIRES(context, TensorShapeUtils::IsScalar(radius.shape()),
90  absl::InvalidArgumentError(
91  std::string("radius must be scalar, got shape ") +
92  radius.shape().DebugString()));
93 
94  const Tensor& points_row_splits = context->input(3);
95  const Tensor& queries_row_splits = context->input(4);
96 
97  const Tensor& hash_table_splits = context->input(5);
98  const Tensor& hash_table_index = context->input(6);
99  const Tensor& hash_table_cell_splits = context->input(7);
100 
101  {
102  using namespace open3d::ml::op_util;
103 
104  Dim num_points("num_points");
105  Dim num_queries("num_queries");
106  Dim batch_size("batch_size");
107  Dim num_cells("num_cells");
108  CHECK_SHAPE(context, points, num_points, 3);
109  CHECK_SHAPE(context, hash_table_index, num_points);
110  CHECK_SHAPE(context, queries, num_queries, 3);
111  CHECK_SHAPE(context, points_row_splits, batch_size + 1);
112  CHECK_SHAPE(context, queries_row_splits, batch_size + 1);
113  CHECK_SHAPE(context, hash_table_splits, batch_size + 1);
114  CHECK_SHAPE(context, hash_table_cell_splits, num_cells + 1);
115  }
116  Tensor* query_neighbors_row_splits = 0;
117  TensorShape query_neighbors_row_splits_shape(
118  {queries.shape().dim_size(0) + 1});
119  OP_REQUIRES_OK(context, context->allocate_output(
120  1, query_neighbors_row_splits_shape,
121  &query_neighbors_row_splits));
122 
123  Kernel(context, points, queries, radius, points_row_splits,
124  queries_row_splits, hash_table_splits, hash_table_index,
125  hash_table_cell_splits, *query_neighbors_row_splits);
126  }
127 
128  virtual void Kernel(tensorflow::OpKernelContext* context,
129  const tensorflow::Tensor& points,
130  const tensorflow::Tensor& queries,
131  const tensorflow::Tensor& radius,
132  const tensorflow::Tensor& points_row_splits,
133  const tensorflow::Tensor& queries_row_splits,
134  const tensorflow::Tensor& hash_table_splits,
135  const tensorflow::Tensor& hash_table_index,
136  const tensorflow::Tensor& hash_table_cell_splits,
137  tensorflow::Tensor& query_neighbors_row_splits) = 0;
138 
139 protected:
141  bool ignore_query_point;
142  bool return_distances;
143 };
144 } // namespace fixed_radius_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
@ Linf
Definition: NeighborSearchCommon.h:19
@ L1
Definition: NeighborSearchCommon.h:19
@ L2
Definition: NeighborSearchCommon.h:19
Definition: ShapeChecking.h:16