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 TG4VisManager::TG4VisManager(G4int verboseLevel) {
99 fVerbose = verboseLevel;
103 TG4VisManager::TG4VisManager(const TG4VisManager& right) {
105 TG4Globals::Exception(
106 "Attempt to copy TG4VisManager singleton.");
109 TG4VisManager::~TG4VisManager() {
115 TG4VisManager& TG4VisManager::operator=(const TG4VisManager& right)
117 // check assignement to self
118 if (this == &right) return *this;
120 TG4Globals::Exception(
121 "Attempt to assign TG4VisManager singleton.");
128 void TG4VisManager::RegisterGraphicsSystems()
130 // Registers the graphics systems.
133 // all created graphics system instances are
134 // deleted in G4VisManager::~G4VisManager()
135 #ifdef G4VIS_USE_DAWN
136 RegisterGraphicsSystem(new G4FukuiRenderer);
139 #ifdef G4VIS_USE_DAWNFILE
140 RegisterGraphicsSystem(new G4DAWNFILE);
143 #ifdef G4VIS_USE_OPACS
144 RegisterGraphicsSystem(new G4Wo);
145 RegisterGraphicsSystem(new G4Xo);
148 #ifdef G4VIS_USE_OPENGLX
149 RegisterGraphicsSystem(new G4OpenGLImmediateX);
150 RegisterGraphicsSystem(new G4OpenGLStoredX);
153 #ifdef G4VIS_USE_OPENGLXM
154 RegisterGraphicsSystem(new G4OpenGLImmediateXm);
155 RegisterGraphicsSystem(new G4OpenGLStoredXm);
158 #ifdef G4VIS_USE_OPENGLWIN32
159 RegisterGraphicsSystem (new G4OpenGLImmediateWin32);
160 RegisterGraphicsSystem (new G4OpenGLStoredWin32);
164 RegisterGraphicsSystem(new G4OpenInventorX);
167 #ifdef G4VIS_USE_OIWIN32
168 RegisterGraphicsSystem(new G4OpenInventorWin32);
171 #ifdef G4VIS_USE_RAYTRACER
172 RegisterGraphicsSystem (new G4RayTracer);
175 #ifdef G4VIS_USE_VRML
176 RegisterGraphicsSystem(new G4VRML1);
177 RegisterGraphicsSystem(new G4VRML2);
180 #ifdef G4VIS_USE_VRMLFILE
181 RegisterGraphicsSystem(new G4VRML1File);
182 RegisterGraphicsSystem(new G4VRML2File);
187 "\nYou have successfully chosen to use the following graphics systems."
189 PrintAvailableGraphicsSystems();
193 //---------------------------------------------------------------
195 //---------------------------------------------------------------
198 G4RWTPtrOrderedVector<G4LogicalVolume> TG4VisManager::GetLVList(G4String name)
200 // Get function returning the list of logical volumes
201 // associated to NAME; G4 built clones of a G3 volume (identified
202 // with NAME_NUMBER will be added to the list)
203 // NAME can be the name of a logical or physical volume
206 G4RWTPtrOrderedVector <G4LogicalVolume> lvList;
207 G4LogicalVolumeStore* pLVStore = G4LogicalVolumeStore::GetInstance();
208 G4LogicalVolume* pLV = 0;
211 for (G4int i=0; i<pLVStore->entries(); i++)
213 pLV = pLVStore->at(i);
214 if (CaseInsensitiveEqual(name,pLV->GetName()))
216 if (!lvList.contains(pLV)) lvList.append(pLV);
220 if (!lvList.isEmpty()) return lvList;
221 G4PhysicalVolumeStore* pPVStore = G4PhysicalVolumeStore::GetInstance();
222 G4VPhysicalVolume* pPV = 0;
225 for (G4int i=0; i<pPVStore->entries(); i++)
227 pPV = pPVStore->at(i);
228 if (CaseInsensitiveEqual(name,pPV->GetName()))
230 pLV = pPV->GetLogicalVolume();
231 if (!lvList.contains(pLV)) lvList.append(pLV);
239 G4RWTPtrOrderedVector<G4VPhysicalVolume> TG4VisManager::GetPVList(G4String name)
241 // Get function returning the physical volume pointer for NAME
244 G4RWTPtrOrderedVector <G4VPhysicalVolume> pvList;
245 G4PhysicalVolumeStore* pPVStore = G4PhysicalVolumeStore::GetInstance();
248 TG4Globals::Warning("TG4VisManager::Gdraw : No volume store !");
251 G4VPhysicalVolume* pPV = 0;
252 for (G4int i=0; i<pPVStore->entries(); i++)
254 pPV = pPVStore->at(i);
255 if (CaseInsensitiveEqual(name,pPV->GetName()))
257 if (!pvList.contains(pPV)) pvList.append(pPV);
264 G4bool TG4VisManager::CaseInsensitiveEqual(const G4String string1,
265 const G4String string2)
267 // Case insensitive comparison of 2 strings
271 if (string1 == string2) return true;
272 // if (string1.length() != string2.length()) return false;
273 for (i=0; i<string1.length(); i++)
275 G4int diff = abs((G4int)string1(i)-(G4int)string2(i));
276 if (diff && (diff!=32)) return false;
278 if (string2.length() > string1.length())
280 if (string2(i) == '_') return true;
287 void TG4VisManager::SetAtt4Daughters(G4LogicalVolume* const lv,
288 const TG3Attribute att, const G4int val)
290 // Iterator for setting a visual attribute for all daughters
293 SetG4Attribute(lv,att,val);
295 G4String lvName = lv->GetName();
296 G4int nOfDaughters = lv->GetNoDaughters();
299 G4String previousName = "";
300 for (G4int i=0; i<nOfDaughters; i++)
302 G4LogicalVolume* lvd = lv->GetDaughter(i)->GetLogicalVolume();
303 G4String currentName = lvd->GetName();
304 if (currentName != lvName && currentName != previousName)
306 SetAtt4Daughters(lvd, att, val);
307 previousName = currentName;
314 G4bool TG4VisManager::IsSharedVisAttributes(const G4LogicalVolume* pLV)
316 // Function seeking if the volume's visible attributes are shared with
320 G4LogicalVolumeStore* pLVStore = G4LogicalVolumeStore::GetInstance();
321 G4LogicalVolume* pLVCurrent = 0;
322 const G4VisAttributes* pVisAtt = pLV->GetVisAttributes();
323 if (!pVisAtt) return false;
324 for (G4int i=0; i<pLVStore->entries(); i++)
326 pLVCurrent = pLVStore->at(i);
327 if (pLVCurrent != pLV)
329 if (pLVCurrent->GetVisAttributes() == pVisAtt)
339 void TG4VisManager::SetG4Attribute(G4LogicalVolume* const lv,
340 const TG3Attribute att, const G4int val)
342 // Set the G4 attribute fo volume LV accordingly to the G3 description
347 // Dupplicating old vis. attributes
348 const G4VisAttributes* visAttributes = lv->GetVisAttributes();
349 G4VisAttributes* newVisAttributes;
351 newVisAttributes = new G4VisAttributes(false);
353 G4bool visibility = visAttributes->IsVisible();
354 G4Colour colour = visAttributes->GetColour();
355 newVisAttributes = new G4VisAttributes(visibility, colour);
358 const G4int kAbsVal = abs(val); // the functionality is given by the abs value
360 // Default visible attributes
361 G4double red(0),green(0),blue(0); // default is black
362 G4bool isVisible(false);
363 G4bool isDaughtersInvisible(false);
364 G4VisAttributes::LineStyle lineStyle = G4VisAttributes::unbroken;
365 G4double lineWidth = 1.0;
366 G4bool isForceDrawingStyle(false);
367 G4VisAttributes::ForcedDrawingStyle drawingStyle = G4VisAttributes::wireframe;
369 // a 'hardcopy' of old vis attributes is needed because the copy constructor
370 // resets to defaults some of the data members of G4VisAttributes class
373 isVisible = visAttributes->IsVisible();
374 isDaughtersInvisible = visAttributes->IsDaughtersInvisible();
375 red = visAttributes->GetColour().GetRed();
376 green = visAttributes->GetColour().GetGreen();
377 blue = visAttributes->GetColour().GetBlue(); // old RGB components
378 lineStyle = visAttributes->GetLineStyle();
379 lineWidth = visAttributes->GetLineWidth();
380 isForceDrawingStyle = visAttributes->IsForceDrawingStyle();
381 if (isForceDrawingStyle)
382 drawingStyle = visAttributes->GetForcedDrawingStyle();
385 G4double luminosityBin(0.04), // bin for luminosity
386 luminosity(0); // colour luminosity
388 // Delete old vis. attributes if they are not shared
389 if (visAttributes && !IsSharedVisAttributes(lv)) delete visAttributes;
391 // Set the required attribute
417 lineStyle = G4VisAttributes::unbroken; break;
419 lineStyle = G4VisAttributes::dashed; break;
421 lineStyle = G4VisAttributes::dotted; break;
424 G4cout << "TG4VisManager::Gsatt() Usage of LSTY :" << G4endl
425 << "ATT = 1,2,3 means line unbroken, dashed or dotted" << G4endl
426 << "any other value resets to the default : unbroken" << G4endl;
427 lineStyle = G4VisAttributes::unbroken;
432 if (lineWidth > 7) lineWidth = 7;
434 G4cout << "TG4VisManager::Gsatt() Usage for LWID :" << G4endl
435 << " The VAL you supply means the width of lines in pixels "
436 << "for the screen and in 0.1*mm for paper." << G4endl
437 << " Negative values means the same, but for all daughters"
441 if (kAbsVal < 8) // G3 base colours
446 red=0; green=0; blue=0; //black
449 red=1; green=0; blue=0; //red
452 red=0; green=1; blue=0; //green
455 red=0; green=0; blue=1; //blue
458 red=1; green=1; blue=0; //yellow
461 red=1; green=0; blue=1; //violet
464 red=0; green=1; blue=1; //lightblue (almost !)
468 if (kAbsVal>=8 && kAbsVal<=16)
470 red=0; green=0; blue=0;
471 luminosity = (kAbsVal-7)*luminosityBin;
473 if (kAbsVal>=17 && kAbsVal<=41)
475 red=1; green=0; blue=0;
476 luminosity = (kAbsVal-16)*luminosityBin;
478 if (kAbsVal>=67 && kAbsVal<=91)
480 red=0; green=1; blue=0;
481 luminosity = (kAbsVal-66)*luminosityBin;
483 if (kAbsVal>=117 && kAbsVal<=141)
485 red=0; green=0; blue=1;
486 luminosity = (kAbsVal-116)*luminosityBin;
488 if (kAbsVal>=42 && kAbsVal<=66)
490 red=1; green=1; blue=0;
491 luminosity = (kAbsVal-41)*luminosityBin;
493 if (kAbsVal>=142 && kAbsVal<=166)
495 red=1; green=0; blue=1;
496 luminosity = (kAbsVal-141)*luminosityBin;
498 if (kAbsVal>=92 && kAbsVal<=116)
500 red=0; green=1; blue=1;
501 luminosity = (kAbsVal-91)*luminosityBin;
503 if (red < luminosityBin) red += luminosity;
504 if (green < luminosityBin) green += luminosity;
505 if (blue < luminosityBin) blue += luminosity;
508 isForceDrawingStyle = true;
512 drawingStyle = G4VisAttributes::wireframe;
515 drawingStyle = G4VisAttributes::solid;
519 G4cout << "TG4VisManager::Gsatt() FILL usage :" << G4endl
520 << " The FILL values you can supply are only :" << G4endl
521 << "+/- 1 : forces wireframe drawing (default)" << G4endl
522 << "+/- 2 : forces solid drawing" << G4endl
523 << "other values sets the drawing style to solid"
525 drawingStyle = G4VisAttributes::solid;
528 // Register vis. attributes
529 newVisAttributes->SetVisibility(isVisible);
530 newVisAttributes->SetDaughtersInvisible(isDaughtersInvisible);
531 newVisAttributes->SetColour(red,green,blue);
532 newVisAttributes->SetLineStyle(lineStyle);
533 newVisAttributes->SetLineWidth(lineWidth);
534 if (drawingStyle == G4VisAttributes::wireframe)
535 newVisAttributes->SetForceWireframe(isForceDrawingStyle);
536 if (drawingStyle == G4VisAttributes::solid)
537 newVisAttributes->SetForceSolid(isForceDrawingStyle);
539 lv->SetVisAttributes(newVisAttributes);
542 //-----------------------------------------------------------------
543 // functions for drawing
544 //-----------------------------------------------------------------
546 void TG4VisManager::DrawOneSpec(const char* name)
548 // Function called when one double-clicks on a volume name
549 // in a TPaveLabel drawn by Gdtree
552 G4cout << "TG4VisManager::DrawOneSpec() Not yet implemented";
556 void TG4VisManager::SetColors()
558 // Function for setting default volume colours
561 G4LogicalVolumeStore* pLVStore = G4LogicalVolumeStore::GetInstance();
562 const G4LogicalVolume* pLV = 0;
565 TG4Globals::Warning("TG4VisManager::SetColors : Ignored, geometry not built.");
568 // parse the LV tree and set colours according to material density
569 for (G4int i=0; i<pLVStore->entries(); i++)
571 pLV = pLVStore->at(i);
572 // G4cout << "VOLUME : " << pLV->GetName() << G4endl;
573 const G4Material* pMaterial = pLV->GetMaterial();
574 const G4State kState = pMaterial->GetState();
575 G4double density = (pMaterial->GetDensity())*cm3/g;
576 G4String nState = "Undefined";
577 G4int colour = 1; //black by default
578 G4double luminosity = 0.;
579 if (kState == kStateUndefined)
581 nState = "Undefined";
583 if (kState == kStateSolid)
589 luminosity = 25 - 25*density/2;
591 else if (density < 3)
594 luminosity = 25 - 25*(density-2);
596 else if (density < 10)
599 luminosity = 25 - 25*(density-5)/5;
601 else if (density < 15)
604 luminosity = 25 - 25*(density-10)/5;
606 else if (density < 20)
609 luminosity = 9 - 9*(density-15)/5;
612 if (kState == kStateLiquid)
615 colour = 142; //violet
616 luminosity = 25 - 25*density/2;
618 if (kState == kStateGas)
621 if (density < 0.001) {colour = 42;} //yellow
622 else if (density < 0.002) {colour = 27;} //light red
623 else if (density < 0.003) {colour = 77;} //light green
624 else {colour = 102;} //light cyan
627 if (luminosity < 0) luminosity=0;
628 colour += (G4int)luminosity;
629 // Setting the corresponding colour
630 Gsatt(pLV->GetName(),"COLO",colour);
635 void TG4VisManager::Gsatt(const char* name, const char* att, Int_t val)
637 // Geant3 description :
640 // IOPT Name of the attribute to be set
641 // IVAL Value to which the attribute is to be set
643 // name= "*" stands for all the volumes.
644 // iopt can be chosen among the following :
645 // kWORK, kSEEN, kLSTY, kLWID, kCOLO, kFILL, kSET, kDET, kDTYP
649 G4LogicalVolume* lv = 0;
650 G4RWTPtrOrderedVector<G4LogicalVolume> lvList;
651 G4String sname(name),
654 // seek for known attributes
655 TG3Attribute attribute = kUNKNOWN;
656 if (CaseInsensitiveEqual(att,"WORK"))
658 G4String message = "TG4VisManager::Gsatt: G3Attribute ";
659 message += satt + " not used in G4";
660 TG4Globals::Warning(message);
663 if (CaseInsensitiveEqual(att,"SEEN")) attribute = kSEEN;
664 if (CaseInsensitiveEqual(att,"LSTY")) attribute = kLSTY;
665 if (CaseInsensitiveEqual(att,"LWID")) attribute = kLWID;
666 if (CaseInsensitiveEqual(att,"COLO")) attribute = kCOLO;
667 if (CaseInsensitiveEqual(att,"FILL")) attribute = kFILL;
668 if (CaseInsensitiveEqual(att,"SET"))
670 G4String message = "TG4VisManager::Gsatt: G3Attribute ";
671 message += satt + " not used in G4";
672 TG4Globals::Warning(message);
675 if (CaseInsensitiveEqual(att,"DET"))
677 G4String message = "TG4VisManager::Gsatt: G3Attribute ";
678 message += satt + " not used in G4";
679 TG4Globals::Warning(message);
682 if (CaseInsensitiveEqual(att,"DTYP"))
684 G4String message = "TG4VisManager::Gsatt: G3Attribute ";
685 message += satt + " not used in G4";
686 TG4Globals::Warning(message);
689 if (attribute == kUNKNOWN)
691 G4String message = "TG4VisManager::Gsatt: G3Attribute ";
692 message += satt + " unknown";
693 TG4Globals::Warning(message);
696 G4bool doForDaughters(false), // tree iterator flag
697 doForAll(false), // activated if NAME is "*"
698 topVisible(false); // activated for kSEEN/-2
699 if (sname == "*") doForAll = true;
700 if (val < 0 && sname!="*") doForDaughters = true;
701 if (attribute==kSEEN && val==-2) topVisible = true;
703 // parse all the tree
706 G4LogicalVolumeStore* pLVStore = G4LogicalVolumeStore::GetInstance();
707 for (G4int i=0; i<pLVStore->entries(); i++)
709 lv = pLVStore->at(i);
710 SetG4Attribute(lv,attribute,ival);
715 // get the logical volume pointer corresponding to NAME
716 lvList = GetLVList(name);
717 if (lvList.isEmpty())
719 G4String message = "TG4VisManager::Gsatt(): Ignored\n";
720 message += " Logical volume " + sname + " has not been found.\n";
721 TG4Globals::Warning(message);
724 // set attribute for all descendents
727 for (G4int i=0; i<lvList.entries(); i++)
730 SetAtt4Daughters(lv,attribute,ival);
735 for (G4int i=0; i<lvList.entries(); i++)
738 SetG4Attribute(lv,attribute,ival);
743 for (G4int i=0; i<lvList.entries(); i++)
746 SetG4Attribute(lv,attribute,1);
752 void TG4VisManager::Gdraw(const char *name,Float_t theta, Float_t phi, Float_t psi,
753 Float_t u0,Float_t v0,Float_t ul,Float_t vl)
755 // Draw the physical volume NAME and all descendents;
756 // Mandatory : the graphics system, scene and view must be
757 // initialized, e.g. "/vis~/create_view/new_graphics_system OGLSX";
758 // Any call of Gdraw() will use the current graphics system and
759 // the current window.
760 // The result will be a centered view drawing of the designated volume,
761 // lights moving with camera, viewpoint direction given by theta/phi
762 // and rotation on the screen given by psi;
763 // The u0, v0, ul, vl factors are ignored since the object will be
764 // automatically centered and will be confortable in the window
765 // at any viewing angle.
767 // check if G4 graphics is ready for drawing
770 G4VVisManager* pVVisManager = G4VVisManager::GetConcreteInstance();
773 "TG4VisManager::Gdraw: Ignored - No graphics driver is built. ");
776 if (NeedSetColours())
779 SetColourFlag(false);
782 const G4double kRad = M_PI/180.;
783 G4RWTPtrOrderedVector<G4VPhysicalVolume> pvList;
784 G4String sname(name);
785 G4bool successful = false;
787 pvList = GetPVList(name);
788 if (pvList.isEmpty())
790 G4String message = "TG4VisManager::Gdraw() :\n";
791 message += "Volume " + sname + " not found. Bailing out";
792 TG4Globals::Warning(message);
796 G4VPhysicalVolume *pPV = 0;
798 // clear the current scene if not empty
799 if (!fpScene->IsEmpty()) fpScene->Clear();
801 // create and add object's model list to the runtime-duration model
803 // (it is deleted in the VisManager destructor within
804 // all the RWTPtrOrderedVectors of the scene)
805 for (G4int i=0; i<pvList.entries(); i++)
808 G4LogicalVolume* pLV = pPV->GetLogicalVolume();
809 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 kTargetPoint = fpScene->GetStandardTargetPoint();
822 // set the viewpoint and the rotation on the screen
823 G4Vector3D viewpointDirection(sin(theta*kRad)*cos(phi*kRad),
824 sin(theta*kRad)*sin(phi*kRad), cos(theta*kRad));
825 G4Vector3D upVector(sin(psi*kRad), cos(psi*kRad),0);
827 // set and register view parameters to the viewer
829 fVP.SetLightsMoveWithCamera(true);
832 fVP.UnsetViewDigis();
833 fVP.SetNoOfSides(48);
834 fVP.SetCurrentTargetPoint(kTargetPoint);
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");