Geant4 11.4.0
Toolkit for the simulation of the passage of particles through matter
Loading...
Searching...
No Matches
G4ReduciblePolygon Class Reference

G4ReduciblePolygon is a utility class used to specify, test, reduce, and/or otherwise manipulate a 2D polygon. More...

#include <G4ReduciblePolygon.hh>

Public Member Functions

 G4ReduciblePolygon (const G4double a[], const G4double b[], G4int n)
 G4ReduciblePolygon (const G4double rmin[], const G4double rmax[], const G4double z[], G4int n)
 G4ReduciblePolygon (const G4ReduciblePolygon &)=delete
G4ReduciblePolygonoperator= (const G4ReduciblePolygon &)=delete
 ~G4ReduciblePolygon ()
G4int NumVertices () const
G4double Amin () const
G4double Amax () const
G4double Bmin () const
G4double Bmax () const
void CopyVertices (G4double a[], G4double b[]) const
void ScaleA (G4double scale)
void ScaleB (G4double scale)
G4bool RemoveDuplicateVertices (G4double tolerance)
G4bool RemoveRedundantVertices (G4double tolerance)
void ReverseOrder ()
void StartWithZMin ()
G4double Area ()
G4bool CrossesItself (G4double tolerance)
G4bool BisectedBy (G4double a1, G4double b1, G4double a2, G4double b2, G4double tolerance)
void Print ()
 G4ReduciblePolygon (__void__ &)

Friends

class G4ReduciblePolygonIterator
struct ABVertex

Detailed Description

G4ReduciblePolygon is a utility class used to specify, test, reduce, and/or otherwise manipulate a 2D polygon.

Definition at line 57 of file G4ReduciblePolygon.hh.

Constructor & Destructor Documentation

◆ G4ReduciblePolygon() [1/4]

G4ReduciblePolygon::G4ReduciblePolygon ( const G4double a[],
const G4double b[],
G4int n )

Constructor of G4ReduciblePolygon via simple a/b arrays.

Parameters
[in]aFirst array of points.
[in]bSecond array of points.
[in]nThe number of vertices of the polygon (has to be >=3).

Definition at line 38 of file G4ReduciblePolygon.cc.

41 : aMin(0.), aMax(0.), bMin(0.), bMax(0.)
42{
43 //
44 // Do all of the real work in Create
45 //
46 Create( a, b, n );
47}

Referenced by G4ReduciblePolygon(), and operator=().

◆ G4ReduciblePolygon() [2/4]

G4ReduciblePolygon::G4ReduciblePolygon ( const G4double rmin[],
const G4double rmax[],
const G4double z[],
G4int n )

Special constructor version for G4Polyhedra and G4Polycone, that takes two a points at planes of b (where a==r and b==z).

Parameters
[in]rminArray of r-min coordinates of corners.
[in]rmaxArray of r-max coordinates of corners.
[in]zArray of Z coordinates of corners.
[in]nThe number of vertices of the polygon.

Definition at line 51 of file G4ReduciblePolygon.cc.

54 : aMin(0.), aMax(0.), bMin(0.), bMax(0.)
55{
56 //
57 // Translate
58 //
59 auto a = new G4double[n*2];
60 auto b = new G4double[n*2];
61
62 G4double *rOut = a + n,
63 *zOut = b + n,
64 *rIn = rOut-1,
65 *zIn = zOut-1;
66
67 for( G4int i=0; i < n; ++i, ++rOut, ++zOut, --rIn, --zIn )
68 {
69 *rOut = rmax[i];
70 *rIn = rmin[i];
71 *zOut = *zIn = z[i];
72 }
73
74 Create( a, b, n*2 );
75
76 delete [] a;
77 delete [] b;
78}
double G4double
Definition G4Types.hh:83
int G4int
Definition G4Types.hh:85

◆ G4ReduciblePolygon() [3/4]

G4ReduciblePolygon::G4ReduciblePolygon ( const G4ReduciblePolygon & )
delete

Copy constructor and assignment operator not allowed.

◆ ~G4ReduciblePolygon()

G4ReduciblePolygon::~G4ReduciblePolygon ( )

Destructor, taking care to clear allocated lists.

Definition at line 131 of file G4ReduciblePolygon.cc.

132{
133 ABVertex* curr = vertexHead;
134 while( curr != nullptr ) // Loop checking, 13.08.2015, G.Cosmo
135 {
136 ABVertex* toDelete = curr;
137 curr = curr->next;
138 delete toDelete;
139 }
140}

◆ G4ReduciblePolygon() [4/4]

G4ReduciblePolygon::G4ReduciblePolygon ( __void__ & )

Fake default constructor for usage restricted to direct object persistency for clients requiring preallocation of memory for persistifiable objects.

Definition at line 122 of file G4ReduciblePolygon.cc.

123 : aMin(0.), aMax(0.), bMin(0.), bMax(0.)
124{
125}

Member Function Documentation

◆ Amax()

G4double G4ReduciblePolygon::Amax ( ) const
inline

Definition at line 98 of file G4ReduciblePolygon.hh.

98{ return aMax; }

Referenced by G4EnclosingCylinder::G4EnclosingCylinder(), and G4PolyPhiFace::G4PolyPhiFace().

◆ Amin()

G4double G4ReduciblePolygon::Amin ( ) const
inline

Definition at line 97 of file G4ReduciblePolygon.hh.

97{ return aMin; }

Referenced by G4PolyPhiFace::G4PolyPhiFace().

◆ Area()

G4double G4ReduciblePolygon::Area ( )

Calculates signed polygon area, where polygons specified in a clockwise manner have negative area.

Definition at line 531 of file G4ReduciblePolygon.cc.

532{
533 G4double answer = 0;
534
535 ABVertex *curr = vertexHead, *next = nullptr;
536 do // Loop checking, 13.08.2015, G.Cosmo
537 {
538 next = curr->next;
539 if (next==nullptr) { next = vertexHead; }
540
541 answer += curr->a*next->b - curr->b*next->a;
542 curr = curr->next;
543 } while( curr != nullptr );
544
545 return 0.5*answer;
546}

◆ BisectedBy()

G4bool G4ReduciblePolygon::BisectedBy ( G4double a1,
G4double b1,
G4double a2,
G4double b2,
G4double tolerance )

Decides if a line through two points crosses the polygon, within tolerance.

Definition at line 489 of file G4ReduciblePolygon.cc.

492{
493 G4int nNeg = 0, nPos = 0;
494
495 G4double a12 = a2-a1, b12 = b2-b1;
496 G4double len12 = std::sqrt( a12*a12 + b12*b12 );
497 a12 /= len12; b12 /= len12;
498
499 ABVertex* curr = vertexHead;
500 do // Loop checking, 13.08.2015, G.Cosmo
501 {
502 G4double av = curr->a - a1,
503 bv = curr->b - b1;
504
505 G4double cross = av*b12 - bv*a12;
506
507 if (cross < -tolerance)
508 {
509 if (nPos != 0) { return true; }
510 ++nNeg;
511 }
512 else if (cross > tolerance)
513 {
514 if (nNeg != 0) { return true; }
515 ++nPos;
516 }
517 curr = curr->next;
518 } while( curr != nullptr );
519
520 return false;
521}

◆ Bmax()

G4double G4ReduciblePolygon::Bmax ( ) const
inline

Definition at line 100 of file G4ReduciblePolygon.hh.

100{ return bMax; }

Referenced by G4EnclosingCylinder::G4EnclosingCylinder(), and G4PolyPhiFace::G4PolyPhiFace().

◆ Bmin()

G4double G4ReduciblePolygon::Bmin ( ) const
inline

Definition at line 99 of file G4ReduciblePolygon.hh.

99{ return bMin; }

Referenced by G4EnclosingCylinder::G4EnclosingCylinder(), and G4PolyPhiFace::G4PolyPhiFace().

◆ CopyVertices()

void G4ReduciblePolygon::CopyVertices ( G4double a[],
G4double b[] ) const

Copies contents of provided arrays into simple linear arrays.

Definition at line 148 of file G4ReduciblePolygon.cc.

149{
150 G4double *anext = a, *bnext = b;
151 ABVertex *curr = vertexHead;
152 while( curr != nullptr ) // Loop checking, 13.08.2015, G.Cosmo
153 {
154 *anext++ = curr->a;
155 *bnext++ = curr->b;
156 curr = curr->next;
157 }
158}

◆ CrossesItself()

G4bool G4ReduciblePolygon::CrossesItself ( G4double tolerance)

Returns "true" if the polygon crosses itself.

Definition at line 434 of file G4ReduciblePolygon.cc.

435{
436 G4double tolerance2 = tolerance*tolerance;
437 G4double one = 1.0-tolerance,
438 zero = tolerance;
439 //
440 // Top loop over line segments. By the time we finish
441 // with the second to last segment, we're done.
442 //
443 ABVertex *curr1 = vertexHead, *next1 = nullptr;
444 while (curr1->next != nullptr) // Loop checking, 13.08.2015, G.Cosmo
445 {
446 next1 = curr1->next;
447 G4double da1 = next1->a-curr1->a,
448 db1 = next1->b-curr1->b;
449
450 //
451 // Inner loop over subsequent line segments
452 //
453 ABVertex* curr2 = next1->next;
454 while( curr2 != nullptr ) // Loop checking, 13.08.2015, G.Cosmo
455 {
456 ABVertex* next2 = curr2->next;
457 if (next2==nullptr) { next2 = vertexHead; }
458 G4double da2 = next2->a-curr2->a,
459 db2 = next2->b-curr2->b;
460 G4double a12 = curr2->a-curr1->a,
461 b12 = curr2->b-curr1->b;
462
463 //
464 // Calculate intersection of the two lines
465 //
466 G4double deter = da1*db2 - db1*da2;
467 if (std::fabs(deter) > tolerance2)
468 {
469 G4double s1, s2;
470 s1 = (a12*db2-b12*da2)/deter;
471
472 if (s1 >= zero && s1 < one)
473 {
474 s2 = -(da1*b12-db1*a12)/deter;
475 if (s2 >= zero && s2 < one) { return true; }
476 }
477 }
478 curr2 = curr2->next;
479 }
480 curr1 = next1;
481 }
482 return false;
483}

◆ NumVertices()

G4int G4ReduciblePolygon::NumVertices ( ) const
inline

Accessors.

Definition at line 96 of file G4ReduciblePolygon.hh.

96{ return numVertices; }

Referenced by G4PolyPhiFace::G4PolyPhiFace().

◆ operator=()

G4ReduciblePolygon & G4ReduciblePolygon::operator= ( const G4ReduciblePolygon & )
delete

◆ Print()

void G4ReduciblePolygon::Print ( )

Print function for debugging.

Definition at line 550 of file G4ReduciblePolygon.cc.

551{
552 ABVertex* curr = vertexHead;
553 do // Loop checking, 13.08.2015, G.Cosmo
554 {
555 G4cerr << curr->a << " " << curr->b << G4endl;
556 curr = curr->next;
557 } while( curr != nullptr );
558}
G4GLOB_DLL std::ostream G4cerr
#define G4endl
Definition G4ios.hh:67

◆ RemoveDuplicateVertices()

G4bool G4ReduciblePolygon::RemoveDuplicateVertices ( G4double tolerance)

Removes adjacent vertices that are equal.

Parameters
[in]toleranceProvided tolerance for adjacent vertices.
Returns
false, if there is a problem (too few vertices remaining).

Definition at line 193 of file G4ReduciblePolygon.cc.

194{
195 ABVertex *curr = vertexHead,
196 *prev = nullptr, *next = nullptr;
197 while( curr != nullptr ) // Loop checking, 13.08.2015, G.Cosmo
198 {
199 next = curr->next;
200 if (next == nullptr) { next = vertexHead; }
201
202 if (std::fabs(curr->a-next->a) < tolerance &&
203 std::fabs(curr->b-next->b) < tolerance )
204 {
205 //
206 // Duplicate found: do we have > 3 vertices?
207 //
208 if (numVertices <= 3)
209 {
210 CalculateMaxMin();
211 return false;
212 }
213
214 //
215 // Delete
216 //
217 ABVertex* toDelete = curr;
218 curr = curr->next;
219 delete toDelete;
220
221 numVertices--;
222
223 if (prev != nullptr)
224 {
225 prev->next = curr;
226 }
227 else
228 {
229 vertexHead = curr;
230 }
231 }
232 else
233 {
234 prev = curr;
235 curr = curr->next;
236 }
237 }
238
239 //
240 // In principle, this is not needed, but why not just play it safe?
241 //
242 CalculateMaxMin();
243
244 return true;
245}

◆ RemoveRedundantVertices()

G4bool G4ReduciblePolygon::RemoveRedundantVertices ( G4double tolerance)

Removes any unneeded vertices, i.e. those vertices which are on the line connecting the previous and next vertices.

Parameters
[in]toleranceProvided tolerance for parallel line segments.
Returns
false, if there is a problem (too few vertices remaining).

Definition at line 252 of file G4ReduciblePolygon.cc.

253{
254 //
255 // Under these circumstances, we can quit now!
256 //
257 if (numVertices <= 2) { return false; }
258
259 G4double tolerance2 = tolerance*tolerance;
260
261 //
262 // Loop over all vertices
263 //
264 ABVertex *curr = vertexHead, *next = nullptr;
265 while( curr != nullptr ) // Loop checking, 13.08.2015, G.Cosmo
266 {
267 next = curr->next;
268 if (next == nullptr) { next = vertexHead; }
269
270 G4double da = next->a - curr->a,
271 db = next->b - curr->b;
272
273 //
274 // Loop over all subsequent vertices, up to curr
275 //
276 for(;;)
277 {
278 //
279 // Get vertex after next
280 //
281 ABVertex* test = next->next;
282 if (test == nullptr) { test = vertexHead; }
283
284 //
285 // If we are back to the original vertex, stop
286 //
287 if (test==curr) { break; }
288
289 //
290 // Test for parallel line segments
291 //
292 G4double dat = test->a - curr->a,
293 dbt = test->b - curr->b;
294
295 if (std::fabs(dat*db-dbt*da)>tolerance2) { break; }
296
297 //
298 // Redundant vertex found: do we have > 3 vertices?
299 //
300 if (numVertices <= 3)
301 {
302 CalculateMaxMin();
303 return false;
304 }
305
306 //
307 // Delete vertex pointed to by next. Carefully!
308 //
309 if (curr->next != nullptr)
310 { // next is not head
311 if (next->next != nullptr)
312 {
313 curr->next = test; // next is not tail
314 }
315 else
316 {
317 curr->next = nullptr; // New tail
318 }
319 }
320 else
321 {
322 vertexHead = test; // New head
323 }
324
325 if ((curr != next) && (next != test)) { delete next; }
326
327 --numVertices;
328
329 //
330 // Replace next by the vertex we just tested,
331 // and keep on going...
332 //
333 next = test;
334 da = dat; db = dbt;
335 }
336 curr = curr->next;
337 }
338
339 //
340 // In principle, this is not needed, but why not just play it safe?
341 //
342 CalculateMaxMin();
343
344 return true;
345}

◆ ReverseOrder()

void G4ReduciblePolygon::ReverseOrder ( )

Reverses the order of the vertices.

Definition at line 351 of file G4ReduciblePolygon.cc.

352{
353 //
354 // Loop over all vertices
355 //
356 ABVertex* prev = vertexHead;
357 if (prev==nullptr) { return; } // No vertices
358
359 ABVertex* curr = prev->next;
360 if (curr==nullptr) { return; } // Just one vertex
361
362 //
363 // Our new tail
364 //
365 vertexHead->next = nullptr;
366
367 for(;;)
368 {
369 //
370 // Save pointer to next vertex (in original order)
371 //
372 ABVertex *save = curr->next;
373
374 //
375 // Replace it with a pointer to the previous one
376 // (in original order)
377 //
378 curr->next = prev;
379
380 //
381 // Last vertex?
382 //
383 if (save == nullptr) { break; }
384
385 //
386 // Next vertex
387 //
388 prev = curr;
389 curr = save;
390 }
391
392 //
393 // Our new head
394 //
395 vertexHead = curr;
396}

◆ ScaleA()

void G4ReduciblePolygon::ScaleA ( G4double scale)

Methods to multiply all a or b values by a common scale.

Definition at line 164 of file G4ReduciblePolygon.cc.

165{
166 ABVertex* curr = vertexHead;
167 while( curr != nullptr ) // Loop checking, 13.08.2015, G.Cosmo
168 {
169 curr->a *= scale;
170 curr = curr->next;
171 }
172}

◆ ScaleB()

void G4ReduciblePolygon::ScaleB ( G4double scale)

Definition at line 178 of file G4ReduciblePolygon.cc.

179{
180 ABVertex* curr = vertexHead;
181 while( curr != nullptr ) // Loop checking, 13.08.2015, G.Cosmo
182 {
183 curr->b *= scale;
184 curr = curr->next;
185 }
186}

◆ StartWithZMin()

void G4ReduciblePolygon::StartWithZMin ( )

Method is used for G4GenericPolycone; starting always with Zmin=bMin.

Definition at line 404 of file G4ReduciblePolygon.cc.

405{
406 ABVertex* curr = vertexHead;
407 G4double bcurr = curr->b;
408 ABVertex* prev = curr;
409 while( curr != nullptr) // Loop checking, 13.08.2015, G.Cosmo
410 {
411 if(curr->b < bcurr)
412 {
413 bcurr = curr->b;
414 ABVertex* curr1 = curr;
415 while( curr1 != nullptr ) // Loop checking, 13.08.2015, G.Cosmo
416 {
417 if(curr1->next == nullptr) { curr1->next = vertexHead; break; }
418 curr1 = curr1->next;
419 }
420 vertexHead = curr;
421 prev->next = nullptr;
422 }
423 prev = curr;
424 curr = curr->next;
425 }
426}

◆ ABVertex

friend struct ABVertex
friend

Definition at line 194 of file G4ReduciblePolygon.hh.

◆ G4ReduciblePolygonIterator

friend class G4ReduciblePolygonIterator
friend

Definition at line 59 of file G4ReduciblePolygon.hh.

Referenced by G4ReduciblePolygonIterator.


The documentation for this class was generated from the following files: