Geant4 11.4.0
Toolkit for the simulation of the passage of particles through matter
Loading...
Searching...
No Matches
G4VisCommandsSceneHandler.cc
Go to the documentation of this file.
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"
34#include "G4VisCommandsScene.hh"
35#include "G4UImanager.hh"
36#include "G4UIcommand.hh"
37#include "G4UIcmdWithAString.hh"
38#include "G4ios.hh"
39#include <sstream>
40
41#define G4warn G4cout
42
43////////////// /vis/sceneHandler/attach ///////////////////////////////////////
44
46 G4bool omitable, currentAsDefault;
47 fpCommand = new G4UIcmdWithAString ("/vis/sceneHandler/attach", this);
48 fpCommand -> SetGuidance ("Attaches scene to current scene handler.");
49 fpCommand -> SetGuidance
50 ("If scene-name is omitted, current scene is attached. To see scenes and"
51 "\nscene handlers, use \"/vis/scene/list\" and \"/vis/sceneHandler/list\"");
52 fpCommand -> SetParameterName ("scene-name",
53 omitable = true,
54 currentAsDefault = true);
55}
56
60
62 G4Scene* pScene = fpVisManager -> GetCurrentScene ();
63 return pScene ? pScene -> GetName () : G4String("");
64}
65
67 G4String newValue) {
68
69 G4VisManager::Verbosity verbosity = fpVisManager->GetVerbosity();
70
71 G4String& sceneName = newValue;
72
73 if (sceneName.length () == 0) {
74 if (verbosity >= G4VisManager::warnings) {
75 G4cout <<
76 "WARNING: No scene specified. Maybe there are no scenes available"
77 "\n yet. Please create one." << G4endl;
78 }
79 return;
80 }
81
82 G4VSceneHandler* pSceneHandler = fpVisManager -> GetCurrentSceneHandler ();
83 if (!pSceneHandler) {
84 if (verbosity >= G4VisManager::errors) {
85 G4warn <<
86 "ERROR: Current scene handler not defined. Please select or create one."
87 << G4endl;
88 }
89 return;
90 }
91
92 G4SceneList& sceneList = fpVisManager -> SetSceneList ();
93
94 if (sceneList.empty ()) {
95 if (verbosity >= G4VisManager::errors) {
96 G4warn <<
97 "ERROR: No valid scenes available yet. Please create one."
98 << G4endl;
99 }
100 return;
101 }
102
103 std::size_t iScene, nScenes = sceneList.size ();
104 for (iScene = 0; iScene < nScenes; ++iScene) {
105 if (sceneList [iScene] -> GetName () == sceneName) break;
106 }
107 if (iScene < nScenes) {
108 G4Scene* pScene = sceneList [iScene];
109 pSceneHandler -> SetScene (pScene);
110 // Make sure scene is current...
111 fpVisManager -> SetCurrentScene (pScene);
112 // Refresh viewer, if any (only if auto-refresh)...
113 G4VViewer* pViewer = pSceneHandler -> GetCurrentViewer();
114 if (pViewer && pViewer -> GetViewParameters().IsAutoRefresh()) {
115 pViewer -> SetView ();
116 pViewer -> ClearView ();
117 pViewer -> DrawView ();
118 }
119 if (verbosity >= G4VisManager::confirmations) {
120 G4cout << "Scene \"" << sceneName
121 << "\" attached to scene handler \""
122 << pSceneHandler -> GetName () <<
123 ".\n (You may have to refresh with \"/vis/viewer/flush\" if view"
124 " is not \"auto-refresh\".)"
125 << G4endl;
126 }
127 }
128 else {
129 if (verbosity >= G4VisManager::errors) {
130 G4warn << "ERROR: Scene \"" << sceneName
131 << "\" not found. Use \"/vis/scene/list\" to see possibilities."
132 << G4endl;
133 }
134 }
135}
136
137////////////// /vis/sceneHandler/create ///////////////////////////////////////
138
140 G4bool omitable;
141 fpCommand = new G4UIcommand ("/vis/sceneHandler/create", this);
142 fpCommand -> SetGuidance
143 ("Creates an scene handler for a specific graphics system.");
144 fpCommand -> SetGuidance
145 ("Attaches current scene, if any. (You can change attached scenes with"
146 "\n\"/vis/sceneHandler/attach\".) Invents a scene handler name if not"
147 "\nsupplied. This scene handler becomes current.");
148 G4UIparameter* parameter;
149 parameter = new G4UIparameter ("graphics-system-name", 's', omitable = true);
150 parameter -> SetCurrentAsDefault(true);
151 const G4GraphicsSystemList& gslist =
152 fpVisManager -> GetAvailableGraphicsSystems ();
153 G4String candidates = "NO_UI_SESSION "; // A dummy candidate to catch issue
154 for (const auto gs: gslist) {
155 const G4String& name = gs -> GetName ();
156 candidates += name + ' ';
157 for (const auto& nickname: gs -> GetNicknames ()) {
158 if (G4StrUtil::contains(nickname, "FALLBACK")) continue;
159 if (nickname != name) candidates += nickname + ' ';
160 }
161 }
162 G4StrUtil::strip(candidates);
163 parameter -> SetParameterCandidates(candidates);
164 fpCommand -> SetParameter (parameter);
165 parameter = new G4UIparameter
166 ("scene-handler-name", 's', omitable = true);
167 parameter -> SetCurrentAsDefault (true);
168 fpCommand -> SetParameter (parameter);
169}
170
174
175G4String G4VisCommandSceneHandlerCreate::NextName () {
176 std::ostringstream oss;
177 oss << "scene-handler-" << fId;
178 return oss.str();
179}
180
182
183 G4String graphicsSystemName;
184 const G4VGraphicsSystem* graphicsSystem =
185 fpVisManager -> GetCurrentGraphicsSystem ();
186 if (graphicsSystem) {
187 graphicsSystemName = graphicsSystem -> GetName ();
188 }
189 else {
190 graphicsSystemName = fpVisManager->GetDefaultGraphicsSystemName();
191 }
192
193 return graphicsSystemName + " " + NextName ();
194}
195
197 G4String newValue) {
198
199 G4VisManager::Verbosity verbosity = fpVisManager->GetVerbosity();
200
201 G4String graphicsSystem, newName;
202 std::istringstream is (newValue);
203 is >> graphicsSystem >> newName;
204
205 const G4GraphicsSystemList& gsl =
206 fpVisManager -> GetAvailableGraphicsSystems ();
207 std::size_t nSystems = gsl.size ();
208 if (nSystems <= 0) {
210 ed <<
211 "ERROR: G4VisCommandSceneHandlerCreate::SetNewValue:"
212 " no graphics systems available."
213 "\n Did you instantiate any in"
214 " YourVisManager::RegisterGraphicsSystems()?";
215 command->CommandFailed(ed);
216 return;
217 }
218 std::size_t iGS; // Selector index.
219 G4bool found = false;
220 for (iGS = 0; iGS < nSystems; ++iGS) {
221 const auto& gs = gsl[iGS];
222 if (G4StrUtil::icompare(graphicsSystem, gs->GetName()) == 0) {
223 found = true;
224 break; // Match found
225 } else {
226 const auto& nicknames = gs->GetNicknames();
227 for (const auto& nickname : nicknames) {
228 if (G4StrUtil::icompare(graphicsSystem, nickname) == 0) {
229 found = true;
230 break; // Match found
231 }
232 }
233 if (found) {
234 break; // Match found
235 }
236 }
237 }
238 if (!found) {
239 if (graphicsSystem == "NO_UI_SESSION") {
241 ("G4VisCommandSceneHandlerCreate::SetNewValue","visman1001",JustWarning,
242 "This looks like an attempt to use run-time vis driver selection."
243 "\nYou have issued \"/vis/open\" or \"/vis/sceneHandler/create\" without"
244 "\na parameter for the vis driver. This is allowed only if you instantiate"
245 "\na UI session, and only if it is instantiated *before* the first"
246 "\n\"/vis/open\" command. So:"
247 "\na) It is not allowed in batch mode. If you really want to create"
248 "\n some graphics with a file-writing driver in batch mode, you must"
249 "\n request a specific driver on the \"/vis/open\" command line, e.g.,"
250 "\n \"/vis/open TSG_OFFSCREEN\". See, examples/basic/B1/tsg_offscreen.mac."
251 "\nb) If you want to exploit this feature in interactive mode, simply move"
252 "\n the instantiation of the UI session earlier. In any case, this is good"
253 "\n practice in order to capture output in a GUI session.");
254 return;
255 }
256 // Shouldn't happen, since graphicsSystem should be a candidate
258 ed <<
259 "ERROR: G4VisCommandSceneHandlerCreate::SetNewValue:"
260 "\n Invalid graphics system \""
261 << graphicsSystem
262 << "\" requested."
263 << "\n Candidates are:";
264 fpVisManager->PrintAvailableGraphicsSystems(verbosity,ed);
265 command->CommandFailed(ed);
266 return;
267 }
268
269 // Check UI session compatibility.
270 G4bool fallback = false;
271 G4int loopCounter = 0;
272 while (!gsl[iGS]->IsUISessionCompatible()) {
273 std::size_t iGSBeingTested = iGS;
274 // Not compatible, search for a fallback
275 fallback = false;
276 G4String fallbackNickname = gsl[iGS]->GetNickname() + "_FALLBACK";
277 for (iGS = 0; iGS < nSystems; iGS++) {
278 const auto& nicknames = gsl[iGS]->GetNicknames();
279 for (const auto& nickname : nicknames) {
280 if (G4StrUtil::icompare(fallbackNickname, nickname) == 0) {
281 fallback = true;
282 break; // Match found
283 }
284 }
285 if (fallback) {
286 break; // Match found
287 }
288 }
289 if (iGS >= nSystems || loopCounter >=3) {
291 ed << "\"" << gsl[iGSBeingTested]->GetNickname()
292 << "\" is not compatible with the session,"
293 "\nand no fallback system found. Make sure your session is"
294 "\ninstantiated _before_ you create a graphics system.";
295 G4Exception("G4VisCommandSceneHandlerCreate::SetNewValue",
296 "visman1002", JustWarning, ed);
297 return;
298 }
299 // A fallback system found...but go back and check this too.
300 ++loopCounter;
301 }
302
303 // A graphics system has been found
304 G4VGraphicsSystem* pSystem = gsl [iGS];
305
306 if (fallback && verbosity >= G4VisManager::warnings) {
307 G4warn << "WARNING: G4VisCommandSceneHandlerCreate::SetNewValue:"
308 "\n Using fallback graphics system: "
309 << pSystem -> GetName ()
310 << " ("
311 << pSystem -> GetNickname ()
312 << ')'
313 << G4endl;
314 }
315
316 // Now deal with name of scene handler.
317 G4String nextName = NextName ();
318 if (newName == "") {
319 newName = nextName;
320 }
321 if (newName == nextName) fId++;
322
323 const G4SceneHandlerList& list = fpVisManager -> GetAvailableSceneHandlers ();
324 std::size_t iScene;
325 for (iScene = 0; iScene < list.size (); ++iScene) {
326 G4VSceneHandler* sceneHandler = list [iScene];
327 if (sceneHandler -> GetName () == newName) {
329 ed <<
330 "ERROR: Scene handler \"" << newName
331 << "\" already exists.";
332 command->CommandFailed(ed);
333 return;
334 }
335 }
336
337 // If there is an existing viewer, store its view parameters and scene tree
338 if (fpVisManager->GetCurrentViewer()) {
339 fThereWasAViewer = true;
340 auto viewer = fpVisManager->GetCurrentViewer();
341 fExistingVP = viewer->GetViewParameters();
342 fExistingSceneTree = viewer->AccessSceneTree();
343 }
344
345 // Set current graphics system in preparation for
346 // creating scene handler.
347 fpVisManager -> SetCurrentGraphicsSystem (pSystem);
348 if (verbosity >= G4VisManager::confirmations) {
349 G4cout << "Graphics system set to "
350 << pSystem -> GetName ()
351 << " ("
352 << pSystem -> GetNickname ()
353 << ')'
354 << G4endl;
355 }
356
357 //Create scene handler.
358 fpVisManager -> CreateSceneHandler (newName);
359 if (fpVisManager -> GetCurrentSceneHandler () -> GetName () != newName) {
361 ed <<
362 "ERROR: G4VisCommandSceneHandlerCreate::SetNewValue:"
363 " Curious name mismatch."
364 "\n Current name \""
365 << fpVisManager -> GetCurrentSceneHandler () -> GetName ()
366 << "\" is not the new name \""
367 << newName
368 << "\".\n Please report to vis coordinator.";
369 command->CommandFailed(ed);
370 return;
371 }
372
373 if (verbosity >= G4VisManager::confirmations)
374 G4cout << "New scene handler \"" << newName << "\" created." << G4endl;
375
376 if (fpVisManager -> GetCurrentScene ()) {
377 auto errorCode = G4UImanager::GetUIpointer () -> ApplyCommand ("/vis/sceneHandler/attach");
378 if (errorCode) {
380 ed << "sub-command \"/vis/sceneHandler/attach\" failed.";
381 command->CommandFailed(errorCode,ed);
382 return;
383 }
384 }
385}
386
387////////////// /vis/sceneHandler/list ///////////////////////////////////////
388
390 G4bool omitable;
391 fpCommand = new G4UIcommand ("/vis/sceneHandler/list", this);
392 fpCommand -> SetGuidance ("Lists scene handler(s).");
393 fpCommand -> SetGuidance
394 ("\"help /vis/verbose\" for definition of verbosity.");
395 G4UIparameter* parameter;
396 parameter = new G4UIparameter("scene-handler-name", 's', omitable = true);
397 parameter -> SetDefaultValue ("all");
398 fpCommand -> SetParameter (parameter);
399 parameter = new G4UIparameter ("verbosity", 's', omitable = true);
400 parameter -> SetDefaultValue ("warnings");
401 fpCommand -> SetParameter (parameter);
402}
403
407
411
413 G4String newValue) {
414 G4String name, verbosityString;
415 std::istringstream is (newValue);
416 is >> name >> verbosityString;
417 G4VisManager::Verbosity verbosity =
418 fpVisManager->GetVerbosityValue(verbosityString);
419 const G4VSceneHandler* currentSceneHandler =
420 fpVisManager -> GetCurrentSceneHandler ();
421 G4String currentName;
422 if (currentSceneHandler) currentName = currentSceneHandler->GetName();
423
424 const G4SceneHandlerList& list = fpVisManager -> GetAvailableSceneHandlers ();
425 G4bool found = false;
426 for (const auto* iSH : list) {
427 const G4String& iName = iSH -> GetName ();
428 if (name != "all") {
429 if (name != iName) continue;
430 }
431 found = true;
432 if (iName == currentName) {
433 G4cout << " (current)";
434 }
435 else {
436 G4cout << " ";
437 }
438 G4cout << " scene handler \"" << iSH -> GetName () << "\""
439 << " (" << iSH -> GetGraphicsSystem () -> GetName () << ")";
440 if (verbosity >= G4VisManager::parameters) {
441 G4cout << "\n " << *iSH;
442 }
443 G4cout << G4endl;
444 }
445 if (!found) {
446 G4cout << "No scene handlers found";
447 if (name != "all") {
448 G4cout << " of name \"" << name << "\"";
449 }
450 G4cout << "." << G4endl;
451 }
452}
453
454////////////// /vis/sceneHandler/select ///////////////////////////////////////
455
457 G4bool omitable;
458 fpCommand = new G4UIcmdWithAString ("/vis/sceneHandler/select", this);
459 fpCommand -> SetGuidance ("Selects a scene handler.");
460 fpCommand -> SetGuidance
461 ("Makes the scene handler current. \"/vis/sceneHandler/list\" to see"
462 "\n possible scene handler names.");
463 fpCommand -> SetParameterName ("scene-handler-name",
464 omitable = false);
465}
466
470
474
476 G4String newValue) {
477
478 G4VisManager::Verbosity verbosity = fpVisManager->GetVerbosity();
479
480 G4String& selectName = newValue;
481 const G4SceneHandlerList& list = fpVisManager -> GetAvailableSceneHandlers ();
482
483 std::size_t iSH;
484 for (iSH = 0; iSH < list.size (); iSH++) {
485 if (list [iSH] -> GetName () == selectName) break;
486 }
487 if (iSH < list.size ()) {
488 if (fpVisManager -> GetCurrentSceneHandler () -> GetName ()
489 == selectName) {
490 if (verbosity >= G4VisManager::confirmations) {
491 G4cout << "Scene handler \"" << selectName << "\""
492 << " already selected." << G4endl;
493 }
494 }
495 else {
496 if (verbosity >= G4VisManager::confirmations) {
497 G4cout << "Scene handler \"" << selectName << "\""
498 << " being selected." << G4endl;
499 }
500 fpVisManager -> SetCurrentSceneHandler (list [iSH]);
501 }
502 }
503 else {
504 if (verbosity >= G4VisManager::errors) {
505 G4warn << "ERROR: Scene handler \"" << selectName << "\""
506 << " not found - \"/vis/sceneHandler/list\" to see possibilities."
507 << G4endl;
508 }
509 }
510}
@ JustWarning
void G4Exception(const char *originOfException, const char *exceptionCode, G4ExceptionSeverity severity, const char *description)
std::ostringstream G4ExceptionDescription
#define G4warn
Definition G4Scene.cc:41
bool G4bool
Definition G4Types.hh:86
int G4int
Definition G4Types.hh:85
#define G4endl
Definition G4ios.hh:67
G4GLOB_DLL std::ostream G4cout
void CommandFailed(G4int errCode, G4ExceptionDescription &ed)
static G4UImanager * GetUIpointer()
const G4String & GetName() const
static G4ViewParameters fExistingVP
static G4VisManager * fpVisManager
static G4SceneTreeItem fExistingSceneTree
static G4bool fThereWasAViewer
G4String GetCurrentValue(G4UIcommand *command)
void SetNewValue(G4UIcommand *command, G4String newValue)
G4String GetCurrentValue(G4UIcommand *command)
void SetNewValue(G4UIcommand *command, G4String newValue)
G4String GetCurrentValue(G4UIcommand *command)
void SetNewValue(G4UIcommand *command, G4String newValue)
void SetNewValue(G4UIcommand *command, G4String newValue)
G4String GetCurrentValue(G4UIcommand *command)
G4int icompare(std::string_view lhs, std::string_view rhs)
Case insensitive comparison of two strings.
void strip(G4String &str, char ch=' ')
Remove leading and trailing characters from string.
G4bool contains(const G4String &str, std::string_view ss)
Check if a string contains a given substring.