Back to home page

sPhenix code displayed by LXR

 
 

    


File indexing completed on 2025-08-06 08:11:20

0001 // This file is part of the Acts project.
0002 //
0003 // Copyright (C) 2019 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 http://mozilla.org/MPL/2.0/.
0008 
0009 #include <boost/test/unit_test.hpp>
0010 
0011 #include "Acts/Definitions/Algebra.hpp"
0012 #include "Acts/Definitions/Direction.hpp"
0013 #include "Acts/Definitions/Tolerance.hpp"
0014 #include "Acts/Geometry/BoundarySurfaceFace.hpp"
0015 #include "Acts/Geometry/CuboidVolumeBounds.hpp"
0016 #include "Acts/Geometry/GeometryContext.hpp"
0017 #include "Acts/Geometry/VolumeBounds.hpp"
0018 #include "Acts/Surfaces/PlaneSurface.hpp"
0019 #include "Acts/Surfaces/Surface.hpp"
0020 #include "Acts/Tests/CommonHelpers/FloatComparisons.hpp"
0021 
0022 #include <algorithm>
0023 #include <array>
0024 #include <memory>
0025 #include <stdexcept>
0026 #include <utility>
0027 #include <vector>
0028 
0029 namespace Acts::Test {
0030 
0031 GeometryContext gctx = GeometryContext();
0032 
0033 double hx{10.}, hy{20.}, hz{30.};
0034 
0035 BOOST_AUTO_TEST_SUITE(Geometry)
0036 
0037 BOOST_AUTO_TEST_CASE(CuboidVolumeConstruction) {
0038   // Test Construction
0039   CuboidVolumeBounds box(hx, hy, hz);
0040 
0041   // Test copy construction
0042   CuboidVolumeBounds copied(box);
0043   BOOST_CHECK_EQUAL(box, copied);
0044 
0045   // Test assigned
0046   CuboidVolumeBounds assigned = box;
0047   BOOST_CHECK_EQUAL(box, assigned);
0048 }
0049 
0050 BOOST_AUTO_TEST_CASE(CuboidVolumeRecreation) {
0051   CuboidVolumeBounds original(hx, hy, hz);
0052   auto valvector = original.values();
0053   std::array<double, CuboidVolumeBounds::eSize> values{};
0054   std::copy_n(valvector.begin(), CuboidVolumeBounds::eSize, values.begin());
0055   CuboidVolumeBounds recreated(values);
0056   BOOST_CHECK_EQUAL(original, recreated);
0057 }
0058 
0059 BOOST_AUTO_TEST_CASE(CuboidVolumeException) {
0060   // Test exception negative x
0061   BOOST_CHECK_THROW(CuboidVolumeBounds(-hx, hy, hz), std::logic_error);
0062   // Test exception negative y
0063   BOOST_CHECK_THROW(CuboidVolumeBounds(hx, -hy, hz), std::logic_error);
0064   // Test exception negative z
0065   BOOST_CHECK_THROW(CuboidVolumeBounds(hx, hy, -hz), std::logic_error);
0066   // Other iterations 0
0067   BOOST_CHECK_THROW(CuboidVolumeBounds(-hx, hy, -hz), std::logic_error);
0068   // Other iterations 1
0069   BOOST_CHECK_THROW(CuboidVolumeBounds(-hx, -hy, hz), std::logic_error);
0070   // Other iterations 2
0071   BOOST_CHECK_THROW(CuboidVolumeBounds(hx, -hy, -hz), std::logic_error);
0072   // Other iterations : all
0073   BOOST_CHECK_THROW(CuboidVolumeBounds(-hx, -hy, -hz), std::logic_error);
0074 }
0075 
0076 BOOST_AUTO_TEST_CASE(CuboidVolumeProperties) {
0077   CuboidVolumeBounds box(hx, hy, hz);
0078   // Test the type
0079   BOOST_CHECK_EQUAL(box.type(), VolumeBounds::eCuboid);
0080   // Test the halflength x
0081   CHECK_CLOSE_ABS(box.get(CuboidVolumeBounds::eHalfLengthX), hx, s_epsilon);
0082   // Test the halflength y
0083   CHECK_CLOSE_ABS(box.get(CuboidVolumeBounds::eHalfLengthY), hy, s_epsilon);
0084   // Test the halflength z
0085   CHECK_CLOSE_ABS(box.get(CuboidVolumeBounds::eHalfLengthZ), hz, s_epsilon);
0086   // Test the streaming
0087   std::vector<double> actvalues = box.values();
0088   std::vector<double> refvalues = {hx, hy, hz};
0089   BOOST_CHECK_EQUAL_COLLECTIONS(actvalues.begin(), actvalues.end(),
0090                                 refvalues.begin(), refvalues.end());
0091 
0092   // Inside position
0093   Vector3 inside({5., 10., 8.});
0094   // Outside positions  in x, y, z
0095   std::vector<Vector3> outsides = {
0096       {20., 1., -2.}, {1., -30., 2.}, {-1., 2., 100.}};
0097 
0098   // Inside position
0099   BOOST_CHECK(box.inside(inside, s_onSurfaceTolerance));
0100 
0101   // Outside position
0102   for (const auto& outside : outsides) {
0103     BOOST_CHECK(!box.inside(outside, s_onSurfaceTolerance));
0104   }
0105 
0106   // Check the binning value positions
0107   CHECK_CLOSE_ABS(box.binningBorder(Acts::binX), hx, s_epsilon);
0108   CHECK_CLOSE_ABS(box.binningBorder(Acts::binY), hy, s_epsilon);
0109   CHECK_CLOSE_ABS(box.binningBorder(Acts::binZ), hz, s_epsilon);
0110   CHECK_CLOSE_ABS(box.binningBorder(Acts::binR), std::sqrt(hx * hx + hy * hy),
0111                   s_epsilon);
0112 }
0113 
0114 BOOST_AUTO_TEST_CASE(CuboidVolumeBoundarySurfaces) {
0115   CuboidVolumeBounds box(5, 8, 7);
0116   auto cvbOrientedSurfaces = box.orientedSurfaces(Transform3::Identity());
0117 
0118   BOOST_CHECK_EQUAL(cvbOrientedSurfaces.size(), 6);
0119 
0120   auto geoCtx = GeometryContext();
0121 
0122   for (auto& os : cvbOrientedSurfaces) {
0123     auto osCenter = os.surface->center(geoCtx);
0124     const auto* pSurface =
0125         dynamic_cast<const Acts::PlaneSurface*>(os.surface.get());
0126     BOOST_REQUIRE_MESSAGE(pSurface != nullptr,
0127                           "The surface is not a plane surface");
0128     auto osNormal = pSurface->normal(geoCtx);
0129     // Check if you step inside the volume with the oriented normal
0130     Vector3 insideBox = osCenter + os.direction * osNormal;
0131     Vector3 outsideBox = osCenter - os.direction * osNormal;
0132     BOOST_CHECK(box.inside(insideBox));
0133     BOOST_CHECK(!box.inside(outsideBox));
0134   }
0135 
0136   Vector3 xaxis(1., 0., 0.);
0137   Vector3 yaxis(0., 1., 0.);
0138   Vector3 zaxis(0., 0., 1.);
0139 
0140   // Test the orientation of the boundary surfaces
0141   auto nFaceXY =
0142       cvbOrientedSurfaces[negativeFaceXY].surface->transform(geoCtx).rotation();
0143   BOOST_CHECK(nFaceXY.col(0).isApprox(xaxis));
0144   BOOST_CHECK(nFaceXY.col(1).isApprox(yaxis));
0145   BOOST_CHECK(nFaceXY.col(2).isApprox(zaxis));
0146 
0147   auto pFaceXY =
0148       cvbOrientedSurfaces[positiveFaceXY].surface->transform(geoCtx).rotation();
0149   BOOST_CHECK(pFaceXY.col(0).isApprox(xaxis));
0150   BOOST_CHECK(pFaceXY.col(1).isApprox(yaxis));
0151   BOOST_CHECK(pFaceXY.col(2).isApprox(zaxis));
0152 
0153   auto nFaceYZ =
0154       cvbOrientedSurfaces[negativeFaceYZ].surface->transform(geoCtx).rotation();
0155   BOOST_CHECK(nFaceYZ.col(0).isApprox(yaxis));
0156   BOOST_CHECK(nFaceYZ.col(1).isApprox(zaxis));
0157   BOOST_CHECK(nFaceYZ.col(2).isApprox(xaxis));
0158 
0159   auto pFaceYZ =
0160       cvbOrientedSurfaces[positiveFaceYZ].surface->transform(geoCtx).rotation();
0161   BOOST_CHECK(pFaceYZ.col(0).isApprox(yaxis));
0162   BOOST_CHECK(pFaceYZ.col(1).isApprox(zaxis));
0163   BOOST_CHECK(pFaceYZ.col(2).isApprox(xaxis));
0164 
0165   auto nFaceZX =
0166       cvbOrientedSurfaces[negativeFaceZX].surface->transform(geoCtx).rotation();
0167   BOOST_CHECK(nFaceZX.col(0).isApprox(zaxis));
0168   BOOST_CHECK(nFaceZX.col(1).isApprox(xaxis));
0169   BOOST_CHECK(nFaceZX.col(2).isApprox(yaxis));
0170 
0171   auto pFaceZX =
0172       cvbOrientedSurfaces[positiveFaceZX].surface->transform(geoCtx).rotation();
0173   BOOST_CHECK(pFaceZX.col(0).isApprox(zaxis));
0174   BOOST_CHECK(pFaceZX.col(1).isApprox(xaxis));
0175   BOOST_CHECK(pFaceZX.col(2).isApprox(yaxis));
0176 }
0177 
0178 BOOST_AUTO_TEST_CASE(CuboidVolumeBoundsSetValues) {
0179   CuboidVolumeBounds box(5, 8, 7);
0180 
0181   for (auto bValue :
0182        {CuboidVolumeBounds::eHalfLengthX, CuboidVolumeBounds::eHalfLengthY,
0183         CuboidVolumeBounds::eHalfLengthZ}) {
0184     ActsScalar target = 0.5 * box.get(bValue);
0185     ActsScalar previous = box.get(bValue);
0186     BOOST_CHECK_THROW(box.set(bValue, -1), std::logic_error);
0187     BOOST_CHECK_EQUAL(box.get(bValue), previous);
0188     box.set(bValue, target);
0189     BOOST_CHECK_EQUAL(box.get(bValue), target);
0190   }
0191 
0192   auto previous = box.values();
0193 
0194   BOOST_CHECK_THROW(box.set({
0195                         {CuboidVolumeBounds::eHalfLengthX, -1},
0196                         {CuboidVolumeBounds::eHalfLengthY, 1},
0197                     }),
0198                     std::logic_error);
0199   auto act = box.values();
0200   BOOST_CHECK_EQUAL_COLLECTIONS(previous.begin(), previous.end(), act.begin(),
0201                                 act.end());
0202 }
0203 
0204 BOOST_AUTO_TEST_SUITE_END()
0205 
0206 }  // namespace Acts::Test