17#include <lagrange/Logger.h>
18#include <lagrange/common.h>
19#include <lagrange/experimental/IndexedAttribute.h>
20#include <lagrange/utils/assert.h>
23namespace experimental {
28 std::vector<std::string> get_names()
const
30 std::vector<std::string> names;
31 for (
const auto& itr : m_data) {
32 names.push_back(itr.first);
37 size_t get_size()
const {
41 bool has(
const std::string& name)
const {
return m_data.find(name) != m_data.end(); }
43 void add(
const std::string& name)
45 m_data.emplace(name, std::make_unique<IndexedAttribute>());
48 template <
typename ValueDerived,
typename IndexDerived>
49 void add(
const std::string& name, ValueDerived&& values, IndexDerived&& indices)
51 auto attr = std::make_unique<IndexedAttribute>(
52 std::forward<ValueDerived>(values),
53 std::forward<IndexDerived>(indices));
54 m_data.emplace(name, std::move(attr));
57 template <
typename ValueDerived,
typename IndexDerived>
58 void set(
const std::string& name, ValueDerived&& values, IndexDerived&& indices)
60 auto itr = m_data.find(name);
61 la_runtime_assert(itr != m_data.end(),
"Indexed attribute " + name +
" does not exist.");
62 if (itr->second ==
nullptr) {
63 itr->second = std::make_unique<IndexedAttribute>(
64 std::forward<ValueDerived>(values),
65 std::forward<IndexDerived>(indices));
67 itr->second->set_values(std::forward<ValueDerived>(values));
68 itr->second->set_indices(std::forward<IndexDerived>(indices));
74 auto itr = m_data.find(name);
75 la_runtime_assert(itr != m_data.end(),
"Indexed attribute " + name +
" does not exist.");
76 return itr->second.get();
81 auto itr = m_data.find(name);
82 la_runtime_assert(itr != m_data.end(),
"Indexed attribute " + name +
" does not exist.");
83 return itr->second.get();
86 template <
typename Derived>
87 decltype(
auto) get_values(
const std::string& name)
91 return ptr->get_values<Derived>();
94 template <
typename Derived>
95 decltype(
auto) get_values(
const std::string& name)
const
97 const auto ptr = get(name);
99 return ptr->get_values<Derived>();
102 template <
typename Derived>
103 decltype(
auto) get_indices(
const std::string& name)
105 auto ptr = get(name);
107 return ptr->get_indices<Derived>();
110 template <
typename Derived>
111 decltype(
auto) get_indices(
const std::string& name)
const
113 const auto ptr = get(name);
115 return ptr->get_indices<Derived>();
118 template <
typename Derived>
119 decltype(
auto) view_values(
const std::string& name)
121 auto ptr = get(name);
123 return ptr->view_values<Derived>();
126 template <
typename Derived>
127 decltype(
auto) view_values(
const std::string& name)
const
129 const auto ptr = get(name);
131 return ptr->view_values<Derived>();
134 template <
typename Derived>
135 decltype(
auto) view_indices(
const std::string& name)
137 auto ptr = get(name);
139 return ptr->view_indices<Derived>();
142 template <
typename Derived>
143 decltype(
auto) view_indices(
const std::string& name)
const
145 const auto ptr = get(name);
147 return ptr->view_indices<Derived>();
150 template <
typename ValueDerived,
typename IndexDerived>
151 void import_data(
const std::string& name, ValueDerived&& values, IndexDerived&& indices)
153 set(name, std::move(values.derived()), std::move(indices.derived()));
156 template <
typename ValueDerived,
typename IndexDerived>
158 const std::string& name,
159 Eigen::PlainObjectBase<ValueDerived>& values,
160 Eigen::PlainObjectBase<IndexDerived>& indices)
162 auto attr = get(name);
164 auto value_array = attr->get_values();
165 auto index_array = attr->get_indices();
170 bool validate =
true;
172 void* index_ptr = index_array->data();
175 ValueDerived& value_matrix = value_array->template get<ValueDerived>();
176 IndexDerived& index_matrix = index_array->template get<IndexDerived>();
177 values.swap(value_matrix);
178 indices.swap(index_matrix);
179 }
catch (std::runtime_error& e) {
182 logger().warn(
"Export cannot be done without coping: {}", e.what());
183 values = value_array->view<ValueDerived>();
184 indices = index_array->view<IndexDerived>();
193 la_runtime_assert(index_ptr == indices.data(),
"Export indices fall back to copying.");
198 void remove(
const std::string& name)
200 auto itr = m_data.find(name);
201 la_runtime_assert(itr != m_data.end(),
"Attribute " + name +
" does not exist.");
205 template <
typename Archive>
206 void serialize_impl(Archive& ar)
210 ar.object([&](
auto& ar) {
211 ar(
"data", DATA) & m_data;
213 LA_IGNORE_SHADOW_WARNING_END
217 std::map<std::string, std::unique_ptr<IndexedAttribute>> m_data;
222template <
typename Archive>
223void serialize(std::pair<std::string, IndexedAttribute>& entry, Archive& ar)
225 enum { KEY = 0, VALUE = 1 };
227 ar.object([&](
auto& ar) {
228 ar(
"key", KEY) & entry.first;
229 ar(
"value", VALUE) & entry.second;
233template <
typename Archive>
235 std::map<std::string, std::unique_ptr<IndexedAttribute>>& attrs,
238 std::vector<std::pair<std::string, ::lagrange::experimental::IndexedAttribute>> data;
239 if (!ar.is_input()) {
240 for (
auto& itr : attrs) {
241 data.emplace_back(itr.first, *itr.second);
249 for (
const auto& itr : data) {
250 attrs[itr.first] = std::make_unique<IndexedAttribute>(itr.second);
255template <
typename Archive>
256void serialize(IndexedAttributeManager& attributes, Archive& ar)
258 attributes.serialize_impl(ar);
261LA_IGNORE_SHADOW_WARNING_END
Definition: IndexedAttribute.h:22
Definition: IndexedAttributeManager.h:26
Smart pointer with value semantics.
Definition: value_ptr.h:134
LA_CORE_API spdlog::logger & logger()
Retrieves the current logger.
Definition: Logger.cpp:40
#define la_runtime_assert(...)
Runtime assertion check.
Definition: assert.h:169
#define LA_IGNORE_SHADOW_WARNING_BEGIN
Ignore shadow warnings.
Definition: warning.h:68
Main namespace for Lagrange.
Definition: AABBIGL.h:30