source: trunk/source/visualization/management/src/G4VSceneHandler.cc@ 1137

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

debug updates

  • Property svn:mime-type set to text/cpp
File size: 30.0 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: 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
84G4VSceneHandler::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
117G4VSceneHandler::~G4VSceneHandler () {
118 G4ViewerListIterator i;
119 for (i = fViewerList.begin(); i != fViewerList.end(); ++i) {
120 delete *i;
121 }
122}
123
124void G4VSceneHandler::PreAddSolid (const G4Transform3D& objectTransformation,
125 const G4VisAttributes& visAttribs) {
126 fpObjectTransformation = &objectTransformation;
127 fpVisAttribs = &visAttribs;
128 fProcessingSolid = true;
129}
130
131void G4VSceneHandler::PostAddSolid () {
132 fpObjectTransformation = 0;
133 fpVisAttribs = 0;
134 fProcessingSolid = false;
135 if (fReadyForTransients) {
136 fTransientsDrawnThisEvent = true;
137 fTransientsDrawnThisRun = true;
138 }
139}
140
141void 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
150void 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
161void 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
170void 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
181void G4VSceneHandler::BeginModeling () {
182}
183
184void G4VSceneHandler::EndModeling ()
185{
186 fpModel = 0;
187}
188
189void 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
197void G4VSceneHandler::ClearTransientStore () {
198}
199
200void 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
215void G4VSceneHandler::AddSolid (const G4Tubs& tubs) {
216 RequestPrimitives (tubs);
217}
218
219void G4VSceneHandler::AddSolid (const G4Cons& cons) {
220 RequestPrimitives (cons);
221}
222
223void G4VSceneHandler::AddSolid (const G4Trd& trd) {
224 RequestPrimitives (trd);
225}
226
227void G4VSceneHandler::AddSolid (const G4Trap& trap) {
228 RequestPrimitives (trap);
229}
230
231void G4VSceneHandler::AddSolid (const G4Sphere& sphere) {
232 RequestPrimitives (sphere );
233}
234
235void G4VSceneHandler::AddSolid (const G4Para& para) {
236 RequestPrimitives (para);
237}
238
239void G4VSceneHandler::AddSolid (const G4Torus& torus) {
240 RequestPrimitives (torus);
241}
242
243void G4VSceneHandler::AddSolid (const G4Polycone& polycone) {
244 RequestPrimitives (polycone);
245}
246
247void G4VSceneHandler::AddSolid (const G4Polyhedra& polyhedra) {
248 RequestPrimitives (polyhedra);
249}
250
251void G4VSceneHandler::AddSolid (const G4VSolid& solid) {
252 RequestPrimitives (solid);
253}
254
255void 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
263void G4VSceneHandler::AddCompound (const G4VHit& hit) {
264 ((G4VHit&)hit).Draw(); // Cast to non-const because Draw is non-const!!!!
265}
266
267void G4VSceneHandler::AddViewerToList (G4VViewer* pViewer) {
268 fViewerList.push_back (pViewer);
269}
270
271void 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
373void 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
408void G4VSceneHandler::RemoveViewerFromList (G4VViewer* pViewer) {
409 fViewerList.remove(pViewer);
410}
411
412void 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
421void 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#ifdef G4DEBUG_VIS_MANAGEMENT
454 printf("G4VSceneHandler::RequestPrimitives VSolid: %d\n",pPolyhedron);
455#endif
456 G4cout <<
457 "G4VSceneHandler::RequestPrimitives VSolid " << solid.GetName () <<G4endl;
458 AddPrimitive (*pPolyhedron);
459 }
460 else {
461 G4VisManager::Verbosity verbosity =
462 G4VisManager::GetInstance()->GetVerbosity();
463 if (verbosity >= G4VisManager::errors) {
464 G4cout <<
465 "ERROR: G4VSceneHandler::RequestPrimitives"
466 "\n Polyhedron not available for " << solid.GetName () <<
467 ".\n This means it cannot be visualized on most systems."
468 "\n Contact the Visualization Coordinator." << G4endl;
469 }
470 }
471 break;
472 }
473 EndPrimitives ();
474}
475
476void G4VSceneHandler::ProcessScene (G4VViewer&) {
477
478#ifdef G4DEBUG_VIS_MANAGEMENT
479 printf("G4VSceneHandler::ProcessScene : BEGIN\n");
480#endif
481
482 if (!fpScene) return;
483
484#ifdef G4DEBUG_VIS_MANAGEMENT
485 printf("G4VSceneHandler::ProcessScene : 2\n");
486#endif
487 G4VisManager* visManager = G4VisManager::GetInstance();
488
489#ifdef G4DEBUG_VIS_MANAGEMENT
490 printf("G4VSceneHandler::ProcessScene : 3\n");
491#endif
492 if (!visManager->GetConcreteInstance()) return;
493
494#ifdef G4DEBUG_VIS_MANAGEMENT
495 printf("G4VSceneHandler::ProcessScene : 4\n");
496#endif
497 G4VisManager::Verbosity verbosity = visManager->GetVerbosity();
498
499 fReadyForTransients = false;
500
501#ifdef G4DEBUG_VIS_MANAGEMENT
502 printf("G4VSceneHandler::ProcessScene : Before clear store \n");
503#endif
504 // Clear stored scene, if any, i.e., display lists, scene graphs.
505 ClearStore ();
506
507#ifdef G4DEBUG_VIS_MANAGEMENT
508 printf("G4VSceneHandler::ProcessScene : 5\n");
509#endif
510 // Reset fMarkForClearingTransientStore. No need to clear transient
511 // store since it has just been cleared above. (Leaving
512 // fMarkForClearingTransientStore true causes problems with
513 // recomputing transients below.) Restore it again at end...
514 G4bool tmpMarkForClearingTransientStore = fMarkForClearingTransientStore;
515 fMarkForClearingTransientStore = false;
516
517 // Traverse geometry tree and send drawing primitives to window(s).
518
519 const std::vector<G4VModel*>& runDurationModelList =
520 fpScene -> GetRunDurationModelList ();
521
522 if (runDurationModelList.size ()) {
523#ifdef G4DEBUG_VIS_MANAGEMENT
524 printf("G4VSceneHandler::ProcessScene : 6\n");
525#endif
526 if (verbosity >= G4VisManager::confirmations) {
527 G4cout << "Traversing scene data..." << G4endl;
528 }
529
530#ifdef G4DEBUG_VIS_MANAGEMENT
531 printf("G4VSceneHandler::ProcessScene : begin model\n");
532#endif
533 BeginModeling ();
534
535 // Create modeling parameters from view parameters...
536 G4ModelingParameters* pMP = CreateModelingParameters ();
537
538 for (size_t i = 0; i < runDurationModelList.size (); i++) {
539#ifdef G4DEBUG_VIS_MANAGEMENT
540 printf("G4VSceneHandler::ProcessScene : 7\n");
541#endif
542 G4VModel* pModel = runDurationModelList[i];
543 // Note: this is not the place to take action on
544 // pModel->GetTransformation(). The model must take care of
545 // this in pModel->DescribeYourselfTo(*this). See, for example,
546 // G4PhysicalVolumeModel and /vis/scene/add/logo.
547 pModel -> SetModelingParameters (pMP);
548 SetModel (pModel); // Store for use by derived class.
549 pModel -> DescribeYourselfTo (*this);
550 pModel -> SetModelingParameters (0);
551 }
552
553 // Repeat if required...
554 if (fSecondPassRequested) {
555#ifdef G4DEBUG_VIS_MANAGEMENT
556 printf("G4VSceneHandler::ProcessScene : 8\n");
557#endif
558 fSecondPass = true;
559 for (size_t i = 0; i < runDurationModelList.size (); i++) {
560#ifdef G4DEBUG_VIS_MANAGEMENT
561 printf("G4VSceneHandler::ProcessScene : 9\n");
562#endif
563 G4VModel* pModel = runDurationModelList[i];
564 pModel -> SetModelingParameters (pMP);
565 SetModel (pModel); // Store for use by derived class.
566 pModel -> DescribeYourselfTo (*this);
567 pModel -> SetModelingParameters (0);
568 }
569 fSecondPass = false;
570 fSecondPassRequested = false;
571 }
572
573 delete pMP;
574 EndModeling ();
575
576 }
577
578 fpViewer->FinishView(); // Flush streams and/or swap buffers.
579
580 fReadyForTransients = true;
581
582#ifdef G4DEBUG_VIS_MANAGEMENT
583 printf("G4VSceneHandler::ProcessScene : Idle ?\n");
584#endif
585 // Refresh event from end-of-event model list. Allow only in Idle state...
586 G4StateManager* stateManager = G4StateManager::GetStateManager();
587 G4ApplicationState state = stateManager->GetCurrentState();
588 if (state == G4State_Idle) {
589#ifdef G4DEBUG_VIS_MANAGEMENT
590 printf("G4VSceneHandler::ProcessScene : IDLE\n");
591#endif
592
593 visManager->SetEventRefreshing(true);
594
595 if (visManager->GetRequestedEvent()) {
596#ifdef G4DEBUG_VIS_MANAGEMENT
597 printf("G4VSceneHandler::ProcessScene : DrawEvent\n");
598#endif
599 DrawEvent(visManager->GetRequestedEvent());
600
601 } else {
602
603#ifdef G4DEBUG_VIS_MANAGEMENT
604 printf("G4VSceneHandler::ProcessScene : no event\n");
605#endif
606 G4RunManager* runManager = G4RunManager::GetRunManager();
607 if (runManager) {
608#ifdef G4DEBUG_VIS_MANAGEMENT
609 printf("G4VSceneHandler::ProcessScene : runManager\n");
610#endif
611 const G4Run* run = runManager->GetCurrentRun();
612 const std::vector<const G4Event*>* events =
613 run? run->GetEventVector(): 0;
614 size_t nKeptEvents = 0;
615 if (events) nKeptEvents = events->size();
616 if (nKeptEvents) {
617
618#ifdef G4DEBUG_VIS_MANAGEMENT
619 printf("G4VSceneHandler::ProcessScene : 1\n");
620#endif
621 if (fpScene->GetRefreshAtEndOfEvent()) {
622#ifdef G4DEBUG_VIS_MANAGEMENT
623 printf("G4VSceneHandler::ProcessScene : 2\n");
624#endif
625
626 if (verbosity >= G4VisManager::confirmations) {
627 G4cout << "Refreshing event..." << G4endl;
628 }
629 const G4Event* event = 0;
630 if (events && events->size()) event = events->back();
631 if (event) DrawEvent(event);
632
633 } else { // Accumulating events.
634#ifdef G4DEBUG_VIS_MANAGEMENT
635 printf("G4VSceneHandler::ProcessScene : 3\n");
636#endif
637
638 if (verbosity >= G4VisManager::confirmations) {
639 G4cout << "Refreshing events in run..." << G4endl;
640 }
641 for (size_t i = 0; i < nKeptEvents; ++i) {
642#ifdef G4DEBUG_VIS_MANAGEMENT
643 printf("G4VSceneHandler::ProcessScene : 4\n");
644#endif
645 const G4Event* event = (*events)[i];
646 if (event) DrawEvent(event);
647 }
648
649 if (!fpScene->GetRefreshAtEndOfRun()) {
650#ifdef G4DEBUG_VIS_MANAGEMENT
651 printf("G4VSceneHandler::ProcessScene : 5\n");
652#endif
653 if (verbosity >= G4VisManager::warnings) {
654 G4cout <<
655 "WARNING: Cannot refresh events accumulated over more"
656 "\n than one runs. Refreshed just the last run."
657 << G4endl;
658 }
659 }
660 }
661 }
662 }
663 }
664 visManager->SetEventRefreshing(false);
665 }
666
667 fMarkForClearingTransientStore = tmpMarkForClearingTransientStore;
668}
669
670void G4VSceneHandler::DrawEvent(const G4Event* event)
671{
672 const std::vector<G4VModel*>& EOEModelList =
673 fpScene -> GetEndOfEventModelList ();
674 size_t nModels = EOEModelList.size();
675 if (nModels) {
676 G4ModelingParameters* pMP = CreateModelingParameters();
677 pMP->SetEvent(event);
678 for (size_t i = 0; i < nModels; i++) {
679 G4VModel* pModel = EOEModelList [i];
680 pModel -> SetModelingParameters(pMP);
681 SetModel (pModel);
682 pModel -> DescribeYourselfTo (*this);
683 pModel -> SetModelingParameters(0);
684 }
685 delete pMP;
686 SetModel (0);
687 }
688}
689
690G4ModelingParameters* G4VSceneHandler::CreateModelingParameters ()
691{
692 // Create modeling parameters from View Parameters...
693 const G4ViewParameters& vp = fpViewer -> GetViewParameters ();
694
695 // Convert drawing styles...
696 G4ModelingParameters::DrawingStyle modelDrawingStyle =
697 G4ModelingParameters::wf;
698 switch (vp.GetDrawingStyle ()) {
699 default:
700 case G4ViewParameters::wireframe:
701 modelDrawingStyle = G4ModelingParameters::wf;
702 break;
703 case G4ViewParameters::hlr:
704 modelDrawingStyle = G4ModelingParameters::hlr;
705 break;
706 case G4ViewParameters::hsr:
707 modelDrawingStyle = G4ModelingParameters::hsr;
708 break;
709 case G4ViewParameters::hlhsr:
710 modelDrawingStyle = G4ModelingParameters::hlhsr;
711 break;
712 }
713
714 // Decide if covered daughters are really to be culled...
715 G4bool reallyCullCovered =
716 vp.IsCullingCovered() // Culling daughters depends also on...
717 && !vp.IsSection () // Sections (DCUT) not requested.
718 && !vp.IsCutaway () // Cutaways not requested.
719 ;
720
721 G4ModelingParameters* pModelingParams = new G4ModelingParameters
722 (vp.GetDefaultVisAttributes (),
723 modelDrawingStyle,
724 vp.IsCulling (),
725 vp.IsCullingInvisible (),
726 vp.IsDensityCulling (),
727 vp.GetVisibleDensity (),
728 reallyCullCovered,
729 vp.GetNoOfSides ()
730 );
731
732 pModelingParams->SetWarning
733 (G4VisManager::GetInstance()->GetVerbosity() >= G4VisManager::warnings);
734
735 pModelingParams->SetExplodeFactor(vp.GetExplodeFactor());
736 pModelingParams->SetExplodeCentre(vp.GetExplodeCentre());
737
738 pModelingParams->SetSectionPolyhedron(CreateSectionPolyhedron());
739 pModelingParams->SetCutawayPolyhedron(CreateCutawayPolyhedron());
740 // The polyhedron objects are deleted in the modeling parameters destructor.
741
742 return pModelingParams;
743}
744
745const G4Polyhedron* G4VSceneHandler::CreateSectionPolyhedron()
746{
747 /* Disable for now. Boolean processor not up to it.
748 const G4ViewParameters& vp = fpViewer->GetViewParameters();
749 if (vp.IsSection () ) {
750 G4double radius = fpScene->GetExtent().GetExtentRadius();
751 G4double safe = radius + fpScene->GetExtent().GetExtentCentre().mag();
752 G4Box sectionBox("clipper",
753 safe, safe, 1.e-5 * radius); // Thin in z-plane.
754 G4Polyhedron* sectioner = sectionBox.CreatePolyhedron();
755 const G4Plane3D& s = vp.GetSectionPlane ();
756 G4double a = s.a();
757 G4double b = s.b();
758 G4double c = s.c();
759 G4double d = s.d();
760 G4Transform3D transform = G4TranslateZ3D(-d);
761 const G4Normal3D normal(a,b,c);
762 if (normal != G4Normal3D(0,0,1)) {
763 const G4double angle = std::acos(normal.dot(G4Normal3D(0,0,1)));
764 const G4Vector3D axis = G4Normal3D(0,0,1).cross(normal);
765 transform = G4Rotate3D(angle, axis) * transform;
766 }
767 sectioner->Transform(transform);
768 return sectioner;
769 } else {
770 return 0;
771 }
772 */
773 return 0;
774}
775
776const G4Polyhedron* G4VSceneHandler::CreateCutawayPolyhedron()
777{
778 return 0;
779}
780
781void G4VSceneHandler::LoadAtts(const G4Visible& visible, G4AttHolder* holder)
782{
783 // Load G4Atts from G4VisAttributes, if any...
784 const G4VisAttributes* va = visible.GetVisAttributes();
785 if (va) {
786 const std::map<G4String,G4AttDef>* vaDefs =
787 va->GetAttDefs();
788 if (vaDefs) {
789 holder->AddAtts(visible.GetVisAttributes()->CreateAttValues(), vaDefs);
790 }
791 }
792
793 G4PhysicalVolumeModel* pPVModel =
794 dynamic_cast<G4PhysicalVolumeModel*>(fpModel);
795 if (pPVModel) {
796 // Load G4Atts from G4PhysicalVolumeModel...
797 const std::map<G4String,G4AttDef>* defs = pPVModel->GetAttDefs();
798 if (defs) {
799 holder->AddAtts(pPVModel->CreateCurrentAttValues(), defs);
800 }
801 }
802
803 G4TrajectoriesModel* trajModel = dynamic_cast<G4TrajectoriesModel*>(fpModel);
804 if (trajModel) {
805 // Load G4Atts from trajectory...
806 const G4VTrajectory* traj = trajModel->GetCurrentTrajectory();
807 const std::map<G4String,G4AttDef>* defs = traj->GetAttDefs();
808 if (defs) {
809 holder->AddAtts(traj->CreateAttValues(), defs);
810 }
811 G4int nPoints = traj->GetPointEntries();
812 for (G4int i = 0; i < nPoints; ++i) {
813 G4VTrajectoryPoint* trajPoint = traj->GetPoint(i);
814 const std::map<G4String,G4AttDef>* defs = trajPoint->GetAttDefs();
815 if (defs) {
816 holder->AddAtts(trajPoint->CreateAttValues(), defs);
817 }
818 }
819 }
820
821 G4HitsModel* hitsModel = dynamic_cast<G4HitsModel*>(fpModel);
822 if (hitsModel) {
823 // Load G4Atts from hit...
824 const G4VHit* hit = hitsModel->GetCurrentHit();
825 const std::map<G4String,G4AttDef>* defs = hit->GetAttDefs();
826 if (defs) {
827 holder->AddAtts(hit->CreateAttValues(), defs);
828 }
829 }
830}
831
832const G4Colour& G4VSceneHandler::GetColour (const G4Visible& visible) {
833 // Colour is determined by the applicable vis attributes.
834 const G4Colour& colour = fpViewer ->
835 GetApplicableVisAttributes (visible.GetVisAttributes ()) -> GetColour ();
836 return colour;
837}
838
839const G4Colour& G4VSceneHandler::GetTextColour (const G4Text& text) {
840 const G4VisAttributes* pVA = text.GetVisAttributes ();
841 if (!pVA) {
842 pVA = fpViewer -> GetViewParameters (). GetDefaultTextVisAttributes ();
843 }
844 const G4Colour& colour = pVA -> GetColour ();
845 return colour;
846}
847
848G4double G4VSceneHandler::GetLineWidth(const G4VisAttributes* pVisAttribs)
849{
850 G4double lineWidth = pVisAttribs->GetLineWidth();
851 if (lineWidth < 1.) lineWidth = 1.;
852 lineWidth *= fpViewer -> GetViewParameters().GetGlobalLineWidthScale();
853 if (lineWidth < 1.) lineWidth = 1.;
854 return lineWidth;
855}
856
857G4ViewParameters::DrawingStyle G4VSceneHandler::GetDrawingStyle
858(const G4VisAttributes* pVisAttribs) {
859 // Drawing style is normally determined by the view parameters, but
860 // it can be overriddden by the ForceDrawingStyle flag in the vis
861 // attributes.
862 G4ViewParameters::DrawingStyle style =
863 fpViewer->GetViewParameters().GetDrawingStyle();
864 if (pVisAttribs -> IsForceDrawingStyle ()) {
865 G4VisAttributes::ForcedDrawingStyle forcedStyle =
866 pVisAttribs -> GetForcedDrawingStyle ();
867 // This is complicated because if hidden line and surface removal
868 // has been requested we wish to preserve this sometimes.
869 switch (forcedStyle) {
870 case (G4VisAttributes::solid):
871 switch (style) {
872 case (G4ViewParameters::hlr):
873 style = G4ViewParameters::hlhsr;
874 break;
875 case (G4ViewParameters::wireframe):
876 style = G4ViewParameters::hsr;
877 break;
878 case (G4ViewParameters::hlhsr):
879 case (G4ViewParameters::hsr):
880 default:
881 break;
882 }
883 break;
884 case (G4VisAttributes::wireframe):
885 default:
886 // But if forced style is wireframe, do it, because one of its
887 // main uses is in displaying the consituent solids of a Boolean
888 // solid and their surfaces overlap with the resulting Booean
889 // solid, making a mess if hlr is specified.
890 style = G4ViewParameters::wireframe;
891 break;
892 }
893 }
894 return style;
895}
896
897G4bool G4VSceneHandler::GetAuxEdgeVisible (const G4VisAttributes* pVisAttribs) {
898 G4bool isAuxEdgeVisible = fpViewer->GetViewParameters().IsAuxEdgeVisible ();
899 if (pVisAttribs -> IsForceAuxEdgeVisible()) isAuxEdgeVisible = true;
900 return isAuxEdgeVisible;
901}
902
903G4double G4VSceneHandler::GetMarkerSize
904(const G4VMarker& marker,
905 G4VSceneHandler::MarkerSizeType& markerSizeType)
906{
907 G4bool userSpecified = marker.GetWorldSize() || marker.GetScreenSize();
908 const G4VMarker& defaultMarker =
909 fpViewer -> GetViewParameters().GetDefaultMarker();
910 G4double size = userSpecified ?
911 marker.GetWorldSize() : defaultMarker.GetWorldSize();
912 if (size) {
913 // Draw in world coordinates.
914 markerSizeType = world;
915 }
916 else {
917 size = userSpecified ?
918 marker.GetScreenSize() : defaultMarker.GetScreenSize();
919 // Draw in screen coordinates.
920 markerSizeType = screen;
921 }
922 if (size <= 1.) size = 1.;
923 size *= fpViewer -> GetViewParameters().GetGlobalMarkerScale();
924 if (size <= 1.) size = 1.;
925 return size;
926}
927
928G4int G4VSceneHandler::GetNoOfSides(const G4VisAttributes* pVisAttribs)
929{
930 // No. of sides (lines segments per circle) is normally determined
931 // by the view parameters, but it can be overriddden by the
932 // ForceLineSegmentsPerCircle in the vis attributes.
933 G4int lineSegmentsPerCircle = fpViewer->GetViewParameters().GetNoOfSides();
934 if (pVisAttribs) {
935 if (pVisAttribs->IsForceLineSegmentsPerCircle())
936 lineSegmentsPerCircle = pVisAttribs->GetForcedLineSegmentsPerCircle();
937 const G4int nSegmentsMin = 12;
938 if (lineSegmentsPerCircle < nSegmentsMin) {
939 lineSegmentsPerCircle = nSegmentsMin;
940 G4cout <<
941 "G4VSceneHandler::GetNoOfSides: attempt to set the"
942 "\nnumber of line segements per circle < " << nSegmentsMin
943 << "; forced to " << lineSegmentsPerCircle << G4endl;
944 }
945 }
946 return lineSegmentsPerCircle;
947}
948
949std::ostream& operator << (std::ostream& os, const G4VSceneHandler& s) {
950
951 os << "Scene handler " << s.fName << " has "
952 << s.fViewerList.size () << " viewer(s):";
953 for (size_t i = 0; i < s.fViewerList.size (); i++) {
954 os << "\n " << *(s.fViewerList [i]);
955 }
956
957 if (s.fpScene) {
958 os << "\n " << *s.fpScene;
959 }
960 else {
961 os << "\n This scene handler currently has no scene.";
962 }
963
964 return os;
965}
Note: See TracBrowser for help on using the repository browser.