Lagrange
quad_to_tri.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 <Eigen/Core>
15
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>
22
23namespace lagrange {
24LAGRANGE_LEGACY_INLINE
25namespace legacy {
26
27template <typename MeshType>
28auto quad_to_tri(const MeshType& mesh)
29{
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();
33 la_runtime_assert(facets.cols() == 4);
34
35 using Index = typename MeshType::Index;
36 using FacetArray = Eigen::Matrix<Index, Eigen::Dynamic, 3, Eigen::RowMajor>;
37 using AttributeArray = typename MeshType::AttributeArray;
38
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);
44 }
45
46 auto tri_mesh = create_mesh(vertices, triangles);
47
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);
53
54 FacetArray tri_uv_indices(num_quads * 2, 3);
55
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);
59 }
60 tri_mesh->initialize_uv(uv, tri_uv_indices);
61 }
62
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++) {
66 facet_map[i * 2] = i;
67 facet_map[i * 2 + 1] = i;
68 }
69 map_facet_attributes(mesh, *tri_mesh, facet_map);
70
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);
82 }
83 tri_mesh->add_corner_attribute(name);
84 tri_mesh->set_corner_attribute(name, tri_attr);
85 }
86
87 return tri_mesh;
88}
89
90} // namespace legacy
91} // namespace lagrange
Definition: Mesh.h:48
#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