AGX Dynamics  2.38.0.2
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 PropertyGetInt = std::function< int( const T* ) >;
35  using PropertyGetBool = std::function< bool( const T* ) >;
36  using PropertyGetString = std::function< agx::String( const T* ) >;
37  using PropertyGetReal = std::function< agx::Real( const T* ) >;
38  using PropertyGetVec3 = std::function< agx::Vec3( const T* ) >;
39 
40  using PropertySetInt = std::function< void(int, T* ) >;
41  using PropertySetBool = std::function< void( bool, T* ) >;
42  using PropertySetString = std::function< void( agx::String, T* ) >;
43  using PropertySetReal = std::function< void( agx::Real, T* ) >;
44  using PropertySetVec3 = std::function< void(agx::Vec3, T*) >;
45 
46 
57  };
58 
59  explicit GenericJsonParameter(const agx::String& name, PropertySetInt setter, PropertyGetInt getter)
60  : m_type(SCALAR_INT), m_name(name), m_getInt(getter), m_setInt(setter)
61  {
62  }
63 
64  explicit GenericJsonParameter(const agx::String& name, PropertySetBool setter, PropertyGetBool getter)
65  : m_type(SCALAR_BOOL), m_name(name), m_getBool(getter), m_setBool(setter)
66  {
67  }
68 
69  explicit GenericJsonParameter( const agx::String& name, PropertySetReal setter, PropertyGetReal getter )
70  : m_type(SCALAR_REAL), m_name(name), m_getReal(getter), m_setReal(setter)
71  {
72  }
73 
75  : m_type(STRING), m_name(name), m_getString(getter), m_setString(setter)
76  {
77  }
78 
79  explicit GenericJsonParameter(const agx::String& name, PropertySetVec3 setter, PropertyGetVec3 getter, unsigned int numValues)
80  : m_type(ARRAY_VEC3), m_name(name), m_numValues(numValues), m_getVec3(getter), m_setVec3(setter)
81  {
82  }
83 
84 
85  explicit GenericJsonParameter(const agx::String& name, const agx::String& group, PropertySetInt setter, PropertyGetInt getter)
86  : m_type(SCALAR_INT), m_name(name), m_group(group), m_getInt(getter), m_setInt(setter)
87  {
88  }
89 
90  explicit GenericJsonParameter(const agx::String& name, const agx::String& group, PropertySetBool setter, PropertyGetBool getter)
91  : m_type(SCALAR_BOOL), m_name(name), m_group(group), m_getBool(getter), m_setBool(setter)
92  {
93  }
94 
95  explicit GenericJsonParameter( const agx::String& name, const agx::String& group, PropertySetReal setter, PropertyGetReal getter )
96  : m_type(SCALAR_REAL), m_name(name), m_group(group), m_getReal(getter), m_setReal(setter)
97  {
98  }
99 
100  explicit GenericJsonParameter(const agx::String& name, const agx::String& group, PropertySetString setter, PropertyGetString getter)
101  : m_type(STRING), m_name(name), m_group(group), m_getString(getter), m_setString(setter)
102  {
103  }
104 
105  explicit GenericJsonParameter(const agx::String& name, const agx::String& group, PropertySetVec3 setter, PropertyGetVec3 getter)
106  : m_type(ARRAY_VEC3), m_name(name), m_group(group), m_getVec3(getter), m_setVec3(setter)
107  {
108  }
109 
113  bool isDefault( const agxJson::Value& value ) const
114  {
115  bool def = false;
116  if ( value.isString() ) {
117  def = agx::String(value.asString()).lower() == "default";
118  }
119  return def;
120  }
121 
125  int parseInt(const agxJson::Value& value) const
126  {
127  int result = false;
128 
129  if (value.isInt())
130  {
131  result = value.asInt();
132  }
133  else if (value.isString()) {
134  result = atoi(value.asString().c_str());
135  }
136 
137  return result;
138  }
139 
143  bool parseBool( const agxJson::Value& value ) const
144  {
145  bool result = false;
146 
147  if ( value.isBool() )
148  {
149  result = value.asBool();
150  }
151  else if ( value.isString() ) {
152  result = agx::String(value.asString()).lower() == "true";
153  }
154 
155  return result;
156  }
157 
161  agx::String parseString(const agxJson::Value& value) const
162  {
163  agx::String result;
164 
165  if (value.isString())
166  result = value.asString();
167 
168  return result;
169  }
170 
174  agx::Real parseReal( const agxJson::Value& value ) const
175  {
176  if ( value.isDouble() ) {
177  return value.asDouble();
178  }
179  else if ( value.isString() ) {
180 
181  if ( value.asString() == "inf" ) {
182  return agx::Infinity;
183  }
184  else if ( value.asString() == "-inf" ) {
185  return -agx::Infinity;
186  }
187  }
188 
189  LOGGER_WARNING() << "Unknown value \"" << value.asString() << "\" in json data for " <<
191 
192  return 0;
193  }
194 
195 
205  void parseVec3( const agxJson::Value& value, agx::Vec3& result ) const
206  {
207 
208  if ( value.isArray() ) {
209  if ( value.size() != m_numValues )
210  LOGGER_WARNING() << "Array with values for " << m_name << " does not have expected length "
211  << m_numValues << "\n" << LOGGER_END();
212 
213  agxJson::Value::ArrayIndex maxIndex = std::min(value.size(), std::min(m_numValues, 3u));
214 
215  for ( agxJson::Value::ArrayIndex i = 0; i < maxIndex; i++) {
216  agxJson::Value valueNode = value[i];
217 
218  if ( !isDefault(valueNode) ) {
219  result[i] = parseReal( valueNode );
220  }
221  }
222 
223  }
224  else if ( value.isDouble() ) {
225  // If the value is a single number, then we return a Vec3 with said value
226  result = agx::Vec3( value.asDouble() );
227  }
228  else if ( value.isString() ) {
229  if ( !isDefault(value) )
230  result = agx::Vec3( parseReal( value ) );
231  }
232  else {
233  LOGGER_WARNING() << "Unknown value \"" << value.asString() << "\" in json data for " <<
235  }
236  }
237 
238 
239 
246  bool readProperty(const agxJson::Value& root, T* material, bool optional=false )
247  {
248  agxJson::Value object;
249 
250  if ( m_group != "" ) {
251  if ( !root.hasMember( m_group.c_str() ) ) {
252  if (!optional)
253  LOGGER_WARNING() << "Json file is missing object " << m_group << LOGGER_ENDL();
254  return false;
255  }
256  object = root[m_group];
257  }
258  else {
259  object = root;
260  }
261 
262  if ( !object.hasMember( m_name.c_str() ) ) {
263  if (!optional)
264  LOGGER_WARNING() << "Json object is missing " << m_name << LOGGER_ENDL();
265  return false;
266  }
267 
268  agxJson::Value valueNode = object[m_name];
269 
270  // Resolved, the item is found as "valueNode" in the json data.
271  // There can still be warnings from the parsing
272  m_resolved = true;
273 
274  if ( m_type == ARRAY_VEC3 ) {
275  // Need to read previous values in case of partial use of default,
276  // e.g. [1, "default", 2]
277  agx::Vec3 v = m_getVec3( material );
278 
279  parseVec3( object[m_name], v );
280 
281  m_setVec3( v, material );
282  }
283  else if ( m_type == SCALAR_REAL) {
284  if ( !isDefault(valueNode)) {
285  agx::Real r = parseReal( valueNode );
286  m_setReal( r, material );
287  }
288  }
289  else if (m_type == STRING) {
290  if (!valueNode.asString().empty()) {
291  agx::String r = parseString(valueNode);
292  m_setString(r, material);
293  }
294  }
295  else if (m_type == SCALAR_INT) {
296  if (!isDefault(valueNode)) {
297  int val = parseInt(valueNode);
298  m_setInt(val, material);
299  }
300  }
301  else if ( m_type == SCALAR_BOOL ) {
302  if ( !isDefault(valueNode)) {
303  bool b = parseBool( valueNode );
304  m_setBool( b, material );
305  }
306  }
307  else {
308  LOGGER_WARNING() << "Unknown parameter type for " << getParameterPath() << LOGGER_ENDL();
309  return false;
310  }
311 
312  return true;
313  }
314 
315 
319  void writeValueReal( agxJson::Value& node, agx::Real value )
320  {
321  if ( agx::equivalent(value, agx::Infinity)) {
322  node = "inf";
323  }
324  else if ( agx::equivalent(value, -agx::Infinity)) {
325  node = "-inf";
326  }
327  else {
328  node = value;
329  }
330  }
331 
335  void writeValueString(agxJson::Value& node, const agx::String value)
336  {
337  node = value;
338  }
339 
343  void writeValueInt(agxJson::Value& node, int value)
344  {
345  node = value;
346  }
347 
348 
352  void writeValueBool( agxJson::Value& node, bool value )
353  {
354  node = (value ? "true" : "false" );
355  }
356 
357 
361  void writeValueVec3( agxJson::Value& node, agx::Vec3 value )
362  {
363  agxJson::Value::ArrayIndex maxIndex = std::min(m_numValues, 3u);
364  node.resize( maxIndex );
365 
366  for ( agxJson::Value::ArrayIndex i = 0; i < maxIndex; ++i) {
367  node[i] = value[i];
368  }
369  }
370 
371 
376  bool writeProperty(agxJson::Value& root, const T* material )
377  {
378  agxJson::Value* outputNode = &root;
379 
380  if ( m_group.length() > 0 ) {
381 
382  if ( !root.hasMember( m_group ) ) {
383  return false;
384  }
385 
386  outputNode = &root[ m_group ];
387  }
388 
389 
390  if (m_type == SCALAR_REAL ) {
391  writeValueReal( (*outputNode)[ m_name ], m_getReal( material ) );
392  }
393  else if ( m_type == SCALAR_BOOL ) {
394  writeValueBool( (*outputNode)[ m_name ], m_getBool( material ) );
395  }
396  else if (m_type == SCALAR_INT) {
397  writeValueInt((*outputNode)[m_name], m_getInt(material));
398  }
399  else if (m_type == STRING) {
400  writeValueString((*outputNode)[m_name], m_getString(material));
401  }
402  else if ( m_type == ARRAY_VEC3 ) {
403  writeValueVec3( (*outputNode)[ m_name ], m_getVec3( material ) );
404  }
405  else {
406  LOGGER_WARNING() << "Unknown parameter type for " << getParameterPath() << LOGGER_ENDL();
407  }
408 
409  m_resolved = outputNode->hasMember( m_name );
410 
411  return m_resolved;
412  }
413 
414 
418  bool isResolved() const
419  {
420  return m_resolved;
421  }
422 
427  {
428  if ( m_group.length() > 0 ) {
429  return agx::String::format("%s.%s", m_group.c_str(), m_name.c_str());
430  }
431 
432  return m_name;
433  }
434 
435 
436  private:
437  ParameterType m_type { UNDEFINED_TYPE };
438  agx::String m_name;
439  agx::String m_group;
440 
441  unsigned int m_numValues { 0 };
442 
443  PropertyGetInt m_getInt;
444  PropertySetInt m_setInt;
445 
446  PropertyGetBool m_getBool;
447  PropertySetBool m_setBool;
448 
449  PropertyGetReal m_getReal;
450  PropertySetReal m_setReal;
451 
452  PropertyGetVec3 m_getVec3;
453  PropertySetVec3 m_setVec3;
454 
455  PropertyGetString m_getString;
456  PropertySetString m_setString;
457 
458  bool m_resolved { false };
459  };
460 
461 }
462 
LOGGER_WARNING
#define LOGGER_WARNING()
Definition: Logger.h:23
agx::AgXString::format
static AgXString format(const char *format,...)
C printf formatting of a string.
Definition: String.h:471
agx::GenericJsonParameter::SCALAR_BOOL
@ SCALAR_BOOL
Definition: GenericJsonParameter.h:53
agx::Real
double Real
Definition: Real.h:42
agx::GenericJsonParameter::GenericJsonParameter
GenericJsonParameter(const agx::String &name, const agx::String &group, PropertySetReal setter, PropertyGetReal getter)
Definition: GenericJsonParameter.h:95
agx::GenericJsonParameter::PropertySetReal
std::function< void(agx::Real, T *) > PropertySetReal
Definition: GenericJsonParameter.h:43
agx::equivalent
AGXPHYSICS_EXPORT agx::Bool equivalent(const agx::AddedMassInteraction::Matrix6x6 &lhs, const agx::AddedMassInteraction::Matrix6x6 &rhs, agx::Real eps=agx::RealEpsilon)
agx::GenericJsonParameter::PropertySetString
std::function< void(agx::String, T *) > PropertySetString
Definition: GenericJsonParameter.h:42
agx::GenericJsonParameter
A template class that can be used to read/write a json property and set/get the value in another obje...
Definition: GenericJsonParameter.h:31
agx::GenericJsonParameter::GenericJsonParameter
GenericJsonParameter(const agx::String &name, PropertySetVec3 setter, PropertyGetVec3 getter, unsigned int numValues)
Definition: GenericJsonParameter.h:79
agx::GenericJsonParameter::ParameterType
ParameterType
Type of parameter that should be handled.
Definition: GenericJsonParameter.h:50
agx::GenericJsonParameter::ARRAY_VEC3
@ ARRAY_VEC3
Definition: GenericJsonParameter.h:56
agx
The agx namespace contains the dynamics/math part of the AGX Dynamics API.
Definition: AddedMassInteraction.h:22
agx::GenericJsonParameter::GenericJsonParameter
GenericJsonParameter(const agx::String &name, PropertySetString setter, PropertyGetString getter)
Definition: GenericJsonParameter.h:74
agx::GenericJsonParameter::parseVec3
void parseVec3(const agxJson::Value &value, agx::Vec3 &result) const
Parse a json node and produce a Vec3.
Definition: GenericJsonParameter.h:205
agx::GenericJsonParameter::PropertyGetInt
std::function< int(const T *) > PropertyGetInt
Definition: GenericJsonParameter.h:34
agx::GenericJsonParameter::writeValueInt
void writeValueInt(agxJson::Value &node, int value)
Writes an int value to a json node.
Definition: GenericJsonParameter.h:343
agx::GenericJsonParameter::SCALAR_REAL
@ SCALAR_REAL
Definition: GenericJsonParameter.h:54
agx::GenericJsonParameter::writeProperty
bool writeProperty(agxJson::Value &root, const T *material)
Store a generic property to json.
Definition: GenericJsonParameter.h:376
agx::GenericJsonParameter::PropertySetVec3
std::function< void(agx::Vec3, T *) > PropertySetVec3
Definition: GenericJsonParameter.h:44
agx::GenericJsonParameter::UNDEFINED_TYPE
@ UNDEFINED_TYPE
Definition: GenericJsonParameter.h:51
agx::Infinity
const AGXCORE_EXPORT Real Infinity
agx::GenericJsonParameter::getParameterPath
agx::String getParameterPath() const
Text representation of where in the json structure this parameter lives.
Definition: GenericJsonParameter.h:426
agx::GenericJsonParameter::writeValueBool
void writeValueBool(agxJson::Value &node, bool value)
Writes a bool value to a json node.
Definition: GenericJsonParameter.h:352
agx::Vec3
Vec3T< Real > Vec3
The object holding 3 dimensional vectors and providing basic arithmetic.
Definition: Vec3.h:36
agx::GenericJsonParameter::parseBool
bool parseBool(const agxJson::Value &value) const
Read a bool from a json value.
Definition: GenericJsonParameter.h:143
Logger.h
agx::GenericJsonParameter::GenericJsonParameter
GenericJsonParameter(const agx::String &name, const agx::String &group, PropertySetBool setter, PropertyGetBool getter)
Definition: GenericJsonParameter.h:90
agx::GenericJsonParameter::isDefault
bool isDefault(const agxJson::Value &value) const
Is value marked as "default"?
Definition: GenericJsonParameter.h:113
agx::GenericJsonParameter::parseString
agx::String parseString(const agxJson::Value &value) const
Read a string from a json value.
Definition: GenericJsonParameter.h:161
agx::min
Vec3T< T > min(const Vec3T< T > &lhs, const Vec3T< T > &rhs)
Definition: Vec3Template.h:861
agx::GenericJsonParameter::GenericJsonParameter
GenericJsonParameter(const agx::String &name, const agx::String &group, PropertySetVec3 setter, PropertyGetVec3 getter)
Definition: GenericJsonParameter.h:105
agx::GenericJsonParameter::PropertyGetVec3
std::function< agx::Vec3(const T *) > PropertyGetVec3
Definition: GenericJsonParameter.h:38
agx::GenericJsonParameter::GenericJsonParameter
GenericJsonParameter(const agx::String &name, PropertySetReal setter, PropertyGetReal getter)
Definition: GenericJsonParameter.h:69
agx::GenericJsonParameter::parseReal
agx::Real parseReal(const agxJson::Value &value) const
Read a real number from a json value.
Definition: GenericJsonParameter.h:174
agx::GenericJsonParameter::GenericJsonParameter
GenericJsonParameter(const agx::String &name, PropertySetBool setter, PropertyGetBool getter)
Definition: GenericJsonParameter.h:64
agx::GenericJsonParameter::SCALAR_INT
@ SCALAR_INT
Definition: GenericJsonParameter.h:52
agx::GenericJsonParameter::writeValueVec3
void writeValueVec3(agxJson::Value &node, agx::Vec3 value)
Write a short array, at most 3 elements.
Definition: GenericJsonParameter.h:361
agx::GenericJsonParameter::GenericJsonParameter
GenericJsonParameter(const agx::String &name, const agx::String &group, PropertySetInt setter, PropertyGetInt getter)
Definition: GenericJsonParameter.h:85
agx::GenericJsonParameter::PropertySetBool
std::function< void(bool, T *) > PropertySetBool
Definition: GenericJsonParameter.h:41
agx::root
AGXCORE_EXPORT Component * root()
LOGGER_END
#define LOGGER_END()
Definition: Logger.h:26
agx::GenericJsonParameter::GenericJsonParameter
GenericJsonParameter(const agx::String &name, const agx::String &group, PropertySetString setter, PropertyGetString getter)
Definition: GenericJsonParameter.h:100
agx::GenericJsonParameter::STRING
@ STRING
Definition: GenericJsonParameter.h:55
agx::GenericJsonParameter::isResolved
bool isResolved() const
Status flag if parameter could be read/written.
Definition: GenericJsonParameter.h:418
agx::GenericJsonParameter::PropertyGetBool
std::function< bool(const T *) > PropertyGetBool
Definition: GenericJsonParameter.h:35
agx::GenericJsonParameter::parseInt
int parseInt(const agxJson::Value &value) const
Read an int from a json value.
Definition: GenericJsonParameter.h:125
agx::GenericJsonParameter::PropertyGetReal
std::function< agx::Real(const T *) > PropertyGetReal
Definition: GenericJsonParameter.h:37
LOGGER_ENDL
#define LOGGER_ENDL()
Definition: Logger.h:27
agx::GenericJsonParameter::PropertySetInt
std::function< void(int, T *) > PropertySetInt
Definition: GenericJsonParameter.h:40
String.h
agx::AgXString< std::string >
agx::GenericJsonParameter::PropertyGetString
std::function< agx::String(const T *) > PropertyGetString
Definition: GenericJsonParameter.h:36
agx::GenericJsonParameter::readProperty
bool readProperty(const agxJson::Value &root, T *material, bool optional=false)
Reads a generic property from a json structure.
Definition: GenericJsonParameter.h:246
agx::String
AgXString< std::string > String
Definition: String.h:427
agx::Vec3T< Real >
agx::AgXString::lower
AgXString lower() const
Definition: String.h:751
Json.h
agx::GenericJsonParameter::writeValueString
void writeValueString(agxJson::Value &node, const agx::String value)
Writes a string value to a json node.
Definition: GenericJsonParameter.h:335
agx::GenericJsonParameter::GenericJsonParameter
GenericJsonParameter(const agx::String &name, PropertySetInt setter, PropertyGetInt getter)
Definition: GenericJsonParameter.h:59
agx::GenericJsonParameter::writeValueReal
void writeValueReal(agxJson::Value &node, agx::Real value)
Writes a real value to a json node.
Definition: GenericJsonParameter.h:319