Geant4 Cross Reference |
1 // 1 // 2 // ******************************************* 2 // ******************************************************************** 3 // * License and Disclaimer << 3 // * DISCLAIMER * 4 // * 4 // * * 5 // * The Geant4 software is copyright of th << 5 // * The following disclaimer summarizes all the specific disclaimers * 6 // * the Geant4 Collaboration. It is provided << 6 // * of contributors to this software. The specific disclaimers,which * 7 // * conditions of the Geant4 Software License << 7 // * govern, are listed with their locations in: * 8 // * LICENSE and available at http://cern.ch/ << 8 // * http://cern.ch/geant4/license * 9 // * include a list of copyright holders. << 10 // * 9 // * * 11 // * Neither the authors of this software syst 10 // * Neither the authors of this software system, nor their employing * 12 // * institutes,nor the agencies providing fin 11 // * institutes,nor the agencies providing financial support for this * 13 // * work make any representation or warran 12 // * work make any representation or warranty, express or implied, * 14 // * regarding this software system or assum 13 // * regarding this software system or assume any liability for its * 15 // * use. Please see the license in the file << 14 // * use. * 16 // * for the full disclaimer and the limitatio << 17 // * 15 // * * 18 // * This code implementation is the result << 16 // * This code implementation is the intellectual property of the * 19 // * technical work of the GEANT4 collaboratio << 17 // * GEANT4 collaboration. * 20 // * By using, copying, modifying or distri << 18 // * By copying, distributing or modifying the Program (or any work * 21 // * any work based on the software) you ag << 19 // * based on the Program) you indicate your acceptance of this * 22 // * use in resulting scientific publicati << 20 // * statement, and all its terms. * 23 // * acceptance of all terms of the Geant4 Sof << 24 // ******************************************* 21 // ******************************************************************** 25 // 22 // 26 // G4Torus implementation << 27 // 23 // 28 // 30.10.96 V.Grichine: first implementation w << 24 // $Id: G4Torus.cc,v 1.58 2005/11/20 16:35:39 grichine Exp $ 29 // 26.05.00 V.Grichine: added new fuctions dev << 25 // GEANT4 tag $Name: geant4-08-00-patch-01 $ 30 // 31.08.00 E.Medernach: numerical computation << 26 // 31 // 11.01.01 E.Medernach: Use G4PolynomialSolve << 27 // 32 // 03.05.05 V.Grichine: SurfaceNormal(p) accor << 28 // class G4Torus >> 29 // >> 30 // Implementation >> 31 // >> 32 // 20.11.05 V.Grichine: Bug fixed in Inside(p) for phi sections, b.810 33 // 25.08.05 O.Link: new methods for DistanceTo 33 // 25.08.05 O.Link: new methods for DistanceToIn/Out using JTPolynomialSolver 34 // 28.10.16 E.Tcherniaev: new CalculateExtent( << 34 // 07.06.05 V.Grichine: SurfaceNormal(p) for rho=0, Constructor as G4Cons 35 // 16.12.16 H.Burkhardt: use radius difference << 35 // 03.05.05 V.Grichine: SurfaceNormal(p) according to J. Apostolakis proposal 36 // ------------------------------------------- << 36 // 18.03.04 V.Grichine: bug fixed in DistanceToIn(p) >> 37 // 11.01.01 E.Medernach: Use G4PolynomialSolver to find roots >> 38 // 03.10.00 E.Medernach: SafeNewton added >> 39 // 31.08.00 E.Medernach: numerical computation of roots wuth bounding >> 40 // volume technique >> 41 // 26.05.00 V.Grichine: new fuctions developed by O.Cremonesi were added >> 42 // 06.03.00 V.Grichine: modifications in Distance ToOut(p,v,...) >> 43 // 19.11.99 V.Grichine: side = kNull in Distance ToOut(p,v,...) >> 44 // 09.10.98 V.Grichine: modifications in Distance ToOut(p,v,...) >> 45 // 30.10.96 V.Grichine: first implementation with G4Tubs elements in Fs >> 46 // 37 47 38 #include "G4Torus.hh" 48 #include "G4Torus.hh" 39 49 40 #if !(defined(G4GEOM_USE_UTORUS) && defined(G4 << 41 << 42 #include "G4GeomTools.hh" << 43 #include "G4VoxelLimits.hh" 50 #include "G4VoxelLimits.hh" 44 #include "G4AffineTransform.hh" 51 #include "G4AffineTransform.hh" 45 #include "G4BoundingEnvelope.hh" << 46 #include "G4GeometryTolerance.hh" << 47 #include "G4JTPolynomialSolver.hh" 52 #include "G4JTPolynomialSolver.hh" 48 53 49 #include "G4VPVParameterisation.hh" 54 #include "G4VPVParameterisation.hh" 50 55 51 #include "meshdefs.hh" 56 #include "meshdefs.hh" 52 57 53 #include "Randomize.hh" 58 #include "Randomize.hh" 54 59 55 #include "G4VGraphicsScene.hh" 60 #include "G4VGraphicsScene.hh" 56 #include "G4Polyhedron.hh" 61 #include "G4Polyhedron.hh" >> 62 #include "G4NURBS.hh" >> 63 #include "G4NURBStube.hh" >> 64 #include "G4NURBScylinder.hh" >> 65 #include "G4NURBStubesector.hh" 57 66 58 using namespace CLHEP; 67 using namespace CLHEP; 59 68 60 ////////////////////////////////////////////// 69 /////////////////////////////////////////////////////////////// 61 // 70 // 62 // Constructor - check parameters, convert ang 71 // Constructor - check parameters, convert angles so 0<sphi+dpshi<=2_PI 63 // - note if pdphi>2PI then reset 72 // - note if pdphi>2PI then reset to 2PI 64 73 65 G4Torus::G4Torus( const G4String& pName, << 74 G4Torus::G4Torus( const G4String &pName, 66 G4double pRmin, 75 G4double pRmin, 67 G4double pRmax, 76 G4double pRmax, 68 G4double pRtor, 77 G4double pRtor, 69 G4double pSPhi, 78 G4double pSPhi, 70 G4double pDPhi ) << 79 G4double pDPhi) 71 : G4CSGSolid(pName) 80 : G4CSGSolid(pName) 72 { 81 { 73 SetAllParameters(pRmin, pRmax, pRtor, pSPhi, 82 SetAllParameters(pRmin, pRmax, pRtor, pSPhi, pDPhi); 74 } 83 } 75 84 76 ////////////////////////////////////////////// 85 //////////////////////////////////////////////////////////////////////////// 77 // 86 // 78 // 87 // 79 88 80 void 89 void 81 G4Torus::SetAllParameters( G4double pRmin, 90 G4Torus::SetAllParameters( G4double pRmin, 82 G4double pRmax, 91 G4double pRmax, 83 G4double pRtor, 92 G4double pRtor, 84 G4double pSPhi, 93 G4double pSPhi, 85 G4double pDPhi ) 94 G4double pDPhi ) 86 { 95 { 87 const G4double fEpsilon = 4.e-11; // relati << 88 << 89 fCubicVolume = 0.; 96 fCubicVolume = 0.; 90 fSurfaceArea = 0.; << 97 fpPolyhedron = 0; 91 fRebuildPolyhedron = true; << 92 << 93 kRadTolerance = G4GeometryTolerance::GetInst << 94 kAngTolerance = G4GeometryTolerance::GetInst << 95 << 96 halfCarTolerance = 0.5*kCarTolerance; << 97 halfAngTolerance = 0.5*kAngTolerance; << 98 << 99 if ( pRtor >= pRmax+1.e3*kCarTolerance ) // 98 if ( pRtor >= pRmax+1.e3*kCarTolerance ) // Check swept radius, as in G4Cons 100 { 99 { 101 fRtor = pRtor ; 100 fRtor = pRtor ; 102 } 101 } 103 else 102 else 104 { 103 { 105 std::ostringstream message; << 104 G4cerr << "ERROR - G4Torus()::SetAllParameters(): " << GetName() << G4endl 106 message << "Invalid swept radius for Solid << 105 << " Invalid swept radius !" << G4endl 107 << " pRtor = " << pRtor << << 106 << "pRtor = " << pRtor << ", pRmax = " << pRmax << G4endl; 108 G4Exception("G4Torus::SetAllParameters()", 107 G4Exception("G4Torus::SetAllParameters()", 109 "GeomSolids0002", FatalExcepti << 108 "InvalidSetup", FatalException, "Invalid swept radius."); 110 } 109 } 111 110 112 // Check radii, as in G4Cons 111 // Check radii, as in G4Cons 113 // 112 // 114 if ( pRmin < pRmax - 1.e2*kCarTolerance && p 113 if ( pRmin < pRmax - 1.e2*kCarTolerance && pRmin >= 0 ) 115 { 114 { 116 if (pRmin >= 1.e2*kCarTolerance) { fRmin = 115 if (pRmin >= 1.e2*kCarTolerance) { fRmin = pRmin ; } 117 else { fRmin = 116 else { fRmin = 0.0 ; } 118 fRmax = pRmax ; 117 fRmax = pRmax ; 119 } 118 } 120 else 119 else 121 { 120 { 122 std::ostringstream message; << 121 G4cerr << "ERROR - G4Torus()::SetAllParameters(): " << GetName() << G4endl 123 message << "Invalid values of radii for So << 122 << " Invalid values for radii !" << G4endl 124 << " pRmin = " << pRmin << << 123 << " pRmin = " << pRmin << ", pRmax = " << pRmax << G4endl; 125 G4Exception("G4Torus::SetAllParameters()", 124 G4Exception("G4Torus::SetAllParameters()", 126 "GeomSolids0002", FatalExcepti << 125 "InvalidSetup", FatalException, "Invalid radii."); 127 } 126 } 128 127 129 // Relative tolerances << 130 // << 131 fRminTolerance = (fRmin) != 0.0 << 132 ? 0.5*std::max( kRadTolerance << 133 fRmaxTolerance = 0.5*std::max( kRadTolerance << 134 << 135 // Check angles 128 // Check angles 136 // 129 // 137 if ( pDPhi >= twopi ) { fDPhi = twopi ; } 130 if ( pDPhi >= twopi ) { fDPhi = twopi ; } 138 else 131 else 139 { 132 { 140 if (pDPhi > 0) { fDPhi = pDPhi ; } 133 if (pDPhi > 0) { fDPhi = pDPhi ; } 141 else 134 else 142 { 135 { 143 std::ostringstream message; << 136 G4cerr << "ERROR - G4Torus::SetAllParameters(): " << GetName() << G4endl 144 message << "Invalid Z delta-Phi for Soli << 137 << " Negative Z delta-Phi ! - " 145 << " pDPhi = " << pDPhi; << 138 << pDPhi << G4endl; 146 G4Exception("G4Torus::SetAllParameters() 139 G4Exception("G4Torus::SetAllParameters()", 147 "GeomSolids0002", FatalExcep << 140 "InvalidSetup", FatalException, "Invalid dphi."); 148 } 141 } 149 } 142 } 150 143 151 // Ensure psphi in 0-2PI or -2PI-0 range if 144 // Ensure psphi in 0-2PI or -2PI-0 range if shape crosses 0 152 // 145 // 153 fSPhi = pSPhi; 146 fSPhi = pSPhi; 154 147 155 if (fSPhi < 0) { fSPhi = twopi-std::fmod(st 148 if (fSPhi < 0) { fSPhi = twopi-std::fmod(std::fabs(fSPhi),twopi) ; } 156 else { fSPhi = std::fmod(fSPhi,tw 149 else { fSPhi = std::fmod(fSPhi,twopi) ; } 157 150 158 if (fSPhi+fDPhi > twopi) { fSPhi-=twopi ; } 151 if (fSPhi+fDPhi > twopi) { fSPhi-=twopi ; } 159 } 152 } 160 153 161 ////////////////////////////////////////////// 154 /////////////////////////////////////////////////////////////////////// 162 // 155 // 163 // Fake default constructor - sets only member 156 // Fake default constructor - sets only member data and allocates memory 164 // for usage restri 157 // for usage restricted to object persistency. 165 // 158 // 166 G4Torus::G4Torus( __void__& a ) 159 G4Torus::G4Torus( __void__& a ) 167 : G4CSGSolid(a) 160 : G4CSGSolid(a) 168 { 161 { 169 } 162 } 170 163 171 ////////////////////////////////////////////// 164 ////////////////////////////////////////////////////////////////////// 172 // 165 // 173 // Destructor 166 // Destructor 174 167 175 G4Torus::~G4Torus() = default; << 168 G4Torus::~G4Torus() 176 << 169 {} 177 ////////////////////////////////////////////// << 178 // << 179 // Copy constructor << 180 << 181 G4Torus::G4Torus(const G4Torus&) = default; << 182 << 183 ////////////////////////////////////////////// << 184 // << 185 // Assignment operator << 186 << 187 G4Torus& G4Torus::operator = (const G4Torus& r << 188 { << 189 // Check assignment to self << 190 // << 191 if (this == &rhs) { return *this; } << 192 << 193 // Copy base class data << 194 // << 195 G4CSGSolid::operator=(rhs); << 196 << 197 // Copy data << 198 // << 199 fRmin = rhs.fRmin; fRmax = rhs.fRmax; << 200 fRtor = rhs.fRtor; fSPhi = rhs.fSPhi; fDPhi << 201 fRminTolerance = rhs.fRminTolerance; fRmaxT << 202 kRadTolerance = rhs.kRadTolerance; kAngTole << 203 halfCarTolerance = rhs.halfCarTolerance; << 204 halfAngTolerance = rhs.halfAngTolerance; << 205 << 206 return *this; << 207 } << 208 170 209 ////////////////////////////////////////////// 171 ////////////////////////////////////////////////////////////////////// 210 // 172 // 211 // Dispatch to parameterisation for replicatio 173 // Dispatch to parameterisation for replication mechanism dimension 212 // computation & modification. 174 // computation & modification. 213 175 214 void G4Torus::ComputeDimensions( G4VPVPa 176 void G4Torus::ComputeDimensions( G4VPVParameterisation* p, 215 const G4int n 177 const G4int n, 216 const G4VPhys 178 const G4VPhysicalVolume* pRep ) 217 { 179 { 218 p->ComputeDimensions(*this,n,pRep); 180 p->ComputeDimensions(*this,n,pRep); 219 } 181 } 220 182 221 183 222 184 223 ////////////////////////////////////////////// 185 //////////////////////////////////////////////////////////////////////////////// 224 // 186 // 225 // Calculate the real roots to torus surface. 187 // Calculate the real roots to torus surface. 226 // Returns negative solutions as well. 188 // Returns negative solutions as well. 227 189 228 void G4Torus::TorusRootsJT( const G4ThreeVecto << 190 std::vector<G4double> G4Torus::TorusRootsJT( const G4ThreeVector& p, 229 const G4ThreeVecto << 191 const G4ThreeVector& v, 230 G4double r, << 192 G4double r ) const 231 std::vector< << 232 { 193 { 233 194 234 G4int i, num ; 195 G4int i, num ; 235 G4double c[5], srd[4], si[4] ; << 196 G4double c[5], sr[4], si[4] ; >> 197 std::vector<G4double> roots ; 236 198 237 G4double Rtor2 = fRtor*fRtor, r2 = r*r ; 199 G4double Rtor2 = fRtor*fRtor, r2 = r*r ; 238 200 239 G4double pDotV = p.x()*v.x() + p.y()*v.y() + 201 G4double pDotV = p.x()*v.x() + p.y()*v.y() + p.z()*v.z() ; 240 G4double pRad2 = p.x()*p.x() + p.y()*p.y() + 202 G4double pRad2 = p.x()*p.x() + p.y()*p.y() + p.z()*p.z() ; 241 203 242 G4double d=pRad2 - Rtor2; << 243 c[0] = 1.0 ; 204 c[0] = 1.0 ; 244 c[1] = 4*pDotV ; 205 c[1] = 4*pDotV ; 245 c[2] = 2*( (d + 2*pDotV*pDotV - r2) + 2*Rto << 206 c[2] = 2*(pRad2 + 2*pDotV*pDotV - Rtor2 - r2 + 2*Rtor2*v.z()*v.z()) ; 246 c[3] = 4*(pDotV*(d - r2) + 2*Rtor2*p.z()*v.z << 207 c[3] = 4*(pDotV*(pRad2 - Rtor2 - r2) + 2*Rtor2*p.z()*v.z()) ; 247 c[4] = (d-r2)*(d-r2) +4*Rtor2*(p.z()*p.z()-r << 208 c[4] = pRad2*pRad2 - 2*pRad2*(Rtor2+r2) 248 << 209 + 4*Rtor2*p.z()*p.z() + (Rtor2-r2)*(Rtor2-r2) ; >> 210 249 G4JTPolynomialSolver torusEq; 211 G4JTPolynomialSolver torusEq; 250 212 251 num = torusEq.FindRoots( c, 4, srd, si ); << 213 num = torusEq.FindRoots( c, 4, sr, si ); 252 214 253 for ( i = 0; i < num; ++i ) << 215 for ( i = 0; i < num; i++ ) 254 { 216 { 255 if( si[i] == 0. ) { roots.push_back(srd[i << 217 if( si[i] == 0. ) { roots.push_back(sr[i]) ; } // store real roots 256 } 218 } 257 219 258 std::sort(roots.begin() , roots.end() ) ; / << 220 std::sort(roots.begin() , roots.end() ) ; // sorting with < >> 221 >> 222 return roots; 259 } 223 } 260 224 261 ////////////////////////////////////////////// 225 ////////////////////////////////////////////////////////////////////////////// 262 // 226 // 263 // Interface for DistanceToIn and DistanceToOu 227 // Interface for DistanceToIn and DistanceToOut. 264 // Calls TorusRootsJT and returns the smalles 228 // Calls TorusRootsJT and returns the smalles possible distance to 265 // the surface. 229 // the surface. 266 // Attention: Difference in DistanceToIn/Out f 230 // Attention: Difference in DistanceToIn/Out for points p on the surface. 267 231 268 G4double G4Torus::SolveNumericJT( const G4Thre 232 G4double G4Torus::SolveNumericJT( const G4ThreeVector& p, 269 const G4Thre 233 const G4ThreeVector& v, 270 G4doub 234 G4double r, 271 G4bool 235 G4bool IsDistanceToIn ) const 272 { 236 { 273 G4double bigdist = 10*mm ; 237 G4double bigdist = 10*mm ; 274 G4double tmin = kInfinity ; 238 G4double tmin = kInfinity ; 275 G4double t, scal ; 239 G4double t, scal ; 276 240 277 // calculate the distances to the intersecti 241 // calculate the distances to the intersections with the Torus 278 // from a given point p and direction v. 242 // from a given point p and direction v. 279 // 243 // 280 std::vector<G4double> roots ; 244 std::vector<G4double> roots ; 281 std::vector<G4double> rootsrefined ; 245 std::vector<G4double> rootsrefined ; 282 TorusRootsJT(p,v,r,roots) ; << 246 roots = TorusRootsJT(p,v,r) ; 283 247 284 G4ThreeVector ptmp ; 248 G4ThreeVector ptmp ; 285 249 286 // determine the smallest non-negative solut 250 // determine the smallest non-negative solution 287 // 251 // 288 for ( std::size_t k = 0 ; k<roots.size() ; + << 252 for ( size_t k = 0 ; k<roots.size() ; k++ ) 289 { 253 { 290 t = roots[k] ; 254 t = roots[k] ; 291 255 292 if ( t < -halfCarTolerance ) { continue ; << 256 if ( t < -0.5*kCarTolerance ) { continue ; } // skip negative roots 293 257 294 if ( t > bigdist && t<kInfinity ) // pr 258 if ( t > bigdist && t<kInfinity ) // problem with big distances 295 { 259 { 296 ptmp = p + t*v ; 260 ptmp = p + t*v ; 297 TorusRootsJT(ptmp,v,r,rootsrefined) ; << 261 rootsrefined = TorusRootsJT(ptmp,v,r) ; 298 if ( rootsrefined.size()==roots.size() ) << 262 t = t + rootsrefined[k] ; 299 { << 300 t = t + rootsrefined[k] ; << 301 } << 302 } 263 } 303 264 304 ptmp = p + t*v ; // calculate the positi 265 ptmp = p + t*v ; // calculate the position of the proposed intersection 305 266 306 G4double theta = std::atan2(ptmp.y(),ptmp. 267 G4double theta = std::atan2(ptmp.y(),ptmp.x()); >> 268 >> 269 if (theta < 0) { theta += twopi; } 307 270 308 if ( fSPhi >= 0 ) << 309 { << 310 if ( theta < - halfAngTolerance ) { the << 311 if ( (std::fabs(theta) < halfAngToleranc << 312 && (std::fabs(fSPhi + fDPhi - twopi) < << 313 { << 314 theta += twopi ; // 0 <= theta < 2pi << 315 } << 316 } << 317 if ((fSPhi <= -pi )&&(theta>halfAngToleran << 318 << 319 // We have to verify if this root is insid 271 // We have to verify if this root is inside the region between 320 // fSPhi and fSPhi + fDPhi 272 // fSPhi and fSPhi + fDPhi 321 // 273 // 322 if ( (theta - fSPhi >= - halfAngTolerance) << 274 if ( (theta - fSPhi >= - kAngTolerance*0.5) 323 && (theta - (fSPhi + fDPhi) <= halfAngT << 275 && (theta - (fSPhi + fDPhi) <= kAngTolerance*0.5) ) 324 { 276 { 325 // check if P is on the surface, and cal 277 // check if P is on the surface, and called from DistanceToIn 326 // DistanceToIn has to return 0.0 if par 278 // DistanceToIn has to return 0.0 if particle is going inside the solid 327 279 328 if ( IsDistanceToIn ) << 280 if ( IsDistanceToIn == true ) 329 { 281 { 330 if (std::fabs(t) < halfCarTolerance ) << 282 if (std::fabs(t) < 0.5*kCarTolerance ) 331 { 283 { 332 // compute scalar product at positio 284 // compute scalar product at position p : v.n 333 // ( n taken from SurfaceNormal, not 285 // ( n taken from SurfaceNormal, not normalized ) 334 286 335 scal = v* G4ThreeVector( p.x()*(1-fR << 287 scal = v* G4ThreeVector( p.x()*(1-fRtor/std::sqrt(p.x()*p.x() 336 p.y()*(1-fR << 288 + p.y()*p.y())), >> 289 p.y()*(1-fRtor/std::sqrt(p.x()*p.x() >> 290 + p.y()*p.y())), 337 p.z() ); 291 p.z() ); 338 292 339 // change sign in case of inner radi 293 // change sign in case of inner radius 340 // 294 // 341 if ( r == GetRmin() ) { scal = -sca 295 if ( r == GetRmin() ) { scal = -scal ; } 342 if ( scal < 0 ) { return 0.0 ; } 296 if ( scal < 0 ) { return 0.0 ; } 343 } 297 } 344 } 298 } 345 299 346 // check if P is on the surface, and cal 300 // check if P is on the surface, and called from DistanceToOut 347 // DistanceToIn has to return 0.0 if par 301 // DistanceToIn has to return 0.0 if particle is leaving the solid 348 302 349 if ( !IsDistanceToIn ) << 303 if ( IsDistanceToIn == false ) 350 { 304 { 351 if (std::fabs(t) < halfCarTolerance ) << 305 if (std::fabs(t) < 0.5*kCarTolerance ) 352 { 306 { 353 // compute scalar product at positio 307 // compute scalar product at position p : v.n 354 // 308 // 355 scal = v* G4ThreeVector( p.x()*(1-fR << 309 scal = v* G4ThreeVector( p.x()*(1-fRtor/std::sqrt(p.x()*p.x() 356 p.y()*(1-fR << 310 + p.y()*p.y())), >> 311 p.y()*(1-fRtor/std::sqrt(p.x()*p.x() >> 312 + p.y()*p.y())), 357 p.z() ); 313 p.z() ); 358 314 359 // change sign in case of inner radi 315 // change sign in case of inner radius 360 // 316 // 361 if ( r == GetRmin() ) { scal = -sca 317 if ( r == GetRmin() ) { scal = -scal ; } 362 if ( scal > 0 ) { return 0.0 ; } 318 if ( scal > 0 ) { return 0.0 ; } 363 } 319 } 364 } 320 } 365 321 366 // check if distance is larger than 1/2 322 // check if distance is larger than 1/2 kCarTolerance 367 // 323 // 368 if( t > halfCarTolerance ) << 324 if( t > 0.5*kCarTolerance ) 369 { 325 { 370 tmin = t ; 326 tmin = t ; 371 return tmin ; 327 return tmin ; 372 } 328 } 373 } 329 } 374 } 330 } 375 331 376 return tmin; 332 return tmin; 377 } 333 } 378 334 379 ////////////////////////////////////////////// 335 ///////////////////////////////////////////////////////////////////////////// 380 // 336 // 381 // Get bounding box << 382 << 383 void G4Torus::BoundingLimits(G4ThreeVector& pM << 384 { << 385 G4double rmax = GetRmax(); << 386 G4double rtor = GetRtor(); << 387 G4double rint = rtor - rmax; << 388 G4double rext = rtor + rmax; << 389 G4double dz = rmax; << 390 << 391 // Find bounding box << 392 // << 393 if (GetDPhi() >= twopi) << 394 { << 395 pMin.set(-rext,-rext,-dz); << 396 pMax.set( rext, rext, dz); << 397 } << 398 else << 399 { << 400 G4TwoVector vmin,vmax; << 401 G4GeomTools::DiskExtent(rint,rext, << 402 GetSinStartPhi(),G << 403 GetSinEndPhi(),Get << 404 vmin,vmax); << 405 pMin.set(vmin.x(),vmin.y(),-dz); << 406 pMax.set(vmax.x(),vmax.y(), dz); << 407 } << 408 << 409 // Check correctness of the bounding box << 410 // << 411 if (pMin.x() >= pMax.x() || pMin.y() >= pMax << 412 { << 413 std::ostringstream message; << 414 message << "Bad bounding box (min >= max) << 415 << GetName() << " !" << 416 << "\npMin = " << pMin << 417 << "\npMax = " << pMax; << 418 G4Exception("G4Torus::BoundingLimits()", " << 419 JustWarning, message); << 420 DumpInfo(); << 421 } << 422 } << 423 << 424 ////////////////////////////////////////////// << 425 // << 426 // Calculate extent under transform and specif 337 // Calculate extent under transform and specified limit 427 338 428 G4bool G4Torus::CalculateExtent( const EAxis p 339 G4bool G4Torus::CalculateExtent( const EAxis pAxis, 429 const G4Voxel 340 const G4VoxelLimits& pVoxelLimit, 430 const G4Affin 341 const G4AffineTransform& pTransform, 431 G4doubl 342 G4double& pMin, G4double& pMax) const 432 { 343 { 433 G4ThreeVector bmin, bmax; << 344 if ((!pTransform.IsRotated()) && (fDPhi==twopi) && (fRmin==0)) 434 G4bool exist; << 345 { >> 346 // Special case handling for unrotated solid torus >> 347 // Compute x/y/z mins and maxs for bounding box respecting limits, >> 348 // with early returns if outside limits. Then switch() on pAxis, >> 349 // and compute exact x and y limit for x/y case >> 350 >> 351 G4double xoffset,xMin,xMax; >> 352 G4double yoffset,yMin,yMax; >> 353 G4double zoffset,zMin,zMax; >> 354 >> 355 G4double diff1,diff2,maxDiff,newMin,newMax; >> 356 G4double xoff1,xoff2,yoff1,yoff2; >> 357 >> 358 xoffset = pTransform.NetTranslation().x(); >> 359 xMin = xoffset - fRmax - fRtor ; >> 360 xMax = xoffset + fRmax + fRtor ; >> 361 >> 362 if (pVoxelLimit.IsXLimited()) >> 363 { >> 364 if ( (xMin > pVoxelLimit.GetMaxXExtent()+kCarTolerance) >> 365 || (xMax < pVoxelLimit.GetMinXExtent()-kCarTolerance) ) >> 366 return false ; >> 367 else >> 368 { >> 369 if (xMin < pVoxelLimit.GetMinXExtent()) >> 370 { >> 371 xMin = pVoxelLimit.GetMinXExtent() ; >> 372 } >> 373 if (xMax > pVoxelLimit.GetMaxXExtent()) >> 374 { >> 375 xMax = pVoxelLimit.GetMaxXExtent() ; >> 376 } >> 377 } >> 378 } >> 379 yoffset = pTransform.NetTranslation().y(); >> 380 yMin = yoffset - fRmax - fRtor ; >> 381 yMax = yoffset + fRmax + fRtor ; 435 382 436 // Get bounding box << 383 if (pVoxelLimit.IsYLimited()) 437 BoundingLimits(bmin,bmax); << 384 { >> 385 if ( (yMin > pVoxelLimit.GetMaxYExtent()+kCarTolerance) >> 386 || (yMax < pVoxelLimit.GetMinYExtent()-kCarTolerance) ) >> 387 { >> 388 return false ; >> 389 } >> 390 else >> 391 { >> 392 if (yMin < pVoxelLimit.GetMinYExtent() ) >> 393 { >> 394 yMin = pVoxelLimit.GetMinYExtent() ; >> 395 } >> 396 if (yMax > pVoxelLimit.GetMaxYExtent() ) >> 397 { >> 398 yMax = pVoxelLimit.GetMaxYExtent() ; >> 399 } >> 400 } >> 401 } >> 402 zoffset = pTransform.NetTranslation().z() ; >> 403 zMin = zoffset - fRmax ; >> 404 zMax = zoffset + fRmax ; 438 405 439 // Check bounding box << 406 if (pVoxelLimit.IsZLimited()) 440 G4BoundingEnvelope bbox(bmin,bmax); << 407 { 441 #ifdef G4BBOX_EXTENT << 408 if ( (zMin > pVoxelLimit.GetMaxZExtent()+kCarTolerance) 442 return bbox.CalculateExtent(pAxis,pVoxelLimi << 409 || (zMax < pVoxelLimit.GetMinZExtent()-kCarTolerance) ) 443 #endif << 410 { 444 if (bbox.BoundingBoxVsVoxelLimits(pAxis,pVox << 411 return false ; 445 { << 412 } 446 return exist = pMin < pMax; << 413 else >> 414 { >> 415 if (zMin < pVoxelLimit.GetMinZExtent() ) >> 416 { >> 417 zMin = pVoxelLimit.GetMinZExtent() ; >> 418 } >> 419 if (zMax > pVoxelLimit.GetMaxZExtent() ) >> 420 { >> 421 zMax = pVoxelLimit.GetMaxZExtent() ; >> 422 } >> 423 } >> 424 } >> 425 >> 426 // Known to cut cylinder >> 427 >> 428 switch (pAxis) >> 429 { >> 430 case kXAxis: >> 431 yoff1=yoffset-yMin; >> 432 yoff2=yMax-yoffset; >> 433 if ( yoff1 >= 0 && yoff2 >= 0 ) >> 434 { >> 435 // Y limits cross max/min x => no change >> 436 // >> 437 pMin = xMin ; >> 438 pMax = xMax ; >> 439 } >> 440 else >> 441 { >> 442 // Y limits don't cross max/min x => compute max delta x, >> 443 // hence new mins/maxs >> 444 // >> 445 diff1 = std::sqrt(fRmax*fRmax - yoff1*yoff1) ; >> 446 diff2 = std::sqrt(fRmax*fRmax - yoff2*yoff2) ; >> 447 maxDiff = (diff1 > diff2) ? diff1:diff2 ; >> 448 newMin = xoffset - maxDiff ; >> 449 newMax = xoffset + maxDiff ; >> 450 pMin = (newMin < xMin) ? xMin : newMin ; >> 451 pMax = (newMax > xMax) ? xMax : newMax ; >> 452 } >> 453 break; >> 454 >> 455 case kYAxis: >> 456 xoff1 = xoffset - xMin ; >> 457 xoff2 = xMax - xoffset ; >> 458 if (xoff1 >= 0 && xoff2 >= 0 ) >> 459 { >> 460 // X limits cross max/min y => no change >> 461 // >> 462 pMin = yMin ; >> 463 pMax = yMax ; >> 464 } >> 465 else >> 466 { >> 467 // X limits don't cross max/min y => compute max delta y, >> 468 // hence new mins/maxs >> 469 // >> 470 diff1 = std::sqrt(fRmax*fRmax - xoff1*xoff1) ; >> 471 diff2 = std::sqrt(fRmax*fRmax - xoff2*xoff2) ; >> 472 maxDiff = (diff1 > diff2) ? diff1 : diff2 ; >> 473 newMin = yoffset - maxDiff ; >> 474 newMax = yoffset + maxDiff ; >> 475 pMin = (newMin < yMin) ? yMin : newMin ; >> 476 pMax = (newMax > yMax) ? yMax : newMax ; >> 477 } >> 478 break; >> 479 >> 480 case kZAxis: >> 481 pMin=zMin; >> 482 pMax=zMax; >> 483 break; >> 484 >> 485 default: >> 486 break; >> 487 } >> 488 pMin -= kCarTolerance ; >> 489 pMax += kCarTolerance ; >> 490 >> 491 return true; 447 } 492 } >> 493 else >> 494 { >> 495 G4int i, noEntries, noBetweenSections4 ; >> 496 G4bool existsAfterClip = false ; 448 497 449 // Get parameters of the solid << 498 // Calculate rotated vertex coordinates 450 G4double rmin = GetRmin(); << 451 G4double rmax = GetRmax(); << 452 G4double rtor = GetRtor(); << 453 G4double dphi = GetDPhi(); << 454 G4double sinStart = GetSinStartPhi(); << 455 G4double cosStart = GetCosStartPhi(); << 456 G4double sinEnd = GetSinEndPhi(); << 457 G4double cosEnd = GetCosEndPhi(); << 458 G4double rint = rtor - rmax; << 459 G4double rext = rtor + rmax; << 460 499 461 // Find bounding envelope and calculate exte << 500 G4ThreeVectorList *vertices ; 462 // << 501 G4int noPolygonVertices ; // will be 4 463 static const G4int NPHI = 24; // number of << 502 vertices = CreateRotatedVertices(pTransform,noPolygonVertices) ; 464 static const G4int NDISK = 16; // number of << 503 465 static const G4double sinHalfDisk = std::sin << 504 pMin = +kInfinity ; 466 static const G4double cosHalfDisk = std::cos << 505 pMax = -kInfinity ; 467 static const G4double sinStepDisk = 2.*sinHa << 506 468 static const G4double cosStepDisk = 1. - 2.* << 507 noEntries = vertices->size() ; 469 << 508 noBetweenSections4 = noEntries - noPolygonVertices ; 470 G4double astep = (360/NPHI)*deg; // max angl << 509 471 G4int kphi = (dphi <= astep) ? 1 : (G4in << 510 for (i=0;i<noEntries;i+=noPolygonVertices) 472 G4double ang = dphi/kphi; << 511 { 473 << 512 ClipCrossSection(vertices,i,pVoxelLimit,pAxis,pMin,pMax); 474 G4double sinHalf = std::sin(0.5*ang); << 513 } 475 G4double cosHalf = std::cos(0.5*ang); << 514 for (i=0;i<noBetweenSections4;i+=noPolygonVertices) 476 G4double sinStep = 2.*sinHalf*cosHalf; << 515 { 477 G4double cosStep = 1. - 2.*sinHalf*sinHalf; << 516 ClipBetweenSections(vertices,i,pVoxelLimit,pAxis,pMin,pMax); 478 << 517 } 479 // define vectors for bounding envelope << 518 if (pMin!=kInfinity||pMax!=-kInfinity) 480 G4ThreeVectorList pols[NDISK+1]; << 519 { 481 for (auto & pol : pols) pol.resize(4); << 520 existsAfterClip = true ; // Add 2*tolerance to avoid precision troubles 482 << 521 pMin -= kCarTolerance ; 483 std::vector<const G4ThreeVectorList *> polyg << 522 pMax += kCarTolerance ; 484 polygons.resize(NDISK+1); << 485 for (G4int k=0; k<NDISK+1; ++k) polygons[k] << 486 << 487 // set internal and external reference circl << 488 G4TwoVector rzmin[NDISK]; << 489 G4TwoVector rzmax[NDISK]; << 490 << 491 if ((rtor-rmin*sinHalfDisk)/cosHalf > (rtor+ << 492 rmax /= cosHalfDisk; << 493 G4double sinCurDisk = sinHalfDisk; << 494 G4double cosCurDisk = cosHalfDisk; << 495 for (G4int k=0; k<NDISK; ++k) << 496 { << 497 G4double rmincur = rtor + rmin*cosCurDisk; << 498 if (cosCurDisk < 0 && rmin > 0) rmincur /= << 499 rzmin[k].set(rmincur,rmin*sinCurDisk); << 500 << 501 G4double rmaxcur = rtor + rmax*cosCurDisk; << 502 if (cosCurDisk > 0) rmaxcur /= cosHalf; << 503 rzmax[k].set(rmaxcur,rmax*sinCurDisk); << 504 << 505 G4double sinTmpDisk = sinCurDisk; << 506 sinCurDisk = sinCurDisk*cosStepDisk + cosC << 507 cosCurDisk = cosCurDisk*cosStepDisk - sinT << 508 } << 509 << 510 // Loop along slices in Phi. The extent is c << 511 // extent of the slices << 512 pMin = kInfinity; << 513 pMax = -kInfinity; << 514 G4double eminlim = pVoxelLimit.GetMinExtent( << 515 G4double emaxlim = pVoxelLimit.GetMaxExtent( << 516 G4double sinCur1 = 0, cosCur1 = 0, sinCur2 = << 517 for (G4int i=0; i<kphi+1; ++i) << 518 { << 519 if (i == 0) << 520 { << 521 sinCur1 = sinStart; << 522 cosCur1 = cosStart; << 523 sinCur2 = sinCur1*cosHalf + cosCur1*sinH << 524 cosCur2 = cosCur1*cosHalf - sinCur1*sinH << 525 } 523 } 526 else 524 else 527 { 525 { 528 sinCur1 = sinCur2; << 526 // Check for case where completely enveloping clipping volume 529 cosCur1 = cosCur2; << 527 // If point inside then we are confident that the solid completely 530 sinCur2 = (i == kphi) ? sinEnd : sinCur1 << 528 // envelopes the clipping volume. Hence set min/max extents according 531 cosCur2 = (i == kphi) ? cosEnd : cosCur1 << 529 // to clipping volume extents along the specified axis. 532 } << 530 533 for (G4int k=0; k<NDISK; ++k) << 531 G4ThreeVector clipCentre( 534 { << 532 (pVoxelLimit.GetMinXExtent()+pVoxelLimit.GetMaxXExtent())*0.5, 535 G4double r1 = rzmin[k].x(), r2 = rzmax[k << 533 (pVoxelLimit.GetMinYExtent()+pVoxelLimit.GetMaxYExtent())*0.5, 536 G4double z1 = rzmin[k].y(), z2 = rzmax[k << 534 (pVoxelLimit.GetMinZExtent()+pVoxelLimit.GetMaxZExtent())*0.5 ) ; 537 pols[k][0].set(r1*cosCur1,r1*sinCur1,z1) << 535 538 pols[k][1].set(r2*cosCur1,r2*sinCur1,z2) << 536 if (Inside(pTransform.Inverse().TransformPoint(clipCentre)) != kOutside ) 539 pols[k][2].set(r2*cosCur2,r2*sinCur2,z2) << 537 { 540 pols[k][3].set(r1*cosCur2,r1*sinCur2,z1) << 538 existsAfterClip = true ; 541 } << 539 pMin = pVoxelLimit.GetMinExtent(pAxis) ; 542 pols[NDISK] = pols[0]; << 540 pMax = pVoxelLimit.GetMaxExtent(pAxis) ; 543 << 541 } 544 // get bounding box of current slice << 542 } 545 G4TwoVector vmin,vmax; << 543 delete vertices; 546 G4GeomTools:: << 544 return existsAfterClip; 547 DiskExtent(rint,rext,sinCur1,cosCur1,sin << 548 bmin.setX(vmin.x()); bmin.setY(vmin.y()); << 549 bmax.setX(vmax.x()); bmax.setY(vmax.y()); << 550 << 551 // set bounding envelope for current slice << 552 G4double emin,emax; << 553 G4BoundingEnvelope benv(bmin,bmax,polygons << 554 if (!benv.CalculateExtent(pAxis,pVoxelLimi << 555 if (emin < pMin) pMin = emin; << 556 if (emax > pMax) pMax = emax; << 557 if (eminlim > pMin && emaxlim < pMax) brea << 558 } 545 } 559 return (pMin < pMax); << 560 } 546 } 561 547 562 ////////////////////////////////////////////// 548 ////////////////////////////////////////////////////////////////////////////// 563 // 549 // 564 // Return whether point inside/outside/on surf 550 // Return whether point inside/outside/on surface 565 551 566 EInside G4Torus::Inside( const G4ThreeVector& 552 EInside G4Torus::Inside( const G4ThreeVector& p ) const 567 { 553 { 568 G4double r, pt2, pPhi, tolRMin, tolRMax ; << 554 G4double r2, pt2, pPhi, tolRMin, tolRMax ; 569 555 570 EInside in = kOutside ; 556 EInside in = kOutside ; >> 557 // General precals >> 558 r2 = p.x()*p.x() + p.y()*p.y() ; >> 559 pt2 = r2 + p.z()*p.z() + fRtor*fRtor - 2*fRtor*std::sqrt(r2) ; 571 560 572 // General precals << 561 if (fRmin) tolRMin = fRmin + kRadTolerance*0.5 ; 573 // << 574 r = std::hypot(p.x(),p.y()); << 575 pt2 = p.z()*p.z() + (r-fRtor)*(r-fRtor); << 576 << 577 if (fRmin != 0.0) tolRMin = fRmin + fRminTol << 578 else tolRMin = 0 ; 562 else tolRMin = 0 ; 579 563 580 tolRMax = fRmax - fRmaxTolerance; << 564 tolRMax = fRmax - kRadTolerance*0.5; 581 565 582 if (pt2 >= tolRMin*tolRMin && pt2 <= tolRMax 566 if (pt2 >= tolRMin*tolRMin && pt2 <= tolRMax*tolRMax ) 583 { 567 { 584 if ( fDPhi == twopi || pt2 == 0 ) // on t 568 if ( fDPhi == twopi || pt2 == 0 ) // on torus swept axis 585 { 569 { 586 in = kInside ; 570 in = kInside ; 587 } 571 } 588 else 572 else 589 { 573 { 590 // Try inner tolerant phi boundaries (=> 574 // Try inner tolerant phi boundaries (=>inside) 591 // if not inside, try outer tolerant phi 575 // if not inside, try outer tolerant phi boundaries 592 576 593 pPhi = std::atan2(p.y(),p.x()) ; 577 pPhi = std::atan2(p.y(),p.x()) ; 594 578 595 if ( pPhi < -halfAngTolerance ) { pPhi << 579 if ( pPhi < -kAngTolerance*0.5 ) { pPhi += twopi ; } // 0<=pPhi<2pi 596 if ( fSPhi >= 0 ) 580 if ( fSPhi >= 0 ) 597 { 581 { 598 if ( (std::fabs(pPhi) < halfAngToleran << 582 if ( (std::abs(pPhi) < kAngTolerance*0.5) 599 && (std::fabs(fSPhi + fDPhi - twop << 583 && (std::abs(fSPhi + fDPhi - twopi) < kAngTolerance*0.5) ) 600 { 584 { 601 pPhi += twopi ; // 0 <= pPhi < 2pi 585 pPhi += twopi ; // 0 <= pPhi < 2pi 602 } 586 } 603 if ( (pPhi >= fSPhi + halfAngTolerance << 587 if ( (pPhi >= fSPhi + kAngTolerance*0.5) 604 && (pPhi <= fSPhi + fDPhi - halfAn << 588 && (pPhi <= fSPhi + fDPhi - kAngTolerance*0.5) ) 605 { 589 { 606 in = kInside ; 590 in = kInside ; 607 } 591 } 608 else if ( (pPhi >= fSPhi - halfAngTo << 592 else if ( (pPhi >= fSPhi - kAngTolerance*0.5) 609 && (pPhi <= fSPhi + fDPhi + h << 593 && (pPhi <= fSPhi + fDPhi + kAngTolerance*0.5) ) 610 { 594 { 611 in = kSurface ; 595 in = kSurface ; 612 } 596 } 613 } 597 } 614 else // fSPhi < 0 598 else // fSPhi < 0 615 { 599 { 616 if ( (pPhi <= fSPhi + twopi - halfAn << 600 if ( (pPhi <= fSPhi + twopi - kAngTolerance*0.5) 617 && (pPhi >= fSPhi + fDPhi + halfA << 601 && (pPhi >= fSPhi + fDPhi + kAngTolerance*0.5) ) {;} 618 else 602 else 619 { 603 { 620 in = kSurface ; 604 in = kSurface ; 621 } 605 } 622 } 606 } 623 } 607 } 624 } 608 } 625 else // Try generous boundaries 609 else // Try generous boundaries 626 { 610 { 627 tolRMin = fRmin - fRminTolerance ; << 611 tolRMin = fRmin - kRadTolerance*0.5 ; 628 tolRMax = fRmax + fRmaxTolerance ; << 612 tolRMax = fRmax + kRadTolerance*0.5 ; 629 613 630 if (tolRMin < 0 ) { tolRMin = 0 ; } 614 if (tolRMin < 0 ) { tolRMin = 0 ; } 631 615 632 if ( (pt2 >= tolRMin*tolRMin) && (pt2 <= t 616 if ( (pt2 >= tolRMin*tolRMin) && (pt2 <= tolRMax*tolRMax) ) 633 { 617 { 634 if ( (fDPhi == twopi) || (pt2 == 0) ) // 618 if ( (fDPhi == twopi) || (pt2 == 0) ) // Continuous in phi or on z-axis 635 { 619 { 636 in = kSurface ; 620 in = kSurface ; 637 } 621 } 638 else // Try outer tolerant phi boundarie 622 else // Try outer tolerant phi boundaries only 639 { 623 { 640 pPhi = std::atan2(p.y(),p.x()) ; 624 pPhi = std::atan2(p.y(),p.x()) ; 641 625 642 if ( pPhi < -halfAngTolerance ) { pPh << 626 if ( pPhi < -kAngTolerance*0.5 ) { pPhi += twopi ; } // 0<=pPhi<2pi 643 if ( fSPhi >= 0 ) 627 if ( fSPhi >= 0 ) 644 { 628 { 645 if ( (std::fabs(pPhi) < halfAngToler << 629 if ( (std::abs(pPhi) < kAngTolerance*0.5) 646 && (std::fabs(fSPhi + fDPhi - twop << 630 && (std::abs(fSPhi + fDPhi - twopi) < kAngTolerance*0.5) ) 647 { 631 { 648 pPhi += twopi ; // 0 <= pPhi < 2pi 632 pPhi += twopi ; // 0 <= pPhi < 2pi 649 } 633 } 650 if ( (pPhi >= fSPhi - halfAngToleran << 634 if ( (pPhi >= fSPhi - kAngTolerance*0.5) 651 && (pPhi <= fSPhi + fDPhi + halfAn << 635 && (pPhi <= fSPhi + fDPhi + kAngTolerance*0.5) ) 652 { 636 { 653 in = kSurface; 637 in = kSurface; 654 } 638 } 655 } 639 } 656 else // fSPhi < 0 640 else // fSPhi < 0 657 { 641 { 658 if ( (pPhi <= fSPhi + twopi - halfAn << 642 if ( (pPhi <= fSPhi + twopi - kAngTolerance*0.5) 659 && (pPhi >= fSPhi + fDPhi + halfA << 643 && (pPhi >= fSPhi + fDPhi + kAngTolerance*0.5) ) {;} 660 else 644 else 661 { 645 { 662 in = kSurface ; 646 in = kSurface ; 663 } 647 } 664 } 648 } 665 } 649 } 666 } 650 } 667 } 651 } 668 return in ; 652 return in ; 669 } 653 } 670 654 671 ////////////////////////////////////////////// 655 ///////////////////////////////////////////////////////////////////////////// 672 // 656 // 673 // Return unit normal of surface closest to p 657 // Return unit normal of surface closest to p 674 // - note if point on z axis, ignore phi divid 658 // - note if point on z axis, ignore phi divided sides 675 // - unsafe if point close to z axis a rmin=0 659 // - unsafe if point close to z axis a rmin=0 - no explicit checks 676 660 677 G4ThreeVector G4Torus::SurfaceNormal( const G4 661 G4ThreeVector G4Torus::SurfaceNormal( const G4ThreeVector& p ) const 678 { 662 { 679 G4int noSurfaces = 0; 663 G4int noSurfaces = 0; 680 G4double rho, pt, pPhi; << 664 G4double rho2, rho, pt2, pt, pPhi; 681 G4double distRMin = kInfinity; 665 G4double distRMin = kInfinity; 682 G4double distSPhi = kInfinity, distEPhi = kI 666 G4double distSPhi = kInfinity, distEPhi = kInfinity; 683 << 667 G4double delta = 0.5*kCarTolerance, dAngle = 0.5*kAngTolerance; 684 // To cope with precision loss << 685 // << 686 const G4double delta = std::max(10.0*kCarTol << 687 1.0e-8*(fRto << 688 const G4double dAngle = 10.0*kAngTolerance; << 689 << 690 G4ThreeVector nR, nPs, nPe; 668 G4ThreeVector nR, nPs, nPe; 691 G4ThreeVector norm, sumnorm(0.,0.,0.); 669 G4ThreeVector norm, sumnorm(0.,0.,0.); 692 670 693 rho = std::hypot(p.x(),p.y()); << 671 rho2 = p.x()*p.x() + p.y()*p.y(); 694 pt = std::hypot(p.z(),rho-fRtor); << 672 rho = std::sqrt(rho2); >> 673 pt2 = std::fabs(rho2+p.z()*p.z() +fRtor*fRtor - 2*fRtor*rho); >> 674 pt = std::sqrt(pt2) ; 695 675 696 G4double distRMax = std::fabs(pt - fRmax); 676 G4double distRMax = std::fabs(pt - fRmax); 697 if(fRmin != 0.0) distRMin = std::fabs(pt - f << 677 if(fRmin) distRMin = std::fabs(pt - fRmin); 698 678 699 if( rho > delta && pt != 0.0 ) << 679 if( rho > delta ) 700 { 680 { 701 G4double redFactor= (rho-fRtor)/rho; << 681 nR = G4ThreeVector( p.x()*(1-fRtor/rho)/pt, 702 nR = G4ThreeVector( p.x()*redFactor, // p << 682 p.y()*(1-fRtor/rho)/pt, 703 p.y()*redFactor, // p << 683 p.z()/pt ); 704 p.z() ); << 705 nR *= 1.0/pt; << 706 } 684 } 707 685 708 if ( fDPhi < twopi ) // && rho ) // old limi 686 if ( fDPhi < twopi ) // && rho ) // old limitation against (0,0,z) 709 { 687 { 710 if ( rho != 0.0 ) << 688 if ( rho ) 711 { 689 { 712 pPhi = std::atan2(p.y(),p.x()); 690 pPhi = std::atan2(p.y(),p.x()); 713 691 714 if(pPhi < fSPhi-delta) { pPhi 692 if(pPhi < fSPhi-delta) { pPhi += twopi; } 715 else if(pPhi > fSPhi+fDPhi+delta) { pPhi 693 else if(pPhi > fSPhi+fDPhi+delta) { pPhi -= twopi; } 716 694 717 distSPhi = std::fabs( pPhi - fSPhi ); 695 distSPhi = std::fabs( pPhi - fSPhi ); 718 distEPhi = std::fabs(pPhi-fSPhi-fDPhi); 696 distEPhi = std::fabs(pPhi-fSPhi-fDPhi); 719 } 697 } 720 nPs = G4ThreeVector(std::sin(fSPhi),-std:: 698 nPs = G4ThreeVector(std::sin(fSPhi),-std::cos(fSPhi),0); 721 nPe = G4ThreeVector(-std::sin(fSPhi+fDPhi) 699 nPe = G4ThreeVector(-std::sin(fSPhi+fDPhi),std::cos(fSPhi+fDPhi),0); 722 } 700 } 723 if( distRMax <= delta ) 701 if( distRMax <= delta ) 724 { 702 { 725 ++noSurfaces; << 703 noSurfaces ++; 726 sumnorm += nR; 704 sumnorm += nR; 727 } 705 } 728 else if( (fRmin != 0.0) && (distRMin <= delt << 706 if( fRmin && distRMin <= delta ) 729 { 707 { 730 ++noSurfaces; << 708 noSurfaces ++; 731 sumnorm -= nR; 709 sumnorm -= nR; 732 } 710 } 733 << 711 if( fDPhi < twopi ) 734 // To be on one of the 'phi' surfaces, << 735 // it must be within the 'tube' - with tole << 736 << 737 if( (fDPhi < twopi) && (fRmin-delta <= pt) & << 738 { 712 { 739 if (distSPhi <= dAngle) 713 if (distSPhi <= dAngle) 740 { 714 { 741 ++noSurfaces; << 715 noSurfaces ++; 742 sumnorm += nPs; 716 sumnorm += nPs; 743 } 717 } 744 if (distEPhi <= dAngle) 718 if (distEPhi <= dAngle) 745 { 719 { 746 ++noSurfaces; << 720 noSurfaces ++; 747 sumnorm += nPe; 721 sumnorm += nPe; 748 } 722 } 749 } 723 } 750 if ( noSurfaces == 0 ) 724 if ( noSurfaces == 0 ) 751 { 725 { 752 #ifdef G4CSGDEBUG 726 #ifdef G4CSGDEBUG 753 G4ExceptionDescription ed; << 727 G4Exception("G4Torus::SurfaceNormal(p)", "Notification", JustWarning, 754 ed.precision(16); << 728 "Point p is not on surface !?" ); 755 << 729 #endif 756 EInside inIt= Inside( p ); << 757 << 758 if( inIt != kSurface ) << 759 { << 760 ed << " ERROR> Surface Normal was cal << 761 << " with point not on surface." << << 762 } << 763 else << 764 { << 765 ed << " ERROR> Surface Normal has not << 766 << " despite the point being on the << 767 } << 768 << 769 if( inIt != kInside) << 770 { << 771 ed << " Safety (Dist To In) = " << D << 772 } << 773 if( inIt != kOutside) << 774 { << 775 ed << " Safety (Dist to Out) = " << D << 776 } << 777 ed << " Coordinates of point : " << p << << 778 ed << " Parameters of solid : " << G4end << 779 << 780 if( inIt == kSurface ) << 781 { << 782 G4Exception("G4Torus::SurfaceNormal(p) << 783 JustWarning, ed, << 784 "Failing to find normal, e << 785 } << 786 else << 787 { << 788 static const char* NameInside[3]= { "I << 789 ed << " The point is " << NameInside[ << 790 G4Exception("G4Torus::SurfaceNormal(p) << 791 JustWarning, ed, "Point p << 792 } << 793 #endif << 794 norm = ApproxSurfaceNormal(p); 730 norm = ApproxSurfaceNormal(p); 795 } 731 } 796 else if ( noSurfaces == 1 ) { norm = sumnor 732 else if ( noSurfaces == 1 ) { norm = sumnorm; } 797 else { norm = sumnor 733 else { norm = sumnorm.unit(); } 798 734 799 return norm ; 735 return norm ; 800 } 736 } 801 737 802 ////////////////////////////////////////////// 738 ////////////////////////////////////////////////////////////////////////////// 803 // 739 // 804 // Algorithm for SurfaceNormal() following the 740 // Algorithm for SurfaceNormal() following the original specification 805 // for points not on the surface 741 // for points not on the surface 806 742 807 G4ThreeVector G4Torus::ApproxSurfaceNormal( co 743 G4ThreeVector G4Torus::ApproxSurfaceNormal( const G4ThreeVector& p ) const 808 { 744 { 809 ENorm side ; 745 ENorm side ; 810 G4ThreeVector norm; 746 G4ThreeVector norm; 811 G4double rho,pt,phi; << 747 G4double rho2,rho,pt2,pt,phi; 812 G4double distRMin,distRMax,distSPhi,distEPhi 748 G4double distRMin,distRMax,distSPhi,distEPhi,distMin; 813 749 814 rho = std::hypot(p.x(),p.y()); << 750 rho2 = p.x()*p.x() + p.y()*p.y(); 815 pt = std::hypot(p.z(),rho-fRtor); << 751 rho = std::sqrt(rho2) ; >> 752 pt2 = std::fabs(rho2+p.z()*p.z() +fRtor*fRtor - 2*fRtor*rho) ; >> 753 pt = std::sqrt(pt2) ; 816 754 817 #ifdef G4CSGDEBUG << 818 G4cout << " G4Torus::ApproximateSurfaceNorma << 819 << G4endl; << 820 #endif << 821 << 822 distRMax = std::fabs(pt - fRmax) ; 755 distRMax = std::fabs(pt - fRmax) ; 823 756 824 if(fRmin != 0.0) // First minimum radius << 757 if(fRmin) // First minimum radius 825 { 758 { 826 distRMin = std::fabs(pt - fRmin) ; 759 distRMin = std::fabs(pt - fRmin) ; 827 760 828 if (distRMin < distRMax) 761 if (distRMin < distRMax) 829 { 762 { 830 distMin = distRMin ; 763 distMin = distRMin ; 831 side = kNRMin ; 764 side = kNRMin ; 832 } 765 } 833 else 766 else 834 { 767 { 835 distMin = distRMax ; 768 distMin = distRMax ; 836 side = kNRMax ; 769 side = kNRMax ; 837 } 770 } 838 } 771 } 839 else 772 else 840 { 773 { 841 distMin = distRMax ; 774 distMin = distRMax ; 842 side = kNRMax ; 775 side = kNRMax ; 843 } 776 } 844 if ( (fDPhi < twopi) && (rho != 0.0) ) << 777 if ( (fDPhi < twopi) && rho ) 845 { 778 { 846 phi = std::atan2(p.y(),p.x()) ; // Protect 779 phi = std::atan2(p.y(),p.x()) ; // Protected against (0,0,z) (above rho!=0) 847 780 848 if (phi < 0) { phi += twopi ; } 781 if (phi < 0) { phi += twopi ; } 849 782 850 if (fSPhi < 0 ) { distSPhi = std::fabs(ph 783 if (fSPhi < 0 ) { distSPhi = std::fabs(phi-(fSPhi+twopi))*rho ; } 851 else { distSPhi = std::fabs(ph 784 else { distSPhi = std::fabs(phi-fSPhi)*rho ; } 852 785 853 distEPhi = std::fabs(phi - fSPhi - fDPhi)* 786 distEPhi = std::fabs(phi - fSPhi - fDPhi)*rho ; 854 787 855 if (distSPhi < distEPhi) // Find new minim 788 if (distSPhi < distEPhi) // Find new minimum 856 { 789 { 857 if (distSPhi<distMin) side = kNSPhi ; 790 if (distSPhi<distMin) side = kNSPhi ; 858 } 791 } 859 else 792 else 860 { 793 { 861 if (distEPhi < distMin) { side = kNEPhi 794 if (distEPhi < distMin) { side = kNEPhi ; } 862 } 795 } 863 } 796 } 864 switch (side) 797 switch (side) 865 { 798 { 866 case kNRMin: // Inner radius 799 case kNRMin: // Inner radius 867 norm = G4ThreeVector( -p.x()*(1-fRtor/rh 800 norm = G4ThreeVector( -p.x()*(1-fRtor/rho)/pt, 868 -p.y()*(1-fRtor/rh 801 -p.y()*(1-fRtor/rho)/pt, 869 -p.z()/pt 802 -p.z()/pt ) ; 870 break ; 803 break ; 871 case kNRMax: // Outer radius 804 case kNRMax: // Outer radius 872 norm = G4ThreeVector( p.x()*(1-fRtor/rho 805 norm = G4ThreeVector( p.x()*(1-fRtor/rho)/pt, 873 p.y()*(1-fRtor/rho 806 p.y()*(1-fRtor/rho)/pt, 874 p.z()/pt 807 p.z()/pt ) ; 875 break; 808 break; 876 case kNSPhi: 809 case kNSPhi: 877 norm = G4ThreeVector(std::sin(fSPhi),-st 810 norm = G4ThreeVector(std::sin(fSPhi),-std::cos(fSPhi),0) ; 878 break; 811 break; 879 case kNEPhi: 812 case kNEPhi: 880 norm = G4ThreeVector(-std::sin(fSPhi+fDP 813 norm = G4ThreeVector(-std::sin(fSPhi+fDPhi),std::cos(fSPhi+fDPhi),0) ; 881 break; 814 break; 882 default: // Should never reach th << 815 default: 883 DumpInfo(); 816 DumpInfo(); 884 G4Exception("G4Torus::ApproxSurfaceNorma 817 G4Exception("G4Torus::ApproxSurfaceNormal()", 885 "GeomSolids1002", JustWarnin << 818 "Notification", JustWarning, 886 "Undefined side for valid su 819 "Undefined side for valid surface normal to solid."); 887 break ; 820 break ; 888 } 821 } 889 return norm ; 822 return norm ; 890 } 823 } 891 824 892 ////////////////////////////////////////////// 825 /////////////////////////////////////////////////////////////////////// 893 // 826 // 894 // Calculate distance to shape from outside, a 827 // Calculate distance to shape from outside, along normalised vector 895 // - return kInfinity if no intersection, or i 828 // - return kInfinity if no intersection, or intersection distance <= tolerance 896 // 829 // 897 // - Compute the intersection with the z plane 830 // - Compute the intersection with the z planes 898 // - if at valid r, phi, return 831 // - if at valid r, phi, return 899 // 832 // 900 // -> If point is outer outer radius, compute 833 // -> If point is outer outer radius, compute intersection with rmax 901 // - if at valid phi,z return 834 // - if at valid phi,z return 902 // 835 // 903 // -> Compute intersection with inner radius, 836 // -> Compute intersection with inner radius, taking largest +ve root 904 // - if valid (phi), save intersction 837 // - if valid (phi), save intersction 905 // 838 // 906 // -> If phi segmented, compute intersectio 839 // -> If phi segmented, compute intersections with phi half planes 907 // - return smallest of valid phi inter 840 // - return smallest of valid phi intersections and 908 // inner radius intersection 841 // inner radius intersection 909 // 842 // 910 // NOTE: 843 // NOTE: 911 // - Precalculations for phi trigonometry are 844 // - Precalculations for phi trigonometry are Done `just in time' 912 // - `if valid' implies tolerant checking of i 845 // - `if valid' implies tolerant checking of intersection points 913 846 914 G4double G4Torus::DistanceToIn( const G4ThreeV 847 G4double G4Torus::DistanceToIn( const G4ThreeVector& p, 915 const G4ThreeV 848 const G4ThreeVector& v ) const 916 { 849 { 917 // Get bounding box of full torus << 918 // << 919 G4double boxDx = fRtor + fRmax; << 920 G4double boxDy = boxDx; << 921 G4double boxDz = fRmax; << 922 G4double boxMax = boxDx; << 923 G4double boxMin = boxDz; << 924 << 925 // Check if point is traveling away << 926 // << 927 G4double distX = std::abs(p.x()) - boxDx; << 928 G4double distY = std::abs(p.y()) - boxDy; << 929 G4double distZ = std::abs(p.z()) - boxDz; << 930 if (distX >= -halfCarTolerance && p.x()*v.x( << 931 if (distY >= -halfCarTolerance && p.y()*v.y( << 932 if (distZ >= -halfCarTolerance && p.z()*v.z( << 933 << 934 // Calculate safety distance to bounding box << 935 // If point is too far, move it closer and c << 936 // << 937 G4double Dmax = 32*boxMax; << 938 G4double safe = std::max(std::max(distX,dist << 939 if (safe > Dmax) << 940 { << 941 G4double dist = safe - 1.e-8*safe - boxMin << 942 dist += DistanceToIn(p + dist*v, v); << 943 return (dist >= kInfinity) ? kInfinity : d << 944 } << 945 850 946 // Find intersection with torus << 947 // << 948 G4double snxt=kInfinity, sphi=kInfinity; // 851 G4double snxt=kInfinity, sphi=kInfinity; // snxt = default return value 949 852 950 G4double sd[4] ; << 853 G4double s[4] ; 951 854 952 // Precalculated trig for phi intersections 855 // Precalculated trig for phi intersections - used by r,z intersections to 953 // 856 // check validity 954 857 955 G4bool seg; // true if segmented 858 G4bool seg; // true if segmented 956 G4double hDPhi; // half dphi << 859 G4double hDPhi,hDPhiOT,hDPhiIT,cosHDPhiOT=0.,cosHDPhiIT=0.; >> 860 // half dphi + outer tolerance 957 G4double cPhi,sinCPhi=0.,cosCPhi=0.; // cen 861 G4double cPhi,sinCPhi=0.,cosCPhi=0.; // central phi 958 862 959 G4double tolORMin2; // `generous' radii squ << 863 G4double tolORMin2,tolIRMin2; // `generous' radii squared 960 G4double tolORMax2; << 864 G4double tolORMax2,tolIRMax2 ; >> 865 >> 866 G4double Dist,xi,yi,zi,rhoi2,it2; // Intersection point variables 961 867 962 G4double Dist,xi,yi,zi,rhoi,it2; // Intersec << 963 868 964 G4double Comp; 869 G4double Comp; 965 G4double cosSPhi,sinSPhi; // Trig for 870 G4double cosSPhi,sinSPhi; // Trig for phi start intersect 966 G4double ePhi,cosEPhi,sinEPhi; // for phi e 871 G4double ePhi,cosEPhi,sinEPhi; // for phi end intersect 967 872 968 // Set phi divided flag and precalcs 873 // Set phi divided flag and precalcs 969 // 874 // 970 if ( fDPhi < twopi ) 875 if ( fDPhi < twopi ) 971 { 876 { 972 seg = true ; 877 seg = true ; 973 hDPhi = 0.5*fDPhi ; // half delta 878 hDPhi = 0.5*fDPhi ; // half delta phi 974 cPhi = fSPhi + hDPhi ; 879 cPhi = fSPhi + hDPhi ; >> 880 hDPhiOT = hDPhi+0.5*kAngTolerance ; // outers tol' half delta phi >> 881 hDPhiIT = hDPhi - 0.5*kAngTolerance ; 975 sinCPhi = std::sin(cPhi) ; 882 sinCPhi = std::sin(cPhi) ; 976 cosCPhi = std::cos(cPhi) ; 883 cosCPhi = std::cos(cPhi) ; >> 884 cosHDPhiOT = std::cos(hDPhiOT) ; >> 885 cosHDPhiIT = std::cos(hDPhiIT) ; 977 } 886 } 978 else 887 else 979 { 888 { 980 seg = false ; 889 seg = false ; 981 } 890 } 982 891 983 if (fRmin > fRminTolerance) // Calculate tol << 892 if (fRmin > kRadTolerance) // Calculate tolerant rmin and rmax 984 { 893 { 985 tolORMin2 = (fRmin - fRminTolerance)*(fRmi << 894 tolORMin2 = (fRmin - 0.5*kRadTolerance)*(fRmin - 0.5*kRadTolerance) ; >> 895 tolIRMin2 = (fRmin + 0.5*kRadTolerance)*(fRmin + 0.5*kRadTolerance) ; 986 } 896 } 987 else 897 else 988 { 898 { 989 tolORMin2 = 0 ; 899 tolORMin2 = 0 ; >> 900 tolIRMin2 = 0 ; 990 } 901 } 991 tolORMax2 = (fRmax + fRmaxTolerance)*(fRmax << 902 tolORMax2 = (fRmax + 0.5*kRadTolerance)*(fRmax + 0.5*kRadTolerance) ; >> 903 tolIRMax2 = (fRmax - kRadTolerance*0.5)*(fRmax - kRadTolerance*0.5) ; 992 904 993 // Intersection with Rmax (possible return) 905 // Intersection with Rmax (possible return) and Rmin (must also check phi) 994 906 995 snxt = SolveNumericJT(p,v,fRmax,true); << 907 G4double Rtor2 = fRtor*fRtor ; 996 908 997 if (fRmin != 0.0) // Possible Rmin intersec << 909 snxt = SolveNumericJT(p,v,fRmax,true); >> 910 if (fRmin) // Possible Rmin intersection 998 { 911 { 999 sd[0] = SolveNumericJT(p,v,fRmin,true); << 912 s[0] = SolveNumericJT(p,v,fRmin,true); 1000 if ( sd[0] < snxt ) { snxt = sd[0] ; } << 913 if ( s[0] < snxt ) { snxt = s[0] ; } 1001 } 914 } 1002 915 1003 // 916 // 1004 // Phi segment intersection 917 // Phi segment intersection 1005 // 918 // 1006 // o Tolerant of points inside phi planes b 919 // o Tolerant of points inside phi planes by up to kCarTolerance*0.5 1007 // 920 // 1008 // o NOTE: Large duplication of code betwee 921 // o NOTE: Large duplication of code between sphi & ephi checks 1009 // -> only diffs: sphi -> ephi, Com 922 // -> only diffs: sphi -> ephi, Comp -> -Comp and half-plane 1010 // intersection check <=0 -> >=0 923 // intersection check <=0 -> >=0 1011 // -> use some form of loop Constru 924 // -> use some form of loop Construct ? 1012 925 1013 if (seg) 926 if (seg) 1014 { 927 { 1015 sinSPhi = std::sin(fSPhi) ; // First phi << 928 sinSPhi = std::sin(fSPhi) ; // First phi surface (`S'tarting phi) 1016 cosSPhi = std::cos(fSPhi) ; 929 cosSPhi = std::cos(fSPhi) ; 1017 Comp = v.x()*sinSPhi - v.y()*cosSPhi ; 930 Comp = v.x()*sinSPhi - v.y()*cosSPhi ; // Component in outwards 1018 931 // normal direction 1019 if (Comp < 0 ) 932 if (Comp < 0 ) 1020 { 933 { 1021 Dist = (p.y()*cosSPhi - p.x()*sinSPhi) 934 Dist = (p.y()*cosSPhi - p.x()*sinSPhi) ; 1022 935 1023 if (Dist < halfCarTolerance) << 936 if (Dist < kCarTolerance*0.5) 1024 { 937 { 1025 sphi = Dist/Comp ; 938 sphi = Dist/Comp ; 1026 if (sphi < snxt) 939 if (sphi < snxt) 1027 { 940 { 1028 if ( sphi < 0 ) { sphi = 0 ; } 941 if ( sphi < 0 ) { sphi = 0 ; } 1029 942 1030 xi = p.x() + sphi*v.x() ; 943 xi = p.x() + sphi*v.x() ; 1031 yi = p.y() + sphi*v.y() ; 944 yi = p.y() + sphi*v.y() ; 1032 zi = p.z() + sphi*v.z() ; 945 zi = p.z() + sphi*v.z() ; 1033 rhoi = std::hypot(xi,yi); << 946 rhoi2 = xi*xi + yi*yi ; 1034 it2 = zi*zi + (rhoi-fRtor)*(rhoi-fR << 947 it2 = std::fabs(rhoi2 + zi*zi + Rtor2 - 2*fRtor*std::sqrt(rhoi2)) ; 1035 948 1036 if ( it2 >= tolORMin2 && it2 <= tol 949 if ( it2 >= tolORMin2 && it2 <= tolORMax2 ) 1037 { 950 { 1038 // r intersection is good - check 951 // r intersection is good - check intersecting 1039 // with correct half-plane 952 // with correct half-plane 1040 // 953 // 1041 if ((yi*cosCPhi-xi*sinCPhi)<=0) 954 if ((yi*cosCPhi-xi*sinCPhi)<=0) { snxt=sphi; } 1042 } << 955 } 1043 } 956 } 1044 } 957 } 1045 } 958 } 1046 ePhi=fSPhi+fDPhi; // Second phi surfac << 959 ePhi=fSPhi+fDPhi; // Second phi surface (`E'nding phi) 1047 sinEPhi=std::sin(ePhi); 960 sinEPhi=std::sin(ePhi); 1048 cosEPhi=std::cos(ePhi); 961 cosEPhi=std::cos(ePhi); 1049 Comp=-(v.x()*sinEPhi-v.y()*cosEPhi); 962 Comp=-(v.x()*sinEPhi-v.y()*cosEPhi); 1050 963 1051 if ( Comp < 0 ) // Component in outward 964 if ( Comp < 0 ) // Component in outwards normal dirn 1052 { 965 { 1053 Dist = -(p.y()*cosEPhi - p.x()*sinEPhi) 966 Dist = -(p.y()*cosEPhi - p.x()*sinEPhi) ; 1054 967 1055 if (Dist < halfCarTolerance ) << 968 if (Dist < kCarTolerance*0.5 ) 1056 { 969 { 1057 sphi = Dist/Comp ; 970 sphi = Dist/Comp ; 1058 << 1059 if (sphi < snxt ) 971 if (sphi < snxt ) 1060 { 972 { 1061 if (sphi < 0 ) { sphi = 0 ; } 973 if (sphi < 0 ) { sphi = 0 ; } 1062 974 1063 xi = p.x() + sphi*v.x() ; 975 xi = p.x() + sphi*v.x() ; 1064 yi = p.y() + sphi*v.y() ; 976 yi = p.y() + sphi*v.y() ; 1065 zi = p.z() + sphi*v.z() ; 977 zi = p.z() + sphi*v.z() ; 1066 rhoi = std::hypot(xi,yi); << 978 rhoi2 = xi*xi + yi*yi ; 1067 it2 = zi*zi + (rhoi-fRtor)*(rhoi-fR << 979 it2 = std::fabs(rhoi2 + zi*zi + Rtor2 - 2*fRtor*std::sqrt(rhoi2)) ; 1068 980 1069 if (it2 >= tolORMin2 && it2 <= tolO 981 if (it2 >= tolORMin2 && it2 <= tolORMax2) 1070 { 982 { 1071 // z and r intersections good - c 983 // z and r intersections good - check intersecting 1072 // with correct half-plane 984 // with correct half-plane 1073 // 985 // 1074 if ((yi*cosCPhi-xi*sinCPhi)>=0) 986 if ((yi*cosCPhi-xi*sinCPhi)>=0) { snxt=sphi; } 1075 } 987 } 1076 } 988 } 1077 } 989 } 1078 } 990 } 1079 } 991 } 1080 if(snxt < halfCarTolerance) { snxt = 0.0 ; << 992 if(snxt < 0.5*kCarTolerance) { snxt = 0.0 ; } 1081 993 1082 return snxt ; 994 return snxt ; 1083 } 995 } 1084 996 1085 ///////////////////////////////////////////// 997 ///////////////////////////////////////////////////////////////////////////// 1086 // 998 // 1087 // Calculate distance (<= actual) to closest 999 // Calculate distance (<= actual) to closest surface of shape from outside 1088 // - Calculate distance to z, radial planes 1000 // - Calculate distance to z, radial planes 1089 // - Only to phi planes if outside phi extent 1001 // - Only to phi planes if outside phi extent 1090 // - Return 0 if point inside 1002 // - Return 0 if point inside 1091 1003 1092 G4double G4Torus::DistanceToIn( const G4Three 1004 G4double G4Torus::DistanceToIn( const G4ThreeVector& p ) const 1093 { 1005 { 1094 G4double safe=0.0, safe1, safe2 ; 1006 G4double safe=0.0, safe1, safe2 ; 1095 G4double phiC, cosPhiC, sinPhiC, safePhi, e 1007 G4double phiC, cosPhiC, sinPhiC, safePhi, ePhi, cosPsi ; 1096 G4double rho, pt ; << 1008 G4double rho2, rho, pt2, pt ; 1097 << 1009 1098 rho = std::hypot(p.x(),p.y()); << 1010 rho2 = p.x()*p.x() + p.y()*p.y() ; 1099 pt = std::hypot(p.z(),rho-fRtor); << 1011 rho = std::sqrt(rho2) ; >> 1012 pt2 = std::fabs(rho2 + p.z()*p.z() + fRtor*fRtor - 2*fRtor*rho) ; >> 1013 pt = std::sqrt(pt2) ; >> 1014 1100 safe1 = fRmin - pt ; 1015 safe1 = fRmin - pt ; 1101 safe2 = pt - fRmax ; 1016 safe2 = pt - fRmax ; 1102 1017 1103 if (safe1 > safe2) { safe = safe1; } 1018 if (safe1 > safe2) { safe = safe1; } 1104 else { safe = safe2; } 1019 else { safe = safe2; } 1105 1020 1106 if ( fDPhi < twopi && (rho != 0.0) ) << 1021 if ( fDPhi < twopi && rho ) 1107 { 1022 { 1108 phiC = fSPhi + fDPhi*0.5 ; 1023 phiC = fSPhi + fDPhi*0.5 ; 1109 cosPhiC = std::cos(phiC) ; 1024 cosPhiC = std::cos(phiC) ; 1110 sinPhiC = std::sin(phiC) ; 1025 sinPhiC = std::sin(phiC) ; 1111 cosPsi = (p.x()*cosPhiC + p.y()*sinPhiC) 1026 cosPsi = (p.x()*cosPhiC + p.y()*sinPhiC)/rho ; 1112 1027 1113 if (cosPsi < std::cos(fDPhi*0.5) ) // Psi 1028 if (cosPsi < std::cos(fDPhi*0.5) ) // Psi=angle from central phi to point 1114 { // Poi 1029 { // Point lies outside phi range 1115 if ((p.y()*cosPhiC - p.x()*sinPhiC) <= 1030 if ((p.y()*cosPhiC - p.x()*sinPhiC) <= 0 ) 1116 { 1031 { 1117 safePhi = std::fabs(p.x()*std::sin(fS 1032 safePhi = std::fabs(p.x()*std::sin(fSPhi) - p.y()*std::cos(fSPhi)) ; 1118 } 1033 } 1119 else 1034 else 1120 { 1035 { 1121 ePhi = fSPhi + fDPhi ; 1036 ePhi = fSPhi + fDPhi ; 1122 safePhi = std::fabs(p.x()*std::sin(eP 1037 safePhi = std::fabs(p.x()*std::sin(ePhi) - p.y()*std::cos(ePhi)) ; 1123 } 1038 } 1124 if (safePhi > safe) { safe = safePhi ; 1039 if (safePhi > safe) { safe = safePhi ; } 1125 } 1040 } 1126 } 1041 } 1127 if (safe < 0 ) { safe = 0 ; } 1042 if (safe < 0 ) { safe = 0 ; } 1128 return safe; 1043 return safe; 1129 } 1044 } 1130 1045 1131 ///////////////////////////////////////////// 1046 /////////////////////////////////////////////////////////////////////////// 1132 // 1047 // 1133 // Calculate distance to surface of shape fro 1048 // Calculate distance to surface of shape from `inside', allowing for tolerance 1134 // - Only Calc rmax intersection if no valid 1049 // - Only Calc rmax intersection if no valid rmin intersection 1135 // 1050 // 1136 1051 1137 G4double G4Torus::DistanceToOut( const G4Thre 1052 G4double G4Torus::DistanceToOut( const G4ThreeVector& p, 1138 const G4Thre 1053 const G4ThreeVector& v, 1139 const G4bool 1054 const G4bool calcNorm, 1140 G4bool << 1055 G4bool *validNorm, 1141 G4Thre << 1056 G4ThreeVector *n ) const 1142 { 1057 { 1143 ESide side = kNull, sidephi = kNull ; 1058 ESide side = kNull, sidephi = kNull ; 1144 G4double snxt = kInfinity, sphi, sd[4] ; << 1059 G4double snxt = kInfinity, sphi, s[4] ; 1145 1060 1146 // Vars for phi intersection 1061 // Vars for phi intersection 1147 // 1062 // 1148 G4double sinSPhi, cosSPhi, ePhi, sinEPhi, c 1063 G4double sinSPhi, cosSPhi, ePhi, sinEPhi, cosEPhi; 1149 G4double cPhi, sinCPhi, cosCPhi ; 1064 G4double cPhi, sinCPhi, cosCPhi ; 1150 G4double pDistS, compS, pDistE, compE, sphi 1065 G4double pDistS, compS, pDistE, compE, sphi2, xi, yi, zi, vphi ; 1151 1066 1152 // Radial Intersections Defenitions & Gener 1067 // Radial Intersections Defenitions & General Precals 1153 1068 1154 //////////////////////// new calculation // 1069 //////////////////////// new calculation ////////////////////// 1155 1070 1156 #if 1 1071 #if 1 1157 1072 1158 // This is the version with the calculation 1073 // This is the version with the calculation of CalcNorm = true 1159 // To be done: Check the precision of this 1074 // To be done: Check the precision of this calculation. 1160 // If you want return always validNorm = fa 1075 // If you want return always validNorm = false, then take the version below 1161 1076 1162 << 1077 G4double Rtor2 = fRtor*fRtor ; 1163 G4double rho = std::hypot(p.x(),p.y()); << 1078 G4double rho2 = p.x()*p.x()+p.y()*p.y(); 1164 G4double pt = hypot(p.z(),rho-fRtor); << 1079 G4double rho = std::sqrt(rho2) ; >> 1080 >> 1081 >> 1082 G4double pt2 = std::fabs(rho2 + p.z()*p.z() + Rtor2 - 2*fRtor*rho) ; >> 1083 G4double pt = std::sqrt(pt2) ; 1165 1084 1166 G4double pDotV = p.x()*v.x() + p.y()*v.y() 1085 G4double pDotV = p.x()*v.x() + p.y()*v.y() + p.z()*v.z() ; 1167 1086 1168 G4double tolRMax = fRmax - fRmaxTolerance ; << 1087 G4double tolRMax = fRmax - kRadTolerance*0.5 ; 1169 1088 1170 G4double vDotNmax = pDotV - fRtor*(v.x()* 1089 G4double vDotNmax = pDotV - fRtor*(v.x()*p.x() + v.y()*p.y())/rho ; 1171 G4double pDotxyNmax = (1 - fRtor/rho) ; 1090 G4double pDotxyNmax = (1 - fRtor/rho) ; 1172 1091 1173 if( (pt*pt > tolRMax*tolRMax) && (vDotNmax << 1092 if( (pt2 > tolRMax*tolRMax) && (vDotNmax >= 0) ) 1174 { 1093 { 1175 // On tolerant boundary & heading outward 1094 // On tolerant boundary & heading outwards (or perpendicular to) outer 1176 // radial surface -> leaving immediately 1095 // radial surface -> leaving immediately with *n for really convex part 1177 // only 1096 // only 1178 1097 1179 if ( calcNorm && (pDotxyNmax >= -2.*fRmax << 1098 if ( calcNorm && (pDotxyNmax >= -kRadTolerance) ) 1180 { 1099 { 1181 *n = G4ThreeVector( p.x()*(1 - fRtor/rh 1100 *n = G4ThreeVector( p.x()*(1 - fRtor/rho)/pt, 1182 p.y()*(1 - fRtor/rh 1101 p.y()*(1 - fRtor/rho)/pt, 1183 p.z()/pt 1102 p.z()/pt ) ; 1184 *validNorm = true ; 1103 *validNorm = true ; 1185 } 1104 } 1186 << 1187 return snxt = 0 ; // Leaving by Rmax imme 1105 return snxt = 0 ; // Leaving by Rmax immediately 1188 } 1106 } 1189 1107 1190 snxt = SolveNumericJT(p,v,fRmax,false); 1108 snxt = SolveNumericJT(p,v,fRmax,false); 1191 side = kRMax ; 1109 side = kRMax ; 1192 1110 1193 // rmin 1111 // rmin 1194 1112 1195 if ( fRmin != 0.0 ) << 1113 if ( fRmin ) 1196 { 1114 { 1197 G4double tolRMin = fRmin + fRminTolerance << 1115 G4double tolRMin = fRmin + kRadTolerance*0.5 ; 1198 1116 1199 if ( (pt*pt < tolRMin*tolRMin) && (vDotNm << 1117 if ( (pt2 < tolRMin*tolRMin) && (vDotNmax < 0) ) 1200 { 1118 { 1201 if (calcNorm) { *validNorm = false ; } 1119 if (calcNorm) { *validNorm = false ; } // Concave surface of the torus 1202 return snxt = 0 ; 1120 return snxt = 0 ; // Leaving by Rmin immediately 1203 } 1121 } 1204 1122 1205 sd[0] = SolveNumericJT(p,v,fRmin,false); << 1123 s[0] = SolveNumericJT(p,v,fRmin,false); 1206 if ( sd[0] < snxt ) << 1124 if ( s[0] < snxt ) 1207 { 1125 { 1208 snxt = sd[0] ; << 1126 snxt = s[0] ; 1209 side = kRMin ; 1127 side = kRMin ; 1210 } 1128 } 1211 } 1129 } 1212 1130 1213 #else 1131 #else 1214 1132 1215 // this is the "conservative" version which 1133 // this is the "conservative" version which return always validnorm = false 1216 // NOTE: using this version the unit test t 1134 // NOTE: using this version the unit test testG4Torus will break 1217 1135 1218 snxt = SolveNumericJT(p,v,fRmax,false); 1136 snxt = SolveNumericJT(p,v,fRmax,false); 1219 side = kRMax ; 1137 side = kRMax ; 1220 1138 1221 if ( fRmin ) 1139 if ( fRmin ) 1222 { 1140 { 1223 sd[0] = SolveNumericJT(p,v,fRmin,false); << 1141 s[0] = SolveNumericJT(p,v,fRmin,false); 1224 if ( sd[0] < snxt ) << 1142 if ( s[0] < snxt ) 1225 { 1143 { 1226 snxt = sd[0] ; << 1144 snxt = s[0] ; 1227 side = kRMin ; 1145 side = kRMin ; 1228 } 1146 } 1229 } 1147 } 1230 1148 1231 if ( calcNorm && (snxt == 0.0) ) 1149 if ( calcNorm && (snxt == 0.0) ) 1232 { 1150 { 1233 *validNorm = false ; // Leaving solid, 1151 *validNorm = false ; // Leaving solid, but possible re-intersection 1234 return snxt ; 1152 return snxt ; 1235 } 1153 } 1236 1154 1237 #endif 1155 #endif 1238 << 1156 1239 if (fDPhi < twopi) // Phi Intersections 1157 if (fDPhi < twopi) // Phi Intersections 1240 { 1158 { 1241 sinSPhi = std::sin(fSPhi) ; 1159 sinSPhi = std::sin(fSPhi) ; 1242 cosSPhi = std::cos(fSPhi) ; 1160 cosSPhi = std::cos(fSPhi) ; 1243 ePhi = fSPhi + fDPhi ; 1161 ePhi = fSPhi + fDPhi ; 1244 sinEPhi = std::sin(ePhi) ; 1162 sinEPhi = std::sin(ePhi) ; 1245 cosEPhi = std::cos(ePhi) ; 1163 cosEPhi = std::cos(ePhi) ; 1246 cPhi = fSPhi + fDPhi*0.5 ; 1164 cPhi = fSPhi + fDPhi*0.5 ; 1247 sinCPhi = std::sin(cPhi) ; 1165 sinCPhi = std::sin(cPhi) ; 1248 cosCPhi = std::cos(cPhi) ; 1166 cosCPhi = std::cos(cPhi) ; 1249 << 1250 // angle calculation with correction << 1251 // of difference in domain of atan2 and S << 1252 // << 1253 vphi = std::atan2(v.y(),v.x()) ; << 1254 << 1255 if ( vphi < fSPhi - halfAngTolerance ) << 1256 else if ( vphi > ePhi + halfAngTolerance << 1257 1167 1258 if ( (p.x() != 0.0) || (p.y() != 0.0) ) / << 1168 if ( p.x() || p.y() ) // Check if on z axis (rho not needed later) 1259 { 1169 { 1260 pDistS = p.x()*sinSPhi - p.y()*cosSPhi 1170 pDistS = p.x()*sinSPhi - p.y()*cosSPhi ; // pDist -ve when inside 1261 pDistE = -p.x()*sinEPhi + p.y()*cosEPhi 1171 pDistE = -p.x()*sinEPhi + p.y()*cosEPhi ; 1262 1172 1263 // Comp -ve when in direction of outwar 1173 // Comp -ve when in direction of outwards normal 1264 // 1174 // 1265 compS = -sinSPhi*v.x() + cosSPhi*v.y( 1175 compS = -sinSPhi*v.x() + cosSPhi*v.y() ; 1266 compE = sinEPhi*v.x() - cosEPhi*v.y() 1176 compE = sinEPhi*v.x() - cosEPhi*v.y() ; 1267 sidephi = kNull ; 1177 sidephi = kNull ; 1268 << 1178 1269 if( ( (fDPhi <= pi) && ( (pDistS <= hal << 1179 if ( (pDistS <= 0) && (pDistE <= 0) ) 1270 && (pDistE <= hal << 1271 || ( (fDPhi > pi) && ((pDistS <= hal << 1272 || (pDistE <= ha << 1273 { 1180 { 1274 // Inside both phi *full* planes 1181 // Inside both phi *full* planes 1275 1182 1276 if ( compS < 0 ) << 1183 if (compS<0) >> 1184 { >> 1185 sphi=pDistS/compS; >> 1186 xi=p.x()+sphi*v.x(); >> 1187 yi=p.y()+sphi*v.y(); >> 1188 >> 1189 // Check intersecting with correct half-plane >> 1190 // (if not -> no intersect) >> 1191 // >> 1192 if ((yi*cosCPhi-xi*sinCPhi)>=0) >> 1193 { >> 1194 sphi=kInfinity; >> 1195 } >> 1196 else >> 1197 { >> 1198 sidephi=kSPhi; >> 1199 if (pDistS>-kCarTolerance*0.5) { sphi=0; } // Leave by sphi >> 1200 // immediately >> 1201 } >> 1202 } >> 1203 else >> 1204 { >> 1205 sphi=kInfinity; >> 1206 } >> 1207 >> 1208 if (compE<0) 1277 { 1209 { 1278 sphi = pDistS/compS ; << 1210 sphi2=pDistE/compE; 1279 << 1211 1280 if (sphi >= -halfCarTolerance) << 1212 // Only check further if < starting phi intersection >> 1213 // >> 1214 if (sphi2<sphi) 1281 { 1215 { 1282 xi = p.x() + sphi*v.x() ; << 1216 xi=p.x()+sphi2*v.x(); 1283 yi = p.y() + sphi*v.y() ; << 1217 yi=p.y()+sphi2*v.y(); 1284 << 1218 1285 // Check intersecting with correc 1219 // Check intersecting with correct half-plane 1286 // (if not -> no intersect) << 1220 // 1287 // << 1221 if ((yi*cosCPhi-xi*sinCPhi)>=0) 1288 if ( (std::fabs(xi)<=kCarToleranc << 1289 && (std::fabs(yi)<=kCarToleranc << 1290 { 1222 { 1291 sidephi = kSPhi; << 1223 // Leaving via ending phi 1292 if ( ((fSPhi-halfAngTolerance)< << 1224 // 1293 && ((ePhi+halfAngTolerance)>= << 1225 sidephi=kEPhi; >> 1226 if (pDistE<=-kCarTolerance*0.5) >> 1227 { >> 1228 sphi=sphi2; >> 1229 } >> 1230 else 1294 { 1231 { 1295 sphi = kInfinity; << 1232 sphi=0; 1296 } 1233 } 1297 } 1234 } 1298 else if ( yi*cosCPhi-xi*sinCPhi > << 1235 } >> 1236 } >> 1237 } >> 1238 else if ( (pDistS>=0) && (pDistE>=0) ) >> 1239 { >> 1240 // Outside both *full* phi planes >> 1241 >> 1242 if (pDistS <= pDistE) >> 1243 { >> 1244 sidephi = kSPhi ; >> 1245 } >> 1246 else >> 1247 { >> 1248 sidephi = kEPhi ; >> 1249 } >> 1250 if (fDPhi>pi) >> 1251 { >> 1252 if ( (compS<0) && (compE<0) ) { sphi=0; } >> 1253 else { sphi=kInfinity; } >> 1254 } >> 1255 else >> 1256 { >> 1257 // if towards both >=0 then once inside (after error) >> 1258 // will remain inside >> 1259 // >> 1260 if ( (compS>=0) && (compE>=0) ) >> 1261 { >> 1262 sphi=kInfinity; >> 1263 } >> 1264 else >> 1265 { >> 1266 sphi=0; >> 1267 } >> 1268 } >> 1269 } >> 1270 else if ( (pDistS>0) && (pDistE<0) ) >> 1271 { >> 1272 // Outside full starting plane, inside full ending plane >> 1273 >> 1274 if (fDPhi>pi) >> 1275 { >> 1276 if (compE<0) >> 1277 { >> 1278 sphi=pDistE/compE; >> 1279 xi=p.x()+sphi*v.x(); >> 1280 yi=p.y()+sphi*v.y(); >> 1281 >> 1282 // Check intersection in correct half-plane >> 1283 // (if not -> not leaving phi extent) >> 1284 // >> 1285 if ((yi*cosCPhi-xi*sinCPhi)<=0) 1299 { 1286 { 1300 sphi = kInfinity ; << 1287 sphi=kInfinity; 1301 } 1288 } 1302 else 1289 else 1303 { 1290 { 1304 sidephi = kSPhi ; << 1291 // Leaving via Ending phi 1305 } << 1292 // >> 1293 sidephi = kEPhi ; >> 1294 if (pDistE>-kCarTolerance*0.5) { sphi=0; } >> 1295 } 1306 } 1296 } 1307 else 1297 else 1308 { 1298 { 1309 sphi = kInfinity ; << 1299 sphi=kInfinity; 1310 } 1300 } 1311 } 1301 } 1312 else 1302 else 1313 { 1303 { 1314 sphi = kInfinity ; << 1304 if (compS>=0) 1315 } << 1316 << 1317 if ( compE < 0 ) << 1318 { << 1319 sphi2 = pDistE/compE ; << 1320 << 1321 // Only check further if < starting << 1322 // << 1323 if ( (sphi2 > -kCarTolerance) && (s << 1324 { 1305 { 1325 xi = p.x() + sphi2*v.x() ; << 1306 if (compE<0) 1326 yi = p.y() + sphi2*v.y() ; << 1327 << 1328 if ( (std::fabs(xi)<=kCarToleranc << 1329 && (std::fabs(yi)<=kCarToleranc << 1330 { 1307 { 1331 // Leaving via ending phi << 1308 sphi=pDistE/compE; >> 1309 xi=p.x()+sphi*v.x(); >> 1310 yi=p.y()+sphi*v.y(); >> 1311 >> 1312 // Check intersection in correct half-plane >> 1313 // (if not -> remain in extent) 1332 // 1314 // 1333 if( (fSPhi-halfAngTolerance > v << 1315 if ((yi*cosCPhi-xi*sinCPhi)<=0) 1334 || (ePhi+halfAngTolerance < << 1335 { 1316 { 1336 sidephi = kEPhi ; << 1317 sphi=kInfinity; 1337 sphi = sphi2; << 1338 } 1318 } 1339 } << 1319 else 1340 else // Check intersecting wit << 1341 { << 1342 if ( (yi*cosCPhi-xi*sinCPhi) >= << 1343 { 1320 { 1344 // Leaving via ending phi << 1321 // otherwise leaving via Ending phi 1345 // 1322 // 1346 sidephi = kEPhi ; << 1323 sidephi=kEPhi; 1347 sphi = sphi2; << 1348 << 1349 } 1324 } 1350 } 1325 } >> 1326 else { sphi=kInfinity; } >> 1327 } >> 1328 else >> 1329 { >> 1330 // leaving immediately by starting phi >> 1331 // >> 1332 sidephi=kSPhi; >> 1333 sphi=0; 1351 } 1334 } 1352 } 1335 } 1353 } 1336 } 1354 else 1337 else 1355 { 1338 { 1356 sphi = kInfinity ; << 1339 // Must be pDistS<0&&pDistE>0 >> 1340 // Inside full starting plane, outside full ending plane >> 1341 >> 1342 if (fDPhi>pi) >> 1343 { >> 1344 if (compS<0) >> 1345 { >> 1346 sphi=pDistS/compS; >> 1347 xi=p.x()+sphi*v.x(); >> 1348 yi=p.y()+sphi*v.y(); >> 1349 >> 1350 // Check intersection in correct half-plane >> 1351 // (if not -> not leaving phi extent) >> 1352 // >> 1353 if ((yi*cosCPhi-xi*sinCPhi)>=0) >> 1354 { >> 1355 sphi=kInfinity; >> 1356 } >> 1357 else >> 1358 { >> 1359 // Leaving via Starting phi >> 1360 // >> 1361 sidephi = kSPhi ; >> 1362 if (pDistS>-kCarTolerance*0.5) { sphi=0; } >> 1363 } >> 1364 } >> 1365 else >> 1366 { >> 1367 sphi=kInfinity; >> 1368 } >> 1369 } >> 1370 else >> 1371 { >> 1372 if (compE>=0) >> 1373 { >> 1374 if (compS<0) >> 1375 { >> 1376 sphi=pDistS/compS; >> 1377 xi=p.x()+sphi*v.x(); >> 1378 yi=p.y()+sphi*v.y(); >> 1379 >> 1380 // Check intersection in correct half-plane >> 1381 // (if not -> remain in extent) >> 1382 // >> 1383 if ((yi*cosCPhi-xi*sinCPhi)>=0) >> 1384 { >> 1385 sphi=kInfinity; >> 1386 } >> 1387 else >> 1388 { >> 1389 // otherwise leaving via Starting phi >> 1390 // >> 1391 sidephi=kSPhi; >> 1392 } >> 1393 } >> 1394 else { sphi=kInfinity; } >> 1395 } >> 1396 else >> 1397 { >> 1398 // leaving immediately by ending >> 1399 // >> 1400 sidephi=kEPhi; >> 1401 sphi=0; >> 1402 } >> 1403 } 1357 } 1404 } 1358 } << 1405 } 1359 else 1406 else 1360 { 1407 { 1361 // On z axis + travel not || to z axis 1408 // On z axis + travel not || to z axis -> if phi of vector direction 1362 // within phi of shape, Step limited by 1409 // within phi of shape, Step limited by rmax, else Step =0 1363 1410 1364 vphi = std::atan2(v.y(),v.x()); << 1411 vphi=std::atan2(v.y(),v.x()); 1365 << 1412 if ( (fSPhi<vphi) && (vphi<fSPhi+fDPhi) ) 1366 if ( ( fSPhi-halfAngTolerance <= vphi ) << 1367 ( vphi <= ( ePhi+halfAngTolerance << 1368 { 1413 { 1369 sphi = kInfinity; << 1414 sphi=kInfinity; 1370 } 1415 } 1371 else 1416 else 1372 { 1417 { 1373 sidephi = kSPhi ; // arbitrary 1418 sidephi = kSPhi ; // arbitrary 1374 sphi=0; 1419 sphi=0; 1375 } 1420 } 1376 } 1421 } 1377 1422 1378 // Order intersections 1423 // Order intersections 1379 1424 1380 if (sphi<snxt) 1425 if (sphi<snxt) 1381 { 1426 { 1382 snxt=sphi; 1427 snxt=sphi; 1383 side=sidephi; 1428 side=sidephi; 1384 } << 1429 } 1385 } 1430 } >> 1431 G4double rhoi2,rhoi,it2,it,iDotxyNmax ; 1386 1432 1387 G4double rhoi,it,iDotxyNmax ; << 1388 // Note: by numerical computation we know w 1433 // Note: by numerical computation we know where the ray hits the torus 1389 // So I propose to return the side where th 1434 // So I propose to return the side where the ray hits 1390 1435 1391 if (calcNorm) 1436 if (calcNorm) 1392 { 1437 { 1393 switch(side) 1438 switch(side) 1394 { 1439 { 1395 case kRMax: // n is 1440 case kRMax: // n is unit vector 1396 xi = p.x() + snxt*v.x() ; 1441 xi = p.x() + snxt*v.x() ; 1397 yi = p.y() + snxt*v.y() ; << 1442 yi =p.y() + snxt*v.y() ; 1398 zi = p.z() + snxt*v.z() ; 1443 zi = p.z() + snxt*v.z() ; 1399 rhoi = std::hypot(xi,yi); << 1444 rhoi2 = xi*xi + yi*yi ; 1400 it = hypot(zi,rhoi-fRtor); << 1445 rhoi = std::sqrt(rhoi2) ; 1401 << 1446 it2 = std::fabs(rhoi2 + zi*zi + fRtor*fRtor - 2*fRtor*rhoi) ; >> 1447 it = std::sqrt(it2) ; 1402 iDotxyNmax = (1-fRtor/rhoi) ; 1448 iDotxyNmax = (1-fRtor/rhoi) ; 1403 if(iDotxyNmax >= -2.*fRmaxTolerance) << 1449 if(iDotxyNmax >= -kRadTolerance) // really convex part of Rmax 1404 { 1450 { 1405 *n = G4ThreeVector( xi*(1-fRtor/rho 1451 *n = G4ThreeVector( xi*(1-fRtor/rhoi)/it, 1406 yi*(1-fRtor/rho 1452 yi*(1-fRtor/rhoi)/it, 1407 zi/it 1453 zi/it ) ; 1408 *validNorm = true ; 1454 *validNorm = true ; 1409 } 1455 } 1410 else 1456 else 1411 { 1457 { 1412 *validNorm = false ; // concave-con 1458 *validNorm = false ; // concave-convex part of Rmax 1413 } 1459 } 1414 break ; 1460 break ; 1415 1461 1416 case kRMin: 1462 case kRMin: 1417 *validNorm = false ; // Rmin is conc 1463 *validNorm = false ; // Rmin is concave or concave-convex 1418 break; 1464 break; 1419 1465 1420 case kSPhi: 1466 case kSPhi: 1421 if (fDPhi <= pi ) 1467 if (fDPhi <= pi ) 1422 { 1468 { 1423 *n=G4ThreeVector(std::sin(fSPhi),-s 1469 *n=G4ThreeVector(std::sin(fSPhi),-std::cos(fSPhi),0); 1424 *validNorm=true; 1470 *validNorm=true; 1425 } 1471 } 1426 else 1472 else 1427 { 1473 { 1428 *validNorm = false ; 1474 *validNorm = false ; 1429 } 1475 } 1430 break ; 1476 break ; 1431 1477 1432 case kEPhi: 1478 case kEPhi: 1433 if (fDPhi <= pi) 1479 if (fDPhi <= pi) 1434 { 1480 { 1435 *n=G4ThreeVector(-std::sin(fSPhi+fD 1481 *n=G4ThreeVector(-std::sin(fSPhi+fDPhi),std::cos(fSPhi+fDPhi),0); 1436 *validNorm=true; 1482 *validNorm=true; 1437 } 1483 } 1438 else 1484 else 1439 { 1485 { 1440 *validNorm = false ; 1486 *validNorm = false ; 1441 } 1487 } 1442 break; 1488 break; 1443 1489 1444 default: 1490 default: 1445 1491 1446 // It seems we go here from time to t 1492 // It seems we go here from time to time ... 1447 1493 >> 1494 G4cout.precision(16); 1448 G4cout << G4endl; 1495 G4cout << G4endl; 1449 DumpInfo(); 1496 DumpInfo(); 1450 std::ostringstream message; << 1497 G4cout << "Position:" << G4endl << G4endl; 1451 G4long oldprc = message.precision(16) << 1498 G4cout << "p.x() = " << p.x()/mm << " mm" << G4endl; 1452 message << "Undefined side for valid << 1499 G4cout << "p.y() = " << p.y()/mm << " mm" << G4endl; 1453 << G4endl << 1500 G4cout << "p.z() = " << p.z()/mm << " mm" << G4endl << G4endl; 1454 << "Position:" << G4endl << << 1501 G4cout << "Direction:" << G4endl << G4endl; 1455 << "p.x() = " << p.x()/mm < << 1502 G4cout << "v.x() = " << v.x() << G4endl; 1456 << "p.y() = " << p.y()/mm < << 1503 G4cout << "v.y() = " << v.y() << G4endl; 1457 << "p.z() = " << p.z()/mm < << 1504 G4cout << "v.z() = " << v.z() << G4endl << G4endl; 1458 << "Direction:" << G4endl << << 1505 G4cout << "Proposed distance :" << G4endl << G4endl; 1459 << "v.x() = " << v.x() << G << 1506 G4cout << "snxt = " << snxt/mm << " mm" << G4endl << G4endl; 1460 << "v.y() = " << v.y() << G << 1461 << "v.z() = " << v.z() << G << 1462 << "Proposed distance :" << G << 1463 << "snxt = " << snxt/mm << " << 1464 message.precision(oldprc); << 1465 G4Exception("G4Torus::DistanceToOut(p 1507 G4Exception("G4Torus::DistanceToOut(p,v,..)", 1466 "GeomSolids1002",JustWarn << 1508 "Notification",JustWarning, >> 1509 "Undefined side for valid surface normal to solid."); 1467 break; 1510 break; 1468 } 1511 } 1469 } 1512 } 1470 if ( snxt<halfCarTolerance ) { snxt=0 ; } << 1471 1513 1472 return snxt; 1514 return snxt; 1473 } 1515 } 1474 1516 1475 ///////////////////////////////////////////// 1517 ///////////////////////////////////////////////////////////////////////// 1476 // 1518 // 1477 // Calculate distance (<=actual) to closest s 1519 // Calculate distance (<=actual) to closest surface of shape from inside 1478 1520 1479 G4double G4Torus::DistanceToOut( const G4Thre 1521 G4double G4Torus::DistanceToOut( const G4ThreeVector& p ) const 1480 { 1522 { 1481 G4double safe=0.0,safeR1,safeR2; 1523 G4double safe=0.0,safeR1,safeR2; 1482 G4double rho,pt ; << 1524 G4double rho2,rho,pt2,pt ; 1483 G4double safePhi,phiC,cosPhiC,sinPhiC,ePhi; 1525 G4double safePhi,phiC,cosPhiC,sinPhiC,ePhi; 1484 << 1526 rho2 = p.x()*p.x() + p.y()*p.y() ; 1485 rho = std::hypot(p.x(),p.y()); << 1527 rho = std::sqrt(rho2) ; 1486 pt = std::hypot(p.z(),rho-fRtor); << 1528 pt2 = std::fabs(rho2 + p.z()*p.z() + fRtor*fRtor - 2*fRtor*rho) ; 1487 << 1529 pt = std::sqrt(pt2) ; >> 1530 1488 #ifdef G4CSGDEBUG 1531 #ifdef G4CSGDEBUG 1489 if( Inside(p) == kOutside ) 1532 if( Inside(p) == kOutside ) 1490 { 1533 { 1491 G4long oldprc = G4cout.precision(16) ; << 1534 G4cout.precision(16) ; 1492 G4cout << G4endl ; 1535 G4cout << G4endl ; 1493 DumpInfo(); 1536 DumpInfo(); 1494 G4cout << "Position:" << G4endl << G4en 1537 G4cout << "Position:" << G4endl << G4endl ; 1495 G4cout << "p.x() = " << p.x()/mm << " 1538 G4cout << "p.x() = " << p.x()/mm << " mm" << G4endl ; 1496 G4cout << "p.y() = " << p.y()/mm << " 1539 G4cout << "p.y() = " << p.y()/mm << " mm" << G4endl ; 1497 G4cout << "p.z() = " << p.z()/mm << " 1540 G4cout << "p.z() = " << p.z()/mm << " mm" << G4endl << G4endl ; 1498 G4cout.precision(oldprc); << 1541 G4Exception("G4Torus::DistanceToOut(p)", "Notification", 1499 G4Exception("G4Torus::DistanceToOut(p)", << 1500 JustWarning, "Point p is out 1542 JustWarning, "Point p is outside !?" ); 1501 } 1543 } 1502 #endif 1544 #endif 1503 1545 1504 if (fRmin != 0.0) << 1546 if (fRmin) 1505 { 1547 { 1506 safeR1 = pt - fRmin ; 1548 safeR1 = pt - fRmin ; 1507 safeR2 = fRmax - pt ; 1549 safeR2 = fRmax - pt ; 1508 1550 1509 if (safeR1 < safeR2) { safe = safeR1 ; } 1551 if (safeR1 < safeR2) { safe = safeR1 ; } 1510 else { safe = safeR2 ; } 1552 else { safe = safeR2 ; } 1511 } 1553 } 1512 else 1554 else 1513 { 1555 { 1514 safe = fRmax - pt ; 1556 safe = fRmax - pt ; 1515 } 1557 } 1516 1558 1517 // Check if phi divided, Calc distances clo 1559 // Check if phi divided, Calc distances closest phi plane 1518 // 1560 // 1519 if (fDPhi < twopi) // Above/below central p << 1561 if (fDPhi<twopi) // Above/below central phi of Torus? 1520 { 1562 { 1521 phiC = fSPhi + fDPhi*0.5 ; 1563 phiC = fSPhi + fDPhi*0.5 ; 1522 cosPhiC = std::cos(phiC) ; 1564 cosPhiC = std::cos(phiC) ; 1523 sinPhiC = std::sin(phiC) ; 1565 sinPhiC = std::sin(phiC) ; 1524 1566 1525 if ((p.y()*cosPhiC-p.x()*sinPhiC)<=0) 1567 if ((p.y()*cosPhiC-p.x()*sinPhiC)<=0) 1526 { 1568 { 1527 safePhi = -(p.x()*std::sin(fSPhi) - p.y 1569 safePhi = -(p.x()*std::sin(fSPhi) - p.y()*std::cos(fSPhi)) ; 1528 } 1570 } 1529 else 1571 else 1530 { 1572 { 1531 ePhi = fSPhi + fDPhi ; 1573 ePhi = fSPhi + fDPhi ; 1532 safePhi = (p.x()*std::sin(ePhi) - p.y() 1574 safePhi = (p.x()*std::sin(ePhi) - p.y()*std::cos(ePhi)) ; 1533 } 1575 } 1534 if (safePhi < safe) { safe = safePhi ; } 1576 if (safePhi < safe) { safe = safePhi ; } 1535 } 1577 } 1536 if (safe < 0) { safe = 0 ; } 1578 if (safe < 0) { safe = 0 ; } 1537 return safe ; 1579 return safe ; 1538 } 1580 } 1539 1581 1540 ///////////////////////////////////////////// << 1582 ///////////////////////////////////////////////////////////////////////////// 1541 // 1583 // 1542 // Stream object contents to an output stream << 1584 // Create a List containing the transformed vertices >> 1585 // Ordering [0-3] -fRtor cross section >> 1586 // [4-7] +fRtor cross section such that [0] is below [4], >> 1587 // [1] below [5] etc. >> 1588 // Note: >> 1589 // Caller has deletion resposibility >> 1590 // Potential improvement: For last slice, use actual ending angle >> 1591 // to avoid rounding error problems. 1543 1592 1544 G4GeometryType G4Torus::GetEntityType() const << 1593 G4ThreeVectorList* >> 1594 G4Torus::CreateRotatedVertices( const G4AffineTransform& pTransform, >> 1595 G4int& noPolygonVertices ) const 1545 { 1596 { 1546 return {"G4Torus"}; << 1597 G4ThreeVectorList *vertices; >> 1598 G4ThreeVector vertex0,vertex1,vertex2,vertex3; >> 1599 G4double meshAngle,meshRMax,crossAngle,cosCrossAngle,sinCrossAngle,sAngle; >> 1600 G4double rMaxX,rMaxY,rMinX,rMinY; >> 1601 G4int crossSection,noCrossSections; >> 1602 >> 1603 // Compute no of cross-sections necessary to mesh tube >> 1604 // >> 1605 noCrossSections = G4int (fDPhi/kMeshAngleDefault) + 1 ; >> 1606 >> 1607 if (noCrossSections < kMinMeshSections) >> 1608 { >> 1609 noCrossSections = kMinMeshSections ; >> 1610 } >> 1611 else if (noCrossSections>kMaxMeshSections) >> 1612 { >> 1613 noCrossSections=kMaxMeshSections; >> 1614 } >> 1615 meshAngle = fDPhi/(noCrossSections - 1) ; >> 1616 meshRMax = (fRtor + fRmax)/std::cos(meshAngle*0.5) ; >> 1617 >> 1618 // If complete in phi, set start angle such that mesh will be at fRmax >> 1619 // on the x axis. Will give better extent calculations when not rotated >> 1620 >> 1621 if ( (fDPhi == pi*2.0) && (fSPhi == 0) ) >> 1622 { >> 1623 sAngle = -meshAngle*0.5 ; >> 1624 } >> 1625 else >> 1626 { >> 1627 sAngle = fSPhi ; >> 1628 } >> 1629 vertices = new G4ThreeVectorList(); >> 1630 vertices->reserve(noCrossSections*4) ; >> 1631 >> 1632 if (vertices) >> 1633 { >> 1634 for (crossSection=0;crossSection<noCrossSections;crossSection++) >> 1635 { >> 1636 // Compute coordinates of cross section at section crossSection >> 1637 >> 1638 crossAngle=sAngle+crossSection*meshAngle; >> 1639 cosCrossAngle=std::cos(crossAngle); >> 1640 sinCrossAngle=std::sin(crossAngle); >> 1641 >> 1642 rMaxX=meshRMax*cosCrossAngle; >> 1643 rMaxY=meshRMax*sinCrossAngle; >> 1644 rMinX=(fRtor-fRmax)*cosCrossAngle; >> 1645 rMinY=(fRtor-fRmax)*sinCrossAngle; >> 1646 vertex0=G4ThreeVector(rMinX,rMinY,-fRmax); >> 1647 vertex1=G4ThreeVector(rMaxX,rMaxY,-fRmax); >> 1648 vertex2=G4ThreeVector(rMaxX,rMaxY,+fRmax); >> 1649 vertex3=G4ThreeVector(rMinX,rMinY,+fRmax); >> 1650 >> 1651 vertices->push_back(pTransform.TransformPoint(vertex0)); >> 1652 vertices->push_back(pTransform.TransformPoint(vertex1)); >> 1653 vertices->push_back(pTransform.TransformPoint(vertex2)); >> 1654 vertices->push_back(pTransform.TransformPoint(vertex3)); >> 1655 } >> 1656 noPolygonVertices = 4 ; >> 1657 } >> 1658 else >> 1659 { >> 1660 DumpInfo(); >> 1661 G4Exception("G4Torus::CreateRotatedVertices()", >> 1662 "FatalError", FatalException, >> 1663 "Error in allocation of vertices. Out of memory !"); >> 1664 } >> 1665 return vertices; 1547 } 1666 } 1548 1667 1549 ///////////////////////////////////////////// 1668 ////////////////////////////////////////////////////////////////////////// 1550 // 1669 // 1551 // Make a clone of the object << 1670 // Stream object contents to an output stream 1552 // << 1671 1553 G4VSolid* G4Torus::Clone() const << 1672 G4GeometryType G4Torus::GetEntityType() const 1554 { 1673 { 1555 return new G4Torus(*this); << 1674 return G4String("G4Torus"); 1556 } 1675 } 1557 1676 1558 ///////////////////////////////////////////// 1677 ////////////////////////////////////////////////////////////////////////// 1559 // 1678 // 1560 // Stream object contents to an output stream 1679 // Stream object contents to an output stream 1561 1680 1562 std::ostream& G4Torus::StreamInfo( std::ostre 1681 std::ostream& G4Torus::StreamInfo( std::ostream& os ) const 1563 { 1682 { 1564 G4long oldprc = os.precision(16); << 1565 os << "------------------------------------ 1683 os << "-----------------------------------------------------------\n" 1566 << " *** Dump for solid - " << GetNam 1684 << " *** Dump for solid - " << GetName() << " ***\n" 1567 << " ================================ 1685 << " ===================================================\n" 1568 << " Solid type: G4Torus\n" 1686 << " Solid type: G4Torus\n" 1569 << " Parameters: \n" 1687 << " Parameters: \n" 1570 << " inner radius: " << fRmin/mm << " 1688 << " inner radius: " << fRmin/mm << " mm \n" 1571 << " outer radius: " << fRmax/mm << " 1689 << " outer radius: " << fRmax/mm << " mm \n" 1572 << " swept radius: " << fRtor/mm << " 1690 << " swept radius: " << fRtor/mm << " mm \n" 1573 << " starting phi: " << fSPhi/degree 1691 << " starting phi: " << fSPhi/degree << " degrees \n" 1574 << " delta phi : " << fDPhi/degree 1692 << " delta phi : " << fDPhi/degree << " degrees \n" 1575 << "------------------------------------ 1693 << "-----------------------------------------------------------\n"; 1576 os.precision(oldprc); << 1577 1694 1578 return os; 1695 return os; 1579 } 1696 } 1580 1697 1581 ///////////////////////////////////////////// 1698 //////////////////////////////////////////////////////////////////////////// 1582 // 1699 // 1583 // GetPointOnSurface 1700 // GetPointOnSurface 1584 1701 1585 G4ThreeVector G4Torus::GetPointOnSurface() co 1702 G4ThreeVector G4Torus::GetPointOnSurface() const 1586 { 1703 { 1587 G4double cosu, sinu,cosv, sinv, aOut, aIn, 1704 G4double cosu, sinu,cosv, sinv, aOut, aIn, aSide, chose, phi, theta, rRand; 1588 1705 1589 phi = G4RandFlat::shoot(fSPhi,fSPhi+fDPhi << 1706 phi = RandFlat::shoot(fSPhi,fSPhi+fDPhi); 1590 theta = G4RandFlat::shoot(0.,twopi); << 1707 theta = RandFlat::shoot(0.,2.*pi); 1591 1708 1592 cosu = std::cos(phi); sinu = std::sin( 1709 cosu = std::cos(phi); sinu = std::sin(phi); 1593 cosv = std::cos(theta); sinv = std::sin( 1710 cosv = std::cos(theta); sinv = std::sin(theta); 1594 1711 1595 // compute the areas 1712 // compute the areas 1596 1713 1597 aOut = (fDPhi)*twopi*fRtor*fRmax; << 1714 aOut = (fDPhi)*2.*pi*fRtor*fRmax; 1598 aIn = (fDPhi)*twopi*fRtor*fRmin; << 1715 aIn = (fDPhi)*2.*pi*fRtor*fRmin; 1599 aSide = pi*(fRmax*fRmax-fRmin*fRmin); 1716 aSide = pi*(fRmax*fRmax-fRmin*fRmin); 1600 1717 1601 if ((fSPhi == 0) && (fDPhi == twopi)){ aSid << 1718 if(fSPhi == 0 && fDPhi == twopi){ aSide = 0; } 1602 chose = G4RandFlat::shoot(0.,aOut + aIn + 2 << 1719 chose = RandFlat::shoot(0.,aOut + aIn + 2.*aSide); 1603 1720 1604 if(chose < aOut) 1721 if(chose < aOut) 1605 { 1722 { 1606 return { (fRtor+fRmax*cosv)*cosu, (fRtor+ << 1723 return G4ThreeVector ((fRtor+fRmax*cosv)*cosu, >> 1724 (fRtor+fRmax*cosv)*sinu, fRmax*sinv); 1607 } 1725 } 1608 else if( (chose >= aOut) && (chose < aOut + 1726 else if( (chose >= aOut) && (chose < aOut + aIn) ) 1609 { 1727 { 1610 return { (fRtor+fRmin*cosv)*cosu, (fRtor+ << 1728 return G4ThreeVector ((fRtor+fRmin*cosv)*cosu, >> 1729 (fRtor+fRmin*cosv)*sinu, fRmin*sinv); 1611 } 1730 } 1612 else if( (chose >= aOut + aIn) && (chose < 1731 else if( (chose >= aOut + aIn) && (chose < aOut + aIn + aSide) ) 1613 { 1732 { 1614 rRand = GetRadiusInRing(fRmin,fRmax); << 1733 rRand = RandFlat::shoot(fRmin,fRmax); 1615 return { (fRtor+rRand*cosv)*std::cos(fSPh << 1734 return G4ThreeVector ((fRtor+rRand*cosv)*std::cos(fSPhi), 1616 (fRtor+rRand*cosv)*std::sin(fSPh << 1735 (fRtor+rRand*cosv)*std::sin(fSPhi), rRand*sinv); 1617 } 1736 } 1618 else 1737 else 1619 { 1738 { 1620 rRand = GetRadiusInRing(fRmin,fRmax); << 1739 rRand = RandFlat::shoot(fRmin,fRmax); 1621 return { (fRtor+rRand*cosv)*std::cos(fSPh << 1740 return G4ThreeVector ((fRtor+rRand*cosv)*std::cos(fSPhi+fDPhi), 1622 (fRtor+rRand*cosv)*std::sin(fSPh << 1741 (fRtor+rRand*cosv)*std::sin(fSPhi+fDPhi), >> 1742 rRand*sinv); 1623 } 1743 } 1624 } 1744 } 1625 1745 1626 ///////////////////////////////////////////// 1746 /////////////////////////////////////////////////////////////////////// 1627 // 1747 // 1628 // Visualisation Functions 1748 // Visualisation Functions 1629 1749 1630 void G4Torus::DescribeYourselfTo ( G4VGraphic 1750 void G4Torus::DescribeYourselfTo ( G4VGraphicsScene& scene ) const 1631 { 1751 { 1632 scene.AddSolid (*this); 1752 scene.AddSolid (*this); 1633 } 1753 } 1634 1754 1635 G4Polyhedron* G4Torus::CreatePolyhedron () co 1755 G4Polyhedron* G4Torus::CreatePolyhedron () const 1636 { 1756 { 1637 return new G4PolyhedronTorus (fRmin, fRmax, 1757 return new G4PolyhedronTorus (fRmin, fRmax, fRtor, fSPhi, fDPhi); 1638 } 1758 } 1639 1759 1640 #endif // !defined(G4GEOM_USE_TORUS) || !defi << 1760 G4NURBS* G4Torus::CreateNURBS () const >> 1761 { >> 1762 G4NURBS* pNURBS; >> 1763 if (fRmin != 0) >> 1764 { >> 1765 if (fDPhi >= 2.0 * pi) >> 1766 { >> 1767 pNURBS = new G4NURBStube(fRmin, fRmax, fRtor); >> 1768 } >> 1769 else >> 1770 { >> 1771 pNURBS = new G4NURBStubesector(fRmin, fRmax, fRtor, fSPhi, fSPhi + fDPhi); >> 1772 } >> 1773 } >> 1774 else >> 1775 { >> 1776 if (fDPhi >= 2.0 * pi) >> 1777 { >> 1778 pNURBS = new G4NURBScylinder (fRmax, fRtor); >> 1779 } >> 1780 else >> 1781 { >> 1782 const G4double epsilon = 1.e-4; // Cylinder sector not yet available! >> 1783 pNURBS = new G4NURBStubesector (epsilon, fRmax, fRtor, >> 1784 fSPhi, fSPhi + fDPhi); >> 1785 } >> 1786 } >> 1787 return pNURBS; >> 1788 } 1641 1789