1 // ----------------------------------------------------------------------------
2 // - CloudViewer: www.cloudViewer.org -
3 // ----------------------------------------------------------------------------
4 // Copyright (c) 2018-2024 www.cloudViewer.org
5 // SPDX-License-Identifier: MIT
6 // ----------------------------------------------------------------------------
8 #include "cloudViewer/t/geometry/kernel/TriangleMeshImpl.h"
10 namespace cloudViewer {
14 namespace trianglemesh {
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);
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>();
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];
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];
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);
53 } // namespace trianglemesh
55 } // namespace geometry
57 } // namespace cloudViewer