Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-07-29 09:08:46

0001 //
0002 // ********************************************************************
0003 // * License and Disclaimer                                           *
0004 // *                                                                  *
0005 // * The  Geant4 software  is  copyright of the Copyright Holders  of *
0006 // * the Geant4 Collaboration.  It is provided  under  the terms  and *
0007 // * conditions of the Geant4 Software License,  included in the file *
0008 // * LICENSE and available at  http://cern.ch/geant4/license .  These *
0009 // * include a list of copyright holders.                             *
0010 // *                                                                  *
0011 // * Neither the authors of this software system, nor their employing *
0012 // * institutes,nor the agencies providing financial support for this *
0013 // * work  make  any representation or  warranty, express or implied, *
0014 // * regarding  this  software system or assume any liability for its *
0015 // * use.  Please see the license in the file  LICENSE  and URL above *
0016 // * for the full disclaimer and the limitation of liability.         *
0017 // *                                                                  *
0018 // * This  code  implementation is the result of  the  scientific and *
0019 // * technical work of the GEANT4 collaboration.                      *
0020 // * By using,  copying,  modifying or  distributing the software (or *
0021 // * any work based  on the software)  you  agree  to acknowledge its *
0022 // * use  in  resulting  scientific  publications,  and indicate your *
0023 // * acceptance of all terms of the Geant4 Software license.          *
0024 // ********************************************************************
0025 //
0026 // G4GeomTools
0027 //
0028 // Class description:
0029 //
0030 // A collection of utilities which can be helpful for a wide range
0031 // of geometry-related tasks
0032 
0033 // Author: Evgueni Tcherniaev (CERN), 10.10.2016
0034 // --------------------------------------------------------------------
0035 #ifndef G4GEOMTOOLS_HH
0036 #define G4GEOMTOOLS_HH
0037 
0038 #include <vector>
0039 #include "G4TwoVector.hh"
0040 #include "G4ThreeVector.hh"
0041 
0042 using G4TwoVectorList = std::vector<G4TwoVector>;
0043 using G4ThreeVectorList = std::vector<G4ThreeVector>;
0044 
0045 /**
0046  * @brief G4GeomTools is a collecting utilities which can be helpful
0047  * for a wide range of geometry-related tasks.
0048  */
0049 
0050 class G4GeomTools
0051 {
0052   public:
0053 
0054     // ==================================================================
0055     //   2D Utilities
0056     // ------------------------------------------------------------------
0057 
0058     /**
0059      * Functions to calculate the area of 2D triangle, returned value is
0060      * positive if the vertices of the triangle are given in anticlockwise
0061      * order, otherwise it is negative.
0062      */
0063     static G4double TriangleArea(G4double Ax, G4double Ay,
0064                                  G4double Bx, G4double By,
0065                                  G4double Cx, G4double Cy);
0066     static G4double TriangleArea(const G4TwoVector& A,
0067                                  const G4TwoVector& B,
0068                                  const G4TwoVector& C);
0069 
0070     /**
0071      * Calculates the area of a 2D quadrilateral, returned value is positive if
0072      * the vertices of the quadrilateral are given in anticlockwise order,
0073      * otherwise it is negative.
0074      */
0075     static G4double QuadArea(const G4TwoVector& A,
0076                              const G4TwoVector& B,
0077                              const G4TwoVector& C,
0078                              const G4TwoVector& D);
0079 
0080     /**
0081      * Calculates the area of a 2D polygon, returned value is positive if
0082      * the vertices of the polygon are defined in anticlockwise order,
0083      * otherwise it is negative.
0084      */
0085     static G4double PolygonArea(const G4TwoVectorList& polygon);
0086 
0087     /**
0088      * Decides if a point (Px,Py) is inside the triangle (Ax,Ay)(Bx,By)(Cx,Cy).
0089      */
0090     static G4bool PointInTriangle(G4double Px, G4double Py,
0091                                   G4double Ax, G4double Ay,
0092                                   G4double Bx, G4double By,
0093                                   G4double Cx, G4double Cy);
0094 
0095     /**
0096      * Decides if a point P is inside the triangle ABC.
0097      */
0098     static G4bool PointInTriangle(const G4TwoVector& P,
0099                                   const G4TwoVector& A,
0100                                   const G4TwoVector& B,
0101                                   const G4TwoVector& C);
0102 
0103     /**
0104      * Decides if a point P is inside the 'Polygon'.
0105      */
0106     static G4bool PointInPolygon(const G4TwoVector& P,
0107                                  const G4TwoVectorList& Polygon);
0108 
0109     /**
0110      * Decides if a 2D 'polygon' is convex, i.e. if all internal angles are
0111      * less than pi.
0112      */
0113     static G4bool IsConvex(const G4TwoVectorList& polygon);
0114 
0115     /**
0116      * Simple implementation of "ear clipping" algorithm for triangulation
0117      * of a simple contour/polygon, it places results in a std::vector as
0118      * triplets of vertices. If triangulation is successful the function
0119      * returns true, otherwise false.
0120      */
0121     static G4bool TriangulatePolygon(const G4TwoVectorList& polygon,
0122                                            std::vector<G4int>& result);
0123 
0124     /**
0125      * Same using the function above and returning as 'result' a list
0126      * of triangles.
0127      */
0128     static G4bool TriangulatePolygon(const G4TwoVectorList& polygon,
0129                                            G4TwoVectorList& result);
0130 
0131     /**
0132      * Removes collinear and coincident points from a 2D 'polygon'.
0133      * Indices of removed points are available in 'iout'.
0134      * Allows to specify a 'tolerance'.
0135      */
0136     static void RemoveRedundantVertices(G4TwoVectorList& polygon,
0137                                         std::vector<G4int>& iout,
0138                                         G4double tolerance = 0.0);
0139 
0140     /**
0141      * Calculates the bounding rectangle of a disk sector. It returns false
0142      * if the input parameters do not meet the following criteria:
0143      *   rmin   >= 0
0144      *   rmax   >  rmin + kCarTolerance
0145      *   delPhi >  0 + kCarTolerance.
0146      */
0147     static G4bool DiskExtent(G4double rmin, G4double rmax,
0148                              G4double startPhi, G4double delPhi,
0149                              G4TwoVector& pmin, G4TwoVector& pmax);
0150 
0151     /**
0152      * Calculates the bounding rectangle of a disk sector.
0153      * Faster version without check of parameters.
0154      */
0155     static void DiskExtent(G4double rmin, G4double rmax,
0156                            G4double sinPhiStart, G4double cosPhiStart,
0157                            G4double sinPhiEnd, G4double cosPhiEnd,
0158                            G4TwoVector& pmin, G4TwoVector& pmax);
0159 
0160     /**
0161      * Computes the circumference (perimeter) of an ellipse.
0162      */
0163     static G4double EllipsePerimeter(G4double a,
0164                                      G4double b);
0165 
0166     /**
0167      * Computes the lateral surface area of an elliptic cone.
0168      */
0169     static G4double EllipticConeLateralArea(G4double a,
0170                                             G4double b,
0171                                             G4double h);
0172 
0173     // ==================================================================
0174     //   3D Utilities
0175     // ------------------------------------------------------------------
0176 
0177     /**
0178      * Finds the normal to the plane of a 3D triangle ABC;
0179      * the length of the normal is equal to the area of the triangle.
0180      */
0181     static G4ThreeVector TriangleAreaNormal(const G4ThreeVector& A,
0182                                             const G4ThreeVector& B,
0183                                             const G4ThreeVector& C);
0184 
0185     /**
0186      * Finds the normal to the plane of a 3D quadrilateral ABCD;
0187      * the length of the normal is equal to the area of the quadrilateral.
0188      */
0189     static G4ThreeVector QuadAreaNormal(const G4ThreeVector& A,
0190                                         const G4ThreeVector& B,
0191                                         const G4ThreeVector& C,
0192                                         const G4ThreeVector& D);
0193 
0194     /**
0195      * Finds the normal to the plane of a 3D polygon; the length of the
0196      * normal is equal to the area of the polygon.
0197      */
0198     static G4ThreeVector PolygonAreaNormal(const G4ThreeVectorList& polygon);
0199 
0200     /**
0201      * Calculates the distance between a point 'P' and line segment AB in 3D.
0202      */
0203     static G4double DistancePointSegment(const G4ThreeVector& P,
0204                                          const G4ThreeVector& A,
0205                                          const G4ThreeVector& B);
0206 
0207     /**
0208      * Finds a point on a 3D line segment AB closest to point 'P'.
0209      */
0210     static G4ThreeVector ClosestPointOnSegment(const G4ThreeVector& P,
0211                                                const G4ThreeVector& A,
0212                                                const G4ThreeVector& B);
0213 
0214     /**
0215      * Finds a point on a 3D triangle ABC closest to point 'P'.
0216      */
0217     static G4ThreeVector ClosestPointOnTriangle(const G4ThreeVector& P,
0218                                                 const G4ThreeVector& A,
0219                                                 const G4ThreeVector& B,
0220                                                 const G4ThreeVector& C);
0221 
0222     /**
0223      * Calculates the bounding box of a spherical sector,
0224      *  @returns false if input parameters do not meet the following criteria:
0225      *   rmin       >= 0
0226      *   rmax       >  rmin + kCarTolerance
0227      *   startTheta >= 0 && <= pi;
0228      *   delTheta   >  0 + kCarTolerance
0229      *   delPhi     >  0 + kCarTolerance.
0230      */
0231     static G4bool SphereExtent(G4double rmin, G4double rmax,
0232                                G4double startTheta, G4double delTheta,
0233                                G4double startPhi, G4double delPhi,
0234                                G4ThreeVector& pmin, G4ThreeVector& pmax);
0235 
0236     /**
0237      * Calculates the hyperbolic surface stereo. Stereo is a half angle at the
0238      * intersection point of the two lines in the tangent plane cross-section.
0239      */
0240     static G4double HypeStereo(G4double r0, // radius at z = 0
0241                                G4double r,  // radius at z = h
0242                                G4double h);
0243 
0244     /**
0245      * Finds the XY-coordinates of the corners of a generic trap that bounds
0246      * specified twisted tube.
0247      *  @param[in] twistAng The twist angle.
0248      *  @param[in] endInnerRad The inner radius at z = halfZ.
0249      *  @param[in] endOuterRad The outer radius at z = halfZ.
0250      *  @param[in] dPhi Delta phi.
0251      *  @param[in] vertices The corners of the generic trap.
0252      */
0253     static void TwistedTubeBoundingTrap(G4double twistAng,
0254                                         G4double endInnerRad,
0255                                         G4double endOuterRad,
0256                                         G4double dPhi,
0257                                         G4TwoVectorList& vertices);
0258 
0259     /**
0260      * Calculate surface area of the hyperboloid between 'zmin' and 'zmax'.
0261      *  @param[in] dphi Delta phi.
0262      *  @param[in] r0 The radius at z = 0.
0263      *  @param[in] tanstereo The tangent of the stereo angle.
0264      *  @param[in] zmin Minimum Z.
0265      *  @param[in] zmax Maximum Z.
0266      */
0267     static G4double HyperboloidSurfaceArea(G4double dphi,
0268                                            G4double r0,
0269                                            G4double tanstereo,
0270                                            G4double zmin,
0271                                            G4double zmax);
0272 
0273   private:
0274 
0275     /**
0276      * Helper function for use by TriangulatePolygon().
0277      */
0278     static G4bool CheckSnip(const G4TwoVectorList& contour,
0279                             G4int a, G4int b, G4int c,
0280                             G4int n, const G4int* V);
0281 
0282     /**
0283      * Complete Elliptic Integral of the Second Kind.
0284      */
0285     static G4double comp_ellint_2(G4double e);
0286 };
0287 
0288 #endif