File indexing completed on 2025-10-18 08:21:14
0001
0002
0003
0004
0005
0006
0007
0008
0009 #include "Acts/Surfaces/CylinderSurface.hpp"
0010
0011 #include "Acts/Definitions/Algebra.hpp"
0012 #include "Acts/Definitions/Tolerance.hpp"
0013 #include "Acts/Definitions/Units.hpp"
0014 #include "Acts/Geometry/GeometryObject.hpp"
0015 #include "Acts/Surfaces/CylinderBounds.hpp"
0016 #include "Acts/Surfaces/SurfaceError.hpp"
0017 #include "Acts/Surfaces/SurfaceMergingException.hpp"
0018 #include "Acts/Surfaces/detail/AlignmentHelper.hpp"
0019 #include "Acts/Surfaces/detail/FacesHelper.hpp"
0020 #include "Acts/Surfaces/detail/MergeHelper.hpp"
0021 #include "Acts/Utilities/Intersection.hpp"
0022 #include "Acts/Utilities/ThrowAssert.hpp"
0023 #include "Acts/Utilities/detail/periodic.hpp"
0024
0025 #include <algorithm>
0026 #include <cassert>
0027 #include <cmath>
0028 #include <iostream>
0029 #include <memory>
0030 #include <stdexcept>
0031 #include <utility>
0032 #include <vector>
0033
0034 namespace Acts {
0035
0036 using VectorHelpers::perp;
0037 using VectorHelpers::phi;
0038
0039 CylinderSurface::CylinderSurface(const CylinderSurface& other)
0040 : GeometryObject(), RegularSurface(other), m_bounds(other.m_bounds) {}
0041
0042 CylinderSurface::CylinderSurface(const GeometryContext& gctx,
0043 const CylinderSurface& other,
0044 const Transform3& shift)
0045 : GeometryObject(),
0046 RegularSurface(gctx, other, shift),
0047 m_bounds(other.m_bounds) {}
0048
0049 CylinderSurface::CylinderSurface(const Transform3& transform, double radius,
0050 double halfz, double halfphi, double avphi,
0051 double bevelMinZ, double bevelMaxZ)
0052 : RegularSurface(transform),
0053 m_bounds(std::make_shared<const CylinderBounds>(
0054 radius, halfz, halfphi, avphi, bevelMinZ, bevelMaxZ)) {}
0055
0056 CylinderSurface::CylinderSurface(std::shared_ptr<const CylinderBounds> cbounds,
0057 const DetectorElementBase& detelement)
0058 : RegularSurface(detelement), m_bounds(std::move(cbounds)) {
0059
0060 throw_assert(m_bounds, "CylinderBounds must not be nullptr");
0061 }
0062
0063 CylinderSurface::CylinderSurface(const Transform3& transform,
0064 std::shared_ptr<const CylinderBounds> cbounds)
0065 : RegularSurface(transform), m_bounds(std::move(cbounds)) {
0066 throw_assert(m_bounds, "CylinderBounds must not be nullptr");
0067 }
0068
0069 CylinderSurface& CylinderSurface::operator=(const CylinderSurface& other) {
0070 if (this != &other) {
0071 Surface::operator=(other);
0072 m_bounds = other.m_bounds;
0073 }
0074 return *this;
0075 }
0076
0077
0078 Vector3 CylinderSurface::referencePosition(const GeometryContext& gctx,
0079 AxisDirection aDir) const {
0080
0081 if (aDir == AxisDirection::AxisR || aDir == AxisDirection::AxisRPhi) {
0082 double R = bounds().get(CylinderBounds::eR);
0083 double phi = bounds().get(CylinderBounds::eAveragePhi);
0084 return localToGlobal(gctx, Vector2{phi * R, 0});
0085 }
0086
0087
0088
0089
0090 return center(gctx);
0091 }
0092
0093
0094 RotationMatrix3 CylinderSurface::referenceFrame(
0095 const GeometryContext& gctx, const Vector3& position,
0096 const Vector3& ) const {
0097 RotationMatrix3 mFrame;
0098
0099
0100 Vector3 measY = rotSymmetryAxis(gctx);
0101
0102 Vector3 measDepth = normal(gctx, position);
0103
0104 Vector3 measX(measY.cross(measDepth).normalized());
0105
0106 mFrame.col(0) = measX;
0107 mFrame.col(1) = measY;
0108 mFrame.col(2) = measDepth;
0109
0110 return mFrame;
0111 }
0112
0113 Surface::SurfaceType CylinderSurface::type() const {
0114 return Surface::Cylinder;
0115 }
0116
0117 Vector3 CylinderSurface::localToGlobal(const GeometryContext& gctx,
0118 const Vector2& lposition) const {
0119
0120 double r = bounds().get(CylinderBounds::eR);
0121 double phi = lposition[0] / r;
0122 Vector3 position(r * cos(phi), r * sin(phi), lposition[1]);
0123 return transform(gctx) * position;
0124 }
0125
0126 Result<Vector2> CylinderSurface::globalToLocal(const GeometryContext& gctx,
0127 const Vector3& position,
0128 double tolerance) const {
0129 double inttol = tolerance;
0130 if (tolerance == s_onSurfaceTolerance) {
0131
0132
0133 inttol = bounds().get(CylinderBounds::eR) * 0.0001;
0134 }
0135 if (inttol < 0.01) {
0136 inttol = 0.01;
0137 }
0138 const Transform3& sfTransform = transform(gctx);
0139 Transform3 inverseTrans(sfTransform.inverse());
0140 Vector3 loc3Dframe(inverseTrans * position);
0141 if (std::abs(perp(loc3Dframe) - bounds().get(CylinderBounds::eR)) > inttol) {
0142 return Result<Vector2>::failure(SurfaceError::GlobalPositionNotOnSurface);
0143 }
0144 return Result<Vector2>::success(
0145 {bounds().get(CylinderBounds::eR) * phi(loc3Dframe), loc3Dframe.z()});
0146 }
0147
0148 std::string CylinderSurface::name() const {
0149 return "Acts::CylinderSurface";
0150 }
0151
0152 Vector3 CylinderSurface::normal(const GeometryContext& gctx,
0153 const Vector2& lposition) const {
0154 double phi = lposition[0] / m_bounds->get(CylinderBounds::eR);
0155 Vector3 localNormal(cos(phi), sin(phi), 0.);
0156 return transform(gctx).linear() * localNormal;
0157 }
0158
0159 Vector3 CylinderSurface::normal(const GeometryContext& gctx,
0160 const Vector3& position) const {
0161 const Transform3& sfTransform = transform(gctx);
0162
0163 Vector3 pos3D = sfTransform.inverse() * position;
0164
0165 pos3D.z() = 0.;
0166
0167 return sfTransform.linear() * pos3D.normalized();
0168 }
0169
0170 double CylinderSurface::pathCorrection(const GeometryContext& gctx,
0171 const Vector3& position,
0172 const Vector3& direction) const {
0173 Vector3 normalT = normal(gctx, position);
0174 double cosAlpha = normalT.dot(direction);
0175 return std::abs(1. / cosAlpha);
0176 }
0177
0178 const CylinderBounds& CylinderSurface::bounds() const {
0179 return *m_bounds;
0180 }
0181
0182 Polyhedron CylinderSurface::polyhedronRepresentation(
0183 const GeometryContext& gctx, unsigned int quarterSegments) const {
0184 auto ctrans = transform(gctx);
0185
0186
0187 std::vector<Vector3> vertices =
0188 bounds().circleVertices(ctrans, quarterSegments);
0189 auto [faces, triangularMesh] =
0190 detail::FacesHelper::cylindricalFaceMesh(vertices);
0191 return Polyhedron(vertices, faces, triangularMesh, false);
0192 }
0193
0194 Vector3 CylinderSurface::rotSymmetryAxis(const GeometryContext& gctx) const {
0195
0196 return transform(gctx).matrix().block<3, 1>(0, 2);
0197 }
0198
0199 detail::RealQuadraticEquation CylinderSurface::intersectionSolver(
0200 const Transform3& transform, const Vector3& position,
0201 const Vector3& direction) const {
0202
0203 double R = bounds().get(CylinderBounds::eR);
0204
0205
0206 const auto& tMatrix = transform.matrix();
0207 Vector3 caxis = tMatrix.block<3, 1>(0, 2).transpose();
0208 Vector3 ccenter = tMatrix.block<3, 1>(0, 3).transpose();
0209
0210
0211 Vector3 pc = position - ccenter;
0212 Vector3 pcXcd = pc.cross(caxis);
0213 Vector3 ldXcd = direction.cross(caxis);
0214 double a = ldXcd.dot(ldXcd);
0215 double b = 2. * (ldXcd.dot(pcXcd));
0216 double c = pcXcd.dot(pcXcd) - (R * R);
0217
0218 return detail::RealQuadraticEquation(a, b, c);
0219 }
0220
0221 MultiIntersection3D CylinderSurface::intersect(
0222 const GeometryContext& gctx, const Vector3& position,
0223 const Vector3& direction, const BoundaryTolerance& boundaryTolerance,
0224 double tolerance) const {
0225 const auto& gctxTransform = transform(gctx);
0226
0227
0228 auto qe = intersectionSolver(gctxTransform, position, direction);
0229
0230
0231 if (qe.solutions == 0) {
0232 return MultiIntersection3D(Intersection3D::Invalid(),
0233 Intersection3D::Invalid());
0234 }
0235
0236
0237 Vector3 solution1 = position + qe.first * direction;
0238 IntersectionStatus status1 = std::abs(qe.first) < std::abs(tolerance)
0239 ? IntersectionStatus::onSurface
0240 : IntersectionStatus::reachable;
0241
0242
0243 auto boundaryCheck = [&](const Vector3& solution,
0244 IntersectionStatus status) -> IntersectionStatus {
0245
0246 if (boundaryTolerance.isInfinite()) {
0247 return status;
0248 }
0249 if (boundaryTolerance.isNone() && bounds().coversFullAzimuth()) {
0250
0251
0252 const auto& tMatrix = gctxTransform.matrix();
0253
0254 const Vector3 vecLocal(solution - tMatrix.block<3, 1>(0, 3));
0255 double cZ = vecLocal.dot(tMatrix.block<3, 1>(0, 2));
0256 double hZ = bounds().get(CylinderBounds::eHalfLengthZ) + tolerance;
0257 return std::abs(cZ) < std::abs(hZ) ? status
0258 : IntersectionStatus::unreachable;
0259 }
0260 return isOnSurface(gctx, solution, direction, boundaryTolerance)
0261 ? status
0262 : IntersectionStatus::unreachable;
0263 };
0264
0265 status1 = boundaryCheck(solution1, status1);
0266
0267 Intersection3D first(solution1, qe.first, status1);
0268 if (qe.solutions == 1) {
0269 return MultiIntersection3D(first, first);
0270 }
0271
0272 Vector3 solution2 = position + qe.second * direction;
0273 IntersectionStatus status2 = std::abs(qe.second) < std::abs(tolerance)
0274 ? IntersectionStatus::onSurface
0275 : IntersectionStatus::reachable;
0276
0277 status2 = boundaryCheck(solution2, status2);
0278 Intersection3D second(solution2, qe.second, status2);
0279
0280 if (first.pathLength() <= second.pathLength()) {
0281 return MultiIntersection3D(first, second);
0282 }
0283 return MultiIntersection3D(second, first);
0284 }
0285
0286 AlignmentToPathMatrix CylinderSurface::alignmentToPathDerivative(
0287 const GeometryContext& gctx, const Vector3& position,
0288 const Vector3& direction) const {
0289 assert(isOnSurface(gctx, position, direction, BoundaryTolerance::Infinite()));
0290
0291
0292 const auto pcRowVec = (position - center(gctx)).transpose().eval();
0293
0294 const auto& rotation = transform(gctx).rotation();
0295
0296 const auto& localXAxis = rotation.col(0);
0297 const auto& localYAxis = rotation.col(1);
0298 const auto& localZAxis = rotation.col(2);
0299
0300 const auto localPos = (rotation.transpose() * position).eval();
0301 const auto dx = direction.dot(localXAxis);
0302 const auto dy = direction.dot(localYAxis);
0303 const auto dz = direction.dot(localZAxis);
0304
0305 const auto norm = 1 / (1 - dz * dz);
0306
0307 const auto& dirRowVec = direction.transpose();
0308
0309
0310
0311 const auto localXAxisToPath =
0312 (-2 * norm * (dx * pcRowVec + localPos.x() * dirRowVec)).eval();
0313 const auto localYAxisToPath =
0314 (-2 * norm * (dy * pcRowVec + localPos.y() * dirRowVec)).eval();
0315 const auto localZAxisToPath =
0316 (-4 * norm * norm * (dx * localPos.x() + dy * localPos.y()) * dz *
0317 dirRowVec)
0318 .eval();
0319
0320 const auto [rotToLocalXAxis, rotToLocalYAxis, rotToLocalZAxis] =
0321 detail::rotationToLocalAxesDerivative(rotation);
0322
0323
0324 AlignmentToPathMatrix alignToPath = AlignmentToPathMatrix::Zero();
0325 alignToPath.segment<3>(eAlignmentCenter0) =
0326 2 * norm * (dx * localXAxis.transpose() + dy * localYAxis.transpose());
0327 alignToPath.segment<3>(eAlignmentRotation0) =
0328 localXAxisToPath * rotToLocalXAxis + localYAxisToPath * rotToLocalYAxis +
0329 localZAxisToPath * rotToLocalZAxis;
0330
0331 return alignToPath;
0332 }
0333
0334 ActsMatrix<2, 3> CylinderSurface::localCartesianToBoundLocalDerivative(
0335 const GeometryContext& gctx, const Vector3& position) const {
0336 using VectorHelpers::perp;
0337 using VectorHelpers::phi;
0338
0339 const auto& sTransform = transform(gctx);
0340
0341 const Vector3 localPos = sTransform.inverse() * position;
0342 const double lr = perp(localPos);
0343 const double lphi = phi(localPos);
0344 const double lcphi = std::cos(lphi);
0345 const double lsphi = std::sin(lphi);
0346
0347 double R = bounds().get(CylinderBounds::eR);
0348 ActsMatrix<2, 3> loc3DToLocBound = ActsMatrix<2, 3>::Zero();
0349 loc3DToLocBound << -R * lsphi / lr, R * lcphi / lr, 0, 0, 0, 1;
0350
0351 return loc3DToLocBound;
0352 }
0353
0354 std::pair<std::shared_ptr<CylinderSurface>, bool> CylinderSurface::mergedWith(
0355 const CylinderSurface& other, AxisDirection direction,
0356 bool externalRotation, const Logger& logger) const {
0357 using namespace UnitLiterals;
0358
0359 ACTS_VERBOSE("Merging cylinder surfaces in " << axisDirectionName(direction)
0360 << " direction");
0361
0362 if (m_associatedDetElement != nullptr ||
0363 other.m_associatedDetElement != nullptr) {
0364 throw SurfaceMergingException(getSharedPtr(), other.getSharedPtr(),
0365 "CylinderSurface::merge: surfaces are "
0366 "associated with a detector element");
0367 }
0368
0369 assert(m_transform != nullptr && other.m_transform != nullptr);
0370
0371 Transform3 otherLocal = m_transform->inverse() * *other.m_transform;
0372
0373 constexpr auto tolerance = s_onSurfaceTolerance;
0374
0375
0376
0377 if (std::abs(otherLocal.linear().col(eX)[eZ]) >= tolerance ||
0378 std::abs(otherLocal.linear().col(eY)[eZ]) >= tolerance) {
0379 ACTS_ERROR("CylinderSurface::merge: surfaces have relative rotation");
0380 throw SurfaceMergingException(
0381 getSharedPtr(), other.getSharedPtr(),
0382 "CylinderSurface::merge: surfaces have relative rotation");
0383 }
0384
0385 auto checkNoBevel = [this, &logger, &other](const auto& bounds) {
0386 if (bounds.get(CylinderBounds::eBevelMinZ) != 0.0) {
0387 ACTS_ERROR(
0388 "CylinderVolumeStack requires all volumes to have a bevel angle of "
0389 "0");
0390 throw SurfaceMergingException(
0391 getSharedPtr(), other.getSharedPtr(),
0392 "CylinderVolumeStack requires all volumes to have a bevel angle of "
0393 "0");
0394 }
0395
0396 if (bounds.get(CylinderBounds::eBevelMaxZ) != 0.0) {
0397 ACTS_ERROR(
0398 "CylinderVolumeStack requires all volumes to have a bevel angle of "
0399 "0");
0400 throw SurfaceMergingException(
0401 getSharedPtr(), other.getSharedPtr(),
0402 "CylinderVolumeStack requires all volumes to have a bevel angle of "
0403 "0");
0404 }
0405 };
0406
0407 checkNoBevel(bounds());
0408 checkNoBevel(other.bounds());
0409
0410
0411 if (std::abs(bounds().get(CylinderBounds::eR) -
0412 other.bounds().get(CylinderBounds::eR)) > tolerance) {
0413 ACTS_ERROR("CylinderSurface::merge: surfaces have different radii");
0414 throw SurfaceMergingException(
0415 getSharedPtr(), other.getSharedPtr(),
0416 "CylinderSurface::merge: surfaces have different radii");
0417 }
0418
0419 double r = bounds().get(CylinderBounds::eR);
0420
0421
0422 Vector3 translation = otherLocal.translation();
0423
0424 if (std::abs(translation[0]) > tolerance ||
0425 std::abs(translation[1]) > tolerance) {
0426 ACTS_ERROR(
0427 "CylinderSurface::merge: surfaces have relative translation in x/y");
0428 throw SurfaceMergingException(
0429 getSharedPtr(), other.getSharedPtr(),
0430 "CylinderSurface::merge: surfaces have relative translation in x/y");
0431 }
0432
0433 double hlZ = bounds().get(CylinderBounds::eHalfLengthZ);
0434 double minZ = -hlZ;
0435 double maxZ = hlZ;
0436
0437 double zShift = translation[2];
0438 double otherHlZ = other.bounds().get(CylinderBounds::eHalfLengthZ);
0439 double otherMinZ = -otherHlZ + zShift;
0440 double otherMaxZ = otherHlZ + zShift;
0441
0442 double hlPhi = bounds().get(CylinderBounds::eHalfPhiSector);
0443 double avgPhi = bounds().get(CylinderBounds::eAveragePhi);
0444
0445 double otherHlPhi = other.bounds().get(CylinderBounds::eHalfPhiSector);
0446 double otherAvgPhi = other.bounds().get(CylinderBounds::eAveragePhi);
0447
0448 if (direction == AxisDirection::AxisZ) {
0449
0450
0451 if (std::abs(otherLocal.linear().col(eY)[eX]) >= tolerance &&
0452 (!bounds().coversFullAzimuth() ||
0453 !other.bounds().coversFullAzimuth())) {
0454 throw SurfaceMergingException(getSharedPtr(), other.getSharedPtr(),
0455 "CylinderSurface::merge: surfaces have "
0456 "relative rotation in z and phi sector");
0457 }
0458
0459 ACTS_VERBOSE("this: [" << minZ << ", " << maxZ << "] ~> "
0460 << (minZ + maxZ) / 2.0 << " +- " << hlZ);
0461 ACTS_VERBOSE("zShift: " << zShift);
0462
0463 ACTS_VERBOSE("other: [" << otherMinZ << ", " << otherMaxZ << "] ~> "
0464 << (otherMinZ + otherMaxZ) / 2.0 << " +- "
0465 << otherHlZ);
0466 if (std::abs(maxZ - otherMinZ) > tolerance &&
0467 std::abs(minZ - otherMaxZ) > tolerance) {
0468 ACTS_ERROR("CylinderSurface::merge: surfaces have incompatible z bounds");
0469 throw SurfaceMergingException(
0470 getSharedPtr(), other.getSharedPtr(),
0471 "CylinderSurface::merge: surfaces have incompatible z bounds");
0472 }
0473
0474 if (hlPhi != otherHlPhi || avgPhi != otherAvgPhi) {
0475 throw SurfaceMergingException(getSharedPtr(), other.getSharedPtr(),
0476 "CylinderSurface::merge: surfaces have "
0477 "different phi sectors");
0478 }
0479
0480 double newMaxZ = std::max(maxZ, otherMaxZ);
0481 double newMinZ = std::min(minZ, otherMinZ);
0482 double newHlZ = (newMaxZ - newMinZ) / 2.0;
0483 double newMidZ = (newMaxZ + newMinZ) / 2.0;
0484 ACTS_VERBOSE("merged: [" << newMinZ << ", " << newMaxZ << "] ~> " << newMidZ
0485 << " +- " << newHlZ);
0486
0487 auto newBounds = std::make_shared<CylinderBounds>(r, newHlZ, hlPhi, avgPhi);
0488
0489 Transform3 newTransform =
0490 *m_transform * Translation3{Vector3::UnitZ() * newMidZ};
0491
0492 return {Surface::makeShared<CylinderSurface>(newTransform, newBounds),
0493 zShift < 0};
0494
0495 } else if (direction == AxisDirection::AxisRPhi) {
0496
0497 if (std::abs(translation[2]) > tolerance) {
0498 ACTS_ERROR(
0499 "CylinderSurface::merge: surfaces have relative translation in z for "
0500 "rPhi merging");
0501 throw SurfaceMergingException(
0502 getSharedPtr(), other.getSharedPtr(),
0503 "CylinderSurface::merge: surfaces have relative translation in z for "
0504 "rPhi merging");
0505 }
0506
0507 if (hlZ != otherHlZ) {
0508 throw SurfaceMergingException(getSharedPtr(), other.getSharedPtr(),
0509 "CylinderSurface::merge: surfaces have "
0510 "different z bounds");
0511 }
0512
0513
0514 Vector2 rotatedX = otherLocal.linear().col(eX).head<2>();
0515 double zrotation = std::atan2(rotatedX[1], rotatedX[0]);
0516
0517 ACTS_VERBOSE("this: [" << avgPhi / 1_degree << " +- " << hlPhi / 1_degree
0518 << "]");
0519 ACTS_VERBOSE("other: [" << otherAvgPhi / 1_degree << " +- "
0520 << otherHlPhi / 1_degree << "]");
0521
0522 ACTS_VERBOSE("Relative rotation around local z: " << zrotation / 1_degree);
0523
0524 double prevOtherAvgPhi = otherAvgPhi;
0525 otherAvgPhi = detail::radian_sym(otherAvgPhi + zrotation);
0526 ACTS_VERBOSE("~> local other average phi: "
0527 << otherAvgPhi / 1_degree
0528 << " (was: " << prevOtherAvgPhi / 1_degree << ")");
0529
0530 try {
0531 auto [newHlPhi, newAvgPhi, reversed] = detail::mergedPhiSector(
0532 hlPhi, avgPhi, otherHlPhi, otherAvgPhi, logger, tolerance);
0533
0534 Transform3 newTransform = *m_transform;
0535
0536 if (externalRotation) {
0537 ACTS_VERBOSE("Modifying transform for external rotation of "
0538 << newAvgPhi / 1_degree);
0539 newTransform = newTransform * AngleAxis3(newAvgPhi, Vector3::UnitZ());
0540 newAvgPhi = 0.;
0541 }
0542
0543 auto newBounds = std::make_shared<CylinderBounds>(
0544 r, bounds().get(CylinderBounds::eHalfLengthZ), newHlPhi, newAvgPhi);
0545
0546 return {Surface::makeShared<CylinderSurface>(newTransform, newBounds),
0547 reversed};
0548 } catch (const std::invalid_argument& e) {
0549 throw SurfaceMergingException(getSharedPtr(), other.getSharedPtr(),
0550 e.what());
0551 }
0552 } else {
0553 throw SurfaceMergingException(getSharedPtr(), other.getSharedPtr(),
0554 "CylinderSurface::merge: invalid direction " +
0555 axisDirectionName(direction));
0556 }
0557 }
0558
0559 }