Wildmeshing Toolkit
regular_space.cpp
Go to the documentation of this file.
1 #include "regular_space.hpp"
2 
3 #include <wmtk/Mesh.hpp>
5 #include <wmtk/io/Cache.hpp>
6 #include <wmtk/utils/Logger.hpp>
8 
11 
12 namespace wmtk::components {
13 
15 {
16  // collect labels that were there already before this component
17  std::vector<attribute::MeshAttributeHandle> original_attributes;
18 
19  std::vector<attribute::MeshAttributeHandle> label_attributes;
20  for (const PrimitiveType& ptype : utils::primitive_below(mesh.top_simplex_type())) {
21  std::string attr_name;
22  switch (ptype) {
23  case PrimitiveType::Vertex: {
24  attr_name = "vertex_label";
25  break;
26  }
27  case PrimitiveType::Edge: {
28  attr_name = "edge_label";
29  break;
30  }
32  attr_name = "face_label";
33  break;
34  }
36  attr_name = "tetrahedron_label";
37  break;
38  }
39  default: log_and_throw_error("Unknown primitive type: {}", ptype);
40  }
41 
42  if (options.attributes.find(attr_name) == options.attributes.end()) {
43  // no attribute was given for that primitive type
44  auto h = mesh.register_attribute<int64_t>(attr_name + "_regular_space", ptype, 1);
45  label_attributes.emplace_back(h);
46  } else {
47  auto h = mesh.get_attribute_handle<int64_t>(options.attributes.at(attr_name), ptype);
48  label_attributes.emplace_back(h);
49  original_attributes.emplace_back(h);
50  }
51  }
52 
53  auto pass_through_attributes = utils::get_attributes(cache, mesh, options.pass_through);
54 
55  return std::make_tuple(original_attributes, label_attributes, pass_through_attributes);
56 }
57 
58 void regular_space(const utils::Paths& paths, const nlohmann::json& j, io::Cache& cache)
59 {
60  using namespace internal;
61 
62  RegularSpaceOptions options = j.get<RegularSpaceOptions>();
63 
64  // input
65  std::shared_ptr<Mesh> mesh_in = cache.read_mesh(options.input);
66 
67  Mesh& mesh = static_cast<Mesh&>(*mesh_in);
68 
69  auto [original_attributes, label_attributes, pass_through_attributes] =
70  gather_attributes(cache, mesh, options);
71 
72  // clean up attributes
73  {
74  std::vector<attribute::MeshAttributeHandle> keeps = pass_through_attributes;
75  keeps.insert(keeps.end(), original_attributes.begin(), original_attributes.end());
76  mesh.clear_attributes(keeps);
77  }
78 
79  std::tie(original_attributes, label_attributes, pass_through_attributes) =
80  gather_attributes(cache, mesh, options);
81 
82  RegularSpace rs(mesh, label_attributes, options.values, pass_through_attributes);
83  rs.regularize_tags();
84 
85  // clean up attributes
86  {
87  std::vector<attribute::MeshAttributeHandle> keeps = pass_through_attributes;
88  keeps.insert(keeps.end(), original_attributes.begin(), original_attributes.end());
89  mesh.clear_attributes(keeps);
90  }
91 
92  cache.write_mesh(mesh, options.output);
93 }
94 
95 } // namespace wmtk::components
void clear_attributes(const std::vector< attribute::MeshAttributeHandle::HandleVariant > &keep_attributes)
Remove all custom attributes besides the one passed in.
attribute::MeshAttributeHandle register_attribute(const std::string &name, PrimitiveType type, int64_t size, bool replace=false, T default_value=T(0))
attribute::MeshAttributeHandle get_attribute_handle(const std::string &name, const PrimitiveType ptype) const
Definition: Mesh.hpp:918
PrimitiveType top_simplex_type() const
Definition: Mesh.hpp:997
void write_mesh(const Mesh &m, const std::string &name, const std::map< std::string, std::vector< int64_t >> &multimesh_names={})
Write a mesh to cache.
Definition: Cache.cpp:194
std::shared_ptr< Mesh > read_mesh(const std::string &name) const
Load a mesh from cache.
Definition: Cache.cpp:171
std::vector< attribute::MeshAttributeHandle > get_attributes(const Mesh &m, const nlohmann::json &attribute_names)
auto gather_attributes(io::Cache &cache, Mesh &mesh, const internal::RegularSpaceOptions &options)
void regular_space(const utils::Paths &paths, const nlohmann::json &j, io::Cache &cache)
std::vector< PrimitiveType > primitive_below(PrimitiveType pt, bool lower_to_upper)
void log_and_throw_error(const std::string &msg)
Definition: Logger.cpp:101
nlohmann::json json
Definition: input.cpp:9