Geant4 Cross Reference |
1 // 2 // ******************************************************************** 3 // * License and Disclaimer * 4 // * * 5 // * The Geant4 software is copyright of the Copyright Holders of * 6 // * the Geant4 Collaboration. It is provided under the terms and * 7 // * conditions of the Geant4 Software License, included in the file * 8 // * LICENSE and available at http://cern.ch/geant4/license . These * 9 // * include a list of copyright holders. * 10 // * * 11 // * Neither the authors of this software system, nor their employing * 12 // * institutes,nor the agencies providing financial support for this * 13 // * work make any representation or warranty, express or implied, * 14 // * regarding this software system or assume any liability for its * 15 // * use. Please see the license in the file LICENSE and URL above * 16 // * for the full disclaimer and the limitation of liability. * 17 // * * 18 // * This code implementation is the result of the scientific and * 19 // * technical work of the GEANT4 collaboration. * 20 // * By using, copying, modifying or distributing the software (or * 21 // * any work based on the software) you agree to acknowledge its * 22 // * use in resulting scientific publications, and indicate your * 23 // * acceptance of all terms of the Geant4 Software license. * 24 // ******************************************************************** 25 // 26 // G4ReduciblePolygon 27 // 28 // Class description: 29 // 30 // Utility class used to specify, test, reduce, and/or otherwise 31 // manipulate a 2D polygon. 32 // 33 // For this class, a polygon consists of n > 2 points in 2D 34 // space (a,b). The polygon is always closed by connecting the 35 // last point to the first. A G4ReduciblePolygon is guaranteed 36 // to fulfill this definition in all instances. 37 // 38 // Illegal manipulations (such that a valid polygon would be 39 // produced) result in an error return if possible and 40 // otherwise a G4Exception. 41 // 42 // The set of manipulations is limited currently to what 43 // is needed for G4Polycone and G4Polyhedra. 44 45 // Author: David C. Williams (davidw@scipp.ucsc.edu) 46 // -------------------------------------------------------------------- 47 #ifndef G4REDUCIBLEPOLYGON_HH 48 #define G4REDUCIBLEPOLYGON_HH 1 49 50 #include "G4Types.hh" 51 52 class G4ReduciblePolygon 53 { 54 friend class G4ReduciblePolygonIterator; 55 56 public: 57 58 G4ReduciblePolygon( const G4double a[], const G4double b[], G4int n ); 59 // Creator: via simple a/b arrays 60 61 G4ReduciblePolygon( const G4double rmin[], const G4double rmax[], 62 const G4double z[], G4int n ); 63 // Creator: a special version for G4Polygon and G4Polycone 64 // that takes two a points at planes of b 65 // (where a==r and b==z for the GEANT3 classic PCON and PGON) 66 67 G4ReduciblePolygon(const G4ReduciblePolygon&) = delete; 68 G4ReduciblePolygon& operator=(const G4ReduciblePolygon&) = delete; 69 // Deleted copy constructor and assignment operator. 70 71 virtual ~G4ReduciblePolygon(); 72 73 // Queries 74 75 inline G4int NumVertices() const { return numVertices; } 76 77 inline G4double Amin() const { return aMin; } 78 inline G4double Amax() const { return aMax; } 79 inline G4double Bmin() const { return bMin; } 80 inline G4double Bmax() const { return bMax; } 81 82 void CopyVertices( G4double a[], G4double b[] ) const; 83 84 // Manipulations 85 86 void ScaleA( G4double scale ); 87 void ScaleB( G4double scale ); 88 89 G4bool RemoveDuplicateVertices( G4double tolerance ); 90 G4bool RemoveRedundantVertices( G4double tolerance ); 91 92 void ReverseOrder(); 93 void StartWithZMin(); 94 95 // Tests 96 // 97 G4double Area(); 98 G4bool CrossesItself( G4double tolerance ); 99 G4bool BisectedBy( G4double a1, G4double b1, 100 G4double a2, G4double b2, G4double tolerance ); 101 102 void Print(); // Debugging only 103 104 G4ReduciblePolygon(__void__&); 105 // Fake default constructor for usage restricted to direct object 106 // persistency for clients requiring preallocation of memory for 107 // persistifiable objects. 108 109 protected: 110 111 void Create( const G4double a[], const G4double b[], G4int n ); 112 113 void CalculateMaxMin(); 114 115 // Below are member values that are *always* kept up to date (please!) 116 // 117 G4double aMin, aMax, bMin, bMax; 118 G4int numVertices = 0; 119 120 // A subclass which holds the vertices in a single-linked list 121 // 122 struct ABVertex; // Secret recipe for allowing 123 friend struct ABVertex; // protected nested structures 124 struct ABVertex 125 { 126 ABVertex() = default; 127 G4double a{0.}, b{0.}; 128 ABVertex *next{nullptr}; 129 }; 130 131 ABVertex* vertexHead = nullptr; 132 }; 133 134 // A companion class for iterating over the vertices of our polygon. 135 // It is simple enough that all routines are declared inline here. 136 // 137 class G4ReduciblePolygonIterator 138 { 139 public: 140 141 G4ReduciblePolygonIterator( const G4ReduciblePolygon* theSubject ) 142 { 143 subject = theSubject; current = nullptr; 144 } 145 146 void Begin() { current = subject->vertexHead; } 147 148 G4bool Next() 149 { 150 if (current != nullptr) current=current->next; 151 return Valid(); 152 } 153 154 G4bool Valid() const { return current != nullptr; } 155 156 G4double GetA() const { return current->a; } 157 G4double GetB() const { return current->b; } 158 159 protected: 160 161 const G4ReduciblePolygon* subject = nullptr; // Who are we iterating over 162 G4ReduciblePolygon::ABVertex* current = nullptr; // Current vertex 163 }; 164 165 #endif 166