Open3D (C++ API)  0.16.1
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
38namespace fixed_radius_search_opkernel {
39
40// class for the allocator object
41template <class T, class TIndex>
42class OutputAllocator {
43public:
44 OutputAllocator(tensorflow::OpKernelContext* context) : context(context) {}
45
46 void AllocIndices(TIndex** 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
53 auto flat_tensor = tensor->flat<TIndex>();
54 *ptr = (TIndex*)flat_tensor.data();
55 }
56
57 void AllocDistances(T** ptr, size_t num) {
58 using namespace tensorflow;
59 *ptr = nullptr;
60 Tensor* tensor = 0;
61 TensorShape shape({int64_t(num)});
62 OP_REQUIRES_OK(context, context->allocate_output(2, shape, &tensor));
63 auto flat_tensor = tensor->flat<T>();
64 *ptr = flat_tensor.data();
65 }
66
67private:
68 tensorflow::OpKernelContext* context;
69};
70
71class FixedRadiusSearchOpKernel : public tensorflow::OpKernel {
72public:
73 explicit FixedRadiusSearchOpKernel(
74 tensorflow::OpKernelConstruction* construction)
75 : OpKernel(construction) {
76 using namespace tensorflow;
77 using namespace open3d::core::nns;
78
79 std::string metric_str;
80 OP_REQUIRES_OK(construction,
81 construction->GetAttr("metric", &metric_str));
82 if (metric_str == "L1")
83 metric = L1;
84 else if (metric_str == "L2")
85 metric = L2;
86 else
87 metric = Linf;
88
89 OP_REQUIRES_OK(construction,
90 construction->GetAttr("ignore_query_point",
91 &ignore_query_point));
92
93 OP_REQUIRES_OK(construction, construction->GetAttr("return_distances",
94 &return_distances));
95 }
96
97 void Compute(tensorflow::OpKernelContext* context) override {
98 using namespace tensorflow;
99 static_assert(sizeof(int64) == sizeof(int64_t),
100 "int64 type is not compatible");
101
102 const Tensor& points = context->input(0);
103 const Tensor& queries = context->input(1);
104
105 const Tensor& radius = context->input(2);
106 OP_REQUIRES(context, TensorShapeUtils::IsScalar(radius.shape()),
107 errors::InvalidArgument("radius must be scalar, got shape ",
108 radius.shape().DebugString()));
109
110 const Tensor& points_row_splits = context->input(3);
111 const Tensor& queries_row_splits = context->input(4);
112
113 const Tensor& hash_table_splits = context->input(5);
114 const Tensor& hash_table_index = context->input(6);
115 const Tensor& hash_table_cell_splits = context->input(7);
116
117 {
118 using namespace open3d::ml::op_util;
119
120 Dim num_points("num_points");
121 Dim num_queries("num_queries");
122 Dim batch_size("batch_size");
123 Dim num_cells("num_cells");
124 CHECK_SHAPE(context, points, num_points, 3);
125 CHECK_SHAPE(context, hash_table_index, num_points);
126 CHECK_SHAPE(context, queries, num_queries, 3);
127 CHECK_SHAPE(context, points_row_splits, batch_size + 1);
128 CHECK_SHAPE(context, queries_row_splits, batch_size + 1);
129 CHECK_SHAPE(context, hash_table_splits, batch_size + 1);
130 CHECK_SHAPE(context, hash_table_cell_splits, num_cells + 1);
131 }
132 Tensor* query_neighbors_row_splits = 0;
133 TensorShape query_neighbors_row_splits_shape(
134 {queries.shape().dim_size(0) + 1});
135 OP_REQUIRES_OK(context, context->allocate_output(
136 1, query_neighbors_row_splits_shape,
137 &query_neighbors_row_splits));
138
139 Kernel(context, points, queries, radius, points_row_splits,
140 queries_row_splits, hash_table_splits, hash_table_index,
141 hash_table_cell_splits, *query_neighbors_row_splits);
142 }
143
144 virtual void Kernel(tensorflow::OpKernelContext* context,
145 const tensorflow::Tensor& points,
146 const tensorflow::Tensor& queries,
147 const tensorflow::Tensor& radius,
148 const tensorflow::Tensor& points_row_splits,
149 const tensorflow::Tensor& queries_row_splits,
150 const tensorflow::Tensor& hash_table_splits,
151 const tensorflow::Tensor& hash_table_index,
152 const tensorflow::Tensor& hash_table_cell_splits,
153 tensorflow::Tensor& query_neighbors_row_splits) = 0;
154
155protected:
157 bool ignore_query_point;
158 bool return_distances;
159};
160} // namespace fixed_radius_search_opkernel
#define CHECK_SHAPE(tensor,...)
Definition: TorchHelper.h:205
ImGuiContext * context
Definition: Window.cpp:95
Definition: Tensor.h:51
Class for dimensions for which the value should be inferred.
Definition: ShapeChecking.h:69
int points
Definition: FilePCD.cpp:73
Definition: FixedRadiusIndex.cpp:35
Metric
Supported metrics.
Definition: NeighborSearchCommon.h:38
@ Linf
Definition: NeighborSearchCommon.h:38
@ L1
Definition: NeighborSearchCommon.h:38
@ L2
Definition: NeighborSearchCommon.h:38
Definition: ShapeChecking.h:35