Geant4 Cross Reference |
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 // 28 // 29 30 // Class Description: 31 // 32 // The GEANT4 Visualization Manager - John Allison 02/Jan/1996. 33 // 34 // G4VisManager is a "Singleton", i.e., only one instance of it or any 35 // derived class may exist. A G4Exception is thrown if an attempt is 36 // made to instantiate more than one. 37 // 38 // It is also an abstract class, so the user must derive his/her own 39 // class from G4VisManager, implement the pure virtual function 40 // RegisterGraphicsSystems, and instantiate an object of the derived 41 // class - for an example see 42 // visualization/include/G4VisExecutive.hh/icc. 43 // 44 // The recommended way for users to obtain a pointer to the vis 45 // manager is with G4VVisManager::GetConcreteInstance (), being always 46 // careful to test for non-zero. This pointer is non-zero only when 47 // (a) an object of the derived class exists and (b) when there is a 48 // valid viewer available. 49 // 50 // Graphics system registration is normally done through the protected 51 // pure virtual function RegisterGraphicsSystems called from 52 // Initialise (). You can also use the public function 53 // RegisterGraphicsSystem (new MyGraphicsSystem) if you have your own 54 // graphics system. A graphics system is, in effect, a factory for 55 // scene handlers and viewers. 56 // 57 // The VisManager creates and manages graphics systems, scenes, scene 58 // handlers, viewers and some models and model makers. You can have 59 // any number. It has the concept of a "current viewer", and the 60 // "current scene handler", the "current scene" and the "current 61 // graphics system" which go with it. You can select the current 62 // viewer. Most of the the operations of the VisManager take place 63 // with the current viewer, in particular, the Draw operations. 64 // 65 // Each scene comprises drawable objects such as detector components 66 // and trajectories, hits and digis when appropriate. A scene handler 67 // translates a scene into graphics-system-specific function calls 68 // and, possibly, a graphics-system-dependent database - display 69 // lists, scene graphs, etc. Each viewer has its "view parameters" 70 // (see class description of G4ViewParameters for available parameters 71 // and also for a description of the concept of a "standard view" and 72 // all that). 73 // 74 // A friend class G4VisStateDependent is "state dependent", i.e., it 75 // is notified on change of state (G4ApplicationState). This is used 76 // to message the G4VisManager to draw hits, digis and trajectories in 77 // the current scene at the end of event, as required. 78 79 #ifndef G4VISMANAGER_HH 80 #define G4VISMANAGER_HH 81 82 // Temporary definition until Xeon Phi can handle full C++11. 83 #ifndef __MIC__ 84 #define G4VIS_USE_STD11 85 #endif 86 87 #include "G4VVisManager.hh" 88 89 #include "globals.hh" 90 #include "G4GraphicsSystemList.hh" 91 #include "G4ModelingParameters.hh" 92 #include "G4NullModel.hh" 93 #include "G4SceneHandlerList.hh" 94 #include "G4SceneList.hh" 95 #include "G4TrajectoriesModel.hh" 96 #include "G4Transform3D.hh" 97 #include "G4UImessenger.hh" 98 99 #include <iostream> 100 #include <vector> 101 #include <map> 102 103 #include "G4Threading.hh" 104 105 class G4Scene; 106 class G4UIcommand; 107 class G4UImessenger; 108 class G4VisStateDependent; 109 class G4VTrajectoryModel; 110 class G4VUserVisAction; 111 template <typename> class G4VFilter; 112 template <typename> class G4VisFilterManager; 113 template <typename> class G4VisModelManager; 114 template <typename> class G4VModelFactory; 115 class G4Event; 116 117 // Useful typedef's 118 typedef G4VModelFactory<G4VTrajectoryModel> G4TrajDrawModelFactory; 119 typedef G4VModelFactory<G4VFilter<G4VTrajectory> > G4TrajFilterFactory; 120 typedef G4VModelFactory<G4VFilter<G4VHit> > G4HitFilterFactory; 121 typedef G4VModelFactory<G4VFilter<G4VDigi> > G4DigiFilterFactory; 122 123 class G4VisManager: public G4VVisManager { 124 125 // Management friends... 126 friend class G4VSceneHandler; 127 friend class G4VViewer; 128 friend class G4VisStateDependent; 129 friend class G4VisCommandList; 130 131 // operator << friends... 132 friend std::ostream& operator << (std::ostream&, const G4VGraphicsSystem&); 133 friend std::ostream& operator << (std::ostream&, const G4VSceneHandler&); 134 135 public: // With description 136 137 enum Verbosity { 138 quiet, // Nothing is printed. 139 startup, // Startup and endup messages are printed... 140 errors, // ...and errors... 141 warnings, // ...and warnings... 142 confirmations, // ...and confirming messages... 143 parameters, // ...and parameters of scenes and views... 144 all // ...and everything available. 145 }; 146 // Simple graded message scheme. 147 148 protected: // With description 149 150 G4VisManager (const G4String& verbosityString = "warnings"); 151 // The constructor is protected so that an object of the derived 152 // class may be constructed. 153 154 public: // With description 155 156 virtual ~G4VisManager (); 157 158 private: 159 160 // Private copy constructor and assigment operator - copying and 161 // assignment not allowed. Keeps CodeWizard happy. 162 G4VisManager (const G4VisManager&); 163 G4VisManager& operator = (const G4VisManager&); 164 165 public: 166 static G4VisManager* GetInstance (); 167 // Returns pointer to itself. Throws a G4Exception if called before 168 // instantiation. Intended only for use within the vis category; the 169 // normal user should instead use G4VVisManager::GetConcreteInstance() 170 // to get a "higher level" pointer for general use - but always test 171 // for non-zero. 172 173 public: // With description 174 175 void Initialise (); 176 void Initialize (); // Alias Initialise () 177 178 // Optional registration of user vis actions. Added to scene with 179 // /vis/scene/add/userAction. 180 void RegisterRunDurationUserVisAction 181 (const G4String& name, G4VUserVisAction*, 182 const G4VisExtent& = G4VisExtent()); 183 void RegisterEndOfEventUserVisAction 184 (const G4String& name, G4VUserVisAction*, 185 const G4VisExtent& = G4VisExtent()); 186 void RegisterEndOfRunUserVisAction 187 (const G4String& name, G4VUserVisAction*, 188 const G4VisExtent& = G4VisExtent()); 189 190 G4bool RegisterGraphicsSystem (G4VGraphicsSystem*); 191 // Register an individual graphics system. Normally this is done in 192 // a sub-class implementation of the protected virtual function, 193 // RegisterGraphicsSystems. See, e.g., G4VisExecutive.icc. 194 195 void RegisterModelFactory(G4TrajDrawModelFactory* factory); 196 // Register trajectory draw model factory. Assumes ownership of factory. 197 198 void RegisterModel(G4VTrajectoryModel* model); 199 // Register trajectory model. Assumes ownership of model. 200 201 void RegisterModelFactory(G4TrajFilterFactory* factory); 202 // Register trajectory filter model factory. Assumes ownership of factory. 203 204 void RegisterModel(G4VFilter<G4VTrajectory>* filter); 205 // Register trajectory filter model. Assumes ownership of model. 206 207 void RegisterModelFactory(G4HitFilterFactory* factory); 208 // Register trajectory hit model factory. Assumes ownership of factory. 209 210 void RegisterModel(G4VFilter<G4VHit>* filter); 211 // Register trajectory hit model. Assumes ownership of model. 212 213 void RegisterModelFactory(G4DigiFilterFactory* factory); 214 // Register trajectory digi model factory. Assumes ownership of factory. 215 216 void RegisterModel(G4VFilter<G4VDigi>* filter); 217 // Register trajectory digi model. Assumes ownership of model. 218 219 void SelectTrajectoryModel(const G4String& model); 220 // Set default trajectory model. Useful for use in compiled code 221 222 void RegisterMessenger(G4UImessenger* messenger); 223 // Register messenger. Assumes ownership of messenger. 224 225 ///////////////////////////////////////////////////////////////// 226 // Now functions that implement the pure virtual functions of 227 // G4VVisManager for drawing various visualization primitives, useful 228 // for representing hits, digis, etc. 229 230 void Draw (const G4Circle&, 231 const G4Transform3D& objectTransformation = G4Transform3D()); 232 233 void Draw (const G4Polyhedron&, 234 const G4Transform3D& objectTransformation = G4Transform3D()); 235 236 void Draw (const G4Polyline&, 237 const G4Transform3D& objectTransformation = G4Transform3D()); 238 239 void Draw (const G4Polymarker&, 240 const G4Transform3D& objectTransformation = G4Transform3D()); 241 242 void Draw (const G4Square&, 243 const G4Transform3D& objectTransformation = G4Transform3D()); 244 245 void Draw (const G4Text&, 246 const G4Transform3D& objectTransformation = G4Transform3D()); 247 248 void Draw2D (const G4Circle&, 249 const G4Transform3D& objectTransformation = G4Transform3D()); 250 251 void Draw2D (const G4Polyhedron&, 252 const G4Transform3D& objectTransformation = G4Transform3D()); 253 254 void Draw2D (const G4Polyline&, 255 const G4Transform3D& objectTransformation = G4Transform3D()); 256 257 void Draw2D (const G4Polymarker&, 258 const G4Transform3D& objectTransformation = G4Transform3D()); 259 260 void Draw2D (const G4Square&, 261 const G4Transform3D& objectTransformation = G4Transform3D()); 262 263 void Draw2D (const G4Text&, 264 const G4Transform3D& objectTransformation = G4Transform3D()); 265 266 //////////////////////////////////////////////////////////////////// 267 // Now functions that implement the pure virtual functions of 268 // G4VVisManager for drawing a GEANT4 object. Note that the 269 // visualization attributes needed in some cases override any 270 // visualization attributes that are associated with the object 271 // itself - thus you can, for example, change the colour of a 272 // physical volume. 273 274 void Draw (const G4VTrajectory&); 275 276 void Draw (const G4VHit&); 277 278 void Draw (const G4VDigi&); 279 280 void Draw (const G4LogicalVolume&, const G4VisAttributes&, 281 const G4Transform3D& objectTransformation = G4Transform3D()); 282 283 void Draw (const G4VPhysicalVolume&, const G4VisAttributes&, 284 const G4Transform3D& objectTransformation = G4Transform3D()); 285 286 void Draw (const G4VSolid&, const G4VisAttributes&, 287 const G4Transform3D& objectTransformation = G4Transform3D()); 288 289 void DrawGeometry 290 (G4VPhysicalVolume*, const G4Transform3D& t = G4Transform3D()); 291 // Draws a geometry tree starting at the specified physical volume. 292 293 ////////////////////////////////////////////////////////////////////// 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 310 //////////////////////////////////////////////////////////////////////// 311 // Now other pure virtual functions of G4VVisManager... 312 313 void GeometryHasChanged (); 314 // Used by run manager to notify change. 315 316 void IgnoreStateChanges(G4bool); 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 332 G4bool FilterTrajectory(const G4VTrajectory&); 333 G4bool FilterHit(const G4VHit&); 334 G4bool FilterDigi(const G4VDigi&); 335 336 virtual void SetUpForAThread(); 337 // This method is invoked by G4WorkerRunManager 338 339 virtual void EventReadyForVis(const G4Event*); 340 // This is invoked by G4SubEvtRunManager. 341 // The event is passed to EndOfEventKernel. 342 343 static G4ThreadFunReturnType G4VisSubThread(G4ThreadFunArgType); 344 // Vis sub-thread function. 345 346 347 //////////////////////////////////////////////////////////////////////// 348 // Administration routines. 349 350 void CreateSceneHandler (const G4String& name = ""); 351 // Creates scene handler for the current system. 352 353 void CreateViewer (const G4String& name = "", const G4String& XGeometry = ""); 354 // Creates viewer for the current scene handler. 355 356 private: 357 358 void BeginOfRun (); 359 360 void BeginOfEvent (); 361 362 void EndOfEvent (); 363 void EndOfEventKernel (const G4Event* currentEvent); 364 void EndOfEventCleanup (const G4Event* currentEvent); 365 G4bool RequiredToBeKeptForVis (G4int eventID); 366 // Cluster of methods to handle end of event. 367 368 void EndOfRun (); 369 370 public: // With description 371 372 ///////////////////////////////////////////////////////////////////// 373 // Access functions. 374 375 void Enable(); 376 void Disable(); 377 G4bool IsEnabled() const; 378 // Global enable/disable functions. 379 380 const G4VTrajectoryModel* CurrentTrajDrawModel() const; 381 382 struct UserVisAction { 383 UserVisAction(const G4String& name, G4VUserVisAction* pUserVisAction) 384 :fName(name), fpUserVisAction(pUserVisAction) {} 385 G4String fName; 386 G4VUserVisAction* fpUserVisAction; 387 }; 388 const std::vector<UserVisAction>& GetRunDurationUserVisActions () const; 389 const std::vector<UserVisAction>& GetEndOfEventUserVisActions () const; 390 const std::vector<UserVisAction>& GetEndOfRunUserVisActions () const; 391 const std::map<G4VUserVisAction*,G4VisExtent>& GetUserVisActionExtents () const; 392 G4VGraphicsSystem* GetCurrentGraphicsSystem () const; 393 G4Scene* GetCurrentScene () const; 394 G4VSceneHandler* GetCurrentSceneHandler () const; 395 G4VViewer* GetCurrentViewer () const; 396 const G4GraphicsSystemList& GetAvailableGraphicsSystems (); 397 // The above is non-const because it checks and updates the List by 398 // calling RegisterGraphicsSystems() if no graphics systems are 399 // already registered. 400 const G4SceneHandlerList& GetAvailableSceneHandlers () const; 401 const G4SceneList& GetSceneList () const; 402 static Verbosity GetVerbosity (); 403 G4bool GetTransientsDrawnThisRun () const; 404 G4bool GetTransientsDrawnThisEvent () const; 405 G4bool GetDrawEventOnlyIfToBeKept () const; 406 const G4Event* GetRequestedEvent () const; 407 G4int GetNKeepForPostProcessingRequests () const; 408 G4int GetNKeepTheEventRequests () const; 409 G4int GetNKeepRequests () const; 410 G4bool GetReviewingKeptEvents () const; 411 G4bool GetAbortReviewKeptEvents () const; 412 G4bool GetReviewingPlots () const; 413 G4bool GetAbortReviewPlots () const; 414 const G4ViewParameters& GetDefaultViewParameters () const; 415 G4int GetMaxEventQueueSize () const; 416 G4bool GetWaitOnEventQueueFull () const; 417 virtual const G4String& GetDefaultGraphicsSystemName(); 418 // The above has to be virtual so that the derived class, G4VisExecutive, 419 // can override and non-const because on first pass it may/should 420 // determine these defaults. 421 const G4String& GetDefaultXGeometryString () const; 422 const G4String& GetDefaultGraphicsSystemBasis() const; 423 const G4String& GetDefaultXGeometryStringBasis() const; 424 425 void SetCurrentGraphicsSystem (G4VGraphicsSystem*); 426 void SetCurrentScene (G4Scene*); 427 void SetCurrentSceneHandler (G4VSceneHandler*); 428 void SetCurrentViewer (G4VViewer*); 429 G4SceneHandlerList& SetAvailableSceneHandlers (); // Returns lvalue. 430 G4SceneList& SetSceneList (); // Returns lvalue. 431 void SetVerboseLevel (G4int); 432 void SetVerboseLevel (const G4String&); 433 void SetVerboseLevel (Verbosity); 434 void SetEventRefreshing (G4bool); 435 void ResetTransientsDrawnFlags (); 436 void SetTransientsDrawnThisRun (G4bool); 437 void SetTransientsDrawnThisEvent (G4bool); 438 void SetDrawEventOnlyIfToBeKept (G4bool); 439 // If non-zero, requested event is used in G4VSceneHandler::ProcessScene. 440 void SetRequestedEvent (const G4Event*); 441 void SetReviewingKeptEvents (G4bool); 442 void SetAbortReviewKeptEvents (G4bool); 443 void SetReviewingPlots (G4bool); 444 void SetAbortReviewPlots (G4bool); 445 void SetDefaultViewParameters (const G4ViewParameters&); 446 void SetMaxEventQueueSize (G4int); 447 void SetWaitOnEventQueueFull (G4bool); 448 void SetDefaultGraphicsSystemName(const G4String&); 449 void SetDefaultXGeometryString (const G4String&); 450 void SetDefaultGraphicsSystemBasis(const G4String&); 451 void SetDefaultXGeometryStringBasis(const G4String&); 452 453 ///////////////////////////////////////////////////////////////////// 454 // Utility functions. 455 456 G4String ViewerShortName (const G4String& viewerName) const; 457 // Returns shortened version of viewer name, i.e., up to first space, 458 // if any. 459 460 G4VViewer* GetViewer (const G4String& viewerName) const; 461 // Returns zero if not found. Can use long or short name, but find 462 // is done on short name. 463 464 static Verbosity GetVerbosityValue(const G4String&); 465 // Returns verbosity given a string. (Uses first character only.) 466 467 static Verbosity GetVerbosityValue(G4int); 468 // Returns verbosity given an integer. If integer is out of range, 469 // selects verbosity at extreme of range. 470 471 static G4String VerbosityString(Verbosity); 472 // Converts the verbosity into a string for suitable for printing. 473 474 static std::vector<G4String> VerbosityGuidanceStrings; 475 // Guidance on the use of visualization verbosity. 476 477 static void PrintAvailableVerbosity (std::ostream& os); 478 479 void PrintAvailableGraphicsSystems (Verbosity, std::ostream& = G4cout) const; 480 481 protected: 482 483 virtual void RegisterGraphicsSystems () = 0; 484 // The sub-class must implement and make successive calls to 485 // RegisterGraphicsSystem. 486 487 virtual void RegisterModelFactories(); 488 // Sub-class must register desired models 489 490 void RegisterMessengers (); // Command messengers. 491 492 const G4int fVerbose; 493 // No longer used. Use fVerbosity and access functions instead. 494 // fVerbose is kept for backwards compatibility for some user 495 // examples. (It is used in the derived user vis managers to print 496 // available graphics systems.) It is initialised to 1 in the 497 // constructor and cannot be changed. 498 static Verbosity fVerbosity; 499 500 G4String fDefaultGraphicsSystemName; 501 G4String fDefaultXGeometryString; 502 G4String fDefaultGraphicsSystemBasis; 503 G4String fDefaultXGeometryStringBasis; 504 505 private: 506 507 // Function templates to implement the Draw methods (to avoid source 508 // code duplication). 509 template <class T> void DrawT 510 (const T& graphics_primitive, const G4Transform3D& objectTransform); 511 template <class T> void DrawT2D 512 (const T& graphics_primitive, const G4Transform3D& objectTransform); 513 514 void PrintAvailableModels (Verbosity) const; 515 void InitialiseG4ColourMap () const; 516 void PrintAvailableColours (Verbosity) const; 517 void PrintAvailableUserVisActions (Verbosity) const; 518 void PrintInvalidPointers () const; 519 G4bool IsValidView (); 520 // True if view is valid. Prints messages and sanitises various data. 521 void ClearTransientStoreIfMarked(); 522 // Clears transient store of current scene handler if it is marked 523 // for clearing. Assumes view is valid. 524 525 static G4VisManager* fpInstance; // Pointer to single instance. 526 G4bool fInitialised; 527 std::vector<UserVisAction> fRunDurationUserVisActions; 528 std::vector<UserVisAction> fEndOfEventUserVisActions; 529 std::vector<UserVisAction> fEndOfRunUserVisActions; 530 std::map<G4VUserVisAction*,G4VisExtent> fUserVisActionExtents; 531 G4VGraphicsSystem* fpGraphicsSystem; // Current graphics system. 532 G4Scene* fpScene; // Current scene. 533 G4VSceneHandler* fpSceneHandler; // Current scene handler. 534 G4VViewer* fpViewer; // Current viewer. 535 G4GraphicsSystemList fAvailableGraphicsSystems; 536 G4SceneList fSceneList; 537 G4SceneHandlerList fAvailableSceneHandlers; 538 std::vector<G4UImessenger*> fMessengerList; 539 std::vector<G4UIcommand*> fDirectoryList; 540 G4VisStateDependent* fpStateDependent; // Friend state dependent class. 541 G4bool fEventRefreshing; 542 G4bool fTransientsDrawnThisRun; 543 G4bool fTransientsDrawnThisEvent; 544 G4int fNoOfEventsDrawnThisRun; 545 G4int fNKeepForPostProcessingRequests; 546 G4int fNKeepTheEventRequests; 547 G4bool fEventKeepingSuspended; 548 G4bool fDrawEventOnlyIfToBeKept; 549 const G4Event* fpRequestedEvent; // If non-zero, scene handler uses. 550 G4bool fReviewingKeptEvents; 551 G4bool fAbortReviewKeptEvents; 552 G4bool fReviewingPlots; 553 G4bool fAbortReviewPlots; 554 G4ViewParameters fDefaultViewParameters; 555 G4bool fIsDrawGroup; 556 G4int fDrawGroupNestingDepth; 557 G4bool fIgnoreStateChanges; 558 G4int fMaxEventQueueSize; 559 G4bool fWaitOnEventQueueFull; 560 561 // Trajectory draw model manager 562 G4VisModelManager<G4VTrajectoryModel>* fpTrajDrawModelMgr; 563 564 // Trajectory filter model manager 565 G4VisFilterManager<G4VTrajectory>* fpTrajFilterMgr; 566 567 // Hit filter model manager 568 G4VisFilterManager<G4VHit>* fpHitFilterMgr; 569 570 // Digi filter model manager 571 G4VisFilterManager<G4VDigi>* fpDigiFilterMgr; 572 }; 573 574 #include "G4VisManager.icc" 575 576 #endif 577