PolyFEM
Loading...
Searching...
No Matches
JSONUtils.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <polyfem/Common.hpp>
4#include <polysolve/JSONUtils.hpp>
5
6#include <Eigen/Core>
7#include <igl/PI.h>
8#include <filesystem>
9#include <memory>
10
11namespace polyfem::io
12{
13 class ResourceIO;
14}
15
16namespace polyfem
17{
18 namespace utils
19 {
20 void apply_common_params(json &args);
21 std::unique_ptr<const io::ResourceIO> apply_common_params(json &args, const io::ResourceIO &resources);
22
24 void expand_bc_sidecars(json &args, const json &rules);
25 void expand_bc_sidecars(json &args, const json &rules, const io::ResourceIO &resources);
26
27 // Templated degree to radians so a scalar or vector can be given
28 template <typename T>
29 inline T deg2rad(T deg)
30 {
31 return deg / 180 * igl::PI;
32 }
33
34 // Converts a JSON rotation expressed in the given rotation mode to a 3D rotation matrix.
35 // NOTE: mode is a copy because the mode will be transformed to be case insensitive
36 Eigen::Matrix3d to_rotation_matrix(const json &jr, std::string mode = "xyz");
37
42 bool is_param_valid(const json &params, const std::string &key);
43
48 template <typename T = json>
49 std::vector<T> json_as_array(const json &j)
50 {
51 if (j.is_array())
52 {
53 return j.get<std::vector<T>>();
54 }
55 else
56 {
57 if constexpr (std::is_same_v<T, json>)
58 return {j};
59 else
60 return {j.get<T>()};
61 }
62 }
63
66 template <typename T>
67 T json_value(const json &params, const std::string &key, const T &default_value)
68 {
69 return is_param_valid(params, key) ? params[key].get<T>() : default_value;
70 }
71 } // namespace utils
72} // namespace polyfem
std::unique_ptr< const io::ResourceIO > apply_common_params(json &args, const io::ResourceIO &resources)
Definition JSONUtils.cpp:24
void expand_bc_sidecars(json &args, const json &rules)
Expand string entries in dirichlet_boundary that point to .json files.
Eigen::Matrix3d to_rotation_matrix(const json &jr, std::string mode)
std::vector< T > json_as_array(const json &j)
Return the value of a json object as an array.
Definition JSONUtils.hpp:49
T deg2rad(T deg)
Definition JSONUtils.hpp:29
bool is_param_valid(const json &params, const std::string &key)
Determine if a key exists and is non-null in a json object.
T json_value(const json &params, const std::string &key, const T &default_value)
Get a parameter from a json object or return a default value if the parameter invalid.
Definition JSONUtils.hpp:67
nlohmann::json json
Definition Common.hpp:9