Geant4 Cross Reference

Cross-Referencing   Geant4
Geant4/geometry/solids/Boolean/src/G4SubtractionSolid.cc

Version: [ ReleaseNotes ] [ 1.0 ] [ 1.1 ] [ 2.0 ] [ 3.0 ] [ 3.1 ] [ 3.2 ] [ 4.0 ] [ 4.0.p1 ] [ 4.0.p2 ] [ 4.1 ] [ 4.1.p1 ] [ 5.0 ] [ 5.0.p1 ] [ 5.1 ] [ 5.1.p1 ] [ 5.2 ] [ 5.2.p1 ] [ 5.2.p2 ] [ 6.0 ] [ 6.0.p1 ] [ 6.1 ] [ 6.2 ] [ 6.2.p1 ] [ 6.2.p2 ] [ 7.0 ] [ 7.0.p1 ] [ 7.1 ] [ 7.1.p1 ] [ 8.0 ] [ 8.0.p1 ] [ 8.1 ] [ 8.1.p1 ] [ 8.1.p2 ] [ 8.2 ] [ 8.2.p1 ] [ 8.3 ] [ 8.3.p1 ] [ 8.3.p2 ] [ 9.0 ] [ 9.0.p1 ] [ 9.0.p2 ] [ 9.1 ] [ 9.1.p1 ] [ 9.1.p2 ] [ 9.1.p3 ] [ 9.2 ] [ 9.2.p1 ] [ 9.2.p2 ] [ 9.2.p3 ] [ 9.2.p4 ] [ 9.3 ] [ 9.3.p1 ] [ 9.3.p2 ] [ 9.4 ] [ 9.4.p1 ] [ 9.4.p2 ] [ 9.4.p3 ] [ 9.4.p4 ] [ 9.5 ] [ 9.5.p1 ] [ 9.5.p2 ] [ 9.6 ] [ 9.6.p1 ] [ 9.6.p2 ] [ 9.6.p3 ] [ 9.6.p4 ] [ 10.0 ] [ 10.0.p1 ] [ 10.0.p2 ] [ 10.0.p3 ] [ 10.0.p4 ] [ 10.1 ] [ 10.1.p1 ] [ 10.1.p2 ] [ 10.1.p3 ] [ 10.2 ] [ 10.2.p1 ] [ 10.2.p2 ] [ 10.2.p3 ] [ 10.3 ] [ 10.3.p1 ] [ 10.3.p2 ] [ 10.3.p3 ] [ 10.4 ] [ 10.4.p1 ] [ 10.4.p2 ] [ 10.4.p3 ] [ 10.5 ] [ 10.5.p1 ] [ 10.6 ] [ 10.6.p1 ] [ 10.6.p2 ] [ 10.6.p3 ] [ 10.7 ] [ 10.7.p1 ] [ 10.7.p2 ] [ 10.7.p3 ] [ 10.7.p4 ] [ 11.0 ] [ 11.0.p1 ] [ 11.0.p2 ] [ 11.0.p3, ] [ 11.0.p4 ] [ 11.1 ] [ 11.1.1 ] [ 11.1.2 ] [ 11.1.3 ] [ 11.2 ] [ 11.2.1 ] [ 11.2.2 ] [ 11.3.0 ]

Diff markup

Differences between /geometry/solids/Boolean/src/G4SubtractionSolid.cc (Version 11.3.0) and /geometry/solids/Boolean/src/G4SubtractionSolid.cc (Version 9.4.p1)


  1 //                                                  1 //
  2 // *******************************************      2 // ********************************************************************
  3 // * License and Disclaimer                         3 // * License and Disclaimer                                           *
  4 // *                                                4 // *                                                                  *
  5 // * The  Geant4 software  is  copyright of th      5 // * The  Geant4 software  is  copyright of the Copyright Holders  of *
  6 // * the Geant4 Collaboration.  It is provided      6 // * the Geant4 Collaboration.  It is provided  under  the terms  and *
  7 // * conditions of the Geant4 Software License      7 // * conditions of the Geant4 Software License,  included in the file *
  8 // * LICENSE and available at  http://cern.ch/      8 // * LICENSE and available at  http://cern.ch/geant4/license .  These *
  9 // * include a list of copyright holders.           9 // * include a list of copyright holders.                             *
 10 // *                                               10 // *                                                                  *
 11 // * Neither the authors of this software syst     11 // * Neither the authors of this software system, nor their employing *
 12 // * institutes,nor the agencies providing fin     12 // * institutes,nor the agencies providing financial support for this *
 13 // * work  make  any representation or  warran     13 // * work  make  any representation or  warranty, express or implied, *
 14 // * regarding  this  software system or assum     14 // * regarding  this  software system or assume any liability for its *
 15 // * use.  Please see the license in the file      15 // * use.  Please see the license in the file  LICENSE  and URL above *
 16 // * for the full disclaimer and the limitatio     16 // * for the full disclaimer and the limitation of liability.         *
 17 // *                                               17 // *                                                                  *
 18 // * This  code  implementation is the result      18 // * This  code  implementation is the result of  the  scientific and *
 19 // * technical work of the GEANT4 collaboratio     19 // * technical work of the GEANT4 collaboration.                      *
 20 // * By using,  copying,  modifying or  distri     20 // * By using,  copying,  modifying or  distributing the software (or *
 21 // * any work based  on the software)  you  ag     21 // * any work based  on the software)  you  agree  to acknowledge its *
 22 // * use  in  resulting  scientific  publicati     22 // * use  in  resulting  scientific  publications,  and indicate your *
 23 // * acceptance of all terms of the Geant4 Sof     23 // * acceptance of all terms of the Geant4 Software license.          *
 24 // *******************************************     24 // ********************************************************************
 25 //                                                 25 //
                                                   >>  26 //
                                                   >>  27 // $Id: G4SubtractionSolid.cc,v 1.35 2010-10-20 07:31:39 gcosmo Exp $
                                                   >>  28 // GEANT4 tag $Name: geant4-09-04-patch-01 $
                                                   >>  29 //
 26 // Implementation of methods for the class G4I     30 // Implementation of methods for the class G4IntersectionSolid
 27 //                                                 31 //
 28 // 22.07.11 T.Nikitina: added detection of inf <<  32 // History:
 29 // 19.10.98 V.Grichine: new algorithm of Dista <<  33 //
 30 // 14.10.98 V.Grichine: implementation of the      34 // 14.10.98 V.Grichine: implementation of the first version 
                                                   >>  35 // 19.10.98 V.Grichine: new algorithm of DistanceToIn(p,v)
                                                   >>  36 // 02.08.99 V.Grichine: bugs fixed in DistanceToOut(p,v,...)
                                                   >>  37 //                      while -> do-while & surfaceA limitations
                                                   >>  38 // 13.09.00 V.Grichine: bug fixed in SurfaceNormal(p), p can be inside
                                                   >>  39 //
 31 // -------------------------------------------     40 // --------------------------------------------------------------------
 32                                                    41 
 33 #include "G4SubtractionSolid.hh"                   42 #include "G4SubtractionSolid.hh"
 34                                                    43 
 35 #include "G4SystemOfUnits.hh"                  << 
 36 #include "G4VoxelLimits.hh"                        44 #include "G4VoxelLimits.hh"
 37 #include "G4VPVParameterisation.hh"                45 #include "G4VPVParameterisation.hh"
 38 #include "G4GeometryTolerance.hh"                  46 #include "G4GeometryTolerance.hh"
 39                                                    47 
 40 #include "G4VGraphicsScene.hh"                     48 #include "G4VGraphicsScene.hh"
 41 #include "G4Polyhedron.hh"                         49 #include "G4Polyhedron.hh"
 42 #include "G4PolyhedronArbitrary.hh"            << 
 43 #include "HepPolyhedronProcessor.h"                50 #include "HepPolyhedronProcessor.h"
 44                                                <<  51 #include "G4NURBS.hh"
 45 #include "G4IntersectionSolid.hh"              <<  52 // #include "G4NURBSbox.hh"
 46                                                    53 
 47 #include <sstream>                                 54 #include <sstream>
 48                                                    55 
 49 ////////////////////////////////////////////// <<  56 ///////////////////////////////////////////////////////////////////
 50 //                                                 57 //
 51 // Transfer all data members to G4BooleanSolid     58 // Transfer all data members to G4BooleanSolid which is responsible
 52 // for them. pName will be in turn sent to G4V     59 // for them. pName will be in turn sent to G4VSolid
 53                                                    60 
 54 G4SubtractionSolid::G4SubtractionSolid( const      61 G4SubtractionSolid::G4SubtractionSolid( const G4String& pName,
 55                                                    62                                               G4VSolid* pSolidA ,
 56                                                    63                                               G4VSolid* pSolidB   )
 57   : G4BooleanSolid(pName,pSolidA,pSolidB)          64   : G4BooleanSolid(pName,pSolidA,pSolidB)
 58 {                                                  65 {
 59 }                                                  66 }
 60                                                    67 
 61 ////////////////////////////////////////////// <<  68 ///////////////////////////////////////////////////////////////
 62 //                                                 69 //
 63 // Constructor                                     70 // Constructor
 64                                                    71  
 65 G4SubtractionSolid::G4SubtractionSolid( const      72 G4SubtractionSolid::G4SubtractionSolid( const G4String& pName,
 66                                                    73                                               G4VSolid* pSolidA ,
 67                                                    74                                               G4VSolid* pSolidB ,
 68                                                    75                                               G4RotationMatrix* rotMatrix,
 69                                         const      76                                         const G4ThreeVector& transVector )
 70   : G4BooleanSolid(pName,pSolidA,pSolidB,rotMa     77   : G4BooleanSolid(pName,pSolidA,pSolidB,rotMatrix,transVector)
 71 {                                                  78 {
 72 }                                                  79 } 
 73                                                    80 
 74 ////////////////////////////////////////////// <<  81 ///////////////////////////////////////////////////////////////
 75 //                                                 82 //
 76 // Constructor                                     83 // Constructor
 77                                                    84 
 78 G4SubtractionSolid::G4SubtractionSolid( const      85 G4SubtractionSolid::G4SubtractionSolid( const G4String& pName,
 79                                                    86                                               G4VSolid* pSolidA ,
 80                                                    87                                               G4VSolid* pSolidB ,
 81                                         const      88                                         const G4Transform3D& transform )
 82   : G4BooleanSolid(pName,pSolidA,pSolidB,trans     89   : G4BooleanSolid(pName,pSolidA,pSolidB,transform)
 83 {                                                  90 {
 84 }                                                  91 }
 85                                                    92 
 86 ////////////////////////////////////////////// <<  93 //////////////////////////////////////////////////////////////////
 87 //                                                 94 //
 88 // Fake default constructor - sets only member     95 // Fake default constructor - sets only member data and allocates memory
 89 //                            for usage restri     96 //                            for usage restricted to object persistency.
 90                                                    97 
 91 G4SubtractionSolid::G4SubtractionSolid( __void     98 G4SubtractionSolid::G4SubtractionSolid( __void__& a )
 92   : G4BooleanSolid(a)                              99   : G4BooleanSolid(a)
 93 {                                                 100 {
 94 }                                                 101 }
 95                                                   102 
 96 ////////////////////////////////////////////// << 103 ///////////////////////////////////////////////////////////////
 97 //                                                104 //
 98 // Destructor                                     105 // Destructor
 99                                                   106 
100 G4SubtractionSolid::~G4SubtractionSolid() = de << 107 G4SubtractionSolid::~G4SubtractionSolid()
                                                   >> 108 {
                                                   >> 109 }
101                                                   110 
102 ////////////////////////////////////////////// << 111 ///////////////////////////////////////////////////////////////
103 //                                                112 //
104 // Copy constructor                               113 // Copy constructor
105                                                   114 
106 G4SubtractionSolid::G4SubtractionSolid(const G << 115 G4SubtractionSolid::G4SubtractionSolid(const G4SubtractionSolid& rhs)
                                                   >> 116   : G4BooleanSolid (rhs)
                                                   >> 117 {
                                                   >> 118 }
107                                                   119 
108 ////////////////////////////////////////////// << 120 ///////////////////////////////////////////////////////////////
109 //                                                121 //
110 // Assignment operator                            122 // Assignment operator
111                                                   123 
112 G4SubtractionSolid&                               124 G4SubtractionSolid&
113 G4SubtractionSolid::operator = (const G4Subtra    125 G4SubtractionSolid::operator = (const G4SubtractionSolid& rhs) 
114 {                                                 126 {
115   // Check assignment to self                     127   // Check assignment to self
116   //                                              128   //
117   if (this == &rhs)  { return *this; }            129   if (this == &rhs)  { return *this; }
118                                                   130 
119   // Copy base class data                         131   // Copy base class data
120   //                                              132   //
121   G4BooleanSolid::operator=(rhs);                 133   G4BooleanSolid::operator=(rhs);
122                                                   134 
123   return *this;                                   135   return *this;
124 }                                                 136 }  
125                                                   137 
126 ////////////////////////////////////////////// << 138 ///////////////////////////////////////////////////////////////
127 //                                                139 //
128 // Get bounding box                            << 140 // CalculateExtent
129                                                << 
130 void                                           << 
131 G4SubtractionSolid::BoundingLimits(G4ThreeVect << 
132                                    G4ThreeVect << 
133 {                                              << 
134   // Since it is unclear how the shape of the  << 
135   // after subtraction, just return its origin << 
136   //                                           << 
137   fPtrSolidA->BoundingLimits(pMin,pMax);       << 
138                                                << 
139   // Check correctness of the bounding box     << 
140   //                                           << 
141   if (pMin.x() >= pMax.x() || pMin.y() >= pMax << 
142   {                                            << 
143     std::ostringstream message;                << 
144     message << "Bad bounding box (min >= max)  << 
145             << GetName() << " !"               << 
146             << "\npMin = " << pMin             << 
147             << "\npMax = " << pMax;            << 
148     G4Exception("G4SubtractionSolid::BoundingL << 
149                 JustWarning, message);         << 
150     DumpInfo();                                << 
151   }                                            << 
152 }                                              << 
153                                                << 
154 ////////////////////////////////////////////// << 
155 //                                             << 
156 // Calculate extent under transform and specif << 
157                                                   141      
158 G4bool                                            142 G4bool 
159 G4SubtractionSolid::CalculateExtent( const EAx    143 G4SubtractionSolid::CalculateExtent( const EAxis pAxis,
160                                      const G4V    144                                      const G4VoxelLimits& pVoxelLimit,
161                                      const G4A    145                                      const G4AffineTransform& pTransform,
162                                            G4d    146                                            G4double& pMin,
163                                            G4d    147                                            G4double& pMax ) const 
164 {                                                 148 {
165   // Since we cannot be sure how much the seco    149   // Since we cannot be sure how much the second solid subtracts 
166   // from the first, we must use the first sol << 150   // from the first,    we must use the first solid's extent!
167                                                   151 
168   return fPtrSolidA->CalculateExtent( pAxis, p    152   return fPtrSolidA->CalculateExtent( pAxis, pVoxelLimit, 
169                                       pTransfo    153                                       pTransform, pMin, pMax );
170 }                                                 154 }
171                                                   155  
172 ////////////////////////////////////////////// << 156 /////////////////////////////////////////////////////
173 //                                                157 //
174 // Touching ? Empty subtraction ?                 158 // Touching ? Empty subtraction ?
175                                                   159 
176 EInside G4SubtractionSolid::Inside( const G4Th    160 EInside G4SubtractionSolid::Inside( const G4ThreeVector& p ) const
177 {                                                 161 {
178   EInside positionA = fPtrSolidA->Inside(p);      162   EInside positionA = fPtrSolidA->Inside(p);
179   if (positionA == kOutside) return positionA; << 163   if (positionA == kOutside) return kOutside;
180                                                   164 
181   EInside positionB = fPtrSolidB->Inside(p);      165   EInside positionB = fPtrSolidB->Inside(p);
182   if (positionB == kOutside) return positionA; << 166   
183                                                << 167   if(positionA == kInside && positionB == kOutside)
184   if (positionB == kInside) return kOutside;   << 168   {
185   if (positionA == kInside) return kSurface; / << 169     return kInside ;
186                                                << 170   }
187   // Point is on both surfaces                 << 171   else
188   //                                           << 172   {
189   static const G4double rtol = 1000*kCarTolera << 173     if(( positionA == kInside && positionB == kSurface) ||
190                                                << 174        ( positionB == kOutside && positionA == kSurface) ||
191   return ((fPtrSolidA->SurfaceNormal(p) -      << 175        ( positionA == kSurface && positionB == kSurface &&
192            fPtrSolidB->SurfaceNormal(p)).mag2( << 176          ( fPtrSolidA->SurfaceNormal(p) - 
                                                   >> 177            fPtrSolidB->SurfaceNormal(p) ).mag2() > 
                                                   >> 178             1000*G4GeometryTolerance::GetInstance()->GetRadialTolerance() ) )
                                                   >> 179     {
                                                   >> 180       return kSurface;
                                                   >> 181     }
                                                   >> 182     else
                                                   >> 183     {
                                                   >> 184       return kOutside;
                                                   >> 185     }
                                                   >> 186   }
193 }                                                 187 }
194                                                   188 
195 ////////////////////////////////////////////// << 189 //////////////////////////////////////////////////////////////
196 //                                                190 //
197 // SurfaceNormal                                  191 // SurfaceNormal
198                                                   192 
199 G4ThreeVector                                     193 G4ThreeVector 
200 G4SubtractionSolid::SurfaceNormal( const G4Thr    194 G4SubtractionSolid::SurfaceNormal( const G4ThreeVector& p ) const 
201 {                                                 195 {
202   G4ThreeVector normal;                           196   G4ThreeVector normal;
203                                                << 197   if( Inside(p) == kOutside )
204   EInside InsideA = fPtrSolidA->Inside(p);     << 
205   EInside InsideB = fPtrSolidB->Inside(p);     << 
206                                                << 
207   if( InsideA == kOutside )                    << 
208   {                                               198   {
209 #ifdef G4BOOLDEBUG                                199 #ifdef G4BOOLDEBUG
210     G4cout << "WARNING - Invalid call [1] in "    200     G4cout << "WARNING - Invalid call [1] in "
211            << "G4SubtractionSolid::SurfaceNorm    201            << "G4SubtractionSolid::SurfaceNormal(p)" << G4endl
212            << "  Point p is outside !" << G4en << 202            << "  Point p is inside !" << G4endl;
213     G4cout << "          p = " << p << G4endl;    203     G4cout << "          p = " << p << G4endl;
214     G4cerr << "WARNING - Invalid call [1] in "    204     G4cerr << "WARNING - Invalid call [1] in "
215            << "G4SubtractionSolid::SurfaceNorm    205            << "G4SubtractionSolid::SurfaceNormal(p)" << G4endl
216            << "  Point p is outside !" << G4en << 206            << "  Point p is inside !" << G4endl;
217     G4cerr << "          p = " << p << G4endl;    207     G4cerr << "          p = " << p << G4endl;
218 #endif                                            208 #endif
219     normal = fPtrSolidA->SurfaceNormal(p) ;    << 
220   }                                               209   }
221   else if( InsideA == kSurface &&              << 210   else
222            InsideB != kInside      )           << 211   { 
223   {                                            << 212     if( fPtrSolidA->Inside(p) == kSurface && 
224     normal = fPtrSolidA->SurfaceNormal(p) ;    << 213         fPtrSolidB->Inside(p) != kInside      ) 
225   }                                            << 
226   else if( InsideA == kInside &&               << 
227            InsideB != kOutside    )            << 
228   {                                            << 
229     normal = -fPtrSolidB->SurfaceNormal(p) ;   << 
230   }                                            << 
231   else                                         << 
232   {                                            << 
233     if ( fPtrSolidA->DistanceToOut(p) <= fPtrS << 
234     {                                             214     {
235       normal = fPtrSolidA->SurfaceNormal(p) ;     215       normal = fPtrSolidA->SurfaceNormal(p) ;
236     }                                             216     }
237     else                                       << 217     else if( fPtrSolidA->Inside(p) == kInside && 
                                                   >> 218              fPtrSolidB->Inside(p) != kOutside    )
238     {                                             219     {
239       normal = -fPtrSolidB->SurfaceNormal(p) ;    220       normal = -fPtrSolidB->SurfaceNormal(p) ;
240     }                                             221     }
241 #ifdef G4BOOLDEBUG                             << 222     else 
242     if(Inside(p) == kInside)                   << 
243     {                                             223     {
244       G4cout << "WARNING - Invalid call [2] in << 224       if ( fPtrSolidA->DistanceToOut(p) <= fPtrSolidB->DistanceToIn(p) )
                                                   >> 225       {
                                                   >> 226         normal = fPtrSolidA->SurfaceNormal(p) ;
                                                   >> 227       }
                                                   >> 228       else
                                                   >> 229       {
                                                   >> 230         normal = -fPtrSolidB->SurfaceNormal(p) ;
                                                   >> 231       }
                                                   >> 232 #ifdef G4BOOLDEBUG
                                                   >> 233       if(Inside(p) == kInside)
                                                   >> 234       {
                                                   >> 235         G4cout << "WARNING - Invalid call [2] in "
245              << "G4SubtractionSolid::SurfaceNo    236              << "G4SubtractionSolid::SurfaceNormal(p)" << G4endl
246              << "  Point p is inside !" << G4e    237              << "  Point p is inside !" << G4endl;
247       G4cout << "          p = " << p << G4end << 238         G4cout << "          p = " << p << G4endl;
248       G4cerr << "WARNING - Invalid call [2] in << 239         G4cerr << "WARNING - Invalid call [2] in "
249              << "G4SubtractionSolid::SurfaceNo    240              << "G4SubtractionSolid::SurfaceNormal(p)" << G4endl
250              << "  Point p is inside !" << G4e    241              << "  Point p is inside !" << G4endl;
251       G4cerr << "          p = " << p << G4end << 242         G4cerr << "          p = " << p << G4endl;
252     }                                          << 243       }
253 #endif                                            244 #endif
                                                   >> 245     }
254   }                                               246   }
255   return normal;                                  247   return normal;
256 }                                                 248 }
257                                                   249 
258 ////////////////////////////////////////////// << 250 /////////////////////////////////////////////////////////////
259 //                                                251 //
260 // The same algorithm as in DistanceToIn(p)       252 // The same algorithm as in DistanceToIn(p)
261                                                   253 
262 G4double                                          254 G4double 
263 G4SubtractionSolid::DistanceToIn( const G4Thre << 255 G4SubtractionSolid::DistanceToIn(  const G4ThreeVector& p,
264                                   const G4Thre << 256                                    const G4ThreeVector& v  ) const 
265 {                                                 257 {
266   G4double dist = 0.0, dist2 = 0.0, disTmp = 0 << 258   G4double dist = 0.0,disTmp = 0.0 ;
267                                                   259     
268 #ifdef G4BOOLDEBUG                                260 #ifdef G4BOOLDEBUG
269   if( Inside(p) == kInside )                      261   if( Inside(p) == kInside )
270   {                                               262   {
271     G4cout << "WARNING - Invalid call in "        263     G4cout << "WARNING - Invalid call in "
272            << "G4SubtractionSolid::DistanceToI    264            << "G4SubtractionSolid::DistanceToIn(p,v)" << G4endl
273            << "  Point p is inside !" << G4end    265            << "  Point p is inside !" << G4endl;
274     G4cout << "          p = " << p << G4endl;    266     G4cout << "          p = " << p << G4endl;
275     G4cout << "          v = " << v << G4endl;    267     G4cout << "          v = " << v << G4endl;
276     G4cerr << "WARNING - Invalid call in "        268     G4cerr << "WARNING - Invalid call in "
277            << "G4SubtractionSolid::DistanceToI    269            << "G4SubtractionSolid::DistanceToIn(p,v)" << G4endl
278            << "  Point p is inside !" << G4end    270            << "  Point p is inside !" << G4endl;
279     G4cerr << "          p = " << p << G4endl;    271     G4cerr << "          p = " << p << G4endl;
280     G4cerr << "          v = " << v << G4endl;    272     G4cerr << "          v = " << v << G4endl;
281   }                                               273   }
282 #endif                                            274 #endif
283                                                   275 
284     // if( // ( fPtrSolidA->Inside(p) != kOuts    276     // if( // ( fPtrSolidA->Inside(p) != kOutside) &&  // case1:p in both A&B 
285     if ( fPtrSolidB->Inside(p) != kOutside )      277     if ( fPtrSolidB->Inside(p) != kOutside )   // start: out of B
286     {                                             278     {
287       dist = fPtrSolidB->DistanceToOut(p,v) ;     279       dist = fPtrSolidB->DistanceToOut(p,v) ; // ,calcNorm,validNorm,n) ;
288                                                   280       
289       if( fPtrSolidA->Inside(p+dist*v) != kIns    281       if( fPtrSolidA->Inside(p+dist*v) != kInside )
290       {                                           282       {
291         G4int count1=0;                        << 283         do
292         do   // Loop checking, 13.08.2015, G.C << 
293         {                                         284         {
294           disTmp = fPtrSolidA->DistanceToIn(p+    285           disTmp = fPtrSolidA->DistanceToIn(p+dist*v,v) ;
295                                                   286 
296           if(disTmp == kInfinity)                 287           if(disTmp == kInfinity)
297           {                                       288           {  
298             return kInfinity ;                    289             return kInfinity ;
299           }                                       290           }
300           dist += disTmp ;                        291           dist += disTmp ;
301                                                   292 
302           if( Inside(p+dist*v) == kOutside )      293           if( Inside(p+dist*v) == kOutside )
303           {                                       294           {
304             disTmp = fPtrSolidB->DistanceToOut << 295             disTmp = fPtrSolidB->DistanceToOut(p+dist*v,v) ; 
305             dist2 = dist+disTmp;               << 296             dist += disTmp ;
306             if (dist == dist2)  { return dist; << 297           }         
307             dist = dist2 ;                     << 
308             ++count1;                          << 
309             if( count1 > 1000 )  // Infinite l << 
310             {                                  << 
311               G4String nameB = fPtrSolidB->Get << 
312               if(fPtrSolidB->GetEntityType()== << 
313               {                                << 
314                 nameB = (dynamic_cast<G4Displa << 
315                         ->GetConstituentMovedS << 
316               }                                << 
317               std::ostringstream message;      << 
318               message << "Illegal condition ca << 
319                       << fPtrSolidA->GetName() << 
320               message.precision(16);           << 
321               message << "Looping detected in  << 
322                       << ", from original poin << 
323                       << " and direction " <<  << 
324                       << "Computed candidate d << 
325               message.precision(6);            << 
326               DumpInfo();                      << 
327               G4Exception("G4SubtractionSolid: << 
328                           "GeomSolids1001", Ju << 
329                           "Returning candidate << 
330               return dist;                     << 
331             }                                  << 
332           }                                    << 
333         }                                         298         }
334         while( Inside(p+dist*v) == kOutside )     299         while( Inside(p+dist*v) == kOutside ) ;
335       }                                           300       }
336     }                                             301     }
337     else // p outside A, start in A               302     else // p outside A, start in A
338     {                                             303     {
339       dist = fPtrSolidA->DistanceToIn(p,v) ;      304       dist = fPtrSolidA->DistanceToIn(p,v) ;
340                                                   305 
341       if( dist == kInfinity ) // past A, hence    306       if( dist == kInfinity ) // past A, hence past A\B
342       {                                           307       {
343         return kInfinity ;                        308         return kInfinity ;
344       }                                           309       }
345       else                                        310       else
346       {                                           311       {
347         G4int count2=0;                        << 
348         while( Inside(p+dist*v) == kOutside )     312         while( Inside(p+dist*v) == kOutside )  // pushing loop
349         {                                         313         {
350           disTmp = fPtrSolidB->DistanceToOut(p    314           disTmp = fPtrSolidB->DistanceToOut(p+dist*v,v) ;
351           dist += disTmp ;                        315           dist += disTmp ;
352                                                   316 
353           if( Inside(p+dist*v) == kOutside )      317           if( Inside(p+dist*v) == kOutside )
354           {                                       318           { 
355             disTmp = fPtrSolidA->DistanceToIn(    319             disTmp = fPtrSolidA->DistanceToIn(p+dist*v,v) ;
356                                                   320 
357             if(disTmp == kInfinity) // past A,    321             if(disTmp == kInfinity) // past A, hence past A\B
358             {                                     322             {  
359               return kInfinity ;                  323               return kInfinity ;
360             }                                     324             }                 
361             dist2 = dist+disTmp;               << 325             dist += disTmp ;
362             if (dist == dist2)  { return dist; << 
363             dist = dist2 ;                     << 
364             ++count2;                          << 
365             if( count2 > 1000 )  // Infinite l << 
366             {                                  << 
367               G4String nameB = fPtrSolidB->Get << 
368               if(fPtrSolidB->GetEntityType()== << 
369               {                                << 
370                 nameB = (dynamic_cast<G4Displa << 
371                         ->GetConstituentMovedS << 
372               }                                << 
373               std::ostringstream message;      << 
374               message << "Illegal condition ca << 
375                       << fPtrSolidA->GetName() << 
376               message.precision(16);           << 
377               message << "Looping detected in  << 
378                       << ", from original poin << 
379                       << " and direction " <<  << 
380                       << "Computed candidate d << 
381               message.precision(6);            << 
382               DumpInfo();                      << 
383               G4Exception("G4SubtractionSolid: << 
384                           "GeomSolids1001", Ju << 
385                           "Returning candidate << 
386               return dist;                     << 
387             }                                  << 
388           }                                       326           }
389         }    // Loop checking, 13.08.2015, G.C << 327         }
390       }                                           328       }
391     }                                             329     }
392                                                   330   
393   return dist ;                                   331   return dist ;
394 }                                                 332 }
395                                                   333 
396 ////////////////////////////////////////////// << 334 ////////////////////////////////////////////////////////
397 //                                                335 //
398 // Approximate nearest distance from the point    336 // Approximate nearest distance from the point p to the intersection of
399 // two solids. It is usually underestimated fr    337 // two solids. It is usually underestimated from the point of view of
400 // isotropic safety                               338 // isotropic safety
401                                                   339 
402 G4double                                          340 G4double 
403 G4SubtractionSolid::DistanceToIn( const G4Thre    341 G4SubtractionSolid::DistanceToIn( const G4ThreeVector& p ) const 
404 {                                                 342 {
405   G4double dist = 0.0;                         << 343   G4double dist=0.0;
406                                                   344 
407 #ifdef G4BOOLDEBUG                                345 #ifdef G4BOOLDEBUG
408   if( Inside(p) == kInside )                      346   if( Inside(p) == kInside )
409   {                                               347   {
410     G4cout << "WARNING - Invalid call in "        348     G4cout << "WARNING - Invalid call in "
411            << "G4SubtractionSolid::DistanceToI    349            << "G4SubtractionSolid::DistanceToIn(p)" << G4endl
412            << "  Point p is inside !" << G4end    350            << "  Point p is inside !" << G4endl;
413     G4cout << "          p = " << p << G4endl;    351     G4cout << "          p = " << p << G4endl;
414     G4cerr << "WARNING - Invalid call in "        352     G4cerr << "WARNING - Invalid call in "
415            << "G4SubtractionSolid::DistanceToI    353            << "G4SubtractionSolid::DistanceToIn(p)" << G4endl
416            << "  Point p is inside !" << G4end    354            << "  Point p is inside !" << G4endl;
417     G4cerr << "          p = " << p << G4endl;    355     G4cerr << "          p = " << p << G4endl;
418   }                                               356   }
419 #endif                                            357 #endif
420                                                   358 
421   if( ( fPtrSolidA->Inside(p) != kOutside) &&     359   if( ( fPtrSolidA->Inside(p) != kOutside) &&   // case 1
422       ( fPtrSolidB->Inside(p) != kOutside)        360       ( fPtrSolidB->Inside(p) != kOutside)    )
423   {                                               361   {
424     dist = fPtrSolidB->DistanceToOut(p);       << 362       dist= fPtrSolidB->DistanceToOut(p)  ;
425   }                                               363   }
426   else                                            364   else
427   {                                               365   {
428     dist = fPtrSolidA->DistanceToIn(p);        << 366       dist= fPtrSolidA->DistanceToIn(p) ; 
429   }                                               367   }
430                                                   368   
431   return dist;                                    369   return dist; 
432 }                                                 370 }
433                                                   371 
434 ////////////////////////////////////////////// << 372 //////////////////////////////////////////////////////////
435 //                                                373 //
436 // The same algorithm as DistanceToOut(p)         374 // The same algorithm as DistanceToOut(p)
437                                                   375 
438 G4double                                          376 G4double 
439 G4SubtractionSolid::DistanceToOut( const G4Thr    377 G4SubtractionSolid::DistanceToOut( const G4ThreeVector& p,
440                                    const G4Thr << 378                  const G4ThreeVector& v,
441                                    const G4boo << 379                  const G4bool calcNorm,
442                                          G4boo << 380                        G4bool *validNorm,
443                                          G4Thr << 381                        G4ThreeVector *n ) const 
444 {                                                 382 {
445 #ifdef G4BOOLDEBUG                                383 #ifdef G4BOOLDEBUG
446     if( Inside(p) == kOutside )                   384     if( Inside(p) == kOutside )
447     {                                             385     {
448       G4cout << "Position:"  << G4endl << G4en    386       G4cout << "Position:"  << G4endl << G4endl;
449       G4cout << "p.x() = "   << p.x()/mm << "     387       G4cout << "p.x() = "   << p.x()/mm << " mm" << G4endl;
450       G4cout << "p.y() = "   << p.y()/mm << "     388       G4cout << "p.y() = "   << p.y()/mm << " mm" << G4endl;
451       G4cout << "p.z() = "   << p.z()/mm << "     389       G4cout << "p.z() = "   << p.z()/mm << " mm" << G4endl << G4endl;
452       G4cout << "Direction:" << G4endl << G4en    390       G4cout << "Direction:" << G4endl << G4endl;
453       G4cout << "v.x() = "   << v.x() << G4end    391       G4cout << "v.x() = "   << v.x() << G4endl;
454       G4cout << "v.y() = "   << v.y() << G4end    392       G4cout << "v.y() = "   << v.y() << G4endl;
455       G4cout << "v.z() = "   << v.z() << G4end    393       G4cout << "v.z() = "   << v.z() << G4endl << G4endl;
456       G4cout << "WARNING - Invalid call in "      394       G4cout << "WARNING - Invalid call in "
457              << "G4SubtractionSolid::DistanceT    395              << "G4SubtractionSolid::DistanceToOut(p,v)" << G4endl
458              << "  Point p is outside !" << G4    396              << "  Point p is outside !" << G4endl;
459       G4cout << "          p = " << p << G4end    397       G4cout << "          p = " << p << G4endl;
460       G4cout << "          v = " << v << G4end    398       G4cout << "          v = " << v << G4endl;
461       G4cerr << "WARNING - Invalid call in "      399       G4cerr << "WARNING - Invalid call in "
462              << "G4SubtractionSolid::DistanceT    400              << "G4SubtractionSolid::DistanceToOut(p,v)" << G4endl
463              << "  Point p is outside !" << G4    401              << "  Point p is outside !" << G4endl;
464       G4cerr << "          p = " << p << G4end    402       G4cerr << "          p = " << p << G4endl;
465       G4cerr << "          v = " << v << G4end    403       G4cerr << "          v = " << v << G4endl;
466     }                                             404     }
467 #endif                                            405 #endif
468                                                   406 
469     G4double distout;                             407     G4double distout;
470     G4double distA = fPtrSolidA->DistanceToOut    408     G4double distA = fPtrSolidA->DistanceToOut(p,v,calcNorm,validNorm,n) ;
471     G4double distB = fPtrSolidB->DistanceToIn(    409     G4double distB = fPtrSolidB->DistanceToIn(p,v) ;
472     if(distB < distA)                             410     if(distB < distA)
473     {                                             411     {
474       if(calcNorm)                                412       if(calcNorm)
475       {                                           413       {
476         *n = -(fPtrSolidB->SurfaceNormal(p+dis    414         *n = -(fPtrSolidB->SurfaceNormal(p+distB*v)) ;
477         *validNorm = false ;                      415         *validNorm = false ;
478       }                                           416       }
479       distout= distB ;                            417       distout= distB ;
480     }                                             418     }
481     else                                          419     else
482     {                                             420     {
483       distout= distA ;                            421       distout= distA ; 
484     }                                             422     } 
485     return distout;                               423     return distout;
486 }                                                 424 }
487                                                   425 
488 ////////////////////////////////////////////// << 426 //////////////////////////////////////////////////////////////
489 //                                                427 //
490 // Inverted algorithm of DistanceToIn(p)          428 // Inverted algorithm of DistanceToIn(p)
491                                                   429 
492 G4double                                          430 G4double 
493 G4SubtractionSolid::DistanceToOut( const G4Thr    431 G4SubtractionSolid::DistanceToOut( const G4ThreeVector& p ) const 
494 {                                                 432 {
495   G4double dist=0.0;                              433   G4double dist=0.0;
496                                                   434 
497   if( Inside(p) == kOutside )                     435   if( Inside(p) == kOutside )
498   {                                               436   { 
499 #ifdef G4BOOLDEBUG                                437 #ifdef G4BOOLDEBUG
500     G4cout << "WARNING - Invalid call in "        438     G4cout << "WARNING - Invalid call in "
501            << "G4SubtractionSolid::DistanceToO    439            << "G4SubtractionSolid::DistanceToOut(p)" << G4endl
502            << "  Point p is outside" << G4endl    440            << "  Point p is outside" << G4endl;
503     G4cout << "          p = " << p << G4endl;    441     G4cout << "          p = " << p << G4endl;
504     G4cerr << "WARNING - Invalid call in "        442     G4cerr << "WARNING - Invalid call in "
505            << "G4SubtractionSolid::DistanceToO    443            << "G4SubtractionSolid::DistanceToOut(p)" << G4endl
506            << "  Point p is outside" << G4endl    444            << "  Point p is outside" << G4endl;
507     G4cerr << "          p = " << p << G4endl;    445     G4cerr << "          p = " << p << G4endl;
508 #endif                                            446 #endif
509   }                                               447   }
510   else                                            448   else
511   {                                               449   {
512      dist= std::min(fPtrSolidA->DistanceToOut(    450      dist= std::min(fPtrSolidA->DistanceToOut(p),
513                       fPtrSolidB->DistanceToIn    451                       fPtrSolidB->DistanceToIn(p) ) ; 
514   }                                               452   }
515   return dist;                                    453   return dist; 
516 }                                                 454 }
517                                                   455 
518 ////////////////////////////////////////////// << 456 //////////////////////////////////////////////////////////////
519 //                                                457 //
520 //                                                458 //
521                                                   459 
522 G4GeometryType G4SubtractionSolid::GetEntityTy    460 G4GeometryType G4SubtractionSolid::GetEntityType() const 
523 {                                                 461 {
524   return {"G4SubtractionSolid"};               << 462   return G4String("G4SubtractionSolid");
525 }                                                 463 }
526                                                   464 
527 //////////////////////////////////////////////    465 //////////////////////////////////////////////////////////////////////////
528 //                                                466 //
529 // Make a clone of the object                     467 // Make a clone of the object
530                                                   468 
531 G4VSolid* G4SubtractionSolid::Clone() const       469 G4VSolid* G4SubtractionSolid::Clone() const
532 {                                                 470 {
533   return new G4SubtractionSolid(*this);           471   return new G4SubtractionSolid(*this);
534 }                                                 472 }
535                                                   473 
536 ////////////////////////////////////////////// << 474 //////////////////////////////////////////////////////////////
                                                   >> 475 //
537 //                                                476 //
538 // ComputeDimensions                           << 
539                                                   477 
540 void                                              478 void 
541 G4SubtractionSolid::ComputeDimensions(       G    479 G4SubtractionSolid::ComputeDimensions(       G4VPVParameterisation*,
542                                        const G    480                                        const G4int,
543                                        const G    481                                        const G4VPhysicalVolume* ) 
544 {                                                 482 {
545 }                                                 483 }
546                                                   484 
547 ////////////////////////////////////////////// << 485 /////////////////////////////////////////////////
548 //                                                486 //
549 // DescribeYourselfTo                          << 487 //                    
550                                                   488 
551 void                                              489 void 
552 G4SubtractionSolid::DescribeYourselfTo ( G4VGr    490 G4SubtractionSolid::DescribeYourselfTo ( G4VGraphicsScene& scene ) const 
553 {                                                 491 {
554   scene.AddSolid (*this);                         492   scene.AddSolid (*this);
555 }                                                 493 }
556                                                   494 
557 ////////////////////////////////////////////// << 495 ////////////////////////////////////////////////////
                                                   >> 496 //
558 //                                                497 //
559 // CreatePolyhedron                            << 
560                                                   498 
561 G4Polyhedron* G4SubtractionSolid::CreatePolyhe << 499 G4Polyhedron* 
                                                   >> 500 G4SubtractionSolid::CreatePolyhedron () const 
562 {                                                 501 {
563   if (fExternalBoolProcessor == nullptr)       << 502   HepPolyhedronProcessor processor;
564   {                                            << 503   // Stack components and components of components recursively
565     HepPolyhedronProcessor processor;          << 504   // See G4BooleanSolid::StackPolyhedron
566     // Stack components and components of comp << 505   G4Polyhedron* top = StackPolyhedron(processor, this);
567     // See G4BooleanSolid::StackPolyhedron     << 506   G4Polyhedron* result = new G4Polyhedron(*top);
568     G4Polyhedron* top = StackPolyhedron(proces << 507   if (processor.execute(*result)) { return result; }
569     auto result = new G4Polyhedron(*top);      << 508   else { return 0; }
570     if (processor.execute(*result))            << 
571     {                                          << 
572       return result;                           << 
573     }                                          << 
574     else                                       << 
575     {                                          << 
576       return nullptr;                          << 
577     }                                          << 
578   }                                            << 
579   else                                         << 
580   {                                            << 
581     return fExternalBoolProcessor->Process(thi << 
582   }                                            << 
583 }                                                 509 }
584                                                   510 
585 ////////////////////////////////////////////// << 511 /////////////////////////////////////////////////////////
586 //                                                512 //
587 // GetCubicVolume                              << 
588 //                                                513 //
589                                                   514 
590 G4double G4SubtractionSolid::GetCubicVolume()  << 515 G4NURBS*      
                                                   >> 516 G4SubtractionSolid::CreateNURBS () const 
591 {                                                 517 {
592   if( fCubicVolume >= 0. )                     << 518   // Take into account boolean operation - see CreatePolyhedron.
593   {                                            << 519   // return new G4NURBSbox (1.0, 1.0, 1.0);
594     return fCubicVolume;                       << 520   return 0;
595   }                                            << 
596   G4ThreeVector bminA, bmaxA, bminB, bmaxB;    << 
597   fPtrSolidA->BoundingLimits(bminA, bmaxA);    << 
598   fPtrSolidB->BoundingLimits(bminB, bmaxB);    << 
599   G4bool noIntersection =                      << 
600      bminA.x() >= bmaxB.x() || bminA.y() >= bm << 
601      bminB.x() >= bmaxA.x() || bminB.y() >= bm << 
602                                                << 
603   if (noIntersection)                          << 
604   {                                            << 
605     fCubicVolume = fPtrSolidA->GetCubicVolume( << 
606   }                                            << 
607   else                                         << 
608   {                                            << 
609     if (GetNumOfConstituents() > 10)           << 
610     {                                          << 
611       fCubicVolume = G4BooleanSolid::GetCubicV << 
612     }                                          << 
613     else                                       << 
614     {                                          << 
615       G4IntersectionSolid intersectVol("Tempor << 
616                                         fPtrSo << 
617       intersectVol.SetCubVolStatistics(GetCubV << 
618       intersectVol.SetCubVolEpsilon(GetCubVolE << 
619                                                << 
620       G4double cubVolumeA = fPtrSolidA->GetCub << 
621       fCubicVolume = cubVolumeA - intersectVol << 
622       if (fCubicVolume < 0.01*cubVolumeA) fCub << 
623     }                                          << 
624   }                                            << 
625   return fCubicVolume;                         << 
626 }                                                 521 }
627                                                   522