Lagrange
extract_submesh.h
1/*
2 * Copyright 2019 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 <memory>
15
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>
22
23namespace lagrange {
24LAGRANGE_LEGACY_INLINE
25namespace legacy {
26
27template <typename MeshType>
28std::vector<std::unique_ptr<MeshType>> extract_component_submeshes(
29 MeshType& mesh,
30 std::vector<std::vector<typename MeshType::Index>>* vertex_mappings = nullptr,
31 std::vector<std::vector<typename MeshType::Index>>* facet_mappings = nullptr)
32{
33 static_assert(MeshTrait<MeshType>::is_mesh(), "Input type is not Mesh");
34
35 if (!mesh.is_components_initialized()) {
36 mesh.initialize_components();
37 }
38
39 return extract_submeshes(mesh, mesh.get_components(), vertex_mappings, facet_mappings);
40}
41
42template <typename MeshType, typename Index>
43std::vector<std::unique_ptr<MeshType>> extract_submeshes(
44 const MeshType& mesh,
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)
48{
49 static_assert(MeshTrait<MeshType>::is_mesh(), "Input type is not Mesh");
50
51 if (vertex_mappings) {
52 vertex_mappings->resize(facet_groups.size());
53 }
54 if (facet_mappings) {
55 facet_mappings->resize(facet_groups.size());
56 }
57
58 std::vector<std::unique_ptr<MeshType>> extracted_meshes;
59 for (auto i : range(facet_groups.size())) {
60 extracted_meshes.emplace_back(extract_submesh(
61 mesh,
62 facet_groups[i],
63 vertex_mappings ? &((*vertex_mappings)[i]) : nullptr,
64 facet_mappings ? &((*facet_mappings)[i]) : nullptr));
65 }
66 return extracted_meshes;
67}
68
69template <typename MeshType, typename Index>
70std::unique_ptr<MeshType> extract_submesh(
71 const MeshType& mesh,
72 const std::vector<Index>& selected_facets,
73 std::vector<Index>* vertex_mapping = nullptr,
74 std::vector<Index>* facet_mapping = nullptr)
75{
76 static_assert(MeshTrait<MeshType>::is_mesh(), "Input type is not Mesh");
77
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();
83
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());
90
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);
94
95 if (facet_mapping) {
96 facet_mapping->resize(num_selected_facets);
97 }
98
99 MeshIndex count = 0;
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;
108 count++;
109 }
110 sub_facets(i, j) = sub_vertex_indices[idx];
111 }
112 if (facet_mapping) {
113 (*facet_mapping)[i] = selected_facets[i];
114 }
115 }
116
117 VertexArray sub_vertices(count, mesh.get_dim());
118 if (vertex_mapping) {
119 vertex_mapping->resize(count);
120 }
121 for (const auto& item : sub_vertex_indices) {
122 // item.second = index in submesh
123 // item.first = index in original mesh
124 sub_vertices.row(item.second) = vertices.row(item.first);
125 if (vertex_mapping) {
126 (*vertex_mapping)[item.second] = item.first;
127 }
128 }
129
130 return lagrange::create_mesh(std::move(sub_vertices), std::move(sub_facets));
131}
132} // namespace legacy
133} // namespace lagrange
Definition: Mesh.h:48
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