Geant4 11.4.0
Toolkit for the simulation of the passage of particles through matter
Loading...
Searching...
No Matches
G4UGenericPolycone.cc
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// Implementation of G4UGenericPolycone wrapper class
27//
28// 30.10.13 G.Cosmo, CERN
29// --------------------------------------------------------------------
30
31#include "G4GenericPolycone.hh"
32#include "G4UGenericPolycone.hh"
33
34#if ( defined(G4GEOM_USE_USOLIDS) || defined(G4GEOM_USE_PARTIAL_USOLIDS) )
35
36#include "G4GeomTools.hh"
37#include "G4AffineTransform.hh"
39#include "G4BoundingEnvelope.hh"
40
41#include "G4Polyhedron.hh"
42
43using namespace CLHEP;
44
45////////////////////////////////////////////////////////////////////////
46//
47// Constructor (generic parameters)
48//
49G4UGenericPolycone::G4UGenericPolycone(const G4String& name,
50 G4double phiStart,
51 G4double phiTotal,
52 G4int numRZ,
53 const G4double r[],
54 const G4double z[] )
55 : Base_t(name, phiStart, phiTotal, numRZ, r, z)
56{
57 wrStart = phiStart; while (wrStart < 0) wrStart += twopi;
58 wrDelta = phiTotal;
59 if (wrDelta <= 0 || wrDelta >= twopi*(1-DBL_EPSILON))
60 {
61 wrStart = 0;
62 wrDelta = twopi;
63 }
64 rzcorners.resize(0);
65 for (G4int i=0; i<numRZ; ++i)
66 {
67 rzcorners.emplace_back(r[i],z[i]);
68 }
69 std::vector<G4int> iout;
71}
72
73
74//////////////////////////////////////////////////////////////////////////
75//
76// Copy constructor
77//
78G4UGenericPolycone::G4UGenericPolycone(const G4UGenericPolycone& source)
79 : Base_t(source)
80{
81 wrStart = source.wrStart;
82 wrDelta = source.wrDelta;
83 rzcorners = source.rzcorners;
84}
85
86
87//////////////////////////////////////////////////////////////////////////
88//
89// Assignment operator
90//
91G4UGenericPolycone&
92G4UGenericPolycone::operator=(const G4UGenericPolycone& source)
93{
94 if (this == &source) return *this;
95
96 Base_t::operator=( source );
97 wrStart = source.wrStart;
98 wrDelta = source.wrDelta;
99 rzcorners = source.rzcorners;
100
101 return *this;
102}
103
104G4double G4UGenericPolycone::GetStartPhi() const
105{
106 return wrStart;
107}
108G4double G4UGenericPolycone::GetEndPhi() const
109{
110 return (wrStart + wrDelta);
111}
112G4double G4UGenericPolycone::GetSinStartPhi() const
113{
114 if (IsOpen()) return 0.;
115 G4double phi = GetStartPhi();
116 return std::sin(phi);
117}
118G4double G4UGenericPolycone::GetCosStartPhi() const
119{
120 if (IsOpen()) return 1.;
121 G4double phi = GetStartPhi();
122 return std::cos(phi);
123}
124G4double G4UGenericPolycone::GetSinEndPhi() const
125{
126 if (IsOpen()) return 0.;
127 G4double phi = GetEndPhi();
128 return std::sin(phi);
129}
130G4double G4UGenericPolycone::GetCosEndPhi() const
131{
132 if (IsOpen()) return 1.;
133 G4double phi = GetEndPhi();
134 return std::cos(phi);
135}
136G4bool G4UGenericPolycone::IsOpen() const
137{
138 return (wrDelta < twopi);
139}
140G4int G4UGenericPolycone::GetNumRZCorner() const
141{
142 return rzcorners.size();
143}
144G4PolyconeSideRZ G4UGenericPolycone::GetCorner(G4int index) const
145{
146 G4TwoVector rz = rzcorners.at(index);
147 G4PolyconeSideRZ psiderz = { rz.x(), rz.y() };
148
149 return psiderz;
150}
151
152//////////////////////////////////////////////////////////////////////////
153//
154// Make a clone of the object
155
156G4VSolid* G4UGenericPolycone::Clone() const
157{
158 return new G4UGenericPolycone(*this);
159}
160
161//////////////////////////////////////////////////////////////////////////
162//
163// Get bounding box
164
165void
166G4UGenericPolycone::BoundingLimits(G4ThreeVector& pMin,
167 G4ThreeVector& pMax) const
168{
169 G4double rmin = kInfinity, rmax = -kInfinity;
170 G4double zmin = kInfinity, zmax = -kInfinity;
171
172 for (G4int i=0; i<GetNumRZCorner(); ++i)
173 {
174 G4PolyconeSideRZ corner = GetCorner(i);
175 if (corner.r < rmin) rmin = corner.r;
176 if (corner.r > rmax) rmax = corner.r;
177 if (corner.z < zmin) zmin = corner.z;
178 if (corner.z > zmax) zmax = corner.z;
179 }
180
181 if (IsOpen())
182 {
183 G4TwoVector vmin,vmax;
184 G4GeomTools::DiskExtent(rmin,rmax,
185 GetSinStartPhi(),GetCosStartPhi(),
186 GetSinEndPhi(),GetCosEndPhi(),
187 vmin,vmax);
188 pMin.set(vmin.x(),vmin.y(),zmin);
189 pMax.set(vmax.x(),vmax.y(),zmax);
190 }
191 else
192 {
193 pMin.set(-rmax,-rmax, zmin);
194 pMax.set( rmax, rmax, zmax);
195 }
196
197 // Check correctness of the bounding box
198 //
199 if (pMin.x() >= pMax.x() || pMin.y() >= pMax.y() || pMin.z() >= pMax.z())
200 {
201 std::ostringstream message;
202 message << "Bad bounding box (min >= max) for solid: "
203 << GetName() << " !"
204 << "\npMin = " << pMin
205 << "\npMax = " << pMax;
206 G4Exception("G4UGenericPolycone::BoundingLimits()", "GeomMgt0001",
207 JustWarning, message);
208 StreamInfo(G4cout);
209 }
210}
211
212//////////////////////////////////////////////////////////////////////////
213//
214// Calculate extent under transform and specified limit
215
216G4bool
217G4UGenericPolycone::CalculateExtent(const EAxis pAxis,
218 const G4VoxelLimits& pVoxelLimit,
219 const G4AffineTransform& pTransform,
220 G4double& pMin, G4double& pMax) const
221{
222 G4ThreeVector bmin, bmax;
223 G4bool exist;
224
225 // Check bounding box (bbox)
226 //
227 BoundingLimits(bmin,bmax);
228 G4BoundingEnvelope bbox(bmin,bmax);
229#ifdef G4BBOX_EXTENT
230 return bbox.CalculateExtent(pAxis,pVoxelLimit,pTransform,pMin,pMax);
231#endif
232 if (bbox.BoundingBoxVsVoxelLimits(pAxis,pVoxelLimit,pTransform,pMin,pMax))
233 {
234 return exist = pMin < pMax;
235 }
236
237 // To find the extent, RZ contour of the polycone is subdivided
238 // in triangles. The extent is calculated as cumulative extent of
239 // all sub-polycones formed by rotation of triangles around Z
240 //
241 G4TwoVectorList contourRZ;
242 G4TwoVectorList triangles;
243 G4double eminlim = pVoxelLimit.GetMinExtent(pAxis);
244 G4double emaxlim = pVoxelLimit.GetMaxExtent(pAxis);
245
246 // get RZ contour, ensure anticlockwise order of corners
247 for (G4int i=0; i<GetNumRZCorner(); ++i)
248 {
249 G4PolyconeSideRZ corner = GetCorner(i);
250 contourRZ.emplace_back(corner.r,corner.z);
251 }
252 G4double area = G4GeomTools::PolygonArea(contourRZ);
253 if (area < 0.) std::reverse(contourRZ.begin(),contourRZ.end());
254
255 // triangulate RZ countour
256 if (!G4GeomTools::TriangulatePolygon(contourRZ,triangles))
257 {
258 std::ostringstream message;
259 message << "Triangulation of RZ contour has failed for solid: "
260 << GetName() << " !"
261 << "\nExtent has been calculated using boundary box";
262 G4Exception("G4UGenericPolycone::CalculateExtent()",
263 "GeomMgt1002", JustWarning, message);
264 return bbox.CalculateExtent(pAxis,pVoxelLimit,pTransform,pMin,pMax);
265 }
266
267 // set trigonometric values
268 const G4int NSTEPS = 24; // number of steps for whole circle
269 G4double astep = twopi/NSTEPS; // max angle for one step
270
271 G4double sphi = GetStartPhi();
272 G4double ephi = GetEndPhi();
273 G4double dphi = IsOpen() ? ephi-sphi : twopi;
274 G4int ksteps = (dphi <= astep) ? 1 : (G4int)((dphi-deg)/astep) + 1;
275 G4double ang = dphi/ksteps;
276
277 G4double sinHalf = std::sin(0.5*ang);
278 G4double cosHalf = std::cos(0.5*ang);
279 G4double sinStep = 2.*sinHalf*cosHalf;
280 G4double cosStep = 1. - 2.*sinHalf*sinHalf;
281
282 G4double sinStart = GetSinStartPhi();
283 G4double cosStart = GetCosStartPhi();
284 G4double sinEnd = GetSinEndPhi();
285 G4double cosEnd = GetCosEndPhi();
286
287 // define vectors and arrays
288 std::vector<const G4ThreeVectorList *> polygons;
289 polygons.resize(ksteps+2);
290 G4ThreeVectorList pols[NSTEPS+2];
291 for (G4int k=0; k<ksteps+2; ++k) pols[k].resize(6);
292 for (G4int k=0; k<ksteps+2; ++k) polygons[k] = &pols[k];
293 G4double r0[6],z0[6]; // contour with original edges of triangle
294 G4double r1[6]; // shifted radii of external edges of triangle
295
296 // main loop along triangles
297 pMin = kInfinity;
298 pMax =-kInfinity;
299 G4int ntria = triangles.size()/3;
300 for (G4int i=0; i<ntria; ++i)
301 {
302 G4int i3 = i*3;
303 for (G4int k=0; k<3; ++k)
304 {
305 G4int e0 = i3+k, e1 = (k<2) ? e0+1 : i3;
306 G4int k2 = k*2;
307 // set contour with original edges of triangle
308 r0[k2+0] = triangles[e0].x(); z0[k2+0] = triangles[e0].y();
309 r0[k2+1] = triangles[e1].x(); z0[k2+1] = triangles[e1].y();
310 // set shifted radii
311 r1[k2+0] = r0[k2+0];
312 r1[k2+1] = r0[k2+1];
313 if (z0[k2+1] - z0[k2+0] <= 0) continue;
314 r1[k2+0] /= cosHalf;
315 r1[k2+1] /= cosHalf;
316 }
317
318 // rotate countour, set sequence of 6-sided polygons
319 G4double sinCur = sinStart*cosHalf + cosStart*sinHalf;
320 G4double cosCur = cosStart*cosHalf - sinStart*sinHalf;
321 for (G4int j=0; j<6; ++j)
322 {
323 pols[0][j].set(r0[j]*cosStart,r0[j]*sinStart,z0[j]);
324 }
325 for (G4int k=1; k<ksteps+1; ++k)
326 {
327 for (G4int j=0; j<6; ++j)
328 {
329 pols[k][j].set(r1[j]*cosCur,r1[j]*sinCur,z0[j]);
330 }
331 G4double sinTmp = sinCur;
332 sinCur = sinCur*cosStep + cosCur*sinStep;
333 cosCur = cosCur*cosStep - sinTmp*sinStep;
334 }
335 for (G4int j=0; j<6; ++j)
336 {
337 pols[ksteps+1][j].set(r0[j]*cosEnd,r0[j]*sinEnd,z0[j]);
338 }
339
340 // set sub-envelope and adjust extent
341 G4double emin,emax;
342 G4BoundingEnvelope benv(polygons);
343 if (!benv.CalculateExtent(pAxis,pVoxelLimit,pTransform,emin,emax)) continue;
344 if (emin < pMin) pMin = emin;
345 if (emax > pMax) pMax = emax;
346 if (eminlim > pMin && emaxlim < pMax) return true; // max possible extent
347 }
348 return (pMin < pMax);
349}
350
351////////////////////////////////////////////////////////////////////////
352//
353// CreatePolyhedron
354
355G4Polyhedron* G4UGenericPolycone::CreatePolyhedron() const
356{
357 return new G4PolyhedronPcon(wrStart, wrDelta, rzcorners);
358}
359
360#endif // G4GEOM_USE_USOLIDS
const G4double kCarTolerance
std::vector< G4ThreeVector > G4ThreeVectorList
@ JustWarning
void G4Exception(const char *originOfException, const char *exceptionCode, G4ExceptionSeverity severity, const char *description)
std::vector< G4TwoVector > G4TwoVectorList
CLHEP::Hep3Vector G4ThreeVector
CLHEP::Hep2Vector G4TwoVector
double G4double
Definition G4Types.hh:83
bool G4bool
Definition G4Types.hh:86
int G4int
Definition G4Types.hh:85
G4GLOB_DLL std::ostream G4cout
double x() const
double y() const
double z() const
double x() const
double y() const
void set(double x, double y, double z)
G4AffineTransform is a class for geometric affine transformations. It supports efficient arbitrary ro...
G4BoundingEnvelope is a helper class to facilitate calculation of the extent of a solid within the li...
static G4bool DiskExtent(G4double rmin, G4double rmax, G4double startPhi, G4double delPhi, G4TwoVector &pmin, G4TwoVector &pmax)
static void RemoveRedundantVertices(G4TwoVectorList &polygon, std::vector< G4int > &iout, G4double tolerance=0.0)
static G4double PolygonArea(const G4TwoVectorList &polygon)
static G4bool TriangulatePolygon(const G4TwoVectorList &polygon, std::vector< G4int > &result)
G4VSolid is an abstract base class for solids, physical shapes that can be tracked through....
Definition G4VSolid.hh:80
G4VoxelLimits represents limitation/restrictions of space, where restrictions are only made perpendic...
G4double GetMinExtent(const EAxis pAxis) const
G4double GetMaxExtent(const EAxis pAxis) const
EAxis
Definition geomdefs.hh:54
const char * name(G4int ptype)
#define DBL_EPSILON
Definition templates.hh:66