Skip to content

Commit

Permalink
feat: add some SVG glue code to help displaying proto material (acts-…
Browse files Browse the repository at this point in the history
…project#3692)

This adds and little glue code to show proto material configurations:

![Screenshot 2024-10-04 at 13 42 02](https://github.com/user-attachments/assets/4e2adff2-ceda-4598-bec0-735e7b086129)

This allows to display on mouse over which material mapping configuration was chosen.
  • Loading branch information
asalzburger authored Oct 8, 2024
1 parent 198708b commit 69e0b6b
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 32 deletions.
10 changes: 10 additions & 0 deletions Core/include/Acts/Material/ISurfaceMaterial.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include "Acts/Material/MaterialSlab.hpp"

#include <memory>
#include <sstream>
#include <vector>

namespace Acts {
Expand Down Expand Up @@ -117,6 +118,15 @@ class ISurfaceMaterial {
/// Output Method for std::ostream, to be overloaded by child classes
virtual std::ostream& toStream(std::ostream& sl) const = 0;

/// @brief output into a string
///
/// @return the string representation
std::string toString() const {
std::stringstream sstrm;
toStream(sstrm);
return sstrm.str();
}

protected:
double m_splitFactor{1.}; //!< the split factor in favour of oppositePre
MappingType m_mappingType{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ class SvgPointWriter final : public WriterT<GeometryIdMultiset<T>> {
s_pointStyle; //!< The style of the space point to be drawn

std::string infoBoxTitle = ""; //!< If an info box title is set, draw it
Acts::Svg::Style infoTitleStyle = s_infoStyle;
Acts::Svg::Style infoBoxStyle = s_infoStyle; // The style of the info box

bool projectionXY = true; ///< xy projection
Expand Down Expand Up @@ -170,7 +171,8 @@ ActsExamples::ProcessCode ActsExamples::SvgPointWriter<T, Acc>::writeT(
auto xyIbox = Acts::Svg::infoBox(
static_cast<actsvg::scalar>(point3D.x() + 10.),
static_cast<actsvg::scalar>(point3D.y() - 10.), m_cfg.infoBoxTitle,
{"Position: " + Acts::toString(point3D)}, m_cfg.infoBoxStyle, p);
m_cfg.infoTitleStyle, {"Position: " + Acts::toString(point3D)},
m_cfg.infoBoxStyle, p);
xyView.add_object(xyIbox);
}
}
Expand Down
4 changes: 3 additions & 1 deletion Examples/Python/src/Geometry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
#include "Acts/Geometry/TrackingGeometry.hpp"
#include "Acts/Geometry/Volume.hpp"
#include "Acts/Geometry/VolumeBounds.hpp"
#include "Acts/Material/ISurfaceMaterial.hpp"
#include "Acts/Plugins/Python/Utilities.hpp"
#include "Acts/Surfaces/Surface.hpp"
#include "Acts/Surfaces/SurfaceArray.hpp"
Expand Down Expand Up @@ -115,7 +116,8 @@ void addGeometry(Context& ctx) {
[](const Surface& self) { return self.geometryId(); })
.def("center", &Surface::center)
.def("type", &Surface::type)
.def("visualize", &Surface::visualize);
.def("visualize", &Surface::visualize)
.def("surfaceMaterial", &Acts::Surface::surfaceMaterialSharedPtr);
}

{
Expand Down
17 changes: 16 additions & 1 deletion Examples/Python/src/Material.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,15 @@
#include "Acts/Geometry/GeometryContext.hpp"
#include "Acts/MagneticField/MagneticFieldContext.hpp"
#include "Acts/Material/BinnedSurfaceMaterialAccumulater.hpp"
#include "Acts/Material/HomogeneousSurfaceMaterial.hpp"
#include "Acts/Material/IMaterialDecorator.hpp"
#include "Acts/Material/ISurfaceMaterial.hpp"
#include "Acts/Material/IVolumeMaterial.hpp"
#include "Acts/Material/IntersectionMaterialAssigner.hpp"
#include "Acts/Material/MaterialMapper.hpp"
#include "Acts/Material/MaterialValidater.hpp"
#include "Acts/Material/PropagatorMaterialAssigner.hpp"
#include "Acts/Material/ProtoSurfaceMaterial.hpp"
#include "Acts/Material/SurfaceMaterialMapper.hpp"
#include "Acts/Material/VolumeMaterialMapper.hpp"
#include "Acts/Plugins/Json/ActsJson.hpp"
Expand Down Expand Up @@ -57,7 +59,20 @@ void addMaterial(Context& ctx) {

{
py::class_<Acts::ISurfaceMaterial, std::shared_ptr<ISurfaceMaterial>>(
m, "ISurfaceMaterial");
m, "ISurfaceMaterial")
.def("toString", &Acts::ISurfaceMaterial::toString);

py::class_<Acts::ProtoGridSurfaceMaterial, Acts::ISurfaceMaterial,
std::shared_ptr<ProtoGridSurfaceMaterial>>(
m, "ProtoGridSurfaceMaterial");

py::class_<Acts::ProtoSurfaceMaterial, Acts::ISurfaceMaterial,
std::shared_ptr<ProtoSurfaceMaterial>>(m,
"ProtoSurfaceMaterial");

py::class_<Acts::HomogeneousSurfaceMaterial, Acts::ISurfaceMaterial,
std::shared_ptr<HomogeneousSurfaceMaterial>>(
m, "HomogeneousSurfaceMaterial");

py::class_<Acts::IVolumeMaterial, std::shared_ptr<IVolumeMaterial>>(
m, "IVolumeMaterial");
Expand Down
6 changes: 6 additions & 0 deletions Examples/Python/src/Svg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,10 @@ void addSvg(Context& ctx) {
ACTS_PYTHON_MEMBER(highlights);
ACTS_PYTHON_MEMBER(strokeWidth);
ACTS_PYTHON_MEMBER(strokeColor);
ACTS_PYTHON_MEMBER(highlightStrokeWidth);
ACTS_PYTHON_MEMBER(highlightStrokeColor);
ACTS_PYTHON_MEMBER(fontSize);
ACTS_PYTHON_MEMBER(fontColor);
ACTS_PYTHON_MEMBER(quarterSegments);
ACTS_PYTHON_STRUCT_END();
}
Expand Down Expand Up @@ -297,6 +301,8 @@ void addSvg(Context& ctx) {
svg.def("drawArrow", &actsvg::draw::arrow);

svg.def("drawText", &actsvg::draw::text);

svg.def("drawInfoBox", &Svg::infoBox);
}

// Draw Eta Lines
Expand Down
44 changes: 27 additions & 17 deletions Plugins/ActSVG/include/Acts/Plugins/ActSVG/SvgUtils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ struct Style {
std::vector<int> strokeDasharray = {};

unsigned int fontSize = 14u;
std::array<int, 3> fontColor = {0};

/// Number of segments to approximate a quarter of a circle
unsigned int quarterSegments = 72u;
Expand All @@ -60,6 +61,19 @@ struct Style {

return std::tie(fll, str);
}

/// Conversion to fill, stroke and font
/// @return a tuple of actsvg digestable objects
std::tuple<actsvg::style::fill, actsvg::style::stroke, actsvg::style::font>
fillStrokeFont() const {
auto [fll, str] = fillAndStroke();

actsvg::style::font fnt;
fnt._size = fontSize;
fnt._fc._rgb = fontColor;

return std::tie(fll, str, fnt);
}
};

/// Create a group
Expand Down Expand Up @@ -133,31 +147,27 @@ inline static actsvg::svg::object axesXY(ActsScalar xMin, ActsScalar xMax,
/// @param xPos the minimum x value
/// @param yPos the maximum x value
/// @param title the title of the info box
/// @param titleStyle the title of the info box
/// @param info the text of the info box
/// @param infoBoxStyle the style of the info box
/// @param infoStyle the style of the info box (body)
/// @param object the connected object
///
/// @return an svg object
inline static actsvg::svg::object infoBox(ActsScalar xPos, ActsScalar yPos,
const std::string& title,
const std::vector<std::string>& info,
const Style& infoBoxStyle,
const actsvg::svg::object& object) {
auto [fill, stroke] = infoBoxStyle.fillAndStroke();

actsvg::style::font titleFont;
titleFont._fc = actsvg::style::color{{255, 255, 255}};
titleFont._size = infoBoxStyle.fontSize;

actsvg::style::fill infoFill = fill;
infoFill._fc._opacity = 0.4;
actsvg::style::font infoFont;
infoFont._size = infoBoxStyle.fontSize;
inline static actsvg::svg::object infoBox(
ActsScalar xPos, ActsScalar yPos, const std::string& title,
const Style& titleStyle, const std::vector<std::string>& info,
const Style& infoStyle, actsvg::svg::object& object,
const std::vector<std::string>& highlights = {"mouseover", "mouseout"}) {
auto [titleFill, titleStroke, titleFont] = titleStyle.fillStrokeFont();
auto [infoFill, infoStroke, infoFont] = infoStyle.fillStrokeFont();

actsvg::style::stroke stroke;

return actsvg::draw::connected_info_box(
object._id + "_infoBox",
{static_cast<actsvg::scalar>(xPos), static_cast<actsvg::scalar>(yPos)},
title, fill, titleFont, info, infoFill, infoFont, stroke, object);
title, titleFill, titleFont, info, infoFill, infoFont, stroke, object,
highlights);
}

/// Helper method to write to file
Expand Down
15 changes: 3 additions & 12 deletions Plugins/ActSVG/src/SurfaceSvgConverter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -130,18 +130,9 @@ Acts::Svg::ProtoSurface Acts::Svg::SurfaceConverter::convert(
geoId._id = std::to_string(surface.geometryId().value());
pSurface._decorations["geo_id"] = geoId;

// Attach the style
pSurface._fill._fc = {
cOptions.style.fillColor,
static_cast<actsvg::scalar>(cOptions.style.fillOpacity)};

// Fill style
pSurface._fill._fc._hl_rgb = cOptions.style.highlightColor;
pSurface._fill._fc._highlight = cOptions.style.highlights;

// Stroke style
pSurface._stroke._sc = actsvg::style::color{cOptions.style.strokeColor};
pSurface._stroke._width = cOptions.style.strokeWidth;
auto [surfaceFill, surfaceStroke] = cOptions.style.fillAndStroke();
pSurface._fill = surfaceFill;
pSurface._stroke = surfaceStroke;

return pSurface;
}

0 comments on commit 69e0b6b

Please sign in to comment.