1+ #include " JsonSchemaGenerator.h"
2+ #include " Utilities/StringTools.h"
3+ #include " Simulator/SimulatorBase.h"
4+ #include " SPlisHSPlasH/Simulation.h"
5+ #include " ParameterObjectParser.h"
6+
7+ using namespace GenParam ;
8+ using namespace SPH ;
9+ using namespace Eigen ;
10+ using namespace std ;
11+ using namespace Utilities ;
12+
13+
14+ void JsonSchemaGenerator::jsonBoolParam (BoolParameter* param)
15+ {
16+ (*m_currentData)[param->getName ()][" description" ] = param->getDescription () + " (Default: " + std::to_string (param->getValue ()) + " , Type: " + typeid (param->getValue ()).name () + " )" ;
17+ (*m_currentData)[param->getName ()][" type" ] = " boolean" ;
18+ }
19+
20+ void JsonSchemaGenerator::jsonStringParam (StringParameter* param)
21+ {
22+ (*m_currentData)[param->getName ()][" description" ] = param->getDescription () + " (Default: " + param->getValue () + " , Type: string)" ;
23+ (*m_currentData)[param->getName ()][" type" ] = " string" ;
24+ }
25+
26+ void JsonSchemaGenerator::jsonEnumParam (EnumParameter* param)
27+ {
28+ auto & vals = param->getEnumValues ();
29+ std::string str = param->getDescription () + " (Default: " + std::to_string (param->getValue ()) + " , Type: enum)" ;
30+ str = str + " , Options:" ;
31+ for (size_t i = 0 ; i < vals.size (); i++)
32+ str = str + " (" + std::to_string (vals[i].id ) + " , " + vals[i].name + " )" ;
33+
34+ (*m_currentData)[param->getName ()][" description" ] = str;
35+ (*m_currentData)[param->getName ()][" type" ] = " integer" ;
36+ }
37+
38+ void JsonSchemaGenerator::jsonVecUintParam (VectorParameter<unsigned int >* param)
39+ {
40+ if (param->getDim () == 3u )
41+ {
42+ Eigen::Matrix<unsigned int , 3 , 1 , Eigen::DontAlign> vec (param->getValue ());
43+ (*m_currentData)[param->getName ()][" description" ] = param->getDescription () + " (Default: (" + std::to_string (vec[0 ]) + " ," + std::to_string (vec[1 ]) + " ," + std::to_string (vec[2 ]) + " ), Type: vec3uint)" ;
44+ (*m_currentData)[param->getName ()][" $ref" ] = " #/definitions/vec3int" ;
45+ }
46+ }
47+
48+ void JsonSchemaGenerator::jsonVecParam (RealVectorParameter* param)
49+ {
50+ if (param->getDim () == 3u )
51+ {
52+ Vector3r vec (param->getValue ());
53+ (*m_currentData)[param->getName ()][" description" ] = param->getDescription () + " (Default: (" + std::to_string (vec[0 ]) + " ," + std::to_string (vec[1 ]) + " ," + std::to_string (vec[2 ]) + " ), Type: vec3real)" ;
54+ (*m_currentData)[param->getName ()][" $ref" ] = " #/definitions/vec3real" ;
55+ }
56+ else if (param->getDim () == 4u )
57+ {
58+ Vector4r vec (param->getValue ());
59+ (*m_currentData)[param->getName ()][" description" ] = param->getDescription () + " (Default: (" + StringTools::real2String (vec[0 ]) + " ," + StringTools::real2String (vec[1 ]) + " ," + StringTools::real2String (vec[2 ]) + " ," + StringTools::real2String (vec[3 ]) + " ), Type: vec4float)" ;
60+ (*m_currentData)[param->getName ()][" $ref" ] = " #/definitions/vec4real" ;
61+ }
62+ }
63+
64+ void JsonSchemaGenerator::jsonParameterObject (const ParameterObject* obj)
65+ {
66+ if ((dynamic_cast <const SimulatorBase*>(obj) != nullptr ) ||
67+ (dynamic_cast <const Simulation*>(obj) != nullptr ))
68+ m_currentData = &m_jsonData[" properties" ][" Configuration" ][" properties" ];
69+ else if ((dynamic_cast <const NonPressureForceBase*>(obj) != nullptr ) ||
70+ (dynamic_cast <const MaterialParameterObject*>(obj) != nullptr ) ||
71+ (dynamic_cast <const FluidModel*>(obj) != nullptr ))
72+ {
73+ m_jsonData[" definitions" ][" Material" ][" type" ] = " object" ;
74+ m_currentData = &m_jsonData[" definitions" ][" Material" ][" properties" ];
75+ }
76+ else if (dynamic_cast <const FluidBlockParameterObject*>(obj) != nullptr )
77+ {
78+ m_jsonData[" definitions" ][" FluidBlock" ][" type" ] = " object" ;
79+ m_currentData = &m_jsonData[" definitions" ][" FluidBlock" ][" properties" ];
80+ }
81+ else if (dynamic_cast <const FluidModelParameterObject*>(obj) != nullptr )
82+ {
83+ m_jsonData[" definitions" ][" FluidModel" ][" type" ] = " object" ;
84+ m_currentData = &m_jsonData[" definitions" ][" FluidModel" ][" properties" ];
85+ }
86+ else if (dynamic_cast <const EmitterParameterObject*>(obj) != nullptr )
87+ {
88+ m_jsonData[" definitions" ][" Emitter" ][" type" ] = " object" ;
89+ m_currentData = &m_jsonData[" definitions" ][" Emitter" ][" properties" ];
90+ }
91+ else if (dynamic_cast <const BoundaryParameterObject*>(obj) != nullptr )
92+ {
93+ m_jsonData[" definitions" ][" RigidBody" ][" type" ] = " object" ;
94+ m_currentData = &m_jsonData[" definitions" ][" RigidBody" ][" properties" ];
95+ }
96+ else if (dynamic_cast <const AnimationFieldParameterObject*>(obj) != nullptr )
97+ {
98+ m_jsonData[" definitions" ][" AnimationField" ][" type" ] = " object" ;
99+ m_currentData = &m_jsonData[" definitions" ][" AnimationField" ][" properties" ];
100+ }
101+ }
102+
103+ void JsonSchemaGenerator::generateSchemaFile (const std::string& fileName)
104+ {
105+ std::cout << " Write schema file: " << fileName << " \n " ;
106+
107+ // ////////////////////////////////////////////////////////////////////////
108+ // update configuration
109+ // ////////////////////////////////////////////////////////////////////////
110+ m_jsonData[" $schema" ] = " http://json-schema.org/schema#" ;
111+ m_jsonData[" title" ] = " SPlisHSPlasH File Format" ;
112+ m_jsonData[" description" ] = " A json schema for SPlisHSPlasH's scene files" ;
113+
114+ m_jsonData[" type" ] = " object" ;
115+
116+ nlohmann::json& config = m_jsonData[" properties" ][" Configuration" ];
117+ config[" type" ] = " object" ;
118+ config[" description" ] = " Contains the general settings of the simulation and the pressure solver" ;
119+
120+ // Basic type definitions
121+ nlohmann::json& defs = m_jsonData[" definitions" ];
122+ nlohmann::json& def_vec3real = defs[" vec3real" ];
123+ def_vec3real[" type" ] = " array" ;
124+ def_vec3real[" minItems" ] = 3 ;
125+ def_vec3real[" maxItems" ] = 3 ;
126+ nlohmann::json& def_vec3real_items = def_vec3real[" items" ];
127+ def_vec3real_items[" type" ] = " number" ;
128+
129+ nlohmann::json& def_vec3int = defs[" vec3int" ];
130+ def_vec3int[" type" ] = " array" ;
131+ def_vec3int[" minItems" ] = 3 ;
132+ def_vec3int[" maxItems" ] = 3 ;
133+ nlohmann::json& def_vec3uint_items = def_vec3int[" items" ];
134+ def_vec3uint_items[" type" ] = " integer" ;
135+
136+ nlohmann::json& def_vec4real = defs[" vec4real" ];
137+ def_vec4real[" type" ] = " array" ;
138+ def_vec4real[" minItems" ] = 4 ;
139+ def_vec4real[" maxItems" ] = 4 ;
140+ nlohmann::json& def_vec4real_items = def_vec4real[" items" ];
141+ def_vec4real_items[" type" ] = " number" ;
142+ def_vec4real_items[" minItems" ] = 0 ;
143+ def_vec4real_items[" maxItems" ] = 1 ;
144+
145+
146+ ParameterObjectParser parser;
147+ parser.addRealParamCB (std::bind (&JsonSchemaGenerator::jsonNumericParam<NumericParameter<Real>>, this , std::placeholders::_1));
148+ parser.addUInt32ParamCB (std::bind (&JsonSchemaGenerator::jsonNumericParam<NumericParameter<unsigned int >>, this , std::placeholders::_1));
149+ parser.addUInt16ParamCB (std::bind (&JsonSchemaGenerator::jsonNumericParam<NumericParameter<unsigned short >>, this , std::placeholders::_1));
150+ parser.addUInt8ParamCB (std::bind (&JsonSchemaGenerator::jsonNumericParam<NumericParameter<unsigned char >>, this , std::placeholders::_1));
151+ parser.addInt32ParamCB (std::bind (&JsonSchemaGenerator::jsonNumericParam<NumericParameter<int >>, this , std::placeholders::_1));
152+ parser.addInt16ParamCB (std::bind (&JsonSchemaGenerator::jsonNumericParam<NumericParameter<short >>, this , std::placeholders::_1));
153+ parser.addInt8ParamCB (std::bind (&JsonSchemaGenerator::jsonNumericParam<NumericParameter<char >>, this , std::placeholders::_1));
154+ parser.addBoolParamCB (std::bind (&JsonSchemaGenerator::jsonBoolParam, this , std::placeholders::_1));
155+ parser.addEnumParamCB (std::bind (&JsonSchemaGenerator::jsonEnumParam, this , std::placeholders::_1));
156+ parser.addStringParamCB (std::bind (&JsonSchemaGenerator::jsonStringParam, this , std::placeholders::_1));
157+ parser.addVecRealParamCB (std::bind (&JsonSchemaGenerator::jsonVecParam, this , std::placeholders::_1));
158+ parser.addVecUintParamCB (std::bind (&JsonSchemaGenerator::jsonVecUintParam, this , std::placeholders::_1));
159+ parser.addParameterObjectCB (std::bind (&JsonSchemaGenerator::jsonParameterObject, this , std::placeholders::_1));
160+
161+ parser.parseParameters ();
162+
163+ // fluid blocks properties
164+ nlohmann::json& props = m_jsonData[" properties" ];
165+ nlohmann::json& blocks = props[" FluidBlocks" ];
166+ blocks[" type" ] = " array" ;
167+ blocks[" description" ] = " Definition of axis-aligned blocks of fluid particles." ;
168+ nlohmann::json& blocks_items = blocks[" items" ];
169+ blocks_items[" $ref" ] = " #/definitions/FluidBlock" ;
170+
171+ // fluid models properties
172+ nlohmann::json& fmodels = props[" FluidModels" ];
173+ fmodels[" type" ] = " array" ;
174+ fmodels[" description" ] = " Definition of fluid models using a sampled geometry." ;
175+ nlohmann::json& fmodels_items = fmodels[" items" ];
176+ fmodels_items[" $ref" ] = " #/definitions/FluidModel" ;
177+
178+ // emitters properties
179+ nlohmann::json& emitters = props[" Emitters" ];
180+ emitters[" type" ] = " array" ;
181+ emitters[" description" ] = " Definition of fluid emitters." ;
182+ nlohmann::json& emitters_items = emitters[" items" ];
183+ emitters_items[" $ref" ] = " #/definitions/Emitter" ;
184+
185+ // rigid bodies properties
186+ nlohmann::json& rbs = props[" RigidBodies" ];
187+ rbs[" type" ] = " array" ;
188+ rbs[" description" ] = " Definition of static and dynamic rigid bodies. The latter are handled by PositionBasedDynamic, thus you may want to take a look at its json format as well." ;
189+ nlohmann::json& rbs_items = rbs[" items" ];
190+ rbs_items[" $ref" ] = " #/definitions/RigidBody" ;
191+
192+ // material properties
193+ nlohmann::json& mat = props[" Materials" ];
194+ mat[" type" ] = " array" ;
195+ mat[" description" ] = " Describes the material properties of the associated fluid models." ;
196+ nlohmann::json& mat_items = mat[" items" ];
197+ mat_items[" $ref" ] = " #/definitions/Material" ;
198+
199+ // animation fields properties
200+ nlohmann::json& afs = props[" AnimationFields" ];
201+ afs[" type" ] = " array" ;
202+ afs[" description" ] = " Definition of animation fields with the following terms: typical math terms: cos, sin; current time: t; Positions: x, y, z; Velocities: vx, vy,vz; Field quantities: valuex, valuey, valuez." ;
203+ nlohmann::json& afs_items = afs[" items" ];
204+ afs_items[" $ref" ] = " #/definitions/AnimationField" ;
205+
206+ std::ofstream output_file (fileName);
207+ if (!output_file.is_open ())
208+ {
209+ LOG_ERR << " Cannot open file!" ;
210+ return ;
211+ }
212+ try
213+ {
214+ output_file << std::setw (4 ) << m_jsonData << std::endl;
215+ }
216+ catch (const std::exception& e)
217+ {
218+ LOG_ERR << e.what ();
219+ exit (1 );
220+ }
221+ }
0 commit comments