WaveBlocksND
shape_decoder.hpp
Go to the documentation of this file.
1 #pragma once
2 
3 #include <iostream>
4 #include <fstream>
5 #include <string>
6 #include <vector>
7 #include <array>
8 #include <cmath>
9 
10 #include <yaml-cpp/yaml.h>
11 
12 #include "../wavepackets/shapes/shape_commons.hpp"
13 
14 
15 namespace waveblocks {
16  namespace yaml {
17  template<dim_t D>
18  struct ShapeDecoder
19  {
20  AbstractShape<D>* operator()(YAML::Node const& config)
21  {
22  if (config["dimensionality"].as<int>() != D) {
23  throw std::runtime_error("encountered shape dimensionality != requested shape dimensionality");
24  }
25 
26  std::string type = config["type"].as<std::string>();
27 
28  if (type == "hyperbolic") {
29  int sparsity = int(config["sparsity"].as<double>()+0.5);
30 
31  if (config["limits"]) {
32  return new LimitedHyperbolicCutShape<D>(sparsity, decode_limits(config["limits"]));
33  }
34  else {
35  return new HyperbolicCutShape<D>(sparsity);
36  }
37  }
38  else if (type == "hypercubic") {
39  return new HyperCubicShape<D>(decode_limits(config["limits"]));
40  }
41 
42  throw std::runtime_error("unknown shape type");
43  }
44 
45  private:
46  std::array<int,D> decode_limits(YAML::Node const& node)
47  {
48  std::array<int,D> limits;
49 
50  if (node.size() != D) {
51  throw std::runtime_error("size of limits != shape dimensionality");
52  }
53 
54  for (std::size_t i = 0; i < node.size(); i++) {
55  limits[i] = node[i].as<int>();
56  }
57 
58  return limits;
59  }
60  };
61  }
62 }
Definition: coefficients_file_parser.cpp:10
std::array< int, D > decode_limits(YAML::Node const &node)
Definition: shape_decoder.hpp:46
AbstractShape< D > * operator()(YAML::Node const &config)
Definition: shape_decoder.hpp:20
Definition: shape_decoder.hpp:18