Open3D (C++ API)  0.18.0+5c982c7
TransformImpl.h
Go to the documentation of this file.
1 // ----------------------------------------------------------------------------
2 // - Open3D: www.open3d.org -
3 // ----------------------------------------------------------------------------
4 // Copyright (c) 2018-2023 www.open3d.org
5 // SPDX-License-Identifier: MIT
6 // ----------------------------------------------------------------------------
7 
9 #include "open3d/core/Dispatch.h"
10 #include "open3d/core/Tensor.h"
11 
12 namespace open3d {
13 namespace t {
14 namespace geometry {
15 namespace kernel {
16 namespace transform {
17 
18 template <typename scalar_t>
20  const scalar_t* transformation_ptr, scalar_t* points_ptr) {
21  scalar_t x[4] = {transformation_ptr[0] * points_ptr[0] +
22  transformation_ptr[1] * points_ptr[1] +
23  transformation_ptr[2] * points_ptr[2] +
24  transformation_ptr[3],
25  transformation_ptr[4] * points_ptr[0] +
26  transformation_ptr[5] * points_ptr[1] +
27  transformation_ptr[6] * points_ptr[2] +
28  transformation_ptr[7],
29  transformation_ptr[8] * points_ptr[0] +
30  transformation_ptr[9] * points_ptr[1] +
31  transformation_ptr[10] * points_ptr[2] +
32  transformation_ptr[11],
33  transformation_ptr[12] * points_ptr[0] +
34  transformation_ptr[13] * points_ptr[1] +
35  transformation_ptr[14] * points_ptr[2] +
36  transformation_ptr[15]};
37 
38  points_ptr[0] = x[0] / x[3];
39  points_ptr[1] = x[1] / x[3];
40  points_ptr[2] = x[2] / x[3];
41 }
42 
43 template <typename scalar_t>
45  const scalar_t* transformation_ptr, scalar_t* normals_ptr) {
46  scalar_t x[3] = {transformation_ptr[0] * normals_ptr[0] +
47  transformation_ptr[1] * normals_ptr[1] +
48  transformation_ptr[2] * normals_ptr[2],
49  transformation_ptr[4] * normals_ptr[0] +
50  transformation_ptr[5] * normals_ptr[1] +
51  transformation_ptr[6] * normals_ptr[2],
52  transformation_ptr[8] * normals_ptr[0] +
53  transformation_ptr[9] * normals_ptr[1] +
54  transformation_ptr[10] * normals_ptr[2]};
55 
56  normals_ptr[0] = x[0];
57  normals_ptr[1] = x[1];
58  normals_ptr[2] = x[2];
59 }
60 
61 template <typename scalar_t>
63  const scalar_t* R_ptr, scalar_t* points_ptr, const scalar_t* center) {
64  scalar_t x[3] = {points_ptr[0] - center[0], points_ptr[1] - center[1],
65  points_ptr[2] - center[2]};
66 
67  points_ptr[0] =
68  R_ptr[0] * x[0] + R_ptr[1] * x[1] + R_ptr[2] * x[2] + center[0];
69  points_ptr[1] =
70  R_ptr[3] * x[0] + R_ptr[4] * x[1] + R_ptr[5] * x[2] + center[1];
71  points_ptr[2] =
72  R_ptr[6] * x[0] + R_ptr[7] * x[1] + R_ptr[8] * x[2] + center[2];
73 }
74 
75 template <typename scalar_t>
77  const scalar_t* R_ptr, scalar_t* normals_ptr) {
78  scalar_t x[3] = {R_ptr[0] * normals_ptr[0] + R_ptr[1] * normals_ptr[1] +
79  R_ptr[2] * normals_ptr[2],
80  R_ptr[3] * normals_ptr[0] + R_ptr[4] * normals_ptr[1] +
81  R_ptr[5] * normals_ptr[2],
82  R_ptr[6] * normals_ptr[0] + R_ptr[7] * normals_ptr[1] +
83  R_ptr[8] * normals_ptr[2]};
84 
85  normals_ptr[0] = x[0];
86  normals_ptr[1] = x[1];
87  normals_ptr[2] = x[2];
88 }
89 
90 #ifdef __CUDACC__
91 void TransformPointsCUDA
92 #else
94 #endif
95  (const core::Tensor& transformation, core::Tensor& points) {
96  DISPATCH_FLOAT_DTYPE_TO_TEMPLATE(points.GetDtype(), [&]() {
97  scalar_t* points_ptr = points.GetDataPtr<scalar_t>();
98  const scalar_t* transformation_ptr =
99  transformation.GetDataPtr<scalar_t>();
100 
101  core::ParallelFor(transformation.GetDevice(), points.GetLength(),
102  [=] OPEN3D_DEVICE(int64_t workload_idx) {
103  TransformPointsKernel(
104  transformation_ptr,
105  points_ptr + 3 * workload_idx);
106  });
107  });
108 }
109 
110 #ifdef __CUDACC__
111 void TransformNormalsCUDA
112 #else
114 #endif
115  (const core::Tensor& transformation, core::Tensor& normals) {
116  DISPATCH_FLOAT_DTYPE_TO_TEMPLATE(normals.GetDtype(), [&]() {
117  scalar_t* normals_ptr = normals.GetDataPtr<scalar_t>();
118  const scalar_t* transformation_ptr =
119  transformation.GetDataPtr<scalar_t>();
120 
121  core::ParallelFor(transformation.GetDevice(), normals.GetLength(),
122  [=] OPEN3D_DEVICE(int64_t workload_idx) {
123  TransformNormalsKernel(
124  transformation_ptr,
125  normals_ptr + 3 * workload_idx);
126  });
127  });
128 }
129 
130 #ifdef __CUDACC__
131 void RotatePointsCUDA
132 #else
134 #endif
135  (const core::Tensor& R,
137  const core::Tensor& center) {
138  DISPATCH_FLOAT_DTYPE_TO_TEMPLATE(points.GetDtype(), [&]() {
139  scalar_t* points_ptr = points.GetDataPtr<scalar_t>();
140  const scalar_t* R_ptr = R.GetDataPtr<scalar_t>();
141  const scalar_t* center_ptr = center.GetDataPtr<scalar_t>();
142 
143  core::ParallelFor(R.GetDevice(), points.GetLength(),
144  [=] OPEN3D_DEVICE(int64_t workload_idx) {
145  RotatePointsKernel(R_ptr,
146  points_ptr + 3 * workload_idx,
147  center_ptr);
148  });
149  });
150 }
151 
152 #ifdef __CUDACC__
153 void RotateNormalsCUDA
154 #else
156 #endif
157  (const core::Tensor& R, core::Tensor& normals) {
158  DISPATCH_FLOAT_DTYPE_TO_TEMPLATE(normals.GetDtype(), [&]() {
159  scalar_t* normals_ptr = normals.GetDataPtr<scalar_t>();
160  const scalar_t* R_ptr = R.GetDataPtr<scalar_t>();
161 
162  core::ParallelFor(R.GetDevice(), normals.GetLength(),
163  [=] OPEN3D_DEVICE(int64_t workload_idx) {
164  RotateNormalsKernel(
165  R_ptr, normals_ptr + 3 * workload_idx);
166  });
167  });
168 }
169 
170 } // namespace transform
171 } // namespace kernel
172 } // namespace geometry
173 } // namespace t
174 } // namespace open3d
Common CUDA utilities.
#define OPEN3D_HOST_DEVICE
Definition: CUDAUtils.h:44
#define OPEN3D_FORCE_INLINE
Definition: CUDAUtils.h:43
#define DISPATCH_FLOAT_DTYPE_TO_TEMPLATE(DTYPE,...)
Definition: Dispatch.h:77
Definition: Tensor.h:32
int points
Definition: FilePCD.cpp:54
void RotatePointsCPU(const core::Tensor &R, core::Tensor &points, const core::Tensor &center)
Definition: TransformImpl.h:135
void TransformPointsCPU(const core::Tensor &transformation, core::Tensor &points)
Definition: TransformImpl.h:95
void RotateNormalsCPU(const core::Tensor &R, core::Tensor &normals)
Definition: TransformImpl.h:157
void TransformNormalsCPU(const core::Tensor &transformation, core::Tensor &normals)
Definition: TransformImpl.h:115
OPEN3D_HOST_DEVICE OPEN3D_FORCE_INLINE void TransformNormalsKernel(const scalar_t *transformation_ptr, scalar_t *normals_ptr)
Definition: TransformImpl.h:44
OPEN3D_HOST_DEVICE OPEN3D_FORCE_INLINE void TransformPointsKernel(const scalar_t *transformation_ptr, scalar_t *points_ptr)
Definition: TransformImpl.h:19
OPEN3D_HOST_DEVICE OPEN3D_FORCE_INLINE void RotatePointsKernel(const scalar_t *R_ptr, scalar_t *points_ptr, const scalar_t *center)
Definition: TransformImpl.h:62
OPEN3D_HOST_DEVICE OPEN3D_FORCE_INLINE void RotateNormalsKernel(const scalar_t *R_ptr, scalar_t *normals_ptr)
Definition: TransformImpl.h:76
Definition: PinholeCameraIntrinsic.cpp:16