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

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

update geant4-09-04-beta-cand-01 interfaces-V09-03-09 vis-V09-03-08

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