Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-12-11 09:40:21

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 #pragma once
0010 
0011 #include "Acts/Geometry/GeometryContext.hpp"
0012 #include "Acts/Utilities/Axis.hpp"
0013 #include "Acts/Utilities/BinningType.hpp"
0014 #include "ActsPlugins/ActSVG/SvgUtils.hpp"
0015 #include <actsvg/core.hpp>
0016 #include <actsvg/meta.hpp>
0017 
0018 #include <array>
0019 #include <optional>
0020 #include <tuple>
0021 #include <vector>
0022 
0023 namespace Acts {
0024 
0025 class Surface;
0026 }  // namespace Acts
0027 
0028 namespace ActsPlugins::Svg {
0029 
0030 using ProtoGrid = actsvg::proto::grid;
0031 
0032 namespace GridConverter {
0033 
0034 // An optional range and binning value
0035 using AxisBound = std::tuple<std::array<double, 2u>, Acts::AxisDirection>;
0036 
0037 /// Nested Options struct
0038 struct Options {
0039   /// A The style for the surfaces
0040   Style style = defaultGridStyle;
0041   /// Optional bound - in case of 1-dim grid
0042   std::optional<AxisBound> optionalBound;
0043 };
0044 
0045 /// Convert an ACTS grid into a actsvg protogrid, it currently works with
0046 ///
0047 /// - 1D: [ AxisX ] , [ AxisY ], [ AxisR ] , [ AxisPhi ]
0048 /// - 2D: [ AxisX, AxisY ], [ AxisZ, AxisPhi ], [ AxisR, AxisPhi ]
0049 ///
0050 /// @tparam grid_type is the type of the grid to be converted
0051 ///
0052 /// @param grid the grid to be converted
0053 /// @param aDirs the axis directions of the grid
0054 /// @param cOptions the conversion options
0055 ///
0056 /// @return an ACTSVG proto grid for displaying
0057 template <typename grid_type>
0058 ProtoGrid convert(const grid_type& grid,
0059                   const std::array<Acts::AxisDirection, grid_type::DIM>& aDirs,
0060                   const GridConverter::Options& cOptions) {
0061   // The return object
0062   ProtoGrid pGrid;
0063 
0064   // Grid axes
0065   auto axes = grid.axes();
0066 
0067   // The edge values - these need to follow the ACTSVG convention,
0068   // so there could be swapping when necessary
0069   std::vector<double> edges0;
0070   std::vector<double> edges1;
0071 
0072   // 1D case (more to be filled in later)
0073   if constexpr (grid_type::DIM == 1u) {
0074     if (aDirs[0u] == Acts::AxisDirection::AxisPhi &&
0075         axes[0]->getBoundaryType() == Acts::AxisBoundaryType::Closed) {
0076       // swap     needed
0077       edges1 = axes[0]->getBinEdges();
0078       pGrid._type = actsvg::proto::grid::e_r_phi;
0079     }
0080     if (cOptions.optionalBound.has_value()) {
0081       auto [boundRange, boundValue] = cOptions.optionalBound.value();
0082       if (boundValue == Acts::AxisDirection::AxisR) {
0083         // good - no swap needed
0084         edges0 = {boundRange[0u], boundRange[1u]};
0085       }
0086     }
0087   }
0088   // 2D cases
0089   if constexpr (grid_type::DIM == 2u) {
0090     // Assign
0091     edges0 = axes[0]->getBinEdges();
0092     edges1 = axes[1]->getBinEdges();
0093     if (aDirs[0] == Acts::AxisDirection::AxisPhi &&
0094         aDirs[1] == Acts::AxisDirection::AxisZ) {
0095       //  swap needed
0096       std::swap(edges0, edges1);
0097       pGrid._type = actsvg::proto::grid::e_z_phi;
0098     } else if (aDirs[0] == Acts::AxisDirection::AxisPhi &&
0099                aDirs[1] == Acts::AxisDirection::AxisR) {
0100       // swap needed
0101       std::swap(edges0, edges1);
0102       pGrid._type = actsvg::proto::grid::e_r_phi;
0103     } else if (aDirs[0] == Acts::AxisDirection::AxisZ &&
0104                aDirs[1] == Acts::AxisDirection::AxisPhi) {
0105       // good - no swap needed
0106       pGrid._type = actsvg::proto::grid::e_z_phi;
0107     } else if (aDirs[0] == Acts::AxisDirection::AxisR &&
0108                aDirs[1] == Acts::AxisDirection::AxisPhi) {
0109       // good - no swap needed
0110       pGrid._type = actsvg::proto::grid::e_r_phi;
0111     } else if (aDirs[0] == Acts::AxisDirection::AxisX &&
0112                aDirs[1] == Acts::AxisDirection::AxisY) {
0113       // good - no swap needed
0114       pGrid._type = actsvg::proto::grid::e_x_y;
0115     }
0116   }
0117 
0118   // Assign grid edges
0119   pGrid._edges_0 = std::vector<actsvg::scalar>(edges0.begin(), edges0.end());
0120   pGrid._edges_1 = std::vector<actsvg::scalar>(edges1.begin(), edges1.end());
0121 
0122   auto [fill, stroke] = cOptions.style.fillAndStroke();
0123   pGrid._fill = fill;
0124   pGrid._stroke = stroke;
0125 
0126   return pGrid;
0127 }
0128 
0129 }  // namespace GridConverter
0130 }  // namespace ActsPlugins::Svg