12#include "detray/core/concepts.hpp"
13#include "detray/definitions/algorithms.hpp"
14#include "detray/definitions/detail/qualifiers.hpp"
15#include "detray/definitions/indexing.hpp"
16#include "detray/definitions/navigation.hpp"
17#include "detray/definitions/units.hpp"
18#include "detray/geometry/surface.hpp"
19#include "detray/navigation/detail/navigation_functions.hpp"
20#include "detray/navigation/intersection/intersection.hpp"
21#include "detray/navigation/intersection/ray_intersector.hpp"
22#include "detray/navigation/navigation_config.hpp"
23#include "detray/navigation/navigation_state.hpp"
24#include "detray/navigation/navigator_base.hpp"
25#include "detray/tracks/ray.hpp"
26#include "detray/utils/logging.hpp"
35static constexpr std::size_t default_cache_size{8u};
52 std::size_t k_cache_capacity = navigation::default_cache_size,
55 typename detector_t::surface_type,
56 typename detector_t::algebra_type, !intersection::contains_pos>>
58 :
public navigator_base<caching_navigator<detector_t, k_cache_capacity,
59 inspector_t, intersection_t>> {
83 inspector_type, intersection_type> {
85 friend class caching_navigator;
92 template <
typename state_t>
93 friend constexpr void navigation::update_status(state_t &,
96 template <
typename track_t,
typename state_t,
typename ctx_t>
97 friend constexpr void navigation::local_navigation(
101 template <
typename track_t,
typename state_t,
typename ctx_t>
102 friend constexpr void navigation::volume_switch(
const track_t &, state_t &,
127 using base_type::base_type;
132 DETRAY_VERBOSE_HOST_DEVICE(
"Flagging re-navigation: \"high trust\"");
135 : navigation::trust_level::e_high);
141 DETRAY_VERBOSE_HOST_DEVICE(
"Flagging re-navigation: \"fair trust\"");
144 : navigation::trust_level::e_fair);
157 assert(detail::is_invalid_value(new_candidate.volume_link()) ||
158 new_candidate.volume_link() < this->detector().volumes().size());
165 assert(
static_cast<std::size_t
>(this->
last_index()) < k_cache_capacity);
170 auto idx{
static_cast<dist_t>(
175 const auto is_overlap_at_pos = [
this, &new_candidate](std::size_t index) {
176 return (math::fabs(this->
candidates()[index].path() -
186 const auto is_clash_at_pos = [
this, &new_candidate,
187 &is_overlap_at_pos](std::size_t index) {
188 return (this->
candidates()[index].surface().identifier() ==
189 new_candidate.surface().identifier()) &&
190 is_overlap_at_pos(index);
193 const auto idxu{
static_cast<std::size_t
>(idx)};
194 if (is_clash_at_pos(idxu) || ((idxu > 0u) && is_clash_at_pos(idxu - 1u)))
201 constexpr auto shift_max{
static_cast<dist_t
>(k_cache_capacity - 2)};
202 const dist_t shift_begin{math::min(this->
last_index(), shift_max)};
206 if (is_overlap_at_pos(idxu) &&
207 (new_candidate.is_edge() || new_candidate.surface().is_portal())) {
211 idx = idx > shift_begin ? shift_begin : idx;
214 for (dist_t i = shift_begin; i >= idx; --i) {
215 const auto j{
static_cast<std::size_t
>(i)};
220 this->
candidates()[
static_cast<std::size_t
>(idx)] = new_candidate;
222 static_cast<dist_t
>(k_cache_capacity - 1)));
225 assert(
static_cast<std::size_t
>(this->
last_index()) < k_cache_capacity);
247 template <
typename track_t>
248 DETRAY_HOST_DEVICE
constexpr bool update_impl(
const track_t &track,
250 const navigation::config &cfg,
252 const auto &det = navigation.detector();
253 constexpr bool is_init{
true};
255 using algebra_t =
typename detector_t::algebra_type;
257 assert(navigation.trust_level() != navigation::trust_level::e_full);
260 const detray::detail::ray<algebra_t> tangential{
262 static_cast<scalar_t
>(navigation.direction()) * track.dir()};
266 if (navigation.trust_level() == navigation::trust_level::e_high) {
267 DETRAY_VERBOSE_HOST_DEVICE(
"Called 'update()' - high trust");
270 if (!navigation::update_candidate(navigation.target(), tangential, det,
272 navigation.external_tol(), ctx)) {
273 DETRAY_VERBOSE_HOST_DEVICE(
274 "-> Candidate not reachable! High trust broken:");
276 navigation.status(navigation::status::e_unknown);
278 navigation.set_fair_trust();
281 navigation::update_status(navigation, cfg);
283 navigation.run_inspector(cfg, track.pos(), track.dir(),
284 "Update complete: high trust: ");
288 if (navigation.status() == navigation::status::e_towards_object ||
289 navigation.is_on_portal()) {
290 if (navigation.status() == navigation::status::e_towards_object) {
291 DETRAY_VERBOSE_HOST_DEVICE(
"-> Towards surface: %d",
292 navigation.next_surface().index());
294 DETRAY_VERBOSE_HOST_DEVICE(
"-> On portal: idx %d",
295 navigation.current_surface().index());
301 if (navigation.trust_level() == navigation::trust_level::e_full &&
302 navigation::update_candidate(navigation.target(), tangential, det,
304 navigation.external_tol(), ctx)) {
305 DETRAY_VERBOSE_HOST_DEVICE(
306 "-> On non-portal surface (idx %d) and next candidate "
307 "in cache is reachable",
308 navigation.current_surface().index());
311 DETRAY_VERBOSE_HOST_DEVICE(
312 "-> Next candidate no longer reachable: High trust broken");
318 navigation.set_fair_trust();
323 if (navigation.trust_level() == navigation::trust_level::e_fair &&
324 !navigation.cache_exhausted()) {
325 DETRAY_VERBOSE_HOST_DEVICE(
"Called 'update()' - fair trust");
327 for (
auto &candidate : navigation) {
329 if (!navigation::update_candidate(candidate, tangential, det,
331 navigation.external_tol(), ctx)) {
333 candidate.set_path(std::numeric_limits<scalar_t>::max());
338 navigation.set_next(navigation.begin());
340 navigation.set_last(find_invalid(navigation.candidates()));
342 navigation::update_status(navigation, cfg);
344 navigation.run_inspector(cfg, track.pos(), track.dir(),
345 "Update complete: fair trust: ");
349 if (navigation.cache_exhausted()) {
350 navigation.set_no_trust();
355 if (navigation.trust_level() == navigation::trust_level::e_no_trust) {
356 DETRAY_VERBOSE_HOST_DEVICE(
"Called 'update()' - no trust");
358 constexpr bool resolve_overstepping{
true};
359 navigation::local_navigation(track, navigation, cfg, ctx,
360 resolve_overstepping);
374 DETRAY_HOST_DEVICE
constexpr auto find_invalid(
375 const typename state::candidate_cache_t &candidates)
const {
378 return candidate.path() == std::numeric_limits<scalar_t>::max();
381 return detray::find_if(candidates.begin(), candidates.end(), not_reachable);
Navigation state that contains a cache of candidates.
Definition caching_navigator.hpp:83
constexpr void set_high_trust()
Update navigation trust level to high trust.
Definition caching_navigator.hpp:131
candidate_t value_type
Value type stored in the bins or result.
Definition caching_navigator.hpp:119
detail::get_view_t< const inspector_t > const_view_type
Const view type exposing inspector state.
Definition caching_navigator.hpp:124
constexpr void set_fair_trust()
Update navigation trust level to fair trust.
Definition caching_navigator.hpp:140
detail::get_view_t< inspector_t > view_type
View type exposing inspector state.
Definition caching_navigator.hpp:122
Navigation class which caches a 'road' through the detector.
Definition caching_navigator.hpp:59
typename detector_type::geometry_context context_type
Geometry context expected by the detector.
Definition caching_navigator.hpp:68
inspector_t inspector_type
Policy used to expose navigation diagnostics.
Definition caching_navigator.hpp:72
intersection_t intersection_type
Intersection result type associated with a surface descriptor.
Definition caching_navigator.hpp:70
detector_t detector_type
Detector whose surfaces are navigated.
Definition caching_navigator.hpp:66
This class holds the intersection information.
Definition intersection.hpp:212
A navigation state object used to cache the information of the current navigation stream.
Definition navigation_state.hpp:81
darray< candidate_t, k_cache_capacity > candidate_cache_t
Definition navigation_state.hpp:113
constexpr void clear_cache()
Definition navigation_state.hpp:600
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 n_candidates() const -> dindex
Definition navigation_state.hpp:323
intersection_type candidate_t
Definition navigation_state.hpp:111
constexpr auto cbegin() const -> candidate_const_itr_t
Definition navigation_state.hpp:159
typename candidate_cache_t::iterator candidate_itr_t
Definition navigation_state.hpp:115
constexpr dist_t last_index() const
Definition navigation_state.hpp:537
constexpr dist_t next_index() const
Definition navigation_state.hpp:533
constexpr auto trust_level() const -> navigation::trust_level
Definition navigation_state.hpp:231
std::int_least8_t dist_t
Definition navigation_state.hpp:119
detector_t detector_type
Definition navigation_state.hpp:127
typename candidate_cache_t::const_iterator candidate_const_itr_t
Definition navigation_state.hpp:117
Navigation base class - interface towards the propagator.
Definition navigator_base.hpp:45
Check if a type can be used as a detector.
Definition concepts.hpp:47
Detray include(s).
Definition array_operators.hpp:18
get_scalar_t< A, T > dscalar
Scalar type for algebra A and value type T.
Definition algebra.hpp:64
intersector_base< ray_intersector_impl< typename shape_t::template local_frame_type< algebra_t >, algebra_t, resolve_pos > > ray_intersector
Select the ray intersector specialization for a shape's local frame.
Definition ray_intersector.hpp:36
void sequential_sort(rand_iter_t first, sentinel_t last)
sequential (single thread) sort function
Definition algorithms.hpp:29
auto find_if(rand_iter_t first, sentinel_t last, Predicate &&comp)
find_if implementation for host/device (single thread)
Definition algorithms.hpp:48
Navigation configuration.
Definition navigation_config.hpp:24
A void inpector that does nothing.
Definition navigation_state.hpp:38
static constexpr scalar_t um
Length, native unit mm.
Definition units.hpp:25