Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-10-23 08:24:46

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 <fstream>
0017 #include <string>
0018 #include <utility>
0019 #include <vector>
0020 
0021 #include <nlohmann/json.hpp>
0022 
0023 using namespace Acts;
0024 
0025 namespace ActsTests {
0026 
0027 BOOST_AUTO_TEST_SUITE(JsonSuite)
0028 
0029 BOOST_AUTO_TEST_CASE(TransformRoundTripTests) {
0030   Transform3 reference = Transform3::Identity();
0031 
0032   std::ofstream out;
0033 
0034   // Test the identity transform
0035   nlohmann::json identityOut;
0036   to_json(identityOut, reference);
0037   out.open("Transform3_Identity.json");
0038   out << identityOut.dump(2);
0039   out.close();
0040 
0041   auto in = std::ifstream("Transform3_Identity.json",
0042                           std::ifstream::in | std::ifstream::binary);
0043   BOOST_CHECK(in.good());
0044   nlohmann::json identityIn;
0045   in >> identityIn;
0046   in.close();
0047 
0048   Transform3 test;
0049   from_json(identityIn, test);
0050 
0051   BOOST_CHECK(test.isApprox(reference));
0052 
0053   // Test a pure translation transform
0054   reference.pretranslate(Vector3(1., 2., 3.));
0055 
0056   nlohmann::json translationOut;
0057   to_json(translationOut, reference);
0058   out.open("Transform3_Translation.json");
0059   out << translationOut.dump(2);
0060   out.close();
0061 
0062   in = std::ifstream("Transform3_Translation.json",
0063                      std::ifstream::in | std::ifstream::binary);
0064   BOOST_CHECK(in.good());
0065   nlohmann::json translationIn;
0066   in >> translationIn;
0067   in.close();
0068 
0069   test = Transform3::Identity();
0070   from_json(translationIn, test);
0071 
0072   BOOST_CHECK(test.isApprox(reference));
0073 
0074   // Test a full transform
0075   reference = Eigen::AngleAxis(0.12334, Vector3(1., 2., 3).normalized());
0076   reference.pretranslate(Vector3(1., 2., 3.));
0077 
0078   nlohmann::json fullOut;
0079   to_json(fullOut, reference);
0080   out.open("Transform3_Full.json");
0081   out << fullOut.dump(2);
0082   out.close();
0083 
0084   in = std::ifstream("Transform3_Full.json",
0085                      std::ifstream::in | std::ifstream::binary);
0086   BOOST_CHECK(in.good());
0087   nlohmann::json fullIn;
0088   in >> fullIn;
0089   in.close();
0090 
0091   test = Transform3::Identity();
0092   from_json(fullIn, test);
0093 
0094   BOOST_CHECK(test.isApprox(reference));
0095 }
0096 
0097 BOOST_AUTO_TEST_CASE(TransformNullIdentity) {
0098   // An identity matrix
0099   Transform3 reference = Transform3::Identity();
0100 
0101   // Test the identity transform with nulled
0102   Transform3JsonConverter::Options nulledOption{false, false};
0103   nlohmann::json nulledOut =
0104       Transform3JsonConverter::toJson(reference, nulledOption);
0105   BOOST_CHECK_EQUAL(nulledOut["translation"], nullptr);
0106   BOOST_CHECK_EQUAL(nulledOut["rotation"], nullptr);
0107 
0108   // Test with writing the identity
0109   Transform3JsonConverter::Options writtenOption{true, false};
0110   nlohmann::json writtenOut =
0111       Transform3JsonConverter::toJson(reference, writtenOption);
0112   BOOST_CHECK_NE(writtenOut["translation"], nullptr);
0113   BOOST_CHECK_NE(writtenOut["rotation"], nullptr);
0114 }
0115 
0116 BOOST_AUTO_TEST_CASE(TransformTranspose) {
0117   // An identity matrix
0118   Transform3 reference = Transform3::Identity();
0119   reference.pretranslate(Vector3(1., 2., 3.));
0120   reference.rotate(Eigen::AngleAxis(0.12334, Vector3(1., 2., 3).normalized()));
0121 
0122   std::vector<double> referenceT = {1., 2., 3.};
0123   std::vector<double> referenceR = {0.992946,   -0.0975562, 0.0673888,
0124                                     0.0997267,  0.994574,   -0.0296247,
0125                                     -0.0641331, 0.0361362,  0.997287};
0126 
0127   // Test standard writing
0128   Transform3JsonConverter::Options standardOptions{true, false};
0129   nlohmann::json standardOut =
0130       Transform3JsonConverter::toJson(reference, standardOptions);
0131   // Check translation read back in
0132   BOOST_CHECK(standardOut["translation"].get<std::vector<double>>() ==
0133               referenceT);
0134   // Check rotation read back in - not transposed
0135   std::vector<double> readR =
0136       standardOut["rotation"].get<std::vector<double>>();
0137   for (auto [i, rr] : Acts::enumerate(referenceR)) {
0138     CHECK_CLOSE_ABS(readR[i], rr, 1e-5);
0139   }
0140 
0141   // Test transposed writing
0142   Transform3JsonConverter::Options transposeOptions{true, true};
0143   nlohmann::json transposeOut =
0144       Transform3JsonConverter::toJson(reference, transposeOptions);
0145   // Check translation read back in
0146   BOOST_CHECK(transposeOut["translation"].get<std::vector<double>>() ==
0147               referenceT);
0148 
0149   // Check rotation read back in - transposed
0150   std::vector<std::size_t> transposedIndices = {0, 3, 6, 1, 4, 7, 2, 5, 8};
0151   readR = transposeOut["rotation"].get<std::vector<double>>();
0152   for (auto [i, rr] : Acts::enumerate(referenceR)) {
0153     CHECK_CLOSE_ABS(readR[transposedIndices[i]], rr, 1e-5);
0154   }
0155 }
0156 
0157 BOOST_AUTO_TEST_SUITE_END()
0158 
0159 }  // namespace ActsTests