Lagrange
rename_attribute.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 <string>
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/assert.h>
21
22namespace lagrange {
23LAGRANGE_LEGACY_INLINE
24namespace legacy {
25
26template <typename MeshType>
27void rename_vertex_attribute(
28 MeshType& mesh,
29 const std::string& old_name,
30 const std::string& new_name)
31{
32 static_assert(MeshTrait<MeshType>::is_mesh(), "MeshType is not a mesh");
33
34 using AttributeArray = typename MeshType::AttributeArray;
35 la_runtime_assert(mesh.has_vertex_attribute(old_name));
36
37 AttributeArray attr_values;
38 mesh.export_vertex_attribute(old_name, attr_values);
39 mesh.remove_vertex_attribute(old_name);
40 mesh.add_vertex_attribute(new_name);
41 mesh.import_vertex_attribute(new_name, attr_values);
42}
43
44template <typename MeshType>
45void rename_facet_attribute(
46 MeshType& mesh,
47 const std::string& old_name,
48 const std::string& new_name)
49{
50 static_assert(MeshTrait<MeshType>::is_mesh(), "MeshType is not a mesh");
51
52 using AttributeArray = typename MeshType::AttributeArray;
53 la_runtime_assert(mesh.has_facet_attribute(old_name));
54
55 AttributeArray attr_values;
56 mesh.export_facet_attribute(old_name, attr_values);
57 mesh.remove_facet_attribute(old_name);
58 mesh.add_facet_attribute(new_name);
59 mesh.import_facet_attribute(new_name, attr_values);
60}
61
62template <typename MeshType>
63void rename_corner_attribute(
64 MeshType& mesh,
65 const std::string& old_name,
66 const std::string& new_name)
67{
68 static_assert(MeshTrait<MeshType>::is_mesh(), "MeshType is not a mesh");
69
70 using AttributeArray = typename MeshType::AttributeArray;
71 la_runtime_assert(mesh.has_corner_attribute(old_name));
72
73 AttributeArray attr_values;
74 mesh.export_corner_attribute(old_name, attr_values);
75 mesh.remove_corner_attribute(old_name);
76 mesh.add_corner_attribute(new_name);
77 mesh.import_corner_attribute(new_name, attr_values);
78}
79
80template <typename MeshType>
81void rename_edge_attribute(MeshType& mesh, const std::string& old_name, const std::string& new_name)
82{
83 static_assert(MeshTrait<MeshType>::is_mesh(), "MeshType is not a mesh");
84
85 using AttributeArray = typename MeshType::AttributeArray;
86 la_runtime_assert(mesh.has_edge_attribute(old_name));
87
88 AttributeArray attr_values;
89 mesh.export_edge_attribute(old_name, attr_values);
90 mesh.remove_edge_attribute(old_name);
91 mesh.add_edge_attribute(new_name);
92 mesh.import_edge_attribute(new_name, attr_values);
93}
94
95template <typename MeshType>
96void rename_indexed_attribute(
97 MeshType& mesh,
98 const std::string& old_name,
99 const std::string& new_name)
100{
101 static_assert(MeshTrait<MeshType>::is_mesh(), "MeshType is not a mesh");
102
103 using AttributeArray = typename MeshType::AttributeArray;
104 using IndexArray = typename MeshType::IndexArray;
105 la_runtime_assert(mesh.has_indexed_attribute(old_name));
106
107 AttributeArray attr_values;
108 IndexArray attr_indices;
109 mesh.export_indexed_attribute(old_name, attr_values, attr_indices);
110 mesh.remove_indexed_attribute(old_name);
111 mesh.add_indexed_attribute(new_name);
112 mesh.import_indexed_attribute(new_name, attr_values, attr_indices);
113}
114
115} // namespace legacy
116} // 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