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.15 2010-11-02 11:28:38 gcosmo Exp $ >> 28 // GEANT4 tag $Name: not supported by cvs2svn $ >> 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.), numVertices(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, *next = 0; 195 while( curr != nullptr ) // Loop checking << 224 while( curr ) 196 { 225 { 197 next = curr->next; 226 next = curr->next; 198 if (next == nullptr) next = vertexHead; << 227 if (next == 0) next = vertexHead; 199 228 200 if (std::fabs(curr->a-next->a) < tolerance 229 if (std::fabs(curr->a-next->a) < tolerance && 201 std::fabs(curr->b-next->b) < tolerance 230 std::fabs(curr->b-next->b) < tolerance ) 202 { 231 { 203 // 232 // 204 // Duplicate found: do we have > 3 verti 233 // Duplicate found: do we have > 3 vertices? 205 // 234 // 206 if (numVertices <= 3) 235 if (numVertices <= 3) 207 { 236 { 208 CalculateMaxMin(); 237 CalculateMaxMin(); 209 return false; 238 return false; 210 } 239 } 211 240 212 // 241 // 213 // Delete 242 // Delete 214 // 243 // 215 ABVertex* toDelete = curr; << 244 ABVertex *toDelete = curr; 216 curr = curr->next; 245 curr = curr->next; 217 delete toDelete; 246 delete toDelete; 218 247 219 numVertices--; 248 numVertices--; 220 249 221 if (prev != nullptr) << 250 if (prev) prev->next = curr; else vertexHead = curr; 222 prev->next = curr; << 223 else << 224 vertexHead = curr; << 225 } 251 } 226 else 252 else 227 { 253 { 228 prev = curr; 254 prev = curr; 229 curr = curr->next; 255 curr = curr->next; 230 } 256 } 231 } 257 } 232 258 233 // 259 // 234 // In principle, this is not needed, but why 260 // In principle, this is not needed, but why not just play it safe? 235 // 261 // 236 CalculateMaxMin(); 262 CalculateMaxMin(); 237 263 238 return true; 264 return true; 239 } 265 } 240 266 >> 267 >> 268 // 241 // RemoveRedundantVertices 269 // RemoveRedundantVertices 242 // 270 // 243 // Remove any unneeded vertices, i.e. those ve 271 // Remove any unneeded vertices, i.e. those vertices which 244 // are on the line connecting the previous and 272 // are on the line connecting the previous and next vertices. 245 // 273 // 246 G4bool G4ReduciblePolygon::RemoveRedundantVert 274 G4bool G4ReduciblePolygon::RemoveRedundantVertices( G4double tolerance ) 247 { 275 { 248 // 276 // 249 // Under these circumstances, we can quit no 277 // Under these circumstances, we can quit now! 250 // 278 // 251 if (numVertices <= 2) return false; 279 if (numVertices <= 2) return false; 252 280 253 G4double tolerance2 = tolerance*tolerance; 281 G4double tolerance2 = tolerance*tolerance; 254 282 255 // 283 // 256 // Loop over all vertices 284 // Loop over all vertices 257 // 285 // 258 ABVertex *curr = vertexHead, *next = nullptr << 286 ABVertex *curr = vertexHead, *next = 0; 259 while( curr != nullptr ) // Loop checking << 287 while( curr ) 260 { 288 { 261 next = curr->next; 289 next = curr->next; 262 if (next == nullptr) next = vertexHead; << 290 if (next == 0) next = vertexHead; 263 291 264 G4double da = next->a - curr->a, 292 G4double da = next->a - curr->a, 265 db = next->b - curr->b; 293 db = next->b - curr->b; 266 294 267 // 295 // 268 // Loop over all subsequent vertices, up t 296 // Loop over all subsequent vertices, up to curr 269 // 297 // 270 for(;;) 298 for(;;) 271 { 299 { 272 // 300 // 273 // Get vertex after next 301 // Get vertex after next 274 // 302 // 275 ABVertex* test = next->next; << 303 ABVertex *test = next->next; 276 if (test == nullptr) test = vertexHead; << 304 if (test == 0) test = vertexHead; 277 305 278 // 306 // 279 // If we are back to the original vertex 307 // If we are back to the original vertex, stop 280 // 308 // 281 if (test==curr) break; 309 if (test==curr) break; 282 310 283 // 311 // 284 // Test for parallel line segments 312 // Test for parallel line segments 285 // 313 // 286 G4double dat = test->a - curr->a, 314 G4double dat = test->a - curr->a, 287 dbt = test->b - curr->b; 315 dbt = test->b - curr->b; 288 316 289 if (std::fabs(dat*db-dbt*da)>tolerance2) 317 if (std::fabs(dat*db-dbt*da)>tolerance2) break; 290 318 291 // 319 // 292 // Redundant vertex found: do we have > 320 // Redundant vertex found: do we have > 3 vertices? 293 // 321 // 294 if (numVertices <= 3) 322 if (numVertices <= 3) 295 { 323 { 296 CalculateMaxMin(); 324 CalculateMaxMin(); 297 return false; 325 return false; 298 } 326 } 299 327 300 // 328 // 301 // Delete vertex pointed to by next. Car 329 // Delete vertex pointed to by next. Carefully! 302 // 330 // 303 if (curr->next != nullptr) << 331 if (curr->next) 304 { // next is not head 332 { // next is not head 305 if (next->next != nullptr) << 333 if (next->next) 306 curr->next = test; // next is not t 334 curr->next = test; // next is not tail 307 else 335 else 308 curr->next = nullptr; // New tail << 336 curr->next = 0; // New tail 309 } 337 } 310 else 338 else 311 vertexHead = test; // New head 339 vertexHead = test; // New head 312 340 313 if ((curr != next) && (next != test)) de 341 if ((curr != next) && (next != test)) delete next; 314 342 315 --numVertices; << 343 numVertices--; 316 344 317 // 345 // 318 // Replace next by the vertex we just te 346 // Replace next by the vertex we just tested, 319 // and keep on going... 347 // and keep on going... 320 // 348 // 321 next = test; 349 next = test; 322 da = dat; db = dbt; 350 da = dat; db = dbt; 323 } 351 } 324 curr = curr->next; 352 curr = curr->next; 325 } 353 } 326 354 327 // 355 // 328 // In principle, this is not needed, but why 356 // In principle, this is not needed, but why not just play it safe? 329 // 357 // 330 CalculateMaxMin(); 358 CalculateMaxMin(); 331 359 332 return true; 360 return true; 333 } 361 } 334 362 >> 363 >> 364 // 335 // ReverseOrder 365 // ReverseOrder 336 // 366 // 337 // Reverse the order of the vertices 367 // Reverse the order of the vertices 338 // 368 // 339 void G4ReduciblePolygon::ReverseOrder() 369 void G4ReduciblePolygon::ReverseOrder() 340 { 370 { 341 // 371 // 342 // Loop over all vertices 372 // Loop over all vertices 343 // 373 // 344 ABVertex* prev = vertexHead; << 374 ABVertex *prev = vertexHead; 345 if (prev==nullptr) return; // No vertices << 375 if (prev==0) return; // No vertices 346 376 347 ABVertex* curr = prev->next; << 377 ABVertex *curr = prev->next; 348 if (curr==nullptr) return; // Just one ve << 378 if (curr==0) return; // Just one vertex 349 379 350 // 380 // 351 // Our new tail 381 // Our new tail 352 // 382 // 353 vertexHead->next = nullptr; << 383 vertexHead->next = 0; 354 384 355 for(;;) 385 for(;;) 356 { 386 { 357 // 387 // 358 // Save pointer to next vertex (in origina 388 // Save pointer to next vertex (in original order) 359 // 389 // 360 ABVertex *save = curr->next; 390 ABVertex *save = curr->next; 361 391 362 // 392 // 363 // Replace it with a pointer to the previo 393 // Replace it with a pointer to the previous one 364 // (in original order) 394 // (in original order) 365 // 395 // 366 curr->next = prev; 396 curr->next = prev; 367 397 368 // 398 // 369 // Last vertex? 399 // Last vertex? 370 // 400 // 371 if (save == nullptr) break; << 401 if (save == 0) break; 372 402 373 // 403 // 374 // Next vertex 404 // Next vertex 375 // 405 // 376 prev = curr; 406 prev = curr; 377 curr = save; 407 curr = save; 378 } 408 } 379 409 380 // 410 // 381 // Our new head 411 // Our new head 382 // 412 // 383 vertexHead = curr; 413 vertexHead = curr; 384 } 414 } 385 415 386 416 387 // StartWithZMin << 388 // 417 // 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 418 // CrossesItself 417 // 419 // 418 // Return "true" if the polygon crosses itself 420 // Return "true" if the polygon crosses itself 419 // 421 // 420 // Warning: this routine is not very fast (run 422 // Warning: this routine is not very fast (runs as N**2) 421 // 423 // 422 G4bool G4ReduciblePolygon::CrossesItself( G4do 424 G4bool G4ReduciblePolygon::CrossesItself( G4double tolerance ) 423 { 425 { 424 G4double tolerance2 = tolerance*tolerance; 426 G4double tolerance2 = tolerance*tolerance; 425 G4double one = 1.0-tolerance, 427 G4double one = 1.0-tolerance, 426 zero = tolerance; 428 zero = tolerance; 427 // 429 // 428 // Top loop over line segments. By the time 430 // Top loop over line segments. By the time we finish 429 // with the second to last segment, we're do 431 // with the second to last segment, we're done. 430 // 432 // 431 ABVertex *curr1 = vertexHead, *next1 = nullp << 433 ABVertex *curr1 = vertexHead, *next1=0; 432 while (curr1->next != nullptr) // Loop ch << 434 while (curr1->next) 433 { 435 { 434 next1 = curr1->next; 436 next1 = curr1->next; 435 G4double da1 = next1->a-curr1->a, 437 G4double da1 = next1->a-curr1->a, 436 db1 = next1->b-curr1->b; 438 db1 = next1->b-curr1->b; 437 439 438 // 440 // 439 // Inner loop over subsequent line segment 441 // Inner loop over subsequent line segments 440 // 442 // 441 ABVertex* curr2 = next1->next; << 443 ABVertex *curr2 = next1->next; 442 while( curr2 != nullptr ) // Loop check << 444 while( curr2 ) 443 { 445 { 444 ABVertex* next2 = curr2->next; << 446 ABVertex *next2 = curr2->next; 445 if (next2==nullptr) next2 = vertexHead; << 447 if (next2==0) 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 { 458 { 457 G4double s1, s2; 459 G4double s1, s2; 458 s1 = (a12*db2-b12*da2)/deter; 460 s1 = (a12*db2-b12*da2)/deter; 459 461 460 if (s1 >= zero && s1 < one) 462 if (s1 >= zero && s1 < one) 461 { 463 { 462 s2 = -(da1*b12-db1*a12)/deter; 464 s2 = -(da1*b12-db1*a12)/deter; 463 if (s2 >= zero && s2 < one) return t 465 if (s2 >= zero && s2 < one) return true; 464 } 466 } 465 } 467 } 466 curr2 = curr2->next; 468 curr2 = curr2->next; 467 } 469 } 468 curr1 = next1; 470 curr1 = next1; 469 } 471 } 470 return false; 472 return false; 471 } 473 } 472 474 >> 475 >> 476 >> 477 // 473 // BisectedBy 478 // BisectedBy 474 // 479 // 475 // Decide if a line through two points crosses 480 // Decide if a line through two points crosses the polygon, within tolerance 476 // 481 // 477 G4bool G4ReduciblePolygon::BisectedBy( G4doubl 482 G4bool G4ReduciblePolygon::BisectedBy( G4double a1, G4double b1, 478 G4doubl 483 G4double a2, G4double b2, 479 G4doubl 484 G4double tolerance ) 480 { 485 { 481 G4int nNeg = 0, nPos = 0; 486 G4int nNeg = 0, nPos = 0; 482 487 483 G4double a12 = a2-a1, b12 = b2-b1; 488 G4double a12 = a2-a1, b12 = b2-b1; 484 G4double len12 = std::sqrt( a12*a12 + b12*b1 489 G4double len12 = std::sqrt( a12*a12 + b12*b12 ); 485 a12 /= len12; b12 /= len12; 490 a12 /= len12; b12 /= len12; 486 491 487 ABVertex* curr = vertexHead; << 492 ABVertex *curr = vertexHead; 488 do // Loop checking, 13.08.2015, G.Cosmo << 493 do 489 { 494 { 490 G4double av = curr->a - a1, 495 G4double av = curr->a - a1, 491 bv = curr->b - b1; << 496 bv = curr->b - b1; 492 497 493 G4double cross = av*b12 - bv*a12; 498 G4double cross = av*b12 - bv*a12; 494 499 495 if (cross < -tolerance) 500 if (cross < -tolerance) 496 { 501 { 497 if (nPos != 0) return true; << 502 if (nPos) return true; 498 ++nNeg; << 503 nNeg++; 499 } 504 } 500 else if (cross > tolerance) 505 else if (cross > tolerance) 501 { 506 { 502 if (nNeg != 0) return true; << 507 if (nNeg) return true; 503 ++nPos; << 508 nPos++; 504 } 509 } 505 curr = curr->next; 510 curr = curr->next; 506 } while( curr != nullptr ); << 511 } while( curr ); 507 512 508 return false; 513 return false; 509 } 514 } 510 515 >> 516 >> 517 >> 518 // 511 // Area 519 // Area 512 // 520 // 513 // Calculated signed polygon area, where polyg 521 // Calculated signed polygon area, where polygons specified in a 514 // clockwise manner (where x==a, y==b) have ne 522 // clockwise manner (where x==a, y==b) have negative area 515 // 523 // 516 // References: [O' Rourke (C)] pp. 18-27; [ 524 // References: [O' Rourke (C)] pp. 18-27; [Gems II] pp. 5-6: 517 // "The Area of a Simple Polygon", Jon Rokn 525 // "The Area of a Simple Polygon", Jon Rokne. 518 // 526 // 519 G4double G4ReduciblePolygon::Area() 527 G4double G4ReduciblePolygon::Area() 520 { 528 { 521 G4double answer = 0; 529 G4double answer = 0; 522 530 523 ABVertex *curr = vertexHead, *next = nullptr << 531 ABVertex *curr = vertexHead, *next; 524 do // Loop checking, 13.08.2015, G.Cosmo << 532 do 525 { 533 { 526 next = curr->next; 534 next = curr->next; 527 if (next==nullptr) next = vertexHead; << 535 if (next==0) next = vertexHead; 528 536 529 answer += curr->a*next->b - curr->b*next-> 537 answer += curr->a*next->b - curr->b*next->a; 530 curr = curr->next; 538 curr = curr->next; 531 } while( curr != nullptr ); << 539 } while( curr ); 532 540 533 return 0.5*answer; 541 return 0.5*answer; 534 } 542 } 535 543 >> 544 >> 545 // 536 // Print 546 // Print 537 // 547 // 538 void G4ReduciblePolygon::Print() 548 void G4ReduciblePolygon::Print() 539 { 549 { 540 ABVertex* curr = vertexHead; << 550 ABVertex *curr = vertexHead; 541 do // Loop checking, 13.08.2015, G.Cosmo << 551 do 542 { 552 { 543 G4cerr << curr->a << " " << curr->b << G4e 553 G4cerr << curr->a << " " << curr->b << G4endl; 544 curr = curr->next; 554 curr = curr->next; 545 } while( curr != nullptr ); << 555 } while( curr ); 546 } 556 } 547 557 >> 558 >> 559 // 548 // CalculateMaxMin 560 // CalculateMaxMin 549 // 561 // 550 // To be called when the vertices are changed, 562 // To be called when the vertices are changed, this 551 // routine re-calculates global values 563 // routine re-calculates global values 552 // 564 // 553 void G4ReduciblePolygon::CalculateMaxMin() 565 void G4ReduciblePolygon::CalculateMaxMin() 554 { 566 { 555 ABVertex* curr = vertexHead; << 567 ABVertex *curr = vertexHead; 556 aMin = aMax = curr->a; 568 aMin = aMax = curr->a; 557 bMin = bMax = curr->b; 569 bMin = bMax = curr->b; 558 curr = curr->next; 570 curr = curr->next; 559 while( curr != nullptr ) // Loop checking << 571 while( curr ) 560 { 572 { 561 if (curr->a < aMin) 573 if (curr->a < aMin) 562 aMin = curr->a; 574 aMin = curr->a; 563 else if (curr->a > aMax) 575 else if (curr->a > aMax) 564 aMax = curr->a; 576 aMax = curr->a; 565 577 566 if (curr->b < bMin) 578 if (curr->b < bMin) 567 bMin = curr->b; 579 bMin = curr->b; 568 else if (curr->b > bMax) 580 else if (curr->b > bMax) 569 bMax = curr->b; 581 bMax = curr->b; 570 582 571 curr = curr->next; 583 curr = curr->next; 572 } 584 } 573 } 585 } 574 586