14#include <lagrange/mesh_convert.h>
16#include <lagrange/IndexedAttribute.h>
17#include <lagrange/MeshTrait.h>
18#include <lagrange/SurfaceMeshTypes.h>
19#include <lagrange/create_mesh.h>
20#include <lagrange/foreach_attribute.h>
21#include <lagrange/utils/Error.h>
22#include <lagrange/utils/assert.h>
23#include <lagrange/utils/strings.h>
24#include <lagrange/views.h>
25#include <lagrange/attribute_names.h>
26#include <lagrange/internal/fast_edge_sort.h>
32namespace mesh_convert_detail {
45template <Policy policy,
typename Scalar,
typename Index,
typename InputMeshType>
48 using CMeshType = std::remove_reference_t<InputMeshType>;
49 using MeshType = std::decay_t<InputMeshType>;
50 static_assert(MeshTrait<MeshType>::is_mesh(),
"Input type is not Mesh");
51 using AttributeArray =
typename MeshType::AttributeArray;
52 using IndexArray =
typename MeshType::IndexArray;
53 using MeshScalar =
typename MeshType::Scalar;
54 using MeshIndex =
typename MeshType::Index;
56 if constexpr (policy == Policy::Wrap) {
57 using VertexArray =
typename MeshType::VertexArray;
58 using FacetArray =
typename MeshType::FacetArray;
60 std::is_lvalue_reference_v<InputMeshType&&>,
61 "Input mesh type must be a lvalue reference when wrapping a mesh object.");
62 static_assert(VertexArray::IsRowMajor,
"Input vertex array must have row major storage");
63 static_assert(FacetArray::IsRowMajor,
"Input facet array must have row major storage");
66#define LA_X_to_surface_mesh_scalar(_, S) || std::is_same_v<Scalar, S>
69 "Scalar type should be float or double");
70#undef LA_X_to_surface_mesh_scalar
72#define LA_X_to_surface_mesh_index(_, I) || std::is_same_v<Index, I>
75 "Index type should be uint32_t or uint64_t");
76#undef LA_X_to_surface_mesh_index
80 if constexpr (policy == Policy::Copy) {
82 new_mesh.add_vertices(
static_cast<Index
>(mesh.get_num_vertices()));
83 new_mesh.add_polygons(
static_cast<Index
>(mesh.get_num_facets()),
84 static_cast<Index
>(mesh.get_vertex_per_facet()));
85 if (mesh.get_num_vertices() > 0) {
86 vertex_ref(new_mesh) = mesh.get_vertices().template cast<Scalar>();
88 if (mesh.get_num_facets() > 0) {
89 facet_ref(new_mesh) = mesh.get_facets().template cast<Index>();
91 }
else if constexpr (policy == Policy::Wrap) {
93 static_assert(std::is_same_v<Scalar, MeshScalar>,
"Mesh scalar type mismatch");
94 static_assert(std::is_same_v<Index, MeshIndex>,
"Mesh index type mismatch");
95 if constexpr (std::is_const_v<CMeshType>) {
96 const auto& vertices = mesh.get_vertices();
97 const auto& facets = mesh.get_facets();
98 new_mesh.wrap_as_const_vertices(
99 {vertices.data(), safe_cast<size_t>(vertices.size())},
100 mesh.get_num_vertices());
101 new_mesh.wrap_as_const_facets(
102 {facets.data(), safe_cast<size_t>(facets.size())},
103 mesh.get_num_facets(),
104 mesh.get_vertex_per_facet());
106 auto& vertices = mesh.ref_vertices();
107 auto& facets = mesh.ref_facets();
108 new_mesh.wrap_as_vertices(
109 {vertices.data(), safe_cast<size_t>(vertices.size())},
110 mesh.get_num_vertices());
111 new_mesh.wrap_as_facets(
112 {facets.data(), safe_cast<size_t>(facets.size())},
113 mesh.get_num_facets(),
114 mesh.get_vertex_per_facet());
119 if (mesh.is_edge_data_initialized()) {
120 new_mesh.initialize_edges(
121 static_cast<Index
>(mesh.get_num_edges()),
122 [&](Index e) -> std::array<Index, 2> {
123 auto v = mesh.get_edge_vertices(
static_cast<typename MeshType::Index
>(e));
124 return {
static_cast<Index
>(v[0]),
static_cast<Index
>(v[1])};
129 auto usage_from_name = [](std::string_view name) {
143 auto get_unique_name = [&](
auto name) -> std::string {
144 if (!new_mesh.has_attribute(name)) {
147 std::string new_name;
148 for (
int cnt = 0; cnt < 1000; ++cnt) {
149 new_name = fmt::format(
"{}.{}", name, cnt);
150 if (!new_mesh.has_attribute(new_name)) {
154 throw Error(fmt::format(
"Could not assign a unique attribute name for: {}", name));
158 auto transfer_attribute = [&](
auto&& name,
auto&& array,
AttributeElement elem) {
159 std::string new_name = get_unique_name(name);
160 decltype(
auto) attr = array->template get<AttributeArray>();
161 if constexpr (policy == Policy::Copy) {
162 new_mesh.template create_attribute<MeshScalar>(
165 usage_from_name(name),
167 attribute_matrix_ref<MeshScalar>(new_mesh, new_name) = attr;
168 }
else if constexpr (policy == Policy::Wrap) {
169 if constexpr (std::is_const_v<CMeshType>) {
170 new_mesh.template wrap_as_const_attribute<MeshScalar>(
173 usage_from_name(name),
175 {attr.data(), static_cast<size_t>(attr.size())});
178 new_mesh.template wrap_as_attribute<MeshScalar>(
181 usage_from_name(name),
183 {attr.data(), static_cast<size_t>(attr.size())});
189 for (
const auto& name : mesh.get_vertex_attribute_names()) {
194 for (
const auto& name : mesh.get_facet_attribute_names()) {
199 for (
const auto& name : mesh.get_corner_attribute_names()) {
204 for (
const auto& name : mesh.get_edge_attribute_names()) {
209 for (
const auto& name : mesh.get_indexed_attribute_names()) {
210 decltype(
auto) attr = mesh.get_indexed_attribute_array(name);
211 decltype(
auto) values = std::get<0>(attr)->template get<AttributeArray>();
212 decltype(
auto) indices = std::get<1>(attr)->template get<IndexArray>();
213 std::string new_name = get_unique_name(name);
215 if constexpr (policy == Policy::Wrap) {
216 static_assert(std::is_same_v<Index, MeshIndex>,
"Mesh attribute index type mismatch");
217 if (std::is_const_v<CMeshType>) {
218 new_mesh.template wrap_as_const_indexed_attribute<MeshScalar>(
220 usage_from_name(name),
223 {values.data(), static_cast<size_t>(values.size())},
224 {indices.data(), static_cast<size_t>(indices.size())});
226 new_mesh.template wrap_as_indexed_attribute<MeshScalar>(
228 usage_from_name(name),
231 {values.data(), static_cast<size_t>(values.size())},
232 {indices.data(), static_cast<size_t>(indices.size())});
234 }
else if constexpr (policy == Policy::Copy) {
235 auto id = new_mesh.template create_attribute<MeshScalar>(
238 usage_from_name(name),
240 auto& new_attr = new_mesh.template ref_indexed_attribute<MeshScalar>(
id);
241 new_attr.values().resize_elements(values.rows());
243 reshaped_ref(new_attr.indices(), indices.cols()) = indices.template cast<Index>();
252template <
typename Scalar,
typename Index,
typename MeshType>
255 return mesh_convert_detail::
256 to_surface_mesh_internal<mesh_convert_detail::Policy::Copy, Scalar, Index>(mesh);
259template <
typename Scalar,
typename Index,
typename MeshType>
262 return mesh_convert_detail::
263 to_surface_mesh_internal<mesh_convert_detail::Policy::Wrap, Scalar, Index>(
264 std::forward<MeshType>(mesh));
267template <
typename MeshType,
typename Scalar,
typename Index>
271 using VertexArray =
typename MeshType::VertexArray;
272 using FacetArray =
typename MeshType::FacetArray;
273 using IndexArray =
typename MeshType::IndexArray;
274 using AttributeArray =
typename MeshType::AttributeArray;
275 using MeshScalar =
typename MeshType::Scalar;
276 using MeshIndex =
typename MeshType::Index;
278#define LA_X_to_legacy_mesh_scalar(_, S) || std::is_same_v<Scalar, S>
281 "Scalar type should be float or double");
282#undef LA_X_to_legacy_mesh_scalar
284#define LA_X_to_legacy_mesh_index(_, I) || std::is_same_v<Index, I>
287 "Index type should be uint32_t or uint64_t");
288#undef LA_X_to_legacy_mesh_index
293 auto new_mesh = [&] {
294 VertexArray vertices;
296 if (mesh.get_num_vertices() > 0) {
297 vertices =
vertex_view(mesh).template cast<MeshScalar>();
299 if (mesh.get_num_facets() > 0) {
300 facets =
facet_view(mesh).template cast<MeshIndex>();
302 return create_mesh<VertexArray, FacetArray>(std::move(vertices), std::move(facets));
306 std::vector<Index> old_edge_ids;
307 std::vector<MeshIndex> new_edge_ids;
308 if (mesh.has_edges()) {
309 logger().warn(
"Mesh contains edges information. A possible reordering may occur.");
310 new_mesh->initialize_edge_data();
312 mesh.get_num_edges() ==
static_cast<Index
>(new_mesh->get_num_edges()),
313 "Number of edges do not match");
314 const auto num_vertices =
static_cast<size_t>(mesh.get_num_vertices());
315 auto buffer = std::make_unique<uint8_t[]>(
316 (num_vertices + 1) * std::max(
sizeof(Index),
sizeof(MeshIndex)));
318 mesh.get_num_edges(),
319 mesh.get_num_vertices(),
320 [&](Index e) -> std::array<Index, 2> { return mesh.get_edge_vertices(e); },
321 {reinterpret_cast<Index*>(buffer.get()), num_vertices + 1});
323 new_mesh->get_num_edges(),
324 new_mesh->get_num_vertices(),
325 [&](MeshIndex e) -> std::array<MeshIndex, 2> { return new_mesh->get_edge_vertices(e); },
326 {reinterpret_cast<MeshIndex*>(buffer.get()), num_vertices + 1});
330 seq_foreach_named_attribute_read<~AttributeElement::Indexed>(
332 [&](std::string_view name_view,
auto&& attr) {
333 using AttributeType = std::decay_t<
decltype(attr)>;
334 using ValueType =
typename AttributeType::ValueType;
335 if (mesh.attr_name_is_reserved(name_view)) {
338 std::string name(name_view);
339 AttributeArray vals =
340 attribute_matrix_view<ValueType>(mesh, name).template cast<MeshScalar>();
341 switch (attr.get_element_type()) {
343 new_mesh->add_vertex_attribute(name);
344 new_mesh->import_vertex_attribute(name, std::move(vals));
348 new_mesh->add_facet_attribute(name);
349 new_mesh->import_facet_attribute(name, std::move(vals));
353 new_mesh->add_corner_attribute(name);
354 new_mesh->import_corner_attribute(name, std::move(vals));
359 auto old_vals = attribute_matrix_view<ValueType>(mesh, name);
360 for (Index e = 0; e < mesh.get_num_edges(); ++e) {
361 vals.row(new_edge_ids[e]) =
362 old_vals.row(old_edge_ids[e]).template cast<MeshScalar>();
364 new_mesh->add_edge_attribute(name);
365 new_mesh->import_edge_attribute(name, std::move(vals));
369 logger().warn(
"Cannot transfer value attribute: {}", name);
378 const auto nvpf =
static_cast<size_t>(new_mesh->get_vertex_per_facet());
379 seq_foreach_named_attribute_read<AttributeElement::Indexed>(
381 [&](std::string_view name_view,
auto&& attr) {
382 if (mesh.attr_name_is_reserved(name_view)) {
385 std::string name(name_view);
386 IndexArray indices =
reshaped_view(attr.indices(), nvpf).template cast<MeshIndex>();
387 AttributeArray values =
matrix_view(attr.values()).template cast<MeshScalar>();
388 new_mesh->add_indexed_attribute(name);
389 new_mesh->import_indexed_attribute(name, std::move(values), std::move(indices));
A general purpose polygonal mesh class.
Definition: SurfaceMesh.h:66
LA_CORE_API spdlog::logger & logger()
Retrieves the current logger.
Definition: Logger.cpp:40
AttributeElement
Type of element to which the attribute is attached.
Definition: AttributeFwd.h:26
@ Vector
Mesh attribute can have any number of channels (including 1 channel).
@ Normal
Mesh attribute can have dim or dim + 1 channels.
@ Color
Mesh attribute can have 1, 2, 3 or 4 channels.
@ UV
Mesh attribute must have exactly 2 channels.
@ Value
Values that are not attached to a specific element.
Definition: AttributeFwd.h:42
@ Edge
Per-edge mesh attributes.
Definition: AttributeFwd.h:34
@ Indexed
Indexed mesh attributes.
Definition: AttributeFwd.h:45
@ Facet
Per-facet mesh attributes.
Definition: AttributeFwd.h:31
@ Corner
Per-corner mesh attributes.
Definition: AttributeFwd.h:37
@ Vertex
Per-vertex mesh attributes.
Definition: AttributeFwd.h:28
SurfaceMesh< Scalar, Index > to_surface_mesh_wrap(MeshType &&mesh)
Wrap a legacy mesh object as a surface mesh object.
Definition: mesh_convert.impl.h:260
SurfaceMesh< Scalar, Index > to_surface_mesh_copy(const MeshType &mesh)
Convert a legacy mesh object to a surface mesh object.
Definition: mesh_convert.impl.h:253
std::unique_ptr< MeshType > to_legacy_mesh(const SurfaceMesh< Scalar, Index > &mesh)
Convert a surface mesh object to a legacy mesh object.
Definition: mesh_convert.impl.h:268
ConstRowMatrixView< ValueType > matrix_view(const Attribute< ValueType > &attribute)
Returns a read-only view of a given attribute in the form of an Eigen matrix.
Definition: views.cpp:35
ConstRowMatrixView< Scalar > vertex_view(const SurfaceMesh< Scalar, Index > &mesh)
Returns a read-only view of the mesh vertices in the form of an Eigen matrix.
Definition: views.cpp:156
RowMatrixView< Scalar > vertex_ref(SurfaceMesh< Scalar, Index > &mesh)
Returns a writable view of the mesh vertices in the form of an Eigen matrix.
Definition: views.cpp:150
ConstRowMatrixView< ValueType > reshaped_view(const Attribute< ValueType > &attribute, size_t num_cols)
Returns a read-only view of a given single-channel attribute in the form of an Eigen matrix with a pr...
Definition: views.cpp:71
ConstRowMatrixView< Index > facet_view(const SurfaceMesh< Scalar, Index > &mesh)
Returns a read-only view of a mesh facets in the form of an Eigen matrix.
Definition: views.cpp:170
RowMatrixView< ValueType > reshaped_ref(Attribute< ValueType > &attribute, size_t num_cols)
Returns a writable view of a given single-channel attribute in the form of an Eigen matrix with a pre...
Definition: views.cpp:58
RowMatrixView< Index > facet_ref(SurfaceMesh< Scalar, Index > &mesh)
Returns a writable view of a mesh facets in the form of an Eigen matrix.
Definition: views.cpp:162
RowMatrixView< ValueType > matrix_ref(Attribute< ValueType > &attribute)
Returns a writable view of a given attribute in the form of an Eigen matrix.
Definition: views.cpp:26
#define LA_SURFACE_MESH_INDEX_X(mode, data)
X Macro arguments to iterate over index types available for the SurfaceMesh<> class.
Definition: SurfaceMeshTypes.h:71
#define LA_SURFACE_MESH_SCALAR_X(mode, data)
X Macro arguments to iterate over scalar types available for the SurfaceMesh<> class.
Definition: SurfaceMeshTypes.h:91
#define la_runtime_assert(...)
Runtime assertion check.
Definition: assert.h:169
LA_CORE_API bool starts_with(std::string_view str, std::string_view prefix)
Checks if the string begins with the given prefix.
Definition: strings.cpp:38
#define LA_IGNORE(x)
Ignore x to avoid an unused variable warning.
Definition: warning.h:144
std::vector< Index > fast_edge_sort(Index num_edges, Index num_vertices, Func get_edge, span< Index > vertex_to_first_edge={})
Sort an array of edges using a parallel bucket sort.
Definition: fast_edge_sort.h:71
Main namespace for Lagrange.
Definition: AABBIGL.h:30
@ Error
Throw an error if collision is detected.
static constexpr std::string_view normal
Normal.
Definition: attribute_names.h:27
static constexpr std::string_view color
Color.
Definition: attribute_names.h:32
static constexpr std::string_view texcoord
Texture coordinates.
Definition: attribute_names.h:37
MeshTrait class provide compiler check for different mesh types.
Definition: MeshTrait.h:108