Lagrange
remove_topologically_degenerate_triangles.h
1/*
2 * Copyright 2016 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 {
37template <typename MeshType>
38std::unique_ptr<MeshType> remove_topologically_degenerate_triangles(const MeshType& mesh)
39{
40 static_assert(MeshTrait<MeshType>::is_mesh(), "Input type is not Mesh");
41 la_runtime_assert(mesh.get_vertex_per_facet() == 3);
42
43 lagrange::logger().trace("[remove_topologically_degenerate_triangles]");
44
45 using Index = typename MeshType::FacetArray::Scalar;
46 const auto& vertices = mesh.get_vertices();
47 const auto& facets = mesh.get_facets();
48
49 auto is_topologically_degenerate = [&facets](Index fid) {
50 return facets(fid, 0) == facets(fid, 1) || facets(fid, 1) == facets(fid, 2) ||
51 facets(fid, 2) == facets(fid, 0);
52 };
53
54 const Index num_facets = mesh.get_num_facets();
55 std::vector<Index> good_triangle_indices; // this is a backward facet map
56 good_triangle_indices.reserve(num_facets);
57 for (Index i = 0; i < num_facets; i++) {
58 if (!is_topologically_degenerate(i)) {
59 good_triangle_indices.push_back(i);
60 }
61 }
62
63 Index num_non_degenerate_facets = safe_cast<Index>(good_triangle_indices.size());
64 typename MeshType::FacetArray good_facets(num_non_degenerate_facets, 3);
65 for (Index i = 0; i < num_non_degenerate_facets; i++) {
66 good_facets.row(i) = facets.row(good_triangle_indices[i]);
67 }
68
69 auto mesh2 = create_mesh(vertices, std::move(good_facets));
70
71 // Port attributes
72 map_attributes(mesh, *mesh2, {}, good_triangle_indices);
73
74 return mesh2;
75}
76} // namespace legacy
77} // namespace lagrange
Definition: Mesh.h:48
LA_CORE_API spdlog::logger & logger()
Retrieves the current logger.
Definition: Logger.cpp:40
#define la_runtime_assert(...)
Runtime assertion check.
Definition: assert.h:169
void map_attributes(const SurfaceMesh< Scalar, Index > &source_mesh, SurfaceMesh< Scalar, Index > &target_mesh, span< const Index > mapping_data, span< const Index > mapping_offsets={}, const MapAttributesOptions &options={})
Map attributes from the source mesh to the target mesh.
Definition: map_attributes.cpp:26
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