Analysis Software
Documentation for sPHENIX simulation software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
SvgUtils.hpp
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file SvgUtils.hpp
1 // This file is part of the Acts project.
2 //
3 // Copyright (C) 2022 CERN for the benefit of the Acts project
4 //
5 // This Source Code Form is subject to the terms of the Mozilla Public
6 // License, v. 2.0. If a copy of the MPL was not distributed with this
7 // file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 
9 #pragma once
10 
12 #include <actsvg/meta.hpp>
13 
14 #include <array>
15 #include <fstream>
16 #include <string>
17 #include <tuple>
18 #include <vector>
19 
20 namespace Acts {
21 namespace Svg {
22 
23 struct Style {
24  // Fill parameters
25  std::array<int, 3> fillColor = {255, 255, 255};
27 
28  // Highlight parameters
29  std::array<int, 3> highlightColor = {0, 0, 0};
30  std::vector<std::string> highlights = {};
31 
33  std::array<int, 3> strokeColor = {0, 0, 0};
34 
36  std::array<int, 3> highlightStrokeColor = {0, 0, 0};
37 
38  std::vector<int> strokeDasharray = {};
39 
40  unsigned int fontSize = 14u;
41 
42  unsigned int nSegments = 72u;
43 
46  std::tuple<actsvg::style::fill, actsvg::style::stroke> fillAndStroke() const {
47  actsvg::style::fill fll;
48  fll._fc._rgb = fillColor;
49  fll._fc._opacity = fillOpacity;
50  fll._fc._hl_rgb = highlightColor;
51  fll._fc._highlight = highlights;
52 
53  actsvg::style::stroke str;
54  str._sc._rgb = strokeColor;
55  str._sc._hl_rgb = highlightStrokeColor;
56  str._width = strokeWidth;
57  str._hl_width = highlightStrokeWidth;
58  str._dasharray = strokeDasharray;
59 
60  return std::tie(fll, str);
61  }
62 };
63 
70 inline static actsvg::svg::object group(
71  const std::vector<actsvg::svg::object>& objects, const std::string& name) {
72  actsvg::svg::object gr;
73  gr._tag = "g";
74  gr._id = name;
75  for (const auto& o : objects) {
76  gr.add_object(o);
77  }
78  return gr;
79 }
80 
89 inline static actsvg::svg::object measure(ActsScalar xStart, ActsScalar yStart,
90  ActsScalar xEnd, ActsScalar yEnd,
91  const std::string& variable = "",
92  ActsScalar value = 0.,
93  const std::string& unit = "") {
94  std::string mlabel = "";
95  if (not variable.empty()) {
96  mlabel = variable + " = ";
97  }
98  if (value != 0.) {
99  mlabel += actsvg::utils::to_string(static_cast<actsvg::scalar>(value));
100  }
101  if (not unit.empty()) {
102  mlabel += " ";
103  mlabel += unit;
104  }
105  return actsvg::draw::measure(
106  "measure",
107  {static_cast<actsvg::scalar>(xStart),
108  static_cast<actsvg::scalar>(yStart)},
109  {static_cast<actsvg::scalar>(xEnd), static_cast<actsvg::scalar>(yEnd)},
110  actsvg::style::stroke(), actsvg::style::marker({"o"}),
111  actsvg::style::marker({"|<<"}), actsvg::style::font(), mlabel);
112 }
113 
114 // Helper method to draw axes
122 inline static actsvg::svg::object axesXY(ActsScalar xMin, ActsScalar xMax,
123  ActsScalar yMin, ActsScalar yMax) {
124  return actsvg::draw::x_y_axes(
125  "x_y_axis",
126  {static_cast<actsvg::scalar>(xMin), static_cast<actsvg::scalar>(xMax)},
127  {static_cast<actsvg::scalar>(yMin), static_cast<actsvg::scalar>(yMax)});
128 }
129 
130 // Helper method to draw axes
140 inline static actsvg::svg::object infoBox(ActsScalar xPos, ActsScalar yPos,
141  const std::string& title,
142  const std::vector<std::string>& info,
143  const Style& infoBoxStyle,
144  const actsvg::svg::object& object) {
145  auto [fill, stroke] = infoBoxStyle.fillAndStroke();
146 
147  actsvg::style::font titleFont;
148  titleFont._fc = actsvg::style::color{{255, 255, 255}};
149  titleFont._size = infoBoxStyle.fontSize;
150 
151  actsvg::style::fill infoFill = fill;
152  infoFill._fc._opacity = 0.4;
153  actsvg::style::font infoFont;
154  infoFont._size = infoBoxStyle.fontSize;
155 
156  return actsvg::draw::connected_info_box(
157  object._id + "_infoBox",
158  {static_cast<actsvg::scalar>(xPos), static_cast<actsvg::scalar>(yPos)},
159  title, fill, titleFont, info, infoFill, infoFont, stroke, object);
160 }
161 
167 inline static void toFile(const std::vector<actsvg::svg::object>& objects,
168  const std::string& fileName) {
169  actsvg::svg::file foutFile;
170 
171  for (const auto& o : objects) {
172  foutFile.add_object(o);
173  }
174 
175  std::ofstream fout;
176  fout.open(fileName);
177  fout << foutFile;
178  fout.close();
179 }
180 
181 } // namespace Svg
182 } // namespace Acts