Skip to content

Commit

Permalink
Merge pull request #571 from niermann999/feat-integrate-svgtools
Browse files Browse the repository at this point in the history
feat: integrate svgtools
  • Loading branch information
asalzburger authored Oct 27, 2023
2 parents eb70263 + d2df63e commit f43b3ed
Show file tree
Hide file tree
Showing 20 changed files with 393 additions and 144 deletions.
20 changes: 19 additions & 1 deletion core/include/detray/tools/generators.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,24 @@ dvector<point3_t> vertices(
return {lh_lc, rh_lc, rh_uc, lh_uc};
}

/** Generate vertices, specialized for masks: line
*
* @note template types are simply forwarded to mask
*
* @param sf is the surface that generates vertices
* @param ls is the number of line segments if
*
* @return a generated list of vertices
*/
template <typename point2_t, typename point3_t, bool cross_section,
template <class> typename intersector_t, typename links_t,
typename transform3_t>
dvector<point3_t> vertices(const mask<line<cross_section, intersector_t>,
links_t, transform3_t> & /*line_mask*/,
unsigned int /*lseg*/) {
return {};
}

/// Functor to produce vertices on a mask collection in a mask tuple container.
template <typename point2_t, typename point3_t>
struct vertexer {
Expand Down Expand Up @@ -271,4 +289,4 @@ std::vector<point2_t> r_phi_polygon(scalar_t rmin, scalar_t rmax,
return r_phi_poly;
}

} // namespace detray
} // namespace detray
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ inline auto landmark(
d_intersection) {
const auto position =
svgtools::utils::intersection_point(context, detector, d_intersection);
return svgtools::conversion::landmark<point3>(position);
return svgtools::conversion::landmark<point3_t>(position);
}

/// @returns The proto landmark of a detray point.
Expand All @@ -38,4 +38,4 @@ inline auto landmark(d_point3_t& position) {
return p_lm;
}

} // namespace detray::svgtools::conversion
} // namespace detray::svgtools::conversion
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,4 @@ auto volume(const typename detector_t::geometry_context& context,
surfaces);
}

} // namespace detray::svgtools::conversion
} // namespace detray::svgtools::conversion
145 changes: 79 additions & 66 deletions plugins/svgtools/include/detray/plugins/svgtools/illustrator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,14 @@

namespace detray::svgtools {

/// @brief SVG generator for a detector and related entities. Provides an easy
/// interface for displaying typical objects in a detector. For more
/// flexibility, use the tools in svgtools::conversion to convert detray objects
/// to their respective proto object. Use functions in svgtools::meta::display
/// and actsvg::display to display the proto objects.
/// @brief SVG generator for a detector and related entities.
///
/// Provides an easy interface for displaying typical objects in a detector.
/// For more flexibility, use the tools in svgtools::conversion to convert
/// detray objects to their respective proto object.
/// Use functions in svgtools::meta::display and actsvg::display to display
/// the proto objects.
///
/// @note Avoid using ids containing spaces or dashes as this seems to cause
/// issues (for instance regarding information boxes). Furthermore, to view
/// information boxes, they must be enabled in the constructor. Furthermore the
Expand All @@ -49,66 +52,61 @@ class illustrator {
illustrator() = delete;

/// @param detector the detector
/// @param context the geometry context
/// @param name_map naming scheme of the detector
/// @param style display style
/// @note information boxes are enabled by default
illustrator(const detector_t& detector, const geometry_context& context)
: _detector{detector}, _context{context} {}

/// @param detector the detector
/// @param context the geometry context
/// @param show_info boolean to choose if information boxes should be
/// included
illustrator(const detector_t& detector, const geometry_context& context,
const bool show_info)
: _detector{detector}, _context{context}, _show_info{show_info} {}

/// @param detector the detector
/// @param context the geometry context
/// @param show_info boolean to choose if information boxes should be
/// included
/// @param style the styling options to apply
illustrator(const detector_t& detector, const geometry_context& context,
const bool show_info, const styling::style& style)
: _detector{detector},
_context{context},
_show_info{show_info},
_style{style} {}
illustrator(const detector_t& detector,
const typename detector_t::name_map& name_map,
const styling::style& style =
detray::svgtools::styling::tableau_colorblind)
: _detector{detector}, _name_map{name_map}, _style{style} {}

/// @brief Converts a detray surface in the detector to an svg.
/// @param identification the id of the svg object.
/// @param index the index of the surface in the detector.
/// @param view the display view.
/// @returns @c actsvg::svg::object of the detector's surface.
template <typename view_t>
inline auto draw_surface(const std::string& identification,
const std::size_t index,
const view_t& view) const {
inline auto draw_surface(
const std::string& identification, const std::size_t index,
const view_t& view,
const typename detector_t::geometry_context& gctx = {}) const {
const auto surface = detray::surface{
_detector,
_detector.surface_lookup()[static_cast<detray::dindex>(index)]};
auto ret = svgtools::utils::group(identification);
actsvg::svg::object svg_sur;
std::array<int, 3> color;
if (surface.is_portal()) {
auto p_portal = svgtools::conversion::portal<point3_container>(
_context, _detector, surface);
svgtools::styling::apply_style(p_portal,
_style._volume_style._portal_style);
std::copy(p_portal._surface._fill._fc._rgb.begin(),
p_portal._surface._fill._fc._rgb.end(), color.begin());
svg_sur = actsvg::display::portal(identification, p_portal, view);
} else {
auto p_surface = svgtools::conversion::surface<point3_container>(
_context, surface);
if (!_hide_portals) {
auto p_portal = svgtools::conversion::portal<point3_container>(
gctx, _detector, surface);

svgtools::styling::apply_style(
p_portal, _style._volume_style._portal_style,
_style._do_random_coloring);

std::copy(p_portal._surface._fill._fc._rgb.begin(),
p_portal._surface._fill._fc._rgb.end(),
color.begin());

svg_sur =
actsvg::display::portal(identification, p_portal, view);
}
} else if (!(surface.is_passive() && _hide_passives)) {
auto p_surface =
svgtools::conversion::surface<point3_container>(gctx, surface);
svgtools::styling::apply_style(p_surface,
_style._volume_style._surface_style);
_style._volume_style._surface_style,
_style._do_random_coloring);
std::copy(p_surface._fill._fc._rgb.begin(),
p_surface._fill._fc._rgb.end(), color.begin());
svg_sur = actsvg::display::surface(identification, p_surface, view);
}
if (_show_info) {
if (_show_info &&
!(_hide_portals && _hide_passives && !surface.is_sensitive())) {
auto p_information_section =
svgtools::conversion::information_section<point3>(_context,
svgtools::conversion::information_section<point3>(gctx,
surface);
std::copy(color.begin(), color.end(),
p_information_section._color.begin());
Expand All @@ -128,14 +126,15 @@ class illustrator {
/// @param view the display view.
/// @returns @c actsvg::svg::object of the detector's surfaces.
template <typename iterator_t, typename view_t>
inline auto draw_surfaces(const std::string& identification,
const iterator_t& indices,
const view_t& view) const {
inline auto draw_surfaces(
const std::string& identification, const iterator_t& indices,
const view_t& view,
const typename detector_t::geometry_context& gctx = {}) const {
auto ret = svgtools::utils::group(identification);
for (const auto index : indices) {
const auto svg = draw_surface(
identification + "_surface" + std::to_string(index), index,
view);
view, gctx);
ret.add_object(svg);
}
return ret;
Expand All @@ -147,11 +146,13 @@ class illustrator {
/// @param view the display view.
/// @returns @c actsvg::svg::object of the detector's volume.
template <typename view_t>
inline auto draw_volume(const std::string& identification,
const std::size_t index, const view_t& view) const {
inline auto draw_volume(
const std::string& identification, const std::size_t index,
const view_t& view,
const typename detector_t::geometry_context& gctx = {}) const {
const auto surface_indices =
svgtools::utils::surface_indices(_detector, index);
return draw_surfaces(identification, surface_indices, view);
return draw_surfaces(identification, surface_indices, view, gctx);
}

/// @brief Converts a collection of detray volumes in the detector to an
Expand All @@ -162,14 +163,15 @@ class illustrator {
/// @param view the display view.
/// @returns @c actsvg::svg::object of the detector's volumes.
template <typename iterator_t, typename view_t>
inline auto draw_volumes(const std::string& identification,
const iterator_t& indices,
const view_t& view) const {
inline auto draw_volumes(
const std::string& identification, const iterator_t& indices,
const view_t& view,
const typename detector_t::geometry_context& gctx = {}) const {
auto ret = svgtools::utils::group(identification);
for (const auto index : indices) {
const auto svg =
draw_volume(identification + "_volume" + std::to_string(index),
index, view);
index, view, gctx);
ret.add_object(svg);
}
return ret;
Expand All @@ -180,11 +182,12 @@ class illustrator {
/// @param view the display view.
/// @returns @c actsvg::svg::object of the detector.
template <typename view_t>
inline auto draw_detector(const std::string& identification,
const view_t& view) const {
inline auto draw_detector(
const std::string& identification, const view_t& view,
const typename detector_t::geometry_context& gctx = {}) const {
auto indices =
detray::views::iota(std::size_t{0u}, _detector.volumes().size());
return draw_volumes(identification, indices, view);
return draw_volumes(identification, indices, view, gctx);
}

/// @brief Converts a point to an svg.
Expand Down Expand Up @@ -214,9 +217,10 @@ class illustrator {
detray::intersection2D<typename detector_t::surface_type,
typename detector_t::transform3>>>&
intersection_record,
const view_t& view) const {
const view_t& view,
const typename detector_t::geometry_context& gctx = {}) const {
auto p_ir = svgtools::conversion::intersection_record<point3>(
_context, _detector, intersection_record);
gctx, _detector, intersection_record);
svgtools::styling::apply_style(p_ir, _style._intersection_style);
return svgtools::meta::display::intersection_record(identification,
p_ir, view);
Expand Down Expand Up @@ -271,14 +275,14 @@ class illustrator {
detray::intersection2D<typename detector_t::surface_type,
typename detector_t::transform3>>>&
intersection_record,
const trajectory_t<transform3_t>& trajectory,
const view_t& view) const {
const trajectory_t<transform3_t>& trajectory, const view_t& view,
const typename detector_t::geometry_context& gctx = {}) const {

auto ret = svgtools::utils::group(identification);
auto i_style = svgtools::styling::copy_fill_colors(
_style._intersection_style, _style._trajectory_style);
auto p_ir = svgtools::conversion::intersection_record<point3>(
_context, _detector, intersection_record);
gctx, _detector, intersection_record);
svgtools::styling::apply_style(p_ir, i_style);
ret.add_object(
draw_trajectory(identification + "_trajectory", trajectory, view));
Expand All @@ -299,16 +303,25 @@ class illustrator {
return {};
}

/// Toggle info boxes
void show_info(bool toggle = true) { _show_info = toggle; }
/// Toggle portal surfaces
void hide_portals(bool toggle = true) { _hide_portals = toggle; }
/// Toggle passive surfaces
void hide_passives(bool toggle = true) { _hide_passives = toggle; }

private:
using point3 = std::array<actsvg::scalar, 3>;
using point3_container = std::vector<point3>;
using geometry_context = typename detector_t::geometry_context;

const actsvg::point2 _info_screen_offset{-300, 300};
const detector_t& _detector;
const geometry_context& _context;
const bool _show_info = true;
const typename detector_t::name_map& _name_map;
bool _show_info = true;
bool _hide_portals = false;
bool _hide_passives = false;
const styling::style _style = styling::style1;
};

} // namespace detray::svgtools
} // namespace detray::svgtools
Loading

0 comments on commit f43b3ed

Please sign in to comment.