Skip to content

Commit

Permalink
Sreamline the code
Browse files Browse the repository at this point in the history
  • Loading branch information
niermann999 committed Nov 15, 2023
1 parent d13463e commit eba8906
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 84 deletions.
13 changes: 0 additions & 13 deletions core/include/detray/masks/masks.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -213,19 +213,6 @@ class mask {
return {bounds, std::numeric_limits<unsigned int>::max()};
}

/// @brief Calculates the center of the min bounds bounding box.
/// @returns The center point in global cartesian coordinates.
template <typename transform3_t>
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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ inline void set_vertices(actsvg::proto::surface<point3_container_t>& 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<transform_t>(trf, vertices[i]);
vertices[i] = m.to_global_frame(trf, vertices[i]);
}

std::transform(
Expand Down Expand Up @@ -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<cylinder2D<kRadialCheck, intersector_t>>;
using shape_t = typename mask_t::shape;

using shape_t = cylinder2D<kRadialCheck, intersector_t>;
using point3_t = typename point3_container_t::value_type;
using scalar_t = typename point3_t::value_type;
using p_surface_t = actsvg::proto::surface<point3_container_t>;
Expand All @@ -89,8 +87,7 @@ auto inline surface(const transform_t& transform,
const auto r = static_cast<scalar_t>(m[shape_t::e_r]);
const auto nhz = static_cast<scalar_t>(m[shape_t::e_n_half_z]);
const auto phz = static_cast<scalar_t>(m[shape_t::e_p_half_z]);
const auto center =
svgtools::conversion::point<point3_t>(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;
Expand All @@ -107,9 +104,7 @@ auto surface(const transform_t& transform, const mask<ring2D<>>& m) {
// Rotation is currently not supported.
// Furthermore, only translation on z axis is supported.

using mask_t = mask<ring2D<>>;
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<point3_container_t>;
Expand All @@ -118,8 +113,7 @@ auto surface(const transform_t& transform, const mask<ring2D<>>& m) {

const auto ri = static_cast<scalar_t>(m[shape_t::e_inner_r]);
const auto ro = static_cast<scalar_t>(m[shape_t::e_outer_r]);
const auto center =
svgtools::conversion::point<point3_t>(transform.translation());
const auto center = m.center(transform);

p_surface._type = p_surface_t::type::e_disc;
p_surface._radii = {ri, ro};
Expand All @@ -135,9 +129,7 @@ auto inline surface(const transform_t& transform, const mask<annulus2D<>>& m) {
// Rotation is currently not supported.
// Furthermore, only translation on z axis is supported.

using mask_t = mask<annulus2D<>>;
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<point3_container_t>;
Expand All @@ -146,14 +138,18 @@ auto inline surface(const transform_t& transform, const mask<annulus2D<>>& m) {

auto ri = static_cast<scalar_t>(m[shape_t::e_min_r]);
auto ro = static_cast<scalar_t>(m[shape_t::e_max_r]);
auto center = svgtools::conversion::point<point3_t>(m.center(transform));
auto center = m.center(transform);

p_surface._type = p_surface_t::type::e_annulus;
p_surface._radii = {ri, ro};
p_surface._zparameters = {center[2], 0.f};
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;
}

Expand All @@ -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<line<kSquareCrossSect, intersector_t>>;
using shape_t = typename mask_t::shape;

using shape_t = line<kSquareCrossSect, intersector_t>;
using point3_t = typename point3_container_t::value_type;
using scalar_t = typename point3_t::value_type;
using p_surface_t = actsvg::proto::surface<point3_container_t>;
Expand All @@ -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<scalar_t>(m[shape_t::e_cross_section])};
const auto hz = static_cast<scalar_t>(m[shape_t::e_half_z]);
const auto center =
svgtools::conversion::point<point3_t>(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<point3_t>(center);
set_measures(p_surface, m);

return p_surface;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,44 +101,35 @@ struct link_start_getter {
template <typename transform_t>
auto inline link_start(const detray::mask<detray::ring2D<>>& mask,
const transform_t& transform) const {
using mask_t = typename detray::mask<detray::ring2D<>>;
using shape_t = typename mask_t::shape;

using shape_t = detray::ring2D<>;
using mask_t = detray::mask<shape_t>;
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<scalar_t>::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 <typename transform_t>
auto inline link_start(const detray::mask<detray::annulus2D<>>& mask,
const transform_t& transform) const {
using mask_t = typename detray::mask<detray::annulus2D<>>;
using shape_t = typename mask_t::shape;

using shape_t = detray::annulus2D<>;
using mask_t = detray::mask<shape_t>;
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).
Expand All @@ -148,49 +139,45 @@ struct link_start_getter {
const detray::mask<detray::cylinder2D<kRadialCheck, intersector_t>>&
mask,
const transform_t& transform) const {
using mask_t = typename detray::mask<
detray::cylinder2D<kRadialCheck, intersector_t>>;
using shape_t = typename mask_t::shape;

using shape_t = detray::cylinder2D<kRadialCheck, intersector_t>;
using mask_t = detray::mask<shape_t>;
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<scalar_t>::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 <typename transform_t>
auto inline link_start(const detray::mask<detray::cylinder3D>& mask,
const transform_t& transform) const {
using mask_t = typename detray::mask<detray::cylinder3D>;
using shape_t = typename mask_t::shape;

using shape_t = detray::cylinder3D;
using mask_t = detray::mask<shape_t>;
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});
}
};

Expand Down
14 changes: 10 additions & 4 deletions tests/unit_tests/svgtools/masks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,17 +43,23 @@ 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<detray::annulus2D<>> ann2D{0u, 100.f, 200.f, -1.f,
1.f, 0.f, 0.f, 100.f};
// e_shift_y
detray::mask<detray::annulus2D<>> ann2D{0u,
100.f,
200.f,
-detray::constant<float>::pi_2,
detray::constant<float>::pi_2,
0.f,
0.f,
0.f};
const auto ann2D_proto =
detray::svgtools::conversion::surface<point3_container>(transform,
ann2D);
const auto ann2D_svg = actsvg::display::surface("", ann2D_proto, view);
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<detray::cylinder2D<>> cyl2D{0u, 100.f, -10.f, 10.f};
const auto cyl2D_proto =
detray::svgtools::conversion::surface<point3_container>(transform,
Expand Down

0 comments on commit eba8906

Please sign in to comment.