Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-11-16 09:23:49

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 "Acts/Surfaces/ConeSurface.hpp"
0010 
0011 #include "Acts/Geometry/GeometryObject.hpp"
0012 #include "Acts/Surfaces/BoundaryTolerance.hpp"
0013 #include "Acts/Surfaces/SurfaceError.hpp"
0014 #include "Acts/Surfaces/detail/AlignmentHelper.hpp"
0015 #include "Acts/Surfaces/detail/FacesHelper.hpp"
0016 #include "Acts/Surfaces/detail/VerticesHelper.hpp"
0017 #include "Acts/Utilities/AlgebraHelpers.hpp"
0018 #include "Acts/Utilities/Diagnostics.hpp"
0019 #include "Acts/Utilities/Intersection.hpp"
0020 #include "Acts/Utilities/ThrowAssert.hpp"
0021 #include "Acts/Utilities/detail/RealQuadraticEquation.hpp"
0022 
0023 #include <algorithm>
0024 #include <cmath>
0025 #include <limits>
0026 #include <numbers>
0027 #include <stdexcept>
0028 #include <utility>
0029 #include <vector>
0030 
0031 namespace Acts {
0032 
0033 using VectorHelpers::perp;
0034 using VectorHelpers::phi;
0035 
0036 namespace {
0037 /// Local azimuth and the bound axes at a cone position, in the global frame.
0038 /// radial = (cos phi, sin phi, 0), phiAxis = (-sin phi, cos phi, 0) and
0039 /// axis = ez. referenceFrame is (phiAxis, n x phiAxis, n). The bound z
0040 /// derivative tan(alpha)*radial - phi*tan(alpha)*phiAxis + axis is not that
0041 /// second column, so boundToFreeJacobian replaces it and freeToBoundJacobian
0042 /// replaces both loc rows.
0043 struct ConeBoundAxes {
0044   double localPhi;
0045   Vector3 radial;
0046   Vector3 phiAxis;
0047   Vector3 axis;
0048 };
0049 
0050 ConeBoundAxes coneBoundAxes(const Transform3& toGlobal,
0051                             const Vector3& position) {
0052   const Vector3 localPosition = toGlobal.inverse() * position;
0053   const double localPhi = phi(localPosition);
0054   const double cosPhi = std::cos(localPhi);
0055   const double sinPhi = std::sin(localPhi);
0056   const auto& rotation = toGlobal.rotation();
0057   return {localPhi, rotation * Vector3(cosPhi, sinPhi, 0.),
0058           rotation * Vector3(-sinPhi, cosPhi, 0.), rotation.col(2)};
0059 }
0060 }  // namespace
0061 
0062 ConeSurface::ConeSurface(const ConeSurface& other)
0063     : GeometryObject{}, RegularSurface(other), m_bounds(other.m_bounds) {}
0064 
0065 ConeSurface::ConeSurface(const GeometryContext& gctx, const ConeSurface& other,
0066                          const Transform3& shift)
0067     : RegularSurface(gctx, other, shift), m_bounds(other.m_bounds) {}
0068 
0069 ConeSurface::ConeSurface(const Transform3& transform, double alpha,
0070                          bool symmetric)
0071     : RegularSurface(transform),
0072       m_bounds(std::make_shared<const ConeBounds>(alpha, symmetric)) {}
0073 
0074 ConeSurface::ConeSurface(const Transform3& transform, double alpha, double zmin,
0075                          double zmax, double halfPhi)
0076     : RegularSurface(transform),
0077       m_bounds(std::make_shared<const ConeBounds>(alpha, zmin, zmax, halfPhi)) {
0078 }
0079 
0080 ConeSurface::ConeSurface(const Transform3& transform,
0081                          std::shared_ptr<const ConeBounds> cbounds)
0082     : RegularSurface(transform), m_bounds(std::move(cbounds)) {
0083   throw_assert(m_bounds, "ConeBounds must not be nullptr");
0084 }
0085 
0086 Vector3 ConeSurface::referencePosition(const GeometryContext& gctx,
0087                                        AxisDirection aDir) const {
0088   const Vector3& sfCenter = center(gctx);
0089 
0090   // special binning type for R-type methods
0091   if (aDir == AxisDirection::AxisR || aDir == AxisDirection::AxisRPhi) {
0092     return Vector3(sfCenter.x() + bounds().r(sfCenter.z()), sfCenter.y(),
0093                    sfCenter.z());
0094   }
0095   // give the center as default for all of these binning types
0096   // AxisDirection::AxisX, AxisDirection::AxisY, AxisDirection::AxisZ,
0097   // AxisDirection::AxisR, AxisDirection::AxisPhi, AxisDirection::AxisRPhi,
0098   // AxisDirection::AxisTheta, AxisDirection::AxisEta
0099   return sfCenter;
0100 }
0101 
0102 Surface::SurfaceType ConeSurface::type() const {
0103   return Surface::Cone;
0104 }
0105 
0106 ConeSurface& ConeSurface::operator=(const ConeSurface& other) {
0107   if (this != &other) {
0108     ACTS_PUSH_IGNORE_DEPRECATED()
0109     Surface::operator=(other);
0110     ACTS_POP_IGNORE_DEPRECATED()
0111     m_bounds = other.m_bounds;
0112   }
0113   return *this;
0114 }
0115 
0116 Vector3 ConeSurface::rotSymmetryAxis(const GeometryContext& gctx) const {
0117   return localToGlobalTransform(gctx).matrix().block<3, 1>(0, 2);
0118 }
0119 
0120 RotationMatrix3 ConeSurface::referenceFrame(
0121     const GeometryContext& gctx, const Vector3& position,
0122     const Vector3& /*direction*/) const {
0123   // Columns are (e_phi, n x e_phi, n). The normal is what the base
0124   // freeToPathDerivative projects onto.
0125   const Vector3 n = normal(gctx, position);
0126   const Transform3& toGlobal = localToGlobalTransform(gctx);
0127   const double localPhi = phi(toGlobal.inverse() * position);
0128   const Vector3 phiAxis = toGlobal.rotation() *
0129                           Vector3(-std::sin(localPhi), std::cos(localPhi), 0.);
0130   RotationMatrix3 frame;
0131   frame.col(0) = phiAxis;
0132   frame.col(1) = n.cross(phiAxis);
0133   frame.col(2) = n;
0134   return frame;
0135 }
0136 
0137 BoundToFreeMatrix ConeSurface::boundToFreeJacobian(
0138     const GeometryContext& gctx, const Vector3& position,
0139     const Vector3& direction) const {
0140   assert(isOnSurface(gctx, position, direction, BoundaryTolerance::Infinite()));
0141 
0142   // referenceFrame supplies e_phi as the loc0 column. Replace loc1.
0143   BoundToFreeMatrix jacToGlobal =
0144       Surface::boundToFreeJacobian(gctx, position, direction);
0145 
0146   const ConeBoundAxes axes =
0147       coneBoundAxes(localToGlobalTransform(gctx), position);
0148   const double tanAlpha = bounds().tanAlpha();
0149   jacToGlobal.block<3, 1>(eFreePos0, eBoundLoc1) =
0150       tanAlpha * axes.radial - axes.localPhi * tanAlpha * axes.phiAxis +
0151       axes.axis;
0152   return jacToGlobal;
0153 }
0154 
0155 FreeToBoundMatrix ConeSurface::freeToBoundJacobian(
0156     const GeometryContext& gctx, const Vector3& position,
0157     const Vector3& direction) const {
0158   assert(isOnSurface(gctx, position, direction, BoundaryTolerance::Infinite()));
0159 
0160   FreeToBoundMatrix jacToLocal =
0161       Surface::freeToBoundJacobian(gctx, position, direction);
0162 
0163   // On the surface R/r = 1, so the (rphi, z) rows are e_phi + phi*tan(alpha)*ez
0164   // and ez.
0165   const ConeBoundAxes axes =
0166       coneBoundAxes(localToGlobalTransform(gctx), position);
0167   const double tanAlpha = bounds().tanAlpha();
0168   jacToLocal.block<1, 3>(eBoundLoc0, eFreePos0) =
0169       (axes.phiAxis + axes.localPhi * tanAlpha * axes.axis).transpose();
0170   jacToLocal.block<1, 3>(eBoundLoc1, eFreePos0) = axes.axis.transpose();
0171   return jacToLocal;
0172 }
0173 
0174 Vector3 ConeSurface::localToGlobal(const GeometryContext& gctx,
0175                                    const Vector2& lposition) const {
0176   // create the position in the local 3d frame
0177   double r = lposition[1] * bounds().tanAlpha();
0178   double phi = lposition[0] / r;
0179   Vector3 loc3Dframe(r * std::cos(phi), r * std::sin(phi), lposition[1]);
0180   return localToGlobalTransform(gctx) * loc3Dframe;
0181 }
0182 
0183 Result<Vector2> ConeSurface::globalToLocal(const GeometryContext& gctx,
0184                                            const Vector3& position,
0185                                            double tolerance) const {
0186   Vector3 loc3Dframe = localToGlobalTransform(gctx).inverse() * position;
0187   double r = loc3Dframe.z() * bounds().tanAlpha();
0188   if (std::abs(perp(loc3Dframe) - r) > tolerance) {
0189     return Result<Vector2>::failure(SurfaceError::GlobalPositionNotOnSurface);
0190   }
0191   return Result<Vector2>::success(
0192       Vector2(r * std::atan2(loc3Dframe.y(), loc3Dframe.x()), loc3Dframe.z()));
0193 }
0194 
0195 double ConeSurface::pathCorrection(const GeometryContext& gctx,
0196                                    const Vector3& position,
0197                                    const Vector3& direction) const {
0198   return std::abs(1. / normal(gctx, position).dot(direction));
0199 }
0200 
0201 std::string ConeSurface::name() const {
0202   return "Acts::ConeSurface";
0203 }
0204 
0205 Vector3 ConeSurface::normal(const GeometryContext& gctx,
0206                             const Vector2& lposition) const {
0207   // (cos phi cos alpha, sin phi cos alpha, sgn z sin alpha)
0208   double phi = lposition[0] / (bounds().r(lposition[1])),
0209          sgn = -std::copysign(1., lposition[1]);
0210   double cosAlpha = std::cos(bounds().get(ConeBounds::eAlpha));
0211   double sinAlpha = std::sin(bounds().get(ConeBounds::eAlpha));
0212   Vector3 localNormal(std::cos(phi) * cosAlpha, std::sin(phi) * cosAlpha,
0213                       sgn * sinAlpha);
0214   return Vector3(localToGlobalTransform(gctx).linear() * localNormal);
0215 }
0216 
0217 Vector3 ConeSurface::normal(const GeometryContext& gctx,
0218                             const Vector3& position) const {
0219   // (cos phi cos alpha, sin phi cos alpha, -sign(z) sin alpha)
0220   const Transform3& toGlobal = localToGlobalTransform(gctx);
0221   const Vector3 localPosition = toGlobal.inverse() * position;
0222   const double localPhi = phi(localPosition);
0223   const double cosAlpha = std::cos(bounds().get(ConeBounds::eAlpha));
0224   const double sinAlpha = std::sin(bounds().get(ConeBounds::eAlpha));
0225   const double sgn = -std::copysign(1., localPosition.z());
0226   const Vector3 localNormal(std::cos(localPhi) * cosAlpha,
0227                             std::sin(localPhi) * cosAlpha, sgn * sinAlpha);
0228   return toGlobal.linear() * localNormal;
0229 }
0230 
0231 const ConeBounds& ConeSurface::bounds() const {
0232   // is safe because no constructor w/o bounds exists
0233   return *m_bounds;
0234 }
0235 
0236 Polyhedron ConeSurface::polyhedronRepresentation(
0237     const GeometryContext& gctx, unsigned int quarterSegments) const {
0238   // Prepare vertices and faces
0239   std::vector<Vector3> vertices;
0240   std::vector<Polyhedron::FaceType> faces;
0241   std::vector<Polyhedron::FaceType> triangularMesh;
0242   double minZ = bounds().get(ConeBounds::eMinZ);
0243   double maxZ = bounds().get(ConeBounds::eMaxZ);
0244 
0245   if (minZ == -std::numeric_limits<double>::infinity() ||
0246       maxZ == std::numeric_limits<double>::infinity()) {
0247     throw std::domain_error(
0248         "Polyhedron representation of boundless surface is not possible");
0249   }
0250 
0251   auto ctransform = localToGlobalTransform(gctx);
0252 
0253   // The tip - created only once and only, if it is not a cut-off cone
0254   bool tipExists = false;
0255   if (minZ * maxZ <= s_onSurfaceTolerance) {
0256     vertices.push_back(ctransform * Vector3(0., 0., 0.));
0257     tipExists = true;
0258   }
0259 
0260   // Cone parameters
0261   double hPhiSec = bounds().get(ConeBounds::eHalfPhiSector);
0262   double avgPhi = bounds().get(ConeBounds::eAveragePhi);
0263   std::vector<double> refPhi = {};
0264   if (bool fullCone =
0265           std::abs(hPhiSec - std::numbers::pi) < s_fullAzimuthTolerance;
0266       !fullCone) {
0267     refPhi = {avgPhi};
0268   }
0269 
0270   // Add the cone sizes
0271   std::vector<double> coneSides;
0272   if (std::abs(minZ) > s_onSurfaceTolerance) {
0273     coneSides.push_back(minZ);
0274   }
0275   if (std::abs(maxZ) > s_onSurfaceTolerance) {
0276     coneSides.push_back(maxZ);
0277   }
0278 
0279   for (auto& z : coneSides) {
0280     std::size_t firstIv = vertices.size();
0281     // Radius and z offset
0282     double r = std::abs(z) * bounds().tanAlpha();
0283     Vector3 zoffset(0., 0., z);
0284     auto svertices = detail::VerticesHelper::segmentVertices(
0285         {r, r}, avgPhi - hPhiSec, avgPhi + hPhiSec, refPhi, quarterSegments,
0286         zoffset, ctransform);
0287     vertices.insert(vertices.end(), svertices.begin(), svertices.end());
0288     // If the tip exists, the faces need to be triangular
0289     if (tipExists) {
0290       for (std::size_t iv = firstIv + 1; iv < svertices.size() + firstIv;
0291            ++iv) {
0292         std::size_t one = 0, two = iv, three = iv - 1;
0293         if (z < 0.) {
0294           std::swap(two, three);
0295         }
0296         faces.push_back({one, two, three});
0297       }
0298     }
0299   }
0300 
0301   // if no tip exists, connect the two bows
0302   if (tipExists) {
0303     triangularMesh = faces;
0304   } else {
0305     auto facesMesh = detail::FacesHelper::cylindricalFaceMesh(vertices);
0306     faces = facesMesh.first;
0307     triangularMesh = facesMesh.second;
0308   }
0309 
0310   return Polyhedron(vertices, faces, triangularMesh, false);
0311 }
0312 
0313 detail::RealQuadraticEquation ConeSurface::intersectionSolver(
0314     const GeometryContext& gctx, const Vector3& position,
0315     const Vector3& direction) const {
0316   // Transform into the local frame
0317   Transform3 invTrans = localToGlobalTransform(gctx).inverse();
0318   Vector3 point1 = invTrans * position;
0319   Vector3 dir1 = invTrans.linear() * direction;
0320 
0321   // See file header for the formula derivation
0322   double tan2Alpha = bounds().tanAlpha() * bounds().tanAlpha(),
0323          A = dir1.x() * dir1.x() + dir1.y() * dir1.y() -
0324              tan2Alpha * dir1.z() * dir1.z(),
0325          B = 2 * (dir1.x() * point1.x() + dir1.y() * point1.y() -
0326                   tan2Alpha * dir1.z() * point1.z()),
0327          C = point1.x() * point1.x() + point1.y() * point1.y() -
0328              tan2Alpha * point1.z() * point1.z();
0329   if (A == 0.) {
0330     A += 1e-16;  // avoid division by zero
0331   }
0332 
0333   return detail::RealQuadraticEquation(A, B, C);
0334 }
0335 
0336 MultiIntersection3D ConeSurface::intersect(
0337     const GeometryContext& gctx, const Vector3& position,
0338     const Vector3& direction, const BoundaryTolerance& boundaryTolerance,
0339     double tolerance) const {
0340   // Solve the quadratic equation
0341   auto qe = intersectionSolver(gctx, position, direction);
0342 
0343   // If no valid solution return a non-valid surfaceIntersection
0344   if (qe.solutions == 0) {
0345     return MultiIntersection3D(Intersection3D::Invalid(),
0346                                Intersection3D::Invalid());
0347   }
0348 
0349   // Check the validity of the first solution
0350   Vector3 solution1 = position + qe.first * direction;
0351   IntersectionStatus status1 = std::abs(qe.first) < std::abs(tolerance)
0352                                    ? IntersectionStatus::onSurface
0353                                    : IntersectionStatus::reachable;
0354 
0355   if (!boundaryTolerance.isInfinite() &&
0356       !isOnSurface(gctx, solution1, direction, boundaryTolerance)) {
0357     status1 = IntersectionStatus::unreachable;
0358   }
0359 
0360   // Check the validity of the second solution
0361   Vector3 solution2 = position + qe.second * direction;
0362   IntersectionStatus status2 = std::abs(qe.second) < std::abs(tolerance)
0363                                    ? IntersectionStatus::onSurface
0364                                    : IntersectionStatus::reachable;
0365   if (!boundaryTolerance.isInfinite() &&
0366       !isOnSurface(gctx, solution2, direction, boundaryTolerance)) {
0367     status2 = IntersectionStatus::unreachable;
0368   }
0369 
0370   // solution1/2 are already in the global frame.
0371   Intersection3D first(solution1, qe.first, status1);
0372   Intersection3D second(solution2, qe.second, status2);
0373   // Order based on path length
0374   if (first.pathLength() <= second.pathLength()) {
0375     return MultiIntersection3D(first, second);
0376   }
0377   return MultiIntersection3D(second, first);
0378 }
0379 
0380 AlignmentToPathMatrix ConeSurface::alignmentToPathDerivative(
0381     const GeometryContext& gctx, const Vector3& position,
0382     const Vector3& direction) const {
0383   assert(isOnSurface(gctx, position, direction, BoundaryTolerance::Infinite()));
0384 
0385   // Local-frame constraint F(x, y, z) = x^2 + y^2 - z^2 tan^2(alpha) = 0.
0386   // Implicit differentiation at the current position gives the
0387   // alignment-to-path derivative.
0388   const auto& rotation = localToGlobalTransform(gctx).rotation();
0389   const Vector3 localPos = rotation.transpose() * (position - center(gctx));
0390   const Vector3 localDir = rotation.transpose() * direction;
0391   const double tanAlpha2 = bounds().tanAlpha() * bounds().tanAlpha();
0392   // Vanishes when the track is tangent to the cone.
0393   const double denom = localPos.x() * localDir.x() +
0394                        localPos.y() * localDir.y() -
0395                        tanAlpha2 * localPos.z() * localDir.z();
0396 
0397   AlignmentToPathMatrix alignToPath = AlignmentToPathMatrix::Zero();
0398   const Vector3 globalCenterDerivative =
0399       (localPos.x() * rotation.col(0) + localPos.y() * rotation.col(1) -
0400        tanAlpha2 * localPos.z() * rotation.col(2)) /
0401       denom;
0402   detail::setAlignToPathLocalCenterDerivative(alignToPath,
0403                                               globalCenterDerivative, rotation);
0404   // A rotation w about the local axes moves the surface point at localPos by
0405   // cross(w, localPos), so ds/dw = cross(localPos, grad(F) / 2) / denom
0406   // = (1 + tan^2(alpha)) z (-y, x, 0) / denom.
0407   const double rotationScale = (1. + tanAlpha2) * localPos.z() / denom;
0408   alignToPath.segment<3>(eAlignmentRotation0) =
0409       Vector3(-rotationScale * localPos.y(), rotationScale * localPos.x(), 0.)
0410           .transpose();
0411 
0412   return alignToPath;
0413 }
0414 
0415 Matrix<2, 3> ConeSurface::localCartesianToBoundLocalDerivative(
0416     const GeometryContext& gctx, const Vector3& position,
0417     const Vector3& /*direction*/) const {
0418   using VectorHelpers::perp;
0419   using VectorHelpers::phi;
0420   // The local frame transform
0421   const auto& sTransform = localToGlobalTransform(gctx);
0422   // calculate the transformation to local coordinates
0423   const Vector3 localPos = sTransform.inverse() * position;
0424   const double lr = perp(localPos);
0425   const double lphi = phi(localPos);
0426   const double lcphi = std::cos(lphi);
0427   const double lsphi = std::sin(lphi);
0428   // Solve for radius R
0429   const double R = localPos.z() * bounds().tanAlpha();
0430   Matrix<2, 3> loc3DToLocBound = Matrix<2, 3>::Zero();
0431   loc3DToLocBound << -R * lsphi / lr, R * lcphi / lr,
0432       lphi * bounds().tanAlpha(), 0, 0, 1;
0433 
0434   return loc3DToLocBound;
0435 }
0436 
0437 const std::shared_ptr<const ConeBounds>& ConeSurface::boundsPtr() const {
0438   return m_bounds;
0439 }
0440 
0441 void ConeSurface::assignSurfaceBounds(
0442     std::shared_ptr<const ConeBounds> newBounds) {
0443   m_bounds = std::move(newBounds);
0444 }
0445 
0446 }  // namespace Acts