Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
glViewerLOD.C
Go to the documentation of this file.
1/// \file
2/// \ingroup tutorial_gl
3/// To set the Level Of Details when rendering geometry shapes.
4///
5/// \macro_code
6///
7/// \author Richard Maunder
8
9void glViewerLOD(Int_t reqNodes = 1000, Bool_t randomDist = kTRUE, Bool_t reqSpheres = kTRUE, Bool_t reqTubes = kTRUE)
10{
11 TGeoManager *geom = new TGeoManager("LODTest", "GL viewer LOD test");
12 geom->SetNsegments(4); // Doesn't matter keep low
13 TGeoMaterial *matEmptySpace = new TGeoMaterial("EmptySpace", 0, 0, 0);
14 TGeoMaterial *matSolid = new TGeoMaterial("Solid", .938, 1., 10000.);
15
16 TGeoMedium *medEmptySpace = new TGeoMedium("Empty", 1, matEmptySpace);
17 TGeoMedium *medSolid = new TGeoMedium("Solid", 1, matSolid);
18
19 Double_t sizeBase = 20.0;
20 Double_t worldRadius;
21 if (randomDist) {
22 worldRadius = pow(reqNodes, .5) * sizeBase;
23 } else {
24 worldRadius = pow(reqNodes, .3) * sizeBase;
25 }
26
27 TGeoVolume *top = geom->MakeBox("WORLD", medEmptySpace, worldRadius, worldRadius, worldRadius);
28 geom->SetTopVolume(top);
29
31
32 // Create random number of unique sphere shapes - up to 25% of
33 // total placed sphere requested
34 UInt_t volumeCount = gRandom->Integer(reqNodes / 4) + 1;
35 TGeoVolume **volumes = new TGeoVolume *[volumeCount];
36 TGeoVolume *volume;
37 UInt_t i;
38 Double_t dummy;
39
40 for (i = 0; i < volumeCount; i++) {
41 char name[128];
42 sprintf(name, "Volume_%d", i);
43
44 // Random volume shape
45 Int_t type = -1;
46 if (reqSpheres && reqTubes) {
47 type = gRandom->Integer(2);
48 if (type == 1)
49 type += gRandom->Integer(3);
50 } else if (reqSpheres)
51 type = 0;
52 else if (reqTubes)
53 type = 1 + gRandom->Integer(3);
54
55 // Random dimensions
56 Double_t rMin = gRandom->Rndm() * sizeBase;
57 Double_t rMax = rMin + gRandom->Rndm() * sizeBase * 2.0;
58 Double_t dz = pow(gRandom->Rndm(), 2.0) * sizeBase * 15.0;
59 Double_t phi1 = gRandom->Rndm() * 90.0;
60 Double_t phi2 = phi1 + gRandom->Rndm() * 270.0;
61
62 // Pick random color (not black)
63 Int_t color = gRandom->Integer(50);
64 if (color == kBlack)
65 color += 1;
66
67 switch (type) {
68 case 0: {
69 // GL viewer only supports solid spheres (0. inner radius)
70 volumes[i] = geom->MakeSphere(name, medSolid, 0., rMax);
71 printf("Volume %d : Color %d, Sphere, Radius %f\n", i, color, rMax);
72 break;
73 }
74 case 1: {
75 volumes[i] = geom->MakeTube(name, medSolid, rMin, rMax, dz);
76 printf("Volume %d : Color %d, Tube, Inner Radius %f, "
77 "Outer Radius %f, Length %f\n",
78 i, color, rMin, rMax, dz);
79 break;
80 }
81 case 2: {
82 volumes[i] = geom->MakeTubs(name, medSolid, rMin, rMax, dz, phi1, phi2);
83 printf("Volume %d : Color %d, Tube Seg, Inner Radius %f, "
84 "Outer Radius %f, Length %f, Phi1 %f, Phi2 %f\n",
85 i, color, rMin, rMax, dz, phi1, phi2);
86 break;
87 }
88 case 3: {
89 Double_t n1[3], n2[3];
90 n1[0] = gRandom->Rndm() * .5;
91 n1[1] = gRandom->Rndm() * .5;
92 n1[2] = -1.0 + gRandom->Rndm() * .5;
93 n2[0] = gRandom->Rndm() * .5;
94 n2[1] = gRandom->Rndm() * .5;
95 n2[2] = 1.0 - gRandom->Rndm() * .5;
96
97 volumes[i] =
98 geom->MakeCtub(name, medSolid, rMin, rMax, dz, phi1, phi2, n1[0], n1[1], n1[2], n2[0], n2[1], n2[2]);
99 printf("Volume %d : Color %d, Cut Tube, Inner Radius %f, "
100 "Outer Radius %f, Length %f, Phi1 %f, Phi2 %f, "
101 "n1 (%f,%f,%f), n2 (%f,%f,%f)\n",
102 i, color, rMin, rMax, dz, phi1, phi2, n1[0], n1[1], n1[2], n2[0], n2[1], n2[2]);
103 break;
104 }
105 default: {
106 assert(kFALSE);
107 }
108 }
109
110 volumes[i]->SetLineColor(color);
111 }
112
113 printf("\nCreated %d volumes\n\n", volumeCount);
114
115 // Scatter reqSpheres placed sphere randomly in space
116 Double_t x, y, z;
117 for (i = 0; i < reqNodes; i++) {
118 // Pick random volume
119 UInt_t useVolume = gRandom->Integer(volumeCount);
120
121 TGeoTranslation *trans;
122 TGeoRotation *rot;
123 if (randomDist) {
124 // Random translation
125 gRandom->Rannor(x, y);
126 gRandom->Rannor(z, dummy);
127 trans = new TGeoTranslation(x * worldRadius, y * worldRadius, z * worldRadius);
128
129 // Random rotation
130 gRandom->Rannor(x, y);
131 gRandom->Rannor(z, dummy);
132 rot = new TGeoRotation("rot", x * 360.0, y * 360.0, z * 360.0);
133 } else {
134 UInt_t perSide = pow(reqNodes, 1.0 / 3.0) + 0.5;
135 Double_t distance = sizeBase * 5.0;
136 UInt_t xi, yi, zi;
137 zi = i / (perSide * perSide);
138 yi = (i / perSide) % perSide;
139 xi = i % perSide;
140 trans = new TGeoTranslation(xi * distance, yi * distance, zi * distance);
141 rot = new TGeoRotation("rot", 0.0, 0.0, 0.0);
142 }
143 top->AddNode(volumes[useVolume], i, new TGeoCombiTrans(*trans, *rot));
144 // printf("Added node %d (Volume %d)\n", i, useVolume);
145 }
146 geom->CloseGeometry();
147 top->Draw("ogl");
148}
bool Bool_t
Definition RtypesCore.h:63
int Int_t
Definition RtypesCore.h:45
unsigned int UInt_t
Definition RtypesCore.h:46
constexpr Bool_t kFALSE
Definition RtypesCore.h:94
double Double_t
Definition RtypesCore.h:59
constexpr Bool_t kTRUE
Definition RtypesCore.h:93
@ kBlack
Definition Rtypes.h:65
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t Int_t Int_t Window_t TString Int_t GCValues_t GetPrimarySelectionOwner GetDisplay GetScreen GetColormap GetNativeEvent const char const char dpyName wid window const char font_name cursor keysym reg const char only_if_exist regb h Point_t winding char text const char depth char const char Int_t count const char ColorStruct_t color const char Pixmap_t Pixmap_t PictureAttributes_t attr const char char ret_data h unsigned char height h Atom_t Int_t ULong_t ULong_t unsigned char prop_list Atom_t Atom_t Atom_t Time_t type
char name[80]
Definition TGX11.cxx:110
R__EXTERN TRandom * gRandom
Definition TRandom.h:62
Class describing rotation + translation.
Definition TGeoMatrix.h:317
The manager class for any TGeo geometry.
Definition TGeoManager.h:44
TGeoVolume * MakeTube(const char *name, TGeoMedium *medium, Double_t rmin, Double_t rmax, Double_t dz)
Make in one step a volume pointing to a tube shape with given medium.
void CloseGeometry(Option_t *option="d")
Closing geometry implies checking the geometry validity, fixing shapes with negative parameters (run-...
TGeoVolume * MakeBox(const char *name, TGeoMedium *medium, Double_t dx, Double_t dy, Double_t dz)
Make in one step a volume pointing to a box shape with given medium.
TGeoVolume * MakeSphere(const char *name, TGeoMedium *medium, Double_t rmin, Double_t rmax, Double_t themin=0, Double_t themax=180, Double_t phimin=0, Double_t phimax=360)
Make in one step a volume pointing to a sphere shape with given medium.
void SetTopVolume(TGeoVolume *vol)
Set the top volume and corresponding node as starting point of the geometry.
TGeoVolume * MakeCtub(const char *name, TGeoMedium *medium, Double_t rmin, Double_t rmax, Double_t dz, Double_t phi1, Double_t phi2, Double_t lx, Double_t ly, Double_t lz, Double_t tx, Double_t ty, Double_t tz)
Make in one step a volume pointing to a tube segment shape with given medium.
void SetNsegments(Int_t nseg)
Set number of segments for approximating circles in drawing.
TGeoVolume * MakeTubs(const char *name, TGeoMedium *medium, Double_t rmin, Double_t rmax, Double_t dz, Double_t phi1, Double_t phi2)
Make in one step a volume pointing to a tube segment shape with given medium.
Base class describing materials.
Media are used to store properties related to tracking and which are useful only when using geometry ...
Definition TGeoMedium.h:23
Class describing rotations.
Definition TGeoMatrix.h:168
Class describing translations.
Definition TGeoMatrix.h:116
TGeoVolume, TGeoVolumeMulti, TGeoVolumeAssembly are the volume classes.
Definition TGeoVolume.h:43
virtual TGeoNode * AddNode(TGeoVolume *vol, Int_t copy_no, TGeoMatrix *mat=nullptr, Option_t *option="")
Add a TGeoNode to the list of nodes.
void Draw(Option_t *option="") override
draw top volume according to option
void SetLineColor(Color_t lcolor) override
Set the line color.
virtual void SetSeed(ULong_t seed=0)
Set the random generator seed.
Definition TRandom.cxx:615
Double_t Rndm() override
Machine independent random number generator.
Definition TRandom.cxx:559
virtual void Rannor(Float_t &a, Float_t &b)
Return 2 numbers distributed following a gaussian with mean=0 and sigma=1.
Definition TRandom.cxx:507
virtual UInt_t Integer(UInt_t imax)
Returns a random integer uniformly distributed on the interval [ 0, imax-1 ].
Definition TRandom.cxx:361
RVec< PromoteTypes< T0, T1 > > pow(const T0 &x, const RVec< T1 > &v)
Definition RVec.hxx:1846
Double_t y[n]
Definition legend1.C:17
Double_t x[n]
Definition legend1.C:17