Lagrange
remove_null_area_triangles.h
1/*
2 * Copyright 2020 Adobe. All rights reserved.
3 * This file is licensed to you under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License. You may obtain a copy
5 * of the License at http://www.apache.org/licenses/LICENSE-2.0
6 *
7 * Unless required by applicable law or agreed to in writing, software distributed under
8 * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
9 * OF ANY KIND, either express or implied. See the License for the specific language
10 * governing permissions and limitations under the License.
11 */
12#pragma once
13
14#include <lagrange/Mesh.h>
15#include <lagrange/MeshTrait.h>
16#include <lagrange/attributes/map_attributes.h>
17#include <lagrange/common.h>
18#include <lagrange/create_mesh.h>
19#include <lagrange/legacy/inline.h>
20
21namespace lagrange {
22LAGRANGE_LEGACY_INLINE
23namespace legacy {
24
25template <typename MeshType>
26std::unique_ptr<MeshType> remove_null_area_triangles(const MeshType& mesh)
27{
28 static_assert(lagrange::MeshTrait<MeshType>::is_mesh(), "Input type is not Mesh");
29 assert(mesh.get_vertex_per_facet() == 3);
30
31 using Index = typename MeshType::FacetArray::Scalar;
32 const auto& vertices = mesh.get_vertices();
33 const auto& facets = mesh.get_facets();
34
35 auto has_non_null_area = [&vertices, &facets](Index fid) {
36 const auto& aa = vertices.row(facets(fid, 0));
37 const auto& bb = vertices.row(facets(fid, 1));
38 const auto& cc = vertices.row(facets(fid, 2));
39 const auto& area = (bb - aa).cross(cc - aa).norm();
40 return area > 0;
41 };
42
43 const Index num_facets = mesh.get_num_facets();
44 std::vector<Index> good_triangle_indices; // this is a backward facet map
45 for (Index i = 0; i < num_facets; i++) {
46 if (has_non_null_area(i)) {
47 good_triangle_indices.push_back(i);
48 }
49 }
50
51 Index num_non_degenerate_facets = lagrange::safe_cast<Index>(good_triangle_indices.size());
52 typename MeshType::FacetArray good_facets(num_non_degenerate_facets, 3);
53 for (Index i = 0; i < num_non_degenerate_facets; i++) {
54 good_facets.row(i) = facets.row(good_triangle_indices[i]);
55 }
56
57 auto mesh_ = lagrange::create_mesh(vertices, good_facets);
58
59 lagrange::map_attributes(mesh, *mesh_, {}, good_triangle_indices);
60
61 return mesh_;
62}
63
64} // namespace legacy
65} // namespace lagrange
Definition: Mesh.h:48
Main namespace for Lagrange.
Definition: AABBIGL.h:30
auto create_mesh(const Eigen::MatrixBase< DerivedV > &vertices, const Eigen::MatrixBase< DerivedF > &facets)
This function create a new mesh given the vertex and facet arrays by copying data into the Mesh objec...
Definition: create_mesh.h:39
MeshTrait class provide compiler check for different mesh types.
Definition: MeshTrait.h:108