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: G4VSceneHandler.cc,v 1.83 2008/01/04 22:03:46 allison Exp $ |
---|
28 | // GEANT4 tag $Name: $ |
---|
29 | // |
---|
30 | // |
---|
31 | // John Allison 19th July 1996 |
---|
32 | // Abstract interface class for graphics scenes. |
---|
33 | |
---|
34 | #include "G4VSceneHandler.hh" |
---|
35 | |
---|
36 | #include "G4ios.hh" |
---|
37 | #include <sstream> |
---|
38 | |
---|
39 | #include "G4VisManager.hh" |
---|
40 | #include "G4VGraphicsSystem.hh" |
---|
41 | #include "G4VViewer.hh" |
---|
42 | #include "G4VSolid.hh" |
---|
43 | #include "G4RotationMatrix.hh" |
---|
44 | #include "G4ThreeVector.hh" |
---|
45 | #include "G4VPhysicalVolume.hh" |
---|
46 | #include "G4Material.hh" |
---|
47 | #include "G4Polyline.hh" |
---|
48 | #include "G4Scale.hh" |
---|
49 | #include "G4Text.hh" |
---|
50 | #include "G4Circle.hh" |
---|
51 | #include "G4Square.hh" |
---|
52 | #include "G4Polymarker.hh" |
---|
53 | #include "G4Polyhedron.hh" |
---|
54 | #include "G4NURBS.hh" |
---|
55 | #include "G4Visible.hh" |
---|
56 | #include "G4VisAttributes.hh" |
---|
57 | #include "G4VModel.hh" |
---|
58 | #include "G4TrajectoriesModel.hh" |
---|
59 | #include "G4Box.hh" |
---|
60 | #include "G4Cons.hh" |
---|
61 | #include "G4Tubs.hh" |
---|
62 | #include "G4Trd.hh" |
---|
63 | #include "G4Trap.hh" |
---|
64 | #include "G4Sphere.hh" |
---|
65 | #include "G4Para.hh" |
---|
66 | #include "G4Torus.hh" |
---|
67 | #include "G4Polycone.hh" |
---|
68 | #include "G4Polyhedra.hh" |
---|
69 | #include "G4LogicalVolume.hh" |
---|
70 | #include "G4PhysicalVolumeModel.hh" |
---|
71 | #include "G4ModelingParameters.hh" |
---|
72 | #include "G4VTrajectory.hh" |
---|
73 | #include "G4VTrajectoryPoint.hh" |
---|
74 | #include "G4HitsModel.hh" |
---|
75 | #include "G4VHit.hh" |
---|
76 | #include "Randomize.hh" |
---|
77 | #include "G4StateManager.hh" |
---|
78 | #include "G4RunManager.hh" |
---|
79 | #include "G4Run.hh" |
---|
80 | #include "G4Transform3D.hh" |
---|
81 | #include "G4AttHolder.hh" |
---|
82 | #include "G4AttDef.hh" |
---|
83 | |
---|
84 | G4VSceneHandler::G4VSceneHandler (G4VGraphicsSystem& system, G4int id, const G4String& name): |
---|
85 | fSystem (system), |
---|
86 | fSceneHandlerId (id), |
---|
87 | fViewCount (0), |
---|
88 | fpViewer (0), |
---|
89 | fpScene (0), |
---|
90 | fMarkForClearingTransientStore (true), // Ready for first |
---|
91 | // ClearTransientStoreIfMarked(), |
---|
92 | // e.g., at end of run (see |
---|
93 | // G4VisManager.cc). |
---|
94 | fReadyForTransients (true), // Only false while processing scene. |
---|
95 | fProcessingSolid (false), |
---|
96 | fSecondPassRequested (false), |
---|
97 | fSecondPass (false), |
---|
98 | fpModel (0), |
---|
99 | fpObjectTransformation (0), |
---|
100 | fNestingDepth (0), |
---|
101 | fpVisAttribs (0) |
---|
102 | { |
---|
103 | G4VisManager* pVMan = G4VisManager::GetInstance (); |
---|
104 | fpScene = pVMan -> GetCurrentScene (); |
---|
105 | if (name == "") { |
---|
106 | std::ostringstream ost; |
---|
107 | ost << fSystem.GetName () << '-' << fSceneHandlerId; |
---|
108 | fName = ost.str(); |
---|
109 | } |
---|
110 | else { |
---|
111 | fName = name; |
---|
112 | } |
---|
113 | fTransientsDrawnThisEvent = pVMan->GetTransientsDrawnThisEvent(); |
---|
114 | fTransientsDrawnThisRun = pVMan->GetTransientsDrawnThisRun(); |
---|
115 | } |
---|
116 | |
---|
117 | G4VSceneHandler::~G4VSceneHandler () { |
---|
118 | G4ViewerListIterator i; |
---|
119 | for (i = fViewerList.begin(); i != fViewerList.end(); ++i) { |
---|
120 | delete *i; |
---|
121 | } |
---|
122 | } |
---|
123 | |
---|
124 | void G4VSceneHandler::PreAddSolid (const G4Transform3D& objectTransformation, |
---|
125 | const G4VisAttributes& visAttribs) { |
---|
126 | fpObjectTransformation = &objectTransformation; |
---|
127 | fpVisAttribs = &visAttribs; |
---|
128 | fProcessingSolid = true; |
---|
129 | } |
---|
130 | |
---|
131 | void G4VSceneHandler::PostAddSolid () { |
---|
132 | fpObjectTransformation = 0; |
---|
133 | fpVisAttribs = 0; |
---|
134 | fProcessingSolid = false; |
---|
135 | if (fReadyForTransients) { |
---|
136 | fTransientsDrawnThisEvent = true; |
---|
137 | fTransientsDrawnThisRun = true; |
---|
138 | } |
---|
139 | } |
---|
140 | |
---|
141 | void G4VSceneHandler::BeginPrimitives |
---|
142 | (const G4Transform3D& objectTransformation) { |
---|
143 | fNestingDepth++; |
---|
144 | if (fNestingDepth > 1) |
---|
145 | G4Exception("G4VSceneHandler::BeginPrimitives: Nesting detected." |
---|
146 | "\n It is illegal to nest Begin/EndPrimitives."); |
---|
147 | fpObjectTransformation = &objectTransformation; |
---|
148 | } |
---|
149 | |
---|
150 | void G4VSceneHandler::EndPrimitives () { |
---|
151 | if (fNestingDepth <= 0) |
---|
152 | G4Exception("G4VSceneHandler::EndPrimitives: Nesting error"); |
---|
153 | fNestingDepth--; |
---|
154 | fpObjectTransformation = 0; |
---|
155 | if (fReadyForTransients) { |
---|
156 | fTransientsDrawnThisEvent = true; |
---|
157 | fTransientsDrawnThisRun = true; |
---|
158 | } |
---|
159 | } |
---|
160 | |
---|
161 | void G4VSceneHandler::BeginPrimitives2D |
---|
162 | (const G4Transform3D& objectTransformation) { |
---|
163 | fNestingDepth++; |
---|
164 | if (fNestingDepth > 1) |
---|
165 | G4Exception("G4VSceneHandler::BeginPrimitives2D: Nesting detected." |
---|
166 | "\n It is illegal to nest Begin/EndPrimitives."); |
---|
167 | fpObjectTransformation = &objectTransformation; |
---|
168 | } |
---|
169 | |
---|
170 | void G4VSceneHandler::EndPrimitives2D () { |
---|
171 | if (fNestingDepth <= 0) |
---|
172 | G4Exception("G4VSceneHandler::EndPrimitives2D: Nesting error"); |
---|
173 | fNestingDepth--; |
---|
174 | fpObjectTransformation = 0; |
---|
175 | if (fReadyForTransients) { |
---|
176 | fTransientsDrawnThisEvent = true; |
---|
177 | fTransientsDrawnThisRun = true; |
---|
178 | } |
---|
179 | } |
---|
180 | |
---|
181 | void G4VSceneHandler::BeginModeling () { |
---|
182 | } |
---|
183 | |
---|
184 | void G4VSceneHandler::EndModeling () |
---|
185 | { |
---|
186 | fpModel = 0; |
---|
187 | } |
---|
188 | |
---|
189 | void G4VSceneHandler::ClearStore () { |
---|
190 | // if (fpViewer) fpViewer -> NeedKernelVisit (true); |
---|
191 | // ?? Viewer is supposed to be smart enough to know when to visit |
---|
192 | // kernel, but a problem in OpenGL Stored seems to require a forced |
---|
193 | // kernel visit triggered by the above code. John Allison Aug 2001 |
---|
194 | // Feb 2005 - commented out. Let's fix OpenGL if necessary. |
---|
195 | } |
---|
196 | |
---|
197 | void G4VSceneHandler::ClearTransientStore () { |
---|
198 | } |
---|
199 | |
---|
200 | void G4VSceneHandler::AddSolid (const G4Box& box) { |
---|
201 | RequestPrimitives (box); |
---|
202 | // If your graphics system is sophisticated enough to handle a |
---|
203 | // particular solid shape as a primitive, in your derived class write a |
---|
204 | // function to override this. (Note: some compilers warn that your |
---|
205 | // function "hides" this one. That's OK.) |
---|
206 | // Your function might look like this... |
---|
207 | // void G4MyScene::AddSolid (const G4Box& box) { |
---|
208 | // Get parameters of appropriate object, e.g.: |
---|
209 | // G4double dx = box.GetXHalfLength (); |
---|
210 | // G4double dy = box.GetYHalfLength (); |
---|
211 | // G4double dz = box.GetZHalfLength (); |
---|
212 | // and Draw or Store in your display List. |
---|
213 | } |
---|
214 | |
---|
215 | void G4VSceneHandler::AddSolid (const G4Tubs& tubs) { |
---|
216 | RequestPrimitives (tubs); |
---|
217 | } |
---|
218 | |
---|
219 | void G4VSceneHandler::AddSolid (const G4Cons& cons) { |
---|
220 | RequestPrimitives (cons); |
---|
221 | } |
---|
222 | |
---|
223 | void G4VSceneHandler::AddSolid (const G4Trd& trd) { |
---|
224 | RequestPrimitives (trd); |
---|
225 | } |
---|
226 | |
---|
227 | void G4VSceneHandler::AddSolid (const G4Trap& trap) { |
---|
228 | RequestPrimitives (trap); |
---|
229 | } |
---|
230 | |
---|
231 | void G4VSceneHandler::AddSolid (const G4Sphere& sphere) { |
---|
232 | RequestPrimitives (sphere ); |
---|
233 | } |
---|
234 | |
---|
235 | void G4VSceneHandler::AddSolid (const G4Para& para) { |
---|
236 | RequestPrimitives (para); |
---|
237 | } |
---|
238 | |
---|
239 | void G4VSceneHandler::AddSolid (const G4Torus& torus) { |
---|
240 | RequestPrimitives (torus); |
---|
241 | } |
---|
242 | |
---|
243 | void G4VSceneHandler::AddSolid (const G4Polycone& polycone) { |
---|
244 | RequestPrimitives (polycone); |
---|
245 | } |
---|
246 | |
---|
247 | void G4VSceneHandler::AddSolid (const G4Polyhedra& polyhedra) { |
---|
248 | RequestPrimitives (polyhedra); |
---|
249 | } |
---|
250 | |
---|
251 | void G4VSceneHandler::AddSolid (const G4VSolid& solid) { |
---|
252 | RequestPrimitives (solid); |
---|
253 | } |
---|
254 | |
---|
255 | void G4VSceneHandler::AddCompound (const G4VTrajectory& traj) { |
---|
256 | G4TrajectoriesModel* pTrModel = |
---|
257 | dynamic_cast<G4TrajectoriesModel*>(fpModel); |
---|
258 | if (!pTrModel) G4Exception |
---|
259 | ("G4VSceneHandler::AddCompound(const G4VTrajectory&): Not a G4TrajectoriesModel."); |
---|
260 | traj.DrawTrajectory(pTrModel->GetDrawingMode()); |
---|
261 | } |
---|
262 | |
---|
263 | void G4VSceneHandler::AddCompound (const G4VHit& hit) { |
---|
264 | ((G4VHit&)hit).Draw(); // Cast to non-const because Draw is non-const!!!! |
---|
265 | } |
---|
266 | |
---|
267 | void G4VSceneHandler::AddViewerToList (G4VViewer* pViewer) { |
---|
268 | fViewerList.push_back (pViewer); |
---|
269 | } |
---|
270 | |
---|
271 | void G4VSceneHandler::AddPrimitive (const G4Scale& scale) { |
---|
272 | |
---|
273 | const G4double margin(0.01); |
---|
274 | // Fractional margin - ensures scale is comfortably inside viewing |
---|
275 | // volume. |
---|
276 | const G4double oneMinusMargin (1. - margin); |
---|
277 | |
---|
278 | const G4VisExtent& sceneExtent = fpScene->GetExtent(); |
---|
279 | |
---|
280 | // Useful constants... |
---|
281 | const G4double length(scale.GetLength()); |
---|
282 | const G4double halfLength(length / 2.); |
---|
283 | const G4double tickLength(length / 20.); |
---|
284 | const G4double piBy2(halfpi); |
---|
285 | |
---|
286 | // Get size of scene... |
---|
287 | const G4double xmin = sceneExtent.GetXmin(); |
---|
288 | const G4double xmax = sceneExtent.GetXmax(); |
---|
289 | const G4double ymin = sceneExtent.GetYmin(); |
---|
290 | const G4double ymax = sceneExtent.GetYmax(); |
---|
291 | const G4double zmin = sceneExtent.GetZmin(); |
---|
292 | const G4double zmax = sceneExtent.GetZmax(); |
---|
293 | |
---|
294 | // Create (empty) polylines having the same vis attributes... |
---|
295 | G4Polyline scaleLine, tick11, tick12, tick21, tick22; |
---|
296 | G4VisAttributes visAtts(*scale.GetVisAttributes()); // Long enough life. |
---|
297 | scaleLine.SetVisAttributes(&visAtts); |
---|
298 | tick11.SetVisAttributes(&visAtts); |
---|
299 | tick12.SetVisAttributes(&visAtts); |
---|
300 | tick21.SetVisAttributes(&visAtts); |
---|
301 | tick22.SetVisAttributes(&visAtts); |
---|
302 | |
---|
303 | // Add points to the polylines to represent an scale parallel to the |
---|
304 | // x-axis centred on the origin... |
---|
305 | G4Point3D r1(G4Point3D(-halfLength, 0., 0.)); |
---|
306 | G4Point3D r2(G4Point3D( halfLength, 0., 0.)); |
---|
307 | scaleLine.push_back(r1); |
---|
308 | scaleLine.push_back(r2); |
---|
309 | G4Point3D ticky(0., tickLength, 0.); |
---|
310 | G4Point3D tickz(0., 0., tickLength); |
---|
311 | tick11.push_back(r1 + ticky); |
---|
312 | tick11.push_back(r1 - ticky); |
---|
313 | tick12.push_back(r1 + tickz); |
---|
314 | tick12.push_back(r1 - tickz); |
---|
315 | tick21.push_back(r2 + ticky); |
---|
316 | tick21.push_back(r2 - ticky); |
---|
317 | tick22.push_back(r2 + tickz); |
---|
318 | tick22.push_back(r2 - tickz); |
---|
319 | G4Point3D textPosition(0., tickLength, 0.); |
---|
320 | |
---|
321 | // Transform appropriately... |
---|
322 | |
---|
323 | G4Transform3D transformation; |
---|
324 | if (scale.GetAutoPlacing()) { |
---|
325 | G4Transform3D rotation; |
---|
326 | switch (scale.GetDirection()) { |
---|
327 | case G4Scale::x: |
---|
328 | break; |
---|
329 | case G4Scale::y: |
---|
330 | rotation = G4RotateZ3D(piBy2); |
---|
331 | break; |
---|
332 | case G4Scale::z: |
---|
333 | rotation = G4RotateY3D(piBy2); |
---|
334 | break; |
---|
335 | } |
---|
336 | G4double sxmid(scale.GetXmid()); |
---|
337 | G4double symid(scale.GetYmid()); |
---|
338 | G4double szmid(scale.GetZmid()); |
---|
339 | sxmid = xmin + oneMinusMargin * (xmax - xmin); |
---|
340 | symid = ymin + margin * (ymax - ymin); |
---|
341 | szmid = zmin + oneMinusMargin * (zmax - zmin); |
---|
342 | switch (scale.GetDirection()) { |
---|
343 | case G4Scale::x: |
---|
344 | sxmid -= halfLength; |
---|
345 | break; |
---|
346 | case G4Scale::y: |
---|
347 | symid += halfLength; |
---|
348 | break; |
---|
349 | case G4Scale::z: |
---|
350 | szmid -= halfLength; |
---|
351 | break; |
---|
352 | } |
---|
353 | G4Translate3D translation(sxmid, symid, szmid); |
---|
354 | transformation = translation * rotation; |
---|
355 | } else { |
---|
356 | if (fpModel) transformation = fpModel->GetTransformation(); |
---|
357 | } |
---|
358 | |
---|
359 | // Draw... |
---|
360 | // We would like to call BeginPrimitives(transformation) here but |
---|
361 | // calling BeginPrimitives from within an AddPrimitive is not |
---|
362 | // allowed! So we have to do our own transformation... |
---|
363 | AddPrimitive(scaleLine.transform(transformation)); |
---|
364 | AddPrimitive(tick11.transform(transformation)); |
---|
365 | AddPrimitive(tick12.transform(transformation)); |
---|
366 | AddPrimitive(tick21.transform(transformation)); |
---|
367 | AddPrimitive(tick22.transform(transformation)); |
---|
368 | G4Text text(scale.GetAnnotation(),textPosition.transform(transformation)); |
---|
369 | text.SetScreenSize(12.); |
---|
370 | AddPrimitive(text); |
---|
371 | } |
---|
372 | |
---|
373 | void G4VSceneHandler::AddPrimitive (const G4Polymarker& polymarker) { |
---|
374 | switch (polymarker.GetMarkerType()) { |
---|
375 | default: |
---|
376 | case G4Polymarker::dots: |
---|
377 | { |
---|
378 | for (size_t iPoint = 0; iPoint < polymarker.size (); iPoint++) { |
---|
379 | G4Circle dot (polymarker); |
---|
380 | dot.SetPosition (polymarker[iPoint]); |
---|
381 | dot.SetWorldSize (0.); |
---|
382 | dot.SetScreenSize (0.1); // Very small circle. |
---|
383 | AddPrimitive (dot); |
---|
384 | } |
---|
385 | } |
---|
386 | break; |
---|
387 | case G4Polymarker::circles: |
---|
388 | { |
---|
389 | for (size_t iPoint = 0; iPoint < polymarker.size (); iPoint++) { |
---|
390 | G4Circle circle (polymarker); |
---|
391 | circle.SetPosition (polymarker[iPoint]); |
---|
392 | AddPrimitive (circle); |
---|
393 | } |
---|
394 | } |
---|
395 | break; |
---|
396 | case G4Polymarker::squares: |
---|
397 | { |
---|
398 | for (size_t iPoint = 0; iPoint < polymarker.size (); iPoint++) { |
---|
399 | G4Square square (polymarker); |
---|
400 | square.SetPosition (polymarker[iPoint]); |
---|
401 | AddPrimitive (square); |
---|
402 | } |
---|
403 | } |
---|
404 | break; |
---|
405 | } |
---|
406 | } |
---|
407 | |
---|
408 | void G4VSceneHandler::RemoveViewerFromList (G4VViewer* pViewer) { |
---|
409 | fViewerList.remove(pViewer); |
---|
410 | } |
---|
411 | |
---|
412 | void G4VSceneHandler::SetScene (G4Scene* pScene) { |
---|
413 | fpScene = pScene; |
---|
414 | // Notify all viewers that a kernel visit is required. |
---|
415 | G4ViewerListIterator i; |
---|
416 | for (i = fViewerList.begin(); i != fViewerList.end(); i++) { |
---|
417 | (*i) -> SetNeedKernelVisit (true); |
---|
418 | } |
---|
419 | } |
---|
420 | |
---|
421 | void G4VSceneHandler::RequestPrimitives (const G4VSolid& solid) { |
---|
422 | BeginPrimitives (*fpObjectTransformation); |
---|
423 | G4NURBS* pNURBS = 0; |
---|
424 | G4Polyhedron* pPolyhedron = 0; |
---|
425 | switch (fpViewer -> GetViewParameters () . GetRepStyle ()) { |
---|
426 | case G4ViewParameters::nurbs: |
---|
427 | pNURBS = solid.CreateNURBS (); |
---|
428 | if (pNURBS) { |
---|
429 | pNURBS -> SetVisAttributes (fpVisAttribs); |
---|
430 | AddPrimitive (*pNURBS); |
---|
431 | delete pNURBS; |
---|
432 | break; |
---|
433 | } |
---|
434 | else { |
---|
435 | G4VisManager::Verbosity verbosity = |
---|
436 | G4VisManager::GetInstance()->GetVerbosity(); |
---|
437 | if (verbosity >= G4VisManager::errors) { |
---|
438 | G4cout << |
---|
439 | "ERROR: G4VSceneHandler::RequestPrimitives" |
---|
440 | "\n NURBS not available for " |
---|
441 | << solid.GetName () << G4endl; |
---|
442 | G4cout << "Trying polyhedron." << G4endl; |
---|
443 | } |
---|
444 | } |
---|
445 | // Dropping through to polyhedron... |
---|
446 | case G4ViewParameters::polyhedron: |
---|
447 | default: |
---|
448 | G4Polyhedron::SetNumberOfRotationSteps (GetNoOfSides (fpVisAttribs)); |
---|
449 | pPolyhedron = solid.GetPolyhedron (); |
---|
450 | G4Polyhedron::ResetNumberOfRotationSteps (); |
---|
451 | if (pPolyhedron) { |
---|
452 | pPolyhedron -> SetVisAttributes (fpVisAttribs); |
---|
453 | printf("G4VSceneHandler::RequestPrimitives VSolid: %d\n",pPolyhedron); |
---|
454 | G4cout << |
---|
455 | "G4VSceneHandler::RequestPrimitives VSolid " << solid.GetName () <<G4endl; |
---|
456 | AddPrimitive (*pPolyhedron); |
---|
457 | } |
---|
458 | else { |
---|
459 | G4VisManager::Verbosity verbosity = |
---|
460 | G4VisManager::GetInstance()->GetVerbosity(); |
---|
461 | if (verbosity >= G4VisManager::errors) { |
---|
462 | G4cout << |
---|
463 | "ERROR: G4VSceneHandler::RequestPrimitives" |
---|
464 | "\n Polyhedron not available for " << solid.GetName () << |
---|
465 | ".\n This means it cannot be visualized on most systems." |
---|
466 | "\n Contact the Visualization Coordinator." << G4endl; |
---|
467 | } |
---|
468 | } |
---|
469 | break; |
---|
470 | } |
---|
471 | EndPrimitives (); |
---|
472 | } |
---|
473 | |
---|
474 | void G4VSceneHandler::ProcessScene (G4VViewer&) { |
---|
475 | |
---|
476 | #ifdef G4DEBUG_VIS_MANAGEMENT |
---|
477 | printf("G4VSceneHandler::ProcessScene : BEGIN\n"); |
---|
478 | #endif |
---|
479 | |
---|
480 | if (!fpScene) return; |
---|
481 | |
---|
482 | G4VisManager* visManager = G4VisManager::GetInstance(); |
---|
483 | |
---|
484 | if (!visManager->GetConcreteInstance()) return; |
---|
485 | |
---|
486 | G4VisManager::Verbosity verbosity = visManager->GetVerbosity(); |
---|
487 | |
---|
488 | fReadyForTransients = false; |
---|
489 | |
---|
490 | #ifdef G4DEBUG_VIS_MANAGEMENT |
---|
491 | printf("G4VSceneHandler::ProcessScene : Before clear store \n"); |
---|
492 | #endif |
---|
493 | // Clear stored scene, if any, i.e., display lists, scene graphs. |
---|
494 | ClearStore (); |
---|
495 | |
---|
496 | // Reset fMarkForClearingTransientStore. No need to clear transient |
---|
497 | // store since it has just been cleared above. (Leaving |
---|
498 | // fMarkForClearingTransientStore true causes problems with |
---|
499 | // recomputing transients below.) Restore it again at end... |
---|
500 | G4bool tmpMarkForClearingTransientStore = fMarkForClearingTransientStore; |
---|
501 | fMarkForClearingTransientStore = false; |
---|
502 | |
---|
503 | // Traverse geometry tree and send drawing primitives to window(s). |
---|
504 | |
---|
505 | const std::vector<G4VModel*>& runDurationModelList = |
---|
506 | fpScene -> GetRunDurationModelList (); |
---|
507 | |
---|
508 | if (runDurationModelList.size ()) { |
---|
509 | if (verbosity >= G4VisManager::confirmations) { |
---|
510 | G4cout << "Traversing scene data..." << G4endl; |
---|
511 | } |
---|
512 | |
---|
513 | #ifdef G4DEBUG_VIS_MANAGEMENT |
---|
514 | printf("G4VSceneHandler::ProcessScene : begin model\n"); |
---|
515 | #endif |
---|
516 | BeginModeling (); |
---|
517 | |
---|
518 | // Create modeling parameters from view parameters... |
---|
519 | G4ModelingParameters* pMP = CreateModelingParameters (); |
---|
520 | |
---|
521 | for (size_t i = 0; i < runDurationModelList.size (); i++) { |
---|
522 | G4VModel* pModel = runDurationModelList[i]; |
---|
523 | // Note: this is not the place to take action on |
---|
524 | // pModel->GetTransformation(). The model must take care of |
---|
525 | // this in pModel->DescribeYourselfTo(*this). See, for example, |
---|
526 | // G4PhysicalVolumeModel and /vis/scene/add/logo. |
---|
527 | pModel -> SetModelingParameters (pMP); |
---|
528 | SetModel (pModel); // Store for use by derived class. |
---|
529 | pModel -> DescribeYourselfTo (*this); |
---|
530 | pModel -> SetModelingParameters (0); |
---|
531 | } |
---|
532 | |
---|
533 | // Repeat if required... |
---|
534 | if (fSecondPassRequested) { |
---|
535 | fSecondPass = true; |
---|
536 | for (size_t i = 0; i < runDurationModelList.size (); i++) { |
---|
537 | G4VModel* pModel = runDurationModelList[i]; |
---|
538 | pModel -> SetModelingParameters (pMP); |
---|
539 | SetModel (pModel); // Store for use by derived class. |
---|
540 | pModel -> DescribeYourselfTo (*this); |
---|
541 | pModel -> SetModelingParameters (0); |
---|
542 | } |
---|
543 | fSecondPass = false; |
---|
544 | fSecondPassRequested = false; |
---|
545 | } |
---|
546 | |
---|
547 | delete pMP; |
---|
548 | EndModeling (); |
---|
549 | |
---|
550 | } |
---|
551 | |
---|
552 | fpViewer->FinishView(); // Flush streams and/or swap buffers. |
---|
553 | |
---|
554 | fReadyForTransients = true; |
---|
555 | |
---|
556 | #ifdef G4DEBUG_VIS_MANAGEMENT |
---|
557 | printf("G4VSceneHandler::ProcessScene : Idle ?\n"); |
---|
558 | #endif |
---|
559 | // Refresh event from end-of-event model list. Allow only in Idle state... |
---|
560 | G4StateManager* stateManager = G4StateManager::GetStateManager(); |
---|
561 | G4ApplicationState state = stateManager->GetCurrentState(); |
---|
562 | if (state == G4State_Idle) { |
---|
563 | #ifdef G4DEBUG_VIS_MANAGEMENT |
---|
564 | printf("G4VSceneHandler::ProcessScene : IDLE\n"); |
---|
565 | #endif |
---|
566 | |
---|
567 | visManager->SetEventRefreshing(true); |
---|
568 | |
---|
569 | if (visManager->GetRequestedEvent()) { |
---|
570 | #ifdef G4DEBUG_VIS_MANAGEMENT |
---|
571 | printf("G4VSceneHandler::ProcessScene : DrawEvent\n"); |
---|
572 | #endif |
---|
573 | DrawEvent(visManager->GetRequestedEvent()); |
---|
574 | |
---|
575 | } else { |
---|
576 | |
---|
577 | #ifdef G4DEBUG_VIS_MANAGEMENT |
---|
578 | printf("G4VSceneHandler::ProcessScene : no event\n"); |
---|
579 | #endif |
---|
580 | G4RunManager* runManager = G4RunManager::GetRunManager(); |
---|
581 | if (runManager) { |
---|
582 | #ifdef G4DEBUG_VIS_MANAGEMENT |
---|
583 | printf("G4VSceneHandler::ProcessScene : runManager\n"); |
---|
584 | #endif |
---|
585 | const G4Run* run = runManager->GetCurrentRun(); |
---|
586 | const std::vector<const G4Event*>* events = |
---|
587 | run? run->GetEventVector(): 0; |
---|
588 | size_t nKeptEvents = 0; |
---|
589 | if (events) nKeptEvents = events->size(); |
---|
590 | if (nKeptEvents) { |
---|
591 | |
---|
592 | #ifdef G4DEBUG_VIS_MANAGEMENT |
---|
593 | printf("G4VSceneHandler::ProcessScene : 1\n"); |
---|
594 | #endif |
---|
595 | if (fpScene->GetRefreshAtEndOfEvent()) { |
---|
596 | #ifdef G4DEBUG_VIS_MANAGEMENT |
---|
597 | printf("G4VSceneHandler::ProcessScene : 2\n"); |
---|
598 | #endif |
---|
599 | |
---|
600 | if (verbosity >= G4VisManager::confirmations) { |
---|
601 | G4cout << "Refreshing event..." << G4endl; |
---|
602 | } |
---|
603 | const G4Event* event = 0; |
---|
604 | if (events && events->size()) event = events->back(); |
---|
605 | if (event) DrawEvent(event); |
---|
606 | |
---|
607 | } else { // Accumulating events. |
---|
608 | #ifdef G4DEBUG_VIS_MANAGEMENT |
---|
609 | printf("G4VSceneHandler::ProcessScene : 3\n"); |
---|
610 | #endif |
---|
611 | |
---|
612 | if (verbosity >= G4VisManager::confirmations) { |
---|
613 | G4cout << "Refreshing events in run..." << G4endl; |
---|
614 | } |
---|
615 | for (size_t i = 0; i < nKeptEvents; ++i) { |
---|
616 | #ifdef G4DEBUG_VIS_MANAGEMENT |
---|
617 | printf("G4VSceneHandler::ProcessScene : 4\n"); |
---|
618 | #endif |
---|
619 | const G4Event* event = (*events)[i]; |
---|
620 | if (event) DrawEvent(event); |
---|
621 | } |
---|
622 | |
---|
623 | if (!fpScene->GetRefreshAtEndOfRun()) { |
---|
624 | #ifdef G4DEBUG_VIS_MANAGEMENT |
---|
625 | printf("G4VSceneHandler::ProcessScene : 5\n"); |
---|
626 | #endif |
---|
627 | if (verbosity >= G4VisManager::warnings) { |
---|
628 | G4cout << |
---|
629 | "WARNING: Cannot refresh events accumulated over more" |
---|
630 | "\n than one runs. Refreshed just the last run." |
---|
631 | << G4endl; |
---|
632 | } |
---|
633 | } |
---|
634 | } |
---|
635 | } |
---|
636 | } |
---|
637 | } |
---|
638 | visManager->SetEventRefreshing(false); |
---|
639 | } |
---|
640 | |
---|
641 | fMarkForClearingTransientStore = tmpMarkForClearingTransientStore; |
---|
642 | } |
---|
643 | |
---|
644 | void G4VSceneHandler::DrawEvent(const G4Event* event) |
---|
645 | { |
---|
646 | const std::vector<G4VModel*>& EOEModelList = |
---|
647 | fpScene -> GetEndOfEventModelList (); |
---|
648 | size_t nModels = EOEModelList.size(); |
---|
649 | if (nModels) { |
---|
650 | G4ModelingParameters* pMP = CreateModelingParameters(); |
---|
651 | pMP->SetEvent(event); |
---|
652 | for (size_t i = 0; i < nModels; i++) { |
---|
653 | G4VModel* pModel = EOEModelList [i]; |
---|
654 | pModel -> SetModelingParameters(pMP); |
---|
655 | SetModel (pModel); |
---|
656 | pModel -> DescribeYourselfTo (*this); |
---|
657 | pModel -> SetModelingParameters(0); |
---|
658 | } |
---|
659 | delete pMP; |
---|
660 | SetModel (0); |
---|
661 | } |
---|
662 | } |
---|
663 | |
---|
664 | G4ModelingParameters* G4VSceneHandler::CreateModelingParameters () |
---|
665 | { |
---|
666 | // Create modeling parameters from View Parameters... |
---|
667 | const G4ViewParameters& vp = fpViewer -> GetViewParameters (); |
---|
668 | |
---|
669 | // Convert drawing styles... |
---|
670 | G4ModelingParameters::DrawingStyle modelDrawingStyle = |
---|
671 | G4ModelingParameters::wf; |
---|
672 | switch (vp.GetDrawingStyle ()) { |
---|
673 | default: |
---|
674 | case G4ViewParameters::wireframe: |
---|
675 | modelDrawingStyle = G4ModelingParameters::wf; |
---|
676 | break; |
---|
677 | case G4ViewParameters::hlr: |
---|
678 | modelDrawingStyle = G4ModelingParameters::hlr; |
---|
679 | break; |
---|
680 | case G4ViewParameters::hsr: |
---|
681 | modelDrawingStyle = G4ModelingParameters::hsr; |
---|
682 | break; |
---|
683 | case G4ViewParameters::hlhsr: |
---|
684 | modelDrawingStyle = G4ModelingParameters::hlhsr; |
---|
685 | break; |
---|
686 | } |
---|
687 | |
---|
688 | // Decide if covered daughters are really to be culled... |
---|
689 | G4bool reallyCullCovered = |
---|
690 | vp.IsCullingCovered() // Culling daughters depends also on... |
---|
691 | && !vp.IsSection () // Sections (DCUT) not requested. |
---|
692 | && !vp.IsCutaway () // Cutaways not requested. |
---|
693 | ; |
---|
694 | |
---|
695 | G4ModelingParameters* pModelingParams = new G4ModelingParameters |
---|
696 | (vp.GetDefaultVisAttributes (), |
---|
697 | modelDrawingStyle, |
---|
698 | vp.IsCulling (), |
---|
699 | vp.IsCullingInvisible (), |
---|
700 | vp.IsDensityCulling (), |
---|
701 | vp.GetVisibleDensity (), |
---|
702 | reallyCullCovered, |
---|
703 | vp.GetNoOfSides () |
---|
704 | ); |
---|
705 | |
---|
706 | pModelingParams->SetWarning |
---|
707 | (G4VisManager::GetInstance()->GetVerbosity() >= G4VisManager::warnings); |
---|
708 | |
---|
709 | pModelingParams->SetExplodeFactor(vp.GetExplodeFactor()); |
---|
710 | pModelingParams->SetExplodeCentre(vp.GetExplodeCentre()); |
---|
711 | |
---|
712 | pModelingParams->SetSectionPolyhedron(CreateSectionPolyhedron()); |
---|
713 | pModelingParams->SetCutawayPolyhedron(CreateCutawayPolyhedron()); |
---|
714 | // The polyhedron objects are deleted in the modeling parameters destructor. |
---|
715 | |
---|
716 | return pModelingParams; |
---|
717 | } |
---|
718 | |
---|
719 | const G4Polyhedron* G4VSceneHandler::CreateSectionPolyhedron() |
---|
720 | { |
---|
721 | /* Disable for now. Boolean processor not up to it. |
---|
722 | const G4ViewParameters& vp = fpViewer->GetViewParameters(); |
---|
723 | if (vp.IsSection () ) { |
---|
724 | G4double radius = fpScene->GetExtent().GetExtentRadius(); |
---|
725 | G4double safe = radius + fpScene->GetExtent().GetExtentCentre().mag(); |
---|
726 | G4Box sectionBox("clipper", |
---|
727 | safe, safe, 1.e-5 * radius); // Thin in z-plane. |
---|
728 | G4Polyhedron* sectioner = sectionBox.CreatePolyhedron(); |
---|
729 | const G4Plane3D& s = vp.GetSectionPlane (); |
---|
730 | G4double a = s.a(); |
---|
731 | G4double b = s.b(); |
---|
732 | G4double c = s.c(); |
---|
733 | G4double d = s.d(); |
---|
734 | G4Transform3D transform = G4TranslateZ3D(-d); |
---|
735 | const G4Normal3D normal(a,b,c); |
---|
736 | if (normal != G4Normal3D(0,0,1)) { |
---|
737 | const G4double angle = std::acos(normal.dot(G4Normal3D(0,0,1))); |
---|
738 | const G4Vector3D axis = G4Normal3D(0,0,1).cross(normal); |
---|
739 | transform = G4Rotate3D(angle, axis) * transform; |
---|
740 | } |
---|
741 | sectioner->Transform(transform); |
---|
742 | return sectioner; |
---|
743 | } else { |
---|
744 | return 0; |
---|
745 | } |
---|
746 | */ |
---|
747 | return 0; |
---|
748 | } |
---|
749 | |
---|
750 | const G4Polyhedron* G4VSceneHandler::CreateCutawayPolyhedron() |
---|
751 | { |
---|
752 | return 0; |
---|
753 | } |
---|
754 | |
---|
755 | void G4VSceneHandler::LoadAtts(const G4Visible& visible, G4AttHolder* holder) |
---|
756 | { |
---|
757 | // Load G4Atts from G4VisAttributes, if any... |
---|
758 | const G4VisAttributes* va = visible.GetVisAttributes(); |
---|
759 | if (va) { |
---|
760 | const std::map<G4String,G4AttDef>* vaDefs = |
---|
761 | va->GetAttDefs(); |
---|
762 | if (vaDefs) { |
---|
763 | holder->AddAtts(visible.GetVisAttributes()->CreateAttValues(), vaDefs); |
---|
764 | } |
---|
765 | } |
---|
766 | |
---|
767 | G4PhysicalVolumeModel* pPVModel = |
---|
768 | dynamic_cast<G4PhysicalVolumeModel*>(fpModel); |
---|
769 | if (pPVModel) { |
---|
770 | // Load G4Atts from G4PhysicalVolumeModel... |
---|
771 | const std::map<G4String,G4AttDef>* defs = pPVModel->GetAttDefs(); |
---|
772 | if (defs) { |
---|
773 | holder->AddAtts(pPVModel->CreateCurrentAttValues(), defs); |
---|
774 | } |
---|
775 | } |
---|
776 | |
---|
777 | G4TrajectoriesModel* trajModel = dynamic_cast<G4TrajectoriesModel*>(fpModel); |
---|
778 | if (trajModel) { |
---|
779 | // Load G4Atts from trajectory... |
---|
780 | const G4VTrajectory* traj = trajModel->GetCurrentTrajectory(); |
---|
781 | const std::map<G4String,G4AttDef>* defs = traj->GetAttDefs(); |
---|
782 | if (defs) { |
---|
783 | holder->AddAtts(traj->CreateAttValues(), defs); |
---|
784 | } |
---|
785 | G4int nPoints = traj->GetPointEntries(); |
---|
786 | for (G4int i = 0; i < nPoints; ++i) { |
---|
787 | G4VTrajectoryPoint* trajPoint = traj->GetPoint(i); |
---|
788 | const std::map<G4String,G4AttDef>* defs = trajPoint->GetAttDefs(); |
---|
789 | if (defs) { |
---|
790 | holder->AddAtts(trajPoint->CreateAttValues(), defs); |
---|
791 | } |
---|
792 | } |
---|
793 | } |
---|
794 | |
---|
795 | G4HitsModel* hitsModel = dynamic_cast<G4HitsModel*>(fpModel); |
---|
796 | if (hitsModel) { |
---|
797 | // Load G4Atts from hit... |
---|
798 | const G4VHit* hit = hitsModel->GetCurrentHit(); |
---|
799 | const std::map<G4String,G4AttDef>* defs = hit->GetAttDefs(); |
---|
800 | if (defs) { |
---|
801 | holder->AddAtts(hit->CreateAttValues(), defs); |
---|
802 | } |
---|
803 | } |
---|
804 | } |
---|
805 | |
---|
806 | const G4Colour& G4VSceneHandler::GetColour (const G4Visible& visible) { |
---|
807 | // Colour is determined by the applicable vis attributes. |
---|
808 | const G4Colour& colour = fpViewer -> |
---|
809 | GetApplicableVisAttributes (visible.GetVisAttributes ()) -> GetColour (); |
---|
810 | return colour; |
---|
811 | } |
---|
812 | |
---|
813 | const G4Colour& G4VSceneHandler::GetTextColour (const G4Text& text) { |
---|
814 | const G4VisAttributes* pVA = text.GetVisAttributes (); |
---|
815 | if (!pVA) { |
---|
816 | pVA = fpViewer -> GetViewParameters (). GetDefaultTextVisAttributes (); |
---|
817 | } |
---|
818 | const G4Colour& colour = pVA -> GetColour (); |
---|
819 | return colour; |
---|
820 | } |
---|
821 | |
---|
822 | G4double G4VSceneHandler::GetLineWidth(const G4VisAttributes* pVisAttribs) |
---|
823 | { |
---|
824 | G4double lineWidth = pVisAttribs->GetLineWidth(); |
---|
825 | if (lineWidth < 1.) lineWidth = 1.; |
---|
826 | lineWidth *= fpViewer -> GetViewParameters().GetGlobalLineWidthScale(); |
---|
827 | if (lineWidth < 1.) lineWidth = 1.; |
---|
828 | return lineWidth; |
---|
829 | } |
---|
830 | |
---|
831 | G4ViewParameters::DrawingStyle G4VSceneHandler::GetDrawingStyle |
---|
832 | (const G4VisAttributes* pVisAttribs) { |
---|
833 | // Drawing style is normally determined by the view parameters, but |
---|
834 | // it can be overriddden by the ForceDrawingStyle flag in the vis |
---|
835 | // attributes. |
---|
836 | G4ViewParameters::DrawingStyle style = |
---|
837 | fpViewer->GetViewParameters().GetDrawingStyle(); |
---|
838 | if (pVisAttribs -> IsForceDrawingStyle ()) { |
---|
839 | G4VisAttributes::ForcedDrawingStyle forcedStyle = |
---|
840 | pVisAttribs -> GetForcedDrawingStyle (); |
---|
841 | // This is complicated because if hidden line and surface removal |
---|
842 | // has been requested we wish to preserve this sometimes. |
---|
843 | switch (forcedStyle) { |
---|
844 | case (G4VisAttributes::solid): |
---|
845 | switch (style) { |
---|
846 | case (G4ViewParameters::hlr): |
---|
847 | style = G4ViewParameters::hlhsr; |
---|
848 | break; |
---|
849 | case (G4ViewParameters::wireframe): |
---|
850 | style = G4ViewParameters::hsr; |
---|
851 | break; |
---|
852 | case (G4ViewParameters::hlhsr): |
---|
853 | case (G4ViewParameters::hsr): |
---|
854 | default: |
---|
855 | break; |
---|
856 | } |
---|
857 | break; |
---|
858 | case (G4VisAttributes::wireframe): |
---|
859 | default: |
---|
860 | // But if forced style is wireframe, do it, because one of its |
---|
861 | // main uses is in displaying the consituent solids of a Boolean |
---|
862 | // solid and their surfaces overlap with the resulting Booean |
---|
863 | // solid, making a mess if hlr is specified. |
---|
864 | style = G4ViewParameters::wireframe; |
---|
865 | break; |
---|
866 | } |
---|
867 | } |
---|
868 | return style; |
---|
869 | } |
---|
870 | |
---|
871 | G4bool G4VSceneHandler::GetAuxEdgeVisible (const G4VisAttributes* pVisAttribs) { |
---|
872 | G4bool isAuxEdgeVisible = fpViewer->GetViewParameters().IsAuxEdgeVisible (); |
---|
873 | if (pVisAttribs -> IsForceAuxEdgeVisible()) isAuxEdgeVisible = true; |
---|
874 | return isAuxEdgeVisible; |
---|
875 | } |
---|
876 | |
---|
877 | G4double G4VSceneHandler::GetMarkerSize |
---|
878 | (const G4VMarker& marker, |
---|
879 | G4VSceneHandler::MarkerSizeType& markerSizeType) |
---|
880 | { |
---|
881 | G4bool userSpecified = marker.GetWorldSize() || marker.GetScreenSize(); |
---|
882 | const G4VMarker& defaultMarker = |
---|
883 | fpViewer -> GetViewParameters().GetDefaultMarker(); |
---|
884 | G4double size = userSpecified ? |
---|
885 | marker.GetWorldSize() : defaultMarker.GetWorldSize(); |
---|
886 | if (size) { |
---|
887 | // Draw in world coordinates. |
---|
888 | markerSizeType = world; |
---|
889 | } |
---|
890 | else { |
---|
891 | size = userSpecified ? |
---|
892 | marker.GetScreenSize() : defaultMarker.GetScreenSize(); |
---|
893 | // Draw in screen coordinates. |
---|
894 | markerSizeType = screen; |
---|
895 | } |
---|
896 | if (size <= 1.) size = 1.; |
---|
897 | size *= fpViewer -> GetViewParameters().GetGlobalMarkerScale(); |
---|
898 | if (size <= 1.) size = 1.; |
---|
899 | return size; |
---|
900 | } |
---|
901 | |
---|
902 | G4int G4VSceneHandler::GetNoOfSides(const G4VisAttributes* pVisAttribs) |
---|
903 | { |
---|
904 | // No. of sides (lines segments per circle) is normally determined |
---|
905 | // by the view parameters, but it can be overriddden by the |
---|
906 | // ForceLineSegmentsPerCircle in the vis attributes. |
---|
907 | G4int lineSegmentsPerCircle = fpViewer->GetViewParameters().GetNoOfSides(); |
---|
908 | if (pVisAttribs) { |
---|
909 | if (pVisAttribs->IsForceLineSegmentsPerCircle()) |
---|
910 | lineSegmentsPerCircle = pVisAttribs->GetForcedLineSegmentsPerCircle(); |
---|
911 | const G4int nSegmentsMin = 12; |
---|
912 | if (lineSegmentsPerCircle < nSegmentsMin) { |
---|
913 | lineSegmentsPerCircle = nSegmentsMin; |
---|
914 | G4cout << |
---|
915 | "G4VSceneHandler::GetNoOfSides: attempt to set the" |
---|
916 | "\nnumber of line segements per circle < " << nSegmentsMin |
---|
917 | << "; forced to " << lineSegmentsPerCircle << G4endl; |
---|
918 | } |
---|
919 | } |
---|
920 | return lineSegmentsPerCircle; |
---|
921 | } |
---|
922 | |
---|
923 | std::ostream& operator << (std::ostream& os, const G4VSceneHandler& s) { |
---|
924 | |
---|
925 | os << "Scene handler " << s.fName << " has " |
---|
926 | << s.fViewerList.size () << " viewer(s):"; |
---|
927 | for (size_t i = 0; i < s.fViewerList.size (); i++) { |
---|
928 | os << "\n " << *(s.fViewerList [i]); |
---|
929 | } |
---|
930 | |
---|
931 | if (s.fpScene) { |
---|
932 | os << "\n " << *s.fpScene; |
---|
933 | } |
---|
934 | else { |
---|
935 | os << "\n This scene handler currently has no scene."; |
---|
936 | } |
---|
937 | |
---|
938 | return os; |
---|
939 | } |
---|