Wildmeshing Toolkit
Attribute.cpp
Go to the documentation of this file.
1 #include "Attribute.hpp"
2 #include <numeric>
4 #include <wmtk/io/MeshWriter.hpp>
7 
8 namespace wmtk::attribute {
9 
10 
11 template <typename T>
12 void Attribute<T>::serialize(const std::string& name, const int dim, MeshWriter& writer) const
13 {
14  auto& stack = get_local_scope_stack();
15  writer.write(name, dim, dimension(), m_data, m_default_value);
16 }
17 
18 
19 template <typename T>
20 Attribute<T>::Attribute(const std::string& name, int64_t dimension, T default_value, int64_t size)
21  : m_scope_stacks(PerThreadAttributeScopeStacks<T>{})
22  , m_dimension(dimension)
23  , m_default_value(default_value)
24  , m_name(name)
25 {
26  assert(m_dimension > 0);
27  if (size > 0) {
28  m_data = std::vector<T>(size * dimension, m_default_value);
29  }
30 }
31 
32 template <typename T>
33 Attribute<T>::Attribute(Attribute&& o) = default;
34 
35 template <typename T>
36 std::map<std::string, size_t> Attribute<T>::child_hashes() const
37 {
38  std::map<std::string, size_t> hashes;
39  hashes["dimension"] = m_dimension;
40  if constexpr (std::is_same_v<T, Rational>) {
41  constexpr static std::hash<std::string> h;
42  hashes["default_value"] = h(m_default_value.serialize());
43  } else {
44  hashes["default_value"] = m_default_value;
45  }
46  hashes["data"] = wmtk::utils::vector_hash(m_data);
47  return hashes;
48 }
49 
50 
51 template <typename T>
52 Attribute<T>::~Attribute() = default;
53 
54 template <typename T>
56 
57 template <typename T>
59 {
60  return m_name == o.m_name && m_dimension == o.m_dimension && m_data == o.m_data &&
61  m_default_value == o.m_default_value;
62 }
63 
64 
65 template <typename T>
66 void Attribute<T>::reserve(const int64_t size)
67 {
68  if (size > (m_data.size() / m_dimension)) {
69  m_data.resize(m_dimension * size, m_default_value);
70  }
71 }
72 template <typename T>
74 {
75  return reserved_size(m_data);
76 }
77 template <typename T>
78 int64_t Attribute<T>::reserved_size(const std::vector<T>& data) const
79 {
80  return data.size() / m_dimension;
81 }
82 
83 template <typename T>
84 void Attribute<T>::set(std::vector<T> val)
85 {
86  assert(!val.empty());
87  assert(val.size() % m_dimension == 0);
88  m_data = std::move(val);
89 }
90 template <typename T>
91 void Attribute<T>::consolidate(const std::vector<int64_t>& new2old)
92 {
93  for (int64_t i = 0; i < new2old.size(); ++i) {
94  vector_attribute(i) = vector_attribute(new2old[i]);
95  }
96 
97  m_data.resize(new2old.size() * m_dimension);
98 }
99 
100 #if defined(__GNUG__) && !defined(__clang__)
101 #pragma GCC diagnostic push
102 #pragma GCC diagnostic ignored "-Wsuggest-attribute=noreturn"
103 #endif
104 template <typename T>
105 void Attribute<T>::index_remap(const std::vector<T>& old2new)
106 {
107  std::vector<Eigen::Index> indices(dimension());
108  std::iota(indices.begin(), indices.end(), Eigen::Index(0));
109  index_remap(old2new, indices);
110 }
111 
112 template <typename T>
113 void Attribute<T>::index_remap(const std::vector<T>& old2new, const std::vector<Eigen::Index>& cols)
114 {
115  if constexpr (std::is_same_v<T, int64_t>) {
116  for (int64_t i = 0; i < reserved_size(); ++i) {
117  auto vec = vector_attribute(i);
118  for (Eigen::Index idx : cols) {
119  int64_t& v = vec(idx);
120  if (v >= 0) // Negative number are error codes, not indices
121  v = old2new[v];
122  }
123  }
124  } else {
125  throw std::runtime_error("Only int64_t attributes can be index remapped.");
126  }
127 }
128 #if defined(__GNUG__) && !defined(__clang__)
129 #pragma GCC diagnostic pop
130 #endif
131 
132 
133 template class Attribute<char>;
134 template class Attribute<int64_t>;
135 template class Attribute<double>;
136 template class Attribute<Rational>;
137 } // namespace wmtk::attribute
virtual bool write(const int dim)=0
This class stores data of type T in a vector.
Definition: Attribute.hpp:32
Attribute & operator=(Attribute &&o)
Attribute(const std::string &name, int64_t dimension, T default_value=T(0), int64_t size=0)
Initialize the attribute.
Definition: Attribute.cpp:20
void index_remap(const std::vector< T > &old2new)
Applies the scalar old2new map to the indices in the attribute This is commonly used after a consolid...
Definition: Attribute.cpp:105
bool operator==(const Attribute< T > &o) const
Definition: Attribute.cpp:58
void serialize(const std::string &name, const int dim, MeshWriter &writer) const
Definition: Attribute.cpp:12
void reserve(const int64_t size)
Definition: Attribute.cpp:66
void consolidate(const std::vector< int64_t > &new2old)
Consolidate the vector, using the new2old map m provided and resizing the vector to m....
Definition: Attribute.cpp:91
int64_t reserved_size() const
The total number of elements in a vector.
Definition: Attribute.cpp:73
std::vector< T > m_data
Definition: Attribute.hpp:181
void set(std::vector< T > val)
Replace the internal data with val.
Definition: Attribute.cpp:84
std::map< std::string, size_t > child_hashes() const override
Definition: Attribute.cpp:36
std::size_t vector_hash(const std::vector< size_t > &data)
Definition: vector_hash.cpp:28