Problem 979

Summary: Optimize boolean solids with "short-circuit" test
Product: Geant4 Reporter: Stan Seibert <volsung>
Component: geometry/solidsAssignee: Vladimir.Grichine
Status: RESOLVED FIXED    
Severity: enhancement CC: Gabriele.Cosmo
Priority: P5    
Version: 9.0   
Hardware: All   
OS: All   

Description Stan Seibert 2007-10-22 02:43:40 CEST
The Inside() method for boolean solids G4UnionSolid, G4SubtractionSolid, and G4IntersectionSolid can be sped up if a short circuit test is applied after testing solid A, but before testing solid B.  For example, in a subtraction solid, you can do:

 EInside G4SubtractionSolid::Inside( const G4ThreeVector& p ) const
 {
   EInside positionA = fPtrSolidA->Inside(p);
+  
+  if (positionA == kOutside)
+     return kOutside;
+  
   EInside positionB = fPtrSolidB->Inside(p);
   
This avoids calling Inside() on solid B unless necessary.  Similarly, one can do the same thing to G4UnionSolid:

 EInside G4UnionSolid::Inside( const G4ThreeVector& p ) const
 {
   EInside positionA = fPtrSolidA->Inside(p);
+  
+  if (positionA == kInside)
+    return kInside;
+  
   EInside positionB = fPtrSolidB->Inside(p);

A similar patch could be made to G4IntersectionSolid, though I haven't tried it yet.  These changes improve the performance of complex boolean solid geometries quite a bit.
Comment 1 Vladimir.Grichine 2007-10-23 16:48:39 CEST
These modifications were introduced in subtracted and union solids, while 
it already existed in intersection.