Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-07-20 08:31:03

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   
0192   //GUI update functions
0193   void UpdateGUIControlWidgets();
0194   void UpdateGUIDrawingStyle();
0195   void UpdateGUIProjectionStyle();
0196   void UpdateGUITransparencySlider();
0197 
0198   const G4int fMaxAllTouchables = 10000;  // Limits memory to about 50 MB per PV model
0199   G4bool fCurtailDescent = false;  // Flag to curtail descent into PV model for scene tree
0200 
0201   //////////////////////////////////////////////////////////////
0202   // Access functions.
0203   const G4String&         GetName           () const;
0204   const G4String&         GetShortName      () const;
0205   void                    SetName           (const G4String&);
0206   G4int                   GetViewId         () const;
0207   G4VSceneHandler*        GetSceneHandler   () const;
0208   const G4ViewParameters& GetViewParameters        () const;
0209   const G4ViewParameters& GetDefaultViewParameters () const;
0210   G4double                GetKernelVisitElapsedTimeSeconds () const;
0211 
0212   virtual const std::vector<G4ModelingParameters::VisAttributesModifier>*
0213   GetPrivateVisAttributesModifiers() const;
0214   // So that privately accumulated vis attributes modifiers may be
0215   // concatenated with the standard vis attributes modifiers for commands
0216   // such as /vis/viewer/set/all and /vis/viewer/save.
0217 
0218   void SetViewParameters         (const G4ViewParameters& vp);
0219   void SetDefaultViewParameters  (const G4ViewParameters& vp);
0220 
0221   //////////////////////////////////////////////////////////////
0222   // Public utility functions.
0223 
0224   const G4VisAttributes*  GetApplicableVisAttributes
0225                                             (const G4VisAttributes*) const;
0226 
0227   void SetNeedKernelVisit (G4bool need);
0228   // Sets individual need-visit flag.
0229 
0230   void NeedKernelVisit ();
0231   // Flags all views the need to re-visit the GEANT4 kernel to refresh
0232   // the scene.
0233 
0234   void ProcessView ();
0235   // Used by DrawView ().  Invokes SetView ().  The basic logic is here.
0236 
0237   void ProcessTransients ();
0238   // Re-draws transients only
0239 
0240   void ZoomFromMouseWheel(G4double delta, G4bool shift = false, G4double xPos = 0, G4double yPos = 0);
0241 
0242   virtual G4bool GetWindowSize(unsigned int& a_w,unsigned int& a_h) {a_w = 0; a_h = 0; return false;}
0243   virtual G4double GetSceneNearWidth() {return 0;}
0244 
0245 protected:
0246 
0247   //////////////////////////////////////////////////////////////
0248   // Protected utility functions.
0249 
0250   void SetTouchable
0251   (const std::vector<G4PhysicalVolumeModel::G4PhysicalVolumeNodeID>& fullPath);
0252   // Set the touchable for /vis/touchable/set/... commands.
0253 
0254   void TouchableSetVisibility
0255   (const std::vector<G4PhysicalVolumeModel::G4PhysicalVolumeNodeID>& fullPath,
0256    G4bool visibility);
0257   // Set the touchable visibility attribute.
0258   // Changes the Vis Attribute Modifiers WITHOUT triggering a rebuild.
0259 
0260   void TouchableSetColour
0261   (const std::vector<G4PhysicalVolumeModel::G4PhysicalVolumeNodeID>& fullPath,
0262    const G4Colour&);
0263   // Set the touchable colour attribute.
0264   // Changes the Vis Attribute Modifiers WITHOUT triggering a rebuild.
0265 
0266   //////////////////////////////////////////////////////////////
0267   // Data members
0268   G4VSceneHandler&        fSceneHandler;     // Abstract scene for this view.
0269   G4int            fViewId;    // Id of this instance.
0270   G4String         fName;
0271   G4String         fShortName; // Up to first ' ' character, if any.
0272   G4ViewParameters fVP;        // View parameters.
0273   G4ViewParameters fDefaultVP; // Default view parameters.
0274   G4double         fKernelVisitElapsedTimeSeconds = 999.;  // Default to a large number
0275   // Note: fKernelVisitElapsedTimeSeconds is measured in ProcessView().
0276   G4SceneTreeItem  fSceneTree;
0277 
0278   //////////////////////////////////////////////////////////////
0279   // Other parameters.
0280   G4bool           fNeedKernelVisit;  // See DrawView() for comments.
0281   G4bool           fTransientsNeedRedrawing;  // See DrawView() for comments.
0282 };
0283 
0284 #include "G4VViewer.icc"
0285 
0286 /*********************************************
0287 
0288 Here is a minimal DrawView () as it might be implemented in the
0289 concrete viewer.
0290 
0291 void G4VViewer::DrawView () {  // Default - concrete view usually overrides.
0292 
0293   // First, a view should decide when to re-visit the G4 kernel.
0294   // Sometimes it might not be necessary, e.g., if the scene is stored
0295   // in a graphical database (e.g., OpenGL's display lists) and only
0296   // the viewing angle has changed.  But graphics systems without a
0297   // graphical database will always need to visit the G4 kernel.
0298 
0299   NeedKernelVisit ();  // Default is - always visit G4 kernel.
0300   // Note: this routine sets the fNeedKernelVisit flag of *all* the views of
0301   // the scene.
0302 
0303   ProcessView ();             // The basic logic is here.
0304 
0305   // Then a view may have more to do, e.g., display the graphical
0306   // database.  That code should come here before finally...
0307 
0308   FinishView ();              // Flush streams and/or swap buffers.
0309 }
0310 
0311 *********************************************/
0312 
0313 #endif