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 // Class G4VoxelLimits implementation 26 // Class G4VoxelLimits implementation 27 // 27 // 28 // 13.07.95, P.Kent - Initial version 28 // 13.07.95, P.Kent - Initial version 29 // ------------------------------------------- 29 // -------------------------------------------------------------------- 30 30 31 #include "G4VoxelLimits.hh" 31 #include "G4VoxelLimits.hh" 32 32 33 #include "G4ios.hh" 33 #include "G4ios.hh" 34 34 35 ////////////////////////////////////////////// 35 /////////////////////////////////////////////////////////////////////////// 36 // 36 // >> 37 // Empty constructor and destructor >> 38 // >> 39 G4VoxelLimits::G4VoxelLimits() >> 40 { >> 41 } >> 42 >> 43 G4VoxelLimits::~G4VoxelLimits() >> 44 { >> 45 } >> 46 >> 47 /////////////////////////////////////////////////////////////////////////// >> 48 // 37 // Further restrict limits 49 // Further restrict limits 38 // No checks for illegal restrictions 50 // No checks for illegal restrictions 39 // 51 // 40 void G4VoxelLimits::AddLimit( const EAxis pAxi 52 void G4VoxelLimits::AddLimit( const EAxis pAxis, 41 const G4double p 53 const G4double pMin, 42 const G4double p 54 const G4double pMax ) 43 { 55 { 44 if ( pAxis == kXAxis ) 56 if ( pAxis == kXAxis ) 45 { 57 { 46 if ( pMin > fxAxisMin ) fxAxisMin = pMin ; 58 if ( pMin > fxAxisMin ) fxAxisMin = pMin ; 47 if ( pMax < fxAxisMax ) fxAxisMax = pMax ; 59 if ( pMax < fxAxisMax ) fxAxisMax = pMax ; 48 } 60 } 49 else if ( pAxis == kYAxis ) 61 else if ( pAxis == kYAxis ) 50 { 62 { 51 if ( pMin > fyAxisMin ) fyAxisMin = pMin ; 63 if ( pMin > fyAxisMin ) fyAxisMin = pMin ; 52 if ( pMax < fyAxisMax ) fyAxisMax = pMax ; 64 if ( pMax < fyAxisMax ) fyAxisMax = pMax ; 53 } 65 } 54 else 66 else 55 { 67 { 56 assert( pAxis == kZAxis ) ; 68 assert( pAxis == kZAxis ) ; 57 69 58 if ( pMin > fzAxisMin ) fzAxisMin = pMin ; 70 if ( pMin > fzAxisMin ) fzAxisMin = pMin ; 59 if ( pMax < fzAxisMax ) fzAxisMax = pMax ; 71 if ( pMax < fzAxisMax ) fzAxisMax = pMax ; 60 } 72 } 61 } 73 } 62 74 63 ////////////////////////////////////////////// 75 /////////////////////////////////////////////////////////////////////////// 64 // 76 // 65 // ClipToLimits 77 // ClipToLimits 66 // 78 // 67 // Clip the line segment pStart->pEnd to the v 79 // Clip the line segment pStart->pEnd to the volume described by the 68 // current limits. Return true if the line rem 80 // current limits. Return true if the line remains after clipping, 69 // else false, and leave the vectors in an und 81 // else false, and leave the vectors in an undefined state. 70 // 82 // 71 // Process: 83 // Process: 72 // 84 // 73 // Use Cohen-Sutherland clipping in 3D 85 // Use Cohen-Sutherland clipping in 3D 74 // [Fundamentals of Interactive Computer Graph 86 // [Fundamentals of Interactive Computer Graphics,Foley & Van Dam] 75 // 87 // 76 G4bool G4VoxelLimits::ClipToLimits( G4ThreeVec 88 G4bool G4VoxelLimits::ClipToLimits( G4ThreeVector& pStart, 77 G4ThreeVec 89 G4ThreeVector& pEnd ) const 78 { 90 { 79 G4int sCode, eCode ; 91 G4int sCode, eCode ; 80 G4bool remainsAfterClip ; 92 G4bool remainsAfterClip ; 81 93 82 // Determine if line is trivially inside (bo 94 // Determine if line is trivially inside (both outcodes==0) or outside 83 // (logical AND of outcodes !=0) 95 // (logical AND of outcodes !=0) 84 96 85 sCode = OutCode(pStart) ; 97 sCode = OutCode(pStart) ; 86 eCode = OutCode(pEnd) ; 98 eCode = OutCode(pEnd) ; 87 99 88 if ( (sCode & eCode) != 0 ) << 100 if ( sCode & eCode ) 89 { 101 { 90 // Trivially outside, no intersection with 102 // Trivially outside, no intersection with region 91 103 92 remainsAfterClip = false; 104 remainsAfterClip = false; 93 } 105 } 94 else if ( sCode == 0 && eCode == 0 ) 106 else if ( sCode == 0 && eCode == 0 ) 95 { 107 { 96 // Trivially inside, no intersections 108 // Trivially inside, no intersections 97 109 98 remainsAfterClip = true ; 110 remainsAfterClip = true ; 99 } 111 } 100 else 112 else 101 { 113 { 102 // Line segment *may* cut volume boundarie 114 // Line segment *may* cut volume boundaries 103 // At most, one end point is inside 115 // At most, one end point is inside 104 116 105 G4double x1, y1, z1, x2, y2, z2 ; 117 G4double x1, y1, z1, x2, y2, z2 ; 106 118 107 x1 = pStart.x() ; 119 x1 = pStart.x() ; 108 y1 = pStart.y() ; 120 y1 = pStart.y() ; 109 z1 = pStart.z() ; 121 z1 = pStart.z() ; 110 122 111 x2 = pEnd.x() ; 123 x2 = pEnd.x() ; 112 y2 = pEnd.y() ; 124 y2 = pEnd.y() ; 113 z2 = pEnd.z() ; 125 z2 = pEnd.z() ; 114 126 115 while ( sCode != eCode ) // Loop checking 127 while ( sCode != eCode ) // Loop checking, 06.08.2015, G.Cosmo 116 { 128 { 117 // Copy vectors to work variables x1-z1, 129 // Copy vectors to work variables x1-z1,x2-z2 118 // Ensure x1-z1 lies outside volume, swa 130 // Ensure x1-z1 lies outside volume, swapping vectors and outcodes 119 // if necessary 131 // if necessary 120 132 121 if ( sCode != 0 ) << 133 if ( sCode ) 122 { 134 { 123 if ( (sCode & 0x01) != 0 ) // Clip ag << 135 if ( sCode & 0x01 ) // Clip against fxAxisMin 124 { 136 { 125 z1 += (fxAxisMin-x1)*(z2-z1)/(x2-x1) 137 z1 += (fxAxisMin-x1)*(z2-z1)/(x2-x1); 126 y1 += (fxAxisMin-x1)*(y2-y1)/(x2-x1) 138 y1 += (fxAxisMin-x1)*(y2-y1)/(x2-x1); 127 x1 = fxAxisMin; 139 x1 = fxAxisMin; 128 } 140 } 129 else if ( (sCode & 0x02) != 0 ) // Cli << 141 else if ( sCode & 0x02 ) // Clip against fxAxisMax 130 { 142 { 131 z1 += (fxAxisMax-x1)*(z2-z1)/(x2-x1) 143 z1 += (fxAxisMax-x1)*(z2-z1)/(x2-x1); 132 y1 += (fxAxisMax-x1)*(y2-y1)/(x2-x1) 144 y1 += (fxAxisMax-x1)*(y2-y1)/(x2-x1); 133 x1 = fxAxisMax ; 145 x1 = fxAxisMax ; 134 } 146 } 135 else if ( (sCode & 0x04) != 0 ) // Cl << 147 else if ( sCode & 0x04 ) // Clip against fyAxisMin 136 { 148 { 137 x1 += (fyAxisMin-y1)*(x2-x1)/(y2-y1) 149 x1 += (fyAxisMin-y1)*(x2-x1)/(y2-y1); 138 z1 += (fyAxisMin-y1)*(z2-z1)/(y2-y1) 150 z1 += (fyAxisMin-y1)*(z2-z1)/(y2-y1); 139 y1 = fyAxisMin; 151 y1 = fyAxisMin; 140 } 152 } 141 else if ( (sCode & 0x08) != 0 ) // Cl << 153 else if ( sCode & 0x08 ) // Clip against fyAxisMax 142 { 154 { 143 x1 += (fyAxisMax-y1)*(x2-x1)/(y2-y1) 155 x1 += (fyAxisMax-y1)*(x2-x1)/(y2-y1); 144 z1 += (fyAxisMax-y1)*(z2-z1)/(y2-y1) 156 z1 += (fyAxisMax-y1)*(z2-z1)/(y2-y1); 145 y1 = fyAxisMax; 157 y1 = fyAxisMax; 146 } 158 } 147 else if ( (sCode & 0x10) != 0 ) // Cl << 159 else if ( sCode & 0x10 ) // Clip against fzAxisMin 148 { 160 { 149 x1 += (fzAxisMin-z1)*(x2-x1)/(z2-z1) 161 x1 += (fzAxisMin-z1)*(x2-x1)/(z2-z1); 150 y1 += (fzAxisMin-z1)*(y2-y1)/(z2-z1) 162 y1 += (fzAxisMin-z1)*(y2-y1)/(z2-z1); 151 z1 = fzAxisMin; 163 z1 = fzAxisMin; 152 } 164 } 153 else if ( (sCode & 0x20) != 0 ) // Cl << 165 else if ( sCode & 0x20 ) // Clip against fzAxisMax 154 { 166 { 155 x1 += (fzAxisMax-z1)*(x2-x1)/(z2-z1) 167 x1 += (fzAxisMax-z1)*(x2-x1)/(z2-z1); 156 y1 += (fzAxisMax-z1)*(y2-y1)/(z2-z1) 168 y1 += (fzAxisMax-z1)*(y2-y1)/(z2-z1); 157 z1 = fzAxisMax; 169 z1 = fzAxisMax; 158 } 170 } 159 } 171 } 160 if ( eCode != 0 ) // Clip 2nd end: repe << 172 if ( eCode ) // Clip 2nd end: repeat of 1st, but 1<>2 161 { 173 { 162 if ( (eCode & 0x01) != 0 ) // Clip ag << 174 if ( eCode & 0x01 ) // Clip against fxAxisMin 163 { 175 { 164 z2 += (fxAxisMin-x2)*(z1-z2)/(x1-x2) 176 z2 += (fxAxisMin-x2)*(z1-z2)/(x1-x2); 165 y2 += (fxAxisMin-x2)*(y1-y2)/(x1-x2) 177 y2 += (fxAxisMin-x2)*(y1-y2)/(x1-x2); 166 x2 = fxAxisMin; 178 x2 = fxAxisMin; 167 } 179 } 168 else if ( (eCode & 0x02) != 0 ) // Cl << 180 else if ( eCode & 0x02 ) // Clip against fxAxisMax 169 { 181 { 170 z2 += (fxAxisMax-x2)*(z1-z2)/(x1-x2) 182 z2 += (fxAxisMax-x2)*(z1-z2)/(x1-x2); 171 y2 += (fxAxisMax-x2)*(y1-y2)/(x1-x2) 183 y2 += (fxAxisMax-x2)*(y1-y2)/(x1-x2); 172 x2 = fxAxisMax; 184 x2 = fxAxisMax; 173 } 185 } 174 else if ( (eCode & 0x04) != 0 ) // Cl << 186 else if ( eCode & 0x04 ) // Clip against fyAxisMin 175 { 187 { 176 x2 += (fyAxisMin-y2)*(x1-x2)/(y1-y2) 188 x2 += (fyAxisMin-y2)*(x1-x2)/(y1-y2); 177 z2 += (fyAxisMin-y2)*(z1-z2)/(y1-y2) 189 z2 += (fyAxisMin-y2)*(z1-z2)/(y1-y2); 178 y2 = fyAxisMin; 190 y2 = fyAxisMin; 179 } 191 } 180 else if ((eCode&0x08) != 0) // Clip a << 192 else if (eCode&0x08) // Clip against fyAxisMax 181 { 193 { 182 x2 += (fyAxisMax-y2)*(x1-x2)/(y1-y2) 194 x2 += (fyAxisMax-y2)*(x1-x2)/(y1-y2); 183 z2 += (fyAxisMax-y2)*(z1-z2)/(y1-y2) 195 z2 += (fyAxisMax-y2)*(z1-z2)/(y1-y2); 184 y2 = fyAxisMax; 196 y2 = fyAxisMax; 185 } 197 } 186 else if ( (eCode & 0x10) != 0 ) // Cl << 198 else if ( eCode & 0x10 ) // Clip against fzAxisMin 187 { 199 { 188 x2 += (fzAxisMin-z2)*(x1-x2)/(z1-z2) 200 x2 += (fzAxisMin-z2)*(x1-x2)/(z1-z2); 189 y2 += (fzAxisMin-z2)*(y1-y2)/(z1-z2) 201 y2 += (fzAxisMin-z2)*(y1-y2)/(z1-z2); 190 z2 = fzAxisMin; 202 z2 = fzAxisMin; 191 } 203 } 192 else if ( (eCode & 0x20) != 0 ) // Cl << 204 else if ( eCode & 0x20 ) // Clip against fzAxisMax 193 { 205 { 194 x2 += (fzAxisMax-z2)*(x1-x2)/(z1-z2) 206 x2 += (fzAxisMax-z2)*(x1-x2)/(z1-z2); 195 y2 += (fzAxisMax-z2)*(y1-y2)/(z1-z2) 207 y2 += (fzAxisMax-z2)*(y1-y2)/(z1-z2); 196 z2 = fzAxisMax; 208 z2 = fzAxisMax; 197 } 209 } 198 } 210 } 199 pStart = G4ThreeVector(x1,y1,z1); 211 pStart = G4ThreeVector(x1,y1,z1); 200 pEnd = G4ThreeVector(x2,y2,z2); 212 pEnd = G4ThreeVector(x2,y2,z2); 201 sCode = OutCode(pStart); 213 sCode = OutCode(pStart); 202 eCode = OutCode(pEnd); 214 eCode = OutCode(pEnd); 203 } 215 } 204 remainsAfterClip = sCode == 0 && eCode == << 216 if ( sCode == 0 && eCode == 0 ) remainsAfterClip = true; >> 217 else remainsAfterClip = false; 205 } 218 } 206 return remainsAfterClip; 219 return remainsAfterClip; 207 } 220 } 208 221 209 ////////////////////////////////////////////// 222 //////////////////////////////////////////////////////////////////////////// 210 // 223 // 211 // Calculate the `outcode' for the specified v 224 // Calculate the `outcode' for the specified vector: 212 // The following bits are set: 225 // The following bits are set: 213 // 0 pVec.x()<fxAxisMin && IsXLimited() 226 // 0 pVec.x()<fxAxisMin && IsXLimited() 214 // 1 pVec.x()>fxAxisMax && IsXLimited() 227 // 1 pVec.x()>fxAxisMax && IsXLimited() 215 // 2 pVec.y()<fyAxisMin && IsYLimited() 228 // 2 pVec.y()<fyAxisMin && IsYLimited() 216 // 3 pVec.y()>fyAxisMax && IsYLimited() 229 // 3 pVec.y()>fyAxisMax && IsYLimited() 217 // 4 pVec.z()<fzAxisMin && IsZLimited() 230 // 4 pVec.z()<fzAxisMin && IsZLimited() 218 // 5 pVec.z()>fzAxisMax && IsZLimited() 231 // 5 pVec.z()>fzAxisMax && IsZLimited() 219 // 232 // 220 G4int G4VoxelLimits::OutCode( const G4ThreeVec 233 G4int G4VoxelLimits::OutCode( const G4ThreeVector& pVec ) const 221 { 234 { 222 G4int code = 0 ; // The outco 235 G4int code = 0 ; // The outcode 223 236 224 if ( IsXLimited() ) 237 if ( IsXLimited() ) 225 { 238 { 226 if ( pVec.x() < fxAxisMin ) code |= 0x01 ; 239 if ( pVec.x() < fxAxisMin ) code |= 0x01 ; 227 if ( pVec.x() > fxAxisMax ) code |= 0x02 ; 240 if ( pVec.x() > fxAxisMax ) code |= 0x02 ; 228 } 241 } 229 if ( IsYLimited() ) 242 if ( IsYLimited() ) 230 { 243 { 231 if ( pVec.y() < fyAxisMin ) code |= 0x04 ; 244 if ( pVec.y() < fyAxisMin ) code |= 0x04 ; 232 if ( pVec.y() > fyAxisMax ) code |= 0x08 ; 245 if ( pVec.y() > fyAxisMax ) code |= 0x08 ; 233 } 246 } 234 if (IsZLimited()) 247 if (IsZLimited()) 235 { 248 { 236 if ( pVec.z() < fzAxisMin ) code |= 0x10 ; 249 if ( pVec.z() < fzAxisMin ) code |= 0x10 ; 237 if ( pVec.z() > fzAxisMax ) code |= 0x20 ; 250 if ( pVec.z() > fzAxisMax ) code |= 0x20 ; 238 } 251 } 239 return code; 252 return code; 240 } 253 } 241 254 242 ////////////////////////////////////////////// 255 /////////////////////////////////////////////////////////////////////////////// 243 256 244 std::ostream& operator << (std::ostream& os, c 257 std::ostream& operator << (std::ostream& os, const G4VoxelLimits& pLim) 245 { 258 { 246 os << "{"; 259 os << "{"; 247 if (pLim.IsXLimited()) 260 if (pLim.IsXLimited()) 248 { 261 { 249 os << "(" << pLim.GetMinXExtent() 262 os << "(" << pLim.GetMinXExtent() 250 << "," << pLim.GetMaxXExtent() 263 << "," << pLim.GetMaxXExtent() << ") "; 251 } 264 } 252 else 265 else 253 { 266 { 254 os << "(-,-) "; 267 os << "(-,-) "; 255 } 268 } 256 if (pLim.IsYLimited()) 269 if (pLim.IsYLimited()) 257 { 270 { 258 os << "(" << pLim.GetMinYExtent() 271 os << "(" << pLim.GetMinYExtent() 259 << "," << pLim.GetMaxYExtent() 272 << "," << pLim.GetMaxYExtent() << ") "; 260 } 273 } 261 else 274 else 262 { 275 { 263 os << "(-,-) "; 276 os << "(-,-) "; 264 } 277 } 265 if (pLim.IsZLimited()) 278 if (pLim.IsZLimited()) 266 { 279 { 267 os << "(" << pLim.GetMinZExtent() 280 os << "(" << pLim.GetMinZExtent() 268 << "," << pLim.GetMaxZExtent() 281 << "," << pLim.GetMaxZExtent() << ")"; 269 } 282 } 270 else 283 else 271 { 284 { 272 os << "(-,-)"; 285 os << "(-,-)"; 273 } 286 } 274 os << "}"; 287 os << "}"; 275 return os; 288 return os; 276 } 289 } 277 290