Wildmeshing Toolkit
Loading...
Searching...
No Matches
SimplexCollection.hpp
1#pragma once
2
3#include <cassert>
4#include "Simplex.hpp"
5
6namespace wmtk::simplex {
7
9{
10public:
11 SimplexCollection() = default;
12
13 // SimplexCollection(std::vector<Simplex>&& simplices = {})
14 // : m_simplices(std::move(simplices))
15 //{}
16
20 // const std::vector<Simplex>& simplex_vector() const { return m_simplices; }
21
25 const std::vector<Vertex>& vertices() const;
26 const std::vector<Edge>& edges() const;
27 const std::vector<Face>& faces() const;
28 const std::vector<Tet>& tets() const;
29
30 void reserve_vertices(size_t new_capacity) { m_v.reserve(new_capacity); }
31 void reserve_edges(size_t new_capacity) { m_e.reserve(new_capacity); }
32 void reserve_faces(size_t new_capacity) { m_f.reserve(new_capacity); }
33 void reserve_tets(size_t new_capacity) { m_t.reserve(new_capacity); }
34
40 void add(const Vertex& s) { m_v.emplace_back(s); }
41 void add(const Edge& s) { m_e.emplace_back(s); }
42 void add(const Face& s) { m_f.emplace_back(s); }
43 void add(const Tet& s) { m_t.emplace_back(s); }
49 // The faces are appended straight into this collection. Going through
50 // faces_from_simplex() would build a whole temporary SimplexCollection -- three heap
51 // allocations for a Tet -- copy it in, and destroy it; TetMeshSubstructure's link
52 // computation calls this ten times per edge.
53 void add_with_faces(const Edge& s)
54 {
55 add(s);
56 const auto& v = s.vertices();
57 m_v.emplace_back(Vertex(v[0]));
58 m_v.emplace_back(Vertex(v[1]));
59 }
60 void add_with_faces(const Face& s)
61 {
62 add(s);
63 const auto& v = s.vertices();
64 m_v.emplace_back(Vertex(v[0]));
65 m_v.emplace_back(Vertex(v[1]));
66 m_v.emplace_back(Vertex(v[2]));
67 m_e.emplace_back(Edge(v[0], v[1]));
68 m_e.emplace_back(Edge(v[0], v[2]));
69 m_e.emplace_back(Edge(v[1], v[2]));
70 }
71 void add_with_faces(const Tet& s)
72 {
73 add(s);
74 const auto& v = s.vertices();
75 m_v.emplace_back(Vertex(v[0]));
76 m_v.emplace_back(Vertex(v[1]));
77 m_v.emplace_back(Vertex(v[2]));
78 m_v.emplace_back(Vertex(v[3]));
79 m_e.emplace_back(Edge(v[0], v[1]));
80 m_e.emplace_back(Edge(v[0], v[2]));
81 m_e.emplace_back(Edge(v[0], v[3]));
82 m_e.emplace_back(Edge(v[1], v[2]));
83 m_e.emplace_back(Edge(v[1], v[3]));
84 m_e.emplace_back(Edge(v[2], v[3]));
85 m_f.emplace_back(Face(v[0], v[1], v[2]));
86 m_f.emplace_back(Face(v[0], v[1], v[3]));
87 m_f.emplace_back(Face(v[0], v[2], v[3]));
88 m_f.emplace_back(Face(v[1], v[2], v[3]));
89 }
90
91 void add(const SimplexCollection& simplex_collection);
92
96 void sort_and_clean();
97
103 bool contains(const Vertex& simplex) const
104 {
105 assert(std::is_sorted(m_v.begin(), m_v.end()));
106 return std::binary_search(m_v.begin(), m_v.end(), simplex);
107 }
108 bool contains(const Edge& simplex) const
109 {
110 assert(std::is_sorted(m_e.begin(), m_e.end()));
111 return std::binary_search(m_e.begin(), m_e.end(), simplex);
112 }
113 bool contains(const Face& simplex) const
114 {
115 assert(std::is_sorted(m_f.begin(), m_f.end()));
116 return std::binary_search(m_f.begin(), m_f.end(), simplex);
117 }
118 bool contains(const Tet& simplex) const
119 {
120 assert(std::is_sorted(m_t.begin(), m_t.end()));
121 return std::binary_search(m_t.begin(), m_t.end(), simplex);
122 }
123
129 static SimplexCollection get_union(
130 const SimplexCollection& collection_a,
131 const SimplexCollection& collection_b);
132
138 static SimplexCollection get_intersection(
139 const SimplexCollection& collection_a,
140 const SimplexCollection& collection_b);
141
148 const SimplexCollection& collection_a,
149 const SimplexCollection& collection_b);
150
154 template <int N>
155 static SimplexCollection faces_from_simplex(const Simplex<N>& simplex);
156
157 std::vector<Face> faces_with_edge(const Edge& e) const;
158
159 size_t size() const;
160 bool empty() const { return size() == 0; }
161
162 bool operator==(const SimplexCollection& that) const;
163 bool operator!=(const SimplexCollection& that) const;
164
165private:
166 std::vector<Vertex> m_v;
167 std::vector<Edge> m_e;
168 std::vector<Face> m_f;
169 std::vector<Tet> m_t;
170};
171
172template <int N>
174{
175 const auto& v = simplex.vertices();
176
178
179 if constexpr (N == 1) {
180 // do nothing
181 } else if constexpr (N == 2) {
182 sc.m_v.emplace_back(Vertex(v[0]));
183 sc.m_v.emplace_back(Vertex(v[1]));
184 } else if constexpr (N == 3) {
185 sc.m_v.emplace_back(Vertex(v[0]));
186 sc.m_v.emplace_back(Vertex(v[1]));
187 sc.m_v.emplace_back(Vertex(v[2]));
188 sc.m_e.emplace_back(Edge(v[0], v[1]));
189 sc.m_e.emplace_back(Edge(v[0], v[2]));
190 sc.m_e.emplace_back(Edge(v[1], v[2]));
191 } else {
192 static_assert(N == 4);
193
194 sc.m_v.emplace_back(Vertex(v[0]));
195 sc.m_v.emplace_back(Vertex(v[1]));
196 sc.m_v.emplace_back(Vertex(v[2]));
197 sc.m_v.emplace_back(Vertex(v[3]));
198 sc.m_e.emplace_back(Edge(v[0], v[1]));
199 sc.m_e.emplace_back(Edge(v[0], v[2]));
200 sc.m_e.emplace_back(Edge(v[0], v[3]));
201 sc.m_e.emplace_back(Edge(v[1], v[2]));
202 sc.m_e.emplace_back(Edge(v[1], v[3]));
203 sc.m_e.emplace_back(Edge(v[2], v[3]));
204 sc.m_f.emplace_back(Face(v[0], v[1], v[2]));
205 sc.m_f.emplace_back(Face(v[0], v[1], v[3]));
206 sc.m_f.emplace_back(Face(v[0], v[2], v[3]));
207 sc.m_f.emplace_back(Face(v[1], v[2], v[3]));
208 }
209
210 return sc;
211}
212
213} // namespace wmtk::simplex
Definition Simplex.hpp:46
Definition Simplex.hpp:57
Definition SimplexCollection.hpp:9
void add_with_faces(const Edge &s)
Add the simplex and its faces to the collection.
Definition SimplexCollection.hpp:53
static bool are_simplex_collections_equal(const SimplexCollection &collection_a, const SimplexCollection &collection_b)
Check if the two simplex collections are equal.
Definition SimplexCollection.cpp:92
static SimplexCollection get_union(const SimplexCollection &collection_a, const SimplexCollection &collection_b)
Get union of two simplex collections.
Definition SimplexCollection.cpp:48
void add(const Vertex &s)
Add simplex to the collection.
Definition SimplexCollection.hpp:40
static SimplexCollection faces_from_simplex(const Simplex< N > &simplex)
Get all faces of the simplex.
Definition SimplexCollection.hpp:173
static SimplexCollection get_intersection(const SimplexCollection &collection_a, const SimplexCollection &collection_b)
Get intersection of two simplex collections.
Definition SimplexCollection.cpp:70
const std::vector< Vertex > & vertices() const
Return const reference to the Simplex vector.
Definition SimplexCollection.cpp:7
void sort_and_clean()
Sort simplex vector and remove duplicates.
Definition SimplexCollection.cpp:35
bool contains(const Vertex &simplex) const
Check if simplex is contained in collection.
Definition SimplexCollection.hpp:103
Definition Simplex.hpp:17
Definition Simplex.hpp:37