Geant4 11.4.0
Toolkit for the simulation of the passage of particles through matter
Loading...
Searching...
No Matches
G4UnionSolid.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// Implementation of methods for the class G4UnionSolid
27//
28// 23.04.18 E.Tcherniaev: added extended BBox, yearly return in Inside()
29// 17.03.17 E.Tcherniaev: revision of SurfaceNormal()
30// 12.09.98 V.Grichine: first implementation
31// --------------------------------------------------------------------
32
33#include <sstream>
34
35#include "G4UnionSolid.hh"
36
37#include "G4SystemOfUnits.hh"
38#include "G4VoxelLimits.hh"
41
42#include "G4VGraphicsScene.hh"
43#include "G4Polyhedron.hh"
46
48#include "G4AutoLock.hh"
49
50namespace
51{
53}
54
55//////////////////////////////////////////////////////////////////////////
56//
57// Transfer all data members to G4BooleanSolid which is responsible
58// for them. pName will be in turn sent to G4VSolid
59
61 G4VSolid* pSolidA ,
62 G4VSolid* pSolidB )
63 : G4BooleanSolid(pName,pSolidA,pSolidB)
64{
65 Init();
66}
67
68//////////////////////////////////////////////////////////////////////////
69//
70// Constructor
71
73 G4VSolid* pSolidA ,
74 G4VSolid* pSolidB ,
75 G4RotationMatrix* rotMatrix,
76 const G4ThreeVector& transVector )
77 : G4BooleanSolid(pName,pSolidA,pSolidB,rotMatrix,transVector)
78
79{
80 Init();
81}
82
83//////////////////////////////////////////////////////////////////////////
84//
85// Constructor
86
88 G4VSolid* pSolidA ,
89 G4VSolid* pSolidB ,
90 const G4Transform3D& transform )
91 : G4BooleanSolid(pName,pSolidA,pSolidB,transform)
92{
93 Init();
94}
95
96//////////////////////////////////////////////////////////////////////////
97//
98// Fake default constructor - sets only member data and allocates memory
99// for usage restricted to object persistency.
100
102 : G4BooleanSolid(a)
103{
104}
105
106//////////////////////////////////////////////////////////////////////////
107//
108// Copy constructor
109
111 : G4BooleanSolid (rhs)
112{
113 fPMin = rhs.fPMin;
114 fPMax = rhs.fPMax;
115 halfCarTolerance = 0.5*kCarTolerance;
116}
117
118//////////////////////////////////////////////////////////////////////////
119//
120// Assignment operator
121
123{
124 // Check assignment to self
125 //
126 if (this == &rhs) { return *this; }
127
128 // Copy base class data
129 //
131
132 fPMin = rhs.fPMin;
133 fPMax = rhs.fPMax;
134 halfCarTolerance = rhs.halfCarTolerance;
135
136 return *this;
137}
138
139//////////////////////////////////////////////////////////////////////////
140//
141// Initialisation
142
143void G4UnionSolid::Init()
144{
146 G4ThreeVector pmin, pmax;
147 BoundingLimits(pmin, pmax);
148 fPMin = pmin - pdelta;
149 fPMax = pmax + pdelta;
150 halfCarTolerance = 0.5*kCarTolerance;
151}
152
153//////////////////////////////////////////////////////////////////////////
154//
155// Get bounding box
156
158 G4ThreeVector& pMax) const
159{
160 G4ThreeVector minA,maxA, minB,maxB;
161 fPtrSolidA->BoundingLimits(minA,maxA);
162 fPtrSolidB->BoundingLimits(minB,maxB);
163
164 pMin.set(std::min(minA.x(),minB.x()),
165 std::min(minA.y(),minB.y()),
166 std::min(minA.z(),minB.z()));
167
168 pMax.set(std::max(maxA.x(),maxB.x()),
169 std::max(maxA.y(),maxB.y()),
170 std::max(maxA.z(),maxB.z()));
171
172 // Check correctness of the bounding box
173 //
174 if (pMin.x() >= pMax.x() || pMin.y() >= pMax.y() || pMin.z() >= pMax.z())
175 {
176 std::ostringstream message;
177 message << "Bad bounding box (min >= max) for solid: "
178 << GetName() << " !"
179 << "\npMin = " << pMin
180 << "\npMax = " << pMax;
181 G4Exception("G4UnionSolid::BoundingLimits()", "GeomMgt0001",
182 JustWarning, message);
183 DumpInfo();
184 }
185}
186
187//////////////////////////////////////////////////////////////////////////
188//
189// Calculate extent under transform and specified limit
190
191G4bool
193 const G4VoxelLimits& pVoxelLimit,
194 const G4AffineTransform& pTransform,
195 G4double& pMin,
196 G4double& pMax ) const
197{
198 G4bool touchesA, touchesB, out ;
199 G4double minA = kInfinity, minB = kInfinity,
200 maxA = -kInfinity, maxB = -kInfinity;
201
202 touchesA = fPtrSolidA->CalculateExtent( pAxis, pVoxelLimit,
203 pTransform, minA, maxA);
204 touchesB = fPtrSolidB->CalculateExtent( pAxis, pVoxelLimit,
205 pTransform, minB, maxB);
206 if( touchesA || touchesB )
207 {
208 pMin = std::min( minA, minB );
209 pMax = std::max( maxA, maxB );
210 out = true ;
211 }
212 else
213 {
214 out = false ;
215 }
216
217 return out ; // It exists in this slice if either one does.
218}
219
220//////////////////////////////////////////////////////////////////////////
221//
222// Important comment: When solids A and B touch together along flat
223// surface the surface points will be considered as kSurface, while points
224// located around will correspond to kInside
225
227{
228 if (std::max(p.z()-fPMax.z(), fPMin.z()-p.z()) > 0) { return kOutside; }
229
230 EInside positionA = fPtrSolidA->Inside(p);
231 if (positionA == kInside) { return positionA; } // inside A
232 EInside positionB = fPtrSolidB->Inside(p);
233 if (positionA == kOutside) { return positionB; }
234
235 if (positionB == kInside) { return positionB; } // inside B
236 if (positionB == kOutside) { return positionA; } // surface A
237
238 // Both points are on surface
239 //
240 static const G4double rtol
242
243 return ((fPtrSolidA->SurfaceNormal(p) +
244 fPtrSolidB->SurfaceNormal(p)).mag2() < rtol) ? kInside : kSurface;
245}
246
247//////////////////////////////////////////////////////////////////////////
248//
249// Get surface normal
250
253{
254 EInside positionA = fPtrSolidA->Inside(p);
255 EInside positionB = fPtrSolidB->Inside(p);
256
257 if (positionA == kSurface &&
258 positionB == kOutside) { return fPtrSolidA->SurfaceNormal(p); }
259
260 if (positionA == kOutside &&
261 positionB == kSurface) { return fPtrSolidB->SurfaceNormal(p); }
262
263 if (positionA == kSurface &&
264 positionB == kSurface)
265 {
266 if (Inside(p) == kSurface)
267 {
268 G4ThreeVector normalA = fPtrSolidA->SurfaceNormal(p);
269 G4ThreeVector normalB = fPtrSolidB->SurfaceNormal(p);
270 return (normalA + normalB).unit();
271 }
272 }
273#ifdef G4BOOLDEBUG
274 G4String surf[3] = { "OUTSIDE", "SURFACE", "INSIDE" };
275 std::ostringstream message;
276 G4int oldprc = message.precision(16);
277 message << "Invalid call of SurfaceNormal(p) for union solid: "
278 << GetName() << " !"
279 << "\nPoint p" << p << " is " << surf[Inside(p)] << " !!!";
280 message.precision(oldprc);
281 G4Exception("G4UnionSolid::SurfaceNormal()", "GeomMgt0001",
282 JustWarning, message);
283#endif
284 return fPtrSolidA->SurfaceNormal(p);
285}
286
287//////////////////////////////////////////////////////////////////////////
288//
289// The same algorithm as in DistanceToIn(p)
290
293 const G4ThreeVector& v ) const
294{
295#ifdef G4BOOLDEBUG
296 if( Inside(p) == kInside )
297 {
298 G4cout << "WARNING - Invalid call in "
299 << "G4UnionSolid::DistanceToIn(p,v)" << G4endl
300 << " Point p is inside !" << G4endl;
301 G4cout << " p = " << p << G4endl;
302 G4cout << " v = " << v << G4endl;
303 G4cerr << "WARNING - Invalid call in "
304 << "G4UnionSolid::DistanceToIn(p,v)" << G4endl
305 << " Point p is inside !" << G4endl;
306 G4cerr << " p = " << p << G4endl;
307 G4cerr << " v = " << v << G4endl;
308 }
309#endif
310
311 return std::min(fPtrSolidA->DistanceToIn(p,v),
312 fPtrSolidB->DistanceToIn(p,v) ) ;
313}
314
315//////////////////////////////////////////////////////////////////////////
316//
317// Approximate nearest distance from the point p to the union of
318// two solids
319
322{
323#ifdef G4BOOLDEBUG
324 if( Inside(p) == kInside )
325 {
326 G4cout << "WARNING - Invalid call in "
327 << "G4UnionSolid::DistanceToIn(p)" << G4endl
328 << " Point p is inside !" << G4endl;
329 G4cout << " p = " << p << G4endl;
330 G4cerr << "WARNING - Invalid call in "
331 << "G4UnionSolid::DistanceToIn(p)" << G4endl
332 << " Point p is inside !" << G4endl;
333 G4cerr << " p = " << p << G4endl;
334 }
335#endif
336 G4double distA = fPtrSolidA->DistanceToIn(p) ;
337 G4double distB = fPtrSolidB->DistanceToIn(p) ;
338 G4double safety = std::min(distA,distB) ;
339 if(safety < 0.0) { safety = 0.0 ; }
340 return safety ;
341}
342
343//////////////////////////////////////////////////////////////////////////
344//
345// The same algorithm as DistanceToOut(p)
346
349 const G4ThreeVector& v,
350 const G4bool calcNorm,
351 G4bool* validNorm,
352 G4ThreeVector* n ) const
353{
354 G4double dist = 0.0, disTmp = 0.0 ;
355 G4ThreeVector normTmp;
356 G4ThreeVector* nTmp = &normTmp;
357
358 if( Inside(p) == kOutside )
359 {
360#ifdef G4BOOLDEBUG
361 G4cout << "Position:" << G4endl << G4endl;
362 G4cout << "p.x() = " << p.x()/mm << " mm" << G4endl;
363 G4cout << "p.y() = " << p.y()/mm << " mm" << G4endl;
364 G4cout << "p.z() = " << p.z()/mm << " mm" << G4endl << G4endl;
365 G4cout << "Direction:" << G4endl << G4endl;
366 G4cout << "v.x() = " << v.x() << G4endl;
367 G4cout << "v.y() = " << v.y() << G4endl;
368 G4cout << "v.z() = " << v.z() << G4endl << G4endl;
369 G4cout << "WARNING - Invalid call in "
370 << "G4UnionSolid::DistanceToOut(p,v)" << G4endl
371 << " Point p is outside !" << G4endl;
372 G4cout << " p = " << p << G4endl;
373 G4cout << " v = " << v << G4endl;
374 G4cerr << "WARNING - Invalid call in "
375 << "G4UnionSolid::DistanceToOut(p,v)" << G4endl
376 << " Point p is outside !" << G4endl;
377 G4cerr << " p = " << p << G4endl;
378 G4cerr << " v = " << v << G4endl;
379#endif
380 }
381 else
382 {
383 EInside positionA = fPtrSolidA->Inside(p) ;
384
385 if( positionA != kOutside )
386 {
387 do // Loop checking, 13.08.2015, G.Cosmo
388 {
389 disTmp = fPtrSolidA->DistanceToOut(p+dist*v,v,calcNorm,
390 validNorm,nTmp);
391 dist += disTmp ;
392
393 if(fPtrSolidB->Inside(p+dist*v) != kOutside)
394 {
395 disTmp = fPtrSolidB->DistanceToOut(p+dist*v,v,calcNorm,
396 validNorm,nTmp);
397 dist += disTmp ;
398 }
399 }
400 while( (fPtrSolidA->Inside(p+dist*v) != kOutside)
401 && (disTmp > halfCarTolerance) );
402 }
403 else // if( positionB != kOutside )
404 {
405 do // Loop checking, 13.08.2015, G.Cosmo
406 {
407 disTmp = fPtrSolidB->DistanceToOut(p+dist*v,v,calcNorm,
408 validNorm,nTmp);
409 dist += disTmp ;
410
411 if(fPtrSolidA->Inside(p+dist*v) != kOutside)
412 {
413 disTmp = fPtrSolidA->DistanceToOut(p+dist*v,v,calcNorm,
414 validNorm,nTmp);
415 dist += disTmp ;
416 }
417 }
418 while( (fPtrSolidB->Inside(p+dist*v) != kOutside)
419 && (disTmp > halfCarTolerance) );
420 }
421 }
422 if( calcNorm )
423 {
424 *validNorm = false ;
425 *n = *nTmp ;
426 }
427 return dist ;
428}
429
430//////////////////////////////////////////////////////////////////////////
431//
432// Inverted algorithm of DistanceToIn(p)
433
436{
437 G4double distout = 0.0;
438 if( Inside(p) == kOutside )
439 {
440#ifdef G4BOOLDEBUG
441 G4cout << "WARNING - Invalid call in "
442 << "G4UnionSolid::DistanceToOut(p)" << G4endl
443 << " Point p is outside !" << G4endl;
444 G4cout << " p = " << p << G4endl;
445 G4cerr << "WARNING - Invalid call in "
446 << "G4UnionSolid::DistanceToOut(p)" << G4endl
447 << " Point p is outside !" << G4endl;
448 G4cerr << " p = " << p << G4endl;
449#endif
450 }
451 else
452 {
453 EInside positionA = fPtrSolidA->Inside(p) ;
454 EInside positionB = fPtrSolidB->Inside(p) ;
455
456 // Is this equivalent ??
457 // if( ! ( (positionA == kOutside)) &&
458 // (positionB == kOutside)) )
459 if((positionA == kInside && positionB == kInside ) ||
460 (positionA == kInside && positionB == kSurface ) ||
461 (positionA == kSurface && positionB == kInside ) )
462 {
463 distout= std::max(fPtrSolidA->DistanceToOut(p),
464 fPtrSolidB->DistanceToOut(p) ) ;
465 }
466 else
467 {
468 if(positionA == kOutside)
469 {
470 distout= fPtrSolidB->DistanceToOut(p) ;
471 }
472 else
473 {
474 distout= fPtrSolidA->DistanceToOut(p) ;
475 }
476 }
477 }
478 return distout;
479}
480
481//////////////////////////////////////////////////////////////////////////
482//
483// GetEntityType
484
486{
487 return {"G4UnionSolid"};
488}
489
490//////////////////////////////////////////////////////////////////////////
491//
492// Make a clone of the object
493
495{
496 return new G4UnionSolid(*this);
497}
498
499//////////////////////////////////////////////////////////////////////////
500//
501// ComputeDimensions
502
503void
505 const G4int,
506 const G4VPhysicalVolume* )
507{
508 DumpInfo();
509 G4Exception("G4UnionSolid::ComputeDimensions()",
510 "GeomSolids0001", FatalException,
511 "Method not applicable in this context!");
512}
513
514//////////////////////////////////////////////////////////////////////////
515//
516// DescribeYourselfTo
517
518void
520{
521 scene.AddSolid (*this);
522}
523
524//////////////////////////////////////////////////////////////////////////
525//
526// CreatePolyhedron
527
530{
531 if (fExternalBoolProcessor == nullptr)
532 {
533 HepPolyhedronProcessor processor;
534 // Stack components and components of components recursively
535 // See G4BooleanSolid::StackPolyhedron
536 G4Polyhedron* top = StackPolyhedron(processor, this);
537 auto result = new G4Polyhedron(*top);
538 if (processor.execute(*result))
539 {
540 return result;
541 }
542 return nullptr;
543 }
544 else
545 {
546 return fExternalBoolProcessor->Process(this);
547 }
548}
549
550//////////////////////////////////////////////////////////////////////////
551//
552// GetCubicVolume
553
555{
556 if( fCubicVolume >= 0. )
557 {
558 return fCubicVolume;
559 }
560 G4RecursiveAutoLock l(&unionMutex);
561 G4ThreeVector bminA, bmaxA, bminB, bmaxB;
562 fPtrSolidA->BoundingLimits(bminA, bmaxA);
563 fPtrSolidB->BoundingLimits(bminB, bmaxB);
564 G4bool noIntersection =
565 bminA.x() >= bmaxB.x() || bminA.y() >= bmaxB.y() || bminA.z() >= bmaxB.z() ||
566 bminB.x() >= bmaxA.x() || bminB.y() >= bmaxA.y() || bminB.z() >= bmaxA.z();
567
568 if (noIntersection)
569 {
570 fCubicVolume = fPtrSolidA->GetCubicVolume() + fPtrSolidB->GetCubicVolume();
571 }
572 else
573 {
574 if (GetNumOfConstituents() > 10)
575 {
577 }
578 else
579 {
580 G4IntersectionSolid intersectVol("Temporary-Intersection-for-Union",
583 intersectVol.SetCubVolEpsilon(GetCubVolEpsilon());
584
585 fCubicVolume = fPtrSolidA->GetCubicVolume() + fPtrSolidB->GetCubicVolume()
586 - intersectVol.GetCubicVolume();
587 }
588 }
589 l.unlock();
590 return fCubicVolume;
591}
G4TemplateAutoLock< G4RecursiveMutex > G4RecursiveAutoLock
const G4double kCarTolerance
@ JustWarning
@ FatalException
void G4Exception(const char *originOfException, const char *exceptionCode, G4ExceptionSeverity severity, const char *description)
CLHEP::HepRotation G4RotationMatrix
#define G4MUTEX_INITIALIZER
std::recursive_mutex G4RecursiveMutex
CLHEP::Hep3Vector G4ThreeVector
HepGeom::Transform3D G4Transform3D
double G4double
Definition G4Types.hh:83
bool G4bool
Definition G4Types.hh:86
int G4int
Definition G4Types.hh:85
G4String G4GeometryType
Definition G4VSolid.hh:70
G4GLOB_DLL std::ostream G4cerr
#define G4endl
Definition G4ios.hh:67
G4GLOB_DLL std::ostream G4cout
double z() const
double x() const
double y() const
void set(double x, double y, double z)
G4AffineTransform is a class for geometric affine transformations. It supports efficient arbitrary ro...
G4VSolid * fPtrSolidA
G4BooleanSolid & operator=(const G4BooleanSolid &rhs)
void SetCubVolEpsilon(G4double ep)
G4VSolid * fPtrSolidB
G4double GetCubVolEpsilon() const
G4double GetCubicVolume() override
G4BooleanSolid(const G4String &pName, G4VSolid *pSolidA, G4VSolid *pSolidB)
G4int GetCubVolStatistics() const
G4int GetNumOfConstituents() const override
static G4VBooleanProcessor * fExternalBoolProcessor
G4Polyhedron * StackPolyhedron(HepPolyhedronProcessor &, const G4VSolid *) const
void SetCubVolStatistics(G4int st)
G4double GetRadialTolerance() const
static G4GeometryTolerance * GetInstance()
G4IntersectionSolid is a solid describing the Boolean intersection of two solids.
G4UnionSolid(const G4String &pName, G4VSolid *pSolidA, G4VSolid *pSolidB)
void DescribeYourselfTo(G4VGraphicsScene &scene) const override
G4GeometryType GetEntityType() const override
G4ThreeVector SurfaceNormal(const G4ThreeVector &p) const override
G4UnionSolid & operator=(const G4UnionSolid &rhs)
EInside Inside(const G4ThreeVector &p) const override
G4double DistanceToOut(const G4ThreeVector &p, const G4ThreeVector &v, const G4bool calcNorm=false, G4bool *validNorm=nullptr, G4ThreeVector *n=nullptr) const override
G4double DistanceToIn(const G4ThreeVector &p, const G4ThreeVector &v) const override
G4double GetCubicVolume() final
G4Polyhedron * CreatePolyhedron() const override
G4bool CalculateExtent(const EAxis pAxis, const G4VoxelLimits &pVoxelLimit, const G4AffineTransform &pTransform, G4double &pMin, G4double &pMax) const override
G4VSolid * Clone() const override
void ComputeDimensions(G4VPVParameterisation *p, const G4int n, const G4VPhysicalVolume *pRep) override
void BoundingLimits(G4ThreeVector &pMin, G4ThreeVector &pMax) const override
virtual void AddSolid(const G4Box &)=0
G4VPVParameterisation ia an abstract base class for Parameterisation, able to compute the transformat...
G4VPhysicalVolume is an abstract base class for the representation of a positioned volume....
G4String GetName() const
G4VSolid(const G4String &name)
Definition G4VSolid.cc:59
void DumpInfo() const
G4double kCarTolerance
Definition G4VSolid.hh:418
G4VoxelLimits represents limitation/restrictions of space, where restrictions are only made perpendic...
bool execute(HepPolyhedron &)
EAxis
Definition geomdefs.hh:54
EInside
Definition geomdefs.hh:67
@ kInside
Definition geomdefs.hh:70
@ kOutside
Definition geomdefs.hh:68
@ kSurface
Definition geomdefs.hh:69