Wildmeshing Toolkit
Grid3Options.hpp
Go to the documentation of this file.
1 #pragma once
2 #include <fmt/ranges.h>
3 #include <spdlog/spdlog.h>
4 #include <array>
5 #include <bitset>
6 #include <nlohmann/json.hpp>
7 #include <optional>
8 #include <string>
9 
12 {
13 public:
14  enum class TilingType { BCC = 0, Freudenthal = 1 };
15  const static std::array<std::string, 2> tiling_names;
17  std::array<int64_t, 3> dimensions;
18  std::bitset<3> cycles;
19  struct Coordinates
20  {
21  std::string name;
22  std::array<double, 3> spacing;
24  };
25  std::optional<Coordinates> coordinates;
26  std::optional<std::string> get_coordinate_name() const
27  {
28  if (coordinates.has_value()) {
29  return coordinates.value().name;
30  } else {
31  return {};
32  }
33  }
34  friend void to_json(nlohmann::json& nlohmann_json_j, const Grid3Options& nlohmann_json_t)
35  {
36  nlohmann_json_j["tiling"] = tiling_names[static_cast<size_t>(nlohmann_json_t.tiling_type)];
37  if (nlohmann_json_t.coordinates.has_value()) {
38  nlohmann_json_j["coordinates"] = *nlohmann_json_t.coordinates;
39  }
40  nlohmann_json_j["dimensions"] = nlohmann_json_t.dimensions;
41  {
42  const auto& b = nlohmann_json_t.cycles;
43  std::array<bool, 3> bs{{b[0], b[1], b[2]}};
44  nlohmann_json_j["cycles"] = bs;
45  }
46  }
47  friend void from_json(const nlohmann::json& nlohmann_json_j, Grid3Options& nlohmann_json_t)
48  {
49  nlohmann_json_t.dimensions = nlohmann_json_j["dimensions"].get<std::array<int64_t, 3>>();
50  {
51  const std::string tiling = nlohmann_json_j["tiling"];
52  bool found = false;
53  for (size_t j = 0; j < tiling_names.size(); ++j) {
54  if (tiling == tiling_names[j]) {
55  found = true;
56  nlohmann_json_t.tiling_type = static_cast<TilingType>(j);
57  }
58  }
59  if (!found) {
60  throw std::runtime_error(fmt::format(
61  "Tiling type was not found, got [{}], expected one of {{[{}]}}",
62  tiling,
63  fmt::join(tiling_names, "],[")));
64  }
65  }
66  {
67  auto& b = nlohmann_json_t.cycles;
68  const auto& c = nlohmann_json_j["cycles"];
69  for (int j = 0; j < 3; ++j) {
70  b[j] = c[j];
71  }
72  }
73  if (nlohmann_json_j.contains("coordinates")) {
74  if (const auto& coords = nlohmann_json_j["coordinates"]; !coords.is_null()) {
75  if (nlohmann_json_j["coordinates"]["spacing"][0] > 0)
76  nlohmann_json_t.coordinates = coords.get<Coordinates>();
77  }
78  }
79  }
80 };
81 } // namespace wmtk::components::procedural
static const std::array< std::string, 2 > tiling_names
std::optional< std::string > get_coordinate_name() const
friend void from_json(const nlohmann::json &nlohmann_json_j, Grid3Options &nlohmann_json_t)
friend void to_json(nlohmann::json &nlohmann_json_j, const Grid3Options &nlohmann_json_t)
std::optional< Coordinates > coordinates
NLOHMANN_DEFINE_TYPE_INTRUSIVE(Coordinates, name, spacing)
nlohmann::json json
Definition: input.cpp:9