Geant4  10.00.p01
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 66356 2012-12-18 09:02:32Z 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  G4double GetMinXExtent () const;
191  G4double GetMaxXExtent () const;
192  G4double GetMinYExtent () const;
193  G4double GetMaxYExtent () const;
194  G4double GetMinZExtent () const;
195  G4double GetMaxZExtent () const;
196 
198  // Create the List of transformed vertices in the format required
199  // for G4VSolid:: ClipCrossSection and ClipBetweenSections.
200 
201  virtual G4Polyhedron* CreatePolyhedron () const;
202  virtual G4Polyhedron* GetPolyhedron () const;
203  virtual void DescribeYourselfTo (G4VGraphicsScene& scene) const;
204  virtual G4VisExtent GetExtent () const;
205 
208  void DisplayAllocatedMemory();
209 
210  private: // without description
211 
212  void Initialize();
213 
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;
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 
245  G4double aPstep = kInfinity) const;
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 
262  G4VFacet * &facet) const;
263 
264  inline G4bool OutsideOfExtent(const G4ThreeVector &p,
265  G4double tolerance=0) const;
266 
267  private:
268 
270 
271  std::vector<G4VFacet *> fFacets;
272  std::set<G4VFacet *> fExtremeFacets; // Does all other facets lie on
273  // or behind this surface?
274 
278 
279  std::vector<G4ThreeVector> fVertexList;
280 
281  std::set<G4VertexInfo,G4VertexComparator> fFacetList;
282 
284 
286 
287  std::vector<G4ThreeVector> fRandir;
288 
290 
292 
293  G4SurfaceVoxelizer fVoxels; // Pointer to the voxelized solid
294 
296 };
297 
299 // Inlined Methods
301 
303 {
304  return fFacets[i];
305 }
306 
308 {
309  fVoxels.SetMaxVoxels(max);
310 }
311 
313 {
314  return fVoxels;
315 }
316 
318  G4double tolerance) const
319 {
320  return ( p.x() < fMinExtent.x() - tolerance
321  || p.x() > fMaxExtent.x() + tolerance
322  || p.y() < fMinExtent.y() - tolerance
323  || p.y() > fMaxExtent.y() + tolerance
324  || p.z() < fMinExtent.z() - tolerance
325  || p.z() > fMaxExtent.z() + tolerance);
326 }
327 
328 #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
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
G4String name
Definition: TRTMaterials.hh:40
G4double GetMaxXExtent() const
virtual G4double GetCubicVolume()
G4double GetMinXExtent() const
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
G4ThreeVectorList * CreateRotatedVertices(const G4AffineTransform &pT) const
G4VFacet * GetFacet(G4int i) const
static const double s
Definition: G4SIunits.hh:150
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)
std::vector< G4ThreeVector > G4ThreeVectorList
Definition: G4VSolid.hh:79
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
G4double GetMinYExtent() const
G4TessellatedSolid & operator=(const G4TessellatedSolid &right)
G4bool GetSolidClosed() const