source: trunk/source/visualization/management/src/G4VisManager.cc @ 1241

Last change on this file since 1241 was 1241, checked in by garnier, 14 years ago

update

  • Property svn:mime-type set to text/cpp
File size: 52.7 KB
Line 
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: G4VisManager.cc,v 1.123 2010/03/08 16:34:17 lgarnier Exp $
27// GEANT4 tag $Name:  $
28//
29//
30// GEANT4 Visualization Manager - John Allison 02/Jan/1996.
31
32#include "G4VisManager.hh"
33
34#include "G4VisCommands.hh"
35#include "G4VisCommandsCompound.hh"
36#include "G4VisCommandsGeometry.hh"
37#include "G4VisCommandsGeometrySet.hh"
38#include "G4VisCommandsScene.hh"
39#include "G4VisCommandsSceneAdd.hh"
40#include "G4VisCommandsSceneHandler.hh"
41#include "G4VisCommandsViewer.hh"
42#include "G4VisCommandsViewerSet.hh"
43#include "G4UImanager.hh"
44#include "G4VisStateDependent.hh"
45#include "G4UIdirectory.hh"
46#include "G4VisFeaturesOfFukuiRenderer.hh"
47#include "G4VisFeaturesOfDAWNFILE.hh"
48#include "G4VisFeaturesOfOpenGL.hh"
49#include "G4VisFeaturesOfOpenInventor.hh"
50#include "G4VGraphicsSystem.hh"
51#include "G4VSceneHandler.hh"
52#include "G4VViewer.hh"
53#include "G4VPhysicalVolume.hh"
54#include "G4LogicalVolume.hh"
55#include "G4VSolid.hh"
56#include "G4Vector3D.hh"
57#include "G4Point3D.hh"
58#include "G4RotationMatrix.hh"
59#include "G4Polyline.hh"
60#include "G4Polyhedron.hh"
61#include "G4NURBS.hh"
62#include "G4NullModel.hh"
63#include "G4ModelingParameters.hh"
64#include "G4TransportationManager.hh"
65#include "G4VisCommandModelCreate.hh"
66#include "G4VisCommandsListManager.hh"
67#include "G4VisModelManager.hh"
68#include "G4VModelFactory.hh"
69#include "G4VisFilterManager.hh"
70#include "G4VTrajectoryModel.hh"
71#include "G4TrajectoryDrawByCharge.hh"
72#include "Randomize.hh"
73#include "G4RunManager.hh"
74#include "G4EventManager.hh"
75#include "G4Run.hh"
76#include "G4Event.hh"
77#include <sstream>
78
79G4VisManager* G4VisManager::fpInstance = 0;
80
81G4VisManager::G4VisManager ():
82  fVerbose         (1),
83  fInitialised     (false),
84  fpUserVisAction  (0),
85  fpGraphicsSystem (0),
86  fpScene          (0),
87  fpSceneHandler   (0),
88  fpViewer         (0),
89  fVerbosity       (warnings),
90  fpStateDependent (0),
91  fEventRefreshing          (false),
92  fTransientsDrawnThisRun   (false),
93  fTransientsDrawnThisEvent (false),
94  fEventKeepingSuspended    (false),
95  fKeptLastEvent            (false),
96  fpRequestedEvent (0),
97  fAbortReviewKeptEvents    (false)
98  // All other objects use default constructors.
99{
100  fpTrajDrawModelMgr = new G4VisModelManager<G4VTrajectoryModel>("/vis/modeling/trajectories");
101  fpTrajFilterMgr = new G4VisFilterManager<G4VTrajectory>("/vis/filtering/trajectories");
102  fpHitFilterMgr = new G4VisFilterManager<G4VHit>("/vis/filtering/hits");
103
104  VerbosityGuidanceStrings.push_back
105    ("Simple graded message scheme - digit or string (1st character defines):");
106  VerbosityGuidanceStrings.push_back
107    ("  0) quiet,         // Nothing is printed.");
108  VerbosityGuidanceStrings.push_back
109    ("  1) startup,       // Startup and endup messages are printed...");
110  VerbosityGuidanceStrings.push_back
111    ("  2) errors,        // ...and errors...");
112  VerbosityGuidanceStrings.push_back
113    ("  3) warnings,      // ...and warnings...");
114  VerbosityGuidanceStrings.push_back
115    ("  4) confirmations, // ...and confirming messages...");
116  VerbosityGuidanceStrings.push_back
117    ("  5) parameters,    // ...and parameters of scenes and views...");
118  VerbosityGuidanceStrings.push_back
119    ("  6) all            // ...and everything available.");
120
121  if (fpInstance) {
122    G4Exception
123      ("G4VisManager: attempt to Construct more than one VisManager.");
124  }
125  else {
126
127    fpInstance = this;
128    SetConcreteInstance(this);
129
130    fpStateDependent = new G4VisStateDependent (this);
131    // No need to delete this; G4StateManager does this.
132
133    if (fVerbosity >= startup) {
134      G4cout << "Visualization Manager instantiating..." << G4endl;
135    }
136
137    // Note: The specific graphics systems must be instantiated in a
138    // higher level library to avoid circular dependencies.  Also,
139    // some specifically need additional external libararies that the
140    // user must supply.  Therefore we ask the user to implement
141    // RegisterGraphicsSystems() and RegisterModelFactories()
142    // in a subclass.  We have to wait for the subclass to instantiate
143    // so RegisterGraphicsSystems() cannot be called from this
144    // constructor; it is called from Initialise().  So we ask the
145    // user:
146    //   (a) to write a subclass and implement  RegisterGraphicsSystems()
147    //       and RegisterModelFactories().  See
148    //       visualization/include/G4VisExecutive.hh/icc as an example.
149    //   (b) instantiate the subclass.
150    //   (c) invoke the Initialise() method of the subclass.
151    // For example:
152    //   ...
153    // #ifdef G4VIS_USE
154    //   // Instantiate and initialise Visualization Manager.
155    //   G4VisManager* visManager = new G4VisExecutive;
156    //   visManager -> SetVerboseLevel (Verbose);
157    //   visManager -> Initialise ();
158    // #endif
159    //   // (Don't forget to delete visManager;)
160    //   ...
161
162    // Make top level command directory...
163    G4UIcommand* directory;
164    directory = new G4UIdirectory ("/vis/");
165    directory -> SetGuidance ("Visualization commands.");
166    fDirectoryList.push_back (directory);
167
168    // Instantiate top level basic commands
169    G4VVisCommand::SetVisManager (this);  // Sets shared pointer
170    RegisterMessenger(new G4VisCommandVerbose);
171    RegisterMessenger(new G4VisCommandInitialize);
172  }
173}
174
175G4VisManager::~G4VisManager () {
176  fpInstance = 0;
177  size_t i;
178  for (i = 0; i < fSceneList.size (); ++i) {
179    delete fSceneList[i];
180  }
181  for (i = 0; i < fAvailableSceneHandlers.size (); ++i) {
182  if (fAvailableSceneHandlers[i] != NULL) {
183    delete fAvailableSceneHandlers[i];
184  }
185  }
186  for (i = 0; i < fAvailableGraphicsSystems.size (); ++i) {
187    if (fAvailableGraphicsSystems[i]) {
188      delete fAvailableGraphicsSystems[i];
189    }
190  }
191  if (fVerbosity >= startup) {
192    G4cout << "Graphics systems deleted." << G4endl;
193    G4cout << "Visualization Manager deleting..." << G4endl;
194  }
195  for (i = 0; i < fMessengerList.size (); ++i) {
196    delete fMessengerList[i];
197  }
198  for (i = 0; i < fDirectoryList.size (); ++i) {
199    delete fDirectoryList[i];
200  }
201
202  delete fpTrajDrawModelMgr;
203  delete fpTrajFilterMgr;
204  delete fpHitFilterMgr;
205}
206
207G4VisManager* G4VisManager::GetInstance () {
208  if (!fpInstance) {
209    G4Exception
210      ("G4VisManager::GetInstance: VisManager not yet instantiated!");
211  }
212  return fpInstance;
213}
214
215void G4VisManager::Initialise () {
216
217  if (fInitialised && fVerbosity >= warnings) {
218    G4cout << "WARNING: G4VisManager::Initialise: already initialised."
219           << G4endl;
220    return;
221  }
222
223  if (fVerbosity >= startup) {
224    G4cout << "Visualization Manager initialising..." << G4endl;
225  }
226
227  if (fVerbosity >= parameters) {
228    G4cout <<
229      "\nYou have instantiated your own Visualization Manager, inheriting"
230      "\n  G4VisManager and implementing RegisterGraphicsSystems(), in which"
231      "\n  you should, normally, instantiate drivers which do not need"
232      "\n  external packages or libraries, and, optionally, drivers under"
233      "\n  control of environment variables."
234      "\n  Also you should implement RegisterModelFactories()."
235      "\n  See visualization/include/G4VisExecutive.hh/icc, for example."
236      "\n  In your main() you will have something like:"
237      "\n  #ifdef G4VIS_USE"
238      "\n    G4VisManager* visManager = new G4VisExecutive;"
239      "\n    visManager -> SetVerboseLevel (Verbose);"
240      "\n    visManager -> Initialize ();"
241      "\n  #endif"
242      "\n  (Don't forget to delete visManager;)"
243      "\n"
244         << G4endl;
245  }
246
247  if (fVerbosity >= startup) {
248    G4cout << "Registering graphics systems..." << G4endl;
249  }
250
251  RegisterGraphicsSystems ();
252
253  if (fVerbosity >= startup) {
254    G4cout <<
255      "\nYou have successfully registered the following graphics systems."
256         << G4endl;
257    PrintAvailableGraphicsSystems ();
258    G4cout << G4endl;
259  }
260
261  // Make command directories for commands instantiated in the
262  // modeling subcategory...
263  G4UIcommand* directory;
264  directory = new G4UIdirectory ("/vis/modeling/");
265  directory -> SetGuidance ("Modeling commands.");
266  fDirectoryList.push_back (directory);
267  directory = new G4UIdirectory ("/vis/modeling/trajectories/");
268  directory -> SetGuidance ("Trajectory model commands.");
269  fDirectoryList.push_back (directory);
270  directory = new G4UIdirectory ("/vis/modeling/trajectories/create/");
271  directory -> SetGuidance ("Create trajectory models and messengers.");
272  fDirectoryList.push_back (directory);
273
274  // Filtering command directory
275  directory = new G4UIdirectory ("/vis/filtering/");
276  directory -> SetGuidance ("Filtering commands.");
277  fDirectoryList.push_back (directory);
278  directory = new G4UIdirectory ("/vis/filtering/trajectories/");
279  directory -> SetGuidance ("Trajectory filtering commands.");
280  fDirectoryList.push_back (directory);
281  directory = new G4UIdirectory ("/vis/filtering/trajectories/create/");
282  directory -> SetGuidance ("Create trajectory filters and messengers.");
283  fDirectoryList.push_back (directory);
284  directory = new G4UIdirectory ("/vis/filtering/hits/");
285  directory -> SetGuidance ("Hit filtering commands.");
286  fDirectoryList.push_back (directory);
287  directory = new G4UIdirectory ("/vis/filtering/hits/create/");
288  directory -> SetGuidance ("Create hit filters and messengers.");
289  fDirectoryList.push_back (directory);
290
291  RegisterMessengers ();
292
293  if (fVerbosity >= startup) {
294    G4cout << "Registering model factories..." << G4endl;
295  }
296
297  RegisterModelFactories();
298
299  if (fVerbosity >= startup) {
300    G4cout <<
301      "\nYou have successfully registered the following model factories."
302         << G4endl;
303    PrintAvailableModels (fVerbosity);
304    G4cout << G4endl;
305  }
306
307  fInitialised = true;
308}
309
310void G4VisManager::Enable() {
311  if (IsValidView ()) {
312    if (fVerbosity >= confirmations) {
313      G4cout << "G4VisManager::Enable: visualization enabled." << G4endl;
314    }
315  }
316  else {
317    if (fVerbosity >= warnings) {
318      G4cout <<
319        "G4VisManager::Enable: WARNING: visualization remains disabled for"
320        "\n  above reasons.  Rectifying with valid vis commands will"
321        "\n  automatically enable."
322             << G4endl;
323    }
324  }
325}
326
327void G4VisManager::Disable() {
328  SetConcreteInstance(0);
329  if (fVerbosity >= confirmations) {
330    G4cout <<
331      "G4VisManager::Disable: visualization disabled."
332      "\n  The pointer returned by GetConcreteInstance will be zero."
333      "\n  Note that it will become enabled after some valid vis commands."
334           << G4endl;
335  }
336}
337
338const G4GraphicsSystemList& G4VisManager::GetAvailableGraphicsSystems () {
339  G4int nSystems = fAvailableGraphicsSystems.size ();
340  if (nSystems == 0) {
341    if (fVerbosity >= warnings) {
342      G4cout << "G4VisManager::GetAvailableGraphicsSystems: WARNING: no"
343        "\n graphics system available!"
344        "\n  1) Did you have environment variables G4VIS_BUILD_xxxx_DRIVER set"
345        "\n     when you compiled/built the visualization code?"
346        "\n  2) Did you instantiate your own Visualization Manager and forget"
347        "\n     to implement RegisterGraphicsSystems correctly?"
348        "\n  3) You can register your own graphics system, e.g.,"
349        "\n     visManager->RegisterGraphicsSystem(new MyGraphicsSystem);)"
350        "\n     after instantiating your vis manager and before"
351        "\n     visManager->Initialize()."
352             << G4endl;
353    }
354  }
355  return fAvailableGraphicsSystems;
356}
357
358G4bool G4VisManager::RegisterGraphicsSystem (G4VGraphicsSystem* pSystem) {
359  G4bool happy = true;
360  if (pSystem) {
361    fAvailableGraphicsSystems.push_back (pSystem);
362    if (fVerbosity >= confirmations) {
363      G4cout << "G4VisManager::RegisterGraphicsSystem: "
364             << pSystem -> GetName ();
365      if (pSystem -> GetNickname () != "") {
366        G4cout << " (" << pSystem -> GetNickname () << ")";
367      }
368      G4cout << " registered." << G4endl;
369    }
370  }
371  else {
372    if (fVerbosity >= errors) {
373      G4cout << "G4VisManager::RegisterGraphicsSystem: null pointer!"
374             << G4endl;
375    }
376    happy=false;
377  }
378  return happy;
379}
380
381const G4VTrajectoryModel*
382G4VisManager::CurrentTrajDrawModel() const
383{
384  assert (0 != fpTrajDrawModelMgr);
385
386  const G4VTrajectoryModel* model = fpTrajDrawModelMgr->Current();
387
388  if (0 == model) {
389    // No model was registered with the trajectory model manager.
390    // Use G4TrajectoryDrawByCharge as a default.
391    fpTrajDrawModelMgr->Register(new G4TrajectoryDrawByCharge("AutoGenerated"));
392
393    if (fVerbosity >= warnings) {
394      G4cout<<"G4VisManager: Using G4TrajectoryDrawByCharge as default trajectory model."<<G4endl;
395      G4cout<<"See commands in /vis/modeling/trajectories/ for other options."<<G4endl;
396    }
397  }
398
399  model = fpTrajDrawModelMgr->Current();
400  assert (0 != model); // Should definitely exist now
401
402  return model;
403}
404
405void G4VisManager::RegisterModel(G4VTrajectoryModel* model)
406{
407  fpTrajDrawModelMgr->Register(model);
408}
409
410void
411G4VisManager::RegisterModelFactory(G4TrajDrawModelFactory* factory)
412{
413  fpTrajDrawModelMgr->Register(factory);
414}
415
416void G4VisManager::RegisterModel(G4VFilter<G4VTrajectory>* model)
417{
418  fpTrajFilterMgr->Register(model);
419}
420
421void
422G4VisManager::RegisterModelFactory(G4TrajFilterFactory* factory)
423{
424  fpTrajFilterMgr->Register(factory);
425}
426
427void G4VisManager::RegisterModel(G4VFilter<G4VHit>* model)
428{
429  fpHitFilterMgr->Register(model);
430}
431
432void
433G4VisManager::RegisterModelFactory(G4HitFilterFactory* factory)
434{
435  fpHitFilterMgr->Register(factory);
436}
437
438void G4VisManager::SelectTrajectoryModel(const G4String& model)
439{
440   fpTrajDrawModelMgr->SetCurrent(model);
441}
442
443void G4VisManager::Draw (const G4Circle& circle,
444                         const G4Transform3D& objectTransform) {
445  if (IsValidView ()) {
446    ClearTransientStoreIfMarked();
447    fpSceneHandler -> BeginPrimitives (objectTransform);
448    fpSceneHandler -> AddPrimitive (circle);
449    fpSceneHandler -> EndPrimitives ();
450  }
451}
452
453void G4VisManager::Draw (const G4NURBS& nurbs,
454                         const G4Transform3D& objectTransform) {
455  if (IsValidView ()) {
456    ClearTransientStoreIfMarked();
457    fpSceneHandler -> BeginPrimitives (objectTransform);
458    fpSceneHandler -> AddPrimitive (nurbs);
459    fpSceneHandler -> EndPrimitives ();
460  }
461}
462
463void G4VisManager::Draw (const G4Polyhedron& polyhedron,
464                         const G4Transform3D& objectTransform) {
465  if (IsValidView ()) {
466    ClearTransientStoreIfMarked();
467    fpSceneHandler -> BeginPrimitives (objectTransform);
468    fpSceneHandler -> AddPrimitive (polyhedron);
469    fpSceneHandler -> EndPrimitives ();
470  }
471}
472
473void G4VisManager::Draw (const G4Polyline& line,
474                         const G4Transform3D& objectTransform) {
475  if (IsValidView ()) {
476    ClearTransientStoreIfMarked();
477    fpSceneHandler -> BeginPrimitives (objectTransform);
478    fpSceneHandler -> AddPrimitive (line);
479    fpSceneHandler -> EndPrimitives ();
480  }
481}
482
483void G4VisManager::Draw (const G4Polymarker& polymarker,
484                         const G4Transform3D& objectTransform) {
485  if (IsValidView ()) {
486    ClearTransientStoreIfMarked();
487    fpSceneHandler -> BeginPrimitives (objectTransform);
488    fpSceneHandler -> AddPrimitive (polymarker);
489    fpSceneHandler -> EndPrimitives ();
490  }
491}
492
493void G4VisManager::Draw (const G4Scale& scale,
494                         const G4Transform3D& objectTransform) {
495  if (IsValidView ()) {
496    ClearTransientStoreIfMarked();
497    fpSceneHandler -> BeginPrimitives (objectTransform);
498    fpSceneHandler -> AddPrimitive (scale);
499    fpSceneHandler -> EndPrimitives ();
500  }
501}
502
503void G4VisManager::Draw (const G4Square& square,
504                         const G4Transform3D& objectTransform) {
505  if (IsValidView ()) {
506    ClearTransientStoreIfMarked();
507    fpSceneHandler -> BeginPrimitives (objectTransform);
508    fpSceneHandler -> AddPrimitive (square);
509    fpSceneHandler -> EndPrimitives ();
510  }
511}
512
513void G4VisManager::Draw (const G4Text& text,
514                         const G4Transform3D& objectTransform) {
515  if (IsValidView ()) {
516    ClearTransientStoreIfMarked();
517    fpSceneHandler -> BeginPrimitives (objectTransform);
518    fpSceneHandler -> AddPrimitive (text);
519    fpSceneHandler -> EndPrimitives ();
520  }
521}
522
523void G4VisManager::Draw2D (const G4Circle& circle,
524                           const G4Transform3D& objectTransform)
525{
526  if (IsValidView()) {
527    ClearTransientStoreIfMarked();
528    fpSceneHandler -> BeginPrimitives2D(objectTransform);
529    fpSceneHandler -> AddPrimitive(circle);
530    fpSceneHandler -> EndPrimitives2D();
531  }
532}
533
534void G4VisManager::Draw2D (const G4NURBS& nurbs,
535                           const G4Transform3D& objectTransform)
536{
537  if (IsValidView()) {
538    ClearTransientStoreIfMarked();
539    fpSceneHandler -> BeginPrimitives2D(objectTransform);
540    fpSceneHandler -> AddPrimitive(nurbs);
541    fpSceneHandler -> EndPrimitives2D();
542  }
543}
544
545void G4VisManager::Draw2D (const G4Polyhedron& polyhedron,
546                           const G4Transform3D& objectTransform)
547{
548  if (IsValidView()) {
549    ClearTransientStoreIfMarked();
550    fpSceneHandler -> BeginPrimitives2D(objectTransform);
551    fpSceneHandler -> AddPrimitive(polyhedron);
552    fpSceneHandler -> EndPrimitives2D();
553  }
554}
555
556void G4VisManager::Draw2D (const G4Polyline& line,
557                           const G4Transform3D& objectTransform)
558{
559  if (IsValidView()) {
560    ClearTransientStoreIfMarked();
561    fpSceneHandler -> BeginPrimitives2D(objectTransform);
562    fpSceneHandler -> AddPrimitive(line);
563    fpSceneHandler -> EndPrimitives2D();
564  }
565}
566
567void G4VisManager::Draw2D (const G4Polymarker& polymarker,
568                           const G4Transform3D& objectTransform)
569{
570  if (IsValidView()) {
571    ClearTransientStoreIfMarked();
572    fpSceneHandler -> BeginPrimitives2D(objectTransform);
573    fpSceneHandler -> AddPrimitive(polymarker);
574    fpSceneHandler -> EndPrimitives2D();
575  }
576}
577
578void G4VisManager::Draw2D (const G4Square& square,
579                           const G4Transform3D& objectTransform)
580{
581  if (IsValidView()) {
582    ClearTransientStoreIfMarked();
583    fpSceneHandler -> BeginPrimitives2D(objectTransform);
584    fpSceneHandler -> AddPrimitive(square);
585    fpSceneHandler -> EndPrimitives2D();
586  }
587}
588
589void G4VisManager::Draw2D (const G4Text& text,
590                           const G4Transform3D& objectTransform)
591{
592  if (IsValidView()) {
593    ClearTransientStoreIfMarked();
594    fpSceneHandler -> BeginPrimitives2D(objectTransform);
595    fpSceneHandler -> AddPrimitive(text);
596    fpSceneHandler -> EndPrimitives2D();
597  }
598}
599
600void G4VisManager::Draw (const G4VHit& hit) {
601  if (IsValidView ()) {
602    ClearTransientStoreIfMarked();
603    fpSceneHandler -> AddCompound (hit);
604  }
605}
606
607void G4VisManager::Draw (const G4VTrajectory& traj,
608                         G4int i_mode) {
609  if (IsValidView ()) {
610    ClearTransientStoreIfMarked();
611    fpSceneHandler -> SetModel (&dummyTrajectoriesModel);
612    dummyTrajectoriesModel.SetDrawingMode(i_mode);
613    fpSceneHandler -> AddCompound (traj);
614  }
615}
616
617void G4VisManager::Draw (const G4LogicalVolume& logicalVol,
618                         const G4VisAttributes& attribs,
619                         const G4Transform3D& objectTransform) {
620  // Find corresponding solid.
621  G4VSolid* pSol = logicalVol.GetSolid ();
622  Draw (*pSol, attribs, objectTransform);
623}
624
625void G4VisManager::Draw (const G4VSolid& solid,
626                         const G4VisAttributes& attribs,
627                         const G4Transform3D& objectTransform) {
628  if (IsValidView ()) {
629    ClearTransientStoreIfMarked();
630    fpSceneHandler -> PreAddSolid (objectTransform, attribs);
631    solid.DescribeYourselfTo (*fpSceneHandler);
632    fpSceneHandler -> PostAddSolid ();
633  }
634}
635
636void G4VisManager::Draw (const G4VPhysicalVolume& physicalVol,
637                         const G4VisAttributes& attribs,
638                         const G4Transform3D& objectTransform) {
639  // Find corresponding logical volume and solid.
640  G4LogicalVolume* pLV  = physicalVol.GetLogicalVolume ();
641  G4VSolid*        pSol = pLV -> GetSolid ();
642  Draw (*pSol, attribs, objectTransform);
643}
644
645void G4VisManager::CreateSceneHandler (G4String name) {
646  if (!fInitialised) Initialise ();
647  if (fpGraphicsSystem) {
648    G4VSceneHandler* pSceneHandler =
649      fpGraphicsSystem -> CreateSceneHandler (name);
650    if (pSceneHandler) {
651      fAvailableSceneHandlers.push_back (pSceneHandler);
652      fpSceneHandler = pSceneHandler;                         // Make current.
653    }
654    else {
655      if(fVerbosity >= errors) {
656        G4cout << "ERROR in G4VisManager::CreateSceneHandler during "
657               << fpGraphicsSystem -> GetName ()
658               << " scene handler creation.\n  No action taken."
659               << G4endl;
660      }
661    }
662  }
663  else PrintInvalidPointers ();
664}
665
666void G4VisManager::CreateViewer (G4String name,G4String XGeometry) {
667
668  if (!fInitialised) Initialise ();
669
670  if (!fpSceneHandler) {
671    PrintInvalidPointers ();
672    return;
673  }
674
675  G4VViewer* p = fpGraphicsSystem -> CreateViewer (*fpSceneHandler, name);
676
677  if (!p) {
678    if (fVerbosity >= errors) {
679      G4cout << "ERROR in G4VisManager::CreateViewer during "
680             << fpGraphicsSystem -> GetName ()
681             << " viewer creation.\n  No action taken."
682             << G4endl;
683    }
684    return;
685  }
686
687  if (p -> GetViewId() < 0) {
688    if (fVerbosity >= errors) {
689      G4cout << "ERROR in G4VisManager::CreateViewer during "
690             << fpGraphicsSystem -> GetName ()
691             << " viewer initialisation.\n  No action taken."
692             << G4endl;
693    }
694    return;
695  }
696
697  // Viewer is created, now we can set geometry parameters
698  // Before 12/2008, it was done in G4VViewer.cc but it did not have to be there!
699   
700  G4ViewParameters initialvp = p -> GetViewParameters();
701  initialvp.SetXGeometryString(XGeometry); //parse string and store parameters
702  p -> SetViewParameters(initialvp);
703  p -> Initialise ();  // (Viewer itself may change view parameters further.)
704
705  fpViewer = p;                             // Make current.
706  fpSceneHandler -> AddViewerToList (fpViewer);
707  fpSceneHandler -> SetCurrentViewer (fpViewer);
708  if (fVerbosity >= confirmations) {
709    G4cout << "G4VisManager::CreateViewer: new viewer created."
710           << G4endl;
711  }
712
713  const G4ViewParameters& vp = fpViewer->GetViewParameters();
714  if (fVerbosity >= parameters) {
715    G4cout << " view parameters are:\n  " << vp << G4endl;
716  }
717
718  if (vp.IsCulling () && vp.IsCullingInvisible ()) {
719    static G4bool warned = false;
720    if (fVerbosity >= confirmations) {
721      if (!warned) {
722        G4cout <<
723  "NOTE: objects with visibility flag set to \"false\""
724  " will not be drawn!"
725  "\n  \"/vis/viewer/set/culling global false\" to Draw such objects."
726  "\n  Also see other \"/vis/viewer/set\" commands."
727               << G4endl;
728        warned = true;
729      }
730    }
731  }
732  if (vp.IsCullingCovered ()) {
733    static G4bool warned = false;
734    if (fVerbosity >= warnings) {
735      if (!warned) {
736        G4cout <<
737  "WARNING: covered objects in solid mode will not be rendered!"
738  "\n  \"/vis/viewer/set/culling coveredDaughters false\" to reverse this."
739  "\n  Also see other \"/vis/viewer/set\" commands."
740               << G4endl;
741        warned = true;
742      }
743    }
744  }
745}
746
747void G4VisManager::GeometryHasChanged () {
748  if (fVerbosity >= confirmations) {
749    G4cout << "G4VisManager::GeometryHasChanged() called." << G4endl;
750  }
751
752  // Change the world...
753  G4VPhysicalVolume* pWorld =
754    G4TransportationManager::GetTransportationManager ()
755    -> GetNavigatorForTracking () -> GetWorldVolume ();
756  if (!pWorld) {
757    if (fVerbosity >= warnings) {
758      G4cout << "WARNING: There is no world volume!" << G4endl;
759    }
760  }
761
762  // Check scenes.
763  G4SceneList& sceneList = fSceneList;
764  G4int iScene, nScenes = sceneList.size ();
765  for (iScene = 0; iScene < nScenes; iScene++) {
766    G4Scene* pScene = sceneList [iScene];
767    std::vector<G4VModel*>& modelList = pScene -> SetRunDurationModelList ();
768
769    if (modelList.size ()) {
770      G4bool modelInvalid;
771      do {  // Remove, if required, one at a time.
772        modelInvalid = false;
773        std::vector<G4VModel*>::iterator iterModel;
774        for (iterModel = modelList.begin();
775             iterModel != modelList.end();
776             ++iterModel) {
777          modelInvalid = !((*iterModel) -> Validate (fVerbosity >= warnings));
778          if (modelInvalid) {
779            // Model invalid - remove and break.
780            if (fVerbosity >= warnings) {
781              G4cout << "WARNING: Model \""
782                     << (*iterModel) -> GetGlobalDescription ()
783                     <<
784                "\" is no longer valid - being removed\n  from scene \""
785                     << pScene -> GetName () << "\""
786                     << G4endl;
787            }
788            modelList.erase (iterModel);
789            break;
790          }
791        }
792      } while (modelInvalid);
793
794      if (modelList.size () == 0) {
795        if (fVerbosity >= warnings) {
796          G4cout << "WARNING: No models left in this scene \""
797                 << pScene -> GetName ()
798                 << "\"."
799                 << G4endl;
800        }
801      }
802      else {
803        pScene->CalculateExtent();
804        G4UImanager::GetUIpointer () ->
805          ApplyCommand (G4String("/vis/scene/notifyHandlers " + pScene->GetName()));
806      }
807    }
808  }
809
810  // Check the manager's current scene...
811  if (fpScene && fpScene -> GetRunDurationModelList ().size () == 0) {
812    if (fVerbosity >= warnings) {
813      G4cout << "WARNING: The current scene \""
814             << fpScene -> GetName ()
815             << "\" has no models."
816             << G4endl;
817    }
818  }
819
820}
821void G4VisManager::NotifyHandlers () {
822
823  if (fVerbosity >= confirmations) {
824    G4cout << "G4VisManager::NotifyHandler() called." << G4endl;
825  }
826
827  // Check scenes.
828  G4SceneList& sceneList = fSceneList;
829  G4int iScene, nScenes = sceneList.size ();
830  for (iScene = 0; iScene < nScenes; iScene++) {
831    G4Scene* pScene = sceneList [iScene];
832    std::vector<G4VModel*>& modelList = pScene -> SetRunDurationModelList ();
833   
834    if (modelList.size ()) {
835      pScene->CalculateExtent();
836      G4UImanager::GetUIpointer () ->
837        ApplyCommand (G4String("/vis/scene/notifyHandlers " + pScene->GetName()));
838    }
839  }
840
841  // Check the manager's current scene...
842  if (fpScene && fpScene -> GetRunDurationModelList ().size () == 0) {
843    if (fVerbosity >= warnings) {
844      G4cout << "WARNING: The current scene \""
845             << fpScene -> GetName ()
846             << "\" has no models."
847             << G4endl;
848    }
849  }
850
851}
852
853G4bool G4VisManager::FilterTrajectory(const G4VTrajectory& trajectory)
854{
855  return fpTrajFilterMgr->Accept(trajectory);
856}   
857
858G4bool G4VisManager::FilterHit(const G4VHit& hit)
859{
860  return fpHitFilterMgr->Accept(hit);
861}   
862
863void G4VisManager::DispatchToModel(const G4VTrajectory& trajectory, G4int i_mode)
864{
865  G4bool visible(true);
866
867  // See if trajectory passes filter
868  G4bool passed = FilterTrajectory(trajectory);
869
870  if (!passed) {
871    // Draw invisible trajectory if trajectory failed filter and
872    // are filtering in soft mode
873    if (fpTrajFilterMgr->GetMode() == FilterMode::Soft) visible = false;
874    else {return;}
875  }
876
877  // Go on to draw trajectory
878  assert (0 != fpTrajDrawModelMgr);
879
880  const G4VTrajectoryModel* model = CurrentTrajDrawModel();
881
882  assert (0 != model); // Should exist
883
884  model->Draw(trajectory, i_mode, visible);
885}
886
887void G4VisManager::SetUserAction
888(G4VUserVisAction* pVisAction,
889 const G4VisExtent& extent) {
890  fpUserVisAction = pVisAction;
891  fUserVisActionExtent = extent;
892  if (extent.GetExtentRadius() <= 0.) {
893    if (fVerbosity >= warnings) {
894      G4cout <<
895        "WARNING: No extent set for user vis action.  (You may"
896        "\n  set it later when adding with /vis/scene/add/userAction.)"
897             << G4endl;
898    }
899  }
900}
901
902void G4VisManager::SetCurrentScene (G4Scene* pScene) {
903  if (pScene != fpScene) {
904    // A change of scene.  Therefore reset transients drawn flags.  All
905    // memory of previous transient proceessing thereby erased...
906    ResetTransientsDrawnFlags();
907  }
908  fpScene = pScene;
909}
910
911void G4VisManager::SetCurrentGraphicsSystem (G4VGraphicsSystem* pSystem) {
912  fpGraphicsSystem = pSystem;
913  if (fVerbosity >= confirmations) {
914    G4cout << "G4VisManager::SetCurrentGraphicsSystem: system now "
915           << pSystem -> GetName () << G4endl;
916  }
917  // If current scene handler is of same graphics system, leave unchanged.
918  // Else find the most recent scene handler of same graphics system.
919  // Or clear pointers.
920  if (!(fpSceneHandler && fpSceneHandler -> GetGraphicsSystem () == pSystem)) {
921    const G4SceneHandlerList& sceneHandlerList = fAvailableSceneHandlers;
922    G4int nSH = sceneHandlerList.size ();  // No. of scene handlers.
923    G4int iSH;
924    for (iSH = nSH - 1; iSH >= 0; iSH--) {
925      if (sceneHandlerList [iSH] -> GetGraphicsSystem () == pSystem) break;
926    }
927    if (iSH >= 0) {
928      fpSceneHandler = sceneHandlerList [iSH];
929      if (fVerbosity >= confirmations) {
930        G4cout << "  Scene Handler now "
931               << fpSceneHandler -> GetName () << G4endl;
932      }
933      if (fpScene != fpSceneHandler -> GetScene ()) {
934        fpScene = fpSceneHandler -> GetScene ();
935        if (fVerbosity >= confirmations) {
936          G4cout << "  Scene now \""
937                 << fpScene -> GetName () << "\"" << G4endl;
938        }
939      }
940      const G4ViewerList& viewerList = fpSceneHandler -> GetViewerList ();
941      if (viewerList.size ()) {
942        fpViewer = viewerList [0];
943        if (fVerbosity >= confirmations) {
944          G4cout << "  Viewer now " << fpViewer -> GetName () << G4endl;
945        }
946      }
947      else {
948        fpViewer = 0;
949      }
950    }
951    else {
952      fpSceneHandler = 0;
953      fpViewer = 0;
954    }
955  }
956}
957
958void G4VisManager::SetCurrentSceneHandler (G4VSceneHandler* pSceneHandler) {
959  fpSceneHandler = pSceneHandler;
960  if (fVerbosity >= confirmations) {
961    G4cout << "G4VisManager::SetCurrentSceneHandler: scene handler now \""
962           << pSceneHandler -> GetName () << "\"" << G4endl;
963  }
964  if (fpScene != fpSceneHandler -> GetScene ()) {
965    fpScene = fpSceneHandler -> GetScene ();
966    if (fVerbosity >= confirmations) {
967      G4cout << "  Scene now \""
968             << fpScene -> GetName () << "\"" << G4endl;
969    }
970  }
971  if (fpGraphicsSystem != pSceneHandler -> GetGraphicsSystem ()) {
972    fpGraphicsSystem = pSceneHandler -> GetGraphicsSystem ();
973    if (fVerbosity >= confirmations) {
974      G4cout << "  Graphics system now \""
975             << fpGraphicsSystem -> GetName () << "\"" << G4endl;
976    }
977  }
978  const G4ViewerList& viewerList = fpSceneHandler -> GetViewerList ();
979  G4int nViewers = viewerList.size ();
980  if (nViewers) {
981    G4int iViewer;
982    for (iViewer = 0; iViewer < nViewers; iViewer++) {
983      if (fpViewer == viewerList [iViewer]) break;
984    }
985    if (iViewer >= nViewers) {
986      fpViewer = viewerList [0];
987      if (fVerbosity >= confirmations) {
988        G4cout << "  Viewer now \"" << fpViewer -> GetName () << "\""
989               << G4endl;
990      }
991    }
992    IsValidView ();  // Checks.
993  }
994  else {
995    fpViewer = 0;
996    if (fVerbosity >= warnings) {
997      G4cout <<
998        "WARNING: No viewers for this scene handler - please create one."
999             << G4endl;
1000    }
1001  }
1002}
1003
1004void G4VisManager::SetCurrentViewer (G4VViewer* pViewer) {
1005  fpViewer  = pViewer;
1006  if (fVerbosity >= confirmations) {
1007    G4cout << "G4VisManager::SetCurrentViewer: viewer now "
1008           << pViewer -> GetName ()
1009           << G4endl;
1010  }
1011  fpSceneHandler = fpViewer -> GetSceneHandler ();
1012  fpSceneHandler -> SetCurrentViewer (pViewer);
1013  fpScene = fpSceneHandler -> GetScene ();
1014  fpGraphicsSystem = fpSceneHandler -> GetGraphicsSystem ();
1015  IsValidView ();  // Checks.
1016}
1017
1018void G4VisManager::RegisterMessengers () {
1019
1020  // Instantiate individual messengers/commands (often - but not
1021  // always - one command per messenger).
1022
1023  G4UIcommand* directory;
1024
1025  // Top level commands...
1026  RegisterMessenger(new G4VisCommandAbortReviewKeptEvents);
1027  RegisterMessenger(new G4VisCommandEnable);
1028  RegisterMessenger(new G4VisCommandList);
1029  RegisterMessenger(new G4VisCommandReviewKeptEvents);
1030
1031  // Compound commands...
1032  RegisterMessenger(new G4VisCommandDrawTree);
1033  RegisterMessenger(new G4VisCommandDrawView);
1034  RegisterMessenger(new G4VisCommandDrawVolume);
1035  RegisterMessenger(new G4VisCommandOpen);
1036  RegisterMessenger(new G4VisCommandSpecify);
1037
1038  directory = new G4UIdirectory ("/vis/geometry/");
1039  directory -> SetGuidance("Operations on vis attributes of Geant4 geometry.");
1040  fDirectoryList.push_back (directory);
1041  RegisterMessenger(new G4VisCommandGeometryList);
1042  RegisterMessenger(new G4VisCommandGeometryRestore);
1043
1044  directory = new G4UIdirectory ("/vis/geometry/set/");
1045  directory -> SetGuidance("Set vis attributes of Geant4 geometry.");
1046  fDirectoryList.push_back (directory);
1047  RegisterMessenger(new G4VisCommandGeometrySetColour);
1048  RegisterMessenger(new G4VisCommandGeometrySetDaughtersInvisible);
1049  RegisterMessenger(new G4VisCommandGeometrySetLineStyle);
1050  RegisterMessenger(new G4VisCommandGeometrySetLineWidth);
1051  RegisterMessenger(new G4VisCommandGeometrySetForceAuxEdgeVisible);
1052  RegisterMessenger(new G4VisCommandGeometrySetForceLineSegmentsPerCircle);
1053  RegisterMessenger(new G4VisCommandGeometrySetForceSolid);
1054  RegisterMessenger(new G4VisCommandGeometrySetForceWireframe);
1055  RegisterMessenger(new G4VisCommandGeometrySetVisibility);
1056
1057  directory = new G4UIdirectory ("/vis/scene/");
1058  directory -> SetGuidance ("Operations on Geant4 scenes.");
1059  fDirectoryList.push_back (directory);
1060  RegisterMessenger(new G4VisCommandSceneCreate);
1061  RegisterMessenger(new G4VisCommandSceneEndOfEventAction);
1062  RegisterMessenger(new G4VisCommandSceneEndOfRunAction);
1063  RegisterMessenger(new G4VisCommandSceneList);
1064  RegisterMessenger(new G4VisCommandSceneNotifyHandlers);
1065  RegisterMessenger(new G4VisCommandSceneSelect);
1066
1067  directory = new G4UIdirectory ("/vis/scene/add/");
1068  directory -> SetGuidance ("Add model to current scene.");
1069  fDirectoryList.push_back (directory);
1070  RegisterMessenger(new G4VisCommandSceneAddAxes);
1071  RegisterMessenger(new G4VisCommandSceneAddEventID);
1072  RegisterMessenger(new G4VisCommandSceneAddGhosts);
1073  RegisterMessenger(new G4VisCommandSceneAddHits);
1074  RegisterMessenger(new G4VisCommandSceneAddLogicalVolume);
1075  RegisterMessenger(new G4VisCommandSceneAddLogo);
1076  RegisterMessenger(new G4VisCommandSceneAddPSHits);
1077  RegisterMessenger(new G4VisCommandSceneAddScale);
1078  RegisterMessenger(new G4VisCommandSceneAddText);
1079  RegisterMessenger(new G4VisCommandSceneAddTrajectories);
1080  RegisterMessenger(new G4VisCommandSceneAddUserAction);
1081  RegisterMessenger(new G4VisCommandSceneAddVolume);
1082
1083  directory = new G4UIdirectory ("/vis/sceneHandler/");
1084  directory -> SetGuidance ("Operations on Geant4 scene handlers.");
1085  fDirectoryList.push_back (directory);
1086  RegisterMessenger(new G4VisCommandSceneHandlerAttach);
1087  RegisterMessenger(new G4VisCommandSceneHandlerCreate);
1088  RegisterMessenger(new G4VisCommandSceneHandlerList);
1089  RegisterMessenger(new G4VisCommandSceneHandlerSelect);
1090
1091  directory = new G4UIdirectory ("/vis/viewer/");
1092  directory -> SetGuidance ("Operations on Geant4 viewers.");
1093  fDirectoryList.push_back (directory);
1094  RegisterMessenger(new G4VisCommandViewerAddCutawayPlane);
1095  RegisterMessenger(new G4VisCommandViewerChangeCutawayPlane);
1096  RegisterMessenger(new G4VisCommandViewerClear);
1097  RegisterMessenger(new G4VisCommandViewerClearCutawayPlanes);
1098  RegisterMessenger(new G4VisCommandViewerClearTransients);
1099  RegisterMessenger(new G4VisCommandViewerClone);
1100  RegisterMessenger(new G4VisCommandViewerCreate);
1101  RegisterMessenger(new G4VisCommandViewerDolly);
1102  RegisterMessenger(new G4VisCommandViewerFlush);
1103  RegisterMessenger(new G4VisCommandViewerList);
1104  RegisterMessenger(new G4VisCommandViewerPan);
1105  RegisterMessenger(new G4VisCommandViewerRebuild);
1106  RegisterMessenger(new G4VisCommandViewerRefresh);
1107  RegisterMessenger(new G4VisCommandViewerReset);
1108  RegisterMessenger(new G4VisCommandViewerScale);
1109  RegisterMessenger(new G4VisCommandViewerSelect);
1110  RegisterMessenger(new G4VisCommandViewerUpdate);
1111  RegisterMessenger(new G4VisCommandViewerZoom);
1112
1113  directory = new G4UIdirectory ("/vis/viewer/set/");
1114  directory -> SetGuidance ("Set view parameters of current viewer.");
1115  fDirectoryList.push_back (directory);
1116  RegisterMessenger(new G4VisCommandsViewerSet);
1117
1118  // List manager commands
1119  RegisterMessenger(new G4VisCommandListManagerList< G4VisModelManager<G4VTrajectoryModel> >
1120                    (fpTrajDrawModelMgr, fpTrajDrawModelMgr->Placement()));
1121  RegisterMessenger(new G4VisCommandListManagerSelect< G4VisModelManager<G4VTrajectoryModel> >
1122                    (fpTrajDrawModelMgr, fpTrajDrawModelMgr->Placement())); 
1123
1124  // Trajectory filter manager commands
1125  RegisterMessenger(new G4VisCommandListManagerList< G4VisFilterManager<G4VTrajectory> >
1126                    (fpTrajFilterMgr, fpTrajFilterMgr->Placement()));
1127  RegisterMessenger(new G4VisCommandManagerMode< G4VisFilterManager<G4VTrajectory> >
1128                    (fpTrajFilterMgr, fpTrajFilterMgr->Placement()));
1129
1130  // Hit filter manager commands
1131  RegisterMessenger(new G4VisCommandListManagerList< G4VisFilterManager<G4VHit> >
1132                    (fpHitFilterMgr, fpHitFilterMgr->Placement()));
1133  RegisterMessenger(new G4VisCommandManagerMode< G4VisFilterManager<G4VHit> >
1134                    (fpHitFilterMgr, fpHitFilterMgr->Placement()));
1135}
1136
1137void G4VisManager::PrintAvailableGraphicsSystems () const {
1138  G4int nSystems = fAvailableGraphicsSystems.size ();
1139  G4cout << "Current available graphics systems are:";
1140  if (nSystems) {
1141    for (int i = 0; i < nSystems; i++) {
1142      const G4VGraphicsSystem* pSystem = fAvailableGraphicsSystems [i];
1143      G4cout << "\n  " << pSystem -> GetName ();
1144      if (pSystem -> GetNickname () != "") {
1145        G4cout << " (" << pSystem -> GetNickname () << ")";
1146      }
1147    }
1148  }
1149  else {
1150    G4cout << "\n  NONE!!!  None registered - yet!  Mmmmm!";
1151  }
1152  G4cout << G4endl;
1153}
1154
1155void G4VisManager::PrintAvailableModels (Verbosity verbosity) const
1156{
1157  {
1158    //fpTrajDrawModelMgr->Print(G4cout);
1159    G4cout << "Registered model factories:" << G4endl;
1160    const std::vector<G4VModelFactory<G4VTrajectoryModel>*>& factoryList =
1161      fpTrajDrawModelMgr->FactoryList();
1162    if (factoryList.empty()) G4cout << "  None" << G4endl;
1163    else {
1164      std::vector<G4VModelFactory<G4VTrajectoryModel>*>::const_iterator i;
1165      for (i = factoryList.begin(); i != factoryList.end(); ++i)
1166        (*i)->Print(G4cout);
1167    }
1168    const G4VisListManager<G4VTrajectoryModel>* listManager =
1169      fpTrajDrawModelMgr->ListManager();
1170    const std::map<G4String, G4VTrajectoryModel*>& modelMap =
1171      listManager->Map();
1172    if (!modelMap.empty()) {
1173      G4cout << "\nRegistered models:" << G4endl;
1174      std::map<G4String, G4VTrajectoryModel*>::const_iterator i;
1175      for (i = modelMap.begin(); i != modelMap.end(); ++i) {
1176        G4cout << "  " << i->second->Name();
1177        if (i->second == listManager->Current()) G4cout << " (Current)";
1178        G4cout << G4endl;
1179        if (verbosity >= parameters) i->second->Print(G4cout);
1180      }
1181    }
1182  }
1183
1184  G4cout << G4endl;
1185
1186  {
1187    //fpTrajFilterMgr->Print(G4cout);
1188    G4cout << "Registered filter factories:" << G4endl;
1189    const std::vector<G4VModelFactory<G4VFilter<G4VTrajectory> >*>&
1190      factoryList = fpTrajFilterMgr->FactoryList();
1191    if (factoryList.empty()) G4cout << "  None" << G4endl;
1192    else {
1193      std::vector<G4VModelFactory<G4VFilter<G4VTrajectory> >*>::const_iterator i;
1194      for (i = factoryList.begin(); i != factoryList.end(); ++i)
1195        (*i)->Print(G4cout);
1196    }
1197    const std::vector<G4VFilter<G4VTrajectory>*>&
1198      filterList = fpTrajFilterMgr->FilterList();
1199    if (!filterList.empty()) {
1200      G4cout << "\nRegistered filters:" << G4endl;
1201      std::vector<G4VFilter<G4VTrajectory>*>::const_iterator i;
1202      for (i = filterList.begin(); i != filterList.end(); ++i) {
1203        G4cout << "  " << (*i)->GetName() << G4endl;
1204        if (verbosity >= parameters) (*i)->PrintAll(G4cout);
1205      }
1206    }
1207  }
1208}
1209
1210void G4VisManager::PrintInvalidPointers () const {
1211  if (fVerbosity >= errors) {
1212    G4cout << "ERROR: G4VisManager::PrintInvalidPointers:";
1213    if (!fpGraphicsSystem) {
1214      G4cout << "\n null graphics system pointer.";
1215    }
1216    else {
1217      G4cout << "\n  Graphics system is " << fpGraphicsSystem -> GetName ()
1218             << " but:";
1219      if (!fpScene)
1220        G4cout <<
1221          "\n  Null scene pointer. Use \"/vis/drawVolume\" or"
1222          " \"/vis/scene/create\".";
1223      if (!fpSceneHandler)
1224        G4cout <<
1225          "\n  Null scene handler pointer. Use \"/vis/open\" or"
1226          " \"/vis/sceneHandler/create\".";
1227      if (!fpViewer )
1228        G4cout <<
1229          "\n  Null viewer pointer. Use \"/vis/viewer/create\".";
1230    }
1231    G4cout << G4endl;
1232  }
1233}
1234
1235void G4VisManager::BeginOfRun ()
1236{
1237  //G4cout << "G4VisManager::BeginOfRun" << G4endl;
1238  fKeptLastEvent = false;
1239  fEventKeepingSuspended = false;
1240  fTransientsDrawnThisRun = false;
1241  if (fpSceneHandler) fpSceneHandler->SetTransientsDrawnThisRun(false);
1242}
1243
1244void G4VisManager::BeginOfEvent ()
1245{
1246  //G4cout << "G4VisManager::BeginOfEvent" << G4endl;
1247  fTransientsDrawnThisEvent = false;
1248  if (fpSceneHandler) fpSceneHandler->SetTransientsDrawnThisEvent(false);
1249}
1250
1251void G4VisManager::EndOfEvent ()
1252{
1253  //G4cout << "G4VisManager::EndOfEvent" << G4endl;
1254
1255  // Don't call IsValidView unless there is a scene handler.  This
1256  // avoids WARNING message at end of event and run when the user has
1257  // not instantiated a scene handler, e.g., in batch mode.
1258  G4bool valid = GetConcreteInstance() && fpSceneHandler && IsValidView();
1259  if (!valid) return;
1260
1261  G4RunManager* runManager = G4RunManager::GetRunManager();
1262  const G4Run* currentRun = runManager->GetCurrentRun();
1263
1264  G4EventManager* eventManager = G4EventManager::GetEventManager();
1265  const G4Event* currentEvent = eventManager->GetConstCurrentEvent();
1266  if (!currentEvent) return;
1267
1268  ClearTransientStoreIfMarked();
1269  fpSceneHandler->DrawEvent(currentEvent);
1270
1271  G4int nEventsToBeProcessed = 0;
1272  G4int nKeptEvents = 0;
1273  G4int eventID = -2;  // (If no run manager, triggers ShowView as normal.)
1274  if (currentRun) {
1275    nEventsToBeProcessed = currentRun->GetNumberOfEventToBeProcessed();
1276    eventID = currentEvent->GetEventID();
1277    const std::vector<const G4Event*>* events =
1278      currentRun->GetEventVector();
1279    if (events) nKeptEvents = events->size();
1280  }
1281
1282  if (fpScene->GetRefreshAtEndOfEvent()) {
1283
1284    // Unless last event (in which case wait end of run)...
1285    if (eventID < nEventsToBeProcessed - 1) {
1286      fpViewer->ShowView();
1287      fpSceneHandler->SetMarkForClearingTransientStore(true);
1288    } else {  // Last event...
1289      // Keep, but only if user has not kept any...
1290      if (!nKeptEvents) {
1291        eventManager->KeepTheCurrentEvent();
1292        fKeptLastEvent = true;
1293      }
1294    }
1295
1296  } else {  //  Accumulating events...
1297
1298    G4int maxNumberOfKeptEvents = fpScene->GetMaxNumberOfKeptEvents();
1299    if (maxNumberOfKeptEvents > 0 && nKeptEvents >= maxNumberOfKeptEvents) {
1300      fEventKeepingSuspended = true;
1301      static G4bool warned = false;
1302      if (!warned) {
1303        if (fVerbosity >= warnings) {
1304          G4cout <<
1305 "WARNING: G4VisManager::EndOfEvent: Automatic event keeping suspended."
1306 "\n  The number of events exceeds the maximum, "
1307                 << maxNumberOfKeptEvents <<
1308 ", that can be kept by the vis manager."
1309                 << G4endl;
1310        }
1311        warned = true;
1312      }
1313    } else if (maxNumberOfKeptEvents != 0) {
1314      eventManager->KeepTheCurrentEvent();
1315    }
1316  }
1317}
1318
1319void G4VisManager::EndOfRun ()
1320{
1321  //G4cout << "G4VisManager::EndOfRun" << G4endl;
1322
1323  // Don't call IsValidView unless there is a scene handler.  This
1324  // avoids WARNING message at end of event and run when the user has
1325  // not instantiated a scene handler, e.g., in batch mode.
1326  G4bool valid = GetConcreteInstance() && fpSceneHandler && IsValidView();
1327  if (valid) {
1328    if (!fpSceneHandler->GetMarkForClearingTransientStore()) {
1329      if (fpScene->GetRefreshAtEndOfRun()) {
1330        fpSceneHandler->DrawEndOfRunModels();
1331        fpViewer->ShowView();
1332        fpSceneHandler->SetMarkForClearingTransientStore(true);
1333      }
1334    }
1335
1336    if (fEventKeepingSuspended && fVerbosity >= warnings) {
1337      G4cout <<
1338 "WARNING: G4VisManager::EndOfRun: Automatic event keeping has been suspended."
1339 "\n  The number of events in the run exceeded the maximum, "
1340             << fpScene->GetMaxNumberOfKeptEvents() <<
1341 ", that can be kept by the vis manager." <<
1342 "\n  The number of events kept by the vis manager can be changed with"
1343 "\n  \"/vis/scene/endOfEventAction accumulate <N>\", where N is the"
1344 "\n  maximum number you wish to allow.  N < 0 means \"unlimited\"."
1345             << G4endl;
1346    }
1347  }
1348  fEventRefreshing = false;
1349
1350  G4RunManager* runManager = G4RunManager::GetRunManager();
1351  const G4Run* currentRun = runManager->GetCurrentRun();
1352 
1353  G4int nKeptEvents = 0;
1354  const std::vector<const G4Event*>* events =
1355    currentRun? currentRun->GetEventVector(): 0;
1356  if (events) nKeptEvents = events->size();
1357
1358  if (nKeptEvents && !fKeptLastEvent) {
1359    if (!valid && fVerbosity >= warnings) G4cout << "WARNING: ";
1360    if (fVerbosity >= warnings) {
1361      G4cout << nKeptEvents;
1362      if (nKeptEvents == 1) G4cout << " event has";
1363      else G4cout << " events have";
1364      G4cout << " been kept for refreshing and/or reviewing." << G4endl;
1365    }
1366    static G4bool warned = false;
1367    if (!valid && fVerbosity >= warnings && !warned) {
1368      G4cout <<
1369        "  Only useful if before starting the run:"
1370        "\n    a) trajectories are stored (\"/vis/scene/add/trajectories [smooth|rich]\"), or"
1371        "\n    b) the Draw method of any hits is implemented."
1372        "\n  To view trajectories and hits:"
1373        "\n    open a viewer, draw a volume, \"/vis/scene/add/trajectories\""
1374        "\n    \"/vis/scene/add/hits\" and, possibly, \"/vis/viewer/flush\"."
1375        "\n  To see all events: \"/vis/scene/endOfEventAction accumulate\"."
1376        "\n  To see events individually: \"/vis/reviewKeptEvents\"."
1377             << G4endl;
1378      warned = true;
1379    }
1380  }
1381}
1382
1383void G4VisManager::ClearTransientStoreIfMarked(){
1384  // Assumes valid view.
1385  if (fpSceneHandler->GetMarkForClearingTransientStore()) {
1386    fpSceneHandler->SetMarkForClearingTransientStore(false);
1387    fpSceneHandler->ClearTransientStore();
1388  }
1389  // Record if transients drawn.  These local flags are only set
1390  // *after* ClearTransientStore.  In the code in G4VSceneHandler
1391  // triggered by ClearTransientStore, use these flags so that
1392  // event refreshing is not done too early.
1393  fTransientsDrawnThisEvent = fpSceneHandler->GetTransientsDrawnThisEvent();
1394  fTransientsDrawnThisRun = fpSceneHandler->GetTransientsDrawnThisRun();
1395}
1396
1397void G4VisManager::ResetTransientsDrawnFlags()
1398{
1399  fTransientsDrawnThisRun = false;
1400  fTransientsDrawnThisEvent = false;
1401  G4SceneHandlerListConstIterator i;
1402  for (i = fAvailableSceneHandlers.begin();
1403       i != fAvailableSceneHandlers.end(); ++i) {
1404    (*i)->SetTransientsDrawnThisEvent(false);
1405    (*i)->SetTransientsDrawnThisRun(false);
1406  }
1407}
1408
1409G4String G4VisManager::ViewerShortName (const G4String& viewerName) const {
1410  G4String viewerShortName (viewerName);
1411  viewerShortName = viewerShortName (0, viewerShortName.find (' '));
1412  return viewerShortName.strip ();
1413}
1414
1415G4VViewer* G4VisManager::GetViewer (const G4String& viewerName) const {
1416  G4String viewerShortName = ViewerShortName (viewerName);
1417  size_t nHandlers = fAvailableSceneHandlers.size ();
1418  size_t iHandler, iViewer;
1419  G4VViewer* viewer = 0;
1420  G4bool found = false;
1421  for (iHandler = 0; iHandler < nHandlers; iHandler++) {
1422    G4VSceneHandler* sceneHandler = fAvailableSceneHandlers [iHandler];
1423    const G4ViewerList& viewerList = sceneHandler -> GetViewerList ();
1424    for (iViewer = 0; iViewer < viewerList.size (); iViewer++) {
1425      viewer = viewerList [iViewer];
1426      if (viewerShortName == viewer -> GetShortName ()) {
1427        found = true;
1428        break;
1429      }
1430    }
1431    if (found) break;
1432  }
1433  if (found) return viewer;
1434  else return 0;
1435}
1436
1437std::vector<G4String> G4VisManager::VerbosityGuidanceStrings;
1438
1439G4String G4VisManager::VerbosityString(Verbosity verbosity) {
1440  G4String s;
1441  switch (verbosity) {
1442  case         quiet: s = "quiet (0)"; break;
1443  case       startup: s = "startup (1)"; break;
1444  case        errors: s = "errors (2)"; break;
1445  case      warnings: s = "warnings (3)"; break;
1446  case confirmations: s = "confirmations (4)"; break;
1447  case    parameters: s = "parameters (5)"; break;
1448  case           all: s = "all (6)"; break;
1449  }
1450  return s;
1451}
1452
1453G4VisManager::Verbosity
1454G4VisManager::GetVerbosityValue(const G4String& verbosityString) {
1455  G4String s(verbosityString); s.toLower();
1456  Verbosity verbosity;
1457  if      (s(0) == 'q') verbosity = quiet;
1458  else if (s(0) == 's') verbosity = startup;
1459  else if (s(0) == 'e') verbosity = errors;
1460  else if (s(0) == 'w') verbosity = warnings;
1461  else if (s(0) == 'c') verbosity = confirmations;
1462  else if (s(0) == 'p') verbosity = parameters;
1463  else if (s(0) == 'a') verbosity = all;
1464  else {
1465    G4int intVerbosity;
1466    std::istringstream is(s);
1467    is >> intVerbosity;
1468    if (!is) {
1469      G4cout << "ERROR: G4VisManager::GetVerbosityValue: invalid verbosity \""
1470             << verbosityString << "\"";
1471      for (size_t i = 0; i < VerbosityGuidanceStrings.size(); ++i) {
1472        G4cout << '\n' << VerbosityGuidanceStrings[i];
1473      }
1474      verbosity = warnings;
1475      G4cout << "\n  Returning " << VerbosityString(verbosity)
1476             << G4endl;
1477    }
1478    else {
1479      verbosity = GetVerbosityValue(intVerbosity);
1480    }
1481  }
1482  return verbosity;
1483}
1484
1485G4VisManager::Verbosity G4VisManager::GetVerbosityValue(G4int intVerbosity) {
1486  Verbosity verbosity;
1487  if      (intVerbosity < quiet) verbosity = quiet;
1488  else if (intVerbosity > all)   verbosity = all;
1489  else                           verbosity = Verbosity(intVerbosity);
1490  return verbosity;
1491}
1492
1493void G4VisManager::SetVerboseLevel (G4int intVerbosity) {
1494  fVerbosity = GetVerbosityValue(intVerbosity);
1495}
1496
1497void G4VisManager::SetVerboseLevel (const G4String& verbosityString) {
1498  fVerbosity = GetVerbosityValue(verbosityString);
1499}
1500
1501G4bool G4VisManager::IsValidView () {
1502
1503  if (!fInitialised) Initialise ();
1504
1505  static G4bool noGSPrinting = true;
1506  if (!fpGraphicsSystem) {
1507    // Limit printing - we do not want printing if the user simply does
1508    // not want to use graphics, e.g., in batch mode.
1509    if (noGSPrinting) {
1510      noGSPrinting = false;
1511      if (fVerbosity >= warnings) {
1512        G4cout <<
1513  "WARNING: G4VisManager::IsValidView(): Attempt to draw when no graphics system"
1514  "\n  has been instantiated.  Use \"/vis/open\" or \"/vis/sceneHandler/create\"."
1515  "\n  Alternatively, to avoid this message, suppress instantiation of vis"
1516  "\n  manager (G4VisExecutive), possibly by setting G4VIS_NONE, and ensure"
1517  "\n  drawing code is executed only if G4VVisManager::GetConcreteInstance()"
1518  "\n  is non-zero."
1519               << G4endl;
1520      }
1521    }
1522    return false;
1523  }
1524
1525  if ((!fpScene) || (!fpSceneHandler) || (!fpViewer)) {
1526    if (fVerbosity >= errors) {
1527      G4cout <<
1528        "ERROR: G4VisManager::IsValidView(): Current view is not valid."
1529             << G4endl;
1530      PrintInvalidPointers ();
1531    }
1532    return false;
1533  }
1534
1535  if (fpScene != fpSceneHandler -> GetScene ()) {
1536    if (fVerbosity >= errors) {
1537      G4cout << "ERROR: G4VisManager::IsValidView ():";
1538      if (fpSceneHandler -> GetScene ()) {
1539        G4cout <<
1540          "\n  The current scene \""
1541               << fpScene -> GetName ()
1542               << "\" is not handled by"
1543          "\n  the current scene handler \""
1544               << fpSceneHandler -> GetName ()
1545               << "\""
1546          "\n  (it currently handles scene \""
1547               << fpSceneHandler -> GetScene () -> GetName ()
1548               << "\")."
1549          "\n  Either:"
1550          "\n  (a) attach it to the scene handler with"
1551          "\n      /vis/sceneHandler/attach "
1552               << fpScene -> GetName ()
1553               <<       ", or"
1554          "\n  (b) create a new scene handler with "
1555          "\n      /vis/sceneHandler/create <graphics-system>,"
1556          "\n      in which case it should pick up the the new scene."
1557               << G4endl;
1558      }
1559      else {
1560        G4cout << "\n  Scene handler \""
1561               << fpSceneHandler -> GetName ()
1562               << "\" has null scene pointer."
1563          "\n  Attach a scene with /vis/sceneHandler/attach [<scene-name>]"
1564               << G4endl;
1565      }
1566    }
1567    return false;
1568  }
1569
1570  const G4ViewerList& viewerList = fpSceneHandler -> GetViewerList ();
1571  if (viewerList.size () == 0) {
1572    if (fVerbosity >= errors) {
1573      G4cout <<
1574        "ERROR: G4VisManager::IsValidView (): the current scene handler\n  \""
1575             << fpSceneHandler -> GetName ()
1576             << "\" has no viewers.  Do /vis/viewer/create."
1577             << G4endl;
1578    }
1579    return false;
1580  }
1581
1582  G4bool isValid = true;
1583  if (fpScene -> IsEmpty ()) {  // Add world by default if possible...
1584    G4bool warn(fVerbosity >= warnings);
1585    G4bool successful = fpScene -> AddWorldIfEmpty (warn);
1586    if (!successful || fpScene -> IsEmpty ()) {        // If still empty...
1587      if (fVerbosity >= errors) {
1588        G4cout << "ERROR: G4VisManager::IsViewValid ():";
1589        G4cout <<
1590          "\n  Attempt at some drawing operation when scene is empty."
1591          "\n  Maybe the geometry has not yet been defined."
1592          "  Try /run/initialize."
1593               << G4endl;
1594      }
1595      isValid = false;
1596    }
1597    else {
1598      G4UImanager::GetUIpointer()->ApplyCommand ("/vis/scene/notifyHandlers");
1599      if (fVerbosity >= warnings) {
1600        G4cout <<
1601          "WARNING: G4VisManager: the scene was empty, \"world\" has been"
1602          "\n  added and the scene handlers notified.";
1603        G4cout << G4endl;
1604      }
1605    }
1606  }
1607  if (isValid) SetConcreteInstance(this);
1608  return isValid;
1609}
1610
1611void
1612G4VisManager::RegisterModelFactories()
1613{
1614  if (fVerbosity >= warnings) {
1615    G4cout<<"G4VisManager: No model factories registered with G4VisManager."<<G4endl;
1616    G4cout<<"G4VisManager::RegisterModelFactories() should be overridden in derived"<<G4endl;
1617    G4cout<<"class. See G4VisExecutive for an example."<<G4endl;
1618  }
1619}
Note: See TracBrowser for help on using the repository browser.