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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
|
#include "StarJsonExtra.hpp"
#include "StarLogging.hpp"
#include "StarRandom.hpp"
namespace Star {
size_t jsonToSize(Json const& v) {
if (v.isNull())
return NPos;
if (!v.canConvert(Json::Type::Int))
throw JsonException("Json not an int in jsonToSize");
return v.toUInt();
}
Json jsonFromSize(size_t s) {
if (s == NPos)
return Json();
return Json(s);
}
Vec2D jsonToVec2D(Json const& v) {
if (v.type() != Json::Type::Array || v.size() != 2)
throw JsonException("Json not an array of size 2 in jsonToVec2D");
return Vec2D(v.getDouble(0), v.getDouble(1));
}
Vec2F jsonToVec2F(Json const& v) {
if (v.type() != Json::Type::Array || v.size() != 2)
throw JsonException("Json not an array of size 2 in jsonToVec2F");
return Vec2F(v.getFloat(0), v.getFloat(1));
}
Json jsonFromVec2F(Vec2F const& v) {
return JsonArray{v[0], v[1]};
}
Vec2I jsonToVec2I(Json const& v) {
if (v.type() != Json::Type::Array || v.size() != 2)
throw JsonException("Json not an array of size 2 in jsonToVec2I");
return Vec2I(v.getInt(0), v.getInt(1));
}
Json jsonFromVec2I(Vec2I const& v) {
return JsonArray{v[0], v[1]};
}
Vec2U jsonToVec2U(Json const& v) {
if (v.type() != Json::Type::Array || v.size() != 2)
throw JsonException("Json not an array of size 2 in jsonToVec2I");
return Vec2U(v.getInt(0), v.getInt(1));
}
Json jsonFromVec2U(Vec2U const& v) {
return JsonArray{v[0], v[1]};
}
Vec2B jsonToVec2B(Json const& v) {
if (v.type() != Json::Type::Array || v.size() != 2)
throw JsonException("Json not an array of size 2 in jsonToVec2B");
return Vec2B(v.getInt(0), v.getInt(1));
}
Json jsonFromVec2B(Vec2B const& v) {
return JsonArray{v[0], v[1]};
}
Vec3D jsonToVec3D(Json const& v) {
if (v.type() != Json::Type::Array || v.size() != 3)
throw JsonException("Json not an array of size size 3 in jsonToVec3D");
return Vec3D(v.getDouble(0), v.getDouble(1), v.getDouble(2));
}
Vec3F jsonToVec3F(Json const& v) {
if (v.type() != Json::Type::Array || v.size() != 3)
throw JsonException("Json not an array of size 3 in jsonToVec3D");
return Vec3F(v.getFloat(0), v.getFloat(1), v.getFloat(2));
}
Json jsonFromVec3F(Vec3F const& v) {
return JsonArray{v[0], v[1], v[2]};
}
Vec3I jsonToVec3I(Json const& v) {
if (v.type() != Json::Type::Array || v.size() != 3)
throw JsonException("Json not an array of size 3 in jsonToVec3I");
return Vec3I(v.getInt(0), v.getInt(1), v.getInt(2));
}
Json jsonFromVec3I(Vec3I const& v) {
JsonArray result;
result.append(v[0]);
result.append(v[1]);
result.append(v[2]);
return result;
}
Vec3B jsonToVec3B(Json const& v) {
if (v.type() != Json::Type::Array || v.size() != 3)
throw JsonException("Json not an array of size 3 in jsonToVec3B");
return Vec3B(v.getInt(0), v.getInt(1), v.getInt(2));
}
Vec4B jsonToVec4B(Json const& v) {
if (v.type() != Json::Type::Array || v.size() != 4)
throw JsonException("Json not an array of size 4 in jsonToVec4B");
return Vec4B(v.getInt(0), v.getInt(1), v.getInt(2), v.getInt(3));
}
Vec4I jsonToVec4I(Json const& v) {
if (v.type() != Json::Type::Array || v.size() != 4)
throw JsonException("Json not an array of size 4 in jsonToVec4B");
return Vec4I(v.getInt(0), v.getInt(1), v.getInt(2), v.getInt(3));
}
Vec4F jsonToVec4F(Json const& v) {
if (v.type() != Json::Type::Array || v.size() != 4)
throw JsonException("Json not an array of size 4 in jsonToVec4B");
return Vec4F(v.getFloat(0), v.getFloat(1), v.getFloat(2), v.getFloat(3));
}
RectD jsonToRectD(Json const& v) {
if (v.type() != Json::Type::Array)
throw JsonException("Json not an array in jsonToRectD");
if (v.size() != 4 && v.size() != 2)
throw JsonException("Json not an array of proper size in jsonToRectD");
if (v.size() == 4)
return RectD(v.getDouble(0), v.getDouble(1), v.getDouble(2), v.getDouble(3));
try {
auto lowerLeft = jsonToVec2D(v.get(0));
auto upperRight = jsonToVec2D(v.get(1));
return RectD(lowerLeft, upperRight);
} catch (JsonException const& e) {
throw JsonException(strf("Inner position not well formed in jsonToRectD: {}", outputException(e, true)));
}
}
Json jsonFromRectD(RectD const& rect) {
return JsonArray{rect.xMin(), rect.yMin(), rect.xMax(), rect.yMax()};
}
RectF jsonToRectF(Json const& v) {
return RectF(jsonToRectD(v));
}
Json jsonFromRectF(RectF const& rect) {
return JsonArray{rect.xMin(), rect.yMin(), rect.xMax(), rect.yMax()};
}
RectI jsonToRectI(Json const& v) {
if (v.type() != Json::Type::Array)
throw JsonException("Json not an array in jsonToRectI");
if (v.size() != 4 && v.size() != 2)
throw JsonException("Json not an array of proper size in jsonToRectI");
if (v.size() == 4)
return RectI(v.getInt(0), v.getInt(1), v.getInt(2), v.getInt(3));
try {
auto lowerLeft = jsonToVec2I(v.get(0));
auto upperRight = jsonToVec2I(v.get(1));
return RectI(lowerLeft, upperRight);
} catch (JsonException const& e) {
throw JsonException(strf("Inner position not well formed in jsonToRectI: {}", outputException(e, true)));
}
}
Json jsonFromRectI(RectI const& rect) {
return JsonArray{rect.xMin(), rect.yMin(), rect.xMax(), rect.yMax()};
}
RectU jsonToRectU(Json const& v) {
if (v.type() != Json::Type::Array)
throw JsonException("Json not an array in jsonToRectU");
if (v.size() != 4 && v.size() != 2)
throw JsonException("Json not an array of proper size in jsonToRectU");
if (v.size() == 4)
return RectU(v.getInt(0), v.getUInt(1), v.getUInt(2), v.getUInt(3));
try {
auto lowerLeft = jsonToVec2U(v.get(0));
auto upperRight = jsonToVec2U(v.get(1));
return RectU(lowerLeft, upperRight);
} catch (JsonException const& e) {
throw JsonException(strf("Inner position not well formed in jsonToRectU: {}", outputException(e, true)));
}
}
Json jsonFromRectU(RectU const& rect) {
return JsonArray{rect.xMin(), rect.yMin(), rect.xMax(), rect.yMax()};
}
Color jsonToColor(Json const& v) {
if (v.type() == Json::Type::Array) {
if (v.type() != Json::Type::Array || (v.size() != 3 && v.size() != 4))
throw JsonException("Json not an array of size 3 or 4 in jsonToColor");
Color c = Color::rgba(0, 0, 0, 255);
c.setRed(v.getInt(0));
c.setGreen(v.getInt(1));
c.setBlue(v.getInt(2));
if (v.size() == 4)
c.setAlpha(v.getInt(3));
return c;
} else if (v.type() == Json::Type::String) {
return Color(v.toString());
} else {
throw JsonException(strf("Json of type {} cannot be converted to color", v.typeName()));
}
}
Json jsonFromColor(Color const& color) {
JsonArray result;
result.push_back(color.red());
result.push_back(color.green());
result.push_back(color.blue());
if (color.alpha() < 255)
result.push_back(color.alpha());
return result;
}
PolyD jsonToPolyD(Json const& v) {
PolyD poly;
for (Json const& vertex : v.iterateArray())
poly.add(jsonToVec2D(vertex));
return fixInsideOutPoly(poly);
}
PolyF jsonToPolyF(Json const& v) {
PolyF poly;
for (Json const& vertex : v.iterateArray())
poly.add(jsonToVec2F(vertex));
return fixInsideOutPoly(poly);
}
PolyI jsonToPolyI(Json const& v) {
PolyI poly;
for (Json const& vertex : v.iterateArray())
poly.add(jsonToVec2I(vertex));
return fixInsideOutPoly(poly);
}
Json jsonFromPolyF(PolyF const& poly) {
JsonArray vertexList;
for (auto const& vertex : poly.vertexes())
vertexList.append(JsonArray{vertex[0], vertex[1]});
return vertexList;
}
Line2F jsonToLine2F(Json const& v) {
return Line2F(jsonToVec2F(v.get(0)), jsonToVec2F(v.get(1)));
}
Json jsonFromLine2F(Line2F const& line) {
return JsonArray{jsonFromVec2F(line.min()), jsonFromVec2F(line.max())};
}
Mat3F jsonToMat3F(Json const& v) {
return Mat3F(jsonToVec3F(v.get(0)), jsonToVec3F(v.get(1)), jsonToVec3F(v.get(2)));
}
Json jsonFromMat3F(Mat3F const& v) {
return JsonArray{jsonFromVec3F(v[0]), jsonFromVec3F(v[1]), jsonFromVec3F(v[2])};
}
StringList jsonToStringList(Json const& v) {
StringList result;
for (auto const& entry : v.iterateArray())
result.push_back(entry.toString());
return result;
}
Json jsonFromStringList(List<String> const& v) {
JsonArray result;
for (auto& e : v)
result.push_back(e);
return result;
}
List<float> jsonToFloatList(Json const& v) {
List<float> result;
for (auto const& entry : v.iterateArray())
result.push_back(entry.toFloat());
return result;
}
StringSet jsonToStringSet(Json const& v) {
StringSet result;
for (auto const& entry : v.iterateArray())
result.add(entry.toString());
return result;
}
Json jsonFromStringSet(StringSet const& v) {
JsonArray result;
for (auto& e : v)
result.push_back(e);
return result;
}
List<int> jsonToIntList(Json const& v) {
List<int> result;
for (auto const& entry : v.iterateArray())
result.push_back(entry.toInt());
return result;
}
List<Vec2I> jsonToVec2IList(Json const& v) {
List<Vec2I> result;
for (auto const& entry : v.iterateArray())
result.append(jsonToVec2I(entry));
return result;
}
List<Vec2U> jsonToVec2UList(Json const& v) {
List<Vec2U> result;
for (auto const& entry : v.iterateArray())
result.append(jsonToVec2U(entry));
return result;
}
List<Vec2F> jsonToVec2FList(Json const& v) {
List<Vec2F> result;
for (auto const& entry : v.iterateArray())
result.append(jsonToVec2F(entry));
return result;
}
List<Vec4B> jsonToVec4BList(Json const& v) {
List<Vec4B> result;
for (auto const& entry : v.iterateArray())
result.append(jsonToVec4B(entry));
return result;
}
List<Color> jsonToColorList(Json const& v) {
List<Color> result;
for (auto const& entry : v.iterateArray())
result.append(jsonToColor(entry));
return result;
}
List<Directives> jsonToDirectivesList(Json const& v) {
List<Directives> result;
for (auto const& entry : v.iterateArray())
result.append(entry.toString());
return result;
}
Json jsonFromDirectivesList(List<Directives> const& v) {
JsonArray result;
for (auto& e : v) {
if (e)
result.push_back(*e.stringPtr());
}
return result;
}
Json weightedChoiceFromJson(Json const& source, Json const& default_) {
if (source.isNull())
return default_;
if (source.type() != Json::Type::Array)
throw StarException("Json of array type expected.");
List<pair<float, Json>> options;
float sum = 0;
size_t idx = 0;
while (idx < source.size()) {
float weight = 1;
Json entry = source.get(idx);
if (entry.type() == Json::Type::Int || entry.type() == Json::Type::Float) {
weight = entry.toDouble();
idx++;
if (idx >= source.size())
throw StarException("Weighted companion cube cannot cry.");
sum += weight;
options.append(pair<float, Json>{weight, source.get(idx)});
} else {
sum += weight;
options.append(pair<float, Json>{weight, entry});
}
idx++;
}
if (!options.size())
return default_;
float choice = Random::randf() * sum;
idx = 0;
while (idx < options.size()) {
auto const& entry = options[idx];
if (entry.first >= choice)
return entry.second;
choice -= entry.first;
idx++;
}
return options[options.size() - 1].second;
}
Json binnedChoiceFromJson(Json const& bins, float target, Json const& def) {
JsonArray binList = bins.toArray();
sortByComputedValue(binList, [](Json const& pair) { return -pair.getFloat(0); });
Json result = def;
for (auto const& pair : binList) {
if (pair.getFloat(0) <= target) {
result = pair.get(1);
break;
}
}
return result;
}
template <>
WeightedPool<int> jsonToWeightedPool(Json const& source) {
return jsonToWeightedPool<int>(source, [](Json const& v) { return v.toInt(); });
}
template <>
WeightedPool<unsigned> jsonToWeightedPool(Json const& source) {
return jsonToWeightedPool<unsigned>(source, [](Json const& v) { return v.toUInt(); });
}
template <>
WeightedPool<float> jsonToWeightedPool(Json const& source) {
return jsonToWeightedPool<float>(source, [](Json const& v) { return v.toFloat(); });
}
template <>
WeightedPool<double> jsonToWeightedPool(Json const& source) {
return jsonToWeightedPool<double>(source, [](Json const& v) { return v.toDouble(); });
}
template <>
WeightedPool<String> jsonToWeightedPool(Json const& source) {
return jsonToWeightedPool<String>(source, [](Json const& v) { return v.toString(); });
}
template <>
WeightedPool<JsonArray> jsonToWeightedPool(Json const& source) {
return jsonToWeightedPool<JsonArray>(source, [](Json const& v) { return v.toArray(); });
}
template <>
WeightedPool<JsonObject> jsonToWeightedPool(Json const& source) {
return jsonToWeightedPool<JsonObject>(source, [](Json const& v) { return v.toObject(); });
}
}
|