Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-11-23 09:33:26

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 Acts::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 = 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 = strokeWidth;
0059     str._hl_width = 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 /// Create a group
0080 ///
0081 /// @param objects are the individual objects to be grouped
0082 /// @param name is the name of the group
0083 ///
0084 /// @return a single svg object as a group
0085 inline static actsvg::svg::object group(
0086     const std::vector<actsvg::svg::object>& objects, const std::string& name) {
0087   actsvg::svg::object gr;
0088   gr._tag = "g";
0089   gr._id = name;
0090   for (const auto& o : objects) {
0091     gr.add_object(o);
0092   }
0093   return gr;
0094 }
0095 
0096 /// Helper method to a measure
0097 ///
0098 /// @param xStart the start position x
0099 /// @param yStart the start position y
0100 /// @param xEnd the end position x
0101 /// @param yEnd the end position y
0102 ///
0103 /// @return a single svg object as a measure
0104 inline static actsvg::svg::object measure(double xStart, double yStart,
0105                                           double xEnd, double yEnd,
0106                                           const std::string& variable = "",
0107                                           double value = 0.,
0108                                           const std::string& unit = "") {
0109   std::string mlabel = "";
0110   if (!variable.empty()) {
0111     mlabel = variable + " = ";
0112   }
0113   if (value != 0.) {
0114     mlabel += actsvg::utils::to_string(static_cast<actsvg::scalar>(value));
0115   }
0116   if (!unit.empty()) {
0117     mlabel += " ";
0118     mlabel += unit;
0119   }
0120   return actsvg::draw::measure(
0121       "measure",
0122       {static_cast<actsvg::scalar>(xStart),
0123        static_cast<actsvg::scalar>(yStart)},
0124       {static_cast<actsvg::scalar>(xEnd), static_cast<actsvg::scalar>(yEnd)},
0125       actsvg::style::stroke(), actsvg::style::marker({"o"}),
0126       actsvg::style::marker({"|<<"}), actsvg::style::font(), mlabel);
0127 }
0128 
0129 // Helper method to draw axes
0130 ///
0131 /// @param xMin the minimum x value
0132 /// @param xMax the maximum x value
0133 /// @param yMin the minimum y value
0134 /// @param yMax the maximum y value
0135 ///
0136 /// @return an svg object
0137 inline static actsvg::svg::object axesXY(double xMin, double xMax, double yMin,
0138                                          double yMax) {
0139   return actsvg::draw::x_y_axes(
0140       "x_y_axis",
0141       {static_cast<actsvg::scalar>(xMin), static_cast<actsvg::scalar>(xMax)},
0142       {static_cast<actsvg::scalar>(yMin), static_cast<actsvg::scalar>(yMax)});
0143 }
0144 
0145 // Helper method to draw axes
0146 ///
0147 /// @param xPos the minimum x value
0148 /// @param yPos the maximum x value
0149 /// @param title the title of the info box
0150 /// @param titleStyle the title of the info box
0151 /// @param info the text of the info box
0152 /// @param infoStyle the style of the info box (body)
0153 /// @param object the connected object
0154 ///
0155 /// @return an svg object
0156 inline static actsvg::svg::object infoBox(
0157     double xPos, double yPos, const std::string& title, const Style& titleStyle,
0158     const std::vector<std::string>& info, const Style& infoStyle,
0159     actsvg::svg::object& object,
0160     const std::vector<std::string>& highlights = {"mouseover", "mouseout"}) {
0161   auto [titleFill, titleStroke, titleFont] = titleStyle.fillStrokeFont();
0162   auto [infoFill, infoStroke, infoFont] = infoStyle.fillStrokeFont();
0163 
0164   actsvg::style::stroke stroke;
0165 
0166   return actsvg::draw::connected_info_box(
0167       object._id + "_infoBox",
0168       {static_cast<actsvg::scalar>(xPos), static_cast<actsvg::scalar>(yPos)},
0169       title, titleFill, titleFont, info, infoFill, infoFont, stroke, object,
0170       highlights);
0171 }
0172 
0173 /// Helper method to write to file
0174 ///
0175 /// @param objects to be written out
0176 /// @param fileName the file name is to be given
0177 ///
0178 inline static void toFile(const std::vector<actsvg::svg::object>& objects,
0179                           const std::string& fileName) {
0180   actsvg::svg::file foutFile;
0181 
0182   for (const auto& o : objects) {
0183     foutFile.add_object(o);
0184   }
0185 
0186   std::ofstream fout;
0187   fout.open(fileName);
0188   fout << foutFile;
0189   fout.close();
0190 }
0191 
0192 }  // namespace Acts::Svg