Lagrange
IndexedAttributes.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 <map>
15#include <string>
16
17#include <lagrange/GenuineMeshGeometry.h>
18#include <lagrange/utils/assert.h>
19
20namespace lagrange {
21
23template <typename _AttributeArray, typename _IndexArray>
25{
26public:
27 using AttributeArray = _AttributeArray;
28 using IndexArray = _IndexArray;
30 using Scalar = typename AttributeArray::Scalar;
31 using Index = typename IndexArray::Scalar;
32
33public:
34 std::vector<std::string> get_attribute_names() const
35 {
36 std::vector<std::string> names;
37 for (const auto& itr : m_data) {
38 names.push_back(itr.first);
39 }
40 return names;
41 }
42
43 bool has_attribute(const std::string& name) const { return m_data.find(name) != m_data.end(); }
44
45 void add_attribute(const std::string& name)
46 {
47 m_data.emplace(name, std::make_shared<AttributeData>());
48 }
49
50 template <typename ValueDerived, typename IndexDerived>
51 void add_attribute(
52 const std::string& name,
53 const Eigen::PlainObjectBase<ValueDerived>& values,
54 const Eigen::PlainObjectBase<IndexDerived>& indices)
55 {
56 auto data =
57 std::make_shared<GenuineMeshGeometry<AttributeArray, IndexArray>>(values, indices);
58 m_data.emplace(name, std::move(data));
59 }
60
61 template <typename ValueDerived, typename IndexDerived>
62 void set_attribute(
63 const std::string& name,
64 const Eigen::PlainObjectBase<ValueDerived>& values,
65 const Eigen::PlainObjectBase<IndexDerived>& indices)
66 {
67 auto itr = m_data.find(name);
68 la_runtime_assert(itr != m_data.end(), "Attribute " + name + " does not exist");
69 itr->second =
70 std::make_shared<GenuineMeshGeometry<AttributeArray, IndexArray>>(values, indices);
71 }
72
73 const AttributeData& get_attribute(const std::string& name) const
74 {
75 auto itr = m_data.find(name);
76 la_runtime_assert(itr != m_data.end(), "Attribute " + name + " does not exist");
77 return *itr->second;
78 }
79
80 AttributeData& get_attribute(const std::string& name)
81 {
82 auto itr = m_data.find(name);
83 la_runtime_assert(itr != m_data.end(), "Attribute " + name + " does not exist");
84 return *itr->second;
85 }
86
87 const AttributeArray& get_attribute_values(const std::string& name) const
88 {
89 return get_attribute(name).get_vertices();
90 }
91
92 AttributeArray& get_attribute_values(const std::string& name)
93 {
94 return get_attribute(name).get_vertices();
95 }
96
97 const IndexArray& get_attribute_indices(const std::string& name) const
98 {
99 return get_attribute(name).get_facets();
100 }
101
102 IndexArray& get_attribute_indices(const std::string& name)
103 {
104 return get_attribute(name).get_facets();
105 }
106
107 void remove_attribute(const std::string& name)
108 {
109 auto itr = m_data.find(name);
110 la_runtime_assert(itr != m_data.end(), "Attribute " + name + " does not exist");
111 m_data.erase(itr);
112 }
113
114 template <typename ValueDerived, typename IndexDerived>
115 void import_attribute(
116 const std::string& name,
117 Eigen::PlainObjectBase<ValueDerived>& values,
118 Eigen::PlainObjectBase<IndexDerived>& indices)
119 {
120 auto itr = m_data.find(name);
121 la_runtime_assert(itr != m_data.end(), "Attribute " + name + " does not exist");
122 itr->second->import_vertices(values);
123 itr->second->import_facets(indices);
124 }
125
126 template <typename ValueDerived, typename IndexDerived>
127 void export_attribute(
128 const std::string& name,
129 Eigen::PlainObjectBase<ValueDerived>& values,
130 Eigen::PlainObjectBase<IndexDerived>& indices)
131 {
132 auto itr = m_data.find(name);
133 la_runtime_assert(itr != m_data.end(), "Attribute " + name + " does not exist");
134 itr->second->export_vertices(values);
135 itr->second->export_facets(indices);
136 }
137
138 decltype(auto) get_attribute_as_mesh(const std::string& name)
139 {
140 auto itr = m_data.find(name);
141 la_runtime_assert(itr != m_data.end(), "Attribute " + name + " does not exist");
143 return std::make_unique<MeshType>(itr->second);
144 }
145
146private:
147 std::map<std::string, std::shared_ptr<AttributeData>> m_data;
148};
149
150} // namespace lagrange
Definition: GenuineMeshGeometry.h:25
Legacy indexed attribute class.
Definition: IndexedAttributes.h:25
Definition: Mesh.h:48
#define la_runtime_assert(...)
Runtime assertion check.
Definition: assert.h:169
Main namespace for Lagrange.
Definition: AABBIGL.h:30