ROOT  6.07/01
Reference Guide
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
glViewerLOD.C
Go to the documentation of this file.
1 //To set the Level Of Details when rendering geometry shapes
2 //Author: Richard Maunder
3 
4 void glViewerLOD(Int_t reqNodes = 1000, Bool_t randomDist = kTRUE,
5  Bool_t reqSpheres = kTRUE, Bool_t reqTubes = kTRUE)
6 {
7  TGeoManager * geom = new TGeoManager("LODTest", "GL viewer LOD test");
8  geom->SetNsegments(4); // Doesn't matter keep low
9  TGeoMaterial *matEmptySpace = new TGeoMaterial("EmptySpace", 0, 0, 0);
10  TGeoMaterial *matSolid = new TGeoMaterial("Solid" , .938, 1., 10000.);
11 
12  TGeoMedium *medEmptySpace = new TGeoMedium("Empty", 1, matEmptySpace);
13  TGeoMedium *medSolid = new TGeoMedium("Solid", 1, matSolid);
14 
15  Double_t sizeBase = 20.0;
16  Double_t worldRadius;
17  if (randomDist) {
18  worldRadius = pow(reqNodes,.5)*sizeBase;
19  } else {
20  worldRadius = pow(reqNodes,.3)*sizeBase;
21  }
22 
23  TGeoVolume *top = geom->MakeBox
24  ("WORLD", medEmptySpace, worldRadius, worldRadius, worldRadius);
25  geom->SetTopVolume(top);
26 
27  gRandom->SetSeed();
28 
29  // Create random number of unique sphere shapes - up to 25% of
30  // total placed sphere requested
31  UInt_t volumeCount = gRandom->Integer(reqNodes/4)+1;
32  TGeoVolume ** volumes = new TGeoVolume *[volumeCount];
33  TGeoVolume * volume;
34 
36 
37  for (UInt_t i = 0; i < volumeCount; i++) {
38  char name[128];
39  sprintf(name, "Volume_%d", i);
40 
41  // Random volume shape
42  Int_t type = -1;
43  if (reqSpheres && reqTubes) {
44  type = gRandom->Integer(2);
45  if (type == 1)
46  type += gRandom->Integer(3);
47  }
48  else if(reqSpheres)
49  type = 0;
50  else if(reqTubes)
51  type = 1 + gRandom->Integer(3);
52 
53  // Random dimensions
54  Double_t rMin = gRandom->Rndm() * sizeBase;
55  Double_t rMax = rMin + gRandom->Rndm() * sizeBase * 2.0;
56  Double_t dz = pow(gRandom->Rndm(),2.0) * sizeBase * 15.0;
57  Double_t phi1 = gRandom->Rndm() * 90.0;
58  Double_t phi2 = phi1 + gRandom->Rndm() * 270.0;
59 
60  // Pick random color (not black)
61  Int_t color = gRandom->Integer(50);
62  if (color == kBlack) color += 1;
63 
64  switch (type) {
65  case 0: {
66  // GL viewer only supports solid spheres (0. inner radius)
67  volumes[i] = geom->MakeSphere(name, medSolid, 0., rMax);
68  printf("Volume %d : Color %d, Sphere, Radius %f\n", i, color, rMax);
69  break;
70  }
71  case 1: {
72  volumes[i] = geom->MakeTube(name, medSolid, rMin, rMax, dz);
73  printf("Volume %d : Color %d, Tube, Inner Radius %f, "
74  "Outer Radius %f, Length %f\n",
75  i, color, rMin, rMax, dz);
76  break;
77  }
78  case 2: {
79  volumes[i] = geom->MakeTubs(name, medSolid, rMin, rMax, dz,
80  phi1, phi2);
81  printf("Volume %d : Color %d, Tube Seg, Inner Radius %f, "
82  "Outer Radius %f, Length %f, Phi1 %f, Phi2 %f\n",
83  i, color, rMin, rMax, dz, phi1, phi2);
84  break;
85  }
86  case 3: {
87  Double_t n1[3], n2[3];
88  n1[0] = gRandom->Rndm()*.5;
89  n1[1] = gRandom->Rndm()*.5; n1[2] = -1.0 + gRandom->Rndm()*.5;
90  n2[0] = gRandom->Rndm()*.5;
91  n2[1] = gRandom->Rndm()*.5; n2[2] = 1.0 - gRandom->Rndm()*.5;
92 
93  volumes[i] = geom->MakeCtub(name, medSolid, rMin, rMax, dz,
94  phi1, phi2, n1[0], n1[1], n1[2],
95  n2[0], n2[1], n2[2]);
96  printf("Volume %d : Color %d, Cut Tube, Inner Radius %f, "
97  "Outer Radius %f, Length %f, Phi1 %f, Phi2 %f, "
98  "n1 (%f,%f,%f), n2 (%f,%f,%f)\n",
99  i, color, rMin, rMax, dz, phi1, phi2,
100  n1[0], n1[1], n1[2], n2[0], n2[1], n2[2]);
101  break;
102  }
103  default: {
104  assert(kFALSE);
105  }
106  }
107 
108  volumes[i]->SetLineColor(color);
109  }
110 
111  printf("\nCreated %d volumes\n\n", volumeCount);
112 
113  // Scatter reqSpheres placed sphere randomly in space
114  Double_t x, y, z;
115  for (i = 0; i < reqNodes; i++) {
116  // Pick random volume
117  UInt_t useVolume = gRandom->Integer(volumeCount);
118 
119  TGeoTranslation * trans;
120  TGeoRotation * rot;
121  if (randomDist) {
122  // Random translation
123  gRandom->Rannor(x, y);
124  gRandom->Rannor(z,dummy);
125  trans = new TGeoTranslation(x*worldRadius, y*worldRadius, z*worldRadius);
126 
127  // Random rotation
128  gRandom->Rannor(x, y);
129  gRandom->Rannor(z,dummy);
130  rot = new TGeoRotation("rot", x*360.0, y*360.0, z*360.0);
131  } else {
132  UInt_t perSide = pow(reqNodes,1.0/3.0)+0.5;
133  Double_t distance = sizeBase*5.0;
134  UInt_t xi, yi, zi;
135  zi = i / (perSide*perSide);
136  yi = (i / perSide) % perSide;
137  xi = i % perSide;
138  trans = new TGeoTranslation(xi*distance,yi*distance,zi*distance);
139  rot = new TGeoRotation("rot",0.0, 0.0, 0.0);
140  }
141  top->AddNode(volumes[useVolume], i, new TGeoCombiTrans(*trans, *rot));
142  //printf("Added node %d (Volume %d)\n", i, useVolume);
143  }
144  geom->CloseGeometry();
145  top->Draw("ogl");
146 }
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:460
virtual Double_t Rndm(Int_t i=0)
Machine independent random number generator.
Definition: TRandom.cxx:512
#define assert(cond)
Definition: unittest.h:542
Double_t distance(const TPoint2 &p1, const TPoint2 &p2)
Definition: CsgOps.cxx:467
Definition: Rtypes.h:60
virtual void Draw(Option_t *option="")
draw top volume according to option
void SetTopVolume(TGeoVolume *vol)
Set the top volume and corresponding node as starting point of the geometry.
int Int_t
Definition: RtypesCore.h:41
bool Bool_t
Definition: RtypesCore.h:59
const Bool_t kFALSE
Definition: Rtypes.h:92
virtual void SetSeed(UInt_t seed=0)
Set the random generator seed.
Definition: TRandom.cxx:568
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.
double pow(double, double)
virtual UInt_t Integer(UInt_t imax)
Returns a random integer on [ 0, imax-1 ].
Definition: TRandom.cxx:320
Float_t z[5]
Definition: Ifit.C:16
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:948
void glViewerLOD(Int_t reqNodes=1000, Bool_t randomDist=kTRUE, Bool_t reqSpheres=kTRUE, Bool_t reqTubes=kTRUE)
Definition: glViewerLOD.C:4
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
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
ClassImp(TMCParticle) void TMCParticle printf(": p=(%7.3f,%7.3f,%9.3f) ;", fPx, fPy, fPz)
static RooMathCoreReg dummy
Double_t y[n]
Definition: legend1.C:17
virtual void SetLineColor(Color_t lcolor)
Set the line color.
#define name(a, b)
Definition: linkTestLib0.cpp:5
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 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: Rtypes.h:91