Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-22 07:49:03

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/Definitions/Units.hpp"
0013 #include "Acts/Geometry/ConeVolumeBounds.hpp"
0014 #include "Acts/Geometry/CuboidVolumeBounds.hpp"
0015 #include "Acts/Geometry/CutoutCylinderVolumeBounds.hpp"
0016 #include "Acts/Geometry/CylinderVolumeBounds.hpp"
0017 #include "Acts/Geometry/DiamondVolumeBounds.hpp"
0018 #include "Acts/Geometry/GenericCuboidVolumeBounds.hpp"
0019 #include "Acts/Geometry/TrapezoidVolumeBounds.hpp"
0020 #include "ActsPlugins/Json/VolumeBoundsJsonConverter.hpp"
0021 
0022 #include <algorithm>
0023 #include <array>
0024 #include <cmath>
0025 #include <fstream>
0026 #include <memory>
0027 #include <numbers>
0028 #include <string>
0029 #include <vector>
0030 
0031 #include <nlohmann/json.hpp>
0032 
0033 using namespace Acts;
0034 
0035 namespace ActsTests {
0036 
0037 BOOST_AUTO_TEST_SUITE(JsonSuite)
0038 
0039 BOOST_AUTO_TEST_CASE(Cuboid) {
0040   std::ofstream out("CuboidVolumeBounds.json");
0041 
0042   auto cuboidRef = std::make_shared<const CuboidVolumeBounds>(2., 4., 6.);
0043   nlohmann::json cuboidOut = VolumeBoundsJsonConverter::toJson(*cuboidRef);
0044   out << cuboidOut.dump(2);
0045   out.close();
0046 
0047   // Read in json file
0048   auto in = std::ifstream("CuboidVolumeBounds.json",
0049                           std::ifstream::in | std::ifstream::binary);
0050   BOOST_CHECK(in.good());
0051   nlohmann::json cuboidIn;
0052   in >> cuboidIn;
0053   in.close();
0054 
0055   auto cuboidTest =
0056       VolumeBoundsJsonConverter::fromJson<CuboidVolumeBounds>(cuboidIn);
0057   BOOST_CHECK(cuboidRef->values() == cuboidTest->values());
0058 }
0059 
0060 BOOST_AUTO_TEST_CASE(Cylinder) {
0061   std::ofstream out("CylinderVolumeBounds.json");
0062 
0063   auto cylinderRef = std::make_shared<const CylinderVolumeBounds>(
0064       10., 20., 30., std::numbers::pi / 4., 0);
0065   nlohmann::json cylinderOut = VolumeBoundsJsonConverter::toJson(*cylinderRef);
0066   out << cylinderOut.dump(2);
0067   out.close();
0068 
0069   // Read in json file
0070   auto in = std::ifstream("CylinderVolumeBounds.json",
0071                           std::ifstream::in | std::ifstream::binary);
0072   BOOST_CHECK(in.good());
0073   nlohmann::json cylinderIn;
0074   in >> cylinderIn;
0075   in.close();
0076 
0077   auto cylinderTest =
0078       VolumeBoundsJsonConverter::fromJson<CylinderVolumeBounds>(cylinderIn);
0079   BOOST_CHECK(cylinderRef->values() == cylinderTest->values());
0080 }
0081 
0082 BOOST_AUTO_TEST_CASE(Cone) {
0083   std::ofstream out("ConeVolumeBounds.json");
0084 
0085   auto coneRef = std::make_shared<const ConeVolumeBounds>(
0086       0., 0., 0.45, 0.050, 0.050, 0., std::numbers::pi);
0087   nlohmann::json coneOut = VolumeBoundsJsonConverter::toJson(*coneRef);
0088   out << coneOut.dump(2);
0089   out.close();
0090 
0091   // Read in json file
0092   auto in = std::ifstream("ConeVolumeBounds.json",
0093                           std::ifstream::in | std::ifstream::binary);
0094   BOOST_CHECK(in.good());
0095   nlohmann::json coneIn;
0096   in >> coneIn;
0097   in.close();
0098 
0099   auto coneTest = VolumeBoundsJsonConverter::fromJson<ConeVolumeBounds>(coneIn);
0100   BOOST_CHECK(coneRef->values() == coneTest->values());
0101 }
0102 
0103 BOOST_AUTO_TEST_CASE(CutoutCylinder) {
0104   std::ofstream out("CutoutCylinderVolumeBounds.json");
0105 
0106   auto cutoutCylinderRef =
0107       std::make_shared<const CutoutCylinderVolumeBounds>(5, 10, 15, 30, 25);
0108   nlohmann::json cutoutCylinderOut =
0109       VolumeBoundsJsonConverter::toJson(*cutoutCylinderRef);
0110   out << cutoutCylinderOut.dump(2);
0111   out.close();
0112 
0113   // Read in json file
0114   auto in = std::ifstream("CutoutCylinderVolumeBounds.json",
0115                           std::ifstream::in | std::ifstream::binary);
0116   BOOST_CHECK(in.good());
0117   nlohmann::json cutoutCylinderIn;
0118   in >> cutoutCylinderIn;
0119   in.close();
0120 
0121   auto cutoutCylinderTest =
0122       VolumeBoundsJsonConverter::fromJson<CutoutCylinderVolumeBounds>(
0123           cutoutCylinderIn);
0124   BOOST_CHECK(cutoutCylinderRef->values() == cutoutCylinderTest->values());
0125 }
0126 
0127 BOOST_AUTO_TEST_CASE(GenericCuboid) {
0128   std::ofstream out("GenericCuboidVolumeBounds.json");
0129   std::array<Vector3, 8> vertices{};
0130   vertices = {{{0, 0, 0},
0131                {2, 0, 0},
0132                {2, 1, 0},
0133                {0, 1, 0},
0134                {0, 0, 1},
0135                {2, 0, 1},
0136                {2, 1, 1},
0137                {0, 1, 1}}};
0138 
0139   auto genericCuboidRef =
0140       std::make_shared<const GenericCuboidVolumeBounds>(vertices);
0141   nlohmann::json genericCuboidOut =
0142       VolumeBoundsJsonConverter::toJson(*genericCuboidRef);
0143   out << genericCuboidOut.dump(2);
0144   out.close();
0145 
0146   // Read in json file
0147   auto in = std::ifstream("GenericCuboidVolumeBounds.json",
0148                           std::ifstream::in | std::ifstream::binary);
0149   BOOST_CHECK(in.good());
0150   nlohmann::json genericCuboidIn;
0151   in >> genericCuboidIn;
0152   in.close();
0153 
0154   auto genericCuboidTest = VolumeBoundsJsonConverter::fromJson(genericCuboidIn);
0155   BOOST_CHECK(genericCuboidRef->values() == genericCuboidTest->values());
0156 }
0157 
0158 BOOST_AUTO_TEST_CASE(Diamond) {
0159   auto diamondRef =
0160       std::make_shared<const DiamondVolumeBounds>(1., 3., 2., 4., 5., 6.);
0161   nlohmann::json diamondOut = VolumeBoundsJsonConverter::toJson(*diamondRef);
0162 
0163   auto diamondTest = VolumeBoundsJsonConverter::fromJson(diamondOut);
0164   BOOST_REQUIRE(diamondTest != nullptr);
0165 
0166   // Only the six primary values are preserved (eAlphaAngle/eBetaAngle are
0167   // computed from them)
0168   for (std::size_t i = 0; i < DiamondVolumeBounds::eHalfLengthZ + 1; ++i) {
0169     BOOST_CHECK_CLOSE(diamondRef->values()[i], diamondTest->values()[i], 1e-6);
0170   }
0171 }
0172 
0173 BOOST_AUTO_TEST_CASE(Trapezoid) {
0174   std::ofstream out("TrapezoidVolumeBounds.json");
0175 
0176   auto trapezoidRef =
0177       std::make_shared<const TrapezoidVolumeBounds>(2., 4., 6., 8.);
0178   nlohmann::json trapezoidOut =
0179       VolumeBoundsJsonConverter::toJson(*trapezoidRef);
0180   out << trapezoidOut.dump(2);
0181   out.close();
0182 
0183   // Read in json file
0184   auto in = std::ifstream("TrapezoidVolumeBounds.json",
0185                           std::ifstream::in | std::ifstream::binary);
0186   BOOST_CHECK(in.good());
0187   nlohmann::json trapezoidIn;
0188   in >> trapezoidIn;
0189   in.close();
0190 
0191   auto trapezoidTest =
0192       VolumeBoundsJsonConverter::fromJson<TrapezoidVolumeBounds>(trapezoidIn);
0193   BOOST_CHECK(trapezoidRef->values() == trapezoidTest->values());
0194 }
0195 BOOST_AUTO_TEST_SUITE_END()
0196 
0197 }  // namespace ActsTests