Lagrange
map_indexed_attributes.h
1/*
2 * Copyright 2020 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 <lagrange/Mesh.h>
15#include <lagrange/MeshTrait.h>
16#include <lagrange/common.h>
17#include <lagrange/legacy/inline.h>
18#include <lagrange/utils/safe_cast.h>
19#include <tbb/parallel_for.h>
20
21namespace lagrange {
22LAGRANGE_LEGACY_INLINE
23namespace legacy {
24
25template <typename MeshType>
26void map_indexed_attributes(const MeshType& from, MeshType& to)
27{
28 static_assert(MeshTrait<MeshType>::is_mesh(), "Input type is not Mesh");
29 la_runtime_assert(from.get_num_facets() == to.get_num_facets());
30
31 using AttributeArray = typename MeshType::AttributeArray;
32 using IndexArray = typename MeshType::IndexArray;
33
34 const auto names = from.get_indexed_attribute_names();
35
36 AttributeArray attr;
37 IndexArray indices;
38 for (const auto& name : names) {
39 std::tie(attr, indices) = from.get_indexed_attribute(name);
40 to.add_indexed_attribute(name);
41 to.import_indexed_attribute(name, attr, indices);
42 }
43}
44
45template <typename MeshType>
46void map_indexed_attributes(
47 const MeshType& from,
48 MeshType& to,
49 const std::vector<typename MeshType::Index>& facet_map)
50{
51 static_assert(MeshTrait<MeshType>::is_mesh(), "Input type is not Mesh");
52
53 using Index = typename MeshType::Index;
54 using AttributeArray = typename MeshType::AttributeArray;
55 using IndexArray = typename MeshType::IndexArray;
56
57 const auto num_out_facets = to.get_num_facets();
58 const auto vertex_per_facet = to.get_vertex_per_facet();
59 const auto names = from.get_indexed_attribute_names();
60
61 AttributeArray attr;
62 IndexArray from_indices, to_indices;
63 for (const auto& name : names) {
64 std::tie(attr, from_indices) = from.get_indexed_attribute(name);
65 assert(safe_cast<Index>(from_indices.rows()) == from.get_num_facets());
66 to_indices.resize(num_out_facets, vertex_per_facet);
67 to_indices.setConstant(invalid<Index>());
68 tbb::parallel_for(Index(0), num_out_facets, [&](Index i) {
69 to_indices.row(i) = from_indices.row(facet_map[i]);
70 });
71 assert((to_indices.array() != invalid<Index>()).all());
72 to.add_indexed_attribute(name);
73 to.import_indexed_attribute(name, attr, to_indices);
74 }
75}
76
77} // namespace legacy
78} // 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