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/Definitions/Algebra.hpp"
0012 #include <actsvg/meta.hpp>
0013 
0014 #include <array>
0015 #include <fstream>
0016 #include <string>
0017 #include <tuple>
0018 #include <vector>
0019 
0020 namespace ActsPlugins::Svg {
0021 
0022 /// @brief Style struct
0023 struct Style {
0024   // Fill parameters
0025   std::array<int, 3> fillColor = {255, 255, 255};
0026   double fillOpacity = 1.;
0027 
0028   // Highlight parameters
0029   std::array<int, 3> highlightColor = {0, 0, 0};
0030   std::vector<std::string> highlights = {};
0031 
0032   double strokeWidth = 0.5;
0033   std::array<int, 3> strokeColor = {0, 0, 0};
0034 
0035   double highlightStrokeWidth = 2;
0036   std::array<int, 3> highlightStrokeColor = {0, 0, 0};
0037 
0038   std::vector<int> strokeDasharray = {};
0039 
0040   unsigned int fontSize = 14u;
0041   std::array<int, 3> fontColor = {0};
0042 
0043   /// Number of segments to approximate a quarter of a circle
0044   unsigned int quarterSegments = 72u;
0045 
0046   /// Conversion to fill and stroke object from the base library
0047   /// @return a tuple of actsvg digestable objects
0048   std::tuple<actsvg::style::fill, actsvg::style::stroke> fillAndStroke() const {
0049     actsvg::style::fill fll;
0050     fll._fc._rgb = fillColor;
0051     fll._fc._opacity = static_cast<actsvg::scalar>(fillOpacity);
0052     fll._fc._hl_rgb = highlightColor;
0053     fll._fc._highlight = highlights;
0054 
0055     actsvg::style::stroke str;
0056     str._sc._rgb = strokeColor;
0057     str._sc._hl_rgb = highlightStrokeColor;
0058     str._width = static_cast<actsvg::scalar>(strokeWidth);
0059     str._hl_width = static_cast<actsvg::scalar>(highlightStrokeWidth);
0060     str._dasharray = strokeDasharray;
0061 
0062     return {fll, str};
0063   }
0064 
0065   /// Conversion to fill, stroke and font
0066   /// @return a tuple of actsvg digestable objects
0067   std::tuple<actsvg::style::fill, actsvg::style::stroke, actsvg::style::font>
0068   fillStrokeFont() const {
0069     auto [fll, str] = fillAndStroke();
0070 
0071     actsvg::style::font fnt;
0072     fnt._size = fontSize;
0073     fnt._fc._rgb = fontColor;
0074 
0075     return std::tie(fll, str, fnt);
0076   }
0077 };
0078 
0079 /// Default style for sensitive objects
0080 inline static const Style defaultSensitiveStyle{{66, 166, 245},
0081                                                 0.85,
0082                                                 {245, 166, 66},
0083                                                 {"mouseover", "mouseout"},
0084                                                 0.25,
0085                                                 {0, 0, 0},
0086                                                 2.,
0087                                                 {0, 0, 0},
0088                                                 {},
0089                                                 14u,
0090                                                 {0},
0091                                                 72u};
0092 
0093 /// Default style for grid objects
0094 inline static const Style defaultGridStyle{{0, 0, 0},
0095                                            0.0,
0096                                            {100, 100, 100},
0097                                            {"mouseover", "mouseout"},
0098                                            0.25,
0099                                            {0, 0, 0},
0100                                            3.,
0101                                            {0, 0, 0},
0102                                            {},
0103                                            14u,
0104                                            {0},
0105                                            72u};
0106 
0107 /// Create a group
0108 ///
0109 /// @param objects are the individual objects to be grouped
0110 /// @param name is the name of the group
0111 ///
0112 /// @return a single svg object as a group
0113 inline static actsvg::svg::object group(
0114     const std::vector<actsvg::svg::object>& objects, const std::string& name) {
0115   actsvg::svg::object gr;
0116   gr._tag = "g";
0117   gr._id = name;
0118   for (const auto& o : objects) {
0119     gr.add_object(o);
0120   }
0121   return gr;
0122 }
0123 
0124 /// Helper method to a measure
0125 ///
0126 /// @param xStart the start position x
0127 /// @param yStart the start position y
0128 /// @param xEnd the end position x
0129 /// @param yEnd the end position y
0130 ///
0131 /// @return a single svg object as a measure
0132 inline static actsvg::svg::object measure(double xStart, double yStart,
0133                                           double xEnd, double yEnd,
0134                                           const std::string& variable = "",
0135                                           double value = 0.,
0136                                           const std::string& unit = "") {
0137   std::string mlabel = "";
0138   if (!variable.empty()) {
0139     mlabel = variable + " = ";
0140   }
0141   if (value != 0.) {
0142     mlabel += actsvg::utils::to_string(static_cast<actsvg::scalar>(value));
0143   }
0144   if (!unit.empty()) {
0145     mlabel += " ";
0146     mlabel += unit;
0147   }
0148   return actsvg::draw::measure(
0149       "measure",
0150       {static_cast<actsvg::scalar>(xStart),
0151        static_cast<actsvg::scalar>(yStart)},
0152       {static_cast<actsvg::scalar>(xEnd), static_cast<actsvg::scalar>(yEnd)},
0153       actsvg::style::stroke(), actsvg::style::marker({"o"}),
0154       actsvg::style::marker({"|<<"}), actsvg::style::font(), mlabel);
0155 }
0156 
0157 // Helper method to draw axes
0158 ///
0159 /// @param xMin the minimum x value
0160 /// @param xMax the maximum x value
0161 /// @param yMin the minimum y value
0162 /// @param yMax the maximum y value
0163 ///
0164 /// @return an svg object
0165 inline static actsvg::svg::object axesXY(double xMin, double xMax, double yMin,
0166                                          double yMax) {
0167   return actsvg::draw::x_y_axes(
0168       "x_y_axis",
0169       {static_cast<actsvg::scalar>(xMin), static_cast<actsvg::scalar>(xMax)},
0170       {static_cast<actsvg::scalar>(yMin), static_cast<actsvg::scalar>(yMax)});
0171 }
0172 
0173 // Helper method to draw axes
0174 ///
0175 /// @param xPos the minimum x value
0176 /// @param yPos the maximum x value
0177 /// @param title the title of the info box
0178 /// @param titleStyle the title of the info box
0179 /// @param info the text of the info box
0180 /// @param infoStyle the style of the info box (body)
0181 /// @param object the connected object
0182 ///
0183 /// @return an svg object
0184 inline static actsvg::svg::object infoBox(
0185     double xPos, double yPos, const std::string& title, const Style& titleStyle,
0186     const std::vector<std::string>& info, const Style& infoStyle,
0187     const actsvg::svg::object& object,
0188     const std::vector<std::string>& highlights = {"mouseover", "mouseout"}) {
0189   auto [titleFill, titleStroke, titleFont] = titleStyle.fillStrokeFont();
0190   auto [infoFill, infoStroke, infoFont] = infoStyle.fillStrokeFont();
0191 
0192   actsvg::style::stroke stroke;
0193 
0194   return actsvg::draw::connected_info_box(
0195       object._id + "_infoBox",
0196       {static_cast<actsvg::scalar>(xPos), static_cast<actsvg::scalar>(yPos)},
0197       title, titleFill, titleFont, info, infoFill, infoFont, stroke, object,
0198       highlights);
0199 }
0200 
0201 /// Helper method to write to file
0202 ///
0203 /// @param objects to be written out
0204 /// @param fileName the file name is to be given
0205 ///
0206 inline static void toFile(const std::vector<actsvg::svg::object>& objects,
0207                           const std::string& fileName) {
0208   actsvg::svg::file foutFile;
0209 
0210   for (const auto& o : objects) {
0211     foutFile.add_object(o);
0212   }
0213 
0214   std::ofstream fout;
0215   fout.open(fileName);
0216   fout << foutFile;
0217   fout.close();
0218 }
0219 
0220 }  // namespace ActsPlugins::Svg