16#include <lagrange/Mesh.h>
17#include <lagrange/MeshTrait.h>
18#include <lagrange/attributes/map_facet_attributes.h>
19#include <lagrange/attributes/map_vertex_attributes.h>
20#include <lagrange/create_mesh.h>
21#include <lagrange/legacy/inline.h>
27template <
typename MeshType>
28auto quad_to_tri(
const MeshType& mesh)
30 static_assert(MeshTrait<MeshType>::is_mesh(),
"Input type is not Mesh");
31 const auto& vertices = mesh.get_vertices();
32 const auto& facets = mesh.get_facets();
35 using Index =
typename MeshType::Index;
36 using FacetArray = Eigen::Matrix<Index, Eigen::Dynamic, 3, Eigen::RowMajor>;
37 using AttributeArray =
typename MeshType::AttributeArray;
39 const Index num_quads = mesh.get_num_facets();
40 FacetArray triangles(num_quads * 2, 3);
41 for (Index i = 0; i < num_quads; i++) {
42 triangles.row(i * 2) << facets(i, 0), facets(i, 1), facets(i, 3);
43 triangles.row(i * 2 + 1) << facets(i, 3), facets(i, 1), facets(i, 2);
48 if (mesh.is_uv_initialized()) {
49 const auto& uv = mesh.get_uv();
50 const auto& uv_indices = mesh.get_uv_indices();
51 assert(uv_indices.rows() == num_quads);
52 assert(uv_indices.cols() == 4);
54 FacetArray tri_uv_indices(num_quads * 2, 3);
56 for (Index i = 0; i < num_quads; i++) {
57 tri_uv_indices.row(i * 2) << uv_indices(i, 0), uv_indices(i, 1), uv_indices(i, 3);
58 tri_uv_indices.row(i * 2 + 1) << uv_indices(i, 3), uv_indices(i, 1), uv_indices(i, 2);
60 tri_mesh->initialize_uv(uv, tri_uv_indices);
63 map_vertex_attributes(mesh, *tri_mesh);
64 std::vector<Index> facet_map(num_quads * 2);
65 for (Index i = 0; i < num_quads; i++) {
67 facet_map[i * 2 + 1] = i;
69 map_facet_attributes(mesh, *tri_mesh, facet_map);
71 const auto& corner_attr_names = mesh.get_corner_attribute_names();
72 for (
const auto& name : corner_attr_names) {
73 const auto& attr = mesh.get_corner_attribute(name);
74 AttributeArray tri_attr(num_quads * 6, attr.cols());
75 for (Index i = 0; i < num_quads; i++) {
76 tri_attr.row(i * 6) = attr.row(i * 4);
77 tri_attr.row(i * 6 + 1) = attr.row(i * 4 + 1);
78 tri_attr.row(i * 6 + 2) = attr.row(i * 4 + 3);
79 tri_attr.row(i * 6 + 3) = attr.row(i * 4 + 3);
80 tri_attr.row(i * 6 + 4) = attr.row(i * 4 + 1);
81 tri_attr.row(i * 6 + 5) = attr.row(i * 4 + 2);
83 tri_mesh->add_corner_attribute(name);
84 tri_mesh->set_corner_attribute(name, tri_attr);
#define la_runtime_assert(...)
Runtime assertion check.
Definition: assert.h:169
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