16#include <lagrange/Mesh.h>
17#include <lagrange/MeshTrait.h>
18#include <lagrange/create_mesh.h>
19#include <lagrange/legacy/inline.h>
20#include <lagrange/utils/range.h>
21#include <lagrange/utils/safe_cast.h>
27template <
typename MeshType>
28std::vector<std::unique_ptr<MeshType>> extract_component_submeshes(
30 std::vector<std::vector<typename MeshType::Index>>* vertex_mappings =
nullptr,
31 std::vector<std::vector<typename MeshType::Index>>* facet_mappings =
nullptr)
33 static_assert(MeshTrait<MeshType>::is_mesh(),
"Input type is not Mesh");
35 if (!mesh.is_components_initialized()) {
36 mesh.initialize_components();
39 return extract_submeshes(mesh, mesh.get_components(), vertex_mappings, facet_mappings);
42template <
typename MeshType,
typename Index>
43std::vector<std::unique_ptr<MeshType>> extract_submeshes(
45 const std::vector<std::vector<Index>>& facet_groups,
46 std::vector<std::vector<Index>>* vertex_mappings =
nullptr,
47 std::vector<std::vector<Index>>* facet_mappings =
nullptr)
49 static_assert(MeshTrait<MeshType>::is_mesh(),
"Input type is not Mesh");
51 if (vertex_mappings) {
52 vertex_mappings->resize(facet_groups.size());
55 facet_mappings->resize(facet_groups.size());
58 std::vector<std::unique_ptr<MeshType>> extracted_meshes;
59 for (
auto i :
range(facet_groups.size())) {
63 vertex_mappings ? &((*vertex_mappings)[i]) :
nullptr,
64 facet_mappings ? &((*facet_mappings)[i]) :
nullptr));
66 return extracted_meshes;
69template <
typename MeshType,
typename Index>
72 const std::vector<Index>& selected_facets,
73 std::vector<Index>* vertex_mapping =
nullptr,
74 std::vector<Index>* facet_mapping =
nullptr)
76 static_assert(MeshTrait<MeshType>::is_mesh(),
"Input type is not Mesh");
78 using MeshIndex =
typename MeshType::Index;
79 using VertexArray =
typename MeshType::VertexArray;
80 using FacetArray =
typename MeshType::FacetArray;
81 const MeshIndex num_selected_facets = safe_cast<MeshIndex>(selected_facets.size());
82 const auto vertex_per_facet = mesh.get_vertex_per_facet();
84 const auto& vertices = mesh.get_vertices();
85 const auto& facets = mesh.get_facets();
86 const auto num_facets = mesh.get_num_facets();
87 const MeshIndex min_num_vertices = std::min(
88 safe_cast<MeshIndex>(num_selected_facets * vertex_per_facet),
89 mesh.get_num_vertices());
91 FacetArray sub_facets(num_selected_facets, vertex_per_facet);
92 std::unordered_map<MeshIndex, MeshIndex> sub_vertex_indices;
93 sub_vertex_indices.reserve(min_num_vertices);
96 facet_mapping->resize(num_selected_facets);
100 for (
auto i :
range(num_selected_facets)) {
102 selected_facets[i] >= 0 && safe_cast<MeshIndex>(selected_facets[i]) < num_facets,
103 "Facet index out of bound for submesh extraction");
104 for (
auto j :
range(vertex_per_facet)) {
105 const MeshIndex idx = facets(selected_facets[i], j);
106 if (sub_vertex_indices.find(idx) == sub_vertex_indices.end()) {
107 sub_vertex_indices[idx] = count;
110 sub_facets(i, j) = sub_vertex_indices[idx];
113 (*facet_mapping)[i] = selected_facets[i];
117 VertexArray sub_vertices(count, mesh.get_dim());
118 if (vertex_mapping) {
119 vertex_mapping->resize(count);
121 for (
const auto& item : sub_vertex_indices) {
124 sub_vertices.row(item.second) = vertices.row(item.first);
125 if (vertex_mapping) {
126 (*vertex_mapping)[item.second] = item.first;
SurfaceMesh< Scalar, Index > extract_submesh(const SurfaceMesh< Scalar, Index > &mesh, span< const Index > selected_facets, const SubmeshOptions &options={})
Extract a submesh that consists of a subset of the facets of the source mesh.
Definition: extract_submesh.cpp:26
#define la_runtime_assert(...)
Runtime assertion check.
Definition: assert.h:169
internal::Range< Index > range(Index end)
Returns an iterable object representing the range [0, end).
Definition: range.h:176
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