Wildmeshing Toolkit
SimplexCollection.cpp
Go to the documentation of this file.
1 #include "SimplexCollection.hpp"
2 
3 #include <algorithm>
4 
5 namespace wmtk::simplex {
6 
7 
8 std::vector<Simplex> SimplexCollection::simplex_vector(const PrimitiveType& ptype) const
9 {
10  std::vector<Simplex> simplices;
11  simplices.reserve(m_simplices.size() / 3); // giving the vector some (hopefully) resonable size
12 
13  // add simplices to the vector
14  for (const Simplex& s : m_simplices) {
15  if (s.primitive_type() == ptype) {
16  simplices.emplace_back(s);
17  }
18  }
19 
20  return simplices;
21 }
22 
24 {
25  return m_mesh;
26 }
27 
29 {
30  std::vector<Tuple> tuples;
31  tuples.reserve(m_simplices.size() / 3); // giving the vector some (hopefully) resonable size
32 
33  // add simplices to the vector
34  for (const Simplex& s : m_simplices) {
35  if (s.primitive_type() == ptype) {
36  tuples.emplace_back(s.tuple());
37  }
38  }
39 
40  return tuples;
41 }
42 
43 void SimplexCollection::add(const Simplex& simplex)
44 {
45  m_simplices.push_back(simplex);
46 }
47 
48 void SimplexCollection::add(const SimplexCollection& simplex_collection)
49 {
50  const auto& s = simplex_collection.m_simplices;
51  m_simplices.insert(m_simplices.end(), s.begin(), s.end());
52 }
53 
54 void SimplexCollection::add(const PrimitiveType ptype, const std::vector<Tuple>& tuple_vec)
55 {
56  m_simplices.reserve(m_simplices.size() + tuple_vec.size());
57 
58  for (const Tuple& t : tuple_vec) {
59  m_simplices.emplace_back(Simplex(mesh(), ptype, t));
60  }
61 }
62 
63 void SimplexCollection::add(const PrimitiveType ptype, const Tuple& tuple)
64 {
65  m_simplices.emplace_back(Simplex(mesh(), ptype, tuple));
66 }
67 
69 {
70  // using SimplexIdPair = std::pair<int64_t, Simplex>;
71  //
72  // std::vector<SimplexIdPair> tmp;
73  // tmp.reserve(m_simplices.size());
74  //
75  // std::transform(
76  // m_simplices.begin(),
77  // m_simplices.end(),
78  // std::back_inserter(tmp),
79  // [&](const Simplex& s) { return std::make_pair(m_mesh.id(s), s); });
80  //
81  // auto cmp = [](const SimplexIdPair& a, const SimplexIdPair& b) {
82  // if (a.second.primitive_type() == b.second.primitive_type()) {
83  // return a.first < b.first;
84  // } else {
85  // return a.second.primitive_type() < b.second.primitive_type();
86  // }
87  // };
88  // auto equal = [](const SimplexIdPair& a, const SimplexIdPair& b) {
89  // return a.first == b.first && a.second.primitive_type() == b.second.primitive_type();
90  // };
91  // std::sort(tmp.begin(), tmp.end(), cmp);
92  // const auto last = std::unique(tmp.begin(), tmp.end(), equal);
93  // tmp.erase(last, tmp.end());
94  //
95  // m_simplices.clear();
96  // std::transform(
97  // tmp.begin(),
98  // tmp.end(),
99  // std::back_inserter(m_simplices),
100  // [&](const SimplexIdPair& p) { return p.second; });
101 
102  std::sort(m_simplices.begin(), m_simplices.end(), m_simplex_is_less);
103  const auto last = std::unique(m_simplices.begin(), m_simplices.end(), m_simplex_is_equal);
104  m_simplices.erase(last, m_simplices.end());
105 }
106 
108 {
109  std::sort(m_simplices.begin(), m_simplices.end(), m_simplex_is_less);
110 }
111 
112 
113 bool SimplexCollection::contains(const Simplex& simplex) const
114 {
115  assert(std::is_sorted(m_simplices.begin(), m_simplices.end(), m_simplex_is_less));
116  return std::binary_search(m_simplices.begin(), m_simplices.end(), simplex, m_simplex_is_less);
117 }
118 
120  const SimplexCollection& collection_a,
121  const SimplexCollection& collection_b)
122 {
123  SimplexCollection sc(collection_a.m_mesh);
124 
125  const auto& a = collection_a.m_simplices;
126  const auto& b = collection_b.m_simplices;
127 
128  std::set_union(
129  a.cbegin(),
130  a.cend(),
131  b.cbegin(),
132  b.cend(),
133  std::back_inserter(sc.m_simplices),
134  sc.m_simplex_is_less);
135 
136  return sc;
137 }
138 
140  const SimplexCollection& collection_a,
141  const SimplexCollection& collection_b)
142 {
143  SimplexCollection sc(collection_a.m_mesh);
144 
145  const auto& a = collection_a.m_simplices;
146  const auto& b = collection_b.m_simplices;
147 
149  a.cbegin(),
150  a.cend(),
151  b.cbegin(),
152  b.cend(),
153  std::back_inserter(sc.m_simplices),
154  sc.m_simplex_is_less);
155 
156  return sc;
157 }
158 
160  const SimplexCollection& collection_a,
161  const SimplexCollection& collection_b)
162 {
163  if (collection_a.m_simplices.size() != collection_b.m_simplices.size()) {
164  return false;
165  }
166  SimplexCollection sc_union = SimplexCollection::get_union(collection_a, collection_b);
167  return sc_union.m_simplices.size() == collection_a.m_simplices.size();
168 }
169 
171 {
172  return are_simplex_collections_equal(*this, other);
173 }
174 
175 void SimplexCollection::reserve(const size_t new_cap)
176 {
177  m_simplices.reserve(new_cap);
178 }
179 
180 } // namespace wmtk::simplex
static bool are_simplex_collections_equal(const SimplexCollection &collection_a, const SimplexCollection &collection_b)
Check if the two simplex collections are equal.
void reserve(const size_t new_cap)
void add(const Simplex &simplex)
Add simplex to the collection.
static SimplexCollection get_union(const SimplexCollection &collection_a, const SimplexCollection &collection_b)
Get union of two simplex collections.
bool contains(const Simplex &simplex) const
Check if simplex is contained in collection.
const std::vector< Simplex > & simplex_vector() const
Return const reference to the simplex vector.
static SimplexCollection get_intersection(const SimplexCollection &collection_a, const SimplexCollection &collection_b)
Get intersection of two simplex collections.
bool operator==(const SimplexCollection &other) const
internal::SimplexEqualFunctor m_simplex_is_equal
internal::SimplexLessFunctor m_simplex_is_less
void sort_and_clean()
Sort simplex vector and remove duplicates.
std::vector< Tuple > simplex_vector_tuples(PrimitiveType ptype) const
Return vector of all simplices of the requested type, as tuples.
void set_intersection(const std::unordered_set< int > &s1, const std::unordered_set< int > &s2, std::vector< int > &v)