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
16 // Templated degree to radians so a scalar or vector can be given
17 template <typename T>
18 inline T deg2rad(T deg)
19 {
20 return deg / 180 * igl::PI;
21 }
22
23 // Converts a JSON rotation expressed in the given rotation mode to a 3D rotation matrix.
24 // NOTE: mode is a copy because the mode will be transformed to be case insensitive
25 Eigen::Matrix3d to_rotation_matrix(const json &jr, std::string mode = "xyz");
26
31 bool is_param_valid(const json &params, const std::string &key);
32
37 template <typename T = json>
38 std::vector<T> json_as_array(const json &j)
39 {
40 if (j.is_array())
41 {
42 return j.get<std::vector<T>>();
43 }
44 else
45 {
46 if constexpr (std::is_same_v<T, json>)
47 return {j};
48 else
49 return {j.get<T>()};
50 }
51 }
52
55 template <typename T>
56 T json_value(const json &params, const std::string &key, const T &default_value)
57 {
58 return is_param_valid(params, key) ? params[key].get<T>() : default_value;
59 }
60 } // namespace utils
61} // namespace polyfem
Eigen::Matrix3d to_rotation_matrix(const json &jr, std::string mode)
Definition JSONUtils.cpp:61
std::vector< T > json_as_array(const json &j)
Return the value of a json object as an array.
Definition JSONUtils.hpp:38
T deg2rad(T deg)
Definition JSONUtils.hpp:18
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:14
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:56
nlohmann::json json
Definition Common.hpp:9