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");
60 using Index =
typename MeshType::Index;
61 using VertexArray =
typename MeshType::VertexArray;
62 using FacetArray =
typename MeshType::FacetArray;
63 using IndexList =
typename MeshType::IndexList;
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");
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));
111 map_attributes(mesh, *mesh2, backward_mapping);
Index get_num_vertices() const
Retrieves the number of vertices.
Definition SurfaceMesh.h:671
Index get_vertex_per_facet() const
Retrieves the number of vertex per facet in a regular mesh.
Definition SurfaceMesh.cpp:2130
#define la_runtime_assert(...)
Runtime assertion check.
Definition assert.h:174
internal::Range< Index > range(Index end)
Returns an iterable object representing the range [0, end).
Definition range.h:176
constexpr T invalid()
You can use invalid<T>() to get a value that can represent "invalid" values, such as invalid indices ...
Definition invalid.h:40
constexpr auto safe_cast(SourceType value) -> std::enable_if_t<!std::is_same< SourceType, TargetType >::value, TargetType >
Perform safe cast from SourceType to TargetType, where "safe" means:
Definition safe_cast.h:50
Main namespace for Lagrange.
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