Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-11-25 09:05:41

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/data/test_case.hpp>
0010 #include <boost/test/detail/log_level.hpp>
0011 #include <boost/test/tools/context.hpp>
0012 #include <boost/test/tools/old/interface.hpp>
0013 #include <boost/test/unit_test.hpp>
0014 #include <boost/test/unit_test_log.hpp>
0015 #include <boost/test/unit_test_parameters.hpp>
0016 #include <boost/test/unit_test_suite.hpp>
0017 
0018 #include "Acts/Definitions/Algebra.hpp"
0019 #include "Acts/Definitions/Tolerance.hpp"
0020 #include "Acts/Definitions/Units.hpp"
0021 #include "Acts/Geometry/CylinderVolumeBounds.hpp"
0022 #include "Acts/Geometry/CylinderVolumeStack.hpp"
0023 #include "Acts/Geometry/VolumeAttachmentStrategy.hpp"
0024 #include "Acts/Geometry/VolumeResizeStrategy.hpp"
0025 #include "Acts/Utilities/BinningType.hpp"
0026 #include "Acts/Utilities/Logger.hpp"
0027 #include "Acts/Utilities/Zip.hpp"
0028 #include "ActsTests/CommonHelpers/FloatComparisons.hpp"
0029 
0030 #include <numbers>
0031 
0032 using namespace Acts;
0033 using namespace Acts::UnitLiterals;
0034 
0035 namespace ActsTests {
0036 
0037 auto logger = getDefaultLogger("UnitTests", Logging::VERBOSE);
0038 
0039 struct Fixture {
0040   Logging::Level m_level;
0041   Fixture() {
0042     m_level = Logging::getFailureThreshold();
0043     Logging::setFailureThreshold(Logging::FATAL);
0044   }
0045 
0046   ~Fixture() { Logging::setFailureThreshold(m_level); }
0047 };
0048 
0049 BOOST_FIXTURE_TEST_SUITE(GeometrySuite, Fixture)
0050 
0051 static const std::vector<VolumeAttachmentStrategy> strategies = {
0052     VolumeAttachmentStrategy::Gap,
0053     VolumeAttachmentStrategy::First,
0054     VolumeAttachmentStrategy::Second,
0055     VolumeAttachmentStrategy::Midpoint,
0056 };
0057 
0058 static const std::vector<VolumeResizeStrategy> resizeStrategies = {
0059     VolumeResizeStrategy::Expand,
0060     VolumeResizeStrategy::Gap,
0061 };
0062 
0063 BOOST_AUTO_TEST_SUITE(CylinderVolumeStackTest)
0064 BOOST_AUTO_TEST_SUITE(ZDirection)
0065 
0066 // Overlap checks must apply the same surface tolerance as gap checks.
0067 BOOST_DATA_TEST_CASE(AttachmentOverlapTolerance,
0068                      boost::unit_test::data::make(strategies), strategy) {
0069   const auto gctx = GeometryContext::dangerouslyDefaultConstruct();
0070   for (double overlap :
0071        {0.5 * s_onSurfaceTolerance, 2.0 * s_onSurfaceTolerance}) {
0072     auto bounds =
0073         std::make_shared<CylinderVolumeBounds>(100_mm, 200_mm, 400_mm);
0074     Volume first(Transform3::Identity(), bounds);
0075     Volume second(Transform3(Translation3(0, 0, 800_mm - overlap)), bounds);
0076     std::vector<Volume*> volumes{&first, &second};
0077     if (overlap < s_onSurfaceTolerance) {
0078       CylinderVolumeStack stack(gctx, volumes, AxisDirection::AxisZ, strategy,
0079                                 VolumeResizeStrategy::Gap, *logger);
0080       BOOST_CHECK(stack.gaps().empty());
0081       BOOST_CHECK_EQUAL(volumes.size(), 2u);
0082     } else {
0083       BOOST_CHECK_THROW(
0084           CylinderVolumeStack(gctx, volumes, AxisDirection::AxisZ, strategy,
0085                               VolumeResizeStrategy::Gap, *logger),
0086           std::invalid_argument);
0087     }
0088   }
0089 }
0090 
0091 BOOST_DATA_TEST_CASE(Baseline,
0092                      (boost::unit_test::data::xrange(-135, 180, 45) *
0093                       boost::unit_test::data::xrange(0, 2, 1) *
0094                       boost::unit_test::data::make(0.8, 1.0, 1.2) *
0095                       boost::unit_test::data::make(Vector3{0_mm, 0_mm, 0_mm},
0096                                                    Vector3{20_mm, 0_mm, 0_mm},
0097                                                    Vector3{0_mm, 20_mm, 0_mm},
0098                                                    Vector3{20_mm, 20_mm, 0_mm},
0099                                                    Vector3{0_mm, 0_mm, 20_mm}) *
0100                       boost::unit_test::data::make(strategies)),
0101                      angle, rotate, shift, offset, strategy) {
0102   double hlZ = 400_mm;
0103 
0104   const auto gctx = Acts::GeometryContext::dangerouslyDefaultConstruct();
0105 
0106   // Cylinder volumes which already line up, but have different1 radii
0107   auto bounds1 = std::make_shared<CylinderVolumeBounds>(100_mm, 400_mm, hlZ);
0108   auto bounds2 = std::make_shared<CylinderVolumeBounds>(200_mm, 600_mm, hlZ);
0109   auto bounds3 = std::make_shared<CylinderVolumeBounds>(300_mm, 500_mm, hlZ);
0110 
0111   Transform3 base =
0112       AngleAxis3(angle * 1_degree, Vector3::UnitX()) * Translation3(offset);
0113 
0114   Transform3 transform1 = base;
0115   transform1.translate(Vector3{0_mm, 0_mm, -2 * hlZ * shift});
0116   auto vol1 = std::make_shared<Volume>(transform1, bounds1);
0117 
0118   Transform3 transform2 = base;
0119   transform2.translate(Vector3{0_mm, 0_mm, 0_mm});
0120   auto vol2 = std::make_shared<Volume>(transform2, bounds2);
0121 
0122   Transform3 transform3 = base;
0123   transform3.translate(Vector3{0_mm, 0_mm, 2 * hlZ * shift});
0124   auto vol3 = std::make_shared<Volume>(transform3, bounds3);
0125 
0126   std::vector<Volume*> volumes = {vol1.get(), vol2.get(), vol3.get()};
0127   // Rotate to simulate unsorted volumes: all results should be the same!
0128   std::rotate(volumes.begin(), volumes.begin() + rotate, volumes.end());
0129 
0130   auto origVolumes = volumes;
0131 
0132   std::vector<CylinderVolumeBounds> originalBounds;
0133   std::transform(
0134       volumes.begin(), volumes.end(), std::back_inserter(originalBounds),
0135       [](const auto& vol) {
0136         return *dynamic_cast<const CylinderVolumeBounds*>(&vol->volumeBounds());
0137       });
0138 
0139   if (shift < 1.0) {
0140     BOOST_CHECK_THROW(
0141         CylinderVolumeStack(gctx, volumes, AxisDirection::AxisZ, strategy,
0142                             VolumeResizeStrategy::Gap, *logger),
0143         std::invalid_argument);
0144     return;
0145   }
0146   CylinderVolumeStack cylStack(gctx, volumes, AxisDirection::AxisZ, strategy,
0147                                VolumeResizeStrategy::Gap, *logger);
0148 
0149   auto stackBounds =
0150       dynamic_cast<const CylinderVolumeBounds*>(&cylStack.volumeBounds());
0151   BOOST_REQUIRE(stackBounds != nullptr);
0152 
0153   BOOST_CHECK_EQUAL(stackBounds->get(CylinderVolumeBounds::eMinR), 100_mm);
0154   BOOST_CHECK_EQUAL(stackBounds->get(CylinderVolumeBounds::eMaxR), 600_mm);
0155   BOOST_CHECK_EQUAL(stackBounds->get(CylinderVolumeBounds::eHalfLengthZ),
0156                     hlZ + 2 * hlZ * shift);
0157   CHECK_CLOSE_OR_SMALL(cylStack.localToGlobalTransform(gctx).matrix(),
0158                        base.matrix(), 1e-10, 1e-12);
0159 
0160   // All volumes (including gaps) are cylinders and have the same radial bounds
0161   for (const auto& volume : volumes) {
0162     const auto* cylinderBounds =
0163         dynamic_cast<const CylinderVolumeBounds*>(&volume->volumeBounds());
0164     BOOST_REQUIRE(cylinderBounds != nullptr);
0165     BOOST_CHECK_EQUAL(cylinderBounds->get(CylinderVolumeBounds::eMinR), 100_mm);
0166     BOOST_CHECK_EQUAL(cylinderBounds->get(CylinderVolumeBounds::eMaxR), 600_mm);
0167   }
0168 
0169   // Volumes are sorted in (local) z
0170   for (std::size_t i = 0; i < volumes.size() - 1; ++i) {
0171     const auto& a = volumes.at(i);
0172     const auto& b = volumes.at(i + 1);
0173 
0174     BOOST_CHECK_LT((base.inverse() * a->center(gctx))[eZ],
0175                    (base.inverse()* b->center(gctx))[eZ]);
0176   }
0177 
0178   if (shift <= 1.0) {
0179     // No gap volumes were added
0180     BOOST_CHECK_EQUAL(volumes.size(), 3);
0181 
0182     // No expansion, original volumes did not move
0183     BOOST_CHECK_EQUAL(vol1->localToGlobalTransform(gctx).matrix(),
0184                       transform1.matrix());
0185     BOOST_CHECK_EQUAL(vol2->localToGlobalTransform(gctx).matrix(),
0186                       transform2.matrix());
0187     BOOST_CHECK_EQUAL(vol3->localToGlobalTransform(gctx).matrix(),
0188                       transform3.matrix());
0189 
0190     for (const auto& [volume, bounds] : zip(origVolumes, originalBounds)) {
0191       const auto* newBounds =
0192           dynamic_cast<const CylinderVolumeBounds*>(&volume->volumeBounds());
0193       BOOST_CHECK_EQUAL(newBounds->get(CylinderVolumeBounds::eHalfLengthZ),
0194                         bounds.get(CylinderVolumeBounds::eHalfLengthZ));
0195     }
0196   } else {
0197     if (strategy == VolumeAttachmentStrategy::Gap) {
0198       // Gap volumes were added
0199       BOOST_CHECK_EQUAL(volumes.size(), 5);
0200       auto gap1 = volumes.at(1);
0201       auto gap2 = volumes.at(3);
0202 
0203       BOOST_TEST_MESSAGE(
0204           "Gap 1: " << gap1->localToGlobalTransform(gctx).matrix());
0205       BOOST_TEST_MESSAGE(
0206           "Gap 2: " << gap2->localToGlobalTransform(gctx).matrix());
0207 
0208       const auto* gapBounds1 =
0209           dynamic_cast<const CylinderVolumeBounds*>(&gap1->volumeBounds());
0210       const auto* gapBounds2 =
0211           dynamic_cast<const CylinderVolumeBounds*>(&gap2->volumeBounds());
0212 
0213       double gapHlZ = (shift - 1.0) * hlZ;
0214 
0215       BOOST_CHECK(std::abs(gapBounds1->get(CylinderVolumeBounds::eHalfLengthZ) -
0216                            gapHlZ) < 1e-10);
0217       BOOST_CHECK(std::abs(gapBounds2->get(CylinderVolumeBounds::eHalfLengthZ) -
0218                            gapHlZ) < 1e-10);
0219 
0220       double gap1Z = (-2 * hlZ * shift) + hlZ + gapHlZ;
0221       double gap2Z = (2 * hlZ * shift) - hlZ - gapHlZ;
0222 
0223       Transform3 gap1Transform = base * Translation3{0_mm, 0_mm, gap1Z};
0224       Transform3 gap2Transform = base * Translation3{0_mm, 0_mm, gap2Z};
0225 
0226       CHECK_CLOSE_OR_SMALL(gap1->localToGlobalTransform(gctx).matrix(),
0227                            gap1Transform.matrix(), 1e-10, 1e-14);
0228       CHECK_CLOSE_OR_SMALL(gap2->localToGlobalTransform(gctx).matrix(),
0229                            gap2Transform.matrix(), 1e-10, 1e-14);
0230 
0231       // Original volumes did not changes bounds
0232       for (const auto& [volume, bounds] : zip(origVolumes, originalBounds)) {
0233         const auto* newBounds =
0234             dynamic_cast<const CylinderVolumeBounds*>(&volume->volumeBounds());
0235         BOOST_CHECK_EQUAL(newBounds->get(CylinderVolumeBounds::eHalfLengthZ),
0236                           bounds.get(CylinderVolumeBounds::eHalfLengthZ));
0237       }
0238 
0239       // No expansion, original volumes did not move
0240       BOOST_CHECK_EQUAL(vol1->localToGlobalTransform(gctx).matrix(),
0241                         transform1.matrix());
0242       BOOST_CHECK_EQUAL(vol2->localToGlobalTransform(gctx).matrix(),
0243                         transform2.matrix());
0244       BOOST_CHECK_EQUAL(vol3->localToGlobalTransform(gctx).matrix(),
0245                         transform3.matrix());
0246 
0247     } else if (strategy == VolumeAttachmentStrategy::First) {
0248       // No gap volumes were added
0249       BOOST_CHECK_EQUAL(volumes.size(), 3);
0250 
0251       double wGap = (shift - 1.0) * hlZ * 2;
0252 
0253       // Volume 1 got bigger and shifted right
0254       auto newBounds1 =
0255           dynamic_cast<const CylinderVolumeBounds*>(&vol1->volumeBounds());
0256       CHECK_CLOSE_REL(newBounds1->get(CylinderVolumeBounds::eHalfLengthZ),
0257                       hlZ + wGap / 2.0, 1e-12);
0258       double pZ1 = -2 * hlZ * shift + wGap / 2.0;
0259       Transform3 expectedTransform1 = base * Translation3{0_mm, 0_mm, pZ1};
0260       CHECK_CLOSE_OR_SMALL(vol1->localToGlobalTransform(gctx).matrix(),
0261                            expectedTransform1.matrix(), 1e-10, 1e-14);
0262 
0263       // Volume 2 got bigger and shifted left
0264       auto newBounds2 =
0265           dynamic_cast<const CylinderVolumeBounds*>(&vol2->volumeBounds());
0266       CHECK_CLOSE_REL(newBounds2->get(CylinderVolumeBounds::eHalfLengthZ),
0267                       hlZ + wGap / 2.0, 1e-12);
0268       double pZ2 = wGap / 2.0;
0269       Transform3 expectedTransform2 = base * Translation3{0_mm, 0_mm, pZ2};
0270       CHECK_CLOSE_OR_SMALL(vol2->localToGlobalTransform(gctx).matrix(),
0271                            expectedTransform2.matrix(), 1e-10, 1e-14);
0272 
0273       // Volume 3 stayed the same
0274       auto newBounds3 =
0275           dynamic_cast<const CylinderVolumeBounds*>(&vol3->volumeBounds());
0276       BOOST_CHECK_EQUAL(newBounds3->get(CylinderVolumeBounds::eHalfLengthZ),
0277                         hlZ);
0278       double pZ3 = 2 * hlZ * shift;
0279       Transform3 expectedTransform3 = base * Translation3{0_mm, 0_mm, pZ3};
0280       CHECK_CLOSE_OR_SMALL(vol3->localToGlobalTransform(gctx).matrix(),
0281                            expectedTransform3.matrix(), 1e-10, 1e-14);
0282     } else if (strategy == VolumeAttachmentStrategy::Second) {
0283       // No gap volumes were added
0284       BOOST_CHECK_EQUAL(volumes.size(), 3);
0285 
0286       double wGap = (shift - 1.0) * hlZ * 2;
0287 
0288       // Volume 1 stayed the same
0289       auto newBounds1 =
0290           dynamic_cast<const CylinderVolumeBounds*>(&vol1->volumeBounds());
0291       BOOST_CHECK_EQUAL(newBounds1->get(CylinderVolumeBounds::eHalfLengthZ),
0292                         hlZ);
0293       double pZ1 = -2 * hlZ * shift;
0294       Transform3 expectedTransform1 = base * Translation3{0_mm, 0_mm, pZ1};
0295       CHECK_CLOSE_OR_SMALL(vol1->localToGlobalTransform(gctx).matrix(),
0296                            expectedTransform1.matrix(), 1e-10, 1e-14);
0297 
0298       // Volume 2 got bigger and shifted left
0299       auto newBounds2 =
0300           dynamic_cast<const CylinderVolumeBounds*>(&vol2->volumeBounds());
0301       CHECK_CLOSE_REL(newBounds2->get(CylinderVolumeBounds::eHalfLengthZ),
0302                       hlZ + wGap / 2.0, 1e-12);
0303       double pZ2 = -wGap / 2.0;
0304       Transform3 expectedTransform2 = base * Translation3{0_mm, 0_mm, pZ2};
0305       CHECK_CLOSE_OR_SMALL(vol2->localToGlobalTransform(gctx).matrix(),
0306                            expectedTransform2.matrix(), 1e-10, 1e-14);
0307 
0308       // Volume 3 got bigger and shifted left
0309       auto newBounds3 =
0310           dynamic_cast<const CylinderVolumeBounds*>(&vol3->volumeBounds());
0311       CHECK_CLOSE_REL(newBounds3->get(CylinderVolumeBounds::eHalfLengthZ),
0312                       hlZ + wGap / 2.0, 1e-12);
0313       double pZ3 = 2 * hlZ * shift - wGap / 2.0;
0314       Transform3 expectedTransform3 = base * Translation3{0_mm, 0_mm, pZ3};
0315       CHECK_CLOSE_OR_SMALL(vol3->localToGlobalTransform(gctx).matrix(),
0316                            expectedTransform3.matrix(), 1e-10, 1e-14);
0317     } else if (strategy == VolumeAttachmentStrategy::Midpoint) {
0318       // No gap volumes were added
0319       BOOST_CHECK_EQUAL(volumes.size(), 3);
0320 
0321       double wGap = (shift - 1.0) * hlZ * 2;
0322 
0323       // Volume 1 got bigger and shifted right
0324       auto newBounds1 =
0325           dynamic_cast<const CylinderVolumeBounds*>(&vol1->volumeBounds());
0326       CHECK_CLOSE_REL(newBounds1->get(CylinderVolumeBounds::eHalfLengthZ),
0327                       hlZ + wGap / 4.0, 1e-12);
0328       double pZ1 = -2 * hlZ * shift + wGap / 4.0;
0329       Transform3 expectedTransform1 = base * Translation3{0_mm, 0_mm, pZ1};
0330       CHECK_CLOSE_OR_SMALL(vol1->localToGlobalTransform(gctx).matrix(),
0331                            expectedTransform1.matrix(), 1e-10, 1e-14);
0332 
0333       // Volume 2 got bigger but didn't move
0334       auto newBounds2 =
0335           dynamic_cast<const CylinderVolumeBounds*>(&vol2->volumeBounds());
0336       CHECK_CLOSE_REL(newBounds2->get(CylinderVolumeBounds::eHalfLengthZ),
0337                       hlZ + wGap / 2.0, 1e-12);
0338       CHECK_CLOSE_OR_SMALL(vol2->localToGlobalTransform(gctx).matrix(),
0339                            base.matrix(), 1e-10, 1e-12);
0340 
0341       // Volume 3 got bigger and shifted left
0342       auto newBounds3 =
0343           dynamic_cast<const CylinderVolumeBounds*>(&vol3->volumeBounds());
0344       CHECK_CLOSE_REL(newBounds3->get(CylinderVolumeBounds::eHalfLengthZ),
0345                       hlZ + wGap / 4.0, 1e-12);
0346       double pZ3 = 2 * hlZ * shift - wGap / 4.0;
0347       Transform3 expectedTransform3 = base * Translation3{0_mm, 0_mm, pZ3};
0348       CHECK_CLOSE_OR_SMALL(vol3->localToGlobalTransform(gctx).matrix(),
0349                            expectedTransform3.matrix(), 1e-10, 1e-14);
0350     }
0351   }
0352 }
0353 
0354 BOOST_AUTO_TEST_CASE(Asymmetric) {
0355   const auto gctx = Acts::GeometryContext::dangerouslyDefaultConstruct();
0356   double hlZ1 = 200_mm;
0357   double pZ1 = -1100_mm;
0358   double hlZ2 = 600_mm;
0359   double pZ2 = -200_mm;
0360   double hlZ3 = 400_mm;
0361   double pZ3 = 850_mm;
0362 
0363   // Cylinder volumes which already line up, but have different1 radii
0364   auto bounds1 = std::make_shared<CylinderVolumeBounds>(100_mm, 400_mm, hlZ1);
0365   auto bounds2 = std::make_shared<CylinderVolumeBounds>(200_mm, 600_mm, hlZ2);
0366   auto bounds3 = std::make_shared<CylinderVolumeBounds>(300_mm, 500_mm, hlZ3);
0367 
0368   Transform3 transform1 = Transform3::Identity();
0369   transform1.translate(Vector3{0_mm, 0_mm, pZ1});
0370   auto vol1 = std::make_shared<Volume>(transform1, bounds1);
0371 
0372   Transform3 transform2 = Transform3::Identity();
0373   transform2.translate(Vector3{0_mm, 0_mm, pZ2});
0374   auto vol2 = std::make_shared<Volume>(transform2, bounds2);
0375 
0376   Transform3 transform3 = Transform3::Identity();
0377   transform3.translate(Vector3{0_mm, 0_mm, pZ3});
0378   auto vol3 = std::make_shared<Volume>(transform3, bounds3);
0379 
0380   std::vector<Volume*> volumes = {vol2.get(), vol1.get(), vol3.get()};
0381 
0382   CylinderVolumeStack cylStack(gctx, volumes, AxisDirection::AxisZ,
0383                                VolumeAttachmentStrategy::Gap,
0384                                VolumeResizeStrategy::Gap, *logger);
0385   BOOST_CHECK_EQUAL(volumes.size(), 5);
0386 
0387   auto stackBounds =
0388       dynamic_cast<const CylinderVolumeBounds*>(&cylStack.volumeBounds());
0389   BOOST_REQUIRE(stackBounds != nullptr);
0390 
0391   BOOST_CHECK_EQUAL(stackBounds->get(CylinderVolumeBounds::eMinR), 100_mm);
0392   BOOST_CHECK_EQUAL(stackBounds->get(CylinderVolumeBounds::eMaxR), 600_mm);
0393   BOOST_CHECK_EQUAL(stackBounds->get(CylinderVolumeBounds::eHalfLengthZ),
0394                     (std::abs(pZ1 - hlZ1) + pZ3 + hlZ3) / 2.0);
0395 
0396   double midZ = (pZ1 - hlZ1 + pZ3 + hlZ3) / 2.0;
0397   Transform3 expectedTransform{Translation3{0_mm, 0_mm, midZ}};
0398   CHECK_CLOSE_OR_SMALL(cylStack.localToGlobalTransform(gctx).matrix(),
0399                        expectedTransform.matrix(), 1e-10, 1e-14);
0400 }
0401 
0402 BOOST_DATA_TEST_CASE(RotationInZ, boost::unit_test::data::make(strategies),
0403                      strategy) {
0404   const auto gctx = Acts::GeometryContext::dangerouslyDefaultConstruct();
0405   double hlZ = 400_mm;
0406   double gap = 100_mm;
0407   double shift = 300_mm;
0408 
0409   auto bounds1 = std::make_shared<CylinderVolumeBounds>(100_mm, 400_mm, hlZ);
0410   auto bounds2 = std::make_shared<CylinderVolumeBounds>(200_mm, 300_mm, hlZ);
0411 
0412   auto vol1 = std::make_shared<Volume>(
0413       Transform3::Identity() *
0414           Translation3{0_mm, 0_mm, -hlZ - gap / 2.0 + shift},
0415       bounds1);
0416 
0417   auto vol2 = std::make_shared<Volume>(
0418       Transform3::Identity() *
0419           Translation3{0_mm, 0_mm, hlZ + gap / 2.0 + shift} *
0420           AngleAxis3{30_degree, Vector3::UnitZ()},
0421       bounds2);
0422 
0423   std::vector<Volume*> volumes = {vol1.get(), vol2.get()};
0424 
0425   CylinderVolumeStack cylStack(gctx, volumes, AxisDirection::AxisZ, strategy,
0426                                VolumeResizeStrategy::Gap, *logger);
0427 
0428   auto stackBounds =
0429       dynamic_cast<const CylinderVolumeBounds*>(&cylStack.volumeBounds());
0430   BOOST_REQUIRE(stackBounds != nullptr);
0431   BOOST_CHECK_EQUAL(stackBounds->get(CylinderVolumeBounds::eMinR), 100_mm);
0432   BOOST_CHECK_EQUAL(stackBounds->get(CylinderVolumeBounds::eMaxR), 400_mm);
0433   BOOST_CHECK_EQUAL(stackBounds->get(CylinderVolumeBounds::eHalfLengthZ),
0434                     2 * hlZ + gap / 2.0);
0435 
0436   auto newBounds1 =
0437       dynamic_cast<const CylinderVolumeBounds*>(&vol1->volumeBounds());
0438   auto newBounds2 =
0439       dynamic_cast<const CylinderVolumeBounds*>(&vol2->volumeBounds());
0440 
0441   for (const auto& bounds : {newBounds1, newBounds2}) {
0442     BOOST_CHECK_EQUAL(bounds->get(CylinderVolumeBounds::eMinR), 100_mm);
0443     BOOST_CHECK_EQUAL(bounds->get(CylinderVolumeBounds::eMaxR), 400_mm);
0444   }
0445 
0446   if (strategy == VolumeAttachmentStrategy::Gap) {
0447     // Volumes stayed at the same position, not resized
0448     BOOST_CHECK_EQUAL(vol1->center(gctx)[eZ], -hlZ - gap / 2.0 + shift);
0449     BOOST_CHECK_EQUAL(vol2->center(gctx)[eZ], hlZ + gap / 2.0 + shift);
0450     BOOST_CHECK_EQUAL(newBounds1->get(CylinderVolumeBounds::eHalfLengthZ), hlZ);
0451     BOOST_CHECK_EQUAL(newBounds2->get(CylinderVolumeBounds::eHalfLengthZ), hlZ);
0452   } else if (strategy == VolumeAttachmentStrategy::First) {
0453     // Left volume moved, got resized
0454     BOOST_CHECK_EQUAL(vol1->center(gctx)[eZ], -hlZ + shift);
0455     BOOST_CHECK_EQUAL(newBounds1->get(CylinderVolumeBounds::eHalfLengthZ),
0456                       hlZ + gap / 2.0);
0457     // Right volume stayed the same
0458     BOOST_CHECK_EQUAL(vol2->center(gctx)[eZ], hlZ + gap / 2.0 + shift);
0459     BOOST_CHECK_EQUAL(newBounds2->get(CylinderVolumeBounds::eHalfLengthZ), hlZ);
0460   } else if (strategy == VolumeAttachmentStrategy::Second) {
0461     // Left volume stayed the same
0462     BOOST_CHECK_EQUAL(vol1->center(gctx)[eZ], -hlZ - gap / 2.0 + shift);
0463     BOOST_CHECK_EQUAL(newBounds1->get(CylinderVolumeBounds::eHalfLengthZ), hlZ);
0464     // Right volume moved, got resized
0465     BOOST_CHECK_EQUAL(vol2->center(gctx)[eZ], hlZ + shift);
0466     BOOST_CHECK_EQUAL(newBounds2->get(CylinderVolumeBounds::eHalfLengthZ),
0467                       hlZ + gap / 2.0);
0468   } else if (strategy == VolumeAttachmentStrategy::Midpoint) {
0469     // Left volume moved, got resized
0470     BOOST_CHECK_EQUAL(vol1->center(gctx)[eZ], -hlZ - gap / 4.0 + shift);
0471     BOOST_CHECK_EQUAL(newBounds1->get(CylinderVolumeBounds::eHalfLengthZ),
0472                       hlZ + gap / 4.0);
0473 
0474     // Right volume moved, got resized
0475     BOOST_CHECK_EQUAL(vol2->center(gctx)[eZ], hlZ + gap / 4.0 + shift);
0476     BOOST_CHECK_EQUAL(newBounds2->get(CylinderVolumeBounds::eHalfLengthZ),
0477                       hlZ + gap / 4.0);
0478   }
0479 }
0480 
0481 BOOST_DATA_TEST_CASE(UpdateStack,
0482                      (boost::unit_test::data::xrange(-135, 180, 45) *
0483                       boost::unit_test::data::make(Vector3{0_mm, 0_mm, 0_mm},
0484                                                    Vector3{20_mm, 0_mm, 0_mm},
0485                                                    Vector3{0_mm, 20_mm, 0_mm},
0486                                                    Vector3{20_mm, 20_mm, 0_mm},
0487                                                    Vector3{0_mm, 0_mm, 20_mm}) *
0488                       boost::unit_test::data::make(-100_mm, 0_mm, 100_mm) *
0489                       boost::unit_test::data::make(resizeStrategies)),
0490                      angle, offset, zshift, strategy) {
0491   const auto gctx = Acts::GeometryContext::dangerouslyDefaultConstruct();
0492   double hlZ = 400_mm;
0493 
0494   // Cylinder volumes which already line up, but have different1 radii
0495   auto bounds1 = std::make_shared<CylinderVolumeBounds>(100_mm, 600_mm, hlZ);
0496   auto bounds2 = std::make_shared<CylinderVolumeBounds>(100_mm, 600_mm, hlZ);
0497   auto bounds3 = std::make_shared<CylinderVolumeBounds>(100_mm, 600_mm, hlZ);
0498 
0499   Transform3 base = AngleAxis3(angle * 1_degree, Vector3::UnitX()) *
0500                     Translation3(offset + Vector3{0_mm, 0_mm, zshift});
0501 
0502   Transform3 transform1 = base;
0503   transform1.translate(Vector3{0_mm, 0_mm, -2 * hlZ});
0504   auto vol1 = std::make_shared<Volume>(transform1, bounds1);
0505 
0506   Transform3 transform2 = base;
0507   transform2.translate(Vector3{0_mm, 0_mm, 0_mm});
0508   auto vol2 = std::make_shared<Volume>(transform2, bounds2);
0509 
0510   Transform3 transform3 = base;
0511   transform3.translate(Vector3{0_mm, 0_mm, 2 * hlZ});
0512   auto vol3 = std::make_shared<Volume>(transform3, bounds3);
0513 
0514   std::vector<Volume*> volumes = {vol1.get(), vol2.get(), vol3.get()};
0515   std::vector<Volume*> originalVolumes = volumes;
0516 
0517   std::vector<Transform3> originalTransforms = {transform1, transform2,
0518                                                 transform3};
0519 
0520   CylinderVolumeStack cylStack(
0521       gctx, volumes, AxisDirection::AxisZ,
0522       VolumeAttachmentStrategy::Gap,  // should not make a
0523                                       // difference
0524       strategy, *logger);
0525 
0526   const auto* originalBounds =
0527       dynamic_cast<const CylinderVolumeBounds*>(&cylStack.volumeBounds());
0528 
0529   auto assertOriginalBounds = [&]() {
0530     const auto* cylBounds =
0531         dynamic_cast<const CylinderVolumeBounds*>(&cylStack.volumeBounds());
0532     BOOST_REQUIRE(cylBounds != nullptr);
0533     BOOST_CHECK_EQUAL(cylBounds, originalBounds);
0534     BOOST_CHECK_EQUAL(cylBounds->get(CylinderVolumeBounds::eMinR), 100_mm);
0535     BOOST_CHECK_EQUAL(cylBounds->get(CylinderVolumeBounds::eMaxR), 600_mm);
0536     BOOST_CHECK_EQUAL(cylBounds->get(CylinderVolumeBounds::eHalfLengthZ),
0537                       3 * hlZ);
0538   };
0539 
0540   assertOriginalBounds();
0541 
0542   {
0543     // Assign a copy of the identical bounds gives identical bounds
0544     auto bounds = std::make_shared<CylinderVolumeBounds>(
0545         dynamic_cast<const CylinderVolumeBounds&>(cylStack.volumeBounds()));
0546     cylStack.update(gctx, bounds, std::nullopt, *logger);
0547     assertOriginalBounds();
0548   }
0549 
0550   {
0551     // Cannot increase mininmum r
0552     auto bounds = std::make_shared<CylinderVolumeBounds>(
0553         dynamic_cast<const CylinderVolumeBounds&>(cylStack.volumeBounds()));
0554     bounds->set(CylinderVolumeBounds::eMinR, 200_mm);
0555     BOOST_CHECK_THROW(cylStack.update(gctx, bounds, std::nullopt, *logger),
0556                       std::invalid_argument);
0557     assertOriginalBounds();
0558   }
0559 
0560   {
0561     // Cannot decrease maximum r
0562     auto bounds = std::make_shared<CylinderVolumeBounds>(
0563         dynamic_cast<const CylinderVolumeBounds&>(cylStack.volumeBounds()));
0564     bounds->set(CylinderVolumeBounds::eMaxR, 500_mm);
0565     BOOST_CHECK_THROW(cylStack.update(gctx, bounds, std::nullopt, *logger),
0566                       std::invalid_argument);
0567     assertOriginalBounds();
0568   }
0569 
0570   {
0571     // Cannot decrease half length z
0572     auto bounds = std::make_shared<CylinderVolumeBounds>(
0573         dynamic_cast<const CylinderVolumeBounds&>(cylStack.volumeBounds()));
0574     bounds->set(CylinderVolumeBounds::eHalfLengthZ, 2 * hlZ);
0575     BOOST_CHECK_THROW(cylStack.update(gctx, bounds, std::nullopt, *logger),
0576                       std::invalid_argument);
0577     assertOriginalBounds();
0578   }
0579 
0580   {
0581     // Reduce minimum r
0582     auto bounds = std::make_shared<CylinderVolumeBounds>(
0583         dynamic_cast<const CylinderVolumeBounds&>(cylStack.volumeBounds()));
0584     bounds->set(CylinderVolumeBounds::eMinR, 50_mm);
0585     cylStack.update(gctx, bounds, std::nullopt, *logger);
0586     const auto* cylBounds =
0587         dynamic_cast<const CylinderVolumeBounds*>(&cylStack.volumeBounds());
0588     BOOST_REQUIRE(cylBounds != nullptr);
0589     BOOST_CHECK_EQUAL(cylBounds->get(CylinderVolumeBounds::eMinR), 50_mm);
0590     // Rest unchanged
0591     BOOST_CHECK_EQUAL(cylBounds->get(CylinderVolumeBounds::eMaxR), 600_mm);
0592     BOOST_CHECK_EQUAL(cylBounds->get(CylinderVolumeBounds::eHalfLengthZ),
0593                       3 * hlZ);
0594 
0595     // No gap volumes were added
0596     BOOST_CHECK_EQUAL(volumes.size(), 3);
0597 
0598     // All volumes reduces min r to accommodate
0599     for (const auto& [volume, origTransform] :
0600          zip(volumes, originalTransforms)) {
0601       const auto* newBounds =
0602           dynamic_cast<const CylinderVolumeBounds*>(&volume->volumeBounds());
0603       BOOST_CHECK_EQUAL(newBounds->get(CylinderVolumeBounds::eMinR), 50_mm);
0604       BOOST_CHECK_EQUAL(newBounds->get(CylinderVolumeBounds::eMaxR), 600_mm);
0605       BOOST_CHECK_EQUAL(newBounds->get(CylinderVolumeBounds::eHalfLengthZ),
0606                         hlZ);
0607 
0608       // Position stayed the same
0609       BOOST_CHECK_EQUAL(volume->localToGlobalTransform(gctx).matrix(),
0610                         origTransform.matrix());
0611     }
0612   }
0613 
0614   {
0615     // Increase maximum r
0616     auto bounds = std::make_shared<CylinderVolumeBounds>(
0617         dynamic_cast<const CylinderVolumeBounds&>(cylStack.volumeBounds()));
0618     bounds->set(CylinderVolumeBounds::eMaxR, 700_mm);
0619     cylStack.update(gctx, bounds, std::nullopt, *logger);
0620     const auto* cylBounds =
0621         dynamic_cast<const CylinderVolumeBounds*>(&cylStack.volumeBounds());
0622     BOOST_REQUIRE(cylBounds != nullptr);
0623     BOOST_CHECK_EQUAL(cylBounds->get(CylinderVolumeBounds::eMaxR), 700_mm);
0624     // Rest as before
0625     BOOST_CHECK_EQUAL(cylBounds->get(CylinderVolumeBounds::eMinR), 50_mm);
0626     BOOST_CHECK_EQUAL(cylBounds->get(CylinderVolumeBounds::eHalfLengthZ),
0627                       3 * hlZ);
0628 
0629     // No gap volumes were added
0630     BOOST_CHECK_EQUAL(volumes.size(), 3);
0631 
0632     // All volumes reduces min r to accommodate
0633     for (const auto& [volume, origTransform] :
0634          zip(volumes, originalTransforms)) {
0635       const auto* newBounds =
0636           dynamic_cast<const CylinderVolumeBounds*>(&volume->volumeBounds());
0637       BOOST_CHECK_EQUAL(newBounds->get(CylinderVolumeBounds::eMinR), 50_mm);
0638       BOOST_CHECK_EQUAL(newBounds->get(CylinderVolumeBounds::eMaxR), 700_mm);
0639       BOOST_CHECK_EQUAL(newBounds->get(CylinderVolumeBounds::eHalfLengthZ),
0640                         hlZ);
0641 
0642       // Position stayed the same
0643       BOOST_CHECK_EQUAL(volume->localToGlobalTransform(gctx).matrix(),
0644                         origTransform.matrix());
0645     }
0646   }
0647 
0648   {
0649     // Increase half length z
0650     auto bounds = std::make_shared<CylinderVolumeBounds>(
0651         dynamic_cast<const CylinderVolumeBounds&>(cylStack.volumeBounds()));
0652     bounds->set(CylinderVolumeBounds::eHalfLengthZ, 4 * hlZ);
0653     cylStack.update(gctx, bounds, std::nullopt, *logger);
0654     const auto* cylBounds =
0655         dynamic_cast<const CylinderVolumeBounds*>(&cylStack.volumeBounds());
0656     BOOST_REQUIRE(cylBounds != nullptr);
0657     BOOST_CHECK_EQUAL(cylBounds->get(CylinderVolumeBounds::eHalfLengthZ),
0658                       4 * hlZ);
0659 
0660     // Rest as before
0661     BOOST_CHECK_EQUAL(cylBounds->get(CylinderVolumeBounds::eMinR), 50_mm);
0662     BOOST_CHECK_EQUAL(cylBounds->get(CylinderVolumeBounds::eMaxR), 700_mm);
0663 
0664     if (strategy == VolumeResizeStrategy::Expand) {
0665       // No gap volumes were added
0666       BOOST_CHECK_EQUAL(volumes.size(), 3);
0667 
0668       // Volume 1 got bigger and shifted left
0669       auto newBounds1 =
0670           dynamic_cast<const CylinderVolumeBounds*>(&vol1->volumeBounds());
0671       CHECK_CLOSE_REL(newBounds1->get(CylinderVolumeBounds::eHalfLengthZ),
0672                       hlZ + hlZ / 2.0, 1e-12);
0673       Transform3 expectedTransform1 =
0674           base * Translation3{0_mm, 0_mm, -2 * hlZ - hlZ / 2.0};
0675       CHECK_CLOSE_OR_SMALL(vol1->localToGlobalTransform(gctx).matrix(),
0676                            expectedTransform1.matrix(), 1e-10, 1e-12);
0677 
0678       // Volume 2 stayed the same
0679       auto newBounds2 =
0680           dynamic_cast<const CylinderVolumeBounds*>(&vol2->volumeBounds());
0681       BOOST_CHECK_EQUAL(newBounds2->get(CylinderVolumeBounds::eHalfLengthZ),
0682                         hlZ);
0683       BOOST_CHECK_EQUAL(vol2->localToGlobalTransform(gctx).matrix(),
0684                         transform2.matrix());
0685 
0686       // Volume 3 got bigger and shifted right
0687       auto newBounds3 =
0688           dynamic_cast<const CylinderVolumeBounds*>(&vol3->volumeBounds());
0689       CHECK_CLOSE_REL(newBounds3->get(CylinderVolumeBounds::eHalfLengthZ),
0690                       hlZ + hlZ / 2.0, 1e-12);
0691       Transform3 expectedTransform3 =
0692           base * Translation3{0_mm, 0_mm, 2 * hlZ + hlZ / 2.0};
0693       CHECK_CLOSE_OR_SMALL(vol3->localToGlobalTransform(gctx).matrix(),
0694                            expectedTransform3.matrix(), 1e-10, 1e-12);
0695     } else if (strategy == VolumeResizeStrategy::Gap) {
0696       // Gap volumes were added
0697       BOOST_CHECK_EQUAL(volumes.size(), 5);
0698 
0699       for (const auto& [volume, origTransform] :
0700            zip(originalVolumes, originalTransforms)) {
0701         const auto* newBounds =
0702             dynamic_cast<const CylinderVolumeBounds*>(&volume->volumeBounds());
0703         BOOST_CHECK_EQUAL(newBounds->get(CylinderVolumeBounds::eMinR), 50_mm);
0704         BOOST_CHECK_EQUAL(newBounds->get(CylinderVolumeBounds::eMaxR), 700_mm);
0705         BOOST_CHECK_EQUAL(newBounds->get(CylinderVolumeBounds::eHalfLengthZ),
0706                           hlZ);
0707         // Position stayed the same
0708         BOOST_CHECK_EQUAL(volume->localToGlobalTransform(gctx).matrix(),
0709                           origTransform.matrix());
0710       }
0711 
0712       auto gap1 = volumes.front();
0713       auto gap2 = volumes.back();
0714 
0715       const auto* gapBounds1 =
0716           dynamic_cast<const CylinderVolumeBounds*>(&gap1->volumeBounds());
0717       const auto* gapBounds2 =
0718           dynamic_cast<const CylinderVolumeBounds*>(&gap2->volumeBounds());
0719 
0720       BOOST_CHECK_EQUAL(gapBounds1->get(CylinderVolumeBounds::eHalfLengthZ),
0721                         hlZ / 2.0);
0722       BOOST_CHECK_EQUAL(gapBounds2->get(CylinderVolumeBounds::eHalfLengthZ),
0723                         hlZ / 2.0);
0724 
0725       Transform3 gap1Transform =
0726           base * Translation3{0_mm, 0_mm, -3 * hlZ - hlZ / 2.0};
0727       Transform3 gap2Transform =
0728           base * Translation3{0_mm, 0_mm, 3 * hlZ + hlZ / 2.0};
0729 
0730       CHECK_CLOSE_OR_SMALL(gap1->localToGlobalTransform(gctx).matrix(),
0731                            gap1Transform.matrix(), 1e-10, 1e-14);
0732       CHECK_CLOSE_OR_SMALL(gap2->localToGlobalTransform(gctx).matrix(),
0733                            gap2Transform.matrix(), 1e-10, 1e-14);
0734     }
0735   }
0736 }
0737 
0738 BOOST_DATA_TEST_CASE(
0739     UpdateStackOneSided,
0740     (boost::unit_test::data::make(-1.0, 1.0) ^
0741      boost::unit_test::data::make(VolumeResizeStrategy::Gap,
0742                                   VolumeResizeStrategy::Expand)),
0743     f, strategy) {
0744   const auto gctx = Acts::GeometryContext::dangerouslyDefaultConstruct();
0745 
0746   auto trf = Transform3::Identity();
0747 
0748   auto trf1 = trf * Translation3{Vector3{0_mm, 0_mm, -500_mm}};
0749   auto vol1 = std::make_shared<Volume>(
0750       trf1, std::make_shared<CylinderVolumeBounds>(100_mm, 300_mm, 400_mm));
0751 
0752   auto trf2 = trf * Translation3{Vector3{0_mm, 0_mm, 500_mm}};
0753   auto vol2 = std::make_shared<Volume>(
0754       trf2, std::make_shared<CylinderVolumeBounds>(100_mm, 300_mm, 400_mm));
0755 
0756   std::vector<Volume*> volumes = {vol1.get(), vol2.get()};
0757 
0758   CylinderVolumeStack cylStack{
0759       gctx,     volumes, AxisDirection::AxisZ, VolumeAttachmentStrategy::Gap,
0760       strategy, *logger};
0761   const auto* originalBounds =
0762       dynamic_cast<const CylinderVolumeBounds*>(&cylStack.volumeBounds());
0763 
0764   // Increase halflength by 50mm
0765   auto newBounds = std::make_shared<CylinderVolumeBounds>(
0766       dynamic_cast<const CylinderVolumeBounds&>(cylStack.volumeBounds()));
0767   newBounds->set(CylinderVolumeBounds::eHalfLengthZ, 950_mm);
0768   // Shift to +z by 50mm
0769   trf *= Translation3{Vector3{0_mm, 0_mm, f * 50_mm}};
0770   // -> left edge should stay at -400mm, right edge should be at 500mm or the
0771   // other direction
0772 
0773   auto checkUnchanged = [&]() {
0774     const auto* cylBounds =
0775         dynamic_cast<const CylinderVolumeBounds*>(&cylStack.volumeBounds());
0776     BOOST_REQUIRE(cylBounds != nullptr);
0777     BOOST_CHECK_EQUAL(*cylBounds, *originalBounds);
0778   };
0779 
0780   // Invalid: shift too far in z
0781   BOOST_CHECK_THROW(
0782       cylStack.update(gctx, newBounds,
0783                       trf * Translation3{Vector3{0, 0, f * 20_mm}}, *logger),
0784       std::invalid_argument);
0785   checkUnchanged();
0786 
0787   // Invalid: shift in x
0788   BOOST_CHECK_THROW(
0789       cylStack.update(gctx, newBounds, trf * Translation3{Vector3{10_mm, 0, 0}},
0790                       *logger),
0791       std::invalid_argument);
0792   checkUnchanged();
0793 
0794   // Invalid: shift in y
0795   BOOST_CHECK_THROW(
0796       cylStack.update(gctx, newBounds, trf * Translation3{Vector3{0, 10_mm, 0}},
0797                       *logger),
0798       std::invalid_argument);
0799   checkUnchanged();
0800 
0801   // Invalid: rotation
0802   BOOST_CHECK_THROW(
0803       cylStack.update(gctx, newBounds,
0804                       trf * AngleAxis3{10_degree, Vector3::UnitY()}, *logger),
0805       std::invalid_argument);
0806   checkUnchanged();
0807 
0808   cylStack.update(gctx, newBounds, trf, *logger);
0809 
0810   BOOST_CHECK_EQUAL(cylStack.localToGlobalTransform(gctx).matrix(),
0811                     trf.matrix());
0812   const auto* cylBounds =
0813       dynamic_cast<const CylinderVolumeBounds*>(&cylStack.volumeBounds());
0814   BOOST_REQUIRE(cylBounds != nullptr);
0815   BOOST_CHECK_EQUAL(cylBounds->get(CylinderVolumeBounds::eHalfLengthZ), 950_mm);
0816 
0817   // All volumes including gaps should have same r size
0818   for (const auto* vol : volumes) {
0819     const auto* volBounds =
0820         dynamic_cast<const CylinderVolumeBounds*>(&vol->volumeBounds());
0821     BOOST_REQUIRE(volBounds != nullptr);
0822     BOOST_CHECK_EQUAL(volBounds->get(CylinderVolumeBounds::eMinR), 100_mm);
0823     BOOST_CHECK_EQUAL(volBounds->get(CylinderVolumeBounds::eMaxR), 300_mm);
0824   }
0825 
0826   if (strategy == VolumeResizeStrategy::Expand) {
0827     // No gaps were added, there was one gap initially
0828     BOOST_CHECK_EQUAL(volumes.size(), 3);
0829     const Volume* vol = nullptr;
0830     if (f < 0.0) {
0831       // first volume should have gotten bigger
0832       vol = volumes.front();
0833     } else {
0834       // last volume should have gotten bigger
0835       vol = volumes.back();
0836     }
0837 
0838     const auto* volBounds =
0839         dynamic_cast<const CylinderVolumeBounds*>(&vol->volumeBounds());
0840     BOOST_REQUIRE(volBounds != nullptr);
0841     BOOST_CHECK_EQUAL(volBounds->get(CylinderVolumeBounds::eHalfLengthZ),
0842                       450_mm);
0843     BOOST_CHECK_EQUAL(vol->center(gctx)[eZ], f * 550_mm);
0844   } else if (strategy == VolumeResizeStrategy::Gap) {
0845     // One gap volume was added
0846     BOOST_CHECK_EQUAL(volumes.size(), 4);
0847 
0848     const Volume* gap = nullptr;
0849     if (f < 0.0) {
0850       gap = volumes.front();
0851     } else {
0852       gap = volumes.back();
0853     }
0854     const auto* gapBounds =
0855         dynamic_cast<const CylinderVolumeBounds*>(&gap->volumeBounds());
0856     BOOST_REQUIRE(gapBounds != nullptr);
0857 
0858     BOOST_CHECK_EQUAL(gapBounds->get(CylinderVolumeBounds::eHalfLengthZ),
0859                       50_mm);
0860     BOOST_CHECK_EQUAL(gap->center(gctx)[eZ], f * 950_mm);
0861   }
0862 }
0863 
0864 BOOST_AUTO_TEST_CASE(ResizeReproduction1) {
0865   const auto gctx = Acts::GeometryContext::dangerouslyDefaultConstruct();
0866   Transform3 trf1 =
0867       Transform3::Identity() * Translation3{Vector3::UnitZ() * -2000};
0868   auto bounds1 = std::make_shared<CylinderVolumeBounds>(70, 100, 100.0);
0869   Volume vol1{trf1, bounds1};
0870 
0871   std::vector<Volume*> volumes = {&vol1};
0872   CylinderVolumeStack stack(gctx, volumes, AxisDirection::AxisZ,
0873                             VolumeAttachmentStrategy::Gap,
0874                             VolumeResizeStrategy::Gap, *logger);
0875 
0876   Transform3 trf2 =
0877       Transform3::Identity() * Translation3{Vector3::UnitZ() * -1500};
0878   stack.update(gctx, std::make_shared<CylinderVolumeBounds>(30.0, 100, 600),
0879                trf2, *logger);
0880 
0881   std::cout << stack.volumeBounds() << std::endl;
0882   std::cout << stack.localToGlobalTransform(gctx).matrix() << std::endl;
0883 
0884   Transform3 trf3 =
0885       Transform3::Identity() * Translation3{Vector3::UnitZ() * -1600};
0886   stack.update(gctx, std::make_shared<CylinderVolumeBounds>(30.0, 100, 700),
0887                trf3, *logger);
0888 }
0889 
0890 BOOST_AUTO_TEST_CASE(ResizeReproduction2) {
0891   const auto gctx = Acts::GeometryContext::dangerouslyDefaultConstruct();
0892   // The numbers are tuned a bit to reproduce the faulty behavior
0893   Transform3 trf1 =
0894       Transform3::Identity() * Translation3{Vector3::UnitZ() * 263};
0895   auto bounds1 = std::make_shared<CylinderVolumeBounds>(30, 100, 4.075);
0896   Volume vol1{trf1, bounds1};
0897 
0898   std::vector<Volume*> volumes = {&vol1};
0899   CylinderVolumeStack stack(gctx, volumes, AxisDirection::AxisZ,
0900                             VolumeAttachmentStrategy::Gap,
0901                             VolumeResizeStrategy::Gap, *logger);
0902 
0903   Transform3 trf2 =
0904       Transform3::Identity() * Translation3{Vector3::UnitZ() * 260.843};
0905   stack.update(gctx, std::make_shared<CylinderVolumeBounds>(30.0, 100, 6.232),
0906                trf2, *logger);
0907 
0908   std::cout << stack.volumeBounds() << std::endl;
0909   std::cout << stack.localToGlobalTransform(gctx).matrix() << std::endl;
0910 
0911   Transform3 trf3 =
0912       Transform3::Identity() * Translation3{Vector3::UnitZ() * 1627.31};
0913   stack.update(gctx,
0914                std::make_shared<CylinderVolumeBounds>(30.0, 100, 1372.699),
0915                trf3, *logger);
0916 }
0917 
0918 //   original size
0919 // <--------------->
0920 // +---------------+
0921 // |               |
0922 // |               |
0923 // |   Volume 1    |
0924 // |               |
0925 // |               |
0926 // +---------------+
0927 //         first resize
0928 // <-------------------------->
0929 // +---------------+----------+
0930 // |               |          |
0931 // |               |          |
0932 // |   Volume 1    |   Gap    |
0933 // |               |          |      Gap is
0934 // |               |          |      reused!--+
0935 // +---------------+----------+               |
0936 //             second resize                  |
0937 // <----------------------------------->      |
0938 // +---------------+-------------------+      |
0939 // |               |                   |      |
0940 // |               |                   |      |
0941 // |   Volume 1    |        Gap        |<-----+
0942 // |               |                   |
0943 // |               |                   |
0944 // +---------------+-------------------+
0945 //
0946 BOOST_AUTO_TEST_CASE(ResizeGapMultiple) {
0947   const auto gctx = Acts::GeometryContext::dangerouslyDefaultConstruct();
0948   Transform3 trf = Transform3::Identity();
0949   auto bounds = std::make_shared<CylinderVolumeBounds>(70, 100, 100.0);
0950   Volume vol{trf, bounds};
0951 
0952   BOOST_TEST_CONTEXT("Positive") {
0953     std::vector<Volume*> volumes = {&vol};
0954     CylinderVolumeStack stack(gctx, volumes, AxisDirection::AxisZ,
0955                               VolumeAttachmentStrategy::Gap,
0956                               VolumeResizeStrategy::Gap, *logger);
0957 
0958     BOOST_CHECK_EQUAL(volumes.size(), 1);
0959     BOOST_CHECK(stack.gaps().empty());
0960 
0961     stack.update(gctx, std::make_shared<CylinderVolumeBounds>(30.0, 100, 200),
0962                  trf * Translation3{Vector3::UnitZ() * 100}, *logger);
0963     BOOST_CHECK_EQUAL(volumes.size(), 2);
0964     BOOST_CHECK_EQUAL(stack.gaps().size(), 1);
0965 
0966     BOOST_CHECK_EQUAL(stack.gaps().front()->center(gctx)[eZ], 200.0);
0967     const auto* cylBounds = dynamic_cast<const CylinderVolumeBounds*>(
0968         &stack.gaps().front()->volumeBounds());
0969     BOOST_REQUIRE_NE(cylBounds, nullptr);
0970     BOOST_CHECK_EQUAL(cylBounds->get(CylinderVolumeBounds::eHalfLengthZ),
0971                       100.0);
0972 
0973     stack.update(gctx, std::make_shared<CylinderVolumeBounds>(30.0, 100, 300),
0974                  trf * Translation3{Vector3::UnitZ() * 200}, *logger);
0975 
0976     BOOST_CHECK_EQUAL(volumes.size(), 2);
0977     // No additional gap volume was added!
0978     BOOST_CHECK_EQUAL(stack.gaps().size(), 1);
0979 
0980     BOOST_CHECK_EQUAL(stack.gaps().front()->center(gctx)[eZ], 300.0);
0981     cylBounds = dynamic_cast<const CylinderVolumeBounds*>(
0982         &stack.gaps().front()->volumeBounds());
0983     BOOST_REQUIRE_NE(cylBounds, nullptr);
0984     BOOST_CHECK_EQUAL(cylBounds->get(CylinderVolumeBounds::eHalfLengthZ),
0985                       200.0);
0986   }
0987 
0988   BOOST_TEST_CONTEXT("Negative") {
0989     std::vector<Volume*> volumes = {&vol};
0990     CylinderVolumeStack stack(gctx, volumes, AxisDirection::AxisZ,
0991                               VolumeAttachmentStrategy::Gap,
0992                               VolumeResizeStrategy::Gap, *logger);
0993 
0994     BOOST_CHECK_EQUAL(volumes.size(), 1);
0995     BOOST_CHECK(stack.gaps().empty());
0996 
0997     stack.update(gctx, std::make_shared<CylinderVolumeBounds>(30.0, 100, 200),
0998                  trf * Translation3{Vector3::UnitZ() * -100}, *logger);
0999     BOOST_CHECK_EQUAL(volumes.size(), 2);
1000     BOOST_CHECK_EQUAL(stack.gaps().size(), 1);
1001 
1002     BOOST_CHECK_EQUAL(stack.gaps().front()->center(gctx)[eZ], -200.0);
1003     const auto* cylBounds = dynamic_cast<const CylinderVolumeBounds*>(
1004         &stack.gaps().front()->volumeBounds());
1005     BOOST_REQUIRE_NE(cylBounds, nullptr);
1006     BOOST_CHECK_EQUAL(cylBounds->get(CylinderVolumeBounds::eHalfLengthZ),
1007                       100.0);
1008 
1009     stack.update(gctx, std::make_shared<CylinderVolumeBounds>(30.0, 100, 300),
1010                  trf * Translation3{Vector3::UnitZ() * -200}, *logger);
1011 
1012     BOOST_CHECK_EQUAL(volumes.size(), 2);
1013     // No additional gap volume was added!
1014     BOOST_CHECK_EQUAL(stack.gaps().size(), 1);
1015 
1016     BOOST_CHECK_EQUAL(stack.gaps().front()->center(gctx)[eZ], -300.0);
1017     cylBounds = dynamic_cast<const CylinderVolumeBounds*>(
1018         &stack.gaps().front()->volumeBounds());
1019     BOOST_REQUIRE_NE(cylBounds, nullptr);
1020     BOOST_CHECK_EQUAL(cylBounds->get(CylinderVolumeBounds::eHalfLengthZ),
1021                       200.0);
1022   }
1023 }
1024 
1025 // Regression test for the degenerate end-gap that surfaced as
1026 // "CaloEndCapDiscNegativeZ::Gap1 has nullptr portal for NegativeDisc".
1027 //
1028 // When a z-stack is resized such that one boundary coincides with the volume's
1029 // existing boundary only to within floating-point rounding (i.e. below
1030 // s_onSurfaceTolerance), ResizeStrategy::Gap must NOT mint a gap volume there:
1031 // the resulting near-zero-thickness gap has two coincident disc portals that
1032 // downstream navigation (CylinderNavigationPolicy) cannot resolve. The offset
1033 // used below (1e-6, well under s_onSurfaceTolerance = 1e-4) stands in for the
1034 // ~1e-13 mm rounding residue seen in the ATLAS calorimeter geometry.
1035 BOOST_AUTO_TEST_CASE(ResizeSubToleranceBoundaryNoDegenerateGap) {
1036   const auto gctx = Acts::GeometryContext::dangerouslyDefaultConstruct();
1037 
1038   // Sub-tolerance boundary offset (well below s_onSurfaceTolerance = 1e-4).
1039   const double subTol = 1e-6;
1040 
1041   auto checkNoDegenerateGap = [&](const CylinderVolumeStack& stack) {
1042     for (const auto& gap : stack.gaps()) {
1043       const auto* cylBounds =
1044           dynamic_cast<const CylinderVolumeBounds*>(&gap->volumeBounds());
1045       BOOST_REQUIRE_NE(cylBounds, nullptr);
1046       BOOST_CHECK_GT(cylBounds->get(CylinderVolumeBounds::eHalfLengthZ),
1047                      s_onSurfaceTolerance);
1048     }
1049   };
1050 
1051   // The -z edge coincides to within tolerance; the +z side genuinely expands.
1052   // This is the exact configuration of the reported CaloEndCapDiscNegativeZ.
1053   BOOST_TEST_CONTEXT("NegativeBoundaryCoincides") {
1054     // Volume spans z in [-100, 100].
1055     Volume vol{Transform3::Identity(),
1056                std::make_shared<CylinderVolumeBounds>(70, 100, 100.0)};
1057     std::vector<Volume*> volumes = {&vol};
1058     CylinderVolumeStack stack(gctx, volumes, AxisDirection::AxisZ,
1059                               VolumeAttachmentStrategy::Gap,
1060                               VolumeResizeStrategy::Gap, *logger);
1061     BOOST_CHECK(stack.gaps().empty());
1062 
1063     // New extent: z in [-100 - subTol, 500]. The -z edge is a hair *below* the
1064     // existing -100 edge purely from rounding; the +z edge is a real expansion.
1065     const double newMinZ = -100.0 - subTol;
1066     const double newMaxZ = 500.0;
1067     const double newHlZ = (newMaxZ - newMinZ) / 2.0;
1068     const double newMidZ = (newMaxZ + newMinZ) / 2.0;
1069     stack.update(
1070         gctx, std::make_shared<CylinderVolumeBounds>(70, 100, newHlZ),
1071         Transform3::Identity() * Translation3{Vector3::UnitZ() * newMidZ},
1072         *logger);
1073 
1074     // Exactly one gap: the genuine one on the +z side. The sub-tolerance -z
1075     // boundary must not have produced a (degenerate) gap.
1076     BOOST_CHECK_EQUAL(stack.gaps().size(), 1);
1077     checkNoDegenerateGap(stack);
1078   }
1079 
1080   // Mirror image: the +z edge coincides to within tolerance, real gap at -z.
1081   BOOST_TEST_CONTEXT("PositiveBoundaryCoincides") {
1082     Volume vol{Transform3::Identity(),
1083                std::make_shared<CylinderVolumeBounds>(70, 100, 100.0)};
1084     std::vector<Volume*> volumes = {&vol};
1085     CylinderVolumeStack stack(gctx, volumes, AxisDirection::AxisZ,
1086                               VolumeAttachmentStrategy::Gap,
1087                               VolumeResizeStrategy::Gap, *logger);
1088     BOOST_CHECK(stack.gaps().empty());
1089 
1090     // New extent: z in [-500, 100 + subTol].
1091     const double newMinZ = -500.0;
1092     const double newMaxZ = 100.0 + subTol;
1093     const double newHlZ = (newMaxZ - newMinZ) / 2.0;
1094     const double newMidZ = (newMaxZ + newMinZ) / 2.0;
1095     stack.update(
1096         gctx, std::make_shared<CylinderVolumeBounds>(70, 100, newHlZ),
1097         Transform3::Identity() * Translation3{Vector3::UnitZ() * newMidZ},
1098         *logger);
1099 
1100     BOOST_CHECK_EQUAL(stack.gaps().size(), 1);
1101     checkNoDegenerateGap(stack);
1102   }
1103 }
1104 
1105 BOOST_AUTO_TEST_SUITE_END()
1106 
1107 BOOST_AUTO_TEST_SUITE(RDirection)
1108 
1109 BOOST_DATA_TEST_CASE(Baseline,
1110                      (boost::unit_test::data::xrange(-135, 180, 45) *
1111                       boost::unit_test::data::xrange(0, 2, 1) *
1112                       boost::unit_test::data::make(-0.1, 0.0, 0.1) *
1113                       boost::unit_test::data::make(Vector3{0_mm, 0_mm, 0_mm},
1114                                                    Vector3{20_mm, 0_mm, 0_mm},
1115                                                    Vector3{0_mm, 20_mm, 0_mm},
1116                                                    Vector3{20_mm, 20_mm, 0_mm},
1117                                                    Vector3{0_mm, 0_mm, 20_mm}) *
1118                       boost::unit_test::data::make(strategies)),
1119                      angle, rotate, f, offset, strategy) {
1120   const auto gctx = Acts::GeometryContext::dangerouslyDefaultConstruct();
1121   double hlZ = 400_mm;
1122 
1123   double fInner = 1.0 + f;
1124   double fOuter = 1.0 - f;
1125 
1126   // Cylinder volumes which already line up in r but have different z and hl
1127   auto bounds1 = std::make_shared<CylinderVolumeBounds>(fInner * 100_mm,
1128                                                         fOuter * 300_mm, hlZ);
1129   auto bounds2 = std::make_shared<CylinderVolumeBounds>(fInner * 300_mm,
1130                                                         fOuter * 600_mm, hlZ);
1131   auto bounds3 = std::make_shared<CylinderVolumeBounds>(fInner * 600_mm,
1132                                                         fOuter * 900_mm, hlZ);
1133 
1134   Transform3 base =
1135       AngleAxis3(angle * 1_degree, Vector3::UnitX()) * Translation3(offset);
1136 
1137   // volumes are shifted in z
1138 
1139   Transform3 transform1 = base;
1140   transform1.translate(Vector3{0_mm, 0_mm, 20_mm});
1141   auto vol1 = std::make_shared<Volume>(transform1, bounds1);
1142 
1143   Transform3 transform2 = base;
1144   transform2.translate(Vector3{0_mm, 0_mm, -30_mm});
1145   auto vol2 = std::make_shared<Volume>(transform2, bounds2);
1146 
1147   Transform3 transform3 = base;
1148   transform3.translate(Vector3{0_mm, 0_mm, 40_mm});
1149   auto vol3 = std::make_shared<Volume>(transform3, bounds3);
1150 
1151   std::vector<Volume*> volumes = {vol1.get(), vol2.get(), vol3.get()};
1152   // Rotate to simulate unsorted volumes: all results should be the same!
1153   std::rotate(volumes.begin(), volumes.begin() + rotate, volumes.end());
1154 
1155   std::vector<Volume*> origVolumes = volumes;
1156 
1157   std::vector<CylinderVolumeBounds> originalBounds;
1158   std::transform(
1159       volumes.begin(), volumes.end(), std::back_inserter(originalBounds),
1160       [](const auto& vol) {
1161         return dynamic_cast<const CylinderVolumeBounds&>(vol->volumeBounds());
1162       });
1163 
1164   if (f < 0.0) {
1165     BOOST_CHECK_THROW(
1166         CylinderVolumeStack(gctx, volumes, AxisDirection::AxisR, strategy,
1167                             VolumeResizeStrategy::Gap, *logger),
1168         std::invalid_argument);
1169     return;
1170   }
1171 
1172   CylinderVolumeStack cylStack(gctx, volumes, AxisDirection::AxisR, strategy,
1173                                VolumeResizeStrategy::Gap, *logger);
1174 
1175   auto stackBounds =
1176       dynamic_cast<const CylinderVolumeBounds*>(&cylStack.volumeBounds());
1177   BOOST_REQUIRE(stackBounds != nullptr);
1178 
1179   BOOST_CHECK_EQUAL(stackBounds->get(CylinderVolumeBounds::eMinR),
1180                     fInner * 100_mm);
1181   BOOST_CHECK_EQUAL(stackBounds->get(CylinderVolumeBounds::eMaxR),
1182                     fOuter * 900_mm);
1183   double expectedHalfLengthZ = (40_mm + 30_mm + 2 * hlZ) / 2.0;
1184   BOOST_CHECK_EQUAL(stackBounds->get(CylinderVolumeBounds::eHalfLengthZ),
1185                     expectedHalfLengthZ);
1186 
1187   // After synchronization, all volumes should have the same z position and half
1188   // length
1189   // This includes possible gap volumes!
1190   Transform3 commonTransform = base * Translation3{0_mm, 0_mm, 5_mm};
1191 
1192   CHECK_CLOSE_OR_SMALL(cylStack.localToGlobalTransform(gctx).matrix(),
1193                        commonTransform.matrix(), 1e-10, 1e-14);
1194 
1195   for (const auto& volume : volumes) {
1196     const auto* cylinderBounds =
1197         dynamic_cast<const CylinderVolumeBounds*>(&volume->volumeBounds());
1198     BOOST_REQUIRE(cylinderBounds != nullptr);
1199     BOOST_CHECK_EQUAL(cylinderBounds->get(CylinderVolumeBounds::eHalfLengthZ),
1200                       expectedHalfLengthZ);
1201   }
1202 
1203   BOOST_CHECK_EQUAL(
1204       dynamic_cast<const CylinderVolumeBounds&>(vol1->volumeBounds())
1205           .get(CylinderVolumeBounds::eMinR),
1206       fInner * 100_mm);
1207 
1208   BOOST_CHECK_EQUAL(
1209       dynamic_cast<const CylinderVolumeBounds&>(vol3->volumeBounds())
1210           .get(CylinderVolumeBounds::eMaxR),
1211       fOuter * 900_mm);
1212 
1213   // Volumes are sorted in r
1214   for (std::size_t i = 0; i < volumes.size() - 1; ++i) {
1215     const auto& a = volumes.at(i);
1216     const auto& b = volumes.at(i + 1);
1217 
1218     const auto* aBounds =
1219         dynamic_cast<const CylinderVolumeBounds*>(&a->volumeBounds());
1220     const auto* bBounds =
1221         dynamic_cast<const CylinderVolumeBounds*>(&b->volumeBounds());
1222 
1223     double aMidR = (aBounds->get(CylinderVolumeBounds::eMinR) +
1224                     aBounds->get(CylinderVolumeBounds::eMaxR)) /
1225                    2.0;
1226 
1227     double bMidR = (bBounds->get(CylinderVolumeBounds::eMinR) +
1228                     bBounds->get(CylinderVolumeBounds::eMaxR)) /
1229                    2.0;
1230 
1231     BOOST_CHECK_LT(aMidR, bMidR);
1232   }
1233 
1234   if (f == 0.0) {
1235     // No gap volumes were added
1236     BOOST_CHECK_EQUAL(volumes.size(), 3);
1237 
1238     // Original volumes did not change r bounds
1239     for (const auto& [volume, origCylBounds] :
1240          zip(origVolumes, originalBounds)) {
1241       const auto* newBounds =
1242           dynamic_cast<const CylinderVolumeBounds*>(&volume->volumeBounds());
1243       BOOST_CHECK_EQUAL(newBounds->get(CylinderVolumeBounds::eMinR),
1244                         origCylBounds.get(CylinderVolumeBounds::eMinR));
1245       BOOST_CHECK_EQUAL(newBounds->get(CylinderVolumeBounds::eMaxR),
1246                         origCylBounds.get(CylinderVolumeBounds::eMaxR));
1247     }
1248   } else {
1249     const auto* newBounds1 =
1250         dynamic_cast<const CylinderVolumeBounds*>(&vol1->volumeBounds());
1251     const auto* newBounds2 =
1252         dynamic_cast<const CylinderVolumeBounds*>(&vol2->volumeBounds());
1253     const auto* newBounds3 =
1254         dynamic_cast<const CylinderVolumeBounds*>(&vol3->volumeBounds());
1255     if (strategy == VolumeAttachmentStrategy::Gap) {
1256       // Two gap volumes were added
1257       BOOST_CHECK_EQUAL(volumes.size(), 5);
1258 
1259       // Original volumes did not change r bounds
1260       BOOST_CHECK_EQUAL(newBounds1->get(CylinderVolumeBounds::eMinR),
1261                         fInner * 100_mm);
1262       BOOST_CHECK_EQUAL(newBounds1->get(CylinderVolumeBounds::eMaxR),
1263                         fOuter * 300_mm);
1264       BOOST_CHECK_EQUAL(newBounds2->get(CylinderVolumeBounds::eMinR),
1265                         fInner * 300_mm);
1266       BOOST_CHECK_EQUAL(newBounds2->get(CylinderVolumeBounds::eMaxR),
1267                         fOuter * 600_mm);
1268       BOOST_CHECK_EQUAL(newBounds3->get(CylinderVolumeBounds::eMinR),
1269                         fInner * 600_mm);
1270       BOOST_CHECK_EQUAL(newBounds3->get(CylinderVolumeBounds::eMaxR),
1271                         fOuter * 900_mm);
1272 
1273       auto gap1 = volumes.at(1);
1274       auto gap2 = volumes.at(3);
1275 
1276       const auto* gapBounds1 =
1277           dynamic_cast<const CylinderVolumeBounds*>(&gap1->volumeBounds());
1278       const auto* gapBounds2 =
1279           dynamic_cast<const CylinderVolumeBounds*>(&gap2->volumeBounds());
1280 
1281       BOOST_CHECK_EQUAL(gapBounds1->get(CylinderVolumeBounds::eMinR),
1282                         fOuter * 300_mm);
1283       BOOST_CHECK_EQUAL(gapBounds1->get(CylinderVolumeBounds::eMaxR),
1284                         fInner * 300_mm);
1285       BOOST_CHECK_EQUAL(gapBounds2->get(CylinderVolumeBounds::eMinR),
1286                         fOuter * 600_mm);
1287       BOOST_CHECK_EQUAL(gapBounds2->get(CylinderVolumeBounds::eMaxR),
1288                         fInner * 600_mm);
1289 
1290     } else if (strategy == VolumeAttachmentStrategy::First) {
1291       // No gap volumes were added
1292       BOOST_CHECK_EQUAL(volumes.size(), 3);
1293 
1294       // Volume 1 got bigger and grew to meet Volume 2
1295       BOOST_CHECK_EQUAL(newBounds1->get(CylinderVolumeBounds::eMinR),
1296                         fInner * 100_mm);
1297       BOOST_CHECK_EQUAL(newBounds1->get(CylinderVolumeBounds::eMaxR),
1298                         fInner * 300_mm);
1299 
1300       // Volume 2 got bigger and grew to meet Volume 3
1301       BOOST_CHECK_EQUAL(newBounds2->get(CylinderVolumeBounds::eMinR),
1302                         fInner * 300_mm);
1303       BOOST_CHECK_EQUAL(newBounds2->get(CylinderVolumeBounds::eMaxR),
1304                         fInner * 600_mm);
1305 
1306       // Volume 3 stayed the same
1307       BOOST_CHECK_EQUAL(newBounds3->get(CylinderVolumeBounds::eMinR),
1308                         fInner * 600_mm);
1309       BOOST_CHECK_EQUAL(newBounds3->get(CylinderVolumeBounds::eMaxR),
1310                         fOuter * 900_mm);
1311 
1312     } else if (strategy == VolumeAttachmentStrategy::Second) {
1313       // No gap volumes were added
1314       BOOST_CHECK_EQUAL(volumes.size(), 3);
1315 
1316       // Volume 1 stayed the same
1317       BOOST_CHECK_EQUAL(newBounds1->get(CylinderVolumeBounds::eMinR),
1318                         fInner * 100_mm);
1319       BOOST_CHECK_EQUAL(newBounds1->get(CylinderVolumeBounds::eMaxR),
1320                         fOuter * 300_mm);
1321 
1322       // Volume 2 got bigger and grew inward to meet Volume 1
1323       BOOST_CHECK_EQUAL(newBounds2->get(CylinderVolumeBounds::eMinR),
1324                         fOuter * 300_mm);
1325       BOOST_CHECK_EQUAL(newBounds2->get(CylinderVolumeBounds::eMaxR),
1326                         fOuter * 600_mm);
1327 
1328       // Volume 3 got bigger and grew inward to meet Volume 2
1329       BOOST_CHECK_EQUAL(newBounds3->get(CylinderVolumeBounds::eMinR),
1330                         fOuter * 600_mm);
1331       BOOST_CHECK_EQUAL(newBounds3->get(CylinderVolumeBounds::eMaxR),
1332                         fOuter * 900_mm);
1333     } else if (strategy == VolumeAttachmentStrategy::Midpoint) {
1334       // No gap volumes were added
1335       BOOST_CHECK_EQUAL(volumes.size(), 3);
1336 
1337       // Volume 1 grew outward to meet Volume 2 half way
1338       BOOST_CHECK_EQUAL(newBounds1->get(CylinderVolumeBounds::eMinR),
1339                         fInner * 100_mm);
1340       BOOST_CHECK_EQUAL(newBounds1->get(CylinderVolumeBounds::eMaxR),
1341                         (fOuter * 300_mm + fInner * 300_mm) / 2.0);
1342 
1343       // Volume 2 grew inward and outward to meet Volume 1 and 3 half way
1344       BOOST_CHECK_EQUAL(newBounds2->get(CylinderVolumeBounds::eMinR),
1345                         (fOuter * 300_mm + fInner * 300_mm) / 2.0);
1346       BOOST_CHECK_EQUAL(newBounds2->get(CylinderVolumeBounds::eMaxR),
1347                         (fOuter * 600_mm + fInner * 600_mm) / 2.0);
1348 
1349       // Volume 3 grew inward to meet Volume 2 half way
1350       BOOST_CHECK_EQUAL(newBounds3->get(CylinderVolumeBounds::eMinR),
1351                         (fOuter * 600_mm + fInner * 600_mm) / 2.0);
1352       BOOST_CHECK_EQUAL(newBounds3->get(CylinderVolumeBounds::eMaxR),
1353                         fOuter * 900_mm);
1354     }
1355   }
1356 }
1357 
1358 BOOST_DATA_TEST_CASE(UpdateStack,
1359                      (boost::unit_test::data::xrange(-135, 180, 45) *
1360                       boost::unit_test::data::make(Vector3{0_mm, 0_mm, 0_mm},
1361                                                    Vector3{20_mm, 0_mm, 0_mm},
1362                                                    Vector3{0_mm, 20_mm, 0_mm},
1363                                                    Vector3{20_mm, 20_mm, 0_mm},
1364                                                    Vector3{0_mm, 0_mm, 20_mm}) *
1365                       boost::unit_test::data::make(-100_mm, 0_mm, 100_mm) *
1366                       boost::unit_test::data::make(resizeStrategies)),
1367                      angle, offset, zshift, strategy) {
1368   const auto gctx = Acts::GeometryContext::dangerouslyDefaultConstruct();
1369   double hlZ = 400_mm;
1370 
1371   // Cylinder volumes which already line up in r but have different z and hl
1372   auto bounds1 = std::make_shared<CylinderVolumeBounds>(100_mm, 300_mm, hlZ);
1373   auto bounds2 = std::make_shared<CylinderVolumeBounds>(300_mm, 600_mm, hlZ);
1374   auto bounds3 = std::make_shared<CylinderVolumeBounds>(600_mm, 900_mm, hlZ);
1375 
1376   Transform3 base = AngleAxis3(angle * 1_degree, Vector3::UnitX()) *
1377                     Translation3(offset + Vector3{0, 0, zshift});
1378 
1379   // volumes are shifted in z
1380   auto vol1 = std::make_shared<Volume>(base, bounds1);
1381   auto vol2 = std::make_shared<Volume>(base, bounds2);
1382   auto vol3 = std::make_shared<Volume>(base, bounds3);
1383 
1384   std::vector<Volume*> volumes = {vol1.get(), vol2.get(), vol3.get()};
1385   std::vector<Volume*> originalVolumes = volumes;
1386 
1387   std::vector<CylinderVolumeBounds> originalBounds;
1388 
1389   std::transform(
1390       volumes.begin(), volumes.end(), std::back_inserter(originalBounds),
1391       [](const auto& vol) {
1392         return *dynamic_cast<const CylinderVolumeBounds*>(&vol->volumeBounds());
1393       });
1394 
1395   const CylinderVolumeBounds* originalOuterBounds = nullptr;
1396 
1397   std::unique_ptr<CylinderVolumeStack> cylStack;
1398 
1399   auto resetCylStack = [&]() {
1400     volumes = originalVolumes;
1401 
1402     for (const auto& [volume, origBounds] : zip(volumes, originalBounds)) {
1403       volume->assignVolumeBounds(
1404           std::make_shared<CylinderVolumeBounds>(origBounds));
1405     }
1406 
1407     cylStack = std::make_unique<CylinderVolumeStack>(
1408         gctx, volumes, AxisDirection::AxisR,
1409         VolumeAttachmentStrategy::Gap,  // should not make a
1410                                         // difference
1411         strategy, *logger);
1412 
1413     originalOuterBounds =
1414         dynamic_cast<const CylinderVolumeBounds*>(&cylStack->volumeBounds());
1415   };
1416 
1417   resetCylStack();
1418 
1419   auto assertInitialVolumesUnchanged = [&]() {
1420     for (const auto& [volume, origCylBounds] :
1421          zip(originalVolumes, originalBounds)) {
1422       const auto* newBounds =
1423           dynamic_cast<const CylinderVolumeBounds*>(&volume->volumeBounds());
1424       BOOST_CHECK_EQUAL(newBounds->get(CylinderVolumeBounds::eMinR),
1425                         origCylBounds.get(CylinderVolumeBounds::eMinR));
1426       BOOST_CHECK_EQUAL(newBounds->get(CylinderVolumeBounds::eMaxR),
1427                         origCylBounds.get(CylinderVolumeBounds::eMaxR));
1428       BOOST_CHECK_EQUAL(newBounds->get(CylinderVolumeBounds::eHalfLengthZ),
1429                         origCylBounds.get(CylinderVolumeBounds::eHalfLengthZ));
1430       BOOST_CHECK_EQUAL(volume->localToGlobalTransform(gctx).matrix(),
1431                         base.matrix());
1432     }
1433   };
1434 
1435   auto assertOriginalBounds = [&]() {
1436     const auto* cylBounds =
1437         dynamic_cast<const CylinderVolumeBounds*>(&cylStack->volumeBounds());
1438     BOOST_REQUIRE(cylBounds != nullptr);
1439     BOOST_CHECK_EQUAL(cylBounds, originalOuterBounds);
1440     BOOST_CHECK_EQUAL(cylBounds->get(CylinderVolumeBounds::eMinR), 100_mm);
1441     BOOST_CHECK_EQUAL(cylBounds->get(CylinderVolumeBounds::eMaxR), 900_mm);
1442     BOOST_CHECK_EQUAL(cylBounds->get(CylinderVolumeBounds::eHalfLengthZ), hlZ);
1443   };
1444 
1445   assertOriginalBounds();
1446 
1447   {
1448     // Assign a copy of the identical bounds gives identical bounds
1449     auto bounds = std::make_shared<CylinderVolumeBounds>(
1450         dynamic_cast<const CylinderVolumeBounds&>(cylStack->volumeBounds()));
1451     cylStack->update(gctx, bounds, std::nullopt, *logger);
1452     assertOriginalBounds();
1453   }
1454 
1455   {
1456     // Cannot increase mininmum r
1457     auto bounds = std::make_shared<CylinderVolumeBounds>(
1458         dynamic_cast<const CylinderVolumeBounds&>(cylStack->volumeBounds()));
1459     bounds->set(CylinderVolumeBounds::eMinR, 200_mm);
1460     BOOST_CHECK_THROW(cylStack->update(gctx, bounds, std::nullopt, *logger),
1461                       std::invalid_argument);
1462     assertOriginalBounds();
1463   }
1464 
1465   {
1466     // Cannot decrease maximum r
1467     auto bounds = std::make_shared<CylinderVolumeBounds>(
1468         dynamic_cast<const CylinderVolumeBounds&>(cylStack->volumeBounds()));
1469     bounds->set(CylinderVolumeBounds::eMaxR, 500_mm);
1470     BOOST_CHECK_THROW(cylStack->update(gctx, bounds, std::nullopt, *logger),
1471                       std::invalid_argument);
1472     assertOriginalBounds();
1473   }
1474 
1475   {
1476     // Cannot decrease half length z
1477     auto bounds = std::make_shared<CylinderVolumeBounds>(
1478         dynamic_cast<const CylinderVolumeBounds&>(cylStack->volumeBounds()));
1479     bounds->set(CylinderVolumeBounds::eHalfLengthZ, 0.5 * hlZ);
1480     BOOST_CHECK_THROW(cylStack->update(gctx, bounds, std::nullopt, *logger),
1481                       std::invalid_argument);
1482     assertOriginalBounds();
1483   }
1484 
1485   {
1486     // Reduce minimum r
1487     auto bounds = std::make_shared<CylinderVolumeBounds>(
1488         dynamic_cast<const CylinderVolumeBounds&>(cylStack->volumeBounds()));
1489     bounds->set(CylinderVolumeBounds::eMinR, 50_mm);
1490     cylStack->update(gctx, bounds, std::nullopt, *logger);
1491     const auto* cylBounds =
1492         dynamic_cast<const CylinderVolumeBounds*>(&cylStack->volumeBounds());
1493     BOOST_REQUIRE(cylBounds != nullptr);
1494     BOOST_CHECK_EQUAL(cylBounds->get(CylinderVolumeBounds::eMinR), 50_mm);
1495     // Rest unchanged
1496     BOOST_CHECK_EQUAL(cylBounds->get(CylinderVolumeBounds::eMaxR), 900_mm);
1497     BOOST_CHECK_EQUAL(cylBounds->get(CylinderVolumeBounds::eHalfLengthZ), hlZ);
1498 
1499     if (strategy == VolumeResizeStrategy::Expand) {
1500       // No gap volumes were added
1501       BOOST_CHECK_EQUAL(volumes.size(), 3);
1502 
1503       // Innermost volume reduced r size
1504       const auto* newBounds1 =
1505           dynamic_cast<const CylinderVolumeBounds*>(&vol1->volumeBounds());
1506       BOOST_CHECK_EQUAL(newBounds1->get(CylinderVolumeBounds::eMinR), 50_mm);
1507       // Position stayed the same
1508       BOOST_CHECK_EQUAL(vol1->localToGlobalTransform(gctx).matrix(),
1509                         base.matrix());
1510 
1511       // Other volumes are unchanged
1512       const auto* newBounds2 =
1513           dynamic_cast<const CylinderVolumeBounds*>(&vol2->volumeBounds());
1514       BOOST_CHECK_EQUAL(*newBounds2, originalBounds[1]);
1515       BOOST_CHECK_EQUAL(vol2->localToGlobalTransform(gctx).matrix(),
1516                         base.matrix());
1517 
1518       const auto* newBounds3 =
1519           dynamic_cast<const CylinderVolumeBounds*>(&vol3->volumeBounds());
1520       BOOST_CHECK_EQUAL(*newBounds3, originalBounds[2]);
1521       BOOST_CHECK_EQUAL(vol3->localToGlobalTransform(gctx).matrix(),
1522                         base.matrix());
1523 
1524     } else if (strategy == VolumeResizeStrategy::Gap) {
1525       // One gap volume was added
1526       BOOST_CHECK_EQUAL(volumes.size(), 4);
1527 
1528       auto gap = volumes.front();
1529       auto gapBounds =
1530           dynamic_cast<const CylinderVolumeBounds*>(&gap->volumeBounds());
1531       BOOST_REQUIRE(gapBounds != nullptr);
1532       BOOST_CHECK_EQUAL(gapBounds->get(CylinderVolumeBounds::eMinR), 50_mm);
1533       BOOST_CHECK_EQUAL(gapBounds->get(CylinderVolumeBounds::eMaxR), 100_mm);
1534       BOOST_CHECK_EQUAL(gapBounds->get(CylinderVolumeBounds::eHalfLengthZ),
1535                         hlZ);
1536       BOOST_CHECK_EQUAL(gap->localToGlobalTransform(gctx).matrix(),
1537                         base.matrix());
1538 
1539       // Other volumes are unchanged
1540       assertInitialVolumesUnchanged();
1541     }
1542   }
1543 
1544   resetCylStack();
1545 
1546   {
1547     // Increase maximum r
1548     auto bounds = std::make_shared<CylinderVolumeBounds>(
1549         dynamic_cast<const CylinderVolumeBounds&>(cylStack->volumeBounds()));
1550     bounds->set(CylinderVolumeBounds::eMaxR, 1000_mm);
1551     cylStack->update(gctx, bounds, std::nullopt, *logger);
1552     const auto* cylBounds =
1553         dynamic_cast<const CylinderVolumeBounds*>(&cylStack->volumeBounds());
1554     BOOST_REQUIRE(cylBounds != nullptr);
1555     BOOST_CHECK_EQUAL(cylBounds->get(CylinderVolumeBounds::eMaxR), 1000_mm);
1556     // Rest as before
1557     BOOST_CHECK_EQUAL(cylBounds->get(CylinderVolumeBounds::eMinR), 100_mm);
1558     BOOST_CHECK_EQUAL(cylBounds->get(CylinderVolumeBounds::eHalfLengthZ), hlZ);
1559 
1560     if (strategy == VolumeResizeStrategy::Expand) {
1561       // No gap volumes were added
1562       BOOST_CHECK_EQUAL(volumes.size(), 3);
1563 
1564       // Outermost volume increased r size
1565       const auto* newBounds3 =
1566           dynamic_cast<const CylinderVolumeBounds*>(&vol3->volumeBounds());
1567       BOOST_CHECK_EQUAL(newBounds3->get(CylinderVolumeBounds::eMaxR), 1000_mm);
1568       // Position stayed the same
1569       BOOST_CHECK_EQUAL(vol3->localToGlobalTransform(gctx).matrix(),
1570                         base.matrix());
1571 
1572       // Other volumes are unchanged
1573       const auto* newBounds1 =
1574           dynamic_cast<const CylinderVolumeBounds*>(&vol1->volumeBounds());
1575       BOOST_CHECK_EQUAL(*newBounds1, originalBounds[0]);
1576       BOOST_CHECK_EQUAL(vol1->localToGlobalTransform(gctx).matrix(),
1577                         base.matrix());
1578 
1579       const auto* newBounds2 =
1580           dynamic_cast<const CylinderVolumeBounds*>(&vol2->volumeBounds());
1581       BOOST_CHECK_EQUAL(*newBounds2, originalBounds[1]);
1582       BOOST_CHECK_EQUAL(vol2->localToGlobalTransform(gctx).matrix(),
1583                         base.matrix());
1584 
1585     } else if (strategy == VolumeResizeStrategy::Gap) {
1586       // One gap volume was added
1587       BOOST_CHECK_EQUAL(volumes.size(), 4);
1588 
1589       auto gap = volumes.back();
1590       auto gapBounds =
1591           dynamic_cast<const CylinderVolumeBounds*>(&gap->volumeBounds());
1592       BOOST_REQUIRE(gapBounds != nullptr);
1593       BOOST_CHECK_EQUAL(gapBounds->get(CylinderVolumeBounds::eMinR), 900_mm);
1594       BOOST_CHECK_EQUAL(gapBounds->get(CylinderVolumeBounds::eMaxR), 1000_mm);
1595       BOOST_CHECK_EQUAL(gapBounds->get(CylinderVolumeBounds::eHalfLengthZ),
1596                         hlZ);
1597       BOOST_CHECK_EQUAL(gap->localToGlobalTransform(gctx).matrix(),
1598                         base.matrix());
1599 
1600       // Other volumes are unchanged
1601       assertInitialVolumesUnchanged();
1602     }
1603   }
1604 
1605   resetCylStack();
1606 
1607   {
1608     // Decrease r min and increase r max
1609     auto bounds = std::make_shared<CylinderVolumeBounds>(
1610         dynamic_cast<const CylinderVolumeBounds&>(cylStack->volumeBounds()));
1611     bounds->set({
1612         {CylinderVolumeBounds::eMinR, 0_mm},
1613         {CylinderVolumeBounds::eMaxR, 1100_mm},
1614     });
1615 
1616     cylStack->update(gctx, bounds, std::nullopt, *logger);
1617     const auto* cylBounds =
1618         dynamic_cast<const CylinderVolumeBounds*>(&cylStack->volumeBounds());
1619     BOOST_REQUIRE(cylBounds != nullptr);
1620     BOOST_CHECK_EQUAL(cylBounds->get(CylinderVolumeBounds::eMaxR), 1100_mm);
1621     // Rest as before
1622     BOOST_CHECK_EQUAL(cylBounds->get(CylinderVolumeBounds::eMinR), 0_mm);
1623     BOOST_CHECK_EQUAL(cylBounds->get(CylinderVolumeBounds::eHalfLengthZ), hlZ);
1624 
1625     if (strategy == VolumeResizeStrategy::Expand) {
1626       // No gap volumes were added
1627       BOOST_CHECK_EQUAL(volumes.size(), 3);
1628 
1629       // Innermost volume reduced r size
1630       const auto* newBounds1 =
1631           dynamic_cast<const CylinderVolumeBounds*>(&vol1->volumeBounds());
1632       BOOST_CHECK_EQUAL(newBounds1->get(CylinderVolumeBounds::eMinR), 0_mm);
1633       // Position stayed the same
1634       BOOST_CHECK_EQUAL(vol1->localToGlobalTransform(gctx).matrix(),
1635                         base.matrix());
1636 
1637       // Middle volume is unchanged
1638       const auto* newBounds2 =
1639           dynamic_cast<const CylinderVolumeBounds*>(&vol2->volumeBounds());
1640       BOOST_CHECK_EQUAL(*newBounds2, originalBounds[1]);
1641       BOOST_CHECK_EQUAL(vol2->localToGlobalTransform(gctx).matrix(),
1642                         base.matrix());
1643 
1644       // Outermost volume increased r size
1645       const auto* newBounds3 =
1646           dynamic_cast<const CylinderVolumeBounds*>(&vol3->volumeBounds());
1647       BOOST_CHECK_EQUAL(newBounds3->get(CylinderVolumeBounds::eMaxR), 1100_mm);
1648       // Position stayed the same
1649       BOOST_CHECK_EQUAL(vol3->localToGlobalTransform(gctx).matrix(),
1650                         base.matrix());
1651 
1652     } else if (strategy == VolumeResizeStrategy::Gap) {
1653       // One gap volume was added
1654       BOOST_CHECK_EQUAL(volumes.size(), 5);
1655 
1656       auto gap1 = volumes.front();
1657       auto gapBounds1 =
1658           dynamic_cast<const CylinderVolumeBounds*>(&gap1->volumeBounds());
1659       BOOST_REQUIRE(gapBounds1 != nullptr);
1660       BOOST_CHECK_EQUAL(gapBounds1->get(CylinderVolumeBounds::eMinR), 0_mm);
1661       BOOST_CHECK_EQUAL(gapBounds1->get(CylinderVolumeBounds::eMaxR), 100_mm);
1662       BOOST_CHECK_EQUAL(gapBounds1->get(CylinderVolumeBounds::eHalfLengthZ),
1663                         hlZ);
1664       BOOST_CHECK_EQUAL(gap1->localToGlobalTransform(gctx).matrix(),
1665                         base.matrix());
1666 
1667       auto gap2 = volumes.back();
1668       auto gapBounds2 =
1669           dynamic_cast<const CylinderVolumeBounds*>(&gap2->volumeBounds());
1670       BOOST_REQUIRE(gapBounds2 != nullptr);
1671       BOOST_CHECK_EQUAL(gapBounds2->get(CylinderVolumeBounds::eMinR), 900_mm);
1672       BOOST_CHECK_EQUAL(gapBounds2->get(CylinderVolumeBounds::eMaxR), 1100_mm);
1673       BOOST_CHECK_EQUAL(gapBounds2->get(CylinderVolumeBounds::eHalfLengthZ),
1674                         hlZ);
1675 
1676       // Other volumes are unchanged
1677       assertInitialVolumesUnchanged();
1678     }
1679   }
1680 
1681   resetCylStack();
1682 
1683   {
1684     // Increase half length z
1685     auto bounds = std::make_shared<CylinderVolumeBounds>(
1686         dynamic_cast<const CylinderVolumeBounds&>(cylStack->volumeBounds()));
1687     bounds->set(CylinderVolumeBounds::eHalfLengthZ, 2 * hlZ);
1688     cylStack->update(gctx, bounds, std::nullopt, *logger);
1689     const auto* cylBounds =
1690         dynamic_cast<const CylinderVolumeBounds*>(&cylStack->volumeBounds());
1691     BOOST_REQUIRE(cylBounds != nullptr);
1692     BOOST_CHECK_EQUAL(cylBounds->get(CylinderVolumeBounds::eHalfLengthZ),
1693                       2 * hlZ);
1694 
1695     // Rest as before
1696     BOOST_CHECK_EQUAL(cylBounds->get(CylinderVolumeBounds::eMinR), 100_mm);
1697     BOOST_CHECK_EQUAL(cylBounds->get(CylinderVolumeBounds::eMaxR), 900_mm);
1698 
1699     // No gap volumes were added
1700     BOOST_CHECK_EQUAL(volumes.size(), 3);
1701 
1702     for (const auto& [volume, origCylBounds] :
1703          zip(originalVolumes, originalBounds)) {
1704       const auto* newBounds =
1705           dynamic_cast<const CylinderVolumeBounds*>(&volume->volumeBounds());
1706       // Radii are all as before
1707       BOOST_CHECK_EQUAL(newBounds->get(CylinderVolumeBounds::eMinR),
1708                         origCylBounds.get(CylinderVolumeBounds::eMinR));
1709       BOOST_CHECK_EQUAL(newBounds->get(CylinderVolumeBounds::eMaxR),
1710                         origCylBounds.get(CylinderVolumeBounds::eMaxR));
1711 
1712       // Half length z is changed on all
1713       BOOST_CHECK_EQUAL(newBounds->get(CylinderVolumeBounds::eHalfLengthZ),
1714                         2 * hlZ);
1715 
1716       // Position stayed the same
1717       BOOST_CHECK_EQUAL(volume->localToGlobalTransform(gctx).matrix(),
1718                         base.matrix());
1719     }
1720   }
1721 }
1722 
1723 BOOST_DATA_TEST_CASE(
1724     UpdateStackOneSided,
1725     (boost::unit_test::data::make(-1.0, 1.0) ^
1726      boost::unit_test::data::make(VolumeResizeStrategy::Gap,
1727                                   VolumeResizeStrategy::Expand)),
1728     f, strategy) {
1729   const auto gctx = Acts::GeometryContext::dangerouslyDefaultConstruct();
1730   // Strategy should not affect the sizing here at all
1731 
1732   auto trf = Transform3::Identity();
1733 
1734   auto vol1 = std::make_shared<Volume>(
1735       trf, std::make_shared<CylinderVolumeBounds>(100_mm, 300_mm, 400_mm));
1736 
1737   auto vol2 = std::make_shared<Volume>(
1738       trf, std::make_shared<CylinderVolumeBounds>(400_mm, 600_mm, 400_mm));
1739 
1740   std::vector<Volume*> volumes = {vol1.get(), vol2.get()};
1741 
1742   CylinderVolumeStack cylStack{
1743       gctx,     volumes, AxisDirection::AxisR, VolumeAttachmentStrategy::Gap,
1744       strategy, *logger};
1745   const auto* originalBounds =
1746       dynamic_cast<const CylinderVolumeBounds*>(&cylStack.volumeBounds());
1747 
1748   // Increase halflength by 50mm
1749   auto newBounds = std::make_shared<CylinderVolumeBounds>(
1750       dynamic_cast<const CylinderVolumeBounds&>(cylStack.volumeBounds()));
1751   newBounds->set(CylinderVolumeBounds::eHalfLengthZ, 450_mm);
1752   // Shift to +z by 50mm
1753   trf *= Translation3{Vector3{0_mm, 0_mm, f * 50_mm}};
1754   // -> left edge should stay at -400mm, right edge should be at 500mm
1755 
1756   auto checkUnchanged = [&]() {
1757     const auto* cylBounds =
1758         dynamic_cast<const CylinderVolumeBounds*>(&cylStack.volumeBounds());
1759     BOOST_REQUIRE(cylBounds != nullptr);
1760     BOOST_CHECK_EQUAL(*cylBounds, *originalBounds);
1761   };
1762 
1763   // Invalid: shift too far in z
1764   BOOST_CHECK_THROW(
1765       cylStack.update(gctx, newBounds,
1766                       trf * Translation3{Vector3{0, 0, f * 20_mm}}, *logger),
1767       std::invalid_argument);
1768   checkUnchanged();
1769 
1770   // Invalid: shift in x
1771   BOOST_CHECK_THROW(
1772       cylStack.update(gctx, newBounds, trf * Translation3{Vector3{10_mm, 0, 0}},
1773                       *logger),
1774       std::invalid_argument);
1775   checkUnchanged();
1776 
1777   // Invalid: shift in y
1778   BOOST_CHECK_THROW(
1779       cylStack.update(gctx, newBounds, trf * Translation3{Vector3{0, 10_mm, 0}},
1780                       *logger),
1781       std::invalid_argument);
1782   checkUnchanged();
1783 
1784   // Invalid: rotation
1785   BOOST_CHECK_THROW(
1786       cylStack.update(gctx, newBounds,
1787                       trf * AngleAxis3{10_degree, Vector3::UnitY()}, *logger),
1788       std::invalid_argument);
1789   checkUnchanged();
1790 
1791   cylStack.update(gctx, newBounds, trf, *logger);
1792 
1793   BOOST_CHECK_EQUAL(cylStack.localToGlobalTransform(gctx).matrix(),
1794                     trf.matrix());
1795   const auto* cylBounds =
1796       dynamic_cast<const CylinderVolumeBounds*>(&cylStack.volumeBounds());
1797   BOOST_REQUIRE(cylBounds != nullptr);
1798   BOOST_CHECK_EQUAL(cylBounds->get(CylinderVolumeBounds::eMinR), 100_mm);
1799   BOOST_CHECK_EQUAL(cylBounds->get(CylinderVolumeBounds::eMaxR), 600_mm);
1800 
1801   // All volumes including gaps should have same new position and halflength
1802   for (const auto* vol : volumes) {
1803     const auto* volBounds =
1804         dynamic_cast<const CylinderVolumeBounds*>(&vol->volumeBounds());
1805     BOOST_REQUIRE(volBounds != nullptr);
1806     BOOST_CHECK_EQUAL(vol->localToGlobalTransform(gctx).matrix(), trf.matrix());
1807     BOOST_CHECK_EQUAL(volBounds->get(CylinderVolumeBounds::eHalfLengthZ),
1808                       450_mm);
1809   }
1810 }
1811 
1812 BOOST_AUTO_TEST_CASE(ResizeGapMultiple) {
1813   const auto gctx = Acts::GeometryContext::dangerouslyDefaultConstruct();
1814   Transform3 trf = Transform3::Identity();
1815   auto bounds = std::make_shared<CylinderVolumeBounds>(100, 200, 100);
1816   Volume vol{trf, bounds};
1817 
1818   BOOST_TEST_CONTEXT("Outer") {
1819     std::vector<Volume*> volumes = {&vol};
1820     CylinderVolumeStack stack(gctx, volumes, AxisDirection::AxisR,
1821                               VolumeAttachmentStrategy::Gap,
1822                               VolumeResizeStrategy::Gap, *logger);
1823 
1824     BOOST_CHECK_EQUAL(volumes.size(), 1);
1825     BOOST_CHECK(stack.gaps().empty());
1826 
1827     stack.update(gctx, std::make_shared<CylinderVolumeBounds>(100, 250, 100),
1828                  trf, *logger);
1829     BOOST_CHECK_EQUAL(volumes.size(), 2);
1830     BOOST_CHECK_EQUAL(stack.gaps().size(), 1);
1831 
1832     const auto* cylBounds = dynamic_cast<const CylinderVolumeBounds*>(
1833         &stack.gaps().front()->volumeBounds());
1834     BOOST_REQUIRE_NE(cylBounds, nullptr);
1835     BOOST_CHECK_EQUAL(cylBounds->get(CylinderVolumeBounds::eMinR), 200);
1836     BOOST_CHECK_EQUAL(cylBounds->get(CylinderVolumeBounds::eMaxR), 250);
1837 
1838     stack.update(gctx, std::make_shared<CylinderVolumeBounds>(100, 300, 100),
1839                  trf, *logger);
1840 
1841     BOOST_CHECK_EQUAL(volumes.size(), 2);
1842     // No additional gap volume was added!
1843     BOOST_CHECK_EQUAL(stack.gaps().size(), 1);
1844 
1845     cylBounds = dynamic_cast<const CylinderVolumeBounds*>(
1846         &stack.gaps().front()->volumeBounds());
1847     BOOST_REQUIRE_NE(cylBounds, nullptr);
1848     BOOST_CHECK_EQUAL(cylBounds->get(CylinderVolumeBounds::eMinR), 200);
1849     BOOST_CHECK_EQUAL(cylBounds->get(CylinderVolumeBounds::eMaxR), 300);
1850   }
1851 
1852   BOOST_TEST_CONTEXT("Inner") {
1853     std::vector<Volume*> volumes = {&vol};
1854     CylinderVolumeStack stack(gctx, volumes, AxisDirection::AxisR,
1855                               VolumeAttachmentStrategy::Gap,
1856                               VolumeResizeStrategy::Gap, *logger);
1857 
1858     BOOST_CHECK_EQUAL(volumes.size(), 1);
1859     BOOST_CHECK(stack.gaps().empty());
1860 
1861     stack.update(gctx, std::make_shared<CylinderVolumeBounds>(50, 200, 100),
1862                  trf, *logger);
1863     BOOST_CHECK_EQUAL(volumes.size(), 2);
1864     BOOST_CHECK_EQUAL(stack.gaps().size(), 1);
1865 
1866     const auto* cylBounds = dynamic_cast<const CylinderVolumeBounds*>(
1867         &stack.gaps().front()->volumeBounds());
1868     BOOST_REQUIRE_NE(cylBounds, nullptr);
1869     BOOST_CHECK_EQUAL(cylBounds->get(CylinderVolumeBounds::eMinR), 50);
1870     BOOST_CHECK_EQUAL(cylBounds->get(CylinderVolumeBounds::eMaxR), 100);
1871 
1872     stack.update(gctx, std::make_shared<CylinderVolumeBounds>(0, 200, 100), trf,
1873                  *logger);
1874 
1875     BOOST_CHECK_EQUAL(volumes.size(), 2);
1876     // No additional gap volume was added!
1877     BOOST_CHECK_EQUAL(stack.gaps().size(), 1);
1878 
1879     cylBounds = dynamic_cast<const CylinderVolumeBounds*>(
1880         &stack.gaps().front()->volumeBounds());
1881     BOOST_REQUIRE_NE(cylBounds, nullptr);
1882     BOOST_CHECK_EQUAL(cylBounds->get(CylinderVolumeBounds::eMinR), 0);
1883     BOOST_CHECK_EQUAL(cylBounds->get(CylinderVolumeBounds::eMaxR), 100);
1884   }
1885 }
1886 
1887 BOOST_AUTO_TEST_SUITE_END()
1888 
1889 BOOST_AUTO_TEST_SUITE(Common)
1890 
1891 BOOST_DATA_TEST_CASE(JoinCylinderVolumesInvalidDirection,
1892                      boost::unit_test::data::make(strategies), strategy) {
1893   const auto gctx = Acts::GeometryContext::dangerouslyDefaultConstruct();
1894   std::vector<Volume*> volumes;
1895   auto vol1 = std::make_shared<Volume>(
1896       Transform3::Identity(),
1897       std::make_shared<CylinderVolumeBounds>(100_mm, 400_mm, 400_mm));
1898   volumes.push_back(vol1.get());
1899 
1900   // Single volume invalid direction still gives an error
1901   BOOST_CHECK_THROW(
1902       CylinderVolumeStack(gctx, volumes, AxisDirection::AxisY, strategy),
1903       std::invalid_argument);
1904 
1905   auto vol2 = std::make_shared<Volume>(
1906       Transform3::Identity(),
1907       std::make_shared<CylinderVolumeBounds>(100_mm, 400_mm, 400_mm));
1908   volumes.push_back(vol2.get());
1909 
1910   BOOST_CHECK_THROW(
1911       CylinderVolumeStack(gctx, volumes, AxisDirection::AxisY, strategy),
1912       std::invalid_argument);
1913 }
1914 
1915 BOOST_DATA_TEST_CASE(JoinCylinderVolumesInvalidInput,
1916                      (boost::unit_test::data::make(strategies) *
1917                       boost::unit_test::data::make(AxisDirection::AxisZ,
1918                                                    AxisDirection::AxisR)),
1919                      strategy, direction) {
1920   const auto gctx = Acts::GeometryContext::dangerouslyDefaultConstruct();
1921   BOOST_TEST_CONTEXT("Empty Volume") {
1922     std::vector<Volume*> volumes;
1923     BOOST_CHECK_THROW(CylinderVolumeStack(gctx, volumes, direction, strategy),
1924                       std::invalid_argument);
1925   }
1926 
1927   BOOST_TEST_CONTEXT("Volumes rotated relative to each other") {
1928     // At this time, all rotations are considered invalid, even around z
1929     for (const Vector3 axis : {Vector3::UnitX(), Vector3::UnitY()}) {
1930       std::vector<Volume*> volumes;
1931       auto vol1 = std::make_shared<Volume>(
1932           Transform3{Translation3{Vector3{0_mm, 0_mm, -500_mm}}},
1933           std::make_shared<CylinderVolumeBounds>(100_mm, 400_mm, 400_mm));
1934       volumes.push_back(vol1.get());
1935 
1936       BOOST_TEST_MESSAGE("Axis: " << axis);
1937       auto vol2 = std::make_shared<Volume>(
1938           Transform3{Translation3{Vector3{0_mm, 0_mm, 500_mm}} *
1939                      AngleAxis3(1_degree, axis)},
1940           std::make_shared<CylinderVolumeBounds>(100_mm, 400_mm, 400_mm));
1941       volumes.push_back(vol2.get());
1942 
1943       BOOST_CHECK_THROW(CylinderVolumeStack(gctx, volumes, direction, strategy,
1944                                             VolumeResizeStrategy::Gap, *logger),
1945                         std::invalid_argument);
1946     }
1947   }
1948 
1949   BOOST_TEST_CONTEXT("Volumes shifted in the xy plane relative to each other") {
1950     for (const Vector3& shift :
1951          {Vector3{5_mm, 0, 0}, Vector3{0, -5_mm, 0}, Vector3{2_mm, -2_mm, 0}}) {
1952       std::vector<Volume*> volumes;
1953       auto vol1 = std::make_shared<Volume>(
1954           Transform3{Translation3{Vector3{0_mm, 0_mm, -500_mm}}},
1955           std::make_shared<CylinderVolumeBounds>(100_mm, 400_mm, 400_mm));
1956       volumes.push_back(vol1.get());
1957 
1958       auto vol2 = std::make_shared<Volume>(
1959           Transform3{Translation3{Vector3{0_mm, 0_mm, 500_mm} + shift}},
1960           std::make_shared<CylinderVolumeBounds>(100_mm, 400_mm, 400_mm));
1961       volumes.push_back(vol2.get());
1962 
1963       BOOST_CHECK_THROW(CylinderVolumeStack(gctx, volumes, direction, strategy,
1964                                             VolumeResizeStrategy::Gap, *logger),
1965                         std::invalid_argument);
1966     }
1967   }
1968 
1969   BOOST_TEST_CONTEXT("Volume has phi values") {
1970     std::vector<std::shared_ptr<CylinderVolumeBounds>> invalidVolumeBounds = {
1971         std::make_shared<CylinderVolumeBounds>(100_mm, 400_mm, 400_mm,
1972                                                0.2 * std::numbers::pi),
1973 
1974         std::make_shared<CylinderVolumeBounds>(
1975             100_mm, 400_mm, 400_mm, std::numbers::pi, 0.3 * std::numbers::pi),
1976     };
1977 
1978     for (const auto& invalid : invalidVolumeBounds) {
1979       std::stringstream ss;
1980       ss << "Invalid bounds: " << *invalid;
1981       BOOST_TEST_CONTEXT(ss.str()) {
1982         std::vector<Volume*> volumes;
1983         auto vol1 = std::make_shared<Volume>(
1984             Transform3{Translation3{Vector3{0_mm, 0_mm, -500_mm}}},
1985             std::make_shared<CylinderVolumeBounds>(100_mm, 400_mm, 400_mm));
1986         volumes.push_back(vol1.get());
1987 
1988         {
1989           // have valid stack, try to assign extra
1990           CylinderVolumeStack cylStack(gctx, volumes, direction, strategy,
1991                                        VolumeResizeStrategy::Gap, *logger);
1992           BOOST_CHECK_THROW(
1993               cylStack.update(gctx, invalid, std::nullopt, *logger),
1994               std::invalid_argument);
1995         }
1996 
1997         {
1998           std::shared_ptr<Volume> vol;
1999           if (direction == AxisDirection::AxisZ) {
2000             vol = std::make_shared<Volume>(
2001                 Transform3{Translation3{Vector3{0_mm, 0_mm, 500_mm}}}, invalid);
2002           } else {
2003             invalid->set({
2004                 {CylinderVolumeBounds::eMinR, 400_mm},
2005                 {CylinderVolumeBounds::eMaxR, 600_mm},
2006             });
2007             vol = std::make_shared<Volume>(
2008                 Transform3{Translation3{Vector3{0_mm, 0_mm, 0_mm}}}, invalid);
2009           }
2010           volumes.push_back(vol.get());
2011           BOOST_CHECK_THROW(
2012               CylinderVolumeStack(gctx, volumes, direction, strategy,
2013                                   VolumeResizeStrategy::Gap, *logger),
2014               std::invalid_argument);
2015         }
2016       }
2017     }
2018   }
2019 }
2020 
2021 BOOST_DATA_TEST_CASE(JoinCylinderVolumeSingle,
2022                      (boost::unit_test::data::make(AxisDirection::AxisZ,
2023                                                    AxisDirection::AxisR) *
2024                       boost::unit_test::data::make(strategies)),
2025                      direction, strategy) {
2026   const auto gctx = Acts::GeometryContext::dangerouslyDefaultConstruct();
2027   auto vol = std::make_shared<Volume>(
2028       Transform3::Identity() * Translation3{14_mm, 24_mm, 0_mm} *
2029           AngleAxis3(73_degree, Vector3::UnitX()),
2030       std::make_shared<CylinderVolumeBounds>(100_mm, 400_mm, 400_mm));
2031 
2032   std::vector<Volume*> volumes{vol.get()};
2033 
2034   CylinderVolumeStack cylStack(gctx, volumes, direction, strategy,
2035                                VolumeResizeStrategy::Gap, *logger);
2036 
2037   // Cylinder stack has the same transform as bounds as the single input
2038   // volume
2039   BOOST_CHECK_EQUAL(volumes.size(), 1);
2040   BOOST_CHECK_EQUAL(volumes.at(0), vol.get());
2041   BOOST_CHECK_EQUAL(vol->localToGlobalTransform(gctx).matrix(),
2042                     cylStack.localToGlobalTransform(gctx).matrix());
2043   BOOST_CHECK_EQUAL(vol->volumeBounds(), cylStack.volumeBounds());
2044 }
2045 
2046 BOOST_AUTO_TEST_SUITE_END()
2047 BOOST_AUTO_TEST_SUITE_END()
2048 BOOST_AUTO_TEST_CASE(AsymmetricResizeZ) {
2049   const auto gctx = Acts::GeometryContext::dangerouslyDefaultConstruct();
2050   double hlZ = 400_mm;
2051   double rMin = 100_mm;
2052   double rMax = 200_mm;
2053 
2054   // Create three cylinder volumes stacked in z
2055   auto bounds1 = std::make_shared<CylinderVolumeBounds>(rMin, rMax, hlZ);
2056   auto bounds2 = std::make_shared<CylinderVolumeBounds>(rMin, rMax, hlZ);
2057   auto bounds3 = std::make_shared<CylinderVolumeBounds>(rMin, rMax, hlZ);
2058 
2059   Transform3 transform1 = Transform3::Identity();
2060   transform1.translate(Vector3{0_mm, 0_mm, -2 * hlZ});
2061   auto vol1 = std::make_shared<Volume>(transform1, bounds1);
2062 
2063   Transform3 transform2 = Transform3::Identity();
2064   transform2.translate(Vector3{0_mm, 0_mm, 0_mm});
2065   auto vol2 = std::make_shared<Volume>(transform2, bounds2);
2066 
2067   Transform3 transform3 = Transform3::Identity();
2068   transform3.translate(Vector3{0_mm, 0_mm, 2 * hlZ});
2069   auto vol3 = std::make_shared<Volume>(transform3, bounds3);
2070 
2071   std::vector<Volume*> volumes = {vol1.get(), vol2.get(), vol3.get()};
2072   // Test with Gap for negative z and Expand for positive z
2073   CylinderVolumeStack cylStack(
2074       gctx, volumes, AxisDirection::AxisZ, VolumeAttachmentStrategy::Gap,
2075       {VolumeResizeStrategy::Gap, VolumeResizeStrategy::Expand}, *logger);
2076   // Initial stack spans [-3*hlZ, 3*hlZ]. Update bounds to test asymmetric
2077   // resize in z only New bounds should span [-4*hlZ, 4*hlZ] to ensure we only
2078   // grow
2079   auto newBounds = std::make_shared<CylinderVolumeBounds>(rMin, rMax, 4 * hlZ);
2080   Transform3 newTransform =
2081       Transform3::Identity() * Translation3{0_mm, 0_mm, 0_mm};
2082 
2083   cylStack.update(gctx, newBounds, newTransform, *logger);
2084 
2085   // Check that we have one gap volume at negative z
2086   BOOST_CHECK_EQUAL(volumes.size(), 4);  // Original 3 + 1 gap volume
2087 
2088   // Check gap volume at negative z
2089   auto gapVol = volumes.front();
2090   auto gapBounds =
2091       dynamic_cast<const CylinderVolumeBounds*>(&gapVol->volumeBounds());
2092   BOOST_REQUIRE(gapBounds != nullptr);
2093   BOOST_CHECK_EQUAL(
2094       gapBounds->get(CylinderVolumeBounds::eHalfLengthZ),
2095       hlZ / 2);  // Half the original half-length to fill 1*hlZ gap
2096   BOOST_CHECK_EQUAL(gapBounds->get(CylinderVolumeBounds::eMinR), rMin);
2097   BOOST_CHECK_EQUAL(gapBounds->get(CylinderVolumeBounds::eMaxR), rMax);
2098   BOOST_CHECK_CLOSE(gapVol->center(gctx)[eZ], -3.5 * hlZ,
2099                     1e-10);  // Center of [-4*hlZ, -3*hlZ]
2100 
2101   // Check that last volume was expanded in positive z
2102   auto* lastVol = volumes.back();
2103   BOOST_CHECK_EQUAL(lastVol, vol3.get());
2104   auto lastBounds =
2105       dynamic_cast<const CylinderVolumeBounds*>(&lastVol->volumeBounds());
2106   BOOST_REQUIRE(lastBounds != nullptr);
2107   BOOST_CHECK_EQUAL(lastBounds->get(CylinderVolumeBounds::eHalfLengthZ),
2108                     1.5 * hlZ);  // Original hlZ plus 0.5*hlZ expansion
2109   BOOST_CHECK_EQUAL(lastBounds->get(CylinderVolumeBounds::eMinR), rMin);
2110   BOOST_CHECK_EQUAL(lastBounds->get(CylinderVolumeBounds::eMaxR), rMax);
2111   BOOST_CHECK_CLOSE(lastVol->center(gctx)[eZ], 2.5 * hlZ,
2112                     1e-10);  // Center of [2*hlZ, 3*hlZ]
2113 
2114   // Check middle volumes maintain their size
2115   for (std::size_t i = 1; i < volumes.size() - 1; i++) {
2116     auto volBounds =
2117         dynamic_cast<const CylinderVolumeBounds*>(&volumes[i]->volumeBounds());
2118     BOOST_REQUIRE(volBounds != nullptr);
2119     BOOST_CHECK_EQUAL(volBounds->get(CylinderVolumeBounds::eHalfLengthZ), hlZ);
2120     BOOST_CHECK_EQUAL(volBounds->get(CylinderVolumeBounds::eMinR), rMin);
2121     BOOST_CHECK_EQUAL(volBounds->get(CylinderVolumeBounds::eMaxR), rMax);
2122   }
2123   BOOST_CHECK_CLOSE(volumes[1]->center(gctx)[eZ], -2 * hlZ, 1e-10);
2124   BOOST_CHECK_CLOSE(volumes[2]->center(gctx)[eZ], 0, 1e-10);
2125 }
2126 
2127 BOOST_AUTO_TEST_CASE(AsymmetricResizeZFlipped) {
2128   const auto gctx = Acts::GeometryContext::dangerouslyDefaultConstruct();
2129 
2130   double hlZ = 400_mm;
2131   double rMin = 100_mm;
2132   double rMax = 200_mm;
2133 
2134   // Create three cylinder volumes stacked in z
2135   auto bounds1 = std::make_shared<CylinderVolumeBounds>(rMin, rMax, hlZ);
2136   auto bounds2 = std::make_shared<CylinderVolumeBounds>(rMin, rMax, hlZ);
2137   auto bounds3 = std::make_shared<CylinderVolumeBounds>(rMin, rMax, hlZ);
2138 
2139   Transform3 transform1 = Transform3::Identity() * Translation3(0, 0, -2 * hlZ);
2140   Transform3 transform2 = Transform3::Identity();
2141   Transform3 transform3 = Transform3::Identity() * Translation3(0, 0, 2 * hlZ);
2142 
2143   auto vol1 = std::make_shared<Volume>(transform1, bounds1);
2144   auto vol2 = std::make_shared<Volume>(transform2, bounds2);
2145   auto vol3 = std::make_shared<Volume>(transform3, bounds3);
2146 
2147   std::vector<Volume*> volumes = {vol1.get(), vol2.get(), vol3.get()};
2148   // Test with Expand for inner radius and Gap for outer radius
2149   CylinderVolumeStack cylStack(
2150       gctx, volumes, AxisDirection::AxisZ, VolumeAttachmentStrategy::Gap,
2151       {VolumeResizeStrategy::Expand, VolumeResizeStrategy::Gap}, *logger);
2152 
2153   // Update bounds to test asymmetric expansion
2154   auto newBounds = std::make_shared<CylinderVolumeBounds>(rMin, rMax, 4 * hlZ);
2155   cylStack.update(gctx, newBounds, std::nullopt, *logger);
2156   // Check that we have one gap volume at positive z
2157   BOOST_CHECK_EQUAL(volumes.size(), 4);  // Original 3 + 1 gap volume
2158 
2159   // Check gap volume at positive z
2160   auto gapVol = volumes.back();
2161   auto gapBounds =
2162       dynamic_cast<const CylinderVolumeBounds*>(&gapVol->volumeBounds());
2163   BOOST_REQUIRE(gapBounds != nullptr);
2164   BOOST_CHECK_EQUAL(
2165       gapBounds->get(CylinderVolumeBounds::eHalfLengthZ),
2166       hlZ / 2);  // Half the original half-length to fill 1*hlZ gap
2167   BOOST_CHECK_EQUAL(gapBounds->get(CylinderVolumeBounds::eMinR), rMin);
2168   BOOST_CHECK_EQUAL(gapBounds->get(CylinderVolumeBounds::eMaxR), rMax);
2169   BOOST_CHECK_CLOSE(gapVol->center(gctx)[eZ], 3.5 * hlZ,
2170                     1e-10);  // Center of [3*hlZ, 4*hlZ]
2171 
2172   // Check that first volume was expanded in positive z
2173   auto* firstVol = volumes.front();
2174   BOOST_CHECK_EQUAL(firstVol, vol1.get());
2175   auto firstBounds =
2176       dynamic_cast<const CylinderVolumeBounds*>(&firstVol->volumeBounds());
2177   BOOST_REQUIRE(firstBounds != nullptr);
2178   BOOST_CHECK_EQUAL(firstBounds->get(CylinderVolumeBounds::eHalfLengthZ),
2179                     1.5 * hlZ);  // Original hlZ plus 0.5*hlZ expansion
2180   BOOST_CHECK_EQUAL(firstBounds->get(CylinderVolumeBounds::eMinR), rMin);
2181   BOOST_CHECK_EQUAL(firstBounds->get(CylinderVolumeBounds::eMaxR), rMax);
2182   BOOST_CHECK_CLOSE(firstVol->center(gctx)[eZ], -2.5 * hlZ,
2183                     1e-10);  // Center of [-3*hlZ, -2*hlZ]
2184 
2185   // Check middle volumes maintain their size
2186   for (std::size_t i = 1; i < volumes.size() - 1; i++) {
2187     auto volBounds =
2188         dynamic_cast<const CylinderVolumeBounds*>(&volumes[i]->volumeBounds());
2189     BOOST_REQUIRE(volBounds != nullptr);
2190     BOOST_CHECK_EQUAL(volBounds->get(CylinderVolumeBounds::eHalfLengthZ), hlZ);
2191     BOOST_CHECK_EQUAL(volBounds->get(CylinderVolumeBounds::eMinR), rMin);
2192     BOOST_CHECK_EQUAL(volBounds->get(CylinderVolumeBounds::eMaxR), rMax);
2193   }
2194   BOOST_CHECK_CLOSE(volumes[1]->center(gctx)[eZ], 0, 1e-10);
2195   BOOST_CHECK_CLOSE(volumes[2]->center(gctx)[eZ], 2 * hlZ, 1e-10);
2196 }
2197 
2198 BOOST_AUTO_TEST_CASE(AsymmetricResizeR) {
2199   const auto gctx = Acts::GeometryContext::dangerouslyDefaultConstruct();
2200   double hlZ = 400_mm;
2201 
2202   // Create three cylinder volumes stacked in r with gaps
2203   auto bounds1 = std::make_shared<CylinderVolumeBounds>(100_mm, 200_mm, hlZ);
2204   auto bounds2 = std::make_shared<CylinderVolumeBounds>(200_mm, 300_mm, hlZ);
2205   auto bounds3 = std::make_shared<CylinderVolumeBounds>(300_mm, 400_mm, hlZ);
2206 
2207   Transform3 transform = Transform3::Identity();
2208   auto vol1 = std::make_shared<Volume>(transform, bounds1);
2209   auto vol2 = std::make_shared<Volume>(transform, bounds2);
2210   auto vol3 = std::make_shared<Volume>(transform, bounds3);
2211 
2212   std::vector<Volume*> volumes = {vol1.get(), vol2.get(), vol3.get()};
2213   // Test with Gap for inner radius and Expand for outer radius
2214   CylinderVolumeStack cylStack(
2215       gctx, volumes, AxisDirection::AxisR, VolumeAttachmentStrategy::Midpoint,
2216       {VolumeResizeStrategy::Gap, VolumeResizeStrategy::Expand}, *logger);
2217 
2218   // Update bounds to test asymmetric resize in r only
2219   auto newBounds = std::make_shared<CylinderVolumeBounds>(50_mm, 500_mm, hlZ);
2220   cylStack.update(gctx, newBounds, std::nullopt, *logger);
2221   // Check that we have one gap volume at inner radius
2222   BOOST_CHECK_EQUAL(volumes.size(), 4);  // Original 3 + 1 gap volume
2223 
2224   // Check gap volume at inner radius
2225   auto innerGap = volumes.front();
2226   auto innerGapBounds =
2227       dynamic_cast<const CylinderVolumeBounds*>(&innerGap->volumeBounds());
2228   BOOST_REQUIRE(innerGapBounds != nullptr);
2229   BOOST_CHECK_EQUAL(innerGapBounds->get(CylinderVolumeBounds::eMinR), 50_mm);
2230   BOOST_CHECK_EQUAL(innerGapBounds->get(CylinderVolumeBounds::eMaxR), 100_mm);
2231   BOOST_CHECK_EQUAL(innerGapBounds->get(CylinderVolumeBounds::eHalfLengthZ),
2232                     hlZ);
2233 
2234   // Check that outer volume was expanded
2235   auto* outerVol = volumes.back();
2236   BOOST_CHECK_EQUAL(outerVol, vol3.get());
2237 
2238   auto outerBounds =
2239       dynamic_cast<const CylinderVolumeBounds*>(&outerVol->volumeBounds());
2240   BOOST_REQUIRE(outerBounds != nullptr);
2241   BOOST_CHECK_EQUAL(outerBounds->get(CylinderVolumeBounds::eMinR), 300_mm);
2242   BOOST_CHECK_EQUAL(outerBounds->get(CylinderVolumeBounds::eMaxR), 500_mm);
2243   BOOST_CHECK_EQUAL(outerBounds->get(CylinderVolumeBounds::eHalfLengthZ), hlZ);
2244 
2245   // Check middle volumes maintain their size
2246   for (std::size_t i = 1; i < volumes.size() - 1; i++) {
2247     auto volBounds =
2248         dynamic_cast<const CylinderVolumeBounds*>(&volumes[i]->volumeBounds());
2249     BOOST_REQUIRE(volBounds != nullptr);
2250     BOOST_CHECK_EQUAL(volBounds->get(CylinderVolumeBounds::eHalfLengthZ), hlZ);
2251   }
2252 }
2253 
2254 BOOST_AUTO_TEST_CASE(AsymmetricResizeRFlipped) {
2255   const auto gctx = Acts::GeometryContext::dangerouslyDefaultConstruct();
2256   double hlZ = 400_mm;
2257 
2258   // Create three cylinder volumes stacked in r
2259   auto bounds1 = std::make_shared<CylinderVolumeBounds>(100_mm, 200_mm, hlZ);
2260   auto bounds2 = std::make_shared<CylinderVolumeBounds>(200_mm, 300_mm, hlZ);
2261   auto bounds3 = std::make_shared<CylinderVolumeBounds>(300_mm, 400_mm, hlZ);
2262 
2263   Transform3 transform = Transform3::Identity();
2264   auto vol1 = std::make_shared<Volume>(transform, bounds1);
2265   auto vol2 = std::make_shared<Volume>(transform, bounds2);
2266   auto vol3 = std::make_shared<Volume>(transform, bounds3);
2267 
2268   std::vector<Volume*> volumes = {vol1.get(), vol2.get(), vol3.get()};
2269   // Test with Expand for inner radius and Gap for outer radius
2270   CylinderVolumeStack cylStack(
2271       gctx, volumes, AxisDirection::AxisR, VolumeAttachmentStrategy::Gap,
2272       {VolumeResizeStrategy::Expand, VolumeResizeStrategy::Gap}, *logger);
2273 
2274   // Update bounds to test asymmetric expansion
2275   auto newBounds = std::make_shared<CylinderVolumeBounds>(50_mm, 500_mm, hlZ);
2276   cylStack.update(gctx, newBounds, std::nullopt, *logger);
2277   // Check that we have one gap volume at outer radius
2278   BOOST_CHECK_EQUAL(volumes.size(), 4);  // Original 3 + 1 gap volume
2279 
2280   // Check gap volume at outer radius
2281   auto outerGap = volumes.back();
2282   auto outerGapBounds =
2283       dynamic_cast<const CylinderVolumeBounds*>(&outerGap->volumeBounds());
2284   BOOST_REQUIRE(outerGapBounds != nullptr);
2285   BOOST_CHECK_EQUAL(outerGapBounds->get(CylinderVolumeBounds::eMinR), 400_mm);
2286   BOOST_CHECK_EQUAL(outerGapBounds->get(CylinderVolumeBounds::eMaxR), 500_mm);
2287   BOOST_CHECK_EQUAL(outerGapBounds->get(CylinderVolumeBounds::eHalfLengthZ),
2288                     hlZ);
2289 
2290   // Check that inner volume was expanded
2291   auto* innerVol = volumes.front();
2292   BOOST_CHECK_EQUAL(innerVol, vol1.get());
2293 
2294   auto innerBounds =
2295       dynamic_cast<const CylinderVolumeBounds*>(&innerVol->volumeBounds());
2296   BOOST_REQUIRE(innerBounds != nullptr);
2297   BOOST_CHECK_EQUAL(innerBounds->get(CylinderVolumeBounds::eMinR), 50_mm);
2298   BOOST_CHECK_EQUAL(innerBounds->get(CylinderVolumeBounds::eMaxR), 200_mm);
2299   BOOST_CHECK_EQUAL(innerBounds->get(CylinderVolumeBounds::eHalfLengthZ), hlZ);
2300 
2301   // Check middle volumes maintain their size
2302   for (std::size_t i = 1; i < volumes.size() - 1; i++) {
2303     auto volBounds =
2304         dynamic_cast<const CylinderVolumeBounds*>(&volumes[i]->volumeBounds());
2305     BOOST_REQUIRE(volBounds != nullptr);
2306     BOOST_CHECK_EQUAL(volBounds->get(CylinderVolumeBounds::eHalfLengthZ), hlZ);
2307   }
2308 }
2309 
2310 BOOST_AUTO_TEST_CASE(AsymmetricSingleSideResizeZ) {
2311   const auto gctx = Acts::GeometryContext::dangerouslyDefaultConstruct();
2312   double hlZ = 400_mm;
2313   double rMin = 100_mm;
2314   double rMax = 200_mm;
2315 
2316   // Create two cylinder volumes stacked in z
2317   auto bounds1 = std::make_shared<CylinderVolumeBounds>(rMin, rMax, hlZ);
2318   auto bounds2 = std::make_shared<CylinderVolumeBounds>(rMin, rMax, hlZ);
2319 
2320   Transform3 transform1 = Transform3::Identity();
2321   transform1.translate(Vector3{0_mm, 0_mm, -hlZ});
2322   auto vol1 = std::make_shared<Volume>(transform1, bounds1);
2323 
2324   Transform3 transform2 = Transform3::Identity();
2325   transform2.translate(Vector3{0_mm, 0_mm, hlZ});
2326   auto vol2 = std::make_shared<Volume>(transform2, bounds2);
2327 
2328   std::vector<Volume*> volumes = {vol1.get(), vol2.get()};
2329 
2330   // Test with Gap for negative z and Expand for positive z
2331   CylinderVolumeStack cylStack(
2332       gctx, volumes, AxisDirection::AxisZ, VolumeAttachmentStrategy::Gap,
2333       {VolumeResizeStrategy::Gap, VolumeResizeStrategy::Expand}, *logger);
2334 
2335   // Update bounds to test only positive z expansion
2336   auto newBounds = std::make_shared<CylinderVolumeBounds>(rMin, rMax, 3 * hlZ);
2337   Transform3 newTransform =
2338       Transform3::Identity() * Translation3{0_mm, 0_mm, hlZ};
2339   cylStack.update(gctx, newBounds, newTransform, *logger);
2340   // Check that first volume maintains its size and position
2341   auto* firstVol = volumes.front();
2342   BOOST_CHECK_EQUAL(firstVol, vol1.get());
2343   auto firstBounds =
2344       dynamic_cast<const CylinderVolumeBounds*>(&firstVol->volumeBounds());
2345   BOOST_REQUIRE(firstBounds != nullptr);
2346   BOOST_CHECK_EQUAL(firstBounds->get(CylinderVolumeBounds::eHalfLengthZ), hlZ);
2347   BOOST_CHECK_EQUAL(firstBounds->get(CylinderVolumeBounds::eMinR), rMin);
2348   BOOST_CHECK_EQUAL(firstBounds->get(CylinderVolumeBounds::eMaxR), rMax);
2349   BOOST_CHECK_CLOSE(firstVol->center(gctx)[eZ], -hlZ, 1e-10);
2350 
2351   // Check that second volume was expanded in positive z
2352   auto* lastVol = volumes.back();
2353   BOOST_CHECK_EQUAL(lastVol, vol2.get());
2354   auto lastBounds =
2355       dynamic_cast<const CylinderVolumeBounds*>(&lastVol->volumeBounds());
2356   BOOST_REQUIRE(lastBounds != nullptr);
2357   BOOST_CHECK_EQUAL(lastBounds->get(CylinderVolumeBounds::eHalfLengthZ),
2358                     2 * hlZ);
2359   BOOST_CHECK_EQUAL(lastBounds->get(CylinderVolumeBounds::eMinR), rMin);
2360   BOOST_CHECK_EQUAL(lastBounds->get(CylinderVolumeBounds::eMaxR), rMax);
2361   BOOST_CHECK_CLOSE(lastVol->center(gctx)[eZ], 2 * hlZ, 1e-10);
2362 
2363   // No gap volumes should be created since only positive z changed
2364   BOOST_CHECK_EQUAL(volumes.size(), 2);
2365 }
2366 
2367 BOOST_AUTO_TEST_CASE(AsymmetricSingleSideResizeZFlipped) {
2368   const auto gctx = Acts::GeometryContext::dangerouslyDefaultConstruct();
2369   double hlZ = 400_mm;
2370   double rMin = 100_mm;
2371   double rMax = 200_mm;
2372 
2373   // Create two cylinder volumes stacked in z
2374   auto bounds1 = std::make_shared<CylinderVolumeBounds>(rMin, rMax, hlZ);
2375   auto bounds2 = std::make_shared<CylinderVolumeBounds>(rMin, rMax, hlZ);
2376 
2377   Transform3 transform1 = Transform3::Identity();
2378   transform1.translate(Vector3{0_mm, 0_mm, -hlZ});
2379   auto vol1 = std::make_shared<Volume>(transform1, bounds1);
2380 
2381   Transform3 transform2 = Transform3::Identity();
2382   transform2.translate(Vector3{0_mm, 0_mm, hlZ});
2383   auto vol2 = std::make_shared<Volume>(transform2, bounds2);
2384 
2385   std::vector<Volume*> volumes = {vol1.get(), vol2.get()};
2386   // Test with Expand for negative z and Gap for positive z
2387   CylinderVolumeStack cylStack(
2388       gctx, volumes, AxisDirection::AxisZ, VolumeAttachmentStrategy::Gap,
2389       {VolumeResizeStrategy::Expand, VolumeResizeStrategy::Gap}, *logger);
2390 
2391   // Update bounds to test only positive z expansion
2392   auto newBounds = std::make_shared<CylinderVolumeBounds>(rMin, rMax, 3 * hlZ);
2393   Transform3 newTransform =
2394       Transform3::Identity() * Translation3{0_mm, 0_mm, hlZ};
2395   cylStack.update(gctx, newBounds, newTransform, *logger);
2396   // Check that first volume maintains its size and position
2397   auto* firstVol = volumes.front();
2398   BOOST_CHECK_EQUAL(firstVol, vol1.get());
2399   auto firstBounds =
2400       dynamic_cast<const CylinderVolumeBounds*>(&firstVol->volumeBounds());
2401   BOOST_REQUIRE(firstBounds != nullptr);
2402   BOOST_CHECK_EQUAL(firstBounds->get(CylinderVolumeBounds::eHalfLengthZ), hlZ);
2403   BOOST_CHECK_EQUAL(firstBounds->get(CylinderVolumeBounds::eMinR), rMin);
2404   BOOST_CHECK_EQUAL(firstBounds->get(CylinderVolumeBounds::eMaxR), rMax);
2405   BOOST_CHECK_CLOSE(firstVol->center(gctx)[eZ], -hlZ, 1e-10);
2406 
2407   // Check that second volume stays the same
2408   auto* midVol = volumes[1];
2409   BOOST_CHECK_EQUAL(midVol, vol2.get());
2410   auto midBounds =
2411       dynamic_cast<const CylinderVolumeBounds*>(&midVol->volumeBounds());
2412   BOOST_REQUIRE(midBounds != nullptr);
2413   BOOST_CHECK_EQUAL(midBounds->get(CylinderVolumeBounds::eHalfLengthZ), hlZ);
2414   BOOST_CHECK_EQUAL(midBounds->get(CylinderVolumeBounds::eMinR), rMin);
2415   BOOST_CHECK_EQUAL(midBounds->get(CylinderVolumeBounds::eMaxR), rMax);
2416   BOOST_CHECK_CLOSE(midVol->center(gctx)[eZ], hlZ, 1e-10);
2417 
2418   // A gap volume should be created at positive z
2419   BOOST_CHECK_EQUAL(volumes.size(), 3);  // 2 volumes + 1 gap volume
2420 
2421   // Check gap volume at positive z
2422   auto* gapVol = volumes.back();
2423   auto gapBounds =
2424       dynamic_cast<const CylinderVolumeBounds*>(&gapVol->volumeBounds());
2425   BOOST_REQUIRE(gapBounds != nullptr);
2426   BOOST_CHECK_EQUAL(gapBounds->get(CylinderVolumeBounds::eHalfLengthZ), hlZ);
2427   BOOST_CHECK_EQUAL(gapBounds->get(CylinderVolumeBounds::eMinR), rMin);
2428   BOOST_CHECK_EQUAL(gapBounds->get(CylinderVolumeBounds::eMaxR), rMax);
2429   BOOST_CHECK_CLOSE(gapVol->center(gctx)[eZ], 3 * hlZ, 1e-10);
2430 }
2431 
2432 BOOST_AUTO_TEST_CASE(AsymmetricSingleSideResizeR) {
2433   const auto gctx = Acts::GeometryContext::dangerouslyDefaultConstruct();
2434   double hlZ = 400_mm;
2435   double rMin1 = 100_mm;
2436   double rMax1 = 200_mm;
2437   double rMin2 = 200_mm;
2438   double rMax2 = 300_mm;
2439 
2440   // Create two cylinder volumes stacked in r
2441   auto bounds1 = std::make_shared<CylinderVolumeBounds>(rMin1, rMax1, hlZ);
2442   auto bounds2 = std::make_shared<CylinderVolumeBounds>(rMin2, rMax2, hlZ);
2443 
2444   Transform3 transform = Transform3::Identity();
2445   auto vol1 = std::make_shared<Volume>(transform, bounds1);
2446   auto vol2 = std::make_shared<Volume>(transform, bounds2);
2447 
2448   std::vector<Volume*> volumes = {vol1.get(), vol2.get()};
2449 
2450   // Test with Gap for inner radius and Expand for outer radius
2451   CylinderVolumeStack cylStack(
2452       gctx, volumes, AxisDirection::AxisR, VolumeAttachmentStrategy::Gap,
2453       {VolumeResizeStrategy::Gap, VolumeResizeStrategy::Expand}, *logger);
2454 
2455   // Update bounds to test only outer radius expansion
2456   auto newBounds = std::make_shared<CylinderVolumeBounds>(rMin1, 500_mm, hlZ);
2457   cylStack.update(gctx, newBounds, std::nullopt, *logger);
2458 
2459   // Check that inner volume maintains its size
2460   auto* innerVol = volumes.front();
2461   BOOST_CHECK_EQUAL(innerVol, vol1.get());
2462   auto innerBounds =
2463       dynamic_cast<const CylinderVolumeBounds*>(&innerVol->volumeBounds());
2464   BOOST_REQUIRE(innerBounds != nullptr);
2465   BOOST_CHECK_EQUAL(innerBounds->get(CylinderVolumeBounds::eMinR), rMin1);
2466   BOOST_CHECK_EQUAL(innerBounds->get(CylinderVolumeBounds::eMaxR), rMax1);
2467   BOOST_CHECK_EQUAL(innerBounds->get(CylinderVolumeBounds::eHalfLengthZ), hlZ);
2468 
2469   // Check that outer volume was expanded only in outer radius
2470   auto* outerVol = volumes.back();
2471   BOOST_CHECK_EQUAL(outerVol, vol2.get());
2472   auto outerBounds =
2473       dynamic_cast<const CylinderVolumeBounds*>(&outerVol->volumeBounds());
2474   BOOST_REQUIRE(outerBounds != nullptr);
2475   BOOST_CHECK_EQUAL(outerBounds->get(CylinderVolumeBounds::eMinR), rMin2);
2476   BOOST_CHECK_EQUAL(outerBounds->get(CylinderVolumeBounds::eMaxR), 500_mm);
2477   BOOST_CHECK_EQUAL(outerBounds->get(CylinderVolumeBounds::eHalfLengthZ), hlZ);
2478 
2479   // No gap volumes should be created since only outer radius changed
2480   BOOST_CHECK_EQUAL(volumes.size(), 2);
2481 }
2482 
2483 BOOST_AUTO_TEST_CASE(AsymmetricSingleSideResizeRFlipped) {
2484   const auto gctx = Acts::GeometryContext::dangerouslyDefaultConstruct();
2485   double hlZ = 400_mm;
2486   double rMin1 = 100_mm;
2487   double rMax1 = 200_mm;
2488   double rMin2 = 200_mm;
2489   double rMax2 = 300_mm;
2490 
2491   // Create two cylinder volumes stacked in r
2492   auto bounds1 = std::make_shared<CylinderVolumeBounds>(rMin1, rMax1, hlZ);
2493   auto bounds2 = std::make_shared<CylinderVolumeBounds>(rMin2, rMax2, hlZ);
2494 
2495   Transform3 transform = Transform3::Identity();
2496   auto vol1 = std::make_shared<Volume>(transform, bounds1);
2497   auto vol2 = std::make_shared<Volume>(transform, bounds2);
2498 
2499   std::vector<Volume*> volumes = {vol1.get(), vol2.get()};
2500   // Test with Expand for inner radius and Gap for outer radius
2501   CylinderVolumeStack cylStack(
2502       gctx, volumes, AxisDirection::AxisR, VolumeAttachmentStrategy::Gap,
2503       {VolumeResizeStrategy::Expand, VolumeResizeStrategy::Gap}, *logger);
2504 
2505   // Update bounds to test only outer radius expansion
2506   auto newBounds = std::make_shared<CylinderVolumeBounds>(rMin1, 500_mm, hlZ);
2507   cylStack.update(gctx, newBounds, std::nullopt, *logger);
2508   // Check that inner volume maintains its size
2509   auto* innerVol = volumes.front();
2510   BOOST_CHECK_EQUAL(innerVol, vol1.get());
2511   auto innerBounds =
2512       dynamic_cast<const CylinderVolumeBounds*>(&innerVol->volumeBounds());
2513   BOOST_REQUIRE(innerBounds != nullptr);
2514   BOOST_CHECK_EQUAL(innerBounds->get(CylinderVolumeBounds::eMinR), rMin1);
2515   BOOST_CHECK_EQUAL(innerBounds->get(CylinderVolumeBounds::eMaxR), rMax1);
2516   BOOST_CHECK_EQUAL(innerBounds->get(CylinderVolumeBounds::eHalfLengthZ), hlZ);
2517   BOOST_CHECK_CLOSE(innerVol->center(gctx)[eZ], 0, 1e-10);
2518 
2519   // Check that second volume maintains its size
2520   auto* midVol = volumes[1];
2521   BOOST_CHECK_EQUAL(midVol, vol2.get());
2522   auto midBounds =
2523       dynamic_cast<const CylinderVolumeBounds*>(&midVol->volumeBounds());
2524   BOOST_REQUIRE(midBounds != nullptr);
2525   BOOST_CHECK_EQUAL(midBounds->get(CylinderVolumeBounds::eMinR), rMin2);
2526   BOOST_CHECK_EQUAL(midBounds->get(CylinderVolumeBounds::eMaxR), rMax2);
2527   BOOST_CHECK_EQUAL(midBounds->get(CylinderVolumeBounds::eHalfLengthZ), hlZ);
2528   BOOST_CHECK_CLOSE(midVol->center(gctx)[eZ], 0, 1e-10);
2529 
2530   // A gap volume should be created at outer radius
2531   BOOST_CHECK_EQUAL(volumes.size(), 3);  // 2 volumes + 1 gap volume
2532 
2533   // Check gap volume at positive z
2534   auto* gapVol = volumes.back();
2535   auto gapBounds =
2536       dynamic_cast<const CylinderVolumeBounds*>(&gapVol->volumeBounds());
2537   BOOST_REQUIRE(gapBounds != nullptr);
2538   BOOST_CHECK_EQUAL(gapBounds->get(CylinderVolumeBounds::eHalfLengthZ), hlZ);
2539   BOOST_CHECK_EQUAL(gapBounds->get(CylinderVolumeBounds::eMinR), rMax2);
2540   BOOST_CHECK_EQUAL(gapBounds->get(CylinderVolumeBounds::eMaxR), 500_mm);
2541   BOOST_CHECK_CLOSE(gapVol->center(gctx)[eZ], 0, 1e-10);
2542 }
2543 
2544 BOOST_AUTO_TEST_CASE(AsymmetricSingleSideResizeZNegative) {
2545   const auto gctx = Acts::GeometryContext::dangerouslyDefaultConstruct();
2546   double hlZ = 400_mm;
2547   double rMin = 100_mm;
2548   double rMax = 200_mm;
2549 
2550   // Create two cylinder volumes stacked in z
2551   auto bounds1 = std::make_shared<CylinderVolumeBounds>(rMin, rMax, hlZ);
2552   auto bounds2 = std::make_shared<CylinderVolumeBounds>(rMin, rMax, hlZ);
2553 
2554   Transform3 transform1 = Transform3::Identity();
2555   transform1.translate(Vector3{0_mm, 0_mm, -hlZ});
2556   auto vol1 = std::make_shared<Volume>(transform1, bounds1);
2557 
2558   Transform3 transform2 = Transform3::Identity();
2559   transform2.translate(Vector3{0_mm, 0_mm, hlZ});
2560   auto vol2 = std::make_shared<Volume>(transform2, bounds2);
2561 
2562   std::vector<Volume*> volumes = {vol1.get(), vol2.get()};
2563   // Test with Gap for positive z and Expand for negative z
2564   CylinderVolumeStack cylStack(
2565       gctx, volumes, AxisDirection::AxisZ, VolumeAttachmentStrategy::Gap,
2566       {VolumeResizeStrategy::Expand, VolumeResizeStrategy::Gap}, *logger);
2567 
2568   // Update bounds to test only negative z expansion
2569   auto newBounds = std::make_shared<CylinderVolumeBounds>(rMin, rMax, 3 * hlZ);
2570   Transform3 newTransform =
2571       Transform3::Identity() * Translation3{0_mm, 0_mm, -hlZ};
2572   cylStack.update(gctx, newBounds, newTransform, *logger);
2573   // Check that first volume was expanded in negative z
2574   auto* firstVol = volumes.front();
2575   BOOST_CHECK_EQUAL(firstVol, vol1.get());
2576   auto firstBounds =
2577       dynamic_cast<const CylinderVolumeBounds*>(&firstVol->volumeBounds());
2578   BOOST_REQUIRE(firstBounds != nullptr);
2579   BOOST_CHECK_EQUAL(firstBounds->get(CylinderVolumeBounds::eHalfLengthZ),
2580                     2 * hlZ);
2581   BOOST_CHECK_EQUAL(firstBounds->get(CylinderVolumeBounds::eMinR), rMin);
2582   BOOST_CHECK_EQUAL(firstBounds->get(CylinderVolumeBounds::eMaxR), rMax);
2583   BOOST_CHECK_CLOSE(firstVol->center(gctx)[eZ], -2 * hlZ, 1e-10);
2584 
2585   // Check that second volume maintains its size and position
2586   auto* lastVol = volumes.back();
2587   BOOST_CHECK_EQUAL(lastVol, vol2.get());
2588   auto lastBounds =
2589       dynamic_cast<const CylinderVolumeBounds*>(&lastVol->volumeBounds());
2590   BOOST_REQUIRE(lastBounds != nullptr);
2591   BOOST_CHECK_EQUAL(lastBounds->get(CylinderVolumeBounds::eHalfLengthZ), hlZ);
2592   BOOST_CHECK_EQUAL(lastBounds->get(CylinderVolumeBounds::eMinR), rMin);
2593   BOOST_CHECK_EQUAL(lastBounds->get(CylinderVolumeBounds::eMaxR), rMax);
2594   BOOST_CHECK_CLOSE(lastVol->center(gctx)[eZ], hlZ, 1e-10);
2595 
2596   // No gap volumes should be created since only negative z changed
2597   BOOST_CHECK_EQUAL(volumes.size(), 2);
2598 }
2599 
2600 BOOST_AUTO_TEST_CASE(AsymmetricSingleSideResizeZNegativeFlipped) {
2601   const auto gctx = Acts::GeometryContext::dangerouslyDefaultConstruct();
2602   double hlZ = 400_mm;
2603   double rMin = 100_mm;
2604   double rMax = 200_mm;
2605 
2606   // Create two cylinder volumes stacked in z
2607   auto bounds1 = std::make_shared<CylinderVolumeBounds>(rMin, rMax, hlZ);
2608   auto bounds2 = std::make_shared<CylinderVolumeBounds>(rMin, rMax, hlZ);
2609 
2610   Transform3 transform1 = Transform3::Identity();
2611   transform1.translate(Vector3{0_mm, 0_mm, -hlZ});
2612   auto vol1 = std::make_shared<Volume>(transform1, bounds1);
2613 
2614   Transform3 transform2 = Transform3::Identity();
2615   transform2.translate(Vector3{0_mm, 0_mm, hlZ});
2616   auto vol2 = std::make_shared<Volume>(transform2, bounds2);
2617 
2618   std::vector<Volume*> volumes = {vol1.get(), vol2.get()};
2619   // Test with Gap for negative z and Expand for positive z
2620   CylinderVolumeStack cylStack(
2621       gctx, volumes, AxisDirection::AxisZ, VolumeAttachmentStrategy::Gap,
2622       {VolumeResizeStrategy::Gap, VolumeResizeStrategy::Expand}, *logger);
2623 
2624   // Update bounds to test only negative z expansion
2625   auto newBounds = std::make_shared<CylinderVolumeBounds>(rMin, rMax, 3 * hlZ);
2626   Transform3 newTransform =
2627       Transform3::Identity() * Translation3{0_mm, 0_mm, -hlZ};
2628   cylStack.update(gctx, newBounds, newTransform, *logger);
2629 
2630   // A gap volume should be created at negative z
2631   BOOST_CHECK_EQUAL(volumes.size(), 3);  // 2 volumes + 1 gap volume
2632 
2633   // Check gap volume at negative z
2634   auto* gapVol = volumes[0];
2635   auto gapBounds =
2636       dynamic_cast<const CylinderVolumeBounds*>(&gapVol->volumeBounds());
2637   BOOST_REQUIRE(gapBounds != nullptr);
2638   BOOST_CHECK_EQUAL(gapBounds->get(CylinderVolumeBounds::eHalfLengthZ), hlZ);
2639   BOOST_CHECK_EQUAL(gapBounds->get(CylinderVolumeBounds::eMinR), rMin);
2640   BOOST_CHECK_EQUAL(gapBounds->get(CylinderVolumeBounds::eMaxR), rMax);
2641   BOOST_CHECK_CLOSE(gapVol->center(gctx)[eZ], -3 * hlZ, 1e-10);
2642 
2643   // Check that first original volume maintains its size and position
2644   auto* originalFirstVol = volumes[1];
2645   BOOST_CHECK_EQUAL(originalFirstVol, vol1.get());
2646   auto originalFirstBounds = dynamic_cast<const CylinderVolumeBounds*>(
2647       &originalFirstVol->volumeBounds());
2648   BOOST_REQUIRE(originalFirstBounds != nullptr);
2649   BOOST_CHECK_EQUAL(
2650       originalFirstBounds->get(CylinderVolumeBounds::eHalfLengthZ), hlZ);
2651   BOOST_CHECK_EQUAL(originalFirstBounds->get(CylinderVolumeBounds::eMinR),
2652                     rMin);
2653   BOOST_CHECK_EQUAL(originalFirstBounds->get(CylinderVolumeBounds::eMaxR),
2654                     rMax);
2655   BOOST_CHECK_CLOSE(originalFirstVol->center(gctx)[eZ], -hlZ, 1e-10);
2656 }
2657 
2658 BOOST_AUTO_TEST_CASE(AsymmetricSingleSideResizeRNegative) {
2659   const auto gctx = Acts::GeometryContext::dangerouslyDefaultConstruct();
2660   double hlZ = 400_mm;
2661   double rMin1 = 100_mm;
2662   double rMax1 = 200_mm;
2663   double rMin2 = 200_mm;
2664   double rMax2 = 300_mm;
2665 
2666   // Create two cylinder volumes stacked in r
2667   auto bounds1 = std::make_shared<CylinderVolumeBounds>(rMin1, rMax1, hlZ);
2668   auto bounds2 = std::make_shared<CylinderVolumeBounds>(rMin2, rMax2, hlZ);
2669 
2670   Transform3 transform = Transform3::Identity();
2671   auto vol1 = std::make_shared<Volume>(transform, bounds1);
2672   auto vol2 = std::make_shared<Volume>(transform, bounds2);
2673 
2674   std::vector<Volume*> volumes = {vol1.get(), vol2.get()};
2675   // Test with Gap for outer radius and Expand for inner radius
2676   CylinderVolumeStack cylStack(
2677       gctx, volumes, AxisDirection::AxisR, VolumeAttachmentStrategy::Gap,
2678       {VolumeResizeStrategy::Expand, VolumeResizeStrategy::Gap}, *logger);
2679 
2680   // Update bounds to test only inner radius expansion
2681   auto newBounds = std::make_shared<CylinderVolumeBounds>(50_mm, rMax2, hlZ);
2682   cylStack.update(gctx, newBounds, std::nullopt, *logger);
2683   // Check that first volume was expanded in inner radius
2684   auto* firstVol = volumes.front();
2685   BOOST_CHECK_EQUAL(firstVol, vol1.get());
2686   auto firstBounds =
2687       dynamic_cast<const CylinderVolumeBounds*>(&firstVol->volumeBounds());
2688   BOOST_REQUIRE(firstBounds != nullptr);
2689   BOOST_CHECK_EQUAL(firstBounds->get(CylinderVolumeBounds::eMinR), 50_mm);
2690   BOOST_CHECK_EQUAL(firstBounds->get(CylinderVolumeBounds::eMaxR), rMax1);
2691   BOOST_CHECK_EQUAL(firstBounds->get(CylinderVolumeBounds::eHalfLengthZ), hlZ);
2692   BOOST_CHECK_CLOSE(firstVol->center(gctx)[eZ], 0, 1e-10);
2693 
2694   // Check that second volume maintains its size and position
2695   auto* lastVol = volumes.back();
2696   BOOST_CHECK_EQUAL(lastVol, vol2.get());
2697   auto lastBounds =
2698       dynamic_cast<const CylinderVolumeBounds*>(&lastVol->volumeBounds());
2699   BOOST_REQUIRE(lastBounds != nullptr);
2700   BOOST_CHECK_EQUAL(lastBounds->get(CylinderVolumeBounds::eMinR), rMin2);
2701   BOOST_CHECK_EQUAL(lastBounds->get(CylinderVolumeBounds::eMaxR), rMax2);
2702   BOOST_CHECK_EQUAL(lastBounds->get(CylinderVolumeBounds::eHalfLengthZ), hlZ);
2703   BOOST_CHECK_CLOSE(lastVol->center(gctx)[eZ], 0, 1e-10);
2704 
2705   // No gap volumes should be created since only inner radius changed
2706   BOOST_CHECK_EQUAL(volumes.size(), 2);
2707 }
2708 
2709 BOOST_AUTO_TEST_CASE(AsymmetricSingleSideResizeRNegativeFlipped) {
2710   const auto gctx = Acts::GeometryContext::dangerouslyDefaultConstruct();
2711   double hlZ = 400_mm;
2712   double rMin1 = 100_mm;
2713   double rMax1 = 200_mm;
2714   double rMin2 = 200_mm;
2715   double rMax2 = 300_mm;
2716 
2717   // Create two cylinder volumes stacked in r
2718   auto bounds1 = std::make_shared<CylinderVolumeBounds>(rMin1, rMax1, hlZ);
2719   auto bounds2 = std::make_shared<CylinderVolumeBounds>(rMin2, rMax2, hlZ);
2720 
2721   Transform3 transform = Transform3::Identity();
2722   auto vol1 = std::make_shared<Volume>(transform, bounds1);
2723   auto vol2 = std::make_shared<Volume>(transform, bounds2);
2724 
2725   std::vector<Volume*> volumes = {vol1.get(), vol2.get()};
2726   // Test with Expand for outer radius and Gap for inner radius
2727   CylinderVolumeStack cylStack(
2728       gctx, volumes, AxisDirection::AxisR, VolumeAttachmentStrategy::Gap,
2729       {VolumeResizeStrategy::Gap, VolumeResizeStrategy::Expand}, *logger);
2730 
2731   // Update bounds to test only inner radius expansion
2732   auto newBounds = std::make_shared<CylinderVolumeBounds>(50_mm, rMax2, hlZ);
2733   cylStack.update(gctx, newBounds, std::nullopt, *logger);
2734   // A gap volume should be created at inner radius
2735   BOOST_CHECK_EQUAL(volumes.size(), 3);  // 2 volumes + 1 gap volume
2736 
2737   // Check gap volume at inner radius
2738   auto* gapVol = volumes[0];
2739   auto gapBounds =
2740       dynamic_cast<const CylinderVolumeBounds*>(&gapVol->volumeBounds());
2741   BOOST_REQUIRE(gapBounds != nullptr);
2742   BOOST_CHECK_EQUAL(gapBounds->get(CylinderVolumeBounds::eHalfLengthZ), hlZ);
2743   BOOST_CHECK_EQUAL(gapBounds->get(CylinderVolumeBounds::eMinR), 50_mm);
2744   BOOST_CHECK_EQUAL(gapBounds->get(CylinderVolumeBounds::eMaxR), rMin1);
2745   BOOST_CHECK_CLOSE(gapVol->center(gctx)[eZ], 0, 1e-10);
2746 
2747   // Check that first original volume maintains its size and position
2748   auto* originalFirstVol = volumes[1];
2749   BOOST_CHECK_EQUAL(originalFirstVol, vol1.get());
2750   auto originalFirstBounds = dynamic_cast<const CylinderVolumeBounds*>(
2751       &originalFirstVol->volumeBounds());
2752   BOOST_REQUIRE(originalFirstBounds != nullptr);
2753   BOOST_CHECK_EQUAL(
2754       originalFirstBounds->get(CylinderVolumeBounds::eHalfLengthZ), hlZ);
2755   BOOST_CHECK_EQUAL(originalFirstBounds->get(CylinderVolumeBounds::eMinR),
2756                     rMin1);
2757   BOOST_CHECK_EQUAL(originalFirstBounds->get(CylinderVolumeBounds::eMaxR),
2758                     rMax1);
2759   BOOST_CHECK_CLOSE(originalFirstVol->center(gctx)[eZ], 0, 1e-10);
2760 
2761   // Check that second volume maintains its size and position
2762   auto* originalSecondVol = volumes[2];
2763   BOOST_CHECK_EQUAL(originalSecondVol, vol2.get());
2764   auto originalSecondBounds = dynamic_cast<const CylinderVolumeBounds*>(
2765       &originalSecondVol->volumeBounds());
2766   BOOST_REQUIRE(originalSecondBounds != nullptr);
2767   BOOST_CHECK_EQUAL(originalSecondBounds->get(CylinderVolumeBounds::eMinR),
2768                     rMin2);
2769   BOOST_CHECK_EQUAL(originalSecondBounds->get(CylinderVolumeBounds::eMaxR),
2770                     rMax2);
2771   BOOST_CHECK_EQUAL(
2772       originalSecondBounds->get(CylinderVolumeBounds::eHalfLengthZ), hlZ);
2773   BOOST_CHECK_CLOSE(originalSecondVol->center(gctx)[eZ], 0, 1e-10);
2774 }
2775 
2776 BOOST_AUTO_TEST_CASE(RStackGapCreationWithUpdatedTransform) {
2777   const auto gctx = Acts::GeometryContext::dangerouslyDefaultConstruct();
2778   double hlZ = 400_mm;
2779   double rMin1 = 100_mm;
2780   double rMax1 = 200_mm;
2781   double rMin2 = 200_mm;
2782   double rMax2 = 300_mm;
2783 
2784   // Create two cylinder volumes stacked in r
2785   auto bounds1 = std::make_shared<CylinderVolumeBounds>(rMin1, rMax1, hlZ);
2786   auto bounds2 = std::make_shared<CylinderVolumeBounds>(rMin2, rMax2, hlZ);
2787 
2788   Transform3 transform = Transform3::Identity();
2789   auto vol1 = std::make_shared<Volume>(transform, bounds1);
2790   auto vol2 = std::make_shared<Volume>(transform, bounds2);
2791 
2792   std::vector<Volume*> volumes = {vol1.get(), vol2.get()};
2793   // Test with Gap for outer radius and Expand for inner radius
2794   CylinderVolumeStack cylStack(gctx, volumes, AxisDirection::AxisR,
2795                                VolumeAttachmentStrategy::Midpoint,
2796                                VolumeResizeStrategy::Gap, *logger);
2797 
2798   cylStack.update(
2799       gctx, std::make_shared<CylinderVolumeBounds>(50_mm, rMax2, hlZ + 5_mm),
2800       Transform3(Translation3(Vector3::UnitZ() * 5_mm)), *logger);
2801 
2802   auto& cylBounds =
2803       dynamic_cast<CylinderVolumeBounds&>(cylStack.volumeBounds());
2804 
2805   BOOST_CHECK_EQUAL(cylBounds.get(CylinderVolumeBounds::eMinR), 50_mm);
2806   BOOST_CHECK_EQUAL(cylBounds.get(CylinderVolumeBounds::eMaxR), rMax2);
2807   BOOST_CHECK_EQUAL(cylBounds.get(CylinderVolumeBounds::eHalfLengthZ),
2808                     hlZ + 5_mm);
2809 
2810   auto gapIt = std::ranges::find_if(volumes, [&](const auto* vol) {
2811     return vol != vol1.get() && vol != vol2.get();  // Find gap volume
2812   });
2813 
2814   BOOST_REQUIRE(gapIt != volumes.end());
2815 
2816   const auto* gap1 = *gapIt;
2817 
2818   BOOST_CHECK_EQUAL(vol1->center(gctx)[eZ], 5_mm);
2819   BOOST_CHECK_EQUAL(vol2->center(gctx)[eZ], 5_mm);
2820   BOOST_CHECK_EQUAL(gap1->center(gctx)[eZ], 5_mm);
2821 
2822   cylStack.update(
2823       gctx, std::make_shared<CylinderVolumeBounds>(50_mm, 350_mm, hlZ + 10_mm),
2824       Transform3(Translation3(Vector3::UnitZ() * 10_mm)), *logger);
2825 
2826   gapIt = std::ranges::find_if(volumes, [&](const auto* vol) {
2827     return vol != vol1.get() && vol != vol2.get() &&
2828            vol != gap1;  // Find gap volume
2829   });
2830 
2831   BOOST_REQUIRE(gapIt != volumes.end());
2832 
2833   const auto* gap2 = *gapIt;
2834 
2835   BOOST_CHECK_EQUAL(vol1->center(gctx)[eZ], 10_mm);
2836   BOOST_CHECK_EQUAL(vol2->center(gctx)[eZ], 10_mm);
2837   BOOST_CHECK_EQUAL(gap1->center(gctx)[eZ], 10_mm);
2838   BOOST_CHECK_EQUAL(gap2->center(gctx)[eZ], 10_mm);
2839 
2840   const auto& gap1Bounds =
2841       dynamic_cast<const CylinderVolumeBounds&>(gap1->volumeBounds());
2842 
2843   const auto& gap2Bounds =
2844       dynamic_cast<const CylinderVolumeBounds&>(gap2->volumeBounds());
2845 
2846   bounds1 =
2847       std::dynamic_pointer_cast<CylinderVolumeBounds>(vol1->volumeBoundsPtr());
2848   bounds2 =
2849       std::dynamic_pointer_cast<CylinderVolumeBounds>(vol2->volumeBoundsPtr());
2850 
2851   BOOST_REQUIRE(bounds1 != nullptr);
2852   BOOST_REQUIRE(bounds2 != nullptr);
2853 
2854   BOOST_CHECK_EQUAL(bounds1->get(CylinderVolumeBounds::eMinR), rMin1);
2855   BOOST_CHECK_EQUAL(bounds1->get(CylinderVolumeBounds::eMaxR), rMax1);
2856   BOOST_CHECK_EQUAL(bounds1->get(CylinderVolumeBounds::eHalfLengthZ),
2857                     hlZ + 10_mm);
2858 
2859   BOOST_CHECK_EQUAL(bounds2->get(CylinderVolumeBounds::eMinR), rMin2);
2860   BOOST_CHECK_EQUAL(bounds2->get(CylinderVolumeBounds::eMaxR), rMax2);
2861   BOOST_CHECK_EQUAL(bounds2->get(CylinderVolumeBounds::eHalfLengthZ),
2862                     hlZ + 10_mm);
2863 
2864   BOOST_CHECK_EQUAL(gap1Bounds.get(CylinderVolumeBounds::eMinR), 50_mm);
2865   BOOST_CHECK_EQUAL(gap1Bounds.get(CylinderVolumeBounds::eMaxR), rMin1);
2866   BOOST_CHECK_EQUAL(gap1Bounds.get(CylinderVolumeBounds::eHalfLengthZ),
2867                     hlZ + 10_mm);
2868 
2869   BOOST_CHECK_EQUAL(gap2Bounds.get(CylinderVolumeBounds::eMinR), rMax2);
2870   BOOST_CHECK_EQUAL(gap2Bounds.get(CylinderVolumeBounds::eMaxR), 350_mm);
2871   BOOST_CHECK_EQUAL(gap2Bounds.get(CylinderVolumeBounds::eHalfLengthZ),
2872                     hlZ + 10_mm);
2873 
2874   BOOST_CHECK_EQUAL(vol1->center(gctx)[eZ], 10_mm);
2875   BOOST_CHECK_EQUAL(vol2->center(gctx)[eZ], 10_mm);
2876   BOOST_CHECK_EQUAL(gap1->center(gctx)[eZ], 10_mm);
2877 }
2878 
2879 // A change in the radial bounds that is smaller than the on-surface tolerance
2880 // must be treated as "no change" and must not spawn a spurious, near-zero
2881 // thickness gap volume. This is the r-direction analogue of the z-direction
2882 // reproduction (see ResizeReproduction2): the r-resize path currently compares
2883 // radii exactly instead of within tolerance, so it creates a degenerate gap
2884 // shell whose inner and outer radius are effectively identical.
2885 BOOST_AUTO_TEST_CASE(RStackGapCreationTolerance) {
2886   const auto gctx = Acts::GeometryContext::dangerouslyDefaultConstruct();
2887   const double hlZ = 400_mm;
2888   const double rMin = 100_mm;
2889   const double rMax = 200_mm;
2890 
2891   // Sub-tolerance perturbation of the radial bounds
2892   const double eps = s_onSurfaceTolerance / 2.0;
2893 
2894   BOOST_TEST_CONTEXT("Outer radius") {
2895     auto bounds = std::make_shared<CylinderVolumeBounds>(rMin, rMax, hlZ);
2896     auto vol = std::make_shared<Volume>(Transform3::Identity(), bounds);
2897 
2898     std::vector<Volume*> volumes = {vol.get()};
2899     CylinderVolumeStack cylStack(gctx, volumes, AxisDirection::AxisR,
2900                                  VolumeAttachmentStrategy::Gap,
2901                                  VolumeResizeStrategy::Gap, *logger);
2902 
2903     BOOST_CHECK(cylStack.gaps().empty());
2904 
2905     // Grow the outer radius by less than the tolerance
2906     cylStack.update(
2907         gctx, std::make_shared<CylinderVolumeBounds>(rMin, rMax + eps, hlZ),
2908         std::nullopt, *logger);
2909 
2910     // No gap volume should have been created for a sub-tolerance change
2911     BOOST_CHECK(cylStack.gaps().empty());
2912     BOOST_CHECK_EQUAL(volumes.size(), 1);
2913   }
2914 
2915   BOOST_TEST_CONTEXT("Inner radius") {
2916     auto bounds = std::make_shared<CylinderVolumeBounds>(rMin, rMax, hlZ);
2917     auto vol = std::make_shared<Volume>(Transform3::Identity(), bounds);
2918 
2919     std::vector<Volume*> volumes = {vol.get()};
2920     CylinderVolumeStack cylStack(gctx, volumes, AxisDirection::AxisR,
2921                                  VolumeAttachmentStrategy::Gap,
2922                                  VolumeResizeStrategy::Gap, *logger);
2923 
2924     BOOST_CHECK(cylStack.gaps().empty());
2925 
2926     // Extend the inner radius inward by less than the tolerance
2927     cylStack.update(
2928         gctx, std::make_shared<CylinderVolumeBounds>(rMin - eps, rMax, hlZ),
2929         std::nullopt, *logger);
2930 
2931     // No gap volume should have been created for a sub-tolerance change
2932     BOOST_CHECK(cylStack.gaps().empty());
2933     BOOST_CHECK_EQUAL(volumes.size(), 1);
2934   }
2935 }
2936 
2937 BOOST_AUTO_TEST_SUITE_END()
2938 
2939 }  // namespace ActsTests