Open3D (C++ API)  0.19.0
RadiusSearchOpKernel.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 radius_search_opkernel {
22 
23 class RadiusSearchOpKernel : public tensorflow::OpKernel {
24 public:
25  explicit RadiusSearchOpKernel(
26  tensorflow::OpKernelConstruction* construction)
27  : OpKernel(construction) {
28  using namespace open3d::core::nns;
29  using namespace tensorflow;
30  std::string metric_str;
31  OP_REQUIRES_OK(construction,
32  construction->GetAttr("metric", &metric_str));
33  if (metric_str == "L1")
34  metric = L1;
35  else
36  metric = L2;
37 
38  OP_REQUIRES_OK(construction,
39  construction->GetAttr("ignore_query_point",
40  &ignore_query_point));
41 
42  OP_REQUIRES_OK(construction, construction->GetAttr("return_distances",
43  &return_distances));
44  OP_REQUIRES_OK(construction,
45  construction->GetAttr("normalize_distances",
46  &normalize_distances));
47  }
48 
49  void Compute(tensorflow::OpKernelContext* context) override {
50  using namespace tensorflow;
51  static_assert(sizeof(int64_t) == sizeof(int64_t),
52  "int64_t type is not compatible");
53 
54  const Tensor& points = context->input(0);
55  const Tensor& queries = context->input(1);
56  const Tensor& radii = context->input(2);
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, radii, num_queries);
68  CHECK_SHAPE(context, points_row_splits, batch_size + 1);
69  CHECK_SHAPE(context, queries_row_splits, batch_size + 1);
70  }
71 
72  Tensor* query_neighbors_row_splits = 0;
73  TensorShape query_neighbors_row_splits_shape(
74  {queries.shape().dim_size(0) + 1});
75  OP_REQUIRES_OK(context, context->allocate_output(
76  1, query_neighbors_row_splits_shape,
77  &query_neighbors_row_splits));
78 
79  Kernel(context, points, queries, radii, points_row_splits,
80  queries_row_splits, *query_neighbors_row_splits);
81  }
82 
83  virtual void Kernel(tensorflow::OpKernelContext* context,
84  const tensorflow::Tensor& points,
85  const tensorflow::Tensor& queries,
86  const tensorflow::Tensor& radius,
87  const tensorflow::Tensor& points_row_splits,
88  const tensorflow::Tensor& queries_row_splits,
89  tensorflow::Tensor& query_neighbors_row_splits) = 0;
90 
91 protected:
93  bool ignore_query_point;
94  bool return_distances;
95  bool normalize_distances;
96 };
97 
98 } // namespace 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
@ L1
Definition: NeighborSearchCommon.h:19
@ L2
Definition: NeighborSearchCommon.h:19
Definition: ShapeChecking.h:16