Open3D (C++ API)  0.19.0
Loading...
Searching...
No Matches
TriangleMeshImpl.h
Go to the documentation of this file.
1// ----------------------------------------------------------------------------
2// - Open3D: www.open3d.org -
3// ----------------------------------------------------------------------------
4// Copyright (c) 2018-2024 www.open3d.org
5// SPDX-License-Identifier: MIT
6// ----------------------------------------------------------------------------
7
10#include "open3d/core/Dtype.h"
12#include "open3d/core/Tensor.h"
17
18namespace open3d {
19namespace t {
20namespace geometry {
21namespace kernel {
22namespace trianglemesh {
23
24#if defined(SYCL_LANGUAGE_VERSION)
25using sycl::isnan;
26#elif defined(__CUDACC__)
27using ::isnan; // CUDA provides host/device isnan() in the global namespace.
28#else
29#include <cmath>
30using std::isnan; // CPU only
31#endif
32
33#if defined(__CUDACC__)
34void NormalizeNormalsCUDA
35#elif defined(SYCL_LANGUAGE_VERSION)
36void NormalizeNormalsSYCL
37#else
39#endif
41 const core::Dtype dtype = normals.GetDtype();
42 const int64_t n = normals.GetLength();
43
45 scalar_t* ptr = normals.GetDataPtr<scalar_t>();
46
47 core::ParallelFor(normals.GetDevice(), n,
48 [=] OPEN3D_DEVICE(int64_t workload_idx) {
49 int64_t idx = 3 * workload_idx;
50 scalar_t x = ptr[idx];
51 scalar_t y = ptr[idx + 1];
52 scalar_t z = ptr[idx + 2];
53 if (trianglemesh::isnan(x)) {
54 x = 0.0;
55 y = 0.0;
56 z = 1.0;
57 } else {
58 scalar_t norm = sqrt(x * x + y * y + z * z);
59 if (norm > 0) {
60 x /= norm;
61 y /= norm;
62 z /= norm;
63 }
64 ptr[idx] = x;
65 ptr[idx + 1] = y;
66 ptr[idx + 2] = z;
67 }
68 });
69 });
70}
71
72#if defined(__CUDACC__)
73void ComputeTriangleNormalsCUDA
74#elif defined(SYCL_LANGUAGE_VERSION)
75void ComputeTriangleNormalsSYCL
76#else
78#endif
79 (const core::Tensor& vertices,
80 const core::Tensor& triangles,
82 const core::Dtype dtype = normals.GetDtype();
83 const int64_t n = normals.GetLength();
84 const core::Tensor triangles_d = triangles.To(core::Int64);
85
87 scalar_t* normal_ptr = normals.GetDataPtr<scalar_t>();
88 const int64_t* triangle_ptr = triangles_d.GetDataPtr<int64_t>();
89 const scalar_t* vertex_ptr = vertices.GetDataPtr<scalar_t>();
90
91 core::ParallelFor(normals.GetDevice(), n,
92 [=] OPEN3D_DEVICE(int64_t workload_idx) {
93 int64_t idx = 3 * workload_idx;
94
95 int64_t triangle_id1 = triangle_ptr[idx];
96 int64_t triangle_id2 = triangle_ptr[idx + 1];
97 int64_t triangle_id3 = triangle_ptr[idx + 2];
98
99 scalar_t v01[3], v02[3];
100 v01[0] = vertex_ptr[3 * triangle_id2] -
101 vertex_ptr[3 * triangle_id1];
102 v01[1] = vertex_ptr[3 * triangle_id2 + 1] -
103 vertex_ptr[3 * triangle_id1 + 1];
104 v01[2] = vertex_ptr[3 * triangle_id2 + 2] -
105 vertex_ptr[3 * triangle_id1 + 2];
106 v02[0] = vertex_ptr[3 * triangle_id3] -
107 vertex_ptr[3 * triangle_id1];
108 v02[1] = vertex_ptr[3 * triangle_id3 + 1] -
109 vertex_ptr[3 * triangle_id1 + 1];
110 v02[2] = vertex_ptr[3 * triangle_id3 + 2] -
111 vertex_ptr[3 * triangle_id1 + 2];
112
114 &normal_ptr[idx]);
115 });
116 });
117}
118
119#if defined(__CUDACC__)
120void ComputeTriangleAreasCUDA
121#elif defined(SYCL_LANGUAGE_VERSION)
122void ComputeTriangleAreasSYCL
123#else
125#endif
126 (const core::Tensor& vertices,
127 const core::Tensor& triangles,
128 core::Tensor& triangle_areas) {
129 const int64_t n = triangle_areas.GetLength();
130 const core::Dtype dtype = triangle_areas.GetDtype();
131 const core::Tensor triangles_d = triangles.To(core::Int64);
132
134 scalar_t* area_ptr = triangle_areas.GetDataPtr<scalar_t>();
135 const int64_t* triangle_ptr = triangles_d.GetDataPtr<int64_t>();
136 const scalar_t* vertex_ptr = vertices.GetDataPtr<scalar_t>();
137
138 core::ParallelFor(triangle_areas.GetDevice(), n,
139 [=] OPEN3D_DEVICE(int64_t workload_idx) {
140 int64_t idx = 3 * workload_idx;
141
142 int64_t triangle_id1 = triangle_ptr[idx];
143 int64_t triangle_id2 = triangle_ptr[idx + 1];
144 int64_t triangle_id3 = triangle_ptr[idx + 2];
145
146 scalar_t v01[3], v02[3];
147 v01[0] = vertex_ptr[3 * triangle_id2] -
148 vertex_ptr[3 * triangle_id1];
149 v01[1] = vertex_ptr[3 * triangle_id2 + 1] -
150 vertex_ptr[3 * triangle_id1 + 1];
151 v01[2] = vertex_ptr[3 * triangle_id2 + 2] -
152 vertex_ptr[3 * triangle_id1 + 2];
153 v02[0] = vertex_ptr[3 * triangle_id3] -
154 vertex_ptr[3 * triangle_id1];
155 v02[1] = vertex_ptr[3 * triangle_id3 + 1] -
156 vertex_ptr[3 * triangle_id1 + 1];
157 v02[2] = vertex_ptr[3 * triangle_id3 + 2] -
158 vertex_ptr[3 * triangle_id1 + 2];
159
160 area_ptr[workload_idx] =
162 v01, v02);
163 });
164 });
165}
166
167} // namespace trianglemesh
168} // namespace kernel
169} // namespace geometry
170} // namespace t
171} // namespace open3d
Common CUDA utilities.
#define OPEN3D_DEVICE
Definition CUDAUtils.h:44
#define DISPATCH_FLOAT_DTYPE_TO_TEMPLATE(DTYPE,...)
Definition Dispatch.h:77
double t
Definition SurfaceReconstructionPoisson.cpp:172
Definition Dtype.h:20
Definition Tensor.h:32
int64_t GetLength() const
Definition Tensor.h:1182
T * GetDataPtr()
Definition Tensor.h:1201
Tensor To(Dtype dtype, bool copy=false) const
Definition Tensor.cpp:784
OPEN3D_HOST_DEVICE OPEN3D_FORCE_INLINE scalar_t cross_mag_3x1(const scalar_t *A_3x1_input, const scalar_t *B_3x1_input)
Definition Matrix.h:77
OPEN3D_HOST_DEVICE OPEN3D_FORCE_INLINE void cross_3x1(const scalar_t *A_3x1_input, const scalar_t *B_3x1_input, scalar_t *C_3x1_output)
Definition Matrix.h:63
const Dtype Int64
Definition Dtype.cpp:47
void ParallelFor(const Device &device, int64_t n, const func_t &func)
Definition ParallelFor.h:135
void NormalizeNormalsCPU(core::Tensor &normals)
Definition TriangleMeshImpl.h:40
void ComputeTriangleAreasCPU(const core::Tensor &vertices, const core::Tensor &triangles, core::Tensor &triangle_areas)
Definition TriangleMeshImpl.h:126
void ComputeTriangleNormalsCPU(const core::Tensor &vertices, const core::Tensor &triangles, core::Tensor &normals)
Definition TriangleMeshImpl.h:79
Definition PinholeCameraIntrinsic.cpp:16
const core::Tensor * normals
Definition TriangleMesh.cpp:2126