ECCE @ EIC Software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
G4XXXStoredSceneHandler.cc
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file G4XXXStoredSceneHandler.cc
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 //
29 // John Allison 7th March 2006
30 // A template for a graphics driver with a store/database.
31 //?? Lines beginning like this require specialisation for your driver.
32 
34 
35 #include "G4XXXStoredViewer.hh"
36 #include "G4PhysicalVolumeModel.hh"
37 #include "G4LogicalVolumeModel.hh"
38 #include "G4VPhysicalVolume.hh"
39 #include "G4LogicalVolume.hh"
40 #include "G4Box.hh"
41 #include "G4Polyline.hh"
42 #include "G4Text.hh"
43 #include "G4Circle.hh"
44 #include "G4Square.hh"
45 #include "G4Polyhedron.hh"
46 #include "G4UnitsTable.hh"
47 #include "G4AttDef.hh"
48 #include "G4AttValue.hh"
49 #include "G4AttCheck.hh"
50 
51 #include <sstream>
52 
54 // Counter for XXX scene handlers.
55 
57  const G4String& name):
58  G4VSceneHandler(system, fSceneIdCount++, name)
59 {}
60 
62 
63 #ifdef G4XXXStoredDEBUG
64 // Useful function...
65 void G4XXXStoredSceneHandler::PrintThings() {
66  G4cout <<
67  " with transformation "
68  << (void*)fpObjectTransformation;
69  if (fpModel) {
70  G4cout << " from " << fpModel->GetCurrentDescription()
71  << " (tag " << fpModel->GetCurrentTag()
72  << ')';
73  } else {
74  G4cout << "(not from a model)";
75  }
76  G4PhysicalVolumeModel* pPVModel =
77  dynamic_cast<G4PhysicalVolumeModel*>(fpModel);
78  if (pPVModel) {
79  G4cout <<
80  "\n current physical volume: "
81  << pPVModel->GetCurrentPV()->GetName() <<
82  "\n current logical volume: "
83 // There might be a problem with the LV pointer if this is a G4LogicalVolumeModel
84  << pPVModel->GetCurrentLV()->GetName() <<
85  "\n current depth of geometry tree: "
86  << pPVModel->GetCurrentDepth();
87  }
88  G4cout << G4endl;
89 }
90 #endif
91 
93 (const G4Transform3D& objectTransformation,
94  const G4VisAttributes& visAttribs)
95 {
96  G4VSceneHandler::PreAddSolid(objectTransformation, visAttribs);
97 
98  // Get user G4Atts...
99  const std::map<G4String,G4AttDef>* userAttDefs = visAttribs.GetAttDefs();
100  if (userAttDefs) {
101 #ifdef G4XXXStoredDEBUG
102  const std::vector<G4AttValue>* userAttValues =
103  visAttribs.CreateAttValues();
104  G4cout << "\nProvided G4Atts:\n"
105  << G4AttCheck(userAttValues, userAttDefs);
106  // Extra checks...
107  G4AttCheck attCheck(userAttValues, userAttDefs);
108  if (attCheck.Check()) G4cout << "Error" << G4endl;
109  else {
110  std::vector<G4AttValue> standardValues;
111  std::map<G4String,G4AttDef> standardDefinitions;
112  attCheck.Standard(&standardValues, &standardDefinitions);
113  G4cout << "\nStandard G4Atts:\n"
114  << G4AttCheck(&standardValues, &standardDefinitions);
115  }
116  // End of extra checks.
117  delete userAttValues; // (Must be deleted after use.)
118 #endif
119  }
120 
121  // Get solid's G4Atts created by G4PhysicalVolumeModel...
122  G4PhysicalVolumeModel* pPVModel =
123  dynamic_cast<G4PhysicalVolumeModel*>(fpModel);
124  if (pPVModel) {
125  const std::map<G4String,G4AttDef>* solidAttDefs = pPVModel->GetAttDefs();
126  if (solidAttDefs) {
127 #ifdef G4XXXStoredDEBUG
128  std::vector<G4AttValue>* solidAttValues =
129  pPVModel->CreateCurrentAttValues();
130  G4cout << "\nProvided G4Atts:\n"
131  << G4AttCheck(solidAttValues, solidAttDefs);
132  // Extra checks...
133  G4AttCheck attCheck(solidAttValues,solidAttDefs);
134  if (attCheck.Check()) G4cout << "Error" << G4endl;
135  else {
136  std::vector<G4AttValue> standardValues;
137  std::map<G4String,G4AttDef> standardDefinitions;
138  attCheck.Standard(&standardValues, &standardDefinitions);
139  G4cout << "\nStandard G4Atts:\n"
140  << G4AttCheck(&standardValues, &standardDefinitions);
141  }
142  // End of extra checks.
143  delete solidAttValues; // (Must be deleted after use.)
144 #endif
145  }
146  }
147 
148  // Create a place for current solid...
149  fCurrentItem = fStore.insert(fStore.end(), G4String("\nPreAddSolid:\n"));
150  if (fReadyForTransients) {
151  fTransients.push_back(fCurrentItem);
152  } else {
153  fPermanents.push_back(fCurrentItem);
154  }
155 }
156 
158 {
159  *fCurrentItem += "\nEndSolid\n";
161 }
162 
164 (const G4Transform3D& objectTransformation)
165 {
166  G4VSceneHandler::BeginPrimitives(objectTransformation);
167 
168  // If thread of control has already passed through PreAddSolid,
169  // avoid opening a graphical data base component again.
170  if (!fProcessingSolid) {
171  // Create a place for current primitive...
172  fCurrentItem = fStore.insert(fStore.end(),
173  G4String("\nBeginPrimitives:\n"));
174  if (fReadyForTransients) {
175  fTransients.push_back(fCurrentItem);
176  } else {
177  fPermanents.push_back(fCurrentItem);
178  }
179  }
180 }
181 
183 {
184  if (!fProcessingSolid) { // Already done if so.
185  *fCurrentItem += "\nEndPrimitives\n";
186  }
188 }
189 
190 // Note: This function overrides G4VSceneHandler::AddSolid(const
191 // G4Box&). You may not want to do this, but this is how it's done if
192 // you do. Certain other specific solids may be treated this way -
193 // see G4VSceneHandler.hh. The simplest possible driver would *not*
194 // implement these polymorphic functions, with the effect that the
195 // default versions in G4VSceneHandler are used, which simply call
196 // G4VSceneHandler::RequestPrimitives to turn the solid into a
197 // G4Polyhedron usually.
198 // Don't forget, solids can be transients too (e.g., representing a hit).
200 #ifdef G4XXXStoredDEBUG
201  G4cout <<
202  "G4XXXStoredSceneHandler::AddSolid(const G4Box& box) called for "
203  << box.GetName()
204  << G4endl;
205 #endif
206  //?? Process your box...
207  std::ostringstream oss;
208  oss << "G4Box(" <<
209  G4String
210  (G4BestUnit
212  (box.GetXHalfLength(), box.GetYHalfLength(), box.GetZHalfLength()),
213  "Length")).strip() << ')';
214  *fCurrentItem += oss.str();
215 }
216 
218 #ifdef G4XXXStoredDEBUG
219  G4cout <<
220  "G4XXXStoredSceneHandler::AddPrimitive(const G4Polyline& polyline) called.\n"
221  << polyline
222  << G4endl;
223 #endif
224  // Get vis attributes - pick up defaults if none.
225  //const G4VisAttributes* pVA =
226  // fpViewer -> GetApplicableVisAttributes (polyline.GetVisAttributes ());
227  //?? Process polyline.
228  std::ostringstream oss;
229  oss << polyline;
230  *fCurrentItem += oss.str();
231 }
232 
234 #ifdef G4XXXStoredDEBUG
235  G4cout <<
236  "G4XXXStoredSceneHandler::AddPrimitive(const G4Text& text) called.|n"
237  << text
238  << G4endl;
239 #endif
240  // Get text colour - special method since default text colour is
241  // determined by the default text vis attributes, which may be
242  // specified independent of default vis attributes of other types of
243  // visible objects.
244  //const G4Colour& c = GetTextColour (text); // Picks up default if none.
245  //?? Process text.
246  std::ostringstream oss;
247  oss << text;
248  *fCurrentItem += oss.str();
249 }
250 
252 #ifdef G4XXXStoredDEBUG
253  G4cout <<
254  "G4XXXStoredSceneHandler::AddPrimitive(const G4Circle& circle) called.\n"
255  << circle
256  << G4endl;
257  MarkerSizeType sizeType;
258  G4double size = GetMarkerSize (circle, sizeType);
259  switch (sizeType) {
260  default:
261  case screen:
262  // Draw in screen coordinates.
263  G4cout << "screen";
264  break;
265  case world:
266  // Draw in world coordinates.
267  G4cout << "world";
268  break;
269  }
270  G4cout << " size: " << size << G4endl;
271 #endif
272  // Get vis attributes - pick up defaults if none.
273  //const G4VisAttributes* pVA =
274  // fpViewer -> GetApplicableVisAttributes (circle.GetVisAttributes ());
275  //?? Process circle.
276  std::ostringstream oss;
277  oss << circle;
278  *fCurrentItem += oss.str();
279 }
280 
282 #ifdef G4XXXStoredDEBUG
283  G4cout <<
284  "G4XXXStoredSceneHandler::AddPrimitive(const G4Square& square) called.\n"
285  << square
286  << G4endl;
287  MarkerSizeType sizeType;
288  G4double size = GetMarkerSize (square, sizeType);
289  switch (sizeType) {
290  default:
291  case screen:
292  // Draw in screen coordinates.
293  G4cout << "screen";
294  break;
295  case world:
296  // Draw in world coordinates.
297  G4cout << "world";
298  break;
299  }
300  G4cout << " size: " << size << G4endl;
301 #endif
302  // Get vis attributes - pick up defaults if none.
303  //const G4VisAttributes* pVA =
304  // fpViewer -> GetApplicableVisAttributes (square.GetVisAttributes ());
305  //?? Process square.
306  std::ostringstream oss;
307  oss << square;
308  *fCurrentItem += oss.str();
309 }
310 
312 #ifdef G4XXXStoredDEBUG
313  G4cout <<
314  "G4XXXStoredSceneHandler::AddPrimitive(const G4Polyhedron&) called.\n"
315  << polyhedron
316  << G4endl;
317 #endif
318  //?? Process polyhedron.
319  std::ostringstream oss;
320  oss << polyhedron;
321  *fCurrentItem += oss.str();
322 
323  //?? Or... here are some ideas for decomposing into polygons...
324  //Assume all facets are convex quadrilaterals.
325  //Draw each G4Facet individually
326 
327  //Get colour, etc..
328  if (polyhedron.GetNoFacets() == 0) return;
329 
330  // Get vis attributes - pick up defaults if none.
331  const G4VisAttributes* pVA =
332  fpViewer -> GetApplicableVisAttributes (polyhedron.GetVisAttributes ());
333 
334  // Get view parameters that the user can force through the vis
335  // attributes, thereby over-riding the current view parameter.
336  G4ViewParameters::DrawingStyle drawing_style = GetDrawingStyle (pVA);
337  //G4bool isAuxEdgeVisible = GetAuxEdgeVisible (pVA);
338 
339  //Get colour, etc..
340  //const G4Colour& c = pVA -> GetColour ();
341 
342  // Initial action depending on drawing style.
343  switch (drawing_style) {
344  case (G4ViewParameters::hsr):
345  {
346  break;
347  }
348  case (G4ViewParameters::hlr):
349  {
350  break;
351  }
353  {
354  break;
355  }
356  default:
357  {
358  break;
359  }
360  }
361 
362  // Loop through all the facets...
363 
364  // Look at G4OpenGLSceneHandler::AddPrimitive(const G4Polyhedron&)
365  // for an example of how to get facets out of a G4Polyhedron,
366  // including how to cope with triangles if that's a problem.
367 }
368 
370 {
371  fStore.clear();
372  fPermanents.clear();
373  fTransients.clear();
374 }
375 
377 {
378  typedef std::vector<StoreIterator>::iterator StoreIteratorIterator;
379  for (StoreIteratorIterator i = fTransients.begin();
380  i != fTransients.end(); ++i) {
381  fStore.erase(*i);
382  }
383  fTransients.clear();
384 
385  // Make sure screen corresponds to graphical database...
386  if (fpViewer) {
387  fpViewer -> SetView ();
388  fpViewer -> ClearView ();
389  fpViewer -> DrawView ();
390  }
391 }