15#include <unordered_map>
16#include <unordered_set>
18#include <lagrange/Mesh.h>
19#include <lagrange/attributes/map_attributes.h>
20#include <lagrange/bvh/create_BVH.h>
21#include <lagrange/common.h>
22#include <lagrange/mesh_cleanup/remove_isolated_vertices.h>
23#include <lagrange/utils/range.h>
31template <
typename MeshType>
32std::unique_ptr<MeshType> zip_boundary(
MeshType& mesh,
typename MeshType::Scalar radius)
34 using Index =
typename MeshType::Index;
36 auto get_boundary_vertices = [&mesh]() {
37 mesh.initialize_edge_data();
39 std::vector<Index> boundary_vertices;
42 if (mesh.is_boundary_vertex(v)) {
43 boundary_vertices.push_back(v);
46 return boundary_vertices;
49 auto get_boundary_points = [&mesh](
const auto& boundary_vertices) {
50 typename MeshType::VertexArray points(boundary_vertices.size(), mesh.get_dim());
51 for (Index i = 0; i < (Index)boundary_vertices.size(); ++i) {
52 const auto v = boundary_vertices[i];
53 points.row(i) = mesh.get_vertices().row(v);
58 auto compute_boundary_vertex_mapping =
59 [&radius](
const auto& engine,
const auto& boundary_vertices,
const auto& boundary_points) {
60 std::unordered_map<Index, Index> vertex_mapping;
61 for (Index bvi = 0; bvi < (Index)boundary_vertices.size(); ++bvi) {
62 const Index vi = boundary_vertices[bvi];
63 if (vertex_mapping.find(vi) != vertex_mapping.end())
continue;
65 auto nearby_vertices =
66 engine->query_in_sphere_neighbours(boundary_points.row(bvi), radius);
67 for (
const auto& entry : nearby_vertices) {
68 const Index bvj = entry.closest_vertex_idx;
69 const Index vj = boundary_vertices[bvj];
71 vertex_mapping[vj] = vi;
75 return vertex_mapping;
79 auto facets = mesh.get_facets();
82 facets.data() + facets.size(),
84 [&vertex_mapping](Index vi) {
85 const auto itr = vertex_mapping.find(vi);
86 if (itr == vertex_mapping.end()) {
94 BackwardMeshMapping<MeshType> mapping;
95 mapping.vertex.resize(out_mesh->get_num_vertices());
96 std::iota(mapping.vertex.begin(), mapping.vertex.end(), 0);
97 for (
const auto& entry : vertex_mapping) {
98 mapping.vertex[entry.first] = entry.second;
104 const auto boundary_vertices = get_boundary_vertices();
105 const auto boundary_points = get_boundary_points(boundary_vertices);
106 auto engine = lagrange::bvh::create_BVH(lagrange::bvh::BVHType::NANOFLANN, boundary_points);
107 const auto vertex_mapping =
108 compute_boundary_vertex_mapping(engine, boundary_vertices, boundary_points);
Index get_num_vertices() const
Retrieves the number of vertices.
Definition: SurfaceMesh.h:671
void remap_vertices(SurfaceMesh< Scalar, Index > &mesh, span< const Index > forward_mapping, RemapVerticesOptions options={})
Remap vertices of a mesh based on provided forward mapping.
Definition: remap_vertices.cpp:136
internal::Range< Index > range(Index end)
Returns an iterable object representing the range [0, end).
Definition: range.h:176
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
void remove_isolated_vertices(SurfaceMesh< Scalar, Index > &mesh)
Removes isolated vertices of a mesh.
Definition: remove_isolated_vertices.cpp:20