Geant4  10.01.p02
UTessellatedSolid.hh
Go to the documentation of this file.
1 //
2 // ********************************************************************
3 // * This Software is part of the AIDA Unified Solids Library package *
4 // * See: https://aidasoft.web.cern.ch/USolids *
5 // ********************************************************************
6 //
7 // $Id:$
8 //
9 // --------------------------------------------------------------------
10 //
11 // UTessellatedSolid
12 //
13 // Class description:
14 //
15 // UTessellatedSolid is a special Geant4 solid defined by a number of
16 // facets (UVFacet). It is important that the supplied facets shall form a
17 // fully enclose space which is the solid.
18 // Only two types of facet can be used for the construction of
19 // a UTessellatedSolid, i.e. the UTriangularFacet and UQuadrangularFacet.
20 //
21 // How to contruct a UTessellatedSolid:
22 //
23 // First declare a tessellated solid:
24 //
25 // UTessellatedSolid* solidTarget = new UTessellatedSolid("Solid_name");
26 //
27 // Define the facets which form the solid
28 //
29 // double targetSiz = 10*cm ;
30 // UTriangularFacet *facet1 = new
31 // UTriangularFacet (UVector3(-targetSize,-targetSize, 0.0),
32 // UVector3(+targetSize,-targetSize, 0.0),
33 // UVector3( 0.0, 0.0,+targetSize),
34 // ABSOLUTE);
35 // UTriangularFacet *facet2 = new
36 // UTriangularFacet (UVector3(+targetSize,-targetSize, 0.0),
37 // UVector3(+targetSize,+targetSize, 0.0),
38 // UVector3( 0.0, 0.0,+targetSize),
39 // ABSOLUTE);
40 // UTriangularFacet *facet3 = new
41 // UTriangularFacet (UVector3(+targetSize,+targetSize, 0.0),
42 // UVector3(-targetSize,+targetSize, 0.0),
43 // UVector3( 0.0, 0.0,+targetSize),
44 // ABSOLUTE);
45 // UTriangularFacet *facet4 = new
46 // UTriangularFacet (UVector3(-targetSize,+targetSize, 0.0),
47 // UVector3(-targetSize,-targetSize, 0.0),
48 // UVector3( 0.0, 0.0,+targetSize),
49 // ABSOLUTE);
50 // UQuadrangularFacet *facet5 = new
51 // UQuadrangularFacet (UVector3(-targetSize,-targetSize, 0.0),
52 // UVector3(-targetSize,+targetSize, 0.0),
53 // UVector3(+targetSize,+targetSize, 0.0),
54 // UVector3(+targetSize,-targetSize, 0.0),
55 // ABSOLUTE);
56 //
57 // Then add the facets to the solid:
58 //
59 // solidTarget->AddFacet((UVFacet*) facet1);
60 // solidTarget->AddFacet((UVFacet*) facet2);
61 // solidTarget->AddFacet((UVFacet*) facet3);
62 // solidTarget->AddFacet((UVFacet*) facet4);
63 // solidTarget->AddFacet((UVFacet*) facet5);
64 //
65 // Finally declare the solid is complete:
66 //
67 // solidTarget->SetSolidClosed(true);
68 //
69 // 11.07.12 Marek Gayer
70 // Created from original implementation in Geant4
71 // --------------------------------------------------------------------
72 
73 #ifndef UTessellatedSolid_hh
74 #define UTessellatedSolid_hh 1
75 
76 #include <iostream>
77 #include <vector>
78 #include <set>
79 #include <map>
80 
81 #include "VUSolid.hh"
82 #include "VUFacet.hh"
83 #include "UVoxelizer.hh"
84 
86 {
87  int id;
88  double mag2;
89 };
90 
92 {
93  public:
94  bool operator()(const UVertexInfo& l, const UVertexInfo& r) const
95  {
96  return l.mag2 == r.mag2 ? l.id < r.id : l.mag2 < r.mag2;
97  }
98 };
99 
101 {
102  public:
103 
105  virtual ~UTessellatedSolid();
106 
107  UTessellatedSolid(const std::string& name);
108 
109  UTessellatedSolid(__void__&);
110  // Fake default constructor for usage restricted to direct object
111  // persistency for clients requiring preallocation of memory for
112  // persistifiable objects.
113 
117 
118  bool AddFacet(VUFacet* aFacet);
119  inline VUFacet* GetFacet(int i) const { return fFacets[i]; }
120  int GetNumberOfFacets() const;
121 
122  virtual double GetSurfaceArea();
123 
124  virtual VUSolid::EnumInside Inside(const UVector3& p) const;
125 
126  virtual bool Normal(const UVector3& p, UVector3& aNormal) const;
127 
128  virtual double SafetyFromOutside(const UVector3& p, bool aAccurate = false) const;
129 
130  virtual double SafetyFromInside(const UVector3& p, bool aAccurate = false) const;
131  virtual UGeometryType GetEntityType() const;
132 
133  void SetSolidClosed(const bool t);
134 
135  bool GetSolidClosed() const;
136 
137  virtual UVector3 GetPointOnSurface() const;
138 
139  virtual std::ostream& StreamInfo(std::ostream& os) const;
140 
141  virtual double Capacity() { return 0; }
142  virtual double SurfaceArea() { return GetSurfaceArea(); }
143 
144  inline virtual void GetParametersList(int /*aNumber*/, double* /*aArray*/) const {}
145  inline virtual void ComputeBBox(UBBox* /*aBox*/, bool /*aStore = false*/) {}
146 
147  inline void SetMaxVoxels(int max) { fVoxels.SetMaxVoxels(max); }
148 
149  inline UVoxelizer& GetVoxels() { return fVoxels; }
150 
151  virtual VUSolid* Clone() const;
152 
153  double GetMinXExtent() const;
154  double GetMaxXExtent() const;
155  double GetMinYExtent() const;
156  double GetMaxYExtent() const;
157  double GetMinZExtent() const;
158  double GetMaxZExtent() const;
159 
160  virtual double DistanceToIn(const UVector3& p, const UVector3& v,
161  double aPstep = UUtils::kInfinity) const
162  {
163  return DistanceToInCore(p, v, aPstep);
164  }
165 
166  virtual double DistanceToOut(const UVector3& p,
167  const UVector3& v,
168  UVector3& aNormalVector,
169  bool& aConvex,
170  double aPstep = UUtils::kInfinity
171  ) const
172  {
173  return DistanceToOutCore(p, v, aNormalVector, aConvex, aPstep);
174  }
175 
176  void Extent(UVector3& aMin, UVector3& aMax) const;
177 
179  int AllocatedMemory();
180  void DisplayAllocatedMemory();
181 
182  private:
183 
184  double DistanceToOutNoVoxels(const UVector3& p,
185  const UVector3& v,
186  UVector3& aNormalVector,
187  bool& aConvex,
188  double aPstep = UUtils::kInfinity
189  ) const;
190 
191  double DistanceToInCandidates(const std::vector<int>& candidates, const UVector3& aPoint, const UVector3& aDirection /*, double aPstep, const UBits &bits*/) const;
192  void DistanceToOutCandidates(const std::vector<int >& candidates, const UVector3& aPoint, const UVector3& direction, double& minDist, UVector3& minNormal, int& minCandidate/*, double aPstep*/ /*, UBits &bits*/) const;
193  double DistanceToInNoVoxels(const UVector3& p, const UVector3& v, double aPstep = UUtils::kInfinity) const;
194 
195  void SetExtremeFacets();
196 
198  VUSolid::EnumInside InsideVoxels(const UVector3& aPoint) const;
199 
200  void Voxelize();
201 
202  void CreateVertexList();
203 
204  void PrecalculateInsides();
205 
206  void SetRandomVectors();
207 
208  double DistanceToInCore(const UVector3& p,
209  const UVector3& v,
210  double aPstep = UUtils::kInfinity) const;
211  double DistanceToOutCore(const UVector3& p,
212  const UVector3& v,
213  UVector3& aNormalVector,
214  bool& aConvex,
215  double aPstep = UUtils::kInfinity) const;
216 
217  int SetAllUsingStack(const std::vector<int>& voxel,
218  const std::vector<int>& max,
219  bool status, UBits& checked);
220 
221  void DeleteObjects();
222  void CopyObjects(const UTessellatedSolid& s);
223 
224  static bool CompareSortedVoxel(const std::pair<int, double>& l,
225  const std::pair<int, double>& r);
226 
227  double MinDistanceFacet(const UVector3& p, bool simple, VUFacet*& facet) const;
228 
229  inline bool OutsideOfExtent(const UVector3& p, double tolerance = 0) const
230  {
231  return (p.x() < fMinExtent.x() - tolerance || p.x() > fMaxExtent.x() + tolerance ||
232  p.y() < fMinExtent.y() - tolerance || p.y() > fMaxExtent.y() + tolerance ||
233  p.z() < fMinExtent.z() - tolerance || p.z() > fMaxExtent.z() + tolerance);
234  }
235 
236  void Initialize();
237 
238  private:
239 
240  std::vector<VUFacet*> fFacets;
241  std::set<VUFacet*> fExtremeFacets; // Does all other facets lie on or behind this surface?
242 
244  double fCubicVolume;
245  double fSurfaceArea;
246 
247  std::vector<UVector3> fVertexList;
248 
249  std::set<UVertexInfo, UVertexComparator> fFacetList;
250 
253 
254  static const double dirTolerance;
255  std::vector<UVector3> fRandir;
256 
258 
260 
261  UVoxelizer fVoxels; // voxelized solid
262 
264 };
265 
266 #endif
virtual double SurfaceArea()
double GetMaxZExtent() const
virtual UGeometryType GetEntityType() const
double GetMinYExtent() const
double & z()
Definition: UVector3.hh:352
static const double dirTolerance
double & y()
Definition: UVector3.hh:348
virtual UVector3 GetPointOnSurface() const
UTessellatedSolid & operator=(const UTessellatedSolid &s)
virtual ~UTessellatedSolid()
double GetMinXExtent() const
virtual bool Normal(const UVector3 &p, UVector3 &aNormal) const
virtual std::ostream & StreamInfo(std::ostream &os) const
std::vector< UVector3 > fRandir
double GetMaxYExtent() const
bool OutsideOfExtent(const UVector3 &p, double tolerance=0) const
std::set< VUFacet * > fExtremeFacets
G4String name
Definition: TRTMaterials.hh:40
double MinDistanceFacet(const UVector3 &p, bool simple, VUFacet *&facet) const
virtual double SafetyFromOutside(const UVector3 &p, bool aAccurate=false) const
void SetMaxVoxels(int max)
Definition: UVoxelizer.cc:1125
bool AddFacet(VUFacet *aFacet)
virtual VUSolid * Clone() const
static const G4double tolerance
bool operator()(const UVertexInfo &l, const UVertexInfo &r) const
int GetNumberOfFacets() const
void DistanceToOutCandidates(const std::vector< int > &candidates, const UVector3 &aPoint, const UVector3 &direction, double &minDist, UVector3 &minNormal, int &minCandidate) const
virtual double DistanceToIn(const UVector3 &p, const UVector3 &v, double aPstep=UUtils::kInfinity) const
double GetMaxXExtent() const
UVoxelizer & GetVoxels()
std::vector< VUFacet * > fFacets
static const double s
Definition: G4SIunits.hh:150
static const double kInfinity
Definition: UUtils.hh:54
static bool CompareSortedVoxel(const std::pair< int, double > &l, const std::pair< int, double > &r)
VUSolid::EnumInside InsideNoVoxels(const UVector3 &p) const
double DistanceToOutNoVoxels(const UVector3 &p, const UVector3 &v, UVector3 &aNormalVector, bool &aConvex, double aPstep=UUtils::kInfinity) const
double DistanceToInNoVoxels(const UVector3 &p, const UVector3 &v, double aPstep=UUtils::kInfinity) const
virtual void GetParametersList(int, double *) const
void Initialize()
TODO: make a benchmark for automatic selection of number of voxels. random voxels will be selected...
double & x()
Definition: UVector3.hh:344
double DistanceToInCandidates(const std::vector< int > &candidates, const UVector3 &aPoint, const UVector3 &aDirection) const
double DistanceToInCore(const UVector3 &p, const UVector3 &v, double aPstep=UUtils::kInfinity) const
virtual VUSolid::EnumInside Inside(const UVector3 &p) const
virtual double Capacity()
EnumInside
Definition: VUSolid.hh:23
void Extent(UVector3 &aMin, UVector3 &aMax) const
virtual void ComputeBBox(UBBox *, bool)
UGeometryType fGeometryType
double GetMinZExtent() const
std::vector< UVector3 > fVertexList
virtual double SafetyFromInside(const UVector3 &p, bool aAccurate=false) const
void SetMaxVoxels(int max)
T max(const T t1, const T t2)
brief Return the largest of the two arguments
VUSolid::EnumInside InsideVoxels(const UVector3 &aPoint) const
std::set< UVertexInfo, UVertexComparator > fFacetList
double DistanceToOutCore(const UVector3 &p, const UVector3 &v, UVector3 &aNormalVector, bool &aConvex, double aPstep=UUtils::kInfinity) const
bool GetSolidClosed() const
void CopyObjects(const UTessellatedSolid &s)
std::string UGeometryType
Definition: UTypes.hh:39
void SetSolidClosed(const bool t)
UTessellatedSolid & operator+=(const UTessellatedSolid &right)
virtual double GetSurfaceArea()
virtual double DistanceToOut(const UVector3 &p, const UVector3 &v, UVector3 &aNormalVector, bool &aConvex, double aPstep=UUtils::kInfinity) const
Definition: UBits.hh:38
VUFacet * GetFacet(int i) const
int SetAllUsingStack(const std::vector< int > &voxel, const std::vector< int > &max, bool status, UBits &checked)