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
130 bool validate() const;
131
132 template <typename T>
133 bool validate_handle(const TypedAttributeHandle<T>& handle) const;
134};
135
136template <typename T>
137inline const std::vector<TypedAttributeManager<T>>& AttributeManager::get() const
138{
139 if constexpr (std::is_same_v<T, char>) {
140 return m_char_attributes;
141 }
142 if constexpr (std::is_same_v<T, int64_t>) {
143 return m_long_attributes;
144 }
145 if constexpr (std::is_same_v<T, double>) {
146 return m_double_attributes;
147 }
148 if constexpr (std::is_same_v<T, Rational>) {
150 }
151}
152template <typename T>
153inline std::vector<TypedAttributeManager<T>>& AttributeManager::get()
154{
155 if constexpr (std::is_same_v<T, char>) {
156 return m_char_attributes;
157 }
158 if constexpr (std::is_same_v<T, int64_t>) {
159 return m_long_attributes;
160 }
161 if constexpr (std::is_same_v<T, double>) {
162 return m_double_attributes;
163 }
164 if constexpr (std::is_same_v<T, Rational>) {
166 }
167}
168
169template <typename T>
171{
172 const int8_t index = get_primitive_type_id(ptype);
173 return get<T>()[index];
174}
175
176template <typename T>
178{
179 size_t index = get_primitive_type_id(ptype);
180 return get<T>().at(index);
181}
182
183template <typename T>
185{
186 return get<T>(handle.m_primitive_type);
187}
188template <typename T>
190{
191 return get<T>(handle.m_primitive_type);
192}
193template <typename T>
195 const std::string& name,
196 PrimitiveType ptype,
197 int64_t size,
198 bool replace,
199 T default_value)
200{
202 r.m_base_handle = get<T>(ptype).register_attribute(name, size, replace, default_value),
203 r.m_primitive_type = ptype;
204
205 return r;
206}
207
208template <typename Functor, typename... Args>
209inline decltype(auto) AttributeManager::parent_scope(Functor&& f, Args&&... args) const
210{
211 // we const-cast here because the scope object resets its state at the end
212 // of this scope and we want to use parent-scope for read-only applications
213 // anyway ( so it's all read-only-like )
214 internal::CheckpointScope scope(const_cast<AttributeManager&>(*this));
215 return std::invoke(std::forward<Functor>(f), std::forward<Args>(args)...);
216}
217template <typename T>
219 const TypedAttributeHandle<T>& handle) const
220{
221 assert(handle.is_valid());
222 return get(handle).dimension(handle.m_base_handle);
223}
224
225template <typename T>
227 const TypedAttributeHandle<T>& handle) const
228{
229 assert(handle.is_valid());
230 return get(handle).default_value(handle.m_base_handle);
231}
232
233template <typename T>
235{
236 return get(handle).validate_handle(handle.m_base_handle);
237}
238
239template <typename T>
240inline std::string AttributeManager::get_name(const TypedAttributeHandle<T>& handle) const
241{
242 return get(handle).get_name(handle.m_base_handle);
243}
244
245template <typename T>
247 const TypedAttributeHandle<T>& handle,
248 const std::string& name)
249{
250 return get(handle).set_name(handle.m_base_handle, name);
251}
252
253} // namespace attribute
254} // 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()
bool validate() const
Validate that handles and attributes are in sync.
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.