Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-10-15 08:05:03

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 "ActsPlugins/GeoModel/detail/GeoPolygonConverter.hpp"
0010 
0011 #include "Acts/Definitions/Common.hpp"
0012 #include "Acts/Definitions/Units.hpp"
0013 #include "Acts/Surfaces/DiamondBounds.hpp"
0014 #include "Acts/Surfaces/PlaneSurface.hpp"
0015 #include "Acts/Surfaces/RectangleBounds.hpp"
0016 #include "Acts/Surfaces/Surface.hpp"
0017 #include "Acts/Surfaces/TrapezoidBounds.hpp"
0018 #include "ActsPlugins/GeoModel/GeoModelConversionError.hpp"
0019 
0020 #include <algorithm>
0021 
0022 #include <GeoModelKernel/GeoBox.h>
0023 #include <GeoModelKernel/GeoFullPhysVol.h>
0024 #include <GeoModelKernel/GeoLogVol.h>
0025 #include <GeoModelKernel/GeoShape.h>
0026 #include <GeoModelKernel/Units.h>
0027 
0028 using namespace Acts;
0029 
0030 Result<ActsPlugins::GeoModelSensitiveSurface>
0031 ActsPlugins::detail::GeoPolygonConverter::operator()(
0032     const PVConstLink& geoPV, const GeoSimplePolygonBrep& polygon,
0033     const Transform3& absTransform, SurfaceBoundFactory& boundFactory,
0034     bool sensitive) const {
0035   /// auto-calculate the unit length conversion
0036   static constexpr double unitLength =
0037       UnitConstants::mm / GeoModelKernelUnits::millimeter;
0038 
0039   // Create the surface transform
0040   Transform3 transform = Transform3::Identity();
0041   transform.translation() = unitLength * absTransform.translation();
0042   auto rotation = absTransform.rotation();
0043   // Get the half lengths
0044   int nVertices = polygon.getNVertices();
0045   std::vector<std::vector<double>> vertices;
0046 
0047   for (int i = 0; i < nVertices; i++) {
0048     vertices.push_back({polygon.getXVertex(i), polygon.getYVertex(i)});
0049   }
0050   // sort based on the y-coordinate
0051   std::ranges::sort(vertices, {}, [](const auto& v) { return v[1]; });
0052   if (nVertices == 4) {
0053     double hlxnegy = std::abs(vertices[0][0] - vertices[1][0]) / 2;
0054     double hlxposy = std::abs(vertices[2][0] - vertices[3][0]) / 2;
0055     double hly = std::abs(vertices[0][1] - vertices[3][1]) / 2;
0056     std::vector<double> halfLengths = {hlxnegy, hlxposy, hly};
0057 
0058     // Create the surface
0059     Vector3 colX = rotation.col(0);
0060     Vector3 colY = rotation.col(1);
0061     Vector3 colZ = rotation.col(2);
0062     rotation.col(0) = colX;
0063     rotation.col(1) = colY;
0064     rotation.col(2) = colZ;
0065     transform.linear() = rotation;
0066     // Create the surface bounds
0067     double halfXnegY = unitLength * halfLengths[0];
0068     double halfXposY = unitLength * halfLengths[1];
0069     double halfY = unitLength * halfLengths[2];
0070     auto trapBounds =
0071         boundFactory.makeBounds<TrapezoidBounds>(halfXnegY, halfXposY, halfY);
0072     if (!sensitive) {
0073       auto surface = Surface::makeShared<PlaneSurface>(transform, trapBounds);
0074       return std::make_tuple(nullptr, surface);
0075     }
0076     // Create the element and the surface
0077     auto detectorElement =
0078         GeoModelDetectorElement::createDetectorElement<PlaneSurface>(
0079             geoPV, trapBounds, transform, unitLength * polygon.getDZ());
0080     auto surface = detectorElement->surface().getSharedPtr();
0081     // Return the detector element and surface
0082     return std::make_tuple(detectorElement, surface);
0083   } else if (nVertices == 6) {
0084     double hlxnegy = std::abs(vertices[0][0] - vertices[1][0]) / 2;
0085     double hlxzeroy = std::abs(vertices[2][0] - vertices[3][0]) / 2;
0086     double hlxposy = std::abs(vertices[4][0] - vertices[5][0]) / 2;
0087     double hly = std::abs(vertices[0][1] - vertices[4][1]) / 2;
0088     std::vector<double> halfLengths = {hlxnegy, hlxzeroy, hlxposy, hly, hly};
0089 
0090     // Create the surface
0091 
0092     Vector3 colX = rotation.col(0);
0093     Vector3 colY = rotation.col(1);
0094     Vector3 colZ = rotation.col(2);
0095     rotation.col(0) = colX;
0096     rotation.col(1) = colY;
0097     rotation.col(2) = colZ;
0098     transform.linear() = rotation;
0099 
0100     // Create the surface bounds
0101     double halfXnegY = unitLength * halfLengths[0];
0102     double halfXzeroY = unitLength * halfLengths[1];
0103     double halfXposY = unitLength * halfLengths[2];
0104     double halfYnegX = unitLength * halfLengths[3];
0105     double halfYposX = unitLength * halfLengths[4];
0106 
0107     auto diamondBounds = boundFactory.makeBounds<DiamondBounds>(
0108         halfXnegY, halfXzeroY, halfXposY, halfYnegX, halfYposX);
0109     if (!sensitive) {
0110       auto surface =
0111           Surface::makeShared<PlaneSurface>(transform, diamondBounds);
0112       return std::make_tuple(nullptr, surface);
0113     }
0114     // Create the element and the surface
0115     auto detectorElement =
0116         GeoModelDetectorElement::createDetectorElement<PlaneSurface>(
0117             geoPV, diamondBounds, transform, unitLength * polygon.getDZ());
0118     auto surface = detectorElement->surface().getSharedPtr();
0119     // Return the detector element and surface
0120     return std::make_tuple(detectorElement, surface);
0121   } else {
0122     throw std::runtime_error("GeoSimplePolygonBrep with " +
0123                              std::to_string(nVertices) +
0124                              " vertices can not be converted");
0125   }
0126 }