53 SetShapeBit(kGeoTrd2);
54 fDz = fDx1 = fDx2 = fDy1 = fDy2 = 0;
60 TGeoTrd2::TGeoTrd2(Double_t dx1, Double_t dx2, Double_t dy1, Double_t dy2, Double_t dz)
63 SetShapeBit(kGeoTrd2);
69 if ((fDx1<0) || (fDx2<0) || (fDy1<0) || (fDy2<0) || (fDz<0)) {
70 SetShapeBit(kGeoRunTimeShape);
71 printf(
"trd2 : dx1=%f, dx2=%f, dy1=%f, dy2=%f, dz=%f\n",
80 TGeoTrd2::TGeoTrd2(
const char * name, Double_t dx1, Double_t dx2, Double_t dy1, Double_t dy2, Double_t dz)
81 :TGeoBBox(name, 0,0,0)
83 SetShapeBit(kGeoTrd2);
89 if ((fDx1<0) || (fDx2<0) || (fDy1<0) || (fDy2<0) || (fDz<0)) {
90 SetShapeBit(kGeoRunTimeShape);
91 printf(
"trd2 : dx1=%f, dx2=%f, dy1=%f, dy2=%f, dz=%f\n",
105 TGeoTrd2::TGeoTrd2(Double_t *param)
108 SetShapeBit(kGeoTrd2);
109 SetDimensions(param);
110 if ((fDx1<0) || (fDx2<0) || (fDy1<0) || (fDy2<0) || (fDz<0)) SetShapeBit(kGeoRunTimeShape);
117 TGeoTrd2::~TGeoTrd2()
124 Double_t TGeoTrd2::Capacity()
const
126 Double_t capacity = 2*(fDx1+fDx2)*(fDy1+fDy2)*fDz +
127 (2./3.)*(fDx1-fDx2)*(fDy1-fDy2)*fDz;
134 void TGeoTrd2::ComputeBBox()
136 fDX = TMath::Max(fDx1, fDx2);
137 fDY = TMath::Max(fDy1, fDy2);
139 memset(fOrigin, 0, 3*
sizeof(Double_t));
145 void TGeoTrd2::ComputeNormal(
const Double_t *point,
const Double_t *dir, Double_t *norm)
147 Double_t safe, safemin;
148 Double_t fx = 0.5*(fDx1-fDx2)/fDz;
149 Double_t calf = 1./TMath::Sqrt(1.0+fx*fx);
151 safe = safemin = TMath::Abs(fDz-TMath::Abs(point[2]));
152 norm[0] = norm[1] = 0;
153 norm[2] = (dir[2]>=0)?1:-1;
154 if (safe<TGeoShape::Tolerance())
return;
156 Double_t distx = 0.5*(fDx1+fDx2)-fx*point[2];
158 safe=TMath::Abs(distx-TMath::Abs(point[0]))*calf;
161 norm[0] = (point[0]>0)?calf:(-calf);
164 Double_t dot = norm[0]*dir[0]+norm[1]*dir[1]+norm[2]*dir[2];
169 if (safe<TGeoShape::Tolerance())
return;
173 Double_t fy = 0.5*(fDy1-fDy2)/fDz;
174 calf = 1./TMath::Sqrt(1.0+fy*fy);
177 distx = 0.5*(fDy1+fDy2)-fy*point[2];
179 safe=TMath::Abs(distx-TMath::Abs(point[1]))*calf;
182 norm[1] = (point[1]>0)?calf:(-calf);
184 Double_t dot = norm[0]*dir[0]+norm[1]*dir[1]+norm[2]*dir[2];
197 Bool_t TGeoTrd2::Contains(
const Double_t *point)
const
199 if (TMath::Abs(point[2]) > fDz)
return kFALSE;
201 Double_t dy = 0.5*(fDy2*(point[2]+fDz)+fDy1*(fDz-point[2]))/fDz;
202 if (TMath::Abs(point[1]) > dy)
return kFALSE;
204 Double_t dx = 0.5*(fDx2*(point[2]+fDz)+fDx1*(fDz-point[2]))/fDz;
205 if (TMath::Abs(point[0]) > dx)
return kFALSE;
213 Double_t TGeoTrd2::DistFromInside(
const Double_t *point,
const Double_t *dir, Int_t iact, Double_t step, Double_t *safe)
const
215 Double_t snxt = TGeoShape::Big();
216 if (iact<3 && safe) {
218 *safe = Safety(point, kTRUE);
219 if (iact==0)
return TGeoShape::Big();
220 if (iact==1 && step<*safe)
return TGeoShape::Big();
223 Double_t fx = 0.5*(fDx1-fDx2)/fDz;
224 Double_t fy = 0.5*(fDy1-fDy2)/fDz;
227 Double_t distx = 0.5*(fDx1+fDx2)-fx*point[2];
228 Double_t disty = 0.5*(fDy1+fDy2)-fy*point[2];
233 for (Int_t i=0; i<3; i++) dist[i]=TGeoShape::Big();
235 dist[0]=-(point[2]+fDz)/dir[2];
236 }
else if (dir[2]>0) {
237 dist[0]=(fDz-point[2])/dir[2];
239 if (dist[0]<=0)
return 0.0;
241 cn = -dir[0]+fx*dir[2];
243 dist[1] = point[0]+distx;
244 if (dist[1]<=0)
return 0.0;
247 cn = dir[0]+fx*dir[2];
249 Double_t s = distx-point[0];
250 if (s<=0)
return 0.0;
252 if (s<dist[1]) dist[1] = s;
255 cn = -dir[1]+fy*dir[2];
257 dist[2] = point[1]+disty;
258 if (dist[2]<=0)
return 0.0;
261 cn = dir[1]+fy*dir[2];
263 Double_t s = disty-point[1];
264 if (s<=0)
return 0.0;
266 if (s<dist[2]) dist[2] = s;
268 snxt = dist[TMath::LocMin(3,dist)];
276 Double_t TGeoTrd2::DistFromOutside(
const Double_t *point,
const Double_t *dir, Int_t iact, Double_t step, Double_t *safe)
const
278 Double_t snxt = TGeoShape::Big();
279 if (iact<3 && safe) {
281 *safe = Safety(point, kFALSE);
282 if (iact==0)
return TGeoShape::Big();
283 if (iact==1 && step<*safe)
return TGeoShape::Big();
286 Double_t xnew,ynew,znew;
287 Double_t fx = 0.5*(fDx1-fDx2)/fDz;
288 Double_t fy = 0.5*(fDy1-fDy2)/fDz;
291 Double_t distx = 0.5*(fDx1+fDx2)-fx*point[2];
292 Double_t disty = 0.5*(fDy1+fDy2)-fy*point[2];
294 Double_t safx = distx-TMath::Abs(point[0]);
295 Double_t safy = disty-TMath::Abs(point[1]);
296 Double_t safz = fDz-TMath::Abs(point[2]);
299 if (point[2]<=-fDz) {
301 if (cn>=0)
return TGeoShape::Big();
303 snxt = (fDz+point[2])/cn;
305 xnew = point[0]+snxt*dir[0];
306 if (TMath::Abs(xnew) < fDx1) {
307 ynew = point[1]+snxt*dir[1];
308 if (TMath::Abs(ynew) < fDy1)
return snxt;
310 }
else if (point[2]>=fDz) {
312 if (cn>=0)
return TGeoShape::Big();
314 snxt = (fDz-point[2])/cn;
316 xnew = point[0]+snxt*dir[0];
317 if (TMath::Abs(xnew) < fDx2) {
318 ynew = point[1]+snxt*dir[1];
319 if (TMath::Abs(ynew) < fDy2)
return snxt;
323 if (point[0]<=-distx) {
324 cn = -dir[0]+fx*dir[2];
325 if (cn>=0)
return TGeoShape::Big();
327 snxt = (point[0]+distx)/cn;
329 znew = point[2]+snxt*dir[2];
330 if (TMath::Abs(znew) < fDz) {
331 Double_t dy = 0.5*(fDy1+fDy2)-fy*znew;
332 ynew = point[1]+snxt*dir[1];
333 if (TMath::Abs(ynew) < dy)
return snxt;
336 if (point[0]>=distx) {
337 cn = dir[0]+fx*dir[2];
338 if (cn>=0)
return TGeoShape::Big();
340 snxt = (distx-point[0])/cn;
342 znew = point[2]+snxt*dir[2];
343 if (TMath::Abs(znew) < fDz) {
344 Double_t dy = 0.5*(fDy1+fDy2)-fy*znew;
345 ynew = point[1]+snxt*dir[1];
346 if (TMath::Abs(ynew) < dy)
return snxt;
350 if (point[1]<=-disty) {
351 cn = -dir[1]+fy*dir[2];
353 if (cn>=0)
return TGeoShape::Big();
354 snxt = (point[1]+disty)/cn;
356 znew = point[2]+snxt*dir[2];
357 if (TMath::Abs(znew) < fDz) {
358 Double_t dx = 0.5*(fDx1+fDx2)-fx*znew;
359 xnew = point[0]+snxt*dir[0];
360 if (TMath::Abs(xnew) < dx)
return snxt;
363 if (point[1]>=disty) {
364 cn = dir[1]+fy*dir[2];
365 if (cn>=0)
return TGeoShape::Big();
367 snxt = (disty-point[1])/cn;
369 znew = point[2]+snxt*dir[2];
370 if (TMath::Abs(znew) < fDz) {
371 Double_t dx = 0.5*(fDx1+fDx2)-fx*znew;
372 xnew = point[0]+snxt*dir[0];
373 if (TMath::Abs(xnew) < dx)
return snxt;
376 if (!in)
return TGeoShape::Big();
378 if (safz<safx && safz<safy) {
379 if (point[2]*dir[2]>=0)
return TGeoShape::Big();
383 cn = TMath::Sign(1.0,point[1])*dir[1]+fy*dir[2];
384 if (cn>=0)
return TGeoShape::Big();
387 cn = TMath::Sign(1.0,point[0])*dir[0]+fx*dir[2];
388 if (cn>=0)
return TGeoShape::Big();
395 Double_t TGeoTrd2::GetAxisRange(Int_t iaxis, Double_t &xlo, Double_t &xhi)
const
413 void TGeoTrd2::GetVisibleCorner(
const Double_t *point, Double_t *vertex, Double_t *normals)
const
415 Double_t fx = 0.5*(fDx1-fDx2)/fDz;
416 Double_t fy = 0.5*(fDy1-fDy2)/fDz;
417 Double_t calf = 1./TMath::Sqrt(1.0+fx*fx);
418 Double_t salf = calf*fx;
419 Double_t cbet = 1./TMath::Sqrt(1.0+fy*fy);
420 Double_t sbet = cbet*fy;
422 Double_t distx = fDx1-fx*(fDz+point[2]);
423 Double_t disty = fDy1-fy*(fDz+point[2]);
424 memset(normals, 0, 9*
sizeof(Double_t));
425 TGeoTrd2 *trd2 = (TGeoTrd2*)
this;
426 if (point[0]>distx) {
428 trd2->SetShapeBit(kGeoVisX);
432 trd2->SetShapeBit(kGeoVisX, kFALSE);
436 if (point[1]>disty) {
438 trd2->SetShapeBit(kGeoVisY);
442 trd2->SetShapeBit(kGeoVisY, kFALSE);
448 trd2->SetShapeBit(kGeoVisZ);
451 trd2->SetShapeBit(kGeoVisZ, kFALSE);
460 void TGeoTrd2::GetOppositeCorner(
const Double_t * , Int_t inorm, Double_t *vertex, Double_t *normals)
const
462 TGeoTrd2 *trd2 = (TGeoTrd2*)
this;
465 trd2->SetShapeBit(kGeoVisX, !TestShapeBit(kGeoVisX));
466 normals[0]=-normals[0];
470 trd2->SetShapeBit(kGeoVisY, !TestShapeBit(kGeoVisY));
471 normals[4]=-normals[4];
475 trd2->SetShapeBit(kGeoVisZ, !TestShapeBit(kGeoVisZ));
476 normals[8]=-normals[8];
488 TGeoVolume *TGeoTrd2::Divide(TGeoVolume *voldiv,
const char *divname, Int_t iaxis, Int_t ndiv,
489 Double_t start, Double_t step)
493 TGeoVolumeMulti *vmulti;
494 TGeoPatternFinder *finder;
496 Double_t zmin, zmax, dx1n, dx2n, dy1n, dy2n;
498 Double_t end = start+ndiv*step;
501 Warning(
"Divide",
"dividing a Trd2 on X not implemented");
504 Warning(
"Divide",
"dividing a Trd2 on Y not implemented");
507 finder =
new TGeoPatternZ(voldiv, ndiv, start, end);
508 vmulti = gGeoManager->MakeVolumeMulti(divname, voldiv->GetMedium());
509 voldiv->SetFinder(finder);
510 finder->SetDivIndex(voldiv->GetNdaughters());
511 for (
id=0;
id<ndiv;
id++) {
512 zmin = start+
id*step;
513 zmax = start+(
id+1)*step;
514 dx1n = 0.5*(fDx1*(fDz-zmin)+fDx2*(fDz+zmin))/fDz;
515 dx2n = 0.5*(fDx1*(fDz-zmax)+fDx2*(fDz+zmax))/fDz;
516 dy1n = 0.5*(fDy1*(fDz-zmin)+fDy2*(fDz+zmin))/fDz;
517 dy2n = 0.5*(fDy1*(fDz-zmax)+fDy2*(fDz+zmax))/fDz;
518 shape =
new TGeoTrd2(dx1n, dx2n, dy1n, dy2n, step/2.);
519 vol =
new TGeoVolume(divname, shape, voldiv->GetMedium());
520 vmulti->AddVolume(vol);
522 voldiv->AddNodeOffset(vol,
id, start+step/2+
id*step, opt.Data());
523 ((TGeoNodeOffset*)voldiv->GetNodes()->At(voldiv->GetNdaughters()-1))->SetFinder(finder);
527 Error(
"Divide",
"Wrong axis type for division");
536 void TGeoTrd2::GetBoundingCylinder(Double_t *param)
const
538 TGeoBBox::GetBoundingCylinder(param);
544 Int_t TGeoTrd2::GetFittingBox(
const TGeoBBox *parambox, TGeoMatrix *mat, Double_t &dx, Double_t &dy, Double_t &dz)
const
547 if (mat->IsRotation()) {
548 Error(
"GetFittingBox",
"cannot handle parametrized rotated volumes");
553 mat->LocalToMaster(parambox->GetOrigin(), origin);
554 if (!Contains(origin)) {
555 Error(
"GetFittingBox",
"wrong matrix - parametrized box is outside this");
560 dd[0] = parambox->GetDX();
561 dd[1] = parambox->GetDY();
562 dd[2] = parambox->GetDZ();
565 dd[2] = TMath::Min(origin[2]+fDz, fDz-origin[2]);
567 Error(
"GetFittingBox",
"wrong matrix");
571 if (dd[0]>=0 && dd[1]>=0) {
578 Double_t fx = 0.5*(fDx1-fDx2)/fDz;
579 Double_t fy = 0.5*(fDy1-fDy2)/fDz;
580 Double_t dx0 = 0.5*(fDx1+fDx2);
581 Double_t dy0 = 0.5*(fDy1+fDy2);
582 Double_t z=origin[2]-dd[2];
583 dd[0] = dx0-fx*z-origin[0];
584 dd[1] = dy0-fy*z-origin[1];
586 dd[0] = TMath::Min(dd[0], dx0-fx*z-origin[0]);
587 dd[1] = TMath::Min(dd[1], dy0-fy*z-origin[1]);
588 if (dd[0]<0 || dd[1]<0) {
589 Error(
"GetFittingBox",
"wrong matrix");
602 TGeoShape *TGeoTrd2::GetMakeRuntimeShape(TGeoShape *mother, TGeoMatrix * )
const
604 if (!TestShapeBit(kGeoRunTimeShape))
return 0;
605 if (!mother->TestShapeBit(kGeoTrd2)) {
606 Error(
"GetMakeRuntimeShape",
"invalid mother");
609 Double_t dx1, dx2, dy1, dy2, dz;
610 if (fDx1<0) dx1=((TGeoTrd2*)mother)->GetDx1();
612 if (fDx2<0) dx2=((TGeoTrd2*)mother)->GetDx2();
614 if (fDy1<0) dy1=((TGeoTrd2*)mother)->GetDy1();
616 if (fDy2<0) dy2=((TGeoTrd2*)mother)->GetDy2();
618 if (fDz<0) dz=((TGeoTrd2*)mother)->GetDz();
621 return (
new TGeoTrd2(dx1, dx2, dy1, dy2, dz));
627 void TGeoTrd2::InspectShape()
const
629 printf(
"*** Shape %s: TGeoTrd2 ***\n", GetName());
630 printf(
" dx1 = %11.5f\n", fDx1);
631 printf(
" dx2 = %11.5f\n", fDx2);
632 printf(
" dy1 = %11.5f\n", fDy1);
633 printf(
" dy2 = %11.5f\n", fDy2);
634 printf(
" dz = %11.5f\n", fDz);
635 printf(
" Bounding box:\n");
636 TGeoBBox::InspectShape();
643 Double_t TGeoTrd2::Safety(
const Double_t *point, Bool_t in)
const
648 saf[0] = fDz-TMath::Abs(point[2]);
649 Double_t fx = 0.5*(fDx1-fDx2)/fDz;
650 Double_t calf = 1./TMath::Sqrt(1.0+fx*fx);
652 Double_t distx = 0.5*(fDx1+fDx2)-fx*point[2];
653 if (distx<0) saf[1]=TGeoShape::Big();
654 else saf[1]=(distx-TMath::Abs(point[0]))*calf;
656 Double_t fy = 0.5*(fDy1-fDy2)/fDz;
657 calf = 1./TMath::Sqrt(1.0+fy*fy);
659 distx = 0.5*(fDy1+fDy2)-fy*point[2];
660 if (distx<0) saf[2]=TGeoShape::Big();
661 else saf[2]=(distx-TMath::Abs(point[1]))*calf;
663 if (in)
return saf[TMath::LocMin(3,saf)];
664 for (Int_t i=0; i<3; i++) saf[i]=-saf[i];
665 return saf[TMath::LocMax(3,saf)];
671 void TGeoTrd2::SavePrimitive(std::ostream &out, Option_t * )
673 if (TObject::TestBit(kGeoSavePrimitive))
return;
674 out <<
" // Shape: " << GetName() <<
" type: " << ClassName() << std::endl;
675 out <<
" dx1 = " << fDx1 <<
";" << std::endl;
676 out <<
" dx2 = " << fDx2 <<
";" << std::endl;
677 out <<
" dy1 = " << fDy1 <<
";" << std::endl;
678 out <<
" dy2 = " << fDy2 <<
";" << std::endl;
679 out <<
" dz = " << fDZ <<
";" << std::endl;
680 out <<
" TGeoShape *" << GetPointerName() <<
" = new TGeoTrd2(\"" << GetName() <<
"\", dx1,dx2,dy1,dy2,dz);" << std::endl;
681 TObject::SetBit(TGeoShape::kGeoSavePrimitive);
687 void TGeoTrd2::SetDimensions(Double_t *param)
700 void TGeoTrd2::SetPoints(Double_t *points)
const
703 points[ 0] = -fDx1; points[ 1] = -fDy1; points[ 2] = -fDz;
704 points[ 3] = -fDx1; points[ 4] = fDy1; points[ 5] = -fDz;
705 points[ 6] = fDx1; points[ 7] = fDy1; points[ 8] = -fDz;
706 points[ 9] = fDx1; points[10] = -fDy1; points[11] = -fDz;
707 points[12] = -fDx2; points[13] = -fDy2; points[14] = fDz;
708 points[15] = -fDx2; points[16] = fDy2; points[17] = fDz;
709 points[18] = fDx2; points[19] = fDy2; points[20] = fDz;
710 points[21] = fDx2; points[22] = -fDy2; points[23] = fDz;
716 void TGeoTrd2::SetPoints(Float_t *points)
const
719 points[ 0] = -fDx1; points[ 1] = -fDy1; points[ 2] = -fDz;
720 points[ 3] = -fDx1; points[ 4] = fDy1; points[ 5] = -fDz;
721 points[ 6] = fDx1; points[ 7] = fDy1; points[ 8] = -fDz;
722 points[ 9] = fDx1; points[10] = -fDy1; points[11] = -fDz;
723 points[12] = -fDx2; points[13] = -fDy2; points[14] = fDz;
724 points[15] = -fDx2; points[16] = fDy2; points[17] = fDz;
725 points[18] = fDx2; points[19] = fDy2; points[20] = fDz;
726 points[21] = fDx2; points[22] = -fDy2; points[23] = fDz;
732 void TGeoTrd2::SetVertex(Double_t *vertex)
const
734 if (TestShapeBit(kGeoVisX)) {
735 if (TestShapeBit(kGeoVisZ)) {
738 vertex[1] = (TestShapeBit(kGeoVisY))?fDy2:-fDy2;
742 vertex[1] = (TestShapeBit(kGeoVisY))?fDy1:-fDy1;
745 if (TestShapeBit(kGeoVisZ)) {
748 vertex[1] = (TestShapeBit(kGeoVisY))?fDy2:-fDy2;
752 vertex[1] = (TestShapeBit(kGeoVisY))?fDy1:-fDy1;
760 void TGeoTrd2::Sizeof3D()
const
762 TGeoBBox::Sizeof3D();
770 void TGeoTrd2::Contains_v(
const Double_t *points, Bool_t *inside, Int_t vecsize)
const
772 for (Int_t i=0; i<vecsize; i++) inside[i] = Contains(&points[3*i]);
780 void TGeoTrd2::ComputeNormal_v(
const Double_t *points,
const Double_t *dirs, Double_t *norms, Int_t vecsize)
782 for (Int_t i=0; i<vecsize; i++) ComputeNormal(&points[3*i], &dirs[3*i], &norms[3*i]);
788 void TGeoTrd2::DistFromInside_v(
const Double_t *points,
const Double_t *dirs, Double_t *dists, Int_t vecsize, Double_t* step)
const
790 for (Int_t i=0; i<vecsize; i++) dists[i] = DistFromInside(&points[3*i], &dirs[3*i], 3, step[i]);
796 void TGeoTrd2::DistFromOutside_v(
const Double_t *points,
const Double_t *dirs, Double_t *dists, Int_t vecsize, Double_t* step)
const
798 for (Int_t i=0; i<vecsize; i++) dists[i] = DistFromOutside(&points[3*i], &dirs[3*i], 3, step[i]);
806 void TGeoTrd2::Safety_v(
const Double_t *points,
const Bool_t *inside, Double_t *safe, Int_t vecsize)
const
808 for (Int_t i=0; i<vecsize; i++) safe[i] = Safety(&points[3*i], inside[i]);