Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-09-20 08:23:25

0001 // This file is part of the ACTS project.
0002 //
0003 // Copyright (C) 2016 CERN for the benefit of the ACTS project
0004 //
0005 // This Source Code Form is subject to the terms of the Mozilla Public
0006 // License, v. 2.0. If a copy of the MPL was not distributed with this
0007 // file, You can obtain one at https://mozilla.org/MPL/2.0/.
0008 
0009 #include <boost/test/unit_test.hpp>
0010 
0011 #include "Acts/Definitions/Algebra.hpp"
0012 #include "Acts/Utilities/Enumerate.hpp"
0013 #include "ActsPlugins/Json/AlgebraJsonConverter.hpp"
0014 #include "ActsTests/CommonHelpers/FloatComparisons.hpp"
0015 
0016 #include <array>
0017 #include <fstream>
0018 #include <stdexcept>
0019 #include <string>
0020 #include <utility>
0021 #include <vector>
0022 
0023 #include <nlohmann/json.hpp>
0024 
0025 using namespace Acts;
0026 
0027 namespace ActsTests {
0028 
0029 BOOST_AUTO_TEST_SUITE(JsonSuite)
0030 
0031 BOOST_AUTO_TEST_CASE(TransformRoundTripTests) {
0032   Transform3 reference = Transform3::Identity();
0033 
0034   std::ofstream out;
0035 
0036   // Test the identity transform
0037   nlohmann::json identityOut;
0038   to_json(identityOut, reference);
0039   out.open("Transform3_Identity.json");
0040   out << identityOut.dump(2);
0041   out.close();
0042 
0043   auto in = std::ifstream("Transform3_Identity.json",
0044                           std::ifstream::in | std::ifstream::binary);
0045   BOOST_CHECK(in.good());
0046   nlohmann::json identityIn;
0047   in >> identityIn;
0048   in.close();
0049 
0050   Transform3 test;
0051   from_json(identityIn, test);
0052 
0053   BOOST_CHECK(test.isApprox(reference));
0054 
0055   // Test a pure translation transform
0056   reference.pretranslate(Vector3(1., 2., 3.));
0057 
0058   nlohmann::json translationOut;
0059   to_json(translationOut, reference);
0060   out.open("Transform3_Translation.json");
0061   out << translationOut.dump(2);
0062   out.close();
0063 
0064   in = std::ifstream("Transform3_Translation.json",
0065                      std::ifstream::in | std::ifstream::binary);
0066   BOOST_CHECK(in.good());
0067   nlohmann::json translationIn;
0068   in >> translationIn;
0069   in.close();
0070 
0071   test = Transform3::Identity();
0072   from_json(translationIn, test);
0073 
0074   BOOST_CHECK(test.isApprox(reference));
0075 
0076   // Test a full transform
0077   reference = Eigen::AngleAxis(0.12334, Vector3(1., 2., 3).normalized());
0078   reference.pretranslate(Vector3(1., 2., 3.));
0079 
0080   nlohmann::json fullOut;
0081   to_json(fullOut, reference);
0082   out.open("Transform3_Full.json");
0083   out << fullOut.dump(2);
0084   out.close();
0085 
0086   in = std::ifstream("Transform3_Full.json",
0087                      std::ifstream::in | std::ifstream::binary);
0088   BOOST_CHECK(in.good());
0089   nlohmann::json fullIn;
0090   in >> fullIn;
0091   in.close();
0092 
0093   test = Transform3::Identity();
0094   from_json(fullIn, test);
0095 
0096   BOOST_CHECK(test.isApprox(reference));
0097 }
0098 
0099 BOOST_AUTO_TEST_CASE(TransformNonOrthogonalRotation) {
0100   // json is an external source, so a scaled rotation has to be rejected
0101   nlohmann::json jTransform;
0102   jTransform["translation"] = std::array<double, 3>{0., 0., 0.};
0103   jTransform["rotation"] =
0104       std::array<double, 9>{2., 0., 0., 0., 1., 0., 0., 0., 1.};
0105 
0106   Transform3 test = Transform3::Identity();
0107   BOOST_CHECK_THROW(from_json(jTransform, test), std::invalid_argument);
0108 }
0109 
0110 BOOST_AUTO_TEST_CASE(TransformNullIdentity) {
0111   // An identity matrix
0112   Transform3 reference = Transform3::Identity();
0113 
0114   // Test the identity transform with nulled
0115   Transform3JsonConverter::Options nulledOption{false, false};
0116   nlohmann::json nulledOut =
0117       Transform3JsonConverter::toJson(reference, nulledOption);
0118   BOOST_CHECK_EQUAL(nulledOut["translation"], nullptr);
0119   BOOST_CHECK_EQUAL(nulledOut["rotation"], nullptr);
0120 
0121   // Test with writing the identity
0122   Transform3JsonConverter::Options writtenOption{true, false};
0123   nlohmann::json writtenOut =
0124       Transform3JsonConverter::toJson(reference, writtenOption);
0125   BOOST_CHECK_NE(writtenOut["translation"], nullptr);
0126   BOOST_CHECK_NE(writtenOut["rotation"], nullptr);
0127 }
0128 
0129 BOOST_AUTO_TEST_CASE(TransformTranspose) {
0130   // An identity matrix
0131   Transform3 reference = Transform3::Identity();
0132   reference.pretranslate(Vector3(1., 2., 3.));
0133   reference.rotate(Eigen::AngleAxis(0.12334, Vector3(1., 2., 3).normalized()));
0134 
0135   std::vector<double> referenceT = {1., 2., 3.};
0136   std::vector<double> referenceR = {0.992946,   -0.0975562, 0.0673888,
0137                                     0.0997267,  0.994574,   -0.0296247,
0138                                     -0.0641331, 0.0361362,  0.997287};
0139 
0140   // Test standard writing
0141   Transform3JsonConverter::Options standardOptions{true, false};
0142   nlohmann::json standardOut =
0143       Transform3JsonConverter::toJson(reference, standardOptions);
0144   // Check translation read back in
0145   BOOST_CHECK(standardOut["translation"].get<std::vector<double>>() ==
0146               referenceT);
0147   // Check rotation read back in - not transposed
0148   std::vector<double> readR =
0149       standardOut["rotation"].get<std::vector<double>>();
0150   for (auto [i, rr] : Acts::enumerate(referenceR)) {
0151     CHECK_CLOSE_ABS(readR[i], rr, 1e-5);
0152   }
0153 
0154   // Test transposed writing
0155   Transform3JsonConverter::Options transposeOptions{true, true};
0156   nlohmann::json transposeOut =
0157       Transform3JsonConverter::toJson(reference, transposeOptions);
0158   // Check translation read back in
0159   BOOST_CHECK(transposeOut["translation"].get<std::vector<double>>() ==
0160               referenceT);
0161 
0162   // Check rotation read back in - transposed
0163   std::vector<std::size_t> transposedIndices = {0, 3, 6, 1, 4, 7, 2, 5, 8};
0164   readR = transposeOut["rotation"].get<std::vector<double>>();
0165   for (auto [i, rr] : Acts::enumerate(referenceR)) {
0166     CHECK_CLOSE_ABS(readR[transposedIndices[i]], rr, 1e-5);
0167   }
0168 }
0169 
0170 BOOST_AUTO_TEST_SUITE_END()
0171 
0172 }  // namespace ActsTests