Lagrange
map_corner_attributes.h
1/*
2 * Copyright 2017 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
20namespace lagrange {
21LAGRANGE_LEGACY_INLINE
22namespace legacy {
23template <typename MeshType>
24void map_corner_attributes(const MeshType& from, MeshType& to)
25{
26 static_assert(MeshTrait<MeshType>::is_mesh(), "Input type is not Mesh");
27 la_runtime_assert(from.get_num_facets() == to.get_num_facets());
28
29 auto corner_attributes = from.get_corner_attribute_names();
30 for (const auto& name : corner_attributes) {
31 auto attr = from.get_corner_attribute_array(name);
32 to.add_corner_attribute(name);
33 to.set_corner_attribute_array(name, to_shared_ptr(attr->clone()));
34 }
35}
36
37/*
38map_corner_attributes assumes a backward mapping, meaning that in the
39facet_map, for each element of "to", we have its respective index in "from".
40
41You can use invert_mapping in map_attributes.h to convert a forward mapping
42*/
43template <typename MeshType>
44void map_corner_attributes(
45 const MeshType& from,
46 MeshType& to,
47 const std::vector<typename MeshType::Index>& facet_map)
48{
49 static_assert(MeshTrait<MeshType>::is_mesh(), "Input type is not Mesh");
50 using Index = typename MeshType::Index;
51
52 const Index dim = from.get_dim();
53 la_runtime_assert(to.get_dim() == dim);
54 la_runtime_assert(from.get_vertex_per_facet() == 3);
55 la_runtime_assert(to.get_vertex_per_facet() == 3);
56 la_runtime_assert(safe_cast<Index>(facet_map.size()) == to.get_num_facets());
57
58 auto corner_map_fn = [&](Eigen::Index i,
59 std::vector<std::pair<Eigen::Index, double>>& weights) {
60 const auto to_fid = i / 3;
61 const auto to_cid = i % 3;
62 weights.clear();
63 weights.emplace_back(facet_map[to_fid] * 3 + to_cid, 1.0);
64 };
65
66 const auto to_num_corners = to.get_num_facets() * 3;
67 const auto& corner_attributes = from.get_corner_attribute_names();
68 for (const auto& name : corner_attributes) {
69 auto attr = from.get_corner_attribute_array(name);
70 auto attr2 = to_shared_ptr(attr->row_slice(to_num_corners, corner_map_fn));
71 if (!to.has_corner_attribute(name)) {
72 to.add_corner_attribute(name);
73 }
74 to.set_corner_attribute_array(name, std::move(attr2));
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
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