Wildmeshing Toolkit
Loading...
Searching...
No Matches
MeshAttributeHandle.hpp
Go to the documentation of this file.
1#pragma once
2//
4//
5#include "AttributeType.hpp"
8
9#include <tuple>
10#include <variant>
11
12namespace wmtk {
13class Mesh;
14} // namespace wmtk
15
16namespace wmtk::attribute {
17template <typename T, typename MeshType, typename AttributeType_, int Dim>
18class Accessor;
19
20/* @brief Handle that can construct an accessor on its own
21 * NOTE: This naming is inconsistent with the existing
22 * AttributeHandle/MeshAttributeHandle nomenclature, but in the future most
23 * applications should store MeshAttributeHandles instead of
24 * MeshAttributeHandle, and after most of those changes are made we will
25 * deprecate that name.
26 */
28{
29public:
30 // The variant type for storing all of the tuple types.
31 // each type within the Handle variant must have a ::Type value that indicates a type-based
32 // descriptor of the data held by the type
33
34 using HandleVariant = std::variant<
39
40 // Convenience class for identifying attribute types
41 using ValueVariant = std::
42 variant<char, int64_t, double, wmtk::Rational, std::tuple<char, wmtk::Rational, double>>;
43
45
46 template <HeldType Type>
47 using held_handle_type = std::variant_alternative_t<size_t(Type), HandleVariant>;
48
49 template <HeldType Type>
51
52
53 // for primitive type attributes returns the held type for primitive types
54 // takes as input basic type (char/int64/double/rational)
55 // Note this doesn't work with the hybrid type, so this only makes esnse to use in basic
56 // attribute storage
57 template <typename T>
59
60 template <typename T>
61 constexpr static HeldType held_type_from_handle();
62
63
64 template <typename T>
65 constexpr static bool handle_type_is_basic();
66
67
68 friend class wmtk::Mesh;
69 friend class wmtk::hash<MeshAttributeHandle>;
76
77 bool operator==(const MeshAttributeHandle& o) const
78 {
79 return m_handle == o.m_handle && m_mesh == o.m_mesh;
80 }
81
82 // reutrns if the target mesh is the same as the one represented in the handle
83 bool is_same_mesh(const Mesh&) const;
84
85
89 bool is_valid() const;
90
92 template <typename T>
94 // AttributeHandle base_handle() const ;
95
96
97 template <typename T>
98 auto as() const -> const held_handle_type<held_type_from_primitive<T>()>&;
99
100 template <HeldType Type>
101 auto as_from_held_type() const -> const held_handle_type<Type>&;
102 // returns if the held attribute uses the primitive T
103
104
105 // returns if the held attribute uses the primitive T
106 template <typename T>
107 bool holds() const;
108
109 // holds basic type
110 bool holds_basic_type() const;
111
112 // returns if the held attribute uses the held type primitive Type
113 template <HeldType Type>
114 bool holds_from_held_type() const;
115
116 HeldType held_type() const;
117
118 Mesh& mesh();
119 const Mesh& mesh() const;
120
122 const HandleVariant& handle() const { return m_handle; }
128 // MutableAccessor<T> create_accessor();
129
130
131 template <typename T, int Dim = Eigen::Dynamic, typename MeshType = Mesh>
133 template <typename T, int Dim = Eigen::Dynamic, typename MeshType = Mesh>
135
136 // return the dimension of the attribute (i.e the number of values stored per simplex)
137 int64_t dimension() const;
138
139 std::string name() const;
140
141
142private:
143 Mesh* m_mesh = nullptr;
145};
146
147template <typename T>
148inline auto MeshAttributeHandle::as() const
149 -> const held_handle_type<held_type_from_primitive<T>()>&
150{
151 return as_from_held_type<held_type_from_primitive<T>()>();
152}
153
154
155template <typename T>
156inline bool MeshAttributeHandle::holds() const
157{
158 return holds_from_held_type<held_type_from_primitive<T>()>();
159}
160
161namespace internal {
162template <typename T>
164{
165 constexpr static bool value = false;
166};
167template <typename T>
169{
170 constexpr static bool value = true;
171};
172} // namespace internal
173
174template <typename T>
179
181{
182 return std::visit(
183 [](const auto& h) noexcept -> bool {
184 return handle_type_is_basic<std::decay_t<decltype(h)>>();
185 },
186 m_handle);
187}
188template <MeshAttributeHandle::HeldType Type>
190{
191 return std::get<held_handle_type<Type>>(m_handle);
192}
193
194template <MeshAttributeHandle::HeldType Type>
196{
197 return std::holds_alternative<held_handle_type<Type>>(m_handle);
198}
199
200template <typename T>
202{
203 return held_type_from_handle<TypedAttributeHandle<T>>();
204}
205template <typename T>
207{
208 return attribute_type_enum_from_type<typename T::Type>();
209}
210
212{
213 return std::visit([](const auto& h) { return h.primitive_type(); }, m_handle);
214}
215template <typename T>
217{
218 return std::get<T>(m_handle).primitive_type();
219}
220
221template <typename T, int Dim, typename MeshType>
227template <typename T, int Dim, typename MeshType>
232} // namespace wmtk::attribute
An Accessor that uses tuples for accessing attributes instead of indices.
Definition Accessor.hpp:28
typename held_handle_type< Type >::Type held_primitive_type
static constexpr HeldType held_type_from_handle()
std::variant< TypedAttributeHandle< char >, TypedAttributeHandle< int64_t >, TypedAttributeHandle< double >, TypedAttributeHandle< wmtk::Rational > > HandleVariant
MeshAttributeHandle & operator=(MeshAttributeHandle &&o)=default
MeshAttributeHandle(MeshAttributeHandle &&o)=default
std::variant_alternative_t< size_t(Type), HandleVariant > held_handle_type
const Accessor< T, MeshType, CachingAttribute< T >, Dim > create_const_accessor() const
static constexpr HeldType held_type_from_primitive()
Accessor< T, MeshType, CachingAttribute< T >, Dim > create_accessor() const
auto as() const -> const held_handle_type< held_type_from_primitive< T >()> &
MeshAttributeHandle & operator=(const MeshAttributeHandle &o)=default
std::variant< char, int64_t, double, wmtk::Rational, std::tuple< char, wmtk::Rational, double > > ValueVariant
bool is_valid() const
Returns true if handle was initialized.
auto as_from_held_type() const -> const held_handle_type< Type > &
MeshAttributeHandle(const MeshAttributeHandle &o)=default
bool operator==(const MeshAttributeHandle &o) const
Handle that represents attributes for some mesh.