Open3D (C++ API)  0.16.1
TriangleMeshImpl.h
Go to the documentation of this file.
1 // ----------------------------------------------------------------------------
2 // - Open3D: www.open3d.org -
3 // ----------------------------------------------------------------------------
4 // The MIT License (MIT)
5 //
6 // Copyright (c) 2018-2021 www.open3d.org
7 //
8 // Permission is hereby granted, free of charge, to any person obtaining a copy
9 // of this software and associated documentation files (the "Software"), to deal
10 // in the Software without restriction, including without limitation the rights
11 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 // copies of the Software, and to permit persons to whom the Software is
13 // furnished to do so, subject to the following conditions:
14 //
15 // The above copyright notice and this permission notice shall be included in
16 // all copies or substantial portions of the Software.
17 //
18 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
24 // IN THE SOFTWARE.
25 // ----------------------------------------------------------------------------
26 
27 #include "open3d/core/CUDAUtils.h"
28 #include "open3d/core/Dispatch.h"
29 #include "open3d/core/Dtype.h"
31 #include "open3d/core/Tensor.h"
35 #include "open3d/utility/Logging.h"
36 
37 namespace open3d {
38 namespace t {
39 namespace geometry {
40 namespace kernel {
41 namespace trianglemesh {
42 
43 #ifndef __CUDACC__
44 using std::isnan;
45 #endif
46 
47 #if defined(__CUDACC__)
48 void NormalizeNormalsCUDA
49 #else
51 #endif
52  (core::Tensor& normals) {
53  const core::Dtype dtype = normals.GetDtype();
54  const int64_t n = normals.GetLength();
55 
57  scalar_t* ptr = normals.GetDataPtr<scalar_t>();
58 
59  core::ParallelFor(normals.GetDevice(), n,
60  [=] OPEN3D_DEVICE(int64_t workload_idx) {
61  int64_t idx = 3 * workload_idx;
62  scalar_t x = ptr[idx];
63  scalar_t y = ptr[idx + 1];
64  scalar_t z = ptr[idx + 2];
65  if (isnan(x)) {
66  x = 0.0;
67  y = 0.0;
68  z = 1.0;
69  } else {
70  scalar_t norm = sqrt(x * x + y * y + z * z);
71  if (norm > 0) {
72  x /= norm;
73  y /= norm;
74  z /= norm;
75  }
76  ptr[idx] = x;
77  ptr[idx + 1] = y;
78  ptr[idx + 2] = z;
79  }
80  });
81  });
82 }
83 
84 #if defined(__CUDACC__)
85 void ComputeTriangleNormalsCUDA
86 #else
88 #endif
89  (const core::Tensor& vertices,
90  const core::Tensor& triangles,
91  core::Tensor& normals) {
92  const core::Dtype dtype = normals.GetDtype();
93  const int64_t n = normals.GetLength();
94  const core::Tensor triangles_d = triangles.To(core::Int64);
95 
97  scalar_t* normal_ptr = normals.GetDataPtr<scalar_t>();
98  const int64_t* triangle_ptr = triangles_d.GetDataPtr<int64_t>();
99  const scalar_t* vertex_ptr = vertices.GetDataPtr<scalar_t>();
100 
101  core::ParallelFor(normals.GetDevice(), n,
102  [=] OPEN3D_DEVICE(int64_t workload_idx) {
103  int64_t idx = 3 * workload_idx;
104 
105  int64_t triangle_id1 = triangle_ptr[idx];
106  int64_t triangle_id2 = triangle_ptr[idx + 1];
107  int64_t triangle_id3 = triangle_ptr[idx + 2];
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 
124  &normal_ptr[idx]);
125  });
126  });
127 }
128 
129 } // namespace trianglemesh
130 } // namespace kernel
131 } // namespace geometry
132 } // namespace t
133 } // namespace open3d
Common CUDA utilities.
#define OPEN3D_DEVICE
Definition: CUDAUtils.h:64
#define DISPATCH_FLOAT_DTYPE_TO_TEMPLATE(DTYPE,...)
Definition: Dispatch.h:96
Definition: Dtype.h:39
Definition: Tensor.h:51
T * GetDataPtr()
Definition: Tensor.h:1149
Tensor To(Dtype dtype, bool copy=false) const
Definition: Tensor.cpp:725
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:82
const Dtype Int64
Definition: Dtype.cpp:66
void ParallelFor(const Device &device, int64_t n, const func_t &func)
Definition: ParallelFor.h:122
void NormalizeNormalsCPU(core::Tensor &normals)
Definition: TriangleMeshImpl.h:52
void ComputeTriangleNormalsCPU(const core::Tensor &vertices, const core::Tensor &triangles, core::Tensor &normals)
Definition: TriangleMeshImpl.h:89
Definition: PinholeCameraIntrinsic.cpp:35