source: trunk/Documentation/geant4/UserDocumentation/UsersGuides/ForApplicationDeveloper/html/ch08s05.html @ 901

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

Add Geant4 Documentation at 8.12.2008

File size: 26.8 KB
Line 
1<html><head><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"><title>8.5.  Controlling Visualization from Compiled Code</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="ch08s04.html" title="8.4.  Controlling Visualization from Commands"><link rel="next" href="ch08s06.html" title="8.6.  Visualization Attributes"><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.5. 
9Controlling Visualization from Compiled Code
10</th></tr><tr><td width="20%" align="left"><a accesskey="p" href="ch08s04.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="ch08s06.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.VisCntCmpl"></a>8.5. 
13Controlling Visualization from Compiled Code
14</h2></div></div></div><p>
15While a Geant4 simulation is running, visualization can be
16performed without user intervention. This is accomplished by
17calling methods of the Visualization Manager from methods of the
18user action classes (<span class="emphasis"><em>G4UserRunAction</em></span> and
19<span class="emphasis"><em>G4UserEventAction</em></span>, for example). In this section methods of
20the class <span class="emphasis"><em>G4VVisManager</em></span>, which is part of the
21<code class="literal">graphics_reps</code> category, are described and examples of
22their use are given.
23</p><div class="sect2" lang="en"><div class="titlepage"><div><div><h3 class="title"><a name="sect.VisCntCmpl.VsMng"></a>8.5.1. 
24G4VVisManager
25</h3></div></div></div><p>
26The Visualization Manager is implemented by classes
27<span class="emphasis"><em>G4VisManager</em></span> and <span class="emphasis"><em>G4VisExecutive</em></span>.
28See <a href="ch08s02.html" title="8.2. 
29Adding Visualization to Your Executable
30">Section 8.2</a>
31"<span class="bold"><strong>Making a Visualization Executable</strong></span>". In order
32that your Geant4 be compilable either with or without the visualization
33category, you should not use these classes directly in your C++
34source code, other than in the <code class="literal">main()</code> function. Instead,
35you should use their abstract base class <span class="emphasis"><em>G4VVisManager</em></span>,
36defined in the <code class="literal">intercoms</code> category.
37</p><p>
38The pointer to the concrete instance of the real Visualization
39Manager can be obtained as follows:
40
41</p><div class="informalexample"><pre class="programlisting">
42  //----- Getting a pointer to the concrete Visualization Manager instance
43  G4VVisManager* pVVisManager = G4VVisManager::GetConcreteInstance();
44</pre></div><p> 
45</p><p>
46The method <code class="literal">G4VVisManager::GetConcreteInstance()</code> returns
47<code class="literal">NULL</code> if Geant4 is not ready for visualization. Thus your
48C++ source code should be protected as follows:
49
50</p><div class="informalexample"><pre class="programlisting">
51  //----- How to protect your C++ source codes in visualization
52  if (pVVisManager) {
53      ....
54      pVVisManager -&gt;Draw (...);
55      ....
56  }
57</pre></div><p> 
58</p></div><div class="sect2" lang="en"><div class="titlepage"><div><div><h3 class="title"><a name="sect.VisCntCmpl.DtcCmp"></a>8.5.2. 
59Visualization of detector components
60</h3></div></div></div><p>
61If you have already constructed detector components with logical
62volumes to which visualization attributes are properly assigned,
63you are almost ready for visualizing detector components. All you
64have to do is to describe proper visualization commands within your
65C++ codes, using the <code class="literal">ApplyCommand()</code> method.
66</p><p>
67For example, the following is sample C++ source codes to
68visualize the detector components:
69
70</p><div class="informalexample"><pre class="programlisting">
71  //----- C++ source code: How to visualize detector components (2)
72  //                       ... using visualization commands in source codes
73 
74  G4VVisManager* pVVisManager = G4VVisManager::GetConcreteInstance() ;
75
76  if(pVVisManager)
77  {
78      ... (camera setting etc) ...
79      G4UImanager::GetUIpointer()-&gt;ApplyCommand("/vis/drawVolume");
80      G4UImanager::GetUIpointer()-&gt;ApplyCommand("/vis/viewer/flush");
81  }
82
83  //-----  end of C++ source code
84 
85</pre></div><p>
86</p><p>
87In the above, you should also describe <code class="literal">/vis/open</code> command
88somewhere in your C++ codes or execute the command from (G)UI at
89the executing stage.
90</p></div><div class="sect2" lang="en"><div class="titlepage"><div><div><h3 class="title"><a name="sect.VisCntCmpl.Trjct"></a>8.5.3. 
91Visualization of trajectories
92</h3></div></div></div><p>
93In order to visualize trajectories, you can use the method <code class="literal">void
94G4Trajectory::DrawTrajectory()</code> defined in the tracking
95category. In the implementation of this method, the following
96drawing method of <span class="emphasis"><em>G4VVisManager</em></span> is used:
97
98</p><div class="informalexample"><pre class="programlisting">
99    //----- A drawing method of G4Polyline
100    virtual void G4VVisManager::Draw (const G4Polyline&amp;, ...) ;
101</pre></div><p>
102</p><p>
103The real implementation of this method is described in the class
104<span class="emphasis"><em>G4VisManager</em></span>.
105</p><p>
106At the end of one event, a set of trajectories can be stored as
107a list of <span class="emphasis"><em>G4Trajectory</em></span> objects. Therefore you can visualize
108trajectories, for example, at the end of each event, by
109implementing the method <code class="literal">MyEventAction::EndOfEventAction()</code>
110as follows:
111
112</p><div class="informalexample"><pre class="programlisting">
113  //----- C++ source codes
114  void ExN03EventAction::EndOfEventAction(const G4Event* evt)
115  {
116    ..... 
117    // extract the trajectories and draw them
118    if (G4VVisManager::GetConcreteInstance())
119      {
120       G4TrajectoryContainer* trajectoryContainer = evt-&gt;GetTrajectoryContainer();
121       G4int n_trajectories = 0;
122       if (trajectoryContainer) n_trajectories = trajectoryContainer-&gt;entries();
123 
124       for (G4int i=0; i &lt; n_trajectories; i++)
125          { G4Trajectory* trj=(G4Trajectory*)((*(evt-&gt;GetTrajectoryContainer()))[i]);
126            if (drawFlag == "all") trj-&gt;DrawTrajectory(50);
127            else if ((drawFlag == "charged")&amp;&amp;(trj-&gt;GetCharge() != 0.))
128                                    trj-&gt;DrawTrajectory(50);
129            else if ((drawFlag == "neutral")&amp;&amp;(trj-&gt;GetCharge() == 0.))
130                                    trj-&gt;DrawTrajectory(50);
131          }
132    }
133  } 
134  //----- end of C++ source codes
135</pre></div><p>
136</p></div><div class="sect2" lang="en"><div class="titlepage"><div><div><h3 class="title"><a name="sect.VisCntCmpl.EnhTrjct"></a>8.5.4. 
137Enhanced trajectory drawing
138</h3></div></div></div><p>
139It is possible to use the enhanced trajectory drawing functionality
140in compiled code as well as from commands. Multiple trajectory
141models can be instantiated, configured and registered with
142G4VisManager. For details, see the section on
143<a href="ch08s07.html#sect.VisEnhTrj.CntlCmpl" title="8.7.4. 
144Controlling from Compiled Code
145">Section 8.7.4</a> 
146Enhanced Trajectory Drawing.
147</p></div><div class="sect2" lang="en"><div class="titlepage"><div><div><h3 class="title"><a name="sect.VisCntCmpl.AttTrjct"></a>8.5.5. 
148HepRep Attributes for Trajectories
149</h3></div></div></div><p>
150The HepRep file formats, HepRepFile and HepRepXML, attach various
151attributes to trajectories such that you can view these attributes,
152label trajectories by these attributes or make visibility cuts
153based on these attributes. If you use the default Geant4 trajectory
154class, from /tracking/src/G4Trajectory.cc, available attributes
155will be:
156
157</p><div class="itemizedlist"><ul type="disc" compact><li><p>
158    Track ID
159  </p></li><li><p>
160    Parent ID
161  </p></li><li><p>
162    Particle Name
163  </p></li><li><p>
164    Charge
165  </p></li><li><p>
166    PDG Encoding
167  </p></li><li><p>
168    Momentum 3-Vector
169  </p></li><li><p>
170    Momentum magnitude
171  </p></li><li><p>
172    Number of points
173  </p></li></ul></div><p>
174</p><p>
175You can add additional attributes of your choosing by modifying the
176relevant part of G4Trajectory (look for the methods GetAttDefs and
177CreateAttValues). If you are using your own trajectory class, you
178may want to consider copying these methods from G4Trajectory.
179</p></div><div class="sect2" lang="en"><div class="titlepage"><div><div><h3 class="title"><a name="sect.VisCntCmpl.Hits"></a>8.5.6. 
180Visualization of hits
181</h3></div></div></div><p>
182Hits are visualized with classes <span class="emphasis"><em>G4Square</em></span> or
183<span class="emphasis"><em>G4Circle</em></span>, or other user-defined classes inheriting the
184abstract base class <span class="emphasis"><em>G4VMarker</em></span>. Drawing methods for hits are
185not supported by default. Instead, ways of their implementation are
186guided by virtual methods, <code class="literal">G4VHit::Draw()</code> and
187<code class="literal">G4VHitsCollection::DrawAllHits()</code>, of the abstract base
188classes <span class="emphasis"><em>G4VHit</em></span> and <span class="emphasis"><em>G4VHitsCollection</em></span>.
189These methods are defined as empty functions in the <code class="literal">digits+hits</code>
190category. You can overload these methods, using the following
191drawing methods of class <span class="emphasis"><em>G4VVisManager</em></span>, in order to
192visualize hits:
193
194</p><div class="informalexample"><pre class="programlisting">
195  //----- Drawing methods of G4Square and G4Circle
196  virtual void G4VVisManager::Draw (const G4Circle&amp;, ...) ;
197  virtual void G4VVisManager::Draw (const G4Square&amp;, ...) ;
198</pre></div><p>
199</p><p>
200The real implementations of these <code class="literal">Draw()</code> methods are
201described in class <span class="emphasis"><em>G4VisManager</em></span>.
202</p><p>
203The overloaded implementation of <code class="literal">G4VHits::Draw()</code> will
204be held by, for example, class <span class="emphasis"><em>MyTrackerHits</em></span> inheriting
205<span class="emphasis"><em>G4VHit</em></span> as follows:
206
207</p><div class="informalexample"><pre class="programlisting">
208  //----- C++ source codes: An example of giving concrete implementation of
209  //       G4VHit::Draw(), using  class MyTrackerHit : public G4VHit {...}
210  //     
211 void MyTrackerHit::Draw()
212 {
213    G4VVisManager* pVVisManager = G4VVisManager::GetConcreteInstance();
214    if(pVVisManager)
215    {
216      // define a circle in a 3D space
217      G4Circle circle(pos);
218      circle.SetScreenSize(0.3);
219      circle.SetFillStyle(G4Circle::filled);
220
221      // make the circle red
222      G4Colour colour(1.,0.,0.);
223      G4VisAttributes attribs(colour);
224      circle.SetVisAttributes(attribs);
225
226      // make a 3D data for visualization
227      pVVisManager-&gt;Draw(circle);
228    }
229  }
230
231  //----- end of C++ source codes
232 
233</pre></div><p>
234</p><p>
235The overloaded implementation of
236<code class="literal">G4VHitsCollection::DrawAllHits()</code> will be held by, for
237example, class <span class="emphasis"><em>MyTrackerHitsCollection</em></span> inheriting class
238<span class="emphasis"><em>G4VHitsCollection</em></span> as follows:
239
240</p><div class="informalexample"><pre class="programlisting">
241  //----- C++ source codes: An example of giving concrete implementation of
242  //       G4VHitsCollection::Draw(),
243  //       using  class MyTrackerHit : public G4VHitsCollection{...}
244  //     
245  void MyTrackerHitsCollection::DrawAllHits()
246  {
247    G4int n_hit = theCollection.entries();
248    for(G4int i=0;i &lt; n_hit;i++)
249    {
250      theCollection[i].Draw();
251    }
252  }
253
254  //----- end of C++ source codes
255 
256</pre></div><p>
257</p><p>
258Thus, you can visualize hits as well as trajectories, for
259example, at the end of each event by implementing the method
260<code class="literal">MyEventAction::EndOfEventAction()</code> as follows:
261
262</p><div class="informalexample"><pre class="programlisting">
263  void MyEventAction::EndOfEventAction()
264  {
265    const G4Event* evt = fpEventManager-&gt;get_const_currentEvent();
266
267    G4SDManager * SDman = G4SDManager::get_SDMpointer();
268    G4String colNam;
269    G4int trackerCollID = SDman-&gt;get_collectionID(colNam="TrackerCollection");
270    G4int calorimeterCollID = SDman-&gt;get_collectionID(colNam="CalCollection");
271
272    G4TrajectoryContainer * trajectoryContainer = evt-&gt;get_trajectoryContainer();
273    G4int n_trajectories = 0;
274    if(trajectoryContainer)
275    { n_trajectories = trajectoryContainer-&gt;entries(); }
276
277    G4HCofThisEvent * HCE = evt-&gt;get_HCofThisEvent();
278    G4int n_hitCollection = 0;
279    if(HCE)
280    { n_hitCollection = HCE-&gt;get_capacity(); }
281 
282    G4VVisManager* pVVisManager = G4VVisManager::GetConcreteInstance();
283
284    if(pVVisManager)
285    {
286 
287      // Declare begininng of visualization
288      G4UImanager::GetUIpointer()-&gt;ApplyCommand("/vis/scene/notifyHandlers");
289
290      // Draw trajectories
291      for(G4int i=0; i &lt; n_trajectories; i++)
292      {
293          (*(evt-&gt;get_trajectoryContainer()))[i]-&gt;DrawTrajectory();
294      }
295
296      // Construct 3D data for hits
297      MyTrackerHitsCollection* THC
298        = (MyTrackerHitsCollection*)(HCE-&gt;get_HC(trackerCollID));
299      if(THC) THC-&gt;DrawAllHits();
300      MyCalorimeterHitsCollection* CHC
301        = (MyCalorimeterHitsCollection*)(HCE-&gt;get_HC(calorimeterCollID));
302      if(CHC) CHC-&gt;DrawAllHits();
303
304      // Declare end of visualization
305      G4UImanager::GetUIpointer()-&gt;ApplyCommand("/vis/viewer/update");
306   
307    }
308
309  }
310
311  //----- end of C++ codes
312 
313</pre></div><p>
314</p><p>
315You can re-visualize a physical volume, where a hit is detected,
316with a highlight color, in addition to the whole set of detector
317components. It is done by calling a drawing method of a physical
318volume directly. The method is:
319
320</p><div class="informalexample"><pre class="programlisting">
321
322  //----- Drawing methods of a physical volume
323  virtual void Draw (const G4VPhysicalVolume&amp;, ...) ;
324 
325</pre></div><p>
326</p><p>
327This method is, for example, called in a method
328<code class="literal">MyXXXHit::Draw()</code>, describing the visualization of hits
329with markers. The following is an example for this:
330
331</p><div class="informalexample"><pre class="programlisting">
332  //----- C++ source codes: An example of visualizing hits with
333  void MyCalorimeterHit::Draw()
334  {
335    G4VVisManager* pVVisManager = G4VVisManager::GetConcreteInstance();
336    if(pVVisManager)
337    {
338      G4Transform3D trans(rot,pos);
339      G4VisAttributes attribs;
340      G4LogicalVolume* logVol = pPhys-&gt;GetLogicalVolume();
341      const G4VisAttributes* pVA = logVol-&gt;GetVisAttributes();
342      if(pVA) attribs = *pVA;
343      G4Colour colour(1.,0.,0.);
344      attribs.SetColour(colour);
345      attribs.SetForceSolid(true);
346
347      //----- Re-visualization of a selected physical volume with red color
348      pVVisManager-&gt;Draw(*pPhys,attribs,trans);
349
350    }
351  }
352
353  //----- end of C++ codes
354 
355</pre></div><p>
356</p></div><div class="sect2" lang="en"><div class="titlepage"><div><div><h3 class="title"><a name="sect.VisCntCmpl.AttHits"></a>8.5.7. 
357HepRep Attributes for Hits
358</h3></div></div></div><p>
359The HepRep file formats, HepRepFile and HepRepXML, attach various
360attributes to hits such that you can view these attributes, label
361trajectories by these attributes or make visibility cuts based on
362these attributes. Examples of adding HepRep attributes to hit
363classes can be found in examples /extended/analysis/A01 and
364/extended/runAndEvent/RE01.
365</p><p>
366For example, in example RE01's class RE01CalorimeterHit.cc,
367available attributes will be:
368
369</p><div class="itemizedlist"><ul type="disc" compact><li><p>
370    Hit Type
371  </p></li><li><p>
372    Track ID
373  </p></li><li><p>
374    Z Cell ID
375  </p></li><li><p>
376    Phi Cell ID
377  </p></li><li><p>
378    Energy Deposited
379  </p></li><li><p>
380    Energy Deposited by Track
381  </p></li><li><p>
382    Position
383  </p></li><li><p>
384    Logical Volume
385  </p></li></ul></div><p>
386</p><p>
387You can add additional attributes of your choosing by modifying the
388relevant part of the hit class (look for the methods GetAttDefs and
389CreateAttValues).
390</p></div><div class="sect2" lang="en"><div class="titlepage"><div><div><h3 class="title"><a name="sect.VisCntCmpl.txt"></a>8.5.8. 
391Visualization of text
392</h3></div></div></div><p>
393In Geant4 Visualization, a text, i.e., a character string, is
394described by class <span class="emphasis"><em>G4Text</em></span> inheriting
395<span class="emphasis"><em>G4VMarker</em></span> as well as <span class="emphasis"><em>G4Square</em></span> 
396and <span class="emphasis"><em>G4Circle</em></span>. Therefore, the way to
397visualize text is the same as for hits. The corresponding drawing
398method of <span class="emphasis"><em>G4VVisManager</em></span> is:
399
400</p><div class="informalexample"><pre class="programlisting">
401  //----- Drawing methods of G4Text
402  virtual void G4VVisManager::Draw (const G4Text&amp;, ...);
403 
404</pre></div><p>
405</p><p>
406The real implementation of this method is described in class
407<span class="emphasis"><em>G4VisManager</em></span>.
408</p></div><div class="sect2" lang="en"><div class="titlepage"><div><div><h3 class="title"><a name="sect.VisCntCmpl.PlyTrkStp"></a>8.5.9. 
409Visualization of polylines and tracking steps
410</h3></div></div></div><p>
411Polylines, i.e., sets of successive line segments, are described by
412class <span class="emphasis"><em>G4Polyline</em></span>. For <span class="emphasis"><em>G4Polyline</em></span>,
413the following drawing method of class <span class="emphasis"><em>G4VVisManager</em></span> 
414is prepared:
415
416</p><div class="informalexample"><pre class="programlisting">
417   //----- A drawing method of G4Polyline
418    virtual void G4VVisManager::Draw (const G4Polyline&amp;, ...) ;
419 
420</pre></div><p>
421</p><p>
422The real implementation of this method is described in class
423<span class="emphasis"><em>G4VisManager</em></span>.
424</p><p>
425Using this method, C++ source codes to visualize
426<span class="emphasis"><em>G4Polyline</em></span> are described as follows:
427
428</p><div class="informalexample"><pre class="programlisting">
429 //----- C++ source code: How to visualize a polyline
430  G4VVisManager* pVVisManager = G4VVisManager::GetConcreteInstance();
431 
432  if (pVVisManager) {
433      G4Polyline polyline ;
434   
435      ..... (C++ source codes to set vertex positions, color, etc)
436 
437      pVVisManager -&gt; Draw(polyline);
438   
439  }
440 
441  //----- end of C++ source codes
442 
443</pre></div><p>
444</p><p>
445Tracking steps are able to be visualized based on the above
446visualization of <span class="emphasis"><em>G4Polyline</em></span>. You can visualize tracking
447steps at each step automatically by writing a proper implementation
448of class <span class="emphasis"><em>MySteppingAction</em></span> inheriting
449<span class="emphasis"><em>G4UserSteppingAction</em></span>, and also with the help of the Run
450Manager.
451</p><p>
452First, you must implement a method,
453<code class="literal">MySteppingAction::UserSteppingAction()</code>. A typical
454implementation of this method is as follows:
455
456</p><div class="informalexample"><pre class="programlisting">
457  //----- C++ source code: An example of visualizing tracking steps
458  void MySteppingAction::UserSteppingAction()
459  {
460      G4VVisManager* pVVisManager = G4VVisManager::GetConcreteInstance();
461
462      if (pVVisManager) {
463
464        //----- Get the Stepping Manager
465        const G4SteppingManager* pSM = GetSteppingManager();
466
467        //----- Define a line segment
468        G4Polyline polyline;
469        G4double charge = pSM-&gt;GetTrack()-&gt;GetDefinition()-&gt;GetPDGCharge();
470        G4Colour colour;
471        if      (charge &lt; 0.) colour = G4Colour(1., 0., 0.);
472        else if (charge &lt; 0.) colour = G4Colour(0., 0., 1.);
473        else                  colour = G4Colour(0., 1., 0.);
474        G4VisAttributes attribs(colour);
475        polyline.SetVisAttributes(attribs);
476        polyline.push_back(pSM-&gt;GetStep()-&gt;GetPreStepPoint()-&gt;GetPosition());
477        polyline.push_back(pSM-&gt;GetStep()-&gt;GetPostStepPoint()-&gt;GetPosition());
478
479        //----- Call a drawing method for G4Polyline
480        pVVisManager -&gt; Draw(polyline);
481     
482      }
483  }
484
485  //----- end of C++ source code
486 
487</pre></div><p>
488</p><p>
489Next, in order that the above C++ source code works, you have to
490pass the information of the <span class="emphasis"><em>MySteppingAction</em></span> to the Run
491Manager in the <code class="literal">main()</code> function:
492
493</p><div class="informalexample"><pre class="programlisting">
494 
495  //----- C++ source code: Passing what to do at each step to the Run Manager
496
497  int main()
498  {
499     ...
500 
501     // Run Manager
502     G4RunManager * runManager = new G4RunManager;
503
504     // User initialization classes
505     ...
506     runManager-&gt;SetUserAction(new MySteppingAction);
507     ...
508  }
509
510  //----- end of C++ source code
511 
512</pre></div><p>
513</p><p>
514Thus you can visualize tracking steps with various visualization
515attributes, e.g., color, at each step, automatically.
516</p><p>
517As well as tracking steps, you can visualize any kind 3D object
518made of line segments, using class <span class="emphasis"><em>G4Polyline</em></span> and its
519drawing method, defined in class <span class="emphasis"><em>G4VVisManager</em></span>. See, for
520example, the implementation of the <code class="literal">/vis/scene/add/axes</code>
521command.
522</p></div><div class="sect2" lang="en"><div class="titlepage"><div><div><h3 class="title"><a name="sect.VisCntCmpl.UsrAct"></a>8.5.10. 
523Visualization User Action
524</h3></div></div></div><p>
525You can implement the <code class="literal">Draw</code> method of
526<code class="literal">G4VUserVisAction</code>, e.g., the class definition could be:
527
528</p><div class="informalexample"><pre class="programlisting">
529class StandaloneVisAction: public G4VUserVisAction {
530  void Draw();
531};
532</pre></div><p>
533
534and the implementation:
535
536</p><div class="informalexample"><pre class="programlisting">
537void StandaloneVisAction::Draw() {
538  G4VVisManager* pVisManager = G4VVisManager::GetConcreteInstance();
539  if (pVisManager) {
540
541    // Simple box...
542    pVisManager-&gt;Draw(G4Box("box",2*m,2*m,2*m),
543                      G4VisAttributes(G4Colour(1,1,0)));
544
545    // Boolean solid...
546    G4Box boxA("boxA",3*m,3*m,3*m);
547    G4Box boxB("boxB",1*m,1*m,1*m);
548    G4SubtractionSolid subtracted("subtracted_boxes",&amp;boxA,&amp;boxB,
549                       G4Translate3D(3*m,3*m,3*m));
550    pVisManager-&gt;Draw(subtracted,
551                      G4VisAttributes(G4Colour(0,1,1)),
552                      G4Translate3D(6*m,6*m,6*m));
553  }
554}
555</pre></div><p>
556</p><p>
557Explicit use of polyhedron objects is equivalent, e.g.:
558
559</p><div class="informalexample"><pre class="programlisting">
560
561    // Same, but explicit polyhedron...
562    G4Polyhedron* pA = G4Box("boxA",3*m,3*m,3*m).CreatePolyhedron();
563    G4Polyhedron* pB = G4Box("boxB",1*m,1*m,1*m).CreatePolyhedron();
564    pB-&gt;Transform(G4Translate3D(3*m,3*m,3*m));
565    G4Polyhedron* pSubtracted = new G4Polyhedron(pA-&gt;subtract(*pB));
566    G4VisAttributes subVisAtts(G4Colour(0,1,1));
567    pSubtracted-&gt;SetVisAttributes(&amp;subVisAtts);
568    pVisManager-&gt;Draw(*pSubtracted,G4Translate3D(6*m,6*m,6*m));
569    delete pA;
570    delete pB;
571    delete pSubtracted;
572</pre></div><p>
573</p><p>
574If efficiency is an issue, create the objects in the constructor,
575delete them in the destructor and draw them in your <code class="literal">Draw</code>
576method. Anyway, an instance of your class needs to be registered
577with the vis manager, e.g.:
578
579</p><div class="informalexample"><pre class="programlisting">
580  ...
581  G4VisManager* visManager = new G4VisExecutive;
582  visManager-&gt;Initialize ();
583
584  visManager-&gt;SetUserAction
585    (new StandaloneVisAction,
586     G4VisExtent(-5*m,5*m,-5*m,5*m,-5*m,5*m));  // 2nd argument optional.
587  ...
588</pre></div><p>
589
590then activate by adding to a scene, e.g:
591
592</p><div class="informalexample"><pre class="programlisting">
593/control/verbose 2
594/vis/verbose c
595/vis/open OGLSXm
596/vis/scene/create
597#/vis/scene/add/userAction
598/vis/scene/add/userAction -10 10 -10 10 -10 10 m
599#/vis/scene/add/axes 0 0 0 10 m
600#/vis/scene/add/scale 10 m
601/vis/sceneHandler/attach
602/vis/viewer/refresh
603</pre></div><p>
604</p><p>
605The extent can be added on registration or on the command line or
606neither (if the extent of the scene is set by other components).
607Your <code class="literal">Draw</code> method will be called whenever needed to refresh
608the screen or rebuild a graphics database, for any chosen viewer.
609The scene can be attached to any scene handler and your drawing
610will be shown.
611</p></div><div class="sect2" lang="en"><div class="titlepage"><div><div><h3 class="title"><a name="sect.VisCntCmpl.StdAln"></a>8.5.11. 
612Standalone Visualization
613</h3></div></div></div><p>
614The above raises the possibility of using Geant4 as a "standalone"
615graphics package without invoking the run manager. The following
616main program, together with a user visualization action and a macro
617file, will allow you to view your drawing interactively on any of
618the supported graphics systems.
619
620</p><div class="informalexample"><pre class="programlisting">
621#include "globals.hh"
622#include "G4VisExecutive.hh"
623#include "G4VisExtent.hh"
624#include "G4UImanager.hh"
625#include "G4UIterminal.hh"
626#include "G4UItcsh.hh"
627
628#include "StandaloneVisAction.hh"
629
630int main() {
631
632  G4VisManager* visManager = new G4VisExecutive;
633  visManager-&gt;Initialize ();
634
635  visManager-&gt;SetUserAction
636    (new StandaloneVisAction,
637     G4VisExtent(-5*m,5*m,-5*m,5*m,-5*m,5*m));  // 2nd argument optional.
638
639  G4UImanager* UI = G4UImanager::GetUIpointer ();
640  UI-&gt;ApplyCommand ("/control/execute standalone.g4m");
641
642  G4UIsession* session = new G4UIterminal(new G4UItcsh);
643  session-&gt;SessionStart();
644
645  delete session;
646  delete visManager;
647}
648</pre></div><p>
649</p></div></div><div class="navfooter"><hr><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="ch08s04.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="ch08s06.html"><img src="AllResources/IconsGIF/next.gif" alt="Next"></a></td></tr><tr><td width="40%" align="left" valign="top">8.4. 
650Controlling Visualization from Commands
651 </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.6. 
652Visualization Attributes
653</td></tr></table></div></body></html>
Note: See TracBrowser for help on using the repository browser.