Geant4  10.03
G4TessellatedSolid.hh
Go to the documentation of this file.
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 // $Id: G4TessellatedSolid.hh 100819 2016-11-02 15:17:36Z gcosmo $
28 //
29 // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
30 //
31 // Class G4TessellatedSolid
32 //
33 // Class description:
34 //
35 // G4TessellatedSolid is a special Geant4 solid defined by a number of
36 // facets (UVFacet). It is important that the supplied facets shall form a
37 // fully enclose space which is the solid.
38 // At the moment only two types of facet can be used for the construction of
39 // a G4TessellatedSolid, i.e. the G4TriangularFacet and G4QuadrangularFacet.
40 //
41 // How to contruct a G4TessellatedSolid:
42 //
43 // First declare a tessellated solid:
44 //
45 // G4TessellatedSolid* solidTarget = new G4TessellatedSolid("Solid_name");
46 //
47 // Define the facets which form the solid
48 //
49 // G4double targetSiz = 10*cm ;
50 // G4TriangularFacet *facet1 = new
51 // G4TriangularFacet (G4ThreeVector(-targetSize,-targetSize, 0.0),
52 // G4ThreeVector(+targetSize,-targetSize, 0.0),
53 // G4ThreeVector( 0.0, 0.0,+targetSize),
54 // ABSOLUTE);
55 // G4TriangularFacet *facet2 = new
56 // G4TriangularFacet (G4ThreeVector(+targetSize,-targetSize, 0.0),
57 // G4ThreeVector(+targetSize,+targetSize, 0.0),
58 // G4ThreeVector( 0.0, 0.0,+targetSize),
59 // ABSOLUTE);
60 // G4TriangularFacet *facet3 = new
61 // G4TriangularFacet (G4ThreeVector(+targetSize,+targetSize, 0.0),
62 // G4ThreeVector(-targetSize,+targetSize, 0.0),
63 // G4ThreeVector( 0.0, 0.0,+targetSize),
64 // ABSOLUTE);
65 // G4TriangularFacet *facet4 = new
66 // G4TriangularFacet (G4ThreeVector(-targetSize,+targetSize, 0.0),
67 // G4ThreeVector(-targetSize,-targetSize, 0.0),
68 // G4ThreeVector( 0.0, 0.0,+targetSize),
69 // ABSOLUTE);
70 // G4QuadrangularFacet *facet5 = new
71 // G4QuadrangularFacet (G4ThreeVector(-targetSize,-targetSize, 0.0),
72 // G4ThreeVector(-targetSize,+targetSize, 0.0),
73 // G4ThreeVector(+targetSize,+targetSize, 0.0),
74 // G4ThreeVector(+targetSize,-targetSize, 0.0),
75 // ABSOLUTE);
76 //
77 // Then add the facets to the solid:
78 //
79 // solidTarget->AddFacet((UVFacet*) facet1);
80 // solidTarget->AddFacet((UVFacet*) facet2);
81 // solidTarget->AddFacet((UVFacet*) facet3);
82 // solidTarget->AddFacet((UVFacet*) facet4);
83 // solidTarget->AddFacet((UVFacet*) facet5);
84 //
85 // Finally declare the solid is complete:
86 //
87 // solidTarget->SetSolidClosed(true);
88 
89 // CHANGE HISTORY
90 // --------------
91 // 31 October 2004, P R Truscott, QinetiQ Ltd, UK
92 // - Created.
93 // 22 November 2005, F Lei,
94 // - Added GetPolyhedron().
95 // 12 October 2012, M Gayer,
96 // - Reviewed optimized implementation including voxelization of surfaces.
97 //
99 #ifndef G4TessellatedSolid_hh
100 #define G4TessellatedSolid_hh 1
101 
102 #include <iostream>
103 #include <vector>
104 #include <set>
105 #include <map>
106 
107 #include "G4VSolid.hh"
108 #include "G4Types.hh"
109 #include "G4SurfaceVoxelizer.hh"
110 
112 {
115 };
116 
117 class G4VFacet;
118 
120 {
121 public:
122  G4bool operator() (const G4VertexInfo &l, const G4VertexInfo &r) const
123  {
124  return l.mag2 == r.mag2 ? l.id < r.id : l.mag2 < r.mag2;
125  }
126 };
127 
129 {
130  public: // with description
131 
133  virtual ~G4TessellatedSolid ();
134 
136 
137  G4TessellatedSolid(__void__&);
138  // Fake default constructor for usage restricted to direct object
139  // persistency for clients requiring preallocation of memory for
140  // persistifiable objects.
141 
145 
146  G4bool AddFacet (G4VFacet *aFacet);
147  inline G4VFacet *GetFacet (G4int i) const;
148 
149  G4int GetNumberOfFacets () const;
150 
151  virtual EInside Inside (const G4ThreeVector &p) const;
152  virtual G4ThreeVector SurfaceNormal(const G4ThreeVector& p) const;
153  virtual G4double DistanceToIn(const G4ThreeVector& p,
154  const G4ThreeVector& v)const;
155  virtual G4double DistanceToIn(const G4ThreeVector& p) const;
156  virtual G4double DistanceToOut(const G4ThreeVector& p) const;
157  virtual G4double DistanceToOut(const G4ThreeVector& p,
158  const G4ThreeVector& v,
159  const G4bool calcNorm,
160  G4bool *validNorm,
161  G4ThreeVector *norm) const;
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  virtual G4GeometryType GetEntityType () const;
170  virtual std::ostream &StreamInfo(std::ostream &os) const;
171 
172  virtual G4VSolid* Clone() const;
173 
174  virtual G4ThreeVector GetPointOnSurface() const;
175  virtual G4double GetSurfaceArea();
176  virtual G4double GetCubicVolume ();
177 
178  void SetSolidClosed (const G4bool t);
179  G4bool GetSolidClosed () const;
180 
181  inline void SetMaxVoxels(G4int max);
182 
183  inline G4SurfaceVoxelizer &GetVoxels();
184 
185  virtual G4bool CalculateExtent(const EAxis pAxis,
186  const G4VoxelLimits& pVoxelLimit,
187  const G4AffineTransform& pTransform,
188  G4double& pMin, G4double& pMax) const;
189 
190  void Extent(G4ThreeVector& pMin, G4ThreeVector& pMax) const;
191 
192  G4double GetMinXExtent () const;
193  G4double GetMaxXExtent () const;
194  G4double GetMinYExtent () const;
195  G4double GetMaxYExtent () const;
196  G4double GetMinZExtent () const;
197  G4double GetMaxZExtent () const;
198 
199  virtual G4Polyhedron* CreatePolyhedron () const;
200  virtual G4Polyhedron* GetPolyhedron () const;
201  virtual void DescribeYourselfTo (G4VGraphicsScene& scene) const;
202  virtual G4VisExtent GetExtent () const;
203 
206  void DisplayAllocatedMemory();
207 
208  private: // without description
209 
210  void Initialize();
211 
213  const G4ThreeVector &v,
214  G4ThreeVector &aNormalVector,
215  G4bool &aConvex,
216  G4double aPstep = kInfinity) const;
217  G4double DistanceToInCandidates(const std::vector<G4int> &candidates,
218  const G4ThreeVector &aPoint,
219  const G4ThreeVector &aDirection) const;
220  void DistanceToOutCandidates(const std::vector<G4int> &candidates,
221  const G4ThreeVector &aPoint,
222  const G4ThreeVector &direction,
223  G4double &minDist,
224  G4ThreeVector &minNormal,
225  G4int &minCandidate) const;
227  const G4ThreeVector &v,
228  G4double aPstep = kInfinity) const;
229  void SetExtremeFacets();
230 
231  EInside InsideNoVoxels (const G4ThreeVector &p) const;
232  EInside InsideVoxels(const G4ThreeVector &aPoint) const;
233 
234  void Voxelize();
235 
236  void CreateVertexList();
237 
238  void PrecalculateInsides();
239 
240  void SetRandomVectors();
241 
243  G4double aPstep = kInfinity) const;
245  G4ThreeVector &aNormalVector,
246  G4bool &aConvex,
247  G4double aPstep = kInfinity) const;
248 
249  G4int SetAllUsingStack(const std::vector<G4int> &voxel,
250  const std::vector<G4int> &max,
251  G4bool status, G4SurfBits &checked);
252 
253  void DeleteObjects ();
254  void CopyObjects (const G4TessellatedSolid &s);
255 
256  static G4bool CompareSortedVoxel(const std::pair<G4int, G4double> &l,
257  const std::pair<G4int, G4double> &r);
258 
260  G4VFacet * &facet) const;
261 
262  inline G4bool OutsideOfExtent(const G4ThreeVector &p,
263  G4double tolerance=0) const;
264 
265  protected:
266 
268 
269  private:
270 
273 
274  std::vector<G4VFacet *> fFacets;
275  std::set<G4VFacet *> fExtremeFacets; // Does all other facets lie on
276  // or behind this surface?
277 
281 
282  std::vector<G4ThreeVector> fVertexList;
283 
284  std::set<G4VertexInfo,G4VertexComparator> fFacetList;
285 
287 
289 
290  std::vector<G4ThreeVector> fRandir;
291 
293 
294  G4SurfaceVoxelizer fVoxels; // Pointer to the voxelized solid
295 
297 };
298 
300 // Inlined Methods
302 
304 {
305  return fFacets[i];
306 }
307 
309 {
310  fVoxels.SetMaxVoxels(max);
311 }
312 
314 {
315  return fVoxels;
316 }
317 
319  G4double tolerance) const
320 {
321  return ( p.x() < fMinExtent.x() - tolerance
322  || p.x() > fMaxExtent.x() + tolerance
323  || p.y() < fMinExtent.y() - tolerance
324  || p.y() > fMaxExtent.y() + tolerance
325  || p.z() < fMinExtent.z() - tolerance
326  || p.z() > fMaxExtent.z() + tolerance);
327 }
328 
329 #endif
void SetMaxVoxels(G4int max)
void SetSolidClosed(const G4bool t)
EInside InsideVoxels(const G4ThreeVector &aPoint) const
G4double DistanceToInCore(const G4ThreeVector &p, const G4ThreeVector &v, G4double aPstep=kInfinity) const
static const G4double kInfinity
Definition: geomdefs.hh:42
G4double DistanceToInNoVoxels(const G4ThreeVector &p, const G4ThreeVector &v, G4double aPstep=kInfinity) const
static constexpr double s
Definition: G4SIunits.hh:169
CLHEP::Hep3Vector G4ThreeVector
void DistanceToOutCandidates(const std::vector< G4int > &candidates, const G4ThreeVector &aPoint, const G4ThreeVector &direction, G4double &minDist, G4ThreeVector &minNormal, G4int &minCandidate) const
G4SurfaceVoxelizer & GetVoxels()
virtual void DescribeYourselfTo(G4VGraphicsScene &scene) const
void CopyObjects(const G4TessellatedSolid &s)
std::vector< G4ThreeVector > fVertexList
virtual G4VisExtent GetExtent() const
G4double GetMaxXExtent() const
virtual G4double GetCubicVolume()
G4double GetMinXExtent() const
const char * name(G4int ptype)
std::vector< G4ThreeVector > fRandir
virtual G4double GetSurfaceArea()
G4double GetMaxZExtent() const
static G4bool CompareSortedVoxel(const std::pair< G4int, G4double > &l, const std::pair< G4int, G4double > &r)
int G4int
Definition: G4Types.hh:78
G4VFacet * GetFacet(G4int i) const
G4double DistanceToInCandidates(const std::vector< G4int > &candidates, const G4ThreeVector &aPoint, const G4ThreeVector &aDirection) const
G4double GetMinZExtent() const
virtual G4double DistanceToOut(const G4ThreeVector &p) const
virtual G4Polyhedron * CreatePolyhedron() const
G4int SetAllUsingStack(const std::vector< G4int > &voxel, const std::vector< G4int > &max, G4bool status, G4SurfBits &checked)
virtual EInside Inside(const G4ThreeVector &p) const
bool G4bool
Definition: G4Types.hh:79
G4bool OutsideOfExtent(const G4ThreeVector &p, G4double tolerance=0) const
G4bool AddFacet(G4VFacet *aFacet)
void SetMaxVoxels(G4int max)
EInside InsideNoVoxels(const G4ThreeVector &p) const
G4TessellatedSolid & operator+=(const G4TessellatedSolid &right)
G4SurfaceVoxelizer fVoxels
const G4int n
virtual G4double DistanceToIn(const G4ThreeVector &p, const G4ThreeVector &v) const
virtual G4VSolid * Clone() const
std::set< G4VertexInfo, G4VertexComparator > fFacetList
virtual G4Polyhedron * GetPolyhedron() const
G4bool operator()(const G4VertexInfo &l, const G4VertexInfo &r) const
G4int GetNumberOfFacets() const
virtual std::ostream & StreamInfo(std::ostream &os) const
T max(const T t1, const T t2)
brief Return the largest of the two arguments
EInside
Definition: geomdefs.hh:58
G4double MinDistanceFacet(const G4ThreeVector &p, G4bool simple, G4VFacet *&facet) const
EAxis
Definition: geomdefs.hh:54
G4Polyhedron * fpPolyhedron
G4double DistanceToOutNoVoxels(const G4ThreeVector &p, const G4ThreeVector &v, G4ThreeVector &aNormalVector, G4bool &aConvex, G4double aPstep=kInfinity) const
virtual G4double SafetyFromInside(const G4ThreeVector &p, G4bool aAccurate=false) const
G4GeometryType fGeometryType
std::set< G4VFacet * > fExtremeFacets
std::vector< G4VFacet * > fFacets
virtual G4GeometryType GetEntityType() const
double G4double
Definition: G4Types.hh:76
virtual G4bool Normal(const G4ThreeVector &p, G4ThreeVector &n) const
virtual G4bool CalculateExtent(const EAxis pAxis, const G4VoxelLimits &pVoxelLimit, const G4AffineTransform &pTransform, G4double &pMin, G4double &pMax) const
G4double GetMaxYExtent() const
virtual G4ThreeVector GetPointOnSurface() const
virtual G4ThreeVector SurfaceNormal(const G4ThreeVector &p) const
G4double DistanceToOutCore(const G4ThreeVector &p, const G4ThreeVector &v, G4ThreeVector &aNormalVector, G4bool &aConvex, G4double aPstep=kInfinity) const
virtual G4double SafetyFromOutside(const G4ThreeVector &p, G4bool aAccurate=false) const
void Extent(G4ThreeVector &pMin, G4ThreeVector &pMax) const
G4double GetMinYExtent() const
G4TessellatedSolid & operator=(const G4TessellatedSolid &right)
G4bool GetSolidClosed() const