Wildmeshing Toolkit
Mesh.cpp
Go to the documentation of this file.
1 #include "Mesh.hpp"
2 #include <numeric>
3 
4 #include <wmtk/io/MeshWriter.hpp>
5 #include <wmtk/utils/Logger.hpp>
7 
8 #include "EdgeMesh.hpp"
9 #include "PointMesh.hpp"
10 #include "TetMesh.hpp"
11 #include "TriMesh.hpp"
12 
13 #include "Primitive.hpp"
14 
15 namespace wmtk {
16 
17 
18 std::vector<Tuple> Mesh::get_all(PrimitiveType type) const
19 {
20  return get_all(type, false);
21 }
22 
23 std::vector<simplex::IdSimplex> Mesh::get_all_id_simplex(PrimitiveType type) const
24 {
25  return get_all_id_simplex(type, false);
26 }
27 
29 {
30  return simplex::IdSimplex(pt, id(tuple, pt));
31 }
32 
34 {
35  return simplex::IdSimplex(s.primitive_type(), id(s.tuple(), s.primitive_type()));
36 }
37 
39 {
40  const Tuple& t = tuple_from_id(s.primitive_type(), s.index());
41  return simplex::Simplex(*this, s.primitive_type(), t);
42 }
43 
45 {
46  return tuple_from_id(s.primitive_type(), s.index());
47 }
48 
49 std::vector<simplex::IdSimplex> Mesh::get_all_id_simplex(
50  PrimitiveType type,
51  const bool include_deleted) const
52 {
53  std::vector<simplex::IdSimplex> ret;
54 
55  if (static_cast<int8_t>(type) > top_cell_dimension()) return ret;
56 
57  const int64_t cap = capacity(type);
58 
59  const attribute::Accessor<char> flag_accessor = get_flag_accessor(type);
60  const attribute::CachingAccessor<char>& flag_accessor_indices = flag_accessor.index_access();
61  ret.reserve(cap);
62  for (size_t index = 0; index < cap; ++index) {
63  if (flag_accessor_indices.const_scalar_attribute(index) & 1)
64  ret.emplace_back(simplex::IdSimplex(type, index));
65  else if (include_deleted)
66  ret.emplace_back();
67  }
68  return ret;
69 }
70 
71 
72 std::vector<Tuple> Mesh::get_all(PrimitiveType type, const bool include_deleted) const
73 {
74  std::vector<Tuple> ret;
75 
76  if (static_cast<int8_t>(type) > top_cell_dimension()) return ret;
77 
78  const int64_t cap = capacity(type);
79 
80  const attribute::Accessor<char> flag_accessor = get_flag_accessor(type);
81  const attribute::CachingAccessor<char>& flag_accessor_indices = flag_accessor.index_access();
82  ret.reserve(cap);
83  for (size_t index = 0; index < cap; ++index) {
84  if (flag_accessor_indices.const_scalar_attribute(index) & 1)
85  ret.emplace_back(tuple_from_id(type, index));
86  else if (include_deleted)
87  ret.emplace_back();
88  }
89  return ret;
90 }
91 
92 void Mesh::serialize(MeshWriter& writer, const Mesh* local_root) const
93 {
94  if (local_root == nullptr) {
96  } else {
97  writer.write_absolute_id(m_multi_mesh_manager.relative_id(*this, *local_root));
98  }
101 
102  m_multi_mesh_manager.serialize(writer, local_root);
103 }
104 
105 
107 {
108  return is_boundary(s.primitive_type(), s.tuple());
109 }
110 
111 
112 bool Mesh::is_valid(const Tuple& tuple) const
113 {
114  return !tuple.is_null() && !is_removed(tuple);
115 }
116 
117 bool Mesh::is_removed(const Tuple& tuple) const
118 {
119  return is_removed(tuple.m_global_cid);
120 }
121 bool Mesh::is_removed(const Tuple& t, PrimitiveType pt) const
122 {
123  if (!is_removed(t)) {
124  return is_removed(id(t, pt), pt);
125  } else {
126  return false;
127  }
128 }
130 {
131  return simplex::NavigatableSimplex(pt, tuple_from_id(pt, gid), gid);
132 }
133 bool Mesh::is_removed(int64_t index) const
134 {
135  return is_removed(index, top_simplex_type());
136 }
137 bool Mesh::is_removed(int64_t index, PrimitiveType pt) const
138 {
139  const auto& flag_accessor = get_const_flag_accessor(pt);
140  return !(flag_accessor.index_access().const_scalar_attribute(index) & 0x1);
141 }
142 
143 bool Mesh::is_valid(const simplex::Simplex& s) const
144 {
145 #if defined(WMTK_ENABLE_SIMPLEX_ID_CACHING)
146  if (!is_valid(s.tuple())) {
147  return false;
148  } else {
149  const int64_t id_tuple = id(s.tuple(), s.primitive_type());
150  return id_tuple == s.m_index;
151  }
152 #else
153  return is_valid(s.tuple()) && !is_removed(s.tuple(), s.primitive_type());
154 #endif
155 }
156 
157 
159 {
160  return get_const_flag_accessor(type);
161 }
163 {
165 }
167 {
169 }
170 
171 
173 {
174  for (int64_t dim = 0; dim < m_attribute_manager.m_capacities.size(); ++dim) {
175  attribute::Accessor<char> flag_accessor = create_accessor<char>(m_flag_handles[dim]);
176  m_attribute_manager.m_capacities[dim] = flag_accessor.reserved_size();
177  }
178 }
179 
180 bool Mesh::operator==(const Mesh& other) const
181 {
183 }
184 
185 
186 std::vector<std::vector<int64_t>> Mesh::simplices_to_gids(
187  const std::vector<std::vector<simplex::Simplex>>& simplices) const
188 {
189  std::vector<std::vector<int64_t>> gids;
190  gids.resize(simplices.size());
191  for (int i = 0; i < simplices.size(); ++i) {
192  auto simplices_i = simplices[i];
193  for (const auto& simplex : simplices_i) {
194  int64_t d = get_primitive_type_id(simplex.primitive_type());
195  assert(d < 3);
196  gids[d].emplace_back(id(simplex.tuple(), simplex.primitive_type()));
197  }
198  }
199  return gids;
200 }
201 
202 
204  const Tuple& tuple,
205  const std::initializer_list<PrimitiveType>& op_sequence) const
206 {
207  return switch_tuples<std::initializer_list<PrimitiveType>>(tuple, op_sequence);
208 }
210  const Tuple& tuple,
211  const std::initializer_list<PrimitiveType>& op_sequence) const
212 {
213  return switch_tuples_unsafe<std::initializer_list<PrimitiveType>>(tuple, op_sequence);
214 }
215 
216 
218 {
220 }
221 
222 } // namespace wmtk
Tuple switch_tuples_unsafe(const Tuple &tuple, const ContainerType &op_sequence) const
Definition: Mesh.hpp:1010
simplex::Simplex get_simplex(const simplex::IdSimplex &s) const
Convert an IdSimplex into a Simplex.
Definition: Mesh.cpp:38
void serialize(MeshWriter &writer, const Mesh *local_root=nullptr) const
Definition: Mesh.cpp:92
int64_t capacity(PrimitiveType type) const
read in the m_capacities return the upper bound for the number of entities of the given dimension
std::vector< TypedAttributeHandle< char > > m_flag_handles
0x1 == true = simplex is active (simplex exists) all flag default to 0
Definition: Mesh.hpp:864
simplex::NavigatableSimplex simplex_from_id(const PrimitiveType type, const int64_t gid) const
Definition: Mesh.cpp:129
const attribute::Accessor< T, Mesh, D > create_const_accessor(const attribute::MeshAttributeHandle &handle) const
int64_t id(const Tuple &tuple, PrimitiveType type) const
return the global id of the Tuple of the given dimension
Definition: Mesh.hpp:1020
bool is_boundary(const simplex::Simplex &tuple) const
check if a simplex lies on a boundary or not
Definition: Mesh.cpp:106
virtual Tuple tuple_from_id(const PrimitiveType type, const int64_t gid) const =0
internal function that returns the tuple of requested type, and has the global index cid
void assert_capacity_valid() const
Definition: Mesh.cpp:217
std::vector< Tuple > get_all(PrimitiveType type) const
Generate a vector of Tuples from global vertex/edge/triangle/tetrahedron index.
Definition: Mesh.cpp:18
const attribute::Accessor< char > get_const_flag_accessor(PrimitiveType type) const
Definition: Mesh.cpp:162
multimesh::MultiMeshManager m_multi_mesh_manager
Definition: Mesh.hpp:841
std::vector< std::vector< int64_t > > simplices_to_gids(const std::vector< std::vector< simplex::Simplex >> &simplices) const
Definition: Mesh.cpp:186
std::vector< simplex::IdSimplex > get_all_id_simplex(PrimitiveType type) const
Definition: Mesh.cpp:23
Tuple switch_tuples(const Tuple &tuple, const ContainerType &op_sequence) const
Definition: Mesh.hpp:967
virtual bool is_valid(const Tuple &tuple) const
check validity of tuple including its hash
Definition: Mesh.cpp:112
void set_capacities_from_flags()
Definition: Mesh.cpp:172
int64_t top_cell_dimension() const
Definition: Mesh.hpp:992
bool operator==(const Mesh &other) const
Definition: Mesh.cpp:180
attribute::Accessor< T, Mesh, D > create_accessor(const attribute::MeshAttributeHandle &handle)
simplex::IdSimplex get_id_simplex(const Tuple &tuple, PrimitiveType pt) const
Retrieve the IdSimplex that is represented by the tuple and primitive type.
Definition: Mesh.cpp:28
PrimitiveType top_simplex_type() const
Definition: Mesh.hpp:996
Tuple get_tuple_from_id_simplex(const simplex::IdSimplex &s) const
Definition: Mesh.cpp:44
bool is_removed(const Tuple &tuple) const
Definition: Mesh.cpp:117
const attribute::Accessor< char > get_flag_accessor(PrimitiveType type) const
Definition: Mesh.cpp:158
attribute::AttributeManager m_attribute_manager
Definition: Mesh.hpp:839
virtual void write_top_simplex_type(const PrimitiveType type)=0
virtual void write_absolute_id(const std::vector< int64_t > &id)=0
bool is_null() const
Checks if a tuple is "null". This merely implies the global index is -1.
Definition: Tuple.hxx:40
int64_t m_global_cid
Definition: Tuple.hpp:46
A CachingAccessor that uses tuples for accessing attributes instead of indices.
Definition: Accessor.hpp:25
CachingBaseType & index_access()
Definition: Accessor.hpp:95
int64_t reserved_size() const
void serialize(MeshWriter &writer) const
std::vector< int64_t > m_capacities
An accessor for cached attribute values.
T const_scalar_attribute(const int64_t index) const
std::vector< int64_t > absolute_id() const
void serialize(MeshWriter &writer, const Mesh *local_root=nullptr) const
static std::vector< int64_t > relative_id(const std::vector< int64_t > &parent, const std::vector< int64_t > &child)
PrimitiveType primitive_type() const
Definition: IdSimplex.hpp:23
int64_t index() const
Definition: IdSimplex.hpp:32
const Tuple & tuple() const
Definition: Simplex.hpp:53
PrimitiveType primitive_type() const
Definition: Simplex.hpp:51
Definition: Accessor.hpp:6
constexpr int8_t get_primitive_type_id(PrimitiveType t)
Get a unique integer id corresponding to each primitive type.