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

#include <G4ReduciblePolygon.hh>

Classes

struct  ABVertex
 

Public Member Functions

 G4ReduciblePolygon (const G4double a[], const G4double b[], G4int n)
 
 G4ReduciblePolygon (const G4double rmin[], const G4double rmax[], const G4double z[], G4int n)
 
virtual ~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 ()
 
G4double Area ()
 
G4bool CrossesItself (G4double tolerance)
 
G4bool BisectedBy (G4double a1, G4double b1, G4double a2, G4double b2, G4double tolerance)
 
void Print ()
 
 G4ReduciblePolygon (__void__ &)
 

Protected Member Functions

void Create (const G4double a[], const G4double b[], G4int n)
 
void CalculateMaxMin ()
 

Protected Attributes

G4double aMin
 
G4double aMax
 
G4double bMin
 
G4double bMax
 
G4int numVertices
 
ABVertexvertexHead
 

Friends

class G4ReduciblePolygonIterator
 
struct ABVertex
 

Detailed Description

Definition at line 61 of file G4ReduciblePolygon.hh.

Constructor & Destructor Documentation

◆ G4ReduciblePolygon() [1/3]

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

Definition at line 49 of file G4ReduciblePolygon.cc.

52 : aMin(0.), aMax(0.), bMin(0.), bMax(0.),
53 vertexHead(0)
54{
55 //
56 // Do all of the real work in Create
57 //
58 Create( a, b, n );
59}
void Create(const G4double a[], const G4double b[], G4int n)

◆ G4ReduciblePolygon() [2/3]

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

Definition at line 65 of file G4ReduciblePolygon.cc.

68 : aMin(0.), aMax(0.), bMin(0.), bMax(0.),
69 vertexHead(0)
70{
71 //
72 // Translate
73 //
74 G4double *a = new G4double[n*2];
75 G4double *b = new G4double[n*2];
76
77 G4double *rOut = a + n,
78 *zOut = b + n,
79 *rIn = rOut-1,
80 *zIn = zOut-1;
81
82 G4int i;
83 for( i=0; i < n; i++, rOut++, zOut++, rIn--, zIn-- )
84 {
85 *rOut = rmax[i];
86 *rIn = rmin[i];
87 *zOut = *zIn = z[i];
88 }
89
90 Create( a, b, n*2 );
91
92 delete [] a;
93 delete [] b;
94}
double G4double
Definition: G4Types.hh:64
int G4int
Definition: G4Types.hh:66

◆ ~G4ReduciblePolygon()

G4ReduciblePolygon::~G4ReduciblePolygon ( )
virtual

Definition at line 149 of file G4ReduciblePolygon.cc.

150{
151 ABVertex *curr = vertexHead;
152 while( curr )
153 {
154 ABVertex *toDelete = curr;
155 curr = curr->next;
156 delete toDelete;
157 }
158}

◆ G4ReduciblePolygon() [3/3]

G4ReduciblePolygon::G4ReduciblePolygon ( __void__ &  )

Definition at line 140 of file G4ReduciblePolygon.cc.

141 : aMin(0.), aMax(0.), bMin(0.), bMax(0.), numVertices(0), vertexHead(0)
142{
143}

Member Function Documentation

◆ Amax()

G4double G4ReduciblePolygon::Amax ( ) const
inline

Definition at line 87 of file G4ReduciblePolygon.hh.

87{ return aMax; }

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

◆ Amin()

G4double G4ReduciblePolygon::Amin ( ) const
inline

Definition at line 86 of file G4ReduciblePolygon.hh.

86{ return aMin; }

Referenced by G4Polyhedra::Create(), G4Polycone::Create(), and G4PolyPhiFace::G4PolyPhiFace().

◆ Area()

G4double G4ReduciblePolygon::Area ( )

Definition at line 526 of file G4ReduciblePolygon.cc.

527{
528 G4double answer = 0;
529
530 ABVertex *curr = vertexHead, *next;
531 do
532 {
533 next = curr->next;
534 if (next==0) next = vertexHead;
535
536 answer += curr->a*next->b - curr->b*next->a;
537 curr = curr->next;
538 } while( curr );
539
540 return 0.5*answer;
541}

Referenced by G4Polyhedra::Create(), and G4Polycone::Create().

◆ BisectedBy()

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

Definition at line 481 of file G4ReduciblePolygon.cc.

484{
485 G4int nNeg = 0, nPos = 0;
486
487 G4double a12 = a2-a1, b12 = b2-b1;
488 G4double len12 = std::sqrt( a12*a12 + b12*b12 );
489 a12 /= len12; b12 /= len12;
490
491 ABVertex *curr = vertexHead;
492 do
493 {
494 G4double av = curr->a - a1,
495 bv = curr->b - b1;
496
497 G4double cross = av*b12 - bv*a12;
498
499 if (cross < -tolerance)
500 {
501 if (nPos) return true;
502 nNeg++;
503 }
504 else if (cross > tolerance)
505 {
506 if (nNeg) return true;
507 nPos++;
508 }
509 curr = curr->next;
510 } while( curr );
511
512 return false;
513}

Referenced by G4Polycone::Create().

◆ Bmax()

G4double G4ReduciblePolygon::Bmax ( ) const
inline

Definition at line 89 of file G4ReduciblePolygon.hh.

89{ return bMax; }

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

◆ Bmin()

G4double G4ReduciblePolygon::Bmin ( ) const
inline

Definition at line 88 of file G4ReduciblePolygon.hh.

88{ return bMin; }

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

◆ CalculateMaxMin()

void G4ReduciblePolygon::CalculateMaxMin ( )
protected

Definition at line 564 of file G4ReduciblePolygon.cc.

565{
566 ABVertex *curr = vertexHead;
567 aMin = aMax = curr->a;
568 bMin = bMax = curr->b;
569 curr = curr->next;
570 while( curr )
571 {
572 if (curr->a < aMin)
573 aMin = curr->a;
574 else if (curr->a > aMax)
575 aMax = curr->a;
576
577 if (curr->b < bMin)
578 bMin = curr->b;
579 else if (curr->b > bMax)
580 bMax = curr->b;
581
582 curr = curr->next;
583 }
584}

Referenced by Create(), RemoveDuplicateVertices(), and RemoveRedundantVertices().

◆ CopyVertices()

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

Definition at line 168 of file G4ReduciblePolygon.cc.

169{
170 G4double *anext = a, *bnext = b;
171 ABVertex *curr = vertexHead;
172 while( curr )
173 {
174 *anext++ = curr->a;
175 *bnext++ = curr->b;
176 curr = curr->next;
177 }
178}

◆ Create()

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

Definition at line 103 of file G4ReduciblePolygon.cc.

105{
106 if (n<3)
107 G4Exception("G4ReduciblePolygon::Create()", "GeomSolids0002",
108 FatalErrorInArgument, "Less than 3 vertices specified.");
109
110 const G4double *anext = a, *bnext = b;
111 ABVertex *prev = 0;
112 do
113 {
114 ABVertex *newVertex = new ABVertex;
115 newVertex->a = *anext;
116 newVertex->b = *bnext;
117 newVertex->next = 0;
118 if (prev==0)
119 {
120 vertexHead = newVertex;
121 }
122 else
123 {
124 prev->next = newVertex;
125 }
126
127 prev = newVertex;
128 } while( ++anext, ++bnext < b+n );
129
130 numVertices = n;
131
133}
@ FatalErrorInArgument
void G4Exception(const char *originOfException, const char *exceptionCode, G4ExceptionSeverity severity, const char *comments)
Definition: G4Exception.cc:41

Referenced by G4ReduciblePolygon().

◆ CrossesItself()

G4bool G4ReduciblePolygon::CrossesItself ( G4double  tolerance)

Definition at line 423 of file G4ReduciblePolygon.cc.

424{
425 G4double tolerance2 = tolerance*tolerance;
426 G4double one = 1.0-tolerance,
427 zero = tolerance;
428 //
429 // Top loop over line segments. By the time we finish
430 // with the second to last segment, we're done.
431 //
432 ABVertex *curr1 = vertexHead, *next1=0;
433 while (curr1->next)
434 {
435 next1 = curr1->next;
436 G4double da1 = next1->a-curr1->a,
437 db1 = next1->b-curr1->b;
438
439 //
440 // Inner loop over subsequent line segments
441 //
442 ABVertex *curr2 = next1->next;
443 while( curr2 )
444 {
445 ABVertex *next2 = curr2->next;
446 if (next2==0) next2 = vertexHead;
447 G4double da2 = next2->a-curr2->a,
448 db2 = next2->b-curr2->b;
449 G4double a12 = curr2->a-curr1->a,
450 b12 = curr2->b-curr1->b;
451
452 //
453 // Calculate intersection of the two lines
454 //
455 G4double deter = da1*db2 - db1*da2;
456 if (std::fabs(deter) > tolerance2)
457 {
458 G4double s1, s2;
459 s1 = (a12*db2-b12*da2)/deter;
460
461 if (s1 >= zero && s1 < one)
462 {
463 s2 = -(da1*b12-db1*a12)/deter;
464 if (s2 >= zero && s2 < one) return true;
465 }
466 }
467 curr2 = curr2->next;
468 }
469 curr1 = next1;
470 }
471 return false;
472}

Referenced by G4Polyhedra::Create(), and G4Polycone::Create().

◆ NumVertices()

G4int G4ReduciblePolygon::NumVertices ( ) const
inline

Definition at line 84 of file G4ReduciblePolygon.hh.

84{ return numVertices; }

Referenced by G4Polyhedra::Create(), G4Polycone::Create(), and G4PolyPhiFace::G4PolyPhiFace().

◆ Print()

void G4ReduciblePolygon::Print ( )

Definition at line 547 of file G4ReduciblePolygon.cc.

548{
549 ABVertex *curr = vertexHead;
550 do
551 {
552 G4cerr << curr->a << " " << curr->b << G4endl;
553 curr = curr->next;
554 } while( curr );
555}
#define G4endl
Definition: G4ios.hh:52
G4DLLIMPORT std::ostream G4cerr

◆ RemoveDuplicateVertices()

G4bool G4ReduciblePolygon::RemoveDuplicateVertices ( G4double  tolerance)

Definition at line 219 of file G4ReduciblePolygon.cc.

220{
221 ABVertex *curr = vertexHead,
222 *prev = 0, *next = 0;
223 while( curr )
224 {
225 next = curr->next;
226 if (next == 0) next = vertexHead;
227
228 if (std::fabs(curr->a-next->a) < tolerance &&
229 std::fabs(curr->b-next->b) < tolerance )
230 {
231 //
232 // Duplicate found: do we have > 3 vertices?
233 //
234 if (numVertices <= 3)
235 {
237 return false;
238 }
239
240 //
241 // Delete
242 //
243 ABVertex *toDelete = curr;
244 curr = curr->next;
245 delete toDelete;
246
247 numVertices--;
248
249 if (prev) prev->next = curr; else vertexHead = curr;
250 }
251 else
252 {
253 prev = curr;
254 curr = curr->next;
255 }
256 }
257
258 //
259 // In principle, this is not needed, but why not just play it safe?
260 //
262
263 return true;
264}

Referenced by G4Polyhedra::Create(), and G4Polycone::Create().

◆ RemoveRedundantVertices()

G4bool G4ReduciblePolygon::RemoveRedundantVertices ( G4double  tolerance)

Definition at line 273 of file G4ReduciblePolygon.cc.

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

Referenced by G4Polyhedra::Create(), and G4Polycone::Create().

◆ ReverseOrder()

void G4ReduciblePolygon::ReverseOrder ( )

Definition at line 368 of file G4ReduciblePolygon.cc.

369{
370 //
371 // Loop over all vertices
372 //
373 ABVertex *prev = vertexHead;
374 if (prev==0) return; // No vertices
375
376 ABVertex *curr = prev->next;
377 if (curr==0) return; // Just one vertex
378
379 //
380 // Our new tail
381 //
382 vertexHead->next = 0;
383
384 for(;;)
385 {
386 //
387 // Save pointer to next vertex (in original order)
388 //
389 ABVertex *save = curr->next;
390
391 //
392 // Replace it with a pointer to the previous one
393 // (in original order)
394 //
395 curr->next = prev;
396
397 //
398 // Last vertex?
399 //
400 if (save == 0) break;
401
402 //
403 // Next vertex
404 //
405 prev = curr;
406 curr = save;
407 }
408
409 //
410 // Our new head
411 //
412 vertexHead = curr;
413}

Referenced by G4Polyhedra::Create(), and G4Polycone::Create().

◆ ScaleA()

void G4ReduciblePolygon::ScaleA ( G4double  scale)

Definition at line 186 of file G4ReduciblePolygon.cc.

187{
188 ABVertex *curr = vertexHead;
189 while( curr )
190 {
191 curr->a *= scale;
192 curr = curr->next;
193 }
194}

Referenced by G4Polyhedra::G4Polyhedra().

◆ ScaleB()

void G4ReduciblePolygon::ScaleB ( G4double  scale)

Definition at line 202 of file G4ReduciblePolygon.cc.

203{
204 ABVertex *curr = vertexHead;
205 while( curr )
206 {
207 curr->b *= scale;
208 curr = curr->next;
209 }
210}

Friends And Related Function Documentation

◆ ABVertex

friend struct ABVertex
friend

Definition at line 140 of file G4ReduciblePolygon.hh.

◆ G4ReduciblePolygonIterator

friend class G4ReduciblePolygonIterator
friend

Definition at line 63 of file G4ReduciblePolygon.hh.

Member Data Documentation

◆ aMax

G4double G4ReduciblePolygon::aMax
protected

Definition at line 130 of file G4ReduciblePolygon.hh.

Referenced by Amax(), and CalculateMaxMin().

◆ aMin

G4double G4ReduciblePolygon::aMin
protected

Definition at line 130 of file G4ReduciblePolygon.hh.

Referenced by Amin(), and CalculateMaxMin().

◆ bMax

G4double G4ReduciblePolygon::bMax
protected

Definition at line 130 of file G4ReduciblePolygon.hh.

Referenced by Bmax(), and CalculateMaxMin().

◆ bMin

G4double G4ReduciblePolygon::bMin
protected

Definition at line 130 of file G4ReduciblePolygon.hh.

Referenced by Bmin(), and CalculateMaxMin().

◆ numVertices

G4int G4ReduciblePolygon::numVertices
protected

◆ vertexHead


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