Wildmeshing Toolkit
EdgeCollapse.cpp
Go to the documentation of this file.
1 #include "EdgeCollapse.hpp"
2 #include <wmtk/utils/Logger.hpp>
3 
4 #include <cassert>
7 
10 
11 
12 namespace wmtk::operations {
13 
15 {
16  bool all_configured = true;
17  for (const auto& strat : m_new_attr_strategies) {
18  if (strat->invalid_state()) {
19  all_configured = false;
20  wmtk::logger().warn("Attribute new {} was not configured", strat->name());
21  }
22  }
23  return all_configured;
24 }
25 
27  : Operation(m)
28 {
29  auto collect_attrs = [&](auto&& mesh) {
30  // can have const variant values here so gotta filter htose out
31  if constexpr (!std::is_const_v<std::remove_reference_t<decltype(mesh)>>) {
32  if (mesh.is_free()) {
33  return;
34  }
35  for (const auto& attr : mesh.custom_attributes()) {
36  std::visit(
37  [&](auto&& tah) noexcept {
38  using HandleType = typename std::decay_t<decltype(tah)>;
39  if constexpr (attribute::MeshAttributeHandle::template handle_type_is_basic<
40  HandleType>()) {
41  using T = typename HandleType::Type;
42  m_new_attr_strategies.emplace_back(
45  }
46  },
47  attr);
48  }
49  }
50  };
51 
52  multimesh::MultiMeshVisitor custom_attribute_collector(collect_attrs);
53  custom_attribute_collector.execute_from_root(m);
54 }
55 
56 std::vector<simplex::Simplex> EdgeCollapse::execute(const simplex::Simplex& simplex)
57 {
59  mesh(),
60  simplex,
62 }
63 
64 std::vector<simplex::Simplex> EdgeCollapse::unmodified_primitives(
65  const simplex::Simplex& simplex) const
66 {
67  return mesh().parent_scope([&]() -> std::vector<simplex::Simplex> {
68  const simplex::Simplex v0 = simplex::Simplex::vertex(mesh(), simplex.tuple());
70  mesh(),
71  mesh().switch_tuple(simplex.tuple(), PrimitiveType::Vertex));
72  return {v0, v1};
73  });
74 }
76 
77 
78 std::shared_ptr<const operations::BaseCollapseNewAttributeStrategy>
80 {
81  for (auto& s : m_new_attr_strategies) {
82  if (s->matches_attribute(attribute)) return s;
83  }
84 
85  throw std::runtime_error("unable to find attribute");
86 }
88 {
89  m_new_attr_strategies.clear();
90 }
91 
93  const attribute::MeshAttributeHandle& attribute,
94  const std::shared_ptr<const operations::BaseCollapseNewAttributeStrategy>& other)
95 {
96  for (size_t i = 0; i < m_new_attr_strategies.size(); ++i) {
97  if (m_new_attr_strategies[i]->matches_attribute(attribute)) {
98  m_new_attr_strategies[i] = other;
99  return;
100  }
101  }
102  if (attribute.mesh().is_free()) {
103  logger().debug("Set new collapse attribute strategy on a free mesh, there are no new "
104  "attributes for free mesh collapses");
105  return;
106  }
107 
108  throw std::runtime_error("unable to find attribute");
109 }
110 
112  const attribute::MeshAttributeHandle& attribute,
114 {
115  std::visit(
116  [&](auto&& val) noexcept -> void {
117  using HandleType = typename std::decay_t<decltype(val)>;
118  if constexpr (attribute::MeshAttributeHandle::template handle_type_is_basic<
119  HandleType>()) {
120  using T = typename HandleType::Type;
122 
123  std::shared_ptr<OpType> tmp = std::make_shared<OpType>(attribute);
124  tmp->set_strategy(strategy);
125 
126  set_new_attribute_strategy(attribute, tmp);
127  }
128  },
129  attribute.handle());
130 }
131 
133  const std::vector<simplex::Simplex>& unmods,
134  const std::vector<simplex::Simplex>& mods) const
135 {
136  if (mesh().is_free()) {
137  return true;
138  } else {
139  return Operation::after(unmods, mods);
140  }
141 }
142 } // namespace wmtk::operations
std::vector< attribute::MeshAttributeHandle::HandleVariant > custom_attributes() const
bool is_free() const
Definition: Mesh.hpp:988
decltype(auto) parent_scope(Functor &&f, Args &&... args) const
Evaluate the passed in function inside the parent scope.
std::vector< simplex::Simplex > unmodified_primitives(const simplex::Simplex &simplex) const override
Returns all simplices that will be potentially affected by the operation.
std::shared_ptr< const operations::BaseCollapseNewAttributeStrategy > get_new_attribute_strategy(const attribute::MeshAttributeHandle &attribute) const
std::vector< simplex::Simplex > execute(const simplex::Simplex &simplex) override
returns an empty vector in case of failure
bool after(const std::vector< simplex::Simplex > &unmods, const std::vector< simplex::Simplex > &mods) const final override
bool attribute_new_all_configured() const
void set_new_attribute_strategy(const attribute::MeshAttributeHandle &attribute, const std::shared_ptr< const operations::BaseCollapseNewAttributeStrategy > &other)
std::vector< std::shared_ptr< const operations::BaseCollapseNewAttributeStrategy > > m_new_attr_strategies
virtual bool after(const std::vector< simplex::Simplex > &unmods, const std::vector< simplex::Simplex > &mods) const
Definition: Operation.cpp:135
const Mesh & mesh() const
Definition: Operation.hpp:45
const Tuple & tuple() const
Definition: Simplex.hpp:53
static Simplex vertex(const Mesh &m, const Tuple &t)
Definition: Simplex.hpp:56
std::vector< simplex::Simplex > multi_mesh_edge_collapse_with_modified_simplices(Mesh &mesh, const simplex::Simplex &simplex, const std::vector< std::shared_ptr< const operations::BaseCollapseNewAttributeStrategy >> &new_attr_strategies)
spdlog::logger & logger()
Retrieves the current logger.
Definition: Logger.cpp:58