Skip to content

Commit

Permalink
feat: write extent envelope to json (#1824)
Browse files Browse the repository at this point in the history
- write envelope of the extend to json
- fix json extent unit test
- clean `Extent.hpp` a little bit
  • Loading branch information
andiwand committed Feb 4, 2023
1 parent 2625e6c commit 2c7b63f
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 17 deletions.
4 changes: 2 additions & 2 deletions Core/include/Acts/Geometry/Extent.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@
namespace Acts {

using Envelope = std::array<ActsScalar, 2>;
using ExtentEnvelope = std::array<std::array<ActsScalar, 2>, binValues>;
using ExtentEnvelope = std::array<Envelope, binValues>;

constexpr Envelope zeroEnvelope = {0., 0};
constexpr Envelope zeroEnvelope = {0, 0};
constexpr ExtentEnvelope zeroEnvelopes = {
zeroEnvelope, zeroEnvelope, zeroEnvelope, zeroEnvelope,
zeroEnvelope, zeroEnvelope, zeroEnvelope, zeroEnvelope};
Expand Down
54 changes: 44 additions & 10 deletions Plugins/Json/src/ExtentJsonConverter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,55 @@
#include "Acts/Utilities/Enumerate.hpp"

void Acts::to_json(nlohmann::json& j, const Acts::Extent& e) {
const auto bValueNames = binningValueNames();
const auto& xrange = e.range();
for (auto [ib, ibv] : enumerate(s_binningValues)) {
if (e.constrains(ibv)) {
const auto& r = xrange[ib];
j[bValueNames[ib]] = r;
const auto& bValueNames = binningValueNames();

{
nlohmann::json jrange;
const auto& xrange = e.range();
for (auto [ib, ibv] : enumerate(s_binningValues)) {
if (e.constrains(ibv)) {
jrange[bValueNames[ib]] = xrange[ib];
}
}
j["range"] = jrange;
}

{
nlohmann::json jenvelope;
const auto& envelope = e.envelope();
for (auto [ib, ibv] : enumerate(s_binningValues)) {
if (envelope[ibv] != zeroEnvelope) {
jenvelope[bValueNames[ib]] =
Range1D<ActsScalar>(envelope[ib][0], envelope[ib][1]);
}
}
if (!jenvelope.empty()) {
j["envelope"] = jenvelope;
}
}
}

void Acts::from_json(const nlohmann::json& j, Acts::Extent& e) {
const auto bValueNames = binningValueNames();
for (auto [ib, bvn] : enumerate(bValueNames)) {
if (j.find(bvn) != j.end()) {
e.set(static_cast<BinningValue>(ib), j[bvn]["min"], j[bvn]["max"]);
const auto& bValueNames = binningValueNames();

{
const auto& jrange = j["range"];
for (auto [ib, bvn] : enumerate(bValueNames)) {
if (jrange.contains(bvn)) {
e.set(static_cast<BinningValue>(ib), jrange[bvn]["min"],
jrange[bvn]["max"]);
}
}
}

if (j.contains("envelope")) {
const auto& jenvelope = j["envelope"];
ExtentEnvelope envelope;
for (auto [ib, bvn] : enumerate(bValueNames)) {
if (jenvelope.find(bvn) != jenvelope.end()) {
envelope[ib] = {jenvelope[bvn]["min"], jenvelope[bvn]["max"]};
}
}
e.setEnvelope(envelope);
}
}
10 changes: 5 additions & 5 deletions Tests/UnitTests/Plugins/Json/ExtentJsonConverterTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,12 @@ BOOST_AUTO_TEST_CASE(ExtentRoundtripTests) {
nlohmann::json j;
j["extent"] = e;

Extent eIn = j["exent"];
Extent eIn = j["extent"];

CHECK_CLOSE_ABS(e.min(binR), 0., 10e-5);
CHECK_CLOSE_ABS(e.max(binR), 200., 10e-5);
CHECK_CLOSE_ABS(e.min(binZ), -50., 10e-5);
CHECK_CLOSE_ABS(e.max(binZ), 50., 10e-5);
CHECK_CLOSE_ABS(eIn.min(binR), e.min(binR), 10e-5);
CHECK_CLOSE_ABS(eIn.max(binR), e.max(binR), 10e-5);
CHECK_CLOSE_ABS(eIn.min(binZ), e.min(binZ), 10e-5);
CHECK_CLOSE_ABS(eIn.max(binZ), e.max(binZ), 10e-5);
}

BOOST_AUTO_TEST_SUITE_END()

0 comments on commit 2c7b63f

Please sign in to comment.