geometry/navigation/include/G4Navigator.hh

Go to the documentation of this file.
00001 //
00002 // ********************************************************************
00003 // * License and Disclaimer                                           *
00004 // *                                                                  *
00005 // * The  Geant4 software  is  copyright of the Copyright Holders  of *
00006 // * the Geant4 Collaboration.  It is provided  under  the terms  and *
00007 // * conditions of the Geant4 Software License,  included in the file *
00008 // * LICENSE and available at  http://cern.ch/geant4/license .  These *
00009 // * include a list of copyright holders.                             *
00010 // *                                                                  *
00011 // * Neither the authors of this software system, nor their employing *
00012 // * institutes,nor the agencies providing financial support for this *
00013 // * work  make  any representation or  warranty, express or implied, *
00014 // * regarding  this  software system or assume any liability for its *
00015 // * use.  Please see the license in the file  LICENSE  and URL above *
00016 // * for the full disclaimer and the limitation of liability.         *
00017 // *                                                                  *
00018 // * This  code  implementation is the result of  the  scientific and *
00019 // * technical work of the GEANT4 collaboration.                      *
00020 // * By using,  copying,  modifying or  distributing the software (or *
00021 // * any work based  on the software)  you  agree  to acknowledge its *
00022 // * use  in  resulting  scientific  publications,  and indicate your *
00023 // * acceptance of all terms of the Geant4 Software license.          *
00024 // ********************************************************************
00025 //
00026 //
00027 // $Id: G4Navigator.hh,v 1.26 2007/10/18 14:18:36 gcosmo Exp $
00028 // GEANT4 tag $Name: geant4-09-01-patch-01 $
00029 //
00030 //
00031 // class G4Navigator
00032 //
00033 // Class description:
00034 //
00035 // A class for use by the tracking management, able to obtain/calculate
00036 // dynamic tracking time information such as the distance to the next volume,
00037 // or to find the physical volume containing a given point in the world
00038 // reference system. The navigator maintains a transformation history and
00039 // other information to optimise the tracking time performance.
00040 //
00041 // History:
00042 // - Created.                                  Paul Kent,     Jul 95/96
00043 // - Zero step protections                     J.A. / G.C.,   Nov  2004
00044 // - Added check mode                          G. Cosmo,      Mar  2004
00045 // - Made Navigator Abstract                   G. Cosmo,      Nov  2003
00046 // *********************************************************************
00047 
00048 #ifndef G4NAVIGATOR_HH
00049 #define G4NAVIGATOR_HH
00050 
00051 #include "geomdefs.hh"
00052 
00053 #include "G4ThreeVector.hh"
00054 #include "G4AffineTransform.hh"
00055 #include "G4RotationMatrix.hh"
00056 
00057 #include "G4LogicalVolume.hh"             // Used in inline methods
00058 #include "G4GRSVolume.hh"                 //    "         "
00059 #include "G4GRSSolid.hh"                  //    "         "
00060 #include "G4TouchableHandle.hh"           //    "         "
00061 #include "G4TouchableHistoryHandle.hh"
00062 
00063 #include "G4NavigationHistory.hh"
00064 #include "G4NormalNavigation.hh"
00065 #include "G4VoxelNavigation.hh"
00066 #include "G4ParameterisedNavigation.hh"
00067 #include "G4ReplicaNavigation.hh"
00068 #include "G4RegularNavigation.hh"
00069 
00070 #include <iostream>
00071 
00072 class G4VPhysicalVolume;
00073 
00074 class G4Navigator
00075 {
00076   public:  // with description
00077 
00078   friend std::ostream& operator << (std::ostream &os, const G4Navigator &n);
00079 
00080   G4Navigator();
00081     // Constructor - initialisers and setup.
00082 
00083   virtual ~G4Navigator();
00084     // Destructor. No actions.
00085 
00086   virtual G4double ComputeStep(const G4ThreeVector &pGlobalPoint,
00087                                const G4ThreeVector &pDirection,
00088                                const G4double pCurrentProposedStepLength,
00089                                      G4double  &pNewSafety);
00090     // Calculate the distance to the next boundary intersected
00091     // along the specified NORMALISED vector direction and
00092     // from the specified point in the global coordinate
00093     // system. LocateGlobalPointAndSetup or LocateGlobalPointWithinVolume 
00094     // must have been called with the same global point prior to this call.
00095     // The isotropic distance to the nearest boundary is also
00096     // calculated (usually an underestimate). The current
00097     // proposed Step length is used to avoid intersection
00098     // calculations: if it can be determined that the nearest
00099     // boundary is >pCurrentProposedStepLength away, kInfinity
00100     // is returned together with the computed isotropic safety
00101     // distance. Geometry must be closed.
00102 
00103   G4double CheckNextStep(const G4ThreeVector &pGlobalPoint,
00104                          const G4ThreeVector &pDirection,
00105                          const G4double pCurrentProposedStepLength,
00106                                G4double &pNewSafety); 
00107     // Same as above, but do not disturb the state of the Navigator.
00108 
00109   virtual
00110   G4VPhysicalVolume* ResetHierarchyAndLocate(const G4ThreeVector &point,
00111                                              const G4ThreeVector &direction,
00112                                              const G4TouchableHistory &h);
00113 
00114     // Resets the geometrical hierarchy and search for the volumes deepest
00115     // in the hierarchy containing the point in the global coordinate space.
00116     // The direction is used to check if a volume is entered.
00117     // The search begin is the geometrical hierarchy at the location of the
00118     // last located point, or the endpoint of the previous Step if
00119     // SetGeometricallyLimitedStep() has been called immediately before.
00120     // 
00121     // Important Note: In order to call this the geometry MUST be closed.
00122 
00123   virtual
00124   G4VPhysicalVolume* LocateGlobalPointAndSetup(const G4ThreeVector& point,
00125                                              const G4ThreeVector* direction=0,
00126                                              const G4bool pRelativeSearch=true,
00127                                              const G4bool ignoreDirection=true);
00128     // Search the geometrical hierarchy for the volumes deepest in the hierarchy
00129     // containing the point in the global coordinate space. Two main cases are:
00130     //  i) If pRelativeSearch=false it makes use of no previous/state
00131     //     information. Returns the physical volume containing the point, 
00132     //     with all previous mothers correctly set up.
00133     // ii) If pRelativeSearch is set to true, the search begin is the
00134     //     geometrical hierarchy at the location of the last located point,
00135     //     or the endpoint of the previous Step if SetGeometricallyLimitedStep()
00136     //     has been called immediately before.
00137     // The direction is used (to check if a volume is entered) if either
00138     //   - the argument ignoreDirection is false, or
00139     //   - the Navigator has determined that it is on an edge shared by two or
00140     //     more volumes.  (This is state information.)
00141     // 
00142     // Important Note: In order to call this the geometry MUST be closed.
00143 
00144   virtual
00145   void LocateGlobalPointWithinVolume(const G4ThreeVector& position);
00146     // Notify the Navigator that a track has moved to the new Global point
00147     // 'position', that is known to be within the current safety.
00148     // No check is performed to ensure that it is within  the volume. 
00149     // This method can be called instead of LocateGlobalPointAndSetup ONLY if
00150     // the caller is certain that the new global point (position) is inside the
00151     // same volume as the previous position.  Usually this can be guaranteed
00152     // only if the point is within safety.
00153 
00154   inline void LocateGlobalPointAndUpdateTouchableHandle(
00155                 const G4ThreeVector&       position,
00156                 const G4ThreeVector&       direction,
00157                       G4TouchableHandle&   oldTouchableToUpdate,
00158                 const G4bool               RelativeSearch = true);
00159     // First, search the geometrical hierarchy like the above method
00160     // LocateGlobalPointAndSetup(). Then use the volume found and its
00161     // navigation history to update the touchable.
00162 
00163   inline void LocateGlobalPointAndUpdateTouchable(
00164                 const G4ThreeVector&       position,
00165                 const G4ThreeVector&       direction,
00166                       G4VTouchable*        touchableToUpdate,
00167                 const G4bool               RelativeSearch = true);
00168     // First, search the geometrical hierarchy like the above method
00169     // LocateGlobalPointAndSetup(). Then use the volume found and its
00170     // navigation history to update the touchable.
00171 
00172   inline void LocateGlobalPointAndUpdateTouchable(
00173                 const G4ThreeVector&       position,
00174                       G4VTouchable*        touchableToUpdate,
00175                 const G4bool               RelativeSearch = true);
00176     // Same as the method above but missing direction.
00177 
00178   inline void SetGeometricallyLimitedStep();
00179     // Inform the navigator that the previous Step calculated
00180     // by the geometry was taken in its entirety.
00181 
00182   virtual G4double ComputeSafety(const G4ThreeVector &globalpoint,
00183                                  const G4double pProposedMaxLength = DBL_MAX);
00184     // Calculate the isotropic distance to the nearest boundary from the
00185     // specified point in the global coordinate system. 
00186     // The globalpoint utilised must be within the current volume.
00187     // The value returned is usually an underestimate.  
00188     // The proposed maximum length is used to avoid volume safety
00189     // calculations.  The geometry must be closed.
00190 
00191   inline G4VPhysicalVolume* GetWorldVolume() const;
00192     // Return the current  world (`topmost') volume.
00193 
00194   inline void SetWorldVolume(G4VPhysicalVolume* pWorld);
00195     // Set the world (`topmost') volume. This must be positioned at
00196     // origin (0,0,0) and unrotated.
00197 
00198   inline G4GRSVolume* CreateGRSVolume() const;
00199   inline G4GRSSolid* CreateGRSSolid() const; 
00200   inline G4TouchableHistory* CreateTouchableHistory() const;
00201     // `Touchable' creation methods: caller has deletion responsibility.
00202 
00203   virtual G4TouchableHistoryHandle CreateTouchableHistoryHandle() const;
00204     // Returns a reference counted handle to a touchable history.
00205 
00206   virtual G4ThreeVector GetLocalExitNormal(G4bool* valid);
00207     // Returns Exit Surface Normal and validity too.
00208     // It can only be called if the Navigator's last Step has crossed a
00209     // volume geometrical boundary.
00210     // It returns the Normal to the surface pointing out of the volume that
00211     // was left behind and/or into the volume that was entered.
00212     // (The normal is in the coordinate system of the final volume.)
00213     // This function takes full care about how to calculate this normal,
00214     // but if the surfaces are not convex it will return valid=false.
00215 
00216   inline G4int GetVerboseLevel() const;
00217   inline void  SetVerboseLevel(G4int level);
00218     // Get/Set Verbose(ness) level.
00219     // [if level>0 && G4VERBOSE, printout can occur]
00220 
00221   inline G4bool IsActive() const;
00222     // Verify if the navigator is active.
00223   inline void  Activate(G4bool flag);
00224     // Activate/inactivate the navigator.
00225 
00226   inline G4bool EnteredDaughterVolume() const;
00227     // The purpose of this function is to inform the caller if the track is
00228     // entering a daughter volume while exiting from the current volume.
00229     // This method returns 
00230     // - True only in case 1) above, that is when the Step has caused
00231     //   the track to arrive at a boundary of a daughter.
00232     // - False in cases 2), 3) and 4), i.e. in all other cases.
00233     // This function is not guaranteed to work if SetGeometricallyLimitedStep()
00234     // was not called when it should have been called.
00235   inline G4bool ExitedMotherVolume() const;
00236     // Verify if the step has exited the mother volume.
00237 
00238   inline void  CheckMode(G4bool mode);
00239     // Run navigation in "check-mode", therefore using additional
00240     // verifications and more strict correctness conditions.
00241     // Is effective only with G4VERBOSE set.
00242 
00243   void PrintState() const;
00244     // Print the internal state of the Navigator (for debugging).
00245     // The level of detail is according to the verbosity.
00246 
00247   inline const G4AffineTransform& GetGlobalToLocalTransform() const;
00248   inline const G4AffineTransform  GetLocalToGlobalTransform() const;
00249     // Obtain the transformations Global/Local (and inverse).
00250     // Clients of these methods must copy the data if they need to keep it.
00251 
00252   inline void ResetStackAndState();
00253     // Reset stack and minimum or navigator state machine necessary for reset
00254     // as needed by LocalGlobalPointAndSetup.
00255     // [Does not perform clears, resizes, or reset fLastLocatedPointLocal]
00256 
00257   inline G4int SeverityOfZeroStepping( G4int* noZeroSteps ) const; 
00258     // Report on severity of error and number of zero steps,
00259     // in case Navigator is stuck and is returning zero steps.
00260     // Values: 1 (small problem),  5 (correcting), 
00261     //         9 (ready to abandon), 10 (abandoned)
00262 
00263   // inline 
00264   void SetSavedState(); 
00265   // ( fValidExitNormal, fExitNormal, fExiting, fEntering, 
00266   //   fBlockedPhysicalVolume, fBlockedReplicaNo, fLastStepWasZero); 
00267   // inline 
00268   void RestoreSavedState(); 
00269     // Copy aspects of the state, to enable a non-state changing
00270     //  call to ComputeStep
00271 
00272  public:  // with description
00273 
00274   inline G4ThreeVector GetCurrentLocalCoordinate() const;
00275     // Return the local coordinate of the point in the reference system
00276     // of its containing volume that was found by LocalGlobalPointAndSetup.
00277     // The local coordinate of the last located track.
00278 
00279   inline G4ThreeVector NetTranslation() const;
00280   inline G4RotationMatrix NetRotation() const;
00281     // Compute+return the local->global translation/rotation of current volume.
00282 
00283  protected:  // with description
00284   inline G4ThreeVector ComputeLocalPoint(const G4ThreeVector& rGlobPoint) const;
00285     // Return position vector in local coordinate system, given a position
00286     // vector in world coordinate system.
00287 
00288   inline G4ThreeVector ComputeLocalAxis(const G4ThreeVector& pVec) const;
00289     // Return the local direction of the specified vector in the reference
00290     // system of the volume that was found by LocalGlobalPointAndSetup.
00291     // The Local Coordinates of point in world coordinate system.
00292 
00293   virtual void ResetState();
00294     // Utility method to reset the navigator state machine.
00295 
00296   inline EVolume VolumeType(const G4VPhysicalVolume *pVol) const;
00297     // Characterise `type' of volume - normal/replicated/parameterised.
00298 
00299   inline EVolume CharacteriseDaughters(const G4LogicalVolume *pLog) const;
00300     // Characterise daughter of logical volume.
00301 
00302   inline G4int GetDaughtersRegularStructureId(const G4LogicalVolume *pLog) const;
00303     // Get regular structure ID of first daughter
00304 
00305   virtual void SetupHierarchy();
00306     // Renavigate & reset hierarchy described by current history
00307     // o Reset volumes
00308     // o Recompute transforms and/or solids of replicated/parameterised
00309     //   volumes.
00310 
00311  protected:  // without description
00312 
00313   G4double kCarTolerance;
00314     // Geometrical tolerance for surface thickness of shapes.
00315 
00316   //
00317   // BEGIN State information
00318   //
00319 
00320   G4NavigationHistory fHistory;
00321     // Transformation and history of the current path
00322     // through the geometrical hierarchy.
00323 
00324   G4bool fEnteredDaughter;
00325     // A memory of whether in this Step a daughter volume is entered 
00326     // (set in Compute & Locate).
00327     //  After Compute: it expects to enter a daughter
00328     //  After Locate:  it has entered a daughter
00329 
00330   G4bool fExitedMother;
00331     // A similar memory whether the Step exited current "mother" volume
00332     // completely, not entering daughter.
00333 
00334   G4bool fWasLimitedByGeometry;
00335     // Set true if last Step was limited by geometry.
00336 
00337   G4ThreeVector fStepEndPoint;
00338     //  Endpoint of last ComputeStep 
00339     //  - can be used for optimisation (eg when computing safety)
00340 
00341   G4int  fVerbose;
00342     // Verbose(ness) level  [if > 0, printout can occur].
00343 
00344  private:
00345 
00346   G4bool fActive;
00347     // States if the navigator is activated or not.
00348 
00349   G4bool fEntering,fExiting;
00350     // Entering/Exiting volumes blocking/setup
00351     // o If exiting
00352     //      volume ptr & replica number (set & used by Locate..())
00353     //      used for blocking on redescent of geometry
00354     // o If entering
00355     //      volume ptr & replica number (set by ComputeStep(),used by
00356     //      Locate..()) of volume for `automatic' entry
00357 
00358   G4VPhysicalVolume *fBlockedPhysicalVolume;
00359   G4int fBlockedReplicaNo;
00360 
00361   // G4VPhysicalVolume *fCandidatePhysicalVolume;   // Unused 
00362   // G4int fCandidateReplicaNo;   
00363 
00364   G4ThreeVector fLastLocatedPointLocal;
00365     // Position of the last located point relative to its containing volume.
00366   G4bool fLocatedOutsideWorld;
00367     // Whether the last call to Locate methods left the world
00368   // G4PhysicalVolume* fLastVolumeLocated; 
00369 
00370   G4bool fValidExitNormal;    // Set true if have leaving volume normal
00371   G4ThreeVector fExitNormal;  // Leaving volume normal, in the
00372                               // volume containing the exited
00373                               // volume's coordinate system
00374   G4ThreeVector fGrandMotherExitNormal;  // Leaving volume normal, in its 
00375                                          // own coordinate system
00376 
00377   // Count zero steps - as one or two can occur due to changing momentum at
00378   //                    a boundary or at an edge common between volumes
00379   //                  - several are likely a problem in the geometry
00380   //                    description or in the navigation
00381   //
00382   G4bool fLastStepWasZero;
00383     // Whether the last ComputeStep moved Zero. Used to check for edges.
00384 
00385   G4bool fLocatedOnEdge;       
00386     // Whether the Navigator has detected an edge
00387   G4int fNumberZeroSteps;
00388     // Number of preceding moves that were Zero. Reset to 0 after finite step
00389   G4int fActionThreshold_NoZeroSteps;  
00390     // After this many failed/zero steps, act (push etc) 
00391   G4int fAbandonThreshold_NoZeroSteps; 
00392     // After this many failed/zero steps, abandon track
00393 
00394   G4ThreeVector  fPreviousSftOrigin;
00395   G4double       fPreviousSafety; 
00396     // Memory of last safety origin & value. Used in ComputeStep to ensure
00397     // that origin of current Step is in the same volume as the point of the
00398     // last relocation
00399 
00400   //
00401   // END State information
00402   //
00403 
00404   // Save key state information (NOT the navigation history stack)
00405   //
00406   struct G4SaveNavigatorState
00407   { 
00408      G4ThreeVector sExitNormal;  
00409      G4bool sValidExitNormal;    
00410      G4bool sEntering, sExiting;
00411      G4VPhysicalVolume* spBlockedPhysicalVolume;
00412      G4int sBlockedReplicaNo;  
00413      G4int sLastStepWasZero; 
00414 
00415      //  Potentially relevant
00416      //
00417      G4bool sLocatedOutsideWorld;
00418      G4ThreeVector sLastLocatedPointLocal; 
00419      G4bool sEnteredDaughter, sExitedMother;
00420      G4ThreeVector  sPreviousSftOrigin;
00421      G4double       sPreviousSafety; 
00422   } fSaveState; 
00423 
00424   // Tracking Invariants
00425   //
00426   G4VPhysicalVolume  *fTopPhysical;
00427     // A link to the topmost physical volume in the detector.
00428     // Must be positioned at the origin and unrotated.
00429 
00430   // Utility information
00431   //
00432   G4bool fCheck;
00433     // Check-mode flag  [if true, more strict checks are performed].
00434   G4bool fPushed;
00435     // Push flag  [if true, means a stuck particle has been pushed].
00436 
00437   // Helpers/Utility classes
00438   //
00439   G4NormalNavigation  fnormalNav;
00440   G4VoxelNavigation fvoxelNav;
00441   G4ParameterisedNavigation fparamNav;
00442   G4ReplicaNavigation freplicaNav;
00443   G4RegularNavigation fregularNav;
00444 };
00445 
00446 #include "G4Navigator.icc"
00447 
00448 #endif
00449 
00450 
00451 // NOTES:
00452 //
00453 // The following methods provide detailed information when a Step has
00454 // arrived at a geometrical boundary.  They distinguish between the different
00455 // causes that can result in the track leaving its current volume.
00456 //
00457 // Four cases are possible:
00458 //
00459 // 1) The particle has reached a boundary of a daughter of the current volume:
00460 //     (this could cause the relocation to enter the daughter itself
00461 //     or a potential granddaughter or further descendant)
00462 //     
00463 // 2) The particle has reached a boundary of the current
00464 //     volume, exiting into a mother (regardless the level
00465 //     at which it is located in the tree):
00466 //
00467 // 3) The particle has reached a boundary of the current
00468 //     volume, exiting into a volume which is not in its
00469 //     parental hierarchy:
00470 //
00471 // 4) The particle is not on a boundary between volumes:
00472 //     the function returns an exception, and the caller is
00473 //     reccomended to compare the G4touchables associated
00474 //     to the preStepPoint and postStepPoint to handle this case.
00475 //
00476 //   G4bool        EnteredDaughterVolume()
00477 //   G4bool        IsExitNormalValid()
00478 //   G4ThreeVector GetLocalExitNormal()
00479 //
00480 // The expected usefulness of these methods is to allow the caller to
00481 // determine how to compute the surface normal at the volume boundary. The two
00482 // possibilities are to obtain the normal from:
00483 //
00484 //   i) the solid associated with the volume of the initial point of the Step.
00485 //      This is valid for cases 2 and 3.  
00486 //      (Note that the initial point is generally the PreStepPoint of a Step).
00487 //   or
00488 // 
00489 //  ii) the solid of the final point, ie of the volume after the relocation.
00490 //      This is valid for case 1.
00491 //      (Note that the final point is generally the PreStepPoint of a Step).
00492 //
00493 // This way the caller can always get a valid normal, pointing outside
00494 // the solid for which it is computed, that can be used at his own
00495 // discretion.

Generated on Fri Apr 11 17:09:52 2008 for Geant4 by  doxygen 1.4.7