12#include "detray/core/concepts.hpp"
13#include "detray/core/detail/container_views.hpp"
14#include "detray/definitions/containers.hpp"
15#include "detray/definitions/detail/qualifiers.hpp"
16#include "detray/definitions/indexing.hpp"
17#include "detray/definitions/navigation.hpp"
18#include "detray/geometry/identifier.hpp"
19#include "detray/geometry/tracking_surface.hpp"
20#include "detray/geometry/tracking_volume.hpp"
21#include "detray/navigation/detail/intersection_kernel.hpp"
22#include "detray/navigation/detail/print_state.hpp"
23#include "detray/navigation/intersection/intersection.hpp"
24#include "detray/navigation/intersection/ray_intersector.hpp"
25#include "detray/navigation/navigation_config.hpp"
26#include "detray/utils/invalid_values.hpp"
27#include "detray/utils/logging.hpp"
28#include "detray/utils/ranges.hpp"
33namespace detray::navigation {
38struct void_inspector {
40 struct void_view :
public detray::detail::dbase_view {};
47 constexpr void_inspector() =
default;
55 template <
typename state_t>
56 DETRAY_HOST_DEVICE
constexpr void operator()(
const state_t & ,
57 const char * )
const {
77 std::size_t k_cache_capacity,
typename inspector_t,
78 typename intersection_t>
80 base_state<derived_t, detector_t, k_cache_capacity,
81 inspector_t, intersection_t>> {
83 static_assert(k_cache_capacity >= 2u,
84 "Navigation cache needs to have a capacity larger than 1");
89 constexpr auto cast_impl() -> derived_t & {
90 return static_cast<derived_t &
>(*this);
93 constexpr auto cast_impl()
const ->
const derived_t & {
94 return static_cast<const derived_t &
>(*this);
123 using nav_link_t =
typename detector_t::surface_type::navigation_link;
134 constexpr explicit base_state(
const detector_t &det) : m_detector(&det) {}
137 template <concepts::device_view view_t>
138 DETRAY_HOST_DEVICE
constexpr base_state(
const detector_t &det, view_t view)
139 : m_detector(&det), m_inspector(view) {}
143 constexpr auto detector() const -> const detector_t & {
144 return (*m_detector);
152 detray::ranges::advance(itr,
160 return std::as_const(*this).begin();
167 detray::ranges::advance(itr, m_last + 1);
174 return std::as_const(*this).end();
182 return m_candidates[
static_cast<std::size_t
>(m_last)];
186 static consteval std::size_t
capacity() {
return k_cache_capacity; }
190 constexpr auto status() const -> navigation::
status {
return m_status; }
195 return cast_impl().status() > navigation::status::e_unknown;
208 if (m_direction != dir) {
210 cast_impl().set_no_trust();
211 DETRAY_VERBOSE_HOST_DEVICE(
"Re-navigate after explicit direction change");
225 DETRAY_VERBOSE_HOST(
"Setting external mask tolerance: " << tol);
226 m_external_mask_tol = tol;
232 return m_trust_level;
239 DETRAY_VERBOSE_HOST_DEVICE(
"Flagging re-navigation: \"no trust\"");
240 m_trust_level = navigation::trust_level::e_no_trust;
256 return (m_status == navigation::status::e_on_object ||
257 m_status == navigation::status::e_on_portal);
264 return (m_status == navigation::status::e_on_object) &&
265 (cast_impl().geometry_identifier().id() == surface_id::e_sensitive);
272 return (m_status == navigation::status::e_on_object) &&
273 (cast_impl().geometry_identifier().id() == surface_id::e_passive);
280 return m_status == navigation::status::e_on_portal;
288 (cast_impl().current().surface().has_material());
295 return (m_status == navigation::status::e_exit);
305 assert(detray::detail::is_invalid_value(
static_cast<nav_link_t>(v)) ||
307 if (v != m_volume_index) {
308 DETRAY_VERBOSE_HOST_DEVICE(
"Setting new volume %d", v);
310 cast_impl().set_no_trust();
324 assert(m_last - cast_impl().
next_index() + 1 >= 0);
325 return static_cast<dindex>(m_last - cast_impl().next_index() + 1);
331 return cast_impl().n_candidates() == 0u;
339 return m_candidates[
static_cast<std::size_t
>(m_next - 1)];
346 return m_candidates[
static_cast<std::size_t
>(m_next)];
353 return cast_impl().current().surface().identifier();
360 return cast_impl().current().is_edge();
367 return cast_impl().current().is_inside();
375 return cast_impl().current().is_probably_inside();
382 assert(math::isfinite(cast_impl().
target().path()));
383 return static_cast<scalar_t>(cast_impl().direction()) *
384 cast_impl().target().path();
392 return surface_t<detector_t>{*m_detector,
current().surface()};
399 return volume_t<detector_t>{*m_detector, m_volume_index};
406 return surface_t<detector_t>{*m_detector, cast_impl().target().surface()};
413 return volume_t<detector_t>{*m_detector,
414 cast_impl().target().surface().volume()};
419 DETRAY_HOST_DEVICE
constexpr void exit() {
420 m_status = navigation::status::e_exit;
421 DETRAY_VERBOSE_HOST_DEVICE(
"Exited");
422 cast_impl().run_inspector({},
point3_t{0.f, 0.f, 0.f},
428 DETRAY_HOST_DEVICE
constexpr void pause() {
429 DETRAY_VERBOSE_HOST_DEVICE(
"Paused by actor");
430 cast_impl().run_inspector({},
point3_t{0.f, 0.f, 0.f},
431 vector3_t{0.f, 0.f, 0.f},
"Paused by actor: ");
438 DETRAY_HOST_DEVICE
constexpr void abort(
439 const char *custom_msg =
"Navigator (unknown reason)") {
440 m_status = navigation::status::e_abort;
444 struct message_wrapper {
445 const char *
const m_msg{
nullptr};
448 constexpr const char *
operator()()
const {
return m_msg; }
451 assert(custom_msg !=
nullptr);
452 DETRAY_ERROR_HOST(
"Aborted: " << custom_msg);
454 cast_impl().run_inspector({},
point3_t{0.f, 0.f, 0.f},
456 "Aborted: ", message_wrapper{custom_msg});
464 template <
typename debug_msg_generator_t>
465 requires(!std::same_as<char *, debug_msg_generator_t>)
466 DETRAY_HOST_DEVICE
constexpr void abort(
467 const debug_msg_generator_t &debug_msg_generator) {
468 m_status = navigation::status::e_abort;
470 DETRAY_ERROR_HOST(
"Aborted: " << debug_msg_generator());
472 cast_impl().run_inspector({},
point3_t{0.f, 0.f, 0.f},
474 "Aborted: ", debug_msg_generator);
479 constexpr const auto &
inspector()
const {
return m_inspector; }
490 const dist_t idx{cast_impl().next_index()};
491 detray::ranges::advance(
492 itr, (cast_impl().
is_on_surface() && (idx >= 1)) ? idx - 1 : idx);
500 detray::ranges::advance(itr, m_last + 1);
507 assert(
static_cast<std::size_t
>(m_last) < m_candidates.size());
508 return m_candidates[
static_cast<std::size_t
>(m_last)];
516 detray::ranges::distance(m_candidates.cbegin(), new_next)};
517 cast_impl().next_index(
static_cast<dist_t>(new_idx));
518 assert(cast_impl().
next_index() <= m_last + 1);
526 detray::ranges::distance(m_candidates.cbegin(), new_last) - 1};
528 assert(m_last <
static_cast<dist_t>(k_cache_capacity));
545 assert(m_next <
static_cast<dist_t>(k_cache_capacity) + 1);
553 assert(m_last >= -1);
554 assert(m_last <
static_cast<dist_t>(k_cache_capacity));
561 assert(m_next <
static_cast<dist_t>(k_cache_capacity) + 1);
574 return m_candidates[
static_cast<std::size_t
>(m_next - 1)];
580 assert(
static_cast<std::size_t
>(m_next) < m_candidates.size());
581 return m_candidates[
static_cast<std::size_t
>(m_next)];
586 constexpr void status(navigation::status s) {
587 DETRAY_DEBUG_HOST(
"Setting nav. status: " << s);
594 DETRAY_DEBUG_HOST(
"Setting trust level: " << t);
602 for (std::size_t i = 0u; i < k_cache_capacity; ++i) {
603 m_candidates[i].set_path(std::numeric_limits<scalar_t>::max());
616 [[maybe_unused]]
const point3_t &track_pos,
617 [[maybe_unused]]
const vector3_t &track_dir,
618 [[maybe_unused]]
const char *message) {
619 [[maybe_unused]]
auto derived =
static_cast<const derived_t &
>(*this);
621 if constexpr (!std::is_same_v<inspector_t, navigation::void_inspector>) {
622 cast_impl().inspector()(derived, cfg, track_pos, track_dir, message);
625 DETRAY_DEBUG_HOST(
"" << message <<
"\n"
626 << detray::navigation::print_state(derived)
627 << detray::navigation::print_candidates(
628 derived, cfg, track_pos, track_dir));
638 template <
typename debug_msg_generator_t>
641 [[maybe_unused]]
const point3_t &track_pos,
642 [[maybe_unused]]
const vector3_t &track_dir,
643 [[maybe_unused]]
const char *message,
644 [[maybe_unused]]
const debug_msg_generator_t &msg_gen) {
645 [[maybe_unused]]
auto derived =
static_cast<const derived_t &
>(*this);
647 if constexpr (!std::is_same_v<inspector_t, navigation::void_inspector>) {
648 cast_impl().inspector()(derived, cfg, track_pos, track_dir, message,
652 DETRAY_DEBUG_HOST(
"" << message << msg_gen() <<
"\n"
653 << detray::navigation::print_state(derived)
654 << detray::navigation::print_candidates(
655 derived, cfg, track_pos, track_dir));
661 friend std::ostream &
operator<<(std::ostream &os,
const derived_t &s) {
662 os << detray::navigation::print_state(s)
663 << detray::navigation::print_candidates(s, {},
point3_t{0.f, 0.f, 0.f},
672 const detector_t *m_detector{
nullptr};
675 navigation::status m_status{navigation::status::e_unknown};
678 navigation::direction m_direction{navigation::direction::e_forward};
682 navigation::trust_level m_trust_level{navigation::trust_level::e_no_trust};
699 DETRAY_NO_UNIQUE_ADDRESS inspector_t m_inspector;
dscalar< algebra_t > scalar_t
Definition navigation_state.hpp:103
darray< candidate_t, k_cache_capacity > candidate_cache_t
Definition navigation_state.hpp:113
constexpr void clear_cache()
Clear the state.
Definition navigation_state.hpp:600
constexpr void abort(const char *custom_msg="Navigator (unknown reason)")
Navigation state that cannot be recovered from.
Definition navigation_state.hpp:438
constexpr bool cache_exhausted() const
Definition navigation_state.hpp:330
constexpr auto next_surface() const
Definition navigation_state.hpp:405
constexpr bool is_alive() const
Definition navigation_state.hpp:194
static consteval std::size_t capacity()
Definition navigation_state.hpp:186
constexpr scalar_t external_tol() const
Definition navigation_state.hpp:219
constexpr void last_index(dist_t pos)
Set the next surface that we want to reach (update target).
Definition navigation_state.hpp:551
typename detector_t::surface_type::navigation_link nav_link_t
Definition navigation_state.hpp:123
constexpr auto candidates() const -> const candidate_cache_t &
Definition navigation_state.hpp:317
constexpr auto end() const -> candidate_const_itr_t
Definition navigation_state.hpp:165
constexpr auto advance() -> void
Set the next surface that we want to reach (update target).
Definition navigation_state.hpp:559
constexpr void next_index(dist_t pos)
Set the next surface that we want to reach (update target).
Definition navigation_state.hpp:542
constexpr auto is_on_surface() const -> bool
Helper method to check the track has reached a module surface.
Definition navigation_state.hpp:255
constexpr auto last() const -> const candidate_t &
Definition navigation_state.hpp:179
constexpr void status(navigation::status s)
Set the status to.
Definition navigation_state.hpp:586
constexpr base_state(const detector_t &det)
Constructor using a given detector.
Definition navigation_state.hpp:134
constexpr auto last() -> candidate_t &
Definition navigation_state.hpp:506
constexpr bool is_probably_candidate() const
Definition navigation_state.hpp:373
constexpr auto cend() const -> candidate_const_itr_t
Definition navigation_state.hpp:173
constexpr auto n_candidates() const -> dindex
Definition navigation_state.hpp:323
constexpr auto target() -> candidate_t &
Definition navigation_state.hpp:579
dpoint3D< algebra_t > point3_t
Definition navigation_state.hpp:105
constexpr void set_fair_trust()
Definition navigation_state.hpp:249
constexpr void pause()
Navigation is being paused by actor: Maintain the navigation state and resume later.
Definition navigation_state.hpp:428
constexpr const auto & inspector() const
Definition navigation_state.hpp:479
constexpr bool is_edge_candidate() const
Definition navigation_state.hpp:358
constexpr void set_direction(const navigation::direction dir)
Set direction in which the navigator should search for candidates.
Definition navigation_state.hpp:207
constexpr void set_no_trust()
Update navigation trust level to 'no trust'.
Definition navigation_state.hpp:238
constexpr auto current() -> candidate_t &
Definition navigation_state.hpp:571
constexpr auto is_on_sensitive() const -> bool
Helper method to check the track has reached a sensitive surface.
Definition navigation_state.hpp:263
typename detector_t::algebra_type algebra_t
Definition navigation_state.hpp:101
intersection_type candidate_t
Definition navigation_state.hpp:111
constexpr auto is_on_portal() const -> bool
Helper method to check the track has reached a portal surface.
Definition navigation_state.hpp:279
constexpr void run_inspector(const navigation::config &cfg, const point3_t &track_pos, const vector3_t &track_dir, const char *message, const debug_msg_generator_t &msg_gen)
Call the navigation inspector.
Definition navigation_state.hpp:639
constexpr auto target() const -> const candidate_t &
Definition navigation_state.hpp:344
dvector3D< algebra_t > vector3_t
Definition navigation_state.hpp:107
constexpr auto direction() const -> navigation::direction
Definition navigation_state.hpp:200
constexpr auto cbegin() const -> candidate_const_itr_t
Definition navigation_state.hpp:159
constexpr auto current() const -> const candidate_t &
Definition navigation_state.hpp:336
typename candidate_cache_t::iterator candidate_itr_t
Definition navigation_state.hpp:115
constexpr auto current_volume() const
Definition navigation_state.hpp:398
constexpr auto begin() const -> candidate_const_itr_t
Definition navigation_state.hpp:149
constexpr bool is_good_candidate() const
Definition navigation_state.hpp:365
base_state()=delete
Default constructor (needs a detector).
constexpr auto volume() const -> nav_link_t
Definition navigation_state.hpp:300
constexpr void set_volume(dindex v)
Set start/new volume from volume index.
Definition navigation_state.hpp:304
constexpr void set_external_tol(const scalar_t tol)
Set externally provided mask tolerance according to noise prediction.
Definition navigation_state.hpp:224
constexpr auto encountered_sf_material() const -> bool
Helper method to check the track has encountered material.
Definition navigation_state.hpp:286
constexpr auto begin() -> candidate_itr_t
Definition navigation_state.hpp:488
friend std::ostream & operator<<(std::ostream &os, const derived_t &s)
Definition navigation_state.hpp:661
constexpr auto current_surface() const
Definition navigation_state.hpp:390
constexpr auto end() -> candidate_itr_t
Definition navigation_state.hpp:498
constexpr base_state(const detector_t &det, view_t view)
Construct from detector.
Definition navigation_state.hpp:138
constexpr auto detector() const -> const detector_t &
Definition navigation_state.hpp:143
constexpr scalar_t operator()() const
Scalar representation of the navigation state,.
Definition navigation_state.hpp:381
constexpr dist_t last_index() const
Definition navigation_state.hpp:537
constexpr bool finished() const
Definition navigation_state.hpp:293
constexpr auto status() const -> navigation::status
Definition navigation_state.hpp:190
constexpr dist_t next_index() const
Definition navigation_state.hpp:533
constexpr auto trust_level() const -> navigation::trust_level
Definition navigation_state.hpp:231
constexpr auto & inspector()
Definition navigation_state.hpp:483
std::int_least8_t dist_t
Definition navigation_state.hpp:119
constexpr void set_high_trust()
Definition navigation_state.hpp:245
constexpr void exit()
Navigation reaches final target or leaves detector world.
Definition navigation_state.hpp:419
constexpr void run_inspector(const navigation::config &cfg, const point3_t &track_pos, const vector3_t &track_dir, const char *message)
Call the navigation inspector.
Definition navigation_state.hpp:614
constexpr void set_next(candidate_const_itr_t new_next)
Set the next surface that we want to reach (update target).
Definition navigation_state.hpp:514
constexpr void set_last(candidate_const_itr_t new_last)
Updates the position of the last valid candidate.
Definition navigation_state.hpp:524
detector_t detector_type
Definition navigation_state.hpp:127
constexpr void trust_level(navigation::trust_level t)
Reset the trustlevel to.
Definition navigation_state.hpp:593
typename candidate_cache_t::const_iterator candidate_const_itr_t
Definition navigation_state.hpp:117
constexpr void abort(const debug_msg_generator_t &debug_msg_generator)
Navigation state that cannot be recovered from.
Definition navigation_state.hpp:466
constexpr auto geometry_identifier() const -> geometry::identifier
Definition navigation_state.hpp:352
constexpr auto is_on_passive() const -> bool
Helper method to check the track has reached a passive surface.
Definition navigation_state.hpp:271
constexpr auto next_volume() const
Definition navigation_state.hpp:412
constexpr auto candidates() -> candidate_cache_t &
Definition navigation_state.hpp:567
Defines a detray 'view'.
Definition ranges.hpp:190
constexpr auto size() const
Definition ranges.hpp:241
Facade for a detray detector surface with extra tracking capabilities.
Definition tracking_surface.hpp:25
Facade for a detray detector volume.
Definition tracking_volume.hpp:44
Check if a type can be used as a detector.
Definition concepts.hpp:47
constexpr double u
atomic mass unit u
Definition Units.hpp:155
get_scalar_t< A, T > dscalar
Scalar type for algebra A and value type T.
Definition algebra.hpp:64
unsigned int dindex
Default index type.
Definition indexing.hpp:18
std::array< value_t, kDIM > darray
Fixed-size array container.
Definition containers.hpp:30
get_vector3D_t< A, T > dvector3D
Three-dimensional vector type for algebra A and value type T.
Definition algebra.hpp:80
get_point3D_t< A, T > dpoint3D
Three-dimensional point type for algebra A and value type T.
Definition algebra.hpp:72
Navigation configuration.
Definition navigation_config.hpp:24
Empty view accepted by the no-op inspector.
Definition navigation_state.hpp:40
void_view view_type
View type used by this inspector.
Definition navigation_state.hpp:43
constexpr void_inspector(const void_view &)
Construct from an unused inspector view.
Definition navigation_state.hpp:51
const void_view const_view_type
Const view type used by this inspector.
Definition navigation_state.hpp:45
constexpr void operator()(const state_t &, const char *) const
Ignore navigation state notifications.
Definition navigation_state.hpp:56
static constexpr scalar_t mm
Native length unit, the millimetre.
Definition units.hpp:27