Geant4 11.4.0
Toolkit for the simulation of the passage of particles through matter
Loading...
Searching...
No Matches
G4ReduciblePolygon.hh
Go to the documentation of this file.
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 (UCSC), 1998
46// --------------------------------------------------------------------
47#ifndef G4REDUCIBLEPOLYGON_HH
48#define G4REDUCIBLEPOLYGON_HH
49
50#include "G4Types.hh"
51
52/**
53 * @brief G4ReduciblePolygon is a utility class used to specify, test, reduce,
54 * and/or otherwise manipulate a 2D polygon.
55 */
56
58{
60
61 public:
62
63 /**
64 * Constructor of G4ReduciblePolygon via simple a/b arrays.
65 * @param[in] a First array of points.
66 * @param[in] b Second array of points.
67 * @param[in] n The number of vertices of the polygon (has to be >=3).
68 */
69 G4ReduciblePolygon( const G4double a[], const G4double b[], G4int n );
70
71 /**
72 * Special constructor version for G4Polyhedra and G4Polycone, that takes
73 * two a points at planes of b (where a==r and b==z).
74 * @param[in] rmin Array of r-min coordinates of corners.
75 * @param[in] rmax Array of r-max coordinates of corners.
76 * @param[in] z Array of Z coordinates of corners.
77 * @param[in] n The number of vertices of the polygon.
78 */
79 G4ReduciblePolygon( const G4double rmin[], const G4double rmax[],
80 const G4double z[], G4int n );
81
82 /**
83 * Copy constructor and assignment operator not allowed.
84 */
87
88 /**
89 * Destructor, taking care to clear allocated lists.
90 */
92
93 /**
94 * Accessors.
95 */
96 inline G4int NumVertices() const { return numVertices; }
97 inline G4double Amin() const { return aMin; }
98 inline G4double Amax() const { return aMax; }
99 inline G4double Bmin() const { return bMin; }
100 inline G4double Bmax() const { return bMax; }
101
102 /**
103 * Copies contents of provided arrays into simple linear arrays.
104 */
105 void CopyVertices( G4double a[], G4double b[] ) const;
106
107 /**
108 * Methods to multiply all a or b values by a common scale.
109 */
110 void ScaleA( G4double scale );
111 void ScaleB( G4double scale );
112
113 /**
114 * Removes adjacent vertices that are equal.
115 * @param[in] tolerance Provided tolerance for adjacent vertices.
116 * @returns false, if there is a problem (too few vertices remaining).
117 */
119
120 /**
121 * Removes any unneeded vertices, i.e. those vertices which are on the
122 * line connecting the previous and next vertices.
123 * @param[in] tolerance Provided tolerance for parallel line segments.
124 * @returns false, if there is a problem (too few vertices remaining).
125 */
127
128 /**
129 * Reverses the order of the vertices.
130 */
131 void ReverseOrder();
132
133 /**
134 * Method is used for G4GenericPolycone; starting always with Zmin=bMin.
135 */
136 void StartWithZMin();
137
138 // Methods for tests
139
140 /**
141 * Calculates signed polygon area, where polygons specified in a
142 * clockwise manner have negative area.
143 */
144 G4double Area();
145
146 /**
147 * Returns "true" if the polygon crosses itself.
148 */
149 G4bool CrossesItself( G4double tolerance );
150
151 /**
152 * Decides if a line through two points crosses the polygon,
153 * within tolerance.
154 */
156 G4double a2, G4double b2, G4double tolerance );
157
158 /**
159 * Print function for debugging.
160 */
161 void Print();
162
163 /**
164 * Fake default constructor for usage restricted to direct object
165 * persistency for clients requiring preallocation of memory for
166 * persistifiable objects.
167 */
168 G4ReduciblePolygon(__void__&);
169
170 private:
171
172 /**
173 * Create the polygon; used in constructors.
174 */
175 void Create( const G4double a[], const G4double b[], G4int n );
176
177 /**
178 * Re-calculates global values. To be called when the vertices are changed.
179 */
180 void CalculateMaxMin();
181
182 private:
183
184 // Below are member values that are *always* kept up to date
185 //
186 G4double aMin, aMax, bMin, bMax;
187 G4int numVertices = 0;
188
189
190 /**
191 * A subclass which holds the vertices in a single-linked list.
192 */
193 struct ABVertex; // Secret recipe for allowing
194 friend struct ABVertex; // protected nested structures
195 struct ABVertex
196 {
197 ABVertex() = default;
198 G4double a{0.}, b{0.};
199 ABVertex *next{nullptr};
200 };
201
202 ABVertex* vertexHead = nullptr;
203};
204
205// A companion class for iterating over the vertices of our polygon.
206// It is simple enough that all routines are declared inline here.
207//
208
209/**
210 * @brief G4ReduciblePolygonIterator is companion class for iterating over
211 * the vertices of a polygon.
212 */
213
215{
216 public:
217
219 {
220 subject = theSubject; current = nullptr;
221 }
222
223 inline void Begin() { current = subject->vertexHead; }
224
225 inline G4bool Next()
226 {
227 if (current != nullptr) { current=current->next; }
228 return Valid();
229 }
230
231 inline G4bool Valid() const { return current != nullptr; }
232
233 inline G4double GetA() const { return current->a; }
234 inline G4double GetB() const { return current->b; }
235
236 private:
237
238 const G4ReduciblePolygon* subject = nullptr; // Who are we iterating over
239 G4ReduciblePolygon::ABVertex* current = nullptr; // Current vertex
240};
241
242#endif
double G4double
Definition G4Types.hh:83
bool G4bool
Definition G4Types.hh:86
int G4int
Definition G4Types.hh:85
G4ReduciblePolygonIterator(const G4ReduciblePolygon *theSubject)
G4ReduciblePolygon is a utility class used to specify, test, reduce, and/or otherwise manipulate a 2D...
G4ReduciblePolygon(const G4double a[], const G4double b[], G4int n)
G4ReduciblePolygon & operator=(const G4ReduciblePolygon &)=delete
G4bool BisectedBy(G4double a1, G4double b1, G4double a2, G4double b2, G4double tolerance)
void ScaleB(G4double scale)
G4double Amin() const
void CopyVertices(G4double a[], G4double b[]) const
G4bool RemoveDuplicateVertices(G4double tolerance)
G4bool RemoveRedundantVertices(G4double tolerance)
G4ReduciblePolygon(const G4ReduciblePolygon &)=delete
G4double Bmin() const
G4bool CrossesItself(G4double tolerance)
G4double Amax() const
void ScaleA(G4double scale)
friend class G4ReduciblePolygonIterator