Wildmeshing Toolkit
Attribute.hpp
Go to the documentation of this file.
1 #pragma once
2 
3 #include <Eigen/Core>
4 #include <vector>
6 #include "MapTypes.hpp"
9 
10 namespace wmtk {
11 class MeshWriter;
12 
13 namespace attribute {
14 template <typename T, int Dim>
15 class AccessorBase;
16 
17 template <typename T>
18 class PerThreadAttributeScopeStacks;
19 namespace internal {
20 template <typename T>
22 } // namespace internal
23 
30 template <typename T>
32 {
33 public:
34  template <int D = Eigen::Dynamic>
36  template <int D = Eigen::Dynamic>
38 
39 
40  // attribute directly hashes its "children" components so it overrides "child_hashes"
41  std::map<std::string, size_t> child_hashes() const override;
42 
43 
44  template <typename U, int D>
45  friend class AccessorBase;
47  void serialize(const std::string& name, const int dim, MeshWriter& writer) const;
48 
58  Attribute(const std::string& name, int64_t dimension, T default_value = T(0), int64_t size = 0);
59 
63 
64  template <int D = Eigen::Dynamic>
65  ConstMapResult<D> const_vector_attribute(const int64_t index) const;
66  template <int D = Eigen::Dynamic>
67  MapResult<D> vector_attribute(const int64_t index);
68  template <int D = Eigen::Dynamic>
69  MapResult<D> vector_attribute2(const int64_t index);
70 
71  T const_scalar_attribute(const int64_t index) const;
72  T& scalar_attribute(const int64_t index);
73  T const_scalar_attribute(const int64_t index, const int8_t offset) const;
74  T& scalar_attribute(const int64_t index, const int8_t offset);
75 
79  void set(std::vector<T> val);
80 
86  int64_t reserved_size() const;
87 
91  int64_t dimension() const;
92  void reserve(const int64_t size);
93 
97  const T& default_value() const;
98 
99  bool operator==(const Attribute<T>& o) const;
100 
101  void push_scope();
102  void pop_scope(bool apply_updates);
103  void rollback_current_scope();
104 
107 
112  void consolidate(const std::vector<int64_t>& new2old);
113 
118  void index_remap(const std::vector<T>& old2new);
119  void index_remap(const std::vector<T>& old2new, const std::vector<Eigen::Index>& cols);
120 
126  template <int D = Eigen::Dynamic>
127  ConstMapResult<D> const_vector_attribute(const int64_t index, const std::vector<T>& data) const;
128 
134  template <int D = Eigen::Dynamic>
136  const int64_t index,
137  const std::vector<T>& data) const;
142  template <int D = Eigen::Dynamic>
143  MapResult<D> vector_attribute(const int64_t index, std::vector<T>& data) const;
144 
150  template <int D = Eigen::Dynamic>
151  MapResult<D> vector_attribute_from_start(const int64_t index, std::vector<T>& data) const;
157  T const_scalar_attribute(const int64_t index, const std::vector<T>& data) const;
162  T& scalar_attribute(const int64_t index, std::vector<T>& data) const;
163 
169  T const_scalar_attribute(const int64_t index, const int8_t offset, const std::vector<T>& data)
170  const;
175  T& scalar_attribute(const int64_t index, const int8_t offset, std::vector<T>& data) const;
176 
177  // computes the "reserved size" but using the passed in data
178  int64_t reserved_size(const std::vector<T>& data) const;
179 
180 private:
181  std::vector<T> m_data;
183  int64_t m_dimension = -1;
184  T m_default_value = T(0);
185 
186 public:
187  std::string m_name;
188 };
189 
190 template <typename T>
191 template <int D>
192 inline auto Attribute<T>::const_vector_attribute(const int64_t index) const -> ConstMapResult<D>
193 {
194  return const_vector_attribute<D>(index, m_data);
195 }
196 
197 template <typename T>
198 template <int D>
199 inline auto Attribute<T>::const_vector_attribute(const int64_t index, const std::vector<T>& data)
200  const -> ConstMapResult<D>
201 {
202  assert(index < reserved_size(data));
203  assert(data.size() % m_dimension == 0);
204  const int64_t start = index * m_dimension;
205  return const_vector_attribute_from_start<D>(start, data);
206 }
207 template <typename T>
208 template <int D>
210  const int64_t start,
211  const std::vector<T>& data) const -> ConstMapResult<D>
212 {
213  assert(m_dimension > 0);
214  if constexpr (D != Eigen::Dynamic) {
215  assert(D == m_dimension);
216  }
217  ConstMapResult<D> R(data.data() + start, m_dimension);
218 
219  assert(R.size() == m_dimension);
220 
221  return R;
222 }
223 
224 
225 template <typename T>
226 template <int D>
227 inline auto Attribute<T>::vector_attribute(const int64_t index) -> MapResult<D>
228 {
229  // return MapResult<D>(m_data.data(), m_dimension);
230  return vector_attribute<D>(index, m_data);
231 }
232 
233 template <typename T>
234 template <int D>
235 inline auto Attribute<T>::vector_attribute2(const int64_t index) -> MapResult<D>
236 {
237  return MapResult<D>(m_data.data(), m_dimension);
238  // return vector_attribute<D>(index, m_data);
239 }
240 
241 template <typename T>
242 template <int D>
243 inline auto Attribute<T>::vector_attribute(const int64_t index, std::vector<T>& data) const
244  -> MapResult<D>
245 {
246  assert(index < reserved_size(data));
247  assert(data.size() % m_dimension == 0);
248  const int64_t start = index * m_dimension;
249  return vector_attribute_from_start<D>(start, data);
250 }
251 
252 template <typename T>
253 template <int D>
254 inline auto Attribute<T>::vector_attribute_from_start(const int64_t start, std::vector<T>& data)
255  const -> MapResult<D>
256 {
257  assert(m_dimension > 0);
258  if constexpr (D != Eigen::Dynamic) {
259  assert(D == m_dimension);
260  }
261  // assert(start < data.size());
262  // assert(start + m_dimension < data.size());
263  MapResult<D> R(data.data() + start, m_dimension);
264  assert(R.size() == m_dimension);
265  return R;
266 }
267 
268 template <typename T>
269 inline T Attribute<T>::const_scalar_attribute(const int64_t index) const
270 {
271  return const_scalar_attribute(index, m_data);
272 }
273 template <typename T>
274 inline T Attribute<T>::const_scalar_attribute(const int64_t index, const std::vector<T>& data) const
275 {
276  assert(index < reserved_size(data));
277  assert(m_dimension == 1);
278  return data[index];
279 }
280 
281 template <typename T>
282 inline T& Attribute<T>::scalar_attribute(const int64_t index)
283 {
284  return scalar_attribute(index, m_data);
285 }
286 template <typename T>
287 inline T& Attribute<T>::scalar_attribute(const int64_t index, std::vector<T>& data) const
288 {
289  assert(index < reserved_size(data));
290  assert(m_dimension == 1);
291  return data[index];
292 }
293 
294 template <typename T>
295 inline T Attribute<T>::const_scalar_attribute(const int64_t index, const int8_t offset) const
296 {
297  return const_scalar_attribute(index, offset, m_data);
298 }
299 template <typename T>
301  const int64_t index,
302  const int8_t offset,
303  const std::vector<T>& data) const
304 {
305  const int64_t idx = index * m_dimension + offset;
306  assert(index < reserved_size(data));
307  return data[idx];
308 }
309 
310 template <typename T>
311 inline T& Attribute<T>::scalar_attribute(const int64_t index, const int8_t offset)
312 {
313  return scalar_attribute(index, offset, m_data);
314 }
315 template <typename T>
316 inline T&
317 Attribute<T>::scalar_attribute(const int64_t index, const int8_t offset, std::vector<T>& data) const
318 {
319  const int64_t idx = index * m_dimension + offset;
320  assert(index < reserved_size(data));
321  return data[idx];
322 }
323 
324 
325 template <typename T>
326 inline int64_t Attribute<T>::dimension() const
327 {
328  return m_dimension;
329 }
330 
331 template <typename T>
332 inline const T& Attribute<T>::default_value() const
333 {
334  return m_default_value;
335 }
336 
337 template <typename T>
339 {
340  return m_scope_stacks.local();
341 }
342 template <typename T>
344 {
345  return m_scope_stacks.local();
346 }
347 
348 template <typename T>
350 {
351  m_scope_stacks.local().emplace();
352 }
353 template <typename T>
354 inline void Attribute<T>::pop_scope(bool apply_updates)
355 {
356  m_scope_stacks.local().pop(*this, apply_updates);
357 }
358 
359 template <typename T>
361 {
362  m_scope_stacks.local().rollback_current_scope(*this);
363 }
364 
365 } // namespace attribute
366 } // namespace wmtk
367 #include "AccessorBase.hpp"
368 #include "AttributeCache.hpp"
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
MapResult< D > vector_attribute_from_start(const int64_t index, std::vector< T > &data) const
Accesses the attribute using the specified vector as the underlying data This is internally used by t...
internal::ConstMapResult< T, D > ConstMapResult
Definition: Attribute.hpp:37
const internal::AttributeTransactionStack< T > & get_local_scope_stack() const
Definition: Attribute.hpp:338
const T & default_value() const
returns the default value of this attribute
Definition: Attribute.hpp:332
MapResult< D > vector_attribute(const int64_t index)
MapResult< D > vector_attribute(const int64_t index, std::vector< T > &data) const
Accesses the attribute using the specified vector as the underlying data This is internally used by t...
PerThreadAttributeScopeStacks< T > m_scope_stacks
Definition: Attribute.hpp:182
int64_t dimension() const
The number of values for each index.
Definition: Attribute.hpp:326
T & scalar_attribute(const int64_t index)
Definition: Attribute.hpp:282
ConstMapResult< D > const_vector_attribute(const int64_t index, const std::vector< T > &data) const
Accesses the attribute using the specified vector as the underlying data This is internally used by t...
internal::MapResult< T, D > MapResult
Definition: Attribute.hpp:35
void pop_scope(bool apply_updates)
Definition: Attribute.hpp:354
bool operator==(const Attribute< T > &o) const
Definition: Attribute.cpp:58
ConstMapResult< D > const_vector_attribute(const int64_t index) const
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
ConstMapResult< D > const_vector_attribute_from_start(const int64_t index, const std::vector< T > &data) const
Accesses the attribute using the specified vector as the underlying data This is internally used by t...
void set(std::vector< T > val)
Replace the internal data with val.
Definition: Attribute.cpp:84
MapResult< D > vector_attribute2(const int64_t index)
std::map< std::string, size_t > child_hashes() const override
Definition: Attribute.cpp:36
T const_scalar_attribute(const int64_t index) const
Definition: Attribute.hpp:269
typename VectorResult< T, R >::MapType MapResult
the default map type used by attributes is a map of our vector type.
Definition: MapTypes.hpp:21
typename VectorResult< T, R >::ConstMapType ConstMapResult
Definition: MapTypes.hpp:23
Definition: Accessor.hpp:6