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
10namespace polyfem
11{
12 namespace utils
13 {
14 void apply_common_params(json &args);
15
17 void expand_bc_sidecars(json &args, const json &rules);
18
19 // Templated degree to radians so a scalar or vector can be given
20 template <typename T>
21 inline T deg2rad(T deg)
22 {
23 return deg / 180 * igl::PI;
24 }
25
26 // Converts a JSON rotation expressed in the given rotation mode to a 3D rotation matrix.
27 // NOTE: mode is a copy because the mode will be transformed to be case insensitive
28 Eigen::Matrix3d to_rotation_matrix(const json &jr, std::string mode = "xyz");
29
34 bool is_param_valid(const json &params, const std::string &key);
35
40 template <typename T = json>
41 std::vector<T> json_as_array(const json &j)
42 {
43 if (j.is_array())
44 {
45 return j.get<std::vector<T>>();
46 }
47 else
48 {
49 if constexpr (std::is_same_v<T, json>)
50 return {j};
51 else
52 return {j.get<T>()};
53 }
54 }
55
58 template <typename T>
59 T json_value(const json &params, const std::string &key, const T &default_value)
60 {
61 return is_param_valid(params, key) ? params[key].get<T>() : default_value;
62 }
63 } // namespace utils
64} // namespace polyfem
void expand_bc_sidecars(json &args, const json &rules)
Expand string entries in dirichlet_boundary that point to .json files.
Definition JSONUtils.cpp:64
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:41
T deg2rad(T deg)
Definition JSONUtils.hpp:21
bool is_param_valid(const json &params, const std::string &key)
Determine if a key exists and is non-null in a json object.
void apply_common_params(json &args)
Definition JSONUtils.cpp:17
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:59
nlohmann::json json
Definition Common.hpp:9