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.5 ] [ 10.5.p1 ] [ 10.6 ] [ 10.6.p1 ] [ 10.6.p2 ] [ 10.6.p3 ] [ 10.7 ] [ 10.7.p1 ] [ 10.7.p2 ] [ 10.7.p3 ] [ 10.7.p4 ] [ 11.0 ] [ 11.0.p1 ] [ 11.0.p2 ] [ 11.0.p3, ] [ 11.0.p4 ] [ 11.1 ] [ 11.1.1 ] [ 11.1.2 ] [ 11.1.3 ] [ 11.2 ] [ 11.2.1 ] [ 11.2.2 ] [ 11.3.0 ]

  1 //
  2 // ********************************************************************
  3 // * License and Disclaimer                                           *
  4 // *                                                                  *
  5 // * The  Geant4 software  is  copyright of the Copyright Holders  of *
  6 // * the Geant4 Collaboration.  It is provided  under  the terms  and *
  7 // * conditions of the Geant4 Software License,  included in the file *
  8 // * LICENSE and available at  http://cern.ch/geant4/license .  These *
  9 // * include a list of copyright holders.                             *
 10 // *                                                                  *
 11 // * Neither the authors of this software system, nor their employing *
 12 // * institutes,nor the agencies providing financial support for this *
 13 // * work  make  any representation or  warranty, express or implied, *
 14 // * regarding  this  software system or assume any liability for its *
 15 // * use.  Please see the license in the file  LICENSE  and URL above *
 16 // * for the full disclaimer and the limitation of liability.         *
 17 // *                                                                  *
 18 // * This  code  implementation is the result of  the  scientific and *
 19 // * technical work of the GEANT4 collaboration and of QinetiQ Ltd,   *
 20 // * subject to DEFCON 705 IPR conditions.                            *
 21 // * By using,  copying,  modifying or  distributing the software (or *
 22 // * any work based  on the software)  you  agree  to acknowledge its *
 23 // * use  in  resulting  scientific  publications,  and indicate your *
 24 // * acceptance of all terms of the Geant4 Software license.          *
 25 // ********************************************************************
 26 //
 27 // G4TessellatedSolid
 28 //
 29 // Class description:
 30 //
 31 //    G4TessellatedSolid is a special Geant4 solid defined by a number of
 32 //    facets (UVFacet). It is important that the supplied facets shall form a
 33 //    fully enclose space which is the solid.
 34 //    At the moment only two types of facet can be used for the construction of
 35 //    a G4TessellatedSolid, i.e. the G4TriangularFacet and G4QuadrangularFacet.
 36 //
 37 //    How to contruct a G4TessellatedSolid:
 38 //
 39 //    First declare a tessellated solid:
 40 //
 41 //      G4TessellatedSolid* solidTarget = new G4TessellatedSolid("Solid_name");
 42 //
 43 //    Define the facets which form the solid
 44 //
 45 //      G4double targetSiz = 10*cm ;
 46 //      G4TriangularFacet *facet1 = new
 47 //      G4TriangularFacet (G4ThreeVector(-targetSize,-targetSize,        0.0),
 48 //                         G4ThreeVector(+targetSize,-targetSize,        0.0),
 49 //                         G4ThreeVector(        0.0,        0.0,+targetSize),
 50 //                         ABSOLUTE);
 51 //      G4TriangularFacet *facet2 = new
 52 //      G4TriangularFacet (G4ThreeVector(+targetSize,-targetSize,        0.0),
 53 //                         G4ThreeVector(+targetSize,+targetSize,        0.0),
 54 //                         G4ThreeVector(        0.0,        0.0,+targetSize),
 55 //                         ABSOLUTE);
 56 //      G4TriangularFacet *facet3 = new
 57 //      G4TriangularFacet (G4ThreeVector(+targetSize,+targetSize,        0.0),
 58 //                         G4ThreeVector(-targetSize,+targetSize,        0.0),
 59 //                         G4ThreeVector(        0.0,        0.0,+targetSize),
 60 //                         ABSOLUTE);
 61 //      G4TriangularFacet *facet4 = new
 62 //      G4TriangularFacet (G4ThreeVector(-targetSize,+targetSize,        0.0),
 63 //                         G4ThreeVector(-targetSize,-targetSize,        0.0),
 64 //                         G4ThreeVector(        0.0,        0.0,+targetSize),
 65 //                         ABSOLUTE);
 66 //      G4QuadrangularFacet *facet5 = new
 67 //      G4QuadrangularFacet (G4ThreeVector(-targetSize,-targetSize,      0.0),
 68 //                           G4ThreeVector(-targetSize,+targetSize,      0.0),
 69 //                           G4ThreeVector(+targetSize,+targetSize,      0.0),
 70 //                           G4ThreeVector(+targetSize,-targetSize,      0.0),
 71 //                           ABSOLUTE);
 72 //
 73 //    Then add the facets to the solid:
 74 //
 75 //      solidTarget->AddFacet((UVFacet*) facet1);
 76 //      solidTarget->AddFacet((UVFacet*) facet2);
 77 //      solidTarget->AddFacet((UVFacet*) facet3);
 78 //      solidTarget->AddFacet((UVFacet*) facet4);
 79 //      solidTarget->AddFacet((UVFacet*) facet5);
 80 //
 81 //    Finally declare the solid is complete:
 82 //
 83 //      solidTarget->SetSolidClosed(true);
 84 
 85 // 31.10.2004, P R Truscott, QinetiQ Ltd, UK - Created.
 86 // 12.10.2012, M Gayer, CERN - New implementation with voxelization of surfaces.
 87 // --------------------------------------------------------------------
 88 #ifndef G4TESSELLATEDSOLID_HH
 89 #define G4TESSELLATEDSOLID_HH 1
 90 
 91 #include "G4GeomTypes.hh"
 92 
 93 #if defined(G4GEOM_USE_USOLIDS)
 94 #define G4GEOM_USE_UTESSELLATEDSOLID 1
 95 #endif
 96 
 97 #if defined(G4GEOM_USE_UTESSELLATEDSOLID)
 98   #define G4UTessellatedSolid G4TessellatedSolid
 99   #include "G4UTessellatedSolid.hh"
100 #else
101 
102 #include <iostream>
103 #include <vector>
104 #include <set>
105 #include <map>
106 
107 #include "G4Types.hh"
108 #include "G4VSolid.hh"
109 #include "G4Voxelizer.hh"
110 #include "G4VFacet.hh"
111 
112 struct G4VertexInfo
113 {
114   G4int id;
115   G4double mag2;
116 };
117 
118 class G4VertexComparator
119 {
120 public:
121   G4bool operator() (const G4VertexInfo& l, const G4VertexInfo& r) const
122   {
123     return l.mag2 == r.mag2 ? l.id < r.id : l.mag2 < r.mag2;
124   }
125 };
126 
127 class G4TessellatedSolid : public G4VSolid
128 {
129   public:
130 
131     G4TessellatedSolid ();
132     ~G4TessellatedSolid () override;
133 
134     G4TessellatedSolid (const G4String& name);
135 
136     G4TessellatedSolid(__void__&);
137       // Fake default constructor for usage restricted to direct object
138       // persistency for clients requiring preallocation of memory for
139       // persistifiable objects.
140 
141     G4TessellatedSolid (const G4TessellatedSolid& ts);
142     G4TessellatedSolid &operator= (const G4TessellatedSolid& right);
143     G4TessellatedSolid &operator+= (const G4TessellatedSolid& right);
144 
145     G4bool AddFacet (G4VFacet* aFacet);
146     inline G4VFacet* GetFacet (G4int i) const;
147 
148     G4int GetNumberOfFacets () const;
149     G4int GetFacetIndex (const G4ThreeVector& p) const;
150 
151     EInside Inside (const G4ThreeVector& p) const override;
152     G4ThreeVector SurfaceNormal(const G4ThreeVector& p) const override;
153     G4double DistanceToIn(const G4ThreeVector& p,
154                                   const G4ThreeVector& v)const override;
155     G4double DistanceToIn(const G4ThreeVector& p) const override;
156     G4double DistanceToOut(const G4ThreeVector& p) const override;
157     G4double DistanceToOut(const G4ThreeVector& p,
158                                    const G4ThreeVector& v,
159                                    const G4bool calcNorm,
160                                          G4bool* validNorm,
161                                          G4ThreeVector* norm) const override;
162 
163     virtual G4bool Normal (const G4ThreeVector& p, G4ThreeVector& n) const;
164     virtual G4double SafetyFromOutside(const G4ThreeVector& p,
165                                              G4bool aAccurate = false) const;
166     virtual G4double SafetyFromInside (const G4ThreeVector& p,
167                                              G4bool aAccurate = false) const;
168 
169     G4GeometryType GetEntityType () const override;
170     G4bool IsFaceted () const override;
171     std::ostream& StreamInfo(std::ostream& os) const override;
172 
173     G4VSolid* Clone() const override;
174 
175     G4ThreeVector GetPointOnSurface() const override;
176     G4double GetSurfaceArea() override;
177     G4double GetCubicVolume() override;
178 
179     void SetSolidClosed (const G4bool t);
180     G4bool GetSolidClosed () const;
181     G4int CheckStructure() const;
182 
183     inline void SetMaxVoxels(G4int max);
184 
185     inline G4Voxelizer& GetVoxels();
186 
187     G4bool CalculateExtent(const EAxis pAxis,
188                            const G4VoxelLimits& pVoxelLimit,
189                            const G4AffineTransform& pTransform,
190                                  G4double& pMin, G4double& pMax) const override;
191 
192     void BoundingLimits(G4ThreeVector& pMin, G4ThreeVector& pMax) const override;
193 
194     G4double      GetMinXExtent () const;
195     G4double      GetMaxXExtent () const;
196     G4double      GetMinYExtent () const;
197     G4double      GetMaxYExtent () const;
198     G4double      GetMinZExtent () const;
199     G4double      GetMaxZExtent () const;
200 
201     G4Polyhedron* CreatePolyhedron () const override;
202     G4Polyhedron* GetPolyhedron    () const override;
203     void DescribeYourselfTo (G4VGraphicsScene& scene) const override;
204     G4VisExtent   GetExtent () const override;
205 
206     G4int AllocatedMemoryWithoutVoxels();
207     G4int AllocatedMemory();
208     void DisplayAllocatedMemory();
209 
210   private:
211 
212     void Initialize();
213 
214     G4double DistanceToOutNoVoxels(const G4ThreeVector& p,
215                                    const G4ThreeVector& v,
216                                          G4ThreeVector& aNormalVector,
217                                          G4bool&        aConvex,
218                                          G4double aPstep = kInfinity) const;
219     G4double DistanceToInCandidates(const std::vector<G4int>& candidates,
220                                     const G4ThreeVector& aPoint,
221                                     const G4ThreeVector& aDirection) const;
222     void DistanceToOutCandidates(const std::vector<G4int>& candidates,
223                                  const G4ThreeVector& aPoint,
224                                  const G4ThreeVector& direction,
225                                        G4double& minDist,
226                                        G4ThreeVector& minNormal,
227                                        G4int& minCandidate) const;
228     G4double DistanceToInNoVoxels(const G4ThreeVector& p,
229                                   const G4ThreeVector& v,
230                                         G4double aPstep = kInfinity) const;
231     void SetExtremeFacets();
232 
233     EInside InsideNoVoxels (const G4ThreeVector& p) const;
234     EInside InsideVoxels(const G4ThreeVector& aPoint) const;
235 
236     void Voxelize();
237 
238     void CreateVertexList();
239 
240     void PrecalculateInsides();
241 
242     void SetRandomVectors();
243 
244     G4double DistanceToInCore(const G4ThreeVector &p, const G4ThreeVector& v,
245                                     G4double aPstep = kInfinity) const;
246     G4double DistanceToOutCore(const G4ThreeVector& p, const G4ThreeVector& v,
247                                      G4ThreeVector& aNormalVector,
248                                      G4bool& aConvex,
249                                      G4double aPstep = kInfinity) const;
250 
251     G4int SetAllUsingStack(const std::vector<G4int>& voxel,
252                            const std::vector<G4int>& max,
253                                  G4bool status, G4SurfBits& checked);
254 
255     void DeleteObjects ();
256     void CopyObjects (const G4TessellatedSolid& s);
257 
258     static G4bool CompareSortedVoxel(const std::pair<G4int, G4double>& l,
259                                      const std::pair<G4int, G4double>& r);
260 
261     G4double MinDistanceFacet(const G4ThreeVector& p, G4bool simple,
262                                     G4VFacet* &facet) const;
263 
264     inline G4bool OutsideOfExtent(const G4ThreeVector& p,
265                                         G4double tolerance = 0.0) const;
266 
267   protected:
268 
269     G4double kCarToleranceHalf;
270 
271   private:
272 
273     mutable G4bool fRebuildPolyhedron = false;
274     mutable G4Polyhedron* fpPolyhedron = nullptr;
275 
276     std::vector<G4VFacet*> fFacets;
277     std::set<G4VFacet*> fExtremeFacets; // Does all other facets lie on
278                                         // or behind this surface?
279 
280     G4GeometryType fGeometryType;
281     G4double       fCubicVolume = 0.0;
282     G4double       fSurfaceArea = 0.0;
283 
284     std::vector<G4ThreeVector> fVertexList;
285 
286     std::set<G4VertexInfo,G4VertexComparator> fFacetList;
287 
288     G4ThreeVector fMinExtent, fMaxExtent;
289 
290     G4bool fSolidClosed = false;
291 
292     std::vector<G4ThreeVector> fRandir;
293 
294     G4int fMaxTries;
295 
296     G4Voxelizer fVoxels;  // Pointer to the voxelized solid
297 
298     G4SurfBits fInsides;
299 };
300 
301 ///////////////////////////////////////////////////////////////////////////////
302 // Inlined Methods
303 ///////////////////////////////////////////////////////////////////////////////
304 
305 inline G4VFacet *G4TessellatedSolid::GetFacet (G4int i) const
306 {
307   return fFacets[i];
308 }
309 
310 inline void G4TessellatedSolid::SetMaxVoxels(G4int max)
311 {
312   fVoxels.SetMaxVoxels(max);
313 }
314 
315 inline G4Voxelizer &G4TessellatedSolid::GetVoxels()
316 {
317   return fVoxels;
318 }
319 
320 inline G4bool G4TessellatedSolid::OutsideOfExtent(const G4ThreeVector& p,
321                                                   G4double tolerance) const
322 {
323   return ( p.x() < fMinExtent.x() - tolerance
324         || p.x() > fMaxExtent.x() + tolerance
325         || p.y() < fMinExtent.y() - tolerance
326         || p.y() > fMaxExtent.y() + tolerance
327         || p.z() < fMinExtent.z() - tolerance
328         || p.z() > fMaxExtent.z() + tolerance);
329 }
330 
331 #endif
332 
333 #endif
334