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

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

en test pour les refresh, ne marche pas bien

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