Wildmeshing Toolkit
Loading...
Searching...
No Matches
AttributeManager.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <vector>
10
11namespace wmtk {
12class Mesh;
13class MeshWriter;
14
15namespace attribute {
17{
19
20public:
21 AttributeManager(int64_t size);
27
28 //=========================================================
29 // Storage of Mesh Attributes
30 //=========================================================
31 std::vector<TypedAttributeManager<char>> m_char_attributes;
32 std::vector<TypedAttributeManager<int64_t>> m_long_attributes;
33 std::vector<TypedAttributeManager<double>> m_double_attributes;
34 std::vector<TypedAttributeManager<Rational>> m_rational_attributes;
35
36
37 // max index used for each type of simplex
38 std::vector<int64_t> m_capacities;
39
40 // the number of types of attributes (types of simplex)
41 int64_t size() const;
42
43 // attribute directly hashes its "children" components so it overrides "child_hashes"
44 std::map<std::string, const wmtk::utils::Hashable*> child_hashables() const override;
45 std::map<std::string, std::size_t> child_hashes() const override;
46
48 void serialize(MeshWriter& writer) const;
49 void reserve_to_fit();
51 void reserve_attributes(int64_t dimension, int64_t size);
52 // specifies the number of simplices of each type and resizes attributes appropritely
53 void set_capacities(std::vector<int64_t> capacities);
54 void reserve_more_attributes(int64_t dimension, int64_t size);
55 void reserve_more_attributes(const std::vector<int64_t>& more_capacities);
56 void guarantee_more_attributes(int64_t dimension, int64_t size);
57 void guarantee_more_attributes(const std::vector<int64_t>& more_capacities);
58 void guarantee_at_least_attributes(int64_t dimension, int64_t size);
59 void guarantee_at_least_attributes(const std::vector<int64_t>& at_least_capacities);
60 bool operator==(const AttributeManager& other) const;
61
62 void assert_capacity_valid() const;
63
64 template <typename T>
66 const std::string& name,
67 PrimitiveType type,
68 int64_t size,
69 bool replace,
70 T default_value);
71
72 std::vector<MeshAttributeHandle::HandleVariant> get_all_attributes() const;
73
74
75 template <typename T>
76 std::vector<TypedAttributeManager<T>>& get();
77
78 template <typename T>
80
81 template <typename T>
83
84 template <typename T>
85 std::string get_name(const TypedAttributeHandle<T>& attr) const;
86
87 std::string get_name(const attribute::MeshAttributeHandle::HandleVariant& attr) const;
88
89 template <typename T>
90 void set_name(const TypedAttributeHandle<T>& attr, const std::string& name);
91
92 template <typename T>
93 const std::vector<TypedAttributeManager<T>>& get() const;
94
95 template <typename T>
96 const TypedAttributeManager<T>& get(PrimitiveType ptype) const;
97
98 template <typename T>
99 const TypedAttributeManager<T>& get(const TypedAttributeHandle<T>& handle) const;
100
101 void push_scope();
102 void pop_scope(bool apply_updates = true);
105
106 void change_to_parent_scope() const;
107 void change_to_child_scope() const;
108 template <typename Functor, typename... Args>
109 decltype(auto) parent_scope(Functor&& f, Args&&... args) const;
110
111 template <typename T>
112 int64_t get_attribute_dimension(const TypedAttributeHandle<T>& handle) const;
113
114 template <typename T>
115 const T& get_attribute_default_value(const TypedAttributeHandle<T>& handle) const;
116
117
123 void clear_attributes(
124 const std::vector<attribute::MeshAttributeHandle::HandleVariant>& custom_attributes);
126
127 template <typename T>
128 bool validate_handle(const TypedAttributeHandle<T>& handle) const;
129};
130
131template <typename T>
132inline const std::vector<TypedAttributeManager<T>>& AttributeManager::get() const
133{
134 if constexpr (std::is_same_v<T, char>) {
135 return m_char_attributes;
136 }
137 if constexpr (std::is_same_v<T, int64_t>) {
138 return m_long_attributes;
139 }
140 if constexpr (std::is_same_v<T, double>) {
141 return m_double_attributes;
142 }
143 if constexpr (std::is_same_v<T, Rational>) {
145 }
146}
147template <typename T>
148inline std::vector<TypedAttributeManager<T>>& AttributeManager::get()
149{
150 if constexpr (std::is_same_v<T, char>) {
151 return m_char_attributes;
152 }
153 if constexpr (std::is_same_v<T, int64_t>) {
154 return m_long_attributes;
155 }
156 if constexpr (std::is_same_v<T, double>) {
157 return m_double_attributes;
158 }
159 if constexpr (std::is_same_v<T, Rational>) {
161 }
162}
163
164template <typename T>
166{
167 const int8_t index = get_primitive_type_id(ptype);
168 return get<T>()[index];
169}
170
171template <typename T>
173{
174 size_t index = get_primitive_type_id(ptype);
175 return get<T>().at(index);
176}
177
178template <typename T>
180{
181 return get<T>(handle.m_primitive_type);
182}
183template <typename T>
185 const TypedAttributeHandle<T>& handle) const
186{
187 return get<T>(handle.m_primitive_type);
188}
189template <typename T>
191 const std::string& name,
192 PrimitiveType ptype,
193 int64_t size,
194 bool replace,
195 T default_value)
196{
198 r.m_base_handle = get<T>(ptype).register_attribute(name, size, replace, default_value),
199 r.m_primitive_type = ptype;
200
201 return r;
202}
203
204template <typename Functor, typename... Args>
205inline decltype(auto) AttributeManager::parent_scope(Functor&& f, Args&&... args) const
206{
207 // we const-cast here because the scope object resets its state at the end
208 // of this scope and we want to use parent-scope for read-only applications
209 // anyway ( so it's all read-only-like )
210 internal::CheckpointScope scope(const_cast<AttributeManager&>(*this));
211 return std::invoke(std::forward<Functor>(f), std::forward<Args>(args)...);
212}
213template <typename T>
215 const TypedAttributeHandle<T>& handle) const
216{
217 assert(handle.is_valid());
218 return get(handle).dimension(handle.m_base_handle);
219}
220
221template <typename T>
223 const TypedAttributeHandle<T>& handle) const
224{
225 assert(handle.is_valid());
226 return get(handle).default_value(handle.m_base_handle);
227}
228
229template <typename T>
231{
232 return get(handle).validate_handle(handle.m_base_handle);
233}
234
235template <typename T>
236inline std::string AttributeManager::get_name(const TypedAttributeHandle<T>& handle) const
237{
238 return get(handle).get_name(handle.m_base_handle);
239}
240
241template <typename T>
243 const TypedAttributeHandle<T>& handle,
244 const std::string& name)
245{
246 return get(handle).set_name(handle.m_base_handle, name);
247}
248
249} // namespace attribute
250} // namespace wmtk
std::vector< TypedAttributeManager< Rational > > m_rational_attributes
decltype(auto) parent_scope(Functor &&f, Args &&... args) const
AttributeScopeHandle create_scope(Mesh &m)
std::vector< TypedAttributeManager< T > > & get()
void set_capacities(std::vector< int64_t > capacities)
void serialize(MeshWriter &writer) const
void pop_scope(bool apply_updates=true)
std::vector< TypedAttributeManager< double > > m_double_attributes
void guarantee_at_least_attributes(int64_t dimension, int64_t size)
bool validate_handle(const TypedAttributeHandle< T > &handle) const
AttributeManager(AttributeManager &&o)=default
int64_t get_attribute_dimension(const TypedAttributeHandle< T > &handle) const
std::map< std::string, const wmtk::utils::Hashable * > child_hashables() const override
bool operator==(const AttributeManager &other) const
const T & get_attribute_default_value(const TypedAttributeHandle< T > &handle) const
AttributeManager & operator=(const AttributeManager &o)=delete
std::map< std::string, std::size_t > child_hashes() const override
void clear_attributes(const std::vector< attribute::MeshAttributeHandle::HandleVariant > &custom_attributes)
Remove all custom attributes besides the one passed in.
void guarantee_more_attributes(int64_t dimension, int64_t size)
std::vector< MeshAttributeHandle::HandleVariant > get_all_attributes() const
AttributeManager & operator=(AttributeManager &&o)=default
void reserve_more_attributes(int64_t dimension, int64_t size)
std::string get_name(const TypedAttributeHandle< T > &attr) const
std::vector< TypedAttributeManager< int64_t > > m_long_attributes
AttributeManager(const AttributeManager &o)=delete
TypedAttributeHandle< T > register_attribute(const std::string &name, PrimitiveType type, int64_t size, bool replace, T default_value)
void delete_attribute(const attribute::MeshAttributeHandle::HandleVariant &to_delete)
void set_name(const TypedAttributeHandle< T > &attr, const std::string &name)
void reserve_attributes(int64_t dimension, int64_t size)
std::vector< TypedAttributeManager< char > > m_char_attributes
This handle is a wrapper for the MeshManager scope funtions.
std::variant< TypedAttributeHandle< char >, TypedAttributeHandle< int64_t >, TypedAttributeHandle< double >, TypedAttributeHandle< wmtk::Rational > > HandleVariant
Handle that represents attributes for some mesh.
wmtk::attribute::AttributeHandle m_base_handle
Contains all attributes of type T for a single mesh.
constexpr int8_t get_primitive_type_id(PrimitiveType t)
Get a unique integer id corresponding to each primitive type.