Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-09-10 09:10:42

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 // G4PolyPhiFace
0027 //
0028 // Class description:
0029 //
0030 // Definition of a face that bounds a polycone or polyhedra when
0031 // it has a phi opening:
0032 //
0033 //   G4PolyPhiFace( const G4ReduciblePolygon* rz,
0034 //                        G4double phi,
0035 //                        G4double deltaPhi,
0036 //                        G4double phiOther )
0037 //
0038 // Specifically: a face that lies on a plane that passes through
0039 // the z axis. It has boundaries that are straight lines of arbitrary
0040 // length and direction, but with corners aways on the same side of
0041 // the z axis.
0042 
0043 // Author: David C. Williams (UCSC), 1998
0044 // --------------------------------------------------------------------
0045 #ifndef G4POLYPHIFACE_HH
0046 #define G4POLYPHIFACE_HH 1
0047 
0048 #include "G4VCSGface.hh"
0049 #include "G4TwoVector.hh"
0050 
0051 class G4ReduciblePolygon;
0052 
0053 struct G4PolyPhiFaceVertex
0054 {
0055   G4double x, y, r, z;   // position
0056   G4double rNorm, 
0057            zNorm;        // r/z normal
0058   G4ThreeVector norm3D;  // 3D normal
0059 
0060   // Needed for Triangulation Algorithm
0061   //
0062   G4bool ear;
0063   G4PolyPhiFaceVertex *next,*prev;
0064 };
0065 
0066 struct G4PolyPhiFaceEdge
0067 {
0068   G4PolyPhiFaceEdge() = default;
0069   G4PolyPhiFaceVertex  *v0{nullptr}, *v1{nullptr};  // Corners
0070   G4double tr{.0}, tz{0.},        // Unit vector along edge
0071            length{0.};            // Length of edge
0072   G4ThreeVector norm3D;           // 3D edge normal vector
0073 };
0074 
0075 /**
0076  * @brief G4PolyPhiFace is a face that bounds a polycone or polyhedra when
0077  * it has a phi opening. Specifically, it is a face that lies on a plane that
0078  * passes through the Z axis, having boundaries that are straight lines of
0079  * arbitrary length and direction, but with corners aways on the same side of
0080  * the Z axis.
0081  */
0082 
0083 class G4PolyPhiFace : public G4VCSGface
0084 {
0085 
0086   public:
0087 
0088     /**
0089      * Constructor where points r,z should be supplied in clockwise order
0090      * in r,z.
0091      * For example:
0092      *                [1]---------[2]         ^ R
0093      *                 |           |          |
0094      *                 |           |          +--> z
0095      *                [0]---------[3]
0096      *  @param[in] rz Pointer to previous r,Z section.
0097      *  @param[in] phi Initial Phi starting angle.
0098      *  @param[in] deltaPhi Total Phi angle.
0099      *  @param[in] phiOther Phi angle of next section.
0100      */
0101     G4PolyPhiFace( const G4ReduciblePolygon* rz,
0102                          G4double phi, G4double deltaPhi, G4double phiOther );
0103 
0104     /**
0105      * Destructor. Removes edges and corners.
0106      */
0107     ~G4PolyPhiFace() override;
0108 
0109     /**
0110      * Copy constructor and assignment operator.
0111      */
0112     G4PolyPhiFace( const G4PolyPhiFace& source );
0113     G4PolyPhiFace& operator=( const G4PolyPhiFace& source );
0114 
0115     /**
0116      * Determines the distance along a line to the face.
0117      *  @param[in] p Position.
0118      *  @param[in] v Direction (assumed to be a unit vector).
0119      *  @param[in] outgoing Flag true, to consider only inside surfaces;
0120      *             false, to consider only outside surfaces.
0121      *  @param[in] surfTolerance Minimum distance from the surface.
0122      *  @param[out] distance Distance to intersection.
0123      *  @param[out] distFromSurface Distance from surface (along surface normal),
0124      *              < 0 if the point is in front of the surface.
0125      *  @param[out] normal Normal of surface at intersection point.
0126      *  @param[out] allBehind Flag, true, if entire surface is behind normal.
0127      *  @returns true if there is an intersection, false otherwise.
0128      */
0129     G4bool Intersect( const G4ThreeVector& p, const G4ThreeVector& v,
0130                             G4bool outgoing, G4double surfTolerance,
0131                             G4double& distance, G4double& distFromSurface,
0132                             G4ThreeVector& normal, G4bool& allBehind ) override;
0133 
0134     /**
0135      * Determines the distance of a point from either the inside or outside
0136      * surfaces of the face.
0137      *  @param[in] p Position.
0138      *  @param[in] outgoing Flag, true, to consider only inside surfaces
0139      *             or false, to consider only outside surfaces.
0140      *  @returns The distance to the closest surface satisfying requirements
0141      *           or kInfinity if no such surface exists.
0142      */
0143     G4double Distance( const G4ThreeVector& p, G4bool outgoing ) override;
0144   
0145     /**
0146      * Determines whether a point is inside, outside, or on the surface of
0147      * the face.
0148      *  @param[in] p Position.
0149      *  @param[in] tolerance Tolerance defining the bounds of the "kSurface",
0150      *             nominally equal to kCarTolerance/2.
0151      *  @param[out] bestDistance Distance to the closest surface (in or out).
0152      *  @returns kInside if the point is closest to the inside surface;
0153      *           kOutside if the point is closest to the outside surface;
0154      *           kSurface if the point is withing tolerance of the surface.
0155      */
0156     EInside Inside( const G4ThreeVector& p, G4double tolerance, 
0157                           G4double* bestDistance ) override;
0158     
0159     /**
0160      * Returns the normal of surface closest to the point.
0161      *  @param[in] p Position.
0162      *  @param[out] bestDistance Distance to the closest surface (in or out).
0163      *  @returns The normal of the surface nearest the point.
0164      */
0165     G4ThreeVector Normal( const G4ThreeVector& p,
0166                                 G4double* bestDistance ) override;
0167 
0168     /**
0169      * Returns the face extent along the axis.
0170      *  @param[in] axis Unit vector defining the direction.
0171      *  @returns The largest point along the given axis of the face's extent.
0172      */
0173     G4double Extent( const G4ThreeVector axis ) override;
0174   
0175     /**
0176      * Calculates the extent of the face for the voxel navigator.
0177      *  @param[in] axis The axis in which to check the shapes 3D extent against.
0178      *  @param[in] voxelLimit Limits along x, y, and/or z axes.
0179      *  @param[in] tranform A coordinate transformation on which to apply to
0180      *             the shape before testing.
0181      *  @param[out] extentList The list of (voxel) extents along the axis.
0182      */
0183     void CalculateExtent( const EAxis axis, 
0184                           const G4VoxelLimits &voxelLimit,
0185                           const G4AffineTransform& tranform,
0186                                 G4SolidExtentList& extentList ) override;
0187 
0188     /**
0189      * Method invoked by the copy constructor or the assignment operator.
0190      * Its purpose is to return a pointer to a duplicate copy of the face.
0191      */
0192     inline G4VCSGface* Clone() override;
0193 
0194     /**
0195      * Returning an estimation of the face surface area, in internal units.
0196      */
0197     G4double SurfaceArea() override;
0198 
0199     /**
0200      * Fake default constructor for usage restricted to direct object
0201      * persistency for clients requiring preallocation of memory for
0202      * persistifiable objects.
0203      */
0204     G4PolyPhiFace(__void__&);
0205 
0206     /**
0207      * Throws an exception if something is found inconsistent with the solid.
0208      * For debugging purposes only.
0209      */
0210     void Diagnose( G4VSolid* solid );
0211 
0212   private:
0213 
0214     /**
0215      * Calculates the surface area of a triangle. 
0216      * At the same time a random point in the triangle is given.
0217      */
0218     G4double SurfaceTriangle( const G4ThreeVector& p1, const G4ThreeVector& p2,
0219                               const G4ThreeVector& p3, G4ThreeVector* p4);
0220 
0221     /**
0222      * Auxiliary method for GetPointOnSurface().
0223      */
0224     G4ThreeVector GetPointOnFace() override;
0225 
0226     /**
0227      * Decides if the point in r,z is inside the edges of a face,
0228      * **but** do so consistently with other faces.
0229      */
0230     G4bool InsideEdgesExact( G4double r, G4double z, G4double normSign,
0231                              const G4ThreeVector& p, const G4ThreeVector& v );
0232 
0233     /**
0234      * Methods to decide if the point in r,z is inside the edges of a face.
0235      */
0236     G4bool InsideEdges( G4double r, G4double z );
0237     G4bool InsideEdges( G4double r, G4double z, G4double* distRZ2,
0238                         G4PolyPhiFaceVertex** base3Dnorm = nullptr,
0239                         G4ThreeVector** head3Dnorm = nullptr );
0240 
0241     /**
0242      * Decides precisely whether a trajectory passes to the left, right,
0243      * or exactly passes through the Z position of a vertex point in face.
0244      */
0245     inline G4double ExactZOrder( G4double z, 
0246                                  G4double qx, G4double qy, G4double qz, 
0247                            const G4ThreeVector& v, 
0248                                  G4double normSign,
0249                            const G4PolyPhiFaceVertex* vert ) const;
0250 
0251     /**
0252      * Copies parameters from other object; used in copy constructor and
0253      * assignment operator.
0254      */
0255     void CopyStuff( const G4PolyPhiFace& source );
0256 
0257     // Functions used for Triangulation in Case of generic Polygone.
0258     // The triangulation is used for GetPointOnFace()
0259 
0260     /**
0261      * Calculates of 2*Area of Triangle with Sign.
0262      */
0263     G4double Area2( const G4TwoVector& a, const G4TwoVector& b, const G4TwoVector& c);
0264 
0265     /**
0266      * Boolean functions for sign of Surface.
0267      */
0268     G4bool Left( const G4TwoVector& a, const G4TwoVector& b, const G4TwoVector& c );
0269     G4bool LeftOn( const G4TwoVector& a, const G4TwoVector& b, const G4TwoVector& c );
0270     G4bool Collinear( const G4TwoVector& a, const G4TwoVector& b, const G4TwoVector& c );
0271 
0272     /**
0273      * Boolean function for finding proper intersection of two
0274      * line segments (a,b) and (c,d).
0275      */
0276     G4bool IntersectProp( const G4TwoVector& a, const G4TwoVector& b,
0277                           const G4TwoVector& c, const G4TwoVector& d );
0278 
0279     /**
0280      * Boolean function for determining if point c is between a and b
0281      * where the three points (a,b,c) are on the same line.
0282      */
0283     G4bool Between( const G4TwoVector& a, const G4TwoVector& b, const G4TwoVector& c );
0284 
0285     /**
0286      * Boolean function for finding proper intersection or not
0287      * of two line segments (a,b) and (c,d).
0288      */
0289     G4bool Intersect( const G4TwoVector& a, const G4TwoVector& b,
0290                       const G4TwoVector& c, const G4TwoVector& d );
0291 
0292     /**
0293      * Boolean Diagonalie help to determine if diagonal s
0294      * of segment (a,b) is convex or reflex.
0295      */
0296     G4bool Diagonalie( G4PolyPhiFaceVertex* a, G4PolyPhiFaceVertex* b );
0297 
0298     /**
0299      * Boolean function for determining if b is inside the cone (a0,a,a1)
0300      * where a is the center of the cone.
0301      */
0302     G4bool InCone( G4PolyPhiFaceVertex *a, G4PolyPhiFaceVertex *b );
0303 
0304     /**
0305      * Boolean function for determining if Diagonal is possible
0306      * inside Polycone or PolyHedra.
0307      */
0308     G4bool Diagonal( G4PolyPhiFaceVertex* a, G4PolyPhiFaceVertex* b );
0309 
0310     /**
0311      * Initialisation for Triangulisation by ear tips.
0312      * For details see "Computational Geometry in C" by Joseph O'Rourke.
0313      */
0314     void EarInit();
0315 
0316     /**
0317      * Triangularisation by ear tips for Polycone or Polyhedra.
0318      * For details see "Computational Geometry in C" by Joseph O'Rourke.
0319      * NOTE: a copy of the shape is made and this copy is reordered in
0320      *       order to have a list of triangles. This list is used by the
0321      *       method GetPointOnFace().
0322      */
0323     void Triangulate();
0324 
0325   private:
0326 
0327     G4int numEdges = 0;  // Number of edges
0328     G4PolyPhiFaceEdge* edges = nullptr;       // The edges of the face
0329     G4PolyPhiFaceVertex* corners = nullptr;   // And the corners
0330     G4ThreeVector normal;        // Normal unit vector
0331     G4ThreeVector radial;        // Unit vector along radial direction
0332     G4ThreeVector surface;       // Point on surface
0333     G4ThreeVector surface_point; // Auxiliary point on surface used for
0334                                  // method GetPointOnFace() 
0335     G4double rMin, rMax, // Extent in r
0336              zMin, zMax; // Extent in z
0337     G4bool allBehind = false; // True if the polycone/polyhedra
0338                               // is behind the place of this face
0339     G4double kCarTolerance;      // Surface thickness
0340     G4double fSurfaceArea = 0.0; // Surface Area of PolyPhiFace 
0341 
0342      /** Auxiliary pointer to 'corners' used for triangulation.
0343          Copy structure, changing the structure of 'corners' (ear removal). */
0344     G4PolyPhiFaceVertex* triangles = nullptr;
0345 };
0346 
0347 #include "G4PolyPhiFace.icc"
0348 
0349 #endif