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