AGX Dynamics  2.37.2.0
GenericJsonParameter.h
Go to the documentation of this file.
1 /*
2 Copyright 2007-2024. Algoryx Simulation AB.
3 
4 All AGX source code, intellectual property, documentation, sample code,
5 tutorials, scene files and technical white papers, are copyrighted, proprietary
6 and confidential material of Algoryx Simulation AB. You may not download, read,
7 store, distribute, publish, copy or otherwise disseminate, use or expose this
8 material unless having a written signed agreement with Algoryx Simulation AB, or having been
9 advised so by Algoryx Simulation AB for a time limited evaluation, or having purchased a
10 valid commercial license from Algoryx Simulation AB.
11 
12 Algoryx Simulation AB disclaims all responsibilities for loss or damage caused
13 from using this software, unless otherwise stated in written agreements with
14 Algoryx Simulation AB.
15 */
16 
17 #pragma once
18 
19 #include <agx/Logger.h>
20 
21 #include <agx/Json.h>
22 #include <agx/String.h>
23 
24 namespace agx
25 {
26 
30  template<typename T>
32  public:
33 
34  using PropertyGetBool = std::function< bool( const T* ) >;
35  using PropertyGetReal = std::function< agx::Real( const T* ) >;
36  using PropertyGetVec3 = std::function< agx::Vec3( const T* ) >;
37 
38  using PropertySetBool = std::function< void( bool, T* ) >;
39  using PropertySetReal = std::function< void( agx::Real, T* ) >;
40  using PropertySetVec3 = std::function< void( agx::Vec3, T* ) >;
41 
42 
51  };
52 
53 
55  : m_type(SCALAR_REAL), m_name(name), m_getReal(getter), m_setReal(setter)
56  {
57  }
58 
59  GenericJsonParameter( const agx::String& name, const agx::String& group, PropertySetReal setter, PropertyGetReal getter )
60  : m_type(SCALAR_REAL), m_name(name), m_group(group), m_getReal(getter), m_setReal(setter)
61  {
62  }
63 
64  GenericJsonParameter( const agx::String& name, PropertySetVec3 setter, PropertyGetVec3 getter, unsigned int numValues )
65  : m_type(ARRAY_VEC3), m_name(name), m_numValues(numValues), m_getVec3(getter), m_setVec3(setter)
66  {
67  }
68 
72  bool isDefault( const agxJson::Value& value ) const
73  {
74  bool def = false;
75  if ( value.isString() ) {
76  def = agx::String(value.asString()).lower() == "default";
77  }
78  return def;
79  }
80 
84  bool parseBool( const agxJson::Value& value ) const
85  {
86  bool result = false;
87 
88  if ( value.isBool() )
89  {
90  result = value.asBool();
91  }
92  else if ( value.isString() ) {
93  result = agx::String(value.asString()).lower() == "true";
94  }
95 
96  return result;
97  }
98 
102  agx::Real parseReal( const agxJson::Value& value ) const
103  {
104  if ( value.isDouble() ) {
105  return value.asDouble();
106  }
107  else if ( value.isString() ) {
108 
109  if ( value.asString() == "inf" ) {
110  return agx::Infinity;
111  }
112  else if ( value.asString() == "-inf" ) {
113  return -agx::Infinity;
114  }
115  }
116 
117  LOGGER_WARNING() << "Unknown value \"" << value.asString() << "\" in json data for " <<
119 
120  return 0;
121  }
122 
123 
133  void parseVec3( const agxJson::Value& value, agx::Vec3& result ) const
134  {
135 
136  if ( value.isArray() ) {
137  if ( value.size() != m_numValues )
138  LOGGER_WARNING() << "Array with values for " << m_name << " does not have expected length "
139  << m_numValues << "\n" << LOGGER_END();
140 
141  agxJson::Value::ArrayIndex maxIndex = std::min(value.size(), std::min(m_numValues, 3u));
142 
143  for ( agxJson::Value::ArrayIndex i = 0; i < maxIndex; i++) {
144  agxJson::Value valueNode = value[i];
145 
146  if ( !isDefault(valueNode) ) {
147  result[i] = parseReal( valueNode );
148  }
149  }
150 
151  }
152  else if ( value.isDouble() ) {
153  // If the value is a single number, then we return a Vec3 with said value
154  result = agx::Vec3( value.asDouble() );
155  }
156  else if ( value.isString() ) {
157  if ( !isDefault(value) )
158  result = agx::Vec3( parseReal( value ) );
159  }
160  else {
161  LOGGER_WARNING() << "Unknown value \"" << value.asString() << "\" in json data for " <<
163  }
164  }
165 
166 
167 
171  bool readProperty(const agxJson::Value& root, T* material )
172  {
173  agxJson::Value object;
174 
175  if ( m_group != "" ) {
176  if ( !root.hasMember( m_group.c_str() ) ) {
177  LOGGER_WARNING() << "Json file is missing object " << m_group << LOGGER_ENDL();
178  return false;
179  }
180  object = root[m_group];
181  }
182  else {
183  object = root;
184  }
185 
186  if ( !object.hasMember( m_name.c_str() ) ) {
187  LOGGER_WARNING() << "Json object is missing " << m_name << LOGGER_ENDL();
188  return false;
189  }
190 
191  agxJson::Value valueNode = object[m_name];
192 
193  // Resolved, the item is found as "valueNode" in the json data.
194  // There can still be warnings from the parsing
195  m_resolved = true;
196 
197  if ( m_type == ARRAY_VEC3 ) {
198  // Need to read previous values in case of partial use of default,
199  // e.g. [1, "default", 2]
200  agx::Vec3 v = m_getVec3( material );
201 
202  parseVec3( object[m_name], v );
203 
204  m_setVec3( v, material );
205  }
206  else if ( m_type == SCALAR_REAL) {
207  if ( !isDefault(valueNode)) {
208  agx::Real r = parseReal( valueNode );
209  m_setReal( r, material );
210  }
211  }
212  else if ( m_type == SCALAR_BOOL ) {
213  if ( !isDefault(valueNode)) {
214  bool b = parseBool( valueNode );
215  m_setBool( b, material );
216  }
217  }
218  else {
219  LOGGER_WARNING() << "Unknown parameter type for " << getParameterPath() << LOGGER_ENDL();
220  return false;
221  }
222 
223  return true;
224  }
225 
226 
230  void writeValueReal( agxJson::Value& node, agx::Real value )
231  {
232  if ( agx::equivalent(value, agx::Infinity)) {
233  node = "inf";
234  }
235  else if ( agx::equivalent(value, -agx::Infinity)) {
236  node = "-inf";
237  }
238  else {
239  node = value;
240  }
241  }
242 
246  void writeValueBool( agxJson::Value& node, bool value )
247  {
248  node = (value ? "true" : "false" );
249  }
250 
251 
255  void writeValueVec3( agxJson::Value& node, agx::Vec3 value )
256  {
257  agxJson::Value::ArrayIndex maxIndex = std::min(m_numValues, 3u);
258  node.resize( maxIndex );
259 
260  for ( agxJson::Value::ArrayIndex i = 0; i < maxIndex; ++i) {
261  node[i] = value[i];
262  }
263  }
264 
265 
270  bool writeProperty(agxJson::Value& root, const T* material )
271  {
272  agxJson::Value* outputNode = &root;
273 
274  if ( m_group.length() > 0 ) {
275 
276  if ( !root.hasMember( m_group ) ) {
277  return false;
278  }
279 
280  outputNode = &root[ m_group ];
281  }
282 
283 
284  if (m_type == SCALAR_REAL ) {
285  writeValueReal( (*outputNode)[ m_name ], m_getReal( material ) );
286  }
287  else if ( m_type == SCALAR_BOOL ) {
288  writeValueBool( (*outputNode)[ m_name ], m_getBool( material ) );
289  }
290  else if ( m_type == ARRAY_VEC3 ) {
291  writeValueVec3( (*outputNode)[ m_name ], m_getVec3( material ) );
292  }
293  else {
294  LOGGER_WARNING() << "Unknown parameter type for " << getParameterPath() << LOGGER_ENDL();
295  }
296 
297  m_resolved = outputNode->hasMember( m_name );
298 
299  return m_resolved;
300  }
301 
302 
306  bool isResolved() const
307  {
308  return m_resolved;
309  }
310 
315  {
316  if ( m_group.length() > 0 ) {
317  return agx::String::format("%s.%s", m_group.c_str(), m_name.c_str());
318  }
319 
320  return m_name;
321  }
322 
323 
324  private:
325  ParameterType m_type { UNDEFINED_TYPE };
326  agx::String m_name;
327  agx::String m_group;
328 
329  unsigned int m_numValues { 0 };
330 
331  PropertyGetBool m_getBool;
332  PropertySetBool m_setBool;
333 
334  PropertyGetReal m_getReal;
335  PropertySetReal m_setReal;
336 
337  PropertyGetVec3 m_getVec3;
338  PropertySetVec3 m_setVec3;
339 
340  bool m_resolved { false };
341  };
342 
343 }
344 
agx::Real parseReal(const agxJson::Value &value) const
Read a real number from a json value.
Vec3T< Real > Vec3
The object holding 3 dimensional vectors and providing basic arithmetic.
Definition: Vec3.h:36
Vec3T< T > min(const Vec3T< T > &lhs, const Vec3T< T > &rhs)
Definition: Vec3Template.h:861
#define LOGGER_ENDL()
Definition: Logger.h:27
bool isResolved() const
Status flag if parameter could be read/written.
std::function< bool(const T *) > PropertyGetBool
AgXString< std::string > String
Definition: String.h:427
void parseVec3(const agxJson::Value &value, agx::Vec3 &result) const
Parse a json node and produce a Vec3.
#define LOGGER_WARNING()
Definition: Logger.h:23
A template class that can be used to read/write a json property and set/get the value in another obje...
ParameterType
Type of parameter that should be handled.
bool isDefault(const agxJson::Value &value) const
Is value marked as "default"?
void writeValueReal(agxJson::Value &node, agx::Real value)
Writes a real value to a json node.
GenericJsonParameter(const agx::String &name, PropertySetReal setter, PropertyGetReal getter)
std::function< void(bool, T *) > PropertySetBool
The agx namespace contains the dynamics/math part of the AGX Dynamics API.
GenericJsonParameter(const agx::String &name, const agx::String &group, PropertySetReal setter, PropertyGetReal getter)
std::function< void(agx::Real, T *) > PropertySetReal
bool parseBool(const agxJson::Value &value) const
Read a bool from a json value.
AGXCORE_EXPORT const Real Infinity
void writeValueVec3(agxJson::Value &node, agx::Vec3 value)
Write a short array, at most 3 elements.
AGXPHYSICS_EXPORT agx::Bool equivalent(const agx::AddedMassInteraction::Matrix6x6 &lhs, const agx::AddedMassInteraction::Matrix6x6 &rhs, agx::Real eps=agx::RealEpsilon)
GenericJsonParameter(const agx::String &name, PropertySetVec3 setter, PropertyGetVec3 getter, unsigned int numValues)
bool writeProperty(agxJson::Value &root, const T *material)
Store a generic property to json.
std::function< agx::Real(const T *) > PropertyGetReal
std::function< agx::Vec3(const T *) > PropertyGetVec3
double Real
Definition: Real.h:42
AGXCORE_EXPORT Component * root()
std::function< void(agx::Vec3, T *) > PropertySetVec3
bool readProperty(const agxJson::Value &root, T *material)
Reads a generic property from a json structure.
static AgXString format(const char *format,...)
C printf formatting of a string.
Definition: String.h:471
agx::String getParameterPath() const
Text representation of where in the json structure this parameter lives.
void writeValueBool(agxJson::Value &node, bool value)
Writes a bool value to a json node.
#define LOGGER_END()
Definition: Logger.h:26