Geant4  10.01.p03
G4BooleanSolid.cc
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. *
20 // * By using, copying, modifying or distributing the software (or *
21 // * any work based on the software) you agree to acknowledge its *
22 // * use in resulting scientific publications, and indicate your *
23 // * acceptance of all terms of the Geant4 Software license. *
24 // ********************************************************************
25 //
26 //
27 // $Id: G4BooleanSolid.cc 95297 2016-02-04 09:30:14Z gcosmo $
28 //
29 // Implementation for the abstract base class for solids created by boolean
30 // operations between other solids
31 //
32 // History:
33 //
34 // 10.09.98 V.Grichine, created
35 //
36 // --------------------------------------------------------------------
37 
38 #include "G4BooleanSolid.hh"
39 #include "G4VSolid.hh"
40 #include "G4Polyhedron.hh"
41 #include "HepPolyhedronProcessor.h"
42 #include "Randomize.hh"
43 
44 #include "G4AutoLock.hh"
45 
46 namespace
47 {
48  G4Mutex polyhedronMutex = G4MUTEX_INITIALIZER;
49 }
50 
52 //
53 // Constructor
54 
56  G4VSolid* pSolidA ,
57  G4VSolid* pSolidB ) :
58  G4VSolid(pName), fAreaRatio(0.), fStatistics(1000000), fCubVolEpsilon(0.001),
59  fAreaAccuracy(-1.), fCubicVolume(0.), fSurfaceArea(0.),
60  fRebuildPolyhedron(false), fpPolyhedron(0), createdDisplacedSolid(false)
61 {
62  fPtrSolidA = pSolidA ;
63  fPtrSolidB = pSolidB ;
64 }
65 
67 //
68 // Constructor
69 
71  G4VSolid* pSolidA ,
72  G4VSolid* pSolidB ,
73  G4RotationMatrix* rotMatrix,
74  const G4ThreeVector& transVector ) :
75  G4VSolid(pName), fAreaRatio(0.), fStatistics(1000000), fCubVolEpsilon(0.001),
76  fAreaAccuracy(-1.), fCubicVolume(0.), fSurfaceArea(0.),
77  fRebuildPolyhedron(false), fpPolyhedron(0), createdDisplacedSolid(true)
78 {
79  fPtrSolidA = pSolidA ;
80  fPtrSolidB = new G4DisplacedSolid("placedB",pSolidB,rotMatrix,transVector) ;
81 }
82 
84 //
85 // Constructor
86 
88  G4VSolid* pSolidA ,
89  G4VSolid* pSolidB ,
90  const G4Transform3D& transform ) :
91  G4VSolid(pName), fAreaRatio(0.), fStatistics(1000000), fCubVolEpsilon(0.001),
92  fAreaAccuracy(-1.), fCubicVolume(0.), fSurfaceArea(0.),
93  fRebuildPolyhedron(false), fpPolyhedron(0), createdDisplacedSolid(true)
94 {
95  fPtrSolidA = pSolidA ;
96  fPtrSolidB = new G4DisplacedSolid("placedB",pSolidB,transform) ;
97 }
98 
100 //
101 // Fake default constructor - sets only member data and allocates memory
102 // for usage restricted to object persistency.
103 
105  : G4VSolid(a), fPtrSolidA(0), fPtrSolidB(0), fAreaRatio(0.),
106  fStatistics(1000000), fCubVolEpsilon(0.001),
107  fAreaAccuracy(-1.), fCubicVolume(0.), fSurfaceArea(0.),
108  fRebuildPolyhedron(false), fpPolyhedron(0), createdDisplacedSolid(false)
109 {
110 }
111 
113 //
114 // Destructor deletes transformation contents of the created displaced solid
115 
117 {
119  {
120  ((G4DisplacedSolid*)fPtrSolidB)->CleanTransformations();
121  }
122  delete fpPolyhedron; fpPolyhedron = 0;
123 }
124 
126 //
127 // Copy constructor
128 
130  : G4VSolid (rhs), fPtrSolidA(rhs.fPtrSolidA), fPtrSolidB(rhs.fPtrSolidB),
131  fAreaRatio(rhs.fAreaRatio),
132  fStatistics(rhs.fStatistics), fCubVolEpsilon(rhs.fCubVolEpsilon),
133  fAreaAccuracy(rhs.fAreaAccuracy), fCubicVolume(rhs.fCubicVolume),
134  fSurfaceArea(rhs.fSurfaceArea), fRebuildPolyhedron(false), fpPolyhedron(0),
135  createdDisplacedSolid(rhs.createdDisplacedSolid)
136 {
137 }
138 
140 //
141 // Assignment operator
142 
144 {
145  // Check assignment to self
146  //
147  if (this == &rhs) { return *this; }
148 
149  // Copy base class data
150  //
151  G4VSolid::operator=(rhs);
152 
153  // Copy data
154  //
156  fAreaRatio= rhs.fAreaRatio;
161  fRebuildPolyhedron = false;
162  delete fpPolyhedron; fpPolyhedron = 0;
163 
164  return *this;
165 }
166 
168 //
169 // If Solid is made up from a Boolean operation of two solids,
170 // return the corresponding solid (for no=0 and 1)
171 // If the solid is not a "Boolean", return 0
172 
174 {
175  const G4VSolid* subSolid=0;
176  if( no == 0 )
177  subSolid = fPtrSolidA;
178  else if( no == 1 )
179  subSolid = fPtrSolidB;
180  else
181  {
182  DumpInfo();
183  G4Exception("G4BooleanSolid::GetConstituentSolid()",
184  "GeomSolids0002", FatalException, "Invalid solid index.");
185  }
186 
187  return subSolid;
188 }
189 
191 //
192 // If Solid is made up from a Boolean operation of two solids,
193 // return the corresponding solid (for no=0 and 1)
194 // If the solid is not a "Boolean", return 0
195 
197 {
198  G4VSolid* subSolid=0;
199  if( no == 0 )
200  subSolid = fPtrSolidA;
201  else if( no == 1 )
202  subSolid = fPtrSolidB;
203  else
204  {
205  DumpInfo();
206  G4Exception("G4BooleanSolid::GetConstituentSolid()",
207  "GeomSolids0002", FatalException, "Invalid solid index.");
208  }
209 
210  return subSolid;
211 }
212 
214 //
215 // Returns entity type
216 
218 {
219  return G4String("G4BooleanSolid");
220 }
221 
223 //
224 // Stream object contents to an output stream
225 
226 std::ostream& G4BooleanSolid::StreamInfo(std::ostream& os) const
227 {
228  os << "-----------------------------------------------------------\n"
229  << " *** Dump for Boolean solid - " << GetName() << " ***\n"
230  << " ===================================================\n"
231  << " Solid type: " << GetEntityType() << "\n"
232  << " Parameters of constituent solids: \n"
233  << "===========================================================\n";
234  fPtrSolidA->StreamInfo(os);
235  fPtrSolidB->StreamInfo(os);
236  os << "===========================================================\n";
237 
238  return os;
239 }
240 
242 //
243 // Returns a point (G4ThreeVector) randomly and uniformly selected
244 // on the solid surface
245 //
246 
248 {
249  G4double rand;
250  G4ThreeVector p;
251 
252  do // Loop checking, 13.08.2015, G.Cosmo
253  {
254  rand = G4UniformRand();
255 
256  if (rand < GetAreaRatio()) { p = fPtrSolidA->GetPointOnSurface(); }
257  else { p = fPtrSolidB->GetPointOnSurface(); }
258  } while (Inside(p) != kSurface);
259 
260  return p;
261 }
262 
264 //
265 // Returns polyhedron for visualization
266 
268 {
269  if (!fpPolyhedron ||
272  fpPolyhedron->GetNumberOfRotationSteps())
273  {
274  G4AutoLock l(&polyhedronMutex);
275  delete fpPolyhedron;
277  fRebuildPolyhedron = false;
278  l.unlock();
279  }
280  return fpPolyhedron;
281 }
282 
284 //
285 // Stacks polyhedra for processing. Returns top polyhedron.
286 
289  const G4VSolid* solid) const
290 {
291  HepPolyhedronProcessor::Operation operation;
292  const G4String& type = solid->GetEntityType();
293  if (type == "G4UnionSolid")
294  { operation = HepPolyhedronProcessor::UNION; }
295  else if (type == "G4IntersectionSolid")
296  { operation = HepPolyhedronProcessor::INTERSECTION; }
297  else if (type == "G4SubtractionSolid")
298  { operation = HepPolyhedronProcessor::SUBTRACTION; }
299  else
300  {
301  std::ostringstream message;
302  message << "Solid - " << solid->GetName()
303  << " - Unrecognised composite solid" << G4endl
304  << " Returning NULL !";
305  G4Exception("StackPolyhedron()", "GeomSolids1001", JustWarning, message);
306  return 0;
307  }
308 
309  G4Polyhedron* top = 0;
310  const G4VSolid* solidA = solid->GetConstituentSolid(0);
311  const G4VSolid* solidB = solid->GetConstituentSolid(1);
312 
313  if (solidA->GetConstituentSolid(0))
314  {
315  top = StackPolyhedron(processor, solidA);
316  }
317  else
318  {
319  top = solidA->GetPolyhedron();
320  }
321  G4Polyhedron* operand = solidB->GetPolyhedron();
322  processor.push_back (operation, *operand);
323 
324  return top;
325 }
virtual G4Polyhedron * GetPolyhedron() const
Definition: G4VSolid.cc:644
G4String GetName() const
G4VSolid * fPtrSolidB
virtual G4Polyhedron * GetPolyhedron() const
G4double fCubicVolume
G4BooleanSolid(const G4String &pName, G4VSolid *pSolidA, G4VSolid *pSolidB)
CLHEP::Hep3Vector G4ThreeVector
CLHEP::HepRotation G4RotationMatrix
virtual G4GeometryType GetEntityType() const
G4double a
Definition: TRTMaterials.hh:39
virtual G4GeometryType GetEntityType() const =0
int G4int
Definition: G4Types.hh:78
#define G4MUTEX_INITIALIZER
Definition: G4Threading.hh:175
void DumpInfo() const
static int operand(pchar begin, pchar end, double &result, pchar &endp, const dic_type &dictionary)
Definition: Evaluator.cc:162
G4bool createdDisplacedSolid
virtual std::ostream & StreamInfo(std::ostream &os) const =0
#define G4UniformRand()
Definition: Randomize.hh:93
G4double fAreaAccuracy
G4double fCubVolEpsilon
virtual EInside Inside(const G4ThreeVector &p) const =0
G4double fSurfaceArea
std::ostream & StreamInfo(std::ostream &os) const
G4Polyhedron * fpPolyhedron
HepGeom::Transform3D G4Transform3D
virtual G4Polyhedron * CreatePolyhedron() const
Definition: G4VSolid.cc:639
virtual ~G4BooleanSolid()
virtual const G4VSolid * GetConstituentSolid(G4int no) const
void G4Exception(const char *originOfException, const char *exceptionCode, G4ExceptionSeverity severity, const char *comments)
Definition: G4Exception.cc:41
G4double GetAreaRatio() const
#define processor
Definition: xmlparse.cc:600
G4int G4Mutex
Definition: G4Threading.hh:173
virtual G4ThreeVector GetPointOnSurface() const
Definition: G4VSolid.cc:152
virtual const G4VSolid * GetConstituentSolid(G4int no) const
Definition: G4VSolid.cc:167
G4bool fRebuildPolyhedron
G4VSolid * fPtrSolidA
G4ThreeVector GetPointOnSurface() const
#define G4endl
Definition: G4ios.hh:61
G4VSolid & operator=(const G4VSolid &rhs)
Definition: G4VSolid.cc:110
G4int GetNumberOfRotationStepsAtTimeOfCreation() const
G4BooleanSolid & operator=(const G4BooleanSolid &rhs)
double G4double
Definition: G4Types.hh:76
G4Polyhedron * StackPolyhedron(HepPolyhedronProcessor &, const G4VSolid *) const