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

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

cvs update

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