File indexing completed on 2026-05-03 07:39:52
0001
0002
0003
0004
0005
0006
0007
0008
0009 #include <boost/test/unit_test.hpp>
0010 #include <boost/test/unit_test_suite.hpp>
0011
0012 #include "Acts/Definitions/Algebra.hpp"
0013 #include "Acts/Geometry/GeometryContext.hpp"
0014 #include "Acts/Geometry/GeometryIdentifier.hpp"
0015 #include "Acts/Surfaces/PlaneSurface.hpp"
0016 #include "Acts/Surfaces/RectangleBounds.hpp"
0017 #include "Acts/Utilities/Zip.hpp"
0018 #include "ActsPlugins/Json/JsonSurfacesReader.hpp"
0019 #include "ActsPlugins/Json/SurfaceJsonConverter.hpp"
0020
0021 #include <filesystem>
0022 #include <fstream>
0023 #include <memory>
0024
0025 #include <Eigen/Geometry>
0026 #include <nlohmann/json.hpp>
0027
0028 using namespace Acts;
0029
0030 namespace ActsTests {
0031 GeometryContext gctx = GeometryContext::dangerouslyDefaultConstruct();
0032
0033 const std::vector<std::shared_ptr<Surface>> surfaces = []() {
0034 std::vector<std::shared_ptr<Surface>> v;
0035
0036 for (int i = 0; i < 3; ++i) {
0037 auto bounds = std::make_shared<RectangleBounds>(1.0, 1.0);
0038 Transform3 transform = Transform3::Identity();
0039 transform.translate(Vector3::Random());
0040 Vector3 randomAngles = Vector3::Random();
0041 SquareMatrix3 rotMatrix;
0042 rotMatrix = Eigen::AngleAxis(randomAngles[0], Vector3::UnitX()) *
0043 Eigen::AngleAxis(randomAngles[1], Vector3::UnitY()) *
0044 Eigen::AngleAxis(randomAngles[2], Vector3::UnitZ());
0045 transform.rotate(rotMatrix);
0046 v.push_back(Surface::makeShared<PlaneSurface>(transform, bounds));
0047 }
0048
0049 return v;
0050 }();
0051
0052 const std::string filename = "json_surfaces_reader_tests.json";
0053
0054 struct FileFixture {
0055 FileFixture() {
0056 nlohmann::json js = nlohmann::json::array();
0057
0058 for (const auto &s : surfaces) {
0059 js.push_back(SurfaceJsonConverter::toJson(gctx, *s));
0060 }
0061
0062 nlohmann::json j;
0063 j["foo"] = js;
0064
0065 std::ofstream f(filename);
0066 f << j.dump(2);
0067 }
0068
0069 ~FileFixture() { std::filesystem::remove(filename); }
0070 };
0071
0072 FileFixture fileFixture;
0073
0074 BOOST_AUTO_TEST_SUITE(JsonSuite)
0075
0076 BOOST_AUTO_TEST_CASE(surface_reading_test) {
0077 auto readBackSurfaces = JsonSurfacesReader::readVector({filename, {"foo"}});
0078
0079 BOOST_REQUIRE_EQUAL(surfaces.size(), readBackSurfaces.size());
0080 for (auto [refSurface, surface] : zip(surfaces, readBackSurfaces)) {
0081 BOOST_CHECK(refSurface->localToGlobalTransform(gctx).isApprox(
0082 surface->localToGlobalTransform(gctx), 1.e-4));
0083 BOOST_CHECK(refSurface->localToGlobalTransform(gctx).isApprox(
0084 surface->localToGlobalTransform(gctx), 1.e-4));
0085 BOOST_CHECK(
0086 refSurface->center(gctx).isApprox(surface->center(gctx), 1.e-4));
0087 BOOST_CHECK_EQUAL(refSurface->type(), surface->type());
0088 BOOST_CHECK_EQUAL(refSurface->bounds().type(), surface->bounds().type());
0089 }
0090 }
0091
0092 BOOST_AUTO_TEST_CASE(json_detelement_reading_test) {
0093 auto readBackDetElements =
0094 JsonSurfacesReader::readDetectorElements({filename, {"foo"}}, 1.0);
0095
0096 BOOST_REQUIRE_EQUAL(surfaces.size(), readBackDetElements.size());
0097 for (auto [refSurface, detElement] : zip(surfaces, readBackDetElements)) {
0098 auto surface = &detElement->surface();
0099 BOOST_CHECK(refSurface->localToGlobalTransform(gctx).isApprox(
0100 surface->localToGlobalTransform(gctx), 1.e-4));
0101 BOOST_CHECK(refSurface->localToGlobalTransform(gctx).isApprox(
0102 surface->localToGlobalTransform(gctx), 1.e-4));
0103 BOOST_CHECK(
0104 refSurface->center(gctx).isApprox(surface->center(gctx), 1.e-4));
0105 BOOST_CHECK_EQUAL(refSurface->type(), surface->type());
0106 BOOST_CHECK_EQUAL(refSurface->bounds().type(), surface->bounds().type());
0107 }
0108 }
0109
0110 BOOST_AUTO_TEST_SUITE_END()
0111
0112 }