File indexing completed on 2025-01-18 09:13:10
0001
0002
0003
0004
0005
0006
0007
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
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
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
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
0097 Transform3 reference = Transform3::Identity();
0098
0099
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
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
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
0126 Transform3JsonConverter::Options standardOptions{true, false};
0127 nlohmann::json standardOut =
0128 Transform3JsonConverter::toJson(reference, standardOptions);
0129
0130 BOOST_CHECK(standardOut["translation"].get<std::vector<double>>() ==
0131 referenceT);
0132
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
0140 Transform3JsonConverter::Options transposeOptions{true, true};
0141 nlohmann::json transposeOut =
0142 Transform3JsonConverter::toJson(reference, transposeOptions);
0143
0144 BOOST_CHECK(transposeOut["translation"].get<std::vector<double>>() ==
0145 referenceT);
0146
0147
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()