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