Geant4 11.1.1
Toolkit for the simulation of the passage of particles through matter
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
G4ToolsSGOffscreenViewer.hh
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// Guy Barrand 09th June 2022
27
28#ifndef G4TOOLSSGOFFSCREENVIEWER_HH
29#define G4TOOLSSGOFFSCREENVIEWER_HH
30
31#include "G4ToolsSGViewer.hh"
32
33#include "G4UIcmdWithABool.hh"
34
35#include <tools/fpng>
36#include <tools/toojpeg>
37
38#include <tools/offscreen/sg_viewer>
39#include <tools/tos>
40#include <tools/sto>
41
42class G4ToolsSGOffscreenViewer : public G4ToolsSGViewer<tools::offscreen::session,tools::offscreen::sg_viewer> {
44public:
45 G4ToolsSGOffscreenViewer(tools::offscreen::session& a_session,G4ToolsSGSceneHandler& a_scene_handler, const G4String& a_name)
46 :parent(a_session,a_scene_handler,a_name)
47 ,fFileName("auto")
48 ,fFilePrefix("auto")
49 ,fFileIndex(0)
50 ,fResetFileIndex(false)
51 {
53 }
54 virtual ~G4ToolsSGOffscreenViewer() = default;
55protected:
58public:
59 virtual void Initialise() {
60 if(fSGViewer) return; //done.
61 //::printf("debug : G4ToolsSGOffscreenViewer::Initialize\n");
62 // auto refresh true produces too much files.
63 fVP.SetAutoRefresh(false);
65 fSGViewer = new tools::offscreen::sg_viewer(fSGSession
70 ,fName);
71 fSGViewer->set_file_format("zb_png");
72 fSGViewer->set_file_name("out.png");
73 fSGViewer->set_png_writer(tools::fpng::write);
74 fSGViewer->set_jpeg_writer(tools::toojpeg::write);
75 fSGViewer->set_do_transparency(true);
76 fSGViewer->set_top_to_bottom(false); //if using tools::fpng, tools::toojpeg.
77 }
78 virtual void SetView() {
79 //::printf("debug : G4ToolsSGOffscreenViewer::SetView\n");
80 fVP.SetGlobalMarkerScale(1); //WARNING: for __APPLE__, the G4ToolsSGQtViewer set it to 2.
82 }
83
84 virtual void DrawView() {
86 fLastVP = fVP;
87 ProcessView(); // Clears store and processes scene only if necessary.
88 //::printf("debug : G4ToolsSGOffscreenViewer::DrawView %s\n",fName.c_str());
89 if(fSGViewer) {
91 if(fFileName=="auto") {
92 std::string prefix;
93 if(fFilePrefix=="auto") {
94 prefix = "g4tsg_offscreen_"+fSGViewer->file_format()+"_";
95 } else {
97 }
98 std::string suffix;
100 std::string file_name = prefix+tools::tos(GetFileIndex())+"."+suffix;
101 fSGViewer->set_file_name(file_name);
102 }
103 } else {
104 fSGViewer->set_file_name(fFileName);
105 }
106 if(fSGViewer->write_paper()) {
108 G4cout << "File " << fSGViewer->file_name() << " produced." << G4endl;
109 }
110 }
111 }
112 }
113
114 virtual void ClearView() {
115 //::printf("debug : G4ToolsSGOffscreenViewer::ClearView %s\n",fName.c_str());
116 }
117 virtual void ShowView() {
118 //::printf("debug : G4ToolsSGOffscreenViewer::ShowView %s\n",fName.c_str());
119 }
120 virtual void FinishView() {
121 //::printf("debug : G4ToolsSGOffscreenViewer::FinishView %s\n",fName.c_str());
122 if(fSGViewer) {
124 }
125 }
126
127protected:
128 void SetSize(unsigned int a_w,unsigned int a_h) {
129 if(!fSGViewer) return;
130 if(!a_w || !a_h) {
132 } else {
133 fSGViewer->set_size(a_w,a_h);
134 }
135 }
136
137 void SetFileFormat(const G4String& a_format) {
138 if(!fSGViewer) return;
139 fSGViewer->set_file_format(a_format);
140 }
141
142 void SetDoTransparency(bool a_value) {
143 if(!fSGViewer) return;
144 fSGViewer->set_do_transparency(a_value);
145 }
146
147 void SetFileName(const G4String& a_file,const G4String& a_prefix,bool a_reset_index) {
148 fFileName = a_file;
149 fFilePrefix = a_prefix;
150 fResetFileIndex = a_reset_index;
151 }
152
153 void SetGL2PSSort(const G4String& a_sort) {
154 if(!fSGViewer) return;
155 fSGViewer->set_opts_1(a_sort);
156 }
157
158 void SetGL2PSOptions(const G4String& a_opts) {
159 if(!fSGViewer) return;
160 fSGViewer->set_opts_2(a_opts);
161 }
162
163 class Messenger: public G4VVisCommand {
164 public:
165 static void Create() {static Messenger s_messenger;}
166 private:
167 Messenger() {
168 G4UIparameter* parameter;
169 //////////////////////////////////////////////////////////
170 //////////////////////////////////////////////////////////
171 cmd_format = new G4UIcommand("/vis/tsg/offscreen/set/format", this);
172 cmd_format->SetGuidance("Set file format.");
173 cmd_format->SetGuidance("Available formats are:");
174 cmd_format->SetGuidance("- zb_png: tools::sg offscreen zbuffer put in a png file.");
175 cmd_format->SetGuidance("- zb_jpeg: tools::sg offscreen zbuffer put in a jpeg file.");
176 cmd_format->SetGuidance("- zb_ps: tools::sg offscreen zbuffer put in a PostScript file.");
177 cmd_format->SetGuidance("- gl2ps_eps: gl2ps producing eps");
178 cmd_format->SetGuidance("- gl2ps_ps: gl2ps producing ps");
179 cmd_format->SetGuidance("- gl2ps_pdf: gl2ps producing pdf");
180 cmd_format->SetGuidance("- gl2ps_svg: gl2ps producing svg");
181 cmd_format->SetGuidance("- gl2ps_tex: gl2ps producing tex");
182 cmd_format->SetGuidance("- gl2ps_pgf: gl2ps producing pgf");
183
184 parameter = new G4UIparameter("format",'s',true);
185 parameter->SetDefaultValue("gl2ps_eps");
186 cmd_format->SetParameter (parameter);
187
188 //////////////////////////////////////////////////////////
189 //////////////////////////////////////////////////////////
190 cmd_file = new G4UIcommand("/vis/tsg/offscreen/set/file", this);
191 cmd_file->SetGuidance("Set file name.");
192 cmd_file->SetGuidance("Default file name is \"auto\" and default format is zb_png.");
193 cmd_file->SetGuidance("If file name is \"auto\", the output file name is built from");
194 cmd_file->SetGuidance("a viewer index counter with the form:");
195 cmd_file->SetGuidance(" g4tsg_offscreen_<format>_<index>.<format extension>");
196 cmd_file->SetGuidance("For example:");
197 cmd_file->SetGuidance(" g4tsg_offscreen_zb_png_1.png");
198 cmd_file->SetGuidance(" g4tsg_offscreen_zb_png_2.png");
199 cmd_file->SetGuidance(" ...");
200 cmd_file->SetGuidance("or if format is changed to \"gl2ps_pdf\":");
201 cmd_file->SetGuidance(" g4tsg_offscreen_gl2ps_pdf_3.pdf");
202 cmd_file->SetGuidance("If a prefix parameter is given, the output file name is built from");
203 cmd_file->SetGuidance("a global index counter with the form:");
204 cmd_file->SetGuidance(" <prefix><index>.<format extension>");
205 cmd_file->SetGuidance("For example:");
206 cmd_file->SetGuidance(" /vis/tsg/offscreen/set/file auto my_prefix_");
207 cmd_file->SetGuidance("will produce:");
208 cmd_file->SetGuidance(" my_prefix_1.png");
209 cmd_file->SetGuidance(" my_prefix_2.png");
210 cmd_file->SetGuidance(" ...");
211 cmd_file->SetGuidance("You can reset the index by specifying true as last argument:");
212 cmd_file->SetGuidance(" /vis/tsg/offscreen/set/file auto other_prefix_ true");
213 cmd_file->SetGuidance("will produce:");
214 cmd_file->SetGuidance(" other_prefix_1.png");
215 cmd_file->SetGuidance(" other_prefix_2.png");
216 cmd_file->SetGuidance(" ...");
217
218 parameter = new G4UIparameter("file",'s',true);
219 parameter->SetDefaultValue("auto");
220 cmd_file->SetParameter (parameter);
221
222 parameter = new G4UIparameter("prefix",'s',true);
223 parameter->SetDefaultValue("auto");
224 cmd_file->SetParameter (parameter);
225
226 parameter = new G4UIparameter("reset_index",'b',true);
227 parameter->SetDefaultValue("false");
228 cmd_file->SetParameter (parameter);
229
230 //////////////////////////////////////////////////////////
231 //////////////////////////////////////////////////////////
232 cmd_size = new G4UIcommand("/vis/tsg/offscreen/set/size", this);
233 cmd_size->SetGuidance("Set viewer size in pixels.");
234 cmd_size->SetGuidance
235 ("If width and/or height is set to zero, the viewer size specified with /vis/viewer/create (/vis/open) is taken.");
236 cmd_size->SetGuidance(" About the picture size, note that the gl2ps files will grow with the number of primitives");
237 cmd_size->SetGuidance("(gl2ps does not have a zbuffer logic). The \"zb\" files will not grow with the number of");
238 cmd_size->SetGuidance("primitives, but with the size of the viewer. It should be preferred for scenes with");
239 cmd_size->SetGuidance("a lot of objects to render. With zb, to have a better rendering, do not hesitate to");
240 cmd_size->SetGuidance("have a large viewer size.");
241
242 parameter = new G4UIparameter("width",'i',false);
243 parameter->SetDefaultValue("0");
244 cmd_size->SetParameter (parameter);
245
246 parameter = new G4UIparameter("height",'i',false);
247 parameter->SetDefaultValue("0");
248 cmd_size->SetParameter (parameter);
249
250 //////////////////////////////////////////////////////////
251 //////////////////////////////////////////////////////////
252 cmd_do_transparency = new G4UIcmdWithABool("/vis/tsg/offscreen/set/transparency", this);
253 cmd_do_transparency->SetGuidance("True/false to enable/disable rendering of transparent objects.");
254 cmd_do_transparency->SetGuidance("This may be usefull if using file formats, as the gl2ps ones, unable to handle transparency.");
255 cmd_do_transparency->SetParameterName("transparency-enabled",true);
256 cmd_do_transparency->SetDefaultValue(true);
257
258 //////////////////////////////////////////////////////////
259 //////////////////////////////////////////////////////////
260 cmd_gl2ps_sort = new G4UIcommand("/vis/tsg/offscreen/gl2ps/set/sort", this);
261 cmd_gl2ps_sort->SetGuidance("Set gl2ps sort algorithm when creating the file.");
262
263 cmd_gl2ps_sort->SetGuidance("The sort argument could be:");
264 cmd_gl2ps_sort->SetGuidance(" NO_SORT");
265 cmd_gl2ps_sort->SetGuidance(" SIMPLE_SORT");
266 cmd_gl2ps_sort->SetGuidance(" BSP_SORT");
267 cmd_gl2ps_sort->SetGuidance("The default being BSP_SORT");
268
269 parameter = new G4UIparameter("sort",'s',true);
270 parameter->SetDefaultValue("BSP_SORT");
271 cmd_gl2ps_sort->SetParameter (parameter);
272
273 //////////////////////////////////////////////////////////
274 //////////////////////////////////////////////////////////
275 cmd_gl2ps_opts = new G4UIcommand("/vis/tsg/offscreen/gl2ps/set/options", this);
276 cmd_gl2ps_opts->SetGuidance("Set gl2ps options passed when creating the file.");
277
278 cmd_gl2ps_opts->SetGuidance("Options is a list of items separated by |. An item can be:");
279 cmd_gl2ps_opts->SetGuidance(" NONE");
280 cmd_gl2ps_opts->SetGuidance(" DRAW_BACKGROUND");
281 cmd_gl2ps_opts->SetGuidance(" SIMPLE_LINE_OFFSET");
282 cmd_gl2ps_opts->SetGuidance(" SILENT");
283 cmd_gl2ps_opts->SetGuidance(" BEST_ROOT");
284 cmd_gl2ps_opts->SetGuidance(" OCCLUSION_CULL");
285 cmd_gl2ps_opts->SetGuidance(" NO_TEXT");
286 cmd_gl2ps_opts->SetGuidance(" LANDSCAPE");
287 cmd_gl2ps_opts->SetGuidance(" NO_PS3_SHADING");
288 cmd_gl2ps_opts->SetGuidance(" NO_PIXMAP");
289 cmd_gl2ps_opts->SetGuidance(" USE_CURRENT_VIEWPORT");
290 cmd_gl2ps_opts->SetGuidance(" COMPRESS");
291 cmd_gl2ps_opts->SetGuidance(" NO_BLENDING");
292 cmd_gl2ps_opts->SetGuidance(" TIGHT_BOUNDING_BOX");
293 cmd_gl2ps_opts->SetGuidance(" NO_OPENGL_CONTEXT");
294 cmd_gl2ps_opts->SetGuidance(" NO_TEX_FONTSIZE");
295 cmd_gl2ps_opts->SetGuidance(" PORTABLE_SORT");
296 cmd_gl2ps_opts->SetGuidance("The default (typical) list of options is:");
297 cmd_gl2ps_opts->SetGuidance(" SILENT|OCCLUSION_CULL|BEST_ROOT|DRAW_BACKGROUND");
298
299 parameter = new G4UIparameter("options",'s',true);
300 parameter->SetDefaultValue("SILENT|OCCLUSION_CULL|BEST_ROOT|DRAW_BACKGROUND");
301 cmd_gl2ps_opts->SetParameter (parameter);
302
303
304 }
305 virtual ~Messenger() {
306 delete cmd_format;
307 delete cmd_file;
308 delete cmd_size;
309 delete cmd_do_transparency;
310 delete cmd_gl2ps_sort;
311 delete cmd_gl2ps_opts;
312 }
313 public:
314 virtual void SetNewValue(G4UIcommand* a_cmd,G4String a_value) {
317 if (!viewer) {
318 if (verbosity >= G4VisManager::errors) G4cerr << "ERROR: No current viewer." << G4endl;
319 return;
320 }
321 G4ToolsSGOffscreenViewer* tsg_viewer = dynamic_cast<G4ToolsSGOffscreenViewer*>(viewer);
322 if(!tsg_viewer) {
323 G4cout << "G4ToolsSGOffscreenViewer::Messenger::SetNewValue:"
324 << " current viewer is not a G4ToolsSGOffscreenViewer." << G4endl;
325 return;
326 }
327 std::vector<std::string> args;
328 tools::double_quotes_tokenize(a_value,args);
329 if(args.size()!=a_cmd->GetParameterEntries()) return;
330 if(a_cmd==cmd_format) {
331 if(!IsKnownFormat(args[0])) {
332 G4cout << "G4ToolsSGOffscreenViewer::Messenger::SetNewValue:"
333 << " unknown file format " << args[0] << "." << G4endl;
334 return;
335 }
336 tsg_viewer->SetFileFormat(args[0]);
337 } else if(a_cmd==cmd_file) {
338 G4bool reset_index = G4UIcommand::ConvertToBool(args[2].c_str());
339 tsg_viewer->SetFileName(args[0],args[1],reset_index);
340 } else if(a_cmd==cmd_size) {
341 unsigned int w,h;
342 if(!tools::to(args[0],w)) w = 0;
343 if(!tools::to(args[1],h)) h = 0;
344 tsg_viewer->SetSize(w,h);
345 } else if(a_cmd==cmd_do_transparency) {
346 G4bool _do = a_cmd->ConvertToBool(args[0].c_str());
347 tsg_viewer->SetDoTransparency(_do);
348 } else if(a_cmd==cmd_gl2ps_sort) {
349 tsg_viewer->SetGL2PSSort(args[0]);
350 } else if(a_cmd==cmd_gl2ps_opts) {
351 tsg_viewer->SetGL2PSOptions(args[0]);
352 }
353 }
354 private:
355 G4UIcommand* cmd_format;
356 G4UIcommand* cmd_file;
357 G4UIcommand* cmd_size;
358 G4UIcmdWithABool* cmd_do_transparency;
359 G4UIcommand* cmd_gl2ps_sort;
360 G4UIcommand* cmd_gl2ps_opts;
361 };
362
363protected:
364 unsigned int GetFileIndex() {
366 fFileIndex++;
367 return fFileIndex;
368 }
369
370 static bool IsKnownFormat(const std::string& a_format) {
371 if(a_format=="gl2ps_eps") return true;
372 if(a_format=="gl2ps_ps") return true;
373 if(a_format=="gl2ps_pdf") return true;
374 if(a_format=="gl2ps_svg") return true;
375 if(a_format=="gl2ps_tex") return true;
376 if(a_format=="gl2ps_pgf") return true;
377 if(a_format=="zb_ps") return true;
378 if(a_format=="zb_png") return true;
379 if(a_format=="zb_jpeg") return true;
380 return false;
381 }
382
383 static bool GetFormatExtension(const std::string& a_format,std::string& a_ext) {
384 if(a_format=="gl2ps_eps") {a_ext = "eps";return true;}
385 if(a_format=="gl2ps_ps") {a_ext = "ps";return true;}
386 if(a_format=="gl2ps_pdf") {a_ext = "pdf";return true;}
387 if(a_format=="gl2ps_svg") {a_ext = "svg";return true;}
388 if(a_format=="gl2ps_tex") {a_ext = "tex";return true;}
389 if(a_format=="gl2ps_pgf") {a_ext = "pgf";return true;}
390 if(a_format=="zb_ps") {a_ext = "ps";return true;}
391 if(a_format=="zb_png") {a_ext = "png";return true;}
392 if(a_format=="zb_jpeg") {a_ext = "jpeg";return true;}
393 a_ext.clear();
394 return false;
395 }
396
397protected:
398 std::string fFileName;
399 std::string fFilePrefix;
400 unsigned int fFileIndex;
402};
403
404#endif
bool G4bool
Definition: G4Types.hh:86
G4GLOB_DLL std::ostream G4cerr
#define G4endl
Definition: G4ios.hh:57
G4GLOB_DLL std::ostream G4cout
virtual void SetNewValue(G4UIcommand *a_cmd, G4String a_value)
void SetSize(unsigned int a_w, unsigned int a_h)
void SetFileFormat(const G4String &a_format)
void SetFileName(const G4String &a_file, const G4String &a_prefix, bool a_reset_index)
void SetGL2PSSort(const G4String &a_sort)
G4ToolsSGOffscreenViewer(const G4ToolsSGOffscreenViewer &a_from)
static bool GetFormatExtension(const std::string &a_format, std::string &a_ext)
void SetGL2PSOptions(const G4String &a_opts)
G4ToolsSGOffscreenViewer & operator=(const G4ToolsSGOffscreenViewer &)
virtual ~G4ToolsSGOffscreenViewer()=default
static bool IsKnownFormat(const std::string &a_format)
G4ToolsSGOffscreenViewer(tools::offscreen::session &a_session, G4ToolsSGSceneHandler &a_scene_handler, const G4String &a_name)
void TouchPlotters(tools::sg::node &)
void SetParameterName(const char *theName, G4bool omittable, G4bool currentAsDefault=false)
void SetDefaultValue(G4bool defVal)
std::size_t GetParameterEntries() const
Definition: G4UIcommand.hh:139
void SetParameter(G4UIparameter *const newParameter)
Definition: G4UIcommand.hh:147
void SetGuidance(const char *aGuidance)
Definition: G4UIcommand.hh:157
static G4bool ConvertToBool(const char *st)
Definition: G4UIcommand.cc:549
void SetDefaultValue(const char *theDefaultValue)
G4bool fNeedKernelVisit
Definition: G4VViewer.hh:227
void ProcessView()
Definition: G4VViewer.cc:107
G4String fName
Definition: G4VViewer.hh:218
G4ViewParameters fDefaultVP
Definition: G4VViewer.hh:221
G4ViewParameters fVP
Definition: G4VViewer.hh:220
static G4VisManager * GetVisManager()
void SetAutoRefresh(G4bool)
void SetGlobalMarkerScale(G4double globalMarkerScale)
G4int GetWindowAbsoluteLocationHintY(G4int) const
unsigned int GetWindowSizeHintX() const
G4int GetWindowAbsoluteLocationHintX(G4int) const
unsigned int GetWindowSizeHintY() const
G4VViewer * GetCurrentViewer() const
static Verbosity GetVerbosity()