45 return ((Bx-Ax)*(Cy-Ay) - (By-Ay)*(Cx-Ax))*0.5;
57 return ((
B.x()-Ax)*(
C.y()-Ay) - (
B.y()-Ay)*(
C.x()-Ax))*0.5;
69 return ((
C.x()-
A.x())*(
D.y()-
B.y()) - (
C.y()-
A.y())*(
D.x()-
B.x()))*0.5;
78 auto n = (
G4int)p.size();
79 if (n < 3) {
return 0.0; }
81 G4double area = p[n-1].x()*p[0].y() - p[0].x()*p[n-1].y();
82 for(
G4int i=1; i<n; ++i)
84 area += p[i-1].x()*p[i].y() - p[i].x()*p[i-1].y();
99 if ((Bx-Ax)*(Cy-Ay) - (By-Ay)*(Cx-Ax) > 0.)
101 if ((Ax-Cx)*(Py-Cy) - (Ay-Cy)*(Px-Cx) < 0.) {
return false; }
102 if ((Bx-Ax)*(Py-Ay) - (By-Ay)*(Px-Ax) < 0.) {
return false; }
103 if ((Cx-Bx)*(Py-By) - (Cy-By)*(Px-Bx) < 0.) {
return false; }
107 if ((Ax-Cx)*(Py-Cy) - (Ay-Cy)*(Px-Cx) > 0.) {
return false; }
108 if ((Bx-Ax)*(Py-Ay) - (By-Ay)*(Px-Ax) > 0.) {
return false; }
109 if ((Cx-Bx)*(Py-By) - (Cy-By)*(Px-Bx) > 0.) {
return false; }
127 if ((Bx-Ax)*(Cy-Ay) - (By-Ay)*(Cx-Ax) > 0.)
129 if ((Ax-Cx)*(Py-Cy) - (Ay-Cy)*(Px-Cx) < 0.) {
return false; }
130 if ((Bx-Ax)*(Py-Ay) - (By-Ay)*(Px-Ax) < 0.) {
return false; }
131 if ((Cx-Bx)*(Py-By) - (Cy-By)*(Px-Bx) < 0.) {
return false; }
135 if ((Ax-Cx)*(Py-Cy) - (Ay-Cy)*(Px-Cx) > 0.) {
return false; }
136 if ((Bx-Ax)*(Py-Ay) - (By-Ay)*(Px-Ax) > 0.) {
return false; }
137 if ((Cx-Bx)*(Py-By) - (Cy-By)*(Px-Bx) > 0.) {
return false; }
149 auto Nv = (
G4int)v.size();
151 for (
G4int i = 0, k = Nv - 1; i < Nv; k = i++)
153 if ((v[i].y() > p.
y()) != (v[k].y() > p.
y()))
155 G4double ctg = (v[k].x()-v[i].x())/(v[k].y()-v[i].y());
156 in ^=
static_cast<int>(p.
x() < (p.
y()-v[i].y())*ctg + v[i].x());
171 G4bool gotNegative =
false;
172 G4bool gotPositive =
false;
173 auto n = (
G4int)polygon.size();
174 if (n <= 0) {
return false; }
175 for (
G4int icur=0; icur<n; ++icur)
177 G4int iprev = (icur == 0) ? n-1 : icur-1;
178 G4int inext = (icur == n-1) ? 0 : icur+1;
181 G4double cross = e1.x()*e2.y() - e1.y()*e2.x();
183 if (cross < 0) { gotNegative =
true; }
184 if (cross > 0) { gotPositive =
true; }
185 if (gotNegative && gotPositive) {
return false; }
198 std::vector<G4int> triangles;
201 auto n = (
G4int)triangles.size();
202 for (
G4int i=0; i<n; ++i) { result.push_back(polygon[triangles[i]]); }
211 std::vector<G4int>& result)
217 auto n = (
G4int)polygon.size();
218 if (n < 3) {
return false; }
223 auto V =
new G4int[n];
226 for (
G4int i=0; i<n; ++i) { V[i] = i; }
230 for (
G4int i=0; i<n; ++i) { V[i] = (n-1)-i; }
237 for(
G4int b=nv-1; nv>2; )
243 if (area < 0.) { std::reverse(result.begin(),result.end()); }
248 G4int a = (b < nv) ? b : 0;
249 b = (a+1 < nv) ? a+1 : 0;
250 G4int c = (b+1 < nv) ? b+1 : 0;
252 if (CheckSnip(polygon, a,b,c, nv,V))
255 result.push_back(V[a]);
256 result.push_back(V[b]);
257 result.push_back(V[c]);
261 for(
G4int i=b; i<nv; ++i) { V[i] = V[i+1]; }
267 if (area < 0.) { std::reverse(result.begin(),result.end()); }
284 G4double Ax = contour[V[a]].x(), Ay = contour[V[a]].y();
285 G4double Bx = contour[V[b]].x(), By = contour[V[b]].y();
286 G4double Cx = contour[V[c]].x(), Cy = contour[V[c]].y();
287 if ((Bx-Ax)*(Cy-Ay) - (By-Ay)*(Cx-Ax) <
kCarTolerance) {
return false; }
290 G4double xmin = std::min(std::min(Ax,Bx),Cx);
291 G4double xmax = std::max(std::max(Ax,Bx),Cx);
292 G4double ymin = std::min(std::min(Ay,By),Cy);
293 G4double ymax = std::max(std::max(Ay,By),Cy);
296 if((i == a) || (i == b) || (i == c)) {
continue; }
298 if (Px < xmin || Px > xmax) {
continue; }
300 if (Py < ymin || Py > ymax) {
continue; }
311 std::vector<G4int>& iout,
320 auto nv = (
G4int)polygon.size();
325 G4int icur = 0, iprev = 0, inext = 0, nout = 0;
326 for (
G4int i=0; i<nv; ++i)
330 for (
G4int k=1; k<nv+1; ++k)
333 if (iprev < 0) { iprev += nv; }
334 if (polygon[iprev].x() != removeIt) {
break; }
337 for (
G4int k=1; k<nv+1; ++k)
340 if (inext >= nv) { inext -= nv; }
341 if (polygon[inext].x() != removeIt) {
break; }
344 if (iprev == inext) {
break; }
356 if (leng1 <= delta || leng2 <= delta || leng3 <= delta)
358 polygon[icur].setX(removeIt); ++nout;
362 G4double lmax = std::max(std::max(leng1,leng2),leng3);
363 G4double area = std::abs(e1.x()*e2.y()-e1.y()*e2.x())*0.5;
364 if (area/std::sqrt(lmax) <= std::abs(tolerance))
366 polygon[icur].setX(removeIt); ++nout;
376 for (
G4int i=0; i<nv; ++i) { iout.push_back(i); }
380 for (
G4int i=0; i<nv; ++i)
382 if (polygon[i].x() != removeIt)
384 polygon[icur++] = polygon[i];
391 if (icur < nv) { polygon.resize(icur); }
410 if (rmin < 0) {
return false; }
416 pmin.
set(-rmax,-rmax);
417 pmax.
set( rmax, rmax);
418 if (delPhi >= CLHEP::twopi) {
return true; }
421 std::sin(startPhi),std::cos(startPhi),
422 std::sin(startPhi+delPhi),std::cos(startPhi+delPhi),
442 pmin.
set(-rmax,-rmax);
443 pmax.
set( rmax, rmax);
454 G4int icase = (cosEnd < 0) ? 1 : 0;
455 if (sinEnd < 0) { icase += 2; }
456 if (cosStart < 0) { icase += 4; }
457 if (sinStart < 0) { icase += 8; }
463 if (sinEnd < sinStart) {
break; }
464 pmin.
set(rmin*cosEnd,rmin*sinStart);
465 pmax.
set(rmax*cosStart,rmax*sinEnd );
468 pmin.
set(rmax*cosEnd,std::min(rmin*sinStart,rmin*sinEnd));
469 pmax.
set(rmax*cosStart,rmax );
472 pmin.
set(-rmax,-rmax);
473 pmax.
set(std::max(rmax*cosStart,rmax*cosEnd),rmax);
476 pmin.
set(-rmax,rmax*sinEnd);
477 pmax.
set(rmax*cosStart,rmax);
481 pmin.
set(-rmax,-rmax);
482 pmax.
set(rmax,std::max(rmax*sinStart,rmax*sinEnd));
485 if (sinEnd > sinStart) {
break; }
486 pmin.
set(rmax*cosEnd,rmin*sinEnd );
487 pmax.
set(rmin*cosStart,rmax*sinStart);
490 pmin.
set(-rmax,-rmax);
491 pmax.
set(rmax*cosEnd,rmax*sinStart);
494 pmin.
set(-rmax,rmax*sinEnd);
495 pmax.
set(std::max(rmin*cosStart,rmin*cosEnd),rmax*sinStart);
499 pmin.
set(std::min(rmin*cosStart,rmin*cosEnd),rmax*sinStart);
500 pmax.
set(rmax,rmax*sinEnd);
503 pmin.
set(rmax*cosEnd,rmax*sinStart);
507 if (sinEnd < sinStart) {
break; }
508 pmin.
set(rmin*cosStart,rmax*sinStart);
509 pmax.
set(rmax*cosEnd,rmin*sinEnd );
512 pmin.
set(-rmax,std::min(rmax*sinStart,rmax*sinEnd));
517 pmin.
set(rmax*cosStart,-rmax);
518 pmax.
set(rmax,rmax*sinEnd);
521 pmin.
set(std::min(rmax*cosStart,rmax*cosEnd),-rmax);
525 pmin.
set(rmax*cosStart,-rmax);
526 pmax.
set(rmax*cosEnd,std::max(rmin*sinStart,rmin*sinEnd));
529 if (sinEnd > sinStart) {
break; }
530 pmin.
set(rmax*cosStart,rmax*sinEnd);
531 pmax.
set(rmin*cosEnd,rmin*sinStart);
547 G4double e = std::sqrt((1. - b/a)*(1. + b/a));
548 return 4. * a * comp_ellint_2(e);
564 G4double e = std::sqrt((1. - b/a)*(1. + b/a)) / std::hypot(1.,b/h);
565 return 2. * a * std::hypot(b,h) * comp_ellint_2(e);
581 const G4double eps = 1. / 134217728.;
584 G4double b = std::sqrt((1. - e)*(1. + e));
585 if (b == 1.) {
return CLHEP::halfpi; }
586 if (b == 0.) {
return 1.; }
592 while (x - y > eps*y)
598 S +=
M * (x - y)*(x - y);
600 return 0.5 * CLHEP::halfpi * ((a + b)*(a + b) -
S) / (x + y);
611 return ((
B-
A).cross(
C-
A))*0.5;
623 return ((
C-
A).cross(
D-
B))*0.5;
632 auto n = (
G4int)p.size();
633 if (n < 3) {
return {0,0,0}; }
635 for(
G4int i=1; i<n; ++i)
637 normal += p[i-1].
cross(p[i]);
654 if (u <= 0) {
return AP.
mag(); }
657 if (u >= len2) {
return (
B-P).mag(); }
659 return ((u/len2)*AB - AP).mag();
675 if (u <= 0) {
return A; }
678 if (u >= len2) {
return B; }
736 region = (t0 < 0) ? ((t1 < 0) ? 4 : 3) : ((t1 < 0) ? 5 : 0);
740 region = (t0 < 0) ? 2 : ((t1 < 0) ? 6 : 1);
748 return A + (t0*invDet)*edge0 + (t1*invDet)*edge1;
753 if (numer <= 0) {
return C; }
755 return (numer >= denom) ?
B :
C + (numer/denom)*(edge0-edge1);
765 return (numer >= denom) ?
B :
C + (numer/denom)*(edge0-edge1);
768 return (tmp1 <= 0) ?
C : (( e >= 0) ?
A :
A + (-e/c)*edge1);
771 return (e >= 0) ?
A : ((-e >= c) ?
C :
A + (-e/c)*edge1);
774 if (d < 0) {
return (-d >= a) ?
B :
A + (-d/a)*edge0; }
775 return (e >= 0) ?
A : ((-e >= c) ?
C :
A + (-e/c)*edge1);
778 return (d >= 0) ?
A : ((-d >= a) ?
B :
A + (-d/a)*edge0);
788 return (numer >= denom) ?
C :
B + (numer/denom)*(edge1-edge0);
791 return (tmp1 <= 0) ?
B : (( d >= 0) ?
A :
A + (-d/a)*edge0);
794 return {kInfinity,kInfinity,kInfinity};
815 if (rmin < 0) {
return false; }
822 if (stheta < 0 && stheta > CLHEP::pi) {
return false; }
823 if (stheta + dtheta > CLHEP::pi) { dtheta = CLHEP::pi - stheta; }
828 pmin.
set(-rmax,-rmax,-rmax);
829 pmax.
set( rmax, rmax, rmax);
830 if (dtheta >= CLHEP::pi && delPhi >= CLHEP::twopi) {
return true; }
833 G4double sinStart = std::sin(stheta);
834 G4double cosStart = std::cos(stheta);
838 G4double rhomin = rmin*std::min(sinStart,sinEnd);
840 if (stheta > CLHEP::halfpi) { rhomax = rmax*sinStart; }
841 if (etheta < CLHEP::halfpi) { rhomax = rmax*sinEnd; }
845 std::sin(startPhi),std::cos(startPhi),
846 std::sin(startPhi+delPhi),std::cos(startPhi+delPhi),
849 G4double zmin = std::min(rmin*cosEnd,rmax*cosEnd);
850 G4double zmax = std::max(rmin*cosStart,rmax*cosStart);
851 pmin.
set(xymin.
x(),xymin.
y(),zmin);
852 pmax.
set(xymax.
x(),xymax.
y(),zmax);
866 return std::atan(std::sqrt((r - r0)*(r + r0))/std::abs(h));
882 G4double rmin = std::abs(endInnerRad);
883 G4double rmax = std::abs(endOuterRad);
890 vertices[0].set(rmin*cosphi, rmin*sinphi);
891 vertices[1].set(rmax, rmax*tanphi);
892 vertices[2].set(rmax,-rmax*tanphi);
893 vertices[3].set(rmin*cosphi,-rmin*sinphi);
894 vertices[4] = vertices[0];
895 vertices[5] = vertices[1];
896 vertices[6] = vertices[2];
897 vertices[7] = vertices[3];
901 for(
auto i = 0; i < 4; ++i)
903 vertices[i].rotate(-ang);
904 vertices[i + 4].rotate(ang);
924 if (t <
kCarTolerance) {
return a*std::abs(zmax - zmin)*phi; }
925 G4double rmin = std::hypot(t*zmin, a);
926 G4double rmax = std::hypot(t*zmax, a);
929 G4double smin = rmin*std::hypot(rmin, zmin);
930 G4double smax = rmax*std::hypot(rmax, zmax);
931 return (zmin*zmax < 0.) ? (smin + smax)*phi/2. : std::abs(smax - smin)*phi/2.;
940 G4double smin = a*(hmin*std::hypot(1., k*hmin) + std::asinh(k*hmin)/k);
941 if (zmax == -zmin) {
return smin*phi; }
944 G4double smax = a*(hmax*std::hypot(1., k*hmax) + std::asinh(k*hmax)/k);
945 return (zmin*zmax < 0.) ? (smin + smax)*phi/2. :std::abs(smax - smin)*phi/2.;
const G4double kCarTolerance
G4double C(G4double temp)
G4double S(G4double temp)
G4double B(G4double temperature)
G4double D(G4double temp)
CLHEP::Hep3Vector G4ThreeVector
CLHEP::Hep2Vector G4TwoVector
void set(double x, double y)
Hep3Vector cross(const Hep3Vector &) const
double dot(const Hep3Vector &) const
void set(double x, double y, double z)
G4double GetSurfaceTolerance() const
static G4GeometryTolerance * GetInstance()