16#include <lagrange/Mesh.h>
17#include <lagrange/MeshTrait.h>
18#include <lagrange/legacy/inline.h>
24template <
typename MeshType>
25auto compute_triangle_normal_const(
const MeshType& mesh) ->
typename MeshType::AttributeArray
27 static_assert(MeshTrait<MeshType>::is_mesh(),
"Input type is not Mesh");
28 using Index = IndexOf<MeshType>;
29 using Scalar = ScalarOf<MeshType>;
30 using AttributeArray =
typename MeshType::AttributeArray;
31 using RowVector3s = Eigen::Matrix<Scalar, 1, 3>;
33 if (mesh.get_vertex_per_facet() != 3) {
34 throw std::runtime_error(
"Input mesh is not triangle mesh.");
36 if (mesh.get_dim() != 3) {
37 throw std::runtime_error(
"Input mesh vertices should have dimension 3.");
40 const auto& vertices = mesh.get_vertices();
41 const auto& facets = mesh.get_facets();
42 AttributeArray normals(facets.rows(), 3);
45 tbb::parallel_for(Index(0), mesh.get_num_facets(), [&](Index f) {
46 const RowVector3s p0 = vertices.row(facets(f, 0));
47 const RowVector3s p1 = vertices.row(facets(f, 1));
48 const RowVector3s p2 = vertices.row(facets(f, 2));
49 normals.row(f) = (p1 - p0).cross(p2 - p0).stableNormalized();
55template <
typename MeshType>
56void compute_triangle_normal(
MeshType& mesh)
58 mesh.add_facet_attribute(
"normal");
59 mesh.import_facet_attribute(
"normal", compute_triangle_normal_const(mesh));
Main namespace for Lagrange.
Definition: AABBIGL.h:30