From eba8906c33b9697b58b0d59ce9f971fc2282a002 Mon Sep 17 00:00:00 2001 From: Joana Niermann Date: Wed, 15 Nov 2023 14:10:47 +0100 Subject: [PATCH] Sreamline the code --- core/include/detray/masks/masks.hpp | 13 --- .../plugins/svgtools/conversion/surface.hpp | 35 ++++---- .../svgtools/utils/surface_kernels.hpp | 79 ++++++++----------- tests/unit_tests/svgtools/masks.cpp | 14 +++- 4 files changed, 57 insertions(+), 84 deletions(-) diff --git a/core/include/detray/masks/masks.hpp b/core/include/detray/masks/masks.hpp index ed6b9b01a7..d31e6b653b 100644 --- a/core/include/detray/masks/masks.hpp +++ b/core/include/detray/masks/masks.hpp @@ -213,19 +213,6 @@ class mask { return {bounds, std::numeric_limits::max()}; } - /// @brief Calculates the center of the min bounds bounding box. - /// @returns The center point in global cartesian coordinates. - template - auto global_min_bounds_center(const transform3_t& trf) const { - const auto m = local_min_bounds(); - const auto center{ - 0.5f * (point3_t{m[cuboid3D<>::e_max_x], m[cuboid3D<>::e_max_y], - m[cuboid3D<>::e_max_z]} + - point3_t{m[cuboid3D<>::e_min_x], m[cuboid3D<>::e_min_y], - m[cuboid3D<>::e_min_z]})}; - return trf.point_to_global(center); - } - /// @brief Vertices of the mask in local cartesian coordinates. /// /// Computes vertices along the mask boundary. diff --git a/plugins/svgtools/include/detray/plugins/svgtools/conversion/surface.hpp b/plugins/svgtools/include/detray/plugins/svgtools/conversion/surface.hpp index 38e2c1ce68..af3fe119ac 100644 --- a/plugins/svgtools/include/detray/plugins/svgtools/conversion/surface.hpp +++ b/plugins/svgtools/include/detray/plugins/svgtools/conversion/surface.hpp @@ -43,7 +43,7 @@ inline void set_vertices(actsvg::proto::surface& p_surface, // Approximate any acrs in the mask shape with ten line segments auto vertices = m.vertices(10u); for (std::size_t i = 0; i < vertices.size(); i++) { - vertices[i] = m.template to_global_frame(trf, vertices[i]); + vertices[i] = m.to_global_frame(trf, vertices[i]); } std::transform( @@ -77,9 +77,7 @@ auto inline surface(const transform_t& transform, // Rotation is currently not supported. // Furthermore, only translation on z axis is supported. - using mask_t = mask>; - using shape_t = typename mask_t::shape; - + using shape_t = cylinder2D; using point3_t = typename point3_container_t::value_type; using scalar_t = typename point3_t::value_type; using p_surface_t = actsvg::proto::surface; @@ -89,8 +87,7 @@ auto inline surface(const transform_t& transform, const auto r = static_cast(m[shape_t::e_r]); const auto nhz = static_cast(m[shape_t::e_n_half_z]); const auto phz = static_cast(m[shape_t::e_p_half_z]); - const auto center = - svgtools::conversion::point(transform.translation()); + const auto center = m.center(transform); const auto hz = (phz - nhz) / 2 + center[2]; p_surface._type = p_surface_t::type::e_cylinder; @@ -107,9 +104,7 @@ auto surface(const transform_t& transform, const mask>& m) { // Rotation is currently not supported. // Furthermore, only translation on z axis is supported. - using mask_t = mask>; - using shape_t = typename mask_t::shape; - + using shape_t = ring2D<>; using point3_t = typename point3_container_t::value_type; using scalar_t = typename point3_t::value_type; using p_surface_t = actsvg::proto::surface; @@ -118,8 +113,7 @@ auto surface(const transform_t& transform, const mask>& m) { const auto ri = static_cast(m[shape_t::e_inner_r]); const auto ro = static_cast(m[shape_t::e_outer_r]); - const auto center = - svgtools::conversion::point(transform.translation()); + const auto center = m.center(transform); p_surface._type = p_surface_t::type::e_disc; p_surface._radii = {ri, ro}; @@ -135,9 +129,7 @@ auto inline surface(const transform_t& transform, const mask>& m) { // Rotation is currently not supported. // Furthermore, only translation on z axis is supported. - using mask_t = mask>; - using shape_t = typename mask_t::shape; - + using shape_t = annulus2D<>; using point3_t = typename point3_container_t::value_type; using scalar_t = typename point3_t::value_type; using p_surface_t = actsvg::proto::surface; @@ -146,7 +138,7 @@ auto inline surface(const transform_t& transform, const mask>& m) { auto ri = static_cast(m[shape_t::e_min_r]); auto ro = static_cast(m[shape_t::e_max_r]); - auto center = svgtools::conversion::point(m.center(transform)); + auto center = m.center(transform); p_surface._type = p_surface_t::type::e_annulus; p_surface._radii = {ri, ro}; @@ -154,6 +146,10 @@ auto inline surface(const transform_t& transform, const mask>& m) { set_measures(p_surface, m); set_vertices(p_surface, transform, m); + for (auto v : p_surface._vertices) { + std::cout << v[0] << ", " << v[1] << ", " << v[2] << std::endl; + } + return p_surface; } @@ -165,9 +161,7 @@ auto surface(const transform_t& transform, // Rotation is currently not supported. // Furthermore, only translation on z axis is supported. - using mask_t = mask>; - using shape_t = typename mask_t::shape; - + using shape_t = line; using point3_t = typename point3_container_t::value_type; using scalar_t = typename point3_t::value_type; using p_surface_t = actsvg::proto::surface; @@ -177,13 +171,12 @@ auto surface(const transform_t& transform, // All line surfaces are drawn as a circles(straws) in xy-view const auto r{static_cast(m[shape_t::e_cross_section])}; const auto hz = static_cast(m[shape_t::e_half_z]); - const auto center = - svgtools::conversion::point(transform.translation()); + const auto center = m.center(transform); p_surface._type = p_surface_t::type::e_straw; p_surface._radii = {1.f, r}; p_surface._zparameters = {-hz, hz}; - p_surface._transform._tr = center; + p_surface._transform._tr = svgtools::conversion::point(center); set_measures(p_surface, m); return p_surface; diff --git a/plugins/svgtools/include/detray/plugins/svgtools/utils/surface_kernels.hpp b/plugins/svgtools/include/detray/plugins/svgtools/utils/surface_kernels.hpp index 573d1e9f3d..a39ed47a33 100644 --- a/plugins/svgtools/include/detray/plugins/svgtools/utils/surface_kernels.hpp +++ b/plugins/svgtools/include/detray/plugins/svgtools/utils/surface_kernels.hpp @@ -101,44 +101,35 @@ struct link_start_getter { template auto inline link_start(const detray::mask>& mask, const transform_t& transform) const { - using mask_t = typename detray::mask>; - using shape_t = typename mask_t::shape; + + using shape_t = detray::ring2D<>; + using mask_t = detray::mask; using point3_t = typename mask_t::point3_t; using scalar_t = typename mask_t::scalar_type; - const scalar_t r{(mask[shape_t::e_inner_r] + mask[shape_t::e_outer_r]) / - 2.f}; + const scalar_t r{0.5f * + (mask[shape_t::e_inner_r] + mask[shape_t::e_outer_r])}; const scalar_t phi{detray::constant::pi_2}; - const scalar_t z{0}; + const scalar_t z{0.f}; - // Polar coordinate system. - const typename mask_t::local_frame_type frame{}; - - return frame.local_to_global(transform, point3_t{r, phi, z}); + return mask.to_global_frame(transform, point3_t{r, phi, 0.f}); } // Calculates the (optimal) link starting point for annuluses. template auto inline link_start(const detray::mask>& mask, const transform_t& transform) const { - using mask_t = typename detray::mask>; - using shape_t = typename mask_t::shape; + + using shape_t = detray::annulus2D<>; + using mask_t = detray::mask; using point3_t = typename mask_t::point3_t; using scalar_t = typename mask_t::scalar_type; const scalar_t r{(mask[shape_t::e_min_r] + mask[shape_t::e_max_r]) / 2.f}; const scalar_t phi{mask[shape_t::e_average_phi]}; - const scalar_t z{0}; - // Polar coordinate system. - const typename mask_t::local_frame_type frame{}; - - const auto true_center = mask.center(transform); - const auto rel_point = - frame.local_to_global(transform, point3_t{r, phi, z}) - - transform.translation(); - return rel_point + true_center; + return mask.to_global_frame(transform, point3_t{r, phi, 0.f}); } // Calculates the (optimal) link starting point for cylinders (2D). @@ -148,49 +139,45 @@ struct link_start_getter { const detray::mask>& mask, const transform_t& transform) const { - using mask_t = typename detray::mask< - detray::cylinder2D>; - using shape_t = typename mask_t::shape; + + using shape_t = detray::cylinder2D; + using mask_t = detray::mask; using point3_t = typename mask_t::point3_t; using scalar_t = typename mask_t::scalar_type; + const auto center = mask.center(transform); + const scalar_t mean_z{ + 0.5f * (mask[shape_t::e_n_half_z] + mask[shape_t::e_p_half_z])}; + const scalar_t r{mask[shape_t::e_r]}; const scalar_t phi{detray::constant::pi_2}; - const scalar_t z{0}; + // Shift the center to the actual cylider bounds + const scalar_t z{center[2] + mean_z}; - // Cylindrical coordinate system. - const typename mask_t::local_frame_type frame{}; - - const auto true_center = mask.global_min_bounds_center(transform); - const auto rel_point = - frame.local_to_global(transform, point3_t{r * phi, z, r}) - - transform.translation(); - return rel_point + true_center; + return mask.to_global_frame(transform, point3_t{r * phi, z, r}); } // Calculates the (optimal) link starting point for cylinders (3D). template auto inline link_start(const detray::mask& mask, const transform_t& transform) const { - using mask_t = typename detray::mask; - using shape_t = typename mask_t::shape; + + using shape_t = detray::cylinder3D; + using mask_t = detray::mask; using point3_t = typename mask_t::point3_t; using scalar_t = typename mask_t::scalar_type; - const scalar_t r{(mask[shape_t::e_min_r] + mask[shape_t::e_max_r]) / - 2.f}; - const scalar_t phi{ - (mask[shape_t::e_max_phi] + mask[shape_t::e_max_phi]) / 2.f}; - const scalar_t z{0}; + const auto center = mask.center(transform); + const scalar_t mean_z{ + 0.5f * (mask[shape_t::e_min_z] + mask[shape_t::e_min_z])}; - // Cylindrical coordinate system. - const typename mask_t::local_frame_type frame{}; + const scalar_t r{0.5f * + (mask[shape_t::e_min_r] + mask[shape_t::e_max_r])}; + const scalar_t phi{ + 0.5f * (mask[shape_t::e_max_phi] + mask[shape_t::e_max_phi])}; + const scalar_t z{center[2] + mean_z}; - const auto true_center = mask.global_min_bounds_center(transform); - const auto rel_point = - frame.local_to_global(transform, point3_t{r * phi, z, r}) - - transform.translation(); - return rel_point + true_center; + return mask.to_global_frame(transform, point3_t{r, phi, z}); } }; diff --git a/tests/unit_tests/svgtools/masks.cpp b/tests/unit_tests/svgtools/masks.cpp index f21d424a09..b9a5ce9adb 100644 --- a/tests/unit_tests/svgtools/masks.cpp +++ b/tests/unit_tests/svgtools/masks.cpp @@ -43,9 +43,15 @@ int main(int, char**) { // Visualize a 2D annulus. // e_min_r, e_max_r, e_min_phi_rel, e_max_phi_rel, e_average_phi, e_shift_x, - // e_shift_y, e_size - detray::mask> ann2D{0u, 100.f, 200.f, -1.f, - 1.f, 0.f, 0.f, 100.f}; + // e_shift_y + detray::mask> ann2D{0u, + 100.f, + 200.f, + -detray::constant::pi_2, + detray::constant::pi_2, + 0.f, + 0.f, + 0.f}; const auto ann2D_proto = detray::svgtools::conversion::surface(transform, ann2D); @@ -53,7 +59,7 @@ int main(int, char**) { detray::svgtools::write_svg("test_svgtools_annulus2D", {axes, ann2D_svg}); // Visualize a 2D cylinder. - // e_r, e_n_half_z, e_p_half_z, e_size + // e_r, e_n_half_z, e_p_half_z detray::mask> cyl2D{0u, 100.f, -10.f, 10.f}; const auto cyl2D_proto = detray::svgtools::conversion::surface(transform,