2 // Category: visualization
4 // According to visualization/management/include/MyVisManager.*
5 // John Allison 24th January 1998.
6 // I. Hrivnacova 12.5.98
8 // Renamed to TG4VisManager
9 // I. Hrivnacova, 3.9.99
11 // Added AliMC implementation
14 // Added OpenGL*Win32, RayTracer (as replacement of RayX) drivers
15 // based on G4 suggestions.
16 // I. Gonzalez, 4.4.2000
18 // Example Visualization Manager description:
19 // Implements virtual function RegisterGraphicsSystems.
20 // Exploits C-pre-processor variables G4VIS_USE_DAWN, etc.,
21 // which are set by the G4 makefiles if
22 // environment variables of the same name are set.
24 // So all you have to do is set environment variables and compile and
25 // instantiate this in your main().
27 // Alternatively, you can implement an empty function here and just
28 // register the systems you want in your main(), e.g.:
29 // G4VisManager* myVisManager = new MyVisManager;
30 // myVisManager -> RegisterGraphicsSystem (new MyGraphicsSystem);
35 #include "TG4VisManager.h"
36 #include "TG4Globals.h"
38 #include <G4LogicalVolumeStore.hh>
39 #include <G4PhysicalVolumeStore.hh>
40 #include <G4TransportationManager.hh>
41 #include <G4Material.hh>
42 #include <G4PhysicalVolumeModel.hh>
43 #include <G4VVisManager.hh>
45 // Supported drivers...
48 #include <G4FukuiRenderer.hh>
51 #ifdef G4VIS_USE_DAWNFILE
52 #include <G4DAWNFILE.hh>
55 #ifdef G4VIS_USE_OPACS
60 #ifdef G4VIS_USE_OPENGLX
61 #include <G4OpenGLImmediateX.hh>
62 #include <G4OpenGLStoredX.hh>
65 #ifdef G4VIS_USE_OPENGLXM
66 #include <G4OpenGLImmediateXm.hh>
67 #include <G4OpenGLStoredXm.hh>
70 #ifdef G4VIS_USE_OPENGLWIN32
71 #include "G4OpenGLImmediateWin32.hh"
72 #include "G4OpenGLStoredWin32.hh"
76 #include <G4OpenInventorX.hh>
79 #ifdef G4VIS_USE_OIWIN32
80 #include <G4OpenInventorWin32.hh>
83 #ifdef G4VIS_USE_RAYTRACER
84 #include "G4RayTracer.hh"
92 #ifdef G4VIS_USE_VRMLFILE
93 #include <G4VRML1File.hh>
94 #include <G4VRML2File.hh>
97 //_____________________________________________________________________________
98 TG4VisManager::TG4VisManager(G4int verboseLevel) {
100 fVerbose = verboseLevel;
104 //_____________________________________________________________________________
105 TG4VisManager::TG4VisManager(const TG4VisManager& right) {
107 TG4Globals::Exception(
108 "Attempt to copy TG4VisManager singleton.");
111 //_____________________________________________________________________________
112 TG4VisManager::~TG4VisManager() {
118 //_____________________________________________________________________________
119 TG4VisManager& TG4VisManager::operator=(const TG4VisManager& right)
121 // check assignement to self
122 if (this == &right) return *this;
124 TG4Globals::Exception(
125 "Attempt to assign TG4VisManager singleton.");
132 //_____________________________________________________________________________
133 void TG4VisManager::RegisterGraphicsSystems()
135 // Registers the graphics systems.
138 // all created graphics system instances are
139 // deleted in G4VisManager::~G4VisManager()
140 #ifdef G4VIS_USE_DAWN
141 RegisterGraphicsSystem(new G4FukuiRenderer);
144 #ifdef G4VIS_USE_DAWNFILE
145 RegisterGraphicsSystem(new G4DAWNFILE);
148 #ifdef G4VIS_USE_OPACS
149 RegisterGraphicsSystem(new G4Wo);
150 RegisterGraphicsSystem(new G4Xo);
153 #ifdef G4VIS_USE_OPENGLX
154 RegisterGraphicsSystem(new G4OpenGLImmediateX);
155 RegisterGraphicsSystem(new G4OpenGLStoredX);
158 #ifdef G4VIS_USE_OPENGLXM
159 RegisterGraphicsSystem(new G4OpenGLImmediateXm);
160 RegisterGraphicsSystem(new G4OpenGLStoredXm);
163 #ifdef G4VIS_USE_OPENGLWIN32
164 RegisterGraphicsSystem (new G4OpenGLImmediateWin32);
165 RegisterGraphicsSystem (new G4OpenGLStoredWin32);
169 RegisterGraphicsSystem(new G4OpenInventorX);
172 #ifdef G4VIS_USE_OIWIN32
173 RegisterGraphicsSystem(new G4OpenInventorWin32);
176 #ifdef G4VIS_USE_RAYTRACER
177 RegisterGraphicsSystem (new G4RayTracer);
180 #ifdef G4VIS_USE_VRML
181 RegisterGraphicsSystem(new G4VRML1);
182 RegisterGraphicsSystem(new G4VRML2);
185 #ifdef G4VIS_USE_VRMLFILE
186 RegisterGraphicsSystem(new G4VRML1File);
187 RegisterGraphicsSystem(new G4VRML2File);
192 "\nYou have successfully chosen to use the following graphics systems."
194 PrintAvailableGraphicsSystems();
198 //---------------------------------------------------------------
200 //---------------------------------------------------------------
203 //_____________________________________________________________________________
204 G4RWTPtrOrderedVector<G4LogicalVolume> TG4VisManager::GetLVList(G4String name)
206 // Get function returning the list of logical volumes
207 // associated to NAME; G4 built clones of a G3 volume (identified
208 // with NAME_NUMBER will be added to the list)
209 // NAME can be the name of a logical or physical volume
212 G4RWTPtrOrderedVector <G4LogicalVolume> lvList;
213 G4LogicalVolumeStore* pLVStore = G4LogicalVolumeStore::GetInstance();
214 G4LogicalVolume* pLV = 0;
217 for (G4int i=0; i<pLVStore->size(); i++)
219 pLV = (*pLVStore)[i];
220 if (CaseInsensitiveEqual(name,pLV->GetName()))
222 if (!lvList.contains(pLV)) lvList.append(pLV);
226 if (!lvList.isEmpty()) return lvList;
227 G4PhysicalVolumeStore* pPVStore = G4PhysicalVolumeStore::GetInstance();
228 G4VPhysicalVolume* pPV = 0;
231 for (G4int i=0; i<pPVStore->size(); i++)
233 pPV = (*pPVStore)[i];
234 if (CaseInsensitiveEqual(name,pPV->GetName()))
236 pLV = pPV->GetLogicalVolume();
237 if (!lvList.contains(pLV)) lvList.append(pLV);
245 //_____________________________________________________________________________
246 G4RWTPtrOrderedVector<G4VPhysicalVolume> TG4VisManager::GetPVList(G4String name)
248 // Get function returning the physical volume pointer for NAME
251 G4RWTPtrOrderedVector <G4VPhysicalVolume> pvList;
252 G4PhysicalVolumeStore* pPVStore = G4PhysicalVolumeStore::GetInstance();
255 TG4Globals::Warning("TG4VisManager::Gdraw : No volume store !");
258 G4VPhysicalVolume* pPV = 0;
259 for (G4int i=0; i<pPVStore->size(); i++)
261 pPV = (*pPVStore)[i];
262 if (CaseInsensitiveEqual(name,pPV->GetName()))
264 if (!pvList.contains(pPV)) pvList.append(pPV);
271 //_____________________________________________________________________________
272 G4bool TG4VisManager::CaseInsensitiveEqual(const G4String string1,
273 const G4String string2)
275 // Case insensitive comparison of 2 strings
279 if (string1 == string2) return true;
280 // if (string1.length() != string2.length()) return false;
281 for (i=0; i<string1.length(); i++)
283 G4int diff = abs((G4int)string1(i)-(G4int)string2(i));
284 if (diff && (diff!=32)) return false;
286 if (string2.length() > string1.length())
288 if (string2(i) == '_') return true;
295 //_____________________________________________________________________________
296 void TG4VisManager::SetAtt4Daughters(G4LogicalVolume* const lv,
297 const TG4G3Attribute att, const G4int val)
299 // Iterator for setting a visual attribute for all daughters
302 SetG4Attribute(lv,att,val);
304 G4String lvName = lv->GetName();
305 G4int nOfDaughters = lv->GetNoDaughters();
308 G4String previousName = "";
309 for (G4int i=0; i<nOfDaughters; i++)
311 G4LogicalVolume* lvd = lv->GetDaughter(i)->GetLogicalVolume();
312 G4String currentName = lvd->GetName();
313 if (currentName != lvName && currentName != previousName)
315 SetAtt4Daughters(lvd, att, val);
316 previousName = currentName;
323 //_____________________________________________________________________________
324 G4bool TG4VisManager::IsSharedVisAttributes(const G4LogicalVolume* pLV)
326 // Function seeking if the volume's visible attributes are shared with
330 G4LogicalVolumeStore* pLVStore = G4LogicalVolumeStore::GetInstance();
331 G4LogicalVolume* pLVCurrent = 0;
332 const G4VisAttributes* pVisAtt = pLV->GetVisAttributes();
333 if (!pVisAtt) return false;
334 for (G4int i=0; i<pLVStore->size(); i++)
336 pLVCurrent = (*pLVStore)[i];
337 if (pLVCurrent != pLV)
339 if (pLVCurrent->GetVisAttributes() == pVisAtt)
349 //_____________________________________________________________________________
350 void TG4VisManager::SetG4Attribute(G4LogicalVolume* const lv,
351 const TG4G3Attribute att, const G4int val)
353 // Set the G4 attribute fo volume LV accordingly to the G3 description
358 // Dupplicating old vis. attributes
359 const G4VisAttributes* visAttributes = lv->GetVisAttributes();
360 G4VisAttributes* newVisAttributes;
362 newVisAttributes = new G4VisAttributes(false);
364 G4bool visibility = visAttributes->IsVisible();
365 G4Colour colour = visAttributes->GetColour();
366 newVisAttributes = new G4VisAttributes(visibility, colour);
369 const G4int kAbsVal = abs(val); // the functionality is given by the abs value
371 // Default visible attributes
372 G4double red(0),green(0),blue(0); // default is black
373 G4bool isVisible(false);
374 G4bool isDaughtersInvisible(false);
375 G4VisAttributes::LineStyle lineStyle = G4VisAttributes::unbroken;
376 G4double lineWidth = 1.0;
377 G4bool isForceDrawingStyle(false);
378 G4VisAttributes::ForcedDrawingStyle drawingStyle = G4VisAttributes::wireframe;
380 // a 'hardcopy' of old vis attributes is needed because the copy constructor
381 // resets to defaults some of the data members of G4VisAttributes class
384 isVisible = visAttributes->IsVisible();
385 isDaughtersInvisible = visAttributes->IsDaughtersInvisible();
386 red = visAttributes->GetColour().GetRed();
387 green = visAttributes->GetColour().GetGreen();
388 blue = visAttributes->GetColour().GetBlue(); // old RGB components
389 lineStyle = visAttributes->GetLineStyle();
390 lineWidth = visAttributes->GetLineWidth();
391 isForceDrawingStyle = visAttributes->IsForceDrawingStyle();
392 if (isForceDrawingStyle)
393 drawingStyle = visAttributes->GetForcedDrawingStyle();
396 G4double luminosityBin(0.04), // bin for luminosity
397 luminosity(0); // colour luminosity
399 // Delete old vis. attributes if they are not shared
400 if (visAttributes && !IsSharedVisAttributes(lv)) delete visAttributes;
402 // Set the required attribute
428 lineStyle = G4VisAttributes::unbroken; break;
430 lineStyle = G4VisAttributes::dashed; break;
432 lineStyle = G4VisAttributes::dotted; break;
435 G4cout << "TG4VisManager::Gsatt() Usage of LSTY :" << G4endl
436 << "ATT = 1,2,3 means line unbroken, dashed or dotted" << G4endl
437 << "any other value resets to the default : unbroken" << G4endl;
438 lineStyle = G4VisAttributes::unbroken;
443 if (lineWidth > 7) lineWidth = 7;
445 G4cout << "TG4VisManager::Gsatt() Usage for LWID :" << G4endl
446 << " The VAL you supply means the width of lines in pixels "
447 << "for the screen and in 0.1*mm for paper." << G4endl
448 << " Negative values means the same, but for all daughters"
452 if (kAbsVal < 8) // G3 base colours
457 red=0; green=0; blue=0; //black
460 red=1; green=0; blue=0; //red
463 red=0; green=1; blue=0; //green
466 red=0; green=0; blue=1; //blue
469 red=1; green=1; blue=0; //yellow
472 red=1; green=0; blue=1; //violet
475 red=0; green=1; blue=1; //lightblue (almost !)
479 if (kAbsVal>=8 && kAbsVal<=16)
481 red=0; green=0; blue=0;
482 luminosity = (kAbsVal-7)*luminosityBin;
484 if (kAbsVal>=17 && kAbsVal<=41)
486 red=1; green=0; blue=0;
487 luminosity = (kAbsVal-16)*luminosityBin;
489 if (kAbsVal>=67 && kAbsVal<=91)
491 red=0; green=1; blue=0;
492 luminosity = (kAbsVal-66)*luminosityBin;
494 if (kAbsVal>=117 && kAbsVal<=141)
496 red=0; green=0; blue=1;
497 luminosity = (kAbsVal-116)*luminosityBin;
499 if (kAbsVal>=42 && kAbsVal<=66)
501 red=1; green=1; blue=0;
502 luminosity = (kAbsVal-41)*luminosityBin;
504 if (kAbsVal>=142 && kAbsVal<=166)
506 red=1; green=0; blue=1;
507 luminosity = (kAbsVal-141)*luminosityBin;
509 if (kAbsVal>=92 && kAbsVal<=116)
511 red=0; green=1; blue=1;
512 luminosity = (kAbsVal-91)*luminosityBin;
514 if (red < luminosityBin) red += luminosity;
515 if (green < luminosityBin) green += luminosity;
516 if (blue < luminosityBin) blue += luminosity;
519 isForceDrawingStyle = true;
523 drawingStyle = G4VisAttributes::wireframe;
526 drawingStyle = G4VisAttributes::solid;
530 G4cout << "TG4VisManager::Gsatt() FILL usage :" << G4endl
531 << " The FILL values you can supply are only :" << G4endl
532 << "+/- 1 : forces wireframe drawing (default)" << G4endl
533 << "+/- 2 : forces solid drawing" << G4endl
534 << "other values sets the drawing style to solid"
536 drawingStyle = G4VisAttributes::solid;
539 // Register vis. attributes
540 newVisAttributes->SetVisibility(isVisible);
541 newVisAttributes->SetDaughtersInvisible(isDaughtersInvisible);
542 newVisAttributes->SetColour(red,green,blue);
543 newVisAttributes->SetLineStyle(lineStyle);
544 newVisAttributes->SetLineWidth(lineWidth);
545 if (drawingStyle == G4VisAttributes::wireframe)
546 newVisAttributes->SetForceWireframe(isForceDrawingStyle);
547 if (drawingStyle == G4VisAttributes::solid)
548 newVisAttributes->SetForceSolid(isForceDrawingStyle);
550 lv->SetVisAttributes(newVisAttributes);
553 //-----------------------------------------------------------------
554 // functions for drawing
555 //-----------------------------------------------------------------
558 //_____________________________________________________________________________
559 void TG4VisManager::DrawOneSpec(const char* name)
561 // Function called when one double-clicks on a volume name
562 // in a TPaveLabel drawn by Gdtree
565 G4cout << "TG4VisManager::DrawOneSpec() Not yet implemented";
569 //_____________________________________________________________________________
570 void TG4VisManager::SetColors()
572 // Function for setting default volume colours
575 G4LogicalVolumeStore* pLVStore = G4LogicalVolumeStore::GetInstance();
576 const G4LogicalVolume* pLV = 0;
579 TG4Globals::Warning("TG4VisManager::SetColors : Ignored, geometry not built.");
582 // parse the LV tree and set colours according to material density
583 for (G4int i=0; i<pLVStore->size(); i++)
585 pLV = (*pLVStore)[i];
586 // G4cout << "VOLUME : " << pLV->GetName() << G4endl;
587 const G4Material* pMaterial = pLV->GetMaterial();
588 const G4State kState = pMaterial->GetState();
589 G4double density = (pMaterial->GetDensity())*cm3/g;
590 G4String nState = "Undefined";
591 G4int colour = 1; //black by default
592 G4double luminosity = 0.;
593 if (kState == kStateUndefined)
595 nState = "Undefined";
597 if (kState == kStateSolid)
603 luminosity = 25 - 25*density/2;
605 else if (density < 3)
608 luminosity = 25 - 25*(density-2);
610 else if (density < 10)
613 luminosity = 25 - 25*(density-5)/5;
615 else if (density < 15)
618 luminosity = 25 - 25*(density-10)/5;
620 else if (density < 20)
623 luminosity = 9 - 9*(density-15)/5;
626 if (kState == kStateLiquid)
629 colour = 142; //violet
630 luminosity = 25 - 25*density/2;
632 if (kState == kStateGas)
635 if (density < 0.001) {colour = 42;} //yellow
636 else if (density < 0.002) {colour = 27;} //light red
637 else if (density < 0.003) {colour = 77;} //light green
638 else {colour = 102;} //light cyan
641 if (luminosity < 0) luminosity=0;
642 colour += (G4int)luminosity;
643 // Setting the corresponding colour
644 Gsatt(pLV->GetName(),"COLO",colour);
649 //_____________________________________________________________________________
650 void TG4VisManager::Gsatt(const char* name, const char* att, Int_t val)
652 // Geant3 description :
655 // IOPT Name of the attribute to be set
656 // IVAL Value to which the attribute is to be set
658 // name= "*" stands for all the volumes.
659 // iopt can be chosen among the following :
660 // kWORK, kSEEN, kLSTY, kLWID, kCOLO, kFILL, kSET, kDET, kDTYP
664 G4LogicalVolume* lv = 0;
665 G4RWTPtrOrderedVector<G4LogicalVolume> lvList;
666 G4String sname(name),
669 // seek for known attributes
670 TG4G3Attribute attribute = kUNKNOWN;
671 if (CaseInsensitiveEqual(att,"WORK"))
673 G4String message = "TG4VisManager::Gsatt: G3Attribute ";
674 message += satt + " not used in G4";
675 TG4Globals::Warning(message);
678 if (CaseInsensitiveEqual(att,"SEEN")) attribute = kSEEN;
679 if (CaseInsensitiveEqual(att,"LSTY")) attribute = kLSTY;
680 if (CaseInsensitiveEqual(att,"LWID")) attribute = kLWID;
681 if (CaseInsensitiveEqual(att,"COLO")) attribute = kCOLO;
682 if (CaseInsensitiveEqual(att,"FILL")) attribute = kFILL;
683 if (CaseInsensitiveEqual(att,"SET"))
685 G4String message = "TG4VisManager::Gsatt: G3Attribute ";
686 message += satt + " not used in G4";
687 TG4Globals::Warning(message);
690 if (CaseInsensitiveEqual(att,"DET"))
692 G4String message = "TG4VisManager::Gsatt: G3Attribute ";
693 message += satt + " not used in G4";
694 TG4Globals::Warning(message);
697 if (CaseInsensitiveEqual(att,"DTYP"))
699 G4String message = "TG4VisManager::Gsatt: G3Attribute ";
700 message += satt + " not used in G4";
701 TG4Globals::Warning(message);
704 if (attribute == kUNKNOWN)
706 G4String message = "TG4VisManager::Gsatt: G3Attribute ";
707 message += satt + " unknown";
708 TG4Globals::Warning(message);
711 G4bool doForDaughters(false), // tree iterator flag
712 doForAll(false), // activated if NAME is "*"
713 topVisible(false); // activated for kSEEN/-2
714 if (sname == "*") doForAll = true;
715 if (val < 0 && sname!="*") doForDaughters = true;
716 if (attribute==kSEEN && val==-2) topVisible = true;
718 // parse all the tree
721 G4LogicalVolumeStore* pLVStore = G4LogicalVolumeStore::GetInstance();
722 for (G4int i=0; i<pLVStore->size(); i++)
725 SetG4Attribute(lv,attribute,ival);
730 // get the logical volume pointer corresponding to NAME
731 lvList = GetLVList(name);
732 if (lvList.isEmpty())
734 G4String message = "TG4VisManager::Gsatt(): Ignored\n";
735 message += " Logical volume " + sname + " has not been found.\n";
736 TG4Globals::Warning(message);
739 // set attribute for all descendents
742 for (G4int i=0; i<lvList.entries(); i++)
745 SetAtt4Daughters(lv,attribute,ival);
750 for (G4int i=0; i<lvList.entries(); i++)
753 SetG4Attribute(lv,attribute,ival);
758 for (G4int i=0; i<lvList.entries(); i++)
761 SetG4Attribute(lv,attribute,1);
767 //_____________________________________________________________________________
768 void TG4VisManager::Gdraw(const char *name,Float_t theta, Float_t phi, Float_t psi,
769 Float_t u0,Float_t v0,Float_t ul,Float_t vl)
771 // Draw the physical volume NAME and all descendents;
772 // Mandatory : the graphics system, scene and view must be
773 // initialized, e.g. "/vis~/create_view/new_graphics_system OGLSX";
774 // Any call of Gdraw() will use the current graphics system and
775 // the current window.
776 // The result will be a centered view drawing of the designated volume,
777 // lights moving with camera, viewpoint direction given by theta/phi
778 // and rotation on the screen given by psi;
779 // The u0, v0, ul, vl factors are ignored since the object will be
780 // automatically centered and will be confortable in the window
781 // at any viewing angle.
783 // check if G4 graphics is ready for drawing
786 G4VVisManager* pVVisManager = G4VVisManager::GetConcreteInstance();
789 "TG4VisManager::Gdraw: Ignored - No graphics driver is built. ");
792 if (NeedSetColours())
795 SetColourFlag(false);
798 const G4double kRad = M_PI/180.;
799 G4RWTPtrOrderedVector<G4VPhysicalVolume> pvList;
800 G4String sname(name);
801 G4bool successful = false;
803 pvList = GetPVList(name);
804 if (pvList.isEmpty())
806 G4String message = "TG4VisManager::Gdraw() :\n";
807 message += "Volume " + sname + " not found. Bailing out";
808 TG4Globals::Warning(message);
812 G4VPhysicalVolume *pPV = 0;
814 // clear the current scene if not empty
815 if (!fpScene->IsEmpty()) fpScene->Clear();
817 // create and add object's model list to the runtime-duration model
819 // (it is deleted in the VisManager destructor within
820 // all the RWTPtrOrderedVectors of the scene)
821 for (G4int i=0; i<pvList.entries(); i++)
824 G4LogicalVolume* pLV = pPV->GetLogicalVolume();
825 G4VSolid* pSolid = pLV->GetSolid();
826 successful = fpScene->AddRunDurationModel(new G4PhysicalVolumeModel(pPV));
829 G4String message = "TG4VisManager::Gdraw() Could not add ";
830 message += pPV->GetName() + " to the drawing list. Probably ";
831 message += "it is already in the list.";
832 TG4Globals::Warning(message);
835 // get the standard target point of the scene
836 const G4Point3D kTargetPoint = fpScene->GetStandardTargetPoint();
838 // set the viewpoint and the rotation on the screen
839 G4Vector3D viewpointDirection(sin(theta*kRad)*cos(phi*kRad),
840 sin(theta*kRad)*sin(phi*kRad), cos(theta*kRad));
841 G4Vector3D upVector(sin(psi*kRad), cos(psi*kRad),0);
843 // set and register view parameters to the viewer
845 fVP.SetLightsMoveWithCamera(true);
848 fVP.UnsetViewDigis();
849 fVP.SetNoOfSides(48);
850 fVP.SetCurrentTargetPoint(kTargetPoint);
851 fVP.SetViewpointDirection(viewpointDirection);
852 fVP.SetUpVector(upVector);
853 fVP.SetDensityCulling(true);
854 fpViewer->SetViewParameters(fVP);
858 fpSceneHandler->SetScene(fpScene);
859 fpSceneHandler->SetCurrentViewer(fpViewer);
860 fpViewer->DrawView();
861 fpViewer->ShowView();
863 else TG4Globals::Warning(
864 "TG4VisManager::Gdraw: Ignored - Failed to register volume");