Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:13:10

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