Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-11-18 10:33:20

0001 //------------------------------- -*- C++ -*- -------------------------------//
0002 // Copyright Celeritas contributors: see top-level COPYRIGHT file for details
0003 // SPDX-License-Identifier: (Apache-2.0 OR MIT)
0004 //---------------------------------------------------------------------------//
0005 //! \file geocel/g4/GeantGeoTrackView.hh
0006 //! \sa geocel/g4/GeantGeo.test.cc
0007 //---------------------------------------------------------------------------//
0008 #pragma once
0009 
0010 #include <algorithm>
0011 #include <G4LogicalVolume.hh>
0012 #include <G4Navigator.hh>
0013 #include <G4TouchableHandle.hh>
0014 #include <G4TouchableHistory.hh>
0015 #include <G4VPhysicalVolume.hh>
0016 
0017 #include "corecel/Macros.hh"
0018 #include "corecel/cont/Array.hh"
0019 #include "corecel/cont/Span.hh"
0020 #include "corecel/io/Logger.hh"
0021 #include "corecel/math/Algorithms.hh"
0022 #include "corecel/math/ArrayUtils.hh"
0023 #include "geocel/Types.hh"
0024 #include "geocel/detail/GeantVolumeInstanceMapper.hh"
0025 
0026 #include "Convert.hh"
0027 #include "GeantGeoData.hh"
0028 
0029 namespace celeritas
0030 {
0031 //---------------------------------------------------------------------------//
0032 /*!
0033  * Navigate through a Geant4 geometry on a single thread.
0034  *
0035  * This wraps a Geant4 geometry navigator and volume hierarchy state with the
0036  * same Celeritas tracker interface. It's not going to be the most efficient
0037  * code since the \c G4Navigator includes a lot of helper functions for
0038  * managing safety distance, tracking through a field, etc. We also
0039  * independently store a "celeritas" native position and direction, as well as
0040  * duplicating the "geant4" position and direction that are also stored under
0041  * the hood in the heavyweight navigator.
0042  *
0043  * \internal
0044  *
0045  * Geant4 thinks about boundaries differently to Celeritas. When we find the
0046  * next step, it will internally store the "blocking" volume
0047  * (daughter="entering", current volume="exiting") and the normal at that
0048  * point. The G4 navigation in practice has three options:
0049  *
0050  * 1. move within the safety distance, which is calculated as a side effect of
0051  *  \c ComputeStep
0052  * 2. move to the end-of-step position, call \c SetGeometricallyLimitedStep,
0053  *   and update the volume via \c LocateGlobalPointAndUpdateTouchableHandle
0054  * 3. move to a position outside the safety and update with \c
0055  *   LocateGlobalPointWithinVolume .
0056  *
0057  * \sa GeoTrackInterface
0058  */
0059 class GeantGeoTrackView
0060 {
0061   public:
0062     //!@{
0063     //! \name Type aliases
0064     using Initializer_t = GeoTrackInitializer;
0065     using ParamsRef = NativeCRef<GeantGeoParamsData>;
0066     using StateRef = NativeRef<GeantGeoStateData>;
0067     using real_type = double;
0068     using Real3 = Array<real_type, 3>;
0069     //!@}
0070 
0071   public:
0072     // Construct from params and state data
0073     inline GeantGeoTrackView(
0074         ParamsRef const& params, StateRef const& state, TrackSlotId tid);
0075 
0076     // Initialize the state
0077     inline GeantGeoTrackView& operator=(Initializer_t const& init);
0078 
0079     //// ACCESSORS ////
0080 
0081     //!@{
0082     //! State accessors
0083     CELER_FORCEINLINE Real3 const& pos() const { return pos_; }
0084     CELER_FORCEINLINE Real3 const& dir() const { return dir_; }
0085     //!@}
0086 
0087     // Get the canonical volume ID in the current impl volume
0088     inline VolumeId volume_id() const;
0089     // Get the physical volume ID in the current cell
0090     inline VolumeInstanceId volume_instance_id() const;
0091     // Get the depth in the geometry hierarchy
0092     inline VolumeLevelId volume_level() const;
0093     // Get the volume instance ID for all levels
0094     inline void volume_instance_id(Span<VolumeInstanceId> levels) const;
0095     // Visit every volume instance in the track's path, including world
0096     template<class F>
0097     inline void foreach_volume_path(F&& visit) const;
0098 
0099     // Get the implementation volume ID
0100     inline ImplVolumeId impl_volume_id() const;
0101 
0102     // Geometry tracking status
0103     inline GeoStatus geo_status() const;
0104     // Whether the track is outside the valid geometry region
0105     inline bool is_outside() const;
0106     // Whether the track is exactly on a surface
0107     inline bool is_on_boundary() const;
0108     // Whether the last operation resulted in an error
0109     inline bool failed() const;
0110     // Get the normal vector of the current surface
0111     inline Real3 const& normal() const;
0112 
0113     //// OPERATIONS ////
0114 
0115     // Find the distance to the next boundary, up to and including a step
0116     inline Propagation find_next_step(real_type max_step);
0117 
0118     // Find the safety at the current position
0119     inline real_type find_safety();
0120 
0121     // Find the safety at the current position up to a maximum step distance
0122     inline real_type find_safety(real_type max_step);
0123 
0124     // Move to the boundary in preparation for crossing it
0125     inline void move_to_boundary();
0126 
0127     // Move within the volume
0128     inline void move_internal(real_type step);
0129 
0130     // Move within the volume to a specific point
0131     inline void move_internal(Real3 const& pos);
0132 
0133     // Cross from one side of the current surface to the other
0134     inline void cross_boundary();
0135 
0136     // Change direction
0137     inline void set_dir(Real3 const& newdir);
0138 
0139     //// IMPLEMENTATION ////
0140 
0141     // Get the Geant4 navigation state
0142     inline G4NavigationHistory const* nav_history() const;
0143 
0144   private:
0145     //// TYPES ////
0146 
0147     //! Helper struct for initializing from an existing geometry state
0148     struct DetailedInitializer
0149     {
0150         TrackSlotId parent;  //!< Parent track with existing geometry
0151         ::celeritas::Real3 const& dir;  //!< New direction
0152     };
0153     using ClhepLength = lengthunits::ClhepLength;
0154 
0155     //// DATA ////
0156 
0157     // Shared data
0158     ParamsRef const& params_;
0159     // Geometry state data
0160     //! \todo This is only needed for the detailed initialization
0161     StateRef const& state_;
0162     TrackSlotId tid_;
0163 
0164     //!@{
0165     //! Referenced thread-local data
0166     Real3& pos_;
0167     Real3& dir_;
0168     Real3& normal_;
0169     real_type& next_step_;
0170     real_type& safety_radius_;
0171     G4TouchableHandle& touch_handle_;
0172     G4Navigator& navi_;
0173     //!@}
0174 
0175     // Temporary data
0176     G4ThreeVector g4pos_;
0177     G4ThreeVector g4dir_;  // [mm]
0178     real_type g4safety_;  // [mm]
0179 
0180     //// HELPER FUNCTIONS ////
0181 
0182     // Initialize the state from a parent state and new direction
0183     inline GeantGeoTrackView& operator=(DetailedInitializer const& init);
0184 
0185     // Whether any next distance-to-boundary has been found
0186     inline bool has_next_step() const;
0187 
0188     // Whether the track direction is exiting the current volume
0189     inline bool is_dir_exiting() const;
0190 
0191     // Set the geometry tracking status
0192     inline void geo_status(GeoStatus);
0193 
0194     //! Get a pointer to the current volume; null if outside
0195     inline G4LogicalVolume const* volume() const;
0196 };
0197 
0198 //---------------------------------------------------------------------------//
0199 // INLINE DEFINITIONS
0200 //---------------------------------------------------------------------------//
0201 /*!
0202  * Construct from params and state data.
0203  */
0204 GeantGeoTrackView::GeantGeoTrackView(
0205     ParamsRef const& params, StateRef const& states, TrackSlotId tid)
0206     : params_{params}
0207     , state_(states)
0208     , tid_(tid)
0209     , pos_(states.pos[tid])
0210     , dir_(states.dir[tid])
0211     , normal_(states.normal[tid])
0212     , next_step_(states.next_step[tid])
0213     , safety_radius_(states.safety_radius[tid])
0214     , touch_handle_(states.nav_state.touch_handle(tid))
0215     , navi_(states.nav_state.navigator(tid))
0216     , g4pos_(native_to_geant<ClhepLength>(pos_))
0217     , g4dir_(to_g4vector(dir_))
0218     , g4safety_(native_to_geant<ClhepLength>(safety_radius_))
0219 {
0220 }
0221 
0222 //---------------------------------------------------------------------------//
0223 /*!
0224  * Construct the state.
0225  */
0226 GeantGeoTrackView& GeantGeoTrackView::operator=(Initializer_t const& init)
0227 {
0228     CELER_EXPECT(is_soft_unit_vector(init.dir));
0229 
0230     if (init.parent)
0231     {
0232         // Initialize from direction and copy of parent state
0233         *this = DetailedInitializer{init.parent, init.dir};
0234         return *this;
0235     }
0236 
0237     // Initialize position/direction
0238     std::copy(init.pos.begin(), init.pos.end(), pos_.begin());
0239     std::copy(init.dir.begin(), init.dir.end(), dir_.begin());
0240     next_step_ = -1;
0241     safety_radius_ = -1;  // Assume *not* on a boundary
0242 
0243     g4pos_ = native_to_geant<ClhepLength>(pos_);
0244     g4dir_ = to_g4vector(dir_);
0245     g4safety_ = -1;
0246 
0247     navi_.LocateGlobalPointAndUpdateTouchable(g4pos_,
0248                                               g4dir_,
0249                                               touch_handle_(),
0250                                               /* relative_search = */ false);
0251     this->geo_status(this->is_outside() ? GeoStatus::invalid
0252                                         : GeoStatus::interior);
0253 
0254     CELER_ENSURE(!this->has_next_step());
0255     return *this;
0256 }
0257 
0258 //---------------------------------------------------------------------------//
0259 /*!
0260  * Construct the state from a direction and a copy of the parent state.
0261  *
0262  * \c G4Track::SetTouchableHandle from \c G4VEmProcess::PostStepDoIt
0263  *
0264  * maybe see \c G4SteppingManager::Stepping
0265  */
0266 GeantGeoTrackView& GeantGeoTrackView::operator=(DetailedInitializer const& init)
0267 {
0268     CELER_EXPECT(is_soft_unit_vector(init.dir));
0269 
0270     if (tid_ != init.parent)
0271     {
0272         // Copy values from the parent state
0273         GeantGeoTrackView other(ParamsRef{}, state_, init.parent);
0274         pos_ = other.pos_;
0275         safety_radius_ = other.safety_radius_;
0276         g4pos_ = other.g4pos_;
0277         g4dir_ = other.g4dir_;
0278         g4safety_ = other.g4safety_;
0279 
0280         // Update the touchable and navigator
0281         touch_handle_ = other.touch_handle_;
0282         navi_.ResetHierarchyAndLocate(
0283             g4pos_,
0284             g4dir_,
0285             dynamic_cast<G4TouchableHistory&>(*touch_handle_()));
0286         this->geo_status(state_.status[init.parent]);
0287     }
0288 
0289     // Set up the next state and initialize the direction
0290     std::copy(init.dir.begin(), init.dir.end(), dir_.begin());
0291     next_step_ = -1;
0292 
0293     CELER_ENSURE(!this->has_next_step());
0294     return *this;
0295 }
0296 
0297 //---------------------------------------------------------------------------//
0298 /*!
0299  * Get the canonical volume ID in the current implementation volume.
0300  *
0301  * \note See GeantGeoParams::volume_id : for now, identical to ImplVolumeId
0302  */
0303 VolumeId GeantGeoTrackView::volume_id() const
0304 {
0305     CELER_EXPECT(!this->is_outside());
0306     return id_cast<VolumeId>(this->impl_volume_id().get());
0307 }
0308 
0309 //---------------------------------------------------------------------------//
0310 /*!
0311  * Get the physical volume ID in the current cell.
0312  */
0313 VolumeInstanceId GeantGeoTrackView::volume_instance_id() const
0314 {
0315     CELER_EXPECT(!this->is_outside());
0316     G4VPhysicalVolume* pv = touch_handle_()->GetVolume(0);
0317     CELER_ASSERT(pv);
0318     return params_.vi_mapper->geant_to_id(*pv);
0319 }
0320 
0321 //---------------------------------------------------------------------------//
0322 /*!
0323  * Get the depth in the geometry hierarchy.
0324  */
0325 VolumeLevelId GeantGeoTrackView::volume_level() const
0326 {
0327     auto* touch = touch_handle_();
0328     return id_cast<VolumeLevelId>(touch->GetHistoryDepth());
0329 }
0330 
0331 //---------------------------------------------------------------------------//
0332 /*!
0333  * Get the volume instance ID at every level.
0334  *
0335  * The input span size must be equal to the value of "volume_level" plus one.
0336  * The top-most level ("world" or level zero) starts at index zero and moves
0337  * downward. Note that Geant4 uses the \em reverse nomenclature.
0338  */
0339 void GeantGeoTrackView::volume_instance_id(Span<VolumeInstanceId> levels) const
0340 {
0341     this->foreach_volume_path(
0342         [levels](VolumeLevelId lev, VolumeInstanceId vol_inst) {
0343             CELER_EXPECT(lev < levels.size());
0344             CELER_EXPECT(vol_inst);
0345             levels[*lev] = vol_inst;
0346         });
0347 }
0348 
0349 //---------------------------------------------------------------------------//
0350 /*!
0351  * Apply the function with the volume instance ID and level.
0352  *
0353  * This can be used to construct a unique volume instance ID or fill a vector
0354  * with volume levels. It is performed in local-to-global order.
0355  */
0356 template<class F>
0357 void GeantGeoTrackView::foreach_volume_path(F&& visit) const
0358 {
0359     auto* touch = touch_handle_();
0360     auto const num_vol_levels
0361         = id_cast<VolumeLevelId>(touch->GetHistoryDepth());
0362     for (auto lev : range(num_vol_levels + 1))
0363     {
0364         VolumeInstanceId vi_id;
0365         G4VPhysicalVolume* pv = touch->GetVolume(num_vol_levels - lev);
0366         CELER_ASSERT(pv);
0367         vi_id = params_.vi_mapper->geant_to_id(*pv);
0368         visit(lev, vi_id);
0369     }
0370 }
0371 
0372 //---------------------------------------------------------------------------//
0373 /*!
0374  * Get the volume ID in the current cell.
0375  */
0376 ImplVolumeId GeantGeoTrackView::impl_volume_id() const
0377 {
0378     CELER_EXPECT(!this->is_outside());
0379     return id_cast<ImplVolumeId>(
0380         this->volume()->GetInstanceID() - params_.lv_offset);
0381 }
0382 
0383 //---------------------------------------------------------------------------//
0384 /*!
0385  * Whether the track is outside the valid geometry region.
0386  */
0387 CELER_FORCEINLINE bool GeantGeoTrackView::is_outside() const
0388 {
0389     return this->volume() == nullptr;
0390 }
0391 
0392 //---------------------------------------------------------------------------//
0393 /*!
0394  * Whether the track is on the boundary of a volume.
0395  */
0396 CELER_FORCEINLINE bool GeantGeoTrackView::is_on_boundary() const
0397 {
0398     return safety_radius_ == 0.0;
0399 }
0400 
0401 //---------------------------------------------------------------------------//
0402 /*!
0403  * Whether the last operation resulted in an error.
0404  */
0405 CELER_FORCEINLINE_FUNCTION bool GeantGeoTrackView::failed() const
0406 {
0407     return this->geo_status() == GeoStatus::error;
0408 }
0409 
0410 //---------------------------------------------------------------------------//
0411 /*!
0412  * Geometry tracking status.
0413  */
0414 GeoStatus GeantGeoTrackView::geo_status() const
0415 {
0416     return state_.status[tid_];
0417 }
0418 
0419 //---------------------------------------------------------------------------//
0420 /*!
0421  * Get the exit surface normal of the boundary the track has just crossed.
0422  *
0423  * This vector is in the global coordinate system.
0424  */
0425 auto GeantGeoTrackView::normal() const -> Real3 const&
0426 {
0427     CELER_EXPECT(this->is_on_boundary());
0428     return normal_;
0429 }
0430 
0431 //---------------------------------------------------------------------------//
0432 /*!
0433  * Find the distance to the next geometric boundary.
0434  *
0435  * Geant4 can return a zero-distance boundary hit within its surface tolerance.
0436  * Cache it like any other hit, and finalize the boundary in move_to_boundary.
0437  */
0438 Propagation GeantGeoTrackView::find_next_step(real_type max_step)
0439 {
0440     CELER_EXPECT(!this->is_outside());
0441     CELER_EXPECT(max_step > 0);
0442 
0443     if (this->geo_status() == GeoStatus::boundary_inc)
0444     {
0445         // On a boundary, headed in: next step is zero
0446         // TODO: add a test for this
0447         next_step_ = 0;
0448         return {0, true};
0449     }
0450 
0451     if (this->geo_status() == GeoStatus::boundary_out)
0452     {
0453         if (!this->is_dir_exiting())
0454         {
0455             // We moved to a boundary but ended up not crossing it. Tell the
0456             // navigator to "relocate" to avoid a warning: the current
0457             // position is likely outside the last calculated safety, but we
0458             // know it's correct because we did it via a straight-line distance
0459             // that's valid.
0460             navi_.LocateGlobalPointWithinVolume(g4pos_);
0461         }
0462     }
0463 
0464     // Compute the step
0465     real_type g4step = native_to_geant<ClhepLength>(max_step);
0466     g4step = navi_.ComputeStep(g4pos_, g4dir_, g4step, g4safety_);
0467 
0468     if (g4safety_ != 0 && !this->is_on_boundary())
0469     {
0470         // Save the resulting safety distance if computed: allow to be
0471         // "negative" to prevent accidentally changing the boundary state
0472         safety_radius_ = native_value_from(ClhepLength{g4safety_});
0473         CELER_ASSERT(!this->is_on_boundary());
0474     }
0475 
0476     // Update result
0477     Propagation result;
0478     result.distance = native_value_from(ClhepLength{g4step});
0479     if (result.distance <= max_step)
0480     {
0481         result.boundary = true;
0482     }
0483     else
0484     {
0485         // No intersection in range -> G4Navigator returns kInfinity
0486         result.distance = max_step;
0487     }
0488 
0489     // Save the next step
0490     next_step_ = result.distance;
0491 
0492     CELER_ENSURE(result.distance >= 0);
0493     CELER_ENSURE(result.distance > 0 || result.boundary);
0494     CELER_ENSURE(result.distance <= max_step);
0495     CELER_ENSURE(result.boundary || result.distance == max_step);
0496     CELER_ENSURE(this->has_next_step());
0497     return result;
0498 }
0499 
0500 //---------------------------------------------------------------------------//
0501 /*!
0502  * Find the safety at the current position.
0503  */
0504 CELER_FORCEINLINE auto GeantGeoTrackView::find_safety() -> real_type
0505 {
0506     return this->find_safety(numeric_limits<real_type>::infinity());
0507 }
0508 
0509 //---------------------------------------------------------------------------//
0510 /*!
0511  * Find the safety at the current position.
0512  *
0513  * \warning This can change the boundary state if the track was moved to or
0514  * initialized a point on the boundary.
0515  */
0516 auto GeantGeoTrackView::find_safety(real_type max_step) -> real_type
0517 {
0518     CELER_EXPECT(!this->is_on_boundary());
0519     CELER_EXPECT(max_step > 0);
0520     if (safety_radius_ < max_step)
0521     {
0522         real_type g4step = native_to_geant<ClhepLength>(max_step);
0523         g4safety_ = navi_.ComputeSafety(g4pos_, g4step);
0524         safety_radius_ = max(native_value_from(ClhepLength{g4safety_}), 0.0);
0525     }
0526 
0527     return safety_radius_;
0528 }
0529 
0530 //---------------------------------------------------------------------------//
0531 /*!
0532  * Move to the next boundary but don't cross yet.
0533  */
0534 void GeantGeoTrackView::move_to_boundary()
0535 {
0536     CELER_EXPECT(this->has_next_step());
0537 
0538     if (this->geo_status() == GeoStatus::boundary_inc)
0539     {
0540         // Already on the surface, e.g. after reversing the direction.
0541         CELER_ASSERT(this->is_on_boundary());
0542         CELER_ASSERT(next_step_ == 0);
0543         next_step_ = -1;
0544         return;
0545     }
0546 
0547     // Move next step
0548     axpy(next_step_, dir_, &pos_);
0549     axpy(native_to_geant<ClhepLength>(next_step_), g4dir_, &g4pos_);
0550     next_step_ = -1;
0551     safety_radius_ = 0;
0552     g4safety_ = 0;
0553 
0554     // Cache the exit normal (valid immediately after ComputeStep hits a
0555     // boundary)
0556     bool normal_valid{false};
0557     G4ThreeVector g4norm = navi_.GetGlobalExitNormal(g4pos_, &normal_valid);
0558     CELER_ASSERT(normal_valid);
0559     normal_ = to_array(g4norm);
0560     CELER_ASSERT(this->is_dir_exiting());
0561     this->geo_status(GeoStatus::boundary_inc);
0562 
0563     CELER_ENSURE(this->is_on_boundary());
0564 }
0565 
0566 //---------------------------------------------------------------------------//
0567 /*!
0568  * Cross from one side of the current surface to the other.
0569  *
0570  * The position \em must be on the boundary following a move-to-boundary.
0571  */
0572 void GeantGeoTrackView::cross_boundary()
0573 {
0574     CELER_EXPECT(this->is_on_boundary());
0575 
0576     if (this->geo_status() == GeoStatus::boundary_out)
0577     {
0578         // Direction changed while on boundary leading to no change in
0579         // volume/surface. This is logically equivalent to a reflection.
0580         return;
0581     }
0582     if (!this->is_dir_exiting())
0583     {
0584         // Reentering after leaving volume but not moving: we have to search
0585         // for the boundary again
0586         navi_.ComputeStep(g4pos_, g4dir_, 1e-9, g4safety_);
0587     }
0588 
0589     navi_.SetGeometricallyLimitedStep();
0590     navi_.LocateGlobalPointAndUpdateTouchableHandle(
0591         g4pos_,
0592         g4dir_,
0593         touch_handle_,
0594         /* relative_search = */ true);
0595 
0596     this->geo_status(GeoStatus::boundary_out);
0597 
0598     CELER_ENSURE(this->is_on_boundary());
0599 }
0600 
0601 //---------------------------------------------------------------------------//
0602 /*!
0603  * Move within the current volume.
0604  *
0605  * The straight-line distance *must* be less than the distance to the
0606  * boundary.
0607  */
0608 void GeantGeoTrackView::move_internal(real_type dist)
0609 {
0610     CELER_EXPECT(this->has_next_step());
0611     CELER_EXPECT(dist > 0 && dist <= next_step_);
0612 
0613     // Move and update next_step
0614     axpy(dist, dir_, &pos_);
0615     axpy(native_to_geant<ClhepLength>(dist), g4dir_, &g4pos_);
0616     next_step_ -= dist;
0617     if (next_step_ == 0)
0618     {
0619         // A non-boundary limited step has been consumed: require a new query.
0620         next_step_ = -1;
0621     }
0622     navi_.LocateGlobalPointWithinVolume(g4pos_);
0623 
0624     safety_radius_ = -1;
0625     g4safety_ = 0;
0626     this->geo_status(GeoStatus::interior);
0627 }
0628 
0629 //---------------------------------------------------------------------------//
0630 /*!
0631  * Move within the current volume to a nearby point.
0632  *
0633  * See \c G4PathFinder::ReLocate from \c G4SafetyHelper::ReLocateWithinVolume
0634  * from \c G4VMultipleScattering::AlongStepDoIt .
0635  */
0636 void GeantGeoTrackView::move_internal(Real3 const& pos)
0637 {
0638     pos_ = pos;
0639     g4pos_ = native_to_geant<ClhepLength>(pos_);
0640     next_step_ = -1;
0641     navi_.LocateGlobalPointWithinVolume(g4pos_);
0642 
0643     safety_radius_ = -1;
0644     g4safety_ = 0;
0645     this->geo_status(GeoStatus::interior);
0646 }
0647 
0648 //---------------------------------------------------------------------------//
0649 /*!
0650  * Change the track's direction.
0651  *
0652  * This happens after a scattering event or movement inside a magnetic field.
0653  * It resets the calculated distance-to-boundary.
0654  */
0655 void GeantGeoTrackView::set_dir(Real3 const& newdir)
0656 {
0657     CELER_EXPECT(is_soft_unit_vector(newdir));
0658 
0659     GeoStatus status{this->geo_status()};
0660     if (::celeritas::is_on_boundary(status))
0661     {
0662         // Changing direction on a boundary may reverse whether the track
0663         // will cross the surface; update stored status to match.
0664         Real3 const& norm = this->normal();
0665 
0666         // Evaluate whether the direction dotted with the surface normal
0667         // changes (i.e. heading from inside to outside or vice versa).
0668         auto new_dot = dot_product(norm, newdir);
0669         if (CELER_UNLIKELY(new_dot == 0))
0670         {
0671             CELER_LOG_LOCAL(error)
0672                 << "track direction cannot change to " << newdir
0673                 << " which is perpendicular to the current surface normal";
0674             this->geo_status(GeoStatus::error);
0675             return;
0676         }
0677         else if ((new_dot > 0) != (dot_product(norm, this->dir()) > 0))
0678         {
0679             // The boundary crossing direction has changed! Reverse our
0680             // plans to change the logical state and move to a new volume.
0681             this->geo_status(flip_boundary(this->geo_status()));
0682         }
0683     }
0684 
0685     dir_ = newdir;
0686     g4dir_ = to_g4vector(newdir);
0687     next_step_ = -1;
0688 }
0689 
0690 //---------------------------------------------------------------------------//
0691 /*!
0692  * Get the navigation state.
0693  */
0694 G4NavigationHistory const* GeantGeoTrackView::nav_history() const
0695 {
0696     auto* touch = touch_handle_();
0697     CELER_ASSERT(touch);
0698     return touch->GetHistory();
0699 }
0700 
0701 //---------------------------------------------------------------------------//
0702 // PRIVATE MEMBER FUNCTIONS
0703 //---------------------------------------------------------------------------//
0704 /*!
0705  * Whether a next step has been calculated, including zero-distance hits.
0706  */
0707 CELER_FORCEINLINE bool GeantGeoTrackView::has_next_step() const
0708 {
0709     return next_step_ >= 0;
0710 }
0711 
0712 //---------------------------------------------------------------------------//
0713 /*!
0714  * Whether the track direction is exiting the current volume.
0715  *
0716  * If the normal direction is \em perpendicular to the direction of travel,
0717  * our status should probably be marked as an error (and bump away?)
0718  */
0719 bool GeantGeoTrackView::is_dir_exiting() const
0720 {
0721     CELER_EXPECT(this->is_on_boundary());
0722     return dot_product(normal_, dir_) > 0;
0723 }
0724 
0725 //---------------------------------------------------------------------------//
0726 //! Set the geometry tracking status
0727 void GeantGeoTrackView::geo_status(GeoStatus gs)
0728 {
0729     state_.status[tid_] = gs;
0730 }
0731 
0732 //---------------------------------------------------------------------------//
0733 /*!
0734  * Get a reference to the current volume, or to world volume if outside.
0735  */
0736 auto GeantGeoTrackView::volume() const -> G4LogicalVolume const*
0737 {
0738     CELER_EXPECT(touch_handle_());
0739     G4VPhysicalVolume* pv = touch_handle_()->GetVolume(0);
0740     if (!pv)
0741         return nullptr;
0742 
0743     return pv->GetLogicalVolume();
0744 }
0745 
0746 //---------------------------------------------------------------------------//
0747 }  // namespace celeritas