json_elements.cpp
Go to the documentation of this file.
1 #include "json_elements.h"
2 
3 #include <SimoxUtility/algorithm/string.h>
4 
6 #include <ArmarXCore/interface/core/BasicVectorTypes.h>
7 
8 #include "json_base.h"
9 
10 namespace armarx::viz
11 {
12 
13  const simox::meta::IntEnumNames data::ElementFlags::names = {
14  {NONE, "None"},
15  {OVERRIDE_MATERIAL, "Override_Material"},
16  };
17 
18 
19  const simox::meta::IntEnumNames data::ModelDrawStyle::names = {
20  {ORIGINAL, "Original"},
21  {COLLISION, "Collision"},
22  {OVERRIDE_COLOR, "Override_Color"},
23  };
24 
25  void
26  data::to_json(nlohmann::json& j, const ColoredPoint& coloredPoint)
27  {
28  j["x"] = coloredPoint.x;
29  j["y"] = coloredPoint.y;
30  j["z"] = coloredPoint.z;
31  j["color"] = coloredPoint.color;
32  j[json::meta::KEY]["color"] = json::meta::COLOR;
33  }
34 
35  void
36  data::from_json(const nlohmann::json& j, ColoredPoint& coloredPoint)
37  {
38  coloredPoint.x = j.at("x");
39  coloredPoint.y = j.at("y");
40  coloredPoint.z = j.at("z");
41  coloredPoint.color = j.at("color");
42  }
43 
44  void
45  data::to_json(nlohmann::json& j, const ElementArrow& arrow)
46  {
47  json::to_json_base(j, arrow);
48  j["length"] = arrow.length;
49  j["width"] = arrow.width;
50  }
51 
52  void
53  data::from_json(const nlohmann::json& j, ElementArrow& arrow)
54  {
55  json::from_json_base(j, arrow);
56  arrow.length = j.at("length");
57  arrow.width = j.at("width");
58  }
59 
60  void
61  data::to_json(nlohmann::json& j, const ElementArrowCircle& arrowCircle)
62  {
63  json::to_json_base(j, arrowCircle);
64  j["radius"] = arrowCircle.radius;
65  j["completion"] = arrowCircle.completion;
66  j["width"] = arrowCircle.width;
67  }
68 
69  void
70  data::from_json(const nlohmann::json& j, ElementArrowCircle& arrowCircle)
71  {
72  json::from_json_base(j, arrowCircle);
73  arrowCircle.radius = j.at("radius");
74  arrowCircle.completion = j.at("completion");
75  arrowCircle.width = j.at("width");
76  }
77 
78  void
79  data::to_json(nlohmann::json& j, const ElementBox& box)
80  {
81  json::to_json_base(j, box);
82  j["size"] = box.size;
83  }
84 
85  void
86  data::from_json(const nlohmann::json& j, ElementBox& box)
87  {
88  json::from_json_base(j, box);
89  box.size = j.at("size").get<armarx::Vector3f>();
90  }
91 
92  void
93  data::to_json(nlohmann::json& j, const ElementCylinder& cylinder)
94  {
95  json::to_json_base(j, cylinder);
96  j["height"] = cylinder.height;
97  j["radius"] = cylinder.radius;
98  }
99 
100  void
101  data::from_json(const nlohmann::json& j, ElementCylinder& cylinder)
102  {
103  json::from_json_base(j, cylinder);
104  cylinder.height = j.at("height");
105  cylinder.radius = j.at("radius");
106  }
107 
108  void
109  data::to_json(nlohmann::json& j, const ElementCylindroid& cylindroid)
110  {
111  json::to_json_base(j, cylindroid);
112  j["height"] = cylindroid.height;
113  j["axisLengths"] = cylindroid.axisLengths;
114  j["curvature"] = cylindroid.curvature;
115  }
116 
117  void
118  data::from_json(const nlohmann::json& j, ElementCylindroid& cylindroid)
119  {
120  json::from_json_base(j, cylindroid);
121  cylindroid.height = j.at("height");
122  cylindroid.axisLengths = j.at("axisLengths").get<armarx::Vector2f>();
123  cylindroid.curvature = j.at("curvature").get<armarx::Vector2f>();
124  }
125 
126  void
127  data::to_json(nlohmann::json& j, const ElementLine& line)
128  {
129  json::to_json_base(j, line);
130  j["from"] = line.from;
131  j["to"] = line.to;
132  j["lineWidth"] = line.lineWidth;
133  }
134 
135  void
136  data::from_json(const nlohmann::json& j, ElementLine& line)
137  {
138  json::from_json_base(j, line);
139  line.from = j.at("from").get<armarx::Vector3f>();
140  line.to = j.at("to").get<armarx::Vector3f>();
141  line.lineWidth = j.at("lineWidth").get<float>();
142  }
143 
144  void
145  data::to_json(nlohmann::json& j, const ElementMesh& mesh)
146  {
147  json::to_json_base(j, mesh);
148  j["# Vertices"] = mesh.vertices.size();
149  j["# Colors"] = mesh.colors.size();
150  j["# Faces"] = mesh.faces.size();
151 
152  j[json::meta::KEY]["# Vertices"] = json::meta::READ_ONLY;
153  j[json::meta::KEY]["# Colors"] = json::meta::READ_ONLY;
154  j[json::meta::KEY]["# Faces"] = json::meta::READ_ONLY;
155  }
156 
157  void
158  data::from_json(const nlohmann::json& j, ElementMesh& mesh)
159  {
160  json::from_json_base(j, mesh);
161  }
162 
163  void
164  data::to_json(nlohmann::json& j, const ElementPointCloud& pointCloud)
165  {
166  json::to_json_base(j, pointCloud);
167  j["transparency"] = pointCloud.transparency;
168  j["pointSizeInPixels"] = pointCloud.pointSizeInPixels;
169 
170  std::size_t numPoints = pointCloud.points.size() / sizeof(ColoredPoint);
171  j["# Points"] = numPoints;
172  j[json::meta::KEY]["# Points"] = json::meta::READ_ONLY;
173 
174  ColoredPoint const* begin = (ColoredPoint const*)pointCloud.points.data();
175  ColoredPoint const* end = begin + std::min(std::size_t(10), numPoints);
176  j["Points[0:10]"] = ColoredPointList(begin, end);
177  }
178 
179  void
180  data::from_json(const nlohmann::json& j, ElementPointCloud& pointCloud)
181  {
182  json::from_json_base(j, pointCloud);
183  pointCloud.transparency = j.at("transparency");
184  pointCloud.pointSizeInPixels = j.at("pointSizeInPixels");
185  }
186 
187  void
188  data::to_json(nlohmann::json& j, const ElementPolygon& polygon)
189  {
190  json::to_json_base(j, polygon);
191  j["lineColor"] = polygon.lineColor;
192  j[json::meta::KEY]["lineColor"] = json::meta::COLOR;
193  j["lineWidth"] = polygon.lineWidth;
194  j["points"] = polygon.points;
195  }
196 
197  void
198  data::from_json(const nlohmann::json& j, ElementPolygon& polygon)
199  {
200  json::from_json_base(j, polygon);
201  polygon.lineColor = j.at("lineColor");
202  polygon.lineWidth = j.at("lineWidth");
203  polygon.points = j.at("points").get<std::vector<armarx::Vector3f>>();
204  }
205 
206  void
207  data::to_json(nlohmann::json& j, const ElementPose& pose)
208  {
209  json::to_json_base(j, pose);
210  }
211 
212  void
213  data::from_json(const nlohmann::json& j, ElementPose& pose)
214  {
215  json::from_json_base(j, pose);
216  }
217 
218  void
219  data::to_json(nlohmann::json& j, const ElementPath& path)
220  {
221  json::to_json_base(j, path);
222  j["points"] = path.points;
223  }
224 
225  void
226  data::from_json(const nlohmann::json& j, ElementPath& path)
227  {
228  json::from_json_base(j, path);
229  path.points = j.at("points").get<armarx::Vector3fSeq>();
230  }
231 
232  void
233  data::to_json(nlohmann::json& j, const ElementSphere& sphere)
234  {
235  json::to_json_base(j, sphere);
236  j["radius"] = sphere.radius;
237  }
238 
239  void
240  data::from_json(const nlohmann::json& j, ElementSphere& sphere)
241  {
242  json::from_json_base(j, sphere);
243  sphere.radius = j.at("radius");
244  }
245 
246  void
247  data::to_json(nlohmann::json& j, const ElementEllipsoid& ellipsoid)
248  {
249  json::to_json_base(j, ellipsoid);
250  j["axisLengths"] = ellipsoid.axisLengths;
251  j["curvature"] = ellipsoid.curvature;
252  }
253 
254  void
255  data::from_json(const nlohmann::json& j, ElementEllipsoid& ellipsoid)
256  {
257  json::from_json_base(j, ellipsoid);
258  ellipsoid.axisLengths = j.at("axisLengths");
259  ellipsoid.curvature = j.at("curvature");
260  }
261 
262  void
263  data::to_json(nlohmann::json& j, const ElementText& text)
264  {
265  json::to_json_base(j, text);
266  j["text"] = text.text;
267  }
268 
269  void
270  data::from_json(const nlohmann::json& j, ElementText& text)
271  {
272  json::from_json_base(j, text);
273  text.text = j.at("text");
274  }
275 
276  namespace data::ModelDrawStyle
277  {
278  std::string
279  to_flag_names(const int drawStyle)
280  {
281  std::vector<std::string> flag_names;
282  for (int flag : names.values())
283  {
284  if (drawStyle == flag // Necessary if flag is 0
285  || drawStyle & flag)
286  {
287  flag_names.push_back(names.to_name(flag));
288  }
289  }
290  return simox::alg::join(flag_names, " | ");
291  }
292 
293  int
294  from_flag_names(const std::string& flagString)
295  {
296  bool trim_elements = true;
297  std::vector<std::string> split = simox::alg::split(flagString, "|", trim_elements);
298  int flag = 0;
299  for (auto& s : split)
300  {
301  flag |= names.from_name(s);
302  }
303  return flag;
304  }
305  } // namespace data::ModelDrawStyle
306 
307  void
308  data::to_json(nlohmann::json& j, const ElementObject& object)
309  {
310  json::to_json_base(j, object);
311  j["project"] = object.project;
312  j["filename"] = object.filename;
313 
314  j["drawStyle"] = ModelDrawStyle::to_flag_names(object.drawStyle) + " (" +
315  std::to_string(object.drawStyle) + ")";
316  j[json::meta::KEY]["drawStyle"] = json::meta::READ_ONLY;
317  }
318 
319  void
320  data::from_json(const nlohmann::json& j, ElementObject& object)
321  {
322  json::from_json_base(j, object);
323  object.project = j.at("project");
324  object.filename = j.at("filename");
325  object.drawStyle = ModelDrawStyle::from_flag_names(j.at("drawStyle"));
326  }
327 
328  void
329  data::to_json(nlohmann::json& j, const ElementRobot& robot)
330  {
331  json::to_json_base(j, robot);
332  j["project"] = robot.project;
333  j["filename"] = robot.filename;
334  j["jointValues"] = robot.jointValues;
335 
336  j["drawStyle"] = ModelDrawStyle::to_flag_names(robot.drawStyle);
337  j[json::meta::KEY]["drawStyle"] = json::meta::READ_ONLY;
338  }
339 
340  void
341  data::from_json(const nlohmann::json& j, ElementRobot& robot)
342  {
343  json::from_json_base(j, robot);
344  robot.project = j.at("project");
345  robot.filename = j.at("filename");
346  robot.jointValues = j.at("jointValues").get<armarx::StringFloatDictionary>();
347  robot.drawStyle = ModelDrawStyle::from_flag_names(j.at("drawStyle"));
348  }
349 
350 } // namespace armarx::viz
armarx::viz::json::to_json_base
void to_json_base(nlohmann::json &j, const data::Element &element)
Definition: json_base.cpp:94
armarx::viz::json::meta::KEY
const std::string KEY
Definition: json_base.cpp:85
armarx::viz::json::meta::COLOR
const std::string COLOR
Use a color picker.
Definition: json_base.cpp:90
armarx::viz::data::from_json
void from_json(nlohmann::json const &j, RecordingBatchHeader &batch)
Definition: ArVizStorage.cpp:432
json_elements.h
armarx::viz::data::ModelDrawStyle::names
const simox::meta::IntEnumNames names
Definition: json_elements.cpp:19
armarx::viz::json::meta::READ_ONLY
const std::string READ_ONLY
Make read-only.
Definition: json_base.cpp:88
json_base.h
armarx::viz::data::ModelDrawStyle::to_flag_names
std::string to_flag_names(const int drawStyle)
Definition: json_elements.cpp:279
armarx::to_string
const std::string & to_string(const std::string &s)
Definition: StringHelpers.h:41
armarx::viz::data::ModelDrawStyle::from_flag_names
int from_flag_names(const std::string &flagString)
Definition: json_elements.cpp:294
ExpressionException.h
armarx::viz::data::ElementFlags::names
const simox::meta::IntEnumNames names
Definition: json_elements.cpp:13
armarx::aron::similarity::FloatSimilarity::NONE
@ NONE
Definition: FloatSimilarity.h:14
min
T min(T t1, T t2)
Definition: gdiam.h:44
armarx::ctrlutil::s
double s(double t, double s0, double v0, double a0, double j)
Definition: CtrlUtil.h:33
armarx::viz
This file is part of ArmarX.
Definition: ArVizStorage.cpp:418
armarx::viz::json::from_json_base
void from_json_base(const nlohmann::json &j, data::Element &value)
Definition: json_base.cpp:108
armarx::split
std::vector< std::string > split(const std::string &source, const std::string &splitBy, bool trimElements=false, bool removeEmptyElements=false)
Definition: StringHelpers.cpp:38
armarx::viz::data::to_json
void to_json(nlohmann::json &j, RecordingBatchHeader const &batch)
Definition: ArVizStorage.cpp:422