16#include <lagrange/Mesh.h>
17#include <lagrange/MeshTrait.h>
18#include <lagrange/attributes/map_attributes.h>
19#include <lagrange/common.h>
20#include <lagrange/create_mesh.h>
21#include <lagrange/legacy/inline.h>
22#include <lagrange/utils/safe_cast.h>
50template <
typename MeshType>
51std::unique_ptr<MeshType> reorder_mesh_vertices(
53 const typename MeshType::IndexList& forward_mapping)
55 static_assert(MeshTrait<MeshType>::is_mesh(),
"Input type is not Mesh");
57 mesh.get_vertex_per_facet() == 3,
58 std::string(
"vertex per facet is ") + std::to_string(mesh.get_vertex_per_facet()));
60 using Index =
typename MeshType::Index;
61 using VertexArray =
typename MeshType::VertexArray;
62 using FacetArray =
typename MeshType::FacetArray;
63 using IndexList =
typename MeshType::IndexList;
65 const auto num_old_vertices = mesh.get_num_vertices();
66 const auto& vertices = mesh.get_vertices();
67 const auto& facets = mesh.get_facets();
70 auto forward_mapping_no_invalid = [&forward_mapping](
const Index io) {
71 return forward_mapping[io] == invalid<Index>() ? io : forward_mapping[io];
75 Index num_new_vertices = 0;
76 for (
const auto io :
range(num_old_vertices)) {
77 num_new_vertices = std::max(num_new_vertices, forward_mapping_no_invalid(io));
79 num_new_vertices += 1;
81 num_new_vertices <= num_old_vertices,
82 "Number of vertices should not increase");
86 IndexList backward_mapping(num_new_vertices, invalid<Index>());
87 VertexArray vertices_new(num_new_vertices, mesh.get_dim());
88 for (
const auto io :
range(num_old_vertices)) {
89 const auto in = forward_mapping_no_invalid(io);
90 backward_mapping[in] = io;
91 vertices_new.row(in) = vertices.row(io);
95 std::find(backward_mapping.begin(), backward_mapping.end(), invalid<Index>()) ==
96 backward_mapping.end(),
97 "Forward mapping is not surjective");
100 FacetArray facets_new(facets.rows(), facets.cols());
101 for (
auto i :
range(facets.rows())) {
102 facets_new(i, 0) = forward_mapping_no_invalid(facets(i, 0));
103 facets_new(i, 1) = forward_mapping_no_invalid(facets(i, 1));
104 facets_new(i, 2) = forward_mapping_no_invalid(facets(i, 2));
108 auto mesh2 =
create_mesh(std::move(vertices_new), std::move(facets_new));
#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
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