Geant4 Cross Reference |
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.29 2006/11/07 14:05:46 gcosmo Exp $ >> 28 // GEANT4 tag $Name: geant4-08-03-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" << 39 46 40 #include "G4VGraphicsScene.hh" 47 #include "G4VGraphicsScene.hh" 41 #include "G4Polyhedron.hh" 48 #include "G4Polyhedron.hh" 42 #include "G4PolyhedronArbitrary.hh" << 49 #include "G4NURBS.hh" 43 #include "HepPolyhedronProcessor.h" << 50 // #include "G4NURBSbox.hh" 44 << 45 #include "G4IntersectionSolid.hh" << 46 51 47 #include <sstream> 52 #include <sstream> 48 53 49 ////////////////////////////////////////////// << 54 /////////////////////////////////////////////////////////////////// 50 // 55 // 51 // Transfer all data members to G4BooleanSolid 56 // Transfer all data members to G4BooleanSolid which is responsible 52 // for them. pName will be in turn sent to G4V 57 // for them. pName will be in turn sent to G4VSolid 53 58 54 G4SubtractionSolid::G4SubtractionSolid( const 59 G4SubtractionSolid::G4SubtractionSolid( const G4String& pName, 55 60 G4VSolid* pSolidA , 56 61 G4VSolid* pSolidB ) 57 : G4BooleanSolid(pName,pSolidA,pSolidB) 62 : G4BooleanSolid(pName,pSolidA,pSolidB) 58 { 63 { 59 } 64 } 60 65 61 ////////////////////////////////////////////// << 66 /////////////////////////////////////////////////////////////// 62 // 67 // 63 // Constructor 68 // Constructor 64 69 65 G4SubtractionSolid::G4SubtractionSolid( const 70 G4SubtractionSolid::G4SubtractionSolid( const G4String& pName, 66 71 G4VSolid* pSolidA , 67 72 G4VSolid* pSolidB , 68 73 G4RotationMatrix* rotMatrix, 69 const 74 const G4ThreeVector& transVector ) 70 : G4BooleanSolid(pName,pSolidA,pSolidB,rotMa 75 : G4BooleanSolid(pName,pSolidA,pSolidB,rotMatrix,transVector) 71 { 76 { 72 } 77 } 73 78 74 ////////////////////////////////////////////// << 79 /////////////////////////////////////////////////////////////// 75 // 80 // 76 // Constructor 81 // Constructor 77 82 78 G4SubtractionSolid::G4SubtractionSolid( const 83 G4SubtractionSolid::G4SubtractionSolid( const G4String& pName, 79 84 G4VSolid* pSolidA , 80 85 G4VSolid* pSolidB , 81 const 86 const G4Transform3D& transform ) 82 : G4BooleanSolid(pName,pSolidA,pSolidB,trans 87 : G4BooleanSolid(pName,pSolidA,pSolidB,transform) 83 { 88 { 84 } 89 } 85 90 86 ////////////////////////////////////////////// << 91 ////////////////////////////////////////////////////////////////// 87 // 92 // 88 // Fake default constructor - sets only member 93 // Fake default constructor - sets only member data and allocates memory 89 // for usage restri 94 // for usage restricted to object persistency. 90 95 91 G4SubtractionSolid::G4SubtractionSolid( __void 96 G4SubtractionSolid::G4SubtractionSolid( __void__& a ) 92 : G4BooleanSolid(a) 97 : G4BooleanSolid(a) 93 { 98 { 94 } 99 } 95 100 96 ////////////////////////////////////////////// << 101 /////////////////////////////////////////////////////////////// 97 // 102 // 98 // Destructor 103 // Destructor 99 104 100 G4SubtractionSolid::~G4SubtractionSolid() = de << 105 G4SubtractionSolid::~G4SubtractionSolid() 101 << 102 ////////////////////////////////////////////// << 103 // << 104 // Copy constructor << 105 << 106 G4SubtractionSolid::G4SubtractionSolid(const G << 107 << 108 ////////////////////////////////////////////// << 109 // << 110 // Assignment operator << 111 << 112 G4SubtractionSolid& << 113 G4SubtractionSolid::operator = (const G4Subtra << 114 { << 115 // Check assignment to self << 116 // << 117 if (this == &rhs) { return *this; } << 118 << 119 // Copy base class data << 120 // << 121 G4BooleanSolid::operator=(rhs); << 122 << 123 return *this; << 124 } << 125 << 126 ////////////////////////////////////////////// << 127 // << 128 // Get bounding box << 129 << 130 void << 131 G4SubtractionSolid::BoundingLimits(G4ThreeVect << 132 G4ThreeVect << 133 { 106 { 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 } 107 } 153 108 154 ////////////////////////////////////////////// << 109 /////////////////////////////////////////////////////////////// 155 // 110 // 156 // Calculate extent under transform and specif << 111 // CalculateExtent 157 112 158 G4bool 113 G4bool 159 G4SubtractionSolid::CalculateExtent( const EAx 114 G4SubtractionSolid::CalculateExtent( const EAxis pAxis, 160 const G4V 115 const G4VoxelLimits& pVoxelLimit, 161 const G4A 116 const G4AffineTransform& pTransform, 162 G4d 117 G4double& pMin, 163 G4d 118 G4double& pMax ) const 164 { 119 { 165 // Since we cannot be sure how much the seco 120 // Since we cannot be sure how much the second solid subtracts 166 // from the first, we must use the first sol << 121 // from the first, we must use the first solid's extent! 167 122 168 return fPtrSolidA->CalculateExtent( pAxis, p 123 return fPtrSolidA->CalculateExtent( pAxis, pVoxelLimit, 169 pTransfo 124 pTransform, pMin, pMax ); 170 } 125 } 171 126 172 ////////////////////////////////////////////// << 127 ///////////////////////////////////////////////////// 173 // 128 // 174 // Touching ? Empty subtraction ? 129 // Touching ? Empty subtraction ? 175 130 176 EInside G4SubtractionSolid::Inside( const G4Th 131 EInside G4SubtractionSolid::Inside( const G4ThreeVector& p ) const 177 { 132 { 178 EInside positionA = fPtrSolidA->Inside(p); 133 EInside positionA = fPtrSolidA->Inside(p); 179 if (positionA == kOutside) return positionA; << 180 << 181 EInside positionB = fPtrSolidB->Inside(p); 134 EInside positionB = fPtrSolidB->Inside(p); 182 if (positionB == kOutside) return positionA; << 135 183 << 136 if(positionA == kInside && positionB == kOutside) 184 if (positionB == kInside) return kOutside; << 137 { 185 if (positionA == kInside) return kSurface; / << 138 return kInside ; 186 << 139 } 187 // Point is on both surfaces << 140 else 188 // << 141 { 189 static const G4double rtol = 1000*kCarTolera << 142 if(( positionA == kInside && positionB == kSurface) || 190 << 143 ( positionB == kOutside && positionA == kSurface) || 191 return ((fPtrSolidA->SurfaceNormal(p) - << 144 ( positionA == kSurface && positionB == kSurface && 192 fPtrSolidB->SurfaceNormal(p)).mag2( << 145 ( fPtrSolidA->SurfaceNormal(p) - >> 146 fPtrSolidB->SurfaceNormal(p) ).mag2() > >> 147 1000*kRadTolerance ) ) return kSurface; >> 148 else return kOutside; >> 149 } 193 } 150 } 194 151 195 ////////////////////////////////////////////// << 152 ////////////////////////////////////////////////////////////// 196 // 153 // 197 // SurfaceNormal 154 // SurfaceNormal 198 155 199 G4ThreeVector 156 G4ThreeVector 200 G4SubtractionSolid::SurfaceNormal( const G4Thr 157 G4SubtractionSolid::SurfaceNormal( const G4ThreeVector& p ) const 201 { 158 { 202 G4ThreeVector normal; 159 G4ThreeVector normal; 203 << 160 if( Inside(p) == kOutside ) 204 EInside InsideA = fPtrSolidA->Inside(p); << 205 EInside InsideB = fPtrSolidB->Inside(p); << 206 << 207 if( InsideA == kOutside ) << 208 { 161 { 209 #ifdef G4BOOLDEBUG 162 #ifdef G4BOOLDEBUG 210 G4cout << "WARNING - Invalid call [1] in " 163 G4cout << "WARNING - Invalid call [1] in " 211 << "G4SubtractionSolid::SurfaceNorm 164 << "G4SubtractionSolid::SurfaceNormal(p)" << G4endl 212 << " Point p is outside !" << G4en << 165 << " Point p is inside !" << G4endl; 213 G4cout << " p = " << p << G4endl; 166 G4cout << " p = " << p << G4endl; 214 G4cerr << "WARNING - Invalid call [1] in " 167 G4cerr << "WARNING - Invalid call [1] in " 215 << "G4SubtractionSolid::SurfaceNorm 168 << "G4SubtractionSolid::SurfaceNormal(p)" << G4endl 216 << " Point p is outside !" << G4en << 169 << " Point p is inside !" << G4endl; 217 G4cerr << " p = " << p << G4endl; 170 G4cerr << " p = " << p << G4endl; 218 #endif 171 #endif 219 normal = fPtrSolidA->SurfaceNormal(p) ; << 220 } 172 } 221 else if( InsideA == kSurface && << 173 else 222 InsideB != kInside ) << 174 { 223 { << 175 if( fPtrSolidA->Inside(p) == kSurface && 224 normal = fPtrSolidA->SurfaceNormal(p) ; << 176 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 { 177 { 235 normal = fPtrSolidA->SurfaceNormal(p) ; 178 normal = fPtrSolidA->SurfaceNormal(p) ; 236 } 179 } 237 else << 180 else if( fPtrSolidA->Inside(p) == kInside && >> 181 fPtrSolidB->Inside(p) != kOutside ) 238 { 182 { 239 normal = -fPtrSolidB->SurfaceNormal(p) ; 183 normal = -fPtrSolidB->SurfaceNormal(p) ; 240 } 184 } 241 #ifdef G4BOOLDEBUG << 185 else 242 if(Inside(p) == kInside) << 243 { 186 { 244 G4cout << "WARNING - Invalid call [2] in << 187 if ( fPtrSolidA->DistanceToOut(p) <= fPtrSolidB->DistanceToIn(p) ) >> 188 { >> 189 normal = fPtrSolidA->SurfaceNormal(p) ; >> 190 } >> 191 else >> 192 { >> 193 normal = -fPtrSolidB->SurfaceNormal(p) ; >> 194 } >> 195 #ifdef G4BOOLDEBUG >> 196 if(Inside(p) == kInside) >> 197 { >> 198 G4cout << "WARNING - Invalid call [2] in " 245 << "G4SubtractionSolid::SurfaceNo 199 << "G4SubtractionSolid::SurfaceNormal(p)" << G4endl 246 << " Point p is inside !" << G4e 200 << " Point p is inside !" << G4endl; 247 G4cout << " p = " << p << G4end << 201 G4cout << " p = " << p << G4endl; 248 G4cerr << "WARNING - Invalid call [2] in << 202 G4cerr << "WARNING - Invalid call [2] in " 249 << "G4SubtractionSolid::SurfaceNo 203 << "G4SubtractionSolid::SurfaceNormal(p)" << G4endl 250 << " Point p is inside !" << G4e 204 << " Point p is inside !" << G4endl; 251 G4cerr << " p = " << p << G4end << 205 G4cerr << " p = " << p << G4endl; 252 } << 206 } 253 #endif 207 #endif >> 208 } 254 } 209 } 255 return normal; 210 return normal; 256 } 211 } 257 212 258 ////////////////////////////////////////////// << 213 ///////////////////////////////////////////////////////////// 259 // 214 // 260 // The same algorithm as in DistanceToIn(p) 215 // The same algorithm as in DistanceToIn(p) 261 216 262 G4double 217 G4double 263 G4SubtractionSolid::DistanceToIn( const G4Thre << 218 G4SubtractionSolid::DistanceToIn( const G4ThreeVector& p, 264 const G4Thre << 219 const G4ThreeVector& v ) const 265 { 220 { 266 G4double dist = 0.0, dist2 = 0.0, disTmp = 0 << 221 G4double dist = 0.0,disTmp = 0.0 ; 267 222 268 #ifdef G4BOOLDEBUG 223 #ifdef G4BOOLDEBUG 269 if( Inside(p) == kInside ) 224 if( Inside(p) == kInside ) 270 { 225 { 271 G4cout << "WARNING - Invalid call in " 226 G4cout << "WARNING - Invalid call in " 272 << "G4SubtractionSolid::DistanceToI 227 << "G4SubtractionSolid::DistanceToIn(p,v)" << G4endl 273 << " Point p is inside !" << G4end 228 << " Point p is inside !" << G4endl; 274 G4cout << " p = " << p << G4endl; 229 G4cout << " p = " << p << G4endl; 275 G4cout << " v = " << v << G4endl; 230 G4cout << " v = " << v << G4endl; 276 G4cerr << "WARNING - Invalid call in " 231 G4cerr << "WARNING - Invalid call in " 277 << "G4SubtractionSolid::DistanceToI 232 << "G4SubtractionSolid::DistanceToIn(p,v)" << G4endl 278 << " Point p is inside !" << G4end 233 << " Point p is inside !" << G4endl; 279 G4cerr << " p = " << p << G4endl; 234 G4cerr << " p = " << p << G4endl; 280 G4cerr << " v = " << v << G4endl; 235 G4cerr << " v = " << v << G4endl; 281 } 236 } 282 #endif 237 #endif 283 238 284 // if( // ( fPtrSolidA->Inside(p) != kOuts 239 // if( // ( fPtrSolidA->Inside(p) != kOutside) && // case1:p in both A&B 285 if ( fPtrSolidB->Inside(p) != kOutside ) 240 if ( fPtrSolidB->Inside(p) != kOutside ) // start: out of B 286 { 241 { 287 dist = fPtrSolidB->DistanceToOut(p,v) ; 242 dist = fPtrSolidB->DistanceToOut(p,v) ; // ,calcNorm,validNorm,n) ; 288 243 289 if( fPtrSolidA->Inside(p+dist*v) != kIns 244 if( fPtrSolidA->Inside(p+dist*v) != kInside ) 290 { 245 { 291 G4int count1=0; << 246 do 292 do // Loop checking, 13.08.2015, G.C << 293 { 247 { 294 disTmp = fPtrSolidA->DistanceToIn(p+ 248 disTmp = fPtrSolidA->DistanceToIn(p+dist*v,v) ; 295 249 296 if(disTmp == kInfinity) 250 if(disTmp == kInfinity) 297 { 251 { 298 return kInfinity ; 252 return kInfinity ; 299 } 253 } 300 dist += disTmp ; 254 dist += disTmp ; 301 255 302 if( Inside(p+dist*v) == kOutside ) 256 if( Inside(p+dist*v) == kOutside ) 303 { 257 { 304 disTmp = fPtrSolidB->DistanceToOut << 258 disTmp = fPtrSolidB->DistanceToOut(p+dist*v,v) ; 305 dist2 = dist+disTmp; << 259 dist += disTmp ; 306 if (dist == dist2) { return dist; << 260 } 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 } 261 } 334 while( Inside(p+dist*v) == kOutside ) 262 while( Inside(p+dist*v) == kOutside ) ; 335 } 263 } 336 } 264 } 337 else // p outside A, start in A 265 else // p outside A, start in A 338 { 266 { 339 dist = fPtrSolidA->DistanceToIn(p,v) ; 267 dist = fPtrSolidA->DistanceToIn(p,v) ; 340 268 341 if( dist == kInfinity ) // past A, hence 269 if( dist == kInfinity ) // past A, hence past A\B 342 { 270 { 343 return kInfinity ; 271 return kInfinity ; 344 } 272 } 345 else 273 else 346 { 274 { 347 G4int count2=0; << 348 while( Inside(p+dist*v) == kOutside ) 275 while( Inside(p+dist*v) == kOutside ) // pushing loop 349 { 276 { 350 disTmp = fPtrSolidB->DistanceToOut(p 277 disTmp = fPtrSolidB->DistanceToOut(p+dist*v,v) ; 351 dist += disTmp ; 278 dist += disTmp ; 352 279 353 if( Inside(p+dist*v) == kOutside ) 280 if( Inside(p+dist*v) == kOutside ) 354 { 281 { 355 disTmp = fPtrSolidA->DistanceToIn( 282 disTmp = fPtrSolidA->DistanceToIn(p+dist*v,v) ; 356 283 357 if(disTmp == kInfinity) // past A, 284 if(disTmp == kInfinity) // past A, hence past A\B 358 { 285 { 359 return kInfinity ; 286 return kInfinity ; 360 } 287 } 361 dist2 = dist+disTmp; << 288 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 } 289 } 389 } // Loop checking, 13.08.2015, G.C << 290 } 390 } 291 } 391 } 292 } 392 293 393 return dist ; 294 return dist ; 394 } 295 } 395 296 396 ////////////////////////////////////////////// << 297 //////////////////////////////////////////////////////// 397 // 298 // 398 // Approximate nearest distance from the point 299 // Approximate nearest distance from the point p to the intersection of 399 // two solids. It is usually underestimated fr 300 // two solids. It is usually underestimated from the point of view of 400 // isotropic safety 301 // isotropic safety 401 302 402 G4double 303 G4double 403 G4SubtractionSolid::DistanceToIn( const G4Thre 304 G4SubtractionSolid::DistanceToIn( const G4ThreeVector& p ) const 404 { 305 { 405 G4double dist = 0.0; << 306 G4double dist=0.0; 406 307 407 #ifdef G4BOOLDEBUG 308 #ifdef G4BOOLDEBUG 408 if( Inside(p) == kInside ) 309 if( Inside(p) == kInside ) 409 { 310 { 410 G4cout << "WARNING - Invalid call in " 311 G4cout << "WARNING - Invalid call in " 411 << "G4SubtractionSolid::DistanceToI 312 << "G4SubtractionSolid::DistanceToIn(p)" << G4endl 412 << " Point p is inside !" << G4end 313 << " Point p is inside !" << G4endl; 413 G4cout << " p = " << p << G4endl; 314 G4cout << " p = " << p << G4endl; 414 G4cerr << "WARNING - Invalid call in " 315 G4cerr << "WARNING - Invalid call in " 415 << "G4SubtractionSolid::DistanceToI 316 << "G4SubtractionSolid::DistanceToIn(p)" << G4endl 416 << " Point p is inside !" << G4end 317 << " Point p is inside !" << G4endl; 417 G4cerr << " p = " << p << G4endl; 318 G4cerr << " p = " << p << G4endl; 418 } 319 } 419 #endif 320 #endif 420 321 421 if( ( fPtrSolidA->Inside(p) != kOutside) && 322 if( ( fPtrSolidA->Inside(p) != kOutside) && // case 1 422 ( fPtrSolidB->Inside(p) != kOutside) 323 ( fPtrSolidB->Inside(p) != kOutside) ) 423 { 324 { 424 dist = fPtrSolidB->DistanceToOut(p); << 325 dist= fPtrSolidB->DistanceToOut(p) ; 425 } 326 } 426 else 327 else 427 { 328 { 428 dist = fPtrSolidA->DistanceToIn(p); << 329 dist= fPtrSolidA->DistanceToIn(p) ; 429 } 330 } 430 331 431 return dist; 332 return dist; 432 } 333 } 433 334 434 ////////////////////////////////////////////// << 335 ////////////////////////////////////////////////////////// 435 // 336 // 436 // The same algorithm as DistanceToOut(p) 337 // The same algorithm as DistanceToOut(p) 437 338 438 G4double 339 G4double 439 G4SubtractionSolid::DistanceToOut( const G4Thr 340 G4SubtractionSolid::DistanceToOut( const G4ThreeVector& p, 440 const G4Thr << 341 const G4ThreeVector& v, 441 const G4boo << 342 const G4bool calcNorm, 442 G4boo << 343 G4bool *validNorm, 443 G4Thr << 344 G4ThreeVector *n ) const 444 { 345 { 445 #ifdef G4BOOLDEBUG 346 #ifdef G4BOOLDEBUG 446 if( Inside(p) == kOutside ) 347 if( Inside(p) == kOutside ) 447 { 348 { 448 G4cout << "Position:" << G4endl << G4en 349 G4cout << "Position:" << G4endl << G4endl; 449 G4cout << "p.x() = " << p.x()/mm << " 350 G4cout << "p.x() = " << p.x()/mm << " mm" << G4endl; 450 G4cout << "p.y() = " << p.y()/mm << " 351 G4cout << "p.y() = " << p.y()/mm << " mm" << G4endl; 451 G4cout << "p.z() = " << p.z()/mm << " 352 G4cout << "p.z() = " << p.z()/mm << " mm" << G4endl << G4endl; 452 G4cout << "Direction:" << G4endl << G4en 353 G4cout << "Direction:" << G4endl << G4endl; 453 G4cout << "v.x() = " << v.x() << G4end 354 G4cout << "v.x() = " << v.x() << G4endl; 454 G4cout << "v.y() = " << v.y() << G4end 355 G4cout << "v.y() = " << v.y() << G4endl; 455 G4cout << "v.z() = " << v.z() << G4end 356 G4cout << "v.z() = " << v.z() << G4endl << G4endl; 456 G4cout << "WARNING - Invalid call in " 357 G4cout << "WARNING - Invalid call in " 457 << "G4SubtractionSolid::DistanceT 358 << "G4SubtractionSolid::DistanceToOut(p,v)" << G4endl 458 << " Point p is outside !" << G4 359 << " Point p is outside !" << G4endl; 459 G4cout << " p = " << p << G4end 360 G4cout << " p = " << p << G4endl; 460 G4cout << " v = " << v << G4end 361 G4cout << " v = " << v << G4endl; 461 G4cerr << "WARNING - Invalid call in " 362 G4cerr << "WARNING - Invalid call in " 462 << "G4SubtractionSolid::DistanceT 363 << "G4SubtractionSolid::DistanceToOut(p,v)" << G4endl 463 << " Point p is outside !" << G4 364 << " Point p is outside !" << G4endl; 464 G4cerr << " p = " << p << G4end 365 G4cerr << " p = " << p << G4endl; 465 G4cerr << " v = " << v << G4end 366 G4cerr << " v = " << v << G4endl; 466 } 367 } 467 #endif 368 #endif 468 369 469 G4double distout; 370 G4double distout; 470 G4double distA = fPtrSolidA->DistanceToOut 371 G4double distA = fPtrSolidA->DistanceToOut(p,v,calcNorm,validNorm,n) ; 471 G4double distB = fPtrSolidB->DistanceToIn( 372 G4double distB = fPtrSolidB->DistanceToIn(p,v) ; 472 if(distB < distA) 373 if(distB < distA) 473 { 374 { 474 if(calcNorm) 375 if(calcNorm) 475 { 376 { 476 *n = -(fPtrSolidB->SurfaceNormal(p+dis 377 *n = -(fPtrSolidB->SurfaceNormal(p+distB*v)) ; 477 *validNorm = false ; 378 *validNorm = false ; 478 } 379 } 479 distout= distB ; 380 distout= distB ; 480 } 381 } 481 else 382 else 482 { 383 { 483 distout= distA ; 384 distout= distA ; 484 } 385 } 485 return distout; 386 return distout; 486 } 387 } 487 388 488 ////////////////////////////////////////////// << 389 ////////////////////////////////////////////////////////////// 489 // 390 // 490 // Inverted algorithm of DistanceToIn(p) 391 // Inverted algorithm of DistanceToIn(p) 491 392 492 G4double 393 G4double 493 G4SubtractionSolid::DistanceToOut( const G4Thr 394 G4SubtractionSolid::DistanceToOut( const G4ThreeVector& p ) const 494 { 395 { 495 G4double dist=0.0; 396 G4double dist=0.0; 496 397 497 if( Inside(p) == kOutside ) 398 if( Inside(p) == kOutside ) 498 { 399 { 499 #ifdef G4BOOLDEBUG 400 #ifdef G4BOOLDEBUG 500 G4cout << "WARNING - Invalid call in " 401 G4cout << "WARNING - Invalid call in " 501 << "G4SubtractionSolid::DistanceToO 402 << "G4SubtractionSolid::DistanceToOut(p)" << G4endl 502 << " Point p is outside" << G4endl 403 << " Point p is outside" << G4endl; 503 G4cout << " p = " << p << G4endl; 404 G4cout << " p = " << p << G4endl; 504 G4cerr << "WARNING - Invalid call in " 405 G4cerr << "WARNING - Invalid call in " 505 << "G4SubtractionSolid::DistanceToO 406 << "G4SubtractionSolid::DistanceToOut(p)" << G4endl 506 << " Point p is outside" << G4endl 407 << " Point p is outside" << G4endl; 507 G4cerr << " p = " << p << G4endl; 408 G4cerr << " p = " << p << G4endl; 508 #endif 409 #endif 509 } 410 } 510 else 411 else 511 { 412 { 512 dist= std::min(fPtrSolidA->DistanceToOut( 413 dist= std::min(fPtrSolidA->DistanceToOut(p), 513 fPtrSolidB->DistanceToIn 414 fPtrSolidB->DistanceToIn(p) ) ; 514 } 415 } 515 return dist; 416 return dist; 516 } 417 } 517 418 518 ////////////////////////////////////////////// << 419 ////////////////////////////////////////////////////////////// 519 // 420 // 520 // 421 // 521 422 522 G4GeometryType G4SubtractionSolid::GetEntityTy 423 G4GeometryType G4SubtractionSolid::GetEntityType() const 523 { 424 { 524 return {"G4SubtractionSolid"}; << 425 return G4String("G4SubtractionSolid"); 525 } 426 } 526 427 527 ////////////////////////////////////////////// << 428 ////////////////////////////////////////////////////////////// 528 // 429 // 529 // Make a clone of the object << 530 << 531 G4VSolid* G4SubtractionSolid::Clone() const << 532 { << 533 return new G4SubtractionSolid(*this); << 534 } << 535 << 536 ////////////////////////////////////////////// << 537 // 430 // 538 // ComputeDimensions << 539 431 540 void 432 void 541 G4SubtractionSolid::ComputeDimensions( G 433 G4SubtractionSolid::ComputeDimensions( G4VPVParameterisation*, 542 const G 434 const G4int, 543 const G 435 const G4VPhysicalVolume* ) 544 { 436 { 545 } 437 } 546 438 547 ////////////////////////////////////////////// << 439 ///////////////////////////////////////////////// 548 // 440 // 549 // DescribeYourselfTo << 441 // 550 442 551 void 443 void 552 G4SubtractionSolid::DescribeYourselfTo ( G4VGr 444 G4SubtractionSolid::DescribeYourselfTo ( G4VGraphicsScene& scene ) const 553 { 445 { 554 scene.AddSolid (*this); 446 scene.AddSolid (*this); 555 } 447 } 556 448 557 ////////////////////////////////////////////// << 449 //////////////////////////////////////////////////// >> 450 // 558 // 451 // 559 // CreatePolyhedron << 560 452 561 G4Polyhedron* G4SubtractionSolid::CreatePolyhe << 453 G4Polyhedron* >> 454 G4SubtractionSolid::CreatePolyhedron () const 562 { 455 { 563 if (fExternalBoolProcessor == nullptr) << 456 G4Polyhedron* pA = fPtrSolidA->GetPolyhedron(); >> 457 G4Polyhedron* pB = fPtrSolidB->GetPolyhedron(); >> 458 if (pA && pB) 564 { 459 { 565 HepPolyhedronProcessor processor; << 460 G4Polyhedron* resultant = new G4Polyhedron (pA->subtract(*pB)); 566 // Stack components and components of comp << 461 return resultant; 567 // See G4BooleanSolid::StackPolyhedron << 568 G4Polyhedron* top = StackPolyhedron(proces << 569 auto result = new G4Polyhedron(*top); << 570 if (processor.execute(*result)) << 571 { << 572 return result; << 573 } << 574 else << 575 { << 576 return nullptr; << 577 } << 578 } 462 } 579 else 463 else 580 { 464 { 581 return fExternalBoolProcessor->Process(thi << 465 std::ostringstream oss; >> 466 oss << "Solid - " << GetName() >> 467 << " - one of the Boolean components has no" << G4endl >> 468 << " corresponding polyhedron. Returning NULL !"; >> 469 G4Exception("G4SubtractionSolid::CreatePolyhedron()", "InvalidSetup", >> 470 JustWarning, oss.str().c_str()); >> 471 return 0; 582 } 472 } 583 } 473 } 584 474 585 ////////////////////////////////////////////// << 475 ///////////////////////////////////////////////////////// 586 // 476 // 587 // GetCubicVolume << 588 // 477 // 589 478 590 G4double G4SubtractionSolid::GetCubicVolume() << 479 G4NURBS* >> 480 G4SubtractionSolid::CreateNURBS () const 591 { 481 { 592 if( fCubicVolume >= 0. ) << 482 // Take into account boolean operation - see CreatePolyhedron. 593 { << 483 // return new G4NURBSbox (1.0, 1.0, 1.0); 594 return fCubicVolume; << 484 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 } 485 } 627 486