Geant4  9.6.p02
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
G4VisManager.cc
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 // $Id$
27 //
28 //
29 // GEANT4 Visualization Manager - John Allison 02/Jan/1996.
30 
31 #include "G4VisManager.hh"
32 
33 #include "G4VisCommands.hh"
34 #include "G4VisCommandsCompound.hh"
35 #include "G4VisCommandsGeometry.hh"
37 #include "G4VisCommandsSet.hh"
38 #include "G4VisCommandsScene.hh"
39 #include "G4VisCommandsSceneAdd.hh"
42 #include "G4VisCommandsViewer.hh"
45 #include "G4UImanager.hh"
46 #include "G4VisStateDependent.hh"
47 #include "G4UIdirectory.hh"
48 #include "G4VGraphicsSystem.hh"
49 #include "G4VSceneHandler.hh"
50 #include "G4VViewer.hh"
51 #include "G4VPhysicalVolume.hh"
52 #include "G4LogicalVolume.hh"
53 #include "G4VSolid.hh"
54 #include "G4Vector3D.hh"
55 #include "G4Point3D.hh"
56 #include "G4RotationMatrix.hh"
57 #include "G4Polyline.hh"
58 #include "G4Polyhedron.hh"
59 #include "G4NURBS.hh"
60 #include "G4NullModel.hh"
61 #include "G4ModelingParameters.hh"
65 #include "G4VisModelManager.hh"
66 #include "G4VModelFactory.hh"
67 #include "G4VisFilterManager.hh"
68 #include "G4VTrajectoryModel.hh"
70 #include "Randomize.hh"
71 #include "G4RunManager.hh"
72 #include "G4EventManager.hh"
73 #include "G4Run.hh"
74 #include "G4Event.hh"
75 #include <map>
76 #include <set>
77 #include <vector>
78 #include <sstream>
79 
80 G4VisManager* G4VisManager::fpInstance = 0;
81 
82 G4VisManager::Verbosity G4VisManager::fVerbosity = G4VisManager::warnings;
83 
84 G4VisManager::G4VisManager (const G4String& verbosityString):
85  fVerbose (1),
86  fInitialised (false),
87  fpGraphicsSystem (0),
88  fpScene (0),
89  fpSceneHandler (0),
90  fpViewer (0),
91  fpStateDependent (0),
92  fEventRefreshing (false),
93  fTransientsDrawnThisRun (false),
94  fTransientsDrawnThisEvent (false),
95  fEventKeepingSuspended (false),
96  fKeptLastEvent (false),
97  fpRequestedEvent (0),
98  fAbortReviewKeptEvents (false),
99  fIsDrawGroup (false),
100  fDrawGroupNestingDepth (0)
101  // All other objects use default constructors.
102 {
103  fpTrajDrawModelMgr = new G4VisModelManager<G4VTrajectoryModel>("/vis/modeling/trajectories");
104  fpTrajFilterMgr = new G4VisFilterManager<G4VTrajectory>("/vis/filtering/trajectories");
105  fpHitFilterMgr = new G4VisFilterManager<G4VHit>("/vis/filtering/hits");
106  fpDigiFilterMgr = new G4VisFilterManager<G4VDigi>("/vis/filtering/digi");
107 
108  VerbosityGuidanceStrings.push_back
109  ("Simple graded message scheme - digit or string (1st character defines):");
110  VerbosityGuidanceStrings.push_back
111  (" 0) quiet, // Nothing is printed.");
112  VerbosityGuidanceStrings.push_back
113  (" 1) startup, // Startup and endup messages are printed...");
114  VerbosityGuidanceStrings.push_back
115  (" 2) errors, // ...and errors...");
116  VerbosityGuidanceStrings.push_back
117  (" 3) warnings, // ...and warnings...");
118  VerbosityGuidanceStrings.push_back
119  (" 4) confirmations, // ...and confirming messages...");
120  VerbosityGuidanceStrings.push_back
121  (" 5) parameters, // ...and parameters of scenes and views...");
122  VerbosityGuidanceStrings.push_back
123  (" 6) all // ...and everything available.");
124 
125  if (fpInstance) {
127  ("G4VisManager::G4VisManager",
128  "visman0001", FatalException,
129  "Attempt to Construct more than one VisManager");
130  }
131 
132  fpInstance = this;
133  SetConcreteInstance(this);
134 
135  fpStateDependent = new G4VisStateDependent (this);
136  // No need to delete this; G4StateManager does this.
137 
138  fVerbosity = GetVerbosityValue(verbosityString);
139  if (fVerbosity >= startup) {
140  G4cout
141  << "Visualization Manager instantiating with verbosity \""
142  << VerbosityString(fVerbosity)
143  << "\"..." << G4endl;
144  }
145 
146  // Note: The specific graphics systems must be instantiated in a
147  // higher level library to avoid circular dependencies. Also,
148  // some specifically need additional external libararies that the
149  // user must supply. Therefore we ask the user to implement
150  // RegisterGraphicsSystems() and RegisterModelFactories()
151  // in a subclass. We have to wait for the subclass to instantiate
152  // so RegisterGraphicsSystems() cannot be called from this
153  // constructor; it is called from Initialise(). So we ask the
154  // user:
155  // (a) to write a subclass and implement RegisterGraphicsSystems()
156  // and RegisterModelFactories(). See
157  // visualization/include/G4VisExecutive.hh/icc as an example.
158  // (b) instantiate the subclass.
159  // (c) invoke the Initialise() method of the subclass.
160  // For example:
161  // ...
162  // #ifdef G4VIS_USE
163  // // Instantiate and initialise Visualization Manager.
164  // G4VisManager* visManager = new G4VisExecutive;
165  // visManager -> SetVerboseLevel (Verbose);
166  // visManager -> Initialise ();
167  // #endif
168  // // (Don't forget to delete visManager;)
169  // ...
170 
171  // Make top level command directory...
172  G4UIcommand* directory;
173  directory = new G4UIdirectory ("/vis/");
174  directory -> SetGuidance ("Visualization commands.");
175  fDirectoryList.push_back (directory);
176 
177  // Instantiate *basic* top level commands so that they can be used
178  // immediately after instantiation of the vis manager. Other top
179  // level and lower level commands are instantiated later in
180  // RegisterMessengers.
181  G4VVisCommand::SetVisManager (this); // Sets shared pointer
184 }
185 
187  fpInstance = 0;
188  size_t i;
189  for (i = 0; i < fSceneList.size (); ++i) {
190  delete fSceneList[i];
191  }
192  for (i = 0; i < fAvailableSceneHandlers.size (); ++i) {
193  if (fAvailableSceneHandlers[i] != NULL) {
194  delete fAvailableSceneHandlers[i];
195  }
196  }
197  for (i = 0; i < fAvailableGraphicsSystems.size (); ++i) {
198  if (fAvailableGraphicsSystems[i]) {
199  delete fAvailableGraphicsSystems[i];
200  }
201  }
202  if (fVerbosity >= startup) {
203  G4cout << "Graphics systems deleted." << G4endl;
204  G4cout << "Visualization Manager deleting..." << G4endl;
205  }
206  for (i = 0; i < fMessengerList.size (); ++i) {
207  delete fMessengerList[i];
208  }
209  for (i = 0; i < fDirectoryList.size (); ++i) {
210  delete fDirectoryList[i];
211  }
212 
213  delete fpDigiFilterMgr;
214  delete fpHitFilterMgr;
215  delete fpTrajFilterMgr;
216  delete fpTrajDrawModelMgr;
217 }
218 
219 G4VisManager* G4VisManager::GetInstance () {
220  if (!fpInstance) {
222  ("G4VisManager::GetInstance",
223  "visman0002", FatalException, "VisManager not yet instantiated");
224  }
225  return fpInstance;
226 }
227 
229 
230  if (fInitialised && fVerbosity >= warnings) {
231  G4cout << "WARNING: G4VisManager::Initialise: already initialised."
232  << G4endl;
233  return;
234  }
235 
236  if (fVerbosity >= startup) {
237  G4cout << "Visualization Manager initialising..." << G4endl;
238  }
239 
240  if (fVerbosity >= parameters) {
241  G4cout <<
242  "\nYou have instantiated your own Visualization Manager, inheriting"
243  "\n G4VisManager and implementing RegisterGraphicsSystems(), in which"
244  "\n you should, normally, instantiate drivers which do not need"
245  "\n external packages or libraries, and, optionally, drivers under"
246  "\n control of environment variables."
247  "\n Also you should implement RegisterModelFactories()."
248  "\n See visualization/include/G4VisExecutive.hh/icc, for example."
249  "\n In your main() you will have something like:"
250  "\n #ifdef G4VIS_USE"
251  "\n G4VisManager* visManager = new G4VisExecutive;"
252  "\n visManager -> SetVerboseLevel (Verbose);"
253  "\n visManager -> Initialize ();"
254  "\n #endif"
255  "\n (Don't forget to delete visManager;)"
256  "\n"
257  << G4endl;
258  }
259 
260  if (fVerbosity >= startup) {
261  G4cout << "Registering graphics systems..." << G4endl;
262  }
263 
265 
266  if (fVerbosity >= startup) {
267  G4cout <<
268  "\nYou have successfully registered the following graphics systems."
269  << G4endl;
271  G4cout << G4endl;
272  }
273 
274  // Make command directories for commands instantiated in the
275  // modeling subcategory...
276  G4UIcommand* directory;
277  directory = new G4UIdirectory ("/vis/modeling/");
278  directory -> SetGuidance ("Modeling commands.");
279  fDirectoryList.push_back (directory);
280  directory = new G4UIdirectory ("/vis/modeling/trajectories/");
281  directory -> SetGuidance ("Trajectory model commands.");
282  fDirectoryList.push_back (directory);
283  directory = new G4UIdirectory ("/vis/modeling/trajectories/create/");
284  directory -> SetGuidance ("Create trajectory models and messengers.");
285  fDirectoryList.push_back (directory);
286 
287  // Filtering command directory
288  directory = new G4UIdirectory ("/vis/filtering/");
289  directory -> SetGuidance ("Filtering commands.");
290  fDirectoryList.push_back (directory);
291  directory = new G4UIdirectory ("/vis/filtering/trajectories/");
292  directory -> SetGuidance ("Trajectory filtering commands.");
293  fDirectoryList.push_back (directory);
294  directory = new G4UIdirectory ("/vis/filtering/trajectories/create/");
295  directory -> SetGuidance ("Create trajectory filters and messengers.");
296  fDirectoryList.push_back (directory);
297  directory = new G4UIdirectory ("/vis/filtering/hits/");
298  directory -> SetGuidance ("Hit filtering commands.");
299  fDirectoryList.push_back (directory);
300  directory = new G4UIdirectory ("/vis/filtering/hits/create/");
301  directory -> SetGuidance ("Create hit filters and messengers.");
302  fDirectoryList.push_back (directory);
303  directory = new G4UIdirectory ("/vis/filtering/digi/");
304  directory -> SetGuidance ("Digi filtering commands.");
305  fDirectoryList.push_back (directory);
306  directory = new G4UIdirectory ("/vis/filtering/digi/create/");
307  directory -> SetGuidance ("Create digi filters and messengers.");
308  fDirectoryList.push_back (directory);
309 
311 
312  if (fVerbosity >= startup) {
313  G4cout << "Registering model factories..." << G4endl;
314  }
315 
317 
318  if (fVerbosity >= startup) {
319  G4cout <<
320  "\nYou have successfully registered the following model factories."
321  << G4endl;
322  PrintAvailableModels (fVerbosity);
323  G4cout << G4endl;
324  }
325 
326  if (fVerbosity >= startup) {
327  PrintAvailableUserVisActions (fVerbosity);
328  G4cout << G4endl;
329  }
330 
331  if (fVerbosity >= startup) {
332  PrintAvailableColours (fVerbosity);
333  G4cout << G4endl;
334  }
335 
336  fInitialised = true;
337 }
338 
340 
341  // Instantiate individual messengers/commands (often - but not
342  // always - one command per messenger).
343 
344  G4UIcommand* directory;
345 
346  // *Basic* top level commands were instantiated in the constructor
347  // so that they can be used immediately after instantiation of the
348  // vis manager. Other top level and lower level commands are
349  // instantiated here.
350 
351  // Other top level commands...
356 
357  // Compound commands...
363 
364  directory = new G4UIdirectory ("/vis/geometry/");
365  directory -> SetGuidance("Operations on vis attributes of Geant4 geometry.");
366  fDirectoryList.push_back (directory);
369 
370  directory = new G4UIdirectory ("/vis/geometry/set/");
371  directory -> SetGuidance("Set vis attributes of Geant4 geometry.");
372  fDirectoryList.push_back (directory);
382 
383  directory = new G4UIdirectory ("/vis/set/");
384  directory -> SetGuidance
385  ("Set quantities for use in future commands where appropriate.");
386  fDirectoryList.push_back (directory);
392 
393  directory = new G4UIdirectory ("/vis/scene/");
394  directory -> SetGuidance ("Operations on Geant4 scenes.");
395  fDirectoryList.push_back (directory);
403 
404  directory = new G4UIdirectory ("/vis/scene/add/");
405  directory -> SetGuidance ("Add model to current scene.");
406  fDirectoryList.push_back (directory);
428 
429  directory = new G4UIdirectory ("/vis/sceneHandler/");
430  directory -> SetGuidance ("Operations on Geant4 scene handlers.");
431  fDirectoryList.push_back (directory);
436 
437  directory = new G4UIdirectory ("/vis/touchable/");
438  directory -> SetGuidance ("Operations on touchables.");
439  directory = new G4UIdirectory ("/vis/touchable/set/");
440  directory -> SetGuidance ("Set vis attributes of current touchable.");
441  fDirectoryList.push_back (directory);
443 
444  directory = new G4UIdirectory ("/vis/viewer/");
445  directory -> SetGuidance ("Operations on Geant4 viewers.");
446  fDirectoryList.push_back (directory);
467 
468  directory = new G4UIdirectory ("/vis/viewer/default/");
469  directory -> SetGuidance("Set default values for future viewers.");
470  fDirectoryList.push_back (directory);
473 
474  directory = new G4UIdirectory ("/vis/viewer/set/");
475  directory -> SetGuidance ("Set view parameters of current viewer.");
476  fDirectoryList.push_back (directory);
478 
479  // List manager commands
481  (fpTrajDrawModelMgr, fpTrajDrawModelMgr->Placement()));
483  (fpTrajDrawModelMgr, fpTrajDrawModelMgr->Placement()));
484 
485  // Trajectory filter manager commands
487  (fpTrajFilterMgr, fpTrajFilterMgr->Placement()));
489  (fpTrajFilterMgr, fpTrajFilterMgr->Placement()));
490 
491  // Hit filter manager commands
493  (fpHitFilterMgr, fpHitFilterMgr->Placement()));
495  (fpHitFilterMgr, fpHitFilterMgr->Placement()));
496 
497  // Digi filter manager commands
499  (fpDigiFilterMgr, fpDigiFilterMgr->Placement()));
501  (fpDigiFilterMgr, fpDigiFilterMgr->Placement()));
502 }
503 
505  if (IsValidView ()) {
506  if (fVerbosity >= confirmations) {
507  G4cout << "G4VisManager::Enable: visualization enabled." << G4endl;
508  }
509  }
510  else {
511  if (fVerbosity >= warnings) {
512  G4cout <<
513  "G4VisManager::Enable: WARNING: visualization remains disabled for"
514  "\n above reasons. Rectifying with valid vis commands will"
515  "\n automatically enable."
516  << G4endl;
517  }
518  }
519 }
520 
523  if (fVerbosity >= confirmations) {
524  G4cout <<
525  "G4VisManager::Disable: visualization disabled."
526  "\n The pointer returned by GetConcreteInstance will be zero."
527  "\n Note that it will become enabled after some valid vis commands."
528  << G4endl;
529  }
530 }
531 
533  G4int nSystems = fAvailableGraphicsSystems.size ();
534  if (nSystems == 0) {
535  if (fVerbosity >= warnings) {
536  G4cout << "G4VisManager::GetAvailableGraphicsSystems: WARNING: no"
537  "\n graphics system available!"
538  "\n 1) Did you have environment variables G4VIS_BUILD_xxxx_DRIVER set"
539  "\n when you compiled/built the visualization code?"
540  "\n 2) Did you instantiate your own Visualization Manager and forget"
541  "\n to implement RegisterGraphicsSystems correctly?"
542  "\n 3) You can register your own graphics system, e.g.,"
543  "\n visManager->RegisterGraphicsSystem(new MyGraphicsSystem);)"
544  "\n after instantiating your vis manager and before"
545  "\n visManager->Initialize()."
546  << G4endl;
547  }
548  }
549  return fAvailableGraphicsSystems;
550 }
551 
553  G4bool happy = true;
554  if (pSystem) {
555  fAvailableGraphicsSystems.push_back (pSystem);
556  if (fVerbosity >= confirmations) {
557  G4cout << "G4VisManager::RegisterGraphicsSystem: "
558  << pSystem -> GetName ();
559  if (pSystem -> GetNickname () != "") {
560  G4cout << " (" << pSystem -> GetNickname () << ")";
561  }
562  G4cout << " registered." << G4endl;
563  }
564  }
565  else {
566  if (fVerbosity >= errors) {
567  G4cout << "G4VisManager::RegisterGraphicsSystem: null pointer!"
568  << G4endl;
569  }
570  happy=false;
571  }
572  return happy;
573 }
574 
575 const G4VTrajectoryModel*
577 {
578  assert (0 != fpTrajDrawModelMgr);
579 
580  const G4VTrajectoryModel* model = fpTrajDrawModelMgr->Current();
581 
582  if (0 == model) {
583  // No model was registered with the trajectory model manager.
584  // Use G4TrajectoryDrawByCharge as a default.
585  fpTrajDrawModelMgr->Register(new G4TrajectoryDrawByCharge("AutoGenerated"));
586 
587  if (fVerbosity >= warnings) {
588  G4cout<<"G4VisManager: Using G4TrajectoryDrawByCharge as default trajectory model."<<G4endl;
589  G4cout<<"See commands in /vis/modeling/trajectories/ for other options."<<G4endl;
590  }
591  }
592 
593  model = fpTrajDrawModelMgr->Current();
594  assert (0 != model); // Should definitely exist now
595 
596  return model;
597 }
598 
600 {
601  fpTrajDrawModelMgr->Register(model);
602 }
603 
604 void
606 {
607  fpTrajDrawModelMgr->Register(factory);
608 }
609 
611 {
612  fpTrajFilterMgr->Register(model);
613 }
614 
615 void
617 {
618  fpTrajFilterMgr->Register(factory);
619 }
620 
622 {
623  fpHitFilterMgr->Register(model);
624 }
625 
626 void
628 {
629  fpHitFilterMgr->Register(factory);
630 }
631 
633 {
634  fpDigiFilterMgr->Register(model);
635 }
636 
637 void
639 {
640  fpDigiFilterMgr->Register(factory);
641 }
642 
644 {
645  fpTrajDrawModelMgr->SetCurrent(model);
646 }
647 
648 void G4VisManager::BeginDraw (const G4Transform3D& objectTransform)
649 {
650  fDrawGroupNestingDepth++;
651  if (fDrawGroupNestingDepth > 1) {
653  ("G4VSceneHandler::BeginDraw",
654  "visman0008", JustWarning,
655  "Nesting detected. It is illegal to nest Begin/EndDraw."
656  "\n Ignored");
657  return;
658  }
659  if (IsValidView ()) {
660  ClearTransientStoreIfMarked();
661  fpSceneHandler -> BeginPrimitives (objectTransform);
662  fIsDrawGroup = true;
663  }
664 }
665 
667 {
668  fDrawGroupNestingDepth--;
669  if (fDrawGroupNestingDepth != 0) {
670  if (fDrawGroupNestingDepth < 0) fDrawGroupNestingDepth = 0;
671  return;
672  }
673  if (IsValidView ()) {
674  fpSceneHandler -> EndPrimitives ();
675  }
676  fIsDrawGroup = false;
677 }
678 
679 void G4VisManager::BeginDraw2D (const G4Transform3D& objectTransform)
680 {
681  fDrawGroupNestingDepth++;
682  if (fDrawGroupNestingDepth > 1) {
684  ("G4VSceneHandler::BeginDraw2D",
685  "visman0009", JustWarning,
686  "Nesting detected. It is illegal to nest Begin/EndDraw2D."
687  "\n Ignored");
688  return;
689  }
690  if (IsValidView ()) {
691  ClearTransientStoreIfMarked();
692  fpSceneHandler -> BeginPrimitives2D (objectTransform);
693  fIsDrawGroup = true;
694  }
695 }
696 
698 {
699  fDrawGroupNestingDepth--;
700  if (fDrawGroupNestingDepth != 0) {
701  if (fDrawGroupNestingDepth < 0) fDrawGroupNestingDepth = 0;
702  return;
703  }
704  if (IsValidView ()) {
705  fpSceneHandler -> EndPrimitives2D ();
706  }
707  fIsDrawGroup = false;
708 }
709 
710 template <class T> void G4VisManager::DrawT
711 (const T& graphics_primitive, const G4Transform3D& objectTransform) {
712  if (fIsDrawGroup) {
713  if (objectTransform != fpSceneHandler->GetObjectTransformation()) {
715  ("G4VSceneHandler::DrawT",
716  "visman0010", FatalException,
717  "Different transform detected in Begin/EndDraw group.");
718  }
719  fpSceneHandler -> AddPrimitive (graphics_primitive);
720  } else {
721  if (IsValidView ()) {
722  ClearTransientStoreIfMarked();
723  fpSceneHandler -> BeginPrimitives (objectTransform);
724  fpSceneHandler -> AddPrimitive (graphics_primitive);
725  fpSceneHandler -> EndPrimitives ();
726  }
727  }
728 }
729 
730 template <class T> void G4VisManager::DrawT2D
731 (const T& graphics_primitive, const G4Transform3D& objectTransform) {
732  if (fIsDrawGroup) {
733  if (objectTransform != fpSceneHandler->GetObjectTransformation()) {
735  ("G4VSceneHandler::DrawT",
736  "visman0011", FatalException,
737  "Different transform detected in Begin/EndDraw2D group.");
738  }
739  fpSceneHandler -> AddPrimitive (graphics_primitive);
740  } else {
741  if (IsValidView ()) {
742  ClearTransientStoreIfMarked();
743  fpSceneHandler -> BeginPrimitives2D (objectTransform);
744  fpSceneHandler -> AddPrimitive (graphics_primitive);
745  fpSceneHandler -> EndPrimitives2D ();
746  }
747  }
748 }
749 
750 void G4VisManager::Draw (const G4Circle& circle,
751  const G4Transform3D& objectTransform)
752 {
753  DrawT (circle, objectTransform);
754 }
755 
756 void G4VisManager::Draw (const G4NURBS& nurbs,
757  const G4Transform3D& objectTransform)
758 {
759  DrawT (nurbs, objectTransform);
760 }
761 
762 void G4VisManager::Draw (const G4Polyhedron& polyhedron,
763  const G4Transform3D& objectTransform)
764 {
765  DrawT (polyhedron, objectTransform);
766 }
767 
768 void G4VisManager::Draw (const G4Polyline& line,
769  const G4Transform3D& objectTransform)
770 {
771  DrawT (line, objectTransform);
772 }
773 
774 void G4VisManager::Draw (const G4Polymarker& polymarker,
775  const G4Transform3D& objectTransform)
776 {
777  DrawT (polymarker, objectTransform);
778 }
779 
781  const G4Transform3D& objectTransform)
782 {
783  DrawT (scale, objectTransform);
784 }
785 
786 void G4VisManager::Draw (const G4Square& square,
787  const G4Transform3D& objectTransform)
788 {
789  DrawT (square, objectTransform);
790 }
791 
792 void G4VisManager::Draw (const G4Text& text,
793  const G4Transform3D& objectTransform)
794 {
795  DrawT (text, objectTransform);
796 }
797 
798 void G4VisManager::Draw2D (const G4Circle& circle,
799  const G4Transform3D& objectTransform)
800 {
801  DrawT2D (circle, objectTransform);
802 }
803 
804 void G4VisManager::Draw2D (const G4NURBS& nurbs,
805  const G4Transform3D& objectTransform)
806 {
807  DrawT2D (nurbs, objectTransform);
808 }
809 
810 void G4VisManager::Draw2D (const G4Polyhedron& polyhedron,
811  const G4Transform3D& objectTransform)
812 {
813  DrawT2D (polyhedron, objectTransform);
814 }
815 
817  const G4Transform3D& objectTransform)
818 {
819  DrawT2D (line, objectTransform);
820 }
821 
822 void G4VisManager::Draw2D (const G4Polymarker& polymarker,
823  const G4Transform3D& objectTransform)
824 {
825  DrawT2D (polymarker, objectTransform);
826 }
827 
828 void G4VisManager::Draw2D (const G4Square& square,
829  const G4Transform3D& objectTransform)
830 {
831  DrawT2D (square, objectTransform);
832 }
833 
834 void G4VisManager::Draw2D (const G4Text& text,
835  const G4Transform3D& objectTransform)
836 {
837  DrawT2D (text, objectTransform);
838 }
839 
840 void G4VisManager::Draw (const G4VHit& hit) {
841  if (fIsDrawGroup) {
842  fpSceneHandler -> AddCompound (hit);
843  } else {
844  if (IsValidView ()) {
845  ClearTransientStoreIfMarked();
846  fpSceneHandler -> AddCompound (hit);
847  }
848  }
849 }
850 
851 void G4VisManager::Draw (const G4VDigi& digi) {
852  if (fIsDrawGroup) {
853  fpSceneHandler -> AddCompound (digi);
854  } else {
855  if (IsValidView ()) {
856  ClearTransientStoreIfMarked();
857  fpSceneHandler -> AddCompound (digi);
858  }
859  }
860 }
861 
863  G4int i_mode) {
864  if (fIsDrawGroup) {
865  fpSceneHandler -> SetModel (&dummyTrajectoriesModel);
866  dummyTrajectoriesModel.SetDrawingMode(i_mode);
867  fpSceneHandler -> AddCompound (traj);
868  } else {
869  if (IsValidView ()) {
870  ClearTransientStoreIfMarked();
871  fpSceneHandler -> SetModel (&dummyTrajectoriesModel);
872  dummyTrajectoriesModel.SetDrawingMode(i_mode);
873  fpSceneHandler -> AddCompound (traj);
874  }
875  }
876 }
877 
878 void G4VisManager::Draw (const G4LogicalVolume& logicalVol,
879  const G4VisAttributes& attribs,
880  const G4Transform3D& objectTransform) {
881  // Find corresponding solid.
882  G4VSolid* pSol = logicalVol.GetSolid ();
883  Draw (*pSol, attribs, objectTransform);
884 }
885 
886 void G4VisManager::Draw (const G4VSolid& solid,
887  const G4VisAttributes& attribs,
888  const G4Transform3D& objectTransform) {
889  if (fIsDrawGroup) {
890  fpSceneHandler -> PreAddSolid (objectTransform, attribs);
891  solid.DescribeYourselfTo (*fpSceneHandler);
892  fpSceneHandler -> PostAddSolid ();
893  } else {
894  if (IsValidView ()) {
895  ClearTransientStoreIfMarked();
896  fpSceneHandler -> PreAddSolid (objectTransform, attribs);
897  solid.DescribeYourselfTo (*fpSceneHandler);
898  fpSceneHandler -> PostAddSolid ();
899  }
900  }
901 }
902 
903 void G4VisManager::Draw (const G4VPhysicalVolume& physicalVol,
904  const G4VisAttributes& attribs,
905  const G4Transform3D& objectTransform) {
906  // Find corresponding logical volume and solid.
907  G4LogicalVolume* pLV = physicalVol.GetLogicalVolume ();
908  G4VSolid* pSol = pLV -> GetSolid ();
909  Draw (*pSol, attribs, objectTransform);
910 }
911 
913  if (!fInitialised) Initialise ();
914  if (fpGraphicsSystem) {
915  G4VSceneHandler* pSceneHandler =
916  fpGraphicsSystem -> CreateSceneHandler (name);
917  if (pSceneHandler) {
918  fAvailableSceneHandlers.push_back (pSceneHandler);
919  fpSceneHandler = pSceneHandler; // Make current.
920  }
921  else {
922  if(fVerbosity >= errors) {
923  G4cout << "ERROR in G4VisManager::CreateSceneHandler during "
924  << fpGraphicsSystem -> GetName ()
925  << " scene handler creation.\n No action taken."
926  << G4endl;
927  }
928  }
929  }
930  else PrintInvalidPointers ();
931 }
932 
934 
935  if (!fInitialised) Initialise ();
936 
937  if (!fpSceneHandler) {
938  PrintInvalidPointers ();
939  return;
940  }
941 
942  G4VViewer* p = fpGraphicsSystem -> CreateViewer (*fpSceneHandler, name);
943 
944  if (!p) {
945  if (fVerbosity >= errors) {
946  G4cout << "ERROR in G4VisManager::CreateViewer during "
947  << fpGraphicsSystem -> GetName ()
948  << " viewer creation.\n No action taken."
949  << G4endl;
950  }
951  return;
952  }
953 
954  if (p -> GetViewId() < 0) {
955  if (fVerbosity >= errors) {
956  G4cout << "ERROR in G4VisManager::CreateViewer during "
957  << fpGraphicsSystem -> GetName ()
958  << " viewer initialisation.\n No action taken."
959  << G4endl;
960  }
961  return;
962  }
963 
964  // Viewer is created, now we can set geometry parameters
965  // Before 12/2008, it was done in G4VViewer.cc but it did not have to be there!
966 
967  G4ViewParameters initialvp = p -> GetViewParameters();
968  initialvp.SetXGeometryString(XGeometry); //parse string and store parameters
969  p -> SetViewParameters(initialvp);
970  p -> Initialise (); // (Viewer itself may change view parameters further.)
971 
972  fpViewer = p; // Make current.
973  fpSceneHandler -> AddViewerToList (fpViewer);
974  fpSceneHandler -> SetCurrentViewer (fpViewer);
975  if (fVerbosity >= confirmations) {
976  G4cout << "G4VisManager::CreateViewer: new viewer created."
977  << G4endl;
978  }
979 
980  const G4ViewParameters& vp = fpViewer->GetViewParameters();
981  if (fVerbosity >= parameters) {
982  G4cout << " view parameters are:\n " << vp << G4endl;
983  }
984 
985  if (vp.IsCulling () && vp.IsCullingInvisible ()) {
986  static G4bool warned = false;
987  if (fVerbosity >= confirmations) {
988  if (!warned) {
989  G4cout <<
990  "NOTE: objects with visibility flag set to \"false\""
991  " will not be drawn!"
992  "\n \"/vis/viewer/set/culling global false\" to Draw such objects."
993  "\n Also see other \"/vis/viewer/set\" commands."
994  << G4endl;
995  warned = true;
996  }
997  }
998  }
999  if (vp.IsCullingCovered ()) {
1000  static G4bool warned = false;
1001  if (fVerbosity >= warnings) {
1002  if (!warned) {
1003  G4cout <<
1004  "WARNING: covered objects in solid mode will not be rendered!"
1005  "\n \"/vis/viewer/set/culling coveredDaughters false\" to reverse this."
1006  "\n Also see other \"/vis/viewer/set\" commands."
1007  << G4endl;
1008  warned = true;
1009  }
1010  }
1011  }
1012 }
1013 
1015  if (fVerbosity >= confirmations) {
1016  G4cout << "G4VisManager::GeometryHasChanged() called." << G4endl;
1017  }
1018 
1019  // Change the world...
1020  G4VPhysicalVolume* pWorld =
1022  -> GetNavigatorForTracking () -> GetWorldVolume ();
1023  if (!pWorld) {
1024  if (fVerbosity >= warnings) {
1025  G4cout << "WARNING: There is no world volume!" << G4endl;
1026  }
1027  }
1028 
1029  // Check scenes.
1030  G4SceneList& sceneList = fSceneList;
1031  G4int iScene, nScenes = sceneList.size ();
1032  for (iScene = 0; iScene < nScenes; iScene++) {
1033  G4Scene* pScene = sceneList [iScene];
1034  std::vector<G4Scene::Model>& modelList = pScene -> SetRunDurationModelList ();
1035  if (modelList.size ()) {
1036  G4bool modelInvalid;
1037  do { // Remove, if required, one at a time.
1038  modelInvalid = false;
1039  std::vector<G4Scene::Model>::iterator iterModel;
1040  for (iterModel = modelList.begin();
1041  iterModel != modelList.end();
1042  ++iterModel) {
1043  modelInvalid = !(iterModel->fpModel->Validate(fVerbosity>=warnings));
1044  if (modelInvalid) {
1045  // Model invalid - remove and break.
1046  if (fVerbosity >= warnings) {
1047  G4cout << "WARNING: Model \""
1048  << iterModel->fpModel->GetGlobalDescription ()
1049  <<
1050  "\" is no longer valid - being removed\n from scene \""
1051  << pScene -> GetName () << "\""
1052  << G4endl;
1053  }
1054  modelList.erase (iterModel);
1055  break;
1056  }
1057  }
1058  } while (modelInvalid);
1059 
1060  if (modelList.size () == 0) {
1061  if (fVerbosity >= warnings) {
1062  G4cout << "WARNING: No models left in this scene \""
1063  << pScene -> GetName ()
1064  << "\"."
1065  << G4endl;
1066  }
1067  }
1068  else {
1069  pScene->CalculateExtent();
1071  ApplyCommand (G4String("/vis/scene/notifyHandlers " + pScene->GetName()));
1072  }
1073  }
1074  }
1075 
1076  // Check the manager's current scene...
1077  if (fpScene && fpScene -> GetRunDurationModelList ().size () == 0) {
1078  if (fVerbosity >= warnings) {
1079  G4cout << "WARNING: The current scene \""
1080  << fpScene -> GetName ()
1081  << "\" has no models."
1082  << G4endl;
1083  }
1084  }
1085 
1086 }
1088 
1089  if (fVerbosity >= confirmations) {
1090  G4cout << "G4VisManager::NotifyHandler() called." << G4endl;
1091  }
1092 
1093  // Check scenes.
1094  G4SceneList& sceneList = fSceneList;
1095  G4int iScene, nScenes = sceneList.size ();
1096  for (iScene = 0; iScene < nScenes; iScene++) {
1097  G4Scene* pScene = sceneList [iScene];
1098  std::vector<G4Scene::Model>& modelList = pScene -> SetRunDurationModelList ();
1099 
1100  if (modelList.size ()) {
1101  pScene->CalculateExtent();
1103  ApplyCommand (G4String("/vis/scene/notifyHandlers " + pScene->GetName()));
1104  }
1105  }
1106 
1107  // Check the manager's current scene...
1108  if (fpScene && fpScene -> GetRunDurationModelList ().size () == 0) {
1109  if (fVerbosity >= warnings) {
1110  G4cout << "WARNING: The current scene \""
1111  << fpScene -> GetName ()
1112  << "\" has no models."
1113  << G4endl;
1114  }
1115  }
1116 
1117 }
1118 
1120 {
1121  return fpTrajFilterMgr->Accept(trajectory);
1122 }
1123 
1125 {
1126  return fpHitFilterMgr->Accept(hit);
1127 }
1128 
1130 {
1131  return fpDigiFilterMgr->Accept(digi);
1132 }
1133 
1135 {
1136  G4bool visible(true);
1137 
1138  // See if trajectory passes filter
1139  G4bool passed = FilterTrajectory(trajectory);
1140 
1141  if (!passed) {
1142  // Draw invisible trajectory if trajectory failed filter and
1143  // are filtering in soft mode
1144  if (fpTrajFilterMgr->GetMode() == FilterMode::Soft) visible = false;
1145  else {return;}
1146  }
1147 
1148  // Go on to draw trajectory
1149  assert (0 != fpTrajDrawModelMgr);
1150 
1151  const G4VTrajectoryModel* trajectoryModel = CurrentTrajDrawModel();
1152 
1153  assert (0 != trajectoryModel); // Should exist
1154 
1155  trajectoryModel->Draw(trajectory, visible);
1156 }
1157 
1158 void G4VisManager::DispatchToModel(const G4VTrajectory& trajectory, G4int i_mode)
1159 {
1160  G4bool visible(true);
1161 
1162  // See if trajectory passes filter
1163  G4bool passed = FilterTrajectory(trajectory);
1164 
1165  if (!passed) {
1166  // Draw invisible trajectory if trajectory failed filter and
1167  // are filtering in soft mode
1168  if (fpTrajFilterMgr->GetMode() == FilterMode::Soft) visible = false;
1169  else {return;}
1170  }
1171 
1172  // Go on to draw trajectory
1173  assert (0 != fpTrajDrawModelMgr);
1174 
1175  const G4VTrajectoryModel* trajectoryModel = CurrentTrajDrawModel();
1176 
1177  assert (0 != trajectoryModel); // Should exist
1178 
1179  if (IsValidView()) {
1180  G4TrajectoriesModel* trajectoriesModel =
1181  dynamic_cast<G4TrajectoriesModel*>(fpSceneHandler->GetModel());
1182  if (trajectoriesModel) {
1183  if (trajectoriesModel->IsDrawingModeSet()) {
1184  trajectoryModel->Draw(trajectory, i_mode, visible);
1185  } else {
1186  trajectoryModel->Draw(trajectory, visible);
1187  }
1188  } else {
1189  // Just draw at user's request
1190  trajectoryModel->Draw(trajectory, i_mode, visible);
1191  }
1192  }
1193 }
1194 
1196 (const G4String& name,
1197  G4VUserVisAction* pVisAction,
1198  const G4VisExtent& extent) {
1199  fRunDurationUserVisActions.push_back(UserVisAction(name,pVisAction));
1200  if (extent.GetExtentRadius() > 0.) {
1201  fUserVisActionExtents[pVisAction] = extent;
1202  } else {
1203  if (fVerbosity >= warnings) {
1204  G4cout <<
1205  "WARNING: No extent set for user vis action \"" << name << "\"."
1206  << G4endl;
1207  }
1208  }
1209 }
1210 
1212 (const G4String& name,
1213  G4VUserVisAction* pVisAction,
1214  const G4VisExtent& extent) {
1215  fEndOfEventUserVisActions.push_back(UserVisAction(name,pVisAction));
1216  if (extent.GetExtentRadius() > 0.) {
1217  fUserVisActionExtents[pVisAction] = extent;
1218  } else {
1219  if (fVerbosity >= warnings) {
1220  G4cout <<
1221  "WARNING: No extent set for user vis action \"" << name << "\"."
1222  << G4endl;
1223  }
1224  }
1225 }
1226 
1228 (const G4String& name,
1229  G4VUserVisAction* pVisAction,
1230  const G4VisExtent& extent) {
1231  fEndOfRunUserVisActions.push_back(UserVisAction(name,pVisAction));
1232  fUserVisActionExtents[pVisAction] = extent;
1233  if (extent.GetExtentRadius() <= 0.) {
1234  if (fVerbosity >= warnings) {
1235  G4cout <<
1236  "WARNING: No extent set for user vis action \"" << name << "\"."
1237  << G4endl;
1238  }
1239  }
1240 }
1241 
1243  if (pScene != fpScene) {
1244  // A change of scene. Therefore reset transients drawn flags. All
1245  // memory of previous transient proceessing thereby erased...
1247  }
1248  fpScene = pScene;
1249 }
1250 
1252  fpGraphicsSystem = pSystem;
1253  if (fVerbosity >= confirmations) {
1254  G4cout << "G4VisManager::SetCurrentGraphicsSystem: system now "
1255  << pSystem -> GetName () << G4endl;
1256  }
1257  // If current scene handler is of same graphics system, leave unchanged.
1258  // Else find the most recent scene handler of same graphics system.
1259  // Or clear pointers.
1260  if (!(fpSceneHandler && fpSceneHandler -> GetGraphicsSystem () == pSystem)) {
1261  const G4SceneHandlerList& sceneHandlerList = fAvailableSceneHandlers;
1262  G4int nSH = sceneHandlerList.size (); // No. of scene handlers.
1263  G4int iSH;
1264  for (iSH = nSH - 1; iSH >= 0; iSH--) {
1265  if (sceneHandlerList [iSH] -> GetGraphicsSystem () == pSystem) break;
1266  }
1267  if (iSH >= 0) {
1268  fpSceneHandler = sceneHandlerList [iSH];
1269  if (fVerbosity >= confirmations) {
1270  G4cout << " Scene Handler now "
1271  << fpSceneHandler -> GetName () << G4endl;
1272  }
1273  if (fpScene != fpSceneHandler -> GetScene ()) {
1274  fpScene = fpSceneHandler -> GetScene ();
1275  if (fVerbosity >= confirmations) {
1276  G4cout << " Scene now \""
1277  << fpScene -> GetName () << "\"" << G4endl;
1278  }
1279  }
1280  const G4ViewerList& viewerList = fpSceneHandler -> GetViewerList ();
1281  if (viewerList.size ()) {
1282  fpViewer = viewerList [0];
1283  if (fVerbosity >= confirmations) {
1284  G4cout << " Viewer now " << fpViewer -> GetName () << G4endl;
1285  }
1286  }
1287  else {
1288  fpViewer = 0;
1289  }
1290  }
1291  else {
1292  fpSceneHandler = 0;
1293  fpViewer = 0;
1294  }
1295  }
1296 }
1297 
1299  fpSceneHandler = pSceneHandler;
1300  if (fVerbosity >= confirmations) {
1301  G4cout << "G4VisManager::SetCurrentSceneHandler: scene handler now \""
1302  << pSceneHandler -> GetName () << "\"" << G4endl;
1303  }
1304  if (fpScene != fpSceneHandler -> GetScene ()) {
1305  fpScene = fpSceneHandler -> GetScene ();
1306  if (fVerbosity >= confirmations) {
1307  G4cout << " Scene now \""
1308  << fpScene -> GetName () << "\"" << G4endl;
1309  }
1310  }
1311  if (fpGraphicsSystem != pSceneHandler -> GetGraphicsSystem ()) {
1312  fpGraphicsSystem = pSceneHandler -> GetGraphicsSystem ();
1313  if (fVerbosity >= confirmations) {
1314  G4cout << " Graphics system now \""
1315  << fpGraphicsSystem -> GetName () << "\"" << G4endl;
1316  }
1317  }
1318  const G4ViewerList& viewerList = fpSceneHandler -> GetViewerList ();
1319  G4int nViewers = viewerList.size ();
1320  if (nViewers) {
1321  G4int iViewer;
1322  for (iViewer = 0; iViewer < nViewers; iViewer++) {
1323  if (fpViewer == viewerList [iViewer]) break;
1324  }
1325  if (iViewer >= nViewers) {
1326  fpViewer = viewerList [0];
1327  if (fVerbosity >= confirmations) {
1328  G4cout << " Viewer now \"" << fpViewer -> GetName () << "\""
1329  << G4endl;
1330  }
1331  }
1332  if (!IsValidView ()) {
1333  if (fVerbosity >= warnings) {
1334  G4cout <<
1335  "WARNING: Problem setting scene handler - please report circumstances."
1336  << G4endl;
1337  }
1338  }
1339  }
1340  else {
1341  fpViewer = 0;
1342  if (fVerbosity >= warnings) {
1343  G4cout <<
1344  "WARNING: No viewers for this scene handler - please create one."
1345  << G4endl;
1346  }
1347  }
1348 }
1349 
1351  fpViewer = pViewer;
1352  if (fVerbosity >= confirmations) {
1353  G4cout << "G4VisManager::SetCurrentViewer: viewer now "
1354  << pViewer -> GetName ()
1355  << G4endl;
1356  }
1357  fpSceneHandler = fpViewer -> GetSceneHandler ();
1358  fpSceneHandler -> SetCurrentViewer (pViewer);
1359  fpScene = fpSceneHandler -> GetScene ();
1360  fpGraphicsSystem = fpSceneHandler -> GetGraphicsSystem ();
1361  if (!IsValidView ()) {
1362  if (fVerbosity >= warnings) {
1363  G4cout <<
1364  "WARNING: Problem setting viewer - please report circumstances."
1365  << G4endl;
1366  }
1367  }
1368 }
1369 
1370 namespace {
1371  struct NicknameComparison {
1372  bool operator() (const G4String& lhs, const G4String& rhs) const
1373  {return lhs.length()<rhs.length();}
1374  };
1375 }
1376 
1378  G4int nSystems = fAvailableGraphicsSystems.size ();
1379  G4cout << "Current available graphics systems are:";
1380  if (nSystems) {
1381  // Make a map of graphics systems names (there may be repeated systems)
1382  // and for each name a set of nicknames. The nicknames are ordered
1383  // by length - see struct NicknameComparison above.
1384  std::map<G4String,std::set<G4String,NicknameComparison> > systemMap;
1385  for (G4int i = 0; i < nSystems; i++) {
1386  const G4VGraphicsSystem* pSystem = fAvailableGraphicsSystems[i];
1387  systemMap[pSystem->GetName()].insert(pSystem->GetNickname());
1388  }
1389  // Print the map.
1390  std::map<G4String,std::set<G4String,NicknameComparison> >::const_iterator i;
1391  for (i = systemMap.begin(); i != systemMap.end(); ++i) {
1392  G4cout << "\n " << i->first << " (";
1393  const std::set<G4String,NicknameComparison>& nicknames = i->second;
1394  std::set<G4String,NicknameComparison>::const_iterator j;
1395  for (j = nicknames.begin(); j != nicknames.end(); ++j) {
1396  if (j != nicknames.begin()) G4cout << ", ";
1397  G4cout << *j;
1398  }
1399  G4cout << ')';
1400  }
1401  }
1402  else {
1403  G4cout << "\n NONE!!! None registered - yet! Mmmmm!";
1404  }
1405  G4cout << G4endl;
1406 }
1407 
1408 void G4VisManager::PrintAvailableModels (Verbosity verbosity) const
1409 {
1410  {
1411  //fpTrajDrawModelMgr->Print(G4cout);
1412  G4cout << "Registered model factories:" << G4endl;
1413  const std::vector<G4VModelFactory<G4VTrajectoryModel>*>& factoryList =
1414  fpTrajDrawModelMgr->FactoryList();
1415  if (factoryList.empty()) G4cout << " None" << G4endl;
1416  else {
1417  std::vector<G4VModelFactory<G4VTrajectoryModel>*>::const_iterator i;
1418  for (i = factoryList.begin(); i != factoryList.end(); ++i)
1419  (*i)->Print(G4cout);
1420  }
1421  const G4VisListManager<G4VTrajectoryModel>* listManager =
1422  fpTrajDrawModelMgr->ListManager();
1423  const std::map<G4String, G4VTrajectoryModel*>& modelMap =
1424  listManager->Map();
1425  if (!modelMap.empty()) {
1426  G4cout << "\nRegistered models:" << G4endl;
1427  std::map<G4String, G4VTrajectoryModel*>::const_iterator i;
1428  for (i = modelMap.begin(); i != modelMap.end(); ++i) {
1429  G4cout << " " << i->second->Name();
1430  if (i->second == listManager->Current()) G4cout << " (Current)";
1431  G4cout << G4endl;
1432  if (verbosity >= parameters) i->second->Print(G4cout);
1433  }
1434  }
1435  }
1436 
1437  G4cout << G4endl;
1438 
1439  {
1440  //fpTrajFilterMgr->Print(G4cout);
1441  G4cout << "Registered filter factories:" << G4endl;
1442  const std::vector<G4VModelFactory<G4VFilter<G4VTrajectory> >*>&
1443  factoryList = fpTrajFilterMgr->FactoryList();
1444  if (factoryList.empty()) G4cout << " None" << G4endl;
1445  else {
1446  std::vector<G4VModelFactory<G4VFilter<G4VTrajectory> >*>::const_iterator i;
1447  for (i = factoryList.begin(); i != factoryList.end(); ++i)
1448  (*i)->Print(G4cout);
1449  }
1450  const std::vector<G4VFilter<G4VTrajectory>*>&
1451  filterList = fpTrajFilterMgr->FilterList();
1452  if (!filterList.empty()) {
1453  G4cout << "\nRegistered filters:" << G4endl;
1454  std::vector<G4VFilter<G4VTrajectory>*>::const_iterator i;
1455  for (i = filterList.begin(); i != filterList.end(); ++i) {
1456  G4cout << " " << (*i)->GetName() << G4endl;
1457  if (verbosity >= parameters) (*i)->PrintAll(G4cout);
1458  }
1459  }
1460  }
1461 }
1462 
1463 void G4VisManager::PrintAvailableUserVisActions (Verbosity) const
1464 {
1465  G4cout <<
1466  "You have successfully registered the following user vis actions."
1467  << G4endl;
1468  G4cout << "Run Duration User Vis Actions:";
1469  if (fRunDurationUserVisActions.empty()) G4cout << " none" << G4endl;
1470  else {
1471  G4cout << G4endl;
1472  for (size_t i = 0; i < fRunDurationUserVisActions.size(); i++) {
1473  const G4String& name = fRunDurationUserVisActions[i].fName;
1474  G4cout << " " << name << G4endl;
1475  }
1476  }
1477 
1478  G4cout << "End of Event User Vis Actions:";
1479  if (fEndOfEventUserVisActions.empty()) G4cout << " none" << G4endl;
1480  else {
1481  G4cout << G4endl;
1482  for (size_t i = 0; i < fEndOfEventUserVisActions.size(); i++) {
1483  const G4String& name = fEndOfEventUserVisActions[i].fName;
1484  G4cout << " " << name << G4endl;
1485  }
1486  }
1487 
1488  G4cout << "End of Run User Vis Actions:";
1489  if (fEndOfRunUserVisActions.empty()) G4cout << " none" << G4endl;
1490  else {
1491  G4cout << G4endl;
1492  for (size_t i = 0; i < fEndOfRunUserVisActions.size(); i++) {
1493  const G4String& name = fEndOfRunUserVisActions[i].fName;
1494  G4cout << " " << name << G4endl;
1495  }
1496  }
1497 }
1498 
1499 void G4VisManager::PrintAvailableColours (Verbosity) const {
1500  G4cout <<
1501  "Some /vis commands (optionally) take a string to specify colour."
1502  "\nAvailable colours:\n ";
1503  const std::map<G4String, G4Colour>& map = G4Colour::GetMap();
1504  for (std::map<G4String, G4Colour>::const_iterator i = map.begin();
1505  i != map.end();) {
1506  G4cout << i->first;
1507  if (++i != map.end()) G4cout << ", ";
1508  }
1509  G4cout << G4endl;
1510 }
1511 
1512 void G4VisManager::PrintInvalidPointers () const {
1513  if (fVerbosity >= errors) {
1514  G4cout << "ERROR: G4VisManager::PrintInvalidPointers:";
1515  if (!fpGraphicsSystem) {
1516  G4cout << "\n null graphics system pointer.";
1517  }
1518  else {
1519  G4cout << "\n Graphics system is " << fpGraphicsSystem -> GetName ()
1520  << " but:";
1521  if (!fpScene)
1522  G4cout <<
1523  "\n Null scene pointer. Use \"/vis/drawVolume\" or"
1524  " \"/vis/scene/create\".";
1525  if (!fpSceneHandler)
1526  G4cout <<
1527  "\n Null scene handler pointer. Use \"/vis/open\" or"
1528  " \"/vis/sceneHandler/create\".";
1529  if (!fpViewer )
1530  G4cout <<
1531  "\n Null viewer pointer. Use \"/vis/viewer/create\".";
1532  }
1533  G4cout << G4endl;
1534  }
1535 }
1536 
1537 void G4VisManager::BeginOfRun ()
1538 {
1539  //G4cout << "G4VisManager::BeginOfRun" << G4endl;
1540  fKeptLastEvent = false;
1541  fEventKeepingSuspended = false;
1542  fTransientsDrawnThisRun = false;
1543  if (fpSceneHandler) fpSceneHandler->SetTransientsDrawnThisRun(false);
1544 }
1545 
1546 void G4VisManager::BeginOfEvent ()
1547 {
1548  //G4cout << "G4VisManager::BeginOfEvent" << G4endl;
1549  fTransientsDrawnThisEvent = false;
1550  if (fpSceneHandler) fpSceneHandler->SetTransientsDrawnThisEvent(false);
1551 }
1552 
1553 void G4VisManager::EndOfEvent ()
1554 {
1555  //G4cout << "G4VisManager::EndOfEvent" << G4endl;
1556 
1557  // Don't call IsValidView unless there is a scene handler. This
1558  // avoids WARNING message at end of event and run when the user has
1559  // not instantiated a scene handler, e.g., in batch mode.
1560  G4bool valid = GetConcreteInstance() && fpSceneHandler && IsValidView();
1561  if (!valid) return;
1562 
1563  G4RunManager* runManager = G4RunManager::GetRunManager();
1564  const G4Run* currentRun = runManager->GetCurrentRun();
1565 
1567  const G4Event* currentEvent = eventManager->GetConstCurrentEvent();
1568  if (!currentEvent) return;
1569 
1570  // We are about to draw the event (trajectories, etc.), but first we
1571  // have to clear the previous event(s) if necessary. If this event
1572  // needs to be drawn afresh, e.g., the first event or any event when
1573  // "accumulate" is not requested, the old event has to be cleared.
1574  // We have postponed this so that, for normal viewers like OGL, the
1575  // previous event(s) stay on screen until this new event comes
1576  // along. For a file-writing viewer the geometry has to be drawn.
1577  // See, for example, G4HepRepFileSceneHandler::ClearTransientStore.
1578  ClearTransientStoreIfMarked();
1579 
1580  // Now draw the event...
1581  fpSceneHandler->DrawEvent(currentEvent);
1582 
1583  G4int nEventsToBeProcessed = 0;
1584  G4int nKeptEvents = 0;
1585  G4int eventID = -2; // (If no run manager, triggers ShowView as normal.)
1586  if (currentRun) {
1587  nEventsToBeProcessed = currentRun->GetNumberOfEventToBeProcessed();
1588  eventID = currentEvent->GetEventID();
1589  const std::vector<const G4Event*>* events =
1590  currentRun->GetEventVector();
1591  if (events) nKeptEvents = events->size();
1592  }
1593 
1594  if (fpScene->GetRefreshAtEndOfEvent()) {
1595 
1596  // Unless last event (in which case wait end of run)...
1597  if (eventID < nEventsToBeProcessed - 1) {
1598  // ShowView guarantees the view comes to the screen. No action
1599  // is taken for normal viewers like OGL, but file-writing
1600  // viewers have to close the file.
1601  fpViewer->ShowView();
1602  fpSceneHandler->SetMarkForClearingTransientStore(true);
1603  } else { // Last event...
1604  // Keep, but only if user has not kept any...
1605  if (!nKeptEvents) {
1606  eventManager->KeepTheCurrentEvent();
1607  fKeptLastEvent = true;
1608  }
1609  }
1610 
1611  } else { // Accumulating events...
1612 
1613  G4int maxNumberOfKeptEvents = fpScene->GetMaxNumberOfKeptEvents();
1614  if (maxNumberOfKeptEvents > 0 && nKeptEvents >= maxNumberOfKeptEvents) {
1615  fEventKeepingSuspended = true;
1616  static G4bool warned = false;
1617  if (!warned) {
1618  if (fVerbosity >= warnings) {
1619  G4cout <<
1620  "WARNING: G4VisManager::EndOfEvent: Automatic event keeping suspended."
1621  "\n The number of events exceeds the maximum, "
1622  << maxNumberOfKeptEvents <<
1623  ", that can be kept by the vis manager."
1624  << G4endl;
1625  }
1626  warned = true;
1627  }
1628  } else if (maxNumberOfKeptEvents != 0) {
1629  eventManager->KeepTheCurrentEvent();
1630  }
1631  }
1632 }
1633 
1634 void G4VisManager::EndOfRun ()
1635 {
1636  //G4cout << "G4VisManager::EndOfRun" << G4endl;
1637 
1638  // Don't call IsValidView unless there is a scene handler. This
1639  // avoids WARNING message at end of event and run when the user has
1640  // not instantiated a scene handler, e.g., in batch mode.
1641  G4bool valid = GetConcreteInstance() && fpSceneHandler && IsValidView();
1642  if (valid) {
1643  if (!fpSceneHandler->GetMarkForClearingTransientStore()) {
1644  if (fpScene->GetRefreshAtEndOfRun()) {
1645  fpSceneHandler->DrawEndOfRunModels();
1646  // ShowView guarantees the view comes to the screen. No
1647  // action is taken for normal viewers like OGL, but
1648  // file-writing viewers have to close the file.
1649  fpViewer->ShowView();
1650  // An extra refresh for auto-refresh viewers
1651  if (fpViewer->GetViewParameters().IsAutoRefresh()) {
1652  fpViewer->RefreshView();
1653  }
1654  fpSceneHandler->SetMarkForClearingTransientStore(true);
1655  }
1656  }
1657 
1658  if (fEventKeepingSuspended && fVerbosity >= warnings) {
1659  G4cout <<
1660  "WARNING: G4VisManager::EndOfRun: Automatic event keeping has been suspended."
1661  "\n The number of events in the run exceeded the maximum, "
1662  << fpScene->GetMaxNumberOfKeptEvents() <<
1663  ", that can be kept by the vis manager." <<
1664  "\n The number of events kept by the vis manager can be changed with"
1665  "\n \"/vis/scene/endOfEventAction accumulate <N>\", where N is the"
1666  "\n maximum number you wish to allow. N < 0 means \"unlimited\"."
1667  << G4endl;
1668  }
1669  }
1670  fEventRefreshing = false;
1671 
1672  G4RunManager* runManager = G4RunManager::GetRunManager();
1673  const G4Run* currentRun = runManager->GetCurrentRun();
1674 
1675  G4int nKeptEvents = 0;
1676  const std::vector<const G4Event*>* events =
1677  currentRun? currentRun->GetEventVector(): 0;
1678  if (events) nKeptEvents = events->size();
1679 
1680  if (nKeptEvents && !fKeptLastEvent) {
1681  if (!valid && fVerbosity >= warnings) G4cout << "WARNING: ";
1682  if (fVerbosity >= warnings) {
1683  G4cout << nKeptEvents;
1684  if (nKeptEvents == 1) G4cout << " event has";
1685  else G4cout << " events have";
1686  G4cout << " been kept for refreshing and/or reviewing." << G4endl;
1687  }
1688  static G4bool warned = false;
1689  if (!valid && fVerbosity >= warnings && !warned) {
1690  G4cout <<
1691  " Only useful if before starting the run:"
1692  "\n a) trajectories are stored (\"/vis/scene/add/trajectories [smooth|rich]\"), or"
1693  "\n b) the Draw method of any hits or digis is implemented."
1694  "\n To view trajectories, hits or digis:"
1695  "\n open a viewer, draw a volume, \"/vis/scene/add/trajectories\""
1696  "\n \"/vis/scene/add/hits\" or \"/vis/scene/add/digitisations\""
1697  "\n and, possibly, \"/vis/viewer/flush\"."
1698  "\n To see all events: \"/vis/scene/endOfEventAction accumulate\"."
1699  "\n To see events individually: \"/vis/reviewKeptEvents\"."
1700  << G4endl;
1701  warned = true;
1702  }
1703  }
1704 }
1705 
1706 void G4VisManager::ClearTransientStoreIfMarked(){
1707  // Assumes valid view.
1708  if (fpSceneHandler->GetMarkForClearingTransientStore()) {
1709  fpSceneHandler->SetMarkForClearingTransientStore(false);
1710  fpSceneHandler->ClearTransientStore();
1711  }
1712  // Record if transients drawn. These local flags are only set
1713  // *after* ClearTransientStore. In the code in G4VSceneHandler
1714  // triggered by ClearTransientStore, use these flags so that
1715  // event refreshing is not done too early.
1716  fTransientsDrawnThisEvent = fpSceneHandler->GetTransientsDrawnThisEvent();
1717  fTransientsDrawnThisRun = fpSceneHandler->GetTransientsDrawnThisRun();
1718 }
1719 
1721 {
1722  fTransientsDrawnThisRun = false;
1723  fTransientsDrawnThisEvent = false;
1725  for (i = fAvailableSceneHandlers.begin();
1726  i != fAvailableSceneHandlers.end(); ++i) {
1727  (*i)->SetTransientsDrawnThisEvent(false);
1728  (*i)->SetTransientsDrawnThisRun(false);
1729  }
1730 }
1731 
1733  G4String viewerShortName (viewerName);
1734  viewerShortName = viewerShortName (0, viewerShortName.find (' '));
1735  return viewerShortName.strip ();
1736 }
1737 
1738 G4VViewer* G4VisManager::GetViewer (const G4String& viewerName) const {
1739  G4String viewerShortName = ViewerShortName (viewerName);
1740  size_t nHandlers = fAvailableSceneHandlers.size ();
1741  size_t iHandler, iViewer;
1742  G4VViewer* viewer = 0;
1743  G4bool found = false;
1744  for (iHandler = 0; iHandler < nHandlers; iHandler++) {
1745  G4VSceneHandler* sceneHandler = fAvailableSceneHandlers [iHandler];
1746  const G4ViewerList& viewerList = sceneHandler -> GetViewerList ();
1747  for (iViewer = 0; iViewer < viewerList.size (); iViewer++) {
1748  viewer = viewerList [iViewer];
1749  if (viewerShortName == viewer -> GetShortName ()) {
1750  found = true;
1751  break;
1752  }
1753  }
1754  if (found) break;
1755  }
1756  if (found) return viewer;
1757  else return 0;
1758 }
1759 
1760 std::vector<G4String> G4VisManager::VerbosityGuidanceStrings;
1761 
1763  G4String rs;
1764  switch (verbosity) {
1765  case quiet: rs = "quiet (0)"; break;
1766  case startup: rs = "startup (1)"; break;
1767  case errors: rs = "errors (2)"; break;
1768  case warnings: rs = "warnings (3)"; break;
1769  case confirmations: rs = "confirmations (4)"; break;
1770  case parameters: rs = "parameters (5)"; break;
1771  case all: rs = "all (6)"; break;
1772  }
1773  return rs;
1774 }
1775 
1778  G4String ss(verbosityString); ss.toLower();
1779  Verbosity verbosity;
1780  if (ss(0) == 'q') verbosity = quiet;
1781  else if (ss(0) == 's') verbosity = startup;
1782  else if (ss(0) == 'e') verbosity = errors;
1783  else if (ss(0) == 'w') verbosity = warnings;
1784  else if (ss(0) == 'c') verbosity = confirmations;
1785  else if (ss(0) == 'p') verbosity = parameters;
1786  else if (ss(0) == 'a') verbosity = all;
1787  else {
1788  G4int intVerbosity;
1789  std::istringstream is(ss);
1790  is >> intVerbosity;
1791  if (!is) {
1792  G4cout << "ERROR: G4VisManager::GetVerbosityValue: invalid verbosity \""
1793  << verbosityString << "\"";
1794  for (size_t i = 0; i < VerbosityGuidanceStrings.size(); ++i) {
1795  G4cout << '\n' << VerbosityGuidanceStrings[i];
1796  }
1797  verbosity = warnings;
1798  G4cout << "\n Returning " << VerbosityString(verbosity)
1799  << G4endl;
1800  }
1801  else {
1802  verbosity = GetVerbosityValue(intVerbosity);
1803  }
1804  }
1805  return verbosity;
1806 }
1807 
1809  Verbosity verbosity;
1810  if (intVerbosity < quiet) verbosity = quiet;
1811  else if (intVerbosity > all) verbosity = all;
1812  else verbosity = Verbosity(intVerbosity);
1813  return verbosity;
1814 }
1815 
1817  return fVerbosity;
1818 }
1819 
1821  fVerbosity = GetVerbosityValue(intVerbosity);
1822 }
1823 
1824 void G4VisManager::SetVerboseLevel (const G4String& verbosityString) {
1825  fVerbosity = GetVerbosityValue(verbosityString);
1826 }
1827 
1828 G4bool G4VisManager::IsValidView () {
1829 
1830  if (!fInitialised) Initialise ();
1831 
1832  static G4bool noGSPrinting = true;
1833  if (!fpGraphicsSystem) {
1834  // Limit printing - we do not want printing if the user simply does
1835  // not want to use graphics, e.g., in batch mode.
1836  if (noGSPrinting) {
1837  noGSPrinting = false;
1838  if (fVerbosity >= warnings) {
1839  G4cout <<
1840  "WARNING: G4VisManager::IsValidView(): Attempt to draw when no graphics system"
1841  "\n has been instantiated. Use \"/vis/open\" or \"/vis/sceneHandler/create\"."
1842  "\n Alternatively, to avoid this message, suppress instantiation of vis"
1843  "\n manager (G4VisExecutive), possibly by setting G4VIS_NONE, and ensure"
1844  "\n drawing code is executed only if G4VVisManager::GetConcreteInstance()"
1845  "\n is non-zero."
1846  << G4endl;
1847  }
1848  }
1849  return false;
1850  }
1851 
1852  if ((!fpScene) || (!fpSceneHandler) || (!fpViewer)) {
1853  if (fVerbosity >= errors) {
1854  G4cout <<
1855  "ERROR: G4VisManager::IsValidView(): Current view is not valid."
1856  << G4endl;
1857  PrintInvalidPointers ();
1858  }
1859  return false;
1860  }
1861 
1862  if (fpScene != fpSceneHandler -> GetScene ()) {
1863  if (fVerbosity >= errors) {
1864  G4cout << "ERROR: G4VisManager::IsValidView ():";
1865  if (fpSceneHandler -> GetScene ()) {
1866  G4cout <<
1867  "\n The current scene \""
1868  << fpScene -> GetName ()
1869  << "\" is not handled by"
1870  "\n the current scene handler \""
1871  << fpSceneHandler -> GetName ()
1872  << "\""
1873  "\n (it currently handles scene \""
1874  << fpSceneHandler -> GetScene () -> GetName ()
1875  << "\")."
1876  "\n Either:"
1877  "\n (a) attach it to the scene handler with"
1878  "\n /vis/sceneHandler/attach "
1879  << fpScene -> GetName ()
1880  << ", or"
1881  "\n (b) create a new scene handler with "
1882  "\n /vis/sceneHandler/create <graphics-system>,"
1883  "\n in which case it should pick up the the new scene."
1884  << G4endl;
1885  }
1886  else {
1887  G4cout << "\n Scene handler \""
1888  << fpSceneHandler -> GetName ()
1889  << "\" has null scene pointer."
1890  "\n Attach a scene with /vis/sceneHandler/attach [<scene-name>]"
1891  << G4endl;
1892  }
1893  }
1894  return false;
1895  }
1896 
1897  const G4ViewerList& viewerList = fpSceneHandler -> GetViewerList ();
1898  if (viewerList.size () == 0) {
1899  if (fVerbosity >= errors) {
1900  G4cout <<
1901  "ERROR: G4VisManager::IsValidView (): the current scene handler\n \""
1902  << fpSceneHandler -> GetName ()
1903  << "\" has no viewers. Do /vis/viewer/create."
1904  << G4endl;
1905  }
1906  return false;
1907  }
1908 
1909  G4bool isValid = true;
1910  if (fpScene -> IsEmpty ()) { // Add world by default if possible...
1911  G4bool warn(fVerbosity >= warnings);
1912  G4bool successful = fpScene -> AddWorldIfEmpty (warn);
1913  if (!successful || fpScene -> IsEmpty ()) { // If still empty...
1914  if (fVerbosity >= errors) {
1915  G4cout << "ERROR: G4VisManager::IsViewValid ():";
1916  G4cout <<
1917  "\n Attempt at some drawing operation when scene is empty."
1918  "\n Maybe the geometry has not yet been defined."
1919  " Try /run/initialize."
1920  << G4endl;
1921  }
1922  isValid = false;
1923  }
1924  else {
1925  G4UImanager::GetUIpointer()->ApplyCommand ("/vis/scene/notifyHandlers");
1926  if (fVerbosity >= warnings) {
1927  G4cout <<
1928  "WARNING: G4VisManager: the scene was empty, \"world\" has been"
1929  "\n added and the scene handlers notified.";
1930  G4cout << G4endl;
1931  }
1932  }
1933  }
1934  if (isValid) SetConcreteInstance(this);
1935  return isValid;
1936 }
1937 
1938 void
1940 {
1941  if (fVerbosity >= warnings) {
1942  G4cout<<"G4VisManager: No model factories registered with G4VisManager."<<G4endl;
1943  G4cout<<"G4VisManager::RegisterModelFactories() should be overridden in derived"<<G4endl;
1944  G4cout<<"class. See G4VisExecutive for an example."<<G4endl;
1945  }
1946 }