SPH
Project.cpp
Go to the documentation of this file.
1 #include "gui/Project.h"
2 #include "run/VirtualSettings.h"
3 
5 
6 Project::Project() {
25  .set(GuiSettingsId::IMAGES_NAME, std::string("frag_%e_%d.png"))
26  .set(GuiSettingsId::IMAGES_MOVIE_NAME, std::string("frag_%e.avi"))
29  .set(GuiSettingsId::PLOT_OVERPLOT_SFD, std::string("reference_sfd.txt"))
33 }
34 
35 void Project::save(Config& config) {
36  this->savePalettes(config);
37  this->saveGui(config);
38 }
39 
40 void Project::load(Config& config) {
41  this->loadPalettes(config);
42  this->loadGui(config);
43 }
44 
45 template <typename T>
46 INLINE auto settingsToConfigValue(const T& value) {
47  return value;
48 }
49 
50 template <>
52  return int(value.value);
53 }
54 template <>
57  return 0;
58 }
59 template <>
62  return 0;
63 }
64 template <>
67  return 0;
68 }
69 
70 template <typename TOrig, typename TNew>
71 INLINE auto configToSettingsValue(const TOrig& UNUSED(original), const TNew& value) {
72  return value;
73 }
74 template <>
75 INLINE auto configToSettingsValue(const EnumWrapper& original, const int& value) {
76  EnumWrapper ew;
77  ew.value = value;
78  ew.index = original.index;
79  return ew;
80 }
81 
82 
83 void Project::saveGui(Config& config) {
84  SharedPtr<ConfigNode> guiNode = config.addNode("gui");
85  for (const auto& entry : gui) {
87  if (!key) {
88  throw ConfigException("No settings entry with id " + std::to_string(int(entry.id)));
89  }
90  forValue(entry.value, [&guiNode, &key](const auto& value) { //
91  guiNode->set(key.value(), settingsToConfigValue(value));
92  });
93  }
94 }
95 
96 void Project::savePalettes(Config& config) {
97  SharedPtr<ConfigNode> paletteParentNode = config.addNode("palettes");
98  for (auto& element : palettes) {
99  SharedPtr<ConfigNode> paletteNode = paletteParentNode->addChild(element.key);
100  const Palette& palette = element.value;
101  paletteNode->set("lower", palette.getInterval().lower());
102  paletteNode->set("upper", palette.getInterval().upper());
103  paletteNode->set("scale", int(palette.getScale()));
104 
105  std::stringstream ss;
106  palette.saveToStream(ss);
107  std::string data = replaceAll(ss.str(), "\n", ";");
108  paletteNode->set("data", data);
109  }
110 }
111 
112 void Project::loadGui(Config& config) {
113  SharedPtr<ConfigNode> guiNode = config.getNode("gui");
114  for (const auto& entry : gui) {
115  const Optional<std::string> key = GuiSettings::getEntryName(entry.id);
116  if (!key) {
117  throw ConfigException("No settings entry with id " + std::to_string(int(entry.id)));
118  }
119 
120  forValue(entry.value, [this, &guiNode, &key, &entry](auto& value) {
121  using Type = decltype(settingsToConfigValue(value));
122 
123  try {
124  const Type loadedValue = guiNode->get<Type>(key.value());
125  gui.set(entry.id, configToSettingsValue(value, loadedValue));
126  } catch (ConfigException& UNUSED(e)) {
129  }
130  });
131  }
132 
134  Rgba background = gui.get<Rgba>(GuiSettingsId::BACKGROUND_COLOR);
135  background.a() = 1.f;
136  gui.set<Rgba>(GuiSettingsId::BACKGROUND_COLOR, background);
137 
138  if (gui.get<Float>(GuiSettingsId::CAMERA_ORTHO_FOV) == 0._f) {
139  gui.set(GuiSettingsId::CAMERA_ORTHO_FOV, 1.e5_f);
140  gui.set(GuiSettingsId::CAMERA_AUTOSETUP, true);
141  }
142 }
143 
144 void Project::loadPalettes(Config& config) {
145  palettes.clear();
146  SharedPtr<ConfigNode> paletteParentNode = config.getNode("palettes");
147  paletteParentNode->enumerateChildren([this](const std::string& name, ConfigNode& paletteNode) {
148  const float lower = float(paletteNode.get<Float>("lower"));
149  const float upper = float(paletteNode.get<Float>("upper"));
150  const PaletteScale scale = PaletteScale(paletteNode.get<int>("scale"));
151  Palette palette({ { lower, Rgba::black() }, { upper, Rgba::white() } }, scale);
152 
153  if (paletteNode.contains("data")) {
154  std::string data = paletteNode.get<std::string>("data");
155  data = replaceAll(data, ";", "\n");
156  std::stringstream ss(data);
157  if (palette.loadFromStream(ss)) {
158  palettes.insert(name, palette);
159  }
160  } else { // older format, palettes in separate files
161  const std::string path = paletteNode.get<std::string>("file");
162  if (palette.loadFromFile(Path(path))) {
163  palettes.insert(name, palette);
164  }
165  }
166  });
167 }
168 
#define NOT_IMPLEMENTED
Helper macro marking missing implementation.
Definition: Assert.h:100
NAMESPACE_SPH_BEGIN
Definition: BarnesHut.cpp:13
Exception ConfigException
Definition: Config.h:17
double Float
Precision used withing the code. Use Float instead of float or double where precision is important.
Definition: Globals.h:13
#define INLINE
Macros for conditional compilation based on selected compiler.
Definition: Object.h:31
#define UNUSED(x)
Definition: Object.h:37
#define NAMESPACE_SPH_END
Definition: Object.h:12
PaletteScale
Definition: Palette.h:13
INLINE auto configToSettingsValue(const TOrig &UNUSED(original), const TNew &value)
Definition: Project.cpp:71
INLINE auto settingsToConfigValue(const T &value)
Definition: Project.cpp:46
std::string replaceAll(const std::string &source, const std::string &old, const std::string &s)
Replaces all occurences of string with a new string.
decltype(auto) INLINE forValue(Variant< TArgs... > &variant, TFunctor &&functor)
Definition: Variant.h:428
BasicVector< Float > Vector
Definition: Vector.h:539
INLINE Vector getNormalized(const Vector &v)
Definition: Vector.h:590
Object providing connection between component parameters and values exposed to the user.
Represents a single node in the hierarchy written into config file.
Definition: Config.h:198
Type get(const std::string &name)
Returns a value stored in the node.
Definition: Config.h:220
bool contains(const std::string &name)
Checks if the node contains an entry of given name.
Definition: Config.h:243
Provides functionality for reading and writing configuration files.
Definition: Config.h:272
SharedPtr< ConfigNode > addNode(const std::string &name)
Adds a new node to the config.
Definition: Config.cpp:81
SharedPtr< ConfigNode > getNode(const std::string &name)
Returns a node with given name.
Definition: Config.cpp:85
Generic exception.
Definition: Exceptions.h:10
INLINE GuiSettings & set(const GuiSettingsId id, const TValue &value)
Definition: Settings.h:250
Object representing a 1D interval of real numbers.
Definition: Interval.h:17
INLINE Float lower() const
Returns lower bound of the interval.
Definition: Interval.h:74
INLINE Float upper() const
Returns upper bound of the interval.
Definition: Interval.h:79
Represents a color palette, used for mapping arbitrary number to a color.
Definition: Palette.h:25
PaletteScale getScale() const
Returns the scale of the palette.
Definition: Palette.cpp:107
Outcome saveToStream(std::ostream &ofs, const Size lineCnt=256) const
Saves the palettes into given output stream.
Definition: Palette.cpp:205
Outcome loadFromFile(const Path &path)
Loads the palette from given .csv file.
Definition: Palette.cpp:200
Outcome loadFromStream(std::istream &ifs)
Loads the palette from given input stream.
Definition: Palette.cpp:167
Interval getInterval() const
Returns the interval for which the palette is defined.
Definition: Palette.cpp:90
Object representing a path on a filesystem.
Definition: Path.h:17
void load(Config &config)
Definition: Project.cpp:40
void save(Config &config)
Definition: Project.cpp:35
Definition: Color.h:8
static Rgba black()
Definition: Color.h:190
float & a()
Definition: Color.h:63
static Rgba white()
Definition: Color.h:194
static Optional< std::string > getEntryName(const GuiSettingsId idx)
Returns the human-readable name of the entry with given index.
Definition: Settings.h:357
Symmetric tensor of 2nd order.
Symmetric traceless 2nd order tensor.
@ RAYTRACE_ITERATION_LIMIT
@ IMAGES_NAME
Mask of the image names, having d where the output number will be placed.
@ PARTICLE_RADIUS
Displayed radius of particle in units of smoothing length.
@ SURFACE_RESOLUTION
Size of the grid used in MarchingCubes (in code units, not h).
@ CAMERA_ORTHO_CUTOFF
Max z-coordinate of particle to be displayed by ortho renderer.
@ SURFACE_LEVEL
Value of iso-surface being constructed; lower value means larget bodies.
@ IMAGES_SAVE
If true, rendered images are saved to disk.
@ SURFACE_SUN_POSITION
Direction to the sun used for shading.
@ CAMERA_ORTHO_FOV
View field of view (zoom). Special value 0 means the field of view is computed from the bounding box.
@ SURFACE_AMBIENT
Ambient color for surface renderer.
@ TOTAL_MOMENTUM
Evolution of the total momentum in time.
@ INTERNAL_ENERGY
Evolution of the total internal energy in time.
@ TOTAL_ANGULAR_MOMENTUM
Evolution of the total angular momentum in time.
@ KINETIC_ENERGY
Evolution of the total kinetic energy in time.
Wrapper of an enum.
Definition: Settings.h:37
int value
Definition: Settings.h:38
EnumIndex index
Definition: Settings.h:40