Wildmeshing Toolkit
AttributeManager.hpp
Go to the documentation of this file.
1 #pragma once
2 
3 #include <vector>
7 #include "MeshAttributes.hpp"
10 
11 namespace wmtk {
12 class Mesh;
13 class MeshWriter;
14 
15 namespace attribute {
17 {
19 
20 public:
21  AttributeManager(int64_t size);
23  AttributeManager(const AttributeManager& o) = delete;
27 
28  //=========================================================
29  // Storage of Mesh Attributes
30  //=========================================================
31  std::vector<MeshAttributes<char>> m_char_attributes;
32  std::vector<MeshAttributes<int64_t>> m_long_attributes;
33  std::vector<MeshAttributes<double>> m_double_attributes;
34  std::vector<MeshAttributes<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<MeshAttributes<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<MeshAttributes<T>>& get() const;
94 
95  template <typename T>
96  const MeshAttributes<T>& get(PrimitiveType ptype) const;
97 
98  template <typename T>
99  const MeshAttributes<T>& get(const TypedAttributeHandle<T>& handle) const;
100 
101  void push_scope();
102  void pop_scope(bool apply_updates = true);
103  void rollback_current_scope();
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);
125  void delete_attribute(
126  const attribute::MeshAttributeHandle::HandleVariant& to_delete);
127 };
128 
129 template <typename T>
130 inline const std::vector<MeshAttributes<T>>& AttributeManager::get() const
131 {
132  if constexpr (std::is_same_v<T, char>) {
133  return m_char_attributes;
134  }
135  if constexpr (std::is_same_v<T, int64_t>) {
136  return m_long_attributes;
137  }
138  if constexpr (std::is_same_v<T, double>) {
139  return m_double_attributes;
140  }
141  if constexpr (std::is_same_v<T, Rational>) {
142  return m_rational_attributes;
143  }
144 }
145 template <typename T>
146 inline std::vector<MeshAttributes<T>>& AttributeManager::get()
147 {
148  if constexpr (std::is_same_v<T, char>) {
149  return m_char_attributes;
150  }
151  if constexpr (std::is_same_v<T, int64_t>) {
152  return m_long_attributes;
153  }
154  if constexpr (std::is_same_v<T, double>) {
155  return m_double_attributes;
156  }
157  if constexpr (std::is_same_v<T, Rational>) {
158  return m_rational_attributes;
159  }
160 }
161 
162 template <typename T>
164 {
165  const int8_t index = get_primitive_type_id(ptype);
166  return get<T>()[index];
167 }
168 
169 template <typename T>
171 {
172  size_t index = get_primitive_type_id(ptype);
173  return get<T>().at(index);
174 }
175 
176 template <typename T>
178 {
179  return get<T>(handle.m_primitive_type);
180 }
181 template <typename T>
183 {
184  return get<T>(handle.m_primitive_type);
185 }
186 template <typename T>
188  const std::string& name,
189  PrimitiveType ptype,
190  int64_t size,
191  bool replace,
192  T default_value)
193 {
195  r.m_base_handle = get<T>(ptype).register_attribute(name, size, replace, default_value),
196  r.m_primitive_type = ptype;
197 
198  return r;
199 }
200 
201 template <typename Functor, typename... Args>
202 inline decltype(auto) AttributeManager::parent_scope(Functor&& f, Args&&... args) const
203 {
204  // we const-cast here because the scope object resets its state at the end
205  // of this scope and we want to use parent-scope for read-only applications
206  // anyway ( so it's all read-only-like )
207  internal::CheckpointScope scope(const_cast<AttributeManager&>(*this));
208  return std::invoke(std::forward<Functor>(f), std::forward<Args>(args)...);
209 }
210 template <typename T>
212  const TypedAttributeHandle<T>& handle) const
213 {
214  assert(handle.is_valid());
215  return get(handle).dimension(handle.m_base_handle);
216 }
217 
218 template <typename T>
220  const TypedAttributeHandle<T>& handle) const
221 {
222  assert(handle.is_valid());
223  return get(handle).default_value(handle.m_base_handle);
224 }
225 
226 template <typename T>
227 inline std::string AttributeManager::get_name(const TypedAttributeHandle<T>& handle) const
228 {
229  return get(handle).get_name(handle.m_base_handle);
230 }
231 
232 template <typename T>
234  const TypedAttributeHandle<T>& handle,
235  const std::string& name)
236 {
237  return get(handle).set_name(handle.m_base_handle, name);
238 }
239 
240 } // namespace attribute
241 } // namespace wmtk
std::vector< MeshAttributes< double > > m_double_attributes
decltype(auto) parent_scope(Functor &&f, Args &&... args) const
AttributeScopeHandle create_scope(Mesh &m)
std::vector< MeshAttributes< char > > m_char_attributes
void set_capacities(std::vector< int64_t > capacities)
void serialize(MeshWriter &writer) const
void pop_scope(bool apply_updates=true)
void guarantee_at_least_attributes(int64_t dimension, int64_t size)
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
AttributeManager & operator=(AttributeManager &&o)=default
const T & get_attribute_default_value(const TypedAttributeHandle< T > &handle) const
std::vector< int64_t > m_capacities
std::map< std::string, std::size_t > child_hashes() const override
std::vector< MeshAttributes< Rational > > m_rational_attributes
void clear_attributes(const std::vector< attribute::MeshAttributeHandle::HandleVariant > &custom_attributes)
Remove all custom attributes besides the one passed in.
std::vector< MeshAttributes< T > > & get()
AttributeManager & operator=(const AttributeManager &o)=delete
void guarantee_more_attributes(int64_t dimension, int64_t size)
std::vector< MeshAttributeHandle::HandleVariant > get_all_attributes() const
void reserve_more_attributes(int64_t dimension, int64_t size)
std::string get_name(const TypedAttributeHandle< T > &attr) const
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< MeshAttributes< int64_t > > m_long_attributes
This handle is a wrapper for the MeshManager scope funtions.
std::variant< TypedAttributeHandle< char >, TypedAttributeHandle< int64_t >, TypedAttributeHandle< double >, TypedAttributeHandle< wmtk::Rational > > HandleVariant
Contains all attributes of type T for a single mesh.
Handle that represents attributes for some mesh.
wmtk::attribute::AttributeHandle m_base_handle
Definition: autodiff.h:995
Definition: Accessor.hpp:6
constexpr int8_t get_primitive_type_id(PrimitiveType t)
Get a unique integer id corresponding to each primitive type.