glViewerLOD.C

Go to the documentation of this file.
00001 //To set the Level Of Details when rendering geometry shapes
00002 //Author: Richard Maunder
00003 
00004 void glViewerLOD(Int_t reqNodes = 1000, Bool_t randomDist = kTRUE,
00005                  Bool_t reqSpheres = kTRUE, Bool_t reqTubes = kTRUE)
00006 {
00007    TGeoManager * geom = new TGeoManager("LODTest", "GL viewer LOD test");
00008    geom->SetNsegments(4); // Doesn't matter keep low
00009    TGeoMaterial *matEmptySpace = new TGeoMaterial("EmptySpace", 0, 0, 0);
00010    TGeoMaterial *matSolid      = new TGeoMaterial("Solid"    , .938, 1., 10000.);
00011 
00012    TGeoMedium *medEmptySpace = new TGeoMedium("Empty", 1, matEmptySpace);
00013    TGeoMedium *medSolid      = new TGeoMedium("Solid", 1, matSolid);
00014 
00015    Double_t sizeBase = 20.0;
00016    Double_t worldRadius;
00017    if (randomDist) {
00018       worldRadius = pow(reqNodes,.5)*sizeBase;
00019    } else {
00020       worldRadius = pow(reqNodes,.3)*sizeBase;
00021    }
00022 
00023    TGeoVolume *top = geom->MakeBox
00024       ("WORLD", medEmptySpace, worldRadius, worldRadius, worldRadius);
00025    geom->SetTopVolume(top);
00026 
00027    gRandom->SetSeed();
00028 
00029    // Create random number of unique sphere shapes - up to 25% of
00030    // total placed sphere requested
00031    UInt_t volumeCount = gRandom->Integer(reqNodes/4)+1;
00032    TGeoVolume ** volumes = new TGeoVolume[volumeCount];
00033    TGeoVolume * volume;
00034 
00035    Double_t dummy;
00036 
00037    for (UInt_t i = 0; i < volumeCount; i++) {
00038       char name[128];
00039       sprintf(name, "Volume_%d", i);
00040 
00041       // Random volume shape
00042       Int_t type = -1;
00043       if (reqSpheres && reqTubes) {
00044          type = gRandom->Integer(2);
00045          if (type == 1)
00046             type += gRandom->Integer(3);
00047       }
00048       else if(reqSpheres)
00049          type = 0;
00050       else if(reqTubes)
00051          type = 1 + gRandom->Integer(3);
00052 
00053       // Random dimensions
00054       Double_t rMin = gRandom->Rndm() * sizeBase;
00055       Double_t rMax = rMin + gRandom->Rndm() * sizeBase * 2.0;
00056       Double_t dz   = pow(gRandom->Rndm(),2.0) * sizeBase * 15.0;
00057       Double_t phi1 = gRandom->Rndm() * 90.0;
00058       Double_t phi2 = phi1 + gRandom->Rndm() * 270.0;
00059 
00060       // Pick random color (not black)
00061       Int_t color = gRandom->Integer(50);
00062       if (color == kBlack) color += 1;
00063 
00064       switch (type) {
00065         case 0: {
00066             // GL viewer only supports solid spheres (0. inner radius)
00067             volumes[i] = geom->MakeSphere(name,  medSolid,  0., rMax);
00068             printf("Volume %d : Color %d, Sphere, Radius %f\n", i, color, rMax);
00069             break;
00070          }
00071          case 1: {
00072             volumes[i] = geom->MakeTube(name,  medSolid,  rMin, rMax, dz);
00073             printf("Volume %d : Color %d, Tube, Inner Radius %f, "
00074                    "Outer Radius %f, Length %f\n",
00075                    i, color, rMin, rMax, dz);
00076             break;
00077          }
00078          case 2: {
00079             volumes[i] = geom->MakeTubs(name,  medSolid,  rMin, rMax, dz,
00080                                         phi1, phi2);
00081             printf("Volume %d : Color %d, Tube Seg, Inner Radius %f, "
00082                    "Outer Radius %f, Length %f, Phi1 %f, Phi2 %f\n",
00083                    i, color, rMin, rMax, dz, phi1, phi2);
00084             break;
00085          }
00086          case 3: {
00087             Double_t n1[3], n2[3];
00088             n1[0] = gRandom->Rndm()*.5;
00089             n1[1] = gRandom->Rndm()*.5; n1[2] = -1.0 + gRandom->Rndm()*.5;
00090             n2[0] = gRandom->Rndm()*.5;
00091             n2[1] = gRandom->Rndm()*.5; n2[2] =  1.0 - gRandom->Rndm()*.5;
00092 
00093             volumes[i] = geom->MakeCtub(name,  medSolid,  rMin, rMax, dz,
00094                                         phi1, phi2, n1[0], n1[1], n1[2],
00095                                         n2[0], n2[1], n2[2]);
00096             printf("Volume %d : Color %d, Cut Tube, Inner Radius %f, "
00097                    "Outer Radius %f, Length %f, Phi1 %f, Phi2 %f, "
00098                    "n1 (%f,%f,%f), n2 (%f,%f,%f)\n",
00099                    i, color, rMin, rMax, dz, phi1, phi2,
00100                    n1[0], n1[1], n1[2], n2[0], n2[1], n2[2]);
00101             break;
00102          }
00103          default: {
00104             assert(kFALSE);
00105          }
00106       }
00107 
00108       volumes[i]->SetLineColor(color);
00109    }
00110 
00111    printf("\nCreated %d volumes\n\n", volumeCount);
00112 
00113    // Scatter reqSpheres placed sphere randomly in space
00114    Double_t x, y, z;
00115    for (i = 0; i < reqNodes; i++) {
00116       // Pick random volume
00117       UInt_t useVolume = gRandom->Integer(volumeCount);
00118 
00119       TGeoTranslation * trans;
00120       TGeoRotation * rot;
00121       if (randomDist) {
00122          // Random translation
00123          gRandom->Rannor(x, y);
00124          gRandom->Rannor(z,dummy);
00125          trans = new TGeoTranslation(x*worldRadius, y*worldRadius, z*worldRadius);
00126 
00127          // Random rotation
00128          gRandom->Rannor(x, y);
00129          gRandom->Rannor(z,dummy);
00130          rot = new TGeoRotation("rot", x*360.0, y*360.0, z*360.0);
00131       } else {
00132          UInt_t perSide = pow(reqNodes,1.0/3.0)+0.5;
00133          Double_t distance = sizeBase*5.0;
00134          UInt_t xi, yi, zi;
00135          zi = i / (perSide*perSide);
00136          yi = (i / perSide) % perSide;
00137          xi = i % perSide;
00138          trans = new TGeoTranslation(xi*distance,yi*distance,zi*distance);
00139          rot = new TGeoRotation("rot",0.0, 0.0, 0.0);
00140       }
00141       top->AddNode(volumes[useVolume], i, new TGeoCombiTrans(*trans, *rot));
00142       //printf("Added node %d (Volume %d)\n", i, useVolume);
00143    }
00144    geom->CloseGeometry();
00145    top->Draw("ogl");
00146 }

Generated on Tue Jul 5 15:44:19 2011 for ROOT_528-00b_version by  doxygen 1.5.1