Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-10-20 08:00:33

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