-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsave.cpp
More file actions
308 lines (277 loc) · 11.2 KB
/
save.cpp
File metadata and controls
308 lines (277 loc) · 11.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
#include "pstack/gui/save.hpp"
#include <jsoncons/json.hpp>
#include <jsoncons_ext/jsonschema/jsonschema.hpp>
#include <cstddef>
#include <expected>
#include <string_view>
#include <string>
#include <ranges>
#include <utility>
#include <vector>
namespace pstack::gui {
namespace {
struct save_part : calc::part_base {
bool in_parts_list;
};
struct save_sinterbox_parameters : calc::sinterbox_settings, calc::sinterbox_bounding {};
using save_result = calc::stack_result_base<std::size_t, save_sinterbox_parameters>;
using internal_save_state = basic_save_state<save_part, save_result>;
} // namespace
} // namespace pstack::gui
JSONCONS_N_MEMBER_TRAITS(pstack::gui::preferences, 0, // Nothing is required
invert_scroll, extra_parts, show_bounding_box, load_environment_popup
);
JSONCONS_N_MEMBER_TRAITS(pstack::calc::stack_settings, 0, // Nothing is required
resolution, x_min, x_max, y_min, y_max, z_min, z_max
);
JSONCONS_N_MEMBER_TRAITS(pstack::calc::sinterbox_settings, 0, // Nothing is required
clearance, thickness, width, spacing
);
JSONCONS_ALL_MEMBER_TRAITS(pstack::gui::save_part,
mesh_file, quantity, mirrored, min_hole, rotation_index, rotate_min_box, in_parts_list
);
JSONCONS_ALL_MEMBER_TRAITS(pstack::geo::point3<float>,
x, y, z
);
JSONCONS_ALL_MEMBER_TRAITS(pstack::geo::vector3<float>,
x, y, z
);
JSONCONS_ALL_MEMBER_TRAITS(pstack::geo::matrix3<float>,
xx, xy, xz, yx, yy, yz, zx, zy, zz
);
JSONCONS_ALL_MEMBER_TRAITS(pstack::gui::save_result::piece,
part, rotation, translation
);
JSONCONS_ALL_MEMBER_TRAITS(pstack::gui::save_sinterbox_parameters,
clearance, thickness, width, spacing, min, max
);
JSONCONS_N_MEMBER_TRAITS(pstack::gui::save_result, 1,
pieces, // mandatory
sinterbox // optional
);
JSONCONS_ALL_MEMBER_TRAITS(pstack::gui::internal_save_state,
preferences, stack, sinterbox, parts, results
);
namespace {
const std::string_view the_schema = R"the_schema({
"$schema": "https://json-schema.org/draft/2020-12/schema",
"description": "A representation of the state of the PartStacker project",
"type": "object",
"$defs": {
"unsigned_int": {
"type": "integer",
"minimum": 0
},
"point3": {
"type": "object",
"properties": {
"x": { "type": "number" },
"y": { "type": "number" },
"z": { "type": "number" }
},
"required": ["x", "y", "z"]
},
"matrix3": {
"type": "object",
"properties": {
"xx": { "type": "number" },
"xy": { "type": "number" },
"xz": { "type": "number" },
"yx": { "type": "number" },
"yy": { "type": "number" },
"yz": { "type": "number" },
"zx": { "type": "number" },
"zy": { "type": "number" },
"zz": { "type": "number" }
},
"required": ["xx", "xy", "xz", "yx", "yy", "yz", "zx", "zy", "zz"]
}
},
"properties": {
"preferences": {
"type": "object",
"properties": {
"invert_scroll": { "type": "boolean" },
"extra_parts": { "type": "boolean" },
"show_bounding_box": { "type": "boolean" },
"load_environment_popup": { "type": "boolean" }
}
},
"stack": {
"type": "object",
"properties": {
"resolution": { "type": "number" },
"x_min": { "$ref": "#/$defs/unsigned_int" },
"x_max": { "$ref": "#/$defs/unsigned_int" },
"y_min": { "$ref": "#/$defs/unsigned_int" },
"y_max": { "$ref": "#/$defs/unsigned_int" },
"z_min": { "$ref": "#/$defs/unsigned_int" },
"z_max": { "$ref": "#/$defs/unsigned_int" }
}
},
"sinterbox": {
"type": "object",
"properties": {
"clearance": { "type": "number" },
"thickness": { "type": "number" },
"width": { "type": "number" },
"spacing": { "type": "number" }
}
},
"parts": {
"type": "array",
"items": {
"type": "object",
"properties": {
"mesh_file": { "type": "string" },
"quantity": { "$ref": "#/$defs/unsigned_int" },
"mirrored": { "type": "boolean" },
"min_hole": { "$ref": "#/$defs/unsigned_int" },
"rotation_index": { "type": "integer", "minimum": 0, "maximum": 2 },
"rotate_min_box": { "type": "boolean" },
"in_parts_list": { "type": "boolean" }
},
"required": ["mesh_file", "quantity", "mirrored", "min_hole", "rotation_index", "rotate_min_box", "in_parts_list"]
}
},
"results": {
"type": "array",
"items": {
"type": "object",
"properties": {
"pieces": {
"type": "array",
"items": {
"type": "object",
"properties": {
"part": { "$ref": "#/$defs/unsigned_int" },
"rotation": { "$ref": "#/$defs/matrix3" },
"translation": { "$ref": "#/$defs/point3" }
},
"required": ["rotation", "translation", "part"]
}
},
"sinterbox": {
"type": "object",
"properties": {
"clearance": { "type": "number" },
"thickness": { "type": "number" },
"width": { "type": "number" },
"spacing": { "type": "number" },
"min": { "$ref": "#/$defs/point3" },
"max": { "$ref": "#/$defs/point3" }
},
"required": ["clearance", "thickness", "width", "spacing", "min", "max"]
}
},
"required": ["pieces"]
}
}
},
"required": ["preferences", "stack", "sinterbox", "parts", "results"]
})the_schema";
} // namespace
namespace pstack::gui {
namespace {
in_save_state from_internal(const internal_save_state& state) {
in_save_state s{};
s.preferences = state.preferences;
s.stack = state.stack;
s.sinterbox = state.sinterbox;
for (const save_part& part : state.parts) {
auto& list = part.in_parts_list ? s.parts : s.extra_parts;
calc::part p = calc::initialize_part(static_cast<const calc::part_base&>(part));
list.push_back(std::make_shared<calc::part>(std::move(p)));
}
for (const save_result& result : state.results) {
auto& r = s.results.emplace_back();
if (result.sinterbox.has_value()) {
r.sinterbox.emplace();
r.sinterbox->settings = static_cast<const calc::sinterbox_settings&>(*result.sinterbox);
r.sinterbox->bounding = static_cast<const calc::sinterbox_bounding&>(*result.sinterbox);
}
for (const auto& piece : result.pieces) {
auto& p = r.pieces.emplace_back();
p.rotation = piece.rotation;
p.translation = piece.translation;
if (piece.part < s.parts.size()) {
p.part = s.parts[piece.part];
} else {
p.part = s.extra_parts[piece.part - s.parts.size()];
}
}
}
return s;
};
internal_save_state to_internal(const out_save_state& state) {
std::vector all_parts = state.parts; // Copy
std::vector<save_result> results{};
for (const auto& in_result : state.results) {
auto& out_result = results.emplace_back();
if (in_result.sinterbox.has_value()) {
out_result.sinterbox.emplace();
static_cast<calc::sinterbox_settings&>(*out_result.sinterbox) = in_result.sinterbox->settings;
static_cast<calc::sinterbox_bounding&>(*out_result.sinterbox) = in_result.sinterbox->bounding;
}
for (const auto& in_piece : in_result.pieces) {
auto& out_piece = out_result.pieces.emplace_back();
out_piece.rotation = in_piece.rotation;
out_piece.translation = in_piece.translation;
auto it = std::ranges::find(all_parts, in_piece.part);
if (it != all_parts.end()) {
out_piece.part = std::ranges::distance(all_parts.begin(), it);
} else {
out_piece.part = all_parts.size();
all_parts.push_back(in_piece.part);
}
}
}
std::vector<save_part> parts{};
for (std::size_t i = 0; i != all_parts.size(); ++i) {
const calc::part_base& part = *(all_parts[i]);
parts.emplace_back(part);
parts.back().in_parts_list = (i < state.parts.size());
}
return internal_save_state{
.preferences = state.preferences,
.stack = state.stack,
.sinterbox = state.sinterbox,
.parts = std::move(parts),
.results = std::move(results),
};
};
} // namespace
std::expected<in_save_state, std::string> save_state_from_json(std::string_view str) {
try {
const jsoncons::json j = jsoncons::json::parse(str);
static const auto schema = []{
auto schema = jsoncons::json::parse(the_schema);
return jsoncons::jsonschema::make_json_schema(std::move(schema));
}();
std::vector<std::string> errors;
const auto schema_reporter = [&](const jsoncons::jsonschema::validation_message& message) {
errors.push_back(std::format("{}: {}", message.instance_location().string(), message.message()));
return jsoncons::jsonschema::walk_result::advance;
};
schema.validate(j, schema_reporter);
if (not errors.empty()) {
errors.insert(errors.begin(), "File does not conform to schema:");
auto view = errors | std::views::join_with(std::string_view("\n "));
std::string full_error{view.begin(), view.end()};
return std::unexpected(std::move(full_error));
}
return from_internal(j.as<internal_save_state>());
} catch (const jsoncons::ser_error& e) {
return std::unexpected(std::string("Failed to parse JSON with ser_error: ") + e.what());
} catch (const jsoncons::json_exception& e) {
return std::unexpected(std::string("Failed to parse JSON with json_exception: ") + e.what());
} catch (const std::exception& e) {
return std::unexpected(std::string("Failed to parse JSON with std::exception: ") + e.what());
} catch (...) {
return std::unexpected("Failed to parse JSON with unknown error");
}
}
std::string save_state_to_json(const out_save_state& state) {
return jsoncons::json(to_internal(state)).to_string();
}
} // namespace pstack::gui