Wildmeshing Toolkit
get_attributes.cpp
Go to the documentation of this file.
1 #include "get_attributes.hpp"
2 
3 #include <wmtk/EdgeMesh.hpp>
4 #include <wmtk/PointMesh.hpp>
5 #include <wmtk/TetMesh.hpp>
6 #include <wmtk/TriMesh.hpp>
7 
9 #include <wmtk/utils/Logger.hpp>
11 
12 namespace wmtk::components::utils {
13 
14 attribute::MeshAttributeHandle get_attribute(const Mesh& m, const std::string& name)
15 {
16  int64_t count = 0;
17 
18  std::vector<attribute::MeshAttributeHandle> handles;
19 
21  if (m.has_attribute<char>(name, ptype)) {
22  handles.emplace_back(m.get_attribute_handle<char>(name, ptype));
23  }
24  if (m.has_attribute<int64_t>(name, ptype)) {
25  handles.emplace_back(m.get_attribute_handle<int64_t>(name, ptype));
26  }
27  if (m.has_attribute<double>(name, ptype)) {
28  handles.emplace_back(m.get_attribute_handle<double>(name, ptype));
29  }
30  if (m.has_attribute<Rational>(name, ptype)) {
31  handles.emplace_back(m.get_attribute_handle<Rational>(name, ptype));
32  }
33  }
34 
35  if (handles.empty()) {
36  log_and_throw_error("Attribute with name {} was not found.", name);
37  }
38  if (handles.size() > 1) {
40  "Attribute name ambiguity: {} attributes have the name {}",
41  handles.size(),
42  name);
43  }
44 
45 
46  return handles.front();
47 }
48 
50 {
51  if (attribute.is_string()) {
52  return get_attribute(m, attribute.get<std::string>());
53  } else if (attribute.is_object()) {
54  log_and_throw_error("Outdated branch that needs to be deleted.");
55  // const std::string name = attribute["name"];
56  // const std::string mesh = attribute["mesh"];
57  //
58  // const auto& child_ptr = cache.read_mesh(mesh);
59  //
60  // return get_attribute(*child_ptr, name);
61  }
62 
63  log_and_throw_error("Invalid type for {}.", std::string(attribute));
64 }
65 
66 
67 std::vector<attribute::MeshAttributeHandle> get_attributes(
68  const Mesh& m,
69  const nlohmann::json& attribute_names)
70 {
71  std::vector<attribute::MeshAttributeHandle> handles;
72 
73  if (attribute_names.is_array()) {
74  for (const auto& name : attribute_names) {
75  handles.push_back(get_attribute(m, name));
76  }
77  } else {
78  handles.push_back(get_attribute(m, attribute_names));
79  }
80 
81  return handles;
82 }
83 
84 } // namespace wmtk::components::utils
attribute::MeshAttributeHandle get_attribute_handle(const std::string &name, const PrimitiveType ptype) const
Definition: Mesh.hpp:917
bool has_attribute(const std::string &name, const PrimitiveType ptype) const
Definition: Mesh.hpp:937
PrimitiveType top_simplex_type() const
Definition: Mesh.hpp:996
nlohmann::json json
attribute::MeshAttributeHandle get_attribute(const Mesh &m, const std::string &name)
Get an attribute handle only by name.
std::vector< attribute::MeshAttributeHandle > get_attributes(const Mesh &m, const nlohmann::json &attribute_names)
std::vector< PrimitiveType > primitive_below(PrimitiveType pt, bool lower_to_upper)
void log_and_throw_error(const std::string &msg)
Definition: Logger.cpp:101