Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-07-15 08:20:43

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 // G4GenericTrap
0027 //
0028 // Class description:
0029 //
0030 // G4GenericTrap is a solid which represents an arbitrary trapezoid with
0031 // up to 8 vertices standing on two parallel planes perpendicular to Z axis.
0032 //
0033 // Parameters in the constructor:
0034 // - name               - solid name
0035 // - halfZ              - the solid half length in Z
0036 // - vertices           - the (x,y) coordinates of vertices:
0037 //                        o first four points: vertices[i], i<4
0038 //                          are the vertices sitting on the -halfZ plane;
0039 //                        o last four points: vertices[i], i>=4
0040 //                          are the vertices sitting on the +halfZ plane.
0041 //
0042 // The order of defining the vertices of the solid is the following:
0043 //      - point 0 is connected with points 1,3,4
0044 //      - point 1 is connected with points 0,2,5
0045 //      - point 2 is connected with points 1,3,6
0046 //      - point 3 is connected with points 0,2,7
0047 //      - point 4 is connected with points 0,5,7
0048 //      - point 5 is connected with points 1,4,6
0049 //      - point 6 is connected with points 2,5,7
0050 //      - point 7 is connected with points 3,4,6
0051 // Points can be identical in order to create shapes with less than 8 vertices.
0052 // Adapted from Arb8 implementation in Root/TGeo.
0053 
0054 // Authors: T.Nikitina (CERN) & I.Hrivnacova (IPN, Orsay), 27.05.2010 - Created
0055 //          Evgueni Tcherniaev (CERN), 27.05.2024 - Complete revision, speed up
0056 // -------------------------------------------------------------------
0057 #ifndef G4GENERICTRAP_HH
0058 #define G4GENERICTRAP_HH
0059 
0060 #include "G4GeomTypes.hh"
0061 
0062 #if defined(G4GEOM_USE_USOLIDS)
0063 #define G4GEOM_USE_UGENERICTRAP 1
0064 #endif
0065 
0066 #if defined(G4GEOM_USE_UGENERICTRAP)
0067   #define G4UGenericTrap G4GenericTrap
0068   #include "G4UGenericTrap.hh"
0069 #else
0070 
0071 #include <vector>
0072 
0073 #include "globals.hh"
0074 #include "G4TwoVector.hh"
0075 #include "G4VSolid.hh"
0076 
0077 /**
0078  * @brief G4GenericTrap is a solid which represents an arbitrary trapezoid with
0079  * up to 8 vertices standing on two parallel planes perpendicular to the Z axis.
0080  * Points can be identical in order to create shapes with less than 8 vertices.
0081  */
0082 
0083 class G4GenericTrap : public G4VSolid
0084 {
0085   public:
0086 
0087     /**
0088      * Constructs an generic trapezoid, given its vertices.
0089      *  @param[in] name The solid name.
0090      *  @param[in] halfZ Half length in Z.
0091      *  @param[in] vertices The (x,y) coordinates of the vertices.
0092      */
0093     G4GenericTrap(const G4String& name, G4double halfZ,
0094                   const std::vector<G4TwoVector>& vertices);
0095 
0096     /**
0097      * Fake default constructor for usage restricted to direct object
0098      * persistency for clients requiring preallocation of memory for
0099      * persistifiable objects.
0100      */
0101     G4GenericTrap(__void__&);
0102 
0103     /**
0104      * Copy constructor and assignment operator.
0105      */
0106     G4GenericTrap(const G4GenericTrap& rhs);
0107     G4GenericTrap& operator=(const G4GenericTrap& rhs);
0108 
0109     /**
0110      * Default Destructor.
0111      */
0112     ~G4GenericTrap() override = default;
0113 
0114     /**
0115      * Accessors and modifiers.
0116      */
0117     inline G4double    GetZHalfLength() const;
0118     inline G4int       GetNofVertices() const;
0119     inline G4TwoVector GetVertex(G4int index) const;
0120     inline const std::vector<G4TwoVector>& GetVertices() const;
0121     inline G4double    GetTwistAngle(G4int index) const;
0122     inline G4bool      IsTwisted() const;
0123     inline G4int       GetVisSubdivisions() const;
0124     inline void        SetVisSubdivisions(G4int subdiv);
0125 
0126     /**
0127      * Concrete implementations of the expected query interfaces for
0128      * solids, as defined in the base class G4VSolid.
0129      */
0130     EInside Inside(const G4ThreeVector& p) const override;
0131     G4ThreeVector SurfaceNormal(const G4ThreeVector& p) const override;
0132     G4double DistanceToIn(const G4ThreeVector& p,
0133                           const G4ThreeVector& v) const override;
0134     G4double DistanceToIn(const G4ThreeVector& p) const override;
0135     G4double DistanceToOut(const G4ThreeVector& p,
0136                            const G4ThreeVector& v,
0137                            const G4bool calcNorm = false,
0138                                  G4bool* validNorm = nullptr,
0139                                  G4ThreeVector* n = nullptr) const override;
0140     G4double DistanceToOut(const G4ThreeVector& p) const override;
0141 
0142     /**
0143      * Computes the bounding limits of the solid.
0144      *  @param[out] pMin The minimum bounding limit point.
0145      *  @param[out] pMax The maximum bounding limit point.
0146      */
0147     void BoundingLimits(G4ThreeVector& pMin, G4ThreeVector& pMax) const override;
0148 
0149     /**
0150      * Calculates the minimum and maximum extent of the solid, when under the
0151      * specified transform, and within the specified limits.
0152      *  @param[in] pAxis The axis along which compute the extent.
0153      *  @param[in] pVoxelLimit The limiting space dictated by voxels.
0154      *  @param[in] pTransform The internal transformation applied to the solid.
0155      *  @param[out] pMin The minimum extent value.
0156      *  @param[out] pMax The maximum extent value.
0157      *  @returns True if the solid is intersected by the extent region.
0158      */
0159     G4bool CalculateExtent(const EAxis pAxis,
0160                            const G4VoxelLimits& pVoxelLimit,
0161                            const G4AffineTransform& pTransform,
0162                                  G4double& pmin, G4double& pmax) const override;
0163 
0164     /**
0165      * Returns the type ID, "G4GenericTrap" of the solid.
0166      */
0167     G4GeometryType GetEntityType() const override;
0168 
0169     /**
0170      * Returns true if the solid has only planar faces; false if twisted.
0171      */
0172     G4bool IsFaceted () const override;
0173 
0174     /**
0175      * Makes a clone of the object for use in multi-treading.
0176      *  @returns A pointer to the new cloned allocated solid.
0177      */
0178     G4VSolid* Clone() const override;
0179 
0180     /**
0181      * Streams the object contents to an output stream.
0182      */
0183     std::ostream& StreamInfo(std::ostream& os) const override;
0184 
0185     /**
0186      * Returns a random point located and uniformly distributed on the
0187      * surface of the solid.
0188      */
0189     G4ThreeVector GetPointOnSurface() const override ;
0190 
0191     /**
0192      * Returning an estimation of the solid volume (capacity) and
0193      * surface area, in internal units.
0194      */
0195     G4double GetCubicVolume() override;
0196     G4double GetSurfaceArea() override;
0197 
0198     /**
0199      * Methods for creating graphical representations (i.e. for visualisation).
0200      */
0201     void DescribeYourselfTo(G4VGraphicsScene& scene) const override;
0202     G4VisExtent GetExtent() const override;
0203     G4Polyhedron* CreatePolyhedron() const override;
0204     G4Polyhedron* GetPolyhedron() const override;
0205 
0206   private:
0207 
0208     /**
0209      * Algorithm for SurfaceNormal() following the original
0210      * specification for points not on the surface.
0211      */
0212     G4ThreeVector ApproxSurfaceNormal(const G4ThreeVector& p) const;
0213 
0214     /**
0215      * Checks the parameters of the solid and issues exception if leading
0216      * to an invalid construct.
0217      */
0218     void CheckParameters(G4double halfZ, const std::vector<G4TwoVector>& vertices);
0219 
0220     /**
0221      * Computes surface equations and twist angles of lateral faces.
0222      */
0223     void ComputeLateralSurfaces();
0224 
0225     /**
0226      * Sets the bounding box.
0227      */
0228     void ComputeBoundingBox();
0229 
0230     /**
0231      * Sets the max length of a scratch.
0232      */
0233     void ComputeScratchLength();
0234 
0235     /**
0236      * Computes the lateral face area, given the face index.
0237      * Used for random sampling of points on surface.
0238      */
0239     G4double GetLateralFaceArea(G4int iface) const;
0240 
0241     /**
0242      * Logger methods for issuing warnings.
0243      */
0244     void WarningSignA(const G4String& method, const G4String& icase, G4double A,
0245                       const G4ThreeVector& p, const G4ThreeVector& v) const;
0246     void WarningSignB(const G4String& method, const G4String& icase, G4double f, G4double B,
0247                       const G4ThreeVector& p, const G4ThreeVector& v) const;
0248     void WarningDistanceToIn(G4int k, const G4ThreeVector& p, const G4ThreeVector& v,
0249                              G4double tmin, G4double tmax,
0250                              const G4double ttin[2], const G4double ttout[2]) const;
0251     void WarningDistanceToOut(const G4ThreeVector& p,
0252                               const G4ThreeVector& v,
0253                               G4double tout) const;
0254 
0255   private:
0256 
0257     struct G4GenericTrapPlane // Ax + By + Cz + D = 0
0258     {
0259       G4double A = 0.;
0260       G4double B = 0.;
0261       G4double C = 0.;
0262       G4double D = 0.;
0263     };
0264     struct G4GenericTrapSurface // Axz + Byz + Czz + Dx + Ey + Fz + G = 0
0265     {
0266       G4double A = 0.;
0267       G4double B = 0.;
0268       G4double C = 0.;
0269       G4double D = 0.;
0270       G4double E = 0.;
0271       G4double F = 0.;
0272       G4double G = 0.;
0273     };
0274 
0275     // Data members
0276     G4double                 halfTolerance = 0.;
0277     G4double                 fScratch = 0.;
0278     G4double                 fDz = 0.;
0279     std::vector<G4TwoVector> fVertices = {0.,0.,0.,0.,0.,0.,0.,0.};
0280     G4TwoVector              fDelta[4];
0281     G4bool                   fIsTwisted = false;
0282     G4double                 fTwist[5] = {0.};
0283     G4ThreeVector            fMinBBox{0.};
0284     G4ThreeVector            fMaxBBox{0.};
0285     G4int                    fVisSubdivisions = 0;
0286     G4GenericTrapPlane       fPlane[8];
0287     G4GenericTrapSurface     fSurf[4];
0288     G4double                 f4k[4] = {0.}; // Lipschitz constants * 4
0289     mutable G4double         fArea[4] = {0.};
0290     mutable G4bool           fRebuildPolyhedron = false;
0291     mutable G4Polyhedron*    fpPolyhedron = nullptr;
0292 
0293     // Surface and Volume
0294     G4double                 fSurfaceArea = 0.;
0295     G4double                 fCubicVolume = 0.;
0296 };
0297 
0298 #include "G4GenericTrap.icc"
0299 
0300 #endif // defined(G4GEOM_USE_UGENERICTRAP)
0301 
0302 #endif // G4GENERICTRAP_HH