Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:59:09

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     std::ostream& StreamInfo(std::ostream& os) const override;
0171 
0172     G4VSolid* Clone() const override;
0173 
0174     G4ThreeVector GetPointOnSurface() const override;
0175     G4double GetSurfaceArea() override;
0176     G4double GetCubicVolume() override;
0177 
0178     void SetSolidClosed (const G4bool t);
0179     G4bool GetSolidClosed () const;
0180     G4int CheckStructure() const;
0181 
0182     inline void SetMaxVoxels(G4int max);
0183 
0184     inline G4Voxelizer& GetVoxels();
0185 
0186     G4bool CalculateExtent(const EAxis pAxis,
0187                            const G4VoxelLimits& pVoxelLimit,
0188                            const G4AffineTransform& pTransform,
0189                                  G4double& pMin, G4double& pMax) const override;
0190 
0191     void BoundingLimits(G4ThreeVector& pMin, G4ThreeVector& pMax) const override;
0192 
0193     G4double      GetMinXExtent () const;
0194     G4double      GetMaxXExtent () const;
0195     G4double      GetMinYExtent () const;
0196     G4double      GetMaxYExtent () const;
0197     G4double      GetMinZExtent () const;
0198     G4double      GetMaxZExtent () const;
0199 
0200     G4Polyhedron* CreatePolyhedron () const override;
0201     G4Polyhedron* GetPolyhedron    () const override;
0202     void DescribeYourselfTo (G4VGraphicsScene& scene) const override;
0203     G4VisExtent   GetExtent () const override;
0204 
0205     G4int AllocatedMemoryWithoutVoxels();
0206     G4int AllocatedMemory();
0207     void DisplayAllocatedMemory();
0208 
0209   private:
0210 
0211     void Initialize();
0212 
0213     G4double DistanceToOutNoVoxels(const G4ThreeVector& p,
0214                                    const G4ThreeVector& v,
0215                                          G4ThreeVector& aNormalVector,
0216                                          G4bool&        aConvex,
0217                                          G4double aPstep = kInfinity) const;
0218     G4double DistanceToInCandidates(const std::vector<G4int>& candidates,
0219                                     const G4ThreeVector& aPoint,
0220                                     const G4ThreeVector& aDirection) const;
0221     void DistanceToOutCandidates(const std::vector<G4int>& candidates,
0222                                  const G4ThreeVector& aPoint,
0223                                  const G4ThreeVector& direction,
0224                                        G4double& minDist,
0225                                        G4ThreeVector& minNormal,
0226                                        G4int& minCandidate) const;
0227     G4double DistanceToInNoVoxels(const G4ThreeVector& p,
0228                                   const G4ThreeVector& v,
0229                                         G4double aPstep = kInfinity) const;
0230     void SetExtremeFacets();
0231 
0232     EInside InsideNoVoxels (const G4ThreeVector& p) const;
0233     EInside InsideVoxels(const G4ThreeVector& aPoint) const;
0234 
0235     void Voxelize();
0236 
0237     void CreateVertexList();
0238 
0239     void PrecalculateInsides();
0240 
0241     void SetRandomVectors();
0242 
0243     G4double DistanceToInCore(const G4ThreeVector &p, const G4ThreeVector& v,
0244                                     G4double aPstep = kInfinity) const;
0245     G4double DistanceToOutCore(const G4ThreeVector& p, const G4ThreeVector& v,
0246                                      G4ThreeVector& aNormalVector,
0247                                      G4bool& aConvex,
0248                                      G4double aPstep = kInfinity) const;
0249 
0250     G4int SetAllUsingStack(const std::vector<G4int>& voxel,
0251                            const std::vector<G4int>& max,
0252                                  G4bool status, G4SurfBits& checked);
0253 
0254     void DeleteObjects ();
0255     void CopyObjects (const G4TessellatedSolid& s);
0256 
0257     static G4bool CompareSortedVoxel(const std::pair<G4int, G4double>& l,
0258                                      const std::pair<G4int, G4double>& r);
0259 
0260     G4double MinDistanceFacet(const G4ThreeVector& p, G4bool simple,
0261                                     G4VFacet* &facet) const;
0262 
0263     inline G4bool OutsideOfExtent(const G4ThreeVector& p,
0264                                         G4double tolerance = 0.0) const;
0265 
0266   protected:
0267 
0268     G4double kCarToleranceHalf;
0269 
0270   private:
0271 
0272     mutable G4bool fRebuildPolyhedron = false;
0273     mutable G4Polyhedron* fpPolyhedron = nullptr;
0274 
0275     std::vector<G4VFacet*> fFacets;
0276     std::set<G4VFacet*> fExtremeFacets; // Does all other facets lie on
0277                                         // or behind this surface?
0278 
0279     G4GeometryType fGeometryType;
0280     G4double       fCubicVolume = 0.0;
0281     G4double       fSurfaceArea = 0.0;
0282 
0283     std::vector<G4ThreeVector> fVertexList;
0284 
0285     std::set<G4VertexInfo,G4VertexComparator> fFacetList;
0286 
0287     G4ThreeVector fMinExtent, fMaxExtent;
0288 
0289     G4bool fSolidClosed = false;
0290 
0291     std::vector<G4ThreeVector> fRandir;
0292 
0293     G4int fMaxTries;
0294 
0295     G4Voxelizer fVoxels;  // Pointer to the voxelized solid
0296 
0297     G4SurfBits fInsides;
0298 };
0299 
0300 ///////////////////////////////////////////////////////////////////////////////
0301 // Inlined Methods
0302 ///////////////////////////////////////////////////////////////////////////////
0303 
0304 inline G4VFacet *G4TessellatedSolid::GetFacet (G4int i) const
0305 {
0306   return fFacets[i];
0307 }
0308 
0309 inline void G4TessellatedSolid::SetMaxVoxels(G4int max)
0310 {
0311   fVoxels.SetMaxVoxels(max);
0312 }
0313 
0314 inline G4Voxelizer &G4TessellatedSolid::GetVoxels()
0315 {
0316   return fVoxels;
0317 }
0318 
0319 inline G4bool G4TessellatedSolid::OutsideOfExtent(const G4ThreeVector& p,
0320                                                   G4double tolerance) const
0321 {
0322   return ( p.x() < fMinExtent.x() - tolerance
0323         || p.x() > fMaxExtent.x() + tolerance
0324         || p.y() < fMinExtent.y() - tolerance
0325         || p.y() > fMaxExtent.y() + tolerance
0326         || p.z() < fMinExtent.z() - tolerance
0327         || p.z() > fMaxExtent.z() + tolerance);
0328 }
0329 
0330 #endif
0331 
0332 #endif