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