Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-11-06 09:18:25

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/ActSVG/SurfaceSvgConverter.hpp"
0010 
0011 #include "Acts/Surfaces/AnnulusBounds.hpp"
0012 #include "Acts/Surfaces/PlanarBounds.hpp"
0013 #include "Acts/Surfaces/Surface.hpp"
0014 
0015 using namespace Acts;
0016 
0017 ActsPlugins::Svg::ProtoSurface ActsPlugins::Svg::SurfaceConverter::convert(
0018     const GeometryContext& gctx, const Surface& surface,
0019     const SurfaceConverter::Options& cOptions) {
0020   ProtoSurface pSurface;
0021 
0022   // In case of non-template surfaces, the polyhedron does the trick
0023   if (!cOptions.templateSurface) {
0024     // Polyhedron surface for vertices needed anyway
0025     Polyhedron surfaceHedron =
0026         surface.polyhedronRepresentation(gctx, cOptions.style.quarterSegments);
0027     auto vertices3D = surfaceHedron.vertices;
0028     pSurface._vertices = vertices3D;
0029   } else {
0030     // In case it's a template surface, only the bounds matter
0031     // Check if planar bounds
0032     auto planarBounds = dynamic_cast<const PlanarBounds*>(&(surface.bounds()));
0033     if (planarBounds != nullptr) {
0034       auto vertices2D = planarBounds->vertices(cOptions.style.quarterSegments);
0035       pSurface._vertices.reserve(vertices2D.size());
0036       for (const auto& v2 : vertices2D) {
0037         pSurface._vertices.push_back({v2[0], v2[1], 0.});
0038       }
0039     } else {
0040       // Or annulus bounds
0041       auto annulusBounds =
0042           dynamic_cast<const AnnulusBounds*>(&(surface.bounds()));
0043       if (annulusBounds != nullptr) {
0044         auto vertices2D =
0045             annulusBounds->vertices(cOptions.style.quarterSegments);
0046         pSurface._vertices.reserve(vertices2D.size());
0047         for (const auto& v2 : vertices2D) {
0048           pSurface._vertices.push_back({v2[0], v2[1], 0.});
0049         }
0050       } else if (surface.type() == Surface::SurfaceType::Disc) {
0051         // Or disc bounds
0052         const auto& boundValues = surface.bounds().values();
0053         if (surface.bounds().type() == SurfaceBounds::BoundsType::eDisc) {
0054           // The radii
0055           actsvg::scalar ri = static_cast<actsvg::scalar>(boundValues[0]);
0056           actsvg::scalar ro = static_cast<actsvg::scalar>(boundValues[1]);
0057           pSurface._radii = {ri, ro};
0058           pSurface._opening = {
0059               static_cast<actsvg::scalar>(boundValues[3] - boundValues[2]),
0060               static_cast<actsvg::scalar>(boundValues[3] + boundValues[2])};
0061 
0062           actsvg::scalar pl = pSurface._opening[0];
0063           actsvg::scalar ph = pSurface._opening[1];
0064 
0065           pSurface._vertices = {
0066               {static_cast<actsvg::scalar>(ri * std::cos(pl)),
0067                static_cast<actsvg::scalar>(ri * std::sin(pl)), 0.},
0068               {static_cast<actsvg::scalar>(ro * std::cos(ph)),
0069                static_cast<actsvg::scalar>(ro * std::sin(ph)), 0.},
0070               {static_cast<actsvg::scalar>(ri * std::cos(pl)),
0071                static_cast<actsvg::scalar>(ri * std::sin(pl)), 0.},
0072               {static_cast<actsvg::scalar>(ro * std::cos(ph)),
0073                static_cast<actsvg::scalar>(ro * std::sin(ph)), 0.}};
0074         }
0075       }
0076     }
0077   }
0078 
0079   // Bound types and values
0080   const auto& boundValues = surface.bounds().values();
0081   auto bType = surface.bounds().type();
0082   auto bValues = surface.bounds().values();
0083   if (bType == SurfaceBounds::BoundsType::eRectangle) {
0084     pSurface._type = ProtoSurface::type::e_rectangle;
0085     // Set the measure
0086     pSurface._measures = {
0087         static_cast<actsvg::scalar>(0.5 * (boundValues[2] - boundValues[0])),
0088         static_cast<actsvg::scalar>(0.5 * (boundValues[3] - boundValues[1]))};
0089   } else if (bType == SurfaceBounds::BoundsType::eTrapezoid) {
0090     pSurface._type = ProtoSurface::type::e_trapez;
0091     // Set the measure
0092     pSurface._measures = {static_cast<actsvg::scalar>(boundValues[0]),
0093                           static_cast<actsvg::scalar>(boundValues[1]),
0094                           static_cast<actsvg::scalar>(boundValues[2])};
0095   } else if (bType == SurfaceBounds::BoundsType::eDiamond) {
0096     // Set the measure
0097     for (const auto& bv : boundValues) {
0098       pSurface._measures.push_back(static_cast<actsvg::scalar>(bv));
0099     }
0100   } else if (bType == SurfaceBounds::BoundsType::eAnnulus) {
0101     pSurface._type = ProtoSurface::type::e_trapez;
0102     // Set the measure
0103     for (const auto& bv : boundValues) {
0104       pSurface._measures.push_back(static_cast<actsvg::scalar>(bv));
0105     }
0106   } else if (bType == SurfaceBounds::BoundsType::eDisc) {
0107     pSurface._type = ProtoSurface::type::e_disc;
0108     // Set the openings
0109     actsvg::scalar ri = static_cast<actsvg::scalar>(boundValues[0]);
0110     actsvg::scalar ro = static_cast<actsvg::scalar>(boundValues[1]);
0111     actsvg::scalar zp = static_cast<actsvg::scalar>(surface.center(gctx).z());
0112     pSurface._radii = {ri, ro};
0113     pSurface._zparameters = {zp, zp};
0114     pSurface._opening = {
0115         static_cast<actsvg::scalar>(boundValues[3] - boundValues[2]),
0116         static_cast<actsvg::scalar>(boundValues[3] + boundValues[2])};
0117     // Set the measure
0118     for (const auto& bv : boundValues) {
0119       pSurface._measures.push_back(static_cast<actsvg::scalar>(bv));
0120     }
0121   }
0122 
0123   // Decorations
0124   // - Flag the material
0125   if (surface.surfaceMaterial() != nullptr) {
0126     pSurface._decorations["material"] = actsvg::svg::object{};
0127   }
0128 
0129   /// - The geometry ID as string
0130   actsvg::svg::object geoId{};
0131   geoId._id = std::to_string(surface.geometryId().value());
0132   pSurface._decorations["geo_id"] = geoId;
0133 
0134   auto [surfaceFill, surfaceStroke] = cOptions.style.fillAndStroke();
0135   pSurface._fill = surfaceFill;
0136   pSurface._stroke = surfaceStroke;
0137 
0138   return pSurface;
0139 }