46#if !defined(G4GEOM_USE_UEXTRUDEDSOLID)
67 const std::vector<G4TwoVector>& polygon,
68 const std::vector<ZSection>& zsections)
71 fNz(zsections.size()),
73 fGeometryType(
"G4ExtrudedSolid"),
82 std::ostringstream message;
83 message <<
"Number of vertices in polygon < 3 - " << pName;
84 G4Exception(
"G4ExtrudedSolid::G4ExtrudedSolid()",
"GeomSolids0002",
90 std::ostringstream message;
91 message <<
"Number of z-sides < 2 - " << pName;
92 G4Exception(
"G4ExtrudedSolid::G4ExtrudedSolid()",
"GeomSolids0002",
98 if ( zsections[i].fZ > zsections[i+1].fZ )
100 std::ostringstream message;
101 message <<
"Z-sections have to be ordered by z value (z0 < z1 < z2...) - "
103 G4Exception(
"G4ExtrudedSolid::G4ExtrudedSolid()",
"GeomSolids0002",
108 std::ostringstream message;
109 message <<
"Z-sections with the same z position are not supported - "
111 G4Exception(
"G4ExtrudedSolid::G4ExtrudedSolid()",
"GeomSolids0001",
122 std::vector<G4int> removedVertices;
125 if (removedVertices.size() != 0)
127 G4int nremoved = removedVertices.size();
128 std::ostringstream message;
129 message <<
"The following "<< nremoved
130 <<
" vertices have been removed from polygon in " << pName
131 <<
"\nas collinear or coincident with other vertices: "
132 << removedVertices[0];
133 for (
G4int i=1; i<nremoved; ++i) message <<
", " << removedVertices[i];
134 G4Exception(
"G4ExtrudedSolid::G4ExtrudedSolid()",
"GeomSolids1001",
141 std::ostringstream message;
142 message <<
"Number of vertices in polygon after removal < 3 - " << pName;
143 G4Exception(
"G4ExtrudedSolid::G4ExtrudedSolid()",
"GeomSolids0002",
166 std::ostringstream message;
167 message <<
"Making facets failed - " << pName;
168 G4Exception(
"G4ExtrudedSolid::G4ExtrudedSolid()",
"GeomSolids0003",
190 const std::vector<G4TwoVector>& polygon,
197 fGeometryType(
"G4ExtrudedSolid")
205 std::ostringstream message;
206 message <<
"Number of vertices in polygon < 3 - " << pName;
207 G4Exception(
"G4ExtrudedSolid::G4ExtrudedSolid()",
"GeomSolids0002",
217 std::vector<G4int> removedVertices;
220 if (removedVertices.size() != 0)
222 G4int nremoved = removedVertices.size();
223 std::ostringstream message;
224 message <<
"The following "<< nremoved
225 <<
" vertices have been removed from polygon in " << pName
226 <<
"\nas collinear or coincident with other vertices: "
227 << removedVertices[0];
228 for (
G4int i=1; i<nremoved; ++i) message <<
", " << removedVertices[i];
229 G4Exception(
"G4ExtrudedSolid::G4ExtrudedSolid()",
"GeomSolids1001",
236 std::ostringstream message;
237 message <<
"Number of vertices in polygon after removal < 3 - " << pName;
238 G4Exception(
"G4ExtrudedSolid::G4ExtrudedSolid()",
"GeomSolids0002",
262 std::ostringstream message;
263 message <<
"Making facets failed - " << pName;
264 G4Exception(
"G4ExtrudedSolid::G4ExtrudedSolid()",
"GeomSolids0003",
273 if ((scale1 == 1) && (scale2 == 1)
285 fGeometryType(
"G4ExtrudedSolid")
295 fPolygon(rhs.fPolygon), fZSections(rhs.fZSections),
296 fTriangles(rhs.fTriangles), fIsConvex(rhs.fIsConvex),
297 fGeometryType(rhs.fGeometryType),
298 fSolidType(rhs.fSolidType), fPlanes(rhs.fPlanes),
299 fLines(rhs.fLines), fLengths(rhs.fLengths),
300 fKScales(rhs.fKScales), fScale0s(rhs.fScale0s),
301 fKOffsets(rhs.fKOffsets), fOffset0s(rhs.fOffset0s)
311 if (
this == &rhs) {
return *
this; }
359 G4double kscale = (scale2 - scale1)/(z2 - z1);
360 G4double scale0 = scale2 - kscale*(z2 - z1)/2.0;
379 for (
G4int i=0, k=Nv-1; i<Nv; k=i++)
393 for (
G4int i=0, k=Nv-1; i<Nv; k=i++)
450 return (p2 - poffset)/pscale;
461 if ( l1.
x() == l2.
x() )
476 G4bool squareComp = (dy*dy < (1+slope*slope)
512 return ( (p1.
x() - l1.
x()) * (l2.
y() - l1.
y())
513 - (l2.
x() - l1.
x()) * (p1.
y() - l1.
y()) )
514 * ( (p2.
x() - l1.
x()) * (l2.
y() - l1.
y())
515 - (l2.
x() - l1.
x()) * (p2.
y() - l1.
y()) ) > 0;
530 if ( ( p.
x() < a.
x() && p.
x() < b.
x() && p.
x() < c.
x() ) ||
531 ( p.
x() > a.
x() && p.
x() > b.
x() && p.
x() > c.
x() ) ||
532 ( p.
y() < a.
y() && p.
y() < b.
y() && p.
y() < c.
y() ) ||
533 ( p.
y() > a.
y() && p.
y() > b.
y() && p.
y() > c.
y() ) )
return false;
545 return inside || onEdge;
560 G4double result = (std::atan2(t1.
y(), t1.
x()) - std::atan2(t2.
y(), t2.
x()));
562 if ( result < 0 ) result += 2*
pi;
575 std::vector<G4ThreeVector> vertices;
583 = (vertices[1]-vertices[0]).cross(vertices[2]-vertices[1]);
585 if ( cross.
z() > 0.0 )
593 vertices[1] = vertices[2];
609 std::vector<G4ThreeVector> vertices;
617 = (vertices[1]-vertices[0]).cross(vertices[2]-vertices[1]);
619 if ( cross.
z() < 0.0 )
627 vertices[1] = vertices[2];
641 typedef std::pair < G4TwoVector, G4int > Vertex;
643 static const G4double kAngTolerance =
648 std::vector< Vertex > verticesToBeDone;
651 verticesToBeDone.push_back(Vertex(
fPolygon[i], i));
653 std::vector< Vertex > ears;
655 std::vector< Vertex >::iterator c1 = verticesToBeDone.begin();
656 std::vector< Vertex >::iterator c2 = c1+1;
657 std::vector< Vertex >::iterator c3 = c1+2;
658 while ( verticesToBeDone.size()>2 )
675 while (
angle >= (
pi-kAngTolerance) )
684 if ( c3 == verticesToBeDone.end() ) { c3 = verticesToBeDone.begin(); }
697 G4Exception(
"G4ExtrudedSolid::AddGeneralPolygonFacets",
699 "Triangularisation has failed.");
705 for (
auto it=verticesToBeDone.cbegin(); it!=verticesToBeDone.cend(); ++it )
709 if ( it == c1 || it == c2 || it == c3 ) {
continue; }
711 if (
IsPointInside(c1->first, c2->first, c3->first, it->first) )
721 if ( c3 == verticesToBeDone.end() ) { c3 = verticesToBeDone.begin(); }
737 if ( ! result ) {
return false; }
740 if ( ! result ) {
return false; }
742 std::vector<G4int> triangle(3);
743 triangle[0] = c1->second;
744 triangle[1] = c2->second;
745 triangle[2] = c3->second;
750 verticesToBeDone.erase(c2);
751 c1 = verticesToBeDone.begin();
773 if ( ! good ) {
return false; }
779 if ( ! good ) {
return false; }
781 std::vector<G4int> triangle(3);
793 if ( ! good ) {
return false; }
800 if ( ! good ) {
return false; }
802 std::vector<G4int> triangle1(3);
808 std::vector<G4int> triangle2(3);
817 if ( ! good ) {
return false; }
822 for (
G4int iz = 0; iz <
fNz-1; ++iz )
830 if ( ! good ) {
return false; }
867 for (
G4int i=0; i<np; ++i)
870 if (dd > dist) { dist = dd; }
937 fPolygon[(*it)[2]], pscaled) ) { inside =
true; }
939 }
while ( (inside ==
false) && (it !=
fTriangles.cend()) );
1011 if (ix*ix + iy*iy > sqrCarToleranceHalf)
continue;
1017 if (kx*kx + ky*ky > sqrCarToleranceHalf)
continue;
1022 if (dd*dd > sqrCarToleranceHalf)
continue;
1042 else if (nsurf != 0)
1051 std::ostringstream message;
1052 G4int oldprc = message.precision(16);
1053 message <<
"Point p is not on surface (!?) of solid: "
1055 message <<
"Position:\n";
1056 message <<
" p.x() = " << p.
x()/
mm <<
" mm\n";
1057 message <<
" p.y() = " << p.
y()/
mm <<
" mm\n";
1058 message <<
" p.z() = " << p.
z()/
mm <<
" mm";
1059 G4cout.precision(oldprc) ;
1060 G4Exception(
"G4TesselatedSolid::SurfaceNormal(p)",
"GeomSolids1002",
1096 if (tmp < dd) { dd = tmp; iside = i; }
1103 if (tmp < dd) { dd = tmp; iside = i; }
1109 if (tmp < dd) { dd = tmp; iside = i; }
1122 if (
std::max(dz0,dz1) > 0) iregion = 1;
1125 if (!in) iregion += 2;
1133 if (ddz0 <= ddz1 && ddz0 <= dd)
return G4ThreeVector(0, 0,-1);
1134 if (ddz1 <= ddz0 && ddz1 <= dd)
return G4ThreeVector(0, 0, 1);
1148 if (dzmax*dzmax > dd)
return G4ThreeVector(0,0,(dz0 > dz1) ? -1 : 1);
1176 G4double ddz = (invz < 0) ? dz : -dz;
1183 G4double txmin = tzmin, txmax = tzmax;
1184 for (
G4int i=0; i<np; ++i)
1192 if (txmin < tmp) { txmin = tmp; }
1197 if (txmax > tmp) { txmax = tmp; }
1203 G4double tmin = txmin, tmax = txmax;
1227 for (
G4int i=0; i<np; ++i)
1230 if (dd > dist) dist = dd;
1232 return (dist > 0) ? dist : 0.;
1240 return (distz > 0) ? distz : 0;
1246 if (distz > 0) dd += distz*distz;
1247 return std::sqrt(dd);
1264 G4bool getnorm = calcNorm;
1265 if (getnorm) *validNorm =
true;
1271 if (getnorm)
n->set(0,0,-1);
1276 if (getnorm)
n->set(0,0,1);
1291 G4int iside = (vz < 0) ? -4 : -2;
1296 for (
G4int i=0; i<np; ++i)
1308 if (tmax > tmp) { tmax = tmp; iside = i; }
1317 {
n->set(0, 0, iside + 3); }
1333 if (validNorm) { *validNorm =
fIsConvex; }
1348 for (
G4int i=0; i<np; ++i)
1351 if (dd > dist) dist = dd;
1353 return (dist < 0) ? -dist : 0.;
1359 if (distz >= 0 || (!in))
return 0;
1380 if (x < xmin0) xmin0 = x;
1381 if (x > xmax0) xmax0 = x;
1383 if (y < ymin0) ymin0 = y;
1384 if (y > ymax0) ymax0 = y;
1391 for (
G4int i=0; i<nsect; ++i)
1397 xmin =
std::min(xmin,xmin0*scale+dx);
1398 xmax =
std::max(xmax,xmax0*scale+dx);
1399 ymin =
std::min(ymin,ymin0*scale+dy);
1400 ymax =
std::max(ymax,ymax0*scale+dy);
1406 pMin.set(xmin,ymin,zmin);
1407 pMax.set(xmax,ymax,zmax);
1413 std::ostringstream message;
1414 message <<
"Bad bounding box (min >= max) for solid: "
1416 <<
"\npMin = " <<
pMin
1417 <<
"\npMax = " <<
pMax;
1445 return exist = (
pMin <
pMax) ?
true :
false;
1459 std::ostringstream message;
1460 message <<
"Triangulation of the base polygon has failed for solid: "
1462 <<
"\nExtent has been calculated using boundary box";
1470 std::vector<const G4ThreeVectorList *> polygons;
1471 polygons.resize(nsect);
1477 G4int ntria = triangles.size()/3;
1478 for (
G4int i=0; i<ntria; ++i)
1481 for (
G4int k=0; k<nsect; ++k)
1490 G4ThreeVectorList::iterator iter = ptr->begin();
1491 G4double x0 = triangles[i3+0].x()*scale+dx;
1492 G4double y0 = triangles[i3+0].y()*scale+dy;
1495 G4double x1 = triangles[i3+1].x()*scale+dx;
1496 G4double y1 = triangles[i3+1].y()*scale+dy;
1499 G4double x2 = triangles[i3+2].x()*scale+dx;
1500 G4double y2 = triangles[i3+2].y()*scale+dy;
1510 if (eminlim >
pMin && emaxlim <
pMax)
break;
1513 for (
G4int k=0; k<nsect; ++k) {
delete polygons[k]; polygons[k]=0;}
1521 G4int oldprc = os.precision(16);
1522 os <<
"-----------------------------------------------------------\n"
1523 <<
" *** Dump for solid - " <<
GetName() <<
" ***\n"
1524 <<
" ===================================================\n"
1528 { os <<
" Convex polygon; list of vertices:" <<
G4endl; }
1530 { os <<
" Concave polygon; list of vertices:" <<
G4endl; }
1534 os << std::setw(5) <<
"#" << i
1539 os <<
" Sections:" <<
G4endl;
1564 os.precision(oldprc);
std::vector< G4ThreeVector > G4ThreeVectorList
static const G4double emax
void G4Exception(const char *originOfException, const char *exceptionCode, G4ExceptionSeverity severity, const char *description)
static const G4double pMax
static const G4double pMin
static constexpr double mm
static constexpr double pi
static const G4double angle[DIMMOTT]
CLHEP::Hep3Vector G4ThreeVector
CLHEP::Hep2Vector G4TwoVector
G4GLOB_DLL std::ostream G4cout
G4bool BoundingBoxVsVoxelLimits(const EAxis pAxis, const G4VoxelLimits &pVoxelLimits, const G4Transform3D &pTransform3D, G4double &pMin, G4double &pMax) const
G4bool CalculateExtent(const EAxis pAxis, const G4VoxelLimits &pVoxelLimits, const G4Transform3D &pTransform3D, G4double &pMin, G4double &pMax) const
G4double DistanceToPolygonSqr(const G4ThreeVector &p) const
G4bool CalculateExtent(const EAxis pAxis, const G4VoxelLimits &pVoxelLimit, const G4AffineTransform &pTransform, G4double &pMin, G4double &pMax) const
std::vector< G4double > fKScales
EInside Inside(const G4ThreeVector &p) const
G4double DistanceToOut(const G4ThreeVector &p, const G4ThreeVector &v, const G4bool calcNorm=false, G4bool *validNorm=nullptr, G4ThreeVector *n=nullptr) const
std::ostream & StreamInfo(std::ostream &os) const
virtual ~G4ExtrudedSolid()
G4double GetAngle(const G4TwoVector &p0, const G4TwoVector &pa, const G4TwoVector &pb) const
G4ExtrudedSolid & operator=(const G4ExtrudedSolid &rhs)
std::vector< ZSection > fZSections
void BoundingLimits(G4ThreeVector &pMin, G4ThreeVector &pMax) const
G4GeometryType fGeometryType
G4bool IsSameLineSegment(const G4TwoVector &p, const G4TwoVector &l1, const G4TwoVector &l2) const
std::vector< G4TwoVector > fOffset0s
G4VFacet * MakeDownFacet(G4int ind1, G4int ind2, G4int ind3) const
G4TwoVector ProjectPoint(const G4ThreeVector &point) const
G4VFacet * MakeUpFacet(G4int ind1, G4int ind2, G4int ind3) const
G4bool IsSameSide(const G4TwoVector &p1, const G4TwoVector &p2, const G4TwoVector &l1, const G4TwoVector &l2) const
void ComputeProjectionParameters()
G4double DistanceToIn(const G4ThreeVector &p, const G4ThreeVector &v) const
std::vector< G4double > fLengths
G4ThreeVector SurfaceNormal(const G4ThreeVector &p) const
G4ThreeVector ApproxSurfaceNormal(const G4ThreeVector &p) const
G4GeometryType GetEntityType() const
void ComputeLateralPlanes()
G4bool PointInPolygon(const G4ThreeVector &p) const
G4bool IsSameLine(const G4TwoVector &p, const G4TwoVector &l1, const G4TwoVector &l2) const
G4bool IsPointInside(const G4TwoVector &a, const G4TwoVector &b, const G4TwoVector &c, const G4TwoVector &p) const
std::vector< G4double > fScale0s
G4bool AddGeneralPolygonFacets()
std::vector< G4TwoVector > fKOffsets
ZSection GetZSection(G4int index) const
G4int GetNofZSections() const
std::vector< plane > fPlanes
G4int GetNofVertices() const
G4TwoVector GetVertex(G4int index) const
G4ExtrudedSolid(const G4String &pName, const std::vector< G4TwoVector > &polygon, const std::vector< ZSection > &zsections)
std::vector< line > fLines
std::vector< std::vector< G4int > > fTriangles
std::vector< G4TwoVector > fPolygon
static G4GeometryTolerance * GetInstance()
G4double GetAngularTolerance() const
G4double GetMinYExtent() const
G4double GetMinZExtent() const
G4TessellatedSolid & operator=(const G4TessellatedSolid &right)
G4double kCarToleranceHalf
virtual G4double DistanceToIn(const G4ThreeVector &p, const G4ThreeVector &v) const
G4bool AddFacet(G4VFacet *aFacet)
G4double GetMaxYExtent() const
G4double GetMaxZExtent() const
G4double GetMaxXExtent() const
virtual G4double DistanceToOut(const G4ThreeVector &p) const
G4double GetMinXExtent() const
void SetSolidClosed(const G4bool t)
virtual G4ThreeVector SurfaceNormal(const G4ThreeVector &p) const
G4double GetMinExtent(const EAxis pAxis) const
G4double GetMaxExtent(const EAxis pAxis) const
static const G4double kInfinity
T max(const T t1, const T t2)
brief Return the largest of the two arguments
T min(const T t1, const T t2)
brief Return the smallest of the two arguments