Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ref: remove the use of infinitiy #594

Merged
merged 1 commit into from
Nov 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions core/include/detray/definitions/indexing.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -296,8 +296,6 @@ DETRAY_HOST_DEVICE constexpr decltype(auto) get(
return index.index();
}

} // namespace detail

/// Overload to check for an invalid typed index link @param ti
template <typename id_t, typename index_t, typename value_t, value_t id_mask,
value_t index_mask>
Expand All @@ -307,4 +305,6 @@ DETRAY_HOST_DEVICE inline constexpr bool is_invalid_value(
return ti.is_invalid();
}

} // namespace detail

} // namespace detray
6 changes: 3 additions & 3 deletions core/include/detray/geometry/detector_volume.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ class detector_volume {
<< *this << std::endl;
return false;
}
if (is_invalid_value(index())) {
if (detail::is_invalid_value(index())) {
os << "ERROR: Volume index undefined in volume:\n"
<< *this << std::endl;
return false;
Expand All @@ -176,7 +176,7 @@ class detector_volume {
<< *this << std::endl;
return false;
}
if (is_invalid_value(m_desc.transform())) {
if (detail::is_invalid_value(m_desc.transform())) {
os << "ERROR: Volume transform undefined in volume:\n"
<< *this << std::endl;
return false;
Expand All @@ -187,7 +187,7 @@ class detector_volume {
return false;
}
const auto &acc_link = m_desc.full_link();
if (is_invalid_value(acc_link[0])) {
if (detail::is_invalid_value(acc_link[0])) {
os << "ERROR: Link to portal lookup broken in volume: " << acc_link
<< "\n in volume: " << *this << std::endl;
return false;
Expand Down
4 changes: 2 additions & 2 deletions core/include/detray/geometry/surface.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,7 @@ class surface {
<< *this << std::endl;
return false;
}
if (is_invalid_value(m_desc.transform())) {
if (detail::is_invalid_value(m_desc.transform())) {
os << "ERROR: Surface transform undefined for surface:\n"
<< *this << std::endl;
return false;
Expand All @@ -338,7 +338,7 @@ class surface {
<< *this << std::endl;
return false;
}
if (is_invalid_value(m_desc.mask())) {
if (detail::is_invalid_value(m_desc.mask())) {
os << "ERROR: Surface does not have a valid mask link:\n"
<< *this << std::endl;
return false;
Expand Down
7 changes: 3 additions & 4 deletions core/include/detray/intersection/detail/trajectories.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

// System include(s).
#include <cmath>
#include <limits>
#include <ostream>

namespace detray::detail {
Expand Down Expand Up @@ -195,7 +194,7 @@ class helix {

// Handle the case of pT ~ 0
if (getter::norm(pT) < 1e-6f) {
_vz_over_vt = std::numeric_limits<scalar_type>::infinity();
_vz_over_vt = detail::invalid_value<scalar_type>();
} else {
// Get vz over vt in new coordinate
_vz_over_vt = pz / getter::norm(pT);
Expand Down Expand Up @@ -226,7 +225,7 @@ class helix {
point3 pos(const scalar_type s) const {

// Handle the case of pT ~ 0
if (_vz_over_vt == std::numeric_limits<scalar_type>::infinity()) {
if (_vz_over_vt == detail::invalid_value<scalar_type>()) {
return _pos + s * _h0;
}

Expand All @@ -246,7 +245,7 @@ class helix {
vector3 dir(const scalar_type s) const {

// Handle the case of pT ~ 0
if (_vz_over_vt == std::numeric_limits<scalar_type>::infinity()) {
if (_vz_over_vt == detail::invalid_value<scalar_type>()) {
return _t0;
}

Expand Down
4 changes: 2 additions & 2 deletions core/include/detray/masks/annulus2D.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -198,8 +198,8 @@ class annulus2D {
c[1], c_pos[0] * math_ns::sin(c_pos[1]) - o_y,
t[1]};

constexpr scalar_t inf{std::numeric_limits<scalar_t>::infinity()};
scalar_t min_x{inf}, min_y{inf}, max_x{-inf}, max_y{-inf};
constexpr scalar_t inv{detail::invalid_value<scalar_t>()};
scalar_t min_x{inv}, min_y{inv}, max_x{-inv}, max_y{-inv};
for (unsigned int i{0u}; i < 5u; ++i) {
min_x = x_pos[i] < min_x ? x_pos[i] : min_x;
max_x = x_pos[i] > max_x ? x_pos[i] : max_x;
Expand Down
4 changes: 2 additions & 2 deletions core/include/detray/masks/unmasked.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,8 @@ class unmasked {
const bounds_t<scalar_t, kDIM>& /*bounds*/,
const scalar_t /*env*/ =
std::numeric_limits<scalar_t>::epsilon()) const {
constexpr scalar_t inf{std::numeric_limits<scalar_t>::infinity()};
return {-inf, -inf, -inf, inf, inf, inf};
constexpr scalar_t inv{detail::invalid_value<scalar_t>()};
return {-inv, -inv, -inv, inv, inv, inv};
}

/// @returns the shapes centroid in global cartesian coordinates
Expand Down
2 changes: 1 addition & 1 deletion core/include/detray/materials/material.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ struct material {
if constexpr (ratio::num == 0) {
return 0.f;
} else if constexpr (ratio::den == 0) {
return std::numeric_limits<scalar_type>::infinity();
return detail::invalid_value<scalar_type>();
} else {
return static_cast<scalar_type>(ratio::num) /
static_cast<scalar_type>(ratio::den);
Expand Down
2 changes: 1 addition & 1 deletion core/include/detray/propagator/navigator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -574,7 +574,7 @@ class navigator {
navigation.set_volume(navigation.current()->volume_link);

// Navigation reached the end of the detector world
if (is_invalid_value(navigation.volume())) {
if (detail::is_invalid_value(navigation.volume())) {
navigation.exit();
return navigation.m_heartbeat;
}
Expand Down
46 changes: 24 additions & 22 deletions core/include/detray/tools/bounding_volume.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,9 @@ class axis_aligned_bounding_volume {
std::array<scalar_t, other_shape_t::template axes<>::dim>;

// Find min/max extent of the local aabb in local coordinates
constexpr scalar_t inf{std::numeric_limits<scalar_t>::infinity()};
scalar_t min_x{inf}, min_y{inf}, min_z{inf}, max_x{-inf}, max_y{-inf},
max_z{-inf};
constexpr scalar_t inv{detail::invalid_value<scalar_t>()};
scalar_t min_x{inv}, min_y{inv}, min_z{inv}, max_x{-inv}, max_y{-inv},
max_z{-inv};
for (const auto& vol : aabbs) {
const auto min_point = vol.template loc_min<loc_point_t>();
const auto max_point = vol.template loc_max<loc_point_t>();
Expand Down Expand Up @@ -136,8 +136,8 @@ class axis_aligned_bounding_volume {

// If the volume shape is not supported, return universal minimum
assert(false);
constexpr scalar_t inf{std::numeric_limits<scalar_t>::infinity()};
return point_t{-inf, -inf, -inf};
constexpr scalar_t inv{detail::invalid_value<scalar_t>()};
return point_t{-inv, -inv, -inv};
}

/// @returns the maximum bounds of the volume in local coordinates
Expand All @@ -155,8 +155,8 @@ class axis_aligned_bounding_volume {
// If the volume shape is not supported, return universal minimum
// (or compilation error for 2D point)
assert(false);
constexpr scalar_t inf{std::numeric_limits<scalar_t>::infinity()};
return point_t{inf, inf, inf};
constexpr scalar_t inv{detail::invalid_value<scalar_t>()};
return point_t{inv, inv, inv};
}
/// @returns the minimum bounds of the volume in global cartesian
/// coordinates
Expand All @@ -176,8 +176,8 @@ class axis_aligned_bounding_volume {
// If the volume shape is not supported, return universal minimum
// (or compilation error for 2D point)
assert(false);
constexpr scalar_t inf{std::numeric_limits<scalar_t>::infinity()};
return point3_t{-inf, -inf, -inf};
constexpr scalar_t inv{detail::invalid_value<scalar_t>()};
return point3_t{-inv, -inv, -inv};
}

/// @returns the maximum bounds of the volume in global cartesian
Expand All @@ -197,8 +197,8 @@ class axis_aligned_bounding_volume {

// If the volume shape is not supported, return universal minimum
assert(false);
constexpr scalar_t inf{std::numeric_limits<scalar_t>::infinity()};
return point3_t{inf, inf, inf};
constexpr scalar_t inv{detail::invalid_value<scalar_t>()};
return point3_t{inv, inv, inv};
}

/// @returns the geometric center position in global cartesian system
Expand All @@ -212,9 +212,9 @@ class axis_aligned_bounding_volume {
const scalar_t center_z{
0.5f * (m_mask[cuboid3D<>::e_max_z] + m_mask[cuboid3D<>::e_min_z])};

return {std::isinf(center_x) ? 0.f : center_x,
std::isinf(center_y) ? 0.f : center_y,
std::isinf(center_z) ? 0.f : center_z};
return {detail::is_invalid_value(center_x) ? 0.f : center_x,
detail::is_invalid_value(center_y) ? 0.f : center_y,
detail::is_invalid_value(center_z) ? 0.f : center_z};
}

/// @brief Lower and upper point for minimum axis aligned bounding box of
Expand All @@ -241,12 +241,14 @@ class axis_aligned_bounding_volume {
const scalar_t scalor_z{
(m_mask[cuboid3D<>::e_max_z] - m_mask[cuboid3D<>::e_min_z])};

// Cannot handle 'inf' propagation through the calculation for now
if (std::isinf(scalor_x) or std::isinf(scalor_y) or
std::isinf(scalor_z)) {
// Cannot handle 'inv' propagation through the calculation for now
if (detail::is_invalid_value(scalor_x) or
detail::is_invalid_value(scalor_y) or
detail::is_invalid_value(scalor_z)) {
// If the box was infinite to begin with, it stays that way
assert(std::isinf(scalor_x) and std::isinf(scalor_y) and
std::isinf(scalor_z));
assert(detail::is_invalid_value(scalor_x) and
detail::is_invalid_value(scalor_y) and
detail::is_invalid_value(scalor_z));

return *this;
}
Expand All @@ -270,9 +272,9 @@ class axis_aligned_bounding_volume {
glob_c_points[7] = glob_c_points[1] - new_box_z;

// Find min/max extent of the local aabb in global coordinates
constexpr scalar_t inf{std::numeric_limits<scalar_t>::infinity()};
scalar_t min_x{inf}, min_y{inf}, min_z{inf}, max_x{-inf}, max_y{-inf},
max_z{-inf};
constexpr scalar_t inv{detail::invalid_value<scalar_t>()};
scalar_t min_x{inv}, min_y{inv}, min_z{inv}, max_x{-inv}, max_y{-inv},
max_z{-inv};
for (const point3_t& p : glob_c_points) {
// Check every coordinate of the point
min_x = p[0] < min_x ? p[0] : min_x;
Expand Down
2 changes: 1 addition & 1 deletion core/include/detray/tools/surface_factory.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ class surface_factory : public surface_factory_interface<detector_t> {

// Append the surfaces relative to the current number of
// surfaces in the stores
const dindex sf_idx{is_invalid_value(m_indices[idx])
const dindex sf_idx{detail::is_invalid_value(m_indices[idx])
? dindex_invalid
: m_indices[idx]};

Expand Down
2 changes: 1 addition & 1 deletion core/include/detray/tools/surface_factory_interface.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ class surface_factory_interface {
container_t &cont, const typename container_t::value_type value,
const dindex idx, Args &&... args) const {
// If no valid position is given, perform push back
if (is_invalid_value(idx)) {
if (detail::is_invalid_value(idx)) {
cont.push_back(value, std::forward<Args>(args)...);

return static_cast<dindex>(cont.size() - 1u);
Expand Down
4 changes: 2 additions & 2 deletions core/include/detray/utils/consistency_checker.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ struct surface_checker {
}

// Does the mask link to an existing volume?
if (!is_invalid_value(sf.volume_link()) &&
if (!detail::is_invalid_value(sf.volume_link()) &&
(sf.volume_link() >= det.volumes().size())) {
err_stream << "ERROR: Incorrect volume link to non-existent volume "
<< sf.volume_link();
Expand Down Expand Up @@ -131,7 +131,7 @@ inline void check_empty(const detector_t &det) {
auto find_volumes =
[](const typename detector_t::volume_finder &vf) -> bool {
for (const auto &v : vf.all()) {
if (not is_invalid_value(v)) {
if (not detail::is_invalid_value(v)) {
return true;
}
}
Expand Down
2 changes: 1 addition & 1 deletion core/include/detray/utils/inspectors.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ struct print_inspector : actor {
break;
};

if (is_invalid_value(navigation.volume())) {
if (detail::is_invalid_value(navigation.volume())) {
printer.stream << "volume: " << std::setw(10) << "invalid";
} else {
printer.stream << "volume: " << std::setw(10)
Expand Down
15 changes: 8 additions & 7 deletions core/include/detray/utils/invalid_values.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,14 @@
#pragma once

// Project include(s).
#include "detray/definitions/math.hpp"
#include "detray/definitions/qualifiers.hpp"

// System include(s)
#include <limits>
#include <type_traits>

namespace detray {

namespace detail {
namespace detray::detail {

/// Invalid value for fundamental types - constexpr
template <typename T,
Expand All @@ -34,13 +33,15 @@ DETRAY_HOST_DEVICE inline T invalid_value() noexcept {
return T{};
}

} // namespace detail

template <typename T,
typename std::enable_if_t<std::is_fundamental_v<T>, bool> = true>
DETRAY_HOST_DEVICE inline constexpr bool is_invalid_value(
const T value) noexcept {
return (value == detail::invalid_value<T>());
if constexpr (std::is_signed_v<T>) {
return (math_ns::abs(value) == detail::invalid_value<T>());
} else {
return (value == detail::invalid_value<T>());
}
}

template <typename T,
Expand All @@ -50,4 +51,4 @@ DETRAY_HOST_DEVICE inline constexpr bool is_invalid_value(
return (value == detail::invalid_value<T>());
}

} // namespace detray
} // namespace detray::detail
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ namespace detray::svgtools::utils {
template <typename detector_t>
inline auto is_not_world_portal(const detray::surface<detector_t>& d_portal) {
const auto d_link_idx = d_portal.template visit_mask<link_getter>();
return !is_invalid_value(d_link_idx);
return !detail::is_invalid_value(d_link_idx);
}

/// @note expects that the detray surface has a volume link.
Expand Down Expand Up @@ -60,4 +60,4 @@ inline auto link_points(const typename detector_t::geometry_context& context,
return std::make_tuple(start, end);
}

} // namespace detray::svgtools::utils
} // namespace detray::svgtools::utils
6 changes: 3 additions & 3 deletions tests/common/include/tests/common/tools/hash_tree.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,9 @@ class hash_tree {

hashed_node(hash_t hash)
: _key(hash),
_parent(std::numeric_limits<dindex>::infinity()),
_left_child(std::numeric_limits<dindex>::infinity()),
_right_child(std::numeric_limits<dindex>::infinity()) {}
_parent(detail::invalid_value<dindex>()),
_left_child(detail::invalid_value<dindex>()),
_right_child(detail::invalid_value<dindex>()) {}

hash_t _key;
dindex _parent, _left_child, _right_child;
Expand Down
Loading
Loading