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