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