Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-09-18 09:15:45

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 and of QinetiQ Ltd,   *
0020 // * subject to DEFCON 705 IPR conditions.                            *
0021 // * By using,  copying,  modifying or  distributing the software (or *
0022 // * any work based  on the software)  you  agree  to acknowledge its *
0023 // * use  in  resulting  scientific  publications,  and indicate your *
0024 // * acceptance of all terms of the Geant4 Software license.          *
0025 // ********************************************************************
0026 //
0027 // G4TessellatedSolid
0028 //
0029 // Class description:
0030 //
0031 //    G4TessellatedSolid is a special Geant4 solid defined by a number of
0032 //    facets (UVFacet). It is important that the supplied facets shall form a
0033 //    fully enclose space which is the solid.
0034 //    At the moment only two types of facet can be used for the construction of
0035 //    a G4TessellatedSolid, i.e. the G4TriangularFacet and G4QuadrangularFacet.
0036 //
0037 //    How to contruct a G4TessellatedSolid:
0038 //
0039 //    First declare a tessellated solid:
0040 //
0041 //      G4TessellatedSolid* solidTarget = new G4TessellatedSolid("Solid_name");
0042 //
0043 //    Define the facets which form the solid
0044 //
0045 //      G4double targetSiz = 10*cm ;
0046 //      G4TriangularFacet *facet1 = new
0047 //      G4TriangularFacet (G4ThreeVector(-targetSize,-targetSize,        0.0),
0048 //                         G4ThreeVector(+targetSize,-targetSize,        0.0),
0049 //                         G4ThreeVector(        0.0,        0.0,+targetSize),
0050 //                         ABSOLUTE);
0051 //      G4TriangularFacet *facet2 = new
0052 //      G4TriangularFacet (G4ThreeVector(+targetSize,-targetSize,        0.0),
0053 //                         G4ThreeVector(+targetSize,+targetSize,        0.0),
0054 //                         G4ThreeVector(        0.0,        0.0,+targetSize),
0055 //                         ABSOLUTE);
0056 //      G4TriangularFacet *facet3 = new
0057 //      G4TriangularFacet (G4ThreeVector(+targetSize,+targetSize,        0.0),
0058 //                         G4ThreeVector(-targetSize,+targetSize,        0.0),
0059 //                         G4ThreeVector(        0.0,        0.0,+targetSize),
0060 //                         ABSOLUTE);
0061 //      G4TriangularFacet *facet4 = new
0062 //      G4TriangularFacet (G4ThreeVector(-targetSize,+targetSize,        0.0),
0063 //                         G4ThreeVector(-targetSize,-targetSize,        0.0),
0064 //                         G4ThreeVector(        0.0,        0.0,+targetSize),
0065 //                         ABSOLUTE);
0066 //      G4QuadrangularFacet *facet5 = new
0067 //      G4QuadrangularFacet (G4ThreeVector(-targetSize,-targetSize,      0.0),
0068 //                           G4ThreeVector(-targetSize,+targetSize,      0.0),
0069 //                           G4ThreeVector(+targetSize,+targetSize,      0.0),
0070 //                           G4ThreeVector(+targetSize,-targetSize,      0.0),
0071 //                           ABSOLUTE);
0072 //
0073 //    Then add the facets to the solid:
0074 //
0075 //      solidTarget->AddFacet((UVFacet*) facet1);
0076 //      solidTarget->AddFacet((UVFacet*) facet2);
0077 //      solidTarget->AddFacet((UVFacet*) facet3);
0078 //      solidTarget->AddFacet((UVFacet*) facet4);
0079 //      solidTarget->AddFacet((UVFacet*) facet5);
0080 //
0081 //    Finally declare the solid is complete:
0082 //
0083 //      solidTarget->SetSolidClosed(true);
0084 
0085 // 31.10.2004, P R Truscott, QinetiQ Ltd, UK - Created.
0086 // 12.10.2012, M Gayer, CERN - New implementation with voxelization of surfaces.
0087 // --------------------------------------------------------------------
0088 #ifndef G4TESSELLATEDSOLID_HH
0089 #define G4TESSELLATEDSOLID_HH 1
0090 
0091 #include "G4GeomTypes.hh"
0092 
0093 #if defined(G4GEOM_USE_USOLIDS)
0094 #define G4GEOM_USE_UTESSELLATEDSOLID 1
0095 #endif
0096 
0097 #if defined(G4GEOM_USE_UTESSELLATEDSOLID)
0098   #define G4UTessellatedSolid G4TessellatedSolid
0099   #include "G4UTessellatedSolid.hh"
0100 #else
0101 
0102 #include <iostream>
0103 #include <vector>
0104 #include <set>
0105 #include <map>
0106 
0107 #include "G4Types.hh"
0108 #include "G4VSolid.hh"
0109 #include "G4Voxelizer.hh"
0110 #include "G4VFacet.hh"
0111 
0112 struct G4VertexInfo
0113 {
0114   G4int id;
0115   G4double mag2;
0116 };
0117 
0118 class G4VertexComparator
0119 {
0120 public:
0121   G4bool operator() (const G4VertexInfo& l, const G4VertexInfo& r) const
0122   {
0123     return l.mag2 == r.mag2 ? l.id < r.id : l.mag2 < r.mag2;
0124   }
0125 };
0126 
0127 class G4TessellatedSolid : public G4VSolid
0128 {
0129   public:
0130 
0131     G4TessellatedSolid ();
0132     ~G4TessellatedSolid () override;
0133 
0134     G4TessellatedSolid (const G4String& name);
0135 
0136     G4TessellatedSolid(__void__&);
0137       // Fake default constructor for usage restricted to direct object
0138       // persistency for clients requiring preallocation of memory for
0139       // persistifiable objects.
0140 
0141     G4TessellatedSolid (const G4TessellatedSolid& ts);
0142     G4TessellatedSolid &operator= (const G4TessellatedSolid& right);
0143     G4TessellatedSolid &operator+= (const G4TessellatedSolid& right);
0144 
0145     G4bool AddFacet (G4VFacet* aFacet);
0146     inline G4VFacet* GetFacet (G4int i) const;
0147 
0148     G4int GetNumberOfFacets () const;
0149     G4int GetFacetIndex (const G4ThreeVector& p) const;
0150 
0151     EInside Inside (const G4ThreeVector& p) const override;
0152     G4ThreeVector SurfaceNormal(const G4ThreeVector& p) const override;
0153     G4double DistanceToIn(const G4ThreeVector& p,
0154                                   const G4ThreeVector& v)const override;
0155     G4double DistanceToIn(const G4ThreeVector& p) const override;
0156     G4double DistanceToOut(const G4ThreeVector& p) const override;
0157     G4double DistanceToOut(const G4ThreeVector& p,
0158                                    const G4ThreeVector& v,
0159                                    const G4bool calcNorm,
0160                                          G4bool* validNorm,
0161                                          G4ThreeVector* norm) const override;
0162 
0163     virtual G4bool Normal (const G4ThreeVector& p, G4ThreeVector& n) const;
0164     virtual G4double SafetyFromOutside(const G4ThreeVector& p,
0165                                              G4bool aAccurate = false) const;
0166     virtual G4double SafetyFromInside (const G4ThreeVector& p,
0167                                              G4bool aAccurate = false) const;
0168 
0169     G4GeometryType GetEntityType () const override;
0170     G4bool IsFaceted () const override;
0171     std::ostream& StreamInfo(std::ostream& os) const override;
0172 
0173     G4VSolid* Clone() const override;
0174 
0175     G4ThreeVector GetPointOnSurface() const override;
0176     G4double GetSurfaceArea() override;
0177     G4double GetCubicVolume() override;
0178 
0179     void SetSolidClosed (const G4bool t);
0180     G4bool GetSolidClosed () const;
0181     G4int CheckStructure() const;
0182 
0183     inline void SetMaxVoxels(G4int max);
0184 
0185     inline G4Voxelizer& GetVoxels();
0186 
0187     G4bool CalculateExtent(const EAxis pAxis,
0188                            const G4VoxelLimits& pVoxelLimit,
0189                            const G4AffineTransform& pTransform,
0190                                  G4double& pMin, G4double& pMax) const override;
0191 
0192     void BoundingLimits(G4ThreeVector& pMin, G4ThreeVector& pMax) const override;
0193 
0194     G4double      GetMinXExtent () const;
0195     G4double      GetMaxXExtent () const;
0196     G4double      GetMinYExtent () const;
0197     G4double      GetMaxYExtent () const;
0198     G4double      GetMinZExtent () const;
0199     G4double      GetMaxZExtent () const;
0200 
0201     G4Polyhedron* CreatePolyhedron () const override;
0202     G4Polyhedron* GetPolyhedron    () const override;
0203     void DescribeYourselfTo (G4VGraphicsScene& scene) const override;
0204     G4VisExtent   GetExtent () const override;
0205 
0206     G4int AllocatedMemoryWithoutVoxels();
0207     G4int AllocatedMemory();
0208     void DisplayAllocatedMemory();
0209 
0210   private:
0211 
0212     void Initialize();
0213 
0214     G4double DistanceToOutNoVoxels(const G4ThreeVector& p,
0215                                    const G4ThreeVector& v,
0216                                          G4ThreeVector& aNormalVector,
0217                                          G4bool&        aConvex,
0218                                          G4double aPstep = kInfinity) const;
0219     G4double DistanceToInCandidates(const std::vector<G4int>& candidates,
0220                                     const G4ThreeVector& aPoint,
0221                                     const G4ThreeVector& aDirection) const;
0222     void DistanceToOutCandidates(const std::vector<G4int>& candidates,
0223                                  const G4ThreeVector& aPoint,
0224                                  const G4ThreeVector& direction,
0225                                        G4double& minDist,
0226                                        G4ThreeVector& minNormal,
0227                                        G4int& minCandidate) const;
0228     G4double DistanceToInNoVoxels(const G4ThreeVector& p,
0229                                   const G4ThreeVector& v,
0230                                         G4double aPstep = kInfinity) const;
0231     void SetExtremeFacets();
0232 
0233     EInside InsideNoVoxels (const G4ThreeVector& p) const;
0234     EInside InsideVoxels(const G4ThreeVector& aPoint) const;
0235 
0236     void Voxelize();
0237 
0238     void CreateVertexList();
0239 
0240     void PrecalculateInsides();
0241 
0242     void SetRandomVectors();
0243 
0244     G4double DistanceToInCore(const G4ThreeVector &p, const G4ThreeVector& v,
0245                                     G4double aPstep = kInfinity) const;
0246     G4double DistanceToOutCore(const G4ThreeVector& p, const G4ThreeVector& v,
0247                                      G4ThreeVector& aNormalVector,
0248                                      G4bool& aConvex,
0249                                      G4double aPstep = kInfinity) const;
0250 
0251     G4int SetAllUsingStack(const std::vector<G4int>& voxel,
0252                            const std::vector<G4int>& max,
0253                                  G4bool status, G4SurfBits& checked);
0254 
0255     void DeleteObjects ();
0256     void CopyObjects (const G4TessellatedSolid& s);
0257 
0258     static G4bool CompareSortedVoxel(const std::pair<G4int, G4double>& l,
0259                                      const std::pair<G4int, G4double>& r);
0260 
0261     G4double MinDistanceFacet(const G4ThreeVector& p, G4bool simple,
0262                                     G4VFacet* &facet) const;
0263 
0264     inline G4bool OutsideOfExtent(const G4ThreeVector& p,
0265                                         G4double tolerance = 0.0) const;
0266 
0267   protected:
0268 
0269     G4double kCarToleranceHalf;
0270 
0271   private:
0272 
0273     mutable G4bool fRebuildPolyhedron = false;
0274     mutable G4Polyhedron* fpPolyhedron = nullptr;
0275 
0276     std::vector<G4VFacet*> fFacets;
0277     std::set<G4VFacet*> fExtremeFacets; // Does all other facets lie on
0278                                         // or behind this surface?
0279 
0280     G4GeometryType fGeometryType;
0281     G4double       fCubicVolume = 0.0;
0282     G4double       fSurfaceArea = 0.0;
0283 
0284     std::vector<G4ThreeVector> fVertexList;
0285 
0286     std::set<G4VertexInfo,G4VertexComparator> fFacetList;
0287 
0288     G4ThreeVector fMinExtent, fMaxExtent;
0289 
0290     G4bool fSolidClosed = false;
0291 
0292     std::vector<G4ThreeVector> fRandir;
0293 
0294     G4int fMaxTries;
0295 
0296     G4Voxelizer fVoxels;  // Pointer to the voxelized solid
0297 
0298     G4SurfBits fInsides;
0299 };
0300 
0301 ///////////////////////////////////////////////////////////////////////////////
0302 // Inlined Methods
0303 ///////////////////////////////////////////////////////////////////////////////
0304 
0305 inline G4VFacet *G4TessellatedSolid::GetFacet (G4int i) const
0306 {
0307   return fFacets[i];
0308 }
0309 
0310 inline void G4TessellatedSolid::SetMaxVoxels(G4int max)
0311 {
0312   fVoxels.SetMaxVoxels(max);
0313 }
0314 
0315 inline G4Voxelizer &G4TessellatedSolid::GetVoxels()
0316 {
0317   return fVoxels;
0318 }
0319 
0320 inline G4bool G4TessellatedSolid::OutsideOfExtent(const G4ThreeVector& p,
0321                                                   G4double tolerance) const
0322 {
0323   return ( p.x() < fMinExtent.x() - tolerance
0324         || p.x() > fMaxExtent.x() + tolerance
0325         || p.y() < fMinExtent.y() - tolerance
0326         || p.y() > fMaxExtent.y() + tolerance
0327         || p.z() < fMinExtent.z() - tolerance
0328         || p.z() > fMaxExtent.z() + tolerance);
0329 }
0330 
0331 #endif
0332 
0333 #endif