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);
33 #include "TG4Globals.h"
35 #include <G4LogicalVolumeStore.hh>
36 #include <G4PhysicalVolumeStore.hh>
37 #include <G4PhysicalVolumeModel.hh>
38 #include <G4LogicalVolumeModel.hh>
39 #include <G4TransportationManager.hh>
40 #include <G4Material.hh>
43 #include "TG4VisManager.h"
45 #include <G4VVisManager.hh>
47 // Supported drivers...
50 #include <G4FukuiRenderer.hh>
53 #ifdef G4VIS_USE_DAWNFILE
54 #include <G4DAWNFILE.hh>
57 #ifdef G4VIS_USE_OPACS
62 #ifdef G4VIS_USE_OPENGLX
63 #include <G4OpenGLImmediateX.hh>
64 #include <G4OpenGLStoredX.hh>
67 #ifdef G4VIS_USE_OPENGLXM
68 #include <G4OpenGLImmediateXm.hh>
69 #include <G4OpenGLStoredXm.hh>
72 #ifdef G4VIS_USE_OPENGLWIN32
73 #include "G4OpenGLImmediateWin32.hh"
74 #include "G4OpenGLStoredWin32.hh"
78 #include <G4OpenInventorX.hh>
81 #ifdef G4VIS_USE_OIWIN32
82 #include <G4OpenInventorWin32.hh>
85 #ifdef G4VIS_USE_RAYTRACER
86 #include "G4RayTracer.hh"
94 #ifdef G4VIS_USE_VRMLFILE
95 #include <G4VRML1File.hh>
96 #include <G4VRML2File.hh>
99 TG4VisManager::TG4VisManager(G4int verboseLevel) {
101 fVerbose = verboseLevel;
105 TG4VisManager::TG4VisManager(const TG4VisManager& right) {
107 TG4Globals::Exception(
108 "Attempt to copy TG4VisManager singleton.");
111 TG4VisManager::~TG4VisManager() {
117 TG4VisManager& TG4VisManager::operator=(const TG4VisManager& right)
119 // check assignement to self
120 if (this == &right) return *this;
122 TG4Globals::Exception(
123 "Attempt to assign TG4VisManager singleton.");
130 void TG4VisManager::RegisterGraphicsSystems()
132 // Registers the graphics systems.
135 // all created graphics system instances are
136 // deleted in G4VisManager::~G4VisManager()
137 #ifdef G4VIS_USE_DAWN
138 RegisterGraphicsSystem(new G4FukuiRenderer);
141 #ifdef G4VIS_USE_DAWNFILE
142 RegisterGraphicsSystem(new G4DAWNFILE);
145 #ifdef G4VIS_USE_OPACS
146 RegisterGraphicsSystem(new G4Wo);
147 RegisterGraphicsSystem(new G4Xo);
150 #ifdef G4VIS_USE_OPENGLX
151 RegisterGraphicsSystem(new G4OpenGLImmediateX);
152 RegisterGraphicsSystem(new G4OpenGLStoredX);
155 #ifdef G4VIS_USE_OPENGLXM
156 RegisterGraphicsSystem(new G4OpenGLImmediateXm);
157 RegisterGraphicsSystem(new G4OpenGLStoredXm);
160 #ifdef G4VIS_USE_OPENGLWIN32
161 RegisterGraphicsSystem (new G4OpenGLImmediateWin32);
162 RegisterGraphicsSystem (new G4OpenGLStoredWin32);
166 RegisterGraphicsSystem(new G4OpenInventorX);
169 #ifdef G4VIS_USE_OIWIN32
170 RegisterGraphicsSystem(new G4OpenInventorWin32);
173 #ifdef G4VIS_USE_RAYTRACER
174 RegisterGraphicsSystem (new G4RayTracer);
177 #ifdef G4VIS_USE_VRML
178 RegisterGraphicsSystem(new G4VRML1);
179 RegisterGraphicsSystem(new G4VRML2);
182 #ifdef G4VIS_USE_VRMLFILE
183 RegisterGraphicsSystem(new G4VRML1File);
184 RegisterGraphicsSystem(new G4VRML2File);
189 "\nYou have successfully chosen to use the following graphics systems."
191 PrintAvailableGraphicsSystems();
195 //---------------------------------------------------------------
197 //---------------------------------------------------------------
200 G4RWTPtrOrderedVector<G4LogicalVolume> TG4VisManager::GetLVList(G4String name)
202 // Get function returning the list of logical volumes
203 // associated to NAME; G4 built clones of a G3 volume (identified
204 // with NAME_NUMBER will be added to the list)
205 // NAME can be the name of a logical or physical volume
208 G4RWTPtrOrderedVector <G4LogicalVolume> lvList;
209 G4LogicalVolumeStore* pLVStore = G4LogicalVolumeStore::GetInstance();
210 G4LogicalVolume* pLV = 0;
213 for (G4int i=0; i<pLVStore->entries(); i++)
215 pLV = pLVStore->at(i);
216 if (CaseInsensitiveEqual(name,pLV->GetName()))
218 if (!lvList.contains(pLV)) lvList.append(pLV);
222 if (!lvList.isEmpty()) return lvList;
223 G4PhysicalVolumeStore* pPVStore = G4PhysicalVolumeStore::GetInstance();
224 G4VPhysicalVolume* pPV = 0;
227 for (G4int i=0; i<pPVStore->entries(); i++)
229 pPV = pPVStore->at(i);
230 if (CaseInsensitiveEqual(name,pPV->GetName()))
232 pLV = pPV->GetLogicalVolume();
233 if (!lvList.contains(pLV)) lvList.append(pLV);
241 G4RWTPtrOrderedVector<G4VPhysicalVolume> TG4VisManager::GetPVList(G4String name)
243 // Get function returning the physical volume pointer for NAME
246 G4RWTPtrOrderedVector <G4VPhysicalVolume> pvList;
247 G4PhysicalVolumeStore* pPVStore = G4PhysicalVolumeStore::GetInstance();
250 TG4Globals::Warning("TG4VisManager::Gdraw : No volume store !");
253 G4VPhysicalVolume* pPV = 0;
254 for (G4int i=0; i<pPVStore->entries(); i++)
256 pPV = pPVStore->at(i);
257 if (CaseInsensitiveEqual(name,pPV->GetName()))
259 if (!pvList.contains(pPV)) pvList.append(pPV);
266 G4bool TG4VisManager::CaseInsensitiveEqual(const G4String string1,
267 const G4String string2)
269 // Case insensitive comparison of 2 strings
273 if (string1 == string2) return true;
274 // if (string1.length() != string2.length()) return false;
275 for (i=0; i<string1.length(); i++)
277 G4int diff = abs((G4int)string1(i)-(G4int)string2(i));
278 if (diff && (diff!=32)) return false;
280 if (string2.length() > string1.length())
282 if (string2(i) == '_') return true;
289 void TG4VisManager::SetAtt4Daughters(G4LogicalVolume* const lv,
290 const TG3Attribute att, const G4int val)
292 // Iterator for setting a visual attribute for all daughters
295 SetG4Attribute(lv,att,val);
297 G4String lvName = lv->GetName();
298 G4int nOfDaughters = lv->GetNoDaughters();
301 G4String previousName = "";
302 for (G4int i=0; i<nOfDaughters; i++)
304 G4LogicalVolume* lvd = lv->GetDaughter(i)->GetLogicalVolume();
305 G4String currentName = lvd->GetName();
306 if (currentName != lvName && currentName != previousName)
308 SetAtt4Daughters(lvd, att, val);
309 previousName = currentName;
316 G4bool TG4VisManager::IsSharedVisAttributes(const G4LogicalVolume* pLV)
318 // Function seeking if the volume's visible attributes are shared with
322 G4LogicalVolumeStore* pLVStore = G4LogicalVolumeStore::GetInstance();
323 G4LogicalVolume* pLVCurrent = 0;
324 const G4VisAttributes* pVisAtt = pLV->GetVisAttributes();
325 if (!pVisAtt) return false;
326 for (G4int i=0; i<pLVStore->entries(); i++)
328 pLVCurrent = pLVStore->at(i);
329 if (pLVCurrent != pLV)
331 if (pLVCurrent->GetVisAttributes() == pVisAtt)
341 void TG4VisManager::SetG4Attribute(G4LogicalVolume* const lv,
342 const TG3Attribute att, const G4int val)
344 // Set the G4 attribute fo volume LV accordingly to the G3 description
349 // Dupplicating old vis. attributes
350 const G4VisAttributes* visAttributes = lv->GetVisAttributes();
351 G4VisAttributes* newVisAttributes;
353 newVisAttributes = new G4VisAttributes(false);
355 newVisAttributes = new G4VisAttributes(visAttributes);
357 const G4int absVal = abs(val); // the functionality is given by the abs value
359 // Default visible attributes
360 G4double red(0),green(0),blue(0); // default is black
361 G4bool isVisible(false);
362 G4bool isDaughtersInvisible(false);
363 G4VisAttributes::LineStyle lineStyle = G4VisAttributes::unbroken;
364 G4double lineWidth = 1.0;
365 G4bool isForceDrawingStyle(false);
366 G4VisAttributes::ForcedDrawingStyle drawingStyle = G4VisAttributes::wireframe;
368 // a 'hardcopy' of old vis attributes is needed because the copy constructor
369 // resets to defaults some of the data members of G4VisAttributes class
372 isVisible = visAttributes->IsVisible();
373 isDaughtersInvisible = visAttributes->IsDaughtersInvisible();
374 red = visAttributes->GetColour().GetRed();
375 green = visAttributes->GetColour().GetGreen();
376 blue = visAttributes->GetColour().GetBlue(); // old RGB components
377 lineStyle = visAttributes->GetLineStyle();
378 lineWidth = visAttributes->GetLineWidth();
379 isForceDrawingStyle = visAttributes->IsForceDrawingStyle();
380 if (isForceDrawingStyle)
381 drawingStyle = visAttributes->GetForcedDrawingStyle();
384 G4double luminosityBin(0.04), // bin for luminosity
385 luminosity(0); // colour luminosity
387 // Delete old vis. attributes if they are not shared
388 if (visAttributes && !IsSharedVisAttributes(lv)) delete visAttributes;
390 // Set the required attribute
416 lineStyle = G4VisAttributes::unbroken; break;
418 lineStyle = G4VisAttributes::dashed; break;
420 lineStyle = G4VisAttributes::dotted; break;
423 G4cout << "TG4VisManager::Gsatt() Usage of LSTY :" << endl
424 << "ATT = 1,2,3 means line unbroken, dashed or dotted" << endl
425 << "any other value resets to the default : unbroken" << endl;
426 lineStyle = G4VisAttributes::unbroken;
431 if (lineWidth > 7) lineWidth = 7;
433 G4cout << "TG4VisManager::Gsatt() Usage for LWID :" << endl
434 << " The VAL you supply means the width of lines in pixels "
435 << "for the screen and in 0.1*mm for paper." << endl
436 << " Negative values means the same, but for all daughters"
440 if (absVal < 8) // G3 base colours
445 red=0; green=0; blue=0; //black
448 red=1; green=0; blue=0; //red
451 red=0; green=1; blue=0; //green
454 red=0; green=0; blue=1; //blue
457 red=1; green=1; blue=0; //yellow
460 red=1; green=0; blue=1; //violet
463 red=0; green=1; blue=1; //lightblue (almost !)
467 if (absVal>=8 && absVal<=16)
469 red=0; green=0; blue=0;
470 luminosity = (absVal-7)*luminosityBin;
472 if (absVal>=17 && absVal<=41)
474 red=1; green=0; blue=0;
475 luminosity = (absVal-16)*luminosityBin;
477 if (absVal>=67 && absVal<=91)
479 red=0; green=1; blue=0;
480 luminosity = (absVal-66)*luminosityBin;
482 if (absVal>=117 && absVal<=141)
484 red=0; green=0; blue=1;
485 luminosity = (absVal-116)*luminosityBin;
487 if (absVal>=42 && absVal<=66)
489 red=1; green=1; blue=0;
490 luminosity = (absVal-41)*luminosityBin;
492 if (absVal>=142 && absVal<=166)
494 red=1; green=0; blue=1;
495 luminosity = (absVal-141)*luminosityBin;
497 if (absVal>=92 && absVal<=116)
499 red=0; green=1; blue=1;
500 luminosity = (absVal-91)*luminosityBin;
502 if (red < luminosityBin) red += luminosity;
503 if (green < luminosityBin) green += luminosity;
504 if (blue < luminosityBin) blue += luminosity;
507 isForceDrawingStyle = true;
511 drawingStyle = G4VisAttributes::wireframe;
514 drawingStyle = G4VisAttributes::solid;
518 G4cout << "TG4VisManager::Gsatt() FILL usage :" << endl
519 << " The FILL values you can supply are only :" << endl
520 << "+/- 1 : forces wireframe drawing (default)" << endl
521 << "+/- 2 : forces solid drawing" << endl
522 << "other values sets the drawing style to solid"
524 drawingStyle = G4VisAttributes::solid;
527 // Register vis. attributes
528 newVisAttributes->SetVisibility(isVisible);
529 newVisAttributes->SetDaughtersInvisible(isDaughtersInvisible);
530 newVisAttributes->SetColour(red,green,blue);
531 newVisAttributes->SetLineStyle(lineStyle);
532 newVisAttributes->SetLineWidth(lineWidth);
533 if (drawingStyle == G4VisAttributes::wireframe)
534 newVisAttributes->SetForceWireframe(isForceDrawingStyle);
535 if (drawingStyle == G4VisAttributes::solid)
536 newVisAttributes->SetForceSolid(isForceDrawingStyle);
538 lv->SetVisAttributes(newVisAttributes);
541 //-----------------------------------------------------------------
542 // functions for drawing
543 //-----------------------------------------------------------------
545 void TG4VisManager::DrawOneSpec(const char* name)
547 // Function called when one double-clicks on a volume name
548 // in a TPaveLabel drawn by Gdtree
551 G4cout << "TG4VisManager::DrawOneSpec() Not yet implemented";
555 void TG4VisManager::SetColors()
557 // Function for setting default volume colours
560 G4LogicalVolumeStore* pLVStore = G4LogicalVolumeStore::GetInstance();
561 const G4LogicalVolume* pLV = 0;
564 TG4Globals::Warning("TG4VisManager::SetColors : Ignored, geometry not built.");
567 // parse the LV tree and set colours according to material density
568 for (G4int i=0; i<pLVStore->entries(); i++)
570 pLV = pLVStore->at(i);
571 // G4cout << "VOLUME : " << pLV->GetName() << endl;
572 const G4Material* pMaterial = pLV->GetMaterial();
573 const G4State state = pMaterial->GetState();
574 G4double density = (pMaterial->GetDensity())*cm3/g;
575 G4String nState = "Undefined";
576 G4int colour = 1; //black by default
577 G4double luminosity = 0.;
578 if (state == kStateUndefined)
580 nState = "Undefined";
582 if (state == kStateSolid)
588 luminosity = 25 - 25*density/2;
590 else if (density < 3)
593 luminosity = 25 - 25*(density-2);
595 else if (density < 10)
598 luminosity = 25 - 25*(density-5)/5;
600 else if (density < 15)
603 luminosity = 25 - 25*(density-10)/5;
605 else if (density < 20)
608 luminosity = 9 - 9*(density-15)/5;
611 if (state == kStateLiquid)
614 colour = 142; //violet
615 luminosity = 25 - 25*density/2;
617 if (state == kStateGas)
620 if (density < 0.001) {colour = 42;} //yellow
621 else if (density < 0.002) {colour = 27;} //light red
622 else if (density < 0.003) {colour = 77;} //light green
623 else {colour = 102;} //light cyan
626 if (luminosity < 0) luminosity=0;
627 colour += (G4int)luminosity;
628 // Setting the corresponding colour
629 Gsatt(pLV->GetName(),"COLO",colour);
634 void TG4VisManager::Gsatt(const char* name, const char* att, Int_t val)
636 // Geant3 description :
639 // IOPT Name of the attribute to be set
640 // IVAL Value to which the attribute is to be set
642 // name= "*" stands for all the volumes.
643 // iopt can be chosen among the following :
644 // kWORK, kSEEN, kLSTY, kLWID, kCOLO, kFILL, kSET, kDET, kDTYP
648 G4LogicalVolume* lv = 0;
649 G4RWTPtrOrderedVector<G4LogicalVolume> lvList;
650 G4String sname(name),
653 // seek for known attributes
654 TG3Attribute attribute = kUNKNOWN;
655 if (CaseInsensitiveEqual(att,"WORK"))
657 G4String message = "TG4VisManager::Gsatt: G3Attribute ";
658 message += satt + " not used in G4";
659 TG4Globals::Warning(message);
662 if (CaseInsensitiveEqual(att,"SEEN")) attribute = kSEEN;
663 if (CaseInsensitiveEqual(att,"LSTY")) attribute = kLSTY;
664 if (CaseInsensitiveEqual(att,"LWID")) attribute = kLWID;
665 if (CaseInsensitiveEqual(att,"COLO")) attribute = kCOLO;
666 if (CaseInsensitiveEqual(att,"FILL")) attribute = kFILL;
667 if (CaseInsensitiveEqual(att,"SET"))
669 G4String message = "TG4VisManager::Gsatt: G3Attribute ";
670 message += satt + " not used in G4";
671 TG4Globals::Warning(message);
674 if (CaseInsensitiveEqual(att,"DET"))
676 G4String message = "TG4VisManager::Gsatt: G3Attribute ";
677 message += satt + " not used in G4";
678 TG4Globals::Warning(message);
681 if (CaseInsensitiveEqual(att,"DTYP"))
683 G4String message = "TG4VisManager::Gsatt: G3Attribute ";
684 message += satt + " not used in G4";
685 TG4Globals::Warning(message);
688 if (attribute == kUNKNOWN)
690 G4String message = "TG4VisManager::Gsatt: G3Attribute ";
691 message += satt + " unknown";
692 TG4Globals::Warning(message);
695 G4bool doForDaughters(false), // tree iterator flag
696 doForAll(false), // activated if NAME is "*"
697 topVisible(false); // activated for kSEEN/-2
698 if (sname == "*") doForAll = true;
699 if (val < 0 && sname!="*") doForDaughters = true;
700 if (attribute==kSEEN && val==-2) topVisible = true;
702 // parse all the tree
705 G4LogicalVolumeStore* pLVStore = G4LogicalVolumeStore::GetInstance();
706 for (G4int i=0; i<pLVStore->entries(); i++)
708 lv = pLVStore->at(i);
709 SetG4Attribute(lv,attribute,ival);
714 // get the logical volume pointer corresponding to NAME
715 lvList = GetLVList(name);
716 if (lvList.isEmpty())
718 G4String message = "TG4VisManager::Gsatt(): Ignored\n";
719 message += " Logical volume " + sname + " has not been found.\n";
720 TG4Globals::Warning(message);
723 // set attribute for all descendents
726 for (G4int i=0; i<lvList.entries(); i++)
729 SetAtt4Daughters(lv,attribute,ival);
734 for (G4int i=0; i<lvList.entries(); i++)
737 SetG4Attribute(lv,attribute,ival);
742 for (G4int i=0; i<lvList.entries(); i++)
745 SetG4Attribute(lv,attribute,1);
751 void TG4VisManager::Gdraw(const char *name,Float_t theta, Float_t phi, Float_t psi,
752 Float_t u0,Float_t v0,Float_t ul,Float_t vl)
754 // Draw the physical volume NAME and all descendents;
755 // Mandatory : the graphics system, scene and view must be
756 // initialized, e.g. "/vis~/create_view/new_graphics_system OGLSX";
757 // Any call of Gdraw() will use the current graphics system and
758 // the current window.
759 // The result will be a centered view drawing of the designated volume,
760 // lights moving with camera, viewpoint direction given by theta/phi
761 // and rotation on the screen given by psi;
762 // The u0, v0, ul, vl factors are ignored since the object will be
763 // automatically centered and will be confortable in the window
764 // at any viewing angle.
766 // check if G4 graphics is ready for drawing
769 G4VVisManager* pVVisManager = G4VVisManager::GetConcreteInstance();
772 "TG4VisManager::Gdraw: Ignored - No graphics driver is built. ");
775 if (NeedSetColours())
778 SetColourFlag(false);
781 const G4double rad = M_PI/180.;
782 G4RWTPtrOrderedVector<G4VPhysicalVolume> pvList;
783 G4String sname(name);
784 G4bool successful = false;
786 pvList = GetPVList(name);
787 if (pvList.isEmpty())
789 G4String message = "TG4VisManager::Gdraw() :\n";
790 message += "Volume " + sname + " not found. Bailing out";
791 TG4Globals::Warning(message);
795 G4VPhysicalVolume *pPV = 0;
797 // clear the current scene if not empty
798 if (!fpScene->IsEmpty()) fpScene->Clear();
800 // create and add object's model list to the runtime-duration model
802 // (it is deleted in the VisManager destructor within
803 // all the RWTPtrOrderedVectors of the scene)
804 for (G4int i=0; i<pvList.entries(); i++)
807 G4LogicalVolume* pLV = pPV->GetLogicalVolume();
808 G4VSolid* pSolid = pLV->GetSolid();
810 successful = fpScene->AddRunDurationModel(new G4PhysicalVolumeModel(pPV));
813 G4String message = "TG4VisManager::Gdraw() Could not add ";
814 message += pPV->GetName() + " to the drawing list. Probably ";
815 message += "it is already in the list.";
816 TG4Globals::Warning(message);
819 // get the standard target point of the scene
820 const G4Point3D targetPoint = fpScene->GetStandardTargetPoint();
822 // set the viewpoint and the rotation on the screen
823 G4Vector3D viewpointDirection(sin(theta*rad)*cos(phi*rad),
824 sin(theta*rad)*sin(phi*rad), cos(theta*rad));
825 G4Vector3D upVector(sin(psi*rad), cos(psi*rad),0);
827 // set and register view parameters to the viewer
829 fVP.SetLightsMoveWithCamera(true);
832 fVP.UnsetViewDigis();
833 fVP.SetNoOfSides(48);
834 fVP.SetCurrentTargetPoint(targetPoint);
835 fVP.SetViewpointDirection(viewpointDirection);
836 fVP.SetUpVector(upVector);
837 fVP.SetDensityCulling(true);
838 fpViewer->SetViewParameters(fVP);
842 fpSceneHandler->SetScene(fpScene);
843 fpSceneHandler->SetCurrentViewer(fpViewer);
844 fpViewer->DrawView();
845 fpViewer->ShowView();
847 else TG4Globals::Warning(
848 "TG4VisManager::Gdraw: Ignored - Failed to register volume");