source: trunk/geant4/visualization/management/src/G4VSceneHandler.cc @ 542

Last change on this file since 542 was 542, checked in by garnier, 17 years ago

r688@mac-90108: laurentgarnier | 2007-07-06 18:41:19 +0200
mise a jour de la visu OpenGl. Ne marche pas encore, en version lente

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