Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-11-20 09:26:01

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/PlaneSurface.hpp"
0010 
0011 #include "Acts/Definitions/Algebra.hpp"
0012 #include "Acts/Geometry/GeometryObject.hpp"
0013 #include "Acts/Surfaces/BoundaryTolerance.hpp"
0014 #include "Acts/Surfaces/CurvilinearSurface.hpp"
0015 #include "Acts/Surfaces/EllipseBounds.hpp"
0016 #include "Acts/Surfaces/InfiniteBounds.hpp"
0017 #include "Acts/Surfaces/PlanarBounds.hpp"
0018 #include "Acts/Surfaces/RectangleBounds.hpp"
0019 #include "Acts/Surfaces/SurfaceBounds.hpp"
0020 #include "Acts/Surfaces/SurfaceError.hpp"
0021 #include "Acts/Surfaces/SurfaceMergingException.hpp"
0022 #include "Acts/Surfaces/detail/FacesHelper.hpp"
0023 #include "Acts/Surfaces/detail/PlanarHelper.hpp"
0024 #include "Acts/Utilities/AlgebraHelpers.hpp"
0025 #include "Acts/Utilities/Diagnostics.hpp"
0026 #include "Acts/Utilities/Intersection.hpp"
0027 #include "Acts/Utilities/ThrowAssert.hpp"
0028 
0029 #include <algorithm>
0030 #include <cmath>
0031 #include <numbers>
0032 #include <numeric>
0033 #include <stdexcept>
0034 #include <utility>
0035 #include <vector>
0036 
0037 namespace Acts {
0038 
0039 PlaneSurface::PlaneSurface(const PlaneSurface& other)
0040     : GeometryObject{}, RegularSurface(other), m_bounds(other.m_bounds) {}
0041 
0042 PlaneSurface::PlaneSurface(const GeometryContext& gctx,
0043                            const PlaneSurface& other,
0044                            const Transform3& transform)
0045     : RegularSurface(gctx, other, transform), m_bounds(other.m_bounds) {}
0046 
0047 PlaneSurface::PlaneSurface(std::shared_ptr<const PlanarBounds> pbounds,
0048                            const SurfacePlacementBase& placement)
0049     : RegularSurface{placement}, m_bounds(std::move(pbounds)) {
0050   // surfaces representing a detector element must have bounds
0051   throw_assert(m_bounds, "PlaneBounds must not be nullptr");
0052 }
0053 
0054 PlaneSurface::PlaneSurface(const Transform3& transform,
0055                            std::shared_ptr<const PlanarBounds> pbounds)
0056     : RegularSurface(transform), m_bounds(std::move(pbounds)) {}
0057 
0058 PlaneSurface& PlaneSurface::operator=(const PlaneSurface& other) {
0059   if (this != &other) {
0060     ACTS_PUSH_IGNORE_DEPRECATED()
0061     Surface::operator=(other);
0062     ACTS_POP_IGNORE_DEPRECATED()
0063     m_bounds = other.m_bounds;
0064   }
0065   return *this;
0066 }
0067 
0068 RotationMatrix3 PlaneSurface::referenceFrame(
0069     const GeometryContext& gctx) const {
0070   return localToGlobalTransform(gctx).matrix().block<3, 3>(0, 0);
0071 }
0072 
0073 Surface::SurfaceType PlaneSurface::type() const {
0074   return Surface::Plane;
0075 }
0076 
0077 Vector3 PlaneSurface::localToGlobal(const GeometryContext& gctx,
0078                                     const Vector2& lposition) const {
0079   return localToGlobalTransform(gctx) * Vector3(lposition[0], lposition[1], 0.);
0080 }
0081 
0082 Result<Vector2> PlaneSurface::globalToLocal(const GeometryContext& gctx,
0083                                             const Vector3& position,
0084                                             double tolerance) const {
0085   Vector3 loc3Dframe = localToGlobalTransform(gctx).inverse() * position;
0086   if (std::abs(loc3Dframe.z()) > std::abs(tolerance)) {
0087     return Result<Vector2>::failure(SurfaceError::GlobalPositionNotOnSurface);
0088   }
0089   return Result<Vector2>::success({loc3Dframe.x(), loc3Dframe.y()});
0090 }
0091 
0092 std::string PlaneSurface::name() const {
0093   return "Acts::PlaneSurface";
0094 }
0095 
0096 const SurfaceBounds& PlaneSurface::bounds() const {
0097   if (m_bounds) {
0098     return *m_bounds;
0099   }
0100   return s_noBounds;
0101 }
0102 
0103 Polyhedron PlaneSurface::polyhedronRepresentation(
0104     const GeometryContext& gctx, unsigned int quarterSegments) const {
0105   // Prepare vertices and faces
0106   std::vector<Vector3> vertices;
0107   bool exactPolyhedron = true;
0108 
0109   // If you have bounds you can create a polyhedron representation
0110   if (m_bounds) {
0111     auto vertices2D = m_bounds->vertices(quarterSegments);
0112     vertices.reserve(vertices2D.size() + 1);
0113     for (const auto& v2D : vertices2D) {
0114       vertices.push_back(localToGlobalTransform(gctx) *
0115                          Vector3(v2D.x(), v2D.y(), 0.));
0116     }
0117     bool isEllipse = bounds().type() == SurfaceBounds::eEllipse;
0118     bool innerExists = false, coversFull = false;
0119     if (isEllipse) {
0120       exactPolyhedron = false;
0121       auto vStore = bounds().values();
0122       innerExists = vStore[EllipseBounds::eInnerRx] > s_epsilon &&
0123                     vStore[EllipseBounds::eInnerRy] > s_epsilon;
0124       coversFull = std::abs(vStore[EllipseBounds::eHalfPhiSector] -
0125                             std::numbers::pi) < s_epsilon;
0126     }
0127     // All of those can be described as convex
0128     // @todo same as for Discs: coversFull is not the right criterium
0129     // for triangulation
0130     if (!isEllipse || !innerExists || !coversFull) {
0131       auto [faces, triangularMesh] =
0132           detail::FacesHelper::convexFaceMesh(vertices);
0133       return Polyhedron(vertices, faces, triangularMesh, exactPolyhedron);
0134     } else {
0135       // Two concentric rings, we use the pure concentric method momentarily,
0136       // but that creates too  many unneccesarry faces, when only two
0137       // are needed to describe the mesh, @todo investigate merging flag
0138       auto [faces, triangularMesh] =
0139           detail::FacesHelper::cylindricalFaceMesh(vertices);
0140       return Polyhedron(vertices, faces, triangularMesh, exactPolyhedron);
0141     }
0142   }
0143   throw std::domain_error(
0144       "Polyhedron representation of boundless surface not possible.");
0145 }
0146 
0147 Vector3 PlaneSurface::normal(const GeometryContext& gctx,
0148                              const Vector2& /*lpos*/) const {
0149   return normal(gctx);
0150 }
0151 
0152 Vector3 PlaneSurface::normal(const GeometryContext& gctx,
0153                              const Vector3& /*pos*/) const {
0154   return normal(gctx);
0155 }
0156 
0157 Vector3 PlaneSurface::normal(const GeometryContext& gctx) const {
0158   return localToGlobalTransform(gctx).linear().col(2);
0159 }
0160 
0161 Vector3 PlaneSurface::referencePosition(const GeometryContext& gctx,
0162                                         AxisDirection /*aDir*/) const {
0163   return center(gctx);
0164 }
0165 
0166 double PlaneSurface::pathCorrection(const GeometryContext& gctx,
0167                                     const Vector3& /*position*/,
0168                                     const Vector3& direction) const {
0169   // We can ignore the global position here
0170   return 1. / std::abs(normal(gctx).dot(direction));
0171 }
0172 
0173 namespace {
0174 
0175 // Apply identical physical bounds to ray and curvature-corrected predictions.
0176 inline MultiIntersection3D boundedIntersection(
0177     const PlaneSurface& surface, const Transform3& gctxTransform,
0178     const Intersection3D& intersection,
0179     const BoundaryTolerance& boundaryTolerance) {
0180   auto status = intersection.status();
0181   // Evaluate boundary check if requested (and reachable)
0182   if (intersection.status() != IntersectionStatus::unreachable) {
0183     // Built-in local to global for speed reasons
0184     const auto& tMatrix = gctxTransform.matrix();
0185     // Create the reference vector in local
0186     const Vector3 vecLocal(intersection.position() - tMatrix.block<3, 1>(0, 3));
0187     if (!surface.insideBounds(tMatrix.block<3, 2>(0, 0).transpose() * vecLocal,
0188                               boundaryTolerance)) {
0189       status = IntersectionStatus::unreachable;
0190     }
0191   }
0192   return MultiIntersection3D(Intersection3D(intersection.position(),
0193                                             intersection.pathLength(), status));
0194 }
0195 
0196 }  // namespace
0197 
0198 MultiIntersection3D PlaneSurface::intersect(
0199     const GeometryContext& gctx, const Vector3& position,
0200     const Vector3& direction, const BoundaryTolerance& boundaryTolerance,
0201     double tolerance) const {
0202   // Get the contextual transform
0203   const auto& gctxTransform = localToGlobalTransform(gctx);
0204   // Use the intersection helper for planar surfaces
0205   auto intersection = Acts::detail::PlanarHelper::intersect(
0206       gctxTransform, position, direction, tolerance);
0207   return boundedIntersection(*this, gctxTransform, intersection,
0208                              boundaryTolerance);
0209 }
0210 
0211 MultiIntersection3D PlaneSurface::intersect(
0212     const GeometryContext& gctx, const Vector3& position,
0213     const Vector3& direction, const Vector3& curvature,
0214     const BoundaryTolerance& boundaryTolerance, double tolerance) const {
0215   if (curvature.isZero(0.) || !curvature.allFinite()) {
0216     return intersect(gctx, position, direction, boundaryTolerance, tolerance);
0217   }
0218   // Get the contextual transform
0219   const auto& gctxTransform = localToGlobalTransform(gctx);
0220   // Use the intersection helper for planar surfaces
0221   auto intersection = detail::PlanarHelper::intersect(
0222       gctxTransform, position, direction, curvature, tolerance);
0223   return boundedIntersection(*this, gctxTransform, intersection,
0224                              boundaryTolerance);
0225 }
0226 
0227 Matrix<2, 3> PlaneSurface::localCartesianToBoundLocalDerivative(
0228     const GeometryContext& /*gctx*/, const Vector3& /*position*/,
0229     const Vector3& /*direction*/) const {
0230   const Matrix<2, 3> loc3DToLocBound = Matrix<2, 3>::Identity();
0231   return loc3DToLocBound;
0232 }
0233 
0234 bool PlaneSurface::contains(const GeometryContext& gctx, const Surface& other,
0235                             double tolerance) const {
0236   const auto* plane = dynamic_cast<const PlaneSurface*>(&other);
0237   if (plane == nullptr) {
0238     return false;
0239   }
0240   // Polygons, so that the vertices describe the bounds exactly. A polygon
0241   // lies within a convex polygon if its vertices do.
0242   auto isPolygon = [](const SurfaceBounds& bounds) {
0243     switch (bounds.type()) {
0244       case SurfaceBounds::eRectangle:
0245       case SurfaceBounds::eTrapezoid:
0246       case SurfaceBounds::eDiamond:
0247       case SurfaceBounds::eConvexPolygon:
0248         return true;
0249       default:
0250         return false;
0251     }
0252   };
0253   if (!isPolygon(bounds()) || !isPolygon(plane->bounds())) {
0254     return false;
0255   }
0256 
0257   // The other surface in the frame of this one
0258   const Transform3 otherLocal = localToGlobalTransform(gctx).inverse() *
0259                                 plane->localToGlobalTransform(gctx);
0260   if (otherLocal.linear().col(2).head<2>().norm() > tolerance ||
0261       std::abs(otherLocal.translation().z()) > tolerance) {
0262     return false;
0263   }
0264 
0265   return std::ranges::all_of(
0266       plane->m_bounds->vertices(), [&](const Vector2& vertex) {
0267         const Vector2 local =
0268             (otherLocal * Vector3{vertex.x(), vertex.y(), 0}).head<2>();
0269         return bounds().inside(local) || bounds().distance(local) <= tolerance;
0270       });
0271 }
0272 
0273 std::pair<std::shared_ptr<PlaneSurface>, bool> PlaneSurface::mergedWith(
0274     const PlaneSurface& other, AxisDirection direction,
0275     const Logger& logger) const {
0276   ACTS_VERBOSE("Merging plane surfaces in " << axisDirectionName(direction)
0277                                             << " direction");
0278 
0279   if (isAlignable() || other.isAlignable()) {
0280     throw SurfaceMergingException(getSharedPtr(), other.getSharedPtr(),
0281                                   "PlaneSurface::merge: surfaces are "
0282                                   "associated with a detector element");
0283   }
0284 
0285   assert(m_transform != nullptr && other.m_transform != nullptr);
0286 
0287   Transform3 otherLocal = m_transform->inverse() * *other.m_transform;
0288 
0289   // TODO: Is it a good tolerance?
0290   constexpr auto tolerance = s_onSurfaceTolerance;
0291 
0292   // Surface cannot have any relative rotation
0293   if ((otherLocal.rotation().matrix() - RotationMatrix3::Identity()).norm() >
0294       tolerance) {
0295     ACTS_ERROR("PlaneSurface::merge: surfaces have relative rotation");
0296     throw SurfaceMergingException(
0297         getSharedPtr(), other.getSharedPtr(),
0298         "PlaneSurface::merge: surfaces have relative rotation");
0299   }
0300 
0301   const auto* thisBounds = dynamic_cast<const RectangleBounds*>(&bounds());
0302   const auto* otherBounds =
0303       dynamic_cast<const RectangleBounds*>(&other.bounds());
0304 
0305   if (thisBounds == nullptr || otherBounds == nullptr) {
0306     throw SurfaceMergingException(
0307         getSharedPtr(), other.getSharedPtr(),
0308         "PlaneSurface::merge: only Rectangle Bounds are supported");
0309   }
0310 
0311   if (direction != AxisDirection::AxisX && direction != AxisDirection::AxisY) {
0312     throw SurfaceMergingException(getSharedPtr(), other.getSharedPtr(),
0313                                   "PlaneSurface::merge: invalid direction " +
0314                                       axisDirectionName(direction));
0315   }
0316 
0317   bool mergeX = direction == AxisDirection::AxisX;
0318 
0319   double thisHalfMerge =
0320       mergeX ? thisBounds->halfLengthX() : thisBounds->halfLengthY();
0321   double otherHalfMerge =
0322       mergeX ? otherBounds->halfLengthX() : otherBounds->halfLengthY();
0323 
0324   double thisHalfNonMerge =
0325       mergeX ? thisBounds->halfLengthY() : thisBounds->halfLengthX();
0326   double otherHalfNonMerge =
0327       mergeX ? otherBounds->halfLengthY() : otherBounds->halfLengthX();
0328 
0329   if (std::abs(thisHalfNonMerge - otherHalfNonMerge) > tolerance) {
0330     ACTS_ERROR(
0331         "PlaneSurface::merge: surfaces have different non-merging lengths");
0332     throw SurfaceMergingException(
0333         getSharedPtr(), other.getSharedPtr(),
0334         "PlaneSurface::merge: surfaces have different non-merging lengths");
0335   }
0336   Vector3 otherTranslation = otherLocal.translation();
0337 
0338   // No translation in non-merging direction/z is allowed
0339   double nonMergeShift = mergeX ? otherTranslation.y() : otherTranslation.x();
0340 
0341   if (std::abs(nonMergeShift) > tolerance ||
0342       std::abs(otherTranslation.z()) > tolerance) {
0343     ACTS_ERROR(
0344         "PlaneSurface::merge: surfaces have relative translation in y/z");
0345     throw SurfaceMergingException(
0346         getSharedPtr(), other.getSharedPtr(),
0347         "PlaneSurface::merge: surfaces have relative translation in y/z");
0348   }
0349 
0350   double mergeShift = mergeX ? otherTranslation.x() : otherTranslation.y();
0351 
0352   double thisMinMerge = -thisHalfMerge;
0353   double thisMaxMerge = thisHalfMerge;
0354 
0355   double otherMinMerge = mergeShift - otherHalfMerge;
0356   double otherMaxMerge = mergeShift + otherHalfMerge;
0357 
0358   // Surfaces have to "touch" along merging direction
0359   if (std::abs(thisMaxMerge - otherMinMerge) > tolerance &&
0360       std::abs(thisMinMerge - otherMaxMerge) > tolerance) {
0361     ACTS_ERROR(
0362         "PlaneSurface::merge: surfaces have incompatible merge bound location");
0363     throw SurfaceMergingException(
0364         getSharedPtr(), other.getSharedPtr(),
0365         "PlaneSurface::merge: surfaces have incompatible merge bound location");
0366   }
0367 
0368   double newMaxMerge = std::max(thisMaxMerge, otherMaxMerge);
0369   double newMinMerge = std::min(thisMinMerge, otherMinMerge);
0370 
0371   double newHalfMerge = std::midpoint(newMaxMerge, -newMinMerge);
0372   double newMidMerge = std::midpoint(newMaxMerge, newMinMerge);
0373 
0374   auto newBounds =
0375       mergeX
0376           ? std::make_shared<RectangleBounds>(newHalfMerge, thisHalfNonMerge)
0377           : std::make_shared<RectangleBounds>(thisHalfNonMerge, newHalfMerge);
0378 
0379   Vector3 unitDir = mergeX ? Vector3::UnitX() : Vector3::UnitY();
0380   Transform3 newTransform = *m_transform * Translation3{unitDir * newMidMerge};
0381   return {Surface::makeShared<PlaneSurface>(newTransform, newBounds),
0382           mergeShift < 0};
0383 }
0384 const std::shared_ptr<const PlanarBounds>& PlaneSurface::boundsPtr() const {
0385   return m_bounds;
0386 }
0387 
0388 void PlaneSurface::assignSurfaceBounds(
0389     std::shared_ptr<const PlanarBounds> newBounds) {
0390   m_bounds = std::move(newBounds);
0391 }
0392 
0393 }  // namespace Acts