Wildmeshing Toolkit
MeshAttributes.hpp
Go to the documentation of this file.
1 #pragma once
2 
4 #include "Attribute.hpp"
5 #include "AttributeHandle.hpp"
6 
7 
8 #include <Eigen/Core>
9 
10 #include <map>
11 #include <vector>
12 
13 
14 // TODO: this is just a fancy vector for attributes, perhaps this can be recycled / simplified. The reserved quantiy should be held by a level above this abstraction (as multiple types should all have hte same reservation size)
15 
16 namespace wmtk {
17 
18 class MeshWriter;
19 class Mesh;
20 namespace attribute {
21 template <typename T, int Dim>
22 class AccessorBase;
23 
28 template <typename T>
30 {
31  template <typename U, int D>
32  friend class AccessorBase;
33 
34  typedef Eigen::Map<Eigen::Matrix<T, Eigen::Dynamic, 1>> MapResult;
35  typedef Eigen::Map<const Eigen::Matrix<T, Eigen::Dynamic, 1>> ConstMapResult;
36 
37 
38 public:
39  MeshAttributes() = default;
40  MeshAttributes(const MeshAttributes& o) = delete;
44 
45  void serialize(const int dim, MeshWriter& writer) const;
46 
47  // attribute directly hashes its "child_hashables" components so it overrides "child_hashes"
48  std::map<std::string, const wmtk::utils::Hashable*> child_hashables() const override;
49  std::map<std::string, std::size_t> child_hashes() const override;
50 
51  [[nodiscard]] AttributeHandle register_attribute(
52  const std::string& name,
53  int64_t dimension,
54  bool replace = false,
55  T default_value = T(0));
56 
57  int64_t reserved_size() const;
58  void reserve(const int64_t size);
59 
60  // adds size more simplices to teh existing reservation
61  void reserve_more(int64_t size);
62  // makes sure we have at least size simplices reserved
63  void guarantee_at_least(int64_t size);
64 
71  void remove_attributes(
72  const std::vector<AttributeHandle>& attributes,
73  bool invalidate_handles = true);
80 
81 
82  bool operator==(const MeshAttributes<T>& other) const;
83  void push_scope();
84  void pop_scope(bool apply_updates = true);
86 
87  void change_to_parent_scope() const;
88  void change_to_child_scope() const;
89 
90 
91  int64_t dimension(const AttributeHandle& handle) const;
92  const T& default_value(const AttributeHandle& handle) const;
93  std::string get_name(const AttributeHandle& handle) const;
94 
95  void set_name(const AttributeHandle& handle, const std::string& name);
96 
97  bool has_attribute(const std::string& name) const;
98 
99  // the number of active attributes held in this object
100  // Note that the set of active attribute indices is not defined by the integers between 0,
101  // attribute_count. To get a list of valid handles use active_attributes This function is not
102  // that fast
103  size_t attribute_count() const;
104 
105  // Returns a vector of handles to the set of active attributes
106  std::vector<AttributeHandle> active_attributes() const;
107  void assert_capacity_valid(int64_t cap) const;
108 
109  Attribute<T>& attribute(const AttributeHandle& handle);
110  const Attribute<T>& attribute(const AttributeHandle& handle) const;
111 
112  AttributeHandle attribute_handle(const std::string& name) const;
113 
114  bool is_active(const AttributeHandle& handle) const;
115 
116  // pass by value due to
117  //https://clang.llvm.org/extra/clang-tidy/checks/modernize/pass-by-value.html
118  void set(const AttributeHandle& handle, std::vector<T> val);
119 
120 protected:
122  void clear_dead_attributes();
123 
124 
125  size_t attribute_size(const AttributeHandle& handle) const;
126 
127 private:
128  std::map<std::string, AttributeHandle> m_handles;
129 
130  // The vector held in each Attribute in m_attributes has this size
131  int64_t m_reserved_size = -1;
132 
133  std::vector<std::unique_ptr<Attribute<T>>> m_attributes;
134 };
135 template <typename T>
137 {
138  Attribute<T>& attr = *m_attributes.at(handle.index);
139  return attr;
140 }
141 template <typename T>
143 {
144  return *m_attributes.at(handle.index);
145 }
146 
147 template <typename T>
148 inline int64_t MeshAttributes<T>::dimension(const AttributeHandle& handle) const
149 {
150  return attribute(handle).dimension();
151 }
152 template <typename T>
153 inline const T& MeshAttributes<T>::default_value(const AttributeHandle& handle) const
154 {
155  return attribute(handle).default_value();
156 }
157 } // namespace attribute
158 } // namespace wmtk
Internal handle representation used by MeshAttributes.
This class stores data of type T in a vector.
Definition: Attribute.hpp:32
Contains all attributes of type T for a single mesh.
std::map< std::string, std::size_t > child_hashes() const override
size_t attribute_size(const AttributeHandle &handle) const
void clear_dead_attributes()
Clears and compactifies the attribute list. This invalidates all existing handles.
Eigen::Map< const Eigen::Matrix< T, Eigen::Dynamic, 1 > > ConstMapResult
std::vector< std::unique_ptr< Attribute< T > > > m_attributes
bool is_active(const AttributeHandle &handle) const
MeshAttributes(const MeshAttributes &o)=delete
void guarantee_at_least(int64_t size)
MeshAttributes & operator=(const MeshAttributes &o)=delete
std::map< std::string, AttributeHandle > m_handles
std::map< std::string, const wmtk::utils::Hashable * > child_hashables() const override
int64_t dimension(const AttributeHandle &handle) const
void remove_attributes(const std::vector< AttributeHandle > &attributes, bool invalidate_handles=true)
Remove all passed in attributes.
MeshAttributes(MeshAttributes &&o)=default
bool operator==(const MeshAttributes< T > &other) const
Eigen::Map< Eigen::Matrix< T, Eigen::Dynamic, 1 > > MapResult
MeshAttributes & operator=(MeshAttributes &&o)=default
AttributeHandle attribute_handle(const std::string &name) const
std::vector< AttributeHandle > active_attributes() const
void remove_attribute(const AttributeHandle &attribute)
Remove a single attribute.
AttributeHandle register_attribute(const std::string &name, int64_t dimension, bool replace=false, T default_value=T(0))
void assert_capacity_valid(int64_t cap) const
void set(const AttributeHandle &handle, std::vector< T > val)
void reserve(const int64_t size)
void pop_scope(bool apply_updates=true)
Attribute< T > & attribute(const AttributeHandle &handle)
bool has_attribute(const std::string &name) const
void set_name(const AttributeHandle &handle, const std::string &name)
std::string get_name(const AttributeHandle &handle) const
void serialize(const int dim, MeshWriter &writer) const
const T & default_value(const AttributeHandle &handle) const
Definition: Accessor.hpp:6