Open3D (C++ API)  0.20.0+2ce4f7e
Loading...
Searching...
No Matches
TriangleMeshImpl.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#include "open3d/Macro.h"
11#include "open3d/core/Dtype.h"
13#include "open3d/core/Tensor.h"
18
19namespace open3d {
20namespace t {
21namespace geometry {
22namespace kernel {
23namespace trianglemesh {
24
25#if defined(SYCL_LANGUAGE_VERSION)
26using sycl::isnan;
27#elif defined(__CUDACC__)
28using ::isnan; // CUDA provides host/device isnan() in the global namespace.
29#else
30#include <cmath>
31using std::isnan; // CPU only
32#endif
33
34#if defined(__CUDACC__)
35void NormalizeNormalsCUDA
36#elif defined(SYCL_LANGUAGE_VERSION)
37void NormalizeNormalsSYCL
38#else
40#endif
42 const core::Dtype dtype = normals.GetDtype();
43 const int64_t n = normals.GetLength();
44
46 scalar_t* ptr = normals.GetDataPtr<scalar_t>();
47
48 core::ParallelFor(normals.GetDevice(), n,
49 [=] OPEN3D_DEVICE(int64_t workload_idx) {
50 int64_t idx = 3 * workload_idx;
51 scalar_t x = ptr[idx];
52 scalar_t y = ptr[idx + 1];
53 scalar_t z = ptr[idx + 2];
54 if (trianglemesh::isnan(x)) {
55 x = 0.0;
56 y = 0.0;
57 z = 1.0;
58 } else {
59 scalar_t norm = sqrt(x * x + y * y + z * z);
60 if (norm > 0) {
61 x /= norm;
62 y /= norm;
63 z /= norm;
64 }
65 ptr[idx] = x;
66 ptr[idx + 1] = y;
67 ptr[idx + 2] = z;
68 }
69 });
70 });
71}
72
73#if defined(__CUDACC__)
74void ComputeTriangleNormalsCUDA
75#elif defined(SYCL_LANGUAGE_VERSION)
76void ComputeTriangleNormalsSYCL
77#else
79#endif
80 (const core::Tensor& vertices,
81 const core::Tensor& triangles,
83 const core::Dtype dtype = normals.GetDtype();
84 const int64_t n = normals.GetLength();
85 const int64_t num_vertices = vertices.GetLength();
86 const core::Tensor triangles_d = triangles.To(core::Int64);
87
89 scalar_t* normal_ptr = normals.GetDataPtr<scalar_t>();
90 const int64_t* triangle_ptr = triangles_d.GetDataPtr<int64_t>();
91 const scalar_t* vertex_ptr = vertices.GetDataPtr<scalar_t>();
92
94 normals.GetDevice(), n,
95 [=] OPEN3D_DEVICE(int64_t workload_idx) {
96 int64_t idx = 3 * workload_idx;
97
98 int64_t triangle_id1 = triangle_ptr[idx];
99 int64_t triangle_id2 = triangle_ptr[idx + 1];
100 int64_t triangle_id3 = triangle_ptr[idx + 2];
101 OPEN3D_ASSERT(triangle_id1 >= 0 &&
102 triangle_id1 < num_vertices &&
103 triangle_id2 >= 0 &&
104 triangle_id2 < num_vertices &&
105 triangle_id3 >= 0 &&
106 triangle_id3 < num_vertices,
107 "Triangle vertex index is out of range.");
108
109 scalar_t v01[3], v02[3];
110 v01[0] = vertex_ptr[3 * triangle_id2] -
111 vertex_ptr[3 * triangle_id1];
112 v01[1] = vertex_ptr[3 * triangle_id2 + 1] -
113 vertex_ptr[3 * triangle_id1 + 1];
114 v01[2] = vertex_ptr[3 * triangle_id2 + 2] -
115 vertex_ptr[3 * triangle_id1 + 2];
116 v02[0] = vertex_ptr[3 * triangle_id3] -
117 vertex_ptr[3 * triangle_id1];
118 v02[1] = vertex_ptr[3 * triangle_id3 + 1] -
119 vertex_ptr[3 * triangle_id1 + 1];
120 v02[2] = vertex_ptr[3 * triangle_id3 + 2] -
121 vertex_ptr[3 * triangle_id1 + 2];
122
123 core::linalg::kernel::cross_3x1(v01, v02, &normal_ptr[idx]);
124 });
125 });
126}
127
128#if defined(__CUDACC__)
129void ComputeTriangleAreasCUDA
130#elif defined(SYCL_LANGUAGE_VERSION)
131void ComputeTriangleAreasSYCL
132#else
134#endif
135 (const core::Tensor& vertices,
136 const core::Tensor& triangles,
137 core::Tensor& triangle_areas) {
138 const int64_t n = triangle_areas.GetLength();
139 const core::Dtype dtype = triangle_areas.GetDtype();
140 const int64_t num_vertices = vertices.GetLength();
141 const core::Tensor triangles_d = triangles.To(core::Int64);
142
144 scalar_t* area_ptr = triangle_areas.GetDataPtr<scalar_t>();
145 const int64_t* triangle_ptr = triangles_d.GetDataPtr<int64_t>();
146 const scalar_t* vertex_ptr = vertices.GetDataPtr<scalar_t>();
147
149 triangle_areas.GetDevice(), n,
150 [=] OPEN3D_DEVICE(int64_t workload_idx) {
151 int64_t idx = 3 * workload_idx;
152
153 int64_t triangle_id1 = triangle_ptr[idx];
154 int64_t triangle_id2 = triangle_ptr[idx + 1];
155 int64_t triangle_id3 = triangle_ptr[idx + 2];
156 OPEN3D_ASSERT(triangle_id1 >= 0 &&
157 triangle_id1 < num_vertices &&
158 triangle_id2 >= 0 &&
159 triangle_id2 < num_vertices &&
160 triangle_id3 >= 0 &&
161 triangle_id3 < num_vertices,
162 "Triangle vertex index is out of range.");
163
164 scalar_t v01[3], v02[3];
165 v01[0] = vertex_ptr[3 * triangle_id2] -
166 vertex_ptr[3 * triangle_id1];
167 v01[1] = vertex_ptr[3 * triangle_id2 + 1] -
168 vertex_ptr[3 * triangle_id1 + 1];
169 v01[2] = vertex_ptr[3 * triangle_id2 + 2] -
170 vertex_ptr[3 * triangle_id1 + 2];
171 v02[0] = vertex_ptr[3 * triangle_id3] -
172 vertex_ptr[3 * triangle_id1];
173 v02[1] = vertex_ptr[3 * triangle_id3 + 1] -
174 vertex_ptr[3 * triangle_id1 + 1];
175 v02[2] = vertex_ptr[3 * triangle_id3 + 2] -
176 vertex_ptr[3 * triangle_id1 + 2];
177
178 area_ptr[workload_idx] =
180 });
181 });
182}
183
184} // namespace trianglemesh
185} // namespace kernel
186} // namespace geometry
187} // namespace t
188} // namespace open3d
Common CUDA utilities.
#define OPEN3D_DEVICE
Definition CUDAUtils.h:44
#define DISPATCH_FLOAT_DTYPE_TO_TEMPLATE(DTYPE,...)
Definition Dispatch.h:77
#define OPEN3D_ASSERT(...)
Definition Macro.h:58
std::int64_t y
Definition NormalDistributionsTransform.cpp:43
std::int64_t x
Definition NormalDistributionsTransform.cpp:42
std::int64_t z
Definition NormalDistributionsTransform.cpp:44
double t
Definition SurfaceReconstructionPoisson.cpp:175
Definition Dtype.h:20
Definition Tensor.h:32
int64_t GetLength() const
Definition Tensor.h:1184
T * GetDataPtr()
Definition Tensor.h:1203
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:190
void NormalizeNormalsCPU(core::Tensor &normals)
Definition TriangleMeshImpl.h:41
void ComputeTriangleAreasCPU(const core::Tensor &vertices, const core::Tensor &triangles, core::Tensor &triangle_areas)
Definition TriangleMeshImpl.h:135
void ComputeTriangleNormalsCPU(const core::Tensor &vertices, const core::Tensor &triangles, core::Tensor &normals)
Definition TriangleMeshImpl.h:80
Definition PinholeCameraIntrinsic.cpp:16
const core::Tensor * normals
Definition TriangleMesh.cpp:2126