Wildmeshing Toolkit
cofaces_single_dimension.cpp
Go to the documentation of this file.
2 #include <queue>
3 #include <set>
4 #include <wmtk/Mesh.hpp>
5 #include <wmtk/TetMesh.hpp>
6 #include <wmtk/TriMesh.hpp>
9 #include <wmtk/utils/Logger.hpp>
13 #include "link.hpp"
15 namespace wmtk::simplex {
16 
17 
18 namespace {
19 
20 // wrapper for calling the internal function boundary_with_preserved_face_tuples
21 std::vector<Tuple> boundary_with_preserved_face_tuples(
22  const Mesh& mesh,
23  const std::vector<Tuple>& tuples,
24  const PrimitiveType pt,
25  const PrimitiveType coface_pt)
26 {
27  std::vector<Tuple> r;
28  for (const Tuple& t : tuples) {
29  const auto tups =
31  r.insert(r.end(), tups.begin(), tups.end());
32  }
33 
35  std::sort(r.begin(), r.end(), [&](const Tuple& a, const Tuple& b) -> bool {
36  return utils::SimplexComparisons::less(mesh, boundary_pt, a, b);
37  });
38  auto last = std::unique(r.begin(), r.end(), [&](const Tuple& a, const Tuple& b) -> bool {
39  return utils::SimplexComparisons::equal(mesh, boundary_pt, a, b);
40  });
41  r.erase(last, r.end());
42 
43  return r;
44 }
45 
46 
47 std::vector<Tuple> cofaces_single_dimension_vertex(
48  const TriMesh& mesh,
49  const Simplex& my_simplex,
50  PrimitiveType cofaces_type)
51 {
52  if (cofaces_type == PrimitiveType::Vertex) {
53  return {my_simplex.tuple()};
54  } else if (cofaces_type == PrimitiveType::Triangle) {
55  return top_dimension_cofaces_tuples(mesh, my_simplex);
56  }
57 
58  assert(cofaces_type == PrimitiveType::Edge);
59 
63 
64  std::vector<Tuple> tuples;
65  tuples.reserve(6);
66 
67  assert(mesh.is_valid(my_simplex.tuple()));
68  const Tuple t_in = my_simplex.tuple();
69  Tuple t = t_in;
70 
71  do {
72  tuples.emplace_back(t);
73 
74  if (mesh.is_boundary_edge(t)) {
75  break;
76  }
77  t = mesh.switch_tuples(t, {PF, PE});
78  } while (t != t_in);
79 
80 
81  if (t == t_in && !mesh.is_boundary_edge(t)) {
82  return tuples;
83  }
84 
85  t = mesh.switch_edge(t_in);
86 
87  tuples.emplace_back(t);
88  if (mesh.is_boundary_edge(t)) {
89  return tuples;
90  }
91  t = mesh.switch_tuples(t, {PF, PE});
92 
93  do {
94  tuples.emplace_back(t);
95 
96  if (mesh.is_boundary_edge(t)) {
97  break;
98  }
99  t = mesh.switch_tuples(t, {PF, PE});
100  } while (true);
101 
102  return tuples;
103 }
104 
105 std::vector<Tuple> cofaces_single_dimension_edge(
106  const TriMesh& mesh,
107  const Simplex& my_simplex,
108  PrimitiveType cofaces_type)
109 {
110  if (cofaces_type == PrimitiveType::Edge) {
111  return {my_simplex.tuple()};
112  }
113 
114  assert(cofaces_type == PrimitiveType::Triangle);
115  return top_dimension_cofaces_tuples(mesh, my_simplex);
116 }
117 
118 std::vector<Tuple> cofaces_single_dimension_face(
119  const TriMesh& mesh,
120  const Simplex& my_simplex,
121  PrimitiveType cofaces_type)
122 {
123  assert(cofaces_type == PrimitiveType::Triangle);
124  return {my_simplex.tuple()};
125 }
126 
127 } // namespace
128 
129 
131  const Mesh& mesh,
132  const Simplex& my_simplex,
133  PrimitiveType cofaces_type)
134 {
135  std::vector<Tuple> tuples;
136  if (my_simplex.primitive_type() == cofaces_type) {
137  tuples = {my_simplex.tuple()};
138  return tuples;
139  }
140 
141  tuples = top_dimension_cofaces_tuples(mesh, my_simplex);
142 
143 
144  assert(my_simplex.primitive_type() < cofaces_type);
145  auto range = wmtk::utils::primitive_range(mesh.top_simplex_type(), cofaces_type);
146  range.pop_back();
147  for (const auto& pt : range) {
148  tuples = boundary_with_preserved_face_tuples(mesh, tuples, pt, my_simplex.primitive_type());
149  }
150  return tuples;
151 }
152 
153 
155  const Mesh& mesh,
156  const Simplex& simplex,
157  PrimitiveType cofaces_type)
158 {
160  mesh,
161  cofaces_single_dimension_tuples(mesh, simplex, cofaces_type),
162  cofaces_type);
163 }
164 
166  const Mesh& mesh,
167  const Simplex& my_simplex,
168  PrimitiveType cofaces_type,
169  bool sort_and_clean)
170 {
171  SimplexCollection collection(
172  mesh,
173  cofaces_single_dimension_simplices(mesh, my_simplex, cofaces_type));
174  if (sort_and_clean) {
175  collection.sort_and_clean();
176  }
177 
178  return collection;
179 }
180 
182  const TriMesh& mesh,
183  const Simplex& my_simplex,
184  PrimitiveType cofaces_type,
185  bool sort_and_clean)
186 {
187  SimplexCollection collection(
188  mesh,
189  cofaces_single_dimension_simplices(mesh, my_simplex, cofaces_type));
190  if (sort_and_clean) {
191  collection.sort();
192  }
193 
194  return collection;
195 }
196 
198  const TriMesh& mesh,
199  const Simplex& my_simplex,
200  PrimitiveType cofaces_type)
201 {
202  switch (my_simplex.primitive_type()) {
204  return cofaces_single_dimension_vertex(mesh, my_simplex, cofaces_type);
205  case PrimitiveType::Edge: return cofaces_single_dimension_edge(mesh, my_simplex, cofaces_type);
207  return cofaces_single_dimension_face(mesh, my_simplex, cofaces_type);
209  default:
210  log_and_throw_error("Unknown primitive type in cofaces_single_dimension_tuples");
211  break;
212  }
213 }
214 
216  const TriMesh& mesh,
217  const Simplex& simplex,
218  PrimitiveType cofaces_type)
219 {
221  mesh,
222  cofaces_single_dimension_tuples(mesh, simplex, cofaces_type),
223  cofaces_type);
224 }
225 
226 } // namespace wmtk::simplex
PrimitiveType top_simplex_type() const
Definition: Mesh.hpp:996
void sort_and_clean()
Sort simplex vector and remove duplicates.
const Tuple & tuple() const
Definition: Simplex.hpp:53
PrimitiveType primitive_type() const
Definition: Simplex.hpp:51
constexpr wmtk::PrimitiveType PF
std::vector< Tuple > boundary_with_preserved_face_tuples(const Mesh &mesh, const Tuple &t, PrimitiveType pt, PrimitiveType face_pt)
Returns the faces (single-dimension) of a simplex that are also cofaces of a given face.
std::vector< Simplex > tuple_vector_to_homogeneous_simplex_vector(const Mesh &m, const std::vector< Tuple > &tups, PrimitiveType primitive)
void top_dimension_cofaces_tuples(const PointMesh &mesh, const Simplex &simplex, SimplexCollection &collection)
SimplexCollection cofaces_single_dimension(const Mesh &mesh, const Simplex &my_simplex, PrimitiveType cofaces_type, bool sort_and_clean)
Returns all cofaces of a simplex that are of the provided primitive type.
std::vector< Simplex > cofaces_single_dimension_simplices(const Mesh &mesh, const Simplex &simplex, PrimitiveType cofaces_type)
std::vector< Tuple > cofaces_single_dimension_tuples(const Mesh &mesh, const Simplex &my_simplex, PrimitiveType cofaces_type)
std::vector< PrimitiveType > primitive_range(PrimitiveType pt0, PrimitiveType pt1)
constexpr PrimitiveType get_primitive_type_from_id(int8_t id)
Get the primitive type corresponding to its unique integer id.
void log_and_throw_error(const std::string &msg)
Definition: Logger.cpp:101
constexpr PrimitiveType PV
constexpr int8_t get_primitive_type_id(PrimitiveType t)
Get a unique integer id corresponding to each primitive type.
constexpr PrimitiveType PE