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[ 10.5 ] [ 10.5.p1 ] [ 10.6 ] [ 10.6.p1 ] [ 10.6.p2 ] [ 10.6.p3 ] [ 10.7 ] [ 10.7.p1 ] [ 10.7.p2 ] [ 10.7.p3 ] [ 10.7.p4 ] [ 11.0 ] [ 11.0.p1 ] [ 11.0.p2 ] [ 11.0.p3, ] [ 11.0.p4 ] [ 11.1 ] [ 11.1.1 ] [ 11.1.2 ] [ 11.1.3 ] [ 11.2 ] [ 11.2.1 ] [ 11.2.2 ] [ 11.3.0 ]

  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 #include "G4OpenGLViewerMessenger.hh"
 29 
 30 #include "G4OpenGLViewer.hh"
 31 #include "G4OpenGLStoredViewer.hh"
 32 #include "G4OpenGLStoredSceneHandler.hh"
 33 #include "G4UImanager.hh"
 34 #include "G4UIcommand.hh"
 35 #include "G4UIdirectory.hh"
 36 #include "G4UIcmdWithoutParameter.hh"
 37 #include "G4UIcmdWithADouble.hh"
 38 #include "G4UIcmdWithABool.hh"
 39 #include "G4UIcmdWithAString.hh"
 40 #include "G4UIcmdWithAnInteger.hh"
 41 #include "G4VisManager.hh"
 42 #include <sstream>
 43 
 44 G4OpenGLViewerMessenger*
 45 G4OpenGLViewerMessenger::fpInstance = 0;
 46 
 47 G4OpenGLViewerMessenger*
 48 G4OpenGLViewerMessenger::GetInstance()
 49 {
 50   if (!fpInstance) fpInstance = new G4OpenGLViewerMessenger;
 51   return fpInstance;
 52 }
 53 
 54 G4OpenGLViewerMessenger::G4OpenGLViewerMessenger()
 55 {
 56   G4bool omitable;
 57 
 58   fpDirectory = new G4UIdirectory("/vis/ogl/");
 59   fpDirectory->SetGuidance("G4OpenGLViewer commands.");
 60 
 61   fpCommandExport =
 62   new G4UIcommand("/vis/ogl/export", this);
 63   fpCommandExport->SetGuidance ("Export a screenshot of current OpenGL viewer");
 64   fpCommandExport->SetGuidance
 65   ("If name is \"\", filename and extension will have the current value");
 66   fpCommandExport->SetGuidance
 67   ("If name is \"toto.png\", set the name to \"toto\" and the format to \"png\".");
 68   fpCommandExport->SetGuidance
 69   ("If name is \"toto\", set the name to \"toto\" and the format to current format.");
 70   fpCommandExport->SetGuidance
 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.");
 73   fpCommandExport->SetGuidance
 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);
 96   fpCommandFlushAt->SetGuidance
 97   ("Controls the rate at which graphics primitives are flushed to screen.");
 98   fpCommandFlushAt->SetGuidance
 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.");
103   fpCommandFlushAt->SetGuidance
104   ("For NthPrimitive and NthEvent the second parameter N is operative.");
105   fpCommandFlushAt->SetGuidance
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 
120   fpCommandDisplayListLimit =
121     new G4UIcmdWithoutParameter("/vis/ogl/set/displayListLimit", this);
122   fpCommandDisplayListLimit->SetGuidance
123   ("This command is no longer relevant. There is no longer any limit on the"
124    "\nnumber of display lists - except, of course, the available memory in"
125    "\nyour computer. Keep an eye on that. Good luck!");
126 
127   fpCommandExportFormat =
128   new G4UIcommand("/vis/ogl/set/exportFormat", this);
129   fpCommandExportFormat->SetGuidance ("Set export format");
130   fpCommandExportFormat->SetGuidance ("By default, pdf/eps/svg/ps are available. Depending of viewers several other format are available.");
131   fpCommandExportFormat->SetGuidance ("Try /vis/ogl/set/exportFormat without parameters to see them.");
132   fpCommandExportFormat->SetGuidance ("Changing format will reset the incremental suffix to 0.");
133   G4UIparameter* parameterExportFormat;
134   parameterExportFormat = new G4UIparameter ("format", 's', omitable = true);
135   parameterExportFormat->SetDefaultValue("");
136   fpCommandExportFormat->SetParameter(parameterExportFormat);
137 
138   fpCommandPrintFilename =
139     new G4UIcommand("/vis/ogl/set/printFilename", this);
140   fpCommandPrintFilename->SetGuidance ("Set print filename");
141   fpCommandPrintFilename->SetGuidance ("Setting 'incremental' will increment filename by one at each new print, starting at 0");
142   G4UIparameter* parameterPrintFilename;
143   parameterPrintFilename = new G4UIparameter ("name", 's', omitable = true);
144   parameterPrintFilename->SetDefaultValue("G4OpenGL");
145   fpCommandPrintFilename->SetParameter(parameterPrintFilename);
146   parameterPrintFilename = new G4UIparameter ("incremental", 'b', omitable = true);
147   parameterPrintFilename->SetDefaultValue(1);
148   fpCommandPrintFilename->SetParameter(parameterPrintFilename);
149   
150   fpCommandPrintMode = new G4UIcmdWithAString("/vis/ogl/set/printMode",this);
151   fpCommandPrintMode->SetGuidance("Set print mode, only available for \"ps\" format");
152   fpCommandPrintMode->SetParameterName("print_mode",omitable = true);
153   fpCommandPrintMode->SetCandidates("vectored pixmap");
154   fpCommandPrintMode->SetDefaultValue("vectored");
155 
156   fpCommandPrintSize =
157     new G4UIcommand("/vis/ogl/set/printSize", this);
158   fpCommandPrintSize->SetGuidance ("Set print size");
159   fpCommandPrintSize->SetGuidance ("Tip : -1 will mean 'print size' = 'window size'");
160   fpCommandPrintSize->SetGuidance ("       Setting size greater than your maximum graphic card capacity , will set the size to maximum  size.");
161   G4UIparameter* parameterPrintSize;
162   parameterPrintSize = new G4UIparameter ("width", 'd', omitable = false);
163   parameterPrintSize->SetDefaultValue(-1);
164   fpCommandPrintSize->SetParameter(parameterPrintSize);
165   parameterPrintSize = new G4UIparameter ("height", 'd', omitable = false);
166   parameterPrintSize->SetDefaultValue(-1);
167   fpCommandPrintSize->SetParameter(parameterPrintSize);
168 
169   fpCommandTransparency =
170     new G4UIcmdWithABool("/vis/ogl/set/transparency", this);
171   fpCommandTransparency->SetGuidance
172     ("True/false to enable/disable rendering of transparent objects.");
173   fpCommandTransparency->SetParameterName
174     ("transparency-enabled", omitable = true);
175   fpCommandTransparency->SetDefaultValue(true);
176 }
177 
178 G4OpenGLViewerMessenger::~G4OpenGLViewerMessenger ()
179 {
180   delete fpCommandTransparency;
181   delete fpCommandPrintSize;
182   delete fpCommandPrintMode;
183   delete fpCommandPrintFilename;
184   delete fpCommandExportFormat;
185   delete fpCommandDisplayListLimit;
186   delete fpDirectorySet;
187   delete fpCommandFlushAt;
188   delete fpCommandExport;
189   delete fpDirectory;
190 
191   delete fpInstance;
192 }
193 
194 void G4OpenGLViewerMessenger::SetNewValue
195 (G4UIcommand* command, G4String newValue)
196 {
197   G4VisManager* pVisManager = G4VisManager::GetInstance();
198 
199   G4VViewer* pViewer = pVisManager->GetCurrentViewer();
200   if (!pViewer) {
201     G4cout <<
202       "G4OpenGLViewerMessenger::SetNewValue: No current viewer."
203       "\n  \"/vis/open\", or similar, to get one."
204            << G4endl;
205     return;
206   }
207 
208   G4VSceneHandler* pSceneHandler = pViewer->GetSceneHandler();
209   if (!pSceneHandler) {
210     G4cout <<
211     "G4OpenGLViewerMessenger::SetNewValue: This viewer has no scene handler."
212     "\n  Shouldn't happen - please report circumstances."
213     "\n  (Viewer is \"" << pViewer->GetName() << "\".)"
214     "\n  Try \"/vis/open\", or similar, to get one."
215     << G4endl;
216     return;
217   }
218   
219   G4OpenGLViewer* pOGLViewer = dynamic_cast<G4OpenGLViewer*>(pViewer);
220   if (!pOGLViewer) {
221     G4cout <<
222       "G4OpenGLViewerMessenger::SetNewValue: Current viewer is not of type"
223       "\n  OGL.  (It is \""
224      << pViewer->GetName() <<
225       "\".)\n  Use \"/vis/viewer/select\" or \"/vis/open\"."
226            << G4endl;
227     return;
228   }
229 
230   G4OpenGLSceneHandler* pOGLSceneHandler =
231   dynamic_cast<G4OpenGLSceneHandler*>(pSceneHandler);
232   if (!pOGLSceneHandler) {
233     G4cout <<
234     "G4OpenGLViewerMessenger::SetNewValue: Current scene handler is not of type"
235     "\n  OGL.  (Viewer is \"" << pViewer->GetName() << "\".)"
236     "\n  (Scene handler is \"" << pSceneHandler->GetName() << "\".)"
237     "\n  Use \"/vis/sceneHandler/list\" and \"/vis/sceneHandler/select\""
238     "\n  or \"/vis/open\"."
239     << G4endl;
240     return;
241   }
242 
243   if (command == fpCommandExport)
244   {
245     G4String name;
246     G4int width,height;
247     std::istringstream iss(newValue);
248     iss >> name >> width >> height;
249     pOGLViewer->exportImage(name, width, height);
250     
251     if (pOGLViewer->fVP.IsAutoRefresh())
252       G4UImanager::GetUIpointer()->ApplyCommand("/vis/viewer/refresh");
253     return;
254   }
255   
256   if (command == fpCommandExportFormat)
257   {
258     G4String name;
259     std::istringstream iss(newValue);
260     iss >> name;
261     pOGLViewer->setExportImageFormat(name);
262 
263     return;
264   }
265 
266   if (command == fpCommandFlushAt)
267   {
268     static G4bool firstTime = true;
269     static std::map<G4String,G4OpenGLSceneHandler::FlushAction> actionMap;
270     if (firstTime) {
271       actionMap["endOfEvent"]    = G4OpenGLSceneHandler::endOfEvent;
272       actionMap["endOfRun"]      = G4OpenGLSceneHandler::endOfRun;
273       actionMap["eachPrimitive"] = G4OpenGLSceneHandler::eachPrimitive;
274       actionMap["NthPrimitive"]  = G4OpenGLSceneHandler::NthPrimitive;
275       actionMap["NthEvent"]      = G4OpenGLSceneHandler::NthEvent;
276       actionMap["never"]         = G4OpenGLSceneHandler::never;
277       firstTime = false;
278     }
279     G4String action;
280     G4int entitiesFlushInterval;
281     std::istringstream iss(newValue);
282     iss >> action >> entitiesFlushInterval;
283     pOGLSceneHandler->SetFlushAction(actionMap[action]);
284     pOGLSceneHandler->SetEntitiesFlushInterval(entitiesFlushInterval);
285     return;
286   }
287 
288   if (command == fpCommandPrintFilename)
289     {
290       G4String name;
291       G4bool inc;
292       std::istringstream iss(newValue);
293       iss >> name
294     >> inc;
295       pOGLViewer->setExportFilename(name,inc);
296       return;
297     }
298 
299   if (command == fpCommandPrintMode)
300     {
301       if (newValue == "vectored") pOGLViewer->fVectoredPs = true;
302 
303       if (newValue == "pixmap") pOGLViewer->fVectoredPs = false;
304       return;
305     }
306 
307   if (command == fpCommandPrintSize)
308     {
309       G4int width,height;
310       std::istringstream iss(newValue);
311       iss >> width
312     >> height;
313       pOGLViewer->setExportSize(width,height);
314       return;
315     }
316 
317   if (command == fpCommandTransparency)
318     {
319       pOGLViewer->transparency_enabled = command->ConvertToBool(newValue);
320       if (pOGLViewer->fVP.IsAutoRefresh())
321   G4UImanager::GetUIpointer()->ApplyCommand("/vis/viewer/refresh");
322       return;
323     }
324 
325   // Stored viewer commands
326   G4OpenGLStoredViewer* pOGLSViewer =
327     dynamic_cast<G4OpenGLStoredViewer*>(pViewer);
328 
329   if (!pOGLSViewer)
330     {
331       G4cout <<
332   "G4OpenGLViewerMessenger::SetNewValue: Current viewer is not of type OGLS."
333   "\n  (It is \"" << pViewer->GetName() << "\".)"
334   "\n  This feature is only implemented for OGL Stored viewers."
335   "\n  Use \"/vis/viewer/select\" or \"/vis/open OGLS...\"."
336        << G4endl;
337       return;
338     }
339 
340   // Scene handler commands
341   G4OpenGLStoredSceneHandler* pOGLSSceneHandler =
342     dynamic_cast<G4OpenGLStoredSceneHandler*>(pViewer->GetSceneHandler());
343 
344   if (!pOGLSSceneHandler) {
345     G4cout <<
346   "G4OpenGLViewerMessenger::SetNewValue: Current scene handler is not of type"
347   "\n  OGLS (Stored).  (Viewer is \"" << pViewer->GetName() << "\".)"
348   "\n  (Scene handler is \"" << pSceneHandler->GetName() << "\".)"
349   "\n  This feature is only implemented for OGL Stored"
350   "\n  scene handlers.  Use \"/vis/viewer/select\" or \"/vis/open OGLS...\"."
351            << G4endl;
352     return;
353   }
354 
355   if (command == fpCommandDisplayListLimit)
356     {
357       G4cerr << command->GetGuidanceLine(0) << G4endl;
358       return;
359     }
360 }
361