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 // $Id: G4Ellipsoid.cc 73430 2013-08-27 11:04:49Z gcosmo $ >> 27 // 26 // class G4Ellipsoid 28 // class G4Ellipsoid 27 // 29 // 28 // Implementation of G4Ellipsoid class << 30 // Implementation for G4Ellipsoid class >> 31 // >> 32 // History: >> 33 // >> 34 // 10.11.99 G.Horton-Smith -- first writing, based on G4Sphere class >> 35 // 25.02.05 G.Guerrieri -- Modified for future Geant4 release 29 // 36 // 30 // 10.11.99 G.Horton-Smith: first writing, bas << 31 // 25.02.05 G.Guerrieri: Revised << 32 // 15.12.19 E.Tcherniaev: Complete revision << 33 // ------------------------------------------- 37 // -------------------------------------------------------------------- 34 38 35 #include "G4Ellipsoid.hh" << 36 << 37 #if !(defined(G4GEOM_USE_UELLIPSOID) && define << 38 << 39 #include "globals.hh" 39 #include "globals.hh" 40 40 >> 41 #include "G4Ellipsoid.hh" >> 42 41 #include "G4VoxelLimits.hh" 43 #include "G4VoxelLimits.hh" 42 #include "G4AffineTransform.hh" 44 #include "G4AffineTransform.hh" 43 #include "G4GeometryTolerance.hh" 45 #include "G4GeometryTolerance.hh" 44 #include "G4BoundingEnvelope.hh" << 46 45 #include "G4RandomTools.hh" << 47 #include "meshdefs.hh" 46 #include "G4QuickRand.hh" << 48 #include "Randomize.hh" 47 49 48 #include "G4VPVParameterisation.hh" 50 #include "G4VPVParameterisation.hh" 49 51 50 #include "G4VGraphicsScene.hh" 52 #include "G4VGraphicsScene.hh" >> 53 #include "G4Polyhedron.hh" 51 #include "G4VisExtent.hh" 54 #include "G4VisExtent.hh" 52 55 53 #include "G4AutoLock.hh" << 54 << 55 namespace << 56 { << 57 G4Mutex polyhedronMutex = G4MUTEX_INITIALIZ << 58 G4Mutex lateralareaMutex = G4MUTEX_INITIALIZ << 59 } << 60 << 61 using namespace CLHEP; 56 using namespace CLHEP; 62 57 63 ////////////////////////////////////////////// << 58 /////////////////////////////////////////////////////////////////////////////// 64 // 59 // 65 // Constructor << 60 // constructor - check parameters, convert angles so 0<sphi+dpshi<=2_PI >> 61 // - note if pDPhi>2PI then reset to 2PI 66 62 67 G4Ellipsoid::G4Ellipsoid(const G4String& name, << 63 G4Ellipsoid::G4Ellipsoid(const G4String& pName, 68 G4double xSemiA << 64 G4double pxSemiAxis, 69 G4double ySemiA << 65 G4double pySemiAxis, 70 G4double zSemiA << 66 G4double pzSemiAxis, 71 G4double zBotto << 67 G4double pzBottomCut, 72 G4double zTopCu << 68 G4double pzTopCut) 73 : G4VSolid(name), fDx(xSemiAxis), fDy(ySemiA << 69 : G4VSolid(pName), fpPolyhedron(0), fCubicVolume(0.), fSurfaceArea(0.), 74 fZBottomCut(zBottomCut), fZTopCut(zTopCut) << 70 zBottomCut(0.), zTopCut(0.) 75 { << 71 { 76 CheckParameters(); << 72 // note: for users that want to use the full ellipsoid it is useful >> 73 // to include a default for the cuts >> 74 >> 75 kRadTolerance = G4GeometryTolerance::GetInstance()->GetRadialTolerance(); >> 76 >> 77 halfCarTolerance = kCarTolerance*0.5; >> 78 halfRadTolerance = kRadTolerance*0.5; >> 79 >> 80 // Check Semi-Axis >> 81 if ( (pxSemiAxis<=0.) || (pySemiAxis<=0.) || (pzSemiAxis<=0.) ) >> 82 { >> 83 std::ostringstream message; >> 84 message << "Invalid semi-axis - " << GetName(); >> 85 G4Exception("G4Ellipsoid::G4Ellipsoid()", "GeomSolids0002", >> 86 FatalErrorInArgument, message); >> 87 } >> 88 SetSemiAxis(pxSemiAxis, pySemiAxis, pzSemiAxis); >> 89 >> 90 if ( pzBottomCut == 0 && pzTopCut == 0 ) >> 91 { >> 92 SetZCuts(-pzSemiAxis, pzSemiAxis); >> 93 } >> 94 else if ( (pzBottomCut < pzSemiAxis) && (pzTopCut > -pzSemiAxis) >> 95 && (pzBottomCut < pzTopCut) ) >> 96 { >> 97 SetZCuts(pzBottomCut, pzTopCut); >> 98 } >> 99 else >> 100 { >> 101 std::ostringstream message; >> 102 message << "Invalid z-coordinate for cutting plane - " << GetName(); >> 103 G4Exception("G4Ellipsoid::G4Ellipsoid()", "GeomSolids0002", >> 104 FatalErrorInArgument, message); >> 105 } 77 } 106 } 78 107 79 ////////////////////////////////////////////// << 108 /////////////////////////////////////////////////////////////////////////////// 80 // 109 // 81 // Fake default constructor - sets only member 110 // Fake default constructor - sets only member data and allocates memory 82 // for usage restri << 111 // for usage restricted to object persistency. 83 << 112 // 84 G4Ellipsoid::G4Ellipsoid( __void__& a ) 113 G4Ellipsoid::G4Ellipsoid( __void__& a ) 85 : G4VSolid(a), fDx(0.), fDy(0.), fDz(0.), fZ << 114 : G4VSolid(a), fpPolyhedron(0), kRadTolerance(0.), >> 115 halfCarTolerance(0.), halfRadTolerance(0.), fCubicVolume(0.), >> 116 fSurfaceArea(0.), xSemiAxis(0.), ySemiAxis(0.), zSemiAxis(0.), >> 117 semiAxisMax(0.), zBottomCut(0.), zTopCut(0.) 86 { 118 { 87 } 119 } 88 120 89 ////////////////////////////////////////////// << 121 /////////////////////////////////////////////////////////////////////////////// 90 // 122 // 91 // Destructor 123 // Destructor 92 124 93 G4Ellipsoid::~G4Ellipsoid() 125 G4Ellipsoid::~G4Ellipsoid() 94 { 126 { 95 delete fpPolyhedron; fpPolyhedron = nullptr; << 96 } 127 } 97 128 98 ////////////////////////////////////////////// << 129 /////////////////////////////////////////////////////////////////////////////// 99 // 130 // 100 // Copy constructor 131 // Copy constructor 101 132 102 G4Ellipsoid::G4Ellipsoid(const G4Ellipsoid& rh 133 G4Ellipsoid::G4Ellipsoid(const G4Ellipsoid& rhs) 103 : G4VSolid(rhs), 134 : G4VSolid(rhs), 104 fDx(rhs.fDx), fDy(rhs.fDy), fDz(rhs.fDz), << 135 fpPolyhedron(0), kRadTolerance(rhs.kRadTolerance), 105 fZBottomCut(rhs.fZBottomCut), fZTopCut(rhs. << 136 halfCarTolerance(rhs.halfCarTolerance), 106 halfTolerance(rhs.halfTolerance), << 137 halfRadTolerance(rhs.halfRadTolerance), 107 fXmax(rhs.fXmax), fYmax(rhs.fYmax), << 138 fCubicVolume(rhs.fCubicVolume), fSurfaceArea(rhs.fSurfaceArea), 108 fRsph(rhs.fRsph), fR(rhs.fR), << 139 xSemiAxis(rhs.xSemiAxis), ySemiAxis(rhs.ySemiAxis), 109 fSx(rhs.fSx), fSy(rhs.fSy), fSz(rhs.fSz), << 140 zSemiAxis(rhs.zSemiAxis), semiAxisMax(rhs.semiAxisMax), 110 fZMidCut(rhs.fZMidCut), fZDimCut(rhs.fZDimC << 141 zBottomCut(rhs.zBottomCut), zTopCut(rhs.zTopCut) 111 fQ1(rhs.fQ1), fQ2(rhs.fQ2), << 112 fCubicVolume(rhs.fCubicVolume), << 113 fSurfaceArea(rhs.fSurfaceArea), << 114 fLateralArea(rhs.fLateralArea) << 115 { 142 { 116 } 143 } 117 144 118 ////////////////////////////////////////////// << 145 /////////////////////////////////////////////////////////////////////////////// 119 // 146 // 120 // Assignment operator 147 // Assignment operator 121 148 122 G4Ellipsoid& G4Ellipsoid::operator = (const G4 << 149 G4Ellipsoid& G4Ellipsoid::operator = (const G4Ellipsoid& rhs) 123 { 150 { 124 // Check assignment to self 151 // Check assignment to self 125 // 152 // 126 if (this == &rhs) { return *this; } 153 if (this == &rhs) { return *this; } 127 154 128 // Copy base class data 155 // Copy base class data 129 // 156 // 130 G4VSolid::operator=(rhs); 157 G4VSolid::operator=(rhs); 131 158 132 // Copy data 159 // Copy data 133 // 160 // 134 fDx = rhs.fDx; << 161 fpPolyhedron = 0; kRadTolerance = rhs.kRadTolerance; 135 fDy = rhs.fDy; << 162 halfCarTolerance = rhs.halfCarTolerance; 136 fDz = rhs.fDz; << 163 halfRadTolerance = rhs.halfRadTolerance; 137 fZBottomCut = rhs.fZBottomCut; << 164 fCubicVolume = rhs.fCubicVolume; fSurfaceArea = rhs.fSurfaceArea; 138 fZTopCut = rhs.fZTopCut; << 165 xSemiAxis = rhs.xSemiAxis; ySemiAxis = rhs.ySemiAxis; 139 << 166 zSemiAxis = rhs.zSemiAxis; semiAxisMax = rhs.semiAxisMax; 140 halfTolerance = rhs.halfTolerance; << 167 zBottomCut = rhs.zBottomCut; zTopCut = rhs.zTopCut; 141 fXmax = rhs.fXmax; << 142 fYmax = rhs.fYmax; << 143 fRsph = rhs.fRsph; << 144 fR = rhs.fR; << 145 fSx = rhs.fSx; << 146 fSy = rhs.fSy; << 147 fSz = rhs.fSz; << 148 fZMidCut = rhs.fZMidCut; << 149 fZDimCut = rhs.fZDimCut; << 150 fQ1 = rhs.fQ1; << 151 fQ2 = rhs.fQ2; << 152 << 153 fCubicVolume = rhs.fCubicVolume; << 154 fSurfaceArea = rhs.fSurfaceArea; << 155 fLateralArea = rhs.fLateralArea; << 156 << 157 fRebuildPolyhedron = false; << 158 delete fpPolyhedron; fpPolyhedron = nullptr << 159 168 160 return *this; 169 return *this; 161 } 170 } 162 171 163 ////////////////////////////////////////////// << 172 //////////////////////////////////////////////////////////////////////// 164 // << 165 // Check parameters and make precalculation << 166 << 167 void G4Ellipsoid::CheckParameters() << 168 { << 169 halfTolerance = 0.5 * kCarTolerance; // half << 170 G4double dmin = 2. * kCarTolerance; << 171 << 172 // Check dimensions << 173 // << 174 if (fDx < dmin || fDy < dmin || fDz < dmin) << 175 { << 176 std::ostringstream message; << 177 message << "Invalid (too small or negative << 178 << GetName() << "\n" << 179 << " semi-axis x: " << fDx << "\n << 180 << " semi-axis y: " << fDy << "\n << 181 << " semi-axis z: " << fDz; << 182 G4Exception("G4Ellipsoid::CheckParameters( << 183 FatalException, message); << 184 } << 185 G4double A = fDx; << 186 G4double B = fDy; << 187 G4double C = fDz; << 188 << 189 // Check cuts << 190 // << 191 if (fZBottomCut == 0. && fZTopCut == 0.) << 192 { << 193 fZBottomCut = -C; << 194 fZTopCut = C; << 195 } << 196 if (fZBottomCut >= C || fZTopCut <= -C || fZ << 197 { << 198 std::ostringstream message; << 199 message << "Invalid Z cuts for Solid: " << 200 << GetName() << "\n" << 201 << " bottom cut: " << fZBottomCut << 202 << " top cut: " << fZTopCut; << 203 G4Exception("G4Ellipsoid::CheckParameters( << 204 FatalException, message); << 205 << 206 } << 207 fZBottomCut = std::max(fZBottomCut, -C); << 208 fZTopCut = std::min(fZTopCut, C); << 209 << 210 // Set extent in x and y << 211 fXmax = A; << 212 fYmax = B; << 213 if (fZBottomCut > 0.) << 214 { << 215 G4double ratio = fZBottomCut / C; << 216 G4double scale = std::sqrt((1. - ratio) * << 217 fXmax *= scale; << 218 fYmax *= scale; << 219 } << 220 if (fZTopCut < 0.) << 221 { << 222 G4double ratio = fZTopCut / C; << 223 G4double scale = std::sqrt((1. - ratio) * << 224 fXmax *= scale; << 225 fYmax *= scale; << 226 } << 227 << 228 // Set scale factors << 229 fRsph = std::max(std::max(A, B), C); // boun << 230 fR = std::min(std::min(A, B), C); // radi << 231 fSx = fR / A; // X scale factor << 232 fSy = fR / B; // Y scale factor << 233 fSz = fR / C; // Z scale factor << 234 << 235 // Scaled cuts << 236 fZMidCut = 0.5 * (fZTopCut + fZBottomCut) * << 237 fZDimCut = 0.5 * (fZTopCut - fZBottomCut) * << 238 << 239 // Coefficients for approximation of distanc << 240 fQ1 = 0.5 / fR; << 241 fQ2 = 0.5 * fR + halfTolerance * halfToleran << 242 << 243 fCubicVolume = 0.; // volume << 244 fSurfaceArea = 0.; // surface area << 245 fLateralArea = 0.; // lateral surface area << 246 } << 247 << 248 ////////////////////////////////////////////// << 249 // 173 // 250 // Dispatch to parameterisation for replicatio 174 // Dispatch to parameterisation for replication mechanism dimension 251 // computation & modification << 175 // computation & modification. 252 176 253 void G4Ellipsoid::ComputeDimensions(G4VPVParam 177 void G4Ellipsoid::ComputeDimensions(G4VPVParameterisation* p, 254 const G4in 178 const G4int n, 255 const G4VP 179 const G4VPhysicalVolume* pRep) 256 { 180 { 257 p->ComputeDimensions(*this,n,pRep); 181 p->ComputeDimensions(*this,n,pRep); 258 } 182 } 259 183 260 ////////////////////////////////////////////// << 184 /////////////////////////////////////////////////////////////////////////////// 261 // 185 // 262 // Get bounding box << 186 // Calculate extent under transform and specified limit 263 << 264 void G4Ellipsoid::BoundingLimits(G4ThreeVector << 265 G4ThreeVector << 266 { << 267 pMin.set(-fXmax,-fYmax, fZBottomCut); << 268 pMax.set( fXmax, fYmax, fZTopCut); << 269 } << 270 << 271 ////////////////////////////////////////////// << 272 // << 273 // Calculate extent under transform and specif << 274 187 275 G4bool 188 G4bool 276 G4Ellipsoid::CalculateExtent(const EAxis pAxis 189 G4Ellipsoid::CalculateExtent(const EAxis pAxis, 277 const G4VoxelLimi 190 const G4VoxelLimits& pVoxelLimit, 278 const G4AffineTra 191 const G4AffineTransform& pTransform, 279 G4double& p 192 G4double& pMin, G4double& pMax) const 280 { 193 { 281 G4ThreeVector bmin, bmax; << 194 if (!pTransform.IsRotated()) >> 195 { >> 196 // Special case handling for unrotated solid ellipsoid >> 197 // Compute x/y/z mins and maxs for bounding box respecting limits, >> 198 // with early returns if outside limits. Then switch() on pAxis, >> 199 // and compute exact x and y limit for x/y case >> 200 >> 201 G4double xoffset,xMin,xMax; >> 202 G4double yoffset,yMin,yMax; >> 203 G4double zoffset,zMin,zMax; >> 204 >> 205 G4double maxDiff,newMin,newMax; >> 206 G4double xoff,yoff; >> 207 >> 208 xoffset=pTransform.NetTranslation().x(); >> 209 xMin=xoffset - xSemiAxis; >> 210 xMax=xoffset + xSemiAxis; >> 211 if (pVoxelLimit.IsXLimited()) >> 212 { >> 213 if ( (xMin>pVoxelLimit.GetMaxXExtent()+kCarTolerance) >> 214 || (xMax<pVoxelLimit.GetMinXExtent()-kCarTolerance) ) >> 215 { >> 216 return false; >> 217 } >> 218 else >> 219 { >> 220 if (xMin<pVoxelLimit.GetMinXExtent()) >> 221 { >> 222 xMin=pVoxelLimit.GetMinXExtent(); >> 223 } >> 224 if (xMax>pVoxelLimit.GetMaxXExtent()) >> 225 { >> 226 xMax=pVoxelLimit.GetMaxXExtent(); >> 227 } >> 228 } >> 229 } >> 230 >> 231 yoffset=pTransform.NetTranslation().y(); >> 232 yMin=yoffset - ySemiAxis; >> 233 yMax=yoffset + ySemiAxis; >> 234 if (pVoxelLimit.IsYLimited()) >> 235 { >> 236 if ( (yMin>pVoxelLimit.GetMaxYExtent()+kCarTolerance) >> 237 || (yMax<pVoxelLimit.GetMinYExtent()-kCarTolerance) ) >> 238 { >> 239 return false; >> 240 } >> 241 else >> 242 { >> 243 if (yMin<pVoxelLimit.GetMinYExtent()) >> 244 { >> 245 yMin=pVoxelLimit.GetMinYExtent(); >> 246 } >> 247 if (yMax>pVoxelLimit.GetMaxYExtent()) >> 248 { >> 249 yMax=pVoxelLimit.GetMaxYExtent(); >> 250 } >> 251 } >> 252 } 282 253 283 // Get bounding box << 254 zoffset=pTransform.NetTranslation().z(); 284 BoundingLimits(bmin,bmax); << 255 zMin=zoffset + (-zSemiAxis > zBottomCut ? -zSemiAxis : zBottomCut); >> 256 zMax=zoffset + ( zSemiAxis < zTopCut ? zSemiAxis : zTopCut); >> 257 if (pVoxelLimit.IsZLimited()) >> 258 { >> 259 if ( (zMin>pVoxelLimit.GetMaxZExtent()+kCarTolerance) >> 260 || (zMax<pVoxelLimit.GetMinZExtent()-kCarTolerance) ) >> 261 { >> 262 return false; >> 263 } >> 264 else >> 265 { >> 266 if (zMin<pVoxelLimit.GetMinZExtent()) >> 267 { >> 268 zMin=pVoxelLimit.GetMinZExtent(); >> 269 } >> 270 if (zMax>pVoxelLimit.GetMaxZExtent()) >> 271 { >> 272 zMax=pVoxelLimit.GetMaxZExtent(); >> 273 } >> 274 } >> 275 } 285 276 286 // Find extent << 277 // if here, then known to cut bounding box around ellipsoid 287 G4BoundingEnvelope bbox(bmin,bmax); << 278 // 288 return bbox.CalculateExtent(pAxis,pVoxelLimi << 279 xoff = (xoffset < xMin) ? (xMin-xoffset) 289 } << 280 : (xoffset > xMax) ? (xoffset-xMax) : 0.0; >> 281 yoff = (yoffset < yMin) ? (yMin-yoffset) >> 282 : (yoffset > yMax) ? (yoffset-yMax) : 0.0; >> 283 >> 284 // detailed calculations >> 285 // NOTE: does not use X or Y offsets to adjust Z range, >> 286 // and does not use Z offset to adjust X or Y range, >> 287 // which is consistent with G4Sphere::CalculateExtent behavior >> 288 // >> 289 switch (pAxis) >> 290 { >> 291 case kXAxis: >> 292 if (yoff==0.) >> 293 { >> 294 // YZ limits cross max/min x => no change >> 295 // >> 296 pMin=xMin; >> 297 pMax=xMax; >> 298 } >> 299 else >> 300 { >> 301 // YZ limits don't cross max/min x => compute max delta x, >> 302 // hence new mins/maxs >> 303 // >> 304 maxDiff= 1.0-sqr(yoff/ySemiAxis); >> 305 if (maxDiff < 0.0) { return false; } >> 306 maxDiff= xSemiAxis * std::sqrt(maxDiff); >> 307 newMin=xoffset-maxDiff; >> 308 newMax=xoffset+maxDiff; >> 309 pMin=(newMin<xMin) ? xMin : newMin; >> 310 pMax=(newMax>xMax) ? xMax : newMax; >> 311 } >> 312 break; >> 313 case kYAxis: >> 314 if (xoff==0.) >> 315 { >> 316 // XZ limits cross max/min y => no change >> 317 // >> 318 pMin=yMin; >> 319 pMax=yMax; >> 320 } >> 321 else >> 322 { >> 323 // XZ limits don't cross max/min y => compute max delta y, >> 324 // hence new mins/maxs >> 325 // >> 326 maxDiff= 1.0-sqr(xoff/xSemiAxis); >> 327 if (maxDiff < 0.0) { return false; } >> 328 maxDiff= ySemiAxis * std::sqrt(maxDiff); >> 329 newMin=yoffset-maxDiff; >> 330 newMax=yoffset+maxDiff; >> 331 pMin=(newMin<yMin) ? yMin : newMin; >> 332 pMax=(newMax>yMax) ? yMax : newMax; >> 333 } >> 334 break; >> 335 case kZAxis: >> 336 pMin=zMin; >> 337 pMax=zMax; >> 338 break; >> 339 default: >> 340 break; >> 341 } >> 342 >> 343 pMin-=kCarTolerance; >> 344 pMax+=kCarTolerance; >> 345 return true; >> 346 } >> 347 else // not rotated >> 348 { >> 349 G4int i,j,noEntries,noBetweenSections; >> 350 G4bool existsAfterClip=false; 290 351 291 ////////////////////////////////////////////// << 352 // Calculate rotated vertex coordinates 292 // << 293 // Return position of point: inside/outside/on << 294 353 295 EInside G4Ellipsoid::Inside(const G4ThreeVecto << 354 G4int noPolygonVertices=0; 296 { << 355 G4ThreeVectorList* vertices = 297 G4double x = p.x() * fSx; << 356 CreateRotatedVertices(pTransform,noPolygonVertices); 298 G4double y = p.y() * fSy; << 357 299 G4double z = p.z() * fSz; << 358 pMin=+kInfinity; 300 G4double rr = x * x + y * y + z * z; << 359 pMax=-kInfinity; 301 G4double distZ = std::abs(z - fZMidCut) - fZ << 360 302 G4double distR = fQ1 * rr - fQ2; << 361 noEntries=vertices->size(); // noPolygonVertices*noPhiCrossSections 303 G4double dist = std::max(distZ, distR); << 362 noBetweenSections=noEntries-noPolygonVertices; >> 363 >> 364 G4ThreeVectorList ThetaPolygon; >> 365 for (i=0;i<noEntries;i+=noPolygonVertices) >> 366 { >> 367 for(j=0;j<(noPolygonVertices/2)-1;j++) >> 368 { >> 369 ThetaPolygon.push_back((*vertices)[i+j]); >> 370 ThetaPolygon.push_back((*vertices)[i+j+1]); >> 371 ThetaPolygon.push_back((*vertices)[i+noPolygonVertices-2-j]); >> 372 ThetaPolygon.push_back((*vertices)[i+noPolygonVertices-1-j]); >> 373 CalculateClippedPolygonExtent(ThetaPolygon,pVoxelLimit,pAxis,pMin,pMax); >> 374 ThetaPolygon.clear(); >> 375 } >> 376 } >> 377 for (i=0;i<noBetweenSections;i+=noPolygonVertices) >> 378 { >> 379 for(j=0;j<noPolygonVertices-1;j++) >> 380 { >> 381 ThetaPolygon.push_back((*vertices)[i+j]); >> 382 ThetaPolygon.push_back((*vertices)[i+j+1]); >> 383 ThetaPolygon.push_back((*vertices)[i+noPolygonVertices+j+1]); >> 384 ThetaPolygon.push_back((*vertices)[i+noPolygonVertices+j]); >> 385 CalculateClippedPolygonExtent(ThetaPolygon,pVoxelLimit,pAxis,pMin,pMax); >> 386 ThetaPolygon.clear(); >> 387 } >> 388 ThetaPolygon.push_back((*vertices)[i+noPolygonVertices-1]); >> 389 ThetaPolygon.push_back((*vertices)[i]); >> 390 ThetaPolygon.push_back((*vertices)[i+noPolygonVertices]); >> 391 ThetaPolygon.push_back((*vertices)[i+2*noPolygonVertices-1]); >> 392 CalculateClippedPolygonExtent(ThetaPolygon,pVoxelLimit,pAxis,pMin,pMax); >> 393 ThetaPolygon.clear(); >> 394 } >> 395 if ( (pMin!=kInfinity) || (pMax!=-kInfinity) ) >> 396 { >> 397 existsAfterClip=true; >> 398 >> 399 // Add 2*tolerance to avoid precision troubles >> 400 // >> 401 pMin-=kCarTolerance; >> 402 pMax+=kCarTolerance; 304 403 305 if (dist > halfTolerance) return kOutside; << 404 } 306 return (dist > -halfTolerance) ? kSurface : << 405 else >> 406 { >> 407 // Check for case where completely enveloping clipping volume >> 408 // If point inside then we are confident that the solid completely >> 409 // envelopes the clipping volume. Hence set min/max extents according >> 410 // to clipping volume extents along the specified axis. >> 411 // >> 412 G4ThreeVector >> 413 clipCentre((pVoxelLimit.GetMinXExtent()+pVoxelLimit.GetMaxXExtent())*0.5, >> 414 (pVoxelLimit.GetMinYExtent()+pVoxelLimit.GetMaxYExtent())*0.5, >> 415 (pVoxelLimit.GetMinZExtent()+pVoxelLimit.GetMaxZExtent())*0.5); >> 416 >> 417 if (Inside(pTransform.Inverse().TransformPoint(clipCentre))!=kOutside) >> 418 { >> 419 existsAfterClip=true; >> 420 pMin=pVoxelLimit.GetMinExtent(pAxis); >> 421 pMax=pVoxelLimit.GetMaxExtent(pAxis); >> 422 } >> 423 } >> 424 delete vertices; >> 425 return existsAfterClip; >> 426 } 307 } 427 } 308 428 309 ////////////////////////////////////////////// << 429 /////////////////////////////////////////////////////////////////////////////// 310 // 430 // 311 // Return unit normal to surface at p << 431 // Return whether point inside/outside/on surface >> 432 // Split into radius, phi, theta checks >> 433 // Each check modifies `in', or returns as approprate 312 434 313 G4ThreeVector G4Ellipsoid::SurfaceNormal( cons << 435 EInside G4Ellipsoid::Inside(const G4ThreeVector& p) const 314 { 436 { 315 G4ThreeVector norm(0., 0., 0.); << 437 G4double rad2oo, // outside surface outer tolerance 316 G4int nsurf = 0; << 438 rad2oi; // outside surface inner tolerance >> 439 EInside in; 317 440 318 // Check cuts << 441 // check this side of z cut first, because that's fast 319 G4double x = p.x() * fSx; << 442 // 320 G4double y = p.y() * fSy; << 443 if (p.z() < zBottomCut-halfRadTolerance) { return in=kOutside; } 321 G4double z = p.z() * fSz; << 444 if (p.z() > zTopCut+halfRadTolerance) { return in=kOutside; } 322 G4double distZ = std::abs(z - fZMidCut) - fZ << 323 if (std::abs(distZ) <= halfTolerance) << 324 { << 325 norm.setZ(std::copysign(1., z - fZMidCut)) << 326 ++nsurf; << 327 } << 328 445 329 // Check lateral surface << 446 rad2oo= sqr(p.x()/(xSemiAxis+halfRadTolerance)) 330 G4double distR = fQ1*(x*x + y*y + z*z) - fQ2 << 447 + sqr(p.y()/(ySemiAxis+halfRadTolerance)) 331 if (std::abs(distR) <= halfTolerance) << 448 + sqr(p.z()/(zSemiAxis+halfRadTolerance)); >> 449 >> 450 if (rad2oo > 1.0) { return in=kOutside; } >> 451 >> 452 rad2oi= sqr(p.x()*(1.0+halfRadTolerance/xSemiAxis)/xSemiAxis) >> 453 + sqr(p.y()*(1.0+halfRadTolerance/ySemiAxis)/ySemiAxis) >> 454 + sqr(p.z()*(1.0+halfRadTolerance/zSemiAxis)/zSemiAxis); >> 455 >> 456 // Check radial surfaces >> 457 // sets `in' (already checked for rad2oo > 1.0) >> 458 // >> 459 if (rad2oi < 1.0) 332 { 460 { 333 // normal = (p.x/a^2, p.y/b^2, p.z/c^2) << 461 in = ( (p.z() < zBottomCut+halfRadTolerance) 334 norm += G4ThreeVector(x*fSx, y*fSy, z*fSz) << 462 || (p.z() > zTopCut-halfRadTolerance) ) ? kSurface : kInside; 335 ++nsurf; << 463 if ( rad2oi > 1.0-halfRadTolerance ) { in=kSurface; } 336 } 464 } 337 << 465 else 338 // Return normal << 339 if (nsurf == 1) return norm; << 340 else if (nsurf > 1) return norm.unit(); // e << 341 else << 342 { 466 { 343 #ifdef G4SPECSDEBUG << 467 in = kSurface; 344 std::ostringstream message; << 345 G4long oldprc = message.precision(16); << 346 message << "Point p is not on surface (!?) << 347 << GetName() << "\n"; << 348 message << "Position:\n"; << 349 message << " p.x() = " << p.x()/mm << " << 350 message << " p.y() = " << p.y()/mm << " << 351 message << " p.z() = " << p.z()/mm << " << 352 G4cout.precision(oldprc); << 353 G4Exception("G4Ellipsoid::SurfaceNormal(p) << 354 JustWarning, message ); << 355 DumpInfo(); << 356 #endif << 357 return ApproxSurfaceNormal(p); << 358 } 468 } >> 469 return in; >> 470 359 } 471 } 360 472 361 ////////////////////////////////////////////// << 473 /////////////////////////////////////////////////////////////////////////////// 362 // 474 // 363 // Find surface nearest to point and return co << 475 // Return unit normal of surface closest to p not protected against p=0 364 // This method normally should not be called. << 365 476 366 G4ThreeVector G4Ellipsoid::ApproxSurfaceNormal << 477 G4ThreeVector G4Ellipsoid::SurfaceNormal( const G4ThreeVector& p) const 367 { 478 { 368 G4double x = p.x() * fSx; << 479 G4double distR, distZBottom, distZTop; 369 G4double y = p.y() * fSy; << 480 370 G4double z = p.z() * fSz; << 481 // normal vector with special magnitude: parallel to normal, units 1/length 371 G4double rr = x * x + y * y + z * z; << 482 // norm*p == 1.0 if on surface, >1.0 if outside, <1.0 if inside 372 G4double distZ = std::abs(z - fZMidCut) - fZ << 483 // 373 G4double distR = std::sqrt(rr) - fR; << 484 G4ThreeVector norm(p.x()/(xSemiAxis*xSemiAxis), 374 if (distR > distZ && rr > 0.) // distR > di << 485 p.y()/(ySemiAxis*ySemiAxis), 375 return G4ThreeVector(x*fSx, y*fSy, z*fSz). << 486 p.z()/(zSemiAxis*zSemiAxis)); 376 else << 487 G4double radius = 1.0/norm.mag(); 377 return { 0., 0., std::copysign(1., z - fZM << 488 >> 489 // approximate distance to curved surface >> 490 // >> 491 distR = std::fabs( (p*norm - 1.0) * radius ) / 2.0; >> 492 >> 493 // Distance to z-cut plane >> 494 // >> 495 distZBottom = std::fabs( p.z() - zBottomCut ); >> 496 distZTop = std::fabs( p.z() - zTopCut ); >> 497 >> 498 if ( (distZBottom < distR) || (distZTop < distR) ) >> 499 { >> 500 return G4ThreeVector(0.,0.,(distZBottom < distZTop) ? -1.0 : 1.0); >> 501 } >> 502 return ( norm *= radius ); 378 } 503 } 379 504 380 ////////////////////////////////////////////// << 505 /////////////////////////////////////////////////////////////////////////////// >> 506 // >> 507 // Calculate distance to shape from outside, along normalised vector >> 508 // - return kInfinity if no intersection, or intersection distance <= tolerance 381 // 509 // 382 // Calculate distance to shape from outside al << 383 510 384 G4double G4Ellipsoid::DistanceToIn(const G4Thr << 511 G4double G4Ellipsoid::DistanceToIn( const G4ThreeVector& p, 385 const G4Thr << 512 const G4ThreeVector& v ) const 386 { 513 { 387 G4double offset = 0.; << 514 G4double distMin = std::min(xSemiAxis,ySemiAxis); 388 G4ThreeVector pcur = p; << 515 const G4double dRmax = 100.*std::min(distMin,zSemiAxis); 389 << 516 distMin= kInfinity; 390 // Check if point is flying away, relative t << 391 // << 392 G4double safex = std::abs(p.x()) - fXmax; << 393 G4double safey = std::abs(p.y()) - fYmax; << 394 G4double safet = p.z() - fZTopCut; << 395 G4double safeb = fZBottomCut - p.z(); << 396 517 397 if (safex >= -halfTolerance && p.x() * v.x() << 518 // check to see if Z plane is relevant 398 if (safey >= -halfTolerance && p.y() * v.y() << 519 if (p.z() <= zBottomCut+halfCarTolerance) 399 if (safet >= -halfTolerance && v.z() >= 0.) << 520 { 400 if (safeb >= -halfTolerance && v.z() <= 0.) << 521 if (v.z() <= 0.0) { return distMin; } >> 522 G4double distZ = (zBottomCut - p.z()) / v.z(); 401 523 402 // Relocate point, if required << 524 if ( (distZ > -halfRadTolerance) && (Inside(p+distZ*v) != kOutside) ) 403 // << 525 { 404 G4double safe = std::max(std::max(std::max(s << 526 // early exit since can't intercept curved surface if we reach here 405 if (safe > 32. * fRsph) << 527 if ( std::fabs(distZ) < halfRadTolerance ) { distZ=0.; } >> 528 return distMin= distZ; >> 529 } >> 530 } >> 531 if (p.z() >= zTopCut-halfCarTolerance) 406 { 532 { 407 offset = (1. - 1.e-08) * safe - 2. * fRsph << 533 if (v.z() >= 0.0) { return distMin;} 408 pcur += offset * v; << 534 G4double distZ = (zTopCut - p.z()) / v.z(); 409 G4double dist = DistanceToIn(pcur, v); << 535 if ( (distZ > -halfRadTolerance) && (Inside(p+distZ*v) != kOutside) ) 410 return (dist == kInfinity) ? kInfinity : d << 536 { >> 537 // early exit since can't intercept curved surface if we reach here >> 538 if ( std::fabs(distZ) < halfRadTolerance ) { distZ=0.; } >> 539 return distMin= distZ; >> 540 } 411 } 541 } >> 542 // if fZCut1 <= p.z() <= fZCut2, then must hit curved surface 412 543 413 // Scale ellipsoid to sphere << 544 // now check curved surface intercept 414 // << 545 G4double A,B,C; 415 G4double px = pcur.x() * fSx; << 416 G4double py = pcur.y() * fSy; << 417 G4double pz = pcur.z() * fSz; << 418 G4double vx = v.x() * fSx; << 419 G4double vy = v.y() * fSy; << 420 G4double vz = v.z() * fSz; << 421 546 422 // Check if point is leaving the solid << 547 A= sqr(v.x()/xSemiAxis) + sqr(v.y()/ySemiAxis) + sqr(v.z()/zSemiAxis); 423 // << 548 C= sqr(p.x()/xSemiAxis) + sqr(p.y()/ySemiAxis) + sqr(p.z()/zSemiAxis) - 1.0; 424 G4double dzcut = fZDimCut; << 549 B= 2.0 * ( p.x()*v.x()/(xSemiAxis*xSemiAxis) 425 G4double pzcut = pz - fZMidCut; << 550 + p.y()*v.y()/(ySemiAxis*ySemiAxis) 426 G4double distZ = std::abs(pzcut) - dzcut; << 551 + p.z()*v.z()/(zSemiAxis*zSemiAxis) ); 427 if (distZ >= -halfTolerance && pzcut * vz >= << 552 >> 553 C= B*B - 4.0*A*C; >> 554 if (C > 0.0) >> 555 { >> 556 G4double distR= (-B - std::sqrt(C)) / (2.0*A); >> 557 G4double intZ = p.z()+distR*v.z(); >> 558 if ( (distR > halfRadTolerance) >> 559 && (intZ >= zBottomCut-halfRadTolerance) >> 560 && (intZ <= zTopCut+halfRadTolerance) ) >> 561 { >> 562 distMin = distR; >> 563 } >> 564 else if( (distR >- halfRadTolerance) >> 565 && (intZ >= zBottomCut-halfRadTolerance) >> 566 && (intZ <= zTopCut+halfRadTolerance) ) >> 567 { >> 568 // p is on the curved surface, DistanceToIn returns 0 or kInfinity: >> 569 // DistanceToIn returns 0, if second root is positive (means going inside) >> 570 // If second root is negative, DistanceToIn returns kInfinity (outside) >> 571 // >> 572 distR = (-B + std::sqrt(C) ) / (2.0*A); >> 573 if(distR>0.) { distMin=0.; } >> 574 } >> 575 else >> 576 { >> 577 distR= (-B + std::sqrt(C)) / (2.0*A); >> 578 intZ = p.z()+distR*v.z(); >> 579 if ( (distR > halfRadTolerance) >> 580 && (intZ >= zBottomCut-halfRadTolerance) >> 581 && (intZ <= zTopCut+halfRadTolerance) ) >> 582 { >> 583 G4ThreeVector norm=SurfaceNormal(p); >> 584 if (norm.dot(v)<0.) { distMin = distR; } >> 585 } >> 586 } >> 587 if ( (distMin!=kInfinity) && (distMin>dRmax) ) >> 588 { // Avoid rounding errors due to precision issues on >> 589 // 64 bits systems. Split long distances and recompute >> 590 G4double fTerm = distMin-std::fmod(distMin,dRmax); >> 591 distMin = fTerm + DistanceToIn(p+fTerm*v,v); >> 592 } >> 593 } >> 594 >> 595 if (std::fabs(distMin)<halfRadTolerance) { distMin=0.; } >> 596 return distMin; >> 597 } 428 598 429 G4double rr = px * px + py * py + pz * pz; << 599 /////////////////////////////////////////////////////////////////////////////// 430 G4double pv = px * vx + py * vy + pz * vz; << 600 // 431 G4double distR = fQ1 * rr - fQ2; << 601 // Calculate distance (<= actual) to closest surface of shape from outside 432 if (distR >= -halfTolerance && pv >= 0.) ret << 602 // - Return 0 if point inside 433 603 434 G4double A = vx * vx + vy * vy + vz * vz; << 604 G4double G4Ellipsoid::DistanceToIn(const G4ThreeVector& p) const 435 G4double B = pv; << 605 { 436 G4double C = rr - fR * fR; << 606 G4double distR, distZ; 437 G4double D = B * B - A * C; << 438 // scratch^2 = R^2 - (R - halfTolerance)^2 = << 439 G4double EPS = A * A * fR * kCarTolerance; / << 440 if (D <= EPS) return kInfinity; // no inters << 441 607 442 // Find intersection with Z planes << 608 // normal vector: parallel to normal, magnitude 1/(characteristic radius) 443 // 609 // 444 G4double invz = (vz == 0) ? DBL_MAX : -1./v << 610 G4ThreeVector norm(p.x()/(xSemiAxis*xSemiAxis), 445 G4double dz = std::copysign(dzcut, invz); << 611 p.y()/(ySemiAxis*ySemiAxis), 446 G4double tzmin = (pzcut - dz) * invz; << 612 p.z()/(zSemiAxis*zSemiAxis)); 447 G4double tzmax = (pzcut + dz) * invz; << 613 G4double radius= 1.0/norm.mag(); 448 614 449 // Find intersection with lateral surface << 615 // approximate distance to curved surface ( <= actual distance ) 450 // 616 // 451 G4double tmp = -B - std::copysign(std::sqrt( << 617 distR= (p*norm - 1.0) * radius / 2.0; 452 G4double t1 = tmp / A; << 453 G4double t2 = C / tmp; << 454 G4double trmin = std::min(t1, t2); << 455 G4double trmax = std::max(t1, t2); << 456 618 457 // Return distance << 619 // Distance to z-cut plane 458 // 620 // 459 G4double tmin = std::max(tzmin, trmin); << 621 distZ= zBottomCut - p.z(); 460 G4double tmax = std::min(tzmax, trmax); << 622 if (distZ < 0.0) 461 << 623 { 462 if (tmax - tmin <= halfTolerance) return kIn << 624 distZ = p.z() - zTopCut; 463 return (tmin < halfTolerance) ? offset : tmi << 625 } 464 } << 465 << 466 ////////////////////////////////////////////// << 467 // << 468 // Estimate distance to surface from outside << 469 626 470 G4double G4Ellipsoid::DistanceToIn(const G4Thr << 627 // Distance to closest surface from outside 471 { << 628 // 472 G4double px = p.x(); << 629 if (distZ < 0.0) 473 G4double py = p.y(); << 630 { 474 G4double pz = p.z(); << 631 return (distR < 0.0) ? 0.0 : distR; 475 << 632 } 476 // Safety distance to bounding box << 633 else if (distR < 0.0) 477 G4double distX = std::abs(px) - fXmax; << 634 { 478 G4double distY = std::abs(py) - fYmax; << 635 return distZ; 479 G4double distZ = std::max(pz - fZTopCut, fZB << 636 } 480 G4double distB = std::max(std::max(distX, di << 637 else 481 << 638 { 482 // Safety distance to lateral surface << 639 return (distZ < distR) ? distZ : distR; 483 G4double x = px * fSx; << 640 } 484 G4double y = py * fSy; << 485 G4double z = pz * fSz; << 486 G4double distR = std::sqrt(x*x + y*y + z*z) << 487 << 488 // Return safety to in << 489 G4double dist = std::max(distB, distR); << 490 return (dist < 0.) ? 0. : dist; << 491 } 641 } 492 642 493 ////////////////////////////////////////////// << 643 /////////////////////////////////////////////////////////////////////////////// 494 // 644 // 495 // Calculate distance to surface from inside a << 645 // Calculate distance to surface of shape from `inside', allowing for tolerance 496 646 497 G4double G4Ellipsoid::DistanceToOut(const G4Th 647 G4double G4Ellipsoid::DistanceToOut(const G4ThreeVector& p, 498 const G4Th 648 const G4ThreeVector& v, 499 const G4bo 649 const G4bool calcNorm, 500 G4bo << 650 G4bool *validNorm, 501 G4Th << 651 G4ThreeVector *n ) const 502 { 652 { 503 // Check if point flying away relative to Z << 653 G4double distMin; >> 654 enum surface_e {kPlaneSurf, kCurvedSurf, kNoSurf} surface; >> 655 >> 656 distMin= kInfinity; >> 657 surface= kNoSurf; >> 658 >> 659 // check to see if Z plane is relevant 504 // 660 // 505 G4double pz = p.z() * fSz; << 661 if (v.z() < 0.0) 506 G4double vz = v.z() * fSz; << 507 G4double dzcut = fZDimCut; << 508 G4double pzcut = pz - fZMidCut; << 509 G4double distZ = std::abs(pzcut) - dzcut; << 510 if (distZ >= -halfTolerance && pzcut * vz > << 511 { 662 { 512 if (calcNorm) << 663 G4double distZ = (zBottomCut - p.z()) / v.z(); >> 664 if (distZ < 0.0) 513 { 665 { 514 *validNorm = true; << 666 distZ= 0.0; 515 n->set(0., 0., std::copysign(1., pzcut)) << 667 if (!calcNorm) {return 0.0;} 516 } 668 } 517 return 0.; << 669 distMin= distZ; >> 670 surface= kPlaneSurf; 518 } 671 } 519 << 672 if (v.z() > 0.0) 520 // Check if point is flying away relative to << 521 // << 522 G4double px = p.x() * fSx; << 523 G4double py = p.y() * fSy; << 524 G4double vx = v.x() * fSx; << 525 G4double vy = v.y() * fSy; << 526 G4double rr = px * px + py * py + pz * pz; << 527 G4double pv = px * vx + py * vy + pz * vz; << 528 G4double distR = fQ1 * rr - fQ2; << 529 if (distR >= -halfTolerance && pv > 0.) << 530 { 673 { 531 if (calcNorm) << 674 G4double distZ = (zTopCut - p.z()) / v.z(); >> 675 if (distZ < 0.0) 532 { 676 { 533 *validNorm = true; << 677 distZ= 0.0; 534 *n = G4ThreeVector(px*fSx, py*fSy, pz*fS << 678 if (!calcNorm) {return 0.0;} 535 } 679 } 536 return 0.; << 680 distMin= distZ; >> 681 surface= kPlaneSurf; 537 } 682 } 538 683 539 // Just in case check if point is outside (n << 684 // normal vector: parallel to normal, magnitude 1/(characteristic radius) >> 685 // >> 686 G4ThreeVector nearnorm(p.x()/(xSemiAxis*xSemiAxis), >> 687 p.y()/(ySemiAxis*ySemiAxis), >> 688 p.z()/(zSemiAxis*zSemiAxis)); >> 689 >> 690 // now check curved surface intercept 540 // 691 // 541 if (std::max(distZ, distR) > halfTolerance) << 692 G4double A,B,C; >> 693 >> 694 A= sqr(v.x()/xSemiAxis) + sqr(v.y()/ySemiAxis) + sqr(v.z()/zSemiAxis); >> 695 C= (p * nearnorm) - 1.0; >> 696 B= 2.0 * (v * nearnorm); >> 697 >> 698 C= B*B - 4.0*A*C; >> 699 if (C > 0.0) 542 { 700 { 543 #ifdef G4SPECSDEBUG << 701 G4double distR= (-B + std::sqrt(C) ) / (2.0*A); 544 std::ostringstream message; << 702 if (distR < 0.0) 545 G4long oldprc = message.precision(16); << 546 message << "Point p is outside (!?) of sol << 547 << GetName() << G4endl; << 548 message << "Position: " << p << G4endl;; << 549 message << "Direction: " << v; << 550 G4cout.precision(oldprc); << 551 G4Exception("G4Ellipsoid::DistanceToOut(p, << 552 JustWarning, message ); << 553 DumpInfo(); << 554 #endif << 555 if (calcNorm) << 556 { 703 { 557 *validNorm = true; << 704 distR= 0.0; 558 *n = ApproxSurfaceNormal(p); << 705 if (!calcNorm) {return 0.0;} >> 706 } >> 707 if (distR < distMin) >> 708 { >> 709 distMin= distR; >> 710 surface= kCurvedSurf; 559 } 711 } 560 return 0.; << 561 } 712 } 562 713 563 // Set coefficients of quadratic equation: A << 714 // set normal if requested 564 // 715 // 565 G4double A = vx * vx + vy * vy + vz * vz; << 716 if (calcNorm) 566 G4double B = pv; << 567 G4double C = rr - fR * fR; << 568 G4double D = B * B - A * C; << 569 // It is expected that the point is located << 570 // max term in the expression for discrimina << 571 // max calculation error can be derived as f << 572 // A * (1 + 2e) * R^2 * (1 + 2e) = A * R^2 + << 573 G4double EPS = 4. * A * fR * fR * DBL_EPSILO << 574 << 575 if (D <= EPS) // no intersection << 576 { 717 { 577 if (calcNorm) << 718 if (surface == kNoSurf) >> 719 { >> 720 *validNorm = false; >> 721 } >> 722 else 578 { 723 { 579 *validNorm = true; 724 *validNorm = true; 580 *n = G4ThreeVector(px*fSx, py*fSy, pz*fS << 725 switch (surface) >> 726 { >> 727 case kPlaneSurf: >> 728 *n= G4ThreeVector(0.,0.,(v.z() > 0.0 ? 1. : -1.)); >> 729 break; >> 730 case kCurvedSurf: >> 731 { >> 732 G4ThreeVector pexit= p + distMin*v; >> 733 G4ThreeVector truenorm(pexit.x()/(xSemiAxis*xSemiAxis), >> 734 pexit.y()/(ySemiAxis*ySemiAxis), >> 735 pexit.z()/(zSemiAxis*zSemiAxis)); >> 736 truenorm *= 1.0/truenorm.mag(); >> 737 *n= truenorm; >> 738 } break; >> 739 default: // Should never reach this case ... >> 740 DumpInfo(); >> 741 std::ostringstream message; >> 742 G4int oldprc = message.precision(16); >> 743 message << "Undefined side for valid surface normal to solid." >> 744 << G4endl >> 745 << "Position:" << G4endl >> 746 << " p.x() = " << p.x()/mm << " mm" << G4endl >> 747 << " p.y() = " << p.y()/mm << " mm" << G4endl >> 748 << " p.z() = " << p.z()/mm << " mm" << G4endl >> 749 << "Direction:" << G4endl << G4endl >> 750 << " v.x() = " << v.x() << G4endl >> 751 << " v.y() = " << v.y() << G4endl >> 752 << " v.z() = " << v.z() << G4endl >> 753 << "Proposed distance :" << G4endl >> 754 << " distMin = " << distMin/mm << " mm"; >> 755 message.precision(oldprc); >> 756 G4Exception("G4Ellipsoid::DistanceToOut(p,v,..)", >> 757 "GeomSolids1002", JustWarning, message); >> 758 break; >> 759 } 581 } 760 } 582 return 0.; << 583 } 761 } >> 762 >> 763 return distMin; >> 764 } >> 765 >> 766 /////////////////////////////////////////////////////////////////////////////// >> 767 // >> 768 // Calculate distance (<=actual) to closest surface of shape from inside >> 769 >> 770 G4double G4Ellipsoid::DistanceToOut(const G4ThreeVector& p) const >> 771 { >> 772 G4double distR, distZ; >> 773 >> 774 #ifdef G4SPECSDEBUG >> 775 if( Inside(p) == kOutside ) >> 776 { >> 777 DumpInfo(); >> 778 std::ostringstream message; >> 779 G4int oldprc = message.precision(16); >> 780 message << "Point p is outside !?" << G4endl >> 781 << "Position:" << G4endl >> 782 << " p.x() = " << p.x()/mm << " mm" << G4endl >> 783 << " p.y() = " << p.y()/mm << " mm" << G4endl >> 784 << " p.z() = " << p.z()/mm << " mm"; >> 785 message.precision(oldprc) ; >> 786 G4Exception("G4Ellipsoid::DistanceToOut(p)", "GeomSolids1002", >> 787 JustWarning, message); >> 788 } >> 789 #endif 584 790 585 // Find intersection with Z cuts << 791 // Normal vector: parallel to normal, magnitude 1/(characteristic radius) 586 // 792 // 587 G4double tzmax = (vz == 0.) ? DBL_MAX : (std << 793 G4ThreeVector norm(p.x()/(xSemiAxis*xSemiAxis), >> 794 p.y()/(ySemiAxis*ySemiAxis), >> 795 p.z()/(zSemiAxis*zSemiAxis)); 588 796 589 // Find intersection with lateral surface << 797 // the following is a safe inlined "radius= min(1.0/norm.mag(),p.mag()) 590 // 798 // 591 G4double tmp = -B - std::copysign(std::sqrt( << 799 G4double radius= p.mag(); 592 G4double trmax = (tmp < 0.) ? C/tmp : tmp/A; << 800 G4double tmp= norm.mag(); >> 801 if ( (tmp > 0.0) && (1.0 < radius*tmp) ) {radius = 1.0/tmp;} 593 802 594 // Find distance and set normal, if required << 803 // Approximate distance to curved surface ( <= actual distance ) >> 804 // >> 805 distR = (1.0 - p*norm) * radius / 2.0; >> 806 >> 807 // Distance to z-cut plane 595 // 808 // 596 G4double tmax = std::min(tzmax, trmax); << 809 distZ = p.z() - zBottomCut; 597 //if (tmax < halfTolerance) tmax = 0.; << 810 if (distZ < 0.0) {distZ= zTopCut - p.z();} 598 811 599 if (calcNorm) << 812 // Distance to closest surface from inside >> 813 // >> 814 if ( (distZ < 0.0) || (distR < 0.0) ) 600 { 815 { 601 *validNorm = true; << 816 return 0.0; 602 if (tmax == tzmax) << 817 } 603 { << 818 else 604 G4double pznew = pz + tmax * vz; << 819 { 605 n->set(0., 0., (pznew > fZMidCut) ? 1. : << 820 return (distZ < distR) ? distZ : distR; 606 } << 607 else << 608 { << 609 G4double nx = (px + tmax * vx) * fSx; << 610 G4double ny = (py + tmax * vy) * fSy; << 611 G4double nz = (pz + tmax * vz) * fSz; << 612 *n = G4ThreeVector(nx, ny, nz).unit(); << 613 } << 614 } 821 } 615 return tmax; << 616 } 822 } 617 823 618 ////////////////////////////////////////////// << 824 /////////////////////////////////////////////////////////////////////////////// 619 // 825 // 620 // Estimate distance to surface from inside << 826 // Create a List containing the transformed vertices >> 827 // Ordering [0-3] -fDz cross section >> 828 // [4-7] +fDz cross section such that [0] is below [4], >> 829 // [1] below [5] etc. >> 830 // Note: >> 831 // Caller has deletion resposibility >> 832 // Potential improvement: For last slice, use actual ending angle >> 833 // to avoid rounding error problems. >> 834 >> 835 G4ThreeVectorList* >> 836 G4Ellipsoid::CreateRotatedVertices(const G4AffineTransform& pTransform, >> 837 G4int& noPolygonVertices) const >> 838 { >> 839 G4ThreeVectorList *vertices; >> 840 G4ThreeVector vertex; >> 841 G4double meshAnglePhi, meshRMaxFactor, >> 842 crossAnglePhi, coscrossAnglePhi, sincrossAnglePhi, sAnglePhi; >> 843 G4double meshTheta, crossTheta, startTheta; >> 844 G4double rMaxX, rMaxY, rMaxZ, rMaxMax, rx, ry, rz; >> 845 G4int crossSectionPhi, noPhiCrossSections, crossSectionTheta, noThetaSections; 621 846 622 G4double G4Ellipsoid::DistanceToOut(const G4Th << 847 // Phi cross sections 623 { << 848 // 624 // Safety distance in z direction << 849 noPhiCrossSections=G4int (twopi/kMeshAngleDefault)+1; // = 9! 625 G4double distZ = std::min(fZTopCut - p.z(), << 850 >> 851 /* >> 852 if (noPhiCrossSections<kMinMeshSections) // <3 >> 853 { >> 854 noPhiCrossSections=kMinMeshSections; >> 855 } >> 856 else if (noPhiCrossSections>kMaxMeshSections) // >37 >> 857 { >> 858 noPhiCrossSections=kMaxMeshSections; >> 859 } >> 860 */ >> 861 meshAnglePhi=twopi/(noPhiCrossSections-1); >> 862 >> 863 // Set start angle such that mesh will be at fRMax >> 864 // on the x axis. Will give better extent calculations when not rotated. >> 865 >> 866 sAnglePhi = -meshAnglePhi*0.5; >> 867 >> 868 // Theta cross sections >> 869 >> 870 noThetaSections = G4int(pi/kMeshAngleDefault)+3; // = 7! >> 871 >> 872 /* >> 873 if (noThetaSections<kMinMeshSections) // <3 >> 874 { >> 875 noThetaSections=kMinMeshSections; >> 876 } >> 877 else if (noThetaSections>kMaxMeshSections) // >37 >> 878 { >> 879 noThetaSections=kMaxMeshSections; >> 880 } >> 881 */ >> 882 meshTheta= pi/(noThetaSections-2); >> 883 >> 884 // Set start angle such that mesh will be at fRMax >> 885 // on the z axis. Will give better extent calculations when not rotated. >> 886 >> 887 startTheta = -meshTheta*0.5; >> 888 >> 889 meshRMaxFactor = 1.0/std::cos(0.5* >> 890 std::sqrt(meshAnglePhi*meshAnglePhi+meshTheta*meshTheta)); >> 891 rMaxMax= (xSemiAxis > ySemiAxis ? xSemiAxis : ySemiAxis); >> 892 if (zSemiAxis > rMaxMax) rMaxMax= zSemiAxis; >> 893 rMaxX= xSemiAxis + rMaxMax*(meshRMaxFactor-1.0); >> 894 rMaxY= ySemiAxis + rMaxMax*(meshRMaxFactor-1.0); >> 895 rMaxZ= zSemiAxis + rMaxMax*(meshRMaxFactor-1.0); >> 896 G4double* cosCrossTheta = new G4double[noThetaSections]; >> 897 G4double* sinCrossTheta = new G4double[noThetaSections]; >> 898 vertices=new G4ThreeVectorList(noPhiCrossSections*noThetaSections); >> 899 if (vertices && cosCrossTheta && sinCrossTheta) >> 900 { >> 901 for (crossSectionTheta=0; crossSectionTheta<noThetaSections; >> 902 crossSectionTheta++) >> 903 { >> 904 // Compute sine and cosine table (for historical reasons) >> 905 // >> 906 crossTheta=startTheta+crossSectionTheta*meshTheta; >> 907 cosCrossTheta[crossSectionTheta]=std::cos(crossTheta); >> 908 sinCrossTheta[crossSectionTheta]=std::sin(crossTheta); >> 909 } >> 910 for (crossSectionPhi=0; crossSectionPhi<noPhiCrossSections; >> 911 crossSectionPhi++) >> 912 { >> 913 crossAnglePhi=sAnglePhi+crossSectionPhi*meshAnglePhi; >> 914 coscrossAnglePhi=std::cos(crossAnglePhi); >> 915 sincrossAnglePhi=std::sin(crossAnglePhi); >> 916 for (crossSectionTheta=0; crossSectionTheta<noThetaSections; >> 917 crossSectionTheta++) >> 918 { >> 919 // Compute coordinates of cross section at section crossSectionPhi >> 920 // >> 921 rx= sinCrossTheta[crossSectionTheta]*coscrossAnglePhi*rMaxX; >> 922 ry= sinCrossTheta[crossSectionTheta]*sincrossAnglePhi*rMaxY; >> 923 rz= cosCrossTheta[crossSectionTheta]*rMaxZ; >> 924 if (rz < zBottomCut) >> 925 { rz= zBottomCut; } >> 926 if (rz > zTopCut) >> 927 { rz= zTopCut; } >> 928 vertex= G4ThreeVector(rx,ry,rz); >> 929 vertices->push_back(pTransform.TransformPoint(vertex)); >> 930 } // Theta forward >> 931 } // Phi >> 932 noPolygonVertices = noThetaSections ; >> 933 } >> 934 else >> 935 { >> 936 DumpInfo(); >> 937 G4Exception("G4Ellipsoid::CreateRotatedVertices()", >> 938 "GeomSolids0003", FatalException, >> 939 "Error in allocation of vertices. Out of memory !"); >> 940 } 626 941 627 // Safety distance to lateral surface << 942 delete[] cosCrossTheta; 628 G4double x = p.x() * fSx; << 943 delete[] sinCrossTheta; 629 G4double y = p.y() * fSy; << 944 630 G4double z = p.z() * fSz; << 945 return vertices; 631 G4double distR = fR - std::sqrt(x*x + y*y + << 632 << 633 // Return safety to out << 634 G4double dist = std::min(distZ, distR); << 635 return (dist < 0.) ? 0. : dist; << 636 } 946 } 637 947 638 ////////////////////////////////////////////// 948 ////////////////////////////////////////////////////////////////////////// 639 // 949 // 640 // Return entity type << 950 // G4EntityType 641 951 642 G4GeometryType G4Ellipsoid::GetEntityType() co 952 G4GeometryType G4Ellipsoid::GetEntityType() const 643 { 953 { 644 return {"G4Ellipsoid"}; << 954 return G4String("G4Ellipsoid"); 645 } 955 } 646 956 647 ////////////////////////////////////////////// 957 ////////////////////////////////////////////////////////////////////////// 648 // 958 // 649 // Make a clone of the object 959 // Make a clone of the object 650 960 651 G4VSolid* G4Ellipsoid::Clone() const 961 G4VSolid* G4Ellipsoid::Clone() const 652 { 962 { 653 return new G4Ellipsoid(*this); 963 return new G4Ellipsoid(*this); 654 } 964 } 655 965 656 ////////////////////////////////////////////// 966 ////////////////////////////////////////////////////////////////////////// 657 // 967 // 658 // Stream object contents to output stream << 968 // Stream object contents to an output stream 659 969 660 std::ostream& G4Ellipsoid::StreamInfo( std::os 970 std::ostream& G4Ellipsoid::StreamInfo( std::ostream& os ) const 661 { 971 { 662 G4long oldprc = os.precision(16); << 972 G4int oldprc = os.precision(16); 663 os << "------------------------------------- 973 os << "-----------------------------------------------------------\n" 664 << " *** Dump for solid - " << GetName 974 << " *** Dump for solid - " << GetName() << " ***\n" 665 << " ================================= 975 << " ===================================================\n" 666 << " Solid type: " << GetEntityType() << << 976 << " Solid type: G4Ellipsoid\n" 667 << " Parameters: \n" 977 << " Parameters: \n" 668 << " semi-axis x: " << GetDx()/mm << " << 978 669 << " semi-axis y: " << GetDy()/mm << " << 979 << " semi-axis x: " << xSemiAxis/mm << " mm \n" 670 << " semi-axis z: " << GetDz()/mm << " << 980 << " semi-axis y: " << ySemiAxis/mm << " mm \n" 671 << " lower cut in z: " << GetZBottomCu << 981 << " semi-axis z: " << zSemiAxis/mm << " mm \n" 672 << " upper cut in z: " << GetZTopCut() << 982 << " max semi-axis: " << semiAxisMax/mm << " mm \n" >> 983 << " lower cut plane level z: " << zBottomCut/mm << " mm \n" >> 984 << " upper cut plane level z: " << zTopCut/mm << " mm \n" 673 << "------------------------------------- 985 << "-----------------------------------------------------------\n"; 674 os.precision(oldprc); 986 os.precision(oldprc); >> 987 675 return os; 988 return os; 676 } 989 } 677 990 678 ////////////////////////////////////////////// << 991 //////////////////////////////////////////////////////////////////// 679 // 992 // 680 // Return volume << 993 // GetPointOnSurface 681 994 682 G4double G4Ellipsoid::GetCubicVolume() << 995 G4ThreeVector G4Ellipsoid::GetPointOnSurface() const 683 { 996 { 684 if (fCubicVolume == 0.) << 997 G4double aTop, aBottom, aCurved, chose, xRand, yRand, zRand, phi; >> 998 G4double cosphi, sinphi, costheta, sintheta, alpha, beta, max1, max2, max3; >> 999 >> 1000 max1 = xSemiAxis > ySemiAxis ? xSemiAxis : ySemiAxis; >> 1001 max1 = max1 > zSemiAxis ? max1 : zSemiAxis; >> 1002 if (max1 == xSemiAxis) { max2 = ySemiAxis; max3 = zSemiAxis; } >> 1003 else if (max1 == ySemiAxis) { max2 = xSemiAxis; max3 = zSemiAxis; } >> 1004 else { max2 = xSemiAxis; max3 = ySemiAxis; } >> 1005 >> 1006 phi = RandFlat::shoot(0.,twopi); >> 1007 >> 1008 cosphi = std::cos(phi); sinphi = std::sin(phi); >> 1009 costheta = RandFlat::shoot(zBottomCut,zTopCut)/zSemiAxis; >> 1010 sintheta = std::sqrt(1.-sqr(costheta)); >> 1011 >> 1012 alpha = 1.-sqr(max2/max1); beta = 1.-sqr(max3/max1); >> 1013 >> 1014 aTop = pi*xSemiAxis*ySemiAxis*(1 - sqr(zTopCut/zSemiAxis)); >> 1015 aBottom = pi*xSemiAxis*ySemiAxis*(1 - sqr(zBottomCut/zSemiAxis)); >> 1016 >> 1017 // approximation >> 1018 // from:" http://www.citr.auckland.ac.nz/techreports/2004/CITR-TR-139.pdf" >> 1019 aCurved = 4.*pi*max1*max2*(1.-1./6.*(alpha+beta)- >> 1020 1./120.*(3.*sqr(alpha)+2.*alpha*beta+3.*sqr(beta))); >> 1021 >> 1022 aCurved *= 0.5*(1.2*zTopCut/zSemiAxis - 1.2*zBottomCut/zSemiAxis); >> 1023 >> 1024 if( ( zTopCut >= zSemiAxis && zBottomCut <= -1.*zSemiAxis ) >> 1025 || ( zTopCut == 0 && zBottomCut ==0 ) ) 685 { 1026 { 686 G4double piAB_3 = CLHEP::pi * fDx * fDy / << 1027 aTop = 0; aBottom = 0; 687 fCubicVolume = 4. * piAB_3 * fDz; << 688 if (fZBottomCut > -fDz) << 689 { << 690 G4double hbot = 1. + fZBottomCut / fDz; << 691 fCubicVolume -= piAB_3 * hbot * hbot * ( << 692 } << 693 if (fZTopCut < fDz) << 694 { << 695 G4double htop = 1. - fZTopCut / fDz; << 696 fCubicVolume -= piAB_3 * htop * htop * ( << 697 } << 698 } 1028 } 699 return fCubicVolume; << 1029 700 } << 1030 chose = RandFlat::shoot(0.,aTop + aBottom + aCurved); 701 << 1031 702 ////////////////////////////////////////////// << 1032 if(chose < aCurved) 703 // << 1033 { 704 // Calculate area of lateral surface << 1034 xRand = xSemiAxis*sintheta*cosphi; 705 << 1035 yRand = ySemiAxis*sintheta*sinphi; 706 G4double G4Ellipsoid::LateralSurfaceArea() con << 1036 zRand = zSemiAxis*costheta; 707 { << 1037 return G4ThreeVector (xRand,yRand,zRand); 708 constexpr G4int NPHI = 1000.; << 709 constexpr G4double dPhi = CLHEP::halfpi/NPHI << 710 constexpr G4double eps = 4.*DBL_EPSILON; << 711 << 712 G4double aa = fDx*fDx; << 713 G4double bb = fDy*fDy; << 714 G4double cc = fDz*fDz; << 715 G4double ab = fDx*fDy; << 716 G4double cc_aa = cc/aa; << 717 G4double cc_bb = cc/bb; << 718 G4double zmax = std::min(fZTopCut, fDz); << 719 G4double zmin = std::max(fZBottomCut,-fDz); << 720 G4double zmax_c = zmax/fDz; << 721 G4double zmin_c = zmin/fDz; << 722 G4double area = 0.; << 723 << 724 if (aa == bb) // spheroid, use analytical ex << 725 { << 726 G4double k = fDz/fDx; << 727 G4double kk = k*k; << 728 if (kk < 1. - eps) << 729 { << 730 G4double invk = fDx/fDz; << 731 G4double root = std::sqrt(1. - kk); << 732 G4double tmax = zmax_c*root; << 733 G4double tmin = zmin_c*root; << 734 area = CLHEP::pi*ab* << 735 ((zmax_c*std::sqrt(kk + tmax*tmax) - z << 736 (std::asinh(tmax*invk) - std::asinh(t << 737 } << 738 else if (kk > 1. + eps) << 739 { << 740 G4double invk = fDx/fDz; << 741 G4double root = std::sqrt(kk - 1.); << 742 G4double tmax = zmax_c*root; << 743 G4double tmin = zmin_c*root; << 744 area = CLHEP::pi*ab* << 745 ((zmax_c*std::sqrt(kk - tmax*tmax) - z << 746 (std::asin(tmax*invk) - std::asin(tmi << 747 } << 748 else << 749 { << 750 area = CLHEP::twopi*fDx*(zmax - zmin); << 751 } << 752 return area; << 753 } 1038 } 754 << 1039 else if(chose >= aCurved && chose < aCurved + aTop) 755 // ellipsoid, integration along phi << 756 for (G4int i = 0; i < NPHI; ++i) << 757 { 1040 { 758 G4double sinPhi = std::sin(dPhi*(i + 0.5)) << 1041 xRand = RandFlat::shoot(-1.,1.)*xSemiAxis 759 G4double kk = cc_aa + (cc_bb - cc_aa)*sinP << 1042 * std::sqrt(1-sqr(zTopCut/zSemiAxis)); 760 if (kk < 1. - eps) << 1043 yRand = RandFlat::shoot(-1.,1.)*ySemiAxis 761 { << 1044 * std::sqrt(1.-sqr(zTopCut/zSemiAxis)-sqr(xRand/xSemiAxis)); 762 G4double root = std::sqrt(1. - kk); << 1045 zRand = zTopCut; 763 G4double tmax = zmax_c*root; << 1046 return G4ThreeVector (xRand,yRand,zRand); 764 G4double tmin = zmin_c*root; << 765 G4double invk = 1./std::sqrt(kk); << 766 area += 2.*ab*dPhi* << 767 ((zmax_c*std::sqrt(kk + tmax*tmax) - z << 768 (std::asinh(tmax*invk) - std::asinh(t << 769 } << 770 else if (kk > 1. + eps) << 771 { << 772 G4double root = std::sqrt(kk - 1.); << 773 G4double tmax = zmax_c*root; << 774 G4double tmin = zmin_c*root; << 775 G4double invk = 1./std::sqrt(kk); << 776 area += 2.*ab*dPhi* << 777 ((zmax_c*std::sqrt(kk - tmax*tmax) - z << 778 (std::asin(tmax*invk) - std::asin(tmi << 779 } << 780 else << 781 { << 782 area += 4.*ab*dPhi*(zmax_c - zmin_c); << 783 } << 784 } 1047 } 785 return area; << 1048 else 786 } << 787 << 788 ////////////////////////////////////////////// << 789 // << 790 // Return surface area << 791 << 792 G4double G4Ellipsoid::GetSurfaceArea() << 793 { << 794 if (fSurfaceArea == 0.) << 795 { 1049 { 796 G4double piAB = CLHEP::pi * fDx * fDy; << 1050 xRand = RandFlat::shoot(-1.,1.)*xSemiAxis 797 fSurfaceArea = LateralSurfaceArea(); << 1051 * std::sqrt(1-sqr(zBottomCut/zSemiAxis)); 798 if (fZBottomCut > -fDz) << 1052 yRand = RandFlat::shoot(-1.,1.)*ySemiAxis 799 { << 1053 * std::sqrt(1.-sqr(zBottomCut/zSemiAxis)-sqr(xRand/xSemiAxis)); 800 G4double hbot = 1. + fZBottomCut / fDz; << 1054 zRand = zBottomCut; 801 fSurfaceArea += piAB * hbot * (2. - hbot << 1055 return G4ThreeVector (xRand,yRand,zRand); 802 } << 803 if (fZTopCut < fDz) << 804 { << 805 G4double htop = 1. - fZTopCut / fDz; << 806 fSurfaceArea += piAB * htop * (2. - htop << 807 } << 808 } << 809 return fSurfaceArea; << 810 } << 811 << 812 ////////////////////////////////////////////// << 813 // << 814 // Return random point on surface << 815 << 816 G4ThreeVector G4Ellipsoid::GetPointOnSurface() << 817 { << 818 G4double A = GetDx(); << 819 G4double B = GetDy(); << 820 G4double C = GetDz(); << 821 G4double Zbot = GetZBottomCut(); << 822 G4double Ztop = GetZTopCut(); << 823 << 824 // Calculate cut areas << 825 G4double Hbot = 1. + Zbot / C; << 826 G4double Htop = 1. - Ztop / C; << 827 G4double piAB = CLHEP::pi * A * B; << 828 G4double Sbot = piAB * Hbot * (2. - Hbot); << 829 G4double Stop = piAB * Htop * (2. - Htop); << 830 << 831 // Get area of lateral surface << 832 if (fLateralArea == 0.) << 833 { << 834 G4AutoLock l(&lateralareaMutex); << 835 fLateralArea = LateralSurfaceArea(); << 836 l.unlock(); << 837 } << 838 G4double Slat = fLateralArea; << 839 << 840 // Select surface (0 - bottom cut, 1 - later << 841 G4double select = (Sbot + Slat + Stop) * G4Q << 842 G4int k = 0; << 843 if (select > Sbot) k = 1; << 844 if (select > Sbot + Slat) k = 2; << 845 << 846 // Pick random point on selected surface (re << 847 G4ThreeVector p; << 848 switch (k) << 849 { << 850 case 0: // bootom z-cut << 851 { << 852 G4double scale = std::sqrt(Hbot * (2. - << 853 G4TwoVector rho = G4RandomPointInEllipse << 854 p.set(rho.x(), rho.y(), Zbot); << 855 break; << 856 } << 857 case 1: // lateral surface << 858 { << 859 G4double x, y, z; << 860 G4double mu_max = std::max(std::max(A * << 861 for (G4int i = 0; i < 1000; ++i) << 862 { << 863 // generate random point on unit spher << 864 z = (Zbot + (Ztop - Zbot) * G4QuickRan << 865 G4double rho = std::sqrt((1. + z) * (1 << 866 G4double phi = CLHEP::twopi * G4QuickR << 867 x = rho * std::cos(phi); << 868 y = rho * std::sin(phi); << 869 // check acceptance << 870 G4double xbc = x * B * C; << 871 G4double yac = y * A * C; << 872 G4double zab = z * A * B; << 873 G4double mu = std::sqrt(xbc * xbc + y << 874 if (mu_max * G4QuickRand() <= mu) brea << 875 } << 876 p.set(A * x, B * y, C * z); << 877 break; << 878 } << 879 case 2: // top z-cut << 880 { << 881 G4double scale = std::sqrt(Htop * (2. - << 882 G4TwoVector rho = G4RandomPointInEllipse << 883 p.set(rho.x(), rho.y(), Ztop); << 884 break; << 885 } << 886 } 1056 } 887 return p; << 888 } 1057 } 889 1058 890 ////////////////////////////////////////////// << 1059 ///////////////////////////////////////////////////////////////////////////// 891 // 1060 // 892 // Methods for visualisation 1061 // Methods for visualisation 893 1062 894 void G4Ellipsoid::DescribeYourselfTo (G4VGraph 1063 void G4Ellipsoid::DescribeYourselfTo (G4VGraphicsScene& scene) const 895 { 1064 { 896 scene.AddSolid(*this); 1065 scene.AddSolid(*this); 897 } 1066 } 898 1067 899 ////////////////////////////////////////////// << 900 // << 901 // Return vis extent << 902 << 903 G4VisExtent G4Ellipsoid::GetExtent() const 1068 G4VisExtent G4Ellipsoid::GetExtent() const 904 { 1069 { 905 return { -fXmax, fXmax, -fYmax, fYmax, fZBot << 1070 // Define the sides of the box into which the G4Ellipsoid instance would fit. >> 1071 // >> 1072 return G4VisExtent (-semiAxisMax, semiAxisMax, >> 1073 -semiAxisMax, semiAxisMax, >> 1074 -semiAxisMax, semiAxisMax); 906 } 1075 } 907 1076 908 ////////////////////////////////////////////// << 909 // << 910 // Create polyhedron << 911 << 912 G4Polyhedron* G4Ellipsoid::CreatePolyhedron () 1077 G4Polyhedron* G4Ellipsoid::CreatePolyhedron () const 913 { 1078 { 914 return new G4PolyhedronEllipsoid(fDx, fDy, f << 1079 return new G4PolyhedronEllipsoid(xSemiAxis, ySemiAxis, zSemiAxis, >> 1080 zBottomCut, zTopCut); 915 } 1081 } 916 1082 917 ////////////////////////////////////////////// << 918 // << 919 // Return pointer to polyhedron << 920 << 921 G4Polyhedron* G4Ellipsoid::GetPolyhedron () co 1083 G4Polyhedron* G4Ellipsoid::GetPolyhedron () const 922 { 1084 { 923 if (fpPolyhedron == nullptr || << 1085 if (!fpPolyhedron || 924 fRebuildPolyhedron || << 925 fpPolyhedron->GetNumberOfRotationStepsAt 1086 fpPolyhedron->GetNumberOfRotationStepsAtTimeOfCreation() != 926 fpPolyhedron->GetNumberOfRotationSteps() 1087 fpPolyhedron->GetNumberOfRotationSteps()) 927 { 1088 { 928 G4AutoLock l(&polyhedronMutex); << 929 delete fpPolyhedron; 1089 delete fpPolyhedron; 930 fpPolyhedron = CreatePolyhedron(); 1090 fpPolyhedron = CreatePolyhedron(); 931 fRebuildPolyhedron = false; << 932 l.unlock(); << 933 } 1091 } 934 return fpPolyhedron; 1092 return fpPolyhedron; 935 } 1093 } 936 << 937 #endif // !defined(G4GEOM_USE_UELLIPSOID) || ! << 938 1094