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::FlagAccessor<> flag_accessor = get_flag_accessor(type);
60  const attribute::IndexFlagAccessor<>& 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.is_active(index))
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::FlagAccessor<> flag_accessor = get_flag_accessor(type);
81  const attribute::IndexFlagAccessor<>& 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.is_active(index)) {
85  ret.emplace_back(tuple_from_id(type, index));
86  } else if (include_deleted) {
87  ret.emplace_back();
88  }
89  }
90  return ret;
91 }
92 
93 void Mesh::serialize(MeshWriter& writer, const Mesh* local_root) const
94 {
95  if (local_root == nullptr) {
97  } else {
98  writer.write_absolute_id(m_multi_mesh_manager.relative_id(*this, *local_root));
99  }
102 
103  m_multi_mesh_manager.serialize(writer, local_root);
104 }
105 
106 
108 {
109  return is_boundary(s.primitive_type(), s.tuple());
110 }
111 
112 
113 bool Mesh::is_valid(const Tuple& tuple) const
114 {
115  return !tuple.is_null() && !is_removed(tuple);
116 }
117 
118 bool Mesh::is_removed(const Tuple& tuple) const
119 {
120  return is_removed(tuple.m_global_cid);
121 }
122 bool Mesh::is_removed(const Tuple& t, PrimitiveType pt) const
123 {
124  if (!is_removed(t)) {
125  return is_removed(id(t, pt), pt);
126  } else {
127  return false;
128  }
129 }
131 {
132  return simplex::NavigatableSimplex(pt, tuple_from_id(pt, gid), gid);
133 }
134 bool Mesh::is_removed(int64_t index) const
135 {
136  return is_removed(index, top_simplex_type());
137 }
138 bool Mesh::is_removed(int64_t index, PrimitiveType pt) const
139 {
140  const auto& flag_accessor = get_const_flag_accessor(pt);
141  return !flag_accessor.index_access().is_active(index);
142 }
143 
144 bool Mesh::is_valid(const simplex::Simplex& s) const
145 {
146 #if defined(WMTK_ENABLE_SIMPLEX_ID_CACHING)
147  if (!is_valid(s.tuple())) {
148  return false;
149  } else {
150  const int64_t id_tuple = id(s.tuple(), s.primitive_type());
151  return id_tuple == s.m_index;
152  }
153 #else
154  return is_valid(s.tuple()) && !is_removed(s.tuple(), s.primitive_type());
155 #endif
156 }
157 
158 
160 {
161  return get_const_flag_accessor(type);
162 }
164 {
167 }
169 {
172 }
173 
174 
176 {
177  for (int64_t dim = 0; dim < m_attribute_manager.m_capacities.size(); ++dim) {
178  attribute::Accessor<char> flag_accessor = create_accessor<char>(m_flag_handles[dim]);
179  m_attribute_manager.m_capacities[dim] = flag_accessor.reserved_size();
180  }
181 }
182 
183 bool Mesh::operator==(const Mesh& other) const
184 {
186 }
187 
188 
189 std::vector<std::vector<int64_t>> Mesh::simplices_to_gids(
190  const std::vector<std::vector<simplex::Simplex>>& simplices) const
191 {
192  std::vector<std::vector<int64_t>> gids;
193  gids.resize(simplices.size());
194  for (int i = 0; i < simplices.size(); ++i) {
195  auto simplices_i = simplices[i];
196  for (const auto& simplex : simplices_i) {
197  int64_t d = get_primitive_type_id(simplex.primitive_type());
198  assert(d < 3);
199  gids[d].emplace_back(id(simplex.tuple(), simplex.primitive_type()));
200  }
201  }
202  return gids;
203 }
204 
205 
207  const Tuple& tuple,
208  const std::initializer_list<PrimitiveType>& op_sequence) const
209 {
210  return switch_tuples<std::initializer_list<PrimitiveType>>(tuple, op_sequence);
211 }
213  const Tuple& tuple,
214  const std::initializer_list<PrimitiveType>& op_sequence) const
215 {
216  return switch_tuples_unsafe<std::initializer_list<PrimitiveType>>(tuple, op_sequence);
217 }
218 
219 
221 {
223 }
224 
225 } // namespace wmtk
Tuple switch_tuples_unsafe(const Tuple &tuple, const ContainerType &op_sequence) const
Definition: Mesh.hpp:1011
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:93
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:865
simplex::NavigatableSimplex simplex_from_id(const PrimitiveType type, const int64_t gid) const
Definition: Mesh.cpp:130
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:1021
bool is_boundary(const simplex::Simplex &tuple) const
check if a simplex lies on a boundary or not
Definition: Mesh.cpp:107
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:220
std::vector< Tuple > get_all(PrimitiveType type) const
Generate a vector of Tuples from global vertex/edge/triangle/tetrahedron index.
Definition: Mesh.cpp:18
multimesh::MultiMeshManager m_multi_mesh_manager
Definition: Mesh.hpp:842
std::vector< std::vector< int64_t > > simplices_to_gids(const std::vector< std::vector< simplex::Simplex >> &simplices) const
Definition: Mesh.cpp:189
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:968
virtual bool is_valid(const Tuple &tuple) const
check validity of tuple including its hash
Definition: Mesh.cpp:113
void set_capacities_from_flags()
Definition: Mesh.cpp:175
int64_t top_cell_dimension() const
Definition: Mesh.hpp:993
bool operator==(const Mesh &other) const
Definition: Mesh.cpp:183
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:997
Tuple get_tuple_from_id_simplex(const simplex::IdSimplex &s) const
Definition: Mesh.cpp:44
const attribute::FlagAccessor< Mesh > get_const_flag_accessor(PrimitiveType type) const
Definition: Mesh.cpp:163
bool is_removed(const Tuple &tuple) const
Definition: Mesh.cpp:118
const attribute::FlagAccessor< Mesh > get_flag_accessor(PrimitiveType type) const
Definition: Mesh.cpp:159
attribute::AttributeManager m_attribute_manager
Definition: Mesh.hpp:840
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
int64_t reserved_size() const
void serialize(MeshWriter &writer) const
std::vector< int64_t > m_capacities
const IndexBaseType & index_access() const
bool is_active(int64_t t) 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.