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