Lagrange
remove_isolated_vertices.h
1/*
2 * Copyright 2017 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/SurfaceMesh.h>
17#include <lagrange/attributes/map_attributes.h>
18#include <lagrange/common.h>
19#include <lagrange/create_mesh.h>
20#include <lagrange/legacy/inline.h>
21
22namespace lagrange {
23LAGRANGE_LEGACY_INLINE
24namespace legacy {
25
26template <
27 typename MeshType,
28 std::enable_if_t<lagrange::MeshTraitHelper::is_mesh<MeshType>::value>* = nullptr>
29std::unique_ptr<MeshType> remove_isolated_vertices(const MeshType& mesh)
30{
31 static_assert(MeshTrait<MeshType>::is_mesh(), "Input type is not Mesh");
32 using Index = typename MeshType::Index;
33 const Index dim = mesh.get_dim();
34 const Index num_vertices = mesh.get_num_vertices();
35 const Index num_facets = mesh.get_num_facets();
36 const Index vertex_per_facet = mesh.get_vertex_per_facet();
37
38 // Note: this is a forward mapping (for each element of mesh, index of new element)
39 std::vector<Index> forward_vertex_map(num_vertices, invalid<Index>());
40
41 const auto& vertices = mesh.get_vertices();
42 auto facets = mesh.get_facets(); // copy, we modify this
43
44 int count = 0;
45 for (Index i = 0; i < num_facets; i++) {
46 for (Index j = 0; j < vertex_per_facet; j++) {
47 if (forward_vertex_map[facets(i, j)] == invalid<Index>()) {
48 forward_vertex_map[facets(i, j)] = count;
49 count++;
50 }
51 facets(i, j) = forward_vertex_map[facets(i, j)];
52 }
53 }
54
55 const Index new_num_vertices = count;
56 typename MeshType::VertexArray new_vertices(new_num_vertices, dim);
57 for (Index i = 0; i < num_vertices; i++) {
58 if (forward_vertex_map[i] == invalid<Index>()) continue;
59 new_vertices.row(forward_vertex_map[i]) = vertices.row(i);
60 }
61
62 auto mesh2 = create_mesh(new_vertices, facets);
63
64 // Port attributes
65 map_attributes(mesh, *mesh2, invert_mapping(forward_vertex_map, new_num_vertices));
66
67 return mesh2;
68}
69
70} // namespace legacy
71} // namespace lagrange
Definition: Mesh.h:48
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
InverseMapping< Index > invert_mapping(Index num_source_elements, Function old_to_new, Index num_target_elements=invalid< Index >())
Compute the target-to-source (i.e.
Definition: invert_mapping.h:61
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
void remove_isolated_vertices(SurfaceMesh< Scalar, Index > &mesh)
Removes isolated vertices of a mesh.
Definition: remove_isolated_vertices.cpp:20