61 const G4int G4GenericTrap::fgkNofVertices = 8;
62 const G4double G4GenericTrap::fgkTolerance = 1E-3;
67 const std::vector<G4TwoVector>& vertices )
85 G4String errorDescription =
"InvalidSetup in \" ";
86 errorDescription +=
name;
87 errorDescription +=
"\"";
93 if (
G4int(vertices.size()) != fgkNofVertices )
95 G4Exception(
"G4GenericTrap::G4GenericTrap()",
"GeomSolids0002",
103 G4Exception(
"G4GenericTrap::G4GenericTrap()",
"GeomSolids0002",
109 if(CheckOrder(vertices))
111 for (
G4int i=0; i<fgkNofVertices; ++i) {fVertices.push_back(vertices[i]);}
115 for (
G4int i=0; i <4; ++i) {fVertices.push_back(vertices[3-i]);}
116 for (
G4int i=0; i <4; ++i) {fVertices.push_back(vertices[7-i]);}
121 for (
G4int j=0; j < 2; j++)
123 for (
G4int i=1; i<4; ++i)
126 length = (fVertices[k]-fVertices[k-1]).mag();
129 std::ostringstream message;
130 message <<
"Length segment is too small." <<
G4endl
131 <<
"Distance between " << fVertices[k-1] <<
" and "
132 << fVertices[k] <<
" is only " << length <<
" mm !";
133 G4Exception(
"G4GenericTrap::G4GenericTrap()",
"GeomSolids1001",
134 JustWarning, message,
"Vertices will be collapsed.");
135 fVertices[k]=fVertices[k-1];
142 for(
G4int i=0; i<4; i++) { fTwist[i]=0.; }
143 fIsTwisted = ComputeIsTwisted();
153 if ( !fIsTwisted ) { fTessellatedSolid = CreateTessellatedSolid(); }
162 halfCarTolerance(0.),
166 fTessellatedSolid(0),
176 for (
size_t i=0; i<4; ++i) { fTwist[i]=0.; }
184 delete fTessellatedSolid;
191 fpPolyhedron(0), halfCarTolerance(rhs.halfCarTolerance),
192 fDz(rhs.fDz), fVertices(rhs.fVertices),
193 fIsTwisted(rhs.fIsTwisted), fTessellatedSolid(0),
194 fMinBBoxVector(rhs.fMinBBoxVector), fMaxBBoxVector(rhs.fMaxBBoxVector),
195 fVisSubdivisions(rhs.fVisSubdivisions),
196 fSurfaceArea(rhs.fSurfaceArea), fCubicVolume(rhs.fCubicVolume)
198 for (
size_t i=0; i<4; ++i) { fTwist[i] = rhs.fTwist[i]; }
200 if (rhs.fTessellatedSolid && !fIsTwisted )
201 { fTessellatedSolid = CreateTessellatedSolid(); }
211 if (
this == &rhs) {
return *
this; }
219 fpPolyhedron = 0; halfCarTolerance = rhs.halfCarTolerance;
220 fDz = rhs.fDz; fVertices = rhs.fVertices;
221 fIsTwisted = rhs.fIsTwisted; fTessellatedSolid = 0;
222 fMinBBoxVector = rhs.fMinBBoxVector; fMaxBBoxVector = rhs.fMaxBBoxVector;
223 fVisSubdivisions = rhs.fVisSubdivisions;
224 fSurfaceArea = rhs.fSurfaceArea; fCubicVolume = rhs.fCubicVolume;
226 for (
size_t i=0; i<4; ++i) { fTwist[i] = rhs.fTwist[i]; }
228 if (rhs.fTessellatedSolid && !fIsTwisted )
229 {
delete fTessellatedSolid; fTessellatedSolid = CreateTessellatedSolid(); }
239 const std::vector<G4TwoVector>& poly)
const
245 for (
G4int i = 0; i < 4; i++)
249 cross = (p.
x()-poly[i].x())*(poly[j].y()-poly[i].y())-
250 (p.
y()-poly[i].y())*(poly[j].
x()-poly[i].x());
252 len2=(poly[i]-poly[j]).mag2();
255 if(cross*cross<=len2*halfCarTolerance*halfCarTolerance)
264 if (poly[j].
x() > poly[i].x())
273 if ( p.
x() > poly[iMax].x()+halfCarTolerance
274 || p.
x() < poly[iMin].x()-halfCarTolerance )
279 if (poly[j].y() > poly[i].y())
289 if ( p.
y() > poly[iMax].y()+halfCarTolerance
290 || p.
y() < poly[iMin].y()-halfCarTolerance )
295 if ( poly[iMax].
x() != poly[iMin].x() )
297 test = (p.
x()-poly[iMin].x())/(poly[iMax].
x()-poly[iMin].x())
298 * (poly[iMax].y()-poly[iMin].y())+poly[iMin].y();
307 if( (test>=(poly[iMin].y()-halfCarTolerance))
308 && (test<=(poly[iMax].y()+halfCarTolerance)) )
317 else if (cross<0.) {
return kOutside; }
329 if ( (std::fabs(p.
x()-poly[0].x())+std::fabs(p.
y()-poly[0].y())) > halfCarTolerance )
344 if ( fTessellatedSolid )
346 return fTessellatedSolid->
Inside(p);
351 std::vector<G4TwoVector> xy;
353 if (std::fabs(p.
z()) <= fDz+halfCarTolerance)
358 for (
G4int i=0; i<4; i++)
360 xy.push_back(fVertices[i+4]+cf*( fVertices[i]-fVertices[i+4]));
363 innew=InsidePolygone(p,xy);
367 if(std::fabs(p.
z()) > fDz-halfCarTolerance) { innew=
kSurface; }
381 if ( fTessellatedSolid )
388 p0, p1, p2, r1, r2, r3, r4;
389 G4int noSurfaces = 0;
393 distz = fDz-std::fabs(p.
z());
394 if (distz < halfCarTolerance)
410 std:: vector<G4TwoVector> vertices;
412 for (
G4int i=0; i<4; i++)
414 vertices.push_back(fVertices[i+4]+cf*(fVertices[i]-fVertices[i+4]));
419 for (
G4int q=0; q<4; q++)
438 p2=
G4ThreeVector(fVertices[(q+1)%4].
x(),fVertices[(q+1)%4].y(),-fDz);
442 p2=
G4ThreeVector(fVertices[(q+1)%4+4].
x(),fVertices[(q+1)%4+4].y(),fDz);
445 lnorm = (p1-p0).cross(p2-p0);
446 lnorm = lnorm.
unit();
447 if(zPlusSide) { lnorm=-lnorm; }
456 G4double proj=(p-p0).dot(p2-p0)/normP;
457 if(proj<0) { proj=0; }
458 if(proj>normP) { proj=normP; }
464 r1=r1+proj*(r2-r1)/normP;
465 r3=r3+proj*(r4-r3)/normP;
472 distxy=std::fabs((p0-p).dot(lnorm));
473 if ( distxy<halfCarTolerance )
479 sumnorm=sumnorm+lnorm;
493 if ( noSurfaces == 0 )
495 G4Exception(
"G4GenericTrap::SurfaceNormal(p)",
"GeomSolids1002",
500 else if ( noSurfaces == 1 ) { sumnorm = sumnorm; }
501 else { sumnorm = sumnorm.unit(); }
509 const G4int ipl )
const
514 if ( fTessellatedSolid )
530 u=fVertices[i+4]+cf*(fVertices[i]-fVertices[i+4]);
531 v=fVertices[j+4]+cf*(fVertices[j]-fVertices[j+4]);
537 if (std::fabs(distz)<halfCarTolerance)
539 p1=
G4ThreeVector(fVertices[i].
x(),fVertices[i].y(),-fDz);distz=-1;}
550 if ( std::fabs(p.
z()+fDz) > halfCarTolerance )
559 lnorm=-(p1-p0).cross(p2-p0);
560 if (distz>-halfCarTolerance) { lnorm=-lnorm.
unit(); }
561 else { lnorm=lnorm.
unit(); }
570 G4double proj=(p-p0).dot(p2-p0)/normP;
571 if (proj<0) { proj=0; }
572 if (proj>normP) { proj=normP; }
578 r1=r1+proj*(r2-r1)/normP;
579 r3=r3+proj*(r4-r3)/normP;
593 const G4int ipl)
const
605 xa=fVertices[ipl].x();
606 ya=fVertices[ipl].y();
607 xb=fVertices[ipl+4].x();
608 yb=fVertices[ipl+4].y();
611 xd=fVertices[4+j].x();
612 yd=fVertices[4+j].y();
629 G4double a = (dtx*v.
y()-dty*v.
x()+(tx1*ty2-tx2*ty1)*v.
z())*v.
z();
630 G4double b = dxs*v.
y()-dys*v.
x()+(dtx*p.
y()-dty*p.
x()+ty2*xs1-ty1*xs2
631 + tx1*ys2-tx2*ys1)*v.
z();
643 if (q>-halfCarTolerance)
645 if (q<halfCarTolerance)
647 if (NormalToPlane(p,ipl).dot(v)<=0)
650 {
return kInfinity; }
656 if (std::fabs(zi)<fDz)
664 zi = (xp-x1)*(xp-x2)+(yp-y1)*(yp-y2);
665 if (zi<=halfCarTolerance) {
return q; }
673 if (a>0) { q=0.5*(-b-std::sqrt(d))/a; }
674 else { q=0.5*(-b+std::sqrt(d))/a; }
678 if (q>-halfCarTolerance)
680 if(q<halfCarTolerance)
682 if (NormalToPlane(p,ipl).
dot(v)<=0)
688 if (a>0) { q=0.5*(-b+std::sqrt(d))/a; }
689 else { q=0.5*(-b-std::sqrt(d))/a; }
690 if (q<=halfCarTolerance) {
return kInfinity; }
696 if (std::fabs(zi)<fDz)
704 zi = (xp-x1)*(xp-x2)+(yp-y1)*(yp-y2);
705 if (zi<=halfCarTolerance) {
return q; }
708 if (a>0) { q=0.5*(-b+std::sqrt(d))/a; }
709 else { q=0.5*(-b-std::sqrt(d))/a; }
713 if (q>-halfCarTolerance)
715 if(q<halfCarTolerance)
717 if (NormalToPlane(p,ipl).
dot(v)<=0)
723 if (a>0) { q=0.5*(-b-std::sqrt(d))/a; }
724 else { q=0.5*(-b+std::sqrt(d))/a; }
725 if (q<=halfCarTolerance) {
return kInfinity; }
731 if (std::fabs(zi)<fDz)
739 zi = (xp-x1)*(xp-x2)+(yp-y1)*(yp-y2);
740 if (zi<=halfCarTolerance) {
return q; }
753 if ( fTessellatedSolid )
767 dist[i]=DistToPlane(p, v, i);
773 if (std::fabs(p.
z())>fDz-halfCarTolerance)
780 dist[4] = (fDz-p.
z())/v.
z();
784 dist[4] = (-fDz-p.
z())/v.
z();
786 if (dist[4]<-halfCarTolerance)
792 if(dist[4]<halfCarTolerance)
796 if (n.
dot(v)<0) { dist[4]=0.; }
797 else { dist[4]=kInfinity; }
807 if (dist[i] < distmin) { distmin = dist[i]; }
810 if (distmin<halfCarTolerance) { distmin=0.; }
822 if ( fTessellatedSolid )
829 if(safz<0) { safz=0; }
835 for (iseg=0; iseg<4; iseg++)
837 safxy = SafetyToFace(p,iseg);
838 if (safxy>safe) { safe=safxy; }
857 norm=NormalToPlane(p,iseg);
858 safe = (p-p1).dot(norm);
879 if ( (std::fabs(xa-xc)+std::fabs(ya-yc)) < halfCarTolerance )
881 xc=fVertices[j+4].x();
882 yc=fVertices[j+4].y();
888 if ( (std::fabs(xb-xc)+std::fabs(yb-yc)) < halfCarTolerance )
895 G4double c=(xb-xa)*(yc-ya)-(xc-xa)*(yb-ya);
901 t=-(a*p.
x()+b*p.
y()+c*p.
z()+d)/t;
903 if ( (t<halfCarTolerance) && (t>-halfCarTolerance) )
928 if ( fTessellatedSolid )
930 return fTessellatedSolid->
DistanceToOut(p, v, calcNorm, validNorm, n);
935 G4bool lateral_cross =
false;
936 ESide side = kUndefined;
938 if (calcNorm) { *validNorm=
true; }
942 distmin=(-fDz-p.
z())/v.
z();
949 distmin = (fDz-p.
z())/v.
z();
952 else { distmin = kInfinity; }
959 for (
G4int ipl=0; ipl<4; ipl++)
962 xa=fVertices[ipl].x();
963 ya=fVertices[ipl].y();
964 xb=fVertices[ipl+4].x();
965 yb=fVertices[ipl+4].y();
968 xd=fVertices[4+j].x();
969 yd=fVertices[4+j].y();
971 if ( ((std::fabs(xb-xd)+std::fabs(yb-yd))<halfCarTolerance)
972 || ((std::fabs(xa-xc)+std::fabs(ya-yc))<halfCarTolerance) )
974 G4double q=DistToTriangle(p,v,ipl) ;
975 if ( (q>=0) && (q<distmin) )
996 G4double a = (dtx*v.
y()-dty*v.
x()+(tx1*ty2-tx2*ty1)*v.
z())*v.
z();
997 G4double b = dxs*v.
y()-dys*v.
x()+(dtx*p.
y()-dty*p.
x()+ty2*xs1-ty1*xs2
998 + tx1*ys2-tx2*ys1)*v.
z();
999 G4double c=dxs*p.
y()-dys*p.
x()+xs1*ys2-xs2*ys1;
1009 if ((q > -halfCarTolerance) && (q < distmin))
1011 if (q < halfCarTolerance)
1013 if (NormalToPlane(p,ipl).dot(v)<0.) {
continue; }
1024 if (a > 0) { q=0.5*(-b-std::sqrt(d))/a; }
1025 else { q=0.5*(-b+std::sqrt(d))/a; }
1029 if (q > -halfCarTolerance )
1033 if(q < halfCarTolerance)
1035 if (NormalToPlane(p,ipl).
dot(v)<0.)
1037 if (a > 0) { q=0.5*(-b+std::sqrt(d))/a; }
1038 else { q=0.5*(-b-std::sqrt(d))/a; }
1039 if (( q > halfCarTolerance) && (q < distmin))
1042 lateral_cross =
true;
1049 lateral_cross =
true;
1055 if (a > 0) { q=0.5*(-b+std::sqrt(d))/a; }
1056 else { q=0.5*(-b-std::sqrt(d))/a; }
1060 if ((q > -halfCarTolerance) && (q < distmin))
1062 if (q < halfCarTolerance)
1064 if (NormalToPlane(p,ipl).
dot(v)<0.)
1066 if (a > 0) { q=0.5*(-b-std::sqrt(d))/a; }
1067 else { q=0.5*(-b+std::sqrt(d))/a; }
1068 if ( ( q > halfCarTolerance) && (q < distmin) )
1071 lateral_cross =
true;
1078 lateral_cross =
true;
1092 if (v.z()>0.) { i=4; }
1093 std::vector<G4TwoVector> xy;
1094 for (
G4int j=0; j<4; j++) { xy.push_back(fVertices[i+j]); }
1098 if (InsidePolygone(pt,xy)==
kOutside)
1109 if(v.z()>0) {side=kPZ;}
1120 *n=NormalToPlane(pt,0);
1123 *n=NormalToPlane(pt,1);
1126 *n=NormalToPlane(pt,2);
1129 *n=NormalToPlane(pt,3);
1139 std::ostringstream message;
1140 G4int oldprc = message.precision(16);
1141 message <<
"Undefined side for valid surface normal to solid." <<
G4endl
1143 <<
" p.x() = " << p.
x()/
mm <<
" mm" <<
G4endl
1144 <<
" p.y() = " << p.
y()/
mm <<
" mm" <<
G4endl
1145 <<
" p.z() = " << p.
z()/
mm <<
" mm" <<
G4endl
1146 <<
"Direction:" <<
G4endl
1147 <<
" v.x() = " << v.
x() <<
G4endl
1148 <<
" v.y() = " << v.
y() <<
G4endl
1149 <<
" v.z() = " << v.
z() <<
G4endl
1150 <<
"Proposed distance :" <<
G4endl
1151 <<
" distmin = " << distmin/
mm <<
" mm";
1152 message.precision(oldprc);
1153 G4Exception(
"G4GenericTrap::DistanceToOut(p,v,..)",
1159 if (distmin<halfCarTolerance) { distmin=0.; }
1170 if ( fTessellatedSolid )
1177 if (safz<0) { safz = 0; }
1182 for (
G4int iseg=0; iseg<4; iseg++)
1184 safxy = std::fabs(SafetyToFace(p,iseg));
1185 if (safxy < safe) { safe = safxy; }
1199 if ( fTessellatedSolid )
1202 pTransform, pMin, pMax);
1211 Dx = 0.5*(maxVec.
x()- minVec.
x());
1212 Dy = 0.5*(maxVec.
y()- minVec.
y());
1319 G4bool existsAfterClip=
false;
1327 vertices=CreateRotatedVertices(pTransform);
1332 if ( (pMin!=kInfinity) || (pMax!=-kInfinity) )
1334 existsAfterClip=
true;
1355 existsAfterClip=
true;
1361 return existsAfterClip;
1384 vertices->reserve(8);
1405 G4Exception(
"G4GenericTrap::CreateRotatedVertices()",
"FatalError",
1429 G4int oldprc = os.precision(16);
1430 os <<
"-----------------------------------------------------------\n"
1431 <<
" *** Dump for solid - " <<
GetName() <<
" *** \n"
1432 <<
" =================================================== \n"
1434 <<
" half length Z: " << fDz/
mm <<
" mm \n"
1435 <<
" list of vertices:\n";
1437 for (
G4int i=0; i<fgkNofVertices; ++i )
1439 os << std::setw(5) <<
"#" << i
1440 <<
" vx = " << fVertices[i].x()/
mm <<
" mm"
1441 <<
" vy = " << fVertices[i].y()/
mm <<
" mm" <<
G4endl;
1443 os.precision(oldprc);
1454 if ( fTessellatedSolid )
1462 G4double rand,area,chose,cf,lambda0,lambda1,alfa,beta,zp;
1465 std::vector<G4ThreeVector> vertices;
1466 for (
G4int i=0; i<4;i++)
1468 vertices.push_back(
G4ThreeVector(fVertices[i].
x(),fVertices[i].y(),-fDz));
1470 for (
G4int i=4; i<8;i++)
1472 vertices.push_back(
G4ThreeVector(fVertices[i].
x(),fVertices[i].y(),fDz));
1477 G4double Surface0=GetFaceSurfaceArea(vertices[0],vertices[1],
1478 vertices[2],vertices[3]);
1479 G4double Surface1=GetFaceSurfaceArea(vertices[0],vertices[1],
1480 vertices[5],vertices[4]);
1481 G4double Surface2=GetFaceSurfaceArea(vertices[3],vertices[0],
1482 vertices[4],vertices[7]);
1483 G4double Surface3=GetFaceSurfaceArea(vertices[2],vertices[3],
1484 vertices[7],vertices[6]);
1485 G4double Surface4=GetFaceSurfaceArea(vertices[2],vertices[1],
1486 vertices[5],vertices[6]);
1487 G4double Surface5=GetFaceSurfaceArea(vertices[4],vertices[5],
1488 vertices[6],vertices[7]);
1490 area = Surface0+Surface1+Surface2+Surface3+Surface4+Surface5;
1493 if ( ( chose < Surface0)
1494 || ( chose > (Surface0+Surface1+Surface2+Surface3+Surface4)) )
1498 if(chose < Surface0)
1501 u = fVertices[ipl]; v = fVertices[j];
1502 w = fVertices[(ipl+3)%4];
1507 u = fVertices[ipl+4]; v = fVertices[j+4];
1508 w = fVertices[(ipl+3)%4+4];
1513 lambda0=alfa-lambda1;
1516 v = u+lambda0*v+lambda1*w;
1520 if (chose < Surface0+Surface1) { ipl=0; }
1521 else if (chose < Surface0+Surface1+Surface2) { ipl=1; }
1522 else if (chose < Surface0+Surface1+Surface2+Surface3) { ipl=2; }
1526 cf = 0.5*(fDz-zp)/fDz;
1527 u = fVertices[ipl+4]+cf*( fVertices[ipl]-fVertices[ipl+4]);
1528 v = fVertices[j+4]+cf*(fVertices[j]-fVertices[j+4]);
1540 if(fCubicVolume != 0.) {;}
1542 return fCubicVolume;
1549 if(fSurfaceArea != 0.) {;}
1552 std::vector<G4ThreeVector> vertices;
1553 for (
G4int i=0; i<4;i++)
1555 vertices.push_back(
G4ThreeVector(fVertices[i].
x(),fVertices[i].y(),-fDz));
1557 for (
G4int i=4; i<8;i++)
1559 vertices.push_back(
G4ThreeVector(fVertices[i].
x(),fVertices[i].y(),fDz));
1564 G4double fSurface0=GetFaceSurfaceArea(vertices[0],vertices[1],
1565 vertices[2],vertices[3]);
1566 G4double fSurface1=GetFaceSurfaceArea(vertices[0],vertices[1],
1567 vertices[5],vertices[4]);
1568 G4double fSurface2=GetFaceSurfaceArea(vertices[3],vertices[0],
1569 vertices[4],vertices[7]);
1570 G4double fSurface3=GetFaceSurfaceArea(vertices[2],vertices[3],
1571 vertices[7],vertices[6]);
1572 G4double fSurface4=GetFaceSurfaceArea(vertices[2],vertices[1],
1573 vertices[5],vertices[6]);
1574 G4double fSurface5=GetFaceSurfaceArea(vertices[4],vertices[5],
1575 vertices[6],vertices[7]);
1581 fSurfaceArea = fSurface0+fSurface1+fSurface2
1582 + fSurface3+fSurface4+fSurface5;
1589 return fSurfaceArea;
1610 aOne = 0.5*Area.
mag();
1613 aTwo = 0.5*Area.
mag();
1620 G4bool G4GenericTrap::ComputeIsTwisted()
1627 G4int nv = fgkNofVertices/2;
1629 for (
G4int i=0; i<4; i++ )
1631 dx1 = fVertices[(i+1)%nv].
x()-fVertices[i].x();
1632 dy1 = fVertices[(i+1)%nv].y()-fVertices[i].y();
1633 if ( (dx1 == 0) && (dy1 == 0) ) {
continue; }
1635 dx2 = fVertices[nv+(i+1)%nv].
x()-fVertices[nv+i].x();
1636 dy2 = fVertices[nv+(i+1)%nv].y()-fVertices[nv+i].y();
1638 if ( dx2 == 0 && dy2 == 0 ) {
continue; }
1639 G4double twist_angle = std::fabs(dy1*dx2 - dx1*dy2);
1640 if ( twist_angle < fgkTolerance ) {
continue; }
1642 SetTwistAngle(i,twist_angle);
1646 twist_angle = std::acos( (dx1*dx2 + dy1*dy2)
1647 / (std::sqrt(dx1*dx1+dy1*dy1)
1648 * std::sqrt(dx2*dx2+dy2*dy2)) );
1652 std::ostringstream message;
1653 message <<
"Twisted Angle is bigger than 90 degrees - " <<
GetName()
1655 <<
" Potential problem of malformed Solid !" <<
G4endl
1656 <<
" TwistANGLE = " << twist_angle
1657 <<
"*rad for lateral plane N= " << i;
1658 G4Exception(
"G4GenericTrap::ComputeIsTwisted()",
"GeomSolids1002",
1668 G4bool G4GenericTrap::CheckOrder(
const std::vector<G4TwoVector>& vertices)
const
1673 G4bool clockwise_order=
true;
1678 for (
G4int i=0; i<4; i++)
1681 sum1 += vertices[i].x()*vertices[j].y() - vertices[j].x()*vertices[i].y();
1682 sum2 += vertices[i+4].x()*vertices[j+4].y()
1683 - vertices[j+4].x()*vertices[i+4].y();
1685 if (sum1*sum2 < -fgkTolerance)
1687 std::ostringstream message;
1688 message <<
"Lower/upper faces defined with opposite clockwise - "
1690 G4Exception(
"G4GenericTrap::CheckOrder()",
"GeomSolids0002",
1694 if ((sum1 > 0.)||(sum2 > 0.))
1696 std::ostringstream message;
1697 message <<
"Vertices must be defined in clockwise XY planes - "
1699 G4Exception(
"G4GenericTrap::CheckOrder()",
"GeomSolids1001",
1701 clockwise_order =
false;
1706 G4bool illegal_cross =
false;
1707 illegal_cross = IsSegCrossingZ(vertices[0],vertices[4],
1708 vertices[1],vertices[5]);
1712 illegal_cross = IsSegCrossingZ(vertices[2],vertices[6],
1713 vertices[3],vertices[7]);
1718 illegal_cross = IsSegCrossing(vertices[0],vertices[1],
1719 vertices[2],vertices[3]);
1723 illegal_cross = IsSegCrossing(vertices[0],vertices[3],
1724 vertices[1],vertices[2]);
1728 illegal_cross = IsSegCrossing(vertices[4],vertices[5],
1729 vertices[6],vertices[7]);
1733 illegal_cross = IsSegCrossing(vertices[4],vertices[7],
1734 vertices[5],vertices[6]);
1739 std::ostringstream message;
1740 message <<
"Malformed polygone with opposite sides - " <<
GetName();
1741 G4Exception(
"G4GenericTrap::CheckOrderAndSetup()",
1744 return clockwise_order;
1749 void G4GenericTrap::ReorderVertices(std::vector<G4ThreeVector>& vertices)
const
1753 std::vector<G4ThreeVector> oldVertices(vertices);
1755 for (
G4int i=0; i <
G4int(oldVertices.size()); ++i )
1757 vertices[i] = oldVertices[oldVertices.size()-1-i];
1771 G4double dx1,dx2,xm=0.,ym=0.,a1=0.,a2=0.,b1=0.,b2=0.;
1775 if( std::fabs(dx1) < fgkTolerance ) { stand1 =
true; }
1776 if( std::fabs(dx2) < fgkTolerance ) { stand2 =
true; }
1779 a1 = (b.
x()*a.
y()-a.
x()*b.
y())/dx1;
1784 a2 = (d.
x()*c.
y()-c.
x()*d.
y())/dx2;
1787 if (stand1 && stand2)
1791 if (std::fabs(a.
x()-c.
x())<fgkTolerance)
1795 if ( ((c.
y()-a.
y())*(c.
y()-b.
y())<-fgkTolerance)
1796 || ((d.
y()-a.
y())*(d.
y()-b.
y())<-fgkTolerance)
1797 || ((a.
y()-c.
y())*(a.
y()-d.
y())<-fgkTolerance)
1798 || ((b.
y()-c.
y())*(b.
y()-d.
y())<-fgkTolerance) ) {
return true; }
1821 if (std::fabs(b1-b2) < fgkTolerance)
1825 if (std::fabs(c.
y()-(a1+b1*c.
x())) > fgkTolerance) {
return false; }
1829 if ( ((c.
x()-a.
x())*(c.
x()-b.
x())<-fgkTolerance)
1830 || ((d.
x()-a.
x())*(d.
x()-b.
x())<-fgkTolerance)
1831 || ((a.
x()-c.
x())*(a.
x()-d.
x())<-fgkTolerance)
1832 || ((b.
x()-c.
x())*(b.
x()-d.
x())<-fgkTolerance) ) {
return true; }
1836 xm = (a1-a2)/(b2-b1);
1837 ym = (a1*b2-a2*b1)/(b2-b1);
1843 G4double check = (xm-a.
x())*(xm-b.
x())+(ym-a.
y())*(ym-b.
y());
1844 if (check > -fgkTolerance) {
return false; }
1845 check = (xm-c.
x())*(xm-d.
x())+(ym-c.
y())*(ym-d.
y());
1846 if (check > -fgkTolerance) {
return false; }
1879 (std::fabs((p4-p3).y()) <
kCarTolerance ) ) {
return false; }
1883 det = dv.
x()*v1.
y()*v2.
z()+dv.
y()*v1.
z()*v2.
x()
1884 - dv.
x()*v1.
z()*v2.
y()-dv.
y()*v1.
x()*v2.
z();
1888 temp1 = v1.
cross(v2);
1889 temp2 = (p2-p1).cross(v2);
1890 if (temp1.
dot(temp2) < 0) {
return false; }
1894 q = ((dv).cross(v2)).mag()/q;
1904 G4GenericTrap::MakeDownFacet(
const std::vector<G4ThreeVector>& fromVertices,
1911 if ( (fromVertices[ind1] == fromVertices[ind2]) ||
1912 (fromVertices[ind2] == fromVertices[ind3]) ||
1913 (fromVertices[ind1] == fromVertices[ind3]) ) {
return 0; }
1915 std::vector<G4ThreeVector> vertices;
1916 vertices.push_back(fromVertices[ind1]);
1917 vertices.push_back(fromVertices[ind2]);
1918 vertices.push_back(fromVertices[ind3]);
1922 G4ThreeVector cross=(vertices[1]-vertices[0]).cross(vertices[2]-vertices[1]);
1924 if ( cross.
z() > 0.0 )
1928 std::ostringstream message;
1929 message <<
"Vertices in wrong order - " <<
GetName();
1930 G4Exception(
"G4GenericTrap::MakeDownFacet",
"GeomSolids0002",
1940 G4GenericTrap::MakeUpFacet(
const std::vector<G4ThreeVector>& fromVertices,
1948 if ( (fromVertices[ind1] == fromVertices[ind2]) ||
1949 (fromVertices[ind2] == fromVertices[ind3]) ||
1950 (fromVertices[ind1] == fromVertices[ind3]) ) {
return 0; }
1952 std::vector<G4ThreeVector> vertices;
1953 vertices.push_back(fromVertices[ind1]);
1954 vertices.push_back(fromVertices[ind2]);
1955 vertices.push_back(fromVertices[ind3]);
1959 G4ThreeVector cross=(vertices[1]-vertices[0]).cross(vertices[2]-vertices[1]);
1961 if ( cross.
z() < 0.0 )
1965 std::ostringstream message;
1966 message <<
"Vertices in wrong order - " <<
GetName();
1967 G4Exception(
"G4GenericTrap::MakeUpFacet",
"GeomSolids0002",
1977 G4GenericTrap::MakeSideFacet(
const G4ThreeVector& downVertex0,
1985 if ( (downVertex0 == downVertex1) && (upVertex0 == upVertex1) )
1990 if ( downVertex0 == downVertex1 )
1995 if ( upVertex0 == upVertex1 )
2010 G4int nv = fgkNofVertices/2;
2011 std::vector<G4ThreeVector> downVertices;
2012 for (
G4int i=0; i<nv; i++ )
2015 fVertices[i].y(), -fDz));
2018 std::vector<G4ThreeVector> upVertices;
2019 for (
G4int i=nv; i<2*nv; i++ )
2022 fVertices[i].y(), fDz));
2028 = (downVertices[1]-downVertices[0]).cross(downVertices[2]-downVertices[1]);
2030 = (upVertices[1]-upVertices[0]).cross(upVertices[2]-upVertices[1]);
2031 if ( (cross.
z() > 0.0) || (cross1.
z() > 0.0) )
2033 ReorderVertices(downVertices);
2034 ReorderVertices(upVertices);
2040 facet = MakeDownFacet(downVertices, 0, 1, 2);
2041 if (facet) { tessellatedSolid->
AddFacet( facet ); }
2042 facet = MakeDownFacet(downVertices, 0, 2, 3);
2043 if (facet) { tessellatedSolid->
AddFacet( facet ); }
2044 facet = MakeUpFacet(upVertices, 0, 2, 1);
2045 if (facet) { tessellatedSolid->
AddFacet( facet ); }
2046 facet = MakeUpFacet(upVertices, 0, 3, 2);
2047 if (facet) { tessellatedSolid->
AddFacet( facet ); }
2051 for (
G4int i = 0; i < nv; ++i )
2053 G4int j = (i+1) % nv;
2054 facet = MakeSideFacet(downVertices[j], downVertices[i],
2055 upVertices[i], upVertices[j]);
2057 if ( facet ) { tessellatedSolid->
AddFacet( facet ); }
2062 return tessellatedSolid;
2067 void G4GenericTrap::ComputeBBox()
2072 minX = maxX = fVertices[0].x();
2073 minY = maxY = fVertices[0].y();
2075 for (
G4int i=1; i< fgkNofVertices; i++)
2077 if (minX>fVertices[i].
x()) { minX=fVertices[i].x(); }
2078 if (maxX<fVertices[i].
x()) { maxX=fVertices[i].x(); }
2079 if (minY>fVertices[i].y()) { minY=fVertices[i].y(); }
2080 if (maxY<fVertices[i].y()) { maxY=fVertices[i].y(); }
2092 if ( fTessellatedSolid )
2114 if ( fTessellatedSolid )
2130 if ( fTessellatedSolid )
2139 Dx = 0.5*(maxVec.
x()- minVec.
x());
2140 Dy = 0.5*(maxVec.
y()- minVec.
y());
2151 if ( fTessellatedSolid )
2161 size_t nVertices, nFacets;
2163 G4int subdivisions=0;
2186 Dx = 0.5*(maxVec.
x()- minVec.
y());
2187 Dy = 0.5*(maxVec.
y()- minVec.
y());
2188 if (Dy > Dx) { Dx=Dy; }
2190 subdivisions=8*
G4int(maxTwist/(Dx*Dx*Dx)*fDz);
2191 if (subdivisions<4) { subdivisions=4; }
2192 if (subdivisions>30) { subdivisions=30; }
2195 G4int sub4=4*subdivisions;
2196 nVertices = 8+subdivisions*4;
2197 nFacets = 6+subdivisions*4;
2206 fVertices[i].y(),-fDz));
2208 for( i=0;i<subdivisions;i++)
2210 for(
G4int j=0;j<4;j++)
2212 G4TwoVector u=fVertices[j]+cf*(i+1)*( fVertices[j+4]-fVertices[j]);
2219 fVertices[i].y(),fDz));
2225 for (i=0;i<subdivisions+1;i++)
2228 polyhedron->
AddFacet(5+is,8+is,4+is,1+is);
2229 polyhedron->
AddFacet(8+is,7+is,3+is,4+is);
2230 polyhedron->
AddFacet(7+is,6+is,2+is,3+is);
2231 polyhedron->
AddFacet(6+is,5+is,1+is,2+is);
2233 polyhedron->
AddFacet(5+sub4,6+sub4,7+sub4,8+sub4);
void ClipCrossSection(G4ThreeVectorList *pVertices, const G4int pSectionIndex, const G4VoxelLimits &pVoxelLimit, const EAxis pAxis, G4double &pMin, G4double &pMax) const
G4Polyhedron * GetPolyhedron() const
G4int GetVisSubdivisions() const
void SetSolidClosed(const G4bool t)
G4GenericTrap(const G4String &name, G4double halfZ, const std::vector< G4TwoVector > &vertices)
G4double GetMinYExtent() const
CLHEP::Hep3Vector G4ThreeVector
G4VisExtent GetExtent() const
virtual void DescribeYourselfTo(G4VGraphicsScene &scene) const
double dot(const Hep3Vector &) const
G4Polyhedron * fpPolyhedron
G4bool IsYLimited() const
virtual G4VisExtent GetExtent() const
virtual G4double GetCubicVolume()
G4bool CalculateExtent(const EAxis pAxis, const G4VoxelLimits &pVoxelLimit, const G4AffineTransform &pTransform, G4double &pmin, G4double &pmax) const
G4GeometryType GetEntityType() const
G4bool IsXLimited() const
virtual void AddSolid(const G4Box &)=0
G4ThreeVector SurfaceNormal(const G4ThreeVector &p) const
G4double GetSurfaceArea()
G4double GetMaxXExtent() const
G4double GetMinZExtent() const
G4ThreeVector GetPointOnSurface() const
virtual G4double DistanceToOut(const G4ThreeVector &p) const
G4GenericTrap & operator=(const G4GenericTrap &rhs)
virtual G4Polyhedron * CreatePolyhedron() const
virtual EInside Inside(const G4ThreeVector &p) const
std::ostream & StreamInfo(std::ostream &os) const
G4bool AddFacet(G4VFacet *aFacet)
G4double GetTwistAngle(G4int index) const
std::vector< G4ThreeVector > G4ThreeVectorList
G4double DistanceToOut(const G4ThreeVector &p, const G4ThreeVector &v, const G4bool calcNorm=false, G4bool *validNorm=0, G4ThreeVector *n=0) const
G4double GetCubicVolume()
virtual G4double DistanceToIn(const G4ThreeVector &p, const G4ThreeVector &v) const
void AddVertex(const G4ThreeVector &v)
EInside Inside(const G4ThreeVector &p) const
void G4Exception(const char *originOfException, const char *exceptionCode, G4ExceptionSeverity severity, const char *comments)
virtual G4Polyhedron * GetPolyhedron() const
G4double GetMinXExtent() const
G4Polyhedron * CreatePolyhedron() const
G4double GetMaxZExtent() const
static G4int GetNumberOfRotationSteps()
void AddFacet(const G4int iv1, const G4int iv2, const G4int iv3, const G4int iv4=0)
G4double GetMaxYExtent() const
G4VSolid & operator=(const G4VSolid &rhs)
G4int GetNumberOfRotationStepsAtTimeOfCreation() const
Hep3Vector cross(const Hep3Vector &) const
G4double DistanceToIn(const G4ThreeVector &p, const G4ThreeVector &v) const
G4double GetMaxExtent(const EAxis pAxis) const
virtual G4bool CalculateExtent(const EAxis pAxis, const G4VoxelLimits &pVoxelLimit, const G4AffineTransform &pTransform, G4double &pMin, G4double &pMax) const
G4bool IsZLimited() const
virtual G4double GetSurfaceArea()
virtual G4ThreeVector GetPointOnSurface() const
virtual G4ThreeVector SurfaceNormal(const G4ThreeVector &p) const
void ClipBetweenSections(G4ThreeVectorList *pVertices, const G4int pSectionIndex, const G4VoxelLimits &pVoxelLimit, const EAxis pAxis, G4double &pMin, G4double &pMax) const
G4double GetMinExtent(const EAxis pAxis) const
void DescribeYourselfTo(G4VGraphicsScene &scene) const