Logo ROOT   6.14/05
Reference Guide
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 
9 void glViewerLOD(Int_t reqNodes = 1000, Bool_t randomDist = kTRUE,
10  Bool_t reqSpheres = kTRUE, Bool_t reqTubes = kTRUE)
11 {
12  TGeoManager * geom = new TGeoManager("LODTest", "GL viewer LOD test");
13  geom->SetNsegments(4); // Doesn't matter keep low
14  TGeoMaterial *matEmptySpace = new TGeoMaterial("EmptySpace", 0, 0, 0);
15  TGeoMaterial *matSolid = new TGeoMaterial("Solid" , .938, 1., 10000.);
16 
17  TGeoMedium *medEmptySpace = new TGeoMedium("Empty", 1, matEmptySpace);
18  TGeoMedium *medSolid = new TGeoMedium("Solid", 1, matSolid);
19 
20  Double_t sizeBase = 20.0;
21  Double_t worldRadius;
22  if (randomDist) {
23  worldRadius = pow(reqNodes,.5)*sizeBase;
24  } else {
25  worldRadius = pow(reqNodes,.3)*sizeBase;
26  }
27 
28  TGeoVolume *top = geom->MakeBox
29  ("WORLD", medEmptySpace, worldRadius, worldRadius, worldRadius);
30  geom->SetTopVolume(top);
31 
32  gRandom->SetSeed();
33 
34  // Create random number of unique sphere shapes - up to 25% of
35  // total placed sphere requested
36  UInt_t volumeCount = gRandom->Integer(reqNodes/4)+1;
37  TGeoVolume ** volumes = new TGeoVolume *[volumeCount];
38  TGeoVolume * volume;
39  UInt_t i;
41 
42  for (i = 0; i < volumeCount; i++) {
43  char name[128];
44  sprintf(name, "Volume_%d", i);
45 
46  // Random volume shape
47  Int_t type = -1;
48  if (reqSpheres && reqTubes) {
49  type = gRandom->Integer(2);
50  if (type == 1)
51  type += gRandom->Integer(3);
52  }
53  else if(reqSpheres)
54  type = 0;
55  else if(reqTubes)
56  type = 1 + gRandom->Integer(3);
57 
58  // Random dimensions
59  Double_t rMin = gRandom->Rndm() * sizeBase;
60  Double_t rMax = rMin + gRandom->Rndm() * sizeBase * 2.0;
61  Double_t dz = pow(gRandom->Rndm(),2.0) * sizeBase * 15.0;
62  Double_t phi1 = gRandom->Rndm() * 90.0;
63  Double_t phi2 = phi1 + gRandom->Rndm() * 270.0;
64 
65  // Pick random color (not black)
66  Int_t color = gRandom->Integer(50);
67  if (color == kBlack) color += 1;
68 
69  switch (type) {
70  case 0: {
71  // GL viewer only supports solid spheres (0. inner radius)
72  volumes[i] = geom->MakeSphere(name, medSolid, 0., rMax);
73  printf("Volume %d : Color %d, Sphere, Radius %f\n", i, color, rMax);
74  break;
75  }
76  case 1: {
77  volumes[i] = geom->MakeTube(name, medSolid, rMin, rMax, dz);
78  printf("Volume %d : Color %d, Tube, Inner Radius %f, "
79  "Outer Radius %f, Length %f\n",
80  i, color, rMin, rMax, dz);
81  break;
82  }
83  case 2: {
84  volumes[i] = geom->MakeTubs(name, medSolid, rMin, rMax, dz,
85  phi1, phi2);
86  printf("Volume %d : Color %d, Tube Seg, Inner Radius %f, "
87  "Outer Radius %f, Length %f, Phi1 %f, Phi2 %f\n",
88  i, color, rMin, rMax, dz, phi1, phi2);
89  break;
90  }
91  case 3: {
92  Double_t n1[3], n2[3];
93  n1[0] = gRandom->Rndm()*.5;
94  n1[1] = gRandom->Rndm()*.5; n1[2] = -1.0 + gRandom->Rndm()*.5;
95  n2[0] = gRandom->Rndm()*.5;
96  n2[1] = gRandom->Rndm()*.5; n2[2] = 1.0 - gRandom->Rndm()*.5;
97 
98  volumes[i] = geom->MakeCtub(name, medSolid, rMin, rMax, dz,
99  phi1, phi2, n1[0], n1[1], n1[2],
100  n2[0], n2[1], n2[2]);
101  printf("Volume %d : Color %d, Cut Tube, Inner Radius %f, "
102  "Outer Radius %f, Length %f, Phi1 %f, Phi2 %f, "
103  "n1 (%f,%f,%f), n2 (%f,%f,%f)\n",
104  i, color, rMin, rMax, dz, phi1, phi2,
105  n1[0], n1[1], n1[2], n2[0], n2[1], n2[2]);
106  break;
107  }
108  default: {
109  assert(kFALSE);
110  }
111  }
112 
113  volumes[i]->SetLineColor(color);
114  }
115 
116  printf("\nCreated %d volumes\n\n", volumeCount);
117 
118  // Scatter reqSpheres placed sphere randomly in space
119  Double_t x, y, z;
120  for (i = 0; i < reqNodes; i++) {
121  // Pick random volume
122  UInt_t useVolume = gRandom->Integer(volumeCount);
123 
124  TGeoTranslation * trans;
125  TGeoRotation * rot;
126  if (randomDist) {
127  // Random translation
128  gRandom->Rannor(x, y);
129  gRandom->Rannor(z,dummy);
130  trans = new TGeoTranslation(x*worldRadius, y*worldRadius, z*worldRadius);
131 
132  // Random rotation
133  gRandom->Rannor(x, y);
134  gRandom->Rannor(z,dummy);
135  rot = new TGeoRotation("rot", x*360.0, y*360.0, z*360.0);
136  } else {
137  UInt_t perSide = pow(reqNodes,1.0/3.0)+0.5;
138  Double_t distance = sizeBase*5.0;
139  UInt_t xi, yi, zi;
140  zi = i / (perSide*perSide);
141  yi = (i / perSide) % perSide;
142  xi = i % perSide;
143  trans = new TGeoTranslation(xi*distance,yi*distance,zi*distance);
144  rot = new TGeoRotation("rot",0.0, 0.0, 0.0);
145  }
146  top->AddNode(volumes[useVolume], i, new TGeoCombiTrans(*trans, *rot));
147  //printf("Added node %d (Volume %d)\n", i, useVolume);
148  }
149  geom->CloseGeometry();
150  top->Draw("ogl");
151 }
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:481
The manager class for any TGeo geometry.
Definition: TGeoManager.h:38
Definition: Rtypes.h:58
TGeoVolume, TGeoVolumeMulti, TGeoVolumeAssembly are the volume classes.
Definition: TGeoVolume.h:48
virtual void Draw(Option_t *option="")
draw top volume according to option
Class describing translations.
Definition: TGeoMatrix.h:121
void SetTopVolume(TGeoVolume *vol)
Set the top volume and corresponding node as starting point of the geometry.
Base class describing materials.
Definition: TGeoMaterial.h:29
int Int_t
Definition: RtypesCore.h:41
bool Bool_t
Definition: RtypesCore.h:59
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.
Double_t x[n]
Definition: legend1.C:17
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.
virtual void SetSeed(ULong_t seed=0)
Set the random generator seed.
Definition: TRandom.cxx:589
double pow(double, double)
virtual UInt_t Integer(UInt_t imax)
Returns a random integer on [ 0, imax-1 ].
Definition: TRandom.cxx:341
virtual void AddNode(TGeoVolume *vol, Int_t copy_no, TGeoMatrix *mat=0, Option_t *option="")
Add a TGeoNode to the list of nodes.
Definition: TGeoVolume.cxx:984
virtual Double_t Rndm()
Machine independent random number generator.
Definition: TRandom.cxx:533
Class describing rotation + translation.
Definition: TGeoMatrix.h:291
unsigned int UInt_t
Definition: RtypesCore.h:42
void CloseGeometry(Option_t *option="d")
Closing geometry implies checking the geometry validity, fixing shapes with negative parameters (run-...
R__EXTERN TRandom * gRandom
Definition: TRandom.h:62
Class describing rotations.
Definition: TGeoMatrix.h:174
const Bool_t kFALSE
Definition: RtypesCore.h:88
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.
double Double_t
Definition: RtypesCore.h:55
int type
Definition: TGX11.cxx:120
static RooMathCoreReg dummy
Double_t y[n]
Definition: legend1.C:17
Media are used to store properties related to tracking and which are useful only when using geometry ...
Definition: TGeoMedium.h:23
virtual void SetLineColor(Color_t lcolor)
Set the line color.
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.
you should not use this method at all Int_t Int_t z
Definition: TRolke.cxx:630
void SetNsegments(Int_t nseg)
Set number of segments for approximating circles in drawing.
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.
const Bool_t kTRUE
Definition: RtypesCore.h:87
char name[80]
Definition: TGX11.cxx:109