ACloudViewer  3.9.4
A Modern Library for 3D Data Processing
TriangleMeshCUDA.cu
Go to the documentation of this file.
1 // ----------------------------------------------------------------------------
2 // - CloudViewer: www.cloudViewer.org -
3 // ----------------------------------------------------------------------------
4 // Copyright (c) 2018-2024 www.cloudViewer.org
5 // SPDX-License-Identifier: MIT
6 // ----------------------------------------------------------------------------
7 
8 #include "cloudViewer/t/geometry/kernel/TriangleMeshImpl.h"
9 
10 namespace cloudViewer {
11 namespace t {
12 namespace geometry {
13 namespace kernel {
14 namespace trianglemesh {
15 
16 void ComputeVertexNormalsCUDA(const core::Tensor& triangles,
17  const core::Tensor& triangle_normals,
18  core::Tensor& vertex_normals) {
19  const core::Dtype dtype = vertex_normals.GetDtype();
20  const int64_t n = triangles.GetLength();
21  const core::Tensor triangles_d = triangles.To(core::Int64);
22 
23  DISPATCH_FLOAT_DTYPE_TO_TEMPLATE(dtype, [&]() {
24  const int64_t* triangle_ptr = triangles_d.GetDataPtr<int64_t>();
25  const scalar_t* triangle_normals_ptr =
26  triangle_normals.GetDataPtr<scalar_t>();
27  scalar_t* vertex_normals_ptr = vertex_normals.GetDataPtr<scalar_t>();
28  core::ParallelFor(
29  vertex_normals.GetDevice(), n,
30  [=] CLOUDVIEWER_DEVICE(int64_t workload_idx) {
31  int64_t idx = 3 * workload_idx;
32  int64_t triangle_id1 = triangle_ptr[idx];
33  int64_t triangle_id2 = triangle_ptr[idx + 1];
34  int64_t triangle_id3 = triangle_ptr[idx + 2];
35 
36  scalar_t n1 = triangle_normals_ptr[idx];
37  scalar_t n2 = triangle_normals_ptr[idx + 1];
38  scalar_t n3 = triangle_normals_ptr[idx + 2];
39 
40  atomicAdd(&vertex_normals_ptr[3 * triangle_id1], n1);
41  atomicAdd(&vertex_normals_ptr[3 * triangle_id1 + 1], n2);
42  atomicAdd(&vertex_normals_ptr[3 * triangle_id1 + 2], n3);
43  atomicAdd(&vertex_normals_ptr[3 * triangle_id2], n1);
44  atomicAdd(&vertex_normals_ptr[3 * triangle_id2 + 1], n2);
45  atomicAdd(&vertex_normals_ptr[3 * triangle_id2 + 2], n3);
46  atomicAdd(&vertex_normals_ptr[3 * triangle_id3], n1);
47  atomicAdd(&vertex_normals_ptr[3 * triangle_id3 + 1], n2);
48  atomicAdd(&vertex_normals_ptr[3 * triangle_id3 + 2], n3);
49  });
50  });
51 }
52 
53 } // namespace trianglemesh
54 } // namespace kernel
55 } // namespace geometry
56 } // namespace t
57 } // namespace cloudViewer