TTUBE.cxx

Go to the documentation of this file.
00001 // @(#)root/g3d:$Id: TTUBE.cxx 31624 2009-12-08 09:58:40Z couet $
00002 // Author: Nenad Buncic   18/09/95
00003 
00004 /*************************************************************************
00005  * Copyright (C) 1995-2000, Rene Brun and Fons Rademakers.               *
00006  * All rights reserved.                                                  *
00007  *                                                                       *
00008  * For the licensing terms see $ROOTSYS/LICENSE.                         *
00009  * For the list of contributors see $ROOTSYS/README/CREDITS.             *
00010  *************************************************************************/
00011 
00012 #include "TTUBE.h"
00013 #include "TNode.h"
00014 #include "TVirtualPad.h"
00015 #include "TBuffer3D.h"
00016 #include "TBuffer3DTypes.h"
00017 #include "TGeometry.h"
00018 #include "TClass.h"
00019 #include "TMath.h"
00020 
00021 ClassImp(TTUBE)
00022 
00023 
00024 //______________________________________________________________________________
00025 // Begin_Html <P ALIGN=CENTER> <IMG SRC="gif/tube.gif"> </P> End_Html
00026 // TUBE is a tube. It has 6 parameters:
00027 //
00028 //     - name       name of the shape
00029 //     - title      shape's title
00030 //     - material  (see TMaterial)
00031 //     - rmin       inside radius
00032 //     - rmax       outside radius
00033 //     - dz         half length in z
00034 
00035 
00036 //______________________________________________________________________________
00037 TTUBE::TTUBE()
00038 {
00039    // TUBE shape default constructor
00040 
00041    fCoTab       = 0;
00042    fSiTab       = 0;
00043    fAspectRatio = 1;
00044    fDz          = 0.;
00045    fNdiv        = 0;
00046    fRmin        = 0.;
00047    fRmax        = 0.;
00048 }
00049 
00050 
00051 //______________________________________________________________________________
00052 TTUBE::TTUBE(const char *name, const char *title, const char *material, Float_t rmin, Float_t rmax, Float_t dz,Float_t aspect)
00053       : TShape(name, title,material)
00054 {
00055    // TUBE shape normal constructor
00056 
00057    fRmin  = rmin;
00058    fRmax  = rmax;
00059 
00060    fDz   = dz;
00061    fNdiv = 0;
00062 
00063    fCoTab = 0;
00064    fSiTab = 0;
00065 
00066    fAspectRatio = aspect;
00067 
00068    MakeTableOfCoSin();
00069 }
00070 
00071 
00072 //______________________________________________________________________________
00073 TTUBE::TTUBE(const char *name, const char *title, const char *material, Float_t rmax, Float_t dz)
00074       : TShape(name, title,material)
00075 {
00076    // TUBE shape "simplified" constructor
00077 
00078    fRmin  = 0;
00079    fRmax  = rmax;
00080 
00081    fDz   = dz;
00082    fNdiv = 0;
00083 
00084    fCoTab = 0;
00085    fSiTab = 0;
00086 
00087    fAspectRatio = 1;
00088 
00089    MakeTableOfCoSin();
00090 }
00091 
00092 //______________________________________________________________________________
00093 TTUBE::TTUBE(const TTUBE& tu) :
00094   TShape(tu),
00095   fRmin(tu.fRmin),
00096   fRmax(tu.fRmax),
00097   fDz(tu.fDz),
00098   fNdiv(tu.fNdiv),
00099   fAspectRatio(tu.fAspectRatio),
00100   fSiTab(tu.fSiTab),
00101   fCoTab(tu.fCoTab)
00102 { 
00103    //copy constructor
00104 }
00105 
00106 //______________________________________________________________________________
00107 TTUBE& TTUBE::operator=(const TTUBE& tu) 
00108 {
00109    //assignement operator
00110    if(this!=&tu) {
00111       TShape::operator=(tu);
00112       fRmin=tu.fRmin;
00113       fRmax=tu.fRmax;
00114       fDz=tu.fDz;
00115       fNdiv=tu.fNdiv;
00116       fAspectRatio=tu.fAspectRatio;
00117       fSiTab=tu.fSiTab;
00118       fCoTab=tu.fCoTab;
00119    } 
00120    return *this;
00121 }
00122 
00123 //______________________________________________________________________________
00124 void TTUBE::MakeTableOfCoSin() const // Internal cache - const so other const fn can use
00125 {
00126    // Make table of sine and cosine.
00127 
00128    const Double_t pi  = TMath::ATan(1) * 4.0;
00129 
00130    Int_t j;
00131    Int_t n = GetNumberOfDivisions ();
00132    if (fCoTab)
00133       delete [] fCoTab; // Delete the old tab if any
00134       fCoTab = new Double_t [n];
00135    if (!fCoTab ) {
00136       Error("MakeTableOfCoSin()","No cos table done");
00137       return;
00138    }
00139 
00140    if (fSiTab) delete [] fSiTab; // Delete the old tab if any
00141    fSiTab = new Double_t [n];
00142    if (!fSiTab ) {
00143       Error("MakeTableOfCoSin()","No sin table done");
00144       return;
00145    }
00146 
00147    Double_t range = 2*pi;
00148 
00149    Double_t angstep = range/n;
00150 
00151    Double_t ph = 0;
00152    for (j = 0; j < n; j++) {
00153       ph = j*angstep;
00154       fCoTab[j] = TMath::Cos(ph);
00155       fSiTab[j] = TMath::Sin(ph);
00156    }
00157 }
00158 
00159 
00160 //______________________________________________________________________________
00161 TTUBE::~TTUBE()
00162 {
00163    // TUBE shape default destructor
00164 
00165    delete [] fCoTab;
00166    delete [] fSiTab;
00167 }
00168 
00169 
00170 //______________________________________________________________________________
00171 Int_t TTUBE::DistancetoPrimitive(Int_t px, Int_t py)
00172 {
00173    // Compute distance from point px,py to a TUBE
00174    //
00175    // Compute the closest distance of approach from point px,py to each
00176    // computed outline point of the TUBE.
00177 
00178    Int_t n = GetNumberOfDivisions();
00179    Int_t numPoints = n*4;
00180    return ShapeDistancetoPrimitive(numPoints,px,py);
00181 }
00182 
00183 
00184 //______________________________________________________________________________
00185 void TTUBE::SetNumberOfDivisions (Int_t ndiv)
00186 {
00187    // Set number of divisions used to draw this tube
00188 
00189    fNdiv = ndiv;
00190    MakeTableOfCoSin();
00191 }
00192 
00193 
00194 //______________________________________________________________________________
00195 void TTUBE::SetPoints(Double_t *points) const
00196 {
00197    // Create TUBE points
00198         
00199    Int_t j, n;
00200    Int_t indx = 0;
00201        
00202    n = GetNumberOfDivisions();
00203    
00204    if (points) {
00205       if (!fCoTab)   MakeTableOfCoSin();
00206       for (j = 0; j < n; j++) {
00207          points[indx+6*n] = points[indx] = fRmin * fCoTab[j];
00208          indx++;
00209          points[indx+6*n] = points[indx] = fAspectRatio*fRmin * fSiTab[j];
00210          indx++;
00211          points[indx+6*n] = fDz;
00212          points[indx]     =-fDz;
00213          indx++;
00214       }
00215       for (j = 0; j < n; j++) {
00216          points[indx+6*n] = points[indx] = fRmax * fCoTab[j];
00217          indx++;
00218          points[indx+6*n] = points[indx] = fAspectRatio*fRmax * fSiTab[j];
00219          indx++;
00220          points[indx+6*n]= fDz;
00221          points[indx]    =-fDz;
00222          indx++;
00223       }
00224    }
00225 }
00226 
00227 
00228 //______________________________________________________________________________
00229 void TTUBE::SetSegsAndPols(TBuffer3D & buffer) const
00230 {
00231    // Set segments and polygons.
00232 
00233    Int_t i, j;
00234    Int_t n = GetNumberOfDivisions();
00235    Int_t c = GetBasicColor();
00236 
00237    for (i = 0; i < 4; i++) {
00238       for (j = 0; j < n; j++) {
00239          buffer.fSegs[(i*n+j)*3  ] = c;
00240          buffer.fSegs[(i*n+j)*3+1] = i*n+j;
00241          buffer.fSegs[(i*n+j)*3+2] = i*n+j+1;
00242       }
00243       buffer.fSegs[(i*n+j-1)*3+2] = i*n;
00244    }
00245    for (i = 4; i < 6; i++) {
00246       for (j = 0; j < n; j++) {
00247          buffer.fSegs[(i*n+j)*3  ] = c+1;
00248          buffer.fSegs[(i*n+j)*3+1] = (i-4)*n+j;
00249          buffer.fSegs[(i*n+j)*3+2] = (i-2)*n+j;
00250       }
00251    }
00252    for (i = 6; i < 8; i++) {
00253       for (j = 0; j < n; j++) {
00254          buffer.fSegs[(i*n+j)*3  ] = c;
00255          buffer.fSegs[(i*n+j)*3+1] = 2*(i-6)*n+j;
00256          buffer.fSegs[(i*n+j)*3+2] = (2*(i-6)+1)*n+j;
00257       }
00258    }
00259 
00260    Int_t indx = 0;
00261    i=0;
00262    for (j = 0; j < n; j++) {
00263       indx = 6*(i*n+j);
00264       buffer.fPols[indx  ] = c;
00265       buffer.fPols[indx+1] = 4;
00266       buffer.fPols[indx+5] = i*n+j;
00267       buffer.fPols[indx+4] = (4+i)*n+j;
00268       buffer.fPols[indx+3] = (2+i)*n+j;
00269       buffer.fPols[indx+2] = (4+i)*n+j+1;
00270    }
00271    buffer.fPols[indx+2] = (4+i)*n;
00272    i=1;
00273    for (j = 0; j < n; j++) {
00274       indx = 6*(i*n+j);
00275       buffer.fPols[indx  ] = c;
00276       buffer.fPols[indx+1] = 4;
00277       buffer.fPols[indx+2] = i*n+j;
00278       buffer.fPols[indx+3] = (4+i)*n+j;
00279       buffer.fPols[indx+4] = (2+i)*n+j;
00280       buffer.fPols[indx+5] = (4+i)*n+j+1;
00281    }
00282    buffer.fPols[indx+5] = (4+i)*n;
00283    i=2;
00284    for (j = 0; j < n; j++) {
00285       indx = 6*(i*n+j);
00286       buffer.fPols[indx  ] = c+i;
00287       buffer.fPols[indx+1] = 4;
00288       buffer.fPols[indx+2] = (i-2)*2*n+j;
00289       buffer.fPols[indx+3] = (4+i)*n+j;
00290       buffer.fPols[indx+4] = ((i-2)*2+1)*n+j;
00291       buffer.fPols[indx+5] = (4+i)*n+j+1;
00292    }
00293    buffer.fPols[indx+5] = (4+i)*n;
00294    i=3;
00295    for (j = 0; j < n; j++) {
00296       indx = 6*(i*n+j);
00297       buffer.fPols[indx  ] = c+i;
00298       buffer.fPols[indx+1] = 4;
00299       buffer.fPols[indx+5] = (i-2)*2*n+j;
00300       buffer.fPols[indx+4] = (4+i)*n+j;
00301       buffer.fPols[indx+3] = ((i-2)*2+1)*n+j;
00302       buffer.fPols[indx+2] = (4+i)*n+j+1;
00303    }
00304    buffer.fPols[indx+2] = (4+i)*n;
00305 }
00306 
00307 
00308 //______________________________________________________________________________
00309 void TTUBE::Sizeof3D() const
00310 {
00311    // Return total X3D needed by TNode::ls (when called with option "x")
00312 
00313    Int_t n = GetNumberOfDivisions();
00314 
00315    gSize3D.numPoints += n*4;
00316    gSize3D.numSegs   += n*8;
00317    gSize3D.numPolys  += n*4;
00318 }  
00319 
00320 
00321 //______________________________________________________________________________
00322 void TTUBE::Streamer(TBuffer &R__b)
00323 {
00324    // Stream an object of class TTUBE.
00325 
00326    if (R__b.IsReading()) {
00327       UInt_t R__s, R__c;
00328       Version_t R__v = R__b.ReadVersion(&R__s, &R__c);
00329       if (R__v > 2) {
00330          R__b.ReadClassBuffer(TTUBE::Class(), this, R__v, R__s, R__c);
00331          return;  
00332       }
00333       //====process old versions before automatic schema evolution
00334       TShape::Streamer(R__b);
00335       R__b >> fRmin;
00336       R__b >> fRmax;
00337       R__b >> fDz;
00338       R__b >> fNdiv;
00339       if (R__v > 1) R__b >> fAspectRatio;
00340       R__b.CheckByteCount(R__s, R__c, TTUBE::IsA());
00341       //====end of old versions
00342    } else {
00343       R__b.WriteClassBuffer(TTUBE::Class(),this);
00344    }
00345 }
00346 
00347 
00348 //______________________________________________________________________________
00349 const TBuffer3D & TTUBE::GetBuffer3D(Int_t reqSections) const
00350 {
00351    // Get buffer 3d.
00352 
00353    static TBuffer3D buffer(TBuffer3DTypes::kGeneric);
00354 
00355    TShape::FillBuffer3D(buffer, reqSections);
00356 
00357    // TODO: Although we now have a TBuffer3DTube class for
00358    // tube shapes, we do not use it for old geometry tube, as 
00359    // OGL viewer needs various rotation matrix info we can't easily
00360    // pass yet. To be revisited.
00361 
00362    // We also do not fill the bounding box as derived classes can adjust shape
00363    // leave up to viewer to work out
00364    if (reqSections & TBuffer3D::kRawSizes) {
00365       Int_t n = GetNumberOfDivisions();
00366       Int_t nbPnts = 4*n;
00367       Int_t nbSegs = 8*n;
00368       Int_t nbPols = 4*n;
00369       if (buffer.SetRawSizes(nbPnts, 3*nbPnts, nbSegs, 3*nbSegs, nbPols, 6*nbPols)) {
00370          buffer.SetSectionsValid(TBuffer3D::kRawSizes);
00371       }
00372    }
00373    if ((reqSections & TBuffer3D::kRaw) && buffer.SectionsValid(TBuffer3D::kRawSizes)) {
00374       SetPoints(buffer.fPnts);
00375       if (!buffer.fLocalFrame) {
00376          TransformPoints(buffer.fPnts, buffer.NbPnts());
00377       }
00378       SetSegsAndPols(buffer);
00379       buffer.SetSectionsValid(TBuffer3D::kRaw);
00380    }
00381    return buffer;
00382 }

Generated on Tue Jul 5 14:16:41 2011 for ROOT_528-00b_version by  doxygen 1.5.1