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