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