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

Last change on this file since 850 was 850, checked in by garnier, 16 years ago

geant4.8.2 beta

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