Analysis Software
Documentation for sPHENIX simulation software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
AlgebraJsonConverterTests.cpp
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file AlgebraJsonConverterTests.cpp
1 // This file is part of the Acts project.
2 //
3 // Copyright (C) 2021 CERN for the benefit of the Acts project
4 //
5 // This Source Code Form is subject to the terms of the Mozilla Public
6 // License, v. 2.0. If a copy of the MPL was not distributed with this
7 // file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 
9 #include <boost/test/unit_test.hpp>
10 
15 
16 #include <fstream>
17 #include <string>
18 #include <utility>
19 #include <vector>
20 
21 #include <nlohmann/json.hpp>
22 
23 using namespace Acts;
24 
25 BOOST_AUTO_TEST_SUITE(AlgebraJsonConversion)
26 
27 BOOST_AUTO_TEST_CASE(TransformRoundTripTests) {
28  Transform3 reference = Transform3::Identity();
29 
30  std::ofstream out;
31 
32  // Test the identity transform
33  nlohmann::json identityOut;
34  to_json(identityOut, reference);
35  out.open("Transform3_Identity.json");
36  out << identityOut.dump(2);
37  out.close();
38 
39  auto in = std::ifstream("Transform3_Identity.json",
40  std::ifstream::in | std::ifstream::binary);
41  BOOST_CHECK(in.good());
42  nlohmann::json identityIn;
43  in >> identityIn;
44  in.close();
45 
47  from_json(identityIn, test);
48 
49  BOOST_CHECK(test.isApprox(reference));
50 
51  // Test a pure translation transform
52  reference.pretranslate(Vector3(1., 2., 3.));
53 
54  nlohmann::json translationOut;
55  to_json(translationOut, reference);
56  out.open("Transform3_Translation.json");
57  out << translationOut.dump(2);
58  out.close();
59 
60  in = std::ifstream("Transform3_Translation.json",
61  std::ifstream::in | std::ifstream::binary);
62  BOOST_CHECK(in.good());
63  nlohmann::json translationIn;
64  in >> translationIn;
65  in.close();
66 
67  test = Transform3::Identity();
68  from_json(translationIn, test);
69 
70  BOOST_CHECK(test.isApprox(reference));
71 
72  // Test a full transform
73  reference = Eigen::AngleAxis(0.12334, Vector3(1., 2., 3).normalized());
74  reference.pretranslate(Vector3(1., 2., 3.));
75 
76  nlohmann::json fullOut;
77  to_json(fullOut, reference);
78  out.open("Transform3_Full.json");
79  out << fullOut.dump(2);
80  out.close();
81 
82  in = std::ifstream("Transform3_Full.json",
83  std::ifstream::in | std::ifstream::binary);
84  BOOST_CHECK(in.good());
85  nlohmann::json fullIn;
86  in >> fullIn;
87  in.close();
88 
89  test = Transform3::Identity();
90  from_json(fullIn, test);
91 
92  BOOST_CHECK(test.isApprox(reference));
93 }
94 
95 BOOST_AUTO_TEST_CASE(TransformNullIdentity) {
96  // An identity matrix
97  Transform3 reference = Transform3::Identity();
98 
99  // Test the identity transform with nulled
100  Transform3JsonConverter::Options nulledOption{false, false};
101  nlohmann::json nulledOut =
102  Transform3JsonConverter::toJson(reference, nulledOption);
103  BOOST_CHECK(nulledOut["translation"] == nullptr);
104  BOOST_CHECK(nulledOut["rotation"] == nullptr);
105 
106  // Test with writing the identity
107  Transform3JsonConverter::Options writtenOption{true, false};
108  nlohmann::json writtenOut =
109  Transform3JsonConverter::toJson(reference, writtenOption);
110  BOOST_CHECK(writtenOut["translation"] != nullptr);
111  BOOST_CHECK(writtenOut["rotation"] != nullptr);
112 }
113 
114 BOOST_AUTO_TEST_CASE(TransformTranspose) {
115  // An identity matrix
116  Transform3 reference = Transform3::Identity();
117  reference.pretranslate(Vector3(1., 2., 3.));
118  reference.rotate(Eigen::AngleAxis(0.12334, Vector3(1., 2., 3).normalized()));
119 
120  std::vector<ActsScalar> referenceT = {1., 2., 3.};
121  std::vector<ActsScalar> referenceR = {0.992946, -0.0975562, 0.0673888,
122  0.0997267, 0.994574, -0.0296247,
123  -0.0641331, 0.0361362, 0.997287};
124 
125  // Test standard writing
126  Transform3JsonConverter::Options standardOptions{true, false};
127  nlohmann::json standardOut =
128  Transform3JsonConverter::toJson(reference, standardOptions);
129  // Check translation read back in
130  BOOST_CHECK(standardOut["translation"].get<std::vector<ActsScalar>>() ==
131  referenceT);
132  // Check rotation read back in - not transposed
133  std::vector<ActsScalar> readR =
134  standardOut["rotation"].get<std::vector<ActsScalar>>();
135  for (auto [i, rr] : Acts::enumerate(referenceR)) {
136  CHECK_CLOSE_ABS(readR[i], rr, 1e-5);
137  }
138 
139  // Test transposed writing
140  Transform3JsonConverter::Options transposeOptions{true, true};
141  nlohmann::json transposeOut =
142  Transform3JsonConverter::toJson(reference, transposeOptions);
143  // Check translation read back in
144  BOOST_CHECK(transposeOut["translation"].get<std::vector<ActsScalar>>() ==
145  referenceT);
146 
147  // Check rotation read back in - transposed
148  std::vector<size_t> transposedIndices = {0, 3, 6, 1, 4, 7, 2, 5, 8};
149  readR = transposeOut["rotation"].get<std::vector<ActsScalar>>();
150  for (auto [i, rr] : Acts::enumerate(referenceR)) {
151  CHECK_CLOSE_ABS(readR[transposedIndices[i]], rr, 1e-5);
152  }
153 }
154 
155 BOOST_AUTO_TEST_SUITE_END()