Loading [MathJax]/extensions/TeX/AMSsymbols.js
Open3D (C++ API)  0.14.1
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
FixedRadiusSearchOpKernel.h
Go to the documentation of this file.
1 // ----------------------------------------------------------------------------
2 // - Open3D: www.open3d.org -
3 // ----------------------------------------------------------------------------
4 // The MIT License (MIT)
5 //
6 // Copyright (c) 2018-2021 www.open3d.org
7 //
8 // Permission is hereby granted, free of charge, to any person obtaining a copy
9 // of this software and associated documentation files (the "Software"), to deal
10 // in the Software without restriction, including without limitation the rights
11 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 // copies of the Software, and to permit persons to whom the Software is
13 // furnished to do so, subject to the following conditions:
14 //
15 // The above copyright notice and this permission notice shall be included in
16 // all copies or substantial portions of the Software.
17 //
18 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
24 // IN THE SOFTWARE.
25 // ----------------------------------------------------------------------------
26 
27 #pragma once
28 
29 #include "../TensorFlowHelper.h"
32 #include "tensorflow/core/framework/op.h"
33 #include "tensorflow/core/framework/op_kernel.h"
34 #include "tensorflow/core/lib/core/errors.h"
35 
37 // namespace for code that is common for all kernels
39 
40 // class for the allocator object
41 template <class T>
42 class OutputAllocator {
43 public:
44  OutputAllocator(tensorflow::OpKernelContext* context) : context(context) {}
45 
46  void AllocIndices(int32_t** ptr, size_t num) {
47  using namespace tensorflow;
48  *ptr = nullptr;
49  Tensor* tensor = 0;
50  TensorShape shape({int64_t(num)});
51  OP_REQUIRES_OK(context, context->allocate_output(0, shape, &tensor));
52  auto flat_tensor = tensor->flat<int32>();
53  static_assert(sizeof(int32) == sizeof(int32_t),
54  "int32 and int32_t not compatible");
55  *ptr = (int32_t*)flat_tensor.data();
56  }
57 
58  void AllocDistances(T** ptr, size_t num) {
59  using namespace tensorflow;
60  *ptr = nullptr;
61  Tensor* tensor = 0;
62  TensorShape shape({int64_t(num)});
63  OP_REQUIRES_OK(context, context->allocate_output(2, shape, &tensor));
64  auto flat_tensor = tensor->flat<T>();
65  *ptr = flat_tensor.data();
66  }
67 
68 private:
69  tensorflow::OpKernelContext* context;
70 };
71 
72 class FixedRadiusSearchOpKernel : public tensorflow::OpKernel {
73 public:
74  explicit FixedRadiusSearchOpKernel(
75  tensorflow::OpKernelConstruction* construction)
76  : OpKernel(construction) {
77  using namespace tensorflow;
78  using namespace open3d::core::nns;
79 
80  std::string metric_str;
81  OP_REQUIRES_OK(construction,
82  construction->GetAttr("metric", &metric_str));
83  if (metric_str == "L1")
84  metric = L1;
85  else if (metric_str == "L2")
86  metric = L2;
87  else
88  metric = Linf;
89 
90  OP_REQUIRES_OK(construction,
91  construction->GetAttr("ignore_query_point",
92  &ignore_query_point));
93 
94  OP_REQUIRES_OK(construction, construction->GetAttr("return_distances",
95  &return_distances));
96  }
97 
98  void Compute(tensorflow::OpKernelContext* context) override {
99  using namespace tensorflow;
100  static_assert(sizeof(int64) == sizeof(int64_t),
101  "int64 type is not compatible");
102 
103  const Tensor& points = context->input(0);
104  const Tensor& queries = context->input(1);
105 
106  const Tensor& radius = context->input(2);
107  OP_REQUIRES(context, TensorShapeUtils::IsScalar(radius.shape()),
108  errors::InvalidArgument("radius must be scalar, got shape ",
109  radius.shape().DebugString()));
110 
111  const Tensor& points_row_splits = context->input(3);
112  const Tensor& queries_row_splits = context->input(4);
113 
114  const Tensor& hash_table_splits = context->input(5);
115  const Tensor& hash_table_index = context->input(6);
116  const Tensor& hash_table_cell_splits = context->input(7);
117 
118  {
119  using namespace open3d::ml::op_util;
120 
121  Dim num_points("num_points");
122  Dim num_queries("num_queries");
123  Dim batch_size("batch_size");
124  Dim num_cells("num_cells");
125  CHECK_SHAPE(context, points, num_points, 3);
126  CHECK_SHAPE(context, hash_table_index, num_points);
127  CHECK_SHAPE(context, queries, num_queries, 3);
128  CHECK_SHAPE(context, points_row_splits, batch_size + 1);
129  CHECK_SHAPE(context, queries_row_splits, batch_size + 1);
130  CHECK_SHAPE(context, hash_table_splits, batch_size + 1);
131  CHECK_SHAPE(context, hash_table_cell_splits, num_cells + 1);
132  }
133  Tensor* query_neighbors_row_splits = 0;
134  TensorShape query_neighbors_row_splits_shape(
135  {queries.shape().dim_size(0) + 1});
136  OP_REQUIRES_OK(context, context->allocate_output(
137  1, query_neighbors_row_splits_shape,
138  &query_neighbors_row_splits));
139 
140  Kernel(context, points, queries, radius, points_row_splits,
141  queries_row_splits, hash_table_splits, hash_table_index,
142  hash_table_cell_splits, *query_neighbors_row_splits);
143  }
144 
145  virtual void Kernel(tensorflow::OpKernelContext* context,
146  const tensorflow::Tensor& points,
147  const tensorflow::Tensor& queries,
148  const tensorflow::Tensor& radius,
149  const tensorflow::Tensor& points_row_splits,
150  const tensorflow::Tensor& queries_row_splits,
151  const tensorflow::Tensor& hash_table_splits,
152  const tensorflow::Tensor& hash_table_index,
153  const tensorflow::Tensor& hash_table_cell_splits,
154  tensorflow::Tensor& query_neighbors_row_splits) = 0;
155 
156 protected:
158  bool ignore_query_point;
159  bool return_distances;
160 };
161 } // namespace fixed_radius_search_opkernel
Definition: NeighborSearchCommon.h:38
#define CHECK_SHAPE(tensor,...)
Definition: TorchHelper.h:205
Metric
Supported metrics.
Definition: NeighborSearchCommon.h:38
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:398
Class for dimensions for which the value should be inferred.
Definition: ShapeChecking.h:69
int points
Definition: FilePCD.cpp:73
ImGuiContext * context
Definition: Window.cpp:95
Definition: NeighborSearchCommon.h:38
Definition: NeighborSearchCommon.h:38
Definition: ShapeChecking.h:35
Definition: FaissIndex.cpp:47