G4ReduciblePolygon Class Reference

#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)
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

Data Structures

struct  ABVertex


Detailed Description

Definition at line 61 of file G4ReduciblePolygon.hh.


Constructor & Destructor Documentation

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

Definition at line 49 of file G4ReduciblePolygon.cc.

References Create().

00052   : aMin(0.), aMax(0.), bMin(0.), bMax(0.),
00053     vertexHead(0)
00054 {
00055   //
00056   // Do all of the real work in Create
00057   //
00058   Create( a, b, n );
00059 }

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

Definition at line 65 of file G4ReduciblePolygon.cc.

References Create().

00068   : aMin(0.), aMax(0.), bMin(0.), bMax(0.),
00069     vertexHead(0)
00070 {
00071   //
00072   // Translate
00073   //
00074   G4double *a = new G4double[n*2];
00075   G4double *b = new G4double[n*2];
00076   
00077   G4double *rOut = a + n,
00078            *zOut = b + n,
00079             *rIn = rOut-1,
00080             *zIn = zOut-1;
00081   
00082   G4int i;   
00083   for( i=0; i < n; i++, rOut++, zOut++, rIn--, zIn-- )
00084   {
00085     *rOut = rmax[i];
00086     *rIn  = rmin[i];
00087     *zOut = *zIn = z[i];
00088   }
00089   
00090   Create( a, b, n*2 );
00091   
00092   delete [] a;
00093   delete [] b;
00094 }

G4ReduciblePolygon::~G4ReduciblePolygon (  )  [virtual]

Definition at line 149 of file G4ReduciblePolygon.cc.

References G4ReduciblePolygon::ABVertex::next, and vertexHead.

00150 {
00151   ABVertex *curr = vertexHead;
00152   while( curr )
00153   {
00154     ABVertex *toDelete = curr;
00155     curr = curr->next;
00156     delete toDelete;
00157   }
00158 }

G4ReduciblePolygon::G4ReduciblePolygon ( __void__ &   ) 

Definition at line 140 of file G4ReduciblePolygon.cc.

00141   : aMin(0.), aMax(0.), bMin(0.), bMax(0.), numVertices(0), vertexHead(0)
00142 {
00143 }


Member Function Documentation

G4double G4ReduciblePolygon::Amax (  )  const [inline]

Definition at line 87 of file G4ReduciblePolygon.hh.

References aMax.

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

00087 { return aMax; }

G4double G4ReduciblePolygon::Amin (  )  const [inline]

Definition at line 86 of file G4ReduciblePolygon.hh.

References aMin.

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

00086 { return aMin; }

G4double G4ReduciblePolygon::Area (  ) 

Definition at line 526 of file G4ReduciblePolygon.cc.

References G4ReduciblePolygon::ABVertex::a, G4ReduciblePolygon::ABVertex::b, G4ReduciblePolygon::ABVertex::next, and vertexHead.

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

00527 {
00528   G4double answer = 0;
00529   
00530   ABVertex *curr = vertexHead, *next;
00531   do
00532   {
00533     next = curr->next;
00534     if (next==0) next = vertexHead;
00535     
00536     answer += curr->a*next->b - curr->b*next->a;
00537     curr = curr->next;
00538   } while( curr );
00539   
00540   return 0.5*answer;
00541 }

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

Definition at line 481 of file G4ReduciblePolygon.cc.

References G4ReduciblePolygon::ABVertex::a, G4ReduciblePolygon::ABVertex::b, G4ReduciblePolygon::ABVertex::next, and vertexHead.

Referenced by G4Polycone::Create().

00484 {
00485   G4int nNeg = 0, nPos = 0;
00486   
00487   G4double a12 = a2-a1, b12 = b2-b1;
00488   G4double len12 = std::sqrt( a12*a12 + b12*b12 );
00489   a12 /= len12; b12 /= len12;
00490   
00491   ABVertex *curr = vertexHead;
00492   do
00493   {
00494     G4double av = curr->a - a1,
00495        bv = curr->b - b1;
00496        
00497     G4double cross = av*b12 - bv*a12;
00498     
00499     if (cross < -tolerance)
00500     {
00501       if (nPos) return true;
00502       nNeg++;
00503     }
00504     else if (cross > tolerance)
00505     {
00506       if (nNeg) return true;
00507       nPos++;
00508     }
00509     curr = curr->next;
00510   } while( curr );
00511     
00512   return false;
00513 }

G4double G4ReduciblePolygon::Bmax (  )  const [inline]

Definition at line 89 of file G4ReduciblePolygon.hh.

References bMax.

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

00089 { return bMax; }

G4double G4ReduciblePolygon::Bmin (  )  const [inline]

Definition at line 88 of file G4ReduciblePolygon.hh.

References bMin.

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

00088 { return bMin; }

void G4ReduciblePolygon::CalculateMaxMin (  )  [protected]

Definition at line 564 of file G4ReduciblePolygon.cc.

References G4ReduciblePolygon::ABVertex::a, aMax, aMin, G4ReduciblePolygon::ABVertex::b, bMax, bMin, G4ReduciblePolygon::ABVertex::next, and vertexHead.

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

00565 {
00566   ABVertex *curr = vertexHead;
00567   aMin = aMax = curr->a;
00568   bMin = bMax = curr->b;
00569   curr = curr->next;
00570   while( curr )
00571   {
00572     if (curr->a < aMin)
00573       aMin = curr->a;
00574     else if (curr->a > aMax)
00575       aMax = curr->a;
00576 
00577     if (curr->b < bMin)
00578       bMin = curr->b;
00579     else if (curr->b > bMax)
00580       bMax = curr->b;
00581     
00582     curr = curr->next;
00583   }
00584 }

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

Definition at line 168 of file G4ReduciblePolygon.cc.

References G4ReduciblePolygon::ABVertex::a, G4ReduciblePolygon::ABVertex::b, G4ReduciblePolygon::ABVertex::next, and vertexHead.

00169 {
00170   G4double *anext = a, *bnext = b;
00171   ABVertex *curr = vertexHead;
00172   while( curr )
00173   {
00174     *anext++ = curr->a;
00175     *bnext++ = curr->b;
00176     curr = curr->next;
00177   }
00178 }

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

Definition at line 103 of file G4ReduciblePolygon.cc.

References G4ReduciblePolygon::ABVertex::a, G4ReduciblePolygon::ABVertex::b, CalculateMaxMin(), FatalErrorInArgument, G4Exception(), G4ReduciblePolygon::ABVertex::next, numVertices, and vertexHead.

Referenced by G4ReduciblePolygon().

00105 {
00106   if (n<3)
00107    G4Exception("G4ReduciblePolygon::Create()", "GeomSolids0002",
00108                FatalErrorInArgument, "Less than 3 vertices specified.");
00109   
00110   const G4double *anext = a, *bnext = b;
00111   ABVertex *prev = 0;
00112   do
00113   {
00114     ABVertex *newVertex = new ABVertex;
00115     newVertex->a = *anext;
00116     newVertex->b = *bnext;
00117     newVertex->next = 0;
00118     if (prev==0)
00119     {
00120       vertexHead = newVertex;
00121     }
00122     else
00123     {
00124       prev->next = newVertex;
00125     }
00126       
00127     prev = newVertex;
00128   } while( ++anext, ++bnext < b+n );
00129 
00130   numVertices = n;
00131   
00132   CalculateMaxMin();
00133 }

G4bool G4ReduciblePolygon::CrossesItself ( G4double  tolerance  ) 

Definition at line 423 of file G4ReduciblePolygon.cc.

References G4ReduciblePolygon::ABVertex::a, G4ReduciblePolygon::ABVertex::b, G4ReduciblePolygon::ABVertex::next, and vertexHead.

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

00424 {
00425   G4double tolerance2 = tolerance*tolerance;
00426   G4double one  = 1.0-tolerance,
00427            zero = tolerance;
00428   //
00429   // Top loop over line segments. By the time we finish
00430   // with the second to last segment, we're done.
00431   //
00432   ABVertex *curr1 = vertexHead, *next1=0;
00433   while (curr1->next)
00434   {
00435     next1 = curr1->next;
00436     G4double da1 = next1->a-curr1->a,
00437              db1 = next1->b-curr1->b;
00438     
00439     //
00440     // Inner loop over subsequent line segments
00441     //
00442     ABVertex *curr2 = next1->next;
00443     while( curr2 )
00444     {
00445       ABVertex *next2 = curr2->next;
00446       if (next2==0) next2 = vertexHead;
00447       G4double da2 = next2->a-curr2->a,
00448                db2 = next2->b-curr2->b;
00449       G4double a12 = curr2->a-curr1->a,
00450                b12 = curr2->b-curr1->b;
00451          
00452       //
00453       // Calculate intersection of the two lines
00454       //
00455       G4double deter = da1*db2 - db1*da2;
00456       if (std::fabs(deter) > tolerance2)
00457       {
00458         G4double s1, s2;
00459         s1 = (a12*db2-b12*da2)/deter;
00460         
00461         if (s1 >= zero && s1 < one)
00462         {
00463           s2 = -(da1*b12-db1*a12)/deter;
00464           if (s2 >= zero && s2 < one) return true;
00465         }
00466       }
00467       curr2 = curr2->next;   
00468     }
00469     curr1 = next1;
00470   }
00471   return false;
00472 }

G4int G4ReduciblePolygon::NumVertices (  )  const [inline]

Definition at line 84 of file G4ReduciblePolygon.hh.

References numVertices.

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

00084 { return numVertices; }

void G4ReduciblePolygon::Print (  ) 

Definition at line 547 of file G4ReduciblePolygon.cc.

References G4ReduciblePolygon::ABVertex::a, G4ReduciblePolygon::ABVertex::b, G4cerr, G4endl, G4ReduciblePolygon::ABVertex::next, and vertexHead.

00548 {
00549   ABVertex *curr = vertexHead;
00550   do
00551   {
00552     G4cerr << curr->a << " " << curr->b << G4endl;
00553     curr = curr->next;
00554   } while( curr );
00555 }

G4bool G4ReduciblePolygon::RemoveDuplicateVertices ( G4double  tolerance  ) 

Definition at line 219 of file G4ReduciblePolygon.cc.

References G4ReduciblePolygon::ABVertex::a, G4ReduciblePolygon::ABVertex::b, CalculateMaxMin(), G4ReduciblePolygon::ABVertex::next, numVertices, and vertexHead.

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

00220 {
00221   ABVertex *curr = vertexHead, 
00222            *prev = 0, *next = 0;
00223   while( curr )
00224   {
00225     next = curr->next;
00226     if (next == 0) next = vertexHead;
00227     
00228     if (std::fabs(curr->a-next->a) < tolerance &&
00229         std::fabs(curr->b-next->b) < tolerance     )
00230     {
00231       //
00232       // Duplicate found: do we have > 3 vertices?
00233       //
00234       if (numVertices <= 3)
00235       {
00236         CalculateMaxMin();
00237         return false;
00238       }
00239       
00240       //
00241       // Delete
00242       //
00243       ABVertex *toDelete = curr;
00244       curr = curr->next;
00245       delete toDelete;
00246       
00247       numVertices--;
00248       
00249       if (prev) prev->next = curr; else vertexHead = curr;
00250     }
00251     else
00252     {
00253       prev = curr;
00254       curr = curr->next;
00255     }
00256   }
00257   
00258   //
00259   // In principle, this is not needed, but why not just play it safe?
00260   //
00261   CalculateMaxMin();
00262   
00263   return true;
00264 }

G4bool G4ReduciblePolygon::RemoveRedundantVertices ( G4double  tolerance  ) 

Definition at line 273 of file G4ReduciblePolygon.cc.

References G4ReduciblePolygon::ABVertex::a, G4ReduciblePolygon::ABVertex::b, CalculateMaxMin(), G4ReduciblePolygon::ABVertex::next, numVertices, and vertexHead.

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

00274 {
00275   //
00276   // Under these circumstances, we can quit now!
00277   //
00278   if (numVertices <= 2) return false;
00279   
00280   G4double tolerance2 = tolerance*tolerance;
00281 
00282   //
00283   // Loop over all vertices
00284   //
00285   ABVertex *curr = vertexHead, *next = 0;
00286   while( curr )
00287   {
00288     next = curr->next;
00289     if (next == 0) next = vertexHead;
00290     
00291     G4double da = next->a - curr->a,
00292              db = next->b - curr->b;
00293     
00294     //
00295     // Loop over all subsequent vertices, up to curr
00296     //
00297     for(;;)
00298     {
00299       //
00300       // Get vertex after next
00301       //
00302       ABVertex *test = next->next;
00303       if (test == 0) test = vertexHead;
00304       
00305       //
00306       // If we are back to the original vertex, stop
00307       //
00308       if (test==curr) break;
00309     
00310       //
00311       // Test for parallel line segments
00312       //
00313       G4double dat = test->a - curr->a,
00314                dbt = test->b - curr->b;
00315          
00316       if (std::fabs(dat*db-dbt*da)>tolerance2) break;
00317       
00318       //
00319       // Redundant vertex found: do we have > 3 vertices?
00320       // 
00321       if (numVertices <= 3)
00322       {
00323         CalculateMaxMin();
00324         return false;
00325       }
00326 
00327       //
00328       // Delete vertex pointed to by next. Carefully!
00329       //
00330       if (curr->next)
00331       {    // next is not head
00332         if (next->next)
00333           curr->next = test;  // next is not tail
00334         else
00335           curr->next = 0;    // New tail
00336       }
00337       else
00338         vertexHead = test;  // New head
00339         
00340       if ((curr != next) && (next != test)) delete next;
00341       
00342       numVertices--;
00343       
00344       //
00345       // Replace next by the vertex we just tested,
00346       // and keep on going...
00347       //
00348       next = test;
00349       da = dat; db = dbt;
00350     }
00351     curr = curr->next;
00352   }
00353   
00354   //
00355   // In principle, this is not needed, but why not just play it safe?
00356   //
00357   CalculateMaxMin();
00358   
00359   return true;
00360 }

void G4ReduciblePolygon::ReverseOrder (  ) 

Definition at line 368 of file G4ReduciblePolygon.cc.

References G4ReduciblePolygon::ABVertex::next, and vertexHead.

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

00369 {
00370   //
00371   // Loop over all vertices
00372   //
00373   ABVertex *prev = vertexHead;
00374   if (prev==0) return;    // No vertices
00375   
00376   ABVertex *curr = prev->next;
00377   if (curr==0) return;    // Just one vertex
00378   
00379   //
00380   // Our new tail
00381   //
00382   vertexHead->next = 0;
00383   
00384   for(;;)
00385   {
00386     //
00387     // Save pointer to next vertex (in original order)
00388     //
00389     ABVertex *save = curr->next;
00390     
00391     //
00392     // Replace it with a pointer to the previous one
00393     // (in original order)
00394     //
00395     curr->next = prev;
00396     
00397     //
00398     // Last vertex?
00399     //
00400     if (save == 0) break;
00401     
00402     //
00403     // Next vertex
00404     //
00405     prev = curr;
00406     curr = save;
00407   }
00408   
00409   //
00410   // Our new head
00411   //
00412   vertexHead = curr;
00413 }

void G4ReduciblePolygon::ScaleA ( G4double  scale  ) 

Definition at line 186 of file G4ReduciblePolygon.cc.

References G4ReduciblePolygon::ABVertex::a, G4ReduciblePolygon::ABVertex::next, and vertexHead.

Referenced by G4Polyhedra::G4Polyhedra().

00187 {
00188   ABVertex *curr = vertexHead;
00189   while( curr )
00190   {
00191     curr->a *= scale;
00192     curr = curr->next;
00193   }
00194 }  

void G4ReduciblePolygon::ScaleB ( G4double  scale  ) 

Definition at line 202 of file G4ReduciblePolygon.cc.

References G4ReduciblePolygon::ABVertex::b, G4ReduciblePolygon::ABVertex::next, and vertexHead.

00203 {
00204   ABVertex *curr = vertexHead;
00205   while( curr )
00206   {
00207     curr->b *= scale;
00208     curr = curr->next;
00209   }
00210 }  


Friends And Related Function Documentation

friend struct ABVertex [friend]

Definition at line 139 of file G4ReduciblePolygon.hh.

friend class G4ReduciblePolygonIterator [friend]

Definition at line 63 of file G4ReduciblePolygon.hh.


Field Documentation

G4double G4ReduciblePolygon::aMax [protected]

Definition at line 130 of file G4ReduciblePolygon.hh.

Referenced by Amax(), and CalculateMaxMin().

G4double G4ReduciblePolygon::aMin [protected]

Definition at line 130 of file G4ReduciblePolygon.hh.

Referenced by Amin(), and CalculateMaxMin().

G4double G4ReduciblePolygon::bMax [protected]

Definition at line 130 of file G4ReduciblePolygon.hh.

Referenced by Bmax(), and CalculateMaxMin().

G4double G4ReduciblePolygon::bMin [protected]

Definition at line 130 of file G4ReduciblePolygon.hh.

Referenced by Bmin(), and CalculateMaxMin().

G4int G4ReduciblePolygon::numVertices [protected]

Definition at line 131 of file G4ReduciblePolygon.hh.

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

ABVertex* G4ReduciblePolygon::vertexHead [protected]

Definition at line 148 of file G4ReduciblePolygon.hh.

Referenced by Area(), G4ReduciblePolygonIterator::Begin(), BisectedBy(), CalculateMaxMin(), CopyVertices(), Create(), CrossesItself(), Print(), RemoveDuplicateVertices(), RemoveRedundantVertices(), ReverseOrder(), ScaleA(), ScaleB(), and ~G4ReduciblePolygon().


The documentation for this class was generated from the following files:
Generated on Mon May 27 17:53:16 2013 for Geant4 by  doxygen 1.4.7