Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-11-07 10:04:31

0001 //
0002 // ********************************************************************
0003 // * License and Disclaimer                                           *
0004 // *                                                                  *
0005 // * The  Geant4 software  is  copyright of the Copyright Holders  of *
0006 // * the Geant4 Collaboration.  It is provided  under  the terms  and *
0007 // * conditions of the Geant4 Software License,  included in the file *
0008 // * LICENSE and available at  http://cern.ch/geant4/license .  These *
0009 // * include a list of copyright holders.                             *
0010 // *                                                                  *
0011 // * Neither the authors of this software system, nor their employing *
0012 // * institutes,nor the agencies providing financial support for this *
0013 // * work  make  any representation or  warranty, express or implied, *
0014 // * regarding  this  software system or assume any liability for its *
0015 // * use.  Please see the license in the file  LICENSE  and URL above *
0016 // * for the full disclaimer and the limitation of liability.         *
0017 // *                                                                  *
0018 // * This  code  implementation is the result of  the  scientific and *
0019 // * technical work of the GEANT4 collaboration.                      *
0020 // * By using,  copying,  modifying or  distributing the software (or *
0021 // * any work based  on the software)  you  agree  to acknowledge its *
0022 // * use  in  resulting  scientific  publications,  and indicate your *
0023 // * acceptance of all terms of the Geant4 Software license.          *
0024 // ********************************************************************
0025 //
0026 //
0027 //
0028 // 
0029 // John Allison  27th March 1996
0030 //
0031 // Class description
0032 //
0033 // Abstract interface class for graphics viewers.
0034 
0035 #ifndef G4VVIEWER_HH
0036 #define G4VVIEWER_HH
0037 
0038 #include "globals.hh"
0039 
0040 #include "G4SceneTreeItem.hh"
0041 
0042 #include "G4ViewParameters.hh"
0043 #include "G4PhysicalVolumeModel.hh"
0044 #include "G4PseudoScene.hh"
0045 
0046 #include <vector>
0047 #include <list>
0048 
0049 class G4VSceneHandler;
0050 
0051 // clang-format off
0052 class G4VViewer {
0053 
0054 public: // With description
0055 
0056   friend std::ostream& operator << (std::ostream& os, const G4VViewer& v);
0057 
0058   G4VViewer (G4VSceneHandler&, G4int id, const G4String& name = "");
0059   virtual ~G4VViewer ();
0060 
0061   virtual void Initialise ();
0062   // Called immediately after construction for those operations that
0063   // must await complete contruction of viewer and all its bases.  For
0064   // example, if this class (G4VViewer) is inherited virtually, as in
0065   // the OpenGL sub-category, it will not be fully constructed until
0066   // *after* the the derived viewer (this is the rule about order of
0067   // construction for virtual inheritance), so the derived viewer may
0068   // not use information in G4VViewer in its contructor.  Hence such
0069   // code must be in Initialise().
0070 
0071   //////////////////////////////////////////////////////////////
0072   // View manipulation functions.
0073 
0074   virtual void ResetView ();
0075   // Reset view parameters to default, including sub-class parameters, if any.
0076   // The sub-class should always invoke the base class implementation, i.e:
0077   // virtual void SubClass::ResetView () {
0078   //   G4VViewer::ResetView();
0079   //   // Then reset sub-class parameters
0080   //   ...
0081 
0082   virtual void SetView () = 0;
0083   // Take view parameters and work out model/view transformation,
0084   // projection transformation, lighting, etc.
0085 
0086   virtual void ClearView () = 0;
0087   // Clear screen/viewing buffers.
0088 
0089   virtual void DrawView () = 0;
0090   // Draw view of the scene currently attached to the scene handler -
0091   // see example of a minimal function at end of this file.
0092   
0093   void RefreshView ();
0094   // Simply invokes SetView, ClearView, DrawView.
0095 
0096   virtual void ShowView ();
0097   // Show view (for graphics systems which require to process
0098   // all drawn objects before finalising the view).
0099 
0100   virtual void FinishView ();
0101   // Called at the end of drawing scene.  Used to flush streams, or
0102   // swap buffers.  (Perhaps it is inappropriately named, perhaps its
0103   // function could be incorporated into EndModeling ().  It marks the
0104   // end of scene drawing; be aware hits and digi drawing may Follow.
0105   // It is not yet the end of all drawing; that is signalled by
0106   // ShowView ().)
0107 
0108   virtual G4bool ReadyToDraw() {return true;}
0109 
0110   std::vector<G4ThreeVector> ComputeFlyThrough(G4Vector3D*);
0111 
0112   // Note: the order of calling of MovingToVisSubThread and SwitchToVisSubThread
0113   // is undefined, so you may need to implement mutexes to ensure your preferred
0114   // order - see, e.g., G4OpenGLQtViewer. To summarise, the order of calling is
0115   // as follows - see G4VisManager.cc.
0116   // DoneWithMasterThread
0117   // MovingToVisSubThread ) or ( SwitchToVisSubThread
0118   // SwitchToVisSubThread )    ( MovingToVisSubThread
0119   // DoneWithVisSubThread
0120   // MovingToMasterThread
0121   // SwitchToMasterThread
0122 
0123   // Called on the master thread before starting the vis sub-thread.
0124   virtual void DoneWithMasterThread () {}
0125 
0126   // Called on the master thread after starting the vis sub-thread.
0127   virtual void MovingToVisSubThread () {}
0128 
0129   // Called on the vis sub-thread at start of vis sub-thread.
0130   virtual void SwitchToVisSubThread () {}
0131 
0132   // Called on the vis sub-thread when all events have been processed.
0133   virtual void DoneWithVisSubThread () {}
0134 
0135   // Called on the vis sub-thread when all events have been processed.
0136   virtual void MovingToMasterThread () {}
0137   
0138   // Called on the master thread after the vis sub-thread has terminated.
0139   virtual void SwitchToMasterThread () {}
0140 
0141   //////////////////////////////////////////////////////////////
0142   // Stuff for scene tree.
0143   /**
0144    - The scene tree is a tree of G4SceneTreeItem objects (see graphics-reps).
0145    - Its root is a data member fSceneTree of all viewers by virtue of
0146    G4VViewer inheritance,
0147    - G4SceneTreeItem is an aggregate of data members that represent
0148    properties of objects in the scene (G4Scene). Its data members are
0149    low-level types - G4String, G4VisAttributes and G4AttDef/Value - so
0150    that it can be used across categories, avoiding coupling.
0151    - The root item has children that represent the models (G4VModel
0152    sub-classes) in the scene.
0153    - For a G4PhysicalVolumeModel (detector components), its children and
0154    children's children, etc., imitate the geometry hierarchy of that
0155    model. These descendants are called "touchables".
0156    - There may be more than one G4PhysicalVolumeModel, depending how
0157    the user creates his/her scene.
0158    - The scene tree is reviewed, and updated if necessary, at every pass
0159    of G4VSceneHandler::ProcessScene.  This is called a "kernel visit".
0160    - A kernel visit is triggered by some vis commands (e.g.,
0161    /vis/viewer/rebuild) and by a viewer if it deems necessary. For
0162    example, a kernel visit may not be required for a rotation, zoom, etc.,
0163    but required for a change from surface to wireframe.
0164    - The idea is that the scene tree can be passed to a GUI, the GUI can
0165    create a tree widget, and interactions with it raise UI commands such as
0166    /vis/scene/activateModel, /vis/set/touchable and /vis/touchable/set/...
0167    The viewer decides if this requires a kernel visit, otherwise it
0168    must update fSceneTree itself (utilities are provided -
0169    G4VViewer::TouchableSetVisibility/Colour).
0170   */
0171   class SceneTreeScene: public G4PseudoScene {
0172     // G4PhysicalVolumeModel sends touchables to this scene
0173   public:
0174     SceneTreeScene(G4VViewer*, G4PhysicalVolumeModel*);
0175     ~SceneTreeScene() = default;
0176   private:
0177     void ProcessVolume(const G4VSolid& solid) override;
0178     std::list<G4SceneTreeItem>::iterator FindOrInsertTouchable
0179     (const G4String& modelID, G4SceneTreeItem& mother,
0180      G4int depth, const G4String& partialPathString, const G4String& fullPathString);
0181     G4VViewer* fpViewer = nullptr;
0182     G4PhysicalVolumeModel* fpPVModel = nullptr;
0183     std::list<G4SceneTreeItem>::iterator  fModelIter;  // Points to scene tree item for this model
0184     G4int fMaximumExpandedDepth = 0;    // To be calculated in constructor
0185     const G4int fMaximumExpanded = 30;  // So as not to swamp the GUI
0186   };
0187   void InsertModelInSceneTree(G4VModel*);
0188   const G4SceneTreeItem& GetSceneTree() {return fSceneTree;}
0189   G4SceneTreeItem& AccessSceneTree() {return fSceneTree;}
0190   void UpdateGUISceneTree();  // A utility
0191   const G4int fMaxNTouchables = 10000;  // Limits memory to about 50 MB
0192   G4bool fCurtailDescent = false;  // Flag to curtail descent into PV model for scene tree
0193 
0194   //////////////////////////////////////////////////////////////
0195   // Access functions.
0196   const G4String&         GetName           () const;
0197   const G4String&         GetShortName      () const;
0198   void                    SetName           (const G4String&);
0199   G4int                   GetViewId         () const;
0200   G4VSceneHandler*        GetSceneHandler   () const;
0201   const G4ViewParameters& GetViewParameters        () const;
0202   const G4ViewParameters& GetDefaultViewParameters () const;
0203   G4double                GetKernelVisitElapsedTimeSeconds () const;
0204 
0205   virtual const std::vector<G4ModelingParameters::VisAttributesModifier>*
0206   GetPrivateVisAttributesModifiers() const;
0207   // So that privately accumulated vis attributes modifiers may be
0208   // concatenated with the standard vis attributes modifiers for commands
0209   // such as /vis/viewer/set/all and /vis/viewer/save.
0210 
0211   void SetViewParameters         (const G4ViewParameters& vp);
0212   void SetDefaultViewParameters  (const G4ViewParameters& vp);
0213 
0214   //////////////////////////////////////////////////////////////
0215   // Public utility functions.
0216 
0217   const G4VisAttributes*  GetApplicableVisAttributes
0218                                             (const G4VisAttributes*) const;
0219 
0220   void SetNeedKernelVisit (G4bool need);
0221   // Sets individual need-visit flag.
0222 
0223   void NeedKernelVisit ();
0224   // Flags all views the need to re-visit the GEANT4 kernel to refresh
0225   // the scene.
0226 
0227   void ProcessView ();
0228   // Used by DrawView ().  Invokes SetView ().  The basic logic is here.
0229 
0230 protected:
0231 
0232   //////////////////////////////////////////////////////////////
0233   // Protected utility functions.
0234 
0235   void SetTouchable
0236   (const std::vector<G4PhysicalVolumeModel::G4PhysicalVolumeNodeID>& fullPath);
0237   // Set the touchable for /vis/touchable/set/... commands.
0238 
0239   void TouchableSetVisibility
0240   (const std::vector<G4PhysicalVolumeModel::G4PhysicalVolumeNodeID>& fullPath,
0241    G4bool visibility);
0242   // Set the touchable visibility attribute.
0243   // Changes the Vis Attribute Modifiers WITHOUT triggering a rebuild.
0244 
0245   void TouchableSetColour
0246   (const std::vector<G4PhysicalVolumeModel::G4PhysicalVolumeNodeID>& fullPath,
0247    const G4Colour&);
0248   // Set the touchable colour attribute.
0249   // Changes the Vis Attribute Modifiers WITHOUT triggering a rebuild.
0250 
0251   //////////////////////////////////////////////////////////////
0252   // Data members
0253   G4VSceneHandler&        fSceneHandler;     // Abstract scene for this view.
0254   G4int            fViewId;    // Id of this instance.
0255   G4String         fName;
0256   G4String         fShortName; // Up to first ' ' character, if any.
0257   G4ViewParameters fVP;        // View parameters.
0258   G4ViewParameters fDefaultVP; // Default view parameters.
0259   G4double         fKernelVisitElapsedTimeSeconds = 999.;  // Default to a large number
0260   // Note: fKernelVisitElapsedTimeSeconds is measured in ProcessView().
0261   G4SceneTreeItem  fSceneTree;
0262 
0263   //////////////////////////////////////////////////////////////
0264   // Other parameters.
0265   G4bool           fNeedKernelVisit;  // See DrawView() for comments.
0266 };
0267 
0268 #include "G4VViewer.icc"
0269 
0270 /*********************************************
0271 
0272 Here is a minimal DrawView () as it might be implemented in the
0273 concrete viewer.
0274 
0275 void G4VViewer::DrawView () {  // Default - concrete view usually overrides.
0276 
0277   // First, a view should decide when to re-visit the G4 kernel.
0278   // Sometimes it might not be necessary, e.g., if the scene is stored
0279   // in a graphical database (e.g., OpenGL's display lists) and only
0280   // the viewing angle has changed.  But graphics systems without a
0281   // graphical database will always need to visit the G4 kernel.
0282 
0283   NeedKernelVisit ();  // Default is - always visit G4 kernel.
0284   // Note: this routine sets the fNeedKernelVisit flag of *all* the views of
0285   // the scene.
0286 
0287   ProcessView ();             // The basic logic is here.
0288 
0289   // Then a view may have more to do, e.g., display the graphical
0290   // database.  That code should come here before finally...
0291 
0292   FinishView ();              // Flush streams and/or swap buffers.
0293 }
0294 
0295 *********************************************/
0296 
0297 #endif