Geant4 Cross Reference |
1 // 1 // 2 // ******************************************* 2 // ******************************************************************** 3 // * License and Disclaimer << 3 // * DISCLAIMER * 4 // * 4 // * * 5 // * The Geant4 software is copyright of th << 5 // * The following disclaimer summarizes all the specific disclaimers * 6 // * the Geant4 Collaboration. It is provided << 6 // * of contributors to this software. The specific disclaimers,which * 7 // * conditions of the Geant4 Software License << 7 // * govern, are listed with their locations in: * 8 // * LICENSE and available at http://cern.ch/ << 8 // * http://cern.ch/geant4/license * 9 // * include a list of copyright holders. << 10 // * 9 // * * 11 // * Neither the authors of this software syst 10 // * Neither the authors of this software system, nor their employing * 12 // * institutes,nor the agencies providing fin 11 // * institutes,nor the agencies providing financial support for this * 13 // * work make any representation or warran 12 // * work make any representation or warranty, express or implied, * 14 // * regarding this software system or assum 13 // * regarding this software system or assume any liability for its * 15 // * use. Please see the license in the file << 14 // * use. * 16 // * for the full disclaimer and the limitatio << 17 // * 15 // * * 18 // * This code implementation is the result << 16 // * This code implementation is the intellectual property of the * 19 // * technical work of the GEANT4 collaboratio << 17 // * GEANT4 collaboration. * 20 // * By using, copying, modifying or distri << 18 // * By copying, distributing or modifying the Program (or any work * 21 // * any work based on the software) you ag << 19 // * based on the Program) you indicate your acceptance of this * 22 // * use in resulting scientific publicati << 20 // * statement, and all its terms. * 23 // * acceptance of all terms of the Geant4 Sof << 24 // ******************************************* 21 // ******************************************************************** 25 // 22 // >> 23 // >> 24 // $Id: G4Sphere.cc,v 1.34 2004/12/10 16:22:37 gcosmo Exp $ >> 25 // GEANT4 tag $Name: geant4-07-00-patch-01 $ >> 26 // >> 27 // class G4Sphere >> 28 // 26 // Implementation for G4Sphere class 29 // Implementation for G4Sphere class 27 // 30 // 28 // 28.03.94 P.Kent: old C++ code converted to << 31 // History: 29 // 17.09.96 V.Grichine: final modifications to << 32 // >> 33 // 16.09.04 V.Grichine: bug fixed in SurfaceNormal(p), theta normals >> 34 // 16.07.04 V.Grichine: bug fixed in DistanceToOut(p,v), Rmin go outside >> 35 // 02.06.04 V.Grichine: bug fixed in DistanceToIn(p,v), on Rmax,Rmin go inside 30 // 30.10.03 J.Apostolakis: new algorithm in In 36 // 30.10.03 J.Apostolakis: new algorithm in Inside for SPhi-sections 31 // 03.05.05 V.Grichine: SurfaceNormal(p) accor << 37 // 29.10.03 J.Apostolakis: fix in Inside for SPhi-0.5*kAngTol < phi < SPhi, SPhi<0 32 // 22.07.05 O.Link: Added check for intersecti << 38 // 19.06.02 V.Grichine: bug fixed in Inside(p), && -> && fDTheta - kAngTolerance 33 // 26.03.09 G.Cosmo: optimisations and uniform << 39 // 30.01.02 V.Grichine: bug fixed in Inside(p), && -> || at l.451 34 // 26.10.16 E.Tcherniaev: re-implemented Calcu << 40 // 06.03.00 V.Grichine: modifications in Distance ToOut(p,v,...) 35 // G4BoundingEnvelope, << 41 // 18.11.99 V.Grichine: side = kNull in Distance ToOut(p,v,...) >> 42 // 25.11.98 V.Grichine: bug fixed in DistanceToIn(p,v), phi intersections >> 43 // 12.11.98 V.Grichine: bug fixed in DistanceToIn(p,v), theta intersections >> 44 // 09.10.98 V.Grichine: modifications in Distance ToOut(p,v,...) >> 45 // 17.09.96 V.Grichine: final modifications to commit >> 46 // 28.03.94 P.Kent: old C++ code converted to tolerant geometry 36 // ------------------------------------------- 47 // -------------------------------------------------------------------- 37 48 38 #include "G4Sphere.hh" << 49 #include <assert.h> 39 50 40 #if !defined(G4GEOM_USE_USPHERE) << 51 #include "G4Sphere.hh" 41 52 42 #include "G4GeomTools.hh" << 43 #include "G4VoxelLimits.hh" 53 #include "G4VoxelLimits.hh" 44 #include "G4AffineTransform.hh" 54 #include "G4AffineTransform.hh" 45 #include "G4GeometryTolerance.hh" << 46 #include "G4BoundingEnvelope.hh" << 47 55 48 #include "G4VPVParameterisation.hh" 56 #include "G4VPVParameterisation.hh" 49 57 50 #include "G4QuickRand.hh" << 51 << 52 #include "meshdefs.hh" 58 #include "meshdefs.hh" 53 59 54 #include "G4VGraphicsScene.hh" 60 #include "G4VGraphicsScene.hh" 55 #include "G4VisExtent.hh" 61 #include "G4VisExtent.hh" 56 << 62 #include "G4Polyhedron.hh" 57 using namespace CLHEP; << 63 #include "G4NURBS.hh" >> 64 #include "G4NURBSbox.hh" 58 65 59 // Private enum: Not for external use - used b 66 // Private enum: Not for external use - used by distanceToOut 60 67 61 enum ESide {kNull,kRMin,kRMax,kSPhi,kEPhi,kSTh 68 enum ESide {kNull,kRMin,kRMax,kSPhi,kEPhi,kSTheta,kETheta}; 62 69 63 // used by normal 70 // used by normal 64 71 65 enum ENorm {kNRMin,kNRMax,kNSPhi,kNEPhi,kNSThe 72 enum ENorm {kNRMin,kNRMax,kNSPhi,kNEPhi,kNSTheta,kNETheta}; 66 73 >> 74 ///////////////////////////////////////////////////////////////////// >> 75 // >> 76 // Destructor >> 77 >> 78 G4Sphere::~G4Sphere() >> 79 { >> 80 } >> 81 67 ////////////////////////////////////////////// 82 //////////////////////////////////////////////////////////////////////// 68 // 83 // 69 // constructor - check parameters, convert ang 84 // constructor - check parameters, convert angles so 0<sphi+dpshi<=2_PI 70 // - note if pDPhi>2PI then reset 85 // - note if pDPhi>2PI then reset to 2PI 71 86 72 G4Sphere::G4Sphere( const G4String& pName, 87 G4Sphere::G4Sphere( const G4String& pName, 73 G4double pRmin, G4do 88 G4double pRmin, G4double pRmax, 74 G4double pSPhi, G4do 89 G4double pSPhi, G4double pDPhi, 75 G4double pSTheta, G4 90 G4double pSTheta, G4double pDTheta ) 76 : G4CSGSolid(pName), fSPhi(0.0), fFullPhiSph << 91 : G4CSGSolid(pName) 77 { 92 { 78 kAngTolerance = G4GeometryTolerance::GetInst << 93 fEpsilon = 1.0e-14; 79 kRadTolerance = G4GeometryTolerance::GetInst << 80 94 81 halfCarTolerance = 0.5*kCarTolerance; << 95 // Check radii 82 halfAngTolerance = 0.5*kAngTolerance; << 83 96 84 // Check radii and set radial tolerances << 97 if (pRmin<pRmax&&pRmin>=0) 85 << 98 { 86 if ( (pRmin >= pRmax) || (pRmax < 1.1*kRadTo << 99 fRmin=pRmin; fRmax=pRmax; >> 100 } >> 101 else 87 { 102 { 88 std::ostringstream message; << 103 G4cerr << "ERROR - G4Sphere()::G4Sphere(): " << GetName() << G4endl 89 message << "Invalid radii for Solid: " << << 104 << " Invalide values for radii ! - " 90 << " pRmin = " << pRmin << << 105 << " pRmin = " << pRmin << ", pRmax = " << pRmax << G4endl; 91 G4Exception("G4Sphere::G4Sphere()", "GeomS << 106 G4Exception("G4Sphere::G4Sphere()", "InvalidSetup", FatalException, 92 FatalException, message); << 107 "Invalid radii"); 93 } 108 } 94 fRmin=pRmin; fRmax=pRmax; << 95 fRminTolerance = (fRmin) != 0.0 ? std::max( << 96 fRmaxTolerance = std::max( kRadTolerance, fE << 97 109 98 // Check angles 110 // Check angles 99 111 100 CheckPhiAngles(pSPhi, pDPhi); << 112 if (pDPhi>=twopi) 101 CheckThetaAngles(pSTheta, pDTheta); << 113 { 102 } << 114 fDPhi=twopi; 103 << 115 } 104 ////////////////////////////////////////////// << 116 else if (pDPhi>0) 105 // << 117 { 106 // Fake default constructor - sets only member << 118 fDPhi=pDPhi; 107 // for usage restri << 119 } 108 // << 120 else 109 G4Sphere::G4Sphere( __void__& a ) << 121 { 110 : G4CSGSolid(a) << 122 G4cerr << "ERROR - G4Sphere()::G4Sphere(): " << GetName() << G4endl 111 { << 123 << " Negative Z delta-Phi ! - " 112 } << 124 << pDPhi << G4endl; 113 << 125 G4Exception("G4Sphere::G4Sphere()", "InvalidSetup", FatalException, 114 ////////////////////////////////////////////// << 126 "Invalid DPhi."); 115 // << 127 } 116 // Destructor << 117 128 118 G4Sphere::~G4Sphere() = default; << 129 // Convert fSPhi to 0-2PI 119 130 120 ////////////////////////////////////////////// << 131 if (pSPhi<0) 121 // << 132 { 122 // Copy constructor << 133 fSPhi=twopi-std::fmod(std::fabs(pSPhi),twopi); >> 134 } >> 135 else >> 136 { >> 137 fSPhi=std::fmod(pSPhi,twopi); >> 138 } 123 139 124 G4Sphere::G4Sphere(const G4Sphere&) = default; << 140 // Sphere is placed such that fSPhi+fDPhi>twopi ! >> 141 // fSPhi could be < 0 !!? >> 142 // >> 143 if (fSPhi+fDPhi>twopi) fSPhi-=twopi; 125 144 126 ////////////////////////////////////////////// << 145 // Check theta angles 127 // << 128 // Assignment operator << 129 146 130 G4Sphere& G4Sphere::operator = (const G4Sphere << 147 if (pSTheta<0 || pSTheta>pi) 131 { << 148 { 132 // Check assignment to self << 149 G4cerr << "ERROR - G4Sphere()::G4Sphere(): " << GetName() << G4endl; 133 // << 150 G4Exception("G4Sphere::G4Sphere()", "InvalidSetup", FatalException, 134 if (this == &rhs) { return *this; } << 151 "stheta outside 0-PI range."); 135 << 152 } 136 // Copy base class data << 153 else 137 // << 154 { 138 G4CSGSolid::operator=(rhs); << 155 fSTheta=pSTheta; 139 << 156 } 140 // Copy data << 141 // << 142 fRminTolerance = rhs.fRminTolerance; fRmaxT << 143 kAngTolerance = rhs.kAngTolerance; kRadTole << 144 fEpsilon = rhs.fEpsilon; fRmin = rhs.fRmin; << 145 fSPhi = rhs.fSPhi; fDPhi = rhs.fDPhi; fSThe << 146 fDTheta = rhs.fDTheta; sinCPhi = rhs.sinCPh << 147 cosHDPhi = rhs.cosHDPhi; << 148 cosHDPhiOT = rhs.cosHDPhiOT; cosHDPhiIT = r << 149 sinSPhi = rhs.sinSPhi; cosSPhi = rhs.cosSPh << 150 sinEPhi = rhs.sinEPhi; cosEPhi = rhs.cosEPh << 151 hDPhi = rhs.hDPhi; cPhi = rhs.cPhi; ePhi = << 152 sinSTheta = rhs.sinSTheta; cosSTheta = rhs. << 153 sinETheta = rhs.sinETheta; cosETheta = rhs. << 154 tanSTheta = rhs.tanSTheta; tanSTheta2 = rhs << 155 tanETheta = rhs.tanETheta; tanETheta2 = rhs << 156 eTheta = rhs.eTheta; fFullPhiSphere = rhs.f << 157 fFullThetaSphere = rhs.fFullThetaSphere; fF << 158 halfCarTolerance = rhs.halfCarTolerance; << 159 halfAngTolerance = rhs.halfAngTolerance; << 160 157 161 return *this; << 158 if (pDTheta+pSTheta>=pi) >> 159 { >> 160 fDTheta=pi-pSTheta; >> 161 } >> 162 else if (pDTheta>0) >> 163 { >> 164 fDTheta=pDTheta; >> 165 } >> 166 else >> 167 { >> 168 G4cerr << "ERROR - G4Sphere()::G4Sphere(): " << GetName() << G4endl >> 169 << " Negative delta-Theta ! - " >> 170 << pDTheta << G4endl; >> 171 G4Exception("G4Sphere::G4Sphere()", "InvalidSetup", FatalException, >> 172 "Invalid pDTheta."); >> 173 } 162 } 174 } 163 175 164 ////////////////////////////////////////////// 176 ////////////////////////////////////////////////////////////////////////// 165 // 177 // 166 // Dispatch to parameterisation for replicatio 178 // Dispatch to parameterisation for replication mechanism dimension 167 // computation & modification. 179 // computation & modification. 168 180 169 void G4Sphere::ComputeDimensions( G4VPVP 181 void G4Sphere::ComputeDimensions( G4VPVParameterisation* p, 170 const G4int 182 const G4int n, 171 const G4VPhy 183 const G4VPhysicalVolume* pRep) 172 { 184 { 173 p->ComputeDimensions(*this,n,pRep); 185 p->ComputeDimensions(*this,n,pRep); 174 } 186 } 175 187 176 ////////////////////////////////////////////// << 177 // << 178 // Get bounding box << 179 << 180 void G4Sphere::BoundingLimits(G4ThreeVector& p << 181 { << 182 G4double rmin = GetInnerRadius(); << 183 G4double rmax = GetOuterRadius(); << 184 << 185 // Find bounding box << 186 // << 187 if (GetDeltaThetaAngle() >= pi && GetDeltaPh << 188 { << 189 pMin.set(-rmax,-rmax,-rmax); << 190 pMax.set( rmax, rmax, rmax); << 191 } << 192 else << 193 { << 194 G4double sinStart = GetSinStartTheta(); << 195 G4double cosStart = GetCosStartTheta(); << 196 G4double sinEnd = GetSinEndTheta(); << 197 G4double cosEnd = GetCosEndTheta(); << 198 << 199 G4double stheta = GetStartThetaAngle(); << 200 G4double etheta = stheta + GetDeltaThetaAn << 201 G4double rhomin = rmin*std::min(sinStart,s << 202 G4double rhomax = rmax; << 203 if (stheta > halfpi) rhomax = rmax*sinStar << 204 if (etheta < halfpi) rhomax = rmax*sinEnd; << 205 << 206 G4TwoVector xymin,xymax; << 207 G4GeomTools::DiskExtent(rhomin,rhomax, << 208 GetSinStartPhi(),G << 209 GetSinEndPhi(),Get << 210 xymin,xymax); << 211 << 212 G4double zmin = std::min(rmin*cosEnd,rmax* << 213 G4double zmax = std::max(rmin*cosStart,rma << 214 pMin.set(xymin.x(),xymin.y(),zmin); << 215 pMax.set(xymax.x(),xymax.y(),zmax); << 216 } << 217 << 218 // Check correctness of the bounding box << 219 // << 220 if (pMin.x() >= pMax.x() || pMin.y() >= pMax << 221 { << 222 std::ostringstream message; << 223 message << "Bad bounding box (min >= max) << 224 << GetName() << " !" << 225 << "\npMin = " << pMin << 226 << "\npMax = " << pMax; << 227 G4Exception("G4Sphere::BoundingLimits()", << 228 JustWarning, message); << 229 DumpInfo(); << 230 } << 231 } << 232 << 233 ////////////////////////////////////////////// 188 //////////////////////////////////////////////////////////////////////////// 234 // 189 // 235 // Calculate extent under transform and specif 190 // Calculate extent under transform and specified limit 236 191 237 G4bool G4Sphere::CalculateExtent( const EAxis 192 G4bool G4Sphere::CalculateExtent( const EAxis pAxis, 238 const G4Voxe 193 const G4VoxelLimits& pVoxelLimit, 239 const G4Affi 194 const G4AffineTransform& pTransform, 240 G4doub 195 G4double& pMin, G4double& pMax ) const 241 { 196 { 242 G4ThreeVector bmin, bmax; << 197 if ( fDPhi==twopi && fDTheta==pi) // !pTransform.IsRotated() && >> 198 { >> 199 // Special case handling for solid spheres-shells >> 200 // (rotation doesn't influence). >> 201 // Compute x/y/z mins and maxs for bounding box respecting limits, >> 202 // with early returns if outside limits. Then switch() on pAxis, >> 203 // and compute exact x and y limit for x/y case >> 204 >> 205 G4double xoffset,xMin,xMax; >> 206 G4double yoffset,yMin,yMax; >> 207 G4double zoffset,zMin,zMax; >> 208 >> 209 G4double diff1,diff2,maxDiff,newMin,newMax; >> 210 G4double xoff1,xoff2,yoff1,yoff2; >> 211 >> 212 xoffset=pTransform.NetTranslation().x(); >> 213 xMin=xoffset-fRmax; >> 214 xMax=xoffset+fRmax; >> 215 if (pVoxelLimit.IsXLimited()) >> 216 { >> 217 if ( (xMin>pVoxelLimit.GetMaxXExtent()+kCarTolerance) >> 218 || (xMax<pVoxelLimit.GetMinXExtent()-kCarTolerance) ) >> 219 { >> 220 return false; >> 221 } >> 222 else >> 223 { >> 224 if (xMin<pVoxelLimit.GetMinXExtent()) >> 225 { >> 226 xMin=pVoxelLimit.GetMinXExtent(); >> 227 } >> 228 if (xMax>pVoxelLimit.GetMaxXExtent()) >> 229 { >> 230 xMax=pVoxelLimit.GetMaxXExtent(); >> 231 } >> 232 } >> 233 } 243 234 244 // Get bounding box << 235 yoffset=pTransform.NetTranslation().y(); 245 BoundingLimits(bmin,bmax); << 236 yMin=yoffset-fRmax; >> 237 yMax=yoffset+fRmax; >> 238 if (pVoxelLimit.IsYLimited()) >> 239 { >> 240 if ( (yMin>pVoxelLimit.GetMaxYExtent()+kCarTolerance) >> 241 || (yMax<pVoxelLimit.GetMinYExtent()-kCarTolerance) ) >> 242 { >> 243 return false; >> 244 } >> 245 else >> 246 { >> 247 if (yMin<pVoxelLimit.GetMinYExtent()) >> 248 { >> 249 yMin=pVoxelLimit.GetMinYExtent(); >> 250 } >> 251 if (yMax>pVoxelLimit.GetMaxYExtent()) >> 252 { >> 253 yMax=pVoxelLimit.GetMaxYExtent(); >> 254 } >> 255 } >> 256 } 246 257 247 // Find extent << 258 zoffset=pTransform.NetTranslation().z(); 248 G4BoundingEnvelope bbox(bmin,bmax); << 259 zMin=zoffset-fRmax; 249 return bbox.CalculateExtent(pAxis,pVoxelLimi << 260 zMax=zoffset+fRmax; 250 } << 261 if (pVoxelLimit.IsZLimited()) >> 262 { >> 263 if ( (zMin>pVoxelLimit.GetMaxZExtent()+kCarTolerance) >> 264 || (zMax<pVoxelLimit.GetMinZExtent()-kCarTolerance) ) >> 265 { >> 266 return false; >> 267 } >> 268 else >> 269 { >> 270 if (zMin<pVoxelLimit.GetMinZExtent()) >> 271 { >> 272 zMin=pVoxelLimit.GetMinZExtent(); >> 273 } >> 274 if (zMax>pVoxelLimit.GetMaxZExtent()) >> 275 { >> 276 zMax=pVoxelLimit.GetMaxZExtent(); >> 277 } >> 278 } >> 279 } 251 280 252 ////////////////////////////////////////////// << 281 // Known to cut sphere 253 // << 254 // Return whether point inside/outside/on surf << 255 // Split into radius, phi, theta checks << 256 // Each check modifies 'in', or returns as app << 257 282 258 EInside G4Sphere::Inside( const G4ThreeVector& << 283 switch (pAxis) 259 { << 284 { 260 G4double rho,rho2,rad2,tolRMin,tolRMax; << 285 case kXAxis: 261 G4double pPhi,pTheta; << 286 yoff1=yoffset-yMin; 262 EInside in = kOutside; << 287 yoff2=yMax-yoffset; >> 288 if (yoff1>=0&&yoff2>=0) >> 289 { >> 290 // Y limits cross max/min x => no change >> 291 // >> 292 pMin=xMin; >> 293 pMax=xMax; >> 294 } >> 295 else >> 296 { >> 297 // Y limits don't cross max/min x => compute max delta x, >> 298 // hence new mins/maxs >> 299 // >> 300 diff1=std::sqrt(fRmax*fRmax-yoff1*yoff1); >> 301 diff2=std::sqrt(fRmax*fRmax-yoff2*yoff2); >> 302 maxDiff=(diff1>diff2) ? diff1:diff2; >> 303 newMin=xoffset-maxDiff; >> 304 newMax=xoffset+maxDiff; >> 305 pMin=(newMin<xMin) ? xMin : newMin; >> 306 pMax=(newMax>xMax) ? xMax : newMax; >> 307 } >> 308 break; >> 309 case kYAxis: >> 310 xoff1=xoffset-xMin; >> 311 xoff2=xMax-xoffset; >> 312 if (xoff1>=0&&xoff2>=0) >> 313 { >> 314 // X limits cross max/min y => no change >> 315 // >> 316 pMin=yMin; >> 317 pMax=yMax; >> 318 } >> 319 else >> 320 { >> 321 // X limits don't cross max/min y => compute max delta y, >> 322 // hence new mins/maxs >> 323 // >> 324 diff1=std::sqrt(fRmax*fRmax-xoff1*xoff1); >> 325 diff2=std::sqrt(fRmax*fRmax-xoff2*xoff2); >> 326 maxDiff=(diff1>diff2) ? diff1:diff2; >> 327 newMin=yoffset-maxDiff; >> 328 newMax=yoffset+maxDiff; >> 329 pMin=(newMin<yMin) ? yMin : newMin; >> 330 pMax=(newMax>yMax) ? yMax : newMax; >> 331 } >> 332 break; >> 333 case kZAxis: >> 334 pMin=zMin; >> 335 pMax=zMax; >> 336 break; >> 337 default: >> 338 break; >> 339 } >> 340 pMin-=kCarTolerance; >> 341 pMax+=kCarTolerance; 263 342 264 const G4double halfRmaxTolerance = fRmaxTole << 343 return true; 265 const G4double halfRminTolerance = fRminTole << 344 } 266 const G4double Rmax_minus = fRmax - halfRmax << 345 else // Transformed cutted sphere 267 const G4double Rmin_plus = (fRmin > 0) ? fR << 346 { >> 347 G4int i,j,noEntries,noBetweenSections; >> 348 G4bool existsAfterClip=false; 268 349 269 rho2 = p.x()*p.x() + p.y()*p.y() ; << 350 // Calculate rotated vertex coordinates 270 rad2 = rho2 + p.z()*p.z() ; << 271 351 272 // Check radial surfaces. Sets 'in' << 352 G4ThreeVectorList* vertices; >> 353 G4int noPolygonVertices ; >> 354 vertices=CreateRotatedVertices(pTransform,noPolygonVertices); 273 355 274 tolRMin = Rmin_plus; << 356 pMin=+kInfinity; 275 tolRMax = Rmax_minus; << 357 pMax=-kInfinity; 276 358 277 if(rad2 == 0.0) << 359 noEntries=vertices->size(); // noPolygonVertices*noPhiCrossSections 278 { << 360 noBetweenSections=noEntries-noPolygonVertices; 279 if (fRmin > 0.0) << 361 >> 362 G4ThreeVectorList ThetaPolygon ; >> 363 for (i=0;i<noEntries;i+=noPolygonVertices) 280 { 364 { 281 return in = kOutside; << 365 for(j=0;j<(noPolygonVertices/2)-1;j++) >> 366 { >> 367 ThetaPolygon.push_back((*vertices)[i+j]) ; >> 368 ThetaPolygon.push_back((*vertices)[i+j+1]) ; >> 369 ThetaPolygon.push_back((*vertices)[i+noPolygonVertices-2-j]) ; >> 370 ThetaPolygon.push_back((*vertices)[i+noPolygonVertices-1-j]) ; >> 371 CalculateClippedPolygonExtent(ThetaPolygon,pVoxelLimit,pAxis,pMin,pMax); >> 372 ThetaPolygon.clear() ; >> 373 } 282 } 374 } 283 if ( (!fFullPhiSphere) || (!fFullThetaSphe << 375 for (i=0;i<noBetweenSections;i+=noPolygonVertices) 284 { 376 { 285 return in = kSurface; << 377 for(j=0;j<noPolygonVertices-1;j++) >> 378 { >> 379 ThetaPolygon.push_back((*vertices)[i+j]) ; >> 380 ThetaPolygon.push_back((*vertices)[i+j+1]) ; >> 381 ThetaPolygon.push_back((*vertices)[i+noPolygonVertices+j+1]) ; >> 382 ThetaPolygon.push_back((*vertices)[i+noPolygonVertices+j]) ; >> 383 CalculateClippedPolygonExtent(ThetaPolygon,pVoxelLimit,pAxis,pMin,pMax); >> 384 ThetaPolygon.clear() ; >> 385 } >> 386 ThetaPolygon.push_back((*vertices)[i+noPolygonVertices-1]) ; >> 387 ThetaPolygon.push_back((*vertices)[i]) ; >> 388 ThetaPolygon.push_back((*vertices)[i+noPolygonVertices]) ; >> 389 ThetaPolygon.push_back((*vertices)[i+2*noPolygonVertices-1]) ; >> 390 CalculateClippedPolygonExtent(ThetaPolygon,pVoxelLimit,pAxis,pMin,pMax); >> 391 ThetaPolygon.clear() ; >> 392 } >> 393 >> 394 if (pMin!=kInfinity || pMax!=-kInfinity) >> 395 { >> 396 existsAfterClip=true; >> 397 >> 398 // Add 2*tolerance to avoid precision troubles >> 399 // >> 400 pMin-=kCarTolerance; >> 401 pMax+=kCarTolerance; 286 } 402 } 287 else 403 else 288 { 404 { 289 return in = kInside; << 405 // Check for case where completely enveloping clipping volume >> 406 // If point inside then we are confident that the solid completely >> 407 // envelopes the clipping volume. Hence set min/max extents according >> 408 // to clipping volume extents along the specified axis. >> 409 >> 410 G4ThreeVector clipCentre( >> 411 (pVoxelLimit.GetMinXExtent()+pVoxelLimit.GetMaxXExtent())*0.5, >> 412 (pVoxelLimit.GetMinYExtent()+pVoxelLimit.GetMaxYExtent())*0.5, >> 413 (pVoxelLimit.GetMinZExtent()+pVoxelLimit.GetMaxZExtent())*0.5); >> 414 >> 415 if (Inside(pTransform.Inverse().TransformPoint(clipCentre))!=kOutside) >> 416 { >> 417 existsAfterClip=true; >> 418 pMin=pVoxelLimit.GetMinExtent(pAxis); >> 419 pMax=pVoxelLimit.GetMaxExtent(pAxis); >> 420 } 290 } 421 } >> 422 delete vertices; >> 423 return existsAfterClip; 291 } 424 } >> 425 } 292 426 293 if ( (rad2 <= Rmax_minus*Rmax_minus) && (rad << 427 /////////////////////////////////////////////////////////////////////////// 294 { << 428 // 295 in = kInside; << 429 // Return whether point inside/outside/on surface 296 } << 430 // Split into radius, phi, theta checks >> 431 // Each check modifies `in', or returns as approprate >> 432 >> 433 EInside G4Sphere::Inside( const G4ThreeVector& p ) const >> 434 { >> 435 G4double rho,rho2,rad2,tolRMin,tolRMax; >> 436 G4double pPhi,pTheta; >> 437 EInside in=kOutside; >> 438 >> 439 rho2 = p.x()*p.x() + p.y()*p.y() ; >> 440 rad2 = rho2 + p.z()*p.z() ; >> 441 >> 442 // if(rad2 >= 1.369e+19) DBG(); >> 443 // G4double rad = std::sqrt(rad2); >> 444 // Check radial surfaces >> 445 // sets `in' >> 446 >> 447 if ( fRmin ) tolRMin = fRmin + kRadTolerance*0.5; >> 448 else tolRMin = 0 ; >> 449 >> 450 tolRMax = fRmax - kRadTolerance*0.5 ; >> 451 // const G4double fractionTolerance = 1.0e-12; >> 452 const G4double flexRadMaxTolerance = // kRadTolerance; >> 453 std::max(kRadTolerance, fEpsilon * fRmax); >> 454 >> 455 const G4double Rmax_minus = fRmax - flexRadMaxTolerance*0.5; >> 456 const G4double flexRadMinTolerance = std::max(kRadTolerance, >> 457 fEpsilon * fRmin); >> 458 const G4double Rmin_plus = (fRmin > 0) ? fRmin + flexRadMinTolerance*0.5 : 0 ; >> 459 >> 460 if(rad2 <= Rmax_minus*Rmax_minus && rad2 >= Rmin_plus*Rmin_plus) in = kInside ; >> 461 >> 462 // if ( rad2 <= tolRMax*tolRMax && rad2 >= tolRMin*tolRMin ) in = kInside ; >> 463 // if ( rad <= tolRMax && rad >= tolRMin ) in = kInside ; 297 else 464 else 298 { 465 { 299 tolRMax = fRmax + halfRmaxTolerance; << 466 tolRMax = fRmax + kRadTolerance*0.5 ; 300 tolRMin = std::max(fRmin-halfRminTolerance << 467 tolRMin = fRmin - kRadTolerance*0.5 ; 301 if ( (rad2 <= tolRMax*tolRMax) && (rad2 >= << 468 302 { << 469 if ( tolRMin < 0.0 ) tolRMin = 0.0 ; 303 in = kSurface; << 470 304 } << 471 if ( rad2 <= tolRMax*tolRMax && rad2 >= tolRMin*tolRMin ) in = kSurface ; 305 else << 472 // if ( rad <= tolRMax && rad >= tolRMin ) in = kSurface ; 306 { << 473 else return in = kOutside ; 307 return in = kOutside; << 308 } << 309 } 474 } 310 475 311 // Phi boundaries : Do not check if it has 476 // Phi boundaries : Do not check if it has no phi boundary! >> 477 // (in != kOutside). It is new J.Apostolakis proposal of 30.10.03 312 478 313 if ( !fFullPhiSphere && (rho2 != 0.0) ) // << 479 if ( ( fDPhi < twopi - kAngTolerance ) && >> 480 ( (p.x() != 0.0 ) || (p.y() != 0.0) ) ) 314 { 481 { 315 pPhi = std::atan2(p.y(),p.x()) ; 482 pPhi = std::atan2(p.y(),p.x()) ; 316 483 317 if ( pPhi < fSPhi - halfAngTolerance << 484 if ( pPhi < fSPhi - kAngTolerance*0.5 ) pPhi += twopi ; 318 else if ( pPhi > ePhi + halfAngTolerance ) << 485 else if ( pPhi > fSPhi + fDPhi + kAngTolerance*0.5 ) pPhi -= twopi; 319 << 486 320 if ( (pPhi < fSPhi - halfAngTolerance) << 487 if ((pPhi < fSPhi - kAngTolerance*0.5) || 321 || (pPhi > ePhi + halfAngTolerance) ) << 488 (pPhi > fSPhi + fDPhi + kAngTolerance*0.5) ) return in = kOutside ; 322 << 489 323 else if (in == kInside) // else it's kSur 490 else if (in == kInside) // else it's kSurface anyway already 324 { 491 { 325 if ( (pPhi < fSPhi + halfAngTolerance) << 492 if ( (pPhi < fSPhi + kAngTolerance*0.5) || 326 || (pPhi > ePhi - halfAngTolerance) ) << 493 (pPhi > fSPhi + fDPhi - kAngTolerance*0.5) ) in = kSurface ; 327 } 494 } 328 } 495 } 329 496 330 // Theta bondaries 497 // Theta bondaries 331 << 498 // (in!=kOutside) 332 if ( ((rho2 != 0.0) || (p.z() != 0.0)) && (! << 499 >> 500 if ( (rho2 || p.z()) && fDTheta < pi - kAngTolerance*0.5 ) 333 { 501 { 334 rho = std::sqrt(rho2); 502 rho = std::sqrt(rho2); 335 pTheta = std::atan2(rho,p.z()); 503 pTheta = std::atan2(rho,p.z()); 336 504 337 if ( in == kInside ) 505 if ( in == kInside ) 338 { 506 { 339 if ( ((fSTheta > 0.0) && (pTheta < fSThe << 507 if ( (pTheta < fSTheta + kAngTolerance*0.5) 340 || ((eTheta < pi) && (pTheta > eTheta << 508 || (pTheta > fSTheta + fDTheta - kAngTolerance*0.5) ) 341 { 509 { 342 if ( (( (fSTheta>0.0)&&(pTheta>=fSThet << 510 if ( (pTheta >= fSTheta - kAngTolerance*0.5) 343 || (fSTheta == 0.0) ) << 511 && (pTheta <= fSTheta + fDTheta + kAngTolerance*0.5) ) 344 && ((eTheta==pi)||(pTheta <= eTheta << 345 { 512 { 346 in = kSurface; << 513 in = kSurface ; 347 } 514 } 348 else 515 else 349 { 516 { 350 in = kOutside; << 517 in = kOutside ; 351 } 518 } 352 } 519 } 353 } 520 } 354 else 521 else 355 { 522 { 356 if ( ((fSTheta > 0.0)&&(pTheta < fSThe << 523 if ( (pTheta < fSTheta - kAngTolerance*0.5) 357 ||((eTheta < pi )&&(pTheta > eThet << 524 || (pTheta > fSTheta + fDTheta + kAngTolerance*0.5) ) 358 { 525 { 359 in = kOutside; << 526 in = kOutside ; 360 } 527 } 361 } 528 } 362 } 529 } 363 return in; 530 return in; 364 } 531 } 365 532 366 ////////////////////////////////////////////// 533 ///////////////////////////////////////////////////////////////////// 367 // 534 // 368 // Return unit normal of surface closest to p 535 // Return unit normal of surface closest to p 369 // - note if point on z axis, ignore phi divid 536 // - note if point on z axis, ignore phi divided sides 370 // - unsafe if point close to z axis a rmin=0 537 // - unsafe if point close to z axis a rmin=0 - no explicit checks 371 538 372 G4ThreeVector G4Sphere::SurfaceNormal( const G 539 G4ThreeVector G4Sphere::SurfaceNormal( const G4ThreeVector& p ) const 373 { 540 { 374 G4int noSurfaces = 0; << 375 G4double rho, rho2, radius, pTheta, pPhi=0.; << 376 G4double distRMin = kInfinity; << 377 G4double distSPhi = kInfinity, distEPhi = kI << 378 G4double distSTheta = kInfinity, distETheta << 379 G4ThreeVector nR, nPs, nPe, nTs, nTe, nZ(0., << 380 G4ThreeVector norm, sumnorm(0.,0.,0.); << 381 << 382 rho2 = p.x()*p.x()+p.y()*p.y(); << 383 radius = std::sqrt(rho2+p.z()*p.z()); << 384 rho = std::sqrt(rho2); << 385 << 386 G4double distRMax = std::fabs(radius-fRma << 387 if (fRmin != 0.0) distRMin = std::fabs(radi << 388 << 389 if ( (rho != 0.0) && !fFullSphere ) << 390 { << 391 pPhi = std::atan2(p.y(),p.x()); << 392 << 393 if (pPhi < fSPhi-halfAngTolerance) { p << 394 else if (pPhi > ePhi+halfAngTolerance) { p << 395 } << 396 if ( !fFullPhiSphere ) << 397 { << 398 if ( rho != 0.0 ) << 399 { << 400 distSPhi = std::fabs( pPhi-fSPhi ); << 401 distEPhi = std::fabs( pPhi-ePhi ); << 402 } << 403 else if( fRmin == 0.0 ) << 404 { << 405 distSPhi = 0.; << 406 distEPhi = 0.; << 407 } << 408 nPs = G4ThreeVector(sinSPhi,-cosSPhi,0); << 409 nPe = G4ThreeVector(-sinEPhi,cosEPhi,0); << 410 } << 411 if ( !fFullThetaSphere ) << 412 { << 413 if ( rho != 0.0 ) << 414 { << 415 pTheta = std::atan2(rho,p.z()); << 416 distSTheta = std::fabs(pTheta-fSTheta); << 417 distETheta = std::fabs(pTheta-eTheta); << 418 << 419 nTs = G4ThreeVector(-cosSTheta*p.x()/rho << 420 -cosSTheta*p.y()/rho << 421 sinSTheta << 422 << 423 nTe = G4ThreeVector( cosETheta*p.x()/rho << 424 cosETheta*p.y()/rho << 425 -sinETheta << 426 } << 427 else if( fRmin == 0.0 ) << 428 { << 429 if ( fSTheta != 0.0 ) << 430 { << 431 distSTheta = 0.; << 432 nTs = G4ThreeVector(0.,0.,-1.); << 433 } << 434 if ( eTheta < pi ) << 435 { << 436 distETheta = 0.; << 437 nTe = G4ThreeVector(0.,0.,1.); << 438 } << 439 } << 440 } << 441 if( radius != 0.0 ) { nR = G4ThreeVector(p. << 442 << 443 if( distRMax <= halfCarTolerance ) << 444 { << 445 ++noSurfaces; << 446 sumnorm += nR; << 447 } << 448 if( (fRmin != 0.0) && (distRMin <= halfCarTo << 449 { << 450 ++noSurfaces; << 451 sumnorm -= nR; << 452 } << 453 if( !fFullPhiSphere ) << 454 { << 455 if (distSPhi <= halfAngTolerance) << 456 { << 457 ++noSurfaces; << 458 sumnorm += nPs; << 459 } << 460 if (distEPhi <= halfAngTolerance) << 461 { << 462 ++noSurfaces; << 463 sumnorm += nPe; << 464 } << 465 } << 466 if ( !fFullThetaSphere ) << 467 { << 468 if ((distSTheta <= halfAngTolerance) && (f << 469 { << 470 ++noSurfaces; << 471 if ((radius <= halfCarTolerance) && fFul << 472 else << 473 } << 474 if ((distETheta <= halfAngTolerance) && (e << 475 { << 476 ++noSurfaces; << 477 if ((radius <= halfCarTolerance) && fFul << 478 else << 479 if(sumnorm.z() == 0.) { sumnorm += nZ; << 480 } << 481 } << 482 if ( noSurfaces == 0 ) << 483 { << 484 #ifdef G4CSGDEBUG << 485 G4Exception("G4Sphere::SurfaceNormal(p)", << 486 JustWarning, "Point p is not o << 487 #endif << 488 norm = ApproxSurfaceNormal(p); << 489 } << 490 else if ( noSurfaces == 1 ) { norm = sumnor << 491 else { norm = sumnor << 492 return norm; << 493 } << 494 << 495 << 496 ////////////////////////////////////////////// << 497 // << 498 // Algorithm for SurfaceNormal() following the << 499 // for points not on the surface << 500 << 501 G4ThreeVector G4Sphere::ApproxSurfaceNormal( c << 502 { << 503 ENorm side; 541 ENorm side; 504 G4ThreeVector norm; 542 G4ThreeVector norm; 505 G4double rho,rho2,radius,pPhi,pTheta; << 543 G4double rho,rho2,rad,pPhi,pTheta; 506 G4double distRMin,distRMax,distSPhi,distEPhi 544 G4double distRMin,distRMax,distSPhi,distEPhi, 507 distSTheta,distETheta,distMin; 545 distSTheta,distETheta,distMin; 508 546 509 rho2=p.x()*p.x()+p.y()*p.y(); 547 rho2=p.x()*p.x()+p.y()*p.y(); 510 radius=std::sqrt(rho2+p.z()*p.z()); << 548 rad=std::sqrt(rho2+p.z()*p.z()); 511 rho=std::sqrt(rho2); 549 rho=std::sqrt(rho2); 512 550 513 // 551 // 514 // Distance to r shells 552 // Distance to r shells 515 // 553 // 516 554 517 distRMax=std::fabs(radius-fRmax); << 555 distRMax=std::fabs(rad-fRmax); 518 if (fRmin != 0.0) << 556 if (fRmin) 519 { 557 { 520 distRMin=std::fabs(radius-fRmin); << 558 distRMin=std::fabs(rad-fRmin); 521 << 559 522 if (distRMin<distRMax) 560 if (distRMin<distRMax) 523 { 561 { 524 distMin=distRMin; 562 distMin=distRMin; 525 side=kNRMin; 563 side=kNRMin; 526 } 564 } 527 else 565 else 528 { << 566 { 529 distMin=distRMax; 567 distMin=distRMax; 530 side=kNRMax; 568 side=kNRMax; 531 } 569 } 532 } 570 } 533 else 571 else 534 { 572 { 535 distMin=distRMax; 573 distMin=distRMax; 536 side=kNRMax; 574 side=kNRMax; 537 } 575 } 538 576 539 // 577 // 540 // Distance to phi planes 578 // Distance to phi planes 541 // 579 // 542 // Protected against (0,0,z) << 580 // Protected against (0,0,z) 543 << 581 544 pPhi = std::atan2(p.y(),p.x()); 582 pPhi = std::atan2(p.y(),p.x()); 545 if (pPhi<0) { pPhi += twopi; } << 583 if (pPhi<0) pPhi += twopi; 546 584 547 if (!fFullPhiSphere && (rho != 0.0)) << 585 if (fDPhi<twopi&&rho) 548 { 586 { 549 if (fSPhi<0) 587 if (fSPhi<0) 550 { 588 { 551 distSPhi=std::fabs(pPhi-(fSPhi+twopi))*r 589 distSPhi=std::fabs(pPhi-(fSPhi+twopi))*rho; 552 } 590 } 553 else 591 else 554 { 592 { 555 distSPhi=std::fabs(pPhi-fSPhi)*rho; 593 distSPhi=std::fabs(pPhi-fSPhi)*rho; 556 } 594 } 557 595 558 distEPhi=std::fabs(pPhi-fSPhi-fDPhi)*rho; 596 distEPhi=std::fabs(pPhi-fSPhi-fDPhi)*rho; 559 597 560 // Find new minimum 598 // Find new minimum 561 // 599 // 562 if (distSPhi<distEPhi) 600 if (distSPhi<distEPhi) 563 { 601 { 564 if (distSPhi<distMin) 602 if (distSPhi<distMin) 565 { 603 { 566 distMin = distSPhi; << 604 distMin=distSPhi; 567 side = kNSPhi; << 605 side=kNSPhi; 568 } 606 } 569 } 607 } 570 else 608 else 571 { 609 { 572 if (distEPhi<distMin) 610 if (distEPhi<distMin) 573 { 611 { 574 distMin = distEPhi; << 612 distMin=distEPhi; 575 side = kNEPhi; << 613 side=kNEPhi; 576 } 614 } 577 } 615 } 578 } 616 } 579 617 580 // 618 // 581 // Distance to theta planes 619 // Distance to theta planes 582 // 620 // 583 621 584 if (!fFullThetaSphere && (radius != 0.0)) << 622 if (fDTheta<pi&&rad) 585 { 623 { 586 pTheta=std::atan2(rho,p.z()); 624 pTheta=std::atan2(rho,p.z()); 587 distSTheta=std::fabs(pTheta-fSTheta)*radiu << 625 distSTheta=std::fabs(pTheta-fSTheta)*rad; 588 distETheta=std::fabs(pTheta-fSTheta-fDThet << 626 distETheta=std::fabs(pTheta-fSTheta-fDTheta)*rad; 589 627 590 // Find new minimum 628 // Find new minimum 591 // 629 // 592 if (distSTheta<distETheta) 630 if (distSTheta<distETheta) 593 { 631 { 594 if (distSTheta<distMin) 632 if (distSTheta<distMin) 595 { 633 { 596 distMin = distSTheta ; 634 distMin = distSTheta ; 597 side = kNSTheta ; 635 side = kNSTheta ; 598 } 636 } 599 } 637 } 600 else 638 else 601 { 639 { 602 if (distETheta<distMin) 640 if (distETheta<distMin) 603 { 641 { 604 distMin = distETheta ; 642 distMin = distETheta ; 605 side = kNETheta ; 643 side = kNETheta ; 606 } 644 } 607 } 645 } 608 } 646 } 609 647 610 switch (side) 648 switch (side) 611 { 649 { 612 case kNRMin: // Inner radius 650 case kNRMin: // Inner radius 613 norm=G4ThreeVector(-p.x()/radius,-p.y()/ << 651 norm=G4ThreeVector(-p.x()/rad,-p.y()/rad,-p.z()/rad); 614 break; 652 break; 615 case kNRMax: // Outer radius 653 case kNRMax: // Outer radius 616 norm=G4ThreeVector(p.x()/radius,p.y()/ra << 654 norm=G4ThreeVector(p.x()/rad,p.y()/rad,p.z()/rad); 617 break; 655 break; 618 case kNSPhi: 656 case kNSPhi: 619 norm=G4ThreeVector(sinSPhi,-cosSPhi,0); << 657 norm=G4ThreeVector(std::sin(fSPhi),-std::cos(fSPhi),0); 620 break; 658 break; 621 case kNEPhi: 659 case kNEPhi: 622 norm=G4ThreeVector(-sinEPhi,cosEPhi,0); << 660 norm=G4ThreeVector(-std::sin(fSPhi+fDPhi),std::cos(fSPhi+fDPhi),0); 623 break; 661 break; 624 case kNSTheta: 662 case kNSTheta: 625 norm=G4ThreeVector(-cosSTheta*std::cos(p << 663 norm=G4ThreeVector(-std::cos(fSTheta)*std::cos(pPhi), 626 -cosSTheta*std::sin(p << 664 -std::cos(fSTheta)*std::sin(pPhi), 627 sinSTheta << 665 std::sin(fSTheta) ); >> 666 // G4cout<<G4endl<<" case kNSTheta:"<<G4endl; >> 667 // G4cout<<"pPhi = "<<pPhi<<G4endl; >> 668 // G4cout<<"rad = "<<rad<<G4endl; >> 669 // G4cout<<"pho = "<<rho<<G4endl; >> 670 // G4cout<<"p: "<<p.x()<<"; "<<p.y()<<"; "<<p.z()<<G4endl; >> 671 // G4cout<<"norm: "<<norm.x()<<"; "<<norm.y()<<"; "<<norm.z()<<G4endl; 628 break; 672 break; 629 case kNETheta: 673 case kNETheta: 630 norm=G4ThreeVector( cosETheta*std::cos(p << 674 norm=G4ThreeVector( std::cos(fSTheta+fDTheta)*std::cos(pPhi), 631 cosETheta*std::sin(p << 675 std::cos(fSTheta+fDTheta)*std::sin(pPhi), 632 -sinETheta << 676 -std::sin(fSTheta+fDTheta) ); >> 677 >> 678 // G4cout<<G4endl<<" case kNETheta:"<<G4endl; >> 679 // G4cout<<"pPhi = "<<pPhi<<G4endl; >> 680 // G4cout<<"rad = "<<rad<<G4endl; >> 681 // G4cout<<"pho = "<<rho<<G4endl; >> 682 // G4cout<<"p: "<<p.x()<<"; "<<p.y()<<"; "<<p.z()<<G4endl; >> 683 // G4cout<<"norm: "<<norm.x()<<"; "<<norm.y()<<"; "<<norm.z()<<G4endl; 633 break; 684 break; 634 default: // Should never reach th << 685 default: 635 DumpInfo(); 686 DumpInfo(); 636 G4Exception("G4Sphere::ApproxSurfaceNorm << 687 G4Exception("G4Sphere::SurfaceNormal()", "Notification", JustWarning, 637 "GeomSolids1002", JustWarnin << 638 "Undefined side for valid su 688 "Undefined side for valid surface normal to solid."); 639 break; << 689 break; 640 } << 690 } // end case 641 691 642 return norm; 692 return norm; 643 } 693 } 644 694 645 ////////////////////////////////////////////// 695 /////////////////////////////////////////////////////////////////////////////// 646 // 696 // 647 // Calculate distance to shape from outside, a 697 // Calculate distance to shape from outside, along normalised vector 648 // - return kInfinity if no intersection, or i 698 // - return kInfinity if no intersection, or intersection distance <= tolerance 649 // 699 // 650 // -> If point is outside outer radius, comput 700 // -> If point is outside outer radius, compute intersection with rmax 651 // - if no intersection return 701 // - if no intersection return 652 // - if valid phi,theta return interse 702 // - if valid phi,theta return intersection Dist 653 // 703 // 654 // -> If shell, compute intersection with inne 704 // -> If shell, compute intersection with inner radius, taking largest +ve root 655 // - if valid phi,theta, save intersect 705 // - if valid phi,theta, save intersection 656 // 706 // 657 // -> If phi segmented, compute intersection w 707 // -> If phi segmented, compute intersection with phi half planes 658 // - if valid intersection(r,theta), re 708 // - if valid intersection(r,theta), return smallest intersection of 659 // inner shell & phi intersection 709 // inner shell & phi intersection 660 // 710 // 661 // -> If theta segmented, compute intersection 711 // -> If theta segmented, compute intersection with theta cones 662 // - if valid intersection(r,phi), retu 712 // - if valid intersection(r,phi), return smallest intersection of 663 // inner shell & theta intersection 713 // inner shell & theta intersection 664 // 714 // 665 // 715 // 666 // NOTE: 716 // NOTE: 667 // - `if valid' (above) implies tolerant check 717 // - `if valid' (above) implies tolerant checking of intersection points 668 // 718 // 669 // OPT: 719 // OPT: 670 // Move tolIO/ORmin/RMax2 precalcs to where th 720 // Move tolIO/ORmin/RMax2 precalcs to where they are needed - 671 // not required for most cases. 721 // not required for most cases. 672 // Avoid atan2 for non theta cut G4Sphere. 722 // Avoid atan2 for non theta cut G4Sphere. 673 723 674 G4double G4Sphere::DistanceToIn( const G4Three 724 G4double G4Sphere::DistanceToIn( const G4ThreeVector& p, 675 const G4Three 725 const G4ThreeVector& v ) const 676 { 726 { 677 G4double snxt = kInfinity ; // snxt = d 727 G4double snxt = kInfinity ; // snxt = default return value >> 728 678 G4double rho2, rad2, pDotV2d, pDotV3d, pThet 729 G4double rho2, rad2, pDotV2d, pDotV3d, pTheta ; 679 G4double tolSTheta=0., tolETheta=0. ; << 680 const G4double dRmax = 100.*fRmax; << 681 730 682 const G4double halfRmaxTolerance = fRmaxTole << 731 G4double tolIRMin2, tolORMin2, tolORMax2, tolIRMax2 ; 683 const G4double halfRminTolerance = fRminTole << 732 G4double tolSTheta=0., tolETheta=0. ; 684 const G4double tolORMin2 = (fRmin>halfRminTo << 685 ? (fRmin-halfRminTolerance)*(fR << 686 const G4double tolIRMin2 = << 687 (fRmin+halfRminTolerance)*(fRmi << 688 const G4double tolORMax2 = << 689 (fRmax+halfRmaxTolerance)*(fRma << 690 const G4double tolIRMax2 = << 691 (fRmax-halfRmaxTolerance)*(fRma << 692 733 693 // Intersection point 734 // Intersection point 694 // << 735 695 G4double xi, yi, zi, rhoi, rhoi2, radi2, iTh 736 G4double xi, yi, zi, rhoi, rhoi2, radi2, iTheta ; 696 737 697 // Phi intersection 738 // Phi intersection 698 // << 699 G4double Comp ; << 700 739 701 // Phi precalcs << 740 G4double sinSPhi, cosSPhi, ePhi, sinEPhi, cosEPhi , Comp ; 702 // << 741 >> 742 // Phi flag and precalcs >> 743 >> 744 G4bool segPhi ; >> 745 G4double hDPhi, hDPhiOT, hDPhiIT, cPhi, sinCPhi=0., cosCPhi=0. ; >> 746 G4double cosHDPhiOT=0., cosHDPhiIT=0. ; 703 G4double Dist, cosPsi ; 747 G4double Dist, cosPsi ; 704 748 705 // Theta precalcs << 749 G4bool segTheta ; // Theta flag and precals 706 // << 750 G4double tanSTheta, tanETheta ; >> 751 G4double tanSTheta2, tanETheta2 ; 707 G4double dist2STheta, dist2ETheta ; 752 G4double dist2STheta, dist2ETheta ; 708 G4double t1, t2, b, c, d2, d, sd = kInfinity << 753 G4double t1, t2, b, c, d2, d, s = kInfinity ; 709 754 710 // General Precalcs 755 // General Precalcs 711 // << 756 712 rho2 = p.x()*p.x() + p.y()*p.y() ; 757 rho2 = p.x()*p.x() + p.y()*p.y() ; 713 rad2 = rho2 + p.z()*p.z() ; 758 rad2 = rho2 + p.z()*p.z() ; 714 pTheta = std::atan2(std::sqrt(rho2),p.z()) ; 759 pTheta = std::atan2(std::sqrt(rho2),p.z()) ; 715 760 716 pDotV2d = p.x()*v.x() + p.y()*v.y() ; 761 pDotV2d = p.x()*v.x() + p.y()*v.y() ; 717 pDotV3d = pDotV2d + p.z()*v.z() ; 762 pDotV3d = pDotV2d + p.z()*v.z() ; 718 763 719 // Theta precalcs << 764 // Radial Precalcs 720 // << 765 721 if (!fFullThetaSphere) << 766 if (fRmin > kRadTolerance*0.5) 722 { 767 { 723 tolSTheta = fSTheta - halfAngTolerance ; << 768 tolORMin2=(fRmin-kRadTolerance*0.5)*(fRmin-kRadTolerance*0.5); 724 tolETheta = eTheta + halfAngTolerance ; << 769 } >> 770 else >> 771 { >> 772 tolORMin2 = 0 ; >> 773 } >> 774 tolIRMin2 = (fRmin+kRadTolerance*0.5)*(fRmin+kRadTolerance*0.5) ; >> 775 tolORMax2 = (fRmax+kRadTolerance*0.5)*(fRmax+kRadTolerance*0.5) ; >> 776 tolIRMax2 = (fRmax-kRadTolerance*0.5)*(fRmax-kRadTolerance*0.5) ; 725 777 726 // Special case rad2 = 0 comparing with di << 778 // Set phi divided flag and precalcs 727 // << 779 728 if ((rad2!=0.0) || (fRmin!=0.0)) << 780 if (fDPhi < twopi) 729 { << 781 { 730 // Keep going for computation of distanc << 782 segPhi = true ; 731 } << 783 hDPhi = 0.5*fDPhi ; // half delta phi 732 else // Positioned on the sphere's origin << 784 cPhi = fSPhi + hDPhi ; 733 { << 785 734 G4double vTheta = std::atan2(std::sqrt(v << 786 hDPhiOT = hDPhi+0.5*kAngTolerance; // Outer Tolerant half delta phi 735 if ( (vTheta < tolSTheta) || (vTheta > t << 787 hDPhiIT = hDPhi-0.5*kAngTolerance; 736 { << 788 737 return snxt ; // kInfinity << 789 sinCPhi = std::sin(cPhi) ; 738 } << 790 cosCPhi = std::cos(cPhi) ; 739 return snxt = 0.0 ; << 791 cosHDPhiOT = std::cos(hDPhiOT) ; 740 } << 792 cosHDPhiIT = std::cos(hDPhiIT) ; >> 793 } >> 794 else >> 795 { >> 796 segPhi = false ; >> 797 } >> 798 >> 799 // Theta precalcs >> 800 >> 801 if (fDTheta < pi ) >> 802 { >> 803 segTheta = true ; >> 804 tolSTheta = fSTheta - kAngTolerance*0.5 ; >> 805 tolETheta = fSTheta + fDTheta + kAngTolerance*0.5 ; >> 806 } >> 807 else >> 808 { >> 809 segTheta = false ; 741 } 810 } 742 811 743 // Outer spherical shell intersection 812 // Outer spherical shell intersection 744 // - Only if outside tolerant fRmax 813 // - Only if outside tolerant fRmax 745 // - Check for if inside and outer G4Sphere 814 // - Check for if inside and outer G4Sphere heading through solid (-> 0) 746 // - No intersect -> no intersection with G4 815 // - No intersect -> no intersection with G4Sphere 747 // 816 // 748 // Shell eqn: x^2+y^2+z^2=RSPH^2 817 // Shell eqn: x^2+y^2+z^2=RSPH^2 749 // 818 // 750 // => (px+svx)^2+(py+svy)^2+(pz+svz)^2=R^2 819 // => (px+svx)^2+(py+svy)^2+(pz+svz)^2=R^2 751 // 820 // 752 // => (px^2+py^2+pz^2) +2sd(pxvx+pyvy+pzvz)+ << 821 // => (px^2+py^2+pz^2) +2s(pxvx+pyvy+pzvz)+s^2(vx^2+vy^2+vz^2)=R^2 753 // => rad2 +2sd(pDotV3d) + << 822 // => rad2 +2s(pDotV3d) +s^2 =R^2 754 // 823 // 755 // => sd=-pDotV3d+-std::sqrt(pDotV3d^2-(rad2 << 824 // => s=-pDotV3d+-std::sqrt(pDotV3d^2-(rad2-R^2)) 756 825 757 c = rad2 - fRmax*fRmax ; 826 c = rad2 - fRmax*fRmax ; >> 827 const G4double flexRadMaxTolerance = // kRadTolerance; >> 828 std::max(kRadTolerance, fEpsilon * fRmax); 758 829 759 if (c > fRmaxTolerance*fRmax) << 830 // if (c > kRadTolerance*fRmax) >> 831 if (c > flexRadMaxTolerance*fRmax) 760 { 832 { 761 // If outside tolerant boundary of outer G << 833 // If outside toleranct boundary of outer G4Sphere 762 // [should be std::sqrt(rad2)-fRmax > half << 834 // [should be std::sqrt(rad2)-fRmax > kRadTolerance*0.5] 763 835 764 d2 = pDotV3d*pDotV3d - c ; 836 d2 = pDotV3d*pDotV3d - c ; 765 837 766 if ( d2 >= 0 ) 838 if ( d2 >= 0 ) 767 { 839 { 768 sd = -pDotV3d - std::sqrt(d2) ; << 840 s = -pDotV3d - std::sqrt(d2) ; 769 841 770 if (sd >= 0 ) << 842 if (s >= 0 ) 771 { 843 { 772 if ( sd>dRmax ) // Avoid rounding erro << 844 xi = p.x() + s*v.x() ; 773 { // 64 bits systems. Sp << 845 yi = p.y() + s*v.y() ; 774 G4double fTerm = sd-std::fmod(sd,dRm << 775 sd = fTerm + DistanceToIn(p+fTerm*v, << 776 } << 777 xi = p.x() + sd*v.x() ; << 778 yi = p.y() + sd*v.y() ; << 779 rhoi = std::sqrt(xi*xi + yi*yi) ; 846 rhoi = std::sqrt(xi*xi + yi*yi) ; 780 847 781 if (!fFullPhiSphere && (rhoi != 0.0)) << 848 if (segPhi && rhoi) // Check phi intersection 782 { 849 { 783 cosPsi = (xi*cosCPhi + yi*sinCPhi)/r 850 cosPsi = (xi*cosCPhi + yi*sinCPhi)/rhoi ; 784 851 785 if (cosPsi >= cosHDPhiOT) 852 if (cosPsi >= cosHDPhiOT) 786 { 853 { 787 if (!fFullThetaSphere) // Check << 854 if (segTheta) // Check theta intersection 788 { 855 { 789 zi = p.z() + sd*v.z() ; << 856 zi = p.z() + s*v.z() ; 790 857 791 // rhoi & zi can never both be 0 858 // rhoi & zi can never both be 0 792 // (=>intersect at origin =>fRma 859 // (=>intersect at origin =>fRmax=0) 793 // 860 // 794 iTheta = std::atan2(rhoi,zi) ; 861 iTheta = std::atan2(rhoi,zi) ; 795 if ( (iTheta >= tolSTheta) && (i 862 if ( (iTheta >= tolSTheta) && (iTheta <= tolETheta) ) 796 { 863 { 797 return snxt = sd ; << 864 return snxt = s ; 798 } 865 } 799 } 866 } 800 else 867 else 801 { 868 { 802 return snxt=sd; << 869 return snxt=s; 803 } 870 } 804 } 871 } 805 } 872 } 806 else 873 else 807 { 874 { 808 if (!fFullThetaSphere) // Check t << 875 if (segTheta) // Check theta intersection 809 { 876 { 810 zi = p.z() + sd*v.z() ; << 877 zi = p.z() + s*v.z() ; 811 878 812 // rhoi & zi can never both be 0 879 // rhoi & zi can never both be 0 813 // (=>intersect at origin => fRmax 880 // (=>intersect at origin => fRmax=0 !) 814 // 881 // 815 iTheta = std::atan2(rhoi,zi) ; 882 iTheta = std::atan2(rhoi,zi) ; 816 if ( (iTheta >= tolSTheta) && (iTh 883 if ( (iTheta >= tolSTheta) && (iTheta <= tolETheta) ) 817 { 884 { 818 return snxt=sd; << 885 return snxt=s; 819 } 886 } 820 } 887 } 821 else 888 else 822 { 889 { 823 return snxt = sd; << 890 return snxt = s ; 824 } 891 } 825 } << 892 } 826 } 893 } 827 } 894 } 828 else // No intersection with G4Sphere 895 else // No intersection with G4Sphere 829 { 896 { 830 return snxt=kInfinity; 897 return snxt=kInfinity; 831 } 898 } 832 } 899 } 833 else 900 else 834 { 901 { 835 // Inside outer radius 902 // Inside outer radius 836 // check not inside, and heading through G 903 // check not inside, and heading through G4Sphere (-> 0 to in) 837 904 838 d2 = pDotV3d*pDotV3d - c ; 905 d2 = pDotV3d*pDotV3d - c ; 839 906 840 if ( (rad2 > tolIRMax2) << 907 // if (rad2 > tolIRMin2 && pDotV3d < 0 ) 841 && ( (d2 >= fRmaxTolerance*fRmax) && (pD << 908 >> 909 if (rad2 > tolIRMax2 && ( d2 >= flexRadMaxTolerance*fRmax && pDotV3d < 0 ) ) 842 { 910 { 843 if (!fFullPhiSphere) << 911 if (segPhi) 844 { 912 { 845 // Use inner phi tolerant boundary -> 913 // Use inner phi tolerant boundary -> if on tolerant 846 // phi boundaries, phi intersect code 914 // phi boundaries, phi intersect code handles leaving/entering checks 847 915 848 cosPsi = (p.x()*cosCPhi + p.y()*sinCPh 916 cosPsi = (p.x()*cosCPhi + p.y()*sinCPhi)/std::sqrt(rho2) ; 849 917 850 if (cosPsi>=cosHDPhiIT) 918 if (cosPsi>=cosHDPhiIT) 851 { << 919 { 852 // inside radii, delta r -ve, inside 920 // inside radii, delta r -ve, inside phi 853 921 854 if ( !fFullThetaSphere ) << 922 if (segTheta) 855 { 923 { 856 if ( (pTheta >= tolSTheta + kAngTo 924 if ( (pTheta >= tolSTheta + kAngTolerance) 857 && (pTheta <= tolETheta - kAngTo 925 && (pTheta <= tolETheta - kAngTolerance) ) 858 { 926 { 859 return snxt=0; 927 return snxt=0; 860 } 928 } 861 } 929 } 862 else // strictly inside Theta in 930 else // strictly inside Theta in both cases 863 { 931 { 864 return snxt=0; 932 return snxt=0; 865 } 933 } 866 } 934 } 867 } 935 } 868 else 936 else 869 { 937 { 870 if ( !fFullThetaSphere ) << 938 if ( segTheta ) 871 { 939 { 872 if ( (pTheta >= tolSTheta + kAngTole 940 if ( (pTheta >= tolSTheta + kAngTolerance) 873 && (pTheta <= tolETheta - kAngTole 941 && (pTheta <= tolETheta - kAngTolerance) ) 874 { 942 { 875 return snxt=0; 943 return snxt=0; 876 } 944 } 877 } 945 } 878 else // strictly inside Theta in bot 946 else // strictly inside Theta in both cases 879 { 947 { 880 return snxt=0; 948 return snxt=0; 881 } 949 } 882 } 950 } 883 } 951 } 884 } 952 } 885 953 886 // Inner spherical shell intersection 954 // Inner spherical shell intersection 887 // - Always farthest root, because would hav 955 // - Always farthest root, because would have passed through outer 888 // surface first. 956 // surface first. 889 // - Tolerant check if travelling through so << 957 // - Tolerant check for if travelling through solid 890 958 891 if (fRmin != 0.0) << 959 if (fRmin) 892 { 960 { 893 c = rad2 - fRmin*fRmin ; 961 c = rad2 - fRmin*fRmin ; 894 d2 = pDotV3d*pDotV3d - c ; 962 d2 = pDotV3d*pDotV3d - c ; 895 963 896 // Within tolerance inner radius of inner 964 // Within tolerance inner radius of inner G4Sphere 897 // Check for immediate entry/already insid 965 // Check for immediate entry/already inside and travelling outwards 898 966 899 if ( (c > -halfRminTolerance) && (rad2 < t << 967 // if (c >- kRadTolerance*0.5 && pDotV3d >= 0 && rad2 < tolIRMin2 ) 900 && ( (d2 < fRmin*kCarTolerance) || (pDot << 968 >> 969 if ( c > -kRadTolerance*0.5 && rad2 < tolIRMin2 && >> 970 ( d2 < fRmin*kCarTolerance || pDotV3d >= 0 ) ) 901 { 971 { 902 if ( !fFullPhiSphere ) << 972 if (segPhi) 903 { 973 { 904 // Use inner phi tolerant boundary -> 974 // Use inner phi tolerant boundary -> if on tolerant 905 // phi boundaries, phi intersect code 975 // phi boundaries, phi intersect code handles leaving/entering checks 906 976 907 cosPsi = (p.x()*cosCPhi+p.y()*sinCPhi) 977 cosPsi = (p.x()*cosCPhi+p.y()*sinCPhi)/std::sqrt(rho2) ; 908 if (cosPsi >= cosHDPhiIT) 978 if (cosPsi >= cosHDPhiIT) 909 { << 979 { 910 // inside radii, delta r -ve, inside 980 // inside radii, delta r -ve, inside phi 911 // 981 // 912 if ( !fFullThetaSphere ) << 982 if (segTheta) 913 { 983 { 914 if ( (pTheta >= tolSTheta + kAngTo 984 if ( (pTheta >= tolSTheta + kAngTolerance) 915 && (pTheta <= tolETheta - kAngTo 985 && (pTheta <= tolETheta - kAngTolerance) ) 916 { 986 { 917 return snxt=0; 987 return snxt=0; 918 } 988 } 919 } 989 } 920 else 990 else 921 { 991 { 922 return snxt = 0 ; 992 return snxt = 0 ; 923 } 993 } 924 } 994 } 925 } 995 } 926 else 996 else 927 { 997 { 928 if ( !fFullThetaSphere ) << 998 if (segTheta) 929 { 999 { 930 if ( (pTheta >= tolSTheta + kAngTole 1000 if ( (pTheta >= tolSTheta + kAngTolerance) 931 && (pTheta <= tolETheta - kAngTole 1001 && (pTheta <= tolETheta - kAngTolerance) ) 932 { 1002 { 933 return snxt = 0 ; 1003 return snxt = 0 ; 934 } 1004 } 935 } 1005 } 936 else 1006 else 937 { 1007 { 938 return snxt=0; 1008 return snxt=0; 939 } 1009 } 940 } 1010 } 941 } 1011 } 942 else // Not special tolerant case 1012 else // Not special tolerant case 943 { 1013 { >> 1014 // d2 = pDotV3d*pDotV3d - c ; >> 1015 944 if (d2 >= 0) 1016 if (d2 >= 0) 945 { 1017 { 946 sd = -pDotV3d + std::sqrt(d2) ; << 1018 s = -pDotV3d + std::sqrt(d2) ; 947 if ( sd >= halfRminTolerance ) // It << 1019 if ( s >= kRadTolerance*0.5 ) // It was >= 0 ?? 948 { 1020 { 949 xi = p.x() + sd*v.x() ; << 1021 xi = p.x() + s*v.x() ; 950 yi = p.y() + sd*v.y() ; << 1022 yi = p.y() + s*v.y() ; 951 rhoi = std::sqrt(xi*xi+yi*yi) ; 1023 rhoi = std::sqrt(xi*xi+yi*yi) ; 952 1024 953 if ( !fFullPhiSphere && (rhoi != 0.0 << 1025 if ( segPhi && rhoi ) // Check phi intersection 954 { 1026 { 955 cosPsi = (xi*cosCPhi + yi*sinCPhi) 1027 cosPsi = (xi*cosCPhi + yi*sinCPhi)/rhoi ; 956 1028 957 if (cosPsi >= cosHDPhiOT) 1029 if (cosPsi >= cosHDPhiOT) 958 { 1030 { 959 if ( !fFullThetaSphere ) // Che << 1031 if (segTheta) // Check theta intersection 960 { 1032 { 961 zi = p.z() + sd*v.z() ; << 1033 zi = p.z() + s*v.z() ; 962 1034 963 // rhoi & zi can never both be 1035 // rhoi & zi can never both be 0 964 // (=>intersect at origin =>fR 1036 // (=>intersect at origin =>fRmax=0) 965 // 1037 // 966 iTheta = std::atan2(rhoi,zi) ; 1038 iTheta = std::atan2(rhoi,zi) ; 967 if ( (iTheta >= tolSTheta) && 1039 if ( (iTheta >= tolSTheta) && (iTheta<=tolETheta) ) 968 { 1040 { 969 snxt = sd; << 1041 snxt = s ; 970 } 1042 } 971 } 1043 } 972 else 1044 else 973 { 1045 { 974 snxt=sd; << 1046 snxt=s; 975 } 1047 } 976 } 1048 } 977 } 1049 } 978 else 1050 else 979 { 1051 { 980 if ( !fFullThetaSphere ) // Chec << 1052 if (segTheta) // Check theta intersection 981 { 1053 { 982 zi = p.z() + sd*v.z() ; << 1054 zi = p.z() + s*v.z() ; 983 1055 984 // rhoi & zi can never both be 0 1056 // rhoi & zi can never both be 0 985 // (=>intersect at origin => fRm 1057 // (=>intersect at origin => fRmax=0 !) 986 // 1058 // 987 iTheta = std::atan2(rhoi,zi) ; 1059 iTheta = std::atan2(rhoi,zi) ; 988 if ( (iTheta >= tolSTheta) && (i 1060 if ( (iTheta >= tolSTheta) && (iTheta <= tolETheta) ) 989 { 1061 { 990 snxt = sd; << 1062 snxt = s ; 991 } 1063 } 992 } 1064 } 993 else 1065 else 994 { 1066 { 995 snxt = sd; << 1067 snxt=s; 996 } 1068 } 997 } 1069 } 998 } 1070 } 999 } 1071 } 1000 } 1072 } 1001 } 1073 } 1002 1074 1003 // Phi segment intersection 1075 // Phi segment intersection 1004 // 1076 // 1005 // o Tolerant of points inside phi planes b 1077 // o Tolerant of points inside phi planes by up to kCarTolerance*0.5 1006 // 1078 // 1007 // o NOTE: Large duplication of code betwee 1079 // o NOTE: Large duplication of code between sphi & ephi checks 1008 // -> only diffs: sphi -> ephi, Com 1080 // -> only diffs: sphi -> ephi, Comp -> -Comp and half-plane 1009 // intersection check <=0 -> >=0 1081 // intersection check <=0 -> >=0 1010 // -> Should use some form of loop 1082 // -> Should use some form of loop Construct 1011 // 1083 // 1012 if ( !fFullPhiSphere ) << 1084 if ( segPhi ) 1013 { 1085 { 1014 // First phi surface ('S'tarting phi) << 1086 // First phi surface (`S'tarting phi) >> 1087 >> 1088 sinSPhi = std::sin(fSPhi) ; >> 1089 cosSPhi = std::cos(fSPhi) ; >> 1090 1015 // Comp = Component in outwards normal di 1091 // Comp = Component in outwards normal dirn 1016 // 1092 // 1017 Comp = v.x()*sinSPhi - v.y()*cosSPhi ; << 1093 Comp = v.x()*sinSPhi - v.y()*cosSPhi ; 1018 << 1094 1019 if ( Comp < 0 ) 1095 if ( Comp < 0 ) 1020 { 1096 { 1021 Dist = p.y()*cosSPhi - p.x()*sinSPhi ; 1097 Dist = p.y()*cosSPhi - p.x()*sinSPhi ; 1022 1098 1023 if (Dist < halfCarTolerance) << 1099 if (Dist < kCarTolerance*0.5) 1024 { 1100 { 1025 sd = Dist/Comp ; << 1101 s = Dist/Comp ; 1026 1102 1027 if (sd < snxt) << 1103 if (s < snxt) 1028 { 1104 { 1029 if ( sd > 0 ) << 1105 if ( s > 0 ) 1030 { 1106 { 1031 xi = p.x() + sd*v.x() ; << 1107 xi = p.x() + s*v.x() ; 1032 yi = p.y() + sd*v.y() ; << 1108 yi = p.y() + s*v.y() ; 1033 zi = p.z() + sd*v.z() ; << 1109 zi = p.z() + s*v.z() ; 1034 rhoi2 = xi*xi + yi*yi ; 1110 rhoi2 = xi*xi + yi*yi ; 1035 radi2 = rhoi2 + zi*zi ; 1111 radi2 = rhoi2 + zi*zi ; 1036 } 1112 } 1037 else 1113 else 1038 { 1114 { 1039 sd = 0 ; << 1115 s = 0 ; 1040 xi = p.x() ; 1116 xi = p.x() ; 1041 yi = p.y() ; 1117 yi = p.y() ; 1042 zi = p.z() ; 1118 zi = p.z() ; 1043 rhoi2 = rho2 ; 1119 rhoi2 = rho2 ; 1044 radi2 = rad2 ; 1120 radi2 = rad2 ; 1045 } 1121 } 1046 if ( (radi2 <= tolORMax2) 1122 if ( (radi2 <= tolORMax2) 1047 && (radi2 >= tolORMin2) 1123 && (radi2 >= tolORMin2) 1048 && ((yi*cosCPhi-xi*sinCPhi) <= 0) 1124 && ((yi*cosCPhi-xi*sinCPhi) <= 0) ) 1049 { 1125 { 1050 // Check theta intersection 1126 // Check theta intersection 1051 // rhoi & zi can never both be 0 1127 // rhoi & zi can never both be 0 1052 // (=>intersect at origin =>fRmax 1128 // (=>intersect at origin =>fRmax=0) 1053 // 1129 // 1054 if ( !fFullThetaSphere ) << 1130 if ( segTheta ) 1055 { 1131 { 1056 iTheta = std::atan2(std::sqrt(r 1132 iTheta = std::atan2(std::sqrt(rhoi2),zi) ; 1057 if ( (iTheta >= tolSTheta) && ( 1133 if ( (iTheta >= tolSTheta) && (iTheta <= tolETheta) ) 1058 { 1134 { 1059 // r and theta intersections 1135 // r and theta intersections good 1060 // - check intersecting with 1136 // - check intersecting with correct half-plane 1061 1137 1062 if ((yi*cosCPhi-xi*sinCPhi) < 1138 if ((yi*cosCPhi-xi*sinCPhi) <= 0) 1063 { 1139 { 1064 snxt = sd; << 1140 snxt = s ; 1065 } 1141 } 1066 } 1142 } 1067 } 1143 } 1068 else 1144 else 1069 { 1145 { 1070 snxt = sd; << 1146 snxt = s ; 1071 } 1147 } 1072 } 1148 } 1073 } 1149 } 1074 } 1150 } 1075 } 1151 } 1076 1152 1077 // Second phi surface ('E'nding phi) << 1153 // Second phi surface (`E'nding phi) 1078 // Component in outwards normal dirn << 1154 >> 1155 ePhi = fSPhi + fDPhi ; >> 1156 sinEPhi = std::sin(ePhi) ; >> 1157 cosEPhi = std::cos(ePhi) ; 1079 1158 1080 Comp = -( v.x()*sinEPhi-v.y()*cosEPhi ) ; << 1159 // Compnent in outwards normal dirn 1081 1160 >> 1161 Comp = -( v.x()*sinEPhi-v.y()*cosEPhi ) ; >> 1162 1082 if (Comp < 0) 1163 if (Comp < 0) 1083 { 1164 { 1084 Dist = -(p.y()*cosEPhi-p.x()*sinEPhi) ; 1165 Dist = -(p.y()*cosEPhi-p.x()*sinEPhi) ; 1085 if ( Dist < halfCarTolerance ) << 1166 if ( Dist < kCarTolerance*0.5 ) 1086 { 1167 { 1087 sd = Dist/Comp ; << 1168 s = Dist/Comp ; 1088 1169 1089 if ( sd < snxt ) << 1170 if ( s < snxt ) 1090 { 1171 { 1091 if (sd > 0) << 1172 if (s > 0) 1092 { 1173 { 1093 xi = p.x() + sd*v.x() ; << 1174 xi = p.x() + s*v.x() ; 1094 yi = p.y() + sd*v.y() ; << 1175 yi = p.y() + s*v.y() ; 1095 zi = p.z() + sd*v.z() ; << 1176 zi = p.z() + s*v.z() ; 1096 rhoi2 = xi*xi + yi*yi ; 1177 rhoi2 = xi*xi + yi*yi ; 1097 radi2 = rhoi2 + zi*zi ; 1178 radi2 = rhoi2 + zi*zi ; 1098 } 1179 } 1099 else 1180 else 1100 { 1181 { 1101 sd = 0 ; << 1182 s = 0 ; 1102 xi = p.x() ; 1183 xi = p.x() ; 1103 yi = p.y() ; 1184 yi = p.y() ; 1104 zi = p.z() ; 1185 zi = p.z() ; 1105 rhoi2 = rho2 ; 1186 rhoi2 = rho2 ; 1106 radi2 = rad2 ; 1187 radi2 = rad2 ; 1107 } << 1188 } if ( (radi2 <= tolORMax2) 1108 if ( (radi2 <= tolORMax2) << 1109 && (radi2 >= tolORMin2) 1189 && (radi2 >= tolORMin2) 1110 && ((yi*cosCPhi-xi*sinCPhi) >= 0) 1190 && ((yi*cosCPhi-xi*sinCPhi) >= 0) ) 1111 { 1191 { 1112 // Check theta intersection 1192 // Check theta intersection 1113 // rhoi & zi can never both be 0 1193 // rhoi & zi can never both be 0 1114 // (=>intersect at origin =>fRmax 1194 // (=>intersect at origin =>fRmax=0) 1115 // 1195 // 1116 if ( !fFullThetaSphere ) << 1196 if ( segTheta ) 1117 { 1197 { 1118 iTheta = std::atan2(std::sqrt(r 1198 iTheta = std::atan2(std::sqrt(rhoi2),zi) ; 1119 if ( (iTheta >= tolSTheta) && ( 1199 if ( (iTheta >= tolSTheta) && (iTheta <= tolETheta) ) 1120 { 1200 { 1121 // r and theta intersections 1201 // r and theta intersections good 1122 // - check intersecting with 1202 // - check intersecting with correct half-plane 1123 1203 1124 if ((yi*cosCPhi-xi*sinCPhi) > 1204 if ((yi*cosCPhi-xi*sinCPhi) >= 0) 1125 { 1205 { 1126 snxt = sd; << 1206 snxt = s ; 1127 } 1207 } 1128 } 1208 } 1129 } 1209 } 1130 else 1210 else 1131 { 1211 { 1132 snxt = sd; << 1212 snxt = s ; 1133 } 1213 } 1134 } 1214 } 1135 } 1215 } 1136 } 1216 } 1137 } 1217 } 1138 } 1218 } 1139 1219 1140 // Theta segment intersection 1220 // Theta segment intersection 1141 1221 1142 if ( !fFullThetaSphere ) << 1222 if ( segTheta ) 1143 { 1223 { 1144 1224 1145 // Intersection with theta surfaces 1225 // Intersection with theta surfaces 1146 // Known failure cases: 1226 // Known failure cases: 1147 // o Inside tolerance of stheta surface, 1227 // o Inside tolerance of stheta surface, skim 1148 // ~parallel to cone and Hit & enter e 1228 // ~parallel to cone and Hit & enter etheta surface [& visa versa] 1149 // 1229 // 1150 // To solve: Check 2nd root of etheta 1230 // To solve: Check 2nd root of etheta surface in addition to stheta 1151 // 1231 // 1152 // o start/end theta is exactly pi/2 << 1232 // o start/end theta is exactly pi/2 1153 // Intersections with cones 1233 // Intersections with cones 1154 // 1234 // 1155 // Cone equation: x^2+y^2=z^2tan^2(t) 1235 // Cone equation: x^2+y^2=z^2tan^2(t) 1156 // 1236 // 1157 // => (px+svx)^2+(py+svy)^2=(pz+svz)^2tan 1237 // => (px+svx)^2+(py+svy)^2=(pz+svz)^2tan^2(t) 1158 // 1238 // 1159 // => (px^2+py^2-pz^2tan^2(t))+2sd(pxvx+p << 1239 // => (px^2+py^2-pz^2tan^2(t))+2s(pxvx+pyvy-pzvztan^2(t)) 1160 // + sd^2(vx^2+vy^2-vz^2tan^2(t)) = << 1240 // + s^2(vx^2+vy^2-vz^2tan^2(t)) = 0 1161 // 1241 // 1162 // => sd^2(1-vz^2(1+tan^2(t))+2sd(pdotv2d << 1242 // => s^2(1-vz^2(1+tan^2(t))+2s(pdotv2d-pzvztan^2(t))+(rho2-pz^2tan^2(t))=0 1163 // + (rho2-pz^2tan^2(t)) = 0 << 1164 1243 1165 if (fSTheta != 0.0) << 1244 tanSTheta = std::tan(fSTheta) ; >> 1245 tanSTheta2 = tanSTheta*tanSTheta ; >> 1246 tanETheta = std::tan(fSTheta+fDTheta) ; >> 1247 tanETheta2 = tanETheta*tanETheta ; >> 1248 >> 1249 if (fSTheta) 1166 { 1250 { 1167 dist2STheta = rho2 - p.z()*p.z()*tanSTh 1251 dist2STheta = rho2 - p.z()*p.z()*tanSTheta2 ; 1168 } 1252 } 1169 else 1253 else 1170 { 1254 { 1171 dist2STheta = kInfinity ; 1255 dist2STheta = kInfinity ; 1172 } 1256 } 1173 if ( eTheta < pi ) << 1257 if ( fSTheta + fDTheta < pi ) 1174 { 1258 { 1175 dist2ETheta=rho2-p.z()*p.z()*tanETheta2 1259 dist2ETheta=rho2-p.z()*p.z()*tanETheta2; 1176 } 1260 } 1177 else << 1261 else 1178 { 1262 { 1179 dist2ETheta=kInfinity; 1263 dist2ETheta=kInfinity; 1180 } << 1264 } 1181 if ( pTheta < tolSTheta ) << 1265 if ( pTheta < tolSTheta) // dist2STheta<-kRadTolerance*0.5 && dist2ETheta>0) 1182 { 1266 { 1183 // Inside (theta<stheta-tol) stheta con << 1267 // Inside (theta<stheta-tol) s theta cone 1184 // First root of stheta cone, second if 1268 // First root of stheta cone, second if first root -ve 1185 1269 1186 t1 = 1 - v.z()*v.z()*(1 + tanSTheta2) ; 1270 t1 = 1 - v.z()*v.z()*(1 + tanSTheta2) ; 1187 t2 = pDotV2d - p.z()*v.z()*tanSTheta2 ; 1271 t2 = pDotV2d - p.z()*v.z()*tanSTheta2 ; 1188 if (t1 != 0.0) << 1272 >> 1273 b = t2/t1 ; >> 1274 c = dist2STheta/t1 ; >> 1275 d2 = b*b - c ; >> 1276 >> 1277 if ( d2 >= 0 ) 1189 { 1278 { >> 1279 d = std::sqrt(d2) ; >> 1280 s = -b - d ; // First root >> 1281 >> 1282 if ( s < 0 ) >> 1283 { >> 1284 s=-b+d; // Second root >> 1285 } >> 1286 if (s >= 0 && s < snxt) >> 1287 { >> 1288 xi = p.x() + s*v.x() ; >> 1289 yi = p.y() + s*v.y() ; >> 1290 zi = p.z() + s*v.z() ; >> 1291 rhoi2 = xi*xi + yi*yi ; >> 1292 radi2 = rhoi2 + zi*zi ; >> 1293 if ( (radi2 <= tolORMax2) >> 1294 && (radi2 >= tolORMin2) >> 1295 && (zi*(fSTheta - halfpi) <= 0) ) >> 1296 { >> 1297 if ( segPhi && rhoi2 ) // Check phi intersection >> 1298 { >> 1299 cosPsi = (xi*cosCPhi + yi*sinCPhi)/std::sqrt(rhoi2) ; >> 1300 if (cosPsi >= cosHDPhiOT) >> 1301 { >> 1302 snxt = s ; >> 1303 } >> 1304 } >> 1305 else >> 1306 { >> 1307 snxt = s ; >> 1308 } >> 1309 } >> 1310 } >> 1311 } >> 1312 >> 1313 // Possible intersection with ETheta cone. >> 1314 // Second >= 0 root should be considered >> 1315 >> 1316 if ( fSTheta + fDTheta < pi ) >> 1317 { >> 1318 t1 = 1 - v.z()*v.z()*(1 + tanETheta2) ; >> 1319 t2 = pDotV2d - p.z()*v.z()*tanETheta2 ; >> 1320 1190 b = t2/t1 ; 1321 b = t2/t1 ; 1191 c = dist2STheta/t1 ; << 1322 c = dist2ETheta/t1 ; 1192 d2 = b*b - c ; 1323 d2 = b*b - c ; 1193 1324 1194 if ( d2 >= 0 ) << 1325 if (d2 >= 0) 1195 { 1326 { 1196 d = std::sqrt(d2) ; << 1327 d = std::sqrt(d2) ; 1197 sd = -b - d ; // First root << 1328 s = -b + d ; // Second root 1198 zi = p.z() + sd*v.z(); << 1199 1329 1200 if ( (sd < 0) || (zi*(fSTheta - hal << 1330 if (s >= 0 && s < snxt) 1201 { 1331 { 1202 sd = -b+d; // Second root << 1332 xi = p.x() + s*v.x() ; 1203 } << 1333 yi = p.y() + s*v.y() ; 1204 if ((sd >= 0) && (sd < snxt)) << 1334 zi = p.z() + s*v.z() ; 1205 { << 1335 rhoi2 = xi*xi + yi*yi ; 1206 xi = p.x() + sd*v.x(); << 1336 radi2 = rhoi2 + zi*zi ; 1207 yi = p.y() + sd*v.y(); << 1337 1208 zi = p.z() + sd*v.z(); << 1209 rhoi2 = xi*xi + yi*yi; << 1210 radi2 = rhoi2 + zi*zi; << 1211 if ( (radi2 <= tolORMax2) 1338 if ( (radi2 <= tolORMax2) 1212 && (radi2 >= tolORMin2) 1339 && (radi2 >= tolORMin2) 1213 && (zi*(fSTheta - halfpi) <= 0) << 1340 && (zi*(fSTheta + fDTheta - halfpi) <= 0) ) 1214 { 1341 { 1215 if ( !fFullPhiSphere && (rhoi2 << 1342 if (segPhi && rhoi2) // Check phi intersection 1216 { 1343 { 1217 cosPsi = (xi*cosCPhi + yi*sin 1344 cosPsi = (xi*cosCPhi + yi*sinCPhi)/std::sqrt(rhoi2) ; 1218 if (cosPsi >= cosHDPhiOT) 1345 if (cosPsi >= cosHDPhiOT) 1219 { 1346 { 1220 snxt = sd; << 1347 snxt = s ; 1221 } 1348 } 1222 } 1349 } 1223 else 1350 else 1224 { 1351 { 1225 snxt = sd; << 1352 snxt = s ; 1226 } 1353 } 1227 } 1354 } 1228 } 1355 } 1229 } 1356 } 1230 } 1357 } >> 1358 } >> 1359 else if (pTheta > tolETheta) >> 1360 { // dist2ETheta<-kRadTolerance*0.5 && dist2STheta>0) >> 1361 // Inside (theta>etheta+tol) e theta cone >> 1362 // First root of etheta cone, second if first root `imaginary' 1231 1363 1232 // Possible intersection with ETheta co << 1364 t1 = 1 - v.z()*v.z()*(1 + tanETheta2) ; 1233 // Second >= 0 root should be considere << 1365 t2 = pDotV2d - p.z()*v.z()*tanETheta2 ; >> 1366 >> 1367 b = t2/t1 ; >> 1368 c = dist2ETheta/t1 ; >> 1369 d2 = b*b - c ; 1234 1370 1235 if ( eTheta < pi ) << 1371 if (d2 >= 0) 1236 { 1372 { 1237 t1 = 1 - v.z()*v.z()*(1 + tanETheta2) << 1373 d = std::sqrt(d2) ; 1238 t2 = pDotV2d - p.z()*v.z()*tanETheta2 << 1374 s = -b - d ; // First root 1239 if (t1 != 0.0) << 1375 if (s < 0) 1240 { << 1376 { 1241 b = t2/t1 ; << 1377 s = -b + d ; // second root 1242 c = dist2ETheta/t1 ; << 1378 } 1243 d2 = b*b - c ; << 1379 if (s >= 0 && s < snxt) >> 1380 { >> 1381 xi = p.x() + s*v.x() ; >> 1382 yi = p.y() + s*v.y() ; >> 1383 zi = p.z() + s*v.z() ; >> 1384 rhoi2 = xi*xi + yi*yi ; >> 1385 radi2 = rhoi2 + zi*zi ; 1244 1386 1245 if (d2 >= 0) << 1387 if ( (radi2 <= tolORMax2) >> 1388 && (radi2 >= tolORMin2) >> 1389 && (zi*(fSTheta + fDTheta - halfpi) <= 0) ) 1246 { 1390 { 1247 d = std::sqrt(d2) ; << 1391 if (segPhi && rhoi2) // Check phi intersection 1248 sd = -b + d ; // Second root << 1249 << 1250 if ( (sd >= 0) && (sd < snxt) ) << 1251 { 1392 { 1252 xi = p.x() + sd*v.x() ; << 1393 cosPsi = (xi*cosCPhi + yi*sinCPhi)/std::sqrt(rhoi2) ; 1253 yi = p.y() + sd*v.y() ; << 1394 if (cosPsi >= cosHDPhiOT) 1254 zi = p.z() + sd*v.z() ; << 1255 rhoi2 = xi*xi + yi*yi ; << 1256 radi2 = rhoi2 + zi*zi ; << 1257 << 1258 if ( (radi2 <= tolORMax2) << 1259 && (radi2 >= tolORMin2) << 1260 && (zi*(eTheta - halfpi) <= 0 << 1261 { 1395 { 1262 if (!fFullPhiSphere && (rhoi2 << 1396 snxt = s ; 1263 { << 1264 cosPsi = (xi*cosCPhi + yi*s << 1265 if (cosPsi >= cosHDPhiOT) << 1266 { << 1267 snxt = sd; << 1268 } << 1269 } << 1270 else << 1271 { << 1272 snxt = sd; << 1273 } << 1274 } 1397 } 1275 } 1398 } >> 1399 else >> 1400 { >> 1401 snxt = s ; >> 1402 } 1276 } 1403 } 1277 } 1404 } 1278 } 1405 } 1279 } << 1280 else if ( pTheta > tolETheta ) << 1281 { << 1282 // dist2ETheta<-kRadTolerance*0.5 && di << 1283 // Inside (theta > etheta+tol) e-theta << 1284 // First root of etheta cone, second if << 1285 1406 1286 t1 = 1 - v.z()*v.z()*(1 + tanETheta2) ; << 1407 // Possible intersection with STheta cone. 1287 t2 = pDotV2d - p.z()*v.z()*tanETheta2 ; << 1408 // Second >= 0 root should be considered 1288 if (t1 != 0.0) << 1409 >> 1410 if ( fSTheta ) 1289 { 1411 { >> 1412 t1 = 1 - v.z()*v.z()*(1 + tanSTheta2) ; >> 1413 t2 = pDotV2d - p.z()*v.z()*tanSTheta2 ; >> 1414 1290 b = t2/t1 ; 1415 b = t2/t1 ; 1291 c = dist2ETheta/t1 ; << 1416 c = dist2STheta/t1 ; 1292 d2 = b*b - c ; 1417 d2 = b*b - c ; 1293 1418 1294 if (d2 >= 0) 1419 if (d2 >= 0) 1295 { 1420 { 1296 d = std::sqrt(d2) ; << 1421 d = std::sqrt(d2) ; 1297 sd = -b - d ; // First root << 1422 s = -b + d ; // Second root 1298 zi = p.z() + sd*v.z(); << 1299 1423 1300 if ( (sd < 0) || (zi*(eTheta - half << 1424 if ( (s >= 0) && (s < snxt) ) 1301 { << 1302 sd = -b + d ; // second << 1303 } << 1304 if ( (sd >= 0) && (sd < snxt) ) << 1305 { 1425 { 1306 xi = p.x() + sd*v.x() ; << 1426 xi = p.x() + s*v.x() ; 1307 yi = p.y() + sd*v.y() ; << 1427 yi = p.y() + s*v.y() ; 1308 zi = p.z() + sd*v.z() ; << 1428 zi = p.z() + s*v.z() ; 1309 rhoi2 = xi*xi + yi*yi ; 1429 rhoi2 = xi*xi + yi*yi ; 1310 radi2 = rhoi2 + zi*zi ; 1430 radi2 = rhoi2 + zi*zi ; 1311 1431 1312 if ( (radi2 <= tolORMax2) 1432 if ( (radi2 <= tolORMax2) 1313 && (radi2 >= tolORMin2) 1433 && (radi2 >= tolORMin2) 1314 && (zi*(eTheta - halfpi) <= 0) << 1434 && (zi*(fSTheta - halfpi) <= 0) ) 1315 { 1435 { 1316 if (!fFullPhiSphere && (rhoi2 ! << 1436 if (segPhi && rhoi2) // Check phi intersection 1317 { 1437 { 1318 cosPsi = (xi*cosCPhi + yi*sin 1438 cosPsi = (xi*cosCPhi + yi*sinCPhi)/std::sqrt(rhoi2) ; 1319 if (cosPsi >= cosHDPhiOT) 1439 if (cosPsi >= cosHDPhiOT) 1320 { 1440 { 1321 snxt = sd; << 1441 snxt = s ; 1322 } 1442 } 1323 } 1443 } 1324 else 1444 else 1325 { 1445 { 1326 snxt = sd; << 1446 snxt = s ; 1327 } 1447 } 1328 } 1448 } 1329 } 1449 } 1330 } 1450 } 1331 } << 1451 } 1332 << 1452 } 1333 // Possible intersection with STheta co << 1453 else if ( (pTheta <tolSTheta + kAngTolerance) 1334 // Second >= 0 root should be considere << 1454 && (fSTheta > kAngTolerance) ) 1335 << 1336 if ( fSTheta != 0.0 ) << 1337 { << 1338 t1 = 1 - v.z()*v.z()*(1 + tanSTheta2) << 1339 t2 = pDotV2d - p.z()*v.z()*tanSTheta2 << 1340 if (t1 != 0.0) << 1341 { << 1342 b = t2/t1 ; << 1343 c = dist2STheta/t1 ; << 1344 d2 = b*b - c ; << 1345 << 1346 if (d2 >= 0) << 1347 { << 1348 d = std::sqrt(d2) ; << 1349 sd = -b + d ; // Second root << 1350 << 1351 if ( (sd >= 0) && (sd < snxt) ) << 1352 { << 1353 xi = p.x() + sd*v.x() ; << 1354 yi = p.y() + sd*v.y() ; << 1355 zi = p.z() + sd*v.z() ; << 1356 rhoi2 = xi*xi + yi*yi ; << 1357 radi2 = rhoi2 + zi*zi ; << 1358 << 1359 if ( (radi2 <= tolORMax2) << 1360 && (radi2 >= tolORMin2) << 1361 && (zi*(fSTheta - halfpi) <= << 1362 { << 1363 if (!fFullPhiSphere && (rhoi2 << 1364 { << 1365 cosPsi = (xi*cosCPhi + yi*s << 1366 if (cosPsi >= cosHDPhiOT) << 1367 { << 1368 snxt = sd; << 1369 } << 1370 } << 1371 else << 1372 { << 1373 snxt = sd; << 1374 } << 1375 } << 1376 } << 1377 } << 1378 } << 1379 } << 1380 } << 1381 else if ( (pTheta < tolSTheta + kAngToler << 1382 && (fSTheta > halfAngTolerance) ) << 1383 { 1455 { 1384 // In tolerance of stheta 1456 // In tolerance of stheta 1385 // If entering through solid [r,phi] => 1457 // If entering through solid [r,phi] => 0 to in 1386 // else try 2nd root 1458 // else try 2nd root 1387 1459 1388 t2 = pDotV2d - p.z()*v.z()*tanSTheta2 ; 1460 t2 = pDotV2d - p.z()*v.z()*tanSTheta2 ; 1389 if ( (t2>=0 && tolIRMin2<rad2 && rad2<t << 1461 if ( (t2>=0 && tolIRMin2<rad2 && rad2<tolIRMax2 && fSTheta<pi*.5) 1390 || (t2<0 && tolIRMin2<rad2 && rad2<t << 1462 || (t2<0 && tolIRMin2<rad2 && rad2<tolIRMax2 && fSTheta>pi*.5) 1391 || (v.z()<0 && tolIRMin2<rad2 && rad2 << 1463 || (v.z()<0 && tolIRMin2<rad2 && rad2<tolIRMax2 && fSTheta==pi*.5) ) 1392 { 1464 { 1393 if (!fFullPhiSphere && (rho2 != 0.0)) << 1465 if (segPhi && rho2) // Check phi intersection 1394 { 1466 { 1395 cosPsi = (p.x()*cosCPhi + p.y()*sin 1467 cosPsi = (p.x()*cosCPhi + p.y()*sinCPhi)/std::sqrt(rho2) ; 1396 if (cosPsi >= cosHDPhiIT) 1468 if (cosPsi >= cosHDPhiIT) 1397 { 1469 { 1398 return 0 ; 1470 return 0 ; 1399 } 1471 } 1400 } 1472 } 1401 else 1473 else 1402 { 1474 { 1403 return 0 ; 1475 return 0 ; 1404 } 1476 } 1405 } 1477 } 1406 1478 1407 // Not entering immediately/travelling 1479 // Not entering immediately/travelling through 1408 1480 1409 t1 = 1 - v.z()*v.z()*(1 + tanSTheta2) ; 1481 t1 = 1 - v.z()*v.z()*(1 + tanSTheta2) ; 1410 if (t1 != 0.0) << 1482 b = t2/t1 ; 1411 { << 1483 c = dist2STheta/t1 ; 1412 b = t2/t1 ; << 1484 d2 = b*b - c ; 1413 c = dist2STheta/t1 ; << 1414 d2 = b*b - c ; << 1415 1485 1416 if (d2 >= 0) << 1486 if (d2 >= 0) 1417 { << 1487 { 1418 d = std::sqrt(d2) ; << 1488 d = std::sqrt(d2) ; 1419 sd = -b + d ; << 1489 s = -b + d ; 1420 if ( (sd >= halfCarTolerance) && (s << 1490 if ( (s >= kCarTolerance*0.5) && (s < snxt) && (fSTheta < pi*0.5) ) 1421 { // ^^^^^^^^^^^^^^^^^^^^^ shoul << 1491 { 1422 xi = p.x() + sd*v.x() ; << 1492 xi = p.x() + s*v.x() ; 1423 yi = p.y() + sd*v.y() ; << 1493 yi = p.y() + s*v.y() ; 1424 zi = p.z() + sd*v.z() ; << 1494 zi = p.z() + s*v.z() ; 1425 rhoi2 = xi*xi + yi*yi ; << 1495 rhoi2 = xi*xi + yi*yi ; 1426 radi2 = rhoi2 + zi*zi ; << 1496 radi2 = rhoi2 + zi*zi ; 1427 1497 1428 if ( (radi2 <= tolORMax2) << 1498 if ( (radi2 <= tolORMax2) 1429 && (radi2 >= tolORMin2) << 1499 && (radi2 >= tolORMin2) 1430 && (zi*(fSTheta - halfpi) <= 0) << 1500 && (zi*(fSTheta - halfpi) <= 0) ) >> 1501 { >> 1502 if ( segPhi && rhoi2 ) // Check phi intersection 1431 { 1503 { 1432 if ( !fFullPhiSphere && (rhoi2 << 1504 cosPsi = (xi*cosCPhi + yi*sinCPhi)/std::sqrt(rhoi2) ; >> 1505 if ( cosPsi >= cosHDPhiOT ) 1433 { 1506 { 1434 cosPsi = (xi*cosCPhi + yi*sin << 1507 snxt = s ; 1435 if ( cosPsi >= cosHDPhiOT ) << 1436 { << 1437 snxt = sd; << 1438 } << 1439 } << 1440 else << 1441 { << 1442 snxt = sd; << 1443 } 1508 } 1444 } 1509 } >> 1510 else >> 1511 { >> 1512 snxt = s ; >> 1513 } 1445 } 1514 } 1446 } 1515 } 1447 } 1516 } 1448 } << 1517 } 1449 else if ((pTheta > tolETheta-kAngToleranc << 1518 else if ( (pTheta > tolETheta - kAngTolerance) >> 1519 && ((fSTheta + fDTheta) < pi-kAngTolerance) ) 1450 { 1520 { 1451 1521 1452 // In tolerance of etheta 1522 // In tolerance of etheta 1453 // If entering through solid [r,phi] => 1523 // If entering through solid [r,phi] => 0 to in 1454 // else try 2nd root 1524 // else try 2nd root 1455 1525 1456 t2 = pDotV2d - p.z()*v.z()*tanETheta2 ; 1526 t2 = pDotV2d - p.z()*v.z()*tanETheta2 ; 1457 1527 1458 if ( ((t2<0) && (eTheta < halfpi) << 1528 if ( 1459 && (tolIRMin2 < rad2) && (rad2 < to << 1529 (t2<0 && (fSTheta+fDTheta) <pi*0.5 && tolIRMin2<rad2 && rad2<tolIRMax2) 1460 || ((t2>=0) && (eTheta > halfpi) << 1530 || (t2>=0 && (fSTheta+fDTheta) >pi*0.5 && tolIRMin2<rad2 && rad2<tolIRMax2) 1461 && (tolIRMin2 < rad2) && (rad2 < to << 1531 || (v.z()>0 && (fSTheta+fDTheta)==pi*0.5 && tolIRMin2<rad2 && rad2<tolIRMax2) 1462 || ((v.z()>0) && (eTheta == halfpi) << 1532 ) 1463 && (tolIRMin2 < rad2) && (rad2 < to << 1464 { 1533 { 1465 if (!fFullPhiSphere && (rho2 != 0.0)) << 1534 if (segPhi && rho2) // Check phi intersection 1466 { 1535 { 1467 cosPsi = (p.x()*cosCPhi + p.y()*sin 1536 cosPsi = (p.x()*cosCPhi + p.y()*sinCPhi)/std::sqrt(rho2) ; 1468 if (cosPsi >= cosHDPhiIT) 1537 if (cosPsi >= cosHDPhiIT) 1469 { 1538 { 1470 return 0 ; 1539 return 0 ; 1471 } 1540 } 1472 } 1541 } 1473 else 1542 else 1474 { 1543 { 1475 return 0 ; 1544 return 0 ; 1476 } 1545 } 1477 } 1546 } 1478 1547 1479 // Not entering immediately/travelling 1548 // Not entering immediately/travelling through 1480 1549 1481 t1 = 1 - v.z()*v.z()*(1 + tanETheta2) ; 1550 t1 = 1 - v.z()*v.z()*(1 + tanETheta2) ; 1482 if (t1 != 0.0) << 1551 b = t2/t1 ; 1483 { << 1552 c = dist2ETheta/t1 ; 1484 b = t2/t1 ; << 1553 d2 = b*b - c ; 1485 c = dist2ETheta/t1 ; << 1486 d2 = b*b - c ; << 1487 1554 1488 if (d2 >= 0) << 1555 if (d2 >= 0) 1489 { << 1556 { 1490 d = std::sqrt(d2) ; << 1557 d = std::sqrt(d2) ; 1491 sd = -b + d ; << 1558 s = -b + d ; >> 1559 >> 1560 if ( (s >= kCarTolerance*0.5) >> 1561 && (s < snxt) && ((fSTheta + fDTheta) > pi*0.5) ) >> 1562 { >> 1563 xi = p.x() + s*v.x() ; >> 1564 yi = p.y() + s*v.y() ; >> 1565 zi = p.z() + s*v.z() ; >> 1566 rhoi2 = xi*xi + yi*yi ; >> 1567 radi2 = rhoi2 + zi*zi ; 1492 1568 1493 if ( (sd >= halfCarTolerance) << 1569 if ( (radi2 <= tolORMax2) 1494 && (sd < snxt) && (eTheta > halfp << 1570 && (radi2 >= tolORMin2) >> 1571 && (zi*(fSTheta + fDTheta - halfpi) <= 0) ) 1495 { 1572 { 1496 xi = p.x() + sd*v.x() ; << 1573 if (segPhi && rhoi2) // Check phi intersection 1497 yi = p.y() + sd*v.y() ; << 1498 zi = p.z() + sd*v.z() ; << 1499 rhoi2 = xi*xi + yi*yi ; << 1500 radi2 = rhoi2 + zi*zi ; << 1501 << 1502 if ( (radi2 <= tolORMax2) << 1503 && (radi2 >= tolORMin2) << 1504 && (zi*(eTheta - halfpi) <= 0) << 1505 { 1574 { 1506 if (!fFullPhiSphere && (rhoi2 ! << 1575 cosPsi = (xi*cosCPhi + yi*sinCPhi)/std::sqrt(rhoi2) ; >> 1576 if (cosPsi>=cosHDPhiOT) 1507 { 1577 { 1508 cosPsi = (xi*cosCPhi + yi*sin << 1578 snxt = s ; 1509 if (cosPsi >= cosHDPhiOT) << 1510 { << 1511 snxt = sd; << 1512 } << 1513 } << 1514 else << 1515 { << 1516 snxt = sd; << 1517 } 1579 } 1518 } 1580 } >> 1581 else >> 1582 { >> 1583 snxt = s ; >> 1584 } 1519 } 1585 } 1520 } 1586 } 1521 } << 1587 } 1522 } << 1588 } 1523 else 1589 else 1524 { 1590 { 1525 // stheta+tol<theta<etheta-tol 1591 // stheta+tol<theta<etheta-tol 1526 // For BOTH stheta & etheta check 2nd r 1592 // For BOTH stheta & etheta check 2nd root for validity [r,phi] 1527 1593 1528 t1 = 1 - v.z()*v.z()*(1 + tanSTheta2) ; 1594 t1 = 1 - v.z()*v.z()*(1 + tanSTheta2) ; 1529 t2 = pDotV2d - p.z()*v.z()*tanSTheta2 ; 1595 t2 = pDotV2d - p.z()*v.z()*tanSTheta2 ; 1530 if (t1 != 0.0) << 1596 >> 1597 b = t2/t1; >> 1598 c = dist2STheta/t1 ; >> 1599 d2 = b*b - c ; >> 1600 >> 1601 if (d2 >= 0) 1531 { 1602 { 1532 b = t2/t1; << 1603 d = std::sqrt(d2) ; 1533 c = dist2STheta/t1 ; << 1604 s = -b + d ; // second root 1534 d2 = b*b - c ; << 1535 1605 1536 if (d2 >= 0) << 1606 if (s >= 0 && s < snxt) 1537 { 1607 { 1538 d = std::sqrt(d2) ; << 1608 xi = p.x() + s*v.x() ; 1539 sd = -b + d ; // second root << 1609 yi = p.y() + s*v.y() ; >> 1610 zi = p.z() + s*v.z() ; >> 1611 rhoi2 = xi*xi + yi*yi ; >> 1612 radi2 = rhoi2 + zi*zi ; 1540 1613 1541 if ((sd >= 0) && (sd < snxt)) << 1614 if ( (radi2 <= tolORMax2) >> 1615 && (radi2 >= tolORMin2) >> 1616 && (zi*(fSTheta - halfpi) <= 0) ) 1542 { 1617 { 1543 xi = p.x() + sd*v.x() ; << 1618 if (segPhi && rhoi2) // Check phi intersection 1544 yi = p.y() + sd*v.y() ; << 1545 zi = p.z() + sd*v.z() ; << 1546 rhoi2 = xi*xi + yi*yi ; << 1547 radi2 = rhoi2 + zi*zi ; << 1548 << 1549 if ( (radi2 <= tolORMax2) << 1550 && (radi2 >= tolORMin2) << 1551 && (zi*(fSTheta - halfpi) <= 0) << 1552 { 1619 { 1553 if (!fFullPhiSphere && (rhoi2 ! << 1620 cosPsi = (xi*cosCPhi + yi*sinCPhi)/std::sqrt(rhoi2) ; >> 1621 if (cosPsi >= cosHDPhiOT) 1554 { 1622 { 1555 cosPsi = (xi*cosCPhi + yi*sin << 1623 snxt = s ; 1556 if (cosPsi >= cosHDPhiOT) << 1557 { << 1558 snxt = sd; << 1559 } << 1560 } << 1561 else << 1562 { << 1563 snxt = sd; << 1564 } 1624 } 1565 } 1625 } >> 1626 else >> 1627 { >> 1628 snxt = s ; >> 1629 } 1566 } 1630 } 1567 } 1631 } 1568 } << 1632 } 1569 t1 = 1 - v.z()*v.z()*(1 + tanETheta2) ; 1633 t1 = 1 - v.z()*v.z()*(1 + tanETheta2) ; 1570 t2 = pDotV2d - p.z()*v.z()*tanETheta2 ; 1634 t2 = pDotV2d - p.z()*v.z()*tanETheta2 ; 1571 if (t1 != 0.0) << 1635 >> 1636 b = t2/t1 ; >> 1637 c = dist2ETheta/t1 ; >> 1638 d2 = b*b - c ; >> 1639 >> 1640 if (d2 >= 0) 1572 { 1641 { 1573 b = t2/t1 ; << 1642 d = std::sqrt(d2) ; 1574 c = dist2ETheta/t1 ; << 1643 s = -b + d; // second root 1575 d2 = b*b - c ; << 1576 1644 1577 if (d2 >= 0) << 1645 if (s >= 0 && s < snxt) 1578 { 1646 { 1579 d = std::sqrt(d2) ; << 1647 xi = p.x() + s*v.x() ; 1580 sd = -b + d; // second root << 1648 yi = p.y() + s*v.y() ; >> 1649 zi = p.z() + s*v.z() ; >> 1650 rhoi2 = xi*xi + yi*yi ; >> 1651 radi2 = rhoi2 + zi*zi ; 1581 1652 1582 if ((sd >= 0) && (sd < snxt)) << 1653 if ( (radi2 <= tolORMax2) >> 1654 && (radi2 >= tolORMin2) >> 1655 && (zi*(fSTheta + fDTheta - halfpi) <= 0) ) 1583 { 1656 { 1584 xi = p.x() + sd*v.x() ; << 1657 if (segPhi && rhoi2) // Check phi intersection 1585 yi = p.y() + sd*v.y() ; << 1586 zi = p.z() + sd*v.z() ; << 1587 rhoi2 = xi*xi + yi*yi ; << 1588 radi2 = rhoi2 + zi*zi ; << 1589 << 1590 if ( (radi2 <= tolORMax2) << 1591 && (radi2 >= tolORMin2) << 1592 && (zi*(eTheta - halfpi) <= 0) << 1593 { 1658 { 1594 if (!fFullPhiSphere && (rhoi2 ! << 1659 cosPsi = (xi*cosCPhi + yi*sinCPhi)/std::sqrt(rhoi2) ; >> 1660 if ( cosPsi >= cosHDPhiOT ) 1595 { 1661 { 1596 cosPsi = (xi*cosCPhi + yi*sin << 1662 snxt=s; 1597 if ( cosPsi >= cosHDPhiOT ) << 1598 { << 1599 snxt = sd; << 1600 } << 1601 } << 1602 else << 1603 { << 1604 snxt = sd; << 1605 } 1663 } 1606 } 1664 } >> 1665 else >> 1666 { >> 1667 snxt = s ; >> 1668 } 1607 } 1669 } 1608 } 1670 } 1609 } 1671 } 1610 } << 1672 } 1611 } 1673 } 1612 return snxt; 1674 return snxt; 1613 } 1675 } 1614 1676 1615 ///////////////////////////////////////////// 1677 ////////////////////////////////////////////////////////////////////// 1616 // 1678 // 1617 // Calculate distance (<= actual) to closest 1679 // Calculate distance (<= actual) to closest surface of shape from outside 1618 // - Calculate distance to radial planes 1680 // - Calculate distance to radial planes 1619 // - Only to phi planes if outside phi extent 1681 // - Only to phi planes if outside phi extent 1620 // - Only to theta planes if outside theta ex 1682 // - Only to theta planes if outside theta extent 1621 // - Return 0 if point inside 1683 // - Return 0 if point inside 1622 1684 1623 G4double G4Sphere::DistanceToIn( const G4Thre 1685 G4double G4Sphere::DistanceToIn( const G4ThreeVector& p ) const 1624 { 1686 { 1625 G4double safe=0.0,safeRMin,safeRMax,safePhi 1687 G4double safe=0.0,safeRMin,safeRMax,safePhi,safeTheta; 1626 G4double rho2,rds,rho; << 1688 G4double rho2,rad,rho; 1627 G4double cosPsi; << 1689 G4double phiC,cosPhiC,sinPhiC,cosPsi,ePhi; 1628 G4double pTheta,dTheta1,dTheta2; 1690 G4double pTheta,dTheta1,dTheta2; 1629 rho2=p.x()*p.x()+p.y()*p.y(); 1691 rho2=p.x()*p.x()+p.y()*p.y(); 1630 rds=std::sqrt(rho2+p.z()*p.z()); << 1692 rad=std::sqrt(rho2+p.z()*p.z()); 1631 rho=std::sqrt(rho2); 1693 rho=std::sqrt(rho2); 1632 1694 1633 // 1695 // 1634 // Distance to r shells 1696 // Distance to r shells 1635 // << 1697 // 1636 if (fRmin != 0.0) << 1698 if (fRmin) 1637 { 1699 { 1638 safeRMin=fRmin-rds; << 1700 safeRMin=fRmin-rad; 1639 safeRMax=rds-fRmax; << 1701 safeRMax=rad-fRmax; 1640 if (safeRMin>safeRMax) 1702 if (safeRMin>safeRMax) 1641 { 1703 { 1642 safe=safeRMin; 1704 safe=safeRMin; 1643 } 1705 } 1644 else 1706 else 1645 { 1707 { 1646 safe=safeRMax; 1708 safe=safeRMax; 1647 } 1709 } 1648 } 1710 } 1649 else 1711 else 1650 { 1712 { 1651 safe=rds-fRmax; << 1713 safe=rad-fRmax; 1652 } 1714 } 1653 1715 1654 // 1716 // 1655 // Distance to phi extent 1717 // Distance to phi extent 1656 // 1718 // 1657 if (!fFullPhiSphere && (rho != 0.0)) << 1719 if (fDPhi<twopi&&rho) 1658 { 1720 { >> 1721 phiC=fSPhi+fDPhi*0.5; >> 1722 cosPhiC=std::cos(phiC); >> 1723 sinPhiC=std::sin(phiC); >> 1724 1659 // Psi=angle from central phi to point 1725 // Psi=angle from central phi to point 1660 // 1726 // 1661 cosPsi=(p.x()*cosCPhi+p.y()*sinCPhi)/rho; << 1727 cosPsi=(p.x()*cosPhiC+p.y()*sinPhiC)/rho; 1662 if (cosPsi<cosHDPhi) << 1728 if (cosPsi<std::cos(fDPhi*0.5)) 1663 { 1729 { 1664 // Point lies outside phi range 1730 // Point lies outside phi range 1665 // 1731 // 1666 if ((p.y()*cosCPhi-p.x()*sinCPhi)<=0) << 1732 if ((p.y()*cosPhiC-p.x()*sinPhiC)<=0) 1667 { 1733 { 1668 safePhi=std::fabs(p.x()*sinSPhi-p.y() << 1734 safePhi=std::fabs(p.x()*std::sin(fSPhi)-p.y()*std::cos(fSPhi)); 1669 } 1735 } 1670 else 1736 else 1671 { 1737 { 1672 safePhi=std::fabs(p.x()*sinEPhi-p.y() << 1738 ePhi=fSPhi+fDPhi; >> 1739 safePhi=std::fabs(p.x()*std::sin(ePhi)-p.y()*std::cos(ePhi)); 1673 } 1740 } 1674 if (safePhi>safe) { safe=safePhi; } << 1741 if (safePhi>safe) safe=safePhi; 1675 } 1742 } 1676 } 1743 } 1677 // 1744 // 1678 // Distance to Theta extent 1745 // Distance to Theta extent 1679 // << 1746 // 1680 if ((rds!=0.0) && (!fFullThetaSphere)) << 1747 if ((rad!=0.0) && (fDTheta<pi)) 1681 { 1748 { 1682 pTheta=std::acos(p.z()/rds); << 1749 pTheta=std::acos(p.z()/rad); 1683 if (pTheta<0) { pTheta+=pi; } << 1750 if (pTheta<0) pTheta+=pi; 1684 dTheta1=fSTheta-pTheta; 1751 dTheta1=fSTheta-pTheta; 1685 dTheta2=pTheta-eTheta; << 1752 dTheta2=pTheta-(fSTheta+fDTheta); 1686 if (dTheta1>dTheta2) 1753 if (dTheta1>dTheta2) 1687 { 1754 { 1688 if (dTheta1>=0) // WHY ???? 1755 if (dTheta1>=0) // WHY ??????????? 1689 { 1756 { 1690 safeTheta=rds*std::sin(dTheta1); << 1757 safeTheta=rad*std::sin(dTheta1); 1691 if (safe<=safeTheta) 1758 if (safe<=safeTheta) 1692 { 1759 { 1693 safe=safeTheta; 1760 safe=safeTheta; 1694 } 1761 } 1695 } 1762 } 1696 } 1763 } 1697 else 1764 else 1698 { 1765 { 1699 if (dTheta2>=0) 1766 if (dTheta2>=0) 1700 { 1767 { 1701 safeTheta=rds*std::sin(dTheta2); << 1768 safeTheta=rad*std::sin(dTheta2); 1702 if (safe<=safeTheta) 1769 if (safe<=safeTheta) 1703 { 1770 { 1704 safe=safeTheta; 1771 safe=safeTheta; 1705 } 1772 } 1706 } 1773 } 1707 } 1774 } 1708 } 1775 } 1709 1776 1710 if (safe<0) { safe=0; } << 1777 if (safe<0) safe=0; 1711 return safe; 1778 return safe; 1712 } 1779 } 1713 1780 1714 ///////////////////////////////////////////// 1781 ///////////////////////////////////////////////////////////////////// 1715 // 1782 // 1716 // Calculate distance to surface of shape fro << 1783 // Calculate distance to surface of shape from `inside', allowing for tolerance 1717 // - Only Calc rmax intersection if no valid 1784 // - Only Calc rmax intersection if no valid rmin intersection 1718 1785 1719 G4double G4Sphere::DistanceToOut( const G4Thr 1786 G4double G4Sphere::DistanceToOut( const G4ThreeVector& p, 1720 const G4Thr 1787 const G4ThreeVector& v, 1721 const G4boo 1788 const G4bool calcNorm, 1722 G4boo << 1789 G4bool *validNorm, 1723 G4Thr << 1790 G4ThreeVector *n ) const 1724 { 1791 { 1725 G4double snxt = kInfinity; // snxt is d 1792 G4double snxt = kInfinity; // snxt is default return value 1726 G4double sphi= kInfinity,stheta= kInfinity; 1793 G4double sphi= kInfinity,stheta= kInfinity; 1727 ESide side=kNull,sidephi=kNull,sidetheta=kN << 1794 ESide side=kNull,sidephi=kNull,sidetheta=kNull; 1728 1795 1729 const G4double halfRmaxTolerance = fRmaxTol << 1730 const G4double halfRminTolerance = fRminTol << 1731 const G4double Rmax_plus = fRmax + halfRma << 1732 const G4double Rmin_minus = (fRmin) != 0.0 << 1733 G4double t1,t2; 1796 G4double t1,t2; 1734 G4double b,c,d; 1797 G4double b,c,d; 1735 1798 1736 // Variables for phi intersection: 1799 // Variables for phi intersection: 1737 1800 >> 1801 G4double sinSPhi,cosSPhi,ePhi,sinEPhi,cosEPhi; >> 1802 G4double cPhi,sinCPhi,cosCPhi; 1738 G4double pDistS,compS,pDistE,compE,sphi2,vp 1803 G4double pDistS,compS,pDistE,compE,sphi2,vphi; >> 1804 >> 1805 G4double rho2,rad2,pDotV2d,pDotV3d,pTheta; 1739 1806 1740 G4double rho2,rad2,pDotV2d,pDotV3d; << 1807 G4double tolSTheta=0.,tolETheta=0.; 1741 << 1742 G4double xi,yi,zi; // Intersection poi 1808 G4double xi,yi,zi; // Intersection point 1743 1809 1744 // Theta precals << 1810 // G4double Comp; // Phi intersection 1745 // << 1811 1746 G4double rhoSecTheta; << 1812 G4bool segPhi; // Phi flag and precalcs 1747 G4double dist2STheta, dist2ETheta, distThet << 1813 G4double hDPhi,hDPhiOT,hDPhiIT; 1748 G4double d2,sd; << 1814 G4double cosHDPhiOT,cosHDPhiIT; >> 1815 >> 1816 G4bool segTheta; // Theta flag and precals >> 1817 G4double tanSTheta=0.,tanETheta, rhoSecTheta; >> 1818 G4double tanSTheta2=0.,tanETheta2=0.; >> 1819 G4double dist2STheta,dist2ETheta; >> 1820 G4double d2,s; 1749 1821 1750 // General Precalcs 1822 // General Precalcs 1751 // << 1752 rho2 = p.x()*p.x()+p.y()*p.y(); << 1753 rad2 = rho2+p.z()*p.z(); << 1754 1823 1755 pDotV2d = p.x()*v.x()+p.y()*v.y(); << 1824 rho2=p.x()*p.x()+p.y()*p.y(); 1756 pDotV3d = pDotV2d+p.z()*v.z(); << 1825 rad2=rho2+p.z()*p.z(); >> 1826 // G4double rad=std::sqrt(rad2); >> 1827 >> 1828 pTheta=std::atan2(std::sqrt(rho2),p.z()); 1757 1829 >> 1830 pDotV2d=p.x()*v.x()+p.y()*v.y(); >> 1831 pDotV3d=pDotV2d+p.z()*v.z(); >> 1832 >> 1833 // Set phi divided flag and precalcs >> 1834 >> 1835 if(fDPhi<twopi) >> 1836 { >> 1837 segPhi=true; >> 1838 hDPhi=0.5*fDPhi; // half delta phi >> 1839 cPhi=fSPhi+hDPhi;; >> 1840 hDPhiOT=hDPhi+0.5*kAngTolerance; // Outer Tolerant half delta phi >> 1841 hDPhiIT=hDPhi-0.5*kAngTolerance; >> 1842 sinCPhi=std::sin(cPhi); >> 1843 cosCPhi=std::cos(cPhi); >> 1844 cosHDPhiOT=std::cos(hDPhiOT); >> 1845 cosHDPhiIT=std::cos(hDPhiIT); >> 1846 } >> 1847 else >> 1848 { >> 1849 segPhi=false; >> 1850 } >> 1851 >> 1852 // Theta precalcs >> 1853 >> 1854 if (fDTheta < pi) >> 1855 { >> 1856 segTheta=true; >> 1857 tolSTheta=fSTheta-kAngTolerance*0.5; >> 1858 tolETheta=fSTheta+fDTheta+kAngTolerance*0.5; >> 1859 } >> 1860 else >> 1861 { >> 1862 segTheta=false; >> 1863 } >> 1864 1758 // Radial Intersections from G4Sphere::Dist 1865 // Radial Intersections from G4Sphere::DistanceToIn 1759 // 1866 // 1760 // Outer spherical shell intersection 1867 // Outer spherical shell intersection 1761 // - Only if outside tolerant fRmax 1868 // - Only if outside tolerant fRmax 1762 // - Check for if inside and outer G4Sphere 1869 // - Check for if inside and outer G4Sphere heading through solid (-> 0) 1763 // - No intersect -> no intersection with G 1870 // - No intersect -> no intersection with G4Sphere 1764 // 1871 // 1765 // Shell eqn: x^2+y^2+z^2=RSPH^2 1872 // Shell eqn: x^2+y^2+z^2=RSPH^2 1766 // 1873 // 1767 // => (px+svx)^2+(py+svy)^2+(pz+svz)^2=R^2 1874 // => (px+svx)^2+(py+svy)^2+(pz+svz)^2=R^2 1768 // 1875 // 1769 // => (px^2+py^2+pz^2) +2sd(pxvx+pyvy+pzvz) << 1876 // => (px^2+py^2+pz^2) +2s(pxvx+pyvy+pzvz)+s^2(vx^2+vy^2+vz^2)=R^2 1770 // => rad2 +2sd(pDotV3d) << 1877 // => rad2 +2s(pDotV3d) +s^2 =R^2 >> 1878 // >> 1879 // => s=-pDotV3d+-std::sqrt(pDotV3d^2-(rad2-R^2)) 1771 // 1880 // 1772 // => sd=-pDotV3d+-std::sqrt(pDotV3d^2-(rad << 1881 // const G4double fractionTolerance = 1.0e-12; >> 1882 const G4double flexRadMaxTolerance = // kRadTolerance; >> 1883 std::max(kRadTolerance, fEpsilon * fRmax); >> 1884 >> 1885 const G4double Rmax_plus = fRmax + flexRadMaxTolerance*0.5; >> 1886 const G4double flexRadMinTolerance = std::max(kRadTolerance, >> 1887 fEpsilon * fRmin); >> 1888 const G4double Rmin_minus= (fRmin > 0) ? fRmin-flexRadMinTolerance*0.5 : 0 ; 1773 1889 1774 if( (rad2 <= Rmax_plus*Rmax_plus) && (rad2 << 1890 if(rad2 <= Rmax_plus*Rmax_plus && rad2 >= Rmin_minus*Rmin_minus) >> 1891 // if(rad <= Rmax_plus && rad >= Rmin_minus) 1775 { 1892 { 1776 c = rad2 - fRmax*fRmax; 1893 c = rad2 - fRmax*fRmax; 1777 1894 1778 if (c < fRmaxTolerance*fRmax) << 1895 if (c < flexRadMaxTolerance*fRmax) 1779 { 1896 { 1780 // Within tolerant Outer radius << 1897 // Within tolerant Outer radius 1781 // << 1898 // 1782 // The test is 1899 // The test is 1783 // rad - fRmax < 0.5*kRadTolerance 1900 // rad - fRmax < 0.5*kRadTolerance 1784 // => rad < fRmax + 0.5*kRadTol 1901 // => rad < fRmax + 0.5*kRadTol 1785 // => rad2 < (fRmax + 0.5*kRadTol)^2 1902 // => rad2 < (fRmax + 0.5*kRadTol)^2 1786 // => rad2 < fRmax^2 + 2.*0.5*fRmax*kR 1903 // => rad2 < fRmax^2 + 2.*0.5*fRmax*kRadTol + 0.25*kRadTol*kRadTol 1787 // => rad2 - fRmax^2 <~ fRmax*kR << 1904 // => rad2 - fRmax^2 <~ fRmax*kRadTol 1788 1905 1789 d2 = pDotV3d*pDotV3d - c; 1906 d2 = pDotV3d*pDotV3d - c; 1790 1907 1791 if( (c >- fRmaxTolerance*fRmax) / << 1908 if( (c >- flexRadMaxTolerance*fRmax) // on tolerant surface 1792 && ((pDotV3d >=0) || (d2 < 0)) ) / << 1909 && ((pDotV3d >=0) || (d2 < 0)) ) // leaving outside from Rmax 1793 / << 1910 // not re-entering 1794 { 1911 { 1795 if(calcNorm) 1912 if(calcNorm) 1796 { 1913 { 1797 *validNorm = true ; 1914 *validNorm = true ; 1798 *n = G4ThreeVector(p.x()/fR 1915 *n = G4ThreeVector(p.x()/fRmax,p.y()/fRmax,p.z()/fRmax) ; 1799 } 1916 } 1800 return snxt = 0; 1917 return snxt = 0; 1801 } 1918 } 1802 else << 1919 else 1803 { 1920 { 1804 snxt = -pDotV3d+std::sqrt(d2); // << 1921 snxt=-pDotV3d+std::sqrt(d2); // second root since inside Rmax 1805 side = kRMax ; << 1922 side = kRMax ; 1806 } 1923 } 1807 } 1924 } 1808 1925 1809 // Inner spherical shell intersection: 1926 // Inner spherical shell intersection: 1810 // Always first >=0 root, because would h 1927 // Always first >=0 root, because would have passed 1811 // from outside of Rmin surface . 1928 // from outside of Rmin surface . 1812 1929 1813 if (fRmin != 0.0) << 1930 if (fRmin) 1814 { 1931 { 1815 c = rad2 - fRmin*fRmin; 1932 c = rad2 - fRmin*fRmin; 1816 d2 = pDotV3d*pDotV3d - c; 1933 d2 = pDotV3d*pDotV3d - c; 1817 1934 1818 if (c >- fRminTolerance*fRmin) // 2.0 * << 1935 if (c >- flexRadMinTolerance*fRmin) // 2.0 * (0.5*kRadTolerance) * fRmin 1819 { 1936 { 1820 if ( (c < fRminTolerance*fRmin) << 1937 if( c < flexRadMinTolerance*fRmin && 1821 && (d2 >= fRminTolerance*fRmin) && << 1938 d2 >= flexRadMinTolerance*fRmin && pDotV3d < 0 ) // leaving from Rmin 1822 { 1939 { 1823 if(calcNorm) { *validNorm = false; << 1940 if(calcNorm) >> 1941 { >> 1942 *validNorm = false ; // Rmin surface is concave >> 1943 } 1824 return snxt = 0 ; 1944 return snxt = 0 ; 1825 } 1945 } 1826 else 1946 else 1827 { << 1947 { 1828 if ( d2 >= 0. ) << 1948 if (d2 >= 0) 1829 { 1949 { 1830 sd = -pDotV3d-std::sqrt(d2); << 1950 s = -pDotV3d-std::sqrt(d2) ; 1831 << 1951 if (s>=0) // Always intersect Rmin first 1832 if ( sd >= 0. ) // Always int << 1833 { 1952 { 1834 snxt = sd ; << 1953 snxt = s ; 1835 side = kRMin ; 1954 side = kRMin ; 1836 } 1955 } 1837 } 1956 } 1838 } 1957 } 1839 } 1958 } 1840 } 1959 } 1841 } 1960 } 1842 1961 1843 // Theta segment intersection 1962 // Theta segment intersection 1844 1963 1845 if ( !fFullThetaSphere ) << 1964 if (segTheta) 1846 { 1965 { 1847 // Intersection with theta surfaces 1966 // Intersection with theta surfaces 1848 // 1967 // 1849 // Known failure cases: 1968 // Known failure cases: 1850 // o Inside tolerance of stheta surface, 1969 // o Inside tolerance of stheta surface, skim 1851 // ~parallel to cone and Hit & enter e 1970 // ~parallel to cone and Hit & enter etheta surface [& visa versa] 1852 // 1971 // 1853 // To solve: Check 2nd root of etheta 1972 // To solve: Check 2nd root of etheta surface in addition to stheta 1854 // 1973 // 1855 // o start/end theta is exactly pi/2 << 1974 // o start/end theta is exactly pi/2 1856 // 1975 // 1857 // Intersections with cones 1976 // Intersections with cones 1858 // 1977 // 1859 // Cone equation: x^2+y^2=z^2tan^2(t) 1978 // Cone equation: x^2+y^2=z^2tan^2(t) 1860 // 1979 // 1861 // => (px+svx)^2+(py+svy)^2=(pz+svz)^2tan 1980 // => (px+svx)^2+(py+svy)^2=(pz+svz)^2tan^2(t) 1862 // 1981 // 1863 // => (px^2+py^2-pz^2tan^2(t))+2sd(pxvx+p << 1982 // => (px^2+py^2-pz^2tan^2(t))+2s(pxvx+pyvy-pzvztan^2(t)) 1864 // + sd^2(vx^2+vy^2-vz^2tan^2(t)) = << 1983 // + s^2(vx^2+vy^2-vz^2tan^2(t)) = 0 1865 // 1984 // 1866 // => sd^2(1-vz^2(1+tan^2(t))+2sd(pdotv2d << 1985 // => s^2(1-vz^2(1+tan^2(t))+2s(pdotv2d-pzvztan^2(t))+(rho2-pz^2tan^2(t))=0 1867 // + (rho2-pz^2tan^2(t)) = 0 << 1868 // 1986 // 1869 << 1987 tanSTheta=std::tan(fSTheta); 1870 if(fSTheta != 0.0) // intersection with f << 1988 tanSTheta2=tanSTheta*tanSTheta; >> 1989 tanETheta=std::tan(fSTheta+fDTheta); >> 1990 tanETheta2=tanETheta*tanETheta; >> 1991 >> 1992 if (fSTheta) >> 1993 { >> 1994 dist2STheta=rho2-p.z()*p.z()*tanSTheta2; >> 1995 } >> 1996 else >> 1997 { >> 1998 dist2STheta = kInfinity; >> 1999 } >> 2000 if (fSTheta + fDTheta < pi) >> 2001 { >> 2002 dist2ETheta = rho2-p.z()*p.z()*tanETheta2; >> 2003 } >> 2004 else >> 2005 { >> 2006 dist2ETheta = kInfinity ; >> 2007 } >> 2008 if (pTheta > tolSTheta && pTheta < tolETheta) // Inside theta 1871 { 2009 { 1872 if( std::fabs(tanSTheta) > 5./kAngToler << 2010 // In tolerance of STheta and possible leaving out to small thetas N- >> 2011 >> 2012 if(pTheta < tolSTheta + kAngTolerance && fSTheta > kAngTolerance) 1873 { 2013 { 1874 if( v.z() > 0. ) << 2014 t2=pDotV2d-p.z()*v.z()*tanSTheta2 ; // =(VdotN+)*rhoSecSTheta >> 2015 >> 2016 if( fSTheta < pi*0.5 && t2 < 0) 1875 { 2017 { 1876 if ( std::fabs( p.z() ) <= halfRmax << 2018 if(calcNorm) *validNorm = false ; >> 2019 return snxt = 0 ; >> 2020 } >> 2021 else if(fSTheta > pi*0.5 && t2 >= 0) >> 2022 { >> 2023 if(calcNorm) 1877 { 2024 { 1878 if(calcNorm) << 2025 rhoSecTheta = std::sqrt(rho2*(1+tanSTheta2)) ; 1879 { << 2026 *validNorm = true ; 1880 *validNorm = true; << 2027 *n = G4ThreeVector(-p.x()/rhoSecTheta, // N- 1881 *n = G4ThreeVector(0.,0.,1.); << 2028 -p.y()/rhoSecTheta, 1882 } << 2029 tanSTheta/std::sqrt(1+tanSTheta2) ) ; 1883 return snxt = 0 ; << 1884 } 2030 } 1885 stheta = -p.z()/v.z(); << 2031 return snxt = 0 ; 1886 sidetheta = kSTheta; << 2032 } >> 2033 else if( fSTheta == pi*0.5 && v.z() > 0) >> 2034 { >> 2035 if(calcNorm) >> 2036 { >> 2037 *validNorm = true ; >> 2038 *n = G4ThreeVector(0,0,1) ; >> 2039 } >> 2040 return snxt = 0 ; 1887 } 2041 } 1888 } 2042 } 1889 else // kons is not plane << 1890 { << 1891 t1 = 1-v.z()*v.z()*(1+tanSTh << 1892 t2 = pDotV2d-p.z()*v.z()*tan << 1893 dist2STheta = rho2-p.z()*p.z()*tanSTh << 1894 2043 1895 distTheta = std::sqrt(rho2)-p.z()*tan << 2044 // In tolerance of ETheta and possible leaving out to larger thetas N+ 1896 2045 1897 if( std::fabs(t1) < halfAngTolerance << 2046 if ( (pTheta > tolETheta - kAngTolerance) 1898 { << 2047 && (( fSTheta + fDTheta) < pi - kAngTolerance) ) 1899 if( v.z() > 0. ) << 2048 { 1900 { << 2049 t2=pDotV2d-p.z()*v.z()*tanETheta2 ; 1901 if(std::fabs(distTheta) < halfRma << 2050 if((fSTheta+fDTheta)>pi*0.5 && t2<0) 1902 { << 2051 { 1903 if( (fSTheta < halfpi) && (p.z( << 2052 if(calcNorm) *validNorm = false ; 1904 { << 2053 return snxt = 0 ; 1905 if( calcNorm ) { *validNorm << 2054 } 1906 return snxt = 0.; << 2055 else if( (fSTheta+fDTheta) < pi*0.5 && t2 >= 0 ) 1907 } << 1908 else if( (fSTheta > halfpi) && << 1909 { << 1910 if( calcNorm ) << 1911 { << 1912 *validNorm = true; << 1913 if (rho2 != 0.0) << 1914 { << 1915 rhoSecTheta = std::sqrt(r << 1916 << 1917 *n = G4ThreeVector( p.x() << 1918 p.y() << 1919 std:: << 1920 } << 1921 else *n = G4ThreeVector(0., << 1922 } << 1923 return snxt = 0.; << 1924 } << 1925 } << 1926 stheta = -0.5*dist2STheta/t2; << 1927 sidetheta = kSTheta; << 1928 } << 1929 } // 2nd order equation, 1st roo << 1930 else // 2nd if 1st root -ve << 1931 { 2056 { 1932 if( std::fabs(distTheta) < halfRmax << 2057 if(calcNorm) 1933 { 2058 { 1934 if( (fSTheta > halfpi) && (t2 >= << 2059 rhoSecTheta = std::sqrt(rho2*(1+tanETheta2)) ; 1935 { << 2060 *validNorm = true ; 1936 if( calcNorm ) << 2061 *n = G4ThreeVector( p.x()/rhoSecTheta, // N+ 1937 { << 2062 p.y()/rhoSecTheta, 1938 *validNorm = true; << 2063 -tanETheta/std::sqrt(1+tanETheta2) ) ; 1939 if (rho2 != 0.0) << 1940 { << 1941 rhoSecTheta = std::sqrt(rho << 1942 << 1943 *n = G4ThreeVector( p.x()/r << 1944 p.y()/r << 1945 std::si << 1946 } << 1947 else { *n = G4ThreeVector(0. << 1948 } << 1949 return snxt = 0.; << 1950 } << 1951 else if( (fSTheta < halfpi) && (t << 1952 { << 1953 if( calcNorm ) { *validNorm = << 1954 return snxt = 0.; << 1955 } << 1956 } 2064 } 1957 b = t2/t1; << 2065 return snxt = 0 ; 1958 c = dist2STheta/t1; << 2066 } 1959 d2 = b*b - c ; << 2067 else if( ( fSTheta+fDTheta) == pi*0.5 && v.z() < 0 ) 1960 << 2068 { 1961 if ( d2 >= 0. ) << 2069 if(calcNorm) 1962 { 2070 { 1963 d = std::sqrt(d2); << 2071 *validNorm = true ; 1964 << 2072 *n = G4ThreeVector(0,0,-1) ; 1965 if( fSTheta > halfpi ) << 1966 { << 1967 sd = -b - d; // First r << 1968 << 1969 if ( ((std::fabs(s) < halfRmaxT << 1970 || (sd < 0.) || ( (sd > 0.) << 1971 { << 1972 sd = -b + d ; // 2nd root << 1973 } << 1974 if( (sd > halfRmaxTolerance) && << 1975 { << 1976 stheta = sd; << 1977 sidetheta = kSTheta; << 1978 } << 1979 } << 1980 else // sTheta < pi/2, concave su << 1981 { << 1982 sd = -b - d; // First r << 1983 << 1984 if ( ( (std::fabs(sd) < halfRma << 1985 || (sd < 0.) || ( (sd > 0.) & << 1986 { << 1987 sd = -b + d ; // 2nd root << 1988 } << 1989 if( (sd > halfRmaxTolerance) && << 1990 { << 1991 stheta = sd; << 1992 sidetheta = kSTheta; << 1993 } << 1994 } << 1995 } 2073 } >> 2074 return snxt = 0 ; 1996 } 2075 } 1997 } 2076 } 1998 } << 2077 if( fSTheta > 0 ) 1999 if (eTheta < pi) // intersection with sec << 2078 { 2000 { << 2079 // First root of fSTheta cone, second if first root -ve 2001 if( std::fabs(tanETheta) > 5./kAngToler << 2080 2002 { << 2081 t1 = 1-v.z()*v.z()*(1+tanSTheta2); 2003 if( v.z() < 0. ) << 2082 t2 = pDotV2d-p.z()*v.z()*tanSTheta2; >> 2083 >> 2084 b = t2/t1; >> 2085 c = dist2STheta/t1; >> 2086 d2 = b*b - c ; >> 2087 >> 2088 if ( d2 >= 0 ) 2004 { 2089 { 2005 if ( std::fabs( p.z() ) <= halfRmax << 2090 d = std::sqrt(d2) ; >> 2091 s = -b - d ; // First root >> 2092 >> 2093 if ( s < 0 ) 2006 { 2094 { 2007 if(calcNorm) << 2095 s = -b + d ; // Second root 2008 { << 2009 *validNorm = true; << 2010 *n = G4ThreeVector(0.,0.,-1.); << 2011 } << 2012 return snxt = 0 ; << 2013 } 2096 } 2014 sd = -p.z()/v.z(); << 2097 if (s > flexRadMaxTolerance*0.5 ) // && s<sr) 2015 << 2016 if( sd < stheta ) << 2017 { 2098 { 2018 stheta = sd; << 2099 stheta = s ; 2019 sidetheta = kETheta; << 2100 sidetheta = kSTheta ; 2020 } 2101 } 2021 } 2102 } 2022 } 2103 } 2023 else // kons is not plane << 2024 { << 2025 t1 = 1-v.z()*v.z()*(1+tanETh << 2026 t2 = pDotV2d-p.z()*v.z()*tan << 2027 dist2ETheta = rho2-p.z()*p.z()*tanETh << 2028 2104 2029 distTheta = std::sqrt(rho2)-p.z()*tan << 2105 // Possible intersection with ETheta cone 2030 << 2106 2031 if( std::fabs(t1) < halfAngTolerance << 2107 if (fSTheta + fDTheta < pi) 2032 { << 2108 { 2033 if( v.z() < 0. ) << 2109 t1 = 1-v.z()*v.z()*(1+tanETheta2); 2034 { << 2110 t2 = pDotV2d-p.z()*v.z()*tanETheta2; 2035 if(std::fabs(distTheta) < halfRma << 2111 b = t2/t1; 2036 { << 2112 c = dist2ETheta/t1; 2037 if( (eTheta > halfpi) && (p.z() << 2113 d2 = b*b-c ; 2038 { << 2039 if( calcNorm ) { *validNorm << 2040 return snxt = 0.; << 2041 } << 2042 else if ( (eTheta < halfpi) && << 2043 { << 2044 if( calcNorm ) << 2045 { << 2046 *validNorm = true; << 2047 if (rho2 != 0.0) << 2048 { << 2049 rhoSecTheta = std::sqrt(r << 2050 *n = G4ThreeVector( p.x() << 2051 p.y() << 2052 -sinE << 2053 } << 2054 else { *n = G4ThreeVector( << 2055 } << 2056 return snxt = 0.; << 2057 } << 2058 } << 2059 sd = -0.5*dist2ETheta/t2; << 2060 2114 2061 if( sd < stheta ) << 2115 if ( d2 >= 0 ) 2062 { << 2063 stheta = sd; << 2064 sidetheta = kETheta; << 2065 } << 2066 } << 2067 } // 2nd order equation, 1st roo << 2068 else // 2nd if 1st root -ve << 2069 { 2116 { 2070 if ( std::fabs(distTheta) < halfRma << 2117 d = std::sqrt(d2); 2071 { << 2118 s = -b - d ; // First root 2072 if( (eTheta < halfpi) && (t2 >= 0 << 2119 2073 { << 2120 if ( s < 0 ) 2074 if( calcNorm ) << 2075 { << 2076 *validNorm = true; << 2077 if (rho2 != 0.0) << 2078 { << 2079 rhoSecTheta = std::sqrt(r << 2080 *n = G4ThreeVector( p.x() << 2081 p.y() << 2082 -sinE << 2083 } << 2084 else *n = G4ThreeVector(0.,0. << 2085 } << 2086 return snxt = 0.; << 2087 } << 2088 else if ( (eTheta > halfpi) << 2089 && (t2 < 0.) && (p.z() <=0 << 2090 { << 2091 if( calcNorm ) { *validNorm = << 2092 return snxt = 0.; << 2093 } << 2094 } << 2095 b = t2/t1; << 2096 c = dist2ETheta/t1; << 2097 d2 = b*b - c ; << 2098 if ( (d2 <halfRmaxTolerance) && (d2 << 2099 { 2121 { 2100 d2 = 0.; << 2122 s=-b+d; // Second root 2101 } 2123 } 2102 if ( d2 >= 0. ) << 2124 if (s > flexRadMaxTolerance*0.5 && s < stheta ) 2103 { 2125 { 2104 d = std::sqrt(d2); << 2126 stheta = s ; 2105 << 2127 sidetheta = kETheta ; 2106 if( eTheta < halfpi ) << 2107 { << 2108 sd = -b - d; // First r << 2109 << 2110 if( ((std::fabs(sd) < halfRmaxT << 2111 || (sd < 0.) ) << 2112 { << 2113 sd = -b + d ; // 2nd root << 2114 } << 2115 if( sd > halfRmaxTolerance ) << 2116 { << 2117 if( sd < stheta ) << 2118 { << 2119 stheta = sd; << 2120 sidetheta = kETheta; << 2121 } << 2122 } << 2123 } << 2124 else // sTheta+fDTheta > pi/2, co << 2125 { << 2126 sd = -b - d; // First r << 2127 << 2128 if ( ((std::fabs(sd) < halfRmax << 2129 || (sd < 0.) << 2130 || ( (sd > 0.) && (p.z() + sd << 2131 { << 2132 sd = -b + d ; // 2nd root << 2133 } << 2134 if ( ( sd>halfRmaxTolerance ) << 2135 && ( p.z()+sd*v.z() <= halfRm << 2136 { << 2137 if( sd < stheta ) << 2138 { << 2139 stheta = sd; << 2140 sidetheta = kETheta; << 2141 } << 2142 } << 2143 } << 2144 } 2128 } 2145 } 2129 } 2146 } 2130 } 2147 } << 2131 } 2148 << 2132 } 2149 } // end theta intersections << 2150 2133 2151 // Phi Intersection 2134 // Phi Intersection 2152 << 2135 2153 if ( !fFullPhiSphere ) << 2136 if ( fDPhi < twopi) 2154 { 2137 { 2155 if ( (p.x() != 0.0) || (p.y() != 0.0) ) / << 2138 sinSPhi=std::sin(fSPhi); >> 2139 cosSPhi=std::cos(fSPhi); >> 2140 ePhi=fSPhi+fDPhi; >> 2141 sinEPhi=std::sin(ePhi); >> 2142 cosEPhi=std::cos(ePhi); >> 2143 cPhi=fSPhi+fDPhi*0.5; >> 2144 sinCPhi=std::sin(cPhi); >> 2145 cosCPhi=std::cos(cPhi); >> 2146 >> 2147 if ( p.x()||p.y() ) // Check if on z axis (rho not needed later) 2156 { 2148 { 2157 // pDist -ve when inside 2149 // pDist -ve when inside 2158 2150 2159 pDistS=p.x()*sinSPhi-p.y()*cosSPhi; 2151 pDistS=p.x()*sinSPhi-p.y()*cosSPhi; 2160 pDistE=-p.x()*sinEPhi+p.y()*cosEPhi; 2152 pDistE=-p.x()*sinEPhi+p.y()*cosEPhi; 2161 2153 2162 // Comp -ve when in direction of outwar 2154 // Comp -ve when in direction of outwards normal 2163 2155 2164 compS = -sinSPhi*v.x()+cosSPhi*v.y() 2156 compS = -sinSPhi*v.x()+cosSPhi*v.y() ; 2165 compE = sinEPhi*v.x()-cosEPhi*v.y() 2157 compE = sinEPhi*v.x()-cosEPhi*v.y() ; 2166 sidephi = kNull ; 2158 sidephi = kNull ; 2167 2159 2168 if ( (pDistS <= 0) && (pDistE <= 0) ) << 2160 if ( pDistS <= 0 && pDistE <= 0 ) 2169 { 2161 { 2170 // Inside both phi *full* planes 2162 // Inside both phi *full* planes 2171 2163 2172 if ( compS < 0 ) 2164 if ( compS < 0 ) 2173 { 2165 { 2174 sphi = pDistS/compS ; 2166 sphi = pDistS/compS ; 2175 xi = p.x()+sphi*v.x() ; 2167 xi = p.x()+sphi*v.x() ; 2176 yi = p.y()+sphi*v.y() ; 2168 yi = p.y()+sphi*v.y() ; 2177 2169 2178 // Check intersection with correct << 2170 // Check intersecting with correct half-plane 2179 // << 2171 // (if not -> no intersect) 2180 if( (std::fabs(xi)<=kCarTolerance) << 2172 2181 { << 2173 if ( ( yi*cosCPhi - xi*sinCPhi ) >= 0 ) 2182 vphi = std::atan2(v.y(),v.x()); << 2183 sidephi = kSPhi; << 2184 if ( ( (fSPhi-halfAngTolerance) < << 2185 && ( (ePhi+halfAngTolerance) > << 2186 { << 2187 sphi = kInfinity; << 2188 } << 2189 } << 2190 else if ( ( yi*cosCPhi - xi*sinCPhi << 2191 { 2174 { 2192 sphi=kInfinity; 2175 sphi=kInfinity; 2193 } 2176 } 2194 else 2177 else 2195 { 2178 { 2196 sidephi = kSPhi ; 2179 sidephi = kSPhi ; 2197 if ( pDistS > -halfCarTolerance) << 2180 if ( pDistS > -0.5*kCarTolerance) sphi =0 ; // Leave by sphi 2198 } 2181 } 2199 } 2182 } 2200 else { sphi = kInfinity; } << 2183 else sphi = kInfinity ; 2201 2184 2202 if ( compE < 0 ) 2185 if ( compE < 0 ) 2203 { 2186 { 2204 sphi2=pDistE/compE ; 2187 sphi2=pDistE/compE ; 2205 if (sphi2 < sphi) // Only check fur 2188 if (sphi2 < sphi) // Only check further if < starting phi intersection 2206 { 2189 { 2207 xi = p.x()+sphi2*v.x() ; 2190 xi = p.x()+sphi2*v.x() ; 2208 yi = p.y()+sphi2*v.y() ; 2191 yi = p.y()+sphi2*v.y() ; 2209 2192 2210 // Check intersection with correc << 2193 // Check intersecting with correct half-plane 2211 // << 2194 2212 if ( (std::fabs(xi)<=kCarToleranc << 2195 if ((yi*cosCPhi-xi*sinCPhi)>=0) // Leaving via ending phi 2213 && (std::fabs(yi)<=kCarToleranc << 2214 { << 2215 // Leaving via ending phi << 2216 // << 2217 vphi = std::atan2(v.y(),v.x()) << 2218 << 2219 if( (fSPhi-halfAngTolerance > v << 2220 ||(fSPhi+fDPhi+halfAngToler << 2221 { << 2222 sidephi = kEPhi; << 2223 if ( pDistE <= -halfCarTolera << 2224 else << 2225 } << 2226 } << 2227 else if ((yi*cosCPhi-xi*sinCPhi)> << 2228 { 2196 { 2229 sidephi = kEPhi ; 2197 sidephi = kEPhi ; 2230 if ( pDistE <= -halfCarToleranc << 2198 if ( pDistE <= -0.5*kCarTolerance ) 2231 { 2199 { 2232 sphi=sphi2; 2200 sphi=sphi2; 2233 } 2201 } 2234 else << 2202 else 2235 { 2203 { 2236 sphi = 0 ; 2204 sphi = 0 ; 2237 } 2205 } 2238 } 2206 } 2239 } 2207 } 2240 } << 2208 } 2241 } 2209 } 2242 else if ((pDistS >= 0) && (pDistE >= 0) << 2210 else if ( pDistS >= 0 && pDistE >= 0 ) // Outside both *full* phi planes 2243 { 2211 { 2244 if ( pDistS <= pDistE ) 2212 if ( pDistS <= pDistE ) 2245 { 2213 { 2246 sidephi = kSPhi ; 2214 sidephi = kSPhi ; 2247 } 2215 } 2248 else 2216 else 2249 { 2217 { 2250 sidephi = kEPhi ; 2218 sidephi = kEPhi ; 2251 } 2219 } 2252 if ( fDPhi > pi ) 2220 if ( fDPhi > pi ) 2253 { 2221 { 2254 if ( (compS < 0) && (compE < 0) ) << 2222 if ( compS < 0 && compE < 0 ) sphi = 0 ; 2255 else << 2223 else sphi = kInfinity ; 2256 } 2224 } 2257 else 2225 else 2258 { 2226 { 2259 // if towards both >=0 then once in 2227 // if towards both >=0 then once inside (after error) 2260 // will remain inside 2228 // will remain inside 2261 2229 2262 if ( (compS >= 0) && (compE >= 0) ) << 2230 if ( compS >= 0 && compE >= 0 ) 2263 else << 2231 { 2264 } << 2232 sphi=kInfinity; >> 2233 } >> 2234 else >> 2235 { >> 2236 sphi=0; >> 2237 } >> 2238 } 2265 } 2239 } 2266 else if ( (pDistS > 0) && (pDistE < 0) << 2240 else if ( pDistS > 0 && pDistE < 0 ) 2267 { 2241 { 2268 // Outside full starting plane, insid 2242 // Outside full starting plane, inside full ending plane 2269 2243 2270 if ( fDPhi > pi ) 2244 if ( fDPhi > pi ) 2271 { 2245 { 2272 if ( compE < 0 ) 2246 if ( compE < 0 ) 2273 { 2247 { 2274 sphi = pDistE/compE ; 2248 sphi = pDistE/compE ; 2275 xi = p.x() + sphi*v.x() ; 2249 xi = p.x() + sphi*v.x() ; 2276 yi = p.y() + sphi*v.y() ; 2250 yi = p.y() + sphi*v.y() ; 2277 2251 2278 // Check intersection in correct 2252 // Check intersection in correct half-plane 2279 // (if not -> not leaving phi ext 2253 // (if not -> not leaving phi extent) 2280 // 2254 // 2281 if( (std::fabs(xi)<=kCarTolerance << 2255 if ( ( yi*cosCPhi - xi*sinCPhi ) <= 0 ) 2282 { << 2283 vphi = std::atan2(v.y(),v.x()); << 2284 sidephi = kSPhi; << 2285 if ( ( (fSPhi-halfAngTolerance) << 2286 && ( (ePhi+halfAngTolerance) << 2287 { << 2288 sphi = kInfinity; << 2289 } << 2290 } << 2291 else if ( ( yi*cosCPhi - xi*sinCP << 2292 { 2256 { 2293 sphi = kInfinity ; 2257 sphi = kInfinity ; 2294 } 2258 } 2295 else // Leaving via Ending phi 2259 else // Leaving via Ending phi 2296 { 2260 { 2297 sidephi = kEPhi ; 2261 sidephi = kEPhi ; 2298 if ( pDistE > -halfCarTolerance << 2262 if ( pDistE > -0.5*kCarTolerance ) sphi = 0. ; 2299 } 2263 } 2300 } 2264 } 2301 else 2265 else 2302 { 2266 { 2303 sphi = kInfinity ; 2267 sphi = kInfinity ; 2304 } 2268 } 2305 } 2269 } 2306 else 2270 else 2307 { 2271 { 2308 if ( compS >= 0 ) 2272 if ( compS >= 0 ) 2309 { 2273 { 2310 if ( compE < 0 ) 2274 if ( compE < 0 ) 2311 { << 2275 { 2312 sphi = pDistE/compE ; 2276 sphi = pDistE/compE ; 2313 xi = p.x() + sphi*v.x() ; 2277 xi = p.x() + sphi*v.x() ; 2314 yi = p.y() + sphi*v.y() ; 2278 yi = p.y() + sphi*v.y() ; 2315 2279 2316 // Check intersection in correc 2280 // Check intersection in correct half-plane 2317 // (if not -> remain in extent) 2281 // (if not -> remain in extent) 2318 // 2282 // 2319 if( (std::fabs(xi)<=kCarToleran << 2283 if ( ( yi*cosCPhi - xi*sinCPhi) <= 0 ) 2320 && (std::fabs(yi)<=kCarToleran << 2321 { << 2322 vphi = std::atan2(v.y(),v.x() << 2323 sidephi = kSPhi; << 2324 if ( ( (fSPhi-halfAngToleranc << 2325 && ( (ePhi+halfAngTolerance << 2326 { << 2327 sphi = kInfinity; << 2328 } << 2329 } << 2330 else if ( ( yi*cosCPhi - xi*sin << 2331 { 2284 { 2332 sphi=kInfinity; 2285 sphi=kInfinity; 2333 } 2286 } 2334 else // otherwise leaving via E 2287 else // otherwise leaving via Ending phi 2335 { 2288 { 2336 sidephi = kEPhi ; 2289 sidephi = kEPhi ; 2337 } 2290 } 2338 } 2291 } 2339 else sphi=kInfinity; 2292 else sphi=kInfinity; 2340 } 2293 } 2341 else // leaving immediately by star 2294 else // leaving immediately by starting phi 2342 { 2295 { 2343 sidephi = kSPhi ; 2296 sidephi = kSPhi ; 2344 sphi = 0 ; 2297 sphi = 0 ; 2345 } 2298 } 2346 } 2299 } 2347 } 2300 } 2348 else 2301 else 2349 { 2302 { 2350 // Must be pDistS < 0 && pDistE > 0 2303 // Must be pDistS < 0 && pDistE > 0 2351 // Inside full starting plane, outsid 2304 // Inside full starting plane, outside full ending plane 2352 2305 2353 if ( fDPhi > pi ) 2306 if ( fDPhi > pi ) 2354 { 2307 { 2355 if ( compS < 0 ) 2308 if ( compS < 0 ) 2356 { 2309 { 2357 sphi=pDistS/compS; 2310 sphi=pDistS/compS; 2358 xi=p.x()+sphi*v.x(); 2311 xi=p.x()+sphi*v.x(); 2359 yi=p.y()+sphi*v.y(); 2312 yi=p.y()+sphi*v.y(); 2360 2313 2361 // Check intersection in correct 2314 // Check intersection in correct half-plane 2362 // (if not -> not leaving phi ext 2315 // (if not -> not leaving phi extent) 2363 // 2316 // 2364 if( (std::fabs(xi)<=kCarTolerance << 2317 if ( ( yi*cosCPhi - xi*sinCPhi ) >= 0 ) 2365 { << 2366 vphi = std::atan2(v.y(),v.x()) << 2367 sidephi = kSPhi; << 2368 if ( ( (fSPhi-halfAngTolerance) << 2369 && ( (ePhi+halfAngTolerance) << 2370 { << 2371 sphi = kInfinity; << 2372 } << 2373 } << 2374 else if ( ( yi*cosCPhi - xi*sinCP << 2375 { 2318 { 2376 sphi = kInfinity ; 2319 sphi = kInfinity ; 2377 } 2320 } 2378 else // Leaving via Starting phi << 2321 else // Leaving via Starting phi 2379 { << 2322 { 2380 sidephi = kSPhi ; 2323 sidephi = kSPhi ; 2381 if ( pDistS > -halfCarTolerance << 2324 if ( pDistS > -0.5*kCarTolerance ) sphi = 0 ; 2382 } 2325 } 2383 } 2326 } 2384 else 2327 else 2385 { 2328 { 2386 sphi = kInfinity ; 2329 sphi = kInfinity ; 2387 } 2330 } 2388 } 2331 } 2389 else 2332 else 2390 { 2333 { 2391 if ( compE >= 0 ) 2334 if ( compE >= 0 ) 2392 { 2335 { 2393 if ( compS < 0 ) 2336 if ( compS < 0 ) 2394 { 2337 { 2395 sphi = pDistS/compS ; 2338 sphi = pDistS/compS ; 2396 xi = p.x()+sphi*v.x() ; 2339 xi = p.x()+sphi*v.x() ; 2397 yi = p.y()+sphi*v.y() ; 2340 yi = p.y()+sphi*v.y() ; 2398 2341 2399 // Check intersection in correc 2342 // Check intersection in correct half-plane 2400 // (if not -> remain in extent) 2343 // (if not -> remain in extent) 2401 // 2344 // 2402 if( (std::fabs(xi)<=kCarToleran << 2345 if ( ( yi*cosCPhi - xi*sinCPhi ) >= 0 ) 2403 && (std::fabs(yi)<=kCarToleran << 2404 { << 2405 vphi = std::atan2(v.y(),v.x() << 2406 sidephi = kSPhi; << 2407 if ( ( (fSPhi-halfAngToleranc << 2408 && ( (ePhi+halfAngTolerance << 2409 { << 2410 sphi = kInfinity; << 2411 } << 2412 } << 2413 else if ( ( yi*cosCPhi - xi*sin << 2414 { 2346 { 2415 sphi = kInfinity ; 2347 sphi = kInfinity ; 2416 } 2348 } 2417 else // otherwise leaving via S 2349 else // otherwise leaving via Starting phi 2418 { 2350 { 2419 sidephi = kSPhi ; 2351 sidephi = kSPhi ; 2420 } 2352 } 2421 } 2353 } 2422 else 2354 else 2423 { 2355 { 2424 sphi = kInfinity ; 2356 sphi = kInfinity ; 2425 } 2357 } 2426 } 2358 } 2427 else // leaving immediately by endi 2359 else // leaving immediately by ending 2428 { 2360 { 2429 sidephi = kEPhi ; 2361 sidephi = kEPhi ; 2430 sphi = 0 ; 2362 sphi = 0 ; 2431 } 2363 } 2432 } 2364 } 2433 } << 2365 } 2434 } 2366 } 2435 else 2367 else 2436 { 2368 { 2437 // On z axis + travel not || to z axis 2369 // On z axis + travel not || to z axis -> if phi of vector direction 2438 // within phi of shape, Step limited by 2370 // within phi of shape, Step limited by rmax, else Step =0 2439 2371 2440 if ( (v.x() != 0.0) || (v.y() != 0.0) ) << 2372 if ( v.x() || v.y() ) 2441 { 2373 { 2442 vphi = std::atan2(v.y(),v.x()) ; 2374 vphi = std::atan2(v.y(),v.x()) ; 2443 if ((fSPhi-halfAngTolerance < vphi) & << 2375 if ( fSPhi < vphi && vphi < fSPhi + fDPhi ) 2444 { 2376 { 2445 sphi = kInfinity; << 2377 sphi=kInfinity; 2446 } 2378 } 2447 else 2379 else 2448 { 2380 { 2449 sidephi = kSPhi ; // arbitrary << 2381 sidephi = kSPhi ; // arbitrary 2450 sphi = 0 ; 2382 sphi = 0 ; 2451 } 2383 } 2452 } 2384 } 2453 else // travel along z - no phi inters << 2385 else // travel along z - no phi intersaction 2454 { 2386 { 2455 sphi = kInfinity ; 2387 sphi = kInfinity ; 2456 } 2388 } 2457 } 2389 } 2458 if ( sphi < snxt ) // Order intersecttio 2390 if ( sphi < snxt ) // Order intersecttions 2459 { 2391 { 2460 snxt = sphi ; 2392 snxt = sphi ; 2461 side = sidephi ; 2393 side = sidephi ; 2462 } 2394 } 2463 } 2395 } 2464 if (stheta < snxt ) // Order intersections 2396 if (stheta < snxt ) // Order intersections 2465 { 2397 { 2466 snxt = stheta ; 2398 snxt = stheta ; 2467 side = sidetheta ; 2399 side = sidetheta ; 2468 } 2400 } 2469 2401 2470 if (calcNorm) // Output switch operator 2402 if (calcNorm) // Output switch operator 2471 { 2403 { 2472 switch( side ) 2404 switch( side ) 2473 { 2405 { 2474 case kRMax: 2406 case kRMax: 2475 xi=p.x()+snxt*v.x(); 2407 xi=p.x()+snxt*v.x(); 2476 yi=p.y()+snxt*v.y(); 2408 yi=p.y()+snxt*v.y(); 2477 zi=p.z()+snxt*v.z(); 2409 zi=p.z()+snxt*v.z(); 2478 *n=G4ThreeVector(xi/fRmax,yi/fRmax,zi 2410 *n=G4ThreeVector(xi/fRmax,yi/fRmax,zi/fRmax); 2479 *validNorm=true; 2411 *validNorm=true; 2480 break; 2412 break; 2481 << 2482 case kRMin: 2413 case kRMin: 2483 *validNorm=false; // Rmin is concave 2414 *validNorm=false; // Rmin is concave 2484 break; 2415 break; 2485 << 2486 case kSPhi: 2416 case kSPhi: 2487 if ( fDPhi <= pi ) // Normal to P << 2417 if (fDPhi<=pi) // Normal to Phi- 2488 { 2418 { 2489 *n=G4ThreeVector(sinSPhi,-cosSPhi,0 << 2419 *n=G4ThreeVector(std::sin(fSPhi),-std::cos(fSPhi),0); 2490 *validNorm=true; 2420 *validNorm=true; 2491 } 2421 } 2492 else { *validNorm=false; } << 2422 else *validNorm=false; 2493 break ; 2423 break ; 2494 << 2495 case kEPhi: 2424 case kEPhi: 2496 if ( fDPhi <= pi ) // Normal to << 2425 if (fDPhi<=pi) // Normal to Phi+ 2497 { 2426 { 2498 *n=G4ThreeVector(-sinEPhi,cosEPhi,0 << 2427 *n=G4ThreeVector(-std::sin(fSPhi+fDPhi),std::cos(fSPhi+fDPhi),0); 2499 *validNorm=true; 2428 *validNorm=true; 2500 } 2429 } 2501 else { *validNorm=false; } << 2430 else *validNorm=false; 2502 break; 2431 break; 2503 << 2504 case kSTheta: 2432 case kSTheta: 2505 if( fSTheta == halfpi ) << 2433 if( fSTheta == pi*0.5 ) 2506 { 2434 { 2507 *n=G4ThreeVector(0.,0.,1.); << 2435 *n=G4ThreeVector(0,0,1); 2508 *validNorm=true; 2436 *validNorm=true; 2509 } 2437 } 2510 else if ( fSTheta > halfpi ) << 2438 else if ( fSTheta > pi ) 2511 { 2439 { 2512 xi = p.x() + snxt*v.x(); << 2440 xi=p.x()+snxt*v.x(); 2513 yi = p.y() + snxt*v.y(); << 2441 yi=p.y()+snxt*v.y(); 2514 rho2=xi*xi+yi*yi; << 2442 rhoSecTheta = std::sqrt((xi*xi+yi*yi)*(1+tanSTheta2)) ; 2515 if (rho2 != 0.0) << 2443 *n = G4ThreeVector(-xi/rhoSecTheta, // N- 2516 { << 2444 -yi/rhoSecTheta, 2517 rhoSecTheta = std::sqrt(rho2*(1+t << 2445 tanSTheta/std::sqrt(1+tanSTheta2)) ; 2518 *n = G4ThreeVector( xi/rhoSecThet << 2519 -tanSTheta/std << 2520 } << 2521 else << 2522 { << 2523 *n = G4ThreeVector(0.,0.,1.); << 2524 } << 2525 *validNorm=true; 2446 *validNorm=true; 2526 } 2447 } 2527 else { *validNorm=false; } // Conca << 2448 else *validNorm=false; // Concave STheta cone 2528 break; 2449 break; 2529 << 2530 case kETheta: 2450 case kETheta: 2531 if( eTheta == halfpi ) << 2451 if( ( fSTheta + fDTheta ) == pi*0.5 ) 2532 { 2452 { 2533 *n = G4ThreeVector(0.,0.,-1 << 2453 *n = G4ThreeVector(0,0,-1); 2534 *validNorm = true; << 2454 *validNorm = true ; 2535 } 2455 } 2536 else if ( eTheta < halfpi ) << 2456 else if ( ( fSTheta + fDTheta ) < pi ) 2537 { 2457 { 2538 xi=p.x()+snxt*v.x(); 2458 xi=p.x()+snxt*v.x(); 2539 yi=p.y()+snxt*v.y(); 2459 yi=p.y()+snxt*v.y(); 2540 rho2=xi*xi+yi*yi; << 2460 rhoSecTheta = std::sqrt((xi*xi+yi*yi)*(1+tanETheta2)) ; 2541 if (rho2 != 0.0) << 2461 *n = G4ThreeVector( xi/rhoSecTheta, // N+ 2542 { << 2462 yi/rhoSecTheta, 2543 rhoSecTheta = std::sqrt(rho2*(1+t << 2463 -tanSTheta/std::sqrt(1+tanSTheta2) ) ; 2544 *n = G4ThreeVector( xi/rhoSecThet << 2545 -tanETheta/std << 2546 } << 2547 else << 2548 { << 2549 *n = G4ThreeVector(0.,0.,-1.); << 2550 } << 2551 *validNorm=true; 2464 *validNorm=true; 2552 } 2465 } 2553 else { *validNorm=false; } // Conc << 2466 else *validNorm=false; // Concave ETheta cone 2554 break; 2467 break; 2555 << 2556 default: 2468 default: >> 2469 G4cout.precision(16); 2557 G4cout << G4endl; 2470 G4cout << G4endl; 2558 DumpInfo(); 2471 DumpInfo(); 2559 std::ostringstream message; << 2472 G4cout << "Position:" << G4endl << G4endl; 2560 G4long oldprc = message.precision(16) << 2473 G4cout << "p.x() = " << p.x()/mm << " mm" << G4endl; 2561 message << "Undefined side for valid << 2474 G4cout << "p.y() = " << p.y()/mm << " mm" << G4endl; 2562 << G4endl << 2475 G4cout << "p.z() = " << p.z()/mm << " mm" << G4endl << G4endl; 2563 << "Position:" << G4endl << << 2476 G4cout << "Direction:" << G4endl << G4endl; 2564 << "p.x() = " << p.x()/mm < << 2477 G4cout << "v.x() = " << v.x() << G4endl; 2565 << "p.y() = " << p.y()/mm < << 2478 G4cout << "v.y() = " << v.y() << G4endl; 2566 << "p.z() = " << p.z()/mm < << 2479 G4cout << "v.z() = " << v.z() << G4endl << G4endl; 2567 << "Direction:" << G4endl << << 2480 G4cout << "Proposed distance :" << G4endl << G4endl; 2568 << "v.x() = " << v.x() << G << 2481 G4cout << "snxt = " << snxt/mm << " mm" << G4endl << G4endl; 2569 << "v.y() = " << v.y() << G << 2570 << "v.z() = " << v.z() << G << 2571 << "Proposed distance :" << G << 2572 << "snxt = " << snxt/mm << << 2573 message.precision(oldprc); << 2574 G4Exception("G4Sphere::DistanceToOut( 2482 G4Exception("G4Sphere::DistanceToOut(p,v,..)", 2575 "GeomSolids1002", JustWar << 2483 "Notification", JustWarning, >> 2484 "Undefined side for valid surface normal to solid."); 2576 break; 2485 break; 2577 } 2486 } 2578 } 2487 } 2579 if (snxt == kInfinity) 2488 if (snxt == kInfinity) 2580 { 2489 { >> 2490 G4cout.precision(24); 2581 G4cout << G4endl; 2491 G4cout << G4endl; 2582 DumpInfo(); 2492 DumpInfo(); 2583 std::ostringstream message; << 2493 G4cout << "Position:" << G4endl << G4endl; 2584 G4long oldprc = message.precision(16); << 2494 G4cout << "p.x() = " << p.x()/mm << " mm" << G4endl; 2585 message << "Logic error: snxt = kInfinity << 2495 G4cout << "p.y() = " << p.y()/mm << " mm" << G4endl; 2586 << "Position:" << G4endl << G4en << 2496 G4cout << "p.z() = " << p.z()/mm << " mm" << G4endl << G4endl; 2587 << "p.x() = " << p.x()/mm << " << 2497 G4cout << "Rp = "<< std::sqrt( p.x()*p.x()+p.y()*p.y()+p.z()*p.z() )/mm << " mm" 2588 << "p.y() = " << p.y()/mm << " << 2498 << G4endl << G4endl; 2589 << "p.z() = " << p.z()/mm << " << 2499 G4cout << "Direction:" << G4endl << G4endl; 2590 << "Rp = "<< std::sqrt( p.x()*p.x << 2500 G4cout << "v.x() = " << v.x() << G4endl; 2591 << " mm" << G4endl << G4endl << 2501 G4cout << "v.y() = " << v.y() << G4endl; 2592 << "Direction:" << G4endl << G4en << 2502 G4cout << "v.z() = " << v.z() << G4endl << G4endl; 2593 << "v.x() = " << v.x() << G4end << 2503 G4cout << "Proposed distance :" << G4endl << G4endl; 2594 << "v.y() = " << v.y() << G4end << 2504 G4cout << "snxt = " << snxt/mm << " mm" << G4endl << G4endl; 2595 << "v.z() = " << v.z() << G4end << 2596 << "Proposed distance :" << G4end << 2597 << "snxt = " << snxt/mm << " m << 2598 message.precision(oldprc); << 2599 G4Exception("G4Sphere::DistanceToOut(p,v, 2505 G4Exception("G4Sphere::DistanceToOut(p,v,..)", 2600 "GeomSolids1002", JustWarning << 2506 "Notification", JustWarning, >> 2507 "Logic error: snxt = kInfinity ???"); 2601 } 2508 } 2602 2509 2603 return snxt; 2510 return snxt; 2604 } 2511 } 2605 2512 2606 ///////////////////////////////////////////// 2513 ///////////////////////////////////////////////////////////////////////// 2607 // 2514 // 2608 // Calculate distance (<=actual) to closest s << 2515 // Calcluate distance (<=actual) to closest surface of shape from inside 2609 2516 2610 G4double G4Sphere::DistanceToOut( const G4Thr 2517 G4double G4Sphere::DistanceToOut( const G4ThreeVector& p ) const 2611 { 2518 { 2612 G4double safe=0.0,safeRMin,safeRMax,safePhi 2519 G4double safe=0.0,safeRMin,safeRMax,safePhi,safeTheta; 2613 G4double rho2,rds,rho; << 2520 G4double rho2,rad,rho; 2614 G4double pTheta,dTheta1 = kInfinity,dTheta2 << 2521 G4double phiC,cosPhiC,sinPhiC,ePhi; >> 2522 G4double pTheta,dTheta1,dTheta2; 2615 rho2=p.x()*p.x()+p.y()*p.y(); 2523 rho2=p.x()*p.x()+p.y()*p.y(); 2616 rds=std::sqrt(rho2+p.z()*p.z()); << 2524 rad=std::sqrt(rho2+p.z()*p.z()); 2617 rho=std::sqrt(rho2); 2525 rho=std::sqrt(rho2); 2618 2526 2619 #ifdef G4CSGDEBUG 2527 #ifdef G4CSGDEBUG 2620 if( Inside(p) == kOutside ) 2528 if( Inside(p) == kOutside ) 2621 { 2529 { 2622 G4long old_prc = G4cout.precision(16); << 2530 G4cout.precision(16) ; 2623 G4cout << G4endl; << 2531 G4cout << G4endl ; 2624 DumpInfo(); 2532 DumpInfo(); 2625 G4cout << "Position:" << G4endl << G4en 2533 G4cout << "Position:" << G4endl << G4endl ; 2626 G4cout << "p.x() = " << p.x()/mm << " 2534 G4cout << "p.x() = " << p.x()/mm << " mm" << G4endl ; 2627 G4cout << "p.y() = " << p.y()/mm << " 2535 G4cout << "p.y() = " << p.y()/mm << " mm" << G4endl ; 2628 G4cout << "p.z() = " << p.z()/mm << " 2536 G4cout << "p.z() = " << p.z()/mm << " mm" << G4endl << G4endl ; 2629 G4cout.precision(old_prc) ; << 2630 G4Exception("G4Sphere::DistanceToOut(p)" 2537 G4Exception("G4Sphere::DistanceToOut(p)", 2631 "GeomSolids1002", JustWarnin << 2538 "Notification", JustWarning, "Point p is outside !?" ); 2632 } 2539 } 2633 #endif 2540 #endif 2634 2541 2635 // Distance to r shells << 2636 // 2542 // 2637 safeRMax = fRmax-rds; << 2543 // Distance to r shells 2638 safe = safeRMax; << 2544 // 2639 if (fRmin != 0.0) << 2545 if (fRmin) >> 2546 { >> 2547 safeRMin=rad-fRmin; >> 2548 safeRMax=fRmax-rad; >> 2549 if (safeRMin<safeRMax) >> 2550 { >> 2551 safe=safeRMin; >> 2552 } >> 2553 else >> 2554 { >> 2555 safe=safeRMax; >> 2556 } >> 2557 } >> 2558 else 2640 { 2559 { 2641 safeRMin = rds-fRmin; << 2560 safe=fRmax-rad; 2642 safe = std::min( safeRMin, safeRMax ); << 2643 } 2561 } 2644 2562 >> 2563 // 2645 // Distance to phi extent 2564 // Distance to phi extent 2646 // 2565 // 2647 if ( !fFullPhiSphere ) << 2566 if (fDPhi<twopi && rho) 2648 { 2567 { 2649 if (rho>0.0) << 2568 phiC=fSPhi+fDPhi*0.5; 2650 { << 2569 cosPhiC=std::cos(phiC); 2651 if ((p.y()*cosCPhi-p.x()*sinCPhi)<=0) << 2570 sinPhiC=std::sin(phiC); 2652 { << 2571 if ((p.y()*cosPhiC-p.x()*sinPhiC)<=0) 2653 safePhi=-(p.x()*sinSPhi-p.y()*cosS << 2572 { 2654 } << 2573 safePhi=-(p.x()*std::sin(fSPhi)-p.y()*std::cos(fSPhi)); 2655 else << 2574 } 2656 { << 2575 else 2657 safePhi=(p.x()*sinEPhi-p.y()*cosEP << 2576 { 2658 } << 2577 ePhi=fSPhi+fDPhi; 2659 } << 2578 safePhi=(p.x()*std::sin(ePhi)-p.y()*std::cos(ePhi)); 2660 else << 2579 } 2661 { << 2580 if (safePhi<safe) safe=safePhi; 2662 safePhi = 0.0; // Distance to both P << 2663 } << 2664 // Both cases above can be improved - in << 2665 // although it may be costlier (good fo << 2666 << 2667 safe= std::min(safe, safePhi); << 2668 } 2581 } 2669 2582 2670 // Distance to Theta extent << 2671 // 2583 // 2672 if ( !fFullThetaSphere ) << 2584 // Distance to Theta extent >> 2585 // >> 2586 if (rad) 2673 { 2587 { 2674 if( rds > 0.0 ) << 2588 pTheta=std::acos(p.z()/rad); >> 2589 if (pTheta<0) pTheta+=pi; >> 2590 dTheta1=pTheta-fSTheta; >> 2591 dTheta2=(fSTheta+fDTheta)-pTheta; >> 2592 if (dTheta1<dTheta2) 2675 { 2593 { 2676 pTheta=std::acos(p.z()/rds); << 2594 safeTheta=rad*std::sin(dTheta1); 2677 if (pTheta<0) { pTheta+=pi; } << 2595 if (safe>safeTheta) 2678 if(fSTheta>0.) << 2596 { 2679 { dTheta1=pTheta-fSTheta;} << 2597 safe=safeTheta; 2680 if(eTheta<pi) << 2598 } 2681 { dTheta2=eTheta-pTheta;} << 2682 << 2683 safeTheta=rds*std::sin(std::min(dTheta << 2684 } 2599 } 2685 else 2600 else 2686 { 2601 { 2687 safeTheta= 0.0; << 2602 safeTheta=rad*std::sin(dTheta2); 2688 // An improvement will be to return << 2603 if (safe>safeTheta) >> 2604 { >> 2605 safe=safeTheta; >> 2606 } 2689 } 2607 } 2690 safe = std::min( safe, safeTheta ); << 2691 } 2608 } 2692 2609 2693 if (safe<0.0) { safe=0; } << 2610 if (safe<0) safe=0; 2694 // An improvement to return negative answ << 2611 return safe; 2695 << 2696 return safe; << 2697 } 2612 } 2698 2613 2699 ///////////////////////////////////////////// 2614 ////////////////////////////////////////////////////////////////////////// 2700 // 2615 // 2701 // G4EntityType << 2616 // Create a List containing the transformed vertices >> 2617 // Ordering [0-3] -fDz cross section >> 2618 // [4-7] +fDz cross section such that [0] is below [4], >> 2619 // [1] below [5] etc. >> 2620 // Note: >> 2621 // Caller has deletion resposibility >> 2622 // Potential improvement: For last slice, use actual ending angle >> 2623 // to avoid rounding error problems. >> 2624 >> 2625 G4ThreeVectorList* >> 2626 G4Sphere::CreateRotatedVertices( const G4AffineTransform& pTransform, >> 2627 G4int& noPolygonVertices ) const >> 2628 { >> 2629 G4ThreeVectorList *vertices; >> 2630 G4ThreeVector vertex; >> 2631 G4double meshAnglePhi,meshRMax,crossAnglePhi, >> 2632 coscrossAnglePhi,sincrossAnglePhi,sAnglePhi; >> 2633 G4double meshTheta,crossTheta,startTheta; >> 2634 G4double rMaxX,rMaxY,rMinX,rMinY,rMinZ,rMaxZ; >> 2635 G4int crossSectionPhi,noPhiCrossSections,crossSectionTheta,noThetaSections; >> 2636 >> 2637 // Phi cross sections >> 2638 >> 2639 noPhiCrossSections=G4int (fDPhi/kMeshAngleDefault)+1; >> 2640 >> 2641 if (noPhiCrossSections<kMinMeshSections) >> 2642 { >> 2643 noPhiCrossSections=kMinMeshSections; >> 2644 } >> 2645 else if (noPhiCrossSections>kMaxMeshSections) >> 2646 { >> 2647 noPhiCrossSections=kMaxMeshSections; >> 2648 } >> 2649 meshAnglePhi=fDPhi/(noPhiCrossSections-1); >> 2650 >> 2651 // If complete in phi, set start angle such that mesh will be at fRMax >> 2652 // on the x axis. Will give better extent calculations when not rotated. >> 2653 >> 2654 if (fDPhi==pi*2.0 && fSPhi==0) >> 2655 { >> 2656 sAnglePhi = -meshAnglePhi*0.5; >> 2657 } >> 2658 else >> 2659 { >> 2660 sAnglePhi=fSPhi; >> 2661 } 2702 2662 2703 G4GeometryType G4Sphere::GetEntityType() cons << 2663 // Theta cross sections 2704 { << 2664 2705 return {"G4Sphere"}; << 2665 noThetaSections = G4int(fDTheta/kMeshAngleDefault)+1; >> 2666 >> 2667 if (noThetaSections<kMinMeshSections) >> 2668 { >> 2669 noThetaSections=kMinMeshSections; >> 2670 } >> 2671 else if (noThetaSections>kMaxMeshSections) >> 2672 { >> 2673 noThetaSections=kMaxMeshSections; >> 2674 } >> 2675 meshTheta=fDTheta/(noThetaSections-1); >> 2676 >> 2677 // If complete in Theta, set start angle such that mesh will be at fRMax >> 2678 // on the z axis. Will give better extent calculations when not rotated. >> 2679 >> 2680 if (fDTheta==pi && fSTheta==0) >> 2681 { >> 2682 startTheta = -meshTheta*0.5; >> 2683 } >> 2684 else >> 2685 { >> 2686 startTheta=fSTheta; >> 2687 } >> 2688 >> 2689 meshRMax = (meshAnglePhi >= meshTheta) ? >> 2690 fRmax/std::cos(meshAnglePhi*0.5) : fRmax/std::cos(meshTheta*0.5); >> 2691 G4double* cosCrossTheta = new G4double[noThetaSections]; >> 2692 G4double* sinCrossTheta = new G4double[noThetaSections]; >> 2693 vertices=new G4ThreeVectorList(); >> 2694 vertices->reserve(noPhiCrossSections*(noThetaSections*2)); >> 2695 if (vertices && cosCrossTheta && sinCrossTheta) >> 2696 { >> 2697 for (crossSectionPhi=0; >> 2698 crossSectionPhi<noPhiCrossSections; crossSectionPhi++) >> 2699 { >> 2700 crossAnglePhi=sAnglePhi+crossSectionPhi*meshAnglePhi; >> 2701 coscrossAnglePhi=std::cos(crossAnglePhi); >> 2702 sincrossAnglePhi=std::sin(crossAnglePhi); >> 2703 for (crossSectionTheta=0; >> 2704 crossSectionTheta<noThetaSections;crossSectionTheta++) >> 2705 { >> 2706 // Compute coordinates of cross section at section crossSectionPhi >> 2707 // >> 2708 crossTheta=startTheta+crossSectionTheta*meshTheta; >> 2709 cosCrossTheta[crossSectionTheta]=std::cos(crossTheta); >> 2710 sinCrossTheta[crossSectionTheta]=std::sin(crossTheta); >> 2711 >> 2712 rMinX=fRmin*sinCrossTheta[crossSectionTheta]*coscrossAnglePhi; >> 2713 rMinY=fRmin*sinCrossTheta[crossSectionTheta]*sincrossAnglePhi; >> 2714 rMinZ=fRmin*cosCrossTheta[crossSectionTheta]; >> 2715 >> 2716 vertex=G4ThreeVector(rMinX,rMinY,rMinZ); >> 2717 vertices->push_back(pTransform.TransformPoint(vertex)); >> 2718 >> 2719 } // Theta forward >> 2720 >> 2721 for (crossSectionTheta=noThetaSections-1; >> 2722 crossSectionTheta>=0; crossSectionTheta--) >> 2723 { >> 2724 rMaxX=meshRMax*sinCrossTheta[crossSectionTheta]*coscrossAnglePhi; >> 2725 rMaxY=meshRMax*sinCrossTheta[crossSectionTheta]*sincrossAnglePhi; >> 2726 rMaxZ=meshRMax*cosCrossTheta[crossSectionTheta]; >> 2727 >> 2728 vertex=G4ThreeVector(rMaxX,rMaxY,rMaxZ); >> 2729 vertices->push_back(pTransform.TransformPoint(vertex)); >> 2730 >> 2731 } // Theta back >> 2732 } // Phi >> 2733 noPolygonVertices = noThetaSections*2 ; >> 2734 } >> 2735 else >> 2736 { >> 2737 DumpInfo(); >> 2738 G4Exception("G4Sphere::CreateRotatedVertices()", >> 2739 "FatalError", FatalException, >> 2740 "Error in allocation of vertices. Out of memory !"); >> 2741 } >> 2742 >> 2743 delete[] cosCrossTheta; >> 2744 delete[] sinCrossTheta; >> 2745 >> 2746 return vertices; 2706 } 2747 } 2707 2748 2708 ///////////////////////////////////////////// 2749 ////////////////////////////////////////////////////////////////////////// 2709 // 2750 // 2710 // Make a clone of the object << 2751 // G4EntityType 2711 // << 2752 2712 G4VSolid* G4Sphere::Clone() const << 2753 G4GeometryType G4Sphere::GetEntityType() const 2713 { 2754 { 2714 return new G4Sphere(*this); << 2755 return G4String("G4Sphere"); 2715 } 2756 } 2716 2757 2717 ///////////////////////////////////////////// 2758 ////////////////////////////////////////////////////////////////////////// 2718 // 2759 // 2719 // Stream object contents to an output stream 2760 // Stream object contents to an output stream 2720 2761 2721 std::ostream& G4Sphere::StreamInfo( std::ostr 2762 std::ostream& G4Sphere::StreamInfo( std::ostream& os ) const 2722 { 2763 { 2723 G4long oldprc = os.precision(16); << 2724 os << "------------------------------------ 2764 os << "-----------------------------------------------------------\n" 2725 << " *** Dump for solid - " << GetNam 2765 << " *** Dump for solid - " << GetName() << " ***\n" 2726 << " ================================ 2766 << " ===================================================\n" 2727 << " Solid type: G4Sphere\n" 2767 << " Solid type: G4Sphere\n" 2728 << " Parameters: \n" 2768 << " Parameters: \n" 2729 << " inner radius: " << fRmin/mm << " 2769 << " inner radius: " << fRmin/mm << " mm \n" 2730 << " outer radius: " << fRmax/mm << " 2770 << " outer radius: " << fRmax/mm << " mm \n" 2731 << " starting phi of segment : " << 2771 << " starting phi of segment : " << fSPhi/degree << " degrees \n" 2732 << " delta phi of segment : " << 2772 << " delta phi of segment : " << fDPhi/degree << " degrees \n" 2733 << " starting theta of segment: " << 2773 << " starting theta of segment: " << fSTheta/degree << " degrees \n" 2734 << " delta theta of segment : " << 2774 << " delta theta of segment : " << fDTheta/degree << " degrees \n" 2735 << "------------------------------------ 2775 << "-----------------------------------------------------------\n"; 2736 os.precision(oldprc); << 2737 2776 2738 return os; 2777 return os; 2739 } 2778 } 2740 2779 2741 ///////////////////////////////////////////// << 2742 // << 2743 // Get volume << 2744 << 2745 G4double G4Sphere::GetCubicVolume() << 2746 { << 2747 if (fCubicVolume == 0.) << 2748 { << 2749 G4double RRR = fRmax*fRmax*fRmax; << 2750 G4double rrr = fRmin*fRmin*fRmin; << 2751 fCubicVolume = fDPhi*(cosSTheta - cosEThe << 2752 } << 2753 return fCubicVolume; << 2754 } << 2755 << 2756 ///////////////////////////////////////////// << 2757 // << 2758 // Get surface area << 2759 << 2760 G4double G4Sphere::GetSurfaceArea() << 2761 { << 2762 if (fSurfaceArea == 0.) << 2763 { << 2764 G4double RR = fRmax*fRmax; << 2765 G4double rr = fRmin*fRmin; << 2766 fSurfaceArea = fDPhi*(RR + rr)*(cosSTheta << 2767 if (!fFullPhiSphere) fSurfaceArea += f << 2768 if (fSTheta > 0) fSurfaceArea += 0 << 2769 if (eTheta < CLHEP::pi) fSurfaceArea += 0 << 2770 } << 2771 return fSurfaceArea; << 2772 } << 2773 << 2774 ///////////////////////////////////////////// << 2775 // << 2776 // Return a point randomly and uniformly sele << 2777 << 2778 G4ThreeVector G4Sphere::GetPointOnSurface() c << 2779 { << 2780 G4double RR = fRmax*fRmax; << 2781 G4double rr = fRmin*fRmin; << 2782 << 2783 // Find surface areas << 2784 // << 2785 G4double aInner = fDPhi*rr*(cosSTheta - c << 2786 G4double aOuter = fDPhi*RR*(cosSTheta - c << 2787 G4double aPhi = (!fFullPhiSphere) ? fDT << 2788 G4double aSTheta = (fSTheta > 0) ? 0.5*fDP << 2789 G4double aETheta = (eTheta < pi) ? 0.5*fDP << 2790 G4double aTotal = aInner + aOuter + aPhi << 2791 << 2792 // Select surface and generate a point << 2793 // << 2794 G4double select = aTotal*G4QuickRand(); << 2795 G4double u = G4QuickRand(); << 2796 G4double v = G4QuickRand(); << 2797 if (select < aInner + aOuter) // << 2798 { << 2799 G4double r = (select < aInner) ? fRmin << 2800 G4double z = cosSTheta + (cosETheta - c << 2801 G4double rho = std::sqrt(1. - z*z); << 2802 G4double phi = fDPhi*v + fSPhi; << 2803 return { r*rho*std::cos(phi), r*rho*std:: << 2804 } << 2805 else if (select < aInner + aOuter + aPhi) / << 2806 { << 2807 G4double phi = (select < aInner + aOute << 2808 G4double r = std::sqrt((RR - rr)*u + << 2809 G4double theta = fDTheta*v + fSTheta; << 2810 G4double z = std::cos(theta); << 2811 G4double rho = std::sin(theta); << 2812 return { r*rho*std::cos(phi), r*rho*std:: << 2813 } << 2814 else // << 2815 { << 2816 G4double theta = (select < aTotal - aEThe << 2817 G4double r = std::sqrt((RR - rr)*u + << 2818 G4double phi = fDPhi*v + fSPhi; << 2819 G4double z = std::cos(theta); << 2820 G4double rho = std::sin(theta); << 2821 return { r*rho*std::cos(phi), r*rho*std:: << 2822 } << 2823 } << 2824 << 2825 ///////////////////////////////////////////// 2780 ///////////////////////////////////////////////////////////////////////////// 2826 // 2781 // 2827 // Methods for visualisation 2782 // Methods for visualisation 2828 2783 2829 G4VisExtent G4Sphere::GetExtent() const 2784 G4VisExtent G4Sphere::GetExtent() const 2830 { 2785 { 2831 return { -fRmax, fRmax,-fRmax, fRmax,-fRmax << 2786 return G4VisExtent(-fRmax, fRmax,-fRmax, fRmax,-fRmax, fRmax ); 2832 } 2787 } 2833 2788 2834 2789 2835 void G4Sphere::DescribeYourselfTo ( G4VGraphi 2790 void G4Sphere::DescribeYourselfTo ( G4VGraphicsScene& scene ) const 2836 { 2791 { 2837 scene.AddSolid (*this); << 2792 scene.AddThis (*this); 2838 } 2793 } 2839 2794 2840 G4Polyhedron* G4Sphere::CreatePolyhedron () c 2795 G4Polyhedron* G4Sphere::CreatePolyhedron () const 2841 { 2796 { 2842 return new G4PolyhedronSphere (fRmin, fRmax 2797 return new G4PolyhedronSphere (fRmin, fRmax, fSPhi, fDPhi, fSTheta, fDTheta); 2843 } 2798 } 2844 2799 2845 #endif << 2800 G4NURBS* G4Sphere::CreateNURBS () const >> 2801 { >> 2802 return new G4NURBSbox (fRmax, fRmax, fRmax); // Box for now!!! >> 2803 } >> 2804 >> 2805 >> 2806 // ****************************** End of G4Sphere.cc **************************************** 2846 2807