59 TGeoTorus::TGeoTorus()
61 SetShapeBit(TGeoShape::kGeoTorus);
72 TGeoTorus::TGeoTorus(Double_t r, Double_t rmin, Double_t rmax, Double_t phi1, Double_t dphi)
75 SetShapeBit(TGeoShape::kGeoTorus);
76 SetTorusDimensions(r, rmin, rmax, phi1, dphi);
77 if ((fRmin<0) || (fRmax<0))
78 SetShapeBit(kGeoRunTimeShape);
85 TGeoTorus::TGeoTorus(
const char *name, Double_t r, Double_t rmin, Double_t rmax, Double_t phi1, Double_t dphi)
86 :TGeoBBox(name, 0, 0, 0)
88 SetShapeBit(TGeoShape::kGeoTorus);
89 SetTorusDimensions(r, rmin, rmax, phi1, dphi);
90 if ((fRmin<0) || (fRmax<0))
91 SetShapeBit(kGeoRunTimeShape);
103 TGeoTorus::TGeoTorus(Double_t *param)
106 SetShapeBit(TGeoShape::kGeoTorus);
107 SetDimensions(param);
108 if (fRmin<0 || fRmax<0) SetShapeBit(kGeoRunTimeShape);
115 Double_t TGeoTorus::Capacity()
const
117 Double_t capacity = (fDphi/180.)*TMath::Pi()*TMath::Pi()*fR*(fRmax*fRmax-fRmin*fRmin);
124 void TGeoTorus::ComputeBBox()
127 if (TGeoShape::IsSameWithinTolerance(fDphi,360)) {
128 fDX = fDY = fR+fRmax;
133 xc[0] = (fR+fRmax)*TMath::Cos(fPhi1*TMath::DegToRad());
134 yc[0] = (fR+fRmax)*TMath::Sin(fPhi1*TMath::DegToRad());
135 xc[1] = (fR+fRmax)*TMath::Cos((fPhi1+fDphi)*TMath::DegToRad());
136 yc[1] = (fR+fRmax)*TMath::Sin((fPhi1+fDphi)*TMath::DegToRad());
137 xc[2] = (fR-fRmax)*TMath::Cos(fPhi1*TMath::DegToRad());
138 yc[2] = (fR-fRmax)*TMath::Sin(fPhi1*TMath::DegToRad());
139 xc[3] = (fR-fRmax)*TMath::Cos((fPhi1+fDphi)*TMath::DegToRad());
140 yc[3] = (fR-fRmax)*TMath::Sin((fPhi1+fDphi)*TMath::DegToRad());
142 Double_t xmin = xc[TMath::LocMin(4, &xc[0])];
143 Double_t xmax = xc[TMath::LocMax(4, &xc[0])];
144 Double_t ymin = yc[TMath::LocMin(4, &yc[0])];
145 Double_t ymax = yc[TMath::LocMax(4, &yc[0])];
146 Double_t ddp = -fPhi1;
147 if (ddp<0) ddp+= 360;
148 if (ddp<=fDphi) xmax = fR+fRmax;
150 if (ddp<0) ddp+= 360;
151 if (ddp>360) ddp-=360;
152 if (ddp<=fDphi) ymax = fR+fRmax;
154 if (ddp<0) ddp+= 360;
155 if (ddp>360) ddp-=360;
156 if (ddp<=fDphi) xmin = -(fR+fRmax);
158 if (ddp<0) ddp+= 360;
159 if (ddp>360) ddp-=360;
160 if (ddp<=fDphi) ymin = -(fR+fRmax);
161 fOrigin[0] = (xmax+xmin)/2;
162 fOrigin[1] = (ymax+ymin)/2;
171 void TGeoTorus::ComputeNormal(
const Double_t *point,
const Double_t *dir, Double_t *norm)
173 Double_t phi = TMath::ATan2(point[1],point[0]);
175 Double_t phi1 = fPhi1*TMath::DegToRad();
176 Double_t phi2 = (fPhi1+fDphi)*TMath::DegToRad();
177 Double_t c1 = TMath::Cos(phi1);
178 Double_t s1 = TMath::Sin(phi1);
179 Double_t c2 = TMath::Cos(phi2);
180 Double_t s2 = TMath::Sin(phi2);
182 Double_t daxis = Daxis(point,dir,0);
183 if ((fRmax-daxis)>1E-5) {
184 if (TGeoShape::IsSameWithinTolerance(fRmin,0) || (daxis-fRmin)>1E-5) {
185 TGeoShape::NormalPhi(point,dir,norm,c1,s1,c2,s2);
191 r0[0] = fR*TMath::Cos(phi);
192 r0[1] = fR*TMath::Sin(phi);
195 for (Int_t i=0; i<3; i++) {
196 norm[i] = point[i] - r0[i];
197 normsq += norm[i]*norm[i];
200 normsq = TMath::Sqrt(normsq);
204 if (dir[0]*norm[0]+dir[1]*norm[1]+dir[2]*norm[2] < 0) {
215 Bool_t TGeoTorus::Contains(
const Double_t *point)
const
217 if (!TGeoShape::IsSameWithinTolerance(fDphi,360)) {
218 Double_t phi = TMath::ATan2(point[1], point[0]) * TMath::RadToDeg();
219 if (phi < 0) phi+=360.0;
220 Double_t ddp = phi-fPhi1;
221 if (ddp<0) ddp+=360.;
222 if (ddp>fDphi)
return kFALSE;
225 Double_t rxy = TMath::Sqrt(point[0]*point[0]+point[1]*point[1]);
226 Double_t radsq = (rxy-fR)*(rxy-fR) + point[2]*point[2];
227 if (radsq<fRmin*fRmin)
return kFALSE;
228 if (radsq>fRmax*fRmax)
return kFALSE;
235 Int_t TGeoTorus::DistancetoPrimitive(Int_t px, Int_t py)
237 Int_t n = gGeoManager->GetNsegments()+1;
238 Int_t numPoints = n*(n-1);
239 if (fRmin>0) numPoints *= 2;
240 else if (fDphi<360) numPoints += 2;
241 return ShapeDistancetoPrimitive(numPoints, px, py);
247 Double_t TGeoTorus::Daxis(
const Double_t *pt,
const Double_t *dir, Double_t t)
const
250 for (Int_t i=0; i<3; i++) p[i] = pt[i]+t*dir[i];
251 Double_t rxy = TMath::Sqrt(p[0]*p[0]+p[1]*p[1]);
252 return TMath::Sqrt((rxy-fR)*(rxy-fR)+p[2]*p[2]);
258 Double_t TGeoTorus::DDaxis(
const Double_t *pt,
const Double_t *dir, Double_t t)
const
261 for (Int_t i=0; i<3; i++) p[i] = pt[i]+t*dir[i];
262 Double_t rxy = TMath::Sqrt(p[0]*p[0]+p[1]*p[1]);
263 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]));
264 Double_t d = TMath::Sqrt((rxy-fR)*(rxy-fR)+p[2]*p[2]);
265 if (TGeoShape::IsSameWithinTolerance(d,0))
return 0.;
266 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;
273 Double_t TGeoTorus::DDDaxis(
const Double_t *pt,
const Double_t *dir, Double_t t)
const
276 for (Int_t i=0; i<3; i++) p[i] = pt[i]+t*dir[i];
277 Double_t rxy = TMath::Sqrt(p[0]*p[0]+p[1]*p[1]);
278 if (rxy<1E-6)
return 0;
279 Double_t daxis = TMath::Sqrt((rxy-fR)*(rxy-fR)+p[2]*p[2]);
280 if (TGeoShape::IsSameWithinTolerance(daxis,0))
return 0;
281 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;
282 Double_t dddaxis = 1 - ddaxis*ddaxis - (1-dir[2]*dir[2])*fR/rxy +
283 fR*(p[0]*dir[0]+p[1]*dir[1])*(p[0]*dir[0]+p[1]*dir[1])/(rxy*rxy*rxy);
291 Double_t TGeoTorus::DistFromInside(
const Double_t *point,
const Double_t *dir, Int_t iact, Double_t step, Double_t *safe)
const
293 if (iact<3 && safe) {
294 *safe = Safety(point, kTRUE);
295 if (iact==0)
return TGeoShape::Big();
296 if ((iact==1) && (step<=*safe))
return TGeoShape::Big();
298 Double_t snext = TGeoShape::Big();
299 Bool_t hasphi = (fDphi<360)?kTRUE:kFALSE;
300 Bool_t hasrmin = (fRmin>0)?kTRUE:kFALSE;
301 Double_t dout = ToBoundary(point,dir,fRmax,kTRUE);
303 Double_t din = (hasrmin)?ToBoundary(point,dir,fRmin,kTRUE):TGeoShape::Big();
304 snext = TMath::Min(dout,din);
305 if (snext>1E10)
return TGeoShape::Tolerance();
306 Double_t dphi = TGeoShape::Big();
309 Double_t c1,s1,c2,s2,cm,sm,cdfi;
310 Double_t phi1=fPhi1*TMath::DegToRad();
311 Double_t phi2=(fPhi1+fDphi)*TMath::DegToRad();
316 Double_t fio=0.5*(phi1+phi2);
319 cdfi = TMath::Cos(0.5*(phi2-phi1));
320 dphi = TGeoTubeSeg::DistFromInsideS(point,dir,fR-fRmax,fR+fRmax, fRmax, c1,s1,c2,s2,cm,sm,cdfi);
321 Double_t daxis = Daxis(point,dir,dphi);
322 if (daxis>=fRmin+1.E-8 && daxis<=fRmax-1.E-8) snext=TMath::Min(snext,dphi);
330 Double_t TGeoTorus::DistFromOutside(
const Double_t *point,
const Double_t *dir, Int_t iact, Double_t step, Double_t *safe)
const
332 if (iact<3 && safe) {
333 *safe = Safety(point, kFALSE);
334 if (iact==0)
return TGeoShape::Big();
335 if ((iact==1) && (step<=*safe))
return TGeoShape::Big();
338 Double_t sdist = TGeoBBox::DistFromOutside(point,dir, fDX, fDY, fDZ, fOrigin, step);
339 if (sdist>=step)
return TGeoShape::Big();
341 Bool_t hasphi = (fDphi<360)?kTRUE:kFALSE;
343 Double_t c1=0,s1=0,c2=0,s2=0,cm=0,sm=0,cdfi=0;
344 Bool_t inphi = kFALSE;
345 Double_t phi, ddp, phi1,phi2,fio;
353 phi=TMath::ATan2(point[1], point[0])*TMath::RadToDeg();;
356 if (ddp<0) ddp+=360;;
357 if (ddp<=fDphi) inphi=kTRUE;
358 phi1=fPhi1*TMath::DegToRad();
359 phi2=(fPhi1+fDphi)*TMath::DegToRad();
367 cdfi=TMath::Cos(0.5*(phi2-phi1));
370 Bool_t inbring = kFALSE;
371 if (TMath::Abs(point[2]) <= fRmax) {
372 rxy2 = point[0]*point[0]+point[1]*point[1];
373 if ((rxy2>=(fR-fRmax)*(fR-fRmax)) && (rxy2<=(fR+fRmax)*(fR+fRmax))) {
374 if (!hasphi || inphi) inbring=kTRUE;
379 Double_t dring = TGeoShape::Big();
380 Double_t eps = 1.E-8;
383 memcpy(pt,point,3*
sizeof(Double_t));
385 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);
386 else dring = TGeoTube::DistFromOutsideS(point,dir,TMath::Max(0.,fR-fRmax-eps),fR+fRmax+eps, fRmax+eps);
388 if (dring>1E10)
return TGeoShape::Big();
391 daxis = Daxis(point,dir,snext);
392 if (daxis>=fRmin && daxis<fRmax)
return snext;
394 for (i=0; i<3; i++) pt[i] = point[i]+snext*dir[i];
398 if (daxis<0) daxis = Daxis(pt,dir,0);
399 if (daxis<fRmin+1.E-8) {
404 for (i=0; i<3; i++) pt[i] += 0.1*eps*dir[i];
408 dd = ToBoundary(pt,dir, fRmin,kFALSE);
410 if (hasphi) dring = TGeoTubeSeg::DistFromInsideS(pt,dir,fR-fRmin,fR+fRmin, fRmin, c1,s1,c2,s2,cm,sm,cdfi);
411 else dring = TGeoTube::DistFromInsideS(pt,dir,fR-fRmin,fR+fRmin, fRmin);
412 if (dd<dring)
return (snext+dd);
415 for (i=0; i<3; i++) pt[i] = point[i] + snext*dir[i];
416 snext += DistFromOutside(pt,dir,3);
424 for (i=0; i<3; i++) pt[i] += 0.1*eps*dir[i];
427 dd = ToBoundary(pt, dir, fRmax, kFALSE);
428 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);
429 else dring = TGeoTube::DistFromInsideS(pt,dir,TMath::Max(0.,fR-fRmax-eps),fR+fRmax+eps, fRmax+eps);
436 for (i=0; i<3; i++) pt[i] = point[i] + snext*dir[i];
437 snext += DistFromOutside(pt,dir,3);
445 TGeoVolume *TGeoTorus::Divide(TGeoVolume * ,
const char * , Int_t , Int_t ,
446 Double_t , Double_t )
454 const char *TGeoTorus::GetAxisName(Int_t iaxis)
const
471 Double_t TGeoTorus::GetAxisRange(Int_t iaxis, Double_t &xlo, Double_t &xhi)
const
498 void TGeoTorus::GetBoundingCylinder(Double_t *param)
const
500 param[0] = (fR-fRmax);
501 param[1] = (fR+fRmax);
503 param[3] = fPhi1+fDphi;
509 TGeoShape *TGeoTorus::GetMakeRuntimeShape(TGeoShape * , TGeoMatrix * )
const
511 if (!TestShapeBit(kGeoRunTimeShape))
return 0;
512 Error(
"GetMakeRuntimeShape",
"parametrized toruses not supported");
519 void TGeoTorus::InspectShape()
const
521 printf(
"*** Shape %s: TGeoTorus ***\n", GetName());
522 printf(
" R = %11.5f\n", fR);
523 printf(
" Rmin = %11.5f\n", fRmin);
524 printf(
" Rmax = %11.5f\n", fRmax);
525 printf(
" Phi1 = %11.5f\n", fPhi1);
526 printf(
" Dphi = %11.5f\n", fDphi);
527 printf(
" Bounding box:\n");
528 TGeoBBox::InspectShape();
535 TBuffer3D *TGeoTorus::MakeBuffer3D()
const
537 Int_t n = gGeoManager->GetNsegments()+1;
538 Int_t nbPnts = n*(n-1);
539 Bool_t hasrmin = (GetRmin()>0)?kTRUE:kFALSE;
540 Bool_t hasphi = (GetDphi()<360)?kTRUE:kFALSE;
541 if (hasrmin) nbPnts *= 2;
542 else if (hasphi) nbPnts += 2;
544 Int_t nbSegs = (2*n-1)*(n-1);
545 Int_t nbPols = (n-1)*(n-1);
547 nbSegs += (2*n-1)*(n-1);
548 nbPols += (n-1)*(n-1);
555 TBuffer3D* buff =
new TBuffer3D(TBuffer3DTypes::kGeneric,
556 nbPnts, 3*nbPnts, nbSegs, 3*nbSegs, nbPols, 6*nbPols);
559 SetPoints(buff->fPnts);
560 SetSegsAndPols(*buff);
569 void TGeoTorus::SetSegsAndPols(TBuffer3D &buff)
const
572 Int_t n = gGeoManager->GetNsegments()+1;
573 Int_t nbPnts = n*(n-1);
574 Int_t indx, indp, startcap=0;
575 Bool_t hasrmin = (GetRmin()>0)?kTRUE:kFALSE;
576 Bool_t hasphi = (GetDphi()<360)?kTRUE:kFALSE;
577 if (hasrmin) nbPnts *= 2;
578 else if (hasphi) nbPnts += 2;
579 Int_t c = GetBasicColor();
582 memset(buff.fSegs, 0, buff.NbSegs()*3*
sizeof(Int_t));
587 for (i = 0; i < n; i++) {
588 for (j = 0; j < n-1; j++) {
589 buff.fSegs[indx+(i*(n-1)+j)*3] = c;
590 buff.fSegs[indx+(i*(n-1)+j)*3+1] = i*(n-1)+j;
591 buff.fSegs[indx+(i*(n-1)+j)*3+2] = i*(n-1)+((j+1)%(n-1));
597 for (i = 0; i < n-1; i++) {
598 for (j = 0; j < n-1; j++) {
599 buff.fSegs[indx+(i*(n-1)+j)*3] = c;
600 buff.fSegs[indx+(i*(n-1)+j)*3+1] = i*(n-1)+j;
601 buff.fSegs[indx+(i*(n-1)+j)*3+2] = (i+1)*(n-1)+j;
604 indx += 3*(n-1)*(n-1);
605 startcap = (2*n-1)*(n-1);
610 for (i = 0; i < n; i++) {
611 for (j = 0; j < n-1; j++) {
612 buff.fSegs[indx+(i*(n-1)+j)*3] = c;
613 buff.fSegs[indx+(i*(n-1)+j)*3+1] = indp + i*(n-1)+j;
614 buff.fSegs[indx+(i*(n-1)+j)*3+2] = indp + i*(n-1)+((j+1)%(n-1));
620 for (i = 0; i < n-1; i++) {
621 for (j = 0; j < n-1; j++) {
622 buff.fSegs[indx+(i*(n-1)+j)*3] = c;
623 buff.fSegs[indx+(i*(n-1)+j)*3+1] = indp + i*(n-1)+j;
624 buff.fSegs[indx+(i*(n-1)+j)*3+2] = indp + (i+1)*(n-1)+j;
627 indx += 3*(n-1)*(n-1);
628 startcap = (4*n-2)*(n-1);
635 for (j = 0; j < n-1; j++) {
636 buff.fSegs[indx+j*3] = c+1;
637 buff.fSegs[indx+j*3+1] = (n-1)*i+j;
638 buff.fSegs[indx+j*3+2] = indp+(n-1)*i+j;
642 for (j = 0; j < n-1; j++) {
643 buff.fSegs[indx+j*3] = c+1;
644 buff.fSegs[indx+j*3+1] = (n-1)*i+j;
645 buff.fSegs[indx+j*3+2] = indp+(n-1)*i+j;
650 for (j = 0; j < n-1; j++) {
651 buff.fSegs[indx+j*3] = c+1;
652 buff.fSegs[indx+j*3+1] = (n-1)*i+j;
653 buff.fSegs[indx+j*3+2] = n*(n-1);
657 for (j = 0; j < n-1; j++) {
658 buff.fSegs[indx+j*3] = c+1;
659 buff.fSegs[indx+j*3+1] = (n-1)*i+j;
660 buff.fSegs[indx+j*3+2] = n*(n-1)+1;
667 memset(buff.fPols, 0, buff.NbPols()*6*
sizeof(Int_t));
671 for (i=0; i<n-1; i++) {
672 for (j=0; j<n-1; j++) {
673 buff.fPols[indx++] = c;
674 buff.fPols[indx++] = 4;
675 buff.fPols[indx++] = n*(n-1)+(n-1)*i+((j+1)%(n-1));
676 buff.fPols[indx++] = (n-1)*(i+1)+j;
677 buff.fPols[indx++] = n*(n-1)+(n-1)*i+j;
678 buff.fPols[indx++] = (n-1)*i+j;
682 indp = (2*n-1)*(n-1);
685 for (i=0; i<n-1; i++) {
686 for (j=0; j<n-1; j++) {
687 buff.fPols[indx++] = c;
688 buff.fPols[indx++] = 4;
689 buff.fPols[indx++] = indp+n*(n-1)+(n-1)*i+j;
690 buff.fPols[indx++] = indp+(n-1)*(i+1)+j;
691 buff.fPols[indx++] = indp+n*(n-1)+(n-1)*i+((j+1)%(n-1));
692 buff.fPols[indx++] = indp+(n-1)*i+j;
699 Int_t np = (hasrmin)?4:3;
700 for (j=0; j<n-1; j++) {
701 buff.fPols[indx++] = c+1;
702 buff.fPols[indx++] = np;
703 buff.fPols[indx++] = j;
704 buff.fPols[indx++] = startcap+j;
706 buff.fPols[indx++] = indp+j;
707 buff.fPols[indx++] = startcap+((j+1)%(n-1));
711 for (j=0; j<n-1; j++) {
712 buff.fPols[indx++] = c+1;
713 buff.fPols[indx++] = np;
714 buff.fPols[indx++] = (n-1)*i+j;
715 buff.fPols[indx++] = startcap+(n-1)+((j+1)%(n-1));
717 buff.fPols[indx++] = indp+(n-1)*i+j;
718 buff.fPols[indx++] = startcap+(n-1)+j;
727 Double_t TGeoTorus::Safety(
const Double_t *point, Bool_t in)
const
731 Double_t rxy = TMath::Sqrt(point[0]*point[0]+point[1]*point[1]);
732 Double_t rad = TMath::Sqrt((rxy-fR)*(rxy-fR) + point[2]*point[2]);
735 if (TGeoShape::IsSameWithinTolerance(fDphi,360)) {
736 if (in)
return TMath::Min(saf[0],saf[1]);
737 for (i=0; i<2; i++) saf[i]=-saf[i];
738 return TMath::Max(saf[0], saf[1]);
741 Double_t safphi = TGeoShape::SafetyPhi(point,in,fPhi1, fPhi1+fDphi);
742 Double_t safe = TGeoShape::Big();
744 safe = TMath::Min(saf[0], saf[1]);
745 return TMath::Min(safe, safphi);
747 for (i=0; i<2; i++) saf[i]=-saf[i];
748 safe = TMath::Max(saf[0], saf[1]);
749 return TMath::Max(safe, safphi);
755 void TGeoTorus::SavePrimitive(std::ostream &out, Option_t * )
757 if (TObject::TestBit(kGeoSavePrimitive))
return;
758 out <<
" // Shape: " << GetName() <<
" type: " << ClassName() << std::endl;
759 out <<
" r = " << fR <<
";" << std::endl;
760 out <<
" rmin = " << fRmin <<
";" << std::endl;
761 out <<
" rmax = " << fRmax <<
";" << std::endl;
762 out <<
" phi1 = " << fPhi1 <<
";" << std::endl;
763 out <<
" dphi = " << fDphi <<
";" << std::endl;
764 out <<
" TGeoShape *" << GetPointerName() <<
" = new TGeoTorus(\"" << GetName() <<
"\",r,rmin,rmax,phi1,dphi);" << std::endl;
765 TObject::SetBit(TGeoShape::kGeoSavePrimitive);
771 void TGeoTorus::SetTorusDimensions(Double_t r, Double_t rmin, Double_t rmax,
772 Double_t phi1, Double_t dphi)
778 if (fPhi1<0) fPhi1+=360.;
785 void TGeoTorus::SetDimensions(Double_t *param)
787 SetTorusDimensions(param[0], param[1], param[2], param[3], param[4]);
793 void TGeoTorus::SetPoints(Double_t *points)
const
796 Int_t n = gGeoManager->GetNsegments()+1;
797 Double_t phin, phout;
798 Double_t dpin = 360./(n-1);
799 Double_t dpout = fDphi/(n-1);
800 Double_t co,so,ci,si;
801 Bool_t havermin = (fRmin<TGeoShape::Tolerance())?kFALSE:kTRUE;
805 for (i=0; i<n; i++) {
806 phout = (fPhi1+i*dpout)*TMath::DegToRad();
807 co = TMath::Cos(phout);
808 so = TMath::Sin(phout);
809 for (j=0; j<n-1; j++) {
810 phin = j*dpin*TMath::DegToRad();
811 ci = TMath::Cos(phin);
812 si = TMath::Sin(phin);
813 points[indx++] = (fR+fRmax*ci)*co;
814 points[indx++] = (fR+fRmax*ci)*so;
815 points[indx++] = fRmax*si;
821 for (i=0; i<n; i++) {
822 phout = (fPhi1+i*dpout)*TMath::DegToRad();
823 co = TMath::Cos(phout);
824 so = TMath::Sin(phout);
825 for (j=0; j<n-1; j++) {
826 phin = j*dpin*TMath::DegToRad();
827 ci = TMath::Cos(phin);
828 si = TMath::Sin(phin);
829 points[indx++] = (fR+fRmin*ci)*co;
830 points[indx++] = (fR+fRmin*ci)*so;
831 points[indx++] = fRmin*si;
837 points[indx++] = fR*TMath::Cos(fPhi1*TMath::DegToRad());
838 points[indx++] = fR*TMath::Sin(fPhi1*TMath::DegToRad());
840 points[indx++] = fR*TMath::Cos((fPhi1+fDphi)*TMath::DegToRad());
841 points[indx++] = fR*TMath::Sin((fPhi1+fDphi)*TMath::DegToRad());
850 void TGeoTorus::SetPoints(Float_t *points)
const
853 Int_t n = gGeoManager->GetNsegments()+1;
854 Double_t phin, phout;
855 Double_t dpin = 360./(n-1);
856 Double_t dpout = fDphi/(n-1);
857 Double_t co,so,ci,si;
858 Bool_t havermin = (fRmin<TGeoShape::Tolerance())?kFALSE:kTRUE;
863 for (i=0; i<n; i++) {
864 phout = (fPhi1+i*dpout)*TMath::DegToRad();
865 co = TMath::Cos(phout);
866 so = TMath::Sin(phout);
867 for (j=0; j<n-1; j++) {
868 phin = j*dpin*TMath::DegToRad();
869 ci = TMath::Cos(phin);
870 si = TMath::Sin(phin);
871 points[indx++] = (fR+fRmax*ci)*co;
872 points[indx++] = (fR+fRmax*ci)*so;
873 points[indx++] = fRmax*si;
880 for (i=0; i<n; i++) {
881 phout = (fPhi1+i*dpout)*TMath::DegToRad();
882 co = TMath::Cos(phout);
883 so = TMath::Sin(phout);
884 for (j=0; j<n-1; j++) {
885 phin = j*dpin*TMath::DegToRad();
886 ci = TMath::Cos(phin);
887 si = TMath::Sin(phin);
888 points[indx++] = (fR+fRmin*ci)*co;
889 points[indx++] = (fR+fRmin*ci)*so;
890 points[indx++] = fRmin*si;
898 points[indx++] = fR*TMath::Cos(fPhi1*TMath::DegToRad());
899 points[indx++] = fR*TMath::Sin(fPhi1*TMath::DegToRad());
901 points[indx++] = fR*TMath::Cos((fPhi1+fDphi)*TMath::DegToRad());
902 points[indx++] = fR*TMath::Sin((fPhi1+fDphi)*TMath::DegToRad());
911 Int_t TGeoTorus::GetNmeshVertices()
const
913 Int_t n = gGeoManager->GetNsegments()+1;
914 Int_t numPoints = n*(n-1);
915 if (fRmin>TGeoShape::Tolerance()) numPoints *= 2;
916 else if (fDphi<360.) numPoints += 2;
923 void TGeoTorus::Sizeof3D()
const
933 Int_t TGeoTorus::SolveCubic(Double_t a, Double_t b, Double_t c, Double_t *x)
const
935 const Double_t ott = 1./3.;
936 const Double_t sq3 = TMath::Sqrt(3.);
938 Double_t p = b-a*a*ott;
939 Double_t q = c-a*b*ott+2.*a*a*a*ott*ott*ott;
940 Double_t delta = 4*p*p*p+27*q*q;
944 delta = TMath::Sqrt(delta);
945 t = (-3*q*sq3+delta)/(6*sq3);
946 u = (3*q*sq3+delta)/(6*sq3);
947 x[0] = TMath::Sign(1.,t)*TMath::Power(TMath::Abs(t),ott)-
948 TMath::Sign(1.,u)*TMath::Power(TMath::Abs(u),ott)-a*ott;
950 delta = TMath::Sqrt(-delta);
953 x[0] = 2.*TMath::Power(t*t+u*u,0.5*ott) * TMath::Cos(ott*TMath::ATan2(u,t));
957 t = x[0]*x[0]+a*x[0]+b;
962 delta = TMath::Sqrt(delta);
963 x[1] = 0.5*(-u-delta);
964 x[2] = 0.5*(-u+delta);
975 Int_t TGeoTorus::SolveQuartic(Double_t a, Double_t b, Double_t c, Double_t d, Double_t *x)
const
977 Double_t e = b-3.*a*a/8.;
978 Double_t f = c+a*a*a/8.-0.5*a*b;
979 Double_t g = d-3.*a*a*a*a/256. + a*a*b/16. - a*c/4.;
986 if (TGeoShape::IsSameWithinTolerance(f,0)) {
988 if (delta<0)
return 0;
989 delta = TMath::Sqrt(delta);
993 x[ireal++] = -h-0.25*a;
994 x[ireal++] = h-0.25*a;
999 x[ireal++] = -h-0.25*a;
1000 x[ireal++] = h-0.25*a;
1003 TMath::Sort(ireal, x, ind,kFALSE);
1004 for (i=0; i<ireal; i++) xx[i] = x[ind[i]];
1005 memcpy(x,xx,ireal*
sizeof(Double_t));
1010 if (TGeoShape::IsSameWithinTolerance(g,0)) {
1011 x[ireal++] = -0.25*a;
1012 ind[0] = SolveCubic(0,e,f,xx);
1013 for (i=0; i<ind[0]; i++) x[ireal++] = xx[i]-0.25*a;
1015 TMath::Sort(ireal, x, ind,kFALSE);
1016 for (i=0; i<ireal; i++) xx[i] = x[ind[i]];
1017 memcpy(x,xx,ireal*
sizeof(Double_t));
1023 ireal = SolveCubic(2.*e, e*e-4.*g, -f*f, xx);
1025 if (xx[0]<=0)
return 0;
1026 h = TMath::Sqrt(xx[0]);
1029 for (i=0; i<3; i++) {
1036 Double_t j = 0.5*(e+h*h-f/h);
1040 delta = TMath::Sqrt(delta);
1041 x[ireal++] = 0.5*(-h-delta)-0.25*a;
1042 x[ireal++] = 0.5*(-h+delta)-0.25*a;
1046 delta = TMath::Sqrt(delta);
1047 x[ireal++] = 0.5*(h-delta)-0.25*a;
1048 x[ireal++] = 0.5*(h+delta)-0.25*a;
1051 TMath::Sort(ireal, x, ind,kFALSE);
1052 for (i=0; i<ireal; i++) xx[i] = x[ind[i]];
1053 memcpy(x,xx,ireal*
sizeof(Double_t));
1063 Double_t TGeoTorus::ToBoundary(
const Double_t *pt,
const Double_t *dir, Double_t r, Bool_t in)
const
1066 Double_t s = TGeoShape::Big();
1067 Double_t tol = TGeoShape::Tolerance();
1068 Double_t r0sq = pt[0]*pt[0]+pt[1]*pt[1]+pt[2]*pt[2];
1069 Double_t rdotn = pt[0]*dir[0]+pt[1]*dir[1]+pt[2]*dir[2];
1070 Double_t rsumsq = fR*fR+r*r;
1071 Double_t a = 4.*rdotn;
1072 Double_t b = 2.*(r0sq+2.*rdotn*rdotn-rsumsq+2.*fR*fR*dir[2]*dir[2]);
1073 Double_t c = 4.*(r0sq*rdotn-rsumsq*rdotn+2.*fR*fR*pt[2]*dir[2]);
1074 Double_t d = r0sq*r0sq-2.*r0sq*rsumsq+4.*fR*fR*pt[2]*pt[2]+(fR*fR-r*r)*(fR*fR-r*r);
1079 if (TMath::Abs(dir[2])<1E-3 && TMath::Abs(pt[2])<0.1*r) {
1080 Double_t r0 = fR - TMath::Sqrt((r-pt[2])*(r+pt[2]));
1081 Double_t b0 = (pt[0]*dir[0]+pt[1]*dir[1])/(dir[0]*dir[0]+dir[1]*dir[1]);
1082 Double_t c0 = (pt[0]*pt[0] + (pt[1]-r0)*(pt[1]+r0))/(dir[0]*dir[0]+dir[1]*dir[1]);
1083 Double_t delta = b0*b0-c0;
1085 y[nsol] = -b0-TMath::Sqrt(delta);
1086 if (y[nsol]>-tol) nsol++;
1087 y[nsol] = -b0+TMath::Sqrt(delta);
1088 if (y[nsol]>-tol) nsol++;
1090 r0 = fR + TMath::Sqrt((r-pt[2])*(r+pt[2]));
1091 c0 = (pt[0]*pt[0] + (pt[1]-r0)*(pt[1]+r0))/(dir[0]*dir[0]+dir[1]*dir[1]);
1094 y[nsol] = -b0-TMath::Sqrt(delta);
1095 if (y[nsol]>-tol) nsol++;
1096 y[nsol] = -b0+TMath::Sqrt(delta);
1097 if (y[nsol]>-tol) nsol++;
1102 TMath::Sort(nsol, y, ind,kFALSE);
1103 for (Int_t j=0; j<nsol; j++) x[j] = y[ind[j]];
1106 nsol = SolveQuartic(a,b,c,d,x);
1108 if (!nsol)
return TGeoShape::Big();
1110 Double_t phi, ndotd;
1111 Double_t r0[3], norm[3];
1112 Bool_t inner = (TMath::Abs(r-fRmin)<TGeoShape::Tolerance())?kTRUE:kFALSE;
1113 for (Int_t i=0; i<nsol; i++) {
1114 if (x[i]<-10)
continue;
1115 phi = TMath::ATan2(pt[1]+x[i]*dir[1],pt[0]+x[i]*dir[0]);
1116 r0[0] = fR*TMath::Cos(phi);
1117 r0[1] = fR*TMath::Sin(phi);
1119 for (Int_t ipt=0; ipt<3; ipt++) norm[ipt] = pt[ipt]+x[i]*dir[ipt] - r0[ipt];
1120 ndotd = norm[0]*dir[0]+norm[1]*dir[1]+norm[2]*dir[2];
1122 if (ndotd<0)
continue;
1124 if (ndotd>0)
continue;
1127 Double_t eps = TGeoShape::Big();
1128 Double_t delta = s*s*s*s + a*s*s*s + b*s*s + c*s + d;
1129 Double_t eps0 = -delta/(4.*s*s*s + 3.*a*s*s + 2.*b*s + c);
1130 while (TMath::Abs(eps)>TGeoShape::Tolerance()) {
1131 if (TMath::Abs(eps0)>100)
break;
1133 if (TMath::Abs(s+eps0)<TGeoShape::Tolerance())
break;
1134 delta = s*s*s*s + a*s*s*s + b*s*s + c*s + d;
1135 eps = -delta/(4.*s*s*s + 3.*a*s*s + 2.*b*s + c);
1136 if (TMath::Abs(eps)>TMath::Abs(eps0))
break;
1139 if (s<-TGeoShape::Tolerance())
continue;
1140 return TMath::Max(0.,s);
1142 return TGeoShape::Big();
1148 void TGeoTorus::GetMeshNumbers(Int_t &nvert, Int_t &nsegs, Int_t &npols)
const
1150 Int_t n = gGeoManager->GetNsegments()+1;
1152 Bool_t hasrmin = (GetRmin()>0)?kTRUE:kFALSE;
1153 Bool_t hasphi = (GetDphi()<360)?kTRUE:kFALSE;
1154 if (hasrmin) nvert *= 2;
1155 else if (hasphi) nvert += 2;
1156 nsegs = (2*n-1)*(n-1);
1157 npols = (n-1)*(n-1);
1159 nsegs += (2*n-1)*(n-1);
1160 npols += (n-1)*(n-1);
1171 const TBuffer3D & TGeoTorus::GetBuffer3D(Int_t reqSections, Bool_t localFrame)
const
1173 static TBuffer3D buffer(TBuffer3DTypes::kGeneric);
1175 TGeoBBox::FillBuffer3D(buffer, reqSections, localFrame);
1177 if (reqSections & TBuffer3D::kRawSizes) {
1178 Int_t n = gGeoManager->GetNsegments()+1;
1179 Int_t nbPnts = n*(n-1);
1180 Bool_t hasrmin = (GetRmin()>0)?kTRUE:kFALSE;
1181 Bool_t hasphi = (GetDphi()<360)?kTRUE:kFALSE;
1182 if (hasrmin) nbPnts *= 2;
1183 else if (hasphi) nbPnts += 2;
1185 Int_t nbSegs = (2*n-1)*(n-1);
1186 Int_t nbPols = (n-1)*(n-1);
1188 nbSegs += (2*n-1)*(n-1);
1189 nbPols += (n-1)*(n-1);
1196 if (buffer.SetRawSizes(nbPnts, 3*nbPnts, nbSegs, 3*nbSegs, nbPols, 6*nbPols)) {
1197 buffer.SetSectionsValid(TBuffer3D::kRawSizes);
1202 if ((reqSections & TBuffer3D::kRaw) && buffer.SectionsValid(TBuffer3D::kRawSizes)) {
1203 SetPoints(buffer.fPnts);
1204 if (!buffer.fLocalFrame) {
1205 TransformPoints(buffer.fPnts, buffer.NbPnts());
1208 SetSegsAndPols(buffer);
1209 buffer.SetSectionsValid(TBuffer3D::kRaw);
1220 void TGeoTorus::Contains_v(
const Double_t *points, Bool_t *inside, Int_t vecsize)
const
1222 for (Int_t i=0; i<vecsize; i++) inside[i] = Contains(&points[3*i]);
1230 void TGeoTorus::ComputeNormal_v(
const Double_t *points,
const Double_t *dirs, Double_t *norms, Int_t vecsize)
1232 for (Int_t i=0; i<vecsize; i++) ComputeNormal(&points[3*i], &dirs[3*i], &norms[3*i]);
1238 void TGeoTorus::DistFromInside_v(
const Double_t *points,
const Double_t *dirs, Double_t *dists, Int_t vecsize, Double_t* step)
const
1240 for (Int_t i=0; i<vecsize; i++) dists[i] = DistFromInside(&points[3*i], &dirs[3*i], 3, step[i]);
1246 void TGeoTorus::DistFromOutside_v(
const Double_t *points,
const Double_t *dirs, Double_t *dists, Int_t vecsize, Double_t* step)
const
1248 for (Int_t i=0; i<vecsize; i++) dists[i] = DistFromOutside(&points[3*i], &dirs[3*i], 3, step[i]);
1256 void TGeoTorus::Safety_v(
const Double_t *points,
const Bool_t *inside, Double_t *safe, Int_t vecsize)
const
1258 for (Int_t i=0; i<vecsize; i++) safe[i] = Safety(&points[3*i], inside[i]);