TGeoTorus.cxx

Go to the documentation of this file.
00001 // @(#)root/geom:$Id: TGeoTorus.cxx 32395 2010-02-24 11:58:25Z brun $
00002 // Author: Andrei Gheata   28/07/03
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 //_____________________________________________________________________________
00013 // TGeoTorus - Torus segment class. A torus has 5 parameters :
00014 //            R    - axial radius
00015 //            Rmin - inner radius
00016 //            Rmax - outer radius 
00017 //            Phi1 - starting phi
00018 //            Dphi - phi extent
00019 //
00020 //_____________________________________________________________________________
00021 
00022 //Begin_Html
00023 /*
00024 <img src="gif/t_ctorus.gif">
00025 */
00026 //End_Html
00027 
00028 #include "Riostream.h"
00029 
00030 #include "TGeoManager.h"
00031 #include "TGeoVolume.h"
00032 #include "TGeoTube.h"
00033 #include "TVirtualGeoPainter.h"
00034 #include "TGeoTorus.h"
00035 #include "TVirtualPad.h"
00036 #include "TBuffer3D.h"
00037 #include "TBuffer3DTypes.h"
00038 #include "TMath.h"
00039 
00040 ClassImp(TGeoTorus)
00041 
00042 //_____________________________________________________________________________
00043 TGeoTorus::TGeoTorus()
00044 {
00045 // Default constructor
00046    SetShapeBit(TGeoShape::kGeoTorus);
00047    fR    = 0.0;
00048    fRmin = 0.0;
00049    fRmax = 0.0;
00050    fPhi1 = 0.0;
00051    fDphi = 0.0;
00052 }   
00053 
00054 //_____________________________________________________________________________
00055 TGeoTorus::TGeoTorus(Double_t r, Double_t rmin, Double_t rmax, Double_t phi1, Double_t dphi)
00056           :TGeoBBox(0, 0, 0)
00057 {
00058 // Constructor without name.
00059    SetShapeBit(TGeoShape::kGeoTorus);
00060    SetTorusDimensions(r, rmin, rmax, phi1, dphi);
00061    if ((fRmin<0) || (fRmax<0)) 
00062       SetShapeBit(kGeoRunTimeShape);
00063    ComputeBBox();
00064 }
00065 
00066 //_____________________________________________________________________________
00067 TGeoTorus::TGeoTorus(const char *name, Double_t r, Double_t rmin, Double_t rmax, Double_t phi1, Double_t dphi)
00068           :TGeoBBox(name, 0, 0, 0)
00069 {
00070 // Constructor with name.
00071    SetShapeBit(TGeoShape::kGeoTorus);
00072    SetTorusDimensions(r, rmin, rmax, phi1, dphi);
00073    if ((fRmin<0) || (fRmax<0)) 
00074       SetShapeBit(kGeoRunTimeShape);
00075    ComputeBBox();
00076 }
00077 
00078 //_____________________________________________________________________________
00079 TGeoTorus::TGeoTorus(Double_t *param)
00080           :TGeoBBox(0, 0, 0)
00081 {
00082 // Constructor based on an array of parameters.
00083 // param[0] = R
00084 // param[1] = Rmin
00085 // param[2] = Rmax
00086 // param[3] = Phi1
00087 // param[4] = Dphi
00088    SetShapeBit(TGeoShape::kGeoTorus);
00089    SetDimensions(param);
00090    if (fRmin<0 || fRmax<0) SetShapeBit(kGeoRunTimeShape);
00091    ComputeBBox();
00092 }
00093 
00094 //_____________________________________________________________________________
00095 Double_t TGeoTorus::Capacity() const
00096 {
00097 // Computes capacity of the shape in [length^3]
00098    Double_t capacity = (fDphi/180.)*TMath::Pi()*TMath::Pi()*fR*(fRmax*fRmax-fRmin*fRmin);
00099    return capacity;
00100 }
00101    
00102 //_____________________________________________________________________________
00103 void TGeoTorus::ComputeBBox()
00104 {
00105 // Compute bounding box of the torus.
00106    fDZ = fRmax;
00107    if (TGeoShape::IsSameWithinTolerance(fDphi,360)) {
00108       fDX = fDY = fR+fRmax;
00109       return;
00110    }
00111    Double_t xc[4];
00112    Double_t yc[4];
00113    xc[0] = (fR+fRmax)*TMath::Cos(fPhi1*TMath::DegToRad());
00114    yc[0] = (fR+fRmax)*TMath::Sin(fPhi1*TMath::DegToRad());
00115    xc[1] = (fR+fRmax)*TMath::Cos((fPhi1+fDphi)*TMath::DegToRad());
00116    yc[1] = (fR+fRmax)*TMath::Sin((fPhi1+fDphi)*TMath::DegToRad());
00117    xc[2] = (fR-fRmax)*TMath::Cos(fPhi1*TMath::DegToRad());
00118    yc[2] = (fR-fRmax)*TMath::Sin(fPhi1*TMath::DegToRad());
00119    xc[3] = (fR-fRmax)*TMath::Cos((fPhi1+fDphi)*TMath::DegToRad());
00120    yc[3] = (fR-fRmax)*TMath::Sin((fPhi1+fDphi)*TMath::DegToRad());
00121       
00122    Double_t xmin = xc[TMath::LocMin(4, &xc[0])];
00123    Double_t xmax = xc[TMath::LocMax(4, &xc[0])]; 
00124    Double_t ymin = yc[TMath::LocMin(4, &yc[0])]; 
00125    Double_t ymax = yc[TMath::LocMax(4, &yc[0])];
00126    Double_t ddp = -fPhi1;
00127    if (ddp<0) ddp+= 360;
00128    if (ddp<=fDphi) xmax = fR+fRmax;
00129    ddp = 90-fPhi1;
00130    if (ddp<0) ddp+= 360;
00131    if (ddp>360) ddp-=360;
00132    if (ddp<=fDphi) ymax = fR+fRmax;
00133    ddp = 180-fPhi1;
00134    if (ddp<0) ddp+= 360;
00135    if (ddp>360) ddp-=360;
00136    if (ddp<=fDphi) xmin = -(fR+fRmax);
00137    ddp = 270-fPhi1;
00138    if (ddp<0) ddp+= 360;
00139    if (ddp>360) ddp-=360;
00140    if (ddp<=fDphi) ymin = -(fR+fRmax);
00141    fOrigin[0] = (xmax+xmin)/2;
00142    fOrigin[1] = (ymax+ymin)/2;
00143    fOrigin[2] = 0;
00144    fDX = (xmax-xmin)/2;
00145    fDY = (ymax-ymin)/2;
00146 }
00147 
00148 //-----------------------------------------------------------------------------   
00149 void TGeoTorus::ComputeNormal(Double_t *point, Double_t *dir, Double_t *norm)
00150 {
00151 // Compute normal to closest surface from POINT. 
00152    Double_t phi = TMath::ATan2(point[1],point[0]);
00153    if (fDphi<360) {
00154       Double_t phi1 = fPhi1*TMath::DegToRad();
00155       Double_t phi2 = (fPhi1+fDphi)*TMath::DegToRad();
00156       Double_t c1 = TMath::Cos(phi1);
00157       Double_t s1 = TMath::Sin(phi1);
00158       Double_t c2 = TMath::Cos(phi2);
00159       Double_t s2 = TMath::Sin(phi2);
00160 
00161       Double_t daxis = Daxis(point,dir,0);
00162       if ((fRmax-daxis)>1E-5) {
00163          if (TGeoShape::IsSameWithinTolerance(fRmin,0) || (daxis-fRmin)>1E-5) {
00164             TGeoShape::NormalPhi(point,dir,norm,c1,s1,c2,s2);
00165             return;
00166          }
00167       }
00168    }   
00169    Double_t r0[3];
00170    r0[0] = fR*TMath::Cos(phi);
00171    r0[1] = fR*TMath::Sin(phi);           
00172    r0[2] = 0;
00173    Double_t normsq = 0;
00174    for (Int_t i=0; i<3; i++) {
00175       norm[i] = point[i] - r0[i];
00176       normsq += norm[i]*norm[i];
00177    }
00178    
00179    normsq = TMath::Sqrt(normsq);
00180    norm[0] /= normsq;
00181    norm[1] /= normsq;
00182    norm[2] /= normsq;
00183    if (dir[0]*norm[0]+dir[1]*norm[1]+dir[2]*norm[2] < 0) {
00184       norm[0] = -norm[0];
00185       norm[1] = -norm[1];
00186       norm[2] = -norm[2];
00187    }      
00188 }
00189 
00190 //_____________________________________________________________________________
00191 Bool_t TGeoTorus::Contains(Double_t *point) const
00192 {
00193 // Test if point is inside the torus.
00194    // check phi range
00195    if (!TGeoShape::IsSameWithinTolerance(fDphi,360)) {
00196       Double_t phi = TMath::ATan2(point[1], point[0]) * TMath::RadToDeg();
00197       if (phi < 0) phi+=360.0;
00198       Double_t ddp = phi-fPhi1;
00199       if (ddp<0) ddp+=360.;
00200       if (ddp>fDphi) return kFALSE;
00201    }
00202    //check radius
00203    Double_t rxy = TMath::Sqrt(point[0]*point[0]+point[1]*point[1]);
00204    Double_t radsq = (rxy-fR)*(rxy-fR) + point[2]*point[2];
00205    if (radsq<fRmin*fRmin) return kFALSE;
00206    if (radsq>fRmax*fRmax) return kFALSE;
00207    return kTRUE;
00208 }   
00209 
00210 //_____________________________________________________________________________
00211 Int_t TGeoTorus::DistancetoPrimitive(Int_t px, Int_t py)
00212 {
00213 // Compute closest distance from point px,py to each vertex.
00214    Int_t n = gGeoManager->GetNsegments()+1;
00215    Int_t numPoints = n*(n-1);
00216    if (fRmin>0) numPoints *= 2;
00217    else if (fDphi<360) numPoints += 2;
00218    return ShapeDistancetoPrimitive(numPoints, px, py);
00219 }
00220 
00221 //_____________________________________________________________________________
00222 Double_t TGeoTorus::Daxis(Double_t *pt, Double_t *dir, Double_t t) const
00223 {
00224 // Computes distance to axis of the torus from point pt + t*dir;
00225    Double_t p[3];
00226    for (Int_t i=0; i<3; i++) p[i] = pt[i]+t*dir[i];
00227    Double_t rxy = TMath::Sqrt(p[0]*p[0]+p[1]*p[1]);
00228    return TMath::Sqrt((rxy-fR)*(rxy-fR)+p[2]*p[2]);
00229 }   
00230 
00231 //_____________________________________________________________________________
00232 Double_t TGeoTorus::DDaxis(Double_t *pt, Double_t *dir, Double_t t) const
00233 {
00234 // Computes derivative w.r.t. t of the distance to axis of the torus from point pt + t*dir;
00235    Double_t p[3];
00236    for (Int_t i=0; i<3; i++) p[i] = pt[i]+t*dir[i];
00237    Double_t rxy = TMath::Sqrt(p[0]*p[0]+p[1]*p[1]);
00238    if (rxy<1E-4) return ((p[2]*dir[2]-fR*TMath::Sqrt(dir[0]*dir[0]+dir[1]*dir[1]))/TMath::Sqrt(fR*fR+p[2]*p[2]));
00239    Double_t d = TMath::Sqrt((rxy-fR)*(rxy-fR)+p[2]*p[2]);
00240    if (TGeoShape::IsSameWithinTolerance(d,0)) return 0.;
00241    Double_t dd = (p[0]*dir[0]+p[1]*dir[1]+p[2]*dir[2] - (p[0]*dir[0]+p[1]*dir[1])*fR/rxy)/d;
00242    return dd;
00243 }   
00244 
00245 //_____________________________________________________________________________
00246 Double_t TGeoTorus::DDDaxis(Double_t *pt, Double_t *dir, Double_t t) const
00247 {
00248 // Second derivative of distance to torus axis w.r.t t.
00249    Double_t p[3];
00250    for (Int_t i=0; i<3; i++) p[i] = pt[i]+t*dir[i];
00251    Double_t rxy = TMath::Sqrt(p[0]*p[0]+p[1]*p[1]);
00252    if (rxy<1E-6) return 0;
00253    Double_t daxis = TMath::Sqrt((rxy-fR)*(rxy-fR)+p[2]*p[2]);
00254    if (TGeoShape::IsSameWithinTolerance(daxis,0)) return 0;
00255    Double_t ddaxis = (p[0]*dir[0]+p[1]*dir[1]+p[2]*dir[2] - (p[0]*dir[0]+p[1]*dir[1])*fR/rxy)/daxis;
00256    Double_t dddaxis = 1 - ddaxis*ddaxis - (1-dir[2]*dir[2])*fR/rxy +
00257                       fR*(p[0]*dir[0]+p[1]*dir[1])*(p[0]*dir[0]+p[1]*dir[1])/(rxy*rxy*rxy);
00258    dddaxis /= daxis;
00259    return dddaxis;
00260 }
00261    
00262 //_____________________________________________________________________________
00263 Double_t TGeoTorus::DistFromInside(Double_t *point, Double_t *dir, Int_t iact, Double_t step, Double_t *safe) const
00264 {
00265 // Compute distance from inside point to surface of the torus.
00266    if (iact<3 && safe) {
00267       *safe = Safety(point, kTRUE);
00268       if (iact==0) return TGeoShape::Big();
00269       if ((iact==1) && (step<=*safe)) return TGeoShape::Big();
00270    }
00271    Double_t snext = TGeoShape::Big();
00272    Bool_t hasphi = (fDphi<360)?kTRUE:kFALSE;
00273    Bool_t hasrmin = (fRmin>0)?kTRUE:kFALSE;
00274    Double_t dout = ToBoundary(point,dir,fRmax);
00275 //   Double_t dax = Daxis(point,dir,dout);
00276    Double_t din = (hasrmin)?ToBoundary(point,dir,fRmin):TGeoShape::Big();
00277    snext = TMath::Min(dout,din);
00278    if (snext>1E10) return TGeoShape::Tolerance();
00279    Double_t dphi = TGeoShape::Big();
00280    if (hasphi) {
00281       // Torus segment case.
00282       Double_t c1,s1,c2,s2,cm,sm,cdfi;
00283       Double_t phi1=fPhi1*TMath::DegToRad();
00284       Double_t phi2=(fPhi1+fDphi)*TMath::DegToRad();
00285       c1=TMath::Cos(phi1);
00286       s1=TMath::Sin(phi1);
00287       c2=TMath::Cos(phi2);
00288       s2=TMath::Sin(phi2);
00289       Double_t fio=0.5*(phi1+phi2);
00290       cm=TMath::Cos(fio);
00291       sm=TMath::Sin(fio);
00292       cdfi = TMath::Cos(0.5*(phi2-phi1));
00293       dphi = TGeoTubeSeg::DistFromInsideS(point,dir,fR-fRmax,fR+fRmax, fRmax, c1,s1,c2,s2,cm,sm,cdfi);
00294       Double_t daxis = Daxis(point,dir,dphi);
00295       if (daxis>=fRmin+1.E-8 && daxis<=fRmax-1.E-8) snext=TMath::Min(snext,dphi);
00296    }      
00297    return snext;
00298 }
00299 
00300 //_____________________________________________________________________________
00301 Double_t TGeoTorus::DistFromOutside(Double_t *point, Double_t *dir, Int_t iact, Double_t step, Double_t *safe) const
00302 {
00303 // Compute distance from outside point to surface of the torus.
00304    if (iact<3 && safe) {
00305       *safe = Safety(point, kFALSE);
00306       if (iact==0) return TGeoShape::Big();
00307       if ((iact==1) && (step<=*safe)) return TGeoShape::Big();
00308    }
00309 // Check if the bounding box is crossed within the requested distance
00310    Double_t sdist = TGeoBBox::DistFromOutside(point,dir, fDX, fDY, fDZ, fOrigin, step);
00311    if (sdist>=step) return TGeoShape::Big();
00312    Double_t daxis;
00313    Bool_t hasphi = (fDphi<360)?kTRUE:kFALSE;
00314 //   Bool_t hasrmin = (fRmin>0)?kTRUE:kFALSE;
00315    Double_t c1=0,s1=0,c2=0,s2=0,cm=0,sm=0,cdfi=0;
00316    Bool_t inphi = kFALSE;
00317    Double_t phi, ddp, phi1,phi2,fio;
00318    Double_t rxy2,dd;
00319    Double_t snext;
00320    Double_t pt[3];
00321    Int_t i;
00322       
00323    if (hasphi) {
00324       // Torus segment case.
00325       phi=TMath::ATan2(point[1], point[0])*TMath::RadToDeg();;
00326       if (phi<0) phi+=360;
00327       ddp = phi-fPhi1;
00328       if (ddp<0) ddp+=360;;
00329       if (ddp<=fDphi) inphi=kTRUE;
00330       phi1=fPhi1*TMath::DegToRad();
00331       phi2=(fPhi1+fDphi)*TMath::DegToRad();
00332       c1=TMath::Cos(phi1);
00333       s1=TMath::Sin(phi1);
00334       c2=TMath::Cos(phi2);
00335       s2=TMath::Sin(phi2);
00336       fio=0.5*(phi1+phi2);
00337       cm=TMath::Cos(fio);
00338       sm=TMath::Sin(fio);
00339       cdfi=TMath::Cos(0.5*(phi2-phi1));
00340    }
00341    // Check if we are inside or outside the bounding ring.
00342    Bool_t inbring = kFALSE;
00343    if (TMath::Abs(point[2]) <= fRmax) {
00344       rxy2 = point[0]*point[0]+point[1]*point[1];
00345       if ((rxy2>=(fR-fRmax)*(fR-fRmax)) && (rxy2<=(fR+fRmax)*(fR+fRmax))) {
00346          if (!hasphi || inphi) inbring=kTRUE;
00347       }
00348    }   
00349    
00350    // If outside the ring, compute distance to it.
00351    Double_t dring = TGeoShape::Big();
00352    Double_t eps = 1.E-5;
00353    snext = 0;
00354    daxis = -1;
00355    memcpy(pt,point,3*sizeof(Double_t));
00356    if (!inbring) {
00357       if (hasphi) dring = TGeoTubeSeg::DistFromOutsideS(point,dir,TMath::Max(0.,fR-fRmax-eps),fR+fRmax+eps, fRmax+eps, c1,s1,c2,s2,cm,sm,cdfi);            
00358       else        dring = TGeoTube::DistFromOutsideS(point,dir,TMath::Max(0.,fR-fRmax-eps),fR+fRmax+eps, fRmax+eps);
00359       // If not crossing it, return BIG.
00360       if (dring>1E10) return TGeoShape::Big();
00361       snext = dring;
00362       // Check if the crossing is due to phi.
00363       daxis = Daxis(point,dir,snext);
00364       if (daxis>=fRmin && daxis<fRmax) return snext;
00365       // Not a phi crossing -> propagate until we cross the ring.
00366       for (i=0; i<3; i++) pt[i] = point[i]+snext*dir[i];      
00367    }
00368    // Point pt is inside the bounding ring, no phi crossing so far.
00369    // Check if we are in the hole.
00370    if (daxis<0) daxis = Daxis(pt,dir,0);
00371    if (daxis<fRmin) {
00372       // We are in the hole. Check if we came from outside.
00373       if (snext>0) {
00374          // we can cross either the inner torus or exit the other hole.
00375          snext += 0.1*eps;
00376          for (i=0; i<3; i++) pt[i] += 0.1*eps*dir[i];
00377       }
00378       // We are in the hole from the begining.   
00379       // find first crossing with inner torus
00380       dd = ToBoundary(pt,dir, fRmin);
00381       // find exit distance from inner bounding ring
00382       if (hasphi) dring = TGeoTubeSeg::DistFromInsideS(pt,dir,fR-fRmin,fR+fRmin, fRmin, c1,s1,c2,s2,cm,sm,cdfi);
00383       else        dring = TGeoTube::DistFromInsideS(pt,dir,fR-fRmin,fR+fRmin, fRmin);
00384       if (dd<dring) return (snext+dd);
00385       // we were exiting a hole
00386       return TGeoShape::Big();
00387    }    
00388    // We are inside the outer ring, having daxis>fRmax
00389    // Compute distance to exit the bounding ring (again)
00390    if (snext>0) {
00391       // we can cross either the inner torus or exit the other hole.
00392       snext += 0.1*eps;
00393       for (i=0; i<3; i++) pt[i] += 0.1*eps*dir[i];
00394    }
00395    // Check intersection with outer torus
00396    dd = ToBoundary(pt, dir, fRmax);
00397    if (hasphi) dring = TGeoTubeSeg::DistFromInsideS(pt,dir,TMath::Max(0.,fR-fRmax-eps),fR+fRmax+eps, fRmax+eps, c1,s1,c2,s2,cm,sm,cdfi);            
00398    else        dring = TGeoTube::DistFromInsideS(pt,dir,TMath::Max(0.,fR-fRmax-eps),fR+fRmax+eps, fRmax+eps);
00399    if (dd<dring) {
00400       snext += dd;
00401       return snext;
00402    }
00403    // We are exiting the bounding ring before crossing the torus -> propagate
00404    snext += dring+eps;   
00405    for (i=0; i<3; i++) pt[i] = point[i] + snext*dir[i];
00406    snext += DistFromOutside(pt,dir,3);
00407    return snext;
00408 }      
00409 
00410 //_____________________________________________________________________________
00411 TGeoVolume *TGeoTorus::Divide(TGeoVolume * /*voldiv*/, const char * /*divname*/, Int_t /*iaxis*/, Int_t /*ndiv*/, 
00412                               Double_t /*start*/, Double_t /*step*/) 
00413 {
00414 //--- Divide this torus shape belonging to volume "voldiv" into ndiv volumes
00415 // called divname, from start position with the given step. 
00416    return 0;
00417 }
00418 
00419 //_____________________________________________________________________________
00420 const char *TGeoTorus::GetAxisName(Int_t iaxis) const
00421 {
00422 // Returns name of axis IAXIS.
00423    switch (iaxis) {
00424       case 1:
00425          return "R";
00426       case 2:
00427          return "PHI";
00428       case 3:
00429          return "Z";
00430       default:
00431          return "UNDEFINED";
00432    }
00433 }   
00434    
00435 //_____________________________________________________________________________
00436 Double_t TGeoTorus::GetAxisRange(Int_t iaxis, Double_t &xlo, Double_t &xhi) const
00437 {
00438 // Get range of shape for a given axis.
00439    xlo = 0;
00440    xhi = 0;
00441    Double_t dx = 0;
00442    switch (iaxis) {
00443       case 1:
00444          xlo = fRmin;
00445          xhi = fRmax;
00446          dx = xhi-xlo;
00447          return dx;
00448       case 2:
00449          xlo = fPhi1;
00450          xhi = fPhi1+fDphi;
00451          dx = fDphi;
00452          return dx;
00453       case 3:
00454          dx = 0;
00455          return dx;
00456    }
00457    return dx;
00458 }         
00459    
00460 //_____________________________________________________________________________
00461 void TGeoTorus::GetBoundingCylinder(Double_t *param) const
00462 {
00463 //--- Fill vector param[4] with the bounding cylinder parameters. The order
00464 // is the following : Rmin, Rmax, Phi1, Phi2, dZ
00465    param[0] = (fR-fRmax); // Rmin
00466    param[1] = (fR+fRmax); // Rmax
00467    param[2] = fPhi1;    // Phi1
00468    param[3] = fPhi1+fDphi;  // Phi2
00469 }   
00470  
00471 //_____________________________________________________________________________
00472 TGeoShape *TGeoTorus::GetMakeRuntimeShape(TGeoShape * /*mother*/, TGeoMatrix * /*mat*/) const
00473 {
00474 // Create a shape fitting the mother.
00475    if (!TestShapeBit(kGeoRunTimeShape)) return 0;
00476    Error("GetMakeRuntimeShape", "parametrized toruses not supported");
00477    return 0;
00478 }
00479       
00480 //_____________________________________________________________________________
00481 void TGeoTorus::InspectShape() const
00482 {
00483 // print shape parameters
00484    printf("*** Shape %s: TGeoTorus ***\n", GetName());
00485    printf("    R    = %11.5f\n", fR);
00486    printf("    Rmin = %11.5f\n", fRmin);
00487    printf("    Rmax = %11.5f\n", fRmax);
00488    printf("    Phi1 = %11.5f\n", fPhi1);
00489    printf("    Dphi = %11.5f\n", fDphi);
00490    printf(" Bounding box:\n");
00491    TGeoBBox::InspectShape();
00492 }
00493 
00494 //_____________________________________________________________________________
00495 TBuffer3D *TGeoTorus::MakeBuffer3D() const
00496 { 
00497    // Creates a TBuffer3D describing *this* shape.
00498    // Coordinates are in local reference frame.
00499 
00500    Int_t n = gGeoManager->GetNsegments()+1;
00501    Int_t nbPnts = n*(n-1);
00502    Bool_t hasrmin = (GetRmin()>0)?kTRUE:kFALSE;
00503    Bool_t hasphi  = (GetDphi()<360)?kTRUE:kFALSE;
00504    if (hasrmin) nbPnts *= 2;
00505    else if (hasphi) nbPnts += 2;
00506 
00507    Int_t nbSegs = (2*n-1)*(n-1);
00508    Int_t nbPols = (n-1)*(n-1);
00509    if (hasrmin) {
00510       nbSegs += (2*n-1)*(n-1);
00511       nbPols += (n-1)*(n-1);
00512    }
00513    if (hasphi) {
00514       nbSegs += 2*(n-1);
00515       nbPols += 2*(n-1);
00516    }
00517 
00518    TBuffer3D* buff = new TBuffer3D(TBuffer3DTypes::kGeneric,
00519                                    nbPnts, 3*nbPnts, nbSegs, 3*nbSegs, nbPols, 6*nbPols);
00520    if (buff)
00521    {
00522       SetPoints(buff->fPnts);   
00523       SetSegsAndPols(*buff);
00524    }
00525 
00526    return buff; 
00527 }
00528 
00529 //_____________________________________________________________________________
00530 void TGeoTorus::SetSegsAndPols(TBuffer3D &buff) const
00531 {
00532 // Fill TBuffer3D structure for segments and polygons.
00533    Int_t i, j;
00534    Int_t n = gGeoManager->GetNsegments()+1;
00535    Int_t nbPnts = n*(n-1);
00536    Int_t indx, indp, startcap=0;
00537    Bool_t hasrmin = (GetRmin()>0)?kTRUE:kFALSE;
00538    Bool_t hasphi  = (GetDphi()<360)?kTRUE:kFALSE;
00539    if (hasrmin) nbPnts *= 2;
00540    else if (hasphi) nbPnts += 2;
00541    Int_t c = GetBasicColor();
00542 
00543    indp = n*(n-1); // start index for points on inner surface
00544    memset(buff.fSegs, 0, buff.NbSegs()*3*sizeof(Int_t));
00545 
00546    // outer surface phi circles = n*(n-1) -> [0, n*(n-1) -1]
00547    // connect point j with point j+1 on same row
00548    indx = 0;
00549    for (i = 0; i < n; i++) { // rows [0,n-1]
00550       for (j = 0; j < n-1; j++) {  // points on a row [0, n-2]
00551          buff.fSegs[indx+(i*(n-1)+j)*3] = c;
00552          buff.fSegs[indx+(i*(n-1)+j)*3+1] = i*(n-1)+j;   // j on row i
00553          buff.fSegs[indx+(i*(n-1)+j)*3+2] = i*(n-1)+((j+1)%(n-1)); // j+1 on row i
00554       }
00555    }
00556    indx += 3*n*(n-1);
00557    // outer surface generators = (n-1)*(n-1) -> [n*(n-1), (2*n-1)*(n-1) -1]
00558    // connect point j on row i with point j on row i+1
00559    for (i = 0; i < n-1; i++) { // rows [0, n-2]
00560       for (j = 0; j < n-1; j++) {  // points on a row [0, n-2]
00561          buff.fSegs[indx+(i*(n-1)+j)*3] = c;
00562          buff.fSegs[indx+(i*(n-1)+j)*3+1] = i*(n-1)+j;     // j on row i
00563          buff.fSegs[indx+(i*(n-1)+j)*3+2] = (i+1)*(n-1)+j; // j on row i+1
00564       }
00565    }
00566    indx += 3*(n-1)*(n-1);
00567    startcap = (2*n-1)*(n-1);
00568 
00569    if (hasrmin) {
00570       // inner surface phi circles = n*(n-1) -> [(2*n-1)*(n-1), (3*n-1)*(n-1) -1]
00571       // connect point j with point j+1 on same row
00572       for (i = 0; i < n; i++) { // rows [0, n-1]
00573          for (j = 0; j < n-1; j++) {  // points on a row [0, n-2]
00574             buff.fSegs[indx+(i*(n-1)+j)*3] = c;              // lighter color
00575             buff.fSegs[indx+(i*(n-1)+j)*3+1] = indp + i*(n-1)+j;   // j on row i
00576             buff.fSegs[indx+(i*(n-1)+j)*3+2] = indp + i*(n-1)+((j+1)%(n-1)); // j+1 on row i
00577          }
00578       }
00579       indx += 3*n*(n-1);
00580       // inner surface generators = (n-1)*n -> [(3*n-1)*(n-1), (4*n-2)*(n-1) -1]
00581       // connect point j on row i with point j on row i+1
00582       for (i = 0; i < n-1; i++) { // rows [0, n-2]
00583          for (j = 0; j < n-1; j++) {  // points on a row [0, n-2]
00584             buff.fSegs[indx+(i*(n-1)+j)*3] = c;                // lighter color
00585             buff.fSegs[indx+(i*(n-1)+j)*3+1] = indp + i*(n-1)+j;     // j on row i
00586             buff.fSegs[indx+(i*(n-1)+j)*3+2] = indp + (i+1)*(n-1)+j; // j on row i+1
00587          }
00588       }
00589       indx += 3*(n-1)*(n-1);
00590       startcap = (4*n-2)*(n-1);
00591    }
00592 
00593    if (hasphi) {
00594       if (hasrmin) {
00595          // endcaps = 2*(n-1) -> [(4*n-2)*(n-1), 4*n*(n-1)-1]
00596          i = 0;
00597          for (j = 0; j < n-1; j++) {
00598             buff.fSegs[indx+j*3] = c+1;
00599             buff.fSegs[indx+j*3+1] = (n-1)*i+j;     // outer j on row 0
00600             buff.fSegs[indx+j*3+2] = indp+(n-1)*i+j; // inner j on row 0
00601          }
00602          indx += 3*(n-1);
00603          i = n-1;
00604          for (j = 0; j < n-1; j++) {
00605             buff.fSegs[indx+j*3] = c+1;
00606             buff.fSegs[indx+j*3+1] = (n-1)*i+j;     // outer j on row n-1
00607             buff.fSegs[indx+j*3+2] = indp+(n-1)*i+j; // inner j on row n-1
00608          }
00609          indx += 3*(n-1);
00610       } else {
00611          i = 0;
00612          for (j = 0; j < n-1; j++) {
00613             buff.fSegs[indx+j*3] = c+1;
00614             buff.fSegs[indx+j*3+1] = (n-1)*i+j;     // outer j on row 0
00615             buff.fSegs[indx+j*3+2] = n*(n-1);       // center of first endcap
00616          }
00617          indx += 3*(n-1);
00618          i = n-1;
00619          for (j = 0; j < n-1; j++) {
00620             buff.fSegs[indx+j*3] = c+1;
00621             buff.fSegs[indx+j*3+1] = (n-1)*i+j;     // outer j on row n-1
00622             buff.fSegs[indx+j*3+2] = n*(n-1)+1;     // center of second endcap
00623          }
00624          indx += 3*(n-1);
00625       }
00626    }
00627 
00628    indx = 0;
00629    memset(buff.fPols, 0, buff.NbPols()*6*sizeof(Int_t));
00630 
00631    // outer surface = (n-1)*(n-1) -> [0, (n-1)*(n-1)-1]
00632    // normal pointing out
00633    for (i=0; i<n-1; i++) {
00634       for (j=0; j<n-1; j++) {
00635          buff.fPols[indx++] = c;
00636          buff.fPols[indx++] = 4;
00637          buff.fPols[indx++] = n*(n-1)+(n-1)*i+((j+1)%(n-1)); // generator j+1 on outer row i
00638          buff.fPols[indx++] = (n-1)*(i+1)+j; // seg j on outer row i+1
00639          buff.fPols[indx++] = n*(n-1)+(n-1)*i+j; // generator j on outer row i
00640          buff.fPols[indx++] = (n-1)*i+j; // seg j on outer row i
00641       }
00642    }
00643    if (hasrmin) {
00644       indp = (2*n-1)*(n-1); // start index of inner segments
00645       // inner surface = (n-1)*(n-1) -> [(n-1)*(n-1), 2*(n-1)*(n-1)-1]
00646       // normal pointing out
00647       for (i=0; i<n-1; i++) {
00648          for (j=0; j<n-1; j++) {
00649             buff.fPols[indx++] = c;
00650             buff.fPols[indx++] = 4;
00651             buff.fPols[indx++] = indp+n*(n-1)+(n-1)*i+j; // generator j on inner row i
00652             buff.fPols[indx++] = indp+(n-1)*(i+1)+j; // seg j on inner row i+1
00653             buff.fPols[indx++] = indp+n*(n-1)+(n-1)*i+((j+1)%(n-1)); // generator j+1 on inner r>
00654             buff.fPols[indx++] = indp+(n-1)*i+j; // seg j on inner row i
00655          }
00656       }
00657    }
00658    if (hasphi) {
00659       // endcaps = 2*(n-1) -> [2*(n-1)*(n-1), 2*n*(n-1)-1]
00660       i=0; // row 0
00661       Int_t np = (hasrmin)?4:3;
00662       for (j=0; j<n-1; j++) {
00663          buff.fPols[indx++] = c+1;
00664          buff.fPols[indx++] = np;
00665          buff.fPols[indx++] = j;         // seg j on outer row 0  a
00666          buff.fPols[indx++] = startcap+j;        // endcap j on row 0  d
00667          if(hasrmin)
00668             buff.fPols[indx++] = indp+j; // seg j on inner row 0  c
00669          buff.fPols[indx++] = startcap+((j+1)%(n-1)); // endcap j+1 on row 0  b
00670       }
00671 
00672       i=n-1; // row n-1
00673       for (j=0; j<n-1; j++) {
00674          buff.fPols[indx++] = c+1;
00675          buff.fPols[indx++] = np;
00676          buff.fPols[indx++] = (n-1)*i+j;         // seg j on outer row n-1 a
00677          buff.fPols[indx++] = startcap+(n-1)+((j+1)%(n-1));    // endcap j+1 on row n-1 d
00678          if (hasrmin)
00679             buff.fPols[indx++] = indp+(n-1)*i+j; // seg j on inner row n-1 c
00680          buff.fPols[indx++] = startcap+(n-1)+j;      // endcap j on row n-1 b
00681       }
00682    }
00683 }
00684 
00685 //_____________________________________________________________________________
00686 Double_t TGeoTorus::Safety(Double_t *point, Bool_t in) const
00687 {
00688 // computes the closest distance from given point to this shape, according
00689 // to option. The matching point on the shape is stored in spoint.
00690    Double_t saf[2];
00691    Int_t i;
00692    Double_t rxy = TMath::Sqrt(point[0]*point[0]+point[1]*point[1]);
00693    Double_t rad = TMath::Sqrt((rxy-fR)*(rxy-fR) + point[2]*point[2]);
00694    saf[0] = rad-fRmin;
00695    saf[1] = fRmax-rad;
00696    if (TGeoShape::IsSameWithinTolerance(fDphi,360)) {
00697       if (in) return TMath::Min(saf[0],saf[1]);
00698       for (i=0; i<2; i++) saf[i]=-saf[i];
00699       return TMath::Max(saf[0], saf[1]);
00700    }   
00701 
00702    Double_t safphi = TGeoShape::SafetyPhi(point,in,fPhi1, fPhi1+fDphi);
00703    Double_t safe = TGeoShape::Big();
00704    if (in) {
00705       safe = TMath::Min(saf[0], saf[1]);
00706       return TMath::Min(safe, safphi);
00707    }   
00708    for (i=0; i<2; i++) saf[i]=-saf[i];
00709    safe = TMath::Max(saf[0], saf[1]);
00710    return TMath::Max(safe, safphi);
00711 }
00712 
00713 //_____________________________________________________________________________
00714 void TGeoTorus::SavePrimitive(ostream &out, Option_t * /*option*/ /*= ""*/)
00715 {
00716 // Save a primitive as a C++ statement(s) on output stream "out".
00717    if (TObject::TestBit(kGeoSavePrimitive)) return;  
00718    out << "   // Shape: " << GetName() << " type: " << ClassName() << endl;
00719    out << "   r    = " << fR << ";" << endl;
00720    out << "   rmin = " << fRmin << ";" << endl;
00721    out << "   rmax = " << fRmax << ";" << endl;
00722    out << "   phi1 = " << fPhi1 << ";" << endl;
00723    out << "   dphi = " << fDphi << ";" << endl;
00724    out << "   TGeoShape *" << GetPointerName() << " = new TGeoTorus(\"" << GetName() << "\",r,rmin,rmax,phi1,dphi);" << endl;
00725    TObject::SetBit(TGeoShape::kGeoSavePrimitive);
00726 }
00727 
00728 //_____________________________________________________________________________
00729 void TGeoTorus::SetTorusDimensions(Double_t r, Double_t rmin, Double_t rmax,
00730                           Double_t phi1, Double_t dphi)
00731 {
00732 // Set torus dimensions.
00733    fR = r;
00734    fRmin = rmin;
00735    fRmax = rmax;
00736    fPhi1 = phi1;
00737    if (fPhi1<0) fPhi1+=360.;
00738    fDphi = dphi;
00739 }
00740 
00741 //_____________________________________________________________________________
00742 void TGeoTorus::SetDimensions(Double_t *param)
00743 {
00744 // Set torus dimensions starting from a list.
00745    SetTorusDimensions(param[0], param[1], param[2], param[3], param[4]);
00746 }
00747 
00748 //_____________________________________________________________________________
00749 void TGeoTorus::SetPoints(Double_t *points) const
00750 {
00751 // Create torus mesh points
00752    if (!points) return;
00753    Int_t n = gGeoManager->GetNsegments()+1;
00754    Double_t phin, phout;
00755    Double_t dpin = 360./(n-1);
00756    Double_t dpout = fDphi/(n-1);
00757    Double_t co,so,ci,si;
00758    Bool_t havermin = (fRmin<TGeoShape::Tolerance())?kFALSE:kTRUE;
00759    Int_t i,j;
00760    Int_t indx = 0;
00761    // loop outer mesh -> n*(n-1) points [0, 3*n*(n-1)-1]
00762    for (i=0; i<n; i++) {
00763       phout = (fPhi1+i*dpout)*TMath::DegToRad();
00764       co = TMath::Cos(phout);
00765       so = TMath::Sin(phout);
00766       for (j=0; j<n-1; j++) {
00767          phin = j*dpin*TMath::DegToRad();
00768          ci = TMath::Cos(phin);
00769          si = TMath::Sin(phin);
00770          points[indx++] = (fR+fRmax*ci)*co;
00771          points[indx++] = (fR+fRmax*ci)*so;
00772          points[indx++] = fRmax*si;
00773       }
00774    }     
00775     
00776    if (havermin) {
00777     // loop inner mesh -> n*(n-1) points [3*n*(n-1), 6*n*(n-1)]
00778       for (i=0; i<n; i++) {
00779          phout = (fPhi1+i*dpout)*TMath::DegToRad();
00780          co = TMath::Cos(phout);
00781          so = TMath::Sin(phout);
00782          for (j=0; j<n-1; j++) {
00783             phin = j*dpin*TMath::DegToRad();
00784             ci = TMath::Cos(phin);
00785             si = TMath::Sin(phin);
00786             points[indx++] = (fR+fRmin*ci)*co;
00787             points[indx++] = (fR+fRmin*ci)*so;
00788             points[indx++] = fRmin*si;
00789          }
00790       }  
00791    } else {
00792       if (fDphi<360.) {
00793       // just add extra 2 points on the centers of the 2 phi cuts [3*n*n, 3*n*n+1]
00794          points[indx++] = fR*TMath::Cos(fPhi1*TMath::DegToRad());
00795          points[indx++] = fR*TMath::Sin(fPhi1*TMath::DegToRad());
00796          points[indx++] = 0;
00797          points[indx++] = fR*TMath::Cos((fPhi1+fDphi)*TMath::DegToRad());
00798          points[indx++] = fR*TMath::Sin((fPhi1+fDphi)*TMath::DegToRad());
00799          points[indx++] = 0;
00800       }
00801    }      
00802 }        
00803 
00804 //_____________________________________________________________________________
00805 void TGeoTorus::SetPoints(Float_t *points) const
00806 {
00807 // Create torus mesh points
00808    if (!points) return;
00809    Int_t n = gGeoManager->GetNsegments()+1;
00810    Double_t phin, phout;
00811    Double_t dpin = 360./(n-1);
00812    Double_t dpout = fDphi/(n-1);
00813    Double_t co,so,ci,si;
00814    Bool_t havermin = (fRmin<TGeoShape::Tolerance())?kFALSE:kTRUE;
00815    Int_t i,j;
00816    Int_t indx = 0;
00817    // loop outer mesh -> n*(n-1) points [0, 3*n*(n-1)-1]
00818    // plane i = 0, n-1  point j = 0, n-1  ipoint = n*i + j
00819    for (i=0; i<n; i++) {
00820       phout = (fPhi1+i*dpout)*TMath::DegToRad();
00821       co = TMath::Cos(phout);
00822       so = TMath::Sin(phout);
00823       for (j=0; j<n-1; j++) {
00824          phin = j*dpin*TMath::DegToRad();
00825          ci = TMath::Cos(phin);
00826          si = TMath::Sin(phin);
00827          points[indx++] = (fR+fRmax*ci)*co;
00828          points[indx++] = (fR+fRmax*ci)*so;
00829          points[indx++] = fRmax*si;
00830       }
00831    }     
00832     
00833    if (havermin) {
00834     // loop inner mesh -> n*(n-1) points [3*n*(n-1), 6*n*(n-1)]
00835       // plane i = 0, n-1  point j = 0, n-1  ipoint = n*n + n*i + j
00836       for (i=0; i<n; i++) {
00837          phout = (fPhi1+i*dpout)*TMath::DegToRad();
00838          co = TMath::Cos(phout);
00839          so = TMath::Sin(phout);
00840          for (j=0; j<n-1; j++) {
00841             phin = j*dpin*TMath::DegToRad();
00842             ci = TMath::Cos(phin);
00843             si = TMath::Sin(phin);
00844             points[indx++] = (fR+fRmin*ci)*co;
00845             points[indx++] = (fR+fRmin*ci)*so;
00846             points[indx++] = fRmin*si;
00847          }
00848       }  
00849    } else {
00850       if (fDphi<360.) {
00851       // just add extra 2 points on the centers of the 2 phi cuts [n*n, n*n+1]
00852       // ip1 = n*(n-1) + 0;
00853       // ip2 = n*(n-1) + 1
00854          points[indx++] = fR*TMath::Cos(fPhi1*TMath::DegToRad());
00855          points[indx++] = fR*TMath::Sin(fPhi1*TMath::DegToRad());
00856          points[indx++] = 0;
00857          points[indx++] = fR*TMath::Cos((fPhi1+fDphi)*TMath::DegToRad());
00858          points[indx++] = fR*TMath::Sin((fPhi1+fDphi)*TMath::DegToRad());
00859          points[indx++] = 0;
00860       }
00861    }      
00862 }        
00863 
00864 //_____________________________________________________________________________
00865 Int_t TGeoTorus::GetNmeshVertices() const
00866 {
00867 // Return number of vertices of the mesh representation
00868    Int_t n = gGeoManager->GetNsegments()+1;
00869    Int_t numPoints = n*(n-1);
00870    if (fRmin>TGeoShape::Tolerance()) numPoints *= 2;
00871    else if (fDphi<360.)              numPoints += 2;
00872    return numPoints;
00873 }
00874 
00875 //_____________________________________________________________________________
00876 void TGeoTorus::Sizeof3D() const
00877 {
00878 ///// fill size of this 3-D object
00879 ///   TVirtualGeoPainter *painter = gGeoManager->GetGeomPainter();
00880 ///   if (!painter) return;
00881 ///   Int_t n = gGeoManager->GetNsegments()+1;
00882 ///   Int_t numPoints = n*(n-1);
00883 ///   Int_t numSegs   = (2*n-1)*(n-1);
00884 ///   Int_t numPolys  = (n-1)*(n-1);
00885 ///
00886 ///   Bool_t hasrmin = (fRmin>0)?kTRUE:kFALSE;
00887 ///   Bool_t hasphi  = (fDphi<360)?kTRUE:kFALSE;
00888 ///   if (hasrmin) numPoints *= 2;
00889 ///   else if (hasphi) numPoints += 2;
00890 ///   if (hasrmin) {
00891 ///      numSegs   += (2*n-1)*(n-1);
00892 ///      numPolys  += (n-1)*(n-1);
00893 ///   }   
00894 ///   if (hasphi) {
00895 ///      numSegs   += 2*(n-1);
00896 ///      numPolys  += 2*(n-1);
00897 ///   }   
00898 ///    
00899 ///   painter->AddSize3D(numPoints, numSegs, numPolys);
00900 }
00901 
00902 //_____________________________________________________________________________
00903 Int_t TGeoTorus::SolveCubic(Double_t a, Double_t b, Double_t c, Double_t *x) const
00904 {
00905 // Find real solutions of the cubic equation : x^3 + a*x^2 + b*x + c = 0
00906 // Input: a,b,c
00907 // Output: x[3] real solutions
00908 // Returns number of real solutions (1 or 3)
00909    const Double_t ott = 1./3.;
00910    const Double_t sq3 = TMath::Sqrt(3.);
00911    Int_t ireal = 1;
00912    Double_t p = b-a*a*ott;
00913    Double_t q = c-a*b*ott+2.*a*a*a*ott*ott*ott;
00914    Double_t delta = 4*p*p*p+27*q*q;
00915 //   Double_t y1r, y1i, y2r, y2i;
00916    Double_t t,u;
00917    if (delta>=0) {
00918       delta = TMath::Sqrt(delta);
00919       t = (-3*q*sq3+delta)/(6*sq3);
00920       u = (3*q*sq3+delta)/(6*sq3);
00921       x[0] = TMath::Sign(1.,t)*TMath::Power(TMath::Abs(t),ott)-
00922              TMath::Sign(1.,u)*TMath::Power(TMath::Abs(u),ott)-a*ott;
00923    } else {
00924       delta = TMath::Sqrt(-delta);
00925       t = -0.5*q;
00926       u = delta/(6*sq3);
00927       x[0] = 2.*TMath::Power(t*t+u*u,0.5*ott) * TMath::Cos(ott*TMath::ATan2(u,t));
00928       x[0] -= a*ott;
00929    }   
00930          
00931    t = x[0]*x[0]+a*x[0]+b;
00932    u = a+x[0];
00933    delta = u*u-4.*t;
00934    if (delta>=0) {
00935       ireal = 3;
00936       delta = TMath::Sqrt(delta);
00937       x[1] = 0.5*(-u-delta);
00938       x[2] = 0.5*(-u+delta);
00939    }
00940    return ireal;
00941 }
00942 
00943 //_____________________________________________________________________________
00944 Int_t TGeoTorus::SolveQuartic(Double_t a, Double_t b, Double_t c, Double_t d, Double_t *x) const
00945 {
00946 // Find real solutions of the quartic equation : x^4 + a*x^3 + b*x^2 + c*x + d = 0
00947 // Input: a,b,c,d
00948 // Output: x[4] - real solutions
00949 // Returns number of real solutions (0 to 3)
00950    Double_t e = b-3.*a*a/8.;
00951    Double_t f = c+a*a*a/8.-0.5*a*b;
00952    Double_t g = d-3.*a*a*a*a/256. + a*a*b/16. - a*c/4.;
00953    Double_t xx[4];
00954    Int_t    ind[4];
00955    Double_t delta;
00956    Double_t h=0;
00957    Int_t ireal = 0;
00958    Int_t i;
00959    if (TGeoShape::IsSameWithinTolerance(f,0)) {
00960       delta = e*e-4.*g;
00961       if (delta<0) return 0;
00962       delta = TMath::Sqrt(delta);
00963       h = 0.5*(-e-delta);
00964       if (h>=0) {
00965          h = TMath::Sqrt(h);
00966          x[ireal++] = -h-0.25*a;
00967          x[ireal++] = h-0.25*a;
00968       }
00969       h = 0.5*(-e+delta);
00970       if (h>=0) {
00971          h = TMath::Sqrt(h);
00972          x[ireal++] = -h-0.25*a;
00973          x[ireal++] = h-0.25*a;
00974       }
00975       if (ireal>0) {
00976          TMath::Sort(ireal, x, ind,kFALSE);
00977          for (i=0; i<ireal; i++) xx[i] = x[ind[i]];
00978          memcpy(x,xx,ireal*sizeof(Double_t));
00979       }
00980       return ireal; 
00981    }     
00982    
00983    if (TGeoShape::IsSameWithinTolerance(g,0)) {
00984       x[ireal++] = -0.25*a;
00985       ind[0] = SolveCubic(0,e,f,xx);
00986       for (i=0; i<ind[0]; i++) x[ireal++] = xx[i]-0.25*a;      
00987       if (ireal>0) {
00988          TMath::Sort(ireal, x, ind,kFALSE);
00989          for (i=0; i<ireal; i++) xx[i] = x[ind[i]];
00990          memcpy(x,xx,ireal*sizeof(Double_t));
00991       }
00992       return ireal;
00993    }    
00994       
00995       
00996    ireal = SolveCubic(2.*e, e*e-4.*g, -f*f, xx);
00997    if (ireal==1) {
00998       if (xx[0]<=0) return 0;
00999       h = TMath::Sqrt(xx[0]);   
01000    } else {
01001       // 3 real solutions of the cubic
01002       for (i=0; i<3; i++) {
01003          h = xx[i];
01004          if (h>=0) break;
01005       }
01006       if (h<=0) return 0;
01007       h = TMath::Sqrt(h);
01008    }
01009    Double_t j = 0.5*(e+h*h-f/h);      
01010    ireal = 0;
01011    delta = h*h-4.*j;
01012    if (delta>=0) {
01013       delta = TMath::Sqrt(delta);
01014       x[ireal++] = 0.5*(-h-delta)-0.25*a;
01015       x[ireal++] = 0.5*(-h+delta)-0.25*a;
01016    }
01017    delta = h*h-4.*g/j;
01018    if (delta>=0) {
01019       delta = TMath::Sqrt(delta);
01020       x[ireal++] = 0.5*(h-delta)-0.25*a;
01021       x[ireal++] = 0.5*(h+delta)-0.25*a;
01022    }
01023    if (ireal>0) {
01024       TMath::Sort(ireal, x, ind,kFALSE);
01025       for (i=0; i<ireal; i++) xx[i] = x[ind[i]];
01026       memcpy(x,xx,ireal*sizeof(Double_t));
01027    }
01028    return ireal;
01029 }
01030 
01031 //_____________________________________________________________________________
01032 Double_t TGeoTorus::ToBoundary(Double_t *pt, Double_t *dir, Double_t r) const
01033 {
01034 // Returns distance to the surface or the torus (fR,r) from a point, along
01035 // a direction. Point is close enough to the boundary so that the distance 
01036 // to the torus is decreasing while moving along the given direction.
01037    
01038    // Compute coeficients of the quartic
01039    Double_t s = TGeoShape::Big();
01040    Double_t r0sq  = pt[0]*pt[0]+pt[1]*pt[1]+pt[2]*pt[2];
01041    Double_t rdotn = pt[0]*dir[0]+pt[1]*dir[1]+pt[2]*dir[2];
01042    Double_t rsumsq = fR*fR+r*r;
01043    Double_t a = 4.*rdotn;
01044    Double_t b = 2.*(r0sq+2.*rdotn*rdotn-rsumsq+2.*fR*fR*dir[2]*dir[2]);
01045    Double_t c = 4.*(r0sq*rdotn-rsumsq*rdotn+2.*fR*fR*pt[2]*dir[2]);
01046    Double_t d = r0sq*r0sq-2.*r0sq*rsumsq+4.*fR*fR*pt[2]*pt[2]+(fR*fR-r*r)*(fR*fR-r*r);
01047    
01048    Double_t x[4];
01049    Int_t nsol = 0;
01050 
01051    if (TMath::Abs(dir[2])<1.E-3 && TMath::Abs(pt[2])<r) {
01052       Double_t r0 = fR - TMath::Sqrt((r-pt[2])*(r+pt[2]));
01053       Double_t b0 = (pt[0]*dir[0]+pt[1]*dir[1])/(dir[0]*dir[0]+dir[1]*dir[1]);
01054       Double_t c0 = (pt[0]*pt[0] + (pt[1]-r0)*(pt[1]+r0))/(dir[0]*dir[0]+dir[1]*dir[1]);
01055       Double_t delta = b0*b0-c0;
01056       if (delta>0) {
01057          x[nsol] = -b0-TMath::Sqrt(delta);
01058          if (x[nsol]>0) nsol++;
01059          else {
01060             x[nsol] = -b0+TMath::Sqrt(delta);
01061             if (x[nsol]>0) nsol++;
01062          }
01063       }
01064       r0 = fR + TMath::Sqrt((r-pt[2])*(r+pt[2]));
01065       c0 = (pt[0]*pt[0] + (pt[1]-r0)*(pt[1]+r0))/(dir[0]*dir[0]+dir[1]*dir[1]);
01066       delta = b0*b0-c0;
01067       if (delta>0) {
01068          x[nsol] = -b0-TMath::Sqrt(delta);
01069          if (x[nsol]>0) nsol++;
01070          else {
01071             x[nsol] = -b0+TMath::Sqrt(delta);
01072             if (x[nsol]>0) nsol++;
01073          }
01074       }
01075       if (nsol==2) {
01076          x[0] = TMath::Min(x[0], x[1]);
01077          nsol = 1;
01078       }
01079    } else {               
01080       nsol = SolveQuartic(a,b,c,d,x);
01081    }   
01082    if (!nsol) return TGeoShape::Big();
01083    // look for first positive solution
01084    for (Int_t i=0; i<nsol; i++) {
01085       if (x[i]<1.e-7) continue;
01086       s = x[i];
01087       Double_t eps = TGeoShape::Big();
01088       Double_t delta = s*s*s*s + a*s*s*s + b*s*s + c*s + d;
01089       Double_t eps0 = -delta/(4.*s*s*s + 3.*a*s*s + 2.*b*s + c);
01090       while (TMath::Abs(eps)>TGeoShape::Tolerance()) {
01091          if ((s+eps0)<TGeoShape::Tolerance()) break;
01092          s += eps0;
01093          delta = s*s*s*s + a*s*s*s + b*s*s + c*s + d;
01094          eps = -delta/(4.*s*s*s + 3.*a*s*s + 2.*b*s + c);
01095          if (TMath::Abs(eps)>TMath::Abs(eps0)) break;
01096          eps0 = eps;
01097       }
01098       return s;   
01099    }
01100    return TGeoShape::Big();   
01101 }      
01102 
01103 //_____________________________________________________________________________
01104 void TGeoTorus::GetMeshNumbers(Int_t &nvert, Int_t &nsegs, Int_t &npols) const
01105 {
01106 // Returns numbers of vertices, segments and polygons composing the shape mesh.
01107    Int_t n = gGeoManager->GetNsegments()+1;
01108    nvert = n*(n-1);
01109    Bool_t hasrmin = (GetRmin()>0)?kTRUE:kFALSE;
01110    Bool_t hasphi  = (GetDphi()<360)?kTRUE:kFALSE;
01111    if (hasrmin) nvert *= 2;
01112    else if (hasphi) nvert += 2;
01113    nsegs = (2*n-1)*(n-1);
01114    npols = (n-1)*(n-1);
01115    if (hasrmin) {
01116       nsegs += (2*n-1)*(n-1);
01117       npols += (n-1)*(n-1);
01118    }
01119    if (hasphi) {
01120       nsegs += 2*(n-1);
01121       npols += 2*(n-1);
01122    }
01123 }
01124 
01125 //_____________________________________________________________________________
01126 const TBuffer3D & TGeoTorus::GetBuffer3D(Int_t reqSections, Bool_t localFrame) const
01127 {
01128 // Fills a static 3D buffer and returns a reference.
01129    static TBuffer3D buffer(TBuffer3DTypes::kGeneric);
01130 
01131    TGeoBBox::FillBuffer3D(buffer, reqSections, localFrame);
01132 
01133    if (reqSections & TBuffer3D::kRawSizes) {
01134       Int_t n = gGeoManager->GetNsegments()+1;
01135       Int_t nbPnts = n*(n-1);
01136       Bool_t hasrmin = (GetRmin()>0)?kTRUE:kFALSE;
01137       Bool_t hasphi  = (GetDphi()<360)?kTRUE:kFALSE;
01138       if (hasrmin) nbPnts *= 2;
01139       else if (hasphi) nbPnts += 2;
01140 
01141       Int_t nbSegs = (2*n-1)*(n-1);
01142       Int_t nbPols = (n-1)*(n-1);
01143       if (hasrmin) {
01144          nbSegs += (2*n-1)*(n-1);
01145          nbPols += (n-1)*(n-1);
01146       }
01147       if (hasphi) {
01148          nbSegs += 2*(n-1);
01149          nbPols += 2*(n-1);
01150       }
01151 
01152       if (buffer.SetRawSizes(nbPnts, 3*nbPnts, nbSegs, 3*nbSegs, nbPols, 6*nbPols)) {
01153          buffer.SetSectionsValid(TBuffer3D::kRawSizes);
01154       }
01155    }
01156    // TODO: Push down to TGeoShape?? But would have to do raw sizes set first..
01157    // can rest of TGeoShape be defered until after
01158    if ((reqSections & TBuffer3D::kRaw) && buffer.SectionsValid(TBuffer3D::kRawSizes)) {
01159       SetPoints(buffer.fPnts);
01160       if (!buffer.fLocalFrame) {
01161          TransformPoints(buffer.fPnts, buffer.NbPnts());
01162       }
01163 
01164       SetSegsAndPols(buffer);      
01165       buffer.SetSectionsValid(TBuffer3D::kRaw);
01166    }
01167       
01168    return buffer;
01169 }
01170 

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