Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:59:28

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   std::vector<G4ThreeVector> ComputeFlyThrough(G4Vector3D*);
0109 
0110 #ifdef G4MULTITHREADED
0111   // Note: the order of calling of MovingToVisSubThread and SwitchToVisSubThread
0112   // is undefined, so you may need to implement mutexes to ensure your preferred
0113   // order - see, e.g., G4OpenGLQtViewer. To summarise, the order of calling is
0114   // as follows - see G4VisManager.cc.
0115   // DoneWithMasterThread
0116   // MovingToVisSubThread ) or ( SwitchToVisSubThread
0117   // SwitchToVisSubThread )    ( MovingToVisSubThread
0118   // DoneWithVisSubThread
0119   // MovingToMasterThread
0120   // SwitchToMasterThread
0121 
0122   // Called on the master thread before starting the vis sub-thread.
0123   virtual void DoneWithMasterThread ();
0124 
0125   // Called on the master thread after starting the vis sub-thread.
0126   virtual void MovingToVisSubThread ();
0127 
0128   // Called on the vis sub-thread at start of vis sub-thread.
0129   virtual void SwitchToVisSubThread ();
0130 
0131   // Called on the vis sub-thread when all events have been processed.
0132   virtual void DoneWithVisSubThread ();
0133 
0134   // Called on the vis sub-thread when all events have been processed.
0135   virtual void MovingToMasterThread ();
0136   
0137   // Called on the master thread after the vis sub-thread has terminated.
0138   virtual void SwitchToMasterThread ();
0139 #endif
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() = default;
0175     ~SceneTreeScene() = default;
0176     void SetViewer(G4VViewer* pViewer) {fpViewer = pViewer;}
0177     void SetModel(G4VModel* pModel);  // ...and more (see .cc)
0178   private:
0179     void ProcessVolume(const G4VSolid& solid) override;
0180     std::list<G4SceneTreeItem>::iterator FindOrInsertModel
0181     (const G4String& modelType,const G4String& modelID);
0182     std::list<G4SceneTreeItem>::iterator FindOrInsertTouchable
0183     (const G4String& modelID, G4SceneTreeItem& mother,
0184      G4int depth, const G4String& partialPathString, const G4String& fullPathString);
0185     G4VViewer* fpViewer = nullptr;
0186     G4VModel* fpModel = nullptr;
0187     G4int fMaximumExpandedDepth = 0;  // To be calculated in SetModel
0188     const G4int fMaximumExpanded = 30;  // So as not to swamp the GUI
0189   };
0190   SceneTreeScene& AccessSceneTreeScene() {return fSceneTreeScene;}
0191   G4SceneTreeItem& AccessSceneTree() {return fSceneTree;}
0192   void UpdateGUISceneTree();  // A utility
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   SceneTreeScene   fSceneTreeScene;  // G4PhysicalVolumeModel sends touchables to this scene
0262   G4SceneTreeItem  fSceneTree;
0263 
0264   //////////////////////////////////////////////////////////////
0265   // Other parameters.
0266   G4bool           fNeedKernelVisit;  // See DrawView() for comments.
0267 };
0268 
0269 #include "G4VViewer.icc"
0270 
0271 /*********************************************
0272 
0273 Here is a minimal DrawView () as it might be implemented in the
0274 concrete viewer.
0275 
0276 void G4VViewer::DrawView () {  // Default - concrete view usually overrides.
0277 
0278   // First, a view should decide when to re-visit the G4 kernel.
0279   // Sometimes it might not be necessary, e.g., if the scene is stored
0280   // in a graphical database (e.g., OpenGL's display lists) and only
0281   // the viewing angle has changed.  But graphics systems without a
0282   // graphical database will always need to visit the G4 kernel.
0283 
0284   NeedKernelVisit ();  // Default is - always visit G4 kernel.
0285   // Note: this routine sets the fNeedKernelVisit flag of *all* the views of
0286   // the scene.
0287 
0288   ProcessView ();             // The basic logic is here.
0289 
0290   // Then a view may have more to do, e.g., display the graphical
0291   // database.  That code should come here before finally...
0292 
0293   FinishView ();              // Flush streams and/or swap buffers.
0294 }
0295 
0296 *********************************************/
0297 
0298 #endif