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 // G4ReduciblePolygon implementation; a utilit << 26 // 27 // test, reduce, and/or otherwise manipulate a << 27 // $Id: G4ReduciblePolygon.cc,v 1.11 2006/06/29 18:48:53 gunter Exp $ >> 28 // GEANT4 tag $Name: geant4-08-02 $ >> 29 // >> 30 // >> 31 // -------------------------------------------------------------------- >> 32 // GEANT 4 class source file >> 33 // >> 34 // >> 35 // G4ReduciblePolygon.cc >> 36 // >> 37 // Implementation of a utility class used to specify, test, reduce, >> 38 // and/or otherwise manipulate a 2D polygon. >> 39 // 28 // See G4ReduciblePolygon.hh for more info. 40 // See G4ReduciblePolygon.hh for more info. 29 // 41 // 30 // Author: David C. Williams (davidw@scipp.ucs << 31 // ------------------------------------------- 42 // -------------------------------------------------------------------- 32 43 33 #include "G4ReduciblePolygon.hh" 44 #include "G4ReduciblePolygon.hh" 34 #include "globals.hh" 45 #include "globals.hh" 35 46 >> 47 // 36 // Constructor: with simple arrays 48 // Constructor: with simple arrays 37 // 49 // 38 G4ReduciblePolygon::G4ReduciblePolygon( const 50 G4ReduciblePolygon::G4ReduciblePolygon( const G4double a[], 39 const 51 const G4double b[], 40 52 G4int n ) 41 : aMin(0.), aMax(0.), bMin(0.), bMax(0.) << 53 : aMin(0.), aMax(0.), bMin(0.), bMax(0.), >> 54 vertexHead(0) 42 { 55 { 43 // 56 // 44 // Do all of the real work in Create 57 // Do all of the real work in Create 45 // 58 // 46 Create( a, b, n ); 59 Create( a, b, n ); 47 } 60 } 48 61 >> 62 >> 63 // 49 // Constructor: special PGON/PCON case 64 // Constructor: special PGON/PCON case 50 // 65 // 51 G4ReduciblePolygon::G4ReduciblePolygon( const 66 G4ReduciblePolygon::G4ReduciblePolygon( const G4double rmin[], 52 const 67 const G4double rmax[], 53 const 68 const G4double z[], G4int n ) 54 : aMin(0.), aMax(0.), bMin(0.), bMax(0.) << 69 : aMin(0.), aMax(0.), bMin(0.), bMax(0.), >> 70 vertexHead(0) 55 { 71 { 56 // 72 // 57 // Translate 73 // Translate 58 // 74 // 59 auto a = new G4double[n*2]; << 75 G4double *a = new G4double[n*2]; 60 auto b = new G4double[n*2]; << 76 G4double *b = new G4double[n*2]; 61 77 62 G4double *rOut = a + n, 78 G4double *rOut = a + n, 63 *zOut = b + n, 79 *zOut = b + n, 64 *rIn = rOut-1, 80 *rIn = rOut-1, 65 *zIn = zOut-1; 81 *zIn = zOut-1; 66 82 67 for( G4int i=0; i < n; ++i, ++rOut, ++zOut, << 83 G4int i; >> 84 for( i=0; i < n; i++, rOut++, zOut++, rIn--, zIn-- ) 68 { 85 { 69 *rOut = rmax[i]; 86 *rOut = rmax[i]; 70 *rIn = rmin[i]; 87 *rIn = rmin[i]; 71 *zOut = *zIn = z[i]; 88 *zOut = *zIn = z[i]; 72 } 89 } 73 90 74 Create( a, b, n*2 ); 91 Create( a, b, n*2 ); 75 92 76 delete [] a; 93 delete [] a; 77 delete [] b; 94 delete [] b; 78 } 95 } 79 96 >> 97 >> 98 // 80 // Create 99 // Create 81 // 100 // 82 // To be called by constructors, fill in the l 101 // To be called by constructors, fill in the list and statistics for a new 83 // polygon 102 // polygon 84 // 103 // 85 void G4ReduciblePolygon::Create( const G4doubl 104 void G4ReduciblePolygon::Create( const G4double a[], 86 const G4doubl 105 const G4double b[], G4int n ) 87 { 106 { 88 if (n<3) 107 if (n<3) 89 G4Exception("G4ReduciblePolygon::Create()", << 108 G4Exception("G4ReduciblePolygon::Create()", "WrongArgumentValue", 90 FatalErrorInArgument, "Less tha << 109 FatalException, "Less than 3 vertices specified."); 91 110 92 const G4double *anext = a, *bnext = b; 111 const G4double *anext = a, *bnext = b; 93 ABVertex* prev = nullptr; << 112 ABVertex *prev = 0; 94 do // Loop checking, 13.08.2015, G.Cosmo << 113 do 95 { 114 { 96 auto newVertex = new ABVertex; << 115 ABVertex *newVertex = new ABVertex; 97 newVertex->a = *anext; 116 newVertex->a = *anext; 98 newVertex->b = *bnext; 117 newVertex->b = *bnext; 99 newVertex->next = nullptr; << 118 newVertex->next = 0; 100 if (prev==nullptr) << 119 if (prev==0) 101 { 120 { 102 vertexHead = newVertex; 121 vertexHead = newVertex; 103 } 122 } 104 else 123 else 105 { 124 { 106 prev->next = newVertex; 125 prev->next = newVertex; 107 } 126 } 108 127 109 prev = newVertex; 128 prev = newVertex; 110 } while( ++anext, ++bnext < b+n ); 129 } while( ++anext, ++bnext < b+n ); 111 130 112 numVertices = n; 131 numVertices = n; 113 132 114 CalculateMaxMin(); 133 CalculateMaxMin(); 115 } 134 } 116 135 >> 136 >> 137 // 117 // Fake default constructor - sets only member 138 // Fake default constructor - sets only member data and allocates memory 118 // for usage restri 139 // for usage restricted to object persistency. 119 // 140 // 120 G4ReduciblePolygon::G4ReduciblePolygon( __void 141 G4ReduciblePolygon::G4ReduciblePolygon( __void__& ) 121 : aMin(0.), aMax(0.), bMin(0.), bMax(0.) << 142 : aMin(0.), aMax(0.), bMin(0.), bMax(0.), vertexHead(0) 122 { 143 { 123 } 144 } 124 145 125 146 126 // 147 // 127 // Destructor 148 // Destructor 128 // 149 // 129 G4ReduciblePolygon::~G4ReduciblePolygon() 150 G4ReduciblePolygon::~G4ReduciblePolygon() 130 { 151 { 131 ABVertex* curr = vertexHead; << 152 ABVertex *curr = vertexHead; 132 while( curr != nullptr ) // Loop checking << 153 while( curr ) 133 { 154 { 134 ABVertex* toDelete = curr; << 155 ABVertex *toDelete = curr; 135 curr = curr->next; 156 curr = curr->next; 136 delete toDelete; 157 delete toDelete; 137 } 158 } 138 } 159 } 139 160 >> 161 >> 162 // 140 // CopyVertices 163 // CopyVertices 141 // 164 // 142 // Copy contents into simple linear arrays. 165 // Copy contents into simple linear arrays. 143 // ***** CAUTION ***** Be care to declare the 166 // ***** CAUTION ***** Be care to declare the arrays to a large 144 // enough size! 167 // enough size! 145 // 168 // 146 void G4ReduciblePolygon::CopyVertices( G4doubl 169 void G4ReduciblePolygon::CopyVertices( G4double a[], G4double b[] ) const 147 { 170 { 148 G4double *anext = a, *bnext = b; 171 G4double *anext = a, *bnext = b; 149 ABVertex *curr = vertexHead; 172 ABVertex *curr = vertexHead; 150 while( curr != nullptr ) // Loop checking << 173 while( curr ) 151 { 174 { 152 *anext++ = curr->a; 175 *anext++ = curr->a; 153 *bnext++ = curr->b; 176 *bnext++ = curr->b; 154 curr = curr->next; 177 curr = curr->next; 155 } 178 } 156 } 179 } 157 180 >> 181 >> 182 // 158 // ScaleA 183 // ScaleA 159 // 184 // 160 // Multiply all a values by a common scale 185 // Multiply all a values by a common scale 161 // 186 // 162 void G4ReduciblePolygon::ScaleA( G4double scal 187 void G4ReduciblePolygon::ScaleA( G4double scale ) 163 { 188 { 164 ABVertex* curr = vertexHead; << 189 ABVertex *curr = vertexHead; 165 while( curr != nullptr ) // Loop checking << 190 while( curr ) 166 { 191 { 167 curr->a *= scale; 192 curr->a *= scale; 168 curr = curr->next; 193 curr = curr->next; 169 } 194 } 170 } 195 } 171 196 >> 197 >> 198 // 172 // ScaleB 199 // ScaleB 173 // 200 // 174 // Multiply all b values by a common scale 201 // Multiply all b values by a common scale 175 // 202 // 176 void G4ReduciblePolygon::ScaleB( G4double scal 203 void G4ReduciblePolygon::ScaleB( G4double scale ) 177 { 204 { 178 ABVertex* curr = vertexHead; << 205 ABVertex *curr = vertexHead; 179 while( curr != nullptr ) // Loop checking << 206 while( curr ) 180 { 207 { 181 curr->b *= scale; 208 curr->b *= scale; 182 curr = curr->next; 209 curr = curr->next; 183 } 210 } 184 } 211 } 185 212 >> 213 >> 214 // 186 // RemoveDuplicateVertices 215 // RemoveDuplicateVertices 187 // 216 // 188 // Remove adjacent vertices that are equal. Re 217 // Remove adjacent vertices that are equal. Returns "false" if there 189 // is a problem (too few vertices remaining). 218 // is a problem (too few vertices remaining). 190 // 219 // 191 G4bool G4ReduciblePolygon::RemoveDuplicateVert 220 G4bool G4ReduciblePolygon::RemoveDuplicateVertices( G4double tolerance ) 192 { 221 { 193 ABVertex *curr = vertexHead, 222 ABVertex *curr = vertexHead, 194 *prev = nullptr, *next = nullptr; << 223 *prev = 0, 195 while( curr != nullptr ) // Loop checking << 224 *next = curr->next; // A little dangerous >> 225 while( curr ) 196 { 226 { 197 next = curr->next; 227 next = curr->next; 198 if (next == nullptr) next = vertexHead; << 228 if (next == 0) next = vertexHead; 199 229 200 if (std::fabs(curr->a-next->a) < tolerance 230 if (std::fabs(curr->a-next->a) < tolerance && 201 std::fabs(curr->b-next->b) < tolerance 231 std::fabs(curr->b-next->b) < tolerance ) 202 { 232 { 203 // 233 // 204 // Duplicate found: do we have > 3 verti 234 // Duplicate found: do we have > 3 vertices? 205 // 235 // 206 if (numVertices <= 3) 236 if (numVertices <= 3) 207 { 237 { 208 CalculateMaxMin(); 238 CalculateMaxMin(); 209 return false; 239 return false; 210 } 240 } 211 241 212 // 242 // 213 // Delete 243 // Delete 214 // 244 // 215 ABVertex* toDelete = curr; << 245 ABVertex *toDelete = curr; 216 curr = curr->next; 246 curr = curr->next; 217 delete toDelete; 247 delete toDelete; 218 248 219 numVertices--; 249 numVertices--; 220 250 221 if (prev != nullptr) << 251 if (prev) prev->next = curr; else vertexHead = curr; 222 prev->next = curr; << 223 else << 224 vertexHead = curr; << 225 } 252 } 226 else 253 else 227 { 254 { 228 prev = curr; 255 prev = curr; 229 curr = curr->next; 256 curr = curr->next; 230 } 257 } 231 } 258 } 232 259 233 // 260 // 234 // In principle, this is not needed, but why 261 // In principle, this is not needed, but why not just play it safe? 235 // 262 // 236 CalculateMaxMin(); 263 CalculateMaxMin(); 237 264 238 return true; 265 return true; 239 } 266 } 240 267 >> 268 >> 269 // 241 // RemoveRedundantVertices 270 // RemoveRedundantVertices 242 // 271 // 243 // Remove any unneeded vertices, i.e. those ve 272 // Remove any unneeded vertices, i.e. those vertices which 244 // are on the line connecting the previous and 273 // are on the line connecting the previous and next vertices. 245 // 274 // 246 G4bool G4ReduciblePolygon::RemoveRedundantVert 275 G4bool G4ReduciblePolygon::RemoveRedundantVertices( G4double tolerance ) 247 { 276 { 248 // 277 // 249 // Under these circumstances, we can quit no 278 // Under these circumstances, we can quit now! 250 // 279 // 251 if (numVertices <= 2) return false; 280 if (numVertices <= 2) return false; 252 281 253 G4double tolerance2 = tolerance*tolerance; 282 G4double tolerance2 = tolerance*tolerance; 254 283 255 // 284 // 256 // Loop over all vertices 285 // Loop over all vertices 257 // 286 // 258 ABVertex *curr = vertexHead, *next = nullptr << 287 ABVertex *curr = vertexHead, 259 while( curr != nullptr ) // Loop checking << 288 *next = curr->next; // A little dangerous >> 289 while( curr ) 260 { 290 { 261 next = curr->next; 291 next = curr->next; 262 if (next == nullptr) next = vertexHead; << 292 if (next == 0) next = vertexHead; 263 293 264 G4double da = next->a - curr->a, 294 G4double da = next->a - curr->a, 265 db = next->b - curr->b; 295 db = next->b - curr->b; 266 296 267 // 297 // 268 // Loop over all subsequent vertices, up t 298 // Loop over all subsequent vertices, up to curr 269 // 299 // 270 for(;;) 300 for(;;) 271 { 301 { 272 // 302 // 273 // Get vertex after next 303 // Get vertex after next 274 // 304 // 275 ABVertex* test = next->next; << 305 ABVertex *test = next->next; 276 if (test == nullptr) test = vertexHead; << 306 if (test == 0) test = vertexHead; 277 307 278 // 308 // 279 // If we are back to the original vertex 309 // If we are back to the original vertex, stop 280 // 310 // 281 if (test==curr) break; 311 if (test==curr) break; 282 312 283 // 313 // 284 // Test for parallel line segments 314 // Test for parallel line segments 285 // 315 // 286 G4double dat = test->a - curr->a, 316 G4double dat = test->a - curr->a, 287 dbt = test->b - curr->b; 317 dbt = test->b - curr->b; 288 318 289 if (std::fabs(dat*db-dbt*da)>tolerance2) 319 if (std::fabs(dat*db-dbt*da)>tolerance2) break; 290 320 291 // 321 // 292 // Redundant vertex found: do we have > 322 // Redundant vertex found: do we have > 3 vertices? 293 // 323 // 294 if (numVertices <= 3) 324 if (numVertices <= 3) 295 { 325 { 296 CalculateMaxMin(); 326 CalculateMaxMin(); 297 return false; 327 return false; 298 } 328 } 299 329 300 // 330 // 301 // Delete vertex pointed to by next. Car 331 // Delete vertex pointed to by next. Carefully! 302 // 332 // 303 if (curr->next != nullptr) << 333 if (curr->next) 304 { // next is not head 334 { // next is not head 305 if (next->next != nullptr) << 335 if (next->next) 306 curr->next = test; // next is not t 336 curr->next = test; // next is not tail 307 else 337 else 308 curr->next = nullptr; // New tail << 338 curr->next = 0; // New tail 309 } 339 } 310 else 340 else 311 vertexHead = test; // New head 341 vertexHead = test; // New head 312 342 313 if ((curr != next) && (next != test)) de << 343 delete next; 314 344 315 --numVertices; << 345 numVertices--; 316 346 317 // 347 // 318 // Replace next by the vertex we just te 348 // Replace next by the vertex we just tested, 319 // and keep on going... 349 // and keep on going... 320 // 350 // 321 next = test; 351 next = test; 322 da = dat; db = dbt; 352 da = dat; db = dbt; 323 } 353 } 324 curr = curr->next; 354 curr = curr->next; 325 } 355 } 326 356 327 // 357 // 328 // In principle, this is not needed, but why 358 // In principle, this is not needed, but why not just play it safe? 329 // 359 // 330 CalculateMaxMin(); 360 CalculateMaxMin(); 331 361 332 return true; 362 return true; 333 } 363 } 334 364 >> 365 >> 366 // 335 // ReverseOrder 367 // ReverseOrder 336 // 368 // 337 // Reverse the order of the vertices 369 // Reverse the order of the vertices 338 // 370 // 339 void G4ReduciblePolygon::ReverseOrder() 371 void G4ReduciblePolygon::ReverseOrder() 340 { 372 { 341 // 373 // 342 // Loop over all vertices 374 // Loop over all vertices 343 // 375 // 344 ABVertex* prev = vertexHead; << 376 ABVertex *prev = vertexHead; 345 if (prev==nullptr) return; // No vertices << 377 if (prev==0) return; // No vertices 346 378 347 ABVertex* curr = prev->next; << 379 ABVertex *curr = prev->next; 348 if (curr==nullptr) return; // Just one ve << 380 if (curr==0) return; // Just one vertex 349 381 350 // 382 // 351 // Our new tail 383 // Our new tail 352 // 384 // 353 vertexHead->next = nullptr; << 385 vertexHead->next = 0; 354 386 355 for(;;) 387 for(;;) 356 { 388 { 357 // 389 // 358 // Save pointer to next vertex (in origina 390 // Save pointer to next vertex (in original order) 359 // 391 // 360 ABVertex *save = curr->next; 392 ABVertex *save = curr->next; 361 393 362 // 394 // 363 // Replace it with a pointer to the previo 395 // Replace it with a pointer to the previous one 364 // (in original order) 396 // (in original order) 365 // 397 // 366 curr->next = prev; 398 curr->next = prev; 367 399 368 // 400 // 369 // Last vertex? 401 // Last vertex? 370 // 402 // 371 if (save == nullptr) break; << 403 if (save == 0) break; 372 404 373 // 405 // 374 // Next vertex 406 // Next vertex 375 // 407 // 376 prev = curr; 408 prev = curr; 377 curr = save; 409 curr = save; 378 } 410 } 379 411 380 // 412 // 381 // Our new head 413 // Our new head 382 // 414 // 383 vertexHead = curr; 415 vertexHead = curr; 384 } 416 } 385 417 386 418 387 // StartWithZMin << 388 // << 389 // Starting alway with Zmin=bMin << 390 // This method is used for GenericPolycone << 391 // 419 // 392 void G4ReduciblePolygon::StartWithZMin() << 393 { << 394 ABVertex* curr = vertexHead; << 395 G4double bcurr = curr->b; << 396 ABVertex* prev = curr; << 397 while( curr != nullptr) // Loop checking, << 398 { << 399 if(curr->b < bcurr) << 400 { << 401 bcurr = curr->b; << 402 ABVertex* curr1 = curr; << 403 while( curr1 != nullptr ) // Loop che << 404 { << 405 if(curr1->next == nullptr) { curr1->ne << 406 curr1 = curr1->next; << 407 } << 408 vertexHead = curr; << 409 prev->next = nullptr; << 410 } << 411 prev = curr; << 412 curr = curr->next; << 413 } << 414 } << 415 << 416 // CrossesItself 420 // CrossesItself 417 // 421 // 418 // Return "true" if the polygon crosses itself 422 // Return "true" if the polygon crosses itself 419 // 423 // 420 // Warning: this routine is not very fast (run 424 // Warning: this routine is not very fast (runs as N**2) 421 // 425 // 422 G4bool G4ReduciblePolygon::CrossesItself( G4do 426 G4bool G4ReduciblePolygon::CrossesItself( G4double tolerance ) 423 { 427 { 424 G4double tolerance2 = tolerance*tolerance; 428 G4double tolerance2 = tolerance*tolerance; 425 G4double one = 1.0-tolerance, << 429 G4double one = 1.0-tolerance, 426 zero = tolerance; << 430 zero = tolerance; 427 // 431 // 428 // Top loop over line segments. By the time 432 // Top loop over line segments. By the time we finish 429 // with the second to last segment, we're do 433 // with the second to last segment, we're done. 430 // 434 // 431 ABVertex *curr1 = vertexHead, *next1 = nullp << 435 ABVertex *curr1 = vertexHead, *next1=0; 432 while (curr1->next != nullptr) // Loop ch << 436 while (curr1->next) { 433 { << 437 next1 = curr1->next; 434 next1 = curr1->next; << 435 G4double da1 = next1->a-curr1->a, 438 G4double da1 = next1->a-curr1->a, 436 db1 = next1->b-curr1->b; << 439 db1 = next1->b-curr1->b; 437 440 438 // 441 // 439 // Inner loop over subsequent line segment 442 // Inner loop over subsequent line segments 440 // 443 // 441 ABVertex* curr2 = next1->next; << 444 ABVertex *curr2 = next1->next; 442 while( curr2 != nullptr ) // Loop check << 445 while( curr2 ) { 443 { << 446 ABVertex *next2 = curr2->next; 444 ABVertex* next2 = curr2->next; << 447 if (next2==0) next2 = vertexHead; 445 if (next2==nullptr) next2 = vertexHead; << 446 G4double da2 = next2->a-curr2->a, 448 G4double da2 = next2->a-curr2->a, 447 db2 = next2->b-curr2->b; << 449 db2 = next2->b-curr2->b; 448 G4double a12 = curr2->a-curr1->a, 450 G4double a12 = curr2->a-curr1->a, 449 b12 = curr2->b-curr1->b; << 451 b12 = curr2->b-curr1->b; 450 452 451 // 453 // 452 // Calculate intersection of the two lin 454 // Calculate intersection of the two lines 453 // 455 // 454 G4double deter = da1*db2 - db1*da2; 456 G4double deter = da1*db2 - db1*da2; 455 if (std::fabs(deter) > tolerance2) << 457 if (std::fabs(deter) > tolerance2) { 456 { << 457 G4double s1, s2; 458 G4double s1, s2; 458 s1 = (a12*db2-b12*da2)/deter; 459 s1 = (a12*db2-b12*da2)/deter; 459 460 460 if (s1 >= zero && s1 < one) << 461 if (s1 >= zero && s1 < one) { 461 { << 462 s2 = -(da1*b12-db1*a12)/deter; 462 s2 = -(da1*b12-db1*a12)/deter; 463 if (s2 >= zero && s2 < one) return t 463 if (s2 >= zero && s2 < one) return true; 464 } 464 } 465 } 465 } 466 curr2 = curr2->next; 466 curr2 = curr2->next; 467 } 467 } 468 curr1 = next1; 468 curr1 = next1; 469 } 469 } 470 return false; 470 return false; 471 } 471 } 472 472 >> 473 >> 474 >> 475 // 473 // BisectedBy 476 // BisectedBy 474 // 477 // 475 // Decide if a line through two points crosses 478 // Decide if a line through two points crosses the polygon, within tolerance 476 // 479 // 477 G4bool G4ReduciblePolygon::BisectedBy( G4doubl 480 G4bool G4ReduciblePolygon::BisectedBy( G4double a1, G4double b1, 478 G4doubl 481 G4double a2, G4double b2, 479 G4doubl 482 G4double tolerance ) 480 { 483 { 481 G4int nNeg = 0, nPos = 0; 484 G4int nNeg = 0, nPos = 0; 482 485 483 G4double a12 = a2-a1, b12 = b2-b1; 486 G4double a12 = a2-a1, b12 = b2-b1; 484 G4double len12 = std::sqrt( a12*a12 + b12*b1 487 G4double len12 = std::sqrt( a12*a12 + b12*b12 ); 485 a12 /= len12; b12 /= len12; 488 a12 /= len12; b12 /= len12; 486 489 487 ABVertex* curr = vertexHead; << 490 ABVertex *curr = vertexHead; 488 do // Loop checking, 13.08.2015, G.Cosmo << 491 do 489 { 492 { 490 G4double av = curr->a - a1, 493 G4double av = curr->a - a1, 491 bv = curr->b - b1; << 494 bv = curr->b - b1; 492 495 493 G4double cross = av*b12 - bv*a12; 496 G4double cross = av*b12 - bv*a12; 494 497 495 if (cross < -tolerance) 498 if (cross < -tolerance) 496 { 499 { 497 if (nPos != 0) return true; << 500 if (nPos) return true; 498 ++nNeg; << 501 nNeg++; 499 } 502 } 500 else if (cross > tolerance) 503 else if (cross > tolerance) 501 { 504 { 502 if (nNeg != 0) return true; << 505 if (nNeg) return true; 503 ++nPos; << 506 nPos++; 504 } 507 } 505 curr = curr->next; 508 curr = curr->next; 506 } while( curr != nullptr ); << 509 } while( curr ); 507 510 508 return false; 511 return false; 509 } 512 } 510 513 >> 514 >> 515 >> 516 // 511 // Area 517 // Area 512 // 518 // 513 // Calculated signed polygon area, where polyg 519 // Calculated signed polygon area, where polygons specified in a 514 // clockwise manner (where x==a, y==b) have ne 520 // clockwise manner (where x==a, y==b) have negative area 515 // 521 // 516 // References: [O' Rourke (C)] pp. 18-27; [ 522 // References: [O' Rourke (C)] pp. 18-27; [Gems II] pp. 5-6: 517 // "The Area of a Simple Polygon", Jon Rokn 523 // "The Area of a Simple Polygon", Jon Rokne. 518 // 524 // 519 G4double G4ReduciblePolygon::Area() 525 G4double G4ReduciblePolygon::Area() 520 { 526 { 521 G4double answer = 0; 527 G4double answer = 0; 522 528 523 ABVertex *curr = vertexHead, *next = nullptr << 529 ABVertex *curr = vertexHead, *next; 524 do // Loop checking, 13.08.2015, G.Cosmo << 530 do 525 { 531 { 526 next = curr->next; 532 next = curr->next; 527 if (next==nullptr) next = vertexHead; << 533 if (next==0) next = vertexHead; 528 534 529 answer += curr->a*next->b - curr->b*next-> 535 answer += curr->a*next->b - curr->b*next->a; 530 curr = curr->next; 536 curr = curr->next; 531 } while( curr != nullptr ); << 537 } while( curr ); 532 538 533 return 0.5*answer; 539 return 0.5*answer; 534 } 540 } 535 541 >> 542 >> 543 // 536 // Print 544 // Print 537 // 545 // 538 void G4ReduciblePolygon::Print() 546 void G4ReduciblePolygon::Print() 539 { 547 { 540 ABVertex* curr = vertexHead; << 548 ABVertex *curr = vertexHead; 541 do // Loop checking, 13.08.2015, G.Cosmo << 549 do 542 { 550 { 543 G4cerr << curr->a << " " << curr->b << G4e 551 G4cerr << curr->a << " " << curr->b << G4endl; 544 curr = curr->next; 552 curr = curr->next; 545 } while( curr != nullptr ); << 553 } while( curr ); 546 } 554 } 547 555 >> 556 >> 557 // 548 // CalculateMaxMin 558 // CalculateMaxMin 549 // 559 // 550 // To be called when the vertices are changed, 560 // To be called when the vertices are changed, this 551 // routine re-calculates global values 561 // routine re-calculates global values 552 // 562 // 553 void G4ReduciblePolygon::CalculateMaxMin() 563 void G4ReduciblePolygon::CalculateMaxMin() 554 { 564 { 555 ABVertex* curr = vertexHead; << 565 ABVertex *curr = vertexHead; 556 aMin = aMax = curr->a; 566 aMin = aMax = curr->a; 557 bMin = bMax = curr->b; 567 bMin = bMax = curr->b; 558 curr = curr->next; 568 curr = curr->next; 559 while( curr != nullptr ) // Loop checking << 569 while( curr ) 560 { 570 { 561 if (curr->a < aMin) 571 if (curr->a < aMin) 562 aMin = curr->a; 572 aMin = curr->a; 563 else if (curr->a > aMax) 573 else if (curr->a > aMax) 564 aMax = curr->a; 574 aMax = curr->a; 565 575 566 if (curr->b < bMin) 576 if (curr->b < bMin) 567 bMin = curr->b; 577 bMin = curr->b; 568 else if (curr->b > bMax) 578 else if (curr->b > bMax) 569 bMax = curr->b; 579 bMax = curr->b; 570 580 571 curr = curr->next; 581 curr = curr->next; 572 } 582 } 573 } 583 } 574 584