Geant4-11
G4OpenGLViewerMessenger.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
29
30#include "G4OpenGLViewer.hh"
33#include "G4UImanager.hh"
34#include "G4UIcommand.hh"
35#include "G4UIdirectory.hh"
37#include "G4UIcmdWithADouble.hh"
38#include "G4UIcmdWithABool.hh"
39#include "G4UIcmdWithAString.hh"
41#include "G4VisManager.hh"
42#include <sstream>
43
46
49{
51 return fpInstance;
52}
53
55{
56 G4bool omitable;
57
58 fpDirectory = new G4UIdirectory("/vis/ogl/");
59 fpDirectory->SetGuidance("G4OpenGLViewer commands.");
60
62 new G4UIcommand("/vis/ogl/export", this);
63 fpCommandExport->SetGuidance ("Export a screenshot of current OpenGL viewer");
65 ("If name is \"\", filename and extension will have the current value");
67 ("If name is \"toto.png\", set the name to \"toto\" and the format to \"png\".");
69 ("If name is \"toto\", set the name to \"toto\" and the format to current format.");
71 ("Will also add an incremented suffix at the end of the name, except if name is"
72 "\nthe same as previous it will not reset the incremented suffix.");
74 ("Setting size is available only on eps/pdf/svg/ps formats.");
75 G4UIparameter* parameterExport;
76 parameterExport = new G4UIparameter ("name", 's', omitable = true);
77 parameterExport->SetDefaultValue("!");
78 parameterExport->SetGuidance
79 ("By default, will take a default value or the last \"/vis/ogl/set/printFilename\""
80 " value if set.");
81 fpCommandExport->SetParameter(parameterExport);
82 parameterExport = new G4UIparameter ("width", 'd', omitable = true);
83 parameterExport->SetGuidance
84 ("By default, will take the current width of the viewer or \"/vis/ogl/set/printSize\""
85 "\nif set. This parameter is only useful for eps/pdf/svg/ps formats !");
86 parameterExport->SetDefaultValue(-1);
87 fpCommandExport->SetParameter(parameterExport);
88 parameterExport = new G4UIparameter ("height", 'd', omitable = true);
89 parameterExport->SetGuidance
90 ("By default, will take the current height of the viewer or \"/vis/ogl/set/printSize\""
91 "\nif set. This parameter is only useful for eps/pdf/svg/ps formats !");
92 parameterExport->SetDefaultValue(-1);
93 fpCommandExport->SetParameter(parameterExport);
94
95 fpCommandFlushAt = new G4UIcommand("/vis/ogl/flushAt", this);
97 ("Controls the rate at which graphics primitives are flushed to screen.");
99 ("Flushing to screen is an expensive operation so to speed drawing choose"
100 "\nan action suitable for your application. Note that detectors are flushed"
101 "\nto screen anyway at end of drawing, and events are flushed to screen"
102 "\nanyway depending on /vis/scene/endOfEventAction and endOfRunAction.");
104 ("For NthPrimitive and NthEvent the second parameter N is operative.");
106 ("For \"never\", detectors and events are still flushed as described above.");
107 G4UIparameter* parameterFlushAt;
108 parameterFlushAt = new G4UIparameter ("action", 's', omitable = true);
109 parameterFlushAt->SetParameterCandidates
110 ("endOfEvent endOfRun eachPrimitive NthPrimitive NthEvent never");
111 parameterFlushAt->SetDefaultValue("NthEvent");
112 fpCommandFlushAt->SetParameter(parameterFlushAt);
113 parameterFlushAt = new G4UIparameter ("N", 'i', omitable = true);
114 parameterFlushAt->SetDefaultValue(100);
115 fpCommandFlushAt->SetParameter(parameterFlushAt);
116
117 fpDirectorySet = new G4UIdirectory ("/vis/ogl/set/");
118 fpDirectorySet->SetGuidance("G4OpenGLViewer set commands.");
119
121 new G4UIcmdWithAnInteger("/vis/ogl/set/displayListLimit", this);
123 ("Set/reset display list number of primitive limit (to avoid memory exhaustion).");
124 fpCommandDisplayListLimit->SetParameterName("limit", omitable = true);
126 fpCommandDisplayListLimit->SetRange("limit>=10000");
127
129 new G4UIcommand("/vis/ogl/set/exportFormat", this);
130 fpCommandExportFormat->SetGuidance ("Set export format");
131 fpCommandExportFormat->SetGuidance ("By default, pdf/eps/svg/ps are available. Depending of viewers several other format are available.");
132 fpCommandExportFormat->SetGuidance ("Try /vis/ogl/set/exportFormat without parameters to see them.");
133 fpCommandExportFormat->SetGuidance ("Changing format will reset the incremental suffix to 0.");
134 G4UIparameter* parameterExportFormat;
135 parameterExportFormat = new G4UIparameter ("format", 's', omitable = true);
136 parameterExportFormat->SetDefaultValue("");
137 fpCommandExportFormat->SetParameter(parameterExportFormat);
138
140 new G4UIcommand("/vis/ogl/set/printFilename", this);
141 fpCommandPrintFilename->SetGuidance ("Set print filename");
142 fpCommandPrintFilename->SetGuidance ("Setting 'incremental' will increment filename by one at each new print, starting at 0");
143 G4UIparameter* parameterPrintFilename;
144 parameterPrintFilename = new G4UIparameter ("name", 's', omitable = true);
145 parameterPrintFilename->SetDefaultValue("G4OpenGL");
146 fpCommandPrintFilename->SetParameter(parameterPrintFilename);
147 parameterPrintFilename = new G4UIparameter ("incremental", 'b', omitable = true);
148 parameterPrintFilename->SetDefaultValue(1);
149 fpCommandPrintFilename->SetParameter(parameterPrintFilename);
150
151 fpCommandPrintMode = new G4UIcmdWithAString("/vis/ogl/set/printMode",this);
152 fpCommandPrintMode->SetGuidance("Set print mode, only available for \"ps\" format");
153 fpCommandPrintMode->SetParameterName("print_mode",omitable = true);
154 fpCommandPrintMode->SetCandidates("vectored pixmap");
156
158 new G4UIcommand("/vis/ogl/set/printSize", this);
159 fpCommandPrintSize->SetGuidance ("Set print size");
160 fpCommandPrintSize->SetGuidance ("Tip : -1 will mean 'print size' = 'window size'");
161 fpCommandPrintSize->SetGuidance (" Setting size greater than your maximum graphic card capacity , will set the size to maximum size.");
162 G4UIparameter* parameterPrintSize;
163 parameterPrintSize = new G4UIparameter ("width", 'd', omitable = false);
164 parameterPrintSize->SetDefaultValue(-1);
165 fpCommandPrintSize->SetParameter(parameterPrintSize);
166 parameterPrintSize = new G4UIparameter ("height", 'd', omitable = false);
167 parameterPrintSize->SetDefaultValue(-1);
168 fpCommandPrintSize->SetParameter(parameterPrintSize);
169
171 new G4UIcmdWithABool("/vis/ogl/set/transparency", this);
173 ("True/false to enable/disable rendering of transparent objects.");
175 ("transparency-enabled", omitable = true);
177}
178
180{
182 delete fpCommandPrintSize;
183 delete fpCommandPrintMode;
187 delete fpDirectorySet;
188 delete fpCommandFlushAt;
189 delete fpCommandExport;
190 delete fpDirectory;
191
192 delete fpInstance;
193}
194
196(G4UIcommand* command, G4String newValue)
197{
199
200 G4VViewer* pViewer = pVisManager->GetCurrentViewer();
201 if (!pViewer) {
202 G4cout <<
203 "G4OpenGLViewerMessenger::SetNewValue: No current viewer."
204 "\n \"/vis/open\", or similar, to get one."
205 << G4endl;
206 return;
207 }
208
209 G4VSceneHandler* pSceneHandler = pViewer->GetSceneHandler();
210 if (!pSceneHandler) {
211 G4cout <<
212 "G4OpenGLViewerMessenger::SetNewValue: This viewer has no scene handler."
213 "\n Shouldn't happen - please report circumstances."
214 "\n (Viewer is \"" << pViewer->GetName() << "\".)"
215 "\n Try \"/vis/open\", or similar, to get one."
216 << G4endl;
217 return;
218 }
219
220 G4OpenGLViewer* pOGLViewer = dynamic_cast<G4OpenGLViewer*>(pViewer);
221 if (!pOGLViewer) {
222 G4cout <<
223 "G4OpenGLViewerMessenger::SetNewValue: Current viewer is not of type"
224 "\n OGL. (It is \""
225 << pViewer->GetName() <<
226 "\".)\n Use \"/vis/viewer/select\" or \"/vis/open\"."
227 << G4endl;
228 return;
229 }
230
231 G4OpenGLSceneHandler* pOGLSceneHandler =
232 dynamic_cast<G4OpenGLSceneHandler*>(pSceneHandler);
233 if (!pOGLSceneHandler) {
234 G4cout <<
235 "G4OpenGLViewerMessenger::SetNewValue: Current scene handler is not of type"
236 "\n OGL. (Viewer is \"" << pViewer->GetName() << "\".)"
237 "\n (Scene handler is \"" << pSceneHandler->GetName() << "\".)"
238 "\n Use \"/vis/sceneHandler/list\" and \"/vis/sceneHandler/select\""
239 "\n or \"/vis/open\"."
240 << G4endl;
241 return;
242 }
243
244 if (command == fpCommandExport)
245 {
247 G4int width,height;
248 std::istringstream iss(newValue);
249 iss >> name >> width >> height;
250 pOGLViewer->exportImage(name, width, height);
251
252 if (pOGLViewer->fVP.IsAutoRefresh())
253 G4UImanager::GetUIpointer()->ApplyCommand("/vis/viewer/refresh");
254 return;
255 }
256
257 if (command == fpCommandExportFormat)
258 {
260 std::istringstream iss(newValue);
261 iss >> name;
262 pOGLViewer->setExportImageFormat(name);
263
264 return;
265 }
266
267 if (command == fpCommandFlushAt)
268 {
269 static G4bool firstTime = true;
270 static std::map<G4String,G4OpenGLSceneHandler::FlushAction> actionMap;
271 if (firstTime) {
272 actionMap["endOfEvent"] = G4OpenGLSceneHandler::endOfEvent;
273 actionMap["endOfRun"] = G4OpenGLSceneHandler::endOfRun;
274 actionMap["eachPrimitive"] = G4OpenGLSceneHandler::eachPrimitive;
275 actionMap["NthPrimitive"] = G4OpenGLSceneHandler::NthPrimitive;
276 actionMap["NthEvent"] = G4OpenGLSceneHandler::NthEvent;
277 actionMap["never"] = G4OpenGLSceneHandler::never;
278 firstTime = false;
279 }
281 G4int entitiesFlushInterval;
282 std::istringstream iss(newValue);
283 iss >> action >> entitiesFlushInterval;
284 pOGLSceneHandler->SetFlushAction(actionMap[action]);
285 pOGLSceneHandler->SetEntitiesFlushInterval(entitiesFlushInterval);
286 return;
287 }
288
289 if (command == fpCommandPrintFilename)
290 {
292 G4bool inc;
293 std::istringstream iss(newValue);
294 iss >> name
295 >> inc;
296 pOGLViewer->setExportFilename(name,inc);
297 return;
298 }
299
300 if (command == fpCommandPrintMode)
301 {
302 if (newValue == "vectored") pOGLViewer->fVectoredPs = true;
303
304 if (newValue == "pixmap") pOGLViewer->fVectoredPs = false;
305 return;
306 }
307
308 if (command == fpCommandPrintSize)
309 {
310 G4int width,height;
311 std::istringstream iss(newValue);
312 iss >> width
313 >> height;
314 pOGLViewer->setExportSize(width,height);
315 return;
316 }
317
318 if (command == fpCommandTransparency)
319 {
320 pOGLViewer->transparency_enabled = command->ConvertToBool(newValue);
321 if (pOGLViewer->fVP.IsAutoRefresh())
322 G4UImanager::GetUIpointer()->ApplyCommand("/vis/viewer/refresh");
323 return;
324 }
325
326 // Stored viewer commands
327 G4OpenGLStoredViewer* pOGLSViewer =
328 dynamic_cast<G4OpenGLStoredViewer*>(pViewer);
329
330 if (!pOGLSViewer)
331 {
332 G4cout <<
333 "G4OpenGLViewerMessenger::SetNewValue: Current viewer is not of type OGLS."
334 "\n (It is \"" << pViewer->GetName() << "\".)"
335 "\n This feature is only implemented for OGL Stored viewers."
336 "\n Use \"/vis/viewer/select\" or \"/vis/open OGLS...\"."
337 << G4endl;
338 return;
339 }
340
341 // Scene handler commands
342 G4OpenGLStoredSceneHandler* pOGLSSceneHandler =
343 dynamic_cast<G4OpenGLStoredSceneHandler*>(pViewer->GetSceneHandler());
344
345 if (!pOGLSSceneHandler) {
346 G4cout <<
347 "G4OpenGLViewerMessenger::SetNewValue: Current scene handler is not of type"
348 "\n OGLS (Stored). (Viewer is \"" << pViewer->GetName() << "\".)"
349 "\n (Scene handler is \"" << pSceneHandler->GetName() << "\".)"
350 "\n This feature is only implemented for OGL Stored"
351 "\n scene handlers. Use \"/vis/viewer/select\" or \"/vis/open OGLS...\"."
352 << G4endl;
353 return;
354 }
355
356 if (command == fpCommandDisplayListLimit)
357 {
358 G4int displayListLimit =
360 pOGLSSceneHandler->SetDisplayListLimit(displayListLimit);
361 return;
362 }
363}
bool G4bool
Definition: G4Types.hh:86
int G4int
Definition: G4Types.hh:85
#define G4endl
Definition: G4ios.hh:57
G4GLOB_DLL std::ostream G4cout
static void SetFlushAction(FlushAction action)
static void SetEntitiesFlushInterval(G4int interval)
static void SetDisplayListLimit(G4int lim)
static G4OpenGLViewerMessenger * GetInstance()
static G4OpenGLViewerMessenger * fpInstance
void SetNewValue(G4UIcommand *, G4String)
G4UIcmdWithAString * fpCommandPrintMode
G4UIcmdWithABool * fpCommandTransparency
G4UIcmdWithAnInteger * fpCommandDisplayListLimit
bool setExportImageFormat(std::string format, bool quiet=false)
bool setExportFilename(G4String name, G4bool inc=true)
void setExportSize(G4int, G4int)
G4bool transparency_enabled
virtual bool exportImage(std::string name="", int width=-1, int height=-1)
void SetParameterName(const char *theName, G4bool omittable, G4bool currentAsDefault=false)
void SetDefaultValue(G4bool defVal)
void SetCandidates(const char *candidateList)
void SetParameterName(const char *theName, G4bool omittable, G4bool currentAsDefault=false)
void SetDefaultValue(const char *defVal)
void SetParameterName(const char *theName, G4bool omittable, G4bool currentAsDefault=false)
static G4int GetNewIntValue(const char *paramString)
void SetDefaultValue(G4int defVal)
void SetParameter(G4UIparameter *const newParameter)
Definition: G4UIcommand.hh:146
void SetGuidance(const char *aGuidance)
Definition: G4UIcommand.hh:156
static G4bool ConvertToBool(const char *st)
Definition: G4UIcommand.cc:545
void SetRange(const char *rs)
Definition: G4UIcommand.hh:120
G4int ApplyCommand(const char *aCommand)
Definition: G4UImanager.cc:485
static G4UImanager * GetUIpointer()
Definition: G4UImanager.cc:77
void SetDefaultValue(const char *theDefaultValue)
void SetGuidance(const char *theGuidance)
void SetParameterCandidates(const char *theString)
const G4String & GetName() const
const G4String & GetName() const
G4ViewParameters fVP
Definition: G4VViewer.hh:219
G4VSceneHandler * GetSceneHandler() const
G4bool IsAutoRefresh() const
G4VViewer * GetCurrentViewer() const
static G4VisManager * GetInstance()
const char * name(G4int ptype)