Wildmeshing Toolkit
tag_intersection.cpp
Go to the documentation of this file.
1 #include "tag_intersection.hpp"
2 
3 #include <deque>
4 #include <wmtk/TriMesh.hpp>
6 #include <wmtk/io/HDF5Writer.hpp>
7 #include <wmtk/io/MeshReader.hpp>
10 #include <wmtk/utils/Logger.hpp>
11 
14 
15 namespace wmtk {
16 namespace components {
18  io::Cache& cache,
19  const Mesh& mesh,
20  const internal::TagIntersectionOptions& options)
21 {
22  using TagVec = std::vector<std::tuple<attribute::MeshAttributeHandle, int64_t>>;
23  using AttrVec = std::vector<attribute::MeshAttributeHandle>;
24 
25  TagVec input_tags;
26  for (size_t i = 0; i < options.attributes.vertex_labels.size(); ++i) {
27  const std::string& name = options.attributes.vertex_labels[i];
28  const int64_t& value = options.values.vertex_values[i];
29 
30  auto handle = mesh.get_attribute_handle<int64_t>(
31  options.attributes.vertex_labels[i],
33 
34  input_tags.emplace_back(std::make_tuple(handle, value));
35  }
36  for (size_t i = 0; i < options.attributes.edge_labels.size(); ++i) {
37  const std::string& name = options.attributes.edge_labels[i];
38  const int64_t& value = options.values.edge_values[i];
39 
40  auto handle = mesh.get_attribute_handle<int64_t>(
41  options.attributes.edge_labels[i],
43 
44  input_tags.emplace_back(std::make_tuple(handle, value));
45  }
46  for (size_t i = 0; i < options.attributes.face_labels.size(); ++i) {
47  const std::string& name = options.attributes.face_labels[i];
48  const int64_t& value = options.values.face_values[i];
49 
50  auto handle = mesh.get_attribute_handle<int64_t>(
51  options.attributes.face_labels[i],
53 
54  input_tags.emplace_back(std::make_tuple(handle, value));
55  }
56  for (size_t i = 0; i < options.attributes.tetrahedron_labels.size(); ++i) {
57  const std::string& name = options.attributes.tetrahedron_labels[i];
58  const int64_t& value = options.values.tetrahedron_values[i];
59 
60  auto handle = mesh.get_attribute_handle<int64_t>(
61  options.attributes.tetrahedron_labels[i],
63 
64  input_tags.emplace_back(std::make_tuple(handle, value));
65  }
66 
67 
68  TagVec output_tags;
69  for (size_t i = 0; i < options.output_attributes.vertex_labels.size(); ++i) {
70  const std::string& name = options.output_attributes.vertex_labels[i];
71  const int64_t& value = options.output_values.vertex_values[i];
72 
73  auto handle = mesh.get_attribute_handle<int64_t>(
76 
77  output_tags.emplace_back(std::make_tuple(handle, value));
78  }
79  for (size_t i = 0; i < options.output_attributes.edge_labels.size(); ++i) {
80  const std::string& name = options.output_attributes.edge_labels[i];
81  const int64_t& value = options.output_values.edge_values[i];
82 
83  auto handle = mesh.get_attribute_handle<int64_t>(
84  options.output_attributes.edge_labels[i],
86 
87  output_tags.emplace_back(std::make_tuple(handle, value));
88  }
89  for (size_t i = 0; i < options.output_attributes.face_labels.size(); ++i) {
90  const std::string& name = options.output_attributes.face_labels[i];
91  const int64_t& value = options.output_values.face_values[i];
92 
93  auto handle = mesh.get_attribute_handle<int64_t>(
94  options.output_attributes.face_labels[i],
96 
97  output_tags.emplace_back(std::make_tuple(handle, value));
98  }
99  for (size_t i = 0; i < options.output_attributes.tetrahedron_labels.size(); ++i) {
100  const std::string& name = options.output_attributes.tetrahedron_labels[i];
101  const int64_t& value = options.output_values.tetrahedron_values[i];
102 
103  auto handle = mesh.get_attribute_handle<int64_t>(
106 
107  output_tags.emplace_back(std::make_tuple(handle, value));
108  }
109 
110  AttrVec pass_through_attributes = utils::get_attributes(cache, mesh, options.pass_through);
111 
112  return std::make_tuple(input_tags, output_tags, pass_through_attributes);
113 }
114 
115 void tag_intersection(const utils::Paths& paths, const nlohmann::json& j, io::Cache& cache)
116 {
117  using namespace internal;
118 
120 
121  auto mesh_in = cache.read_mesh(options.input);
122 
123  Mesh& mesh = static_cast<Mesh&>(*mesh_in);
124 
125  auto [input_tags, output_tags, pass_through_attributes] =
126  gather_attributes(cache, mesh, options);
127 
128  // clear attributes
129  {
130  std::vector<attribute::MeshAttributeHandle> keeps = pass_through_attributes;
131  for (const auto& [h, v] : input_tags) {
132  keeps.emplace_back(h);
133  }
134  for (const auto& [h, v] : output_tags) {
135  keeps.emplace_back(h);
136  }
137  mesh.clear_attributes();
138  }
139 
140  std::tie(input_tags, output_tags, pass_through_attributes) =
141  gather_attributes(cache, mesh, options);
142 
143  switch (mesh_in->top_simplex_type()) {
145  TriMesh& m = static_cast<TriMesh&>(*mesh_in);
147  tag_intersection.compute_intersection(m, input_tags, output_tags);
148  break;
149  }
151  TetMesh& m = static_cast<TetMesh&>(*mesh_in);
153  tag_intersection.compute_intersection(m, input_tags, output_tags);
154  break;
155  }
156  default: {
158  "Works only for faces and tetrahedrons, error-type: {}",
159  mesh_in->top_simplex_type());
160  break;
161  }
162  }
163 
164  cache.write_mesh(*mesh_in, options.output);
165 }
166 } // namespace components
167 } // namespace wmtk
void clear_attributes(const std::vector< attribute::MeshAttributeHandle::HandleVariant > &keep_attributes)
Remove all custom attributes besides the one passed in.
attribute::MeshAttributeHandle get_attribute_handle(const std::string &name, const PrimitiveType ptype) const
Definition: Mesh.hpp:917
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 tag_intersection(const utils::Paths &paths, const nlohmann::json &j, io::Cache &cache)
Definition: Accessor.hpp:6
void log_and_throw_error(const std::string &msg)
Definition: Logger.cpp:101
nlohmann::json json
Definition: input.cpp:9