Open3D (C++ API)  0.20.0
Loading...
Searching...
No Matches
BuildSpatialHashTableOpKernel.h
Go to the documentation of this file.
1// ----------------------------------------------------------------------------
2// - Open3D: www.open3d.org -
3// ----------------------------------------------------------------------------
4// Copyright (c) 2018-2026 www.open3d.org
5// SPDX-License-Identifier: MIT
6// ----------------------------------------------------------------------------
7
8#pragma once
9
10#include <cstdint>
11
12#include "../TensorFlowHelper.h"
14#include "tensorflow/core/framework/op.h"
15#include "tensorflow/core/framework/op_kernel.h"
16#include "tensorflow/core/lib/core/errors.h"
17
18class BuildSpatialHashTableOpKernel : public tensorflow::OpKernel {
19public:
21 tensorflow::OpKernelConstruction* construction)
22 : OpKernel(construction) {
23 using namespace tensorflow;
24
25 OP_REQUIRES_OK(construction,
26 construction->GetAttr("max_hash_table_size",
28 }
29
30 void Compute(tensorflow::OpKernelContext* context) override {
31 using namespace tensorflow;
32 using namespace open3d::ml::op_util;
33
34 const Tensor& points = context->input(0);
35 const Tensor& radius = context->input(1);
36 OP_REQUIRES(context, TensorShapeUtils::IsScalar(radius.shape()),
37 absl::InvalidArgumentError(
38 std::string("radius must be scalar, got shape ") +
39 radius.shape().DebugString()));
40
41 const Tensor& points_row_splits = context->input(2);
42
43 const Tensor& hash_table_size_factor_tensor = context->input(3);
44 OP_REQUIRES(
45 context,
46 TensorShapeUtils::IsScalar(
47 hash_table_size_factor_tensor.shape()),
48 absl::InvalidArgumentError(
49 std::string("hash_table_size_factor must be scalar, "
50 "got shape ") +
51 hash_table_size_factor_tensor.shape().DebugString()));
52 const double hash_table_size_factor =
53 hash_table_size_factor_tensor.scalar<double>()();
54
55 Dim num_points("num_points");
56 Dim batch_size("batch_size");
57 CHECK_SHAPE(context, points, num_points, 3);
58 CHECK_SHAPE(context, points_row_splits, batch_size + 1);
59
60 std::vector<uint32_t> hash_table_splits(batch_size.value() + 1, 0);
61 for (int i = 0; i < batch_size.value(); ++i) {
62 int64_t num_points_i = points_row_splits.flat<int64_t>()(i + 1) -
63 points_row_splits.flat<int64_t>()(i);
64
65 int64_t hash_table_size = std::min<int64_t>(
66 std::max<int64_t>(hash_table_size_factor * num_points_i, 1),
68 hash_table_splits[i + 1] = hash_table_splits[i] + hash_table_size;
69 }
70
71 Tensor* hash_table_index = 0;
72 TensorShape hash_table_index_shape({num_points.value()});
73 OP_REQUIRES_OK(context,
74 context->allocate_output(0, hash_table_index_shape,
75 &hash_table_index));
76
77 Tensor* hash_table_cell_splits = 0;
78 TensorShape hash_table_cell_splits_shape(
79 {hash_table_splits.back() + 1});
80 OP_REQUIRES_OK(context,
81 context->allocate_output(1, hash_table_cell_splits_shape,
82 &hash_table_cell_splits));
83
84 Tensor* out_hash_table_splits = 0;
85 TensorShape out_hash_table_splits_shape({batch_size.value() + 1});
86 OP_REQUIRES_OK(context,
87 context->allocate_output(2, out_hash_table_splits_shape,
88 &out_hash_table_splits));
89 for (size_t i = 0; i < hash_table_splits.size(); ++i) {
90 out_hash_table_splits->flat<uint32_t>()(i) = hash_table_splits[i];
91 }
92
93 Kernel(context, points, radius, points_row_splits, hash_table_splits,
94 *hash_table_index, *hash_table_cell_splits);
95 }
96
97 virtual void Kernel(tensorflow::OpKernelContext* context,
98 const tensorflow::Tensor& points,
99 const tensorflow::Tensor& radius,
100 const tensorflow::Tensor& points_row_splits,
101 const std::vector<uint32_t>& hash_table_splits,
102 tensorflow::Tensor& hash_table_index,
103 tensorflow::Tensor& hash_table_cell_splits) = 0;
104
105protected:
107};
#define CHECK_SHAPE(tensor,...)
Definition TorchHelper.h:232
ImGuiContext * context
Definition Window.cpp:99
Definition BuildSpatialHashTableOpKernel.h:18
int max_hash_table_size
Definition BuildSpatialHashTableOpKernel.h:106
virtual void Kernel(tensorflow::OpKernelContext *context, const tensorflow::Tensor &points, const tensorflow::Tensor &radius, const tensorflow::Tensor &points_row_splits, const std::vector< uint32_t > &hash_table_splits, tensorflow::Tensor &hash_table_index, tensorflow::Tensor &hash_table_cell_splits)=0
void Compute(tensorflow::OpKernelContext *context) override
Definition BuildSpatialHashTableOpKernel.h:30
BuildSpatialHashTableOpKernel(tensorflow::OpKernelConstruction *construction)
Definition BuildSpatialHashTableOpKernel.h:20
Class for dimensions for which the value should be inferred.
Definition ShapeChecking.h:50
int64_t & value()
Definition ShapeChecking.h:70
int points
Definition FilePCD.cpp:55
Definition ShapeChecking.h:16