source: trunk/source/visualization/XXX/src/G4XXXSGViewer.cc @ 850

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

geant4.8.2 beta

File size: 6.5 KB
Line 
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// $Id: G4XXXSGViewer.cc,v 1.4 2006/08/29 16:11:25 allison Exp $
28// GEANT4 tag $Name: HEAD $
29//
30//
31// John Allison  10th March 2006
32// A template for a sophisticated graphics driver with a scene graph.
33//?? Lines beginning like this require specialisation for your driver.
34
35#ifdef G4VIS_BUILD_XXXSG_DRIVER
36
37#include "G4XXXSGViewer.hh"
38
39#include "G4VSceneHandler.hh"
40#include "G4XXXSGSceneHandler.hh"
41
42#include <fstream>
43
44G4XXXSGViewer::G4XXXSGViewer
45(G4VSceneHandler& sceneHandler, const G4String& name):
46  G4VViewer(sceneHandler, sceneHandler.IncrementViewCount(), name)
47{}
48
49G4XXXSGViewer::~G4XXXSGViewer() {}
50
51void G4XXXSGViewer::SetView() {
52  //#ifdef G4XXXSGDEBUG
53  G4cout << "G4XXXSGViewer::SetView() called." << G4endl;
54  //#endif
55}
56
57void G4XXXSGViewer::ClearView() {
58  //#ifdef G4XXXSGDEBUG
59  G4cout << "G4XXXSGViewer::ClearView() called." << G4endl;
60  //#endif
61}
62
63void G4XXXSGViewer::DrawView() {
64  //#ifdef G4XXXSGDEBUG
65  G4cout << "G4XXXSGViewer::DrawView() called." << G4endl;
66  //#endif
67
68  // First, a view should decide when to re-visit the G4 kernel.
69  // Sometimes it might not be necessary, e.g., if the scene is stored
70  // in a graphical database (e.g., OpenGL's display lists) and only
71  // the viewing angle has changed.  But graphics systems without a
72  // graphical database will always need to visit the G4 kernel.
73
74  // The fNeedKernelVisit flag might have been set by the user in
75  // /vis/viewer/rebuild, but if not, make decision and set flag only
76  // if necessary...
77  if (!fNeedKernelVisit) KernelVisitDecision();
78  G4bool kernelVisitWasNeeded = fNeedKernelVisit; // Keep (ProcessView resets).
79
80  ProcessView ();  // Clears store and processes scene only if necessary.
81
82  if (kernelVisitWasNeeded) {
83    // Some systems, notably OpenGL, can draw while re-building, so
84    // there might not be a need to draw from store again here.  But
85    // in this case...
86    DrawFromStore("G4XXXSGViewer::DrawView");
87  } else {
88    DrawFromStore("G4XXXSGViewer::DrawView");
89  }
90
91  // ...before finally...
92  FinishView ();       // Flush streams and/or swap buffers.
93}
94
95void G4XXXSGViewer::ShowView() {
96  //#ifdef G4XXXSGDEBUG
97  G4cout << "G4XXXSGViewer::ShowView() called." << G4endl;
98  //#endif
99  // This is what you should see...
100  DrawFromStore("G4XXXSGViewer::ShowView");
101}
102
103void G4XXXSGViewer::KernelVisitDecision () {
104 
105  // If there's a significant difference with the last view parameters
106  // of either the scene handler or this viewer, trigger a rebuild.
107
108  typedef taj::tree<G4String> SceneGraph;
109  typedef taj::tree<G4String>::iterator SceneGraphIterator;
110  SceneGraph& sceneGraph =
111    static_cast<G4XXXSGSceneHandler&>(fSceneHandler).fSceneGraph;
112  if (sceneGraph.size() == 3  // I.e., only the root nodes.
113      || CompareForKernelVisit(fLastVP)) {
114    NeedKernelVisit ();  // Sets fNeedKernelVisit.
115  }     
116  fLastVP = fVP;
117}
118
119G4bool G4XXXSGViewer::CompareForKernelVisit(G4ViewParameters& lastVP)
120{
121  // Typical comparison.  Taken from OpenGL.
122  if (
123      (lastVP.GetDrawingStyle ()    != fVP.GetDrawingStyle ())    ||
124      (lastVP.IsAuxEdgeVisible ()   != fVP.IsAuxEdgeVisible ())   ||
125      (lastVP.GetRepStyle ()        != fVP.GetRepStyle ())        ||
126      (lastVP.IsCulling ()          != fVP.IsCulling ())          ||
127      (lastVP.IsCullingInvisible () != fVP.IsCullingInvisible ()) ||
128      (lastVP.IsDensityCulling ()   != fVP.IsDensityCulling ())   ||
129      (lastVP.IsCullingCovered ()   != fVP.IsCullingCovered ())   ||
130      // No need to visit kernel if section plane changes.
131      // No need to visit kernel if cutaway planes change.
132      (lastVP.IsExplode ()          != fVP.IsExplode ())          ||
133      (lastVP.GetNoOfSides ()       != fVP.GetNoOfSides ())       ||
134      (lastVP.IsMarkerNotHidden ()  != fVP.IsMarkerNotHidden ())  ||
135      (lastVP.GetBackgroundColour ()!= fVP.GetBackgroundColour ())
136      ) {
137    return true;
138  }
139
140  if (lastVP.IsDensityCulling () &&
141      (lastVP.GetVisibleDensity () != fVP.GetVisibleDensity ()))
142    return true;
143
144  if (lastVP.IsExplode () &&
145      (lastVP.GetExplodeFactor () != fVP.GetExplodeFactor ()))
146    return true;
147
148  return false;
149}
150
151void G4XXXSGViewer::DrawFromStore(const G4String& source) {
152  typedef taj::tree<G4String> SceneGraph;
153  typedef taj::tree<G4String>::preorder_iterator SceneGraphPreIterator;
154  SceneGraph& sceneGraph =
155    static_cast<G4XXXSGSceneHandler&>(fSceneHandler).fSceneGraph;
156  // Write to a file for testing...
157  static G4int iCount = 0;
158  std::ostringstream oss;
159  oss << source << '.' << fName << '.' << iCount++ << ".out";
160  G4cout << "Writing " << oss.str() << G4endl;
161  std::ofstream ofs(oss.str().c_str());
162  for (SceneGraphPreIterator i = sceneGraph.beginPre();
163       i != sceneGraph.endPre(); ++i) {
164    G4int depth = 0;
165    SceneGraphPreIterator j = i;
166    while (j != sceneGraph.getRoot()) {j = j.parent(); ++depth;}
167    ofs << "\nDepth: " << depth;
168    ofs << *i;
169  }
170  ofs.close();
171}
172
173#endif
Note: See TracBrowser for help on using the repository browser.