G4ReduciblePolygon.hh

Go to the documentation of this file.
00001 //
00002 // ********************************************************************
00003 // * License and Disclaimer                                           *
00004 // *                                                                  *
00005 // * The  Geant4 software  is  copyright of the Copyright Holders  of *
00006 // * the Geant4 Collaboration.  It is provided  under  the terms  and *
00007 // * conditions of the Geant4 Software License,  included in the file *
00008 // * LICENSE and available at  http://cern.ch/geant4/license .  These *
00009 // * include a list of copyright holders.                             *
00010 // *                                                                  *
00011 // * Neither the authors of this software system, nor their employing *
00012 // * institutes,nor the agencies providing financial support for this *
00013 // * work  make  any representation or  warranty, express or implied, *
00014 // * regarding  this  software system or assume any liability for its *
00015 // * use.  Please see the license in the file  LICENSE  and URL above *
00016 // * for the full disclaimer and the limitation of liability.         *
00017 // *                                                                  *
00018 // * This  code  implementation is the result of  the  scientific and *
00019 // * technical work of the GEANT4 collaboration.                      *
00020 // * By using,  copying,  modifying or  distributing the software (or *
00021 // * any work based  on the software)  you  agree  to acknowledge its *
00022 // * use  in  resulting  scientific  publications,  and indicate your *
00023 // * acceptance of all terms of the Geant4 Software license.          *
00024 // ********************************************************************
00025 //
00026 //
00027 // $Id: G4ReduciblePolygon.hh 67011 2013-01-29 16:17:41Z gcosmo $
00028 //
00029 // 
00030 // --------------------------------------------------------------------
00031 // GEANT 4 class header file
00032 //
00033 //
00034 // G4ReduciblePolygon.hh
00035 //
00036 // Class description:
00037 //
00038 //   Utility class used to specify, test, reduce, and/or otherwise
00039 //   manipulate a 2D polygon.
00040 //
00041 //   For this class, a polygon consists of n > 2 points in 2D
00042 //   space (a,b). The polygon is always closed by connecting the
00043 //   last point to the first. A G4ReduciblePolygon is guaranteed
00044 //   to fulfill this definition in all instances. 
00045 //
00046 //   Illegal manipulations (such that a valid polygon would be
00047 //   produced) result in an error return if possible and 
00048 //   otherwise a G4Exception.
00049 //
00050 //   The set of manipulations is limited currently to what
00051 //   is needed for G4Polycone and G4Polyhedra.
00052 
00053 // Author: 
00054 //   David C. Williams (davidw@scipp.ucsc.edu)
00055 // --------------------------------------------------------------------
00056 #ifndef G4ReduciblePolygon_hh
00057 #define G4ReduciblePolygon_hh
00058 
00059 #include "G4Types.hh"
00060 
00061 class G4ReduciblePolygon
00062 {
00063   friend class G4ReduciblePolygonIterator;
00064 
00065   public:
00066     //
00067     // Creator: via simple a/b arrays
00068     //
00069     G4ReduciblePolygon( const G4double a[], const G4double b[], G4int n );
00070   
00071     //
00072     // Creator: a special version for G4Polygon and G4Polycone
00073     // that takes two a points at planes of b
00074     // (where a==r and b==z for the GEANT3 classic PCON and PGON)
00075     //
00076     G4ReduciblePolygon( const G4double rmin[], const G4double rmax[],
00077                         const G4double z[], G4int n );
00078   
00079     virtual ~G4ReduciblePolygon();
00080   
00081     //
00082     // Queries
00083     //
00084     inline G4int NumVertices() const { return numVertices; }
00085   
00086     inline G4double Amin() const { return aMin; }
00087     inline G4double Amax() const { return aMax; }
00088     inline G4double Bmin() const { return bMin; }
00089     inline G4double Bmax() const { return bMax; }
00090   
00091     void CopyVertices( G4double a[], G4double b[] ) const;
00092 
00093     //
00094     // Manipulations
00095     //
00096     void ScaleA( G4double scale );
00097     void ScaleB( G4double scale );
00098   
00099     G4bool RemoveDuplicateVertices( G4double tolerance );
00100     G4bool RemoveRedundantVertices( G4double tolerance );
00101   
00102     void ReverseOrder();
00103 
00104     //
00105     // Tests
00106     //
00107     G4double Area();
00108     G4bool CrossesItself( G4double tolerance );
00109     G4bool BisectedBy( G4double a1, G4double b1,
00110            G4double a2, G4double b2, G4double tolerance );
00111   
00112     void Print();  // Debugging only
00113   
00114   public:  // without description
00115 
00116     G4ReduciblePolygon(__void__&);
00117       // Fake default constructor for usage restricted to direct object
00118       // persistency for clients requiring preallocation of memory for
00119       // persistifiable objects.
00120 
00121   protected:
00122   
00123     void Create( const G4double a[], const G4double b[], G4int n );
00124   
00125     void CalculateMaxMin();
00126   
00127     //
00128     // Below are member values that are *always* kept up to date (please!)
00129     //
00130     G4double aMin, aMax, bMin, bMax;
00131     G4int   numVertices;
00132   
00133     //
00134     // A subclass which holds the vertices in a single-linked list
00135     //
00136     // Yeah, call me an old-fashioned c hacker, but I cannot make
00137     // myself use the rogue tools for this trivial list.
00138     //
00139     struct ABVertex;              // Secret recipe for allowing
00140     friend struct ABVertex;       // protected nested structures
00141     struct ABVertex
00142     {
00143       ABVertex() : a(0.), b(0.), next(0) {}
00144       G4double a, b;
00145       ABVertex *next;
00146     };
00147   
00148     ABVertex *vertexHead;
00149 
00150     private:
00151 
00152       G4ReduciblePolygon(const G4ReduciblePolygon&);
00153       G4ReduciblePolygon& operator=(const G4ReduciblePolygon&);
00154       // Private copy constructor and assignment operator.
00155 };
00156 
00157 
00158 //
00159 // A companion class for iterating over the vertices of our polygon.
00160 // It is simple enough that all routines are declared inline here.
00161 //
00162 class G4ReduciblePolygonIterator
00163 {
00164   public:
00165 
00166     G4ReduciblePolygonIterator( const G4ReduciblePolygon *theSubject )
00167      { subject = theSubject; current=0; }
00168   
00169     void  Begin() { current = subject->vertexHead; }  
00170     G4bool  Next()  { if (current) current=current->next; return Valid(); }
00171   
00172     G4bool  Valid() const { return current!=0; }  
00173   
00174     G4double GetA() const { return current->a; }
00175     G4double GetB() const { return current->b; }
00176   
00177   protected:
00178 
00179     const G4ReduciblePolygon  *subject;      // Who are we iterating over
00180     G4ReduciblePolygon::ABVertex  *current;  // Current vertex
00181 };
00182 
00183 #endif

Generated on Mon May 27 17:49:42 2013 for Geant4 by  doxygen 1.4.7