Geant4 Cross Reference |
1 // 1 // 2 // ******************************************* 2 // ******************************************************************** 3 // * License and Disclaimer 3 // * License and Disclaimer * 4 // * 4 // * * 5 // * The Geant4 software is copyright of th 5 // * The Geant4 software is copyright of the Copyright Holders of * 6 // * the Geant4 Collaboration. It is provided 6 // * the Geant4 Collaboration. It is provided under the terms and * 7 // * conditions of the Geant4 Software License 7 // * conditions of the Geant4 Software License, included in the file * 8 // * LICENSE and available at http://cern.ch/ 8 // * LICENSE and available at http://cern.ch/geant4/license . These * 9 // * include a list of copyright holders. 9 // * include a list of copyright holders. * 10 // * 10 // * * 11 // * Neither the authors of this software syst 11 // * Neither the authors of this software system, nor their employing * 12 // * institutes,nor the agencies providing fin 12 // * institutes,nor the agencies providing financial support for this * 13 // * work make any representation or warran 13 // * work make any representation or warranty, express or implied, * 14 // * regarding this software system or assum 14 // * regarding this software system or assume any liability for its * 15 // * use. Please see the license in the file 15 // * use. Please see the license in the file LICENSE and URL above * 16 // * for the full disclaimer and the limitatio 16 // * for the full disclaimer and the limitation of liability. * 17 // * 17 // * * 18 // * This code implementation is the result 18 // * This code implementation is the result of the scientific and * 19 // * technical work of the GEANT4 collaboratio 19 // * technical work of the GEANT4 collaboration. * 20 // * By using, copying, modifying or distri 20 // * By using, copying, modifying or distributing the software (or * 21 // * any work based on the software) you ag 21 // * any work based on the software) you agree to acknowledge its * 22 // * use in resulting scientific publicati 22 // * use in resulting scientific publications, and indicate your * 23 // * acceptance of all terms of the Geant4 Sof 23 // * acceptance of all terms of the Geant4 Software license. * 24 // ******************************************* 24 // ******************************************************************** 25 // 25 // >> 26 // >> 27 // $Id: G4Trd.cc 83572 2014-09-01 15:23:27Z gcosmo $ >> 28 // >> 29 // 26 // Implementation for G4Trd class 30 // Implementation for G4Trd class 27 // 31 // 28 // 12.01.95 P.Kent: First version << 32 // History: 29 // 28.04.05 V.Grichine: new SurfaceNormal acco << 33 // 30 // 25.05.17 E.Tcherniaev: complete revision, s << 34 // 28.04.05 V.Grichine: new SurfaceNormal according to J. Apostolakis proposal 31 // ------------------------------------------- << 35 // 26.04.05, V.Grichine, new SurfaceNoramal is default >> 36 // 07.12.04, V.Grichine, SurfaceNoramal with edges/vertices. >> 37 // 07.05.00, V.Grichine, in d = DistanceToIn(p,v), if d<0.5*kCarTolerance, d=0 >> 38 // ~1996, V.Grichine, 1st implementation based on old code of P.Kent >> 39 // >> 40 ////////////////////////////////////////////////////////////////////////////// 32 41 33 #include "G4Trd.hh" 42 #include "G4Trd.hh" 34 43 35 #if !defined(G4GEOM_USE_UTRD) 44 #if !defined(G4GEOM_USE_UTRD) 36 45 37 #include "G4GeomTools.hh" << 46 #include "G4VPVParameterisation.hh" 38 << 39 #include "G4VoxelLimits.hh" 47 #include "G4VoxelLimits.hh" 40 #include "G4AffineTransform.hh" 48 #include "G4AffineTransform.hh" 41 #include "G4BoundingEnvelope.hh" << 49 #include "Randomize.hh" 42 #include "G4QuickRand.hh" << 43 << 44 #include "G4VPVParameterisation.hh" << 45 50 46 #include "G4VGraphicsScene.hh" 51 #include "G4VGraphicsScene.hh" 47 52 48 using namespace CLHEP; 53 using namespace CLHEP; 49 54 50 ////////////////////////////////////////////// << 55 ///////////////////////////////////////////////////////////////////////// 51 // 56 // 52 // Constructor - set & check half widths << 57 // Constructor - check & set half widths 53 58 54 G4Trd::G4Trd(const G4String& pName, << 59 G4Trd::G4Trd( const G4String& pName, 55 G4double pdx1, G4double pdx << 60 G4double pdx1, G4double pdx2, 56 G4double pdy1, G4double pdy << 61 G4double pdy1, G4double pdy2, 57 G4double pdz) << 62 G4double pdz ) 58 : G4CSGSolid(pName), halfCarTolerance(0.5*kC << 63 : G4CSGSolid(pName) 59 fDx1(pdx1), fDx2(pdx2), fDy1(pdy1), fDy2(p << 60 { 64 { 61 CheckParameters(); << 65 CheckAndSetAllParameters (pdx1, pdx2, pdy1, pdy2, pdz); 62 MakePlanes(); << 63 } 66 } 64 67 65 ////////////////////////////////////////////// << 68 ///////////////////////////////////////////////////////////////////////// >> 69 // >> 70 // Set and check (coplanarity) of trd parameters >> 71 >> 72 void G4Trd::CheckAndSetAllParameters ( G4double pdx1, G4double pdx2, >> 73 G4double pdy1, G4double pdy2, >> 74 G4double pdz ) >> 75 { >> 76 if ( pdx1>0&&pdx2>0&&pdy1>0&&pdy2>0&&pdz>0 ) >> 77 { >> 78 fDx1=pdx1; fDx2=pdx2; >> 79 fDy1=pdy1; fDy2=pdy2; >> 80 fDz=pdz; >> 81 } >> 82 else >> 83 { >> 84 if ( pdx1>=0 && pdx2>=0 && pdy1>=0 && pdy2>=0 && pdz>=0 ) >> 85 { >> 86 // G4double Minimum_length= (1+per_thousand) * kCarTolerance/2.; >> 87 // FIX-ME : temporary solution for ZERO or very-small parameters >> 88 // >> 89 G4double Minimum_length= kCarTolerance/2.; >> 90 fDx1=std::max(pdx1,Minimum_length); >> 91 fDx2=std::max(pdx2,Minimum_length); >> 92 fDy1=std::max(pdy1,Minimum_length); >> 93 fDy2=std::max(pdy2,Minimum_length); >> 94 fDz=std::max(pdz,Minimum_length); >> 95 } >> 96 else >> 97 { >> 98 std::ostringstream message; >> 99 message << "Invalid negative dimensions for Solid: " << GetName() >> 100 << G4endl >> 101 << " X - " << pdx1 << ", " << pdx2 << G4endl >> 102 << " Y - " << pdy1 << ", " << pdy2 << G4endl >> 103 << " Z - " << pdz; >> 104 G4Exception("G4Trd::CheckAndSetAllParameters()", >> 105 "GeomSolids0002", FatalException, message); >> 106 } >> 107 } >> 108 fCubicVolume= 0.; >> 109 fSurfaceArea= 0.; >> 110 fRebuildPolyhedron = true; >> 111 } >> 112 >> 113 /////////////////////////////////////////////////////////////////////// 66 // 114 // 67 // Fake default constructor - sets only member 115 // Fake default constructor - sets only member data and allocates memory 68 // for usage restri << 116 // for usage restricted to object persistency. 69 // 117 // 70 G4Trd::G4Trd( __void__& a ) 118 G4Trd::G4Trd( __void__& a ) 71 : G4CSGSolid(a), halfCarTolerance(0.5*kCarTo << 119 : G4CSGSolid(a), fDx1(0.), fDx2(0.), fDy1(0.), fDy2(0.), fDz(0.) 72 fDx1(1.), fDx2(1.), fDy1(1.), fDy2(1.), fD << 73 { 120 { 74 MakePlanes(); << 75 } 121 } 76 122 77 ////////////////////////////////////////////// 123 ////////////////////////////////////////////////////////////////////////// 78 // 124 // 79 // Destructor 125 // Destructor 80 126 81 G4Trd::~G4Trd() = default; << 127 G4Trd::~G4Trd() >> 128 { >> 129 } 82 130 83 ////////////////////////////////////////////// 131 ////////////////////////////////////////////////////////////////////////// 84 // 132 // 85 // Copy constructor 133 // Copy constructor 86 134 87 G4Trd::G4Trd(const G4Trd& rhs) 135 G4Trd::G4Trd(const G4Trd& rhs) 88 : G4CSGSolid(rhs), halfCarTolerance(rhs.half << 136 : G4CSGSolid(rhs), fDx1(rhs.fDx1), fDx2(rhs.fDx2), 89 fDx1(rhs.fDx1), fDx2(rhs.fDx2), << 137 fDy1(rhs.fDy1), fDy2(rhs.fDy2), fDz(rhs.fDz) 90 fDy1(rhs.fDy1), fDy2(rhs.fDy2), fDz(rhs.fD << 91 fHx(rhs.fHx), fHy(rhs.fHy) << 92 { 138 { 93 for (G4int i=0; i<4; ++i) { fPlanes[i] = rhs << 94 } 139 } 95 140 96 ////////////////////////////////////////////// 141 ////////////////////////////////////////////////////////////////////////// 97 // 142 // 98 // Assignment operator 143 // Assignment operator 99 144 100 G4Trd& G4Trd::operator = (const G4Trd& rhs) << 145 G4Trd& G4Trd::operator = (const G4Trd& rhs) 101 { 146 { 102 // Check assignment to self 147 // Check assignment to self 103 // 148 // 104 if (this == &rhs) { return *this; } 149 if (this == &rhs) { return *this; } 105 150 106 // Copy base class data 151 // Copy base class data 107 // 152 // 108 G4CSGSolid::operator=(rhs); 153 G4CSGSolid::operator=(rhs); 109 154 110 // Copy data 155 // Copy data 111 // 156 // 112 halfCarTolerance = rhs.halfCarTolerance; << 113 fDx1 = rhs.fDx1; fDx2 = rhs.fDx2; 157 fDx1 = rhs.fDx1; fDx2 = rhs.fDx2; 114 fDy1 = rhs.fDy1; fDy2 = rhs.fDy2; 158 fDy1 = rhs.fDy1; fDy2 = rhs.fDy2; 115 fDz = rhs.fDz; 159 fDz = rhs.fDz; 116 fHx = rhs.fHx; fHy = rhs.fHy; << 117 for (G4int i=0; i<4; ++i) { fPlanes[i] = rh << 118 160 119 return *this; 161 return *this; 120 } 162 } 121 163 122 ////////////////////////////////////////////// << 164 //////////////////////////////////////////////////////////////////////////// 123 // 165 // 124 // Set all parameters, as for constructor - se << 125 << 126 void G4Trd::SetAllParameters(G4double pdx1, G4 << 127 G4double pdy1, G4 << 128 { << 129 // Reset data of the base class << 130 fCubicVolume = 0.; << 131 fSurfaceArea = 0.; << 132 fRebuildPolyhedron = true; << 133 << 134 // Set parameters << 135 fDx1 = pdx1; fDx2 = pdx2; << 136 fDy1 = pdy1; fDy2 = pdy2; << 137 fDz = pdz; << 138 << 139 CheckParameters(); << 140 MakePlanes(); << 141 } << 142 << 143 ////////////////////////////////////////////// << 144 // 166 // 145 // Check dimensions << 146 167 147 void G4Trd::CheckParameters() << 168 void G4Trd::SetAllParameters ( G4double pdx1, G4double pdx2, G4double pdy1, >> 169 G4double pdy2, G4double pdz ) 148 { 170 { 149 G4double dmin = 2*kCarTolerance; << 171 CheckAndSetAllParameters (pdx1, pdx2, pdy1, pdy2, pdz); 150 if ((fDx1 < 0 || fDx2 < 0 || fDy1 < 0 || fDy << 151 (fDx1 < dmin && fDx2 < dmin) || << 152 (fDy1 < dmin && fDy2 < dmin)) << 153 { << 154 std::ostringstream message; << 155 message << "Invalid (too small or negative << 156 << GetName() << 157 << "\n X - " << fDx1 << ", " << f << 158 << "\n Y - " << fDy1 << ", " << f << 159 << "\n Z - " << fDz; << 160 G4Exception("G4Trd::CheckParameters()", "G << 161 FatalException, message); << 162 } << 163 } 172 } 164 173 165 ////////////////////////////////////////////// << 166 // << 167 // Set side planes << 168 174 169 void G4Trd::MakePlanes() << 175 ///////////////////////////////////////////////////////////////////////// 170 { << 171 G4double dx = fDx1 - fDx2; << 172 G4double dy = fDy1 - fDy2; << 173 G4double dz = 2*fDz; << 174 fHx = std::sqrt(dy*dy + dz*dz); << 175 fHy = std::sqrt(dx*dx + dz*dz); << 176 << 177 // Set X planes at -Y & +Y << 178 // << 179 fPlanes[0].a = 0.; << 180 fPlanes[0].b = -dz/fHx; << 181 fPlanes[0].c = dy/fHx; << 182 fPlanes[0].d = fPlanes[0].b*fDy1 + fPlanes[0 << 183 << 184 fPlanes[1].a = fPlanes[0].a; << 185 fPlanes[1].b = -fPlanes[0].b; << 186 fPlanes[1].c = fPlanes[0].c; << 187 fPlanes[1].d = fPlanes[0].d; << 188 << 189 // Set Y planes at -X & +X << 190 // << 191 fPlanes[2].a = -dz/fHy; << 192 fPlanes[2].b = 0.; << 193 fPlanes[2].c = dx/fHy; << 194 fPlanes[2].d = fPlanes[2].a*fDx1 + fPlanes[2 << 195 << 196 fPlanes[3].a = -fPlanes[2].a; << 197 fPlanes[3].b = fPlanes[2].b; << 198 fPlanes[3].c = fPlanes[2].c; << 199 fPlanes[3].d = fPlanes[2].d; << 200 } << 201 << 202 ////////////////////////////////////////////// << 203 // << 204 // Get volume << 205 << 206 G4double G4Trd::GetCubicVolume() << 207 { << 208 if (fCubicVolume == 0.) << 209 { << 210 fCubicVolume = 2*fDz*( (fDx1+fDx2)*(fDy1+f << 211 (fDx2-fDx1)*(fDy2-f << 212 } << 213 return fCubicVolume; << 214 } << 215 << 216 ////////////////////////////////////////////// << 217 // << 218 // Get surface area << 219 << 220 G4double G4Trd::GetSurfaceArea() << 221 { << 222 if (fSurfaceArea == 0.) << 223 { << 224 fSurfaceArea = << 225 4*(fDx1*fDy1 + fDx2*fDy2) + 2*(fDx1+fDx2 << 226 } << 227 return fSurfaceArea; << 228 } << 229 << 230 ////////////////////////////////////////////// << 231 // 176 // 232 // Dispatch to parameterisation for replicatio 177 // Dispatch to parameterisation for replication mechanism dimension 233 // computation & modification << 178 // computation & modification. 234 179 235 void G4Trd::ComputeDimensions( G4VPVPara 180 void G4Trd::ComputeDimensions( G4VPVParameterisation* p, 236 const G4int n, 181 const G4int n, 237 const G4VPhysic 182 const G4VPhysicalVolume* pRep ) 238 { 183 { 239 p->ComputeDimensions(*this,n,pRep); 184 p->ComputeDimensions(*this,n,pRep); 240 } 185 } 241 186 242 ////////////////////////////////////////////// << 243 // << 244 // Get bounding box << 245 187 246 void G4Trd::BoundingLimits(G4ThreeVector& pMin << 188 /////////////////////////////////////////////////////////////////////////// 247 { << 248 G4double dx1 = GetXHalfLength1(); << 249 G4double dx2 = GetXHalfLength2(); << 250 G4double dy1 = GetYHalfLength1(); << 251 G4double dy2 = GetYHalfLength2(); << 252 G4double dz = GetZHalfLength(); << 253 << 254 G4double xmax = std::max(dx1,dx2); << 255 G4double ymax = std::max(dy1,dy2); << 256 pMin.set(-xmax,-ymax,-dz); << 257 pMax.set( xmax, ymax, dz); << 258 << 259 // Check correctness of the bounding box << 260 // << 261 if (pMin.x() >= pMax.x() || pMin.y() >= pMax << 262 { << 263 std::ostringstream message; << 264 message << "Bad bounding box (min >= max) << 265 << GetName() << " !" << 266 << "\npMin = " << pMin << 267 << "\npMax = " << pMax; << 268 G4Exception("G4Trd::BoundingLimits()", "Ge << 269 DumpInfo(); << 270 } << 271 } << 272 << 273 ////////////////////////////////////////////// << 274 // 189 // 275 // Calculate extent under transform and specif 190 // Calculate extent under transform and specified limit 276 191 277 G4bool G4Trd::CalculateExtent( const EAxis pAx 192 G4bool G4Trd::CalculateExtent( const EAxis pAxis, 278 const G4VoxelLi 193 const G4VoxelLimits& pVoxelLimit, 279 const G4AffineT 194 const G4AffineTransform& pTransform, 280 G4double& 195 G4double& pMin, G4double& pMax ) const 281 { 196 { 282 G4ThreeVector bmin, bmax; << 197 if (!pTransform.IsRotated()) 283 G4bool exist; << 284 << 285 // Check bounding box (bbox) << 286 // << 287 BoundingLimits(bmin,bmax); << 288 G4BoundingEnvelope bbox(bmin,bmax); << 289 #ifdef G4BBOX_EXTENT << 290 return bbox.CalculateExtent(pAxis,pVoxelLimi << 291 #endif << 292 if (bbox.BoundingBoxVsVoxelLimits(pAxis,pVox << 293 { 198 { 294 return exist = pMin < pMax; << 199 // Special case handling for unrotated solids >> 200 // Compute x/y/z mins and maxs respecting limits, with early returns >> 201 // if outside limits. Then switch() on pAxis >> 202 >> 203 G4double xoffset,xMin,xMax; >> 204 G4double yoffset,yMin,yMax; >> 205 G4double zoffset,zMin,zMax; >> 206 >> 207 zoffset=pTransform.NetTranslation().z(); >> 208 zMin=zoffset-fDz; >> 209 zMax=zoffset+fDz; >> 210 if (pVoxelLimit.IsZLimited()) >> 211 { >> 212 if ( (zMin>pVoxelLimit.GetMaxZExtent()+kCarTolerance) >> 213 || (zMax<pVoxelLimit.GetMinZExtent()-kCarTolerance) ) >> 214 { >> 215 return false; >> 216 } >> 217 else >> 218 { >> 219 if (zMin<pVoxelLimit.GetMinZExtent()) >> 220 { >> 221 zMin=pVoxelLimit.GetMinZExtent(); >> 222 } >> 223 if (zMax>pVoxelLimit.GetMaxZExtent()) >> 224 { >> 225 zMax=pVoxelLimit.GetMaxZExtent(); >> 226 } >> 227 } >> 228 } >> 229 xoffset=pTransform.NetTranslation().x(); >> 230 if (fDx2 >= fDx1) >> 231 { >> 232 xMax = xoffset+(fDx1+fDx2)/2+(zMax-zoffset)*(fDx2-fDx1)/(2*fDz) ; >> 233 xMin = 2*xoffset - xMax ; >> 234 } >> 235 else >> 236 { >> 237 xMax = xoffset+(fDx1+fDx2)/2+(zMin-zoffset)*(fDx2-fDx1)/(2*fDz) ; >> 238 xMin = 2*xoffset - xMax ; >> 239 } >> 240 if (pVoxelLimit.IsXLimited()) >> 241 { >> 242 if ( (xMin>pVoxelLimit.GetMaxXExtent()+kCarTolerance) >> 243 || (xMax<pVoxelLimit.GetMinXExtent()-kCarTolerance) ) >> 244 { >> 245 return false; >> 246 } >> 247 else >> 248 { >> 249 if (xMin<pVoxelLimit.GetMinXExtent()) >> 250 { >> 251 xMin=pVoxelLimit.GetMinXExtent(); >> 252 } >> 253 if (xMax>pVoxelLimit.GetMaxXExtent()) >> 254 { >> 255 xMax=pVoxelLimit.GetMaxXExtent(); >> 256 } >> 257 } >> 258 } >> 259 yoffset= pTransform.NetTranslation().y() ; >> 260 if(fDy2 >= fDy1) >> 261 { >> 262 yMax = yoffset+(fDy2+fDy1)/2+(zMax-zoffset)*(fDy2-fDy1)/(2*fDz) ; >> 263 yMin = 2*yoffset - yMax ; >> 264 } >> 265 else >> 266 { >> 267 yMax = yoffset+(fDy2+fDy1)/2+(zMin-zoffset)*(fDy2-fDy1)/(2*fDz) ; >> 268 yMin = 2*yoffset - yMax ; >> 269 } >> 270 if (pVoxelLimit.IsYLimited()) >> 271 { >> 272 if ( (yMin>pVoxelLimit.GetMaxYExtent()+kCarTolerance) >> 273 || (yMax<pVoxelLimit.GetMinYExtent()-kCarTolerance) ) >> 274 { >> 275 return false; >> 276 } >> 277 else >> 278 { >> 279 if (yMin<pVoxelLimit.GetMinYExtent()) >> 280 { >> 281 yMin=pVoxelLimit.GetMinYExtent(); >> 282 } >> 283 if (yMax>pVoxelLimit.GetMaxYExtent()) >> 284 { >> 285 yMax=pVoxelLimit.GetMaxYExtent(); >> 286 } >> 287 } >> 288 } >> 289 >> 290 switch (pAxis) >> 291 { >> 292 case kXAxis: >> 293 pMin=xMin; >> 294 pMax=xMax; >> 295 break; >> 296 case kYAxis: >> 297 pMin=yMin; >> 298 pMax=yMax; >> 299 break; >> 300 case kZAxis: >> 301 pMin=zMin; >> 302 pMax=zMax; >> 303 break; >> 304 default: >> 305 break; >> 306 } >> 307 >> 308 // Add 2*Tolerance to avoid precision troubles ? >> 309 // >> 310 pMin-=kCarTolerance; >> 311 pMax+=kCarTolerance; >> 312 >> 313 return true; 295 } 314 } >> 315 else >> 316 { >> 317 // General rotated case - create and clip mesh to boundaries 296 318 297 // Set bounding envelope (benv) and calculat << 319 G4bool existsAfterClip=false; 298 // << 320 G4ThreeVectorList *vertices; 299 G4double dx1 = GetXHalfLength1(); << 321 300 G4double dx2 = GetXHalfLength2(); << 322 pMin=+kInfinity; 301 G4double dy1 = GetYHalfLength1(); << 323 pMax=-kInfinity; 302 G4double dy2 = GetYHalfLength2(); << 324 303 G4double dz = GetZHalfLength(); << 325 // Calculate rotated vertex coordinates 304 << 326 // 305 G4ThreeVectorList baseA(4), baseB(4); << 327 vertices=CreateRotatedVertices(pTransform); 306 baseA[0].set(-dx1,-dy1,-dz); << 328 ClipCrossSection(vertices,0,pVoxelLimit,pAxis,pMin,pMax); 307 baseA[1].set( dx1,-dy1,-dz); << 329 ClipCrossSection(vertices,4,pVoxelLimit,pAxis,pMin,pMax); 308 baseA[2].set( dx1, dy1,-dz); << 330 ClipBetweenSections(vertices,0,pVoxelLimit,pAxis,pMin,pMax); 309 baseA[3].set(-dx1, dy1,-dz); << 331 310 baseB[0].set(-dx2,-dy2, dz); << 332 if (pMin!=kInfinity||pMax!=-kInfinity) 311 baseB[1].set( dx2,-dy2, dz); << 333 { 312 baseB[2].set( dx2, dy2, dz); << 334 existsAfterClip=true; 313 baseB[3].set(-dx2, dy2, dz); << 335 314 << 336 // Add 2*tolerance to avoid precision troubles 315 std::vector<const G4ThreeVectorList *> polyg << 337 // 316 polygons[0] = &baseA; << 338 pMin-=kCarTolerance; 317 polygons[1] = &baseB; << 339 pMax+=kCarTolerance; 318 << 340 319 G4BoundingEnvelope benv(bmin,bmax,polygons); << 341 } 320 exist = benv.CalculateExtent(pAxis,pVoxelLim << 342 else 321 return exist; << 343 { >> 344 // Check for case where completely enveloping clipping volume >> 345 // If point inside then we are confident that the solid completely >> 346 // envelopes the clipping volume. Hence set min/max extents according >> 347 // to clipping volume extents along the specified axis. >> 348 >> 349 G4ThreeVector clipCentre( >> 350 (pVoxelLimit.GetMinXExtent()+pVoxelLimit.GetMaxXExtent())*0.5, >> 351 (pVoxelLimit.GetMinYExtent()+pVoxelLimit.GetMaxYExtent())*0.5, >> 352 (pVoxelLimit.GetMinZExtent()+pVoxelLimit.GetMaxZExtent())*0.5); >> 353 >> 354 if (Inside(pTransform.Inverse().TransformPoint(clipCentre))!=kOutside) >> 355 { >> 356 existsAfterClip=true; >> 357 pMin=pVoxelLimit.GetMinExtent(pAxis); >> 358 pMax=pVoxelLimit.GetMaxExtent(pAxis); >> 359 } >> 360 } >> 361 delete vertices; >> 362 return existsAfterClip; >> 363 } 322 } 364 } 323 365 324 ////////////////////////////////////////////// << 366 /////////////////////////////////////////////////////////////////// 325 // 367 // 326 // Return whether point inside/outside/on surf 368 // Return whether point inside/outside/on surface, using tolerance 327 369 328 EInside G4Trd::Inside( const G4ThreeVector& p 370 EInside G4Trd::Inside( const G4ThreeVector& p ) const 329 { << 371 { 330 G4double dx = fPlanes[3].a*std::abs(p.x())+f << 372 EInside in=kOutside; 331 G4double dy = fPlanes[1].b*std::abs(p.y())+f << 373 G4double x,y,zbase1,zbase2; 332 G4double dxy = std::max(dx,dy); << 374 >> 375 if (std::fabs(p.z())<=fDz-kCarTolerance/2) >> 376 { >> 377 zbase1=p.z()+fDz; // Dist from -ve z plane >> 378 zbase2=fDz-p.z(); // Dist from +ve z plane 333 379 334 G4double dz = std::abs(p.z())-fDz; << 380 // Check whether inside x tolerance 335 G4double dist = std::max(dz,dxy); << 381 // >> 382 x=0.5*(fDx2*zbase1+fDx1*zbase2)/fDz - kCarTolerance/2; >> 383 if (std::fabs(p.x())<=x) >> 384 { >> 385 y=0.5*((fDy2*zbase1+fDy1*zbase2))/fDz - kCarTolerance/2; >> 386 if (std::fabs(p.y())<=y) >> 387 { >> 388 in=kInside; >> 389 } >> 390 else if (std::fabs(p.y())<=y+kCarTolerance) >> 391 { >> 392 in=kSurface; >> 393 } >> 394 } >> 395 else if (std::fabs(p.x())<=x+kCarTolerance) >> 396 { >> 397 // y = y half width of shape at z of point + tolerant boundary >> 398 // >> 399 y=0.5*((fDy2*zbase1+fDy1*zbase2))/fDz + kCarTolerance/2; >> 400 if (std::fabs(p.y())<=y) >> 401 { >> 402 in=kSurface; >> 403 } >> 404 } >> 405 } >> 406 else if (std::fabs(p.z())<=fDz+kCarTolerance/2) >> 407 { >> 408 // Only need to check outer tolerant boundaries >> 409 // >> 410 zbase1=p.z()+fDz; // Dist from -ve z plane >> 411 zbase2=fDz-p.z(); // Dist from +ve z plane 336 412 337 return (dist > halfCarTolerance) ? kOutside << 413 // x = x half width of shape at z of point plus tolerance 338 ((dist > -halfCarTolerance) ? kSurface : k << 414 // >> 415 x=0.5*(fDx2*zbase1+fDx1*zbase2)/fDz + kCarTolerance/2; >> 416 if (std::fabs(p.x())<=x) >> 417 { >> 418 // y = y half width of shape at z of point >> 419 // >> 420 y=0.5*((fDy2*zbase1+fDy1*zbase2))/fDz + kCarTolerance/2; >> 421 if (std::fabs(p.y())<=y) in=kSurface; >> 422 } >> 423 } >> 424 return in; 339 } 425 } 340 426 341 ////////////////////////////////////////////// 427 ////////////////////////////////////////////////////////////////////////// 342 // 428 // 343 // Determine side where point is, and return c << 429 // Calculate side nearest to p, and return normal >> 430 // If two sides are equidistant, normal of first side (x/y/z) >> 431 // encountered returned 344 432 345 G4ThreeVector G4Trd::SurfaceNormal( const G4Th 433 G4ThreeVector G4Trd::SurfaceNormal( const G4ThreeVector& p ) const 346 { 434 { 347 G4int nsurf = 0; // number of surfaces where << 435 G4ThreeVector norm, sumnorm(0.,0.,0.); 348 << 436 G4int noSurfaces = 0; 349 // Check Z faces << 437 G4double z = 2.0*fDz, tanx, secx, newpx, widx; 350 // << 438 G4double tany, secy, newpy, widy; 351 G4double nz = 0; << 439 G4double distx, disty, distz, fcos; 352 G4double dz = std::abs(p.z()) - fDz; << 440 G4double delta = 0.5*kCarTolerance; 353 if (std::abs(dz) <= halfCarTolerance) << 441 354 { << 442 tanx = (fDx2 - fDx1)/z; 355 nz = (p.z() < 0) ? -1 : 1; << 443 secx = std::sqrt(1.0+tanx*tanx); 356 ++nsurf; << 444 newpx = std::fabs(p.x())-p.z()*tanx; >> 445 widx = fDx2 - fDz*tanx; >> 446 >> 447 tany = (fDy2 - fDy1)/z; >> 448 secy = std::sqrt(1.0+tany*tany); >> 449 newpy = std::fabs(p.y())-p.z()*tany; >> 450 widy = fDy2 - fDz*tany; >> 451 >> 452 distx = std::fabs(newpx-widx)/secx; // perp. distance to x side >> 453 disty = std::fabs(newpy-widy)/secy; // to y side >> 454 distz = std::fabs(std::fabs(p.z())-fDz); // to z side >> 455 >> 456 fcos = 1.0/secx; >> 457 G4ThreeVector nX = G4ThreeVector( fcos,0,-tanx*fcos); >> 458 G4ThreeVector nmX = G4ThreeVector(-fcos,0,-tanx*fcos); >> 459 >> 460 fcos = 1.0/secy; >> 461 G4ThreeVector nY = G4ThreeVector(0, fcos,-tany*fcos); >> 462 G4ThreeVector nmY = G4ThreeVector(0,-fcos,-tany*fcos); >> 463 G4ThreeVector nZ = G4ThreeVector( 0, 0, 1.0); >> 464 >> 465 if (distx <= delta) >> 466 { >> 467 noSurfaces ++; >> 468 if ( p.x() >= 0.) sumnorm += nX; >> 469 else sumnorm += nmX; >> 470 } >> 471 if (disty <= delta) >> 472 { >> 473 noSurfaces ++; >> 474 if ( p.y() >= 0.) sumnorm += nY; >> 475 else sumnorm += nmY; >> 476 } >> 477 if (distz <= delta) >> 478 { >> 479 noSurfaces ++; >> 480 if ( p.z() >= 0.) sumnorm += nZ; >> 481 else sumnorm -= nZ; 357 } 482 } 358 << 483 if ( noSurfaces == 0 ) 359 // Check Y faces << 360 // << 361 G4double ny = 0; << 362 G4double dy1 = fPlanes[0].b*p.y(); << 363 G4double dy2 = fPlanes[0].c*p.z() + fPlanes[ << 364 if (std::abs(dy2 + dy1) <= halfCarTolerance) << 365 { << 366 ny += fPlanes[0].b; << 367 nz += fPlanes[0].c; << 368 ++nsurf; << 369 } << 370 if (std::abs(dy2 - dy1) <= halfCarTolerance) << 371 { << 372 ny += fPlanes[1].b; << 373 nz += fPlanes[1].c; << 374 ++nsurf; << 375 } << 376 << 377 // Check X faces << 378 // << 379 G4double nx = 0; << 380 G4double dx1 = fPlanes[2].a*p.x(); << 381 G4double dx2 = fPlanes[2].c*p.z() + fPlanes[ << 382 if (std::abs(dx2 + dx1) <= halfCarTolerance) << 383 { << 384 nx += fPlanes[2].a; << 385 nz += fPlanes[2].c; << 386 ++nsurf; << 387 } << 388 if (std::abs(dx2 - dx1) <= halfCarTolerance) << 389 { << 390 nx += fPlanes[3].a; << 391 nz += fPlanes[3].c; << 392 ++nsurf; << 393 } << 394 << 395 // Return normal << 396 // << 397 if (nsurf == 1) return {nx,ny,nz}; << 398 else if (nsurf != 0) return G4ThreeVector(nx << 399 else << 400 { 484 { 401 // Point is not on the surface << 402 // << 403 #ifdef G4CSGDEBUG 485 #ifdef G4CSGDEBUG 404 std::ostringstream message; << 486 G4Exception("G4Trd::SurfaceNormal(p)", "GeomSolids1002", JustWarning, 405 G4long oldprc = message.precision(16); << 487 "Point p is not on surface !?" ); 406 message << "Point p is not on surface (!?) << 488 #endif 407 << GetName() << G4endl; << 489 norm = ApproxSurfaceNormal(p); 408 message << "Position:\n"; << 409 message << " p.x() = " << p.x()/mm << " << 410 message << " p.y() = " << p.y()/mm << " << 411 message << " p.z() = " << p.z()/mm << " << 412 G4cout.precision(oldprc) ; << 413 G4Exception("G4Trd::SurfaceNormal(p)", "Ge << 414 JustWarning, message ); << 415 DumpInfo(); << 416 #endif << 417 return ApproxSurfaceNormal(p); << 418 } 490 } >> 491 else if ( noSurfaces == 1 ) norm = sumnorm; >> 492 else norm = sumnorm.unit(); >> 493 return norm; 419 } 494 } 420 495 421 ////////////////////////////////////////////// << 496 >> 497 ///////////////////////////////////////////////////////////////////////////// 422 // 498 // 423 // Algorithm for SurfaceNormal() following the 499 // Algorithm for SurfaceNormal() following the original specification 424 // for points not on the surface 500 // for points not on the surface 425 501 426 G4ThreeVector G4Trd::ApproxSurfaceNormal( cons 502 G4ThreeVector G4Trd::ApproxSurfaceNormal( const G4ThreeVector& p ) const 427 { 503 { 428 G4double dist = -DBL_MAX; << 504 G4ThreeVector norm; 429 G4int iside = 0; << 505 G4double z,tanx,secx,newpx,widx; 430 for (G4int i=0; i<4; ++i) << 506 G4double tany,secy,newpy,widy; 431 { << 507 G4double distx,disty,distz,fcos; 432 G4double d = fPlanes[i].a*p.x() + << 508 433 fPlanes[i].b*p.y() + << 509 z=2.0*fDz; 434 fPlanes[i].c*p.z() + fPlanes[ << 510 435 if (d > dist) { dist = d; iside = i; } << 511 tanx=(fDx2-fDx1)/z; 436 } << 512 secx=std::sqrt(1.0+tanx*tanx); >> 513 newpx=std::fabs(p.x())-p.z()*tanx; >> 514 widx=fDx2-fDz*tanx; >> 515 >> 516 tany=(fDy2-fDy1)/z; >> 517 secy=std::sqrt(1.0+tany*tany); >> 518 newpy=std::fabs(p.y())-p.z()*tany; >> 519 widy=fDy2-fDz*tany; >> 520 >> 521 distx=std::fabs(newpx-widx)/secx; // perpendicular distance to x side >> 522 disty=std::fabs(newpy-widy)/secy; // to y side >> 523 distz=std::fabs(std::fabs(p.z())-fDz); // to z side 437 524 438 G4double distz = std::abs(p.z()) - fDz; << 525 // find closest side 439 if (dist > distz) << 526 // 440 return { fPlanes[iside].a, fPlanes[iside]. << 527 if (distx<=disty) >> 528 { >> 529 if (distx<=distz) >> 530 { >> 531 // Closest to X >> 532 // >> 533 fcos=1.0/secx; >> 534 // normal=(+/-std::cos(ang),0,-std::sin(ang)) >> 535 if (p.x()>=0) >> 536 norm=G4ThreeVector(fcos,0,-tanx*fcos); >> 537 else >> 538 norm=G4ThreeVector(-fcos,0,-tanx*fcos); >> 539 } >> 540 else >> 541 { >> 542 // Closest to Z >> 543 // >> 544 if (p.z()>=0) >> 545 norm=G4ThreeVector(0,0,1); >> 546 else >> 547 norm=G4ThreeVector(0,0,-1); >> 548 } >> 549 } 441 else 550 else 442 return { 0, 0, (G4double)((p.z() < 0) ? -1 << 551 { >> 552 if (disty<=distz) >> 553 { >> 554 // Closest to Y >> 555 // >> 556 fcos=1.0/secy; >> 557 if (p.y()>=0) >> 558 norm=G4ThreeVector(0,fcos,-tany*fcos); >> 559 else >> 560 norm=G4ThreeVector(0,-fcos,-tany*fcos); >> 561 } >> 562 else >> 563 { >> 564 // Closest to Z >> 565 // >> 566 if (p.z()>=0) >> 567 norm=G4ThreeVector(0,0,1); >> 568 else >> 569 norm=G4ThreeVector(0,0,-1); >> 570 } >> 571 } >> 572 return norm; 443 } 573 } 444 574 445 ////////////////////////////////////////////// << 575 //////////////////////////////////////////////////////////////////////////// 446 // 576 // 447 // Calculate distance to shape from outside 577 // Calculate distance to shape from outside 448 // - return kInfinity if no intersection << 578 // - return kInfinity if no intersection >> 579 // >> 580 // ALGORITHM: >> 581 // For each component, calculate pair of minimum and maximum intersection >> 582 // values for which the particle is in the extent of the shape >> 583 // - The smallest (MAX minimum) allowed distance of the pairs is intersect >> 584 // - Z plane intersectin uses tolerance >> 585 // - XZ YZ planes use logic & *SLIGHTLY INCORRECT* tolerance >> 586 // (this saves at least 1 sqrt, 1 multiply and 1 divide... in applicable >> 587 // cases) >> 588 // - Note: XZ and YZ planes each divide space into four regions, >> 589 // characterised by ss1 ss2 >> 590 // NOTE: >> 591 // >> 592 // `Inside' safe - meaningful answers given if point is inside the exact >> 593 // shape. >> 594 >> 595 G4double G4Trd::DistanceToIn( const G4ThreeVector& p, >> 596 const G4ThreeVector& v ) const >> 597 { >> 598 G4double snxt = kInfinity ; // snxt = default return value >> 599 G4double smin,smax; >> 600 G4double s1,s2,tanxz,tanyz,ds1,ds2; >> 601 G4double ss1,ss2,sn1=0.,sn2=0.,Dist; 449 602 450 G4double G4Trd::DistanceToIn(const G4ThreeVect << 603 if ( v.z() ) // Calculate valid z intersect range 451 const G4ThreeVect << 604 { 452 { << 605 if ( v.z() > 0 ) // Calculate smax: must be +ve or no intersection. 453 // Z intersections << 606 { 454 // << 607 Dist = fDz - p.z() ; // to plane at +dz 455 if ((std::abs(p.z()) - fDz) >= -halfCarToler << 456 return kInfinity; << 457 G4double invz = (-v.z() == 0) ? DBL_MAX : -1 << 458 G4double dz = (invz < 0) ? fDz : -fDz; << 459 G4double tzmin = (p.z() + dz)*invz; << 460 G4double tzmax = (p.z() - dz)*invz; << 461 608 462 // Y intersections << 609 if (Dist >= 0.5*kCarTolerance) 463 // << 610 { 464 G4double tmin0 = tzmin, tmax0 = tzmax; << 611 smax = Dist/v.z() ; 465 G4double ya = fPlanes[0].b*v.y(), yb = fPlan << 612 smin = -(fDz + p.z())/v.z() ; 466 G4double yc = fPlanes[0].b*p.y(), yd = fPlan << 613 } 467 G4double cos0 = yb + ya; << 614 else return snxt ; 468 G4double dis0 = yd + yc; << 615 } 469 if (dis0 >= -halfCarTolerance) << 616 else // v.z <0 >> 617 { >> 618 Dist=fDz+p.z(); // plane at -dz >> 619 >> 620 if ( Dist >= 0.5*kCarTolerance ) >> 621 { >> 622 smax = -Dist/v.z() ; >> 623 smin = (fDz - p.z())/v.z() ; >> 624 } >> 625 else return snxt ; >> 626 } >> 627 if (smin < 0 ) smin = 0 ; >> 628 } >> 629 else // v.z=0 470 { 630 { 471 if (cos0 >= 0) return kInfinity; << 631 if (std::fabs(p.z()) >= fDz ) return snxt ; // Outside & no intersect 472 G4double tmp = -dis0/cos0; << 632 else 473 if (tmin0 < tmp) tmin0 = tmp; << 633 { >> 634 smin = 0 ; // Always inside z range >> 635 smax = kInfinity; >> 636 } 474 } 637 } 475 else if (cos0 > 0) << 638 >> 639 // Calculate x intersection range >> 640 // >> 641 // Calc half width at p.z, and components towards planes >> 642 >> 643 tanxz = (fDx2 - fDx1)*0.5/fDz ; >> 644 s1 = 0.5*(fDx1+fDx2) + tanxz*p.z() ; // x half width at p.z >> 645 ds1 = v.x() - tanxz*v.z() ; // Components of v towards faces at +-x >> 646 ds2 = v.x() + tanxz*v.z() ; >> 647 ss1 = s1 - p.x() ; // -delta x to +ve plane >> 648 // -ve when outside >> 649 ss2 = -s1 - p.x() ; // -delta x to -ve plane >> 650 // +ve when outside >> 651 >> 652 if (ss1 < 0 && ss2 <= 0 ) 476 { 653 { 477 G4double tmp = -dis0/cos0; << 654 if (ds1 < 0) // In +ve coord Area 478 if (tmax0 > tmp) tmax0 = tmp; << 655 { >> 656 sn1 = ss1/ds1 ; >> 657 >> 658 if ( ds2 < 0 ) sn2 = ss2/ds2 ; >> 659 else sn2 = kInfinity ; >> 660 } >> 661 else return snxt ; 479 } 662 } >> 663 else if ( ss1 >= 0 && ss2 > 0 ) >> 664 { >> 665 if ( ds2 > 0 ) // In -ve coord Area >> 666 { >> 667 sn1 = ss2/ds2 ; 480 668 481 G4double tmin1 = tmin0, tmax1 = tmax0; << 669 if (ds1 > 0) sn2 = ss1/ds1 ; 482 G4double cos1 = yb - ya; << 670 else sn2 = kInfinity; 483 G4double dis1 = yd - yc; << 671 484 if (dis1 >= -halfCarTolerance) << 672 } >> 673 else return snxt ; >> 674 } >> 675 else if (ss1 >= 0 && ss2 <= 0 ) 485 { 676 { 486 if (cos1 >= 0) return kInfinity; << 677 // Inside Area - calculate leaving distance 487 G4double tmp = -dis1/cos1; << 678 // *Don't* use exact distance to side for tolerance 488 if (tmin1 < tmp) tmin1 = tmp; << 679 // = ss1*std::cos(ang xz) >> 680 // = ss1/std::sqrt(1.0+tanxz*tanxz) >> 681 sn1 = 0 ; >> 682 >> 683 if ( ds1 > 0 ) >> 684 { >> 685 if (ss1 > 0.5*kCarTolerance) sn2 = ss1/ds1 ; // Leave +ve side extent >> 686 else return snxt ; // Leave immediately by +ve >> 687 } >> 688 else sn2 = kInfinity ; >> 689 >> 690 if ( ds2 < 0 ) >> 691 { >> 692 if ( ss2 < -0.5*kCarTolerance ) >> 693 { >> 694 Dist = ss2/ds2 ; // Leave -ve side extent >> 695 if ( Dist < sn2 ) sn2 = Dist ; >> 696 } >> 697 else return snxt ; >> 698 } 489 } 699 } 490 else if (cos1 > 0) << 700 else if (ss1 < 0 && ss2 > 0 ) 491 { 701 { 492 G4double tmp = -dis1/cos1; << 702 // Within +/- plane cross-over areas (not on boundaries ss1||ss2==0) 493 if (tmax1 > tmp) tmax1 = tmp; << 703 >> 704 if ( ds1 >= 0 || ds2 <= 0 ) >> 705 { >> 706 return snxt ; >> 707 } >> 708 else // Will intersect & stay inside >> 709 { >> 710 sn1 = ss1/ds1 ; >> 711 Dist = ss2/ds2 ; >> 712 if (Dist > sn1 ) sn1 = Dist ; >> 713 sn2 = kInfinity ; >> 714 } 494 } 715 } 495 716 496 // X intersections << 717 // Reduce allowed range of distances as appropriate 497 // << 718 498 G4double tmin2 = tmin1, tmax2 = tmax1; << 719 if ( sn1 > smin ) smin = sn1 ; 499 G4double xa = fPlanes[2].a*v.x(), xb = fPlan << 720 if ( sn2 < smax ) smax = sn2 ; 500 G4double xc = fPlanes[2].a*p.x(), xd = fPlan << 721 501 G4double cos2 = xb + xa; << 722 // Check for incompatible ranges (eg z intersects between 50 ->100 and x 502 G4double dis2 = xd + xc; << 723 // only 10-40 -> no intersection) 503 if (dis2 >= -halfCarTolerance) << 724 >> 725 if ( smax < smin ) return snxt ; >> 726 >> 727 // Calculate valid y intersection range >> 728 // (repeat of x intersection code) >> 729 >> 730 tanyz = (fDy2-fDy1)*0.5/fDz ; >> 731 s2 = 0.5*(fDy1+fDy2) + tanyz*p.z() ; // y half width at p.z >> 732 ds1 = v.y() - tanyz*v.z() ; // Components of v towards faces at +-y >> 733 ds2 = v.y() + tanyz*v.z() ; >> 734 ss1 = s2 - p.y() ; // -delta y to +ve plane >> 735 ss2 = -s2 - p.y() ; // -delta y to -ve plane >> 736 >> 737 if ( ss1 < 0 && ss2 <= 0 ) 504 { 738 { 505 if (cos2 >= 0) return kInfinity; << 739 if (ds1 < 0 ) // In +ve coord Area 506 G4double tmp = -dis2/cos2; << 740 { 507 if (tmin2 < tmp) tmin2 = tmp; << 741 sn1 = ss1/ds1 ; >> 742 if ( ds2 < 0 ) sn2 = ss2/ds2 ; >> 743 else sn2 = kInfinity ; >> 744 } >> 745 else return snxt ; 508 } 746 } 509 else if (cos2 > 0) << 747 else if ( ss1 >= 0 && ss2 > 0 ) 510 { 748 { 511 G4double tmp = -dis2/cos2; << 749 if ( ds2 > 0 ) // In -ve coord Area 512 if (tmax2 > tmp) tmax2 = tmp; << 750 { >> 751 sn1 = ss2/ds2 ; >> 752 if ( ds1 > 0 ) sn2 = ss1/ds1 ; >> 753 else sn2 = kInfinity ; >> 754 } >> 755 else return snxt ; 513 } 756 } 514 << 757 else if (ss1 >= 0 && ss2 <= 0 ) 515 G4double tmin3 = tmin2, tmax3 = tmax2; << 516 G4double cos3 = xb - xa; << 517 G4double dis3 = xd - xc; << 518 if (dis3 >= -halfCarTolerance) << 519 { 758 { 520 if (cos3 >= 0) return kInfinity; << 759 // Inside Area - calculate leaving distance 521 G4double tmp = -dis3/cos3; << 760 // *Don't* use exact distance to side for tolerance 522 if (tmin3 < tmp) tmin3 = tmp; << 761 // = ss1*std::cos(ang yz) >> 762 // = ss1/std::sqrt(1.0+tanyz*tanyz) >> 763 sn1 = 0 ; >> 764 >> 765 if ( ds1 > 0 ) >> 766 { >> 767 if (ss1 > 0.5*kCarTolerance) sn2 = ss1/ds1 ; // Leave +ve side extent >> 768 else return snxt ; // Leave immediately by +ve >> 769 } >> 770 else sn2 = kInfinity ; >> 771 >> 772 if ( ds2 < 0 ) >> 773 { >> 774 if ( ss2 < -0.5*kCarTolerance ) >> 775 { >> 776 Dist = ss2/ds2 ; // Leave -ve side extent >> 777 if (Dist < sn2) sn2=Dist; >> 778 } >> 779 else return snxt ; >> 780 } 523 } 781 } 524 else if (cos3 > 0) << 782 else if (ss1 < 0 && ss2 > 0 ) 525 { 783 { 526 G4double tmp = -dis3/cos3; << 784 // Within +/- plane cross-over areas (not on boundaries ss1||ss2==0) 527 if (tmax3 > tmp) tmax3 = tmp; << 785 >> 786 if (ds1 >= 0 || ds2 <= 0 ) >> 787 { >> 788 return snxt ; >> 789 } >> 790 else // Will intersect & stay inside >> 791 { >> 792 sn1 = ss1/ds1 ; >> 793 Dist = ss2/ds2 ; >> 794 if (Dist > sn1 ) sn1 = Dist ; >> 795 sn2 = kInfinity ; >> 796 } 528 } 797 } >> 798 >> 799 // Reduce allowed range of distances as appropriate 529 800 530 // Find distance << 801 if ( sn1 > smin) smin = sn1 ; 531 // << 802 if ( sn2 < smax) smax = sn2 ; 532 G4double tmin = tmin3, tmax = tmax3; << 803 533 if (tmax <= tmin + halfCarTolerance) return << 804 // Check for incompatible ranges (eg x intersects between 50 ->100 and y 534 return (tmin < halfCarTolerance ) ? 0. : tmi << 805 // only 10-40 -> no intersection). Set snxt if ok >> 806 >> 807 if ( smax > smin ) snxt = smin ; >> 808 if (snxt < 0.5*kCarTolerance ) snxt = 0.0 ; >> 809 >> 810 return snxt ; 535 } 811 } 536 812 537 ////////////////////////////////////////////// << 813 ///////////////////////////////////////////////////////////////////////// 538 // 814 // 539 // Calculate exact shortest distance to any bo << 815 // Approximate distance to shape 540 // This is the best fast estimation of the sho << 816 // Calculate perpendicular distances to z/x/y surfaces, return largest 541 // - returns 0 if point is inside << 817 // which is the most fast estimation of shortest distance to Trd >> 818 // - Safe underestimate >> 819 // - If point within exact shape, return 0 542 820 543 G4double G4Trd::DistanceToIn( const G4ThreeVec 821 G4double G4Trd::DistanceToIn( const G4ThreeVector& p ) const 544 { 822 { 545 G4double dx = fPlanes[3].a*std::abs(p.x())+f << 823 G4double safe=0.0; 546 G4double dy = fPlanes[1].b*std::abs(p.y())+f << 824 G4double tanxz,distx,safx; 547 G4double dxy = std::max(dx,dy); << 825 G4double tanyz,disty,safy; >> 826 G4double zbase; >> 827 >> 828 safe=std::fabs(p.z())-fDz; >> 829 if (safe<0) safe=0; // Also used to ensure x/y distances >> 830 // POSITIVE 548 831 549 G4double dz = std::abs(p.z())-fDz; << 832 zbase=fDz+p.z(); 550 G4double dist = std::max(dz,dxy); << 551 833 552 return (dist > 0) ? dist : 0.; << 834 // Find distance along x direction to closest x plane >> 835 // >> 836 tanxz=(fDx2-fDx1)*0.5/fDz; >> 837 // widx=fDx1+tanxz*(fDz+p.z()); // x width at p.z >> 838 // distx=std::fabs(p.x())-widx; // distance to plane >> 839 distx=std::fabs(p.x())-(fDx1+tanxz*zbase); >> 840 if (distx>safe) >> 841 { >> 842 safx=distx/std::sqrt(1.0+tanxz*tanxz); // vector Dist=Dist*std::cos(ang) >> 843 if (safx>safe) safe=safx; >> 844 } >> 845 >> 846 // Find distance along y direction to slanted wall >> 847 tanyz=(fDy2-fDy1)*0.5/fDz; >> 848 // widy=fDy1+tanyz*(fDz+p.z()); // y width at p.z >> 849 // disty=std::fabs(p.y())-widy; // distance to plane >> 850 disty=std::fabs(p.y())-(fDy1+tanyz*zbase); >> 851 if (disty>safe) >> 852 { >> 853 safy=disty/std::sqrt(1.0+tanyz*tanyz); // distance along vector >> 854 if (safy>safe) safe=safy; >> 855 } >> 856 return safe; 553 } 857 } 554 858 555 ////////////////////////////////////////////// << 859 //////////////////////////////////////////////////////////////////////// 556 // 860 // 557 // Calculate distance to surface of shape from << 861 // Calcluate distance to surface of shape from inside 558 // find normal at exit point, if required << 862 // Calculate distance to x/y/z planes - smallest is exiting distance 559 // - when leaving the surface, return 0 << 863 // - z planes have std. check for tolerance 560 << 864 // - xz yz planes have check based on distance || to x or y axis 561 G4double G4Trd::DistanceToOut(const G4ThreeVec << 865 // (not corrected for slope of planes) 562 const G4bool cal << 866 // ?BUG? If v.z==0 are there cases when snside not set???? 563 G4bool* va << 867 >> 868 G4double G4Trd::DistanceToOut( const G4ThreeVector& p, >> 869 const G4ThreeVector& v, >> 870 const G4bool calcNorm, >> 871 G4bool *validNorm, >> 872 G4ThreeVector *n ) const 564 { 873 { 565 // Z intersections << 874 ESide side = kUndefined, snside = kUndefined; 566 // << 875 G4double snxt,pdist; 567 if ((std::abs(p.z()) - fDz) >= -halfCarToler << 876 G4double central,ss1,ss2,ds1,ds2,sn=0.,sn2=0.; >> 877 G4double tanxz=0.,cosxz=0.,tanyz=0.,cosyz=0.; >> 878 >> 879 if (calcNorm) *validNorm=true; // All normals are valid >> 880 >> 881 // Calculate z plane intersection >> 882 if (v.z()>0) 568 { 883 { 569 if (calcNorm) << 884 pdist=fDz-p.z(); >> 885 if (pdist>kCarTolerance/2) 570 { 886 { 571 *validNorm = true; << 887 snxt=pdist/v.z(); 572 n->set(0, 0, (p.z() < 0) ? -1 : 1); << 888 side=kPZ; >> 889 } >> 890 else >> 891 { >> 892 if (calcNorm) >> 893 { >> 894 *n=G4ThreeVector(0,0,1); >> 895 } >> 896 return snxt=0; 573 } 897 } 574 return 0; << 575 } 898 } 576 G4double vz = v.z(); << 899 else if (v.z()<0) 577 G4double tmax = (vz == 0) ? DBL_MAX : (std:: << 900 { 578 G4int iside = (vz < 0) ? -4 : -2; // little << 901 pdist=fDz+p.z(); >> 902 if (pdist>kCarTolerance/2) >> 903 { >> 904 snxt=-pdist/v.z(); >> 905 side=kMZ; >> 906 } >> 907 else >> 908 { >> 909 if (calcNorm) >> 910 { >> 911 *n=G4ThreeVector(0,0,-1); >> 912 } >> 913 return snxt=0; >> 914 } >> 915 } >> 916 else >> 917 { >> 918 snxt=kInfinity; >> 919 } 579 920 580 // Y intersections << 581 // 921 // 582 G4int i = 0; << 922 // Calculate x intersection 583 for ( ; i<2; ++i) << 923 // 584 { << 924 tanxz=(fDx2-fDx1)*0.5/fDz; 585 G4double cosa = fPlanes[i].b*v.y() + fPlan << 925 central=0.5*(fDx1+fDx2); 586 if (cosa > 0) << 926 >> 927 // +ve plane (1) >> 928 // >> 929 ss1=central+tanxz*p.z()-p.x(); // distance || x axis to plane >> 930 // (+ve if point inside) >> 931 ds1=v.x()-tanxz*v.z(); // component towards plane at +x >> 932 // (-ve if +ve -> -ve direction) >> 933 // -ve plane (2) >> 934 // >> 935 ss2=-tanxz*p.z()-p.x()-central; //distance || x axis to plane >> 936 // (-ve if point inside) >> 937 ds2=tanxz*v.z()+v.x(); // component towards plane at -x >> 938 >> 939 if (ss1>0&&ss2<0) >> 940 { >> 941 // Normal case - entirely inside region >> 942 if (ds1<=0&&ds2<0) >> 943 { >> 944 if (ss2<-kCarTolerance/2) >> 945 { >> 946 sn=ss2/ds2; // Leave by -ve side >> 947 snside=kMX; >> 948 } >> 949 else >> 950 { >> 951 sn=0; // Leave immediately by -ve side >> 952 snside=kMX; >> 953 } >> 954 } >> 955 else if (ds1>0&&ds2>=0) 587 { 956 { 588 G4double dist = fPlanes[i].b*p.y()+fPlan << 957 if (ss1>kCarTolerance/2) 589 if (dist >= -halfCarTolerance) << 958 { >> 959 sn=ss1/ds1; // Leave by +ve side >> 960 snside=kPX; >> 961 } >> 962 else 590 { 963 { 591 if (calcNorm) << 964 sn=0; // Leave immediately by +ve side >> 965 snside=kPX; >> 966 } >> 967 } >> 968 else if (ds1>0&&ds2<0) >> 969 { >> 970 if (ss1>kCarTolerance/2) >> 971 { >> 972 // sn=ss1/ds1; // Leave by +ve side >> 973 if (ss2<-kCarTolerance/2) 592 { 974 { 593 *validNorm = true; << 975 sn=ss1/ds1; // Leave by +ve side 594 n->set(0, fPlanes[i].b, fPlanes[i].c << 976 sn2=ss2/ds2; >> 977 if (sn2<sn) >> 978 { >> 979 sn=sn2; >> 980 snside=kMX; >> 981 } >> 982 else >> 983 { >> 984 snside=kPX; >> 985 } 595 } 986 } 596 return 0; << 987 else >> 988 { >> 989 sn=0; // Leave immediately by -ve >> 990 snside=kMX; >> 991 } >> 992 } >> 993 else >> 994 { >> 995 sn=0; // Leave immediately by +ve side >> 996 snside=kPX; >> 997 } >> 998 } >> 999 else >> 1000 { >> 1001 // Must be || to both >> 1002 // >> 1003 sn=kInfinity; // Don't leave by either side >> 1004 } >> 1005 } >> 1006 else if (ss1<=0&&ss2<0) >> 1007 { >> 1008 // Outside, in +ve Area >> 1009 >> 1010 if (ds1>0) >> 1011 { >> 1012 sn=0; // Away from shape >> 1013 // Left by +ve side >> 1014 snside=kPX; >> 1015 } >> 1016 else >> 1017 { >> 1018 if (ds2<0) >> 1019 { >> 1020 // Ignore +ve plane and use -ve plane intersect >> 1021 // >> 1022 sn=ss2/ds2; // Leave by -ve side >> 1023 snside=kMX; >> 1024 } >> 1025 else >> 1026 { >> 1027 // Must be || to both -> exit determined by other axes >> 1028 // >> 1029 sn=kInfinity; // Don't leave by either side 597 } 1030 } 598 G4double tmp = -dist/cosa; << 599 if (tmax > tmp) { tmax = tmp; iside = i; << 600 } 1031 } 601 } 1032 } >> 1033 else if (ss1>0&&ss2>=0) >> 1034 { >> 1035 // Outside, in -ve Area 602 1036 603 // X intersections << 1037 if (ds2<0) 604 // << 1038 { 605 for ( ; i<4; ++i) << 1039 sn=0; // away from shape >> 1040 // Left by -ve side >> 1041 snside=kMX; >> 1042 } >> 1043 else >> 1044 { >> 1045 if (ds1>0) >> 1046 { >> 1047 // Ignore +ve plane and use -ve plane intersect >> 1048 // >> 1049 sn=ss1/ds1; // Leave by +ve side >> 1050 snside=kPX; >> 1051 } >> 1052 else >> 1053 { >> 1054 // Must be || to both -> exit determined by other axes >> 1055 // >> 1056 sn=kInfinity; // Don't leave by either side >> 1057 } >> 1058 } >> 1059 } >> 1060 >> 1061 // Update minimum exit distance >> 1062 >> 1063 if (sn<snxt) 606 { 1064 { 607 G4double cosa = fPlanes[i].a*v.x()+fPlanes << 1065 snxt=sn; 608 if (cosa > 0) << 1066 side=snside; >> 1067 } >> 1068 if (snxt>0) >> 1069 { >> 1070 // Calculate y intersection >> 1071 >> 1072 tanyz=(fDy2-fDy1)*0.5/fDz; >> 1073 central=0.5*(fDy1+fDy2); >> 1074 >> 1075 // +ve plane (1) >> 1076 // >> 1077 ss1=central+tanyz*p.z()-p.y(); // distance || y axis to plane >> 1078 // (+ve if point inside) >> 1079 ds1=v.y()-tanyz*v.z(); // component towards +ve plane >> 1080 // (-ve if +ve -> -ve direction) >> 1081 // -ve plane (2) >> 1082 // >> 1083 ss2=-tanyz*p.z()-p.y()-central; // distance || y axis to plane >> 1084 // (-ve if point inside) >> 1085 ds2=tanyz*v.z()+v.y(); // component towards -ve plane >> 1086 >> 1087 if (ss1>0&&ss2<0) >> 1088 { >> 1089 // Normal case - entirely inside region >> 1090 >> 1091 if (ds1<=0&&ds2<0) >> 1092 { >> 1093 if (ss2<-kCarTolerance/2) >> 1094 { >> 1095 sn=ss2/ds2; // Leave by -ve side >> 1096 snside=kMY; >> 1097 } >> 1098 else >> 1099 { >> 1100 sn=0; // Leave immediately by -ve side >> 1101 snside=kMY; >> 1102 } >> 1103 } >> 1104 else if (ds1>0&&ds2>=0) >> 1105 { >> 1106 if (ss1>kCarTolerance/2) >> 1107 { >> 1108 sn=ss1/ds1; // Leave by +ve side >> 1109 snside=kPY; >> 1110 } >> 1111 else >> 1112 { >> 1113 sn=0; // Leave immediately by +ve side >> 1114 snside=kPY; >> 1115 } >> 1116 } >> 1117 else if (ds1>0&&ds2<0) >> 1118 { >> 1119 if (ss1>kCarTolerance/2) >> 1120 { >> 1121 // sn=ss1/ds1; // Leave by +ve side >> 1122 if (ss2<-kCarTolerance/2) >> 1123 { >> 1124 sn=ss1/ds1; // Leave by +ve side >> 1125 sn2=ss2/ds2; >> 1126 if (sn2<sn) >> 1127 { >> 1128 sn=sn2; >> 1129 snside=kMY; >> 1130 } >> 1131 else >> 1132 { >> 1133 snside=kPY; >> 1134 } >> 1135 } >> 1136 else >> 1137 { >> 1138 sn=0; // Leave immediately by -ve >> 1139 snside=kMY; >> 1140 } >> 1141 } >> 1142 else >> 1143 { >> 1144 sn=0; // Leave immediately by +ve side >> 1145 snside=kPY; >> 1146 } >> 1147 } >> 1148 else >> 1149 { >> 1150 // Must be || to both >> 1151 // >> 1152 sn=kInfinity; // Don't leave by either side >> 1153 } >> 1154 } >> 1155 else if (ss1<=0&&ss2<0) >> 1156 { >> 1157 // Outside, in +ve Area >> 1158 >> 1159 if (ds1>0) >> 1160 { >> 1161 sn=0; // Away from shape >> 1162 // Left by +ve side >> 1163 snside=kPY; >> 1164 } >> 1165 else >> 1166 { >> 1167 if (ds2<0) >> 1168 { >> 1169 // Ignore +ve plane and use -ve plane intersect >> 1170 // >> 1171 sn=ss2/ds2; // Leave by -ve side >> 1172 snside=kMY; >> 1173 } >> 1174 else >> 1175 { >> 1176 // Must be || to both -> exit determined by other axes >> 1177 // >> 1178 sn=kInfinity; // Don't leave by either side >> 1179 } >> 1180 } >> 1181 } >> 1182 else if (ss1>0&&ss2>=0) 609 { 1183 { 610 G4double dist = fPlanes[i].a*p.x()+fPlan << 1184 // Outside, in -ve Area 611 if (dist >= -halfCarTolerance) << 1185 if (ds2<0) 612 { 1186 { 613 if (calcNorm) << 1187 sn=0; // away from shape >> 1188 // Left by -ve side >> 1189 snside=kMY; >> 1190 } >> 1191 else >> 1192 { >> 1193 if (ds1>0) >> 1194 { >> 1195 // Ignore +ve plane and use -ve plane intersect >> 1196 // >> 1197 sn=ss1/ds1; // Leave by +ve side >> 1198 snside=kPY; >> 1199 } >> 1200 else 614 { 1201 { 615 *validNorm = true; << 1202 // Must be || to both -> exit determined by other axes 616 n->set(fPlanes[i].a, fPlanes[i].b, << 1203 // >> 1204 sn=kInfinity; // Don't leave by either side 617 } 1205 } 618 return 0; << 619 } 1206 } 620 G4double tmp = -dist/cosa; << 1207 } 621 if (tmax > tmp) { tmax = tmp; iside = i; << 1208 >> 1209 // Update minimum exit distance >> 1210 >> 1211 if (sn<snxt) >> 1212 { >> 1213 snxt=sn; >> 1214 side=snside; 622 } 1215 } 623 } 1216 } 624 1217 625 // Set normal, if required, and return dista << 626 // << 627 if (calcNorm) 1218 if (calcNorm) 628 { 1219 { 629 *validNorm = true; << 1220 switch (side) 630 if (iside < 0) << 1221 { 631 n->set(0, 0, iside + 3); // (-4+3)=-1, ( << 1222 case kPX: 632 else << 1223 cosxz=1.0/std::sqrt(1.0+tanxz*tanxz); 633 n->set(fPlanes[iside].a, fPlanes[iside]. << 1224 *n=G4ThreeVector(cosxz,0,-tanxz*cosxz); >> 1225 break; >> 1226 case kMX: >> 1227 cosxz=-1.0/std::sqrt(1.0+tanxz*tanxz); >> 1228 *n=G4ThreeVector(cosxz,0,tanxz*cosxz); >> 1229 break; >> 1230 case kPY: >> 1231 cosyz=1.0/std::sqrt(1.0+tanyz*tanyz); >> 1232 *n=G4ThreeVector(0,cosyz,-tanyz*cosyz); >> 1233 break; >> 1234 case kMY: >> 1235 cosyz=-1.0/std::sqrt(1.0+tanyz*tanyz); >> 1236 *n=G4ThreeVector(0,cosyz,tanyz*cosyz); >> 1237 break; >> 1238 case kPZ: >> 1239 *n=G4ThreeVector(0,0,1); >> 1240 break; >> 1241 case kMZ: >> 1242 *n=G4ThreeVector(0,0,-1); >> 1243 break; >> 1244 default: >> 1245 DumpInfo(); >> 1246 G4Exception("G4Trd::DistanceToOut(p,v,..)", >> 1247 "GeomSolids1002", JustWarning, >> 1248 "Undefined side for valid surface normal to solid."); >> 1249 break; >> 1250 } 634 } 1251 } 635 return tmax; << 1252 return snxt; 636 } 1253 } 637 1254 638 ////////////////////////////////////////////// << 1255 /////////////////////////////////////////////////////////////////////////// 639 // 1256 // 640 // Calculate exact shortest distance to any bo 1257 // Calculate exact shortest distance to any boundary from inside 641 // - returns 0 if point is outside << 1258 // - Returns 0 is point outside 642 1259 643 G4double G4Trd::DistanceToOut( const G4ThreeVe 1260 G4double G4Trd::DistanceToOut( const G4ThreeVector& p ) const 644 { 1261 { >> 1262 G4double safe=0.0; >> 1263 G4double tanxz,xdist,saf1; >> 1264 G4double tanyz,ydist,saf2; >> 1265 G4double zbase; >> 1266 645 #ifdef G4CSGDEBUG 1267 #ifdef G4CSGDEBUG 646 if( Inside(p) == kOutside ) 1268 if( Inside(p) == kOutside ) 647 { 1269 { 648 std::ostringstream message; << 1270 G4int oldprc = G4cout.precision(16) ; 649 G4long oldprc = message.precision(16); << 1271 G4cout << G4endl ; 650 message << "Point p is outside (!?) of sol << 1272 DumpInfo(); 651 message << "Position:\n"; << 1273 G4cout << "Position:" << G4endl << G4endl ; 652 message << " p.x() = " << p.x()/mm << " << 1274 G4cout << "p.x() = " << p.x()/mm << " mm" << G4endl ; 653 message << " p.y() = " << p.y()/mm << " << 1275 G4cout << "p.y() = " << p.y()/mm << " mm" << G4endl ; 654 message << " p.z() = " << p.z()/mm << " << 1276 G4cout << "p.z() = " << p.z()/mm << " mm" << G4endl << G4endl ; 655 G4cout.precision(oldprc); << 1277 G4cout.precision(oldprc) ; 656 G4Exception("G4Trd::DistanceToOut(p)", "Ge << 1278 G4Exception("G4Trd::DistanceToOut(p)", "GeomSolids1002", JustWarning, 657 JustWarning, message ); << 1279 "Point p is outside !?" ); 658 DumpInfo(); << 659 } 1280 } 660 #endif 1281 #endif 661 G4double dx = fPlanes[3].a*std::abs(p.x())+f << 662 G4double dy = fPlanes[1].b*std::abs(p.y())+f << 663 G4double dxy = std::max(dx,dy); << 664 1282 665 G4double dz = std::abs(p.z())-fDz; << 1283 safe=fDz-std::fabs(p.z()); // z perpendicular Dist 666 G4double dist = std::max(dz,dxy); << 1284 >> 1285 zbase=fDz+p.z(); >> 1286 >> 1287 // xdist = distance perpendicular to z axis to closest x plane from p >> 1288 // = (x half width of shape at p.z) - std::fabs(p.x) >> 1289 // >> 1290 tanxz=(fDx2-fDx1)*0.5/fDz; >> 1291 xdist=fDx1+tanxz*zbase-std::fabs(p.x()); >> 1292 saf1=xdist/std::sqrt(1.0+tanxz*tanxz); // x*std::cos(ang_xz) = >> 1293 // shortest (perpendicular) >> 1294 // distance to plane >> 1295 tanyz=(fDy2-fDy1)*0.5/fDz; >> 1296 ydist=fDy1+tanyz*zbase-std::fabs(p.y()); >> 1297 saf2=ydist/std::sqrt(1.0+tanyz*tanyz); 667 1298 668 return (dist < 0) ? -dist : 0.; << 1299 // Return minimum x/y/z distance >> 1300 // >> 1301 if (safe>saf1) safe=saf1; >> 1302 if (safe>saf2) safe=saf2; >> 1303 >> 1304 if (safe<0) safe=0; >> 1305 return safe; 669 } 1306 } 670 1307 671 ////////////////////////////////////////////// << 1308 //////////////////////////////////////////////////////////////////////////// 672 // 1309 // 673 // GetEntityType << 1310 // Create a List containing the transformed vertices 674 << 1311 // Ordering [0-3] -fDz cross section 675 G4GeometryType G4Trd::GetEntityType() const << 1312 // [4-7] +fDz cross section such that [0] is below [4], 676 { << 1313 // [1] below [5] etc. 677 return {"G4Trd"}; << 1314 // Note: >> 1315 // Caller has deletion resposibility >> 1316 >> 1317 G4ThreeVectorList* >> 1318 G4Trd::CreateRotatedVertices( const G4AffineTransform& pTransform ) const >> 1319 { >> 1320 G4ThreeVectorList *vertices; >> 1321 vertices=new G4ThreeVectorList(); >> 1322 if (vertices) >> 1323 { >> 1324 vertices->reserve(8); >> 1325 G4ThreeVector vertex0(-fDx1,-fDy1,-fDz); >> 1326 G4ThreeVector vertex1(fDx1,-fDy1,-fDz); >> 1327 G4ThreeVector vertex2(fDx1,fDy1,-fDz); >> 1328 G4ThreeVector vertex3(-fDx1,fDy1,-fDz); >> 1329 G4ThreeVector vertex4(-fDx2,-fDy2,fDz); >> 1330 G4ThreeVector vertex5(fDx2,-fDy2,fDz); >> 1331 G4ThreeVector vertex6(fDx2,fDy2,fDz); >> 1332 G4ThreeVector vertex7(-fDx2,fDy2,fDz); >> 1333 >> 1334 vertices->push_back(pTransform.TransformPoint(vertex0)); >> 1335 vertices->push_back(pTransform.TransformPoint(vertex1)); >> 1336 vertices->push_back(pTransform.TransformPoint(vertex2)); >> 1337 vertices->push_back(pTransform.TransformPoint(vertex3)); >> 1338 vertices->push_back(pTransform.TransformPoint(vertex4)); >> 1339 vertices->push_back(pTransform.TransformPoint(vertex5)); >> 1340 vertices->push_back(pTransform.TransformPoint(vertex6)); >> 1341 vertices->push_back(pTransform.TransformPoint(vertex7)); >> 1342 } >> 1343 else >> 1344 { >> 1345 DumpInfo(); >> 1346 G4Exception("G4Trd::CreateRotatedVertices()", >> 1347 "GeomSolids0003", FatalException, >> 1348 "Error in allocation of vertices. Out of memory !"); >> 1349 } >> 1350 return vertices; 678 } 1351 } 679 1352 680 ////////////////////////////////////////////// 1353 ////////////////////////////////////////////////////////////////////////// 681 // 1354 // 682 // IsFaceted << 1355 // GetEntityType 683 1356 684 G4bool G4Trd::IsFaceted() const << 1357 G4GeometryType G4Trd::GetEntityType() const 685 { 1358 { 686 return true; << 1359 return G4String("G4Trd"); 687 } 1360 } 688 1361 689 ////////////////////////////////////////////// 1362 ////////////////////////////////////////////////////////////////////////// 690 // 1363 // 691 // Make a clone of the object 1364 // Make a clone of the object 692 // 1365 // 693 G4VSolid* G4Trd::Clone() const 1366 G4VSolid* G4Trd::Clone() const 694 { 1367 { 695 return new G4Trd(*this); 1368 return new G4Trd(*this); 696 } 1369 } 697 1370 698 ////////////////////////////////////////////// 1371 ////////////////////////////////////////////////////////////////////////// 699 // 1372 // 700 // Stream object contents to an output stream 1373 // Stream object contents to an output stream 701 1374 702 std::ostream& G4Trd::StreamInfo( std::ostream& 1375 std::ostream& G4Trd::StreamInfo( std::ostream& os ) const 703 { 1376 { 704 G4long oldprc = os.precision(16); << 1377 G4int oldprc = os.precision(16); 705 os << "------------------------------------- 1378 os << "-----------------------------------------------------------\n" 706 << " *** Dump for solid - " << GetName 1379 << " *** Dump for solid - " << GetName() << " ***\n" 707 << " ================================= 1380 << " ===================================================\n" 708 << " Solid type: G4Trd\n" 1381 << " Solid type: G4Trd\n" 709 << " Parameters: \n" 1382 << " Parameters: \n" 710 << " half length X, surface -dZ: " << 1383 << " half length X, surface -dZ: " << fDx1/mm << " mm \n" 711 << " half length X, surface +dZ: " << 1384 << " half length X, surface +dZ: " << fDx2/mm << " mm \n" 712 << " half length Y, surface -dZ: " << 1385 << " half length Y, surface -dZ: " << fDy1/mm << " mm \n" 713 << " half length Y, surface +dZ: " << 1386 << " half length Y, surface +dZ: " << fDy2/mm << " mm \n" 714 << " half length Z : " << << 1387 << " half length Z : " << fDz/mm << " mm \n" 715 << "------------------------------------- 1388 << "-----------------------------------------------------------\n"; 716 os.precision(oldprc); 1389 os.precision(oldprc); 717 1390 718 return os; 1391 return os; 719 } 1392 } 720 1393 721 ////////////////////////////////////////////// << 1394 >> 1395 //////////////////////////////////////////////////////////////////////// 722 // 1396 // 723 // Return a point randomly and uniformly selec << 1397 // GetPointOnSurface >> 1398 // >> 1399 // Return a point (G4ThreeVector) randomly and uniformly >> 1400 // selected on the solid surface 724 1401 725 G4ThreeVector G4Trd::GetPointOnSurface() const 1402 G4ThreeVector G4Trd::GetPointOnSurface() const 726 { 1403 { 727 // Set areas << 1404 G4double px, py, pz, tgX, tgY, secX, secY, select, sumS, tmp; 728 // << 1405 G4double Sxy1, Sxy2, Sxy, Sxz, Syz; 729 G4double sxz = (fDx1 + fDx2)*fHx; << 730 G4double syz = (fDy1 + fDy2)*fHy; << 731 G4double ssurf[6] = { 4.*fDx1*fDy1, sxz, sxz << 732 ssurf[1] += ssurf[0]; << 733 ssurf[2] += ssurf[1]; << 734 ssurf[3] += ssurf[2]; << 735 ssurf[4] += ssurf[3]; << 736 ssurf[5] += ssurf[4]; << 737 << 738 // Select face << 739 // << 740 G4double select = ssurf[5]*G4QuickRand(); << 741 G4int k = 5; << 742 k -= (G4int)(select <= ssurf[4]); << 743 k -= (G4int)(select <= ssurf[3]); << 744 k -= (G4int)(select <= ssurf[2]); << 745 k -= (G4int)(select <= ssurf[1]); << 746 k -= (G4int)(select <= ssurf[0]); << 747 1406 748 // Generate point on selected surface << 1407 tgX = 0.5*(fDx2-fDx1)/fDz; 749 // << 1408 secX = std::sqrt(1+tgX*tgX); 750 G4double u = G4QuickRand(); << 1409 tgY = 0.5*(fDy2-fDy1)/fDz; 751 G4double v = G4QuickRand(); << 1410 secY = std::sqrt(1+tgY*tgY); 752 switch(k) << 1411 >> 1412 // calculate 0.25 of side surfaces, sumS is 0.25 of total surface >> 1413 >> 1414 Sxy1 = fDx1*fDy1; >> 1415 Sxy2 = fDx2*fDy2; >> 1416 Sxy = Sxy1 + Sxy2; >> 1417 Sxz = (fDx1 + fDx2)*fDz*secY; >> 1418 Syz = (fDy1 + fDy2)*fDz*secX; >> 1419 sumS = Sxy + Sxz + Syz; >> 1420 >> 1421 select = sumS*G4UniformRand(); >> 1422 >> 1423 if( select < Sxy ) // Sxy1 or Sxy2 753 { 1424 { 754 case 0: // base at -Z << 1425 if( select < Sxy1 ) 755 { << 756 return { (2.*u - 1.)*fDx1, (2.*v - 1.)*f << 757 } << 758 case 1: // X face at -Y << 759 { << 760 if (u + v > 1.) { u = 1. - u; v = 1. - v << 761 G4ThreeVector p0(-fDx1,-fDy1,-fDz); << 762 G4ThreeVector p1( fDx2,-fDy2, fDz); << 763 return (select <= ssurf[0] + fDx1*fHx) ? << 764 (1. - u - v)*p0 + u*p1 + v*G4ThreeVect << 765 (1. - u - v)*p0 + u*p1 + v*G4ThreeVect << 766 } << 767 case 2: // X face at +Y << 768 { << 769 if (u + v > 1.) { u = 1. - u; v = 1. - v << 770 G4ThreeVector p0( fDx1, fDy1,-fDz); << 771 G4ThreeVector p1(-fDx2, fDy2, fDz); << 772 return (select <= ssurf[1] + fDx1*fHx) ? << 773 (1. - u - v)*p0 + u*p1 + v*G4ThreeVect << 774 (1. - u - v)*p0 + u*p1 + v*G4ThreeVect << 775 } << 776 case 3: // Y face at -X << 777 { << 778 if (u + v > 1.) { u = 1. - u; v = 1. - v << 779 G4ThreeVector p0(-fDx1, fDy1,-fDz); << 780 G4ThreeVector p1(-fDx2,-fDy2, fDz); << 781 return (select <= ssurf[2] + fDy1*fHy) ? << 782 (1. - u - v)*p0 + u*p1 + v*G4ThreeVect << 783 (1. - u - v)*p0 + u*p1 + v*G4ThreeVect << 784 } << 785 case 4: // Y face at +X << 786 { 1426 { 787 if (u + v > 1.) { u = 1. - u; v = 1. - v << 1427 pz = -fDz; 788 G4ThreeVector p0( fDx1,-fDy1,-fDz); << 1428 px = -fDx1 + 2*fDx1*G4UniformRand(); 789 G4ThreeVector p1( fDx2, fDy2, fDz); << 1429 py = -fDy1 + 2*fDy1*G4UniformRand(); 790 return (select <= ssurf[3] + fDy1*fHy) ? << 791 (1. - u - v)*p0 + u*p1 + v*G4ThreeVect << 792 (1. - u - v)*p0 + u*p1 + v*G4ThreeVect << 793 } 1430 } 794 case 5: // base at +Z << 1431 else 795 { 1432 { 796 return { (2.*u - 1.)*fDx2, (2.*v - 1.)*f << 1433 pz = fDz; >> 1434 px = -fDx2 + 2*fDx2*G4UniformRand(); >> 1435 py = -fDy2 + 2*fDy2*G4UniformRand(); 797 } 1436 } 798 } 1437 } 799 return {0., 0., 0.}; << 1438 else if ( ( select - Sxy ) < Sxz ) // Sxz >> 1439 { >> 1440 pz = -fDz + 2*fDz*G4UniformRand(); >> 1441 tmp = fDx1 + (pz + fDz)*tgX; >> 1442 px = -tmp + 2*tmp*G4UniformRand(); >> 1443 tmp = fDy1 + (pz + fDz)*tgY; >> 1444 >> 1445 if(G4UniformRand() > 0.5) { py = tmp; } >> 1446 else { py = -tmp; } >> 1447 } >> 1448 else // Syz >> 1449 { >> 1450 pz = -fDz + 2*fDz*G4UniformRand(); >> 1451 tmp = fDy1 + (pz + fDz)*tgY; >> 1452 py = -tmp + 2*tmp*G4UniformRand(); >> 1453 tmp = fDx1 + (pz + fDz)*tgX; >> 1454 >> 1455 if(G4UniformRand() > 0.5) { px = tmp; } >> 1456 else { px = -tmp; } >> 1457 } >> 1458 return G4ThreeVector(px,py,pz); 800 } 1459 } 801 1460 802 ////////////////////////////////////////////// << 1461 /////////////////////////////////////////////////////////////////////// 803 // 1462 // 804 // Methods for visualisation 1463 // Methods for visualisation 805 1464 806 void G4Trd::DescribeYourselfTo ( G4VGraphicsSc 1465 void G4Trd::DescribeYourselfTo ( G4VGraphicsScene& scene ) const 807 { 1466 { 808 scene.AddSolid (*this); 1467 scene.AddSolid (*this); 809 } 1468 } 810 1469 811 G4Polyhedron* G4Trd::CreatePolyhedron () const 1470 G4Polyhedron* G4Trd::CreatePolyhedron () const 812 { 1471 { 813 return new G4PolyhedronTrd2 (fDx1, fDx2, fDy 1472 return new G4PolyhedronTrd2 (fDx1, fDx2, fDy1, fDy2, fDz); 814 } 1473 } 815 1474 816 #endif 1475 #endif 817 1476