Wildmeshing Toolkit
RawSimplex.cpp
Go to the documentation of this file.
1 #include "RawSimplex.hpp"
2 
3 #include <algorithm>
4 
6 #include "Simplex.hpp"
8 
9 namespace wmtk::simplex {
10 
11 RawSimplex::RawSimplex(const Mesh& mesh, const std::vector<Tuple>& vertices)
12 {
13  m_vertices.reserve(vertices.size());
14 
15 
16  for (size_t i = 0; i < vertices.size(); ++i) {
17  m_vertices.emplace_back(
18  mesh.is_valid(vertices[i]) ? mesh.id(vertices[i], PrimitiveType::Vertex) : -1);
19  }
20 
21  std::sort(m_vertices.begin(), m_vertices.end());
22 }
23 
24 RawSimplex::RawSimplex(std::vector<int64_t>&& vertices)
25  : m_vertices{std::move(vertices)}
26 {
27  std::sort(m_vertices.begin(), m_vertices.end());
28 }
29 
30 RawSimplex::RawSimplex(const Mesh& mesh, const Simplex& simplex)
31  : RawSimplex(
32  mesh,
33  simplex.primitive_type() == PrimitiveType::Vertex
34  ? std::vector<Tuple>{simplex.tuple()}
36 {}
37 
38 int64_t RawSimplex::dimension() const
39 {
40  return m_vertices.size() - 1;
41 }
42 
43 bool RawSimplex::operator==(const RawSimplex& o) const
44 {
45  return std::equal(
46  m_vertices.begin(),
47  m_vertices.end(),
48  o.m_vertices.begin(),
49  o.m_vertices.end());
50 }
51 
52 bool RawSimplex::operator<(const RawSimplex& o) const
53 {
54  if (dimension() != o.dimension()) {
55  return dimension() < o.dimension();
56  }
57 
58  return std::lexicographical_compare(
59  m_vertices.begin(),
60  m_vertices.end(),
61  o.m_vertices.begin(),
62  o.m_vertices.end());
63 }
64 
65 RawSimplex RawSimplex::opposite_face(const int64_t excluded_id)
66 {
67  std::vector<int64_t> face_ids;
68  face_ids.reserve(m_vertices.size() - 1);
69 
70  for (const int64_t& v : m_vertices) {
71  if (v != excluded_id) {
72  face_ids.emplace_back(v);
73  }
74  }
75 
76  RawSimplex face(std::move(face_ids));
77  assert(face.dimension() == dimension() - 1);
78 
79  return face;
80 }
81 
82 RawSimplex RawSimplex::opposite_face(const Mesh& mesh, const Tuple& vertex)
83 {
84  int64_t excluded_id = mesh.is_valid(vertex) ? mesh.id(vertex, PrimitiveType::Vertex) : -1;
85 
86  return opposite_face(excluded_id);
87 }
88 
90 {
91  const auto& s_v = m_vertices;
92  const auto& f_v = face.m_vertices;
93 
94  assert(f_v.size() <= s_v.size());
95 
96  std::vector<int64_t> o_v;
97  o_v.reserve(s_v.size() - f_v.size());
98 
99  std::set_difference(
100  s_v.begin(),
101  s_v.end(),
102  f_v.begin(),
103  f_v.end(),
104  std::inserter(o_v, o_v.begin()));
105 
106  assert(o_v.size() == s_v.size() - f_v.size());
107 
108  return RawSimplex(std::move(o_v));
109 }
110 
112 {
113  const auto& v = m_vertices;
114 
115  std::vector<RawSimplex> faces;
116 
117  switch (dimension()) {
118  case 0: { // simplex is a vertex
119  break;
120  }
121  case 1: { // simplex is an edge
122  faces.reserve(2);
123  faces.emplace_back(RawSimplex({v[0]}));
124  faces.emplace_back(RawSimplex({v[1]}));
125  break;
126  }
127  case 2: { // simplex is a triangle
128  faces.reserve(6);
129  faces.emplace_back(RawSimplex({v[0]}));
130  faces.emplace_back(RawSimplex({v[1]}));
131  faces.emplace_back(RawSimplex({v[2]}));
132  faces.emplace_back(RawSimplex({v[0], v[1]}));
133  faces.emplace_back(RawSimplex({v[0], v[2]}));
134  faces.emplace_back(RawSimplex({v[1], v[2]}));
135  break;
136  }
137  case 3: { // simplex is a tetrahedron
138  faces.reserve(14);
139  faces.emplace_back(RawSimplex({v[0]}));
140  faces.emplace_back(RawSimplex({v[1]}));
141  faces.emplace_back(RawSimplex({v[2]}));
142  faces.emplace_back(RawSimplex({v[3]}));
143  faces.emplace_back(RawSimplex({v[0], v[1]}));
144  faces.emplace_back(RawSimplex({v[0], v[2]}));
145  faces.emplace_back(RawSimplex({v[0], v[3]}));
146  faces.emplace_back(RawSimplex({v[1], v[2]}));
147  faces.emplace_back(RawSimplex({v[1], v[3]}));
148  faces.emplace_back(RawSimplex({v[2], v[3]}));
149  faces.emplace_back(RawSimplex({v[0], v[1], v[2]}));
150  faces.emplace_back(RawSimplex({v[0], v[1], v[3]}));
151  faces.emplace_back(RawSimplex({v[0], v[2], v[3]}));
152  faces.emplace_back(RawSimplex({v[1], v[2], v[3]}));
153  break;
154  }
155  default: assert(false); // "Unexpected dimension in RawSimplex."
156  }
157 
158  return RawSimplexCollection(std::move(faces));
159 }
160 
161 } // namespace wmtk::simplex
int64_t id(const Tuple &tuple, PrimitiveType type) const
return the global id of the Tuple of the given dimension
Definition: Mesh.hpp:1021
virtual bool is_valid(const Tuple &tuple) const
check validity of tuple including its hash
Definition: Mesh.cpp:113
A meshless implementation of the simplex that just stores an array of ids.
Definition: RawSimplex.hpp:20
std::vector< int64_t > m_vertices
Definition: RawSimplex.hpp:73
bool operator==(const RawSimplex &o) const
Definition: RawSimplex.cpp:43
RawSimplex opposite_face(const int64_t excluded_id)
Get the face opposite to the given vertex.
Definition: RawSimplex.cpp:65
RawSimplexCollection faces()
Get all faces of the simplex.
Definition: RawSimplex.cpp:111
int64_t dimension() const
Definition: RawSimplex.cpp:38
bool operator<(const RawSimplex &o) const
Definition: RawSimplex.cpp:52
const Tuple & tuple() const
Definition: Simplex.hpp:53
Definition: autodiff.h:995
std::vector< Tuple > vertices(const Mesh &m, const Simplex &simplex)
std::vector< Tuple > faces_single_dimension_tuples(const Mesh &mesh, const Simplex &simplex, const PrimitiveType face_type)