Skip to content

Commit

Permalink
Implemented remaining
Browse files Browse the repository at this point in the history
  • Loading branch information
fredevb committed Aug 4, 2023
1 parent 214b7e4 commit 8e0be4b
Show file tree
Hide file tree
Showing 38 changed files with 994 additions and 886 deletions.
14 changes: 14 additions & 0 deletions core/include/detray/core/detector.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,20 @@ class detector {
return _surface_lookup;
}

/// @return the surface by @param surface_index - const
DETRAY_HOST_DEVICE
inline auto surface_by_index(dindex surface_index) const
-> const surface<detector> {
return detray::surface{*this, _surface_lookup[surface_index]};
}

/// @return the surface by @param surface_index - non-const access
DETRAY_HOST_DEVICE
inline auto surface_by_index(dindex surface_index)
-> surface<detector> {
return detray::surface{*this, _surface_lookup[surface_index]};
}

/// @returns a surface using its barcode - const
DETRAY_HOST_DEVICE
constexpr auto surface(geometry::barcode bcd) const
Expand Down
81 changes: 75 additions & 6 deletions core/include/detray/masks/annulus2D.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -305,22 +305,91 @@ class annulus2D {
typename scalar_t, std::size_t kDIM,
typename std::enable_if_t<kDIM == e_size, bool> = true>
DETRAY_HOST inline point3_container_t local_vertices(
const bounds_t<scalar_t, kDIM>&) const {
return {};
const bounds_t<scalar_t, kDIM>& bounds) const {
using point3_t = typename point3_container_t::value_type;
scalar_t z{0};
const auto c = corners(bounds);
point3_t v1 = {c[0], c[1], z};
point3_t v2 = {c[2], c[3], z};
point3_t v3 = {c[4], c[4], z};
point3_t v4 = {c[6], c[7], z};
return {v1, v2, v4, v3};
}

/// @brief Finds the closest point lying on the surface to the given point.
/// @brief Finds the shape's nearest point to the given point.
///
/// @param bounds the boundary values for this shape.
/// @param loc_p the point in the local coordinate system.
///
/// @returns the closest point lying on the surface in the local_coordinate system.
/// @returns the nearest point in the local_coordinate system.
template <template <typename, std::size_t> class bounds_t,
typename scalar_t, std::size_t kDIM, typename point_t,
typename std::enable_if_t<kDIM == e_size, bool> = true>
DETRAY_HOST inline point_t closest_surface_point(
DETRAY_HOST inline point_t nearest_point(
const bounds_t<scalar_t, kDIM>& bounds, const point_t& loc_p) const {
return point_t{};

// For transforming between polar and cartesian coordinate systems.
auto glo_to_loc = [](const point_t& point){
return point_t{getter::perp(point), getter::phi(point), point[2]};
};

auto loc_to_glo = [](const point_t& point){
const scalar_t x = point[0] * math_ns::cos(point[1]);
const scalar_t y = point[0] * math_ns::sin(point[1]);
return point_t{x, y, point[2]};
};

const point_t glo_shift{bounds[e_shift_x], bounds[e_shift_y], 0};
const auto loc_shift = glo_to_loc(glo_shift);

// To account for the shift.
loc_p -= loc_shift;
const auto glo_p = loc_to_glo(loc_p);

// There are 3 options either the closest point is on the left side or right side of the opening or it is not by the opening.

// Checking if it is not by the opening by calculating under the assumption that there is no opening.
const auto r = std::clamp(loc_p[0], bounds[e_min_r], bounds[e_max_r]);
const auto phi = loc_p[1];
const scalar_t z{0};
const auto loc_option1 = point_t{r, phi, z} + loc_shift;

// Since it was assumed there was no opening, now checking if the point found is valid.
if (check_boundaries(bounds, loc_option1, 0)){
return loc_option1;
}

// If it's not in the bounds it's because there is an opening and the closest point is by the opening.

// For finding closest point on the "straight" part of the opening (returns point in cartesian coordinate system).
auto line_closest = [glo_p, bounds, loc_shift](const scalar_t& phi_opening){
const scalar_t z{0};
const auto r_min = bounds[e_min_r];
const auto r_max = bounds[e_max_r];

// Start and end point of the line
const auto p1 = loc_to_glo(point_t{r_min, phi_opening, z});
const auto p2 = loc_to_glo(point_t{r_max, phi_opening, z});

// The direction of the line
const auto d = p1 - p2;

// The direction of the given point relative to p2.
const auto point = glo_p - p2;

//Projection.
return algebra::cmath::dot(d, point)/algebra::cmath::normalize(d) * d + p2;
};

const auto glo_option1 = line_closest(bounds[e_min_phi_rel] - bounds[e_average_phi]);
const auto glo_option2 = line_closest(bounds[e_max_phi_rel] - bounds[e_average_phi]);

// Determine if which of the two remaining possibilities are closest.
if (algebra::cmath::dot(glo_option1, glo_option1) < algebra::cmath::dot(glo_option2, glo_option2)){
return glo_to_loc(glo_option1) + loc_shift;
}

return glo_to_loc(glo_option2) + loc_shift;
}
};

Expand Down
8 changes: 4 additions & 4 deletions core/include/detray/masks/cuboid3D.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -152,19 +152,19 @@ class cuboid3D {
return {v1, v2, v4, v3, v7, v8, v6, v5};
}

/// @brief Finds the closest point lying on the surface to the given point.
/// @brief Finds the shape's nearest point to the given point.
///
/// @param bounds the boundary values for this shape.
/// @param loc_p the point in the local coordinate system.
///
/// @returns the closest point lying on the surface in the local_coordinate system.
/// @returns the nearest point in the local_coordinate system.
template <template <typename, std::size_t> class bounds_t,
typename scalar_t, std::size_t kDIM, typename point_t,
typename std::enable_if_t<kDIM == e_size, bool> = true>
DETRAY_HOST inline point_t closest_surface_point(
DETRAY_HOST inline point_t nearest_point(
const bounds_t<scalar_t, kDIM>& bounds, const point_t& loc_p) const {
const scalar_t x = std::clamp(loc_p[0], bounds[e_min_x], bounds[e_max_x]);
const scalar_t y = std::clamp(loc_p[1], bounds[e_min_z], bounds[e_max_z]);
const scalar_t y = std::clamp(loc_p[1], bounds[e_min_y], bounds[e_max_y]);
const scalar_t z = std::clamp(loc_p[2], bounds[e_min_z], bounds[e_max_z]);
return point_t{x, y, z};
}
Expand Down
7 changes: 4 additions & 3 deletions core/include/detray/masks/cylinder2D.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -157,16 +157,17 @@ class cylinder2D {
const bounds_t<scalar_t, kDIM>&) const {
return {};
}
/// @brief Finds the closest point lying on the surface to the given point.

/// @brief Finds the shape's nearest point to the given point.
///
/// @param bounds the boundary values for this shape.
/// @param loc_p the point in the local coordinate system.
///
/// @returns the closest point lying on the surface in the local_coordinate system.
/// @returns the nearest point in the local_coordinate system.
template <template <typename, std::size_t> class bounds_t,
typename scalar_t, std::size_t kDIM, typename point_t,
typename std::enable_if_t<kDIM == e_size, bool> = true>
DETRAY_HOST inline point_t closest_surface_point(
DETRAY_HOST inline point_t nearest_point(
const bounds_t<scalar_t, kDIM>& bounds, const point_t& loc_p) const {
const scalar_t phi{loc_p[0] / loc_p[2]};
const scalar_t z = std::clamp(loc_p[1], bounds[e_n_half_z], bounds[e_p_half_z]);
Expand Down
29 changes: 25 additions & 4 deletions core/include/detray/masks/cylinder3D.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include "detray/intersection/cylinder_intersector.hpp"
#include "detray/surface_finders/grid/detail/axis_binning.hpp"
#include "detray/surface_finders/grid/detail/axis_bounds.hpp"
#include "detray/masks/annulus2D.hpp"

// System include(s)
#include <cmath>
Expand Down Expand Up @@ -140,18 +141,38 @@ class cylinder3D {
return {};
}

/// @brief Finds the closest point lying on the surface to the given point.
/// @brief Finds the shape's nearest point to the given point.
///
/// @param bounds the boundary values for this shape.
/// @param loc_p the point in the local coordinate system.
///
/// @returns the closest point lying on the surface in the local_coordinate system.
/// @returns the nearest point in the local_coordinate system.
template <template <typename, std::size_t> class bounds_t,
typename scalar_t, std::size_t kDIM, typename point_t,
typename std::enable_if_t<kDIM == e_size, bool> = true>
DETRAY_HOST inline point_t closest_surface_point(
DETRAY_HOST inline point_t nearest_point(
const bounds_t<scalar_t, kDIM>& bounds, const point_t& loc_p) const {
return point_t{};
const auto ann_bounds = annulus_slice(bounds);
const annulus2D<> ann{};

const auto ann_nearest = ann.nearest_point(ann_bounds, loc_p);
const auto z = std::clamp(loc_p[2], bounds[e_min_z], bounds[e_max_z]);
return point_t{ann_nearest[0], ann_nearest[1], z};
}

/// @brief Calculates the 2D annulus bounds.
///
/// @returns the bounds.
template <template <typename, std::size_t> class bounds_t,
typename scalar_t, std::size_t kDIM,
typename std::enable_if_t<kDIM == e_size, bool> = true>
DETRAY_HOST inline auto annulus_slice(
const bounds_t<scalar_t, kDIM>& bounds) const {
const scalar_t average_phi{(bounds[e_min_phi] + bounds[e_max_phi])/2};
const scalar_t shift_x{0};
const scalar_t shift_y{0};
const bounds_t<scalar_t, kDIM> ann_bounds{bounds[e_min_r], bounds[e_max_r], bounds[e_min_phi]-average_phi, bounds[e_max_phi]-average_phi, average_phi, shift_x, shift_y};
return ann_bounds;
}
};

Expand Down
24 changes: 18 additions & 6 deletions core/include/detray/masks/line.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -170,22 +170,34 @@ class line {
typename scalar_t, std::size_t kDIM,
typename std::enable_if_t<kDIM == e_size, bool> = true>
DETRAY_HOST inline point3_container_t local_vertices(
const bounds_t<scalar_t, kDIM>&) const {
return {};
const bounds_t<scalar_t, kDIM>& bounds) const {
using point3_t = typename point3_container_t::value_type;
point3_t v1 = {-bounds[e_cross_section], -bounds[e_cross_section], -bounds[e_half_z]};
point3_t v2 = {-bounds[e_cross_section], -bounds[e_cross_section], bounds[e_half_z]};
point3_t v3 = {-bounds[e_cross_section], bounds[e_cross_section], -bounds[e_half_z]};
point3_t v4 = {-bounds[e_cross_section], bounds[e_cross_section], bounds[e_half_z]};
point3_t v5 = {bounds[e_cross_section], -bounds[e_cross_section], -bounds[e_half_z]};
point3_t v6 = {bounds[e_cross_section], -bounds[e_cross_section], bounds[e_half_z]};
point3_t v7 = {bounds[e_cross_section], bounds[e_cross_section], -bounds[e_half_z]};
point3_t v8 = {bounds[e_cross_section], bounds[e_cross_section], bounds[e_half_z]};
return {v1, v2, v4, v3, v7, v8, v6, v5};
}

/// @brief Finds the closest point lying on the surface to the given point.
/// @brief Finds the shape's nearest point to the given point.
///
/// @param bounds the boundary values for this shape.
/// @param loc_p the point in the local coordinate system.
///
/// @returns the closest point lying on the surface in the local_coordinate system.
/// @returns the nearest point in the local_coordinate system.
template <template <typename, std::size_t> class bounds_t,
typename scalar_t, std::size_t kDIM, typename point_t,
typename std::enable_if_t<kDIM == e_size, bool> = true>
DETRAY_HOST inline point_t closest_surface_point(
DETRAY_HOST inline point_t nearest_point(
const bounds_t<scalar_t, kDIM>& bounds, const point_t& loc_p) const {
return point_t{};
const scalar_t x = std::clamp(loc_p[0], -bounds[e_cross_section], bounds[e_cross_section]);
const scalar_t y = std::clamp(loc_p[1], -bounds[e_cross_section], bounds[e_cross_section]);
const scalar_t z = std::clamp(loc_p[2], -bounds[e_half_z], bounds[e_half_z]);
return point_t{x, y, z};
}
};

Expand Down
8 changes: 4 additions & 4 deletions core/include/detray/masks/masks.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -231,14 +231,14 @@ class mask {
return _shape.template local_vertices<point3_container_t>(_values);
}

/// @brief Finds the closest point lying on the surface to the given point.
/// @brief Finds the shape's nearest point to the given point.
///
/// @param bounds the boundary values for this shape.
/// @param loc_p the point in the local coordinate system.
///
/// @returns the closest point lying on the surface in the local_coordinate system.
DETRAY_HOST inline auto closest_surface_point(const point3_t& local_point) const {
return _shape.template closest_surface_point(_values, local_point);
/// @returns the nearest point in the local_coordinate system.
DETRAY_HOST inline auto nearest_point(const point3_t& local_point) const {
return _shape.template nearest_point(_values, local_point);
}

/// @returns a string representation of the mask
Expand Down
20 changes: 12 additions & 8 deletions core/include/detray/masks/rectangle2D.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -137,25 +137,29 @@ class rectangle2D {
DETRAY_HOST inline point3_container_t local_vertices(
const bounds_t<scalar_t, kDIM>& bounds) const {
using point3_t = typename point3_container_t::value_type;
point3_t v1 = {bounds[e_half_x], bounds[e_half_y], 0};
point3_t v2 = {bounds[e_half_x], -bounds[e_half_y], 0};
point3_t v3 = {-bounds[e_half_x], -bounds[e_half_y], 0};
point3_t v4 = {-bounds[e_half_x], bounds[e_half_y], 0};
scalar_t z{0};
point3_t v1 = {bounds[e_half_x], bounds[e_half_y], z};
point3_t v2 = {bounds[e_half_x], -bounds[e_half_y], z};
point3_t v3 = {-bounds[e_half_x], -bounds[e_half_y], z};
point3_t v4 = {-bounds[e_half_x], bounds[e_half_y], z};
return {v1, v2, v3, v4};
}

/// @brief Finds the closest point lying on the surface to the given point.
/// @brief Finds the shape's nearest point to the given point.
///
/// @param bounds the boundary values for this shape.
/// @param loc_p the point in the local coordinate system.
///
/// @returns the closest point lying on the surface in the local_coordinate system.
/// @returns the nearest point in the local_coordinate system.
template <template <typename, std::size_t> class bounds_t,
typename scalar_t, std::size_t kDIM, typename point_t,
typename std::enable_if_t<kDIM == e_size, bool> = true>
DETRAY_HOST inline point_t closest_surface_point(
DETRAY_HOST inline point_t nearest_point(
const bounds_t<scalar_t, kDIM>& bounds, const point_t& loc_p) const {
return point_t{};
const scalar_t x = std::clamp(loc_p[0], -bounds[e_half_x], bounds[e_half_x]);
const scalar_t y = std::clamp(loc_p[1], -bounds[e_half_y], bounds[e_half_y]);
const scalar_t z = scalar_t{0};
return point_t{x, y, z};
}
};

Expand Down
8 changes: 4 additions & 4 deletions core/include/detray/masks/ring2D.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -141,20 +141,20 @@ class ring2D {
return {};
}

/// @brief Finds the closest point lying on the surface to the given point.
/// @brief Finds the shape's nearest point to the given point.
///
/// @param bounds the boundary values for this shape.
/// @param loc_p the point in the local coordinate system.
///
/// @returns the closest point lying on the surface in the local_coordinate system.
/// @returns the nearest point in the local_coordinate system.
template <template <typename, std::size_t> class bounds_t,
typename scalar_t, std::size_t kDIM, typename point_t,
typename std::enable_if_t<kDIM == e_size, bool> = true>
DETRAY_HOST inline point_t closest_surface_point(
DETRAY_HOST inline point_t nearest_point(
const bounds_t<scalar_t, kDIM>& bounds, const point_t& loc_p) const {
const auto r = std::clamp(loc_p[0], bounds[e_inner_r], bounds[e_outer_r]);
const auto phi = loc_p[1];
const auto z = 0;
const scalar_t z{0};
return point_t{r, phi, z};
}
};
Expand Down
20 changes: 14 additions & 6 deletions core/include/detray/masks/single3D.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -133,22 +133,30 @@ class single3D {
typename scalar_t, std::size_t kDIM,
typename std::enable_if_t<kDIM == e_size, bool> = true>
DETRAY_HOST inline point3_container_t local_vertices(
const bounds_t<scalar_t, kDIM>&) const {
return {};
const bounds_t<scalar_t, kDIM>& bounds) const {
using point3_t = typename point3_container_t::value_type;
point3_t v1{};
v1[kCheckIndex] += bounds[e_lower];
point3_t v2{};
v2[kCheckIndex] += bounds[e_upper];
return {v1, v2};
}

/// @brief Finds the closest point lying on the surface to the given point.
/// @brief Finds the shape's nearest point to the given point.
///
/// @param bounds the boundary values for this shape.
/// @param loc_p the point in the local coordinate system.
///
/// @returns the closest point lying on the surface in the local_coordinate system.
/// @returns the nearest point in the local_coordinate system.
template <template <typename, std::size_t> class bounds_t,
typename scalar_t, std::size_t kDIM, typename point_t,
typename std::enable_if_t<kDIM == e_size, bool> = true>
DETRAY_HOST inline point_t closest_surface_point(
DETRAY_HOST inline point_t nearest_point(
const bounds_t<scalar_t, kDIM>& bounds, const point_t& loc_p) const {
return point_t{};
auto a = std::clamp(loc_p[kCheckIndex], bounds[e_lower], bounds[e_upper]);
point_t ret{};
ret[kCheckIndex] = a;
return ret;
}
};

Expand Down
Loading

0 comments on commit 8e0be4b

Please sign in to comment.