source: trunk/Documentation/geant4/UserDocumentation/UsersGuides/ForApplicationDeveloper/html/ch08s02.html @ 902

Last change on this file since 902 was 902, checked in by garnier, 16 years ago

debut de modif de la doc

File size: 17.8 KB
Line 
1<html><head><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"><title>8.2.  Adding Visualization to Your Executable</title><link rel="stylesheet" href="../xml/XSLCustomizationLayer/G4HTMLStylesheet.css" type="text/css"><meta name="generator" content="DocBook XSL Stylesheets V1.71.1"><link rel="start" href="index.html" title="Geant4 User's Guide for Application Developers"><link rel="up" href="ch08.html" title="Chapter 8.  Visualization"><link rel="prev" href="ch08.html" title="Chapter 8.  Visualization"><link rel="next" href="ch08s03.html" title="8.3.  The Visualization Drivers"><script language="JavaScript">
2function remote_win(fName)
3{
4   var url = "AllResources/Detector/geometry.src/" + fName;
5   RemoteWin=window.open(url,"","resizable=no,toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=0,copyhistory=0,width=520,height=520")
6   RemoteWin.creator=self
7}
8</script></head><body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"><div class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="3" align="center">8.2. 
9Adding Visualization to Your Executable
10</th></tr><tr><td width="20%" align="left"><a accesskey="p" href="ch08.html"><img src="AllResources/IconsGIF/prev.gif" alt="Prev"></a> </td><th width="60%" align="center">Chapter 8. 
11Visualization
12</th><td width="20%" align="right"> <a accesskey="n" href="ch08s03.html"><img src="AllResources/IconsGIF/next.gif" alt="Next"></a></td></tr></table><hr></div><div class="sect1" lang="en"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="sect.VisAddExe"></a>8.2. 
13Adding Visualization to Your Executable
14</h2></div></div></div><p>
15This section explains how to incorporate your selected
16visualization drivers into the <code class="literal">main()</code> function and create
17an executable for it. In order to perform visualization with your
18Geant4 executable, you must compile it with realized visualization
19driver(s). You may be dazzled by the number of choices of
20visualization driver, but you need not use all of them at one time.
21</p><div class="sect2" lang="en"><div class="titlepage"><div><div><h3 class="title"><a name="sect.VisAddExe.InstDrv"></a>8.2.1. 
22Installing Visualization Drivers
23</h3></div></div></div><p>
24Depending on what has been installed on your system, several kinds
25of visualization driver are available. One or many drivers may be
26chosen for realization in compilation, depending on your
27visualization requirements. Features and notes on each driver are
28briefly described in <a href="ch08s03.html" title="8.3. 
29The Visualization Drivers
30">Section 8.3</a>
31"<span class="bold"><strong>Visualization Drivers</strong></span>",
32along with links to detailed web pages for the various drivers.
33</p><p>
34Note that not all drivers can be installed on all systems;
35<a href="ch08s03.html#table.VisDrv_1" title="Table 8.1. 
36Required graphics systems and supported platforms for the various
37visualization drivers.
38">Table 8.1</a> in
39<a href="ch08s03.html" title="8.3. 
40The Visualization Drivers
41">Section 8.3</a> lists all the available drivers
42and the platforms on which they can be installed.
43For any of the visualization drivers to work, the corresponding
44graphics system must be installed beforehand.
45</p><p>
46Unless the environment variable <code class="literal">G4VIS_NONE</code> is set to
47"1", visualization drivers that do not depend on external libraries
48are automatically incorporated into Geant4 libraries during their
49installation. (Here "installation of Geant4 libraries" means the
50generation of Geant4 libraries by compilation.) The automatically
51incorporated visualization drivers are: DAWNFILE, HepRepFile,
52HepRepXML, RayTracer, VRML1FILE, VRML2FILE and ATree and
53GAGTree.
54</p><p>
55The OpenGL, Qt, OpenInventor and RayTracerX drivers are not
56incorporated by default. Nor are the DAWN-Network and VRML-Network
57drivers, because they require the network setting of the installed
58machine. In order to incorporate them, the environment variables
59"<code class="literal">G4VIS_BUILD_DRIVERNAME_DRIVER</code>" should be set to
60"<code class="literal">1</code>" before installing the Geant4 libraries:
61
62</p><div class="informalexample"><pre class="programlisting">
63     setenv G4VIS_BUILD_OPENGLX_DRIVER      1  # OpenGL-Xlib driver
64     setenv G4VIS_BUILD_OPENGLXM_DRIVER     1  # OpenGL-Motif driver
65     setenv G4VIS_BUILD_OPENGLQT_DRIVER     1  # Qt driver
66     setenv G4VIS_BUILD_OIX_DRIVER          1  # OpenInventor-Xlib driver
67     setenv G4VIS_BUILD_RAYTRACERX_DRIVER   1  # RayTracer-XLib driver
68     setenv G4VIS_BUILD_DAWN_DRIVER         1  # DAWN-Network driver
69     setenv G4VIS_BUILD_VRML_DRIVER         1  # VRML-Network
70</pre></div><p>
71</p><p>
72Unless the environment variable <code class="literal">G4VIS_NONE</code> is set to "1",
73setting any of the above variables sets a C-pre-processor flag of
74the same name. Also the C-pre-processor flag <code class="literal">G4VIS_BUILD</code>
75is set (see config/G4VIS_BUILD.gmk), which incorparates the
76selected driver into the Geant4 libraries.
77</p></div><div class="sect2" lang="en"><div class="titlepage"><div><div><h3 class="title"><a name="sect.VisAddExe.RealVisDrv"></a>8.2.2. 
78How to Realize Visualization Drivers in an Executable
79</h3></div></div></div><p>
80You can realize and use any of the visualization driver(s) you want
81in your Geant4 executable, provided they are among the set
82installed beforehand into the Geant4 libraries. A warning will
83appear if this is not the case.
84</p><p>
85In order to realize visualization drivers, you must instantiate
86and initialize a subclass of <code class="literal">G4VisManager</code> that implements
87the pure virtual function <code class="literal">RegisterGraphicsSystems()</code>. This
88subclass must be compiled in the user's domain to force the loading
89of appropriate libraries in the right order. The easiest way to do
90this is to use <code class="literal">G4VisExecutive</code>, a provided class with
91included implementation. <code class="literal">G4VisExecutive</code> is sensitive to
92the <code class="literal">G4VIS_USE...</code> variables mentioned below.
93</p><p>
94If you do wish to write your own subclass, you may do so. You
95will see how to do this by looking at <code class="literal">G4VisExecutive.icc</code>.
96A typical extract is:
97
98</p><div class="informalexample"><pre class="programlisting">
99     ...
100       RegisterGraphicsSystem (new G4DAWNFILE);
101     ...
102     #ifdef G4VIS_USE_OPENGLX
103       RegisterGraphicsSystem (new G4OpenGLImmediateX);
104       RegisterGraphicsSystem (new G4OpenGLStoredX);
105     #endif
106     ...
107</pre></div><p>
108</p><p>
109If you wish to use <code class="literal">G4VisExecutive</code> but register an
110additional graphics system, <code class="literal">XXX</code> say, you may do so either
111before or after initializing:
112
113</p><div class="informalexample"><pre class="programlisting">
114     visManager-&gt;RegisterGraphicsSytem(new XXX);
115     visManager-&gt;Initialize();
116</pre></div><p>
117</p><p>
118By default, you get the DAWNFILE, HepRepFile, RayTracer,
119VRML1FILE, VRML2FILE, ATree and GAGTree drivers. Additionally, you
120may choose from the OpenGL-Xlib, OpenGL-Motif, Qt, OpenInventor,
121RayTracerX, DAWN-Network and VRML-Network drivers, each of which
122can be selected by setting the proper environment variable:
123
124</p><div class="informalexample"><pre class="programlisting">
125     setenv G4VIS_USE_OPENGLX      1
126     setenv G4VIS_USE_OPENGLXM     1
127     setenv G4VIS_USE_OPENGLQT     1
128     setenv G4VIS_USE_OIX          1
129     setenv G4VIS_USE_RAYTRACERX   1
130     setenv G4VIS_USE_DAWN         1
131     setenv G4VIS_USE_VRML         1
132</pre></div><p>
133</p><p>
134(Of course, this has to be chosen from the set incorporated into
135the Geant4 libraries during their compilation.) Unless the
136environment variable <code class="literal">G4VIS_NONE</code> is set, these set
137C-pre-processor flags of the same name.
138</p><p>
139Also, unless the environment variable <code class="literal">G4VIS_NONE</code> is
140set, the C-pre-processor flag <code class="literal">G4VIS_USE</code> is always set by
141default. This flag is available in describing the <code class="literal">main()</code>
142function.
143</p><p>
144You may have to set additional environment variables for your
145selected visualization drivers and graphics systems. For example,
146the OpenGL driver may require the setting of <code class="literal">OGLHOME</code> which
147points to the location of the OpenGL libraries. For more details,
148see <a href="ch08s03.html" title="8.3. 
149The Visualization Drivers
150">Section 8.3</a> 
151"<span class="bold"><strong>Visualization Drivers</strong></span>" and
152pages linked from there.
153</p></div><div class="sect2" lang="en"><div class="titlepage"><div><div><h3 class="title"><a name="sect.VisAddExe.SampSetup"></a>8.2.3. 
154A Sample Set-up File
155</h3></div></div></div><p>
156The following can be part of the user's <code class="literal">.cshrc</code> or
157<code class="literal">.tcshrc</code> file on a Linux platform to configure the
158environment for creating Geant4 executables available for Geant4
159visualization. This sample is shown only as an example; it may NOT
160correspond to a specific platform configuration and does NOT apply
161in general for any specific setup !
162
163</p><div class="example"><a name="programlist_VisAddExe_1"></a><p class="title"><b>Example 8.1. 
164Part of a sample <code class="literal">.cshrc</code> setup file for the Linux platform.
165</b></p><div class="example-contents"><pre class="programlisting">
166 ############################################################
167 # Main Environment Variables for GEANT4 with Visualization #
168 ############################################################
169
170 ### Platform
171 setenv G4SYSTEM Linux-g++
172
173 ### CLHEP base directory
174 setenv CLHEP_BASE_DIR /opt/local
175
176 ### OpenGL base directory
177 setenv OGLHOME /usr/X11R6
178 
179 ### G4VIS_BUILD
180 ###   Incorporation of OpenGL-Xlib and OpenGL-Motif drivers
181 ###   into Geant4 libraries.
182 setenv G4VIS_BUILD_OPENGLX_DRIVER 1
183 setenv G4VIS_BUILD_OPENGLXM_DRIVER 1
184
185 ### G4VIS_USE
186 ###   Incorporation of OpenGL-Xlib and OpenGL-Motif drivers
187 ###   into Geant4 executables.
188 setenv G4VIS_USE_OPENGLX       1
189 setenv G4VIS_USE_OPENGLXM      1
190
191 ### Viewer for DAWNFILE driver
192 ### Default value is "dawn".  You can change it to, say,
193 ### "david" for volume overlapping tests
194 # setenv G4DAWNFILE_VIEWER david
195
196 ### Viewer for VRMLFILE drivers
197 setenv G4VRMLFILE_VIEWER vrmlview
198
199 ########## end
200</pre></div></div><p><br class="example-break">
201</p></div><div class="sect2" lang="en"><div class="titlepage"><div><div><h3 class="title"><a name="sect.VisAddExe.VisMan"></a>8.2.4. 
202Visualization Manager
203</h3></div></div></div><p>
204Visualization procedures are controlled by the "Visualization
205Manager", a class which must inherit from <span class="emphasis"><em>G4VisManager</em></span>
206defined in the visualization category. Most users will find that
207they can just use the default visualization manager,
208<span class="emphasis"><em>G4VisExecutive</em></span>. The Visualization Manager accepts users'
209requests for visualization, processes them, and passes the
210processed requirements to the abstract interface, i.e., to the
211currently selected visualization driver.
212</p></div><div class="sect2" lang="en"><div class="titlepage"><div><div><h3 class="title"><a name="sect.VisAddExe.WrtMain"></a>8.2.5. 
213How to Write the <code class="literal">main()</code> Function
214</h3></div></div></div><p>
215In order for your Geant4 executable to perform visualization, you
216must instantiate and initialize "your" Visualization Manager in the
217<code class="literal">main()</code> function. The core of the Visualization Manager is
218the class <code class="literal">G4VisManager</code>, defined in the visualization
219category. This class requires that one pure virtual function be
220implemented, namely, <code class="literal">void RegisterGraphicsSystems()</code>. The
221easiest way to do this is to use <code class="literal">G4VisExecutive</code>, as
222described above (but you may write your own class - see above).
223</p><p>
224<a href="ch08s02.html#programlist_VisAddExe_2" title="Example 8.2. 
225The form of the main() function.
226">Example 8.2</a> shows the form of the
227<code class="literal">main()</code> function.
228
229</p><div class="example"><a name="programlist_VisAddExe_2"></a><p class="title"><b>Example 8.2. 
230The form of the <code class="literal">main()</code> function.
231</b></p><div class="example-contents"><pre class="programlisting">
232 //----- C++ source codes: Instantiation and initialization of G4VisManager
233
234 .....
235 // Your Visualization Manager
236 #include "G4VisExecutive.hh"
237 .....
238
239 // Instantiation and initialization of the Visualization Manager
240 #ifdef G4VIS_USE
241 G4VisManager* visManager = new G4VisExecutive;
242 visManager -&gt; initialize ();
243 #endif
244
245 .....
246 #ifdef G4VIS_USE
247 delete visManager;
248 #endif
249
250 //----- end of C++
251</pre></div></div><p><br class="example-break">
252</p><p>
253Alternatively, you can implement an empty
254<code class="literal">RegisterGraphicsSystems()</code> function, and register
255visualization drivers you want directly in your <code class="literal">main()</code>
256function. See <a href="ch08s02.html#programlist_VisAddExe_3" title="Example 8.3. 
257An alternative style for the main() function.
258">Example 8.3</a>.
259
260</p><div class="example"><a name="programlist_VisAddExe_3"></a><p class="title"><b>Example 8.3. 
261An alternative style for the <code class="literal">main()</code> function.
262</b></p><div class="example-contents"><pre class="programlisting">
263 //----- C++ source codes: How to register a visualization driver directly
264 //                        in main() function
265
266 .....
267 G4VisManager* visManager = new G4VisExecutive;
268 visManager -&gt; RegisterGraphicsSystem (new MyGraphicsSystem);
269 .....
270 delete visManager
271
272 //----- end of C++
273</pre></div></div><p><br class="example-break">
274</p><p>
275<span class="emphasis"><em>Do not forget</em></span> to delete the instantiated Visualization
276Manager by yourself. Note that a graphics system for Geant4 Visualization
277may run as a different process. In that case, the destructor of
278<code class="literal">G4VisManager</code> might have to terminate the graphics system
279and/or close the connection.
280</p><p>
281We recommend that the instantiation, initialization, and
282deletion of the Visualization Manager be protected by
283C-pre-processor commands, as in the novice examples. The
284C-pre-processor macro <code class="literal">G4VIS_USE</code> is automatically defined
285unless the environment variable <code class="literal">G4VIS_NONE</code> is set. This
286assumes that you are compiling your Geant4 executable with the
287standard version of GNUmakefile found in the <code class="literal">config</code>
288directory.
289</p><p>
290<a href="ch08s02.html#programlist_VisAddExe_4" title="Example 8.4. 
291An example of the main() function available for
292Geant4 Visualization.
293">Example 8.4</a> shows an example of the
294<code class="literal">main()</code> function available for Geant4 Visualization.
295
296</p><div class="example"><a name="programlist_VisAddExe_4"></a><p class="title"><b>Example 8.4. 
297An example of the <code class="literal">main()</code> function available for
298Geant4 Visualization.
299</b></p><div class="example-contents"><pre class="programlisting">
300 //----- C++ source codes: An example of main() for visualization
301 .....
302 #include "G4VisExecutive.hh"
303 .....
304
305 int main()
306 {
307      // Run Manager
308      G4RunManager * runManager = new G4RunManager;
309
310      // Detector components
311      runManager-&gt;set_userInitialization(new MyDetectorConstruction);
312      runManager-&gt;set_userInitialization(new MyPhysicsList);
313
314      // UserAction classes.
315      runManager-&gt;set_userAction(new MyRunAction);
316      runManager-&gt;set_userAction(new MyPrimaryGeneratorAction);
317      runManager-&gt;set_userAction(new MyEventAction);
318      runManager-&gt;set_userAction(new MySteppingAction);
319
320 #ifdef G4VIS_USE
321      G4VisManager* visManager = new G4VisExecutive;
322      visManager -&gt; initialize ();
323 #endif
324
325      // Event loop
326      // Define (G)UI terminal
327      G4UIsession * session = new G4UIterminal
328      session-&gt;sessionStart();
329
330      delete session;
331      delete runManager;
332
333 #ifdef G4VIS_USE
334      delete visManager;
335 #endif
336
337      return 0;
338 }
339
340 //----- end of C++
341</pre></div></div><p><br class="example-break">
342</p><p>
343Useful information on incorporated visualization drivers can be
344displayed in initializing the Visualization Manager. This is done
345by setting the verbosity flag to an appropriate string:
346
347</p><div class="informalexample"><pre class="programlisting">
348   Simple graded message scheme - give first letter or a digit:
349    0) quiet,         // Nothing is printed.
350    1) startup,       // Startup and endup messages are printed...
351    2) errors,        // ...and errors...
352    3) warnings,      // ...and warnings...
353    4) confirmations, // ...and confirming messages...
354    5) parameters,    // ...and parameters of scenes and views...
355    6) all            // ...and everything available.
356</pre></div><p>
357</p><p>
358For example, in your <code class="literal">main()</code> function, write the following
359code:
360
361</p><div class="informalexample"><pre class="programlisting">
362  ...
363  G4VisManager* visManager = new G4VisExecutive ();
364  visManager -&gt; SetVerboseLevel (1);
365  visManager -&gt; Initialize ();
366  ...
367</pre></div><p>
368</p><p>
369(This can also be set with the <code class="literal">/vis/verbose</code> command.)
370</p></div></div><div class="navfooter"><hr><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="ch08.html"><img src="AllResources/IconsGIF/prev.gif" alt="Prev"></a> </td><td width="20%" align="center"><a accesskey="u" href="ch08.html"><img src="AllResources/IconsGIF/up.gif" alt="Up"></a></td><td width="40%" align="right"> <a accesskey="n" href="ch08s03.html"><img src="AllResources/IconsGIF/next.gif" alt="Next"></a></td></tr><tr><td width="40%" align="left" valign="top">Chapter 8. 
371Visualization
372 </td><td width="20%" align="center"><a accesskey="h" href="index.html"><img src="AllResources/IconsGIF/home.gif" alt="Home"></a></td><td width="40%" align="right" valign="top"> 8.3. 
373The Visualization Drivers
374</td></tr></table></div></body></html>
Note: See TracBrowser for help on using the repository browser.