-
Notifications
You must be signed in to change notification settings - Fork 257
Expand file tree
/
Copy pathMaterialJsonConverter.cpp
More file actions
672 lines (601 loc) · 24.7 KB
/
Copy pathMaterialJsonConverter.cpp
File metadata and controls
672 lines (601 loc) · 24.7 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
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
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
// This file is part of the ACTS project.
//
// Copyright (C) 2016 CERN for the benefit of the ACTS project
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
#include "ActsPlugins/Json/MaterialJsonConverter.hpp"
#include "Acts/Definitions/Algebra.hpp"
#include "Acts/Material/BinnedSurfaceMaterial.hpp"
#include "Acts/Material/GridSurfaceMaterial.hpp"
#include "Acts/Material/HomogeneousSurfaceMaterial.hpp"
#include "Acts/Material/HomogeneousVolumeMaterial.hpp"
#include "Acts/Material/ISurfaceMaterial.hpp"
#include "Acts/Material/IVolumeMaterial.hpp"
#include "Acts/Material/InterpolatedMaterialMap.hpp"
#include "Acts/Material/MaterialGridHelper.hpp"
#include "Acts/Material/MaterialSlab.hpp"
#include "Acts/Material/ProtoSurfaceMaterial.hpp"
#include "Acts/Material/ProtoVolumeMaterial.hpp"
#include "Acts/Surfaces/Surface.hpp"
#include "Acts/Utilities/BinUtility.hpp"
#include "Acts/Utilities/Grid.hpp"
#include "Acts/Utilities/GridAxisGenerators.hpp"
#include "Acts/Utilities/TypeList.hpp"
#include "ActsPlugins/Json/GeometryJsonKeys.hpp"
#include "ActsPlugins/Json/GridJsonConverter.hpp"
#include "ActsPlugins/Json/UtilitiesJsonConverter.hpp"
#include <algorithm>
#include <cstddef>
#include <functional>
#include <string>
#include <tuple>
#include <utility>
#include <vector>
namespace {
// Grid definition : eq bound
template <typename value_type>
using GridEqBound =
Acts::Grid<value_type, Acts::Axis<Acts::AxisType::Equidistant,
Acts::AxisBoundaryType::Bound>>;
// Grid definition : eq closed
template <typename value_type>
using GridEqClosed =
Acts::Grid<value_type, Acts::Axis<Acts::AxisType::Equidistant,
Acts::AxisBoundaryType::Closed>>;
// Grid definition : eq bound eq bound
template <typename value_type>
using GridEqBoundEqBound = Acts::Grid<
value_type,
Acts::Axis<Acts::AxisType::Equidistant, Acts::AxisBoundaryType::Bound>,
Acts::Axis<Acts::AxisType::Equidistant, Acts::AxisBoundaryType::Bound>>;
// Grid definition : eq bound eq closed
template <typename value_type>
using GridEqBoundEqClosed = Acts::Grid<
value_type,
Acts::Axis<Acts::AxisType::Equidistant, Acts::AxisBoundaryType::Bound>,
Acts::Axis<Acts::AxisType::Equidistant, Acts::AxisBoundaryType::Closed>>;
// Grid definition : eq closed eq bound
template <typename value_type>
using GridEqClosedEqBound = Acts::Grid<
value_type,
Acts::Axis<Acts::AxisType::Equidistant, Acts::AxisBoundaryType::Closed>,
Acts::Axis<Acts::AxisType::Equidistant, Acts::AxisBoundaryType::Bound>>;
/// @brief Helper function to convert a grid surface material to json
///
/// @tparam indexed_grid_materital_t
/// @param jMaterial the json object to written into
/// @param indexedMaterialCandidate the actual indexed material
template <typename indexed_grid_materital_t>
void convertIndexedGridMaterial(
nlohmann::json& jMaterial,
const Acts::ISurfaceMaterial& indexedMaterialCandidate) {
// Check if the material is of the right type
const indexed_grid_materital_t* indexedMaterial =
dynamic_cast<const indexed_grid_materital_t*>(&indexedMaterialCandidate);
if (indexedMaterial != nullptr) {
// It is a grid type material
jMaterial[Acts::jsonKey().typekey] = "grid";
nlohmann::json jMaterialAccessor;
// Assume globally indexed first
jMaterialAccessor["type"] = "globally_indexed";
// If we have a globally indexed map, the material data is loaded elsewhere,
// locally indexed material vectors are written though
const auto& materialAccessor = indexedMaterial->materialAccessor();
if constexpr (std::is_same_v<decltype(materialAccessor),
const Acts::IndexedMaterialAccessor&>) {
// It's actually locally indexed
jMaterialAccessor["type"] = "indexed";
nlohmann::json jMaterialData;
for (const auto& msl : materialAccessor.material) {
jMaterialData.push_back(msl);
}
jMaterialAccessor["storage_vector"] = jMaterialData;
}
// Write the index grid
jMaterialAccessor["grid"] =
Acts::GridJsonConverter::toJson(indexedMaterial->grid());
jMaterial["accessor"] = jMaterialAccessor;
// Global and bound -> grid local
jMaterial["global_to_grid_local"] = Acts::GridAccessJsonConverter::toJson(
*(indexedMaterial->globalToGridLocalDelegate().instance()));
jMaterial["bound_to_grid_local"] = Acts::GridAccessJsonConverter::toJson(
*(indexedMaterial->boundToGridLocalDelegate().instance()));
}
}
/// @brief Unrolling function for catching the right instance
///
/// @param jMaterial is the json object to be written into
/// @param indexedMaterial is the indexed material
template <typename... Args>
void unrollIndexedGridConversion(nlohmann::json& jMaterial,
const Acts::ISurfaceMaterial& indexedMaterial,
Acts::TypeList<Args...> /*unused*/) {
(convertIndexedGridMaterial<Args>(jMaterial, indexedMaterial), ...);
}
template <typename IndexedAccessorType>
Acts::ISurfaceMaterial* indexedMaterialFromJson(nlohmann::json& jMaterial) {
// Load accessor and grid
nlohmann::json jMaterialAccessor = jMaterial["accessor"];
// Prepare the material and its accessor
IndexedAccessorType materialAccessor(std::vector<Acts::MaterialSlab>{});
// If it's locally indexed, we need to load the material vector
if constexpr (std::is_same_v<IndexedAccessorType,
Acts::IndexedMaterialAccessor>) {
// It's actually locally indexed
for (const auto& msl : jMaterialAccessor["storage_vector"]) {
Acts::MaterialSlab mat = Acts::MaterialSlab::Nothing();
from_json(msl, mat);
materialAccessor.material.push_back(mat);
}
}
// Now make the grid and the axes
nlohmann::json jGrid = jMaterialAccessor["grid"];
nlohmann::json jGridAxes = jGrid["axes"];
Acts::AxisBoundaryType boundaryType0 = jGridAxes[0]["boundary_type"];
// 1-dimensional case
if (jGridAxes.size() == 1u) {
// Bound case
if (boundaryType0 == Acts::AxisBoundaryType::Bound) {
Acts::GridAxisGenerators::EqBound eqBound{jGridAxes[0]["range"],
jGridAxes[0]["bins"]};
auto grid =
Acts::GridJsonConverter::fromJson<decltype(eqBound), std::size_t>(
jGrid, eqBound);
auto boundToGridLocal =
Acts::GridAccessJsonConverter::boundToGridLocal1DimDelegateFromJson(
jMaterial["bound_to_grid_local"]);
auto globalToGridLocal =
Acts::GridAccessJsonConverter::globalToGridLocal1DimDelegateFromJson(
jMaterial["global_to_grid_local"]);
return new Acts::IndexedSurfaceMaterial<decltype(grid)>(
std::move(grid), std::move(materialAccessor),
std::move(boundToGridLocal), std::move(globalToGridLocal));
}
// Closed case
if (boundaryType0 == Acts::AxisBoundaryType::Closed) {
Acts::GridAxisGenerators::EqClosed eqClosed{jGridAxes[0]["range"],
jGridAxes[0]["bins"]};
auto grid =
Acts::GridJsonConverter::fromJson<decltype(eqClosed), std::size_t>(
jGrid, eqClosed);
auto boundToGridLocal =
Acts::GridAccessJsonConverter::boundToGridLocal1DimDelegateFromJson(
jMaterial["bound_to_grid_local"]);
auto globalToGridLocal =
Acts::GridAccessJsonConverter::globalToGridLocal1DimDelegateFromJson(
jMaterial["global_to_grid_local"]);
return new Acts::IndexedSurfaceMaterial<decltype(grid)>(
std::move(grid), std::move(materialAccessor),
std::move(boundToGridLocal), std::move(globalToGridLocal));
}
}
// 2-dimensional case
if (jGridAxes.size() == 2u) {
// Second boundary type
Acts::AxisBoundaryType boundaryType1 = jGridAxes[1]["boundary_type"];
// Bound-bound setup
if (boundaryType0 == Acts::AxisBoundaryType::Bound &&
boundaryType1 == Acts::AxisBoundaryType::Bound) {
Acts::GridAxisGenerators::EqBoundEqBound eqBoundEqBound{
jGridAxes[0]["range"], jGridAxes[0]["bins"], jGridAxes[1]["range"],
jGridAxes[1]["bins"]};
auto grid =
Acts::GridJsonConverter::fromJson<decltype(eqBoundEqBound),
std::size_t>(jGrid, eqBoundEqBound);
auto boundToGridLocal =
Acts::GridAccessJsonConverter::boundToGridLocal2DimDelegateFromJson(
jMaterial["bound_to_grid_local"]);
auto globalToGridLocal =
Acts::GridAccessJsonConverter::globalToGridLocal2DimDelegateFromJson(
jMaterial["global_to_grid_local"]);
return new Acts::IndexedSurfaceMaterial<decltype(grid)>(
std::move(grid), std::move(materialAccessor),
std::move(boundToGridLocal), std::move(globalToGridLocal));
}
// Bound-closed setup
if (boundaryType0 == Acts::AxisBoundaryType::Bound &&
boundaryType1 == Acts::AxisBoundaryType::Closed) {
Acts::GridAxisGenerators::EqBoundEqClosed eqBoundEqClosed{
jGridAxes[0]["range"], jGridAxes[0]["bins"], jGridAxes[1]["range"],
jGridAxes[1]["bins"]};
auto grid = Acts::GridJsonConverter::fromJson<decltype(eqBoundEqClosed),
std::size_t>(
jGrid, eqBoundEqClosed);
auto boundToGridLocal =
Acts::GridAccessJsonConverter::boundToGridLocal2DimDelegateFromJson(
jMaterial["bound_to_grid_local"]);
auto globalToGridLocal =
Acts::GridAccessJsonConverter::globalToGridLocal2DimDelegateFromJson(
jMaterial["global_to_grid_local"]);
return new Acts::IndexedSurfaceMaterial<decltype(grid)>(
std::move(grid), std::move(materialAccessor),
std::move(boundToGridLocal), std::move(globalToGridLocal));
}
// Closed-bound setup
if (boundaryType0 == Acts::AxisBoundaryType::Closed &&
boundaryType1 == Acts::AxisBoundaryType::Bound) {
Acts::GridAxisGenerators::EqClosedEqBound eqClosedEqBound{
jGridAxes[0]["range"], jGridAxes[0]["bins"], jGridAxes[1]["range"],
jGridAxes[1]["bins"]};
auto grid = Acts::GridJsonConverter::fromJson<decltype(eqClosedEqBound),
std::size_t>(
jGrid, eqClosedEqBound);
auto boundToGridLocal =
Acts::GridAccessJsonConverter::boundToGridLocal2DimDelegateFromJson(
jMaterial["bound_to_grid_local"]);
auto globalToGridLocal =
Acts::GridAccessJsonConverter::globalToGridLocal2DimDelegateFromJson(
jMaterial["global_to_grid_local"]);
return new Acts::IndexedSurfaceMaterial<decltype(grid)>(
std::move(grid), std::move(materialAccessor),
std::move(boundToGridLocal), std::move(globalToGridLocal));
}
}
return nullptr;
}
} // namespace
void Acts::to_json(nlohmann::json& j, const Material& t) {
if (t.isVacuum()) {
return;
}
for (unsigned i = 0; i < t.parameters().size(); ++i) {
j.push_back(t.parameters()[i]);
}
}
void Acts::from_json(const nlohmann::json& j, Material& t) {
if (j.is_null()) {
return;
}
Acts::Material::ParametersVector params =
Acts::Material::ParametersVector::Zero();
for (auto i = params.size(); 0 < i--;) {
// .at(...) ensures bound checks
params[i] = j.at(i);
}
t = Acts::Material(params);
return;
}
void Acts::to_json(nlohmann::json& j, const MaterialSlab& t) {
nlohmann::json jmat(t.material());
j["material"] = jmat;
j["thickness"] = t.thickness();
}
void Acts::from_json(const nlohmann::json& j, MaterialSlab& t) {
Material mat = Material::Vacuum();
from_json(j.at("material"), mat);
t = Acts::MaterialSlab(mat, j.at("thickness").get<float>());
}
void Acts::from_json(const nlohmann::json& j, MaterialSlabMatrix& t) {
// the input data must be array[array[object]]
for (auto& outer : j) {
Acts::MaterialSlabVector mpVector;
for (auto& inner : outer) {
MaterialSlab mat = MaterialSlab::Nothing();
from_json(inner, mat);
mpVector.emplace_back(mat);
}
t.push_back(std::move(mpVector));
}
}
void Acts::to_json(nlohmann::json& j, const surfaceMaterialPointer& material) {
nlohmann::json jMaterial;
// A bin utility needs to be written
const Acts::BinUtility* bUtility = nullptr;
// First: Check if we have a proto material
auto psMaterial = dynamic_cast<const Acts::ProtoSurfaceMaterial*>(material);
if (psMaterial != nullptr) {
// Type is proto material
jMaterial[Acts::jsonKey().typekey] = "proto";
// Set mapping type
nlohmann::json mapType(material->mappingType());
jMaterial[Acts::jsonKey().maptype] = mapType;
// by default the protoMaterial is not used for mapping
jMaterial[Acts::jsonKey().mapkey] = false;
// write the bin utility
bUtility = &(psMaterial->binning());
// Check in the number of bin is different from 1
auto& binningData = bUtility->binningData();
for (std::size_t ibin = 0; ibin < binningData.size(); ++ibin) {
if (binningData[ibin].bins() > 1) {
jMaterial[Acts::jsonKey().mapkey] = true;
break;
}
}
nlohmann::json jBin(*bUtility);
jMaterial[Acts::jsonKey().binkey] = jBin;
j[Acts::jsonKey().materialkey] = jMaterial;
return;
}
// Second: check if we have a homogeneous material
auto hsMaterial =
dynamic_cast<const Acts::HomogeneousSurfaceMaterial*>(material);
if (hsMaterial != nullptr) {
// type is homogeneous
jMaterial[Acts::jsonKey().typekey] = "homogeneous";
// Set mapping type
nlohmann::json mapType(material->mappingType());
jMaterial[Acts::jsonKey().maptype] = mapType;
// Material has been mapped
jMaterial[Acts::jsonKey().mapkey] = true;
nlohmann::json jmat(hsMaterial->materialSlab());
jMaterial[Acts::jsonKey().datakey] = nlohmann::json::array({
nlohmann::json::array({
jmat,
}),
});
j[Acts::jsonKey().materialkey] = jMaterial;
return;
}
// Next option remaining: BinnedSurface material
auto bsMaterial = dynamic_cast<const Acts::BinnedSurfaceMaterial*>(material);
if (bsMaterial != nullptr) {
// type is binned
jMaterial[Acts::jsonKey().typekey] = "binned";
// Set mapping type
nlohmann::json mapType(material->mappingType());
jMaterial[Acts::jsonKey().maptype] = mapType;
// Material has been mapped
jMaterial[Acts::jsonKey().mapkey] = true;
bUtility = &(bsMaterial->binUtility());
// convert the data
// get the material matrix
nlohmann::json mmat = nlohmann::json::array();
for (const auto& mpVector : bsMaterial->fullMaterial()) {
nlohmann::json mvec = nlohmann::json::array();
for (const auto& mp : mpVector) {
nlohmann::json jmat(mp);
mvec.push_back(jmat);
}
mmat.push_back(std::move(mvec));
}
jMaterial[Acts::jsonKey().datakey] = std::move(mmat);
// write the bin utility
nlohmann::json jBin(*bUtility);
jMaterial[Acts::jsonKey().binkey] = jBin;
j[Acts::jsonKey().materialkey] = jMaterial;
return;
}
// Possible indexed grid types
using IndexedSurfaceGrids = Acts::TypeList<
Acts::IndexedSurfaceMaterial<GridEqBound<std::size_t>>,
Acts::IndexedSurfaceMaterial<GridEqClosed<std::size_t>>,
Acts::IndexedSurfaceMaterial<GridEqBoundEqBound<std::size_t>>,
Acts::IndexedSurfaceMaterial<GridEqBoundEqClosed<std::size_t>>,
Acts::IndexedSurfaceMaterial<GridEqClosedEqBound<std::size_t>>>;
unrollIndexedGridConversion(jMaterial, *material, IndexedSurfaceGrids{});
if (!jMaterial.empty()) {
j[Acts::jsonKey().materialkey] = jMaterial;
return;
}
// Possible: globally indexed grid types
using GloballyIndexedSurfaceGrids = Acts::TypeList<
Acts::GloballyIndexedSurfaceMaterial<GridEqBound<std::size_t>>,
Acts::GloballyIndexedSurfaceMaterial<GridEqClosed<std::size_t>>,
Acts::GloballyIndexedSurfaceMaterial<GridEqBoundEqBound<std::size_t>>,
Acts::GloballyIndexedSurfaceMaterial<GridEqBoundEqClosed<std::size_t>>,
Acts::GloballyIndexedSurfaceMaterial<GridEqClosedEqBound<std::size_t>>>;
unrollIndexedGridConversion(jMaterial, *material,
GloballyIndexedSurfaceGrids{});
if (!jMaterial.empty()) {
j[Acts::jsonKey().materialkey] = jMaterial;
return;
}
// Possible: material grid types
// using MaterialSurfaceGrids = Acts::TypeList<
// Acts::GridSurfaceMaterial<GridEqBound<std::size_t>>,
// Acts::GridSurfaceMaterial<GridEqClosed<std::size_t>>,
// Acts::GridSurfaceMaterial<GridEqBoundEqBound<std::size_t>>,
// Acts::GridSurfaceMaterial<GridEqBoundEqClosed<std::size_t>>,
// Acts::GridSurfaceMaterial<GridEqClosedEqBound<std::size_t>>>;
// No material the json object is left empty.
return;
}
void Acts::from_json(const nlohmann::json& j,
surfaceMaterialPointer& material) {
if (j.find(Acts::jsonKey().materialkey) == j.end()) {
return;
}
nlohmann::json jMaterial = j[Acts::jsonKey().materialkey];
// By default no material is return.
material = nullptr;
if (jMaterial[Acts::jsonKey().mapkey] == false) {
return;
}
// Grid based material maps
if (jMaterial[Acts::jsonKey().typekey] == "grid") {
material =
indexedMaterialFromJson<Acts::IndexedMaterialAccessor>(jMaterial);
return;
}
// The bin utility and material
Acts::BinUtility bUtility;
Acts::MaterialSlabMatrix mpMatrix;
Acts::MappingType mapType = Acts::MappingType::Default;
std::vector<std::vector<unsigned int>> binCounts{};
for (auto& [key, value] : jMaterial.items()) {
if (key == Acts::jsonKey().binkey && !value.empty()) {
from_json(value, bUtility);
}
if (key == Acts::jsonKey().datakey && !value.empty()) {
from_json(value, mpMatrix);
}
if (key == Acts::jsonKey().maptype && !value.empty()) {
from_json(value, mapType);
}
}
// Return the appropriate typr of material
if (mpMatrix.empty()) {
material = new Acts::ProtoSurfaceMaterial(bUtility, mapType);
} else if (bUtility.bins() == 1) {
material = new Acts::HomogeneousSurfaceMaterial(mpMatrix[0][0], 1, mapType);
} else {
material = new Acts::BinnedSurfaceMaterial(bUtility, mpMatrix, 1, binCounts,
mapType);
}
}
void Acts::to_json(nlohmann::json& j, const volumeMaterialPointer& material) {
nlohmann::json jMaterial;
// A bin utility needs to be written
const Acts::BinUtility* bUtility = nullptr;
// Check if we have a proto material
auto pvMaterial = dynamic_cast<const Acts::ProtoVolumeMaterial*>(material);
if (pvMaterial != nullptr) {
// Type is proto material
jMaterial[Acts::jsonKey().typekey] = "proto";
// By default the protoMaterial is not used for mapping
jMaterial[Acts::jsonKey().mapkey] = false;
bUtility = &(pvMaterial->binUtility());
// Check in the number of bin is different from 1
auto& binningData = bUtility->binningData();
for (std::size_t ibin = 0; ibin < binningData.size(); ++ibin) {
if (binningData[ibin].bins() > 1) {
jMaterial[Acts::jsonKey().mapkey] = true;
break;
}
}
// Write the bin utility
nlohmann::json jBin(*bUtility);
jMaterial[Acts::jsonKey().binkey] = jBin;
j[Acts::jsonKey().materialkey] = jMaterial;
return;
}
// Now check if we have a homogeneous material
auto hvMaterial =
dynamic_cast<const Acts::HomogeneousVolumeMaterial*>(material);
if (hvMaterial != nullptr) {
// type is homogeneous
jMaterial[Acts::jsonKey().typekey] = "homogeneous";
jMaterial[Acts::jsonKey().mapkey] = true;
// array of encoded materials w/ one entry
nlohmann::json jmat(hvMaterial->material({0, 0, 0}));
jMaterial[Acts::jsonKey().datakey] = nlohmann::json::array({
jmat,
});
j[Acts::jsonKey().materialkey] = jMaterial;
return;
}
// Only option remaining: material map
auto bvMaterial2D = dynamic_cast<const Acts::InterpolatedMaterialMap<
Acts::MaterialMapLookup<Acts::MaterialGrid2D>>*>(material);
// Now check if we have a 2D map
if (bvMaterial2D != nullptr) {
// type is binned
jMaterial[Acts::jsonKey().typekey] = "interpolated2D";
jMaterial[Acts::jsonKey().mapkey] = true;
bUtility = &(bvMaterial2D->binUtility());
// convert the data
nlohmann::json mmat = nlohmann::json::array();
Acts::MaterialGrid2D grid = bvMaterial2D->getMapper().getGrid();
for (std::size_t bin = 0; bin < grid.size(); bin++) {
nlohmann::json jmat(Material(grid.at(bin)));
mmat.push_back(jmat);
}
jMaterial[Acts::jsonKey().datakey] = std::move(mmat);
// Write the bin utility
nlohmann::json jBin(*bUtility);
jMaterial[Acts::jsonKey().binkey] = jBin;
j[Acts::jsonKey().materialkey] = jMaterial;
return;
}
// Only option remaining: material map
auto bvMaterial3D = dynamic_cast<const Acts::InterpolatedMaterialMap<
Acts::MaterialMapLookup<Acts::MaterialGrid3D>>*>(material);
// Now check if we have a 3D map
if (bvMaterial3D != nullptr) {
// type is binned
jMaterial[Acts::jsonKey().typekey] = "interpolated3D";
jMaterial[Acts::jsonKey().mapkey] = true;
bUtility = &(bvMaterial3D->binUtility());
// convert the data
nlohmann::json mmat = nlohmann::json::array();
Acts::MaterialGrid3D grid = bvMaterial3D->getMapper().getGrid();
for (std::size_t bin = 0; bin < grid.size(); bin++) {
nlohmann::json jmat(Material(grid.at(bin)));
mmat.push_back(jmat);
}
jMaterial[Acts::jsonKey().datakey] = std::move(mmat);
// Write the bin utility
nlohmann::json jBin(*bUtility);
jMaterial[Acts::jsonKey().binkey] = jBin;
j[Acts::jsonKey().materialkey] = jMaterial;
return;
}
}
void Acts::from_json(const nlohmann::json& j, volumeMaterialPointer& material) {
if (j.find(Acts::jsonKey().materialkey) == j.end()) {
return;
}
nlohmann::json jMaterial = j[Acts::jsonKey().materialkey];
// By default no material is return.
material = nullptr;
if (jMaterial[Acts::jsonKey().mapkey] == false) {
return;
}
// The bin utility and material
Acts::BinUtility bUtility;
std::vector<Acts::Material> mmat;
for (auto& [key, value] : jMaterial.items()) {
if (key == Acts::jsonKey().binkey && !value.empty()) {
from_json(value, bUtility);
}
if (key == Acts::jsonKey().datakey && !value.empty()) {
for (const auto& bin : value) {
Acts::Material mat = Material::Vacuum();
from_json(bin, mat);
mmat.push_back(mat);
}
}
}
// We have protoMaterial
if (mmat.empty()) {
material = new Acts::ProtoVolumeMaterial(bUtility);
return;
}
if (mmat.size() == 1) {
material = new Acts::HomogeneousVolumeMaterial(mmat[0]);
return;
}
if (bUtility.dimensions() == 2) {
std::function<Acts::Vector2(Acts::Vector3)> transfoGlobalToLocal;
Acts::Grid2D grid = createGrid2D(bUtility, transfoGlobalToLocal);
Acts::Grid2D::point_t min = grid.minPosition();
Acts::Grid2D::point_t max = grid.maxPosition();
Acts::Grid2D::index_t nBins = grid.numLocalBins();
Acts::EAxis axis1(min[0], max[0], nBins[0]);
Acts::EAxis axis2(min[1], max[1], nBins[1]);
// Build the grid and fill it with data
Acts::MaterialGrid2D mGrid(std::make_tuple(axis1, axis2));
for (std::size_t bin = 0; bin < mmat.size(); bin++) {
mGrid.at(bin) = mmat[bin].parameters();
}
Acts::MaterialMapLookup<Acts::MaterialGrid2D> matMap(transfoGlobalToLocal,
mGrid);
material = new Acts::InterpolatedMaterialMap<
Acts::MaterialMapLookup<Acts::MaterialGrid2D>>(std::move(matMap),
bUtility);
return;
}
if (bUtility.dimensions() == 3) {
std::function<Acts::Vector3(Acts::Vector3)> transfoGlobalToLocal;
Acts::Grid3D grid = createGrid3D(bUtility, transfoGlobalToLocal);
Acts::Grid3D::point_t min = grid.minPosition();
Acts::Grid3D::point_t max = grid.maxPosition();
Acts::Grid3D::index_t nBins = grid.numLocalBins();
Acts::EAxis axis1(min[0], max[0], nBins[0]);
Acts::EAxis axis2(min[1], max[1], nBins[1]);
Acts::EAxis axis3(min[2], max[2], nBins[2]);
// Build the grid and fill it with data
Acts::MaterialGrid3D mGrid(std::make_tuple(axis1, axis2, axis3));
for (std::size_t bin = 0; bin < mmat.size(); bin++) {
mGrid.at(bin) = mmat[bin].parameters();
}
Acts::MaterialMapLookup<Acts::MaterialGrid3D> matMap(transfoGlobalToLocal,
mGrid);
material = new Acts::InterpolatedMaterialMap<
Acts::MaterialMapLookup<Acts::MaterialGrid3D>>(std::move(matMap),
bUtility);
return;
}
}