Lagrange
map_facet_attributes.h
1/*
2 * Copyright 2018 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/common.h>
19#include <lagrange/legacy/inline.h>
20#include <lagrange/utils/safe_cast.h>
21
22namespace lagrange {
23LAGRANGE_LEGACY_INLINE
24namespace legacy {
25
26/*
27map_facet_attributes assumes a backward mapping, meaning that in the
28facet_map, for each element of "to", we have its respective index in "from".
29
30You can use invert_mapping in map_attributes.h to convert a forward mapping
31*/
32
33template <typename MeshType1, typename MeshType2>
34void map_facet_attributes(
35 const MeshType1& from,
36 MeshType2& to,
37 const std::vector<typename MeshType1::Index>& facet_map = {})
38{
39 static_assert(MeshTrait<MeshType1>::is_mesh(), "Input type is not Mesh");
40 static_assert(MeshTrait<MeshType2>::is_mesh(), "Input type is not Mesh");
41 using Index = typename MeshType1::Index;
43 facet_map.empty() || safe_cast<Index>(facet_map.size()) == to.get_num_facets());
44
45 const auto& facet_attributes = from.get_facet_attribute_names();
46 const auto num_facets = to.get_num_facets();
47 const bool use_map = safe_cast<Index>(facet_map.size()) == num_facets;
48 for (const auto& name : facet_attributes) {
49 auto attr = from.get_facet_attribute_array(name);
50 to.add_facet_attribute(name);
51 if (use_map) {
52 auto attr2 = to_shared_ptr(attr->row_slice(facet_map));
53 to.set_facet_attribute_array(name, std::move(attr2));
54 } else {
55 auto attr2 = to_shared_ptr(attr->clone());
56 to.set_facet_attribute_array(name, std::move(attr2));
57 }
58 }
59}
60} // namespace legacy
61} // namespace lagrange
#define la_runtime_assert(...)
Runtime assertion check.
Definition: assert.h:169
Main namespace for Lagrange.
Definition: AABBIGL.h:30
std::shared_ptr< T > to_shared_ptr(std::unique_ptr< T > &&ptr)
Helper for automatic type deduction for unique_ptr to shared_ptr conversion.
Definition: common.h:88