INSTINCT Code Coverage Report


Directory: src/
File: util/Json.cpp
Date: 2025-02-07 16:54:41
Exec Total Coverage
Lines: 16 171 9.4%
Functions: 2 8 25.0%
Branches: 6 302 2.0%

Line Branch Exec Source
1 // This file is part of INSTINCT, the INS Toolkit for Integrated
2 // Navigation Concepts and Training by the Institute of Navigation of
3 // the University of Stuttgart, Germany.
4 //
5 // This Source Code Form is subject to the terms of the Mozilla Public
6 // License, v. 2.0. If a copy of the MPL was not distributed with this
7 // file, You can obtain one at https://mozilla.org/MPL/2.0/.
8
9 #include "Json.hpp"
10
11 #include <vector>
12
13 #include "implot_internal.h"
14
15 #include "util/Logger.hpp"
16
17 void to_json(json& j, const ImColor& color)
18 {
19 j = json{
20 { "r", static_cast<int>(color.Value.x * 255.0F) },
21 { "g", static_cast<int>(color.Value.y * 255.0F) },
22 { "b", static_cast<int>(color.Value.z * 255.0F) },
23 { "a", static_cast<int>(color.Value.w * 255.0F) },
24 };
25 }
26 void from_json(const json& j, ImColor& color)
27 {
28 int r = 0;
29 int g = 0;
30 int b = 0;
31 int a = 255;
32 if (j.contains("r"))
33 {
34 j.at("r").get_to(r);
35 }
36 if (j.contains("g"))
37 {
38 j.at("g").get_to(g);
39 }
40 if (j.contains("b"))
41 {
42 j.at("b").get_to(b);
43 }
44 if (j.contains("a"))
45 {
46 j.at("a").get_to(a);
47 }
48
49 color = ImColor(r, g, b, a);
50 }
51
52 void to_json(json& j, const ImVec2& vec2)
53 {
54 j = json{
55 { "x", vec2.x },
56 { "y", vec2.y },
57 };
58 }
59 853 void from_json(const json& j, ImVec2& vec2)
60 {
61
1/2
✓ Branch 1 taken 853 times.
✗ Branch 2 not taken.
853 if (j.contains("x"))
62 {
63 853 j.at("x").get_to(vec2.x);
64 }
65
1/2
✓ Branch 1 taken 853 times.
✗ Branch 2 not taken.
853 if (j.contains("y"))
66 {
67 853 j.at("y").get_to(vec2.y);
68 }
69 853 }
70
71 void to_json(json& j, const ImVec4& vec4)
72 {
73 j = json{
74 { "x", vec4.x },
75 { "y", vec4.y },
76 { "z", vec4.z },
77 { "w", vec4.w },
78 };
79 }
80 2020 void from_json(const json& j, ImVec4& vec4)
81 {
82
1/2
✓ Branch 1 taken 2020 times.
✗ Branch 2 not taken.
2020 if (j.contains("x"))
83 {
84 2020 j.at("x").get_to(vec4.x);
85 }
86
1/2
✓ Branch 1 taken 2020 times.
✗ Branch 2 not taken.
2020 if (j.contains("y"))
87 {
88 2020 j.at("y").get_to(vec4.y);
89 }
90
1/2
✓ Branch 1 taken 2020 times.
✗ Branch 2 not taken.
2020 if (j.contains("z"))
91 {
92 2020 j.at("z").get_to(vec4.z);
93 }
94
1/2
✓ Branch 1 taken 2020 times.
✗ Branch 2 not taken.
2020 if (j.contains("w"))
95 {
96 2020 j.at("w").get_to(vec4.w);
97 }
98 2020 }
99
100 void to_json(json& j, const ImPlotStyle& style)
101 {
102 j = json{
103 { "LineWeight", style.LineWeight },
104 { "MarkerSize", style.MarkerSize },
105 { "MarkerWeight", style.MarkerWeight },
106 { "FillAlpha", style.FillAlpha },
107 { "ErrorBarSize", style.ErrorBarSize },
108 { "ErrorBarWeight", style.ErrorBarWeight },
109 { "DigitalBitHeight", style.DigitalBitHeight },
110 { "DigitalBitGap", style.DigitalBitGap },
111 { "PlotBorderSize", style.PlotBorderSize },
112 { "MinorAlpha", style.MinorAlpha },
113 { "MajorTickLen", style.MajorTickLen },
114 { "MinorTickLen", style.MinorTickLen },
115 { "MajorTickSize", style.MajorTickSize },
116 { "MinorTickSize", style.MinorTickSize },
117 { "MajorGridSize", style.MajorGridSize },
118 { "MinorGridSize", style.MinorGridSize },
119 { "PlotDefaultSize", style.PlotDefaultSize },
120 { "PlotMinSize", style.PlotMinSize },
121 { "PlotPadding", style.PlotPadding },
122 { "LabelPadding", style.LabelPadding },
123 { "LegendPadding", style.LegendPadding },
124 { "LegendInnerPadding", style.LegendInnerPadding },
125 { "LegendSpacing", style.LegendSpacing },
126 { "MousePosPadding", style.MousePosPadding },
127 { "AnnotationPadding", style.AnnotationPadding },
128 { "FitPadding", style.FitPadding },
129 };
130
131 for (int i = 0; i < ImPlotCol_COUNT; i++)
132 {
133 if (ImPlot::IsColorAuto(i))
134 {
135 j["Colors"][ImPlot::GetStyleColorName(i)]["col"] = "Auto";
136 }
137 else
138 {
139 j["Colors"][ImPlot::GetStyleColorName(i)]["col"] = ImPlot::GetStyleColorVec4(i);
140 }
141 }
142
143 j["Colormap"]["active"] = style.Colormap;
144
145 for (int i = 16; i < ImPlot::GetCurrentContext()->ColormapData.Count; ++i)
146 {
147 j["Colormap"]["maps"][static_cast<size_t>(i - 16)]["name"] = ImPlot::GetColormapName(i);
148 j["Colormap"]["maps"][static_cast<size_t>(i - 16)]["qualitative"] = ImPlot::GetCurrentContext()->ColormapData.IsQual(i);
149
150 for (int c = 0; c < ImPlot::GetCurrentContext()->ColormapData.GetKeyCount(i); ++c)
151 {
152 j["Colormap"]["maps"][static_cast<size_t>(i - 16)]["keys"][static_cast<size_t>(c)] = ImGui::ColorConvertU32ToFloat4(ImPlot::GetCurrentContext()->ColormapData.GetKeyColor(i, c));
153 }
154 }
155 }
156 void from_json(const json& j, ImPlotStyle& style)
157 {
158 if (j.contains("LineWeight"))
159 {
160 j.at("LineWeight").get_to(style.LineWeight);
161 }
162 if (j.contains("LineWeight"))
163 {
164 j.at("LineWeight").get_to(style.LineWeight);
165 }
166 if (j.contains("MarkerSize"))
167 {
168 j.at("MarkerSize").get_to(style.MarkerSize);
169 }
170 if (j.contains("MarkerWeight"))
171 {
172 j.at("MarkerWeight").get_to(style.MarkerWeight);
173 }
174 if (j.contains("FillAlpha"))
175 {
176 j.at("FillAlpha").get_to(style.FillAlpha);
177 }
178 if (j.contains("ErrorBarSize"))
179 {
180 j.at("ErrorBarSize").get_to(style.ErrorBarSize);
181 }
182 if (j.contains("ErrorBarWeight"))
183 {
184 j.at("ErrorBarWeight").get_to(style.ErrorBarWeight);
185 }
186 if (j.contains("DigitalBitHeight"))
187 {
188 j.at("DigitalBitHeight").get_to(style.DigitalBitHeight);
189 }
190 if (j.contains("DigitalBitGap"))
191 {
192 j.at("DigitalBitGap").get_to(style.DigitalBitGap);
193 }
194 if (j.contains("PlotBorderSize"))
195 {
196 j.at("PlotBorderSize").get_to(style.PlotBorderSize);
197 }
198 if (j.contains("MinorAlpha"))
199 {
200 j.at("MinorAlpha").get_to(style.MinorAlpha);
201 }
202 if (j.contains("MajorTickLen"))
203 {
204 j.at("MajorTickLen").get_to(style.MajorTickLen);
205 }
206 if (j.contains("MinorTickLen"))
207 {
208 j.at("MinorTickLen").get_to(style.MinorTickLen);
209 }
210 if (j.contains("MajorTickSize"))
211 {
212 j.at("MajorTickSize").get_to(style.MajorTickSize);
213 }
214 if (j.contains("MinorTickSize"))
215 {
216 j.at("MinorTickSize").get_to(style.MinorTickSize);
217 }
218 if (j.contains("MajorGridSize"))
219 {
220 j.at("MajorGridSize").get_to(style.MajorGridSize);
221 }
222 if (j.contains("MinorGridSize"))
223 {
224 j.at("MinorGridSize").get_to(style.MinorGridSize);
225 }
226 if (j.contains("PlotDefaultSize"))
227 {
228 j.at("PlotDefaultSize").get_to(style.PlotDefaultSize);
229 }
230 if (j.contains("PlotMinSize"))
231 {
232 j.at("PlotMinSize").get_to(style.PlotMinSize);
233 }
234 if (j.contains("PlotPadding"))
235 {
236 j.at("PlotPadding").get_to(style.PlotPadding);
237 }
238 if (j.contains("LabelPadding"))
239 {
240 j.at("LabelPadding").get_to(style.LabelPadding);
241 }
242 if (j.contains("LegendPadding"))
243 {
244 j.at("LegendPadding").get_to(style.LegendPadding);
245 }
246 if (j.contains("LegendInnerPadding"))
247 {
248 j.at("LegendInnerPadding").get_to(style.LegendInnerPadding);
249 }
250 if (j.contains("LegendSpacing"))
251 {
252 j.at("LegendSpacing").get_to(style.LegendSpacing);
253 }
254 if (j.contains("MousePosPadding"))
255 {
256 j.at("MousePosPadding").get_to(style.MousePosPadding);
257 }
258 if (j.contains("AnnotationPadding"))
259 {
260 j.at("AnnotationPadding").get_to(style.AnnotationPadding);
261 }
262 if (j.contains("FitPadding"))
263 {
264 j.at("FitPadding").get_to(style.FitPadding);
265 }
266
267 if (j.contains("Colors"))
268 {
269 for (int i = 0; i < ImPlotCol_COUNT; i++)
270 {
271 if (j.at("Colors").contains(ImPlot::GetStyleColorName(i)))
272 {
273 if (j.at("Colors").at(ImPlot::GetStyleColorName(i)).contains("col"))
274 {
275 if (j.at("Colors").at(ImPlot::GetStyleColorName(i)).at("col").is_string()
276 && j.at("Colors").at(ImPlot::GetStyleColorName(i)).at("col").get<std::string>() == "Auto")
277 {
278 style.Colors[i] = IMPLOT_AUTO_COL; // NOLINT(cppcoreguidelines-pro-bounds-constant-array-index)
279 }
280 else
281 {
282 j.at("Colors").at(ImPlot::GetStyleColorName(i)).at("col").get_to(style.Colors[i]); // NOLINT(cppcoreguidelines-pro-bounds-constant-array-index)
283 }
284 }
285 }
286 else
287 {
288 LOG_WARN("Problem reading the ImPlot style: The color '{}' was not found in the json file.", ImPlot::GetStyleColorName(i));
289 }
290 }
291 }
292
293 if (j.contains("Colormap"))
294 {
295 if (j.at("Colormap").contains("maps"))
296 {
297 for (const auto& map : j.at("Colormap").at("maps"))
298 {
299 std::vector<ImVec4> custom;
300 for (const auto& key : map.at("keys"))
301 {
302 custom.push_back(key.get<ImVec4>());
303 }
304 auto name = map.at("name").get<std::string>();
305 auto qual = map.at("qualitative").get<bool>();
306
307 if (ImPlot::GetCurrentContext()->ColormapData.GetIndex(name.c_str()) == -1 && custom.size() > 1)
308 {
309 ImPlot::AddColormap(name.c_str(), custom.data(), static_cast<int>(custom.size()), qual);
310 }
311 }
312 }
313 if (j.at("Colormap").contains("active"))
314 {
315 j.at("Colormap").at("active").get_to(style.Colormap);
316 }
317 }
318 }
319