Geant4  10.03.p01
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
G4VisManager.hh
Go to the documentation of this file.
1 //
2 // ********************************************************************
3 // * License and Disclaimer *
4 // * *
5 // * The Geant4 software is copyright of the Copyright Holders of *
6 // * the Geant4 Collaboration. It is provided under the terms and *
7 // * conditions of the Geant4 Software License, included in the file *
8 // * LICENSE and available at http://cern.ch/geant4/license . These *
9 // * include a list of copyright holders. *
10 // * *
11 // * Neither the authors of this software system, nor their employing *
12 // * institutes,nor the agencies providing financial support for this *
13 // * work make any representation or warranty, express or implied, *
14 // * regarding this software system or assume any liability for its *
15 // * use. Please see the license in the file LICENSE and URL above *
16 // * for the full disclaimer and the limitation of liability. *
17 // * *
18 // * This code implementation is the result of the scientific and *
19 // * technical work of the GEANT4 collaboration. *
20 // * By using, copying, modifying or distributing the software (or *
21 // * any work based on the software) you agree to acknowledge its *
22 // * use in resulting scientific publications, and indicate your *
23 // * acceptance of all terms of the Geant4 Software license. *
24 // ********************************************************************
25 //
26 //
27 // $Id: G4VisManager.hh 97316 2016-06-01 12:12:58Z gcosmo $
28 //
29 //
30 
31 // Class Description:
32 //
33 // The GEANT4 Visualization Manager - John Allison 02/Jan/1996.
34 //
35 // G4VisManager is a "Singleton", i.e., only one instance of it or any
36 // derived class may exist. A G4Exception is thrown if an attempt is
37 // made to instantiate more than one.
38 //
39 // It is also an abstract class, so the user must derive his/her own
40 // class from G4VisManager, implement the pure virtual function
41 // RegisterGraphicsSystems, and instantiate an object of the derived
42 // class - for an example see
43 // visualization/include/G4VisExecutive.hh/icc.
44 //
45 // The recommended way for users to obtain a pointer to the vis
46 // manager is with G4VVisManager::GetConcreteInstance (), being always
47 // careful to test for non-zero. This pointer is non-zero only when
48 // (a) an object of the derived class exists and (b) when there is a
49 // valid viewer available.
50 //
51 // Graphics system registration is normally done through the protected
52 // pure virtual function RegisterGraphicsSystems called from
53 // Initialise (). You can also use the public function
54 // RegisterGraphicsSystem (new MyGraphicsSystem) if you have your own
55 // graphics system. A graphics system is, in effect, a factory for
56 // scene handlers and viewers.
57 //
58 // The VisManager creates and manages graphics systems, scenes, scene
59 // handlers, viewers and some models and model makers. You can have
60 // any number. It has the concept of a "current viewer", and the
61 // "current scene handler", the "current scene" and the "current
62 // graphics system" which go with it. You can select the current
63 // viewer. Most of the the operations of the VisManager take place
64 // with the current viewer, in particular, the Draw operations.
65 //
66 // Each scene comprises drawable objects such as detector components
67 // and trajectories, hits and digis when appropriate. A scene handler
68 // translates a scene into graphics-system-specific function calls
69 // and, possibly, a graphics-system-dependent database - display
70 // lists, scene graphs, etc. Each viewer has its "view parameters"
71 // (see class description of G4ViewParameters for available parameters
72 // and also for a description of the concept of a "standard view" and
73 // all that).
74 //
75 // A friend class G4VisStateDependent is "state dependent", i.e., it
76 // is notified on change of state (G4ApplicationState). This is used
77 // to message the G4VisManager to draw hits, digis and trajectories in
78 // the current scene at the end of event, as required.
79 
80 #ifndef G4VISMANAGER_HH
81 #define G4VISMANAGER_HH
82 
83 // Temporary definition until Xeon Phi can handle full C++11.
84 #ifndef __MIC__
85 #define G4VIS_USE_STD11
86 #endif
87 
88 #include "G4VVisManager.hh"
89 
90 #include "globals.hh"
91 #include "G4GraphicsSystemList.hh"
92 #include "G4ModelingParameters.hh"
93 #include "G4NullModel.hh"
94 #include "G4SceneHandlerList.hh"
95 #include "G4SceneList.hh"
96 #include "G4TrajectoriesModel.hh"
97 #include "G4Transform3D.hh"
98 #include "G4UImessenger.hh"
99 
100 #include <iostream>
101 #include <vector>
102 #include <map>
103 
104 #include "G4Threading.hh"
105 
106 class G4Scene;
107 class G4UIcommand;
108 class G4UImessenger;
109 class G4VisStateDependent;
110 class G4VTrajectoryModel;
111 class G4VUserVisAction;
112 template <typename> class G4VFilter;
113 template <typename> class G4VisFilterManager;
114 template <typename> class G4VisModelManager;
115 template <typename> class G4VModelFactory;
116 class G4Event;
117 
118 // Useful typedef's
123 
125 
126  // Management friends...
127  friend class G4VSceneHandler;
128  friend class G4VViewer;
129  friend class G4VisStateDependent;
130  friend class G4VisCommandList;
131 
132  // operator << friends...
133  friend std::ostream& operator << (std::ostream&, const G4VGraphicsSystem&);
134  friend std::ostream& operator << (std::ostream&, const G4VSceneHandler&);
135 
136 public: // With description
137 
138  enum Verbosity {
139  quiet, // Nothing is printed.
140  startup, // Startup and endup messages are printed...
141  errors, // ...and errors...
142  warnings, // ...and warnings...
143  confirmations, // ...and confirming messages...
144  parameters, // ...and parameters of scenes and views...
145  all // ...and everything available.
146  };
147  // Simple graded message scheme.
148 
149 protected: // With description
150 
151  G4VisManager (const G4String& verbosityString = "warnings");
152  // The constructor is protected so that an object of the derived
153  // class may be constructed.
154 
155 public: // With description
156 
157  virtual ~G4VisManager ();
158 
159 private:
160 
161  // Private copy constructor and assigment operator - copying and
162  // assignment not allowed. Keeps CodeWizard happy.
163  G4VisManager (const G4VisManager&);
164  G4VisManager& operator = (const G4VisManager&);
165 
166 public:
167  static G4VisManager* GetInstance ();
168  // Returns pointer to itself. Throws a G4Exception if called before
169  // instantiation. Intended only for use within the vis category; the
170  // normal user should instead use G4VVisManager::GetConcreteInstance()
171  // to get a "higher level" pointer for general use - but always test
172  // for non-zero.
173 
174 public: // With description
175 
176  void Initialise ();
177  void Initialize (); // Alias Initialise ().
178 
179  // Optional registration of user vis actions. Added to scene with
180  // /vis/scene/add/userAction.
182  (const G4String& name, G4VUserVisAction*,
185  (const G4String& name, G4VUserVisAction*,
188  (const G4String& name, G4VUserVisAction*,
190 
192  // Register an individual graphics system. Normally this is done in
193  // a sub-class implementation of the protected virtual function,
194  // RegisterGraphicsSystems. See, e.g., G4VisExecutive.icc.
195 
197  // Register trajectory draw model factory. Assumes ownership of factory.
198 
200  // Register trajectory model. Assumes ownership of model.
201 
203  // Register trajectory filter model factory. Assumes ownership of factory.
204 
206  // Register trajectory filter model. Assumes ownership of model.
207 
209  // Register trajectory hit model factory. Assumes ownership of factory.
210 
211  void RegisterModel(G4VFilter<G4VHit>* filter);
212  // Register trajectory hit model. Assumes ownership of model.
213 
215  // Register trajectory digi model factory. Assumes ownership of factory.
216 
217  void RegisterModel(G4VFilter<G4VDigi>* filter);
218  // Register trajectory digi model. Assumes ownership of model.
219 
220  void SelectTrajectoryModel(const G4String& model);
221  // Set default trajectory model. Useful for use in compiled code
222 
223  void RegisterMessenger(G4UImessenger* messenger);
224  // Register messenger. Assumes ownership of messenger.
225 
227  // Now functions that implement the pure virtual functions of
228  // G4VVisManager for drawing various visualization primitives, useful
229  // for representing hits, digis, etc.
230 
231  void Draw (const G4Circle&,
232  const G4Transform3D& objectTransformation = G4Transform3D());
233 
234  void Draw (const G4Polyhedron&,
235  const G4Transform3D& objectTransformation = G4Transform3D());
236 
237  void Draw (const G4Polyline&,
238  const G4Transform3D& objectTransformation = G4Transform3D());
239 
240  void Draw (const G4Polymarker&,
241  const G4Transform3D& objectTransformation = G4Transform3D());
242 
243  void Draw (const G4Scale&,
244  const G4Transform3D& objectTransformation = G4Transform3D());
245 
246  void Draw (const G4Square&,
247  const G4Transform3D& objectTransformation = G4Transform3D());
248 
249  void Draw (const G4Text&,
250  const G4Transform3D& objectTransformation = G4Transform3D());
251 
252  void Draw2D (const G4Circle&,
253  const G4Transform3D& objectTransformation = G4Transform3D());
254 
255  void Draw2D (const G4Polyhedron&,
256  const G4Transform3D& objectTransformation = G4Transform3D());
257 
258  void Draw2D (const G4Polyline&,
259  const G4Transform3D& objectTransformation = G4Transform3D());
260 
261  void Draw2D (const G4Polymarker&,
262  const G4Transform3D& objectTransformation = G4Transform3D());
263 
264  void Draw2D (const G4Square&,
265  const G4Transform3D& objectTransformation = G4Transform3D());
266 
267  void Draw2D (const G4Text&,
268  const G4Transform3D& objectTransformation = G4Transform3D());
269 
271  // Now functions that implement the pure virtual functions of
272  // G4VVisManager for drawing a GEANT4 object. Note that the
273  // visualization attributes needed in some cases override any
274  // visualization attributes that are associated with the object
275  // itself - thus you can, for example, change the colour of a
276  // physical volume.
277 
278  void Draw (const G4VTrajectory&);
279 
280  void Draw (const G4VHit&);
281 
282  void Draw (const G4VDigi&);
283 
284  void Draw (const G4LogicalVolume&, const G4VisAttributes&,
285  const G4Transform3D& objectTransformation = G4Transform3D());
286 
287  void Draw (const G4VPhysicalVolume&, const G4VisAttributes&,
288  const G4Transform3D& objectTransformation = G4Transform3D());
289 
290  void Draw (const G4VSolid&, const G4VisAttributes&,
291  const G4Transform3D& objectTransformation = G4Transform3D());
292 
294  // Optional methods that you may use to bracket a series of Draw
295  // messages that have identical objectTransformation to improve
296  // drawing speed. Use Begin/EndDraw for a series of Draw messages,
297  // Begin/EndDraw2D for a series of Draw2D messages. Do not mix Draw
298  // and Draw2D messages.
299 
300  void BeginDraw
301  (const G4Transform3D& objectTransformation = G4Transform3D());
302 
303  void EndDraw ();
304 
305  void BeginDraw2D
306  (const G4Transform3D& objectTransformation = G4Transform3D());
307 
308  void EndDraw2D ();
309 
311  // Now other pure virtual functions of G4VVisManager...
312 
313  void GeometryHasChanged ();
314  // Used by run manager to notify change.
315 
317  // This method shoud be invoked by a class that has its own event loop,
318  // such as the RayTracer, material scanner, etc. If the argument is true,
319  // the following state changes among Idle, GeomClosed and EventProc are
320  // caused by such a class, and thus not by the ordinary event simulation.
321  // The same method with false should be invoked once such an event loop
322  // is over.
323 
324  void NotifyHandlers();
325  // Notify scene handlers (G4VGraphicsScene objects) that the scene
326  // has changed so that they may rebuild their graphics database, if
327  // any, and redraw all views.
328 
329  void DispatchToModel(const G4VTrajectory&);
330  // Draw the trajectory.
331 
333  G4bool FilterHit(const G4VHit&);
334  G4bool FilterDigi(const G4VDigi&);
335 
336 #ifdef G4MULTITHREADED
337 
338  virtual void SetUpForAThread();
339  // This method is invoked by G4WorkerRunManager
340 
341  static G4ThreadFunReturnType G4VisSubThread(G4ThreadFunArgType);
342  // Vis sub-thread function.
343 
344 #endif
345 
347  // Administration routines.
348 
349  void CreateSceneHandler (const G4String& name = "");
350  // Creates scene handler for the current system.
351 
352  void CreateViewer (const G4String& name = "", const G4String& XGeometry = "");
353  // Creates viewer for the current scene handler.
354 
355 private:
356 
357  void BeginOfRun ();
358 
359  void BeginOfEvent ();
360 
361  void EndOfEvent ();
362  // This is called on change of state (G4ApplicationState). It is
363  // used to draw hits, digis and trajectories if included in the
364  // current scene at the end of event, as required.
365 
366  void EndOfRun ();
367 
368 public: // With description
369 
371  // Access functions.
372 
373  void Enable();
374  void Disable();
375  // Global enable/disable functions.
376 
378 
379  struct UserVisAction {
380  UserVisAction(const G4String& name, G4VUserVisAction* pUserVisAction)
381  :fName(name), fpUserVisAction(pUserVisAction) {}
384  };
385  const std::vector<UserVisAction>& GetRunDurationUserVisActions () const;
386  const std::vector<UserVisAction>& GetEndOfEventUserVisActions () const;
387  const std::vector<UserVisAction>& GetEndOfRunUserVisActions () const;
388  const std::map<G4VUserVisAction*,G4VisExtent>& GetUserVisActionExtents () const;
390  G4Scene* GetCurrentScene () const;
392  G4VViewer* GetCurrentViewer () const;
394  // The above is non-const because it checks and updates the List by
395  // calling RegisterGraphicsSystems() if no graphics systems are
396  // already registered.
398  const G4SceneList& GetSceneList () const;
399  static Verbosity GetVerbosity ();
402  const G4Event* GetRequestedEvent () const;
405 #ifdef G4MULTITHREADED
406  G4int GetMaxEventQueueSize () const;
407  G4bool GetWaitOnEventQueueFull () const;
408 #endif
409 
410  void SetUserAction
411  (G4VUserVisAction* pVisAction,
412  const G4VisExtent& = G4VisExtent::NullExtent); // Register run-duration.
413  void SetUserActionExtent (const G4VisExtent&); //Legacy: deprecated.
415  void SetCurrentScene (G4Scene*);
417  void SetCurrentViewer (G4VViewer*);
418  G4SceneHandlerList& SetAvailableSceneHandlers (); // Returns lvalue.
419  G4SceneList& SetSceneList (); // Returns lvalue.
420  void SetVerboseLevel (G4int);
421  void SetVerboseLevel (const G4String&);
422  void SetVerboseLevel (Verbosity);
423  void SetEventRefreshing (G4bool);
427  // If non-zero, requested event is used in G4VSceneHandler::ProcessScene.
428  void SetRequestedEvent (const G4Event*);
431 #ifdef G4MULTITHREADED
432  void SetMaxEventQueueSize (G4int);
433  void SetWaitOnEventQueueFull (G4bool);
434 #endif
435 
437  // Utility functions.
438 
439  G4String ViewerShortName (const G4String& viewerName) const;
440  // Returns shortened version of viewer name, i.e., up to first space,
441  // if any.
442 
443  G4VViewer* GetViewer (const G4String& viewerName) const;
444  // Returns zero if not found. Can use long or short name, but find
445  // is done on short name.
446 
447  static Verbosity GetVerbosityValue(const G4String&);
448  // Returns verbosity given a string. (Uses first character only.)
449 
451  // Returns verbosity given an integer. If integer is out of range,
452  // selects verbosity at extreme of range.
453 
455  // Converts the verbosity into a string for suitable for printing.
456 
457  static std::vector<G4String> VerbosityGuidanceStrings;
458  // Guidance on the use of visualization verbosity.
459 
460 protected:
461 
462  virtual void RegisterGraphicsSystems () = 0;
463  // The sub-class must implement and make successive calls to
464  // RegisterGraphicsSystem.
465 
466  virtual void RegisterModelFactories();
467  // Sub-class must register desired models
468 
469  void RegisterMessengers (); // Command messengers.
470 
472  // fVerbose is kept for backwards compatibility for some user
473  // examples. (It is used in the derived user vis managers to print
474  // available graphics systems.) It is initialised to 1 in the
475  // constructor and cannot be changed.
476 
478 
479 private:
480 
481  // Function templates to implement the Draw methods (to avoid source
482  // code duplication).
483  template <class T> void DrawT
484  (const T& graphics_primitive, const G4Transform3D& objectTransform);
485  template <class T> void DrawT2D
486  (const T& graphics_primitive, const G4Transform3D& objectTransform);
487 
488  void PrintAvailableModels (Verbosity) const;
489  void PrintAvailableColours (Verbosity) const;
490  void PrintAvailableUserVisActions (Verbosity) const;
491  void PrintInvalidPointers () const;
492  G4bool IsValidView ();
493  // True if view is valid. Prints messages and sanitises various data.
494  void ClearTransientStoreIfMarked();
495  // Clears transient store of current scene handler if it is marked
496  // for clearing. Assumes view is valid.
497 
498  static G4VisManager* fpInstance; // Pointer to single instance.
499  G4bool fInitialised;
500  std::vector<UserVisAction> fRunDurationUserVisActions;
501  std::vector<UserVisAction> fEndOfEventUserVisActions;
502  std::vector<UserVisAction> fEndOfRunUserVisActions;
503  std::map<G4VUserVisAction*,G4VisExtent> fUserVisActionExtents;
504  G4VGraphicsSystem* fpGraphicsSystem; // Current graphics system.
505  G4Scene* fpScene; // Current scene.
506  G4VSceneHandler* fpSceneHandler; // Current scene handler.
507  G4VViewer* fpViewer; // Current viewer.
508  G4GraphicsSystemList fAvailableGraphicsSystems;
509  G4SceneList fSceneList;
510  G4SceneHandlerList fAvailableSceneHandlers;
511  static Verbosity fVerbosity;
512  std::vector<G4UImessenger*> fMessengerList;
513  std::vector<G4UIcommand*> fDirectoryList;
514  G4VisStateDependent* fpStateDependent; // Friend state dependent class.
515  G4bool fEventRefreshing;
516  G4bool fTransientsDrawnThisRun;
517  G4bool fTransientsDrawnThisEvent;
518  G4int fNoOfEventsDrawnThisRun;
519  G4int fNKeepRequests;
520  G4bool fEventKeepingSuspended;
521  G4bool fKeptLastEvent;
522  const G4Event* fpRequestedEvent; // If non-zero, scene handler uses.
523  G4bool fAbortReviewKeptEvents;
524  G4ViewParameters fDefaultViewParameters;
525  G4bool fIsDrawGroup;
526  G4int fDrawGroupNestingDepth;
527  G4bool fIgnoreStateChanges;
528 #ifdef G4MULTITHREADED
529  G4int fMaxEventQueueSize;
530  G4bool fWaitOnEventQueueFull;
531 #endif
532 
533  // Trajectory draw model manager
534  G4VisModelManager<G4VTrajectoryModel>* fpTrajDrawModelMgr;
535 
536  // Trajectory filter model manager
537  G4VisFilterManager<G4VTrajectory>* fpTrajFilterMgr;
538 
539  // Hit filter model manager
540  G4VisFilterManager<G4VHit>* fpHitFilterMgr;
541 
542  // Digi filter model manager
543  G4VisFilterManager<G4VDigi>* fpDigiFilterMgr;
544 };
545 
546 #include "G4VisManager.icc"
547 
548 #endif
G4bool GetTransientsDrawnThisRun() const
G4bool FilterDigi(const G4VDigi &)
const XML_Char * name
Definition: expat.h:151
void Initialise()
void RegisterEndOfEventUserVisAction(const G4String &name, G4VUserVisAction *, const G4VisExtent &=G4VisExtent::NullExtent)
void RegisterModelFactory(G4TrajDrawModelFactory *factory)
Definition: G4Text.hh:73
void SetTransientsDrawnThisRun(G4bool)
static const G4VisExtent NullExtent
Definition: G4VisExtent.hh:80
friend std::ostream & operator<<(std::ostream &, const G4VGraphicsSystem &)
virtual void RegisterModelFactories()
void SetCurrentSceneHandler(G4VSceneHandler *)
void PrintAvailableGraphicsSystems(Verbosity) const
void SetEventRefreshing(G4bool)
const G4int fVerbose
void RegisterEndOfRunUserVisAction(const G4String &name, G4VUserVisAction *, const G4VisExtent &=G4VisExtent::NullExtent)
void SetVerboseLevel(G4int)
void SetRequestedEvent(const G4Event *)
G4VModelFactory< G4VFilter< G4VDigi > > G4DigiFilterFactory
void GeometryHasChanged()
G4VGraphicsSystem * GetCurrentGraphicsSystem() const
Definition: G4VHit.hh:48
void SetCurrentScene(G4Scene *)
void SetUserActionExtent(const G4VisExtent &)
static std::vector< G4String > VerbosityGuidanceStrings
int G4int
Definition: G4Types.hh:78
static Verbosity GetVerbosityValue(const G4String &)
void EndDraw2D()
void BeginDraw(const G4Transform3D &objectTransformation=G4Transform3D())
const G4VTrajectoryModel * CurrentTrajDrawModel() const
void SetDefaultViewParameters(const G4ViewParameters &)
void RegisterMessengers()
const G4SceneHandlerList & GetAvailableSceneHandlers() const
G4VModelFactory< G4VFilter< G4VHit > > G4HitFilterFactory
void RegisterMessenger(G4UImessenger *messenger)
void SelectTrajectoryModel(const G4String &model)
G4VModelFactory< G4VFilter< G4VTrajectory > > G4TrajFilterFactory
const G4ViewParameters & GetDefaultViewParameters() const
G4SceneList & SetSceneList()
void IgnoreStateChanges(G4bool)
G4String ViewerShortName(const G4String &viewerName) const
bool G4bool
Definition: G4Types.hh:79
G4bool FilterTrajectory(const G4VTrajectory &)
void ResetTransientsDrawnFlags()
static G4VisManager * GetInstance()
const G4SceneList & GetSceneList() const
const std::vector< UserVisAction > & GetEndOfEventUserVisActions() const
G4SceneHandlerList & SetAvailableSceneHandlers()
void SetCurrentGraphicsSystem(G4VGraphicsSystem *)
HepGeom::Transform3D G4Transform3D
const std::map< G4VUserVisAction *, G4VisExtent > & GetUserVisActionExtents() const
virtual ~G4VisManager()
G4VUserVisAction * fpUserVisAction
const std::vector< UserVisAction > & GetRunDurationUserVisActions() const
G4bool GetTransientsDrawnThisEvent() const
void Initialize()
void RegisterRunDurationUserVisAction(const G4String &name, G4VUserVisAction *, const G4VisExtent &=G4VisExtent::NullExtent)
void SetCurrentViewer(G4VViewer *)
void SetTransientsDrawnThisEvent(G4bool)
G4VisManager(const G4String &verbosityString="warnings")
Definition: G4VisManager.cc:99
G4bool FilterHit(const G4VHit &)
void SetUserAction(G4VUserVisAction *pVisAction, const G4VisExtent &=G4VisExtent::NullExtent)
G4VViewer * GetViewer(const G4String &viewerName) const
void BeginDraw2D(const G4Transform3D &objectTransformation=G4Transform3D())
G4VModelFactory< G4VTrajectoryModel > G4TrajDrawModelFactory
static G4String VerbosityString(Verbosity)
G4VSceneHandler * GetCurrentSceneHandler() const
UserVisAction(const G4String &name, G4VUserVisAction *pUserVisAction)
static Verbosity GetVerbosity()
const G4Event * GetRequestedEvent() const
void Draw(const G4Circle &, const G4Transform3D &objectTransformation=G4Transform3D())
virtual void RegisterGraphicsSystems()=0
void RegisterModel(G4VTrajectoryModel *model)
G4bool RegisterGraphicsSystem(G4VGraphicsSystem *)
G4VViewer * GetCurrentViewer() const
G4bool GetAbortReviewKeptEvents() const
const std::vector< UserVisAction > & GetEndOfRunUserVisActions() const
void DispatchToModel(const G4VTrajectory &)
void CreateViewer(const G4String &name="", const G4String &XGeometry="")
void CreateSceneHandler(const G4String &name="")
const G4GraphicsSystemList & GetAvailableGraphicsSystems()
const XML_Char XML_Content * model
Definition: expat.h:151
void NotifyHandlers()
void Draw2D(const G4Circle &, const G4Transform3D &objectTransformation=G4Transform3D())
void * G4ThreadFunArgType
Definition: G4Threading.hh:185
G4Scene * GetCurrentScene() const
void * G4ThreadFunReturnType
Definition: G4Threading.hh:184
void SetAbortReviewKeptEvents(G4bool)