Lagrange
map_attributes.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 <vector>
15
16#include <lagrange/Mesh.h>
17#include <lagrange/MeshTrait.h>
18#include <lagrange/attributes/map_corner_attributes.h>
19#include <lagrange/attributes/map_facet_attributes.h>
20#include <lagrange/attributes/map_indexed_attributes.h>
21#include <lagrange/attributes/map_vertex_attributes.h>
22#include <lagrange/common.h>
23#include <lagrange/legacy/inline.h>
24#include <lagrange/utils/range.h>
25
26namespace lagrange {
27LAGRANGE_LEGACY_INLINE
28namespace legacy {
29
30/*
31Inverts a mapping from some indexes to other indexes. This mapping can be either
32forward (meaning "from" to "to"), or backwards ("to" to "from").
33
34 - map: the forward or backward map as a vector of indexes.
35 The number of elements that we are mapping 'from' is the size of this vector.
36
37 - target_count: Number of elements that we are mapping 'to'. The returned vector will have this
38size.
39*/
40template <typename Index>
41std::vector<Index> invert_mapping(const std::vector<Index>& map, Index target_count)
42{
43 if (map.empty()) {
44 return {}; // empty vector
45 }
46
47 std::vector<Index> ret(target_count, invalid<Index>());
48 for (auto i : range((Index)map.size())) {
49 Index value = map[i];
50 if (value != invalid<Index>()) {
51 la_runtime_assert(value < target_count);
52 ret[value] = i;
53 }
54 }
55 return ret;
56}
57
58/*
59Mapping defines a mapping from a lagrange::Mesh to another.
60
61Does not hold references to the two meshes.
62
63Holds a vector of indexes for each of vertices, facets, corners.
64Those vectors can be either empty, meaning that the correspondence
65has not changed and that those elements can be mapped 1 to 1.
66
67Or, they can have the same size as the elements in a mesh, and for each
68element i, the element at [i] will contain the index in the other mesh.
69This index can be INVALID in case the element does not exist in the old mesh.
70*/
71
72template <typename MeshType>
74{
75 using Index = typename MeshType::Index;
76 std::vector<Index> vertex;
77 std::vector<Index> facet;
78};
79
80
81// Forward mapping: ("from" mesh) --> ("to" mesh)
82// For each element of "from" (vertices, facets, corners), has index of element in "to".
83template <typename MeshType>
84struct ForwardMeshMapping : public MeshMapping<MeshType>
85{
86};
87
88
89// Backward mapping: ("from" mesh) <-- ("to" mesh)
90// For each element of "to" (vertices, facets), has index of element in "from".
91template <typename MeshType>
92struct BackwardMeshMapping : public MeshMapping<MeshType>
93{
94};
95
96
97template <typename MeshType>
98MeshMapping<MeshType> invert_mapping(const MeshMapping<MeshType>& map1, const MeshType& target_mesh)
99{
100 static_assert(MeshTrait<MeshType>::is_mesh(), "Input type is not Mesh");
102 map2.vertex = invert_mapping(map1.vertex, target_mesh.get_num_vertices());
103 map2.facet = invert_mapping(map1.facet, target_mesh.get_num_facets());
104 return map2;
105}
106
107template <typename MeshType>
108void map_attributes(const MeshType& from, MeshType& to, const BackwardMeshMapping<MeshType>& map)
109{
110 static_assert(MeshTrait<MeshType>::is_mesh(), "Input type is not Mesh");
111 map_vertex_attributes(from, to, map.vertex);
112 map_facet_attributes(from, to, map.facet);
113 map_corner_attributes(from, to);
114 map_indexed_attributes(from, to);
115}
116
117template <typename MeshType>
118void map_attributes(const MeshType& from, MeshType& to, const ForwardMeshMapping<MeshType>& map)
119{
120 static_assert(MeshTrait<MeshType>::is_mesh(), "Input type is not Mesh");
121 map_attributes(from, to, invert_mapping(map, to));
122}
123
124template <typename MeshType>
125void map_attributes(
126 const MeshType& from,
127 MeshType& to,
128 const std::vector<typename MeshType::Index>& backward_vertex_mapping = {},
129 const std::vector<typename MeshType::Index>& backward_facet_mapping = {})
130{
131 static_assert(MeshTrait<MeshType>::is_mesh(), "Input type is not Mesh");
132 map_vertex_attributes(from, to, backward_vertex_mapping);
133 map_facet_attributes(from, to, backward_facet_mapping);
134 if (backward_facet_mapping.empty() && to.get_num_facets() != 0) {
135 map_corner_attributes(from, to);
136 map_indexed_attributes(from, to);
137 } else {
138 map_corner_attributes(from, to, backward_facet_mapping);
139 map_indexed_attributes(from, to, backward_facet_mapping);
140 }
141}
142
143} // namespace legacy
144} // namespace lagrange
Definition: Mesh.h:48
#define la_runtime_assert(...)
Runtime assertion check.
Definition: assert.h:169
internal::Range< Index > range(Index end)
Returns an iterable object representing the range [0, end).
Definition: range.h:176
void map_attributes(const SurfaceMesh< Scalar, Index > &source_mesh, SurfaceMesh< Scalar, Index > &target_mesh, span< const Index > mapping_data, span< const Index > mapping_offsets={}, const MapAttributesOptions &options={})
Map attributes from the source mesh to the target mesh.
Definition: map_attributes.cpp:26
InverseMapping< Index > invert_mapping(Index num_source_elements, Function old_to_new, Index num_target_elements=invalid< Index >())
Compute the target-to-source (i.e.
Definition: invert_mapping.h:61
Main namespace for Lagrange.
Definition: AABBIGL.h:30
MeshTrait class provide compiler check for different mesh types.
Definition: MeshTrait.h:108
Definition: map_attributes.h:93
Definition: map_attributes.h:85
Definition: map_attributes.h:74