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

Last change on this file since 893 was 893, checked in by garnier, 15 years ago

make 3.80 added because 3.81 is bad

  • 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#ifdef G4DEBUG
676  printf("G4VisManager::CreateViewer\n");
677#endif
678        fpSceneHandler -> SetCurrentViewer (fpViewer);
679
680        if (fVerbosity >= confirmations) {
681          G4cout << "G4VisManager::CreateViewer: new viewer created."
682                 << G4endl;
683        }
684
685        const G4ViewParameters& vp = fpViewer->GetViewParameters();
686        if (fVerbosity >= parameters) {
687          G4cout << " view parameters are:\n  " << vp << G4endl;
688        }
689
690        if (vp.IsCulling () && vp.IsCullingInvisible ()) {
691          static G4bool warned = false;
692          if (fVerbosity >= confirmations) {
693            if (!warned) {
694              G4cout <<
695  "NOTE: objects with visibility flag set to \"false\""
696  " will not be drawn!"
697  "\n  \"/vis/viewer/set/culling global false\" to Draw such objects."
698  "\n  Also see other \"/vis/viewer/set\" commands."
699                     << G4endl;
700              warned = true;
701            }
702          }
703        }
704        if (vp.IsCullingCovered ()) {
705          static G4bool warned = false;
706          if (fVerbosity >= warnings) {
707            if (!warned) {
708              G4cout <<
709  "WARNING: covered objects in solid mode will not be rendered!"
710  "\n  \"/vis/viewer/set/culling coveredDaughters false\" to reverse this."
711  "\n  Also see other \"/vis/viewer/set\" commands."
712                     << G4endl;
713              warned = true;
714            }
715          }
716        }
717      }
718    }
719  }
720  else PrintInvalidPointers ();
721}
722
723void G4VisManager::GeometryHasChanged () {
724  if (fVerbosity >= confirmations) {
725    G4cout << "G4VisManager::GeometryHasChanged() called." << G4endl;
726  }
727
728  // Change the world...
729  G4VPhysicalVolume* pWorld =
730    G4TransportationManager::GetTransportationManager ()
731    -> GetNavigatorForTracking () -> GetWorldVolume ();
732  if (!pWorld) {
733    if (fVerbosity >= warnings) {
734      G4cout << "WARNING: There is no world volume!" << G4endl;
735    }
736  }
737
738  // Check scenes.
739  G4SceneList& sceneList = fSceneList;
740  G4int iScene, nScenes = sceneList.size ();
741  for (iScene = 0; iScene < nScenes; iScene++) {
742    G4Scene* pScene = sceneList [iScene];
743    std::vector<G4VModel*>& modelList = pScene -> SetRunDurationModelList ();
744
745    if (modelList.size ()) {
746      G4bool modelInvalid;
747      do {  // Remove, if required, one at a time.
748        modelInvalid = false;
749        std::vector<G4VModel*>::iterator iterModel;
750        for (iterModel = modelList.begin();
751             iterModel != modelList.end();
752             ++iterModel) {
753          modelInvalid = !((*iterModel) -> Validate (fVerbosity >= warnings));
754          if (modelInvalid) {
755            // Model invalid - remove and break.
756            if (fVerbosity >= warnings) {
757              G4cout << "WARNING: Model \""
758                     << (*iterModel) -> GetGlobalDescription ()
759                     <<
760                "\" is no longer valid - being removed\n  from scene \""
761                     << pScene -> GetName () << "\""
762                     << G4endl;
763            }
764            modelList.erase (iterModel);
765            break;
766          }
767        }
768      } while (modelInvalid);
769
770      if (modelList.size () == 0) {
771        if (fVerbosity >= warnings) {
772          G4cout << "WARNING: No models left in this scene \""
773                 << pScene -> GetName ()
774                 << "\"."
775                 << G4endl;
776        }
777      }
778      else {
779        pScene->CalculateExtent();
780        G4UImanager::GetUIpointer () ->
781          ApplyCommand (G4String("/vis/scene/notifyHandlers " + pScene->GetName()));
782      }
783    }
784  }
785
786  // Check the manager's current scene...
787  if (fpScene && fpScene -> GetRunDurationModelList ().size () == 0) {
788    if (fVerbosity >= warnings) {
789      G4cout << "WARNING: The current scene \""
790             << fpScene -> GetName ()
791             << "\" has no models."
792             << G4endl;
793    }
794  }
795
796}
797
798G4bool G4VisManager::FilterTrajectory(const G4VTrajectory& trajectory)
799{
800  return fpTrajFilterMgr->Accept(trajectory);
801}   
802
803G4bool G4VisManager::FilterHit(const G4VHit& hit)
804{
805  return fpHitFilterMgr->Accept(hit);
806}   
807
808void G4VisManager::DispatchToModel(const G4VTrajectory& trajectory, G4int i_mode)
809{
810  G4bool visible(true);
811
812  // See if trajectory passes filter
813  G4bool passed = FilterTrajectory(trajectory);
814
815  if (!passed) {
816    // Draw invisible trajectory if trajectory failed filter and
817    // are filtering in soft mode
818    if (fpTrajFilterMgr->GetMode() == FilterMode::Soft) visible = false;
819    else {return;}
820  }
821
822  // Go on to draw trajectory
823  assert (0 != fpTrajDrawModelMgr);
824
825  const G4VTrajectoryModel* model = CurrentTrajDrawModel();
826
827  assert (0 != model); // Should exist
828
829  model->Draw(trajectory, i_mode, visible);
830}
831
832void G4VisManager::SetUserAction
833(G4VUserVisAction* pVisAction,
834 const G4VisExtent& extent) {
835  fpUserVisAction = pVisAction;
836  fUserVisActionExtent = extent;
837  if (extent.GetExtentRadius() <= 0.) {
838    if (fVerbosity >= warnings) {
839      G4cout <<
840        "WARNING: No extent set for user vis action.  (You may"
841        "\n  set it later when adding with /vis/scene/add/userAction.)"
842             << G4endl;
843    }
844  }
845}
846
847void G4VisManager::SetCurrentScene (G4Scene* pScene) {
848  if (pScene != fpScene) {
849    // A change of scene.  Therefore reset transients drawn flags.  All
850    // memory of previous transient proceessing thereby erased...
851    ResetTransientsDrawnFlags();
852  }
853  fpScene = pScene;
854}
855
856void G4VisManager::SetCurrentGraphicsSystem (G4VGraphicsSystem* pSystem) {
857  fpGraphicsSystem = pSystem;
858  if (fVerbosity >= confirmations) {
859    G4cout << "G4VisManager::SetCurrentGraphicsSystem: system now "
860           << pSystem -> GetName () << G4endl;
861  }
862  // If current scene handler is of same graphics system, leave unchanged.
863  // Else find the most recent scene handler of same graphics system.
864  // Or clear pointers.
865  if (!(fpSceneHandler && fpSceneHandler -> GetGraphicsSystem () == pSystem)) {
866    const G4SceneHandlerList& sceneHandlerList = fAvailableSceneHandlers;
867    G4int nSH = sceneHandlerList.size ();  // No. of scene handlers.
868    G4int iSH;
869    for (iSH = nSH - 1; iSH >= 0; iSH--) {
870      if (sceneHandlerList [iSH] -> GetGraphicsSystem () == pSystem) break;
871    }
872    if (iSH >= 0) {
873      fpSceneHandler = sceneHandlerList [iSH];
874      if (fVerbosity >= confirmations) {
875        G4cout << "  Scene Handler now "
876               << fpSceneHandler -> GetName () << G4endl;
877      }
878      if (fpScene != fpSceneHandler -> GetScene ()) {
879        fpScene = fpSceneHandler -> GetScene ();
880        if (fVerbosity >= confirmations) {
881          G4cout << "  Scene now \""
882                 << fpScene -> GetName () << "\"" << G4endl;
883        }
884      }
885      const G4ViewerList& viewerList = fpSceneHandler -> GetViewerList ();
886      if (viewerList.size ()) {
887        fpViewer = viewerList [0];
888        if (fVerbosity >= confirmations) {
889          G4cout << "  Viewer now " << fpViewer -> GetName () << G4endl;
890        }
891      }
892      else {
893        fpViewer = 0;
894      }
895    }
896    else {
897      fpSceneHandler = 0;
898      fpViewer = 0;
899    }
900  }
901}
902
903void G4VisManager::SetCurrentSceneHandler (G4VSceneHandler* pSceneHandler) {
904  fpSceneHandler = pSceneHandler;
905  if (fVerbosity >= confirmations) {
906    G4cout << "G4VisManager::SetCurrentSceneHandler: scene handler now \""
907           << pSceneHandler -> GetName () << "\"" << G4endl;
908  }
909  if (fpScene != fpSceneHandler -> GetScene ()) {
910    fpScene = fpSceneHandler -> GetScene ();
911    if (fVerbosity >= confirmations) {
912      G4cout << "  Scene now \""
913             << fpScene -> GetName () << "\"" << G4endl;
914    }
915  }
916  if (fpGraphicsSystem != pSceneHandler -> GetGraphicsSystem ()) {
917    fpGraphicsSystem = pSceneHandler -> GetGraphicsSystem ();
918    if (fVerbosity >= confirmations) {
919      G4cout << "  Graphics system now \""
920             << fpGraphicsSystem -> GetName () << "\"" << G4endl;
921    }
922  }
923  const G4ViewerList& viewerList = fpSceneHandler -> GetViewerList ();
924  G4int nViewers = viewerList.size ();
925  if (nViewers) {
926    G4int iViewer;
927    for (iViewer = 0; iViewer < nViewers; iViewer++) {
928      if (fpViewer == viewerList [iViewer]) break;
929    }
930    if (iViewer >= nViewers) {
931      fpViewer = viewerList [0];
932      if (fVerbosity >= confirmations) {
933        G4cout << "  Viewer now \"" << fpViewer -> GetName () << "\""
934               << G4endl;
935      }
936    }
937    IsValidView ();  // Checks.
938  }
939  else {
940    fpViewer = 0;
941    if (fVerbosity >= warnings) {
942      G4cout <<
943        "WARNING: No viewers for this scene handler - please create one."
944             << G4endl;
945    }
946  }
947}
948
949void G4VisManager::SetCurrentViewer (G4VViewer* pViewer) {
950  fpViewer  = pViewer;
951  if (fVerbosity >= confirmations) {
952    G4cout << "G4VisManager::SetCurrentViewer: viewer now "
953           << pViewer -> GetName ()
954           << G4endl;
955  }
956  fpSceneHandler = fpViewer -> GetSceneHandler ();
957  fpSceneHandler -> SetCurrentViewer (pViewer);
958  fpScene = fpSceneHandler -> GetScene ();
959  fpGraphicsSystem = fpSceneHandler -> GetGraphicsSystem ();
960  IsValidView ();  // Checks.
961}
962
963void G4VisManager::RegisterMessengers () {
964
965  // Instantiate individual messengers/commands (often - but not
966  // always - one command per messenger).
967
968  G4VVisCommand::SetVisManager (this);  // Sets shared pointer to vis manager.
969
970  G4UIcommand* directory;
971
972  // Top level basic commands...
973  RegisterMessenger(new G4VisCommandAbortReviewKeptEvents);
974  RegisterMessenger(new G4VisCommandEnable);
975  RegisterMessenger(new G4VisCommandList);
976  RegisterMessenger(new G4VisCommandVerbose);
977
978  // Other top level commands...
979  RegisterMessenger(new G4VisCommandReviewKeptEvents);
980
981  // Compound commands...
982  RegisterMessenger(new G4VisCommandDrawTree);
983  RegisterMessenger(new G4VisCommandDrawView);
984  RegisterMessenger(new G4VisCommandDrawVolume);
985  RegisterMessenger(new G4VisCommandOpen);
986  RegisterMessenger(new G4VisCommandSpecify);
987
988  directory = new G4UIdirectory ("/vis/geometry/");
989  directory -> SetGuidance("Operations on vis attributes of Geant4 geometry.");
990  fDirectoryList.push_back (directory);
991  RegisterMessenger(new G4VisCommandGeometryList);
992  RegisterMessenger(new G4VisCommandGeometryRestore);
993
994  directory = new G4UIdirectory ("/vis/geometry/set/");
995  directory -> SetGuidance("Set vis attributes of Geant4 geometry.");
996  fDirectoryList.push_back (directory);
997  RegisterMessenger(new G4VisCommandGeometrySetColour);
998  RegisterMessenger(new G4VisCommandGeometrySetDaughtersInvisible);
999  RegisterMessenger(new G4VisCommandGeometrySetLineStyle);
1000  RegisterMessenger(new G4VisCommandGeometrySetLineWidth);
1001  RegisterMessenger(new G4VisCommandGeometrySetForceAuxEdgeVisible);
1002  RegisterMessenger(new G4VisCommandGeometrySetForceLineSegmentsPerCircle);
1003  RegisterMessenger(new G4VisCommandGeometrySetForceSolid);
1004  RegisterMessenger(new G4VisCommandGeometrySetForceWireframe);
1005  RegisterMessenger(new G4VisCommandGeometrySetVisibility);
1006
1007  directory = new G4UIdirectory ("/vis/scene/");
1008  directory -> SetGuidance ("Operations on Geant4 scenes.");
1009  fDirectoryList.push_back (directory);
1010  RegisterMessenger(new G4VisCommandSceneCreate);
1011  RegisterMessenger(new G4VisCommandSceneEndOfEventAction);
1012  RegisterMessenger(new G4VisCommandSceneEndOfRunAction);
1013  RegisterMessenger(new G4VisCommandSceneList);
1014  RegisterMessenger(new G4VisCommandSceneNotifyHandlers);
1015  RegisterMessenger(new G4VisCommandSceneSelect);
1016
1017  directory = new G4UIdirectory ("/vis/scene/add/");
1018  directory -> SetGuidance ("Add model to current scene.");
1019  fDirectoryList.push_back (directory);
1020  RegisterMessenger(new G4VisCommandSceneAddAxes);
1021  RegisterMessenger(new G4VisCommandSceneAddEventID);
1022  RegisterMessenger(new G4VisCommandSceneAddGhosts);
1023  RegisterMessenger(new G4VisCommandSceneAddHits);
1024  RegisterMessenger(new G4VisCommandSceneAddLogicalVolume);
1025  RegisterMessenger(new G4VisCommandSceneAddLogo);
1026  RegisterMessenger(new G4VisCommandSceneAddScale);
1027  RegisterMessenger(new G4VisCommandSceneAddText);
1028  RegisterMessenger(new G4VisCommandSceneAddTrajectories);
1029  RegisterMessenger(new G4VisCommandSceneAddUserAction);
1030  RegisterMessenger(new G4VisCommandSceneAddVolume);
1031
1032  directory = new G4UIdirectory ("/vis/sceneHandler/");
1033  directory -> SetGuidance ("Operations on Geant4 scene handlers.");
1034  fDirectoryList.push_back (directory);
1035  RegisterMessenger(new G4VisCommandSceneHandlerAttach);
1036  RegisterMessenger(new G4VisCommandSceneHandlerCreate);
1037  RegisterMessenger(new G4VisCommandSceneHandlerList);
1038  RegisterMessenger(new G4VisCommandSceneHandlerSelect);
1039
1040  directory = new G4UIdirectory ("/vis/viewer/");
1041  directory -> SetGuidance ("Operations on Geant4 viewers.");
1042  fDirectoryList.push_back (directory);
1043  RegisterMessenger(new G4VisCommandViewerAddCutawayPlane);
1044  RegisterMessenger(new G4VisCommandViewerChangeCutawayPlane);
1045  RegisterMessenger(new G4VisCommandViewerClear);
1046  RegisterMessenger(new G4VisCommandViewerClearCutawayPlanes);
1047  RegisterMessenger(new G4VisCommandViewerClearTransients);
1048  RegisterMessenger(new G4VisCommandViewerClone);
1049  RegisterMessenger(new G4VisCommandViewerCreate);
1050  RegisterMessenger(new G4VisCommandViewerDolly);
1051  RegisterMessenger(new G4VisCommandViewerFlush);
1052  RegisterMessenger(new G4VisCommandViewerList);
1053  RegisterMessenger(new G4VisCommandViewerPan);
1054  RegisterMessenger(new G4VisCommandViewerRebuild);
1055  RegisterMessenger(new G4VisCommandViewerRefresh);
1056  RegisterMessenger(new G4VisCommandViewerReset);
1057  RegisterMessenger(new G4VisCommandViewerScale);
1058  RegisterMessenger(new G4VisCommandViewerSelect);
1059  RegisterMessenger(new G4VisCommandViewerUpdate);
1060  RegisterMessenger(new G4VisCommandViewerZoom);
1061
1062  directory = new G4UIdirectory ("/vis/viewer/set/");
1063  directory -> SetGuidance ("Set view parameters of current viewer.");
1064  fDirectoryList.push_back (directory);
1065  RegisterMessenger(new G4VisCommandsViewerSet);
1066
1067  // List manager commands
1068  RegisterMessenger(new G4VisCommandListManagerList< G4VisModelManager<G4VTrajectoryModel> >
1069                    (fpTrajDrawModelMgr, fpTrajDrawModelMgr->Placement()));
1070  RegisterMessenger(new G4VisCommandListManagerSelect< G4VisModelManager<G4VTrajectoryModel> >
1071                    (fpTrajDrawModelMgr, fpTrajDrawModelMgr->Placement())); 
1072
1073  // Trajectory filter manager commands
1074  RegisterMessenger(new G4VisCommandListManagerList< G4VisFilterManager<G4VTrajectory> >
1075                    (fpTrajFilterMgr, fpTrajFilterMgr->Placement()));
1076  RegisterMessenger(new G4VisCommandManagerMode< G4VisFilterManager<G4VTrajectory> >
1077                    (fpTrajFilterMgr, fpTrajFilterMgr->Placement()));
1078
1079  // Hit filter manager commands
1080  RegisterMessenger(new G4VisCommandListManagerList< G4VisFilterManager<G4VHit> >
1081                    (fpHitFilterMgr, fpHitFilterMgr->Placement()));
1082  RegisterMessenger(new G4VisCommandManagerMode< G4VisFilterManager<G4VHit> >
1083                    (fpHitFilterMgr, fpHitFilterMgr->Placement()));
1084}
1085
1086void G4VisManager::PrintAvailableGraphicsSystems () const {
1087  G4int nSystems = fAvailableGraphicsSystems.size ();
1088  G4cout << "Current available graphics systems are:";
1089  if (nSystems) {
1090    for (int i = 0; i < nSystems; i++) {
1091      const G4VGraphicsSystem* pSystem = fAvailableGraphicsSystems [i];
1092      G4cout << "\n  " << pSystem -> GetName ();
1093      if (pSystem -> GetNickname () != "") {
1094        G4cout << " (" << pSystem -> GetNickname () << ")";
1095      }
1096    }
1097  }
1098  else {
1099    G4cout << "\n  NONE!!!  None registered - yet!  Mmmmm!";
1100  }
1101  G4cout << G4endl;
1102}
1103
1104void G4VisManager::PrintAvailableModels (Verbosity verbosity) const
1105{
1106  {
1107    //fpTrajDrawModelMgr->Print(G4cout);
1108    G4cout << "Registered model factories:" << G4endl;
1109    const std::vector<G4VModelFactory<G4VTrajectoryModel>*>& factoryList =
1110      fpTrajDrawModelMgr->FactoryList();
1111    if (factoryList.empty()) G4cout << "  None" << G4endl;
1112    else {
1113      std::vector<G4VModelFactory<G4VTrajectoryModel>*>::const_iterator i;
1114      for (i = factoryList.begin(); i != factoryList.end(); ++i)
1115        (*i)->Print(G4cout);
1116    }
1117    const G4VisListManager<G4VTrajectoryModel>* listManager =
1118      fpTrajDrawModelMgr->ListManager();
1119    const std::map<G4String, G4VTrajectoryModel*>& modelMap =
1120      listManager->Map();
1121    if (!modelMap.empty()) {
1122      G4cout << "\nRegistered models:" << G4endl;
1123      std::map<G4String, G4VTrajectoryModel*>::const_iterator i;
1124      for (i = modelMap.begin(); i != modelMap.end(); ++i) {
1125        G4cout << "  " << i->second->Name();
1126        if (i->second == listManager->Current()) G4cout << " (Current)";
1127        G4cout << G4endl;
1128        if (verbosity >= parameters) i->second->Print(G4cout);
1129      }
1130    }
1131  }
1132
1133  G4cout << G4endl;
1134
1135  {
1136    //fpTrajFilterMgr->Print(G4cout);
1137    G4cout << "Registered filter factories:" << G4endl;
1138    const std::vector<G4VModelFactory<G4VFilter<G4VTrajectory> >*>&
1139      factoryList = fpTrajFilterMgr->FactoryList();
1140    if (factoryList.empty()) G4cout << "  None" << G4endl;
1141    else {
1142      std::vector<G4VModelFactory<G4VFilter<G4VTrajectory> >*>::const_iterator i;
1143      for (i = factoryList.begin(); i != factoryList.end(); ++i)
1144        (*i)->Print(G4cout);
1145    }
1146    const std::vector<G4VFilter<G4VTrajectory>*>&
1147      filterList = fpTrajFilterMgr->FilterList();
1148    if (!filterList.empty()) {
1149      G4cout << "\nRegistered filters:" << G4endl;
1150      std::vector<G4VFilter<G4VTrajectory>*>::const_iterator i;
1151      for (i = filterList.begin(); i != filterList.end(); ++i) {
1152        G4cout << "  " << (*i)->GetName() << G4endl;
1153        if (verbosity >= parameters) (*i)->PrintAll(G4cout);
1154      }
1155    }
1156  }
1157}
1158
1159void G4VisManager::PrintInvalidPointers () const {
1160  if (fVerbosity >= errors) {
1161    G4cout << "ERROR: G4VisManager::PrintInvalidPointers:";
1162    if (!fpGraphicsSystem) {
1163      G4cout << "\n null graphics system pointer.";
1164    }
1165    else {
1166      G4cout << "\n  Graphics system is " << fpGraphicsSystem -> GetName ()
1167             << " but:";
1168      if (!fpScene)
1169        G4cout <<
1170          "\n  Null scene pointer. Use \"/vis/drawVolume\" or"
1171          " \"/vis/scene/create\".";
1172      if (!fpSceneHandler)
1173        G4cout <<
1174          "\n  Null scene handler pointer. Use \"/vis/open\" or"
1175          " \"/vis/sceneHandler/create\".";
1176      if (!fpViewer )
1177        G4cout <<
1178          "\n  Null viewer pointer. Use \"/vis/viewer/create\".";
1179    }
1180    G4cout << G4endl;
1181  }
1182}
1183
1184void G4VisManager::BeginOfRun ()
1185{
1186  //G4cout << "G4VisManager::BeginOfRun" << G4endl;
1187  fKeptLastEvent = false;
1188  fEventKeepingSuspended = false;
1189  fTransientsDrawnThisRun = false;
1190  if (fpSceneHandler) fpSceneHandler->SetTransientsDrawnThisRun(false);
1191}
1192
1193void G4VisManager::BeginOfEvent ()
1194{
1195  //G4cout << "G4VisManager::BeginOfEvent" << G4endl;
1196  fTransientsDrawnThisEvent = false;
1197  if (fpSceneHandler) fpSceneHandler->SetTransientsDrawnThisEvent(false);
1198}
1199
1200void G4VisManager::EndOfEvent ()
1201{
1202  //G4cout << "G4VisManager::EndOfEvent" << G4endl;
1203
1204  // Don't call IsValidView unless there is a scene handler.  This
1205  // avoids WARNING message at end of event and run when the user has
1206  // not instantiated a scene handler, e.g., in batch mode.
1207  G4bool valid = GetConcreteInstance() && fpSceneHandler && IsValidView();
1208  if (!valid) return;
1209
1210  G4RunManager* runManager = G4RunManager::GetRunManager();
1211  const G4Run* currentRun = runManager->GetCurrentRun();
1212
1213  G4EventManager* eventManager = G4EventManager::GetEventManager();
1214  const G4Event* currentEvent = eventManager->GetConstCurrentEvent();
1215  if (!currentEvent) return;
1216
1217  ClearTransientStoreIfMarked();
1218  fpSceneHandler->DrawEvent(currentEvent);
1219
1220  G4int nEventsToBeProcessed = 0;
1221  G4int nKeptEvents = 0;
1222  G4int eventID = -2;  // (If no run manager, triggers ShowView as normal.)
1223  if (currentRun) {
1224    nEventsToBeProcessed = currentRun->GetNumberOfEventToBeProcessed();
1225    eventID = currentEvent->GetEventID();
1226    const std::vector<const G4Event*>* events =
1227      currentRun->GetEventVector();
1228    if (events) nKeptEvents = events->size();
1229  }
1230
1231  if (fpScene->GetRefreshAtEndOfEvent()) {
1232
1233    // Unless last event (in which case wait end of run)...
1234    if (eventID < nEventsToBeProcessed - 1) {
1235      fpViewer->ShowView();
1236      fpSceneHandler->SetMarkForClearingTransientStore(true);
1237    } else {  // Last event...
1238      // Keep, but only if user has not kept any...
1239      if (!nKeptEvents) {
1240        eventManager->KeepTheCurrentEvent();
1241        fKeptLastEvent = true;
1242      }
1243    }
1244
1245  } else {  //  Accumulating events...
1246
1247    G4int maxNumberOfKeptEvents = fpScene->GetMaxNumberOfKeptEvents();
1248    if (maxNumberOfKeptEvents > 0 && nKeptEvents >= maxNumberOfKeptEvents) {
1249      fEventKeepingSuspended = true;
1250      static G4bool warned = false;
1251      if (!warned) {
1252        if (fVerbosity >= warnings) {
1253          G4cout <<
1254            "WARNING: G4VisManager::EndOfEvent: Event keeping suspended."
1255            "\n  The number of events exceeds the maximum that may be kept, "
1256                 << maxNumberOfKeptEvents << '.'
1257                 << G4endl;
1258        }
1259        warned = true;
1260      }
1261    } else if (maxNumberOfKeptEvents != 0) {
1262      eventManager->KeepTheCurrentEvent();
1263    }
1264  }
1265}
1266
1267void G4VisManager::EndOfRun ()
1268{
1269  //G4cout << "G4VisManager::EndOfRun" << G4endl;
1270
1271  // Don't call IsValidView unless there is a scene handler.  This
1272  // avoids WARNING message at end of event and run when the user has
1273  // not instantiated a scene handler, e.g., in batch mode.
1274  G4bool valid = GetConcreteInstance() && fpSceneHandler && IsValidView();
1275  if (valid) {
1276    if (!fpSceneHandler->GetMarkForClearingTransientStore()) {
1277      if (fpScene->GetRefreshAtEndOfRun()) {
1278        fpViewer->ShowView();
1279        fpSceneHandler->SetMarkForClearingTransientStore(true);
1280      }
1281    }
1282
1283    if (fEventKeepingSuspended && fVerbosity >= warnings) {
1284      G4cout <<
1285        "WARNING: G4VisManager::EndOfRun: Event keeping was suspended."
1286        "\n  The number of events in the run exceeded the maximum to be kept, "
1287             << fpScene->GetMaxNumberOfKeptEvents() << '.' <<
1288        "\n  The number of events to be kept can be changed with"
1289        "\n  \"/vis/scene/endOfEventAction accumulate <N>\", where N is the"
1290        "\n  maximum number you wish to allow.  N < 0 means \"unlimited\"."
1291             << G4endl;
1292    }
1293  }
1294  fEventRefreshing = false;
1295
1296  G4RunManager* runManager = G4RunManager::GetRunManager();
1297  const G4Run* currentRun = runManager->GetCurrentRun();
1298 
1299  G4int nKeptEvents = 0;
1300  const std::vector<const G4Event*>* events =
1301    currentRun? currentRun->GetEventVector(): 0;
1302  if (events) nKeptEvents = events->size();
1303
1304  if (nKeptEvents && !fKeptLastEvent) {
1305    if (!valid && fVerbosity >= warnings) G4cout << "WARNING: ";
1306    if (fVerbosity >= warnings) {
1307      G4cout << nKeptEvents;
1308      if (nKeptEvents == 1) G4cout << " event has";
1309      else G4cout << " events have";
1310      G4cout << " been kept for refreshing and/or reviewing." << G4endl;
1311    }
1312    static G4bool warned = false;
1313    if (!valid && fVerbosity >= warnings && !warned) {
1314      G4cout <<
1315        "  Only useful if before starting the run:"
1316        "\n    a) trajectories are stored (\"/vis/scene/add/trajectories [smooth|rich]\"), or"
1317        "\n    b) the Draw method of any hits is implemented."
1318        "\n  To view trajectories and hits:"
1319        "\n    open a viewer, draw a volume, \"/vis/scene/add/trajectories\""
1320        "\n    \"/vis/scene/add/hits\" and, possibly, \"/vis/viewer/flush\"."
1321        "\n  To see all events: \"/vis/scene/endOfEventAction accumulate\"."
1322        "\n  To see events individually: \"/vis/reviewKeptEvents\"."
1323             << G4endl;
1324      warned = true;
1325    }
1326  }
1327}
1328
1329void G4VisManager::ClearTransientStoreIfMarked(){
1330  // Assumes valid view.
1331  if (fpSceneHandler->GetMarkForClearingTransientStore()) {
1332    fpSceneHandler->SetMarkForClearingTransientStore(false);
1333    fpSceneHandler->ClearTransientStore();
1334  }
1335  // Record if transients drawn.  These local flags are only set
1336  // *after* ClearTransientStore.  In the code in G4VSceneHandler
1337  // triggered by ClearTransientStore, use these flags so that
1338  // event refreshing is not done too early.
1339  fTransientsDrawnThisEvent = fpSceneHandler->GetTransientsDrawnThisEvent();
1340  fTransientsDrawnThisRun = fpSceneHandler->GetTransientsDrawnThisRun();
1341}
1342
1343void G4VisManager::ResetTransientsDrawnFlags()
1344{
1345  fTransientsDrawnThisRun = false;
1346  fTransientsDrawnThisEvent = false;
1347  G4SceneHandlerListConstIterator i;
1348  for (i = fAvailableSceneHandlers.begin();
1349       i != fAvailableSceneHandlers.end(); ++i) {
1350    (*i)->SetTransientsDrawnThisEvent(false);
1351    (*i)->SetTransientsDrawnThisRun(false);
1352  }
1353}
1354
1355G4String G4VisManager::ViewerShortName (const G4String& viewerName) const {
1356  G4String viewerShortName (viewerName);
1357  viewerShortName = viewerShortName (0, viewerShortName.find (' '));
1358  return viewerShortName.strip ();
1359}
1360
1361G4VViewer* G4VisManager::GetViewer (const G4String& viewerName) const {
1362  G4String viewerShortName = ViewerShortName (viewerName);
1363  size_t nHandlers = fAvailableSceneHandlers.size ();
1364  size_t iHandler, iViewer;
1365  G4VViewer* viewer = 0;
1366  G4bool found = false;
1367  for (iHandler = 0; iHandler < nHandlers; iHandler++) {
1368    G4VSceneHandler* sceneHandler = fAvailableSceneHandlers [iHandler];
1369    const G4ViewerList& viewerList = sceneHandler -> GetViewerList ();
1370    for (iViewer = 0; iViewer < viewerList.size (); iViewer++) {
1371      viewer = viewerList [iViewer];
1372      if (viewerShortName == viewer -> GetShortName ()) {
1373        found = true;
1374        break;
1375      }
1376    }
1377    if (found) break;
1378  }
1379  if (found) return viewer;
1380  else return 0;
1381}
1382
1383std::vector<G4String> G4VisManager::VerbosityGuidanceStrings;
1384
1385G4String G4VisManager::VerbosityString(Verbosity verbosity) {
1386  G4String s;
1387  switch (verbosity) {
1388  case         quiet: s = "quiet (0)"; break;
1389  case       startup: s = "startup (1)"; break;
1390  case        errors: s = "errors (2)"; break;
1391  case      warnings: s = "warnings (3)"; break;
1392  case confirmations: s = "confirmations (4)"; break;
1393  case    parameters: s = "parameters (5)"; break;
1394  case           all: s = "all (6)"; break;
1395  }
1396  return s;
1397}
1398
1399G4VisManager::Verbosity
1400G4VisManager::GetVerbosityValue(const G4String& verbosityString) {
1401  G4String s(verbosityString); s.toLower();
1402  Verbosity verbosity;
1403  if      (s(0) == 'q') verbosity = quiet;
1404  else if (s(0) == 's') verbosity = startup;
1405  else if (s(0) == 'e') verbosity = errors;
1406  else if (s(0) == 'w') verbosity = warnings;
1407  else if (s(0) == 'c') verbosity = confirmations;
1408  else if (s(0) == 'p') verbosity = parameters;
1409  else if (s(0) == 'a') verbosity = all;
1410  else {
1411    G4int intVerbosity;
1412    std::istringstream is(s);
1413    is >> intVerbosity;
1414    if (!is) {
1415      G4cout << "ERROR: G4VisManager::GetVerbosityValue: invalid verbosity \""
1416             << verbosityString << "\"";
1417      for (size_t i = 0; i < VerbosityGuidanceStrings.size(); ++i) {
1418        G4cout << '\n' << VerbosityGuidanceStrings[i];
1419      }
1420      verbosity = warnings;
1421      G4cout << "\n  Returning " << VerbosityString(verbosity)
1422             << G4endl;
1423    }
1424    else {
1425      verbosity = GetVerbosityValue(intVerbosity);
1426    }
1427  }
1428  return verbosity;
1429}
1430
1431G4VisManager::Verbosity G4VisManager::GetVerbosityValue(G4int intVerbosity) {
1432  Verbosity verbosity;
1433  if      (intVerbosity < quiet) verbosity = quiet;
1434  else if (intVerbosity > all)   verbosity = all;
1435  else                           verbosity = Verbosity(intVerbosity);
1436  return verbosity;
1437}
1438
1439void G4VisManager::SetVerboseLevel (G4int intVerbosity) {
1440  fVerbosity = GetVerbosityValue(intVerbosity);
1441}
1442
1443void G4VisManager::SetVerboseLevel (const G4String& verbosityString) {
1444  fVerbosity = GetVerbosityValue(verbosityString);
1445}
1446
1447G4bool G4VisManager::IsValidView () {
1448
1449  if (!fInitialised) Initialise ();
1450
1451  static G4bool noGSPrinting = true;
1452  if (!fpGraphicsSystem) {
1453    // Limit printing - we do not want printing if the user simply does
1454    // not want to use graphics, e.g., in batch mode.
1455    if (noGSPrinting) {
1456      noGSPrinting = false;
1457      if (fVerbosity >= warnings) {
1458        G4cout <<
1459  "WARNING: G4VisManager::IsValidView(): Attempt to draw when no graphics system"
1460  "\n  has been instantiated.  Use \"/vis/open\" or \"/vis/sceneHandler/create\"."
1461  "\n  Alternatively, to avoid this message, suppress instantiation of vis"
1462  "\n  manager (G4VisExecutive), possibly by setting G4VIS_NONE, and ensure"
1463  "\n  drawing code is executed only if G4VVisManager::GetConcreteInstance()"
1464  "\n  is non-zero."
1465               << G4endl;
1466      }
1467    }
1468    return false;
1469  }
1470
1471  if ((!fpScene) || (!fpSceneHandler) || (!fpViewer)) {
1472    if (fVerbosity >= errors) {
1473      G4cout <<
1474        "ERROR: G4VisManager::IsValidView(): Current view is not valid."
1475             << G4endl;
1476      PrintInvalidPointers ();
1477    }
1478    return false;
1479  }
1480
1481  if (fpScene != fpSceneHandler -> GetScene ()) {
1482    if (fVerbosity >= errors) {
1483      G4cout << "ERROR: G4VisManager::IsValidView ():";
1484      if (fpSceneHandler -> GetScene ()) {
1485        G4cout <<
1486          "\n  The current scene \""
1487               << fpScene -> GetName ()
1488               << "\" is not handled by"
1489          "\n  the current scene handler \""
1490               << fpSceneHandler -> GetName ()
1491               << "\""
1492          "\n  (it currently handles scene \""
1493               << fpSceneHandler -> GetScene () -> GetName ()
1494               << "\")."
1495          "\n  Either:"
1496          "\n  (a) attach it to the scene handler with"
1497          "\n      /vis/sceneHandler/attach "
1498               << fpScene -> GetName ()
1499               <<       ", or"
1500          "\n  (b) create a new scene handler with "
1501          "\n      /vis/sceneHandler/create <graphics-system>,"
1502          "\n      in which case it should pick up the the new scene."
1503               << G4endl;
1504      }
1505      else {
1506        G4cout << "\n  Scene handler \""
1507               << fpSceneHandler -> GetName ()
1508               << "\" has null scene pointer."
1509          "\n  Attach a scene with /vis/sceneHandler/attach [<scene-name>]"
1510               << G4endl;
1511      }
1512    }
1513    return false;
1514  }
1515
1516  const G4ViewerList& viewerList = fpSceneHandler -> GetViewerList ();
1517  if (viewerList.size () == 0) {
1518    if (fVerbosity >= errors) {
1519      G4cout <<
1520        "ERROR: G4VisManager::IsValidView (): the current scene handler\n  \""
1521             << fpSceneHandler -> GetName ()
1522             << "\" has no viewers.  Do /vis/viewer/create."
1523             << G4endl;
1524    }
1525    return false;
1526  }
1527
1528  G4bool isValid = true;
1529  if (fpScene -> IsEmpty ()) {  // Add world by default if possible...
1530    G4bool warn(fVerbosity >= warnings);
1531    G4bool successful = fpScene -> AddWorldIfEmpty (warn);
1532    if (!successful || fpScene -> IsEmpty ()) {        // If still empty...
1533      if (fVerbosity >= errors) {
1534        G4cout << "ERROR: G4VisManager::IsViewValid ():";
1535        G4cout <<
1536          "\n  Attempt at some drawing operation when scene is empty."
1537          "\n  Maybe the geometry has not yet been defined."
1538          "  Try /run/initialize."
1539               << G4endl;
1540      }
1541      isValid = false;
1542    }
1543    else {
1544      G4UImanager::GetUIpointer()->ApplyCommand ("/vis/scene/notifyHandlers");
1545      if (fVerbosity >= warnings) {
1546        G4cout <<
1547          "WARNING: G4VisManager: the scene was empty, \"world\" has been"
1548          "\n  added and the scene handlers notified.";
1549        G4cout << G4endl;
1550      }
1551    }
1552  }
1553  if (isValid) SetConcreteInstance(this);
1554  return isValid;
1555}
1556
1557void
1558G4VisManager::RegisterModelFactories()
1559{
1560  if (fVerbosity >= warnings) {
1561    G4cout<<"G4VisManager: No model factories registered with G4VisManager."<<G4endl;
1562    G4cout<<"G4VisManager::RegisterModelFactories() should be overridden in derived"<<G4endl;
1563    G4cout<<"class. See G4VisExecutive for an example."<<G4endl;
1564  }
1565}
Note: See TracBrowser for help on using the repository browser.