Geant4 Cross Reference |
1 // 1 // 2 // ******************************************* 2 // ******************************************************************** 3 // * License and Disclaimer << 3 // * DISCLAIMER * 4 // * 4 // * * 5 // * The Geant4 software is copyright of th << 5 // * The following disclaimer summarizes all the specific disclaimers * 6 // * the Geant4 Collaboration. It is provided << 6 // * of contributors to this software. The specific disclaimers,which * 7 // * conditions of the Geant4 Software License << 7 // * govern, are listed with their locations in: * 8 // * LICENSE and available at http://cern.ch/ << 8 // * http://cern.ch/geant4/license * 9 // * include a list of copyright holders. << 10 // * 9 // * * 11 // * Neither the authors of this software syst 10 // * Neither the authors of this software system, nor their employing * 12 // * institutes,nor the agencies providing fin 11 // * institutes,nor the agencies providing financial support for this * 13 // * work make any representation or warran 12 // * work make any representation or warranty, express or implied, * 14 // * regarding this software system or assum 13 // * regarding this software system or assume any liability for its * 15 // * use. Please see the license in the file << 14 // * use. * 16 // * for the full disclaimer and the limitatio << 17 // * 15 // * * 18 // * This code implementation is the result << 16 // * This code implementation is the intellectual property of the * 19 // * technical work of the GEANT4 collaboratio << 17 // * GEANT4 collaboration. * 20 // * By using, copying, modifying or distri << 18 // * By copying, distributing or modifying the Program (or any work * 21 // * any work based on the software) you ag << 19 // * based on the Program) you indicate your acceptance of this * 22 // * use in resulting scientific publicati << 20 // * statement, and all its terms. * 23 // * acceptance of all terms of the Geant4 Sof << 24 // ******************************************* 21 // ******************************************************************** 25 // 22 // 26 // Class G4VoxelLimits implementation << 27 // 23 // 28 // 13.07.95, P.Kent - Initial version << 24 // $Id: G4VoxelLimits.cc,v 1.10 2004/12/02 09:31:22 gcosmo Exp $ >> 25 // GEANT4 tag $Name: geant4-07-01 $ >> 26 // >> 27 // class G4VoxelLimits >> 28 // >> 29 // Implementation >> 30 // >> 31 // History: >> 32 // >> 33 // 14.03.02 V. Grichine, cosmetics >> 34 // 13.07.95 P.Kent Initial version 29 // ------------------------------------------- 35 // -------------------------------------------------------------------- 30 36 31 #include "G4VoxelLimits.hh" 37 #include "G4VoxelLimits.hh" 32 38 33 #include "G4ios.hh" 39 #include "G4ios.hh" 34 40 35 ////////////////////////////////////////////// 41 /////////////////////////////////////////////////////////////////////////// 36 // 42 // >> 43 // Empty constructor and destructor >> 44 // >> 45 >> 46 G4VoxelLimits::G4VoxelLimits() >> 47 : fxAxisMin(-kInfinity),fxAxisMax(kInfinity), >> 48 fyAxisMin(-kInfinity),fyAxisMax(kInfinity), >> 49 fzAxisMin(-kInfinity),fzAxisMax(kInfinity) >> 50 { >> 51 } >> 52 >> 53 G4VoxelLimits::~G4VoxelLimits() >> 54 { >> 55 } >> 56 >> 57 /////////////////////////////////////////////////////////////////////////// >> 58 // 37 // Further restrict limits 59 // Further restrict limits 38 // No checks for illegal restrictions 60 // No checks for illegal restrictions 39 // 61 // >> 62 40 void G4VoxelLimits::AddLimit( const EAxis pAxi 63 void G4VoxelLimits::AddLimit( const EAxis pAxis, 41 const G4double p 64 const G4double pMin, 42 const G4double p 65 const G4double pMax ) 43 { 66 { 44 if ( pAxis == kXAxis ) 67 if ( pAxis == kXAxis ) 45 { 68 { 46 if ( pMin > fxAxisMin ) fxAxisMin = pMin ; 69 if ( pMin > fxAxisMin ) fxAxisMin = pMin ; 47 if ( pMax < fxAxisMax ) fxAxisMax = pMax ; 70 if ( pMax < fxAxisMax ) fxAxisMax = pMax ; 48 } 71 } 49 else if ( pAxis == kYAxis ) 72 else if ( pAxis == kYAxis ) 50 { 73 { 51 if ( pMin > fyAxisMin ) fyAxisMin = pMin ; 74 if ( pMin > fyAxisMin ) fyAxisMin = pMin ; 52 if ( pMax < fyAxisMax ) fyAxisMax = pMax ; 75 if ( pMax < fyAxisMax ) fyAxisMax = pMax ; 53 } 76 } 54 else 77 else 55 { 78 { 56 assert( pAxis == kZAxis ) ; 79 assert( pAxis == kZAxis ) ; 57 80 58 if ( pMin > fzAxisMin ) fzAxisMin = pMin ; 81 if ( pMin > fzAxisMin ) fzAxisMin = pMin ; 59 if ( pMax < fzAxisMax ) fzAxisMax = pMax ; 82 if ( pMax < fzAxisMax ) fzAxisMax = pMax ; 60 } 83 } 61 } 84 } 62 85 63 ////////////////////////////////////////////// 86 /////////////////////////////////////////////////////////////////////////// 64 // 87 // 65 // ClipToLimits 88 // ClipToLimits 66 // 89 // 67 // Clip the line segment pStart->pEnd to the v 90 // Clip the line segment pStart->pEnd to the volume described by the 68 // current limits. Return true if the line rem 91 // current limits. Return true if the line remains after clipping, 69 // else false, and leave the vectors in an und 92 // else false, and leave the vectors in an undefined state. 70 // 93 // 71 // Process: 94 // Process: 72 // 95 // 73 // Use Cohen-Sutherland clipping in 3D 96 // Use Cohen-Sutherland clipping in 3D 74 // [Fundamentals of Interactive Computer Graph 97 // [Fundamentals of Interactive Computer Graphics,Foley & Van Dam] 75 // 98 // >> 99 76 G4bool G4VoxelLimits::ClipToLimits( G4ThreeVec 100 G4bool G4VoxelLimits::ClipToLimits( G4ThreeVector& pStart, 77 G4ThreeVec 101 G4ThreeVector& pEnd ) const 78 { 102 { 79 G4int sCode, eCode ; 103 G4int sCode, eCode ; 80 G4bool remainsAfterClip ; 104 G4bool remainsAfterClip ; 81 105 82 // Determine if line is trivially inside (bo 106 // Determine if line is trivially inside (both outcodes==0) or outside 83 // (logical AND of outcodes !=0) 107 // (logical AND of outcodes !=0) 84 108 85 sCode = OutCode(pStart) ; 109 sCode = OutCode(pStart) ; 86 eCode = OutCode(pEnd) ; 110 eCode = OutCode(pEnd) ; 87 111 88 if ( (sCode & eCode) != 0 ) << 112 if ( sCode & eCode ) 89 { 113 { 90 // Trivially outside, no intersection with 114 // Trivially outside, no intersection with region 91 115 92 remainsAfterClip = false; 116 remainsAfterClip = false; 93 } 117 } 94 else if ( sCode == 0 && eCode == 0 ) 118 else if ( sCode == 0 && eCode == 0 ) 95 { 119 { 96 // Trivially inside, no intersections 120 // Trivially inside, no intersections 97 121 98 remainsAfterClip = true ; 122 remainsAfterClip = true ; 99 } 123 } 100 else 124 else 101 { 125 { 102 // Line segment *may* cut volume boundarie 126 // Line segment *may* cut volume boundaries 103 // At most, one end point is inside 127 // At most, one end point is inside 104 128 105 G4double x1, y1, z1, x2, y2, z2 ; 129 G4double x1, y1, z1, x2, y2, z2 ; 106 130 107 x1 = pStart.x() ; 131 x1 = pStart.x() ; 108 y1 = pStart.y() ; 132 y1 = pStart.y() ; 109 z1 = pStart.z() ; 133 z1 = pStart.z() ; 110 134 111 x2 = pEnd.x() ; 135 x2 = pEnd.x() ; 112 y2 = pEnd.y() ; 136 y2 = pEnd.y() ; 113 z2 = pEnd.z() ; 137 z2 = pEnd.z() ; 114 << 138 /* 115 while ( sCode != eCode ) // Loop checking << 139 if( std::abs(x1-x2) < kCarTolerance*kCarTolerance) >> 140 { >> 141 G4cout<<"x1 = "<<x1<<"\t"<<"x2 = "<<x2<<G4endl; >> 142 } >> 143 if( std::abs(y1-y2) < kCarTolerance*kCarTolerance) >> 144 { >> 145 G4cout<<"y1 = "<<y1<<"\t"<<"y2 = "<<y2<<G4endl; >> 146 } >> 147 if( std::abs(z1-z2) < kCarTolerance*kCarTolerance) >> 148 { >> 149 G4cout<<"z1 = "<<z1<<"\t"<<"z2 = "<<z2<<G4endl; >> 150 } >> 151 */ >> 152 while ( sCode != eCode ) 116 { 153 { 117 // Copy vectors to work variables x1-z1, 154 // Copy vectors to work variables x1-z1,x2-z2 118 // Ensure x1-z1 lies outside volume, swa 155 // Ensure x1-z1 lies outside volume, swapping vectors and outcodes 119 // if necessary 156 // if necessary 120 157 121 if ( sCode != 0 ) << 158 if ( sCode ) 122 { 159 { 123 if ( (sCode & 0x01) != 0 ) // Clip ag << 160 if ( sCode & 0x01 ) // Clip against fxAxisMin 124 { 161 { 125 z1 += (fxAxisMin-x1)*(z2-z1)/(x2-x1) 162 z1 += (fxAxisMin-x1)*(z2-z1)/(x2-x1); 126 y1 += (fxAxisMin-x1)*(y2-y1)/(x2-x1) 163 y1 += (fxAxisMin-x1)*(y2-y1)/(x2-x1); 127 x1 = fxAxisMin; 164 x1 = fxAxisMin; 128 } 165 } 129 else if ( (sCode & 0x02) != 0 ) // Cli << 166 else if ( sCode & 0x02 ) // Clip against fxAxisMax 130 { 167 { 131 z1 += (fxAxisMax-x1)*(z2-z1)/(x2-x1) 168 z1 += (fxAxisMax-x1)*(z2-z1)/(x2-x1); 132 y1 += (fxAxisMax-x1)*(y2-y1)/(x2-x1) 169 y1 += (fxAxisMax-x1)*(y2-y1)/(x2-x1); 133 x1 = fxAxisMax ; 170 x1 = fxAxisMax ; 134 } 171 } 135 else if ( (sCode & 0x04) != 0 ) // Cl << 172 else if ( sCode & 0x04 ) // Clip against fyAxisMin 136 { 173 { 137 x1 += (fyAxisMin-y1)*(x2-x1)/(y2-y1) 174 x1 += (fyAxisMin-y1)*(x2-x1)/(y2-y1); 138 z1 += (fyAxisMin-y1)*(z2-z1)/(y2-y1) 175 z1 += (fyAxisMin-y1)*(z2-z1)/(y2-y1); 139 y1 = fyAxisMin; 176 y1 = fyAxisMin; 140 } 177 } 141 else if ( (sCode & 0x08) != 0 ) // Cl << 178 else if ( sCode & 0x08 ) // Clip against fyAxisMax 142 { 179 { 143 x1 += (fyAxisMax-y1)*(x2-x1)/(y2-y1) 180 x1 += (fyAxisMax-y1)*(x2-x1)/(y2-y1); 144 z1 += (fyAxisMax-y1)*(z2-z1)/(y2-y1) 181 z1 += (fyAxisMax-y1)*(z2-z1)/(y2-y1); 145 y1 = fyAxisMax; 182 y1 = fyAxisMax; 146 } 183 } 147 else if ( (sCode & 0x10) != 0 ) // Cl << 184 else if ( sCode & 0x10 ) // Clip against fzAxisMin 148 { 185 { 149 x1 += (fzAxisMin-z1)*(x2-x1)/(z2-z1) 186 x1 += (fzAxisMin-z1)*(x2-x1)/(z2-z1); 150 y1 += (fzAxisMin-z1)*(y2-y1)/(z2-z1) 187 y1 += (fzAxisMin-z1)*(y2-y1)/(z2-z1); 151 z1 = fzAxisMin; 188 z1 = fzAxisMin; 152 } 189 } 153 else if ( (sCode & 0x20) != 0 ) // Cl << 190 else if ( sCode & 0x20 ) // Clip against fzAxisMax 154 { 191 { 155 x1 += (fzAxisMax-z1)*(x2-x1)/(z2-z1) 192 x1 += (fzAxisMax-z1)*(x2-x1)/(z2-z1); 156 y1 += (fzAxisMax-z1)*(y2-y1)/(z2-z1) 193 y1 += (fzAxisMax-z1)*(y2-y1)/(z2-z1); 157 z1 = fzAxisMax; 194 z1 = fzAxisMax; 158 } 195 } 159 } 196 } 160 if ( eCode != 0 ) // Clip 2nd end: repe << 197 if ( eCode ) // Clip 2nd end: repeat of 1st, but 1<>2 161 { 198 { 162 if ( (eCode & 0x01) != 0 ) // Clip ag << 199 if ( eCode & 0x01 ) // Clip against fxAxisMin 163 { 200 { 164 z2 += (fxAxisMin-x2)*(z1-z2)/(x1-x2) 201 z2 += (fxAxisMin-x2)*(z1-z2)/(x1-x2); 165 y2 += (fxAxisMin-x2)*(y1-y2)/(x1-x2) 202 y2 += (fxAxisMin-x2)*(y1-y2)/(x1-x2); 166 x2 = fxAxisMin; 203 x2 = fxAxisMin; 167 } 204 } 168 else if ( (eCode & 0x02) != 0 ) // Cl << 205 else if ( eCode & 0x02 ) // Clip against fxAxisMax 169 { 206 { 170 z2 += (fxAxisMax-x2)*(z1-z2)/(x1-x2) 207 z2 += (fxAxisMax-x2)*(z1-z2)/(x1-x2); 171 y2 += (fxAxisMax-x2)*(y1-y2)/(x1-x2) 208 y2 += (fxAxisMax-x2)*(y1-y2)/(x1-x2); 172 x2 = fxAxisMax; 209 x2 = fxAxisMax; 173 } 210 } 174 else if ( (eCode & 0x04) != 0 ) // Cl << 211 else if ( eCode & 0x04 ) // Clip against fyAxisMin 175 { 212 { 176 x2 += (fyAxisMin-y2)*(x1-x2)/(y1-y2) 213 x2 += (fyAxisMin-y2)*(x1-x2)/(y1-y2); 177 z2 += (fyAxisMin-y2)*(z1-z2)/(y1-y2) 214 z2 += (fyAxisMin-y2)*(z1-z2)/(y1-y2); 178 y2 = fyAxisMin; 215 y2 = fyAxisMin; 179 } 216 } 180 else if ((eCode&0x08) != 0) // Clip a << 217 else if (eCode&0x08) // Clip against fyAxisMax 181 { 218 { 182 x2 += (fyAxisMax-y2)*(x1-x2)/(y1-y2) 219 x2 += (fyAxisMax-y2)*(x1-x2)/(y1-y2); 183 z2 += (fyAxisMax-y2)*(z1-z2)/(y1-y2) 220 z2 += (fyAxisMax-y2)*(z1-z2)/(y1-y2); 184 y2 = fyAxisMax; 221 y2 = fyAxisMax; 185 } 222 } 186 else if ( (eCode & 0x10) != 0 ) // Cl << 223 else if ( eCode & 0x10 ) // Clip against fzAxisMin 187 { 224 { 188 x2 += (fzAxisMin-z2)*(x1-x2)/(z1-z2) 225 x2 += (fzAxisMin-z2)*(x1-x2)/(z1-z2); 189 y2 += (fzAxisMin-z2)*(y1-y2)/(z1-z2) 226 y2 += (fzAxisMin-z2)*(y1-y2)/(z1-z2); 190 z2 = fzAxisMin; 227 z2 = fzAxisMin; 191 } 228 } 192 else if ( (eCode & 0x20) != 0 ) // Cl << 229 else if ( eCode & 0x20 ) // Clip against fzAxisMax 193 { 230 { 194 x2 += (fzAxisMax-z2)*(x1-x2)/(z1-z2) 231 x2 += (fzAxisMax-z2)*(x1-x2)/(z1-z2); 195 y2 += (fzAxisMax-z2)*(y1-y2)/(z1-z2) 232 y2 += (fzAxisMax-z2)*(y1-y2)/(z1-z2); 196 z2 = fzAxisMax; 233 z2 = fzAxisMax; 197 } 234 } 198 } 235 } >> 236 // G4endl; G4cout<<"x1 = "<<x1<<"\t"<<"x2 = "<<x2<<G4endl<<G4endl; 199 pStart = G4ThreeVector(x1,y1,z1); 237 pStart = G4ThreeVector(x1,y1,z1); 200 pEnd = G4ThreeVector(x2,y2,z2); 238 pEnd = G4ThreeVector(x2,y2,z2); 201 sCode = OutCode(pStart); 239 sCode = OutCode(pStart); 202 eCode = OutCode(pEnd); 240 eCode = OutCode(pEnd); 203 } 241 } 204 remainsAfterClip = sCode == 0 && eCode == << 242 if ( sCode == 0 && eCode == 0 ) remainsAfterClip = true; >> 243 else remainsAfterClip = false; 205 } 244 } 206 return remainsAfterClip; 245 return remainsAfterClip; 207 } 246 } 208 247 209 ////////////////////////////////////////////// 248 //////////////////////////////////////////////////////////////////////////// 210 // 249 // 211 // Calculate the `outcode' for the specified v 250 // Calculate the `outcode' for the specified vector: 212 // The following bits are set: 251 // The following bits are set: 213 // 0 pVec.x()<fxAxisMin && IsXLimited() 252 // 0 pVec.x()<fxAxisMin && IsXLimited() 214 // 1 pVec.x()>fxAxisMax && IsXLimited() 253 // 1 pVec.x()>fxAxisMax && IsXLimited() 215 // 2 pVec.y()<fyAxisMin && IsYLimited() 254 // 2 pVec.y()<fyAxisMin && IsYLimited() 216 // 3 pVec.y()>fyAxisMax && IsYLimited() 255 // 3 pVec.y()>fyAxisMax && IsYLimited() 217 // 4 pVec.z()<fzAxisMin && IsZLimited() 256 // 4 pVec.z()<fzAxisMin && IsZLimited() 218 // 5 pVec.z()>fzAxisMax && IsZLimited() 257 // 5 pVec.z()>fzAxisMax && IsZLimited() 219 // 258 // >> 259 220 G4int G4VoxelLimits::OutCode( const G4ThreeVec 260 G4int G4VoxelLimits::OutCode( const G4ThreeVector& pVec ) const 221 { 261 { 222 G4int code = 0 ; // The outco 262 G4int code = 0 ; // The outcode 223 263 224 if ( IsXLimited() ) 264 if ( IsXLimited() ) 225 { 265 { 226 if ( pVec.x() < fxAxisMin ) code |= 0x01 ; 266 if ( pVec.x() < fxAxisMin ) code |= 0x01 ; 227 if ( pVec.x() > fxAxisMax ) code |= 0x02 ; 267 if ( pVec.x() > fxAxisMax ) code |= 0x02 ; 228 } 268 } 229 if ( IsYLimited() ) 269 if ( IsYLimited() ) 230 { 270 { 231 if ( pVec.y() < fyAxisMin ) code |= 0x04 ; 271 if ( pVec.y() < fyAxisMin ) code |= 0x04 ; 232 if ( pVec.y() > fyAxisMax ) code |= 0x08 ; 272 if ( pVec.y() > fyAxisMax ) code |= 0x08 ; 233 } 273 } 234 if (IsZLimited()) 274 if (IsZLimited()) 235 { 275 { 236 if ( pVec.z() < fzAxisMin ) code |= 0x10 ; 276 if ( pVec.z() < fzAxisMin ) code |= 0x10 ; 237 if ( pVec.z() > fzAxisMax ) code |= 0x20 ; 277 if ( pVec.z() > fzAxisMax ) code |= 0x20 ; 238 } 278 } 239 return code; 279 return code; 240 } 280 } 241 281 242 ////////////////////////////////////////////// 282 /////////////////////////////////////////////////////////////////////////////// 243 283 244 std::ostream& operator << (std::ostream& os, c 284 std::ostream& operator << (std::ostream& os, const G4VoxelLimits& pLim) 245 { 285 { 246 os << "{"; 286 os << "{"; 247 if (pLim.IsXLimited()) 287 if (pLim.IsXLimited()) 248 { 288 { 249 os << "(" << pLim.GetMinXExtent() 289 os << "(" << pLim.GetMinXExtent() 250 << "," << pLim.GetMaxXExtent() 290 << "," << pLim.GetMaxXExtent() << ") "; 251 } 291 } 252 else 292 else 253 { 293 { 254 os << "(-,-) "; 294 os << "(-,-) "; 255 } 295 } 256 if (pLim.IsYLimited()) 296 if (pLim.IsYLimited()) 257 { 297 { 258 os << "(" << pLim.GetMinYExtent() 298 os << "(" << pLim.GetMinYExtent() 259 << "," << pLim.GetMaxYExtent() 299 << "," << pLim.GetMaxYExtent() << ") "; 260 } 300 } 261 else 301 else 262 { 302 { 263 os << "(-,-) "; 303 os << "(-,-) "; 264 } 304 } 265 if (pLim.IsZLimited()) 305 if (pLim.IsZLimited()) 266 { 306 { 267 os << "(" << pLim.GetMinZExtent() 307 os << "(" << pLim.GetMinZExtent() 268 << "," << pLim.GetMaxZExtent() 308 << "," << pLim.GetMaxZExtent() << ")"; 269 } 309 } 270 else 310 else 271 { 311 { 272 os << "(-,-)"; 312 os << "(-,-)"; 273 } 313 } 274 os << "}"; 314 os << "}"; 275 return os; 315 return os; 276 } 316 } 277 317