Open3D (C++ API)  0.11.0
KnnSearchOpKernel.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
33 namespace knn_search_opkernel {
34 
35 class KnnSearchOpKernel : public tensorflow::OpKernel {
36 public:
37  explicit KnnSearchOpKernel(tensorflow::OpKernelConstruction* construction)
38  : OpKernel(construction) {
39  using namespace open3d::ml::impl;
40  using namespace tensorflow;
41  std::string metric_str;
42  OP_REQUIRES_OK(construction,
43  construction->GetAttr("metric", &metric_str));
44  if (metric_str == "L1")
45  metric = L1;
46  else
47  metric = L2;
48 
49  OP_REQUIRES_OK(construction,
50  construction->GetAttr("ignore_query_point",
51  &ignore_query_point));
52 
53  OP_REQUIRES_OK(construction, construction->GetAttr("return_distances",
54  &return_distances));
55  }
56 
57  void Compute(tensorflow::OpKernelContext* context) override {
58  using namespace tensorflow;
59  static_assert(sizeof(int64) == sizeof(int64_t),
60  "int64 type is not compatible");
61 
62  const Tensor& points = context->input(0);
63  const Tensor& queries = context->input(1);
64  const Tensor& k_tensor = context->input(2);
65  const TensorShape k_shape(k_tensor.shape());
66  OP_REQUIRES(context, k_shape.dims() == 0,
67  errors::InvalidArgument("k must be a rank 0 tensor"));
68  const int k = k_tensor.scalar<int32_t>()();
69  const Tensor& points_row_splits = context->input(3);
70  const Tensor& queries_row_splits = context->input(4);
71  {
72  using namespace open3d::ml::op_util;
73 
74  Dim num_points("num_points");
75  Dim num_queries("num_queries");
76  Dim batch_size("batch_size");
77  CHECK_SHAPE(context, points, num_points, 3);
78  CHECK_SHAPE(context, queries, num_queries, 3);
79  CHECK_SHAPE(context, points_row_splits, batch_size + 1);
80  CHECK_SHAPE(context, queries_row_splits, batch_size + 1);
81  }
82 
83  Tensor* query_neighbors_row_splits = 0;
84  TensorShape query_neighbors_row_splits_shape(
85  {queries.shape().dim_size(0) + 1});
86  OP_REQUIRES_OK(context, context->allocate_output(
87  1, query_neighbors_row_splits_shape,
88  &query_neighbors_row_splits));
89 
90  Kernel(context, points, queries, k, points_row_splits,
91  queries_row_splits, *query_neighbors_row_splits);
92  }
93 
94  virtual void Kernel(tensorflow::OpKernelContext* context,
95  const tensorflow::Tensor& points,
96  const tensorflow::Tensor& queries,
97  const int k,
98  const tensorflow::Tensor& points_row_splits,
99  const tensorflow::Tensor& queries_row_splits,
100  tensorflow::Tensor& query_neighbors_row_splits) = 0;
101 
102 protected:
104  bool ignore_query_point;
105  bool return_distances;
106 };
107 
108 } // namespace knn_search_opkernel
Definition: NanoFlannIndex.h:53
#define CHECK_SHAPE(tensor,...)
Definition: TorchHelper.h:204
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:395
Class for dimensions for which the value should be inferred.
Definition: ShapeChecking.h:69
Definition: NanoFlannIndex.h:53
Definition: ContinuousConv.h:35
int points
Definition: FilePCD.cpp:73
Definition: ShapeChecking.h:35