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