Open3D (C++ API)  0.16.1
TransformImpl.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
29#include "open3d/core/Tensor.h"
30
31namespace open3d {
32namespace t {
33namespace geometry {
34namespace kernel {
35namespace transform {
36
37template <typename scalar_t>
39 const scalar_t* transformation_ptr, scalar_t* points_ptr) {
40 scalar_t x[4] = {transformation_ptr[0] * points_ptr[0] +
41 transformation_ptr[1] * points_ptr[1] +
42 transformation_ptr[2] * points_ptr[2] +
43 transformation_ptr[3],
44 transformation_ptr[4] * points_ptr[0] +
45 transformation_ptr[5] * points_ptr[1] +
46 transformation_ptr[6] * points_ptr[2] +
47 transformation_ptr[7],
48 transformation_ptr[8] * points_ptr[0] +
49 transformation_ptr[9] * points_ptr[1] +
50 transformation_ptr[10] * points_ptr[2] +
51 transformation_ptr[11],
52 transformation_ptr[12] * points_ptr[0] +
53 transformation_ptr[13] * points_ptr[1] +
54 transformation_ptr[14] * points_ptr[2] +
55 transformation_ptr[15]};
56
57 points_ptr[0] = x[0] / x[3];
58 points_ptr[1] = x[1] / x[3];
59 points_ptr[2] = x[2] / x[3];
60}
61
62template <typename scalar_t>
64 const scalar_t* transformation_ptr, scalar_t* normals_ptr) {
65 scalar_t x[3] = {transformation_ptr[0] * normals_ptr[0] +
66 transformation_ptr[1] * normals_ptr[1] +
67 transformation_ptr[2] * normals_ptr[2],
68 transformation_ptr[4] * normals_ptr[0] +
69 transformation_ptr[5] * normals_ptr[1] +
70 transformation_ptr[6] * normals_ptr[2],
71 transformation_ptr[8] * normals_ptr[0] +
72 transformation_ptr[9] * normals_ptr[1] +
73 transformation_ptr[10] * normals_ptr[2]};
74
75 normals_ptr[0] = x[0];
76 normals_ptr[1] = x[1];
77 normals_ptr[2] = x[2];
78}
79
80template <typename scalar_t>
82 const scalar_t* R_ptr, scalar_t* points_ptr, const scalar_t* center) {
83 scalar_t x[3] = {points_ptr[0] - center[0], points_ptr[1] - center[1],
84 points_ptr[2] - center[2]};
85
86 points_ptr[0] =
87 R_ptr[0] * x[0] + R_ptr[1] * x[1] + R_ptr[2] * x[2] + center[0];
88 points_ptr[1] =
89 R_ptr[3] * x[0] + R_ptr[4] * x[1] + R_ptr[5] * x[2] + center[1];
90 points_ptr[2] =
91 R_ptr[6] * x[0] + R_ptr[7] * x[1] + R_ptr[8] * x[2] + center[2];
92}
93
94template <typename scalar_t>
96 const scalar_t* R_ptr, scalar_t* normals_ptr) {
97 scalar_t x[3] = {R_ptr[0] * normals_ptr[0] + R_ptr[1] * normals_ptr[1] +
98 R_ptr[2] * normals_ptr[2],
99 R_ptr[3] * normals_ptr[0] + R_ptr[4] * normals_ptr[1] +
100 R_ptr[5] * normals_ptr[2],
101 R_ptr[6] * normals_ptr[0] + R_ptr[7] * normals_ptr[1] +
102 R_ptr[8] * normals_ptr[2]};
103
104 normals_ptr[0] = x[0];
105 normals_ptr[1] = x[1];
106 normals_ptr[2] = x[2];
107}
108
109#ifdef __CUDACC__
110void TransformPointsCUDA
111#else
113#endif
114 (const core::Tensor& transformation, core::Tensor& points) {
115 DISPATCH_FLOAT_DTYPE_TO_TEMPLATE(points.GetDtype(), [&]() {
116 scalar_t* points_ptr = points.GetDataPtr<scalar_t>();
117 const scalar_t* transformation_ptr =
118 transformation.GetDataPtr<scalar_t>();
119
120 core::ParallelFor(transformation.GetDevice(), points.GetLength(),
121 [=] OPEN3D_DEVICE(int64_t workload_idx) {
122 TransformPointsKernel(
123 transformation_ptr,
124 points_ptr + 3 * workload_idx);
125 });
126 });
127}
128
129#ifdef __CUDACC__
130void TransformNormalsCUDA
131#else
133#endif
134 (const core::Tensor& transformation, core::Tensor& normals) {
135 DISPATCH_FLOAT_DTYPE_TO_TEMPLATE(normals.GetDtype(), [&]() {
136 scalar_t* normals_ptr = normals.GetDataPtr<scalar_t>();
137 const scalar_t* transformation_ptr =
138 transformation.GetDataPtr<scalar_t>();
139
140 core::ParallelFor(transformation.GetDevice(), normals.GetLength(),
141 [=] OPEN3D_DEVICE(int64_t workload_idx) {
142 TransformNormalsKernel(
143 transformation_ptr,
144 normals_ptr + 3 * workload_idx);
145 });
146 });
147}
148
149#ifdef __CUDACC__
150void RotatePointsCUDA
151#else
153#endif
154 (const core::Tensor& R,
156 const core::Tensor& center) {
157 DISPATCH_FLOAT_DTYPE_TO_TEMPLATE(points.GetDtype(), [&]() {
158 scalar_t* points_ptr = points.GetDataPtr<scalar_t>();
159 const scalar_t* R_ptr = R.GetDataPtr<scalar_t>();
160 const scalar_t* center_ptr = center.GetDataPtr<scalar_t>();
161
162 core::ParallelFor(R.GetDevice(), points.GetLength(),
163 [=] OPEN3D_DEVICE(int64_t workload_idx) {
164 RotatePointsKernel(R_ptr,
165 points_ptr + 3 * workload_idx,
166 center_ptr);
167 });
168 });
169}
170
171#ifdef __CUDACC__
172void RotateNormalsCUDA
173#else
175#endif
176 (const core::Tensor& R, core::Tensor& normals) {
177 DISPATCH_FLOAT_DTYPE_TO_TEMPLATE(normals.GetDtype(), [&]() {
178 scalar_t* normals_ptr = normals.GetDataPtr<scalar_t>();
179 const scalar_t* R_ptr = R.GetDataPtr<scalar_t>();
180
181 core::ParallelFor(R.GetDevice(), normals.GetLength(),
182 [=] OPEN3D_DEVICE(int64_t workload_idx) {
183 RotateNormalsKernel(
184 R_ptr, normals_ptr + 3 * workload_idx);
185 });
186 });
187}
188
189} // namespace transform
190} // namespace kernel
191} // namespace geometry
192} // namespace t
193} // namespace open3d
Common CUDA utilities.
#define OPEN3D_HOST_DEVICE
Definition: CUDAUtils.h:63
#define OPEN3D_FORCE_INLINE
Definition: CUDAUtils.h:62
#define DISPATCH_FLOAT_DTYPE_TO_TEMPLATE(DTYPE,...)
Definition: Dispatch.h:96
Definition: Tensor.h:51
int points
Definition: FilePCD.cpp:73
void RotatePointsCPU(const core::Tensor &R, core::Tensor &points, const core::Tensor &center)
Definition: TransformImpl.h:154
void TransformPointsCPU(const core::Tensor &transformation, core::Tensor &points)
Definition: TransformImpl.h:114
void RotateNormalsCPU(const core::Tensor &R, core::Tensor &normals)
Definition: TransformImpl.h:176
void TransformNormalsCPU(const core::Tensor &transformation, core::Tensor &normals)
Definition: TransformImpl.h:134
OPEN3D_HOST_DEVICE OPEN3D_FORCE_INLINE void TransformNormalsKernel(const scalar_t *transformation_ptr, scalar_t *normals_ptr)
Definition: TransformImpl.h:63
OPEN3D_HOST_DEVICE OPEN3D_FORCE_INLINE void TransformPointsKernel(const scalar_t *transformation_ptr, scalar_t *points_ptr)
Definition: TransformImpl.h:38
OPEN3D_HOST_DEVICE OPEN3D_FORCE_INLINE void RotatePointsKernel(const scalar_t *R_ptr, scalar_t *points_ptr, const scalar_t *center)
Definition: TransformImpl.h:81
OPEN3D_HOST_DEVICE OPEN3D_FORCE_INLINE void RotateNormalsKernel(const scalar_t *R_ptr, scalar_t *normals_ptr)
Definition: TransformImpl.h:95
Definition: PinholeCameraIntrinsic.cpp:35