ECCE @ EIC Software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
G4VisCommandsSceneHandler.cc
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file G4VisCommandsSceneHandler.cc
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 //
27 
28 // /vis/sceneHandler commands - John Allison 10th October 1998
29 
31 
32 #include "G4VisManager.hh"
33 #include "G4GraphicsSystemList.hh"
34 #include "G4VisCommandsScene.hh"
35 #include "G4UImanager.hh"
36 #include "G4UIcommand.hh"
37 #include "G4UIcmdWithAString.hh"
38 #include "G4ios.hh"
39 #include <sstream>
40 
42 
44  G4bool omitable, currentAsDefault;
45  fpCommand = new G4UIcmdWithAString ("/vis/sceneHandler/attach", this);
46  fpCommand -> SetGuidance ("Attaches scene to current scene handler.");
47  fpCommand -> SetGuidance
48  ("If scene-name is omitted, current scene is attached. To see scenes and"
49  "\nscene handlers, use \"/vis/scene/list\" and \"/vis/sceneHandler/list\"");
50  fpCommand -> SetParameterName ("scene-name",
51  omitable = true,
52  currentAsDefault = true);
53 }
54 
56  delete fpCommand;
57 }
58 
60  G4Scene* pScene = fpVisManager -> GetCurrentScene ();
61  return pScene ? pScene -> GetName () : G4String("");
62 }
63 
65  G4String newValue) {
66 
68 
69  G4String& sceneName = newValue;
70 
71  if (sceneName.length () == 0) {
72  if (verbosity >= G4VisManager::warnings) {
73  G4cout <<
74  "WARNING: No scene specified. Maybe there are no scenes available"
75  "\n yet. Please create one." << G4endl;
76  }
77  return;
78  }
79 
80  G4VSceneHandler* pSceneHandler = fpVisManager -> GetCurrentSceneHandler ();
81  if (!pSceneHandler) {
82  if (verbosity >= G4VisManager::errors) {
83  G4cerr <<
84  "ERROR: Current scene handler not defined. Please select or create one."
85  << G4endl;
86  }
87  return;
88  }
89 
90  G4SceneList& sceneList = fpVisManager -> SetSceneList ();
91 
92  if (sceneList.empty ()) {
93  if (verbosity >= G4VisManager::errors) {
94  G4cerr <<
95  "ERROR: No valid scenes available yet. Please create one."
96  << G4endl;
97  }
98  return;
99  }
100 
101  G4int iScene, nScenes = sceneList.size ();
102  for (iScene = 0; iScene < nScenes; iScene++) {
103  if (sceneList [iScene] -> GetName () == sceneName) break;
104  }
105  if (iScene < nScenes) {
106  G4Scene* pScene = sceneList [iScene];
107  pSceneHandler -> SetScene (pScene);
108  // Make sure scene is current...
109  fpVisManager -> SetCurrentScene (pScene);
110  // Refresh viewer, if any (only if auto-refresh)...
111  G4VViewer* pViewer = pSceneHandler -> GetCurrentViewer();
112  if (pViewer && pViewer -> GetViewParameters().IsAutoRefresh()) {
113  pViewer -> SetView ();
114  pViewer -> ClearView ();
115  pViewer -> DrawView ();
116  }
117  if (verbosity >= G4VisManager::confirmations) {
118  G4cout << "Scene \"" << sceneName
119  << "\" attached to scene handler \""
120  << pSceneHandler -> GetName () <<
121  ".\n (You may have to refresh with \"/vis/viewer/flush\" if view"
122  " is not \"auto-refresh\".)"
123  << G4endl;
124  }
125  }
126  else {
127  if (verbosity >= G4VisManager::errors) {
128  G4cerr << "ERROR: Scene \"" << sceneName
129  << "\" not found. Use \"/vis/scene/list\" to see possibilities."
130  << G4endl;
131  }
132  }
133 }
134 
136 
138  G4bool omitable;
139  fpCommand = new G4UIcommand ("/vis/sceneHandler/create", this);
140  fpCommand -> SetGuidance
141  ("Creates an scene handler for a specific graphics system.");
142  fpCommand -> SetGuidance
143  ("Attaches current scene, if any. (You can change attached scenes with"
144  "\n\"/vis/sceneHandler/attach\".) Invents a scene handler name if not"
145  "\nsupplied. This scene handler becomes current.");
146  G4UIparameter* parameter;
147  parameter = new G4UIparameter ("graphics-system-name",
148  's', omitable = false);
149  const G4GraphicsSystemList& gslist =
150  fpVisManager -> GetAvailableGraphicsSystems ();
151  G4String candidates;
152  for (const auto gs: gslist) {
153  const G4String& name = gs -> GetName ();
154  candidates += name + ' ';
155  for (const auto& nickname: gs -> GetNicknames ()) {
156  if (nickname != name) candidates += nickname + ' ';
157  }
158  }
159  candidates = candidates.strip ();
160  parameter -> SetParameterCandidates(candidates);
161  fpCommand -> SetParameter (parameter);
162  parameter = new G4UIparameter
163  ("scene-handler-name", 's', omitable = true);
164  parameter -> SetCurrentAsDefault (true);
165  fpCommand -> SetParameter (parameter);
166 }
167 
169  delete fpCommand;
170 }
171 
173  std::ostringstream oss;
174  oss << "scene-handler-" << fId;
175  return oss.str();
176 }
177 
179 
180  G4String graphicsSystemName;
181  const G4VGraphicsSystem* graphicsSystem =
182  fpVisManager -> GetCurrentGraphicsSystem ();
183  if (graphicsSystem) {
184  graphicsSystemName = graphicsSystem -> GetName ();
185  }
186  else {
187  const G4GraphicsSystemList& gslist =
188  fpVisManager -> GetAvailableGraphicsSystems ();
189  if (gslist.size ()) {
190  graphicsSystemName = gslist [0] -> GetName ();
191  }
192  else {
193  graphicsSystemName = "none";
194  }
195  }
196 
197  return graphicsSystemName + " " + NextName ();
198 }
199 
201  G4String newValue) {
202 
204 
205  G4String graphicsSystem, newName;
206  std::istringstream is (newValue);
207  is >> graphicsSystem >> newName;
208 
209  const G4GraphicsSystemList& gsl =
210  fpVisManager -> GetAvailableGraphicsSystems ();
211  G4int nSystems = gsl.size ();
212  if (nSystems <= 0) {
213  if (verbosity >= G4VisManager::errors) {
214  G4cerr << "ERROR: G4VisCommandSceneHandlerCreate::SetNewValue:"
215  " no graphics systems available."
216  "\n Did you instantiate any in"
217  " YourVisManager::RegisterGraphicsSystems()?"
218  << G4endl;
219  }
220  return;
221  }
222  G4int iGS; // Selector index.
223  G4bool found = false;
224  for (iGS = 0; iGS < nSystems; iGS++) {
225  const auto& gs = gsl[iGS];
226  if (graphicsSystem.compareTo(gs->GetName(), G4String::ignoreCase) == 0) {
227  found = true;
228  break; // Match found
229  } else {
230  const auto& nicknames = gs->GetNicknames();
231  for (size_t i = 0; i < nicknames.size(); ++i) {
232  const auto& nickname = nicknames[i];
233  if (graphicsSystem.compareTo (nickname, G4String::ignoreCase) == 0) {
234  found = true;
235  break; // Match found
236  }
237  }
238  if (found) {
239  break; // Match found
240  }
241  }
242  }
243  if (!found) {
244  // Invalid command line argument or none.
245  // This shouldn't happen!!!!!!
246  if (verbosity >= G4VisManager::errors) {
247  G4cerr <<
248  "ERROR: G4VisCommandSceneHandlerCreate::SetNewValue:"
249  "\n invalid graphics system \""
250  << graphicsSystem
251  << "\" requested."
252  << G4endl;
253  }
254  return;
255  }
256 
257  // Check UI session compatibility.
258  G4bool fallback = false;
259  while (!gsl[iGS]->IsUISessionCompatible()) {
260  // Not compatible, search for a fallback
261  fallback = false;
262  G4String fallbackNickname = gsl[iGS]->GetNickname() + "_FALLBACK";
263  for (iGS = 0; iGS < nSystems; iGS++) {
264  const auto& nicknames = gsl[iGS]->GetNicknames();
265  for (size_t i = 0; i < nicknames.size(); ++i) {
266  const auto& nickname = nicknames[i];
267  if (fallbackNickname.compareTo (nickname, G4String::ignoreCase) == 0) {
268  fallback = true;
269  break; // Match found
270  }
271  }
272  if (fallback) {
273  break; // Match found
274  }
275  }
276  if (iGS < 0 || iGS >= nSystems) {
277  if (verbosity >= G4VisManager::errors) {
278  G4cerr <<
279  "ERROR: G4VisCommandSceneHandlerCreate::SetNewValue:"
280  " could not find fallback graphics system for \""
281  << graphicsSystem
282  << "\"."
283  << G4endl;
284  }
285  return;
286  }
287  // A fallback system found...but go back and check this too.
288  }
289 
290  // A graphics system has been found
291  G4VGraphicsSystem* pSystem = gsl [iGS];
292 
293  if (fallback && verbosity >= G4VisManager::warnings) {
294  G4cout << "WARNING: G4VisCommandSceneHandlerCreate::SetNewValue:"
295  "\n Using fallback graphics system: "
296  << pSystem -> GetName ()
297  << " ("
298  << pSystem -> GetNickname ()
299  << ')'
300  << G4endl;
301  }
302 
303  // Set current graphics system in preparation for
304  // creating scene handler.
305  fpVisManager -> SetCurrentGraphicsSystem (pSystem);
306  if (verbosity >= G4VisManager::confirmations) {
307  G4cout << "Graphics system set to "
308  << pSystem -> GetName ()
309  << " ("
310  << pSystem -> GetNickname ()
311  << ')'
312  << G4endl;
313  }
314 
315  // Now deal with name of scene handler.
316  G4String nextName = NextName ();
317  if (newName == "") {
318  newName = nextName;
319  }
320  if (newName == nextName) fId++;
321 
322  const G4SceneHandlerList& list = fpVisManager -> GetAvailableSceneHandlers ();
323  size_t iScene;
324  for (iScene = 0; iScene < list.size (); iScene++) {
325  G4VSceneHandler* sceneHandler = list [iScene];
326  if (sceneHandler -> GetName () == newName) {
327  if (verbosity >= G4VisManager::errors) {
328  G4cerr << "ERROR: Scene handler \"" << newName
329  << "\" already exists." << G4endl;
330  }
331  return;
332  }
333  }
334 
335  //Create scene handler.
336  fpVisManager -> CreateSceneHandler (newName);
337  if (fpVisManager -> GetCurrentSceneHandler () -> GetName () != newName) {
338  if (verbosity >= G4VisManager::errors) {
339  G4cerr << "ERROR: G4VisCommandSceneHandlerCreate::SetNewValue:"
340  " Curious name mismatch."
341  "\n Current name \""
342  << fpVisManager -> GetCurrentSceneHandler () -> GetName ()
343  << "\" is not the new name \""
344  << newName
345  << "\".\n Please report to vis coordinator."
346  << G4endl;
347  }
348  return;
349  }
350 
351  if (verbosity >= G4VisManager::confirmations) {
352  G4cout << "New scene handler \"" << newName << "\" created." << G4endl;
353  }
354 
355  // Attach scene.
356  if (fpVisManager -> GetCurrentScene ())
357  G4UImanager::GetUIpointer () -> ApplyCommand ("/vis/sceneHandler/attach");
358 }
359 
361 
363  G4bool omitable;
364  fpCommand = new G4UIcommand ("/vis/sceneHandler/list", this);
365  fpCommand -> SetGuidance ("Lists scene handler(s).");
366  fpCommand -> SetGuidance
367  ("\"help /vis/verbose\" for definition of verbosity.");
368  G4UIparameter* parameter;
369  parameter = new G4UIparameter("scene-handler-name", 's', omitable = true);
370  parameter -> SetDefaultValue ("all");
371  fpCommand -> SetParameter (parameter);
372  parameter = new G4UIparameter ("verbosity", 's', omitable = true);
373  parameter -> SetDefaultValue ("warnings");
374  fpCommand -> SetParameter (parameter);
375 }
376 
378  delete fpCommand;
379 }
380 
382  return "";
383 }
384 
386  G4String newValue) {
387  G4String name, verbosityString;
388  std::istringstream is (newValue);
389  is >> name >> verbosityString;
390  G4VisManager::Verbosity verbosity =
391  fpVisManager->GetVerbosityValue(verbosityString);
392  const G4VSceneHandler* currentSceneHandler =
393  fpVisManager -> GetCurrentSceneHandler ();
394  G4String currentName;
395  if (currentSceneHandler) currentName = currentSceneHandler->GetName();
396 
397  const G4SceneHandlerList& list = fpVisManager -> GetAvailableSceneHandlers ();
398  G4bool found = false;
399  for (size_t iSH = 0; iSH < list.size (); iSH++) {
400  const G4String& iName = list [iSH] -> GetName ();
401  if (name != "all") {
402  if (name != iName) continue;
403  }
404  found = true;
405  if (iName == currentName) {
406  G4cout << " (current)";
407  }
408  else {
409  G4cout << " ";
410  }
411  G4cout << " scene handler \"" << list [iSH] -> GetName () << "\""
412  << " (" << list [iSH] -> GetGraphicsSystem () -> GetName () << ")";
413  if (verbosity >= G4VisManager::parameters) {
414  G4cout << "\n " << *(list [iSH]);
415  }
416  G4cout << G4endl;
417  }
418  if (!found) {
419  G4cout << "No scene handlers found";
420  if (name != "all") {
421  G4cout << " of name \"" << name << "\"";
422  }
423  G4cout << "." << G4endl;
424  }
425 }
426 
428 
430  G4bool omitable;
431  fpCommand = new G4UIcmdWithAString ("/vis/sceneHandler/select", this);
432  fpCommand -> SetGuidance ("Selects a scene handler.");
433  fpCommand -> SetGuidance
434  ("Makes the scene handler current. \"/vis/sceneHandler/list\" to see"
435  "\n possible scene handler names.");
436  fpCommand -> SetParameterName ("scene-handler-name",
437  omitable = false);
438 }
439 
441  delete fpCommand;
442 }
443 
445  return "";
446 }
447 
449  G4String newValue) {
450 
452 
453  G4String& selectName = newValue;
454  const G4SceneHandlerList& list = fpVisManager -> GetAvailableSceneHandlers ();
455 
456  size_t iSH;
457  for (iSH = 0; iSH < list.size (); iSH++) {
458  if (list [iSH] -> GetName () == selectName) break;
459  }
460  if (iSH < list.size ()) {
461  if (fpVisManager -> GetCurrentSceneHandler () -> GetName ()
462  == selectName) {
463  if (verbosity >= G4VisManager::confirmations) {
464  G4cout << "Scene handler \"" << selectName << "\""
465  << " already selected." << G4endl;
466  }
467  }
468  else {
469  if (verbosity >= G4VisManager::confirmations) {
470  G4cout << "Scene handler \"" << selectName << "\""
471  << " being selected." << G4endl;
472  }
473  fpVisManager -> SetCurrentSceneHandler (list [iSH]);
474  }
475  }
476  else {
477  if (verbosity >= G4VisManager::errors) {
478  G4cerr << "ERROR: Scene handler \"" << selectName << "\""
479  << " not found - \"/vis/sceneHandler/list\" to see possibilities."
480  << G4endl;
481  }
482  }
483 }