Open3D (C++ API)  0.16.1
VoxelizeOpKernel.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 "open3d/ml/impl/misc/VoxelPooling.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 voxelize_opkernel {
38
39class OutputAllocator {
40public:
41 OutputAllocator(tensorflow::OpKernelContext* context) : context(context) {}
42
43 void AllocVoxelCoords(int32_t** ptr, int64_t rows, int64_t cols) {
44 using namespace tensorflow;
45 *ptr = nullptr;
46 Tensor* tensor = 0;
47 TensorShape shape({rows, cols});
48 OP_REQUIRES_OK(context, context->allocate_output(0, shape, &tensor));
49 auto flat_tensor = tensor->flat<int32_t>();
50 *ptr = flat_tensor.data();
51 }
52
53 void AllocVoxelPointIndices(int64_t** ptr, int64_t num) {
54 using namespace tensorflow;
55 *ptr = nullptr;
56 Tensor* tensor = 0;
57 TensorShape shape({num});
58 OP_REQUIRES_OK(context, context->allocate_output(1, shape, &tensor));
59 auto flat_tensor = tensor->flat<int64>();
60 *ptr = (int64_t*)flat_tensor.data();
61 }
62
63 void AllocVoxelPointRowSplits(int64_t** ptr, int64_t num) {
64 using namespace tensorflow;
65 *ptr = nullptr;
66 Tensor* tensor = 0;
67 TensorShape shape({num});
68 OP_REQUIRES_OK(context, context->allocate_output(2, shape, &tensor));
69 auto flat_tensor = tensor->flat<int64>();
70 *ptr = (int64_t*)flat_tensor.data();
71 }
72
73 void AllocVoxelBatchSplits(int64_t** ptr, int64_t num) {
74 using namespace tensorflow;
75 *ptr = nullptr;
76 Tensor* tensor = 0;
77 TensorShape shape({num});
78 OP_REQUIRES_OK(context, context->allocate_output(3, shape, &tensor));
79 auto flat_tensor = tensor->flat<int64>();
80 *ptr = (int64_t*)flat_tensor.data();
81 }
82
83private:
84 tensorflow::OpKernelContext* context;
85};
86
87// Base class with common code for the OpKernel implementations
88class VoxelizeOpKernel : public tensorflow::OpKernel {
89public:
90 explicit VoxelizeOpKernel(tensorflow::OpKernelConstruction* construction)
91 : OpKernel(construction) {
92 OP_REQUIRES_OK(construction,
93 construction->GetAttr("max_points_per_voxel",
94 &max_points_per_voxel));
95 OP_REQUIRES_OK(construction,
96 construction->GetAttr("max_voxels", &max_voxels));
97 }
98
99 void Compute(tensorflow::OpKernelContext* context) override {
100 using namespace tensorflow;
101 const Tensor& points = context->input(0);
102 const Tensor& row_splits = context->input(1);
103 const Tensor& voxel_size = context->input(2);
104 const Tensor& points_range_min = context->input(3);
105 const Tensor& points_range_max = context->input(4);
106
107 {
108 using namespace open3d::ml::op_util;
109 Dim num_points("num_points");
110 Dim ndim("ndim");
111 CHECK_SHAPE(context, points, num_points, ndim);
112 CHECK_SHAPE(context, voxel_size, ndim);
113 CHECK_SHAPE(context, points_range_min, ndim);
114 CHECK_SHAPE(context, points_range_max, ndim);
115 OP_REQUIRES(
116 context, ndim.value() > 0 && ndim.value() < 9,
117 errors::InvalidArgument(
118 "the number of dimensions must be in [1,..,8]"));
119 }
120
121 Kernel(context, points, row_splits, voxel_size, points_range_min,
122 points_range_max);
123 }
124
125 // Function with the device specific code
126 virtual void Kernel(tensorflow::OpKernelContext* context,
127 const tensorflow::Tensor& points,
128 const tensorflow::Tensor& row_splits,
129 const tensorflow::Tensor& voxel_size,
130 const tensorflow::Tensor& points_range_min,
131 const tensorflow::Tensor& points_range_max) = 0;
132
133protected:
134 tensorflow::int64 max_points_per_voxel;
135 tensorflow::int64 max_voxels;
136};
137
138} // namespace voxelize_opkernel
#define CHECK_SHAPE(tensor,...)
Definition: TorchHelper.h:205
ImGuiContext * context
Definition: Window.cpp:95
Class for dimensions for which the value should be inferred.
Definition: ShapeChecking.h:69
int points
Definition: FilePCD.cpp:73
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