Back to home page

EIC code displayed by LXR

 
 

    


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

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 // Project include(s)
0010 #include "detray/algebra/concepts.hpp"
0011 #include "detray/core/detector.hpp"
0012 #include "detray/definitions/algebra.hpp"
0013 #include "detray/detectors/default_metadata.hpp"
0014 
0015 // Plugin include(s)
0016 #include "detray/plugins/svgtools/illustrator.hpp"
0017 #include "detray/plugins/svgtools/styling/styling.hpp"
0018 #include "detray/plugins/svgtools/writer.hpp"
0019 
0020 // Actsvg include(s)
0021 #include <actsvg/core/defs.hpp>
0022 #include <actsvg/core/draw.hpp>
0023 #include <actsvg/core/style.hpp>
0024 #include <actsvg/core/svg.hpp>
0025 #include <actsvg/core/views.hpp>
0026 
0027 // Pybind11 include(s)
0028 #include <pybind11/pybind11.h>
0029 #include <pybind11/stl.h>
0030 
0031 // System include(s)
0032 #include <array>
0033 #include <string>
0034 #include <vector>
0035 
0036 namespace py = pybind11;
0037 
0038 namespace {
0039 
0040 using scalar_t = DETRAY_CUSTOM_SCALARTYPE;
0041 using algebra_t = detray::array<scalar_t>;
0042 using detector_t = detray::host::detector<detray::default_metadata<algebra_t>>;
0043 using geometry_context_t = detector_t::geometry_context;
0044 using style_t = detray::svgtools::styling::style;
0045 using illustrator_t = detray::svgtools::illustrator<detector_t>;
0046 
0047 /// Bind the draw methods of the illustrator for @tparam view_t .
0048 template <typename view_t>
0049 void bind_draw_methods(py::class_<illustrator_t> &c) {
0050   c.def(
0051       "draw_detector",
0052       [](const illustrator_t &il, const view_t &view,
0053          const geometry_context_t &gctx, float r_length, float z_length) {
0054         return il.draw_detector(view, gctx, r_length, z_length);
0055       },
0056       py::arg("view"), py::arg("gctx"), py::arg("r_length") = 1100.f,
0057       py::arg("z_length") = 3100.f, "Draw the whole detector");
0058 
0059   c.def(
0060       "draw_volumes",
0061       [](const illustrator_t &il, const std::vector<detray::dindex> &indices,
0062          const view_t &view, const geometry_context_t &gctx) {
0063         return il.draw_volumes(indices, view, gctx);
0064       },
0065       py::arg("indices"), py::arg("view"), py::arg("gctx"),
0066       "Draw the volumes with the given indices. Returns their svg together "
0067       "with the svgs of their surface grids");
0068   c.def(
0069       "draw_volumes",
0070       [](const illustrator_t &il, const std::vector<std::string> &names,
0071          const view_t &view, const geometry_context_t &gctx) {
0072         return il.draw_volumes(names, view, gctx);
0073       },
0074       py::arg("names"), py::arg("view"), py::arg("gctx"),
0075       "Draw the volumes with the given names. Returns their svg together with "
0076       "the svgs of their surface grids");
0077 
0078   c.def(
0079       "draw_surfaces",
0080       [](const illustrator_t &il, const std::vector<detray::dindex> &indices,
0081          const view_t &view, const geometry_context_t &gctx) {
0082         return il.draw_surfaces(indices, view, gctx);
0083       },
0084       py::arg("indices"), py::arg("view"), py::arg("gctx"),
0085       "Draw the surfaces with the given indices. Returns their svg together "
0086       "with the svg of their material");
0087 }
0088 
0089 }  // namespace
0090 
0091 PYBIND11_MODULE(detray_svgtools_python_bindings, m) {
0092   m.doc() = "Detray svgtools bindings";
0093 
0094   // Svg primitives
0095   {
0096     // Duplicates actsvg's binding. Registered module-locally, so that importing
0097     // this module alongside actsvg does not clash over the same C++ type
0098     py::class_<actsvg::svg::object>(m, "SvgObject", py::module_local())
0099         .def_readwrite("id", &actsvg::svg::object::_id,
0100                        "Identification string");
0101 
0102     m.def(
0103         "write_svg",
0104         [](const std::string &path,
0105            const std::vector<actsvg::svg::object> &svgs,
0106            bool replace) { detray::svgtools::write_svg(path, svgs, replace); },
0107         py::arg("path"), py::arg("svgs"), py::arg("replace") = true,
0108         "Write svg objects to '<path>.svg'. Their ids must be unique");
0109 
0110     m.def(
0111         "draw_axes",
0112         [](const std::string &name,
0113            const std::array<actsvg::scalar, 2> &x_range,
0114            const std::array<actsvg::scalar, 2> &y_range,
0115            const std::string &x_label, const std::string &y_label,
0116            unsigned int font_size) {
0117           actsvg::style::font font{};
0118           font._size = font_size;
0119 
0120           // The default stroke is black
0121           return actsvg::draw::x_y_axes(name, x_range, y_range,
0122                                         actsvg::style::stroke{}, x_label,
0123                                         y_label, font);
0124         },
0125         py::arg("name"), py::arg("x_range"), py::arg("y_range"),
0126         py::arg("x_label") = "", py::arg("y_label") = "",
0127         py::arg("font_size") = 12u, "Draw a pair of x-y axes");
0128   }
0129 
0130   // Svg views
0131   {
0132     py::class_<actsvg::views::x_y>(m, "ViewXY", py::module_local())
0133         .def(py::init<>(), "Transverse view");
0134     py::class_<actsvg::views::z_r>(m, "ViewZR", py::module_local())
0135         .def(py::init<>(), "Longitudinal view");
0136     py::class_<actsvg::views::z_phi>(m, "ViewZPhi", py::module_local())
0137         .def(py::init<>(), "Unrolled cylinder view");
0138     py::class_<actsvg::views::z_rphi>(m, "ViewZRPhi", py::module_local())
0139         .def(py::init<>(), "Unrolled cylinder view at a fixed radius");
0140   }
0141 
0142   // Svg styling
0143   {
0144     py::class_<style_t>(m, "Style")
0145         .def_property(
0146             "font_size",
0147             [](const style_t &s) {
0148               return s._detector_style._volume_style._sensitive_surface_style
0149                   ._font_size;
0150             },
0151             [](style_t &s, unsigned int font_size) {
0152               auto &vol_style = s._detector_style._volume_style;
0153               vol_style._sensitive_surface_style._font_size = font_size;
0154               vol_style._passive_surface_style._font_size = font_size;
0155               vol_style._portal_style._surface_style._font_size = font_size;
0156               s._eta_lines_style._font_size = font_size;
0157             },
0158             "Font size of all surface and eta line labels")
0159         .def_property(
0160             "material_gradient_pos",
0161             [](const style_t &s) {
0162               return s._detector_style._volume_style._sensitive_surface_style
0163                   ._material_style._gradient_pos;
0164             },
0165             [](style_t &s, const actsvg::point2 &pos) {
0166               auto &vol_style = s._detector_style._volume_style;
0167               vol_style._sensitive_surface_style._material_style._gradient_pos =
0168                   pos;
0169               vol_style._passive_surface_style._material_style._gradient_pos =
0170                   pos;
0171               vol_style._portal_style._surface_style._material_style
0172                   ._gradient_pos = pos;
0173             },
0174             "Position of the material gradient box in all surface styles");
0175 
0176     m.def(
0177         "tableau_colorblind_style",
0178         []() { return detray::svgtools::styling::tableau_colorblind::style; },
0179         "Style that matches the colors used in the data plotting");
0180   }
0181 
0182   // Svg illustrator
0183   {
0184     auto c =
0185         py::class_<illustrator_t>(m, "Illustrator")
0186             .def(py::init<const detector_t &, const detray::name_map &,
0187                           const style_t &>(),
0188                  py::arg("detector"), py::arg("names"), py::arg("style"),
0189                  py::keep_alive<1, 2>(), py::keep_alive<1, 3>(),
0190                  "Build the svg generator of a detector. The detector and its "
0191                  "names are kept alive for as long as the illustrator is used, "
0192                  "as it only references them. The style is copied")
0193             .def("show_info", &illustrator_t::show_info,
0194                  py::arg("toggle") = true, "Toggle the information boxes")
0195             .def("hide_eta_lines", &illustrator_t::hide_eta_lines,
0196                  py::arg("toggle") = true,
0197                  "Toggle the eta lines of the z-r view")
0198             .def("hide_grids", &illustrator_t::hide_grids,
0199                  py::arg("toggle") = true, "Toggle the surface grids")
0200             .def("hide_material", &illustrator_t::hide_material,
0201                  py::arg("toggle") = true, "Toggle the surface material")
0202             .def("hide_portals", &illustrator_t::hide_portals,
0203                  py::arg("toggle") = true, "Toggle the portal surfaces")
0204             .def("hide_passives", &illustrator_t::hide_passives,
0205                  py::arg("toggle") = true, "Toggle the passive surfaces")
0206             .def("search_window", &illustrator_t::search_window,
0207                  py::arg("window"),
0208                  "Neighborhood search window for the grid display")
0209             .def_property_readonly("det_name", &illustrator_t::det_name,
0210                                    "Name of the detector");
0211 
0212     bind_draw_methods<actsvg::views::x_y>(c);
0213     bind_draw_methods<actsvg::views::z_r>(c);
0214     bind_draw_methods<actsvg::views::z_phi>(c);
0215     bind_draw_methods<actsvg::views::z_rphi>(c);
0216   }
0217 }