Wildmeshing Toolkit
MappableContainer.cpp
Go to the documentation of this file.
1 #include "MappableContainer.hpp"
2 #include <type_traits>
3 #include <wmtk/Mesh.hpp>
5 
6 namespace wmtk {
7 
8 MappableContainer::MappableContainer(const Mesh& m)
9  : Mappable(m)
10 {}
12 {
13  assert(o.mesh() == mesh());
15  return *this;
16 }
17 MappableContainer& MappableContainer::operator=(MappableContainer&& o)
18 {
19  assert(o.mesh() == mesh());
20  m_mappables = std::move(o.m_mappables);
21  return *this;
22 }
23 
24 void MappableContainer::add(std::shared_ptr<Mappable> invariant)
25 {
26  m_mappables.emplace_back(std::move(invariant));
27 }
28 bool MappableContainer::before(const simplex::Simplex& t) const
29 {
30  for (const auto& invariant : m_mappables) {
31  if (&mesh() != &invariant->mesh()) {
32  for (const Tuple& ct : mesh().map_tuples(invariant->mesh(), t)) {
33  if (!invariant->before(t)) {
34  return false;
35  }
36  }
37  } else {
38  if (!invariant->before(t)) {
39  return false;
40  }
41  }
42  }
43  return true;
44 }
45 bool MappableContainer::after(
46  const std::vector<Tuple>& top_dimension_tuples_before,
47  const std::vector<Tuple>& top_dimension_tuples_after) const
48 {
49  for (const auto& invariant : m_mappables) {
50  if (&mesh() != &invariant->mesh()) {
51  auto mapped_tuples_after = mesh().map_tuples(
52  invariant->mesh(),
53  mesh().top_simplex_type(),
54  top_dimension_tuples_after);
55  auto mapped_tuples_before = mesh().parent_scope([&]() {
56  return mesh().map_tuples(
57  invariant->mesh(),
58  mesh().top_simplex_type(),
59  top_dimension_tuples_before);
60  });
61  if (!invariant->after(mapped_tuples_before, mapped_tuples_after)) {
62  return false;
63  }
64  } else {
65  if (!invariant->after(top_dimension_tuples_before, top_dimension_tuples_after)) {
66  return false;
67  }
68  }
69  assert(&mesh() != &invariant->mesh());
70  // if (!invariant->after(type, tuples)) {
71  // return false;
72  // }
73  }
74  return true;
75 }
76 
77 bool MappableContainer::directly_modified_after(
78  const std::vector<simplex::Simplex>& simplices_before,
79  const std::vector<simplex::Simplex>& simplices_after) const
80 {
81  for (const auto& invariant : m_mappables) {
82  if (&mesh() != &invariant->mesh()) {
83  auto mapped_simplices_after = mesh().map(invariant->mesh(), simplices_after);
84  auto mapped_simplices_before = mesh().parent_scope(
85  [&]() { return mesh().map(invariant->mesh(), simplices_before); });
86  if (!invariant->directly_modified_after(
87  mapped_simplices_before,
88  mapped_simplices_after)) {
89  return false;
90  }
91  } else {
92  if (!invariant->directly_modified_after(simplices_before, simplices_after)) {
93  return false;
94  }
95  }
96  }
97  return true;
98 }
99 
100 const std::shared_ptr<Mappable>& MappableContainer::get(int64_t index) const
101 {
102  return m_mappables.at(index);
103 }
104 int64_t MappableContainer::size() const
105 {
106  return m_mappables.size();
107 }
109 {
110  return m_mappables.empty();
111 }
112 const std::vector<std::shared_ptr<Mappable>>& MappableContainer::invariants() const
113 {
114  return m_mappables;
115 }
116 
117 std::map<Mesh const*, std::vector<std::shared_ptr<Mappable>>>
119 {
120  decltype(get_map_mesh_to_invariants()) mesh_invariants_map;
121 
122  throw std::runtime_error("Untested code. Potentially wrong.");
123 
124  for (std::shared_ptr<Mappable> inv : m_mappables) {
125  // TODO check if that if statement is correct
126  if (std::is_base_of<MappableContainer, decltype(inv)::element_type>()) {
127  // go through invariant collections
128  MappableContainer& sub_ic = static_cast<MappableContainer&>(*inv);
129  decltype(mesh_invariants_map) sub_map = sub_ic.get_map_mesh_to_invariants();
130 
131  for (const auto& [mptr, i] : sub_map) {
132  auto& vec = mesh_invariants_map[mptr];
133  vec.insert(vec.end(), i.begin(), i.end());
134  }
135  } else {
136  mesh_invariants_map[&(inv->mesh())].push_back(inv);
137  }
138  }
139  //
140 }
141 
142 } // namespace wmtk
std::vector< Tuple > map_tuples(const Mesh &other_mesh, const simplex::Simplex &my_simplex) const
maps a simplex from this mesh to any other mesh
std::vector< simplex::Simplex > map(const Mesh &other_mesh, const simplex::Simplex &my_simplex) const
maps a simplex from this mesh to any other mesh
decltype(auto) parent_scope(Functor &&f, Args &&... args) const
Evaluate the passed in function inside the parent scope.
std::map< Mesh const *, std::vector< std::shared_ptr< Mappable > > > get_map_mesh_to_invariants()
void add(std::shared_ptr< Mappable > mappable)
const std::shared_ptr< Mappable > & get(int64_t index) const
MappableContainer & operator=(const MappableContainer &)
std::vector< std::shared_ptr< Mappable > > m_mappables
virtual const Mesh & mesh() const =0
Definition: Accessor.hpp:6