Geant4 11.4.0
Toolkit for the simulation of the passage of particles through matter
Loading...
Searching...
No Matches
G4UPolycone.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 G4UPolycone wrapper class
27//
28// 31.10.13 G.Cosmo, CERN
29// --------------------------------------------------------------------
30
31#include "G4Polycone.hh"
32#include "G4UPolycone.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
41using namespace CLHEP;
42
43////////////////////////////////////////////////////////////////////////
44//
45// Constructor (GEANT3 style parameters)
46//
47G4UPolycone::G4UPolycone( const G4String& name,
48 G4double phiStart,
49 G4double phiTotal,
50 G4int numZPlanes,
51 const G4double zPlane[],
52 const G4double rInner[],
53 const G4double rOuter[] )
54 : Base_t(name, phiStart, phiTotal, numZPlanes, zPlane, rInner, rOuter)
55{
56 fGenericPcon = false;
57 SetOriginalParameters();
58 wrStart = phiStart;
59 while (wrStart < 0)
60 {
61 wrStart += twopi;
62 }
63 wrDelta = phiTotal;
64 if (wrDelta <= 0 || wrDelta >= twopi*(1-DBL_EPSILON))
65 {
66 wrStart = 0;
67 wrDelta = twopi;
68 }
69 rzcorners.resize(0);
70 for (G4int i=0; i<numZPlanes; ++i)
71 {
72 G4double z = zPlane[i];
73 G4double r = rOuter[i];
74 rzcorners.emplace_back(r,z);
75 }
76 for (G4int i=numZPlanes-1; i>=0; --i)
77 {
78 G4double z = zPlane[i];
79 G4double r = rInner[i];
80 rzcorners.emplace_back(r,z);
81 }
82 std::vector<G4int> iout;
84}
85
86
87////////////////////////////////////////////////////////////////////////
88//
89// Constructor (generic parameters)
90//
91G4UPolycone::G4UPolycone(const G4String& name,
92 G4double phiStart,
93 G4double phiTotal,
94 G4int numRZ,
95 const G4double r[],
96 const G4double z[] )
97 : Base_t(name, phiStart, phiTotal, numRZ, r, z)
98{
99 fGenericPcon = true;
100 SetOriginalParameters();
101 wrStart = phiStart; while (wrStart < 0) wrStart += twopi;
102 wrDelta = phiTotal;
103 if (wrDelta <= 0 || wrDelta >= twopi*(1-DBL_EPSILON))
104 {
105 wrStart = 0;
106 wrDelta = twopi;
107 }
108 rzcorners.resize(0);
109 for (G4int i=0; i<numRZ; ++i)
110 {
111 rzcorners.emplace_back(r[i],z[i]);
112 }
113 std::vector<G4int> iout;
115}
116
117
118////////////////////////////////////////////////////////////////////////
119//
120// Copy constructor
121//
122G4UPolycone::G4UPolycone( const G4UPolycone& source )
123 : Base_t( source )
124{
125 fGenericPcon = source.fGenericPcon;
126 fOriginalParameters = source.fOriginalParameters;
127 wrStart = source.wrStart;
128 wrDelta = source.wrDelta;
129 rzcorners = source.rzcorners;
130}
131
132
133////////////////////////////////////////////////////////////////////////
134//
135// Assignment operator
136//
137G4UPolycone& G4UPolycone::operator=( const G4UPolycone& source )
138{
139 if (this == &source) return *this;
140
141 Base_t::operator=( source );
142 fGenericPcon = source.fGenericPcon;
143 fOriginalParameters = source.fOriginalParameters;
144 wrStart = source.wrStart;
145 wrDelta = source.wrDelta;
146 rzcorners = source.rzcorners;
147
148 return *this;
149}
150
151
152////////////////////////////////////////////////////////////////////////
153//
154// Accessors & modifiers
155//
156G4double G4UPolycone::GetStartPhi() const
157{
158 return wrStart;
159}
160G4double G4UPolycone::GetDeltaPhi() const
161{
162 return wrDelta;
163}
164G4double G4UPolycone::GetEndPhi() const
165{
166 return (wrStart + wrDelta);
167}
168G4double G4UPolycone::GetSinStartPhi() const
169{
170 if (!IsOpen()) return 0.;
171 G4double phi = GetStartPhi();
172 return std::sin(phi);
173}
174G4double G4UPolycone::GetCosStartPhi() const
175{
176 if (!IsOpen()) return 1.;
177 G4double phi = GetStartPhi();
178 return std::cos(phi);
179}
180G4double G4UPolycone::GetSinEndPhi() const
181{
182 if (!IsOpen()) return 0.;
183 G4double phi = GetEndPhi();
184 return std::sin(phi);
185}
186G4double G4UPolycone::GetCosEndPhi() const
187{
188 if (!IsOpen()) return 1.;
189 G4double phi = GetEndPhi();
190 return std::cos(phi);
191}
192G4bool G4UPolycone::IsOpen() const
193{
194 return (wrDelta < twopi);
195}
196G4int G4UPolycone::GetNumRZCorner() const
197{
198 return rzcorners.size();
199}
200G4PolyconeSideRZ G4UPolycone::GetCorner(G4int index) const
201{
202 G4TwoVector rz = rzcorners.at(index);
203 G4PolyconeSideRZ psiderz = { rz.x(), rz.y() };
204
205 return psiderz;
206}
207G4PolyconeHistorical* G4UPolycone::GetOriginalParameters() const
208{
209 return new G4PolyconeHistorical(fOriginalParameters);
210}
211void G4UPolycone::SetOriginalParameters()
212{
213 vecgeom::PolyconeHistorical* original_parameters = Base_t::GetOriginalParameters();
214
215 fOriginalParameters.Start_angle = original_parameters->fHStart_angle;
216 fOriginalParameters.Opening_angle = original_parameters->fHOpening_angle;
217 fOriginalParameters.Num_z_planes = original_parameters->fHNum_z_planes;
218
219 delete [] fOriginalParameters.Z_values;
220 delete [] fOriginalParameters.Rmin;
221 delete [] fOriginalParameters.Rmax;
222
223 G4int numPlanes = fOriginalParameters.Num_z_planes;
224 fOriginalParameters.Z_values = new G4double[numPlanes];
225 fOriginalParameters.Rmin = new G4double[numPlanes];
226 fOriginalParameters.Rmax = new G4double[numPlanes];
227 for (G4int i=0; i<numPlanes; ++i)
228 {
229 fOriginalParameters.Z_values[i] = original_parameters->fHZ_values[i];
230 fOriginalParameters.Rmin[i] = original_parameters->fHRmin[i];
231 fOriginalParameters.Rmax[i] = original_parameters->fHRmax[i];
232 }
233}
234void G4UPolycone::SetOriginalParameters(G4PolyconeHistorical* pars)
235{
236 fOriginalParameters = *pars;
237 fRebuildPolyhedron = true;
238 Reset();
239}
240
241G4bool G4UPolycone::Reset()
242{
243 if (fGenericPcon)
244 {
245 std::ostringstream message;
246 message << "Solid " << GetName() << " built using generic construct."
247 << G4endl << "Not applicable to the generic construct !";
248 G4Exception("G4UPolycone::Reset()", "GeomSolids1001",
249 JustWarning, message, "Parameters NOT reset.");
250 return true; // error code set
251 }
252
253 //
254 // Rebuild polycone based on original parameters
255 //
256 wrStart = fOriginalParameters.Start_angle;
257 while (wrStart < 0.)
258 {
259 wrStart += twopi;
260 }
261 wrDelta = fOriginalParameters.Opening_angle;
262 if (wrDelta <= 0. || wrDelta >= twopi*(1-DBL_EPSILON))
263 {
264 wrStart = 0.;
265 wrDelta = twopi;
266 }
267 rzcorners.resize(0);
268 for (G4int i=0; i<fOriginalParameters.Num_z_planes; ++i)
269 {
270 G4double z = fOriginalParameters.Z_values[i];
271 G4double r = fOriginalParameters.Rmax[i];
272 rzcorners.emplace_back(r,z);
273 }
274 for (G4int i=fOriginalParameters.Num_z_planes-1; i>=0; --i)
275 {
276 G4double z = fOriginalParameters.Z_values[i];
277 G4double r = fOriginalParameters.Rmin[i];
278 rzcorners.emplace_back(r,z);
279 }
280 std::vector<G4int> iout;
282
283 return false; // error code unset
284}
285
286////////////////////////////////////////////////////////////////////////
287//
288// Dispatch to parameterisation for replication mechanism dimension
289// computation & modification.
290//
291void G4UPolycone::ComputeDimensions(G4VPVParameterisation* p,
292 const G4int n,
293 const G4VPhysicalVolume* pRep)
294{
295 p->ComputeDimensions(*(G4Polycone*)this,n,pRep);
296}
297
298
299//////////////////////////////////////////////////////////////////////////
300//
301// Make a clone of the object
302
303G4VSolid* G4UPolycone::Clone() const
304{
305 return new G4UPolycone(*this);
306}
307
308//////////////////////////////////////////////////////////////////////////
309//
310// Get bounding box
311
312void G4UPolycone::BoundingLimits(G4ThreeVector& pMin,
313 G4ThreeVector& pMax) const
314{
315 static G4bool checkBBox = true;
316 static G4bool checkPhi = true;
317
318 G4double rmin = kInfinity, rmax = -kInfinity;
319 G4double zmin = kInfinity, zmax = -kInfinity;
320
321 for (G4int i=0; i<GetNumRZCorner(); ++i)
322 {
323 G4PolyconeSideRZ corner = GetCorner(i);
324 if (corner.r < rmin) rmin = corner.r;
325 if (corner.r > rmax) rmax = corner.r;
326 if (corner.z < zmin) zmin = corner.z;
327 if (corner.z > zmax) zmax = corner.z;
328 }
329
330 if (IsOpen())
331 {
332 G4TwoVector vmin,vmax;
333 G4GeomTools::DiskExtent(rmin,rmax,
334 GetSinStartPhi(),GetCosStartPhi(),
335 GetSinEndPhi(),GetCosEndPhi(),
336 vmin,vmax);
337 pMin.set(vmin.x(),vmin.y(),zmin);
338 pMax.set(vmax.x(),vmax.y(),zmax);
339 }
340 else
341 {
342 pMin.set(-rmax,-rmax, zmin);
343 pMax.set( rmax, rmax, zmax);
344 }
345
346 // Check correctness of the bounding box
347 //
348 if (pMin.x() >= pMax.x() || pMin.y() >= pMax.y() || pMin.z() >= pMax.z())
349 {
350 std::ostringstream message;
351 message << "Bad bounding box (min >= max) for solid: "
352 << GetName() << " !"
353 << "\npMin = " << pMin
354 << "\npMax = " << pMax;
355 G4Exception("G4UPolycone::BoundingLimits()", "GeomMgt0001",
356 JustWarning, message);
357 StreamInfo(G4cout);
358 }
359
360 // Check consistency of bounding boxes
361 //
362 if (checkBBox)
363 {
364 U3Vector vmin, vmax;
365 Extent(vmin,vmax);
366 if (std::abs(pMin.x()-vmin.x()) > kCarTolerance ||
367 std::abs(pMin.y()-vmin.y()) > kCarTolerance ||
368 std::abs(pMin.z()-vmin.z()) > kCarTolerance ||
369 std::abs(pMax.x()-vmax.x()) > kCarTolerance ||
370 std::abs(pMax.y()-vmax.y()) > kCarTolerance ||
371 std::abs(pMax.z()-vmax.z()) > kCarTolerance)
372 {
373 std::ostringstream message;
374 message << "Inconsistency in bounding boxes for solid: "
375 << GetName() << " !"
376 << "\nBBox min: wrapper = " << pMin << " solid = " << vmin
377 << "\nBBox max: wrapper = " << pMax << " solid = " << vmax;
378 G4Exception("G4UPolycone::BoundingLimits()", "GeomMgt0001",
379 JustWarning, message);
380 checkBBox = false;
381 }
382 }
383
384 // Check consistency of angles
385 //
386 if (checkPhi)
387 {
388 if (GetStartPhi() != Base_t::GetStartPhi() ||
389 GetEndPhi() != Base_t::GetEndPhi() ||
390 IsOpen() != (Base_t::GetDeltaPhi() < twopi))
391 {
392 std::ostringstream message;
393 message << "Inconsistency in Phi angles or # of sides for solid: "
394 << GetName() << " !"
395 << "\nPhi start : wrapper = " << GetStartPhi()
396 << " solid = " << Base_t::GetStartPhi()
397 << "\nPhi end : wrapper = " << GetEndPhi()
398 << " solid = " << Base_t::GetEndPhi()
399 << "\nPhi is open: wrapper = " << (IsOpen() ? "true" : "false")
400 << " solid = "
401 << ((Base_t::GetDeltaPhi() < twopi) ? "true" : "false");
402 G4Exception("G4UPolycone::BoundingLimits()", "GeomMgt0001",
403 JustWarning, message);
404 checkPhi = false;
405 }
406 }
407}
408
409//////////////////////////////////////////////////////////////////////////
410//
411// Calculate extent under transform and specified limit
412
413G4bool G4UPolycone::CalculateExtent(const EAxis pAxis,
414 const G4VoxelLimits& pVoxelLimit,
415 const G4AffineTransform& pTransform,
416 G4double& pMin, G4double& pMax) const
417{
418 G4ThreeVector bmin, bmax;
419 G4bool exist;
420
421 // Check bounding box (bbox)
422 //
423 BoundingLimits(bmin,bmax);
424 G4BoundingEnvelope bbox(bmin,bmax);
425#ifdef G4BBOX_EXTENT
426 return bbox.CalculateExtent(pAxis,pVoxelLimit,pTransform,pMin,pMax);
427#endif
428 if (bbox.BoundingBoxVsVoxelLimits(pAxis,pVoxelLimit,pTransform,pMin,pMax))
429 {
430 return exist = pMin < pMax;
431 }
432
433 // To find the extent, RZ contour of the polycone is subdivided
434 // in triangles. The extent is calculated as cumulative extent of
435 // all sub-polycones formed by rotation of triangles around Z
436 //
437 G4TwoVectorList contourRZ;
438 G4TwoVectorList triangles;
439 std::vector<G4int> iout;
440 G4double eminlim = pVoxelLimit.GetMinExtent(pAxis);
441 G4double emaxlim = pVoxelLimit.GetMaxExtent(pAxis);
442
443 // get RZ contour, ensure anticlockwise order of corners
444 for (G4int i=0; i<GetNumRZCorner(); ++i)
445 {
446 G4PolyconeSideRZ corner = GetCorner(i);
447 contourRZ.emplace_back(corner.r,corner.z);
448 }
450 G4double area = G4GeomTools::PolygonArea(contourRZ);
451 if (area < 0.) std::reverse(contourRZ.begin(),contourRZ.end());
452
453 // triangulate RZ countour
454 if (!G4GeomTools::TriangulatePolygon(contourRZ,triangles))
455 {
456 std::ostringstream message;
457 message << "Triangulation of RZ contour has failed for solid: "
458 << GetName() << " !"
459 << "\nExtent has been calculated using boundary box";
460 G4Exception("G4UPolycone::CalculateExtent()",
461 "GeomMgt1002", JustWarning, message);
462 return bbox.CalculateExtent(pAxis,pVoxelLimit,pTransform,pMin,pMax);
463 }
464
465 // set trigonometric values
466 const G4int NSTEPS = 24; // number of steps for whole circle
467 G4double astep = twopi/NSTEPS; // max angle for one step
468
469 G4double sphi = GetStartPhi();
470 G4double ephi = GetEndPhi();
471 G4double dphi = IsOpen() ? ephi-sphi : twopi;
472 G4int ksteps = (dphi <= astep) ? 1 : (G4int)((dphi-deg)/astep) + 1;
473 G4double ang = dphi/ksteps;
474
475 G4double sinHalf = std::sin(0.5*ang);
476 G4double cosHalf = std::cos(0.5*ang);
477 G4double sinStep = 2.*sinHalf*cosHalf;
478 G4double cosStep = 1. - 2.*sinHalf*sinHalf;
479
480 G4double sinStart = GetSinStartPhi();
481 G4double cosStart = GetCosStartPhi();
482 G4double sinEnd = GetSinEndPhi();
483 G4double cosEnd = GetCosEndPhi();
484
485 // define vectors and arrays
486 std::vector<const G4ThreeVectorList *> polygons;
487 polygons.resize(ksteps+2);
488 G4ThreeVectorList pols[NSTEPS+2];
489 for (G4int k=0; k<ksteps+2; ++k) pols[k].resize(6);
490 for (G4int k=0; k<ksteps+2; ++k) polygons[k] = &pols[k];
491 G4double r0[6],z0[6]; // contour with original edges of triangle
492 G4double r1[6]; // shifted radii of external edges of triangle
493
494 // main loop along triangles
495 pMin = kInfinity;
496 pMax =-kInfinity;
497 G4int ntria = triangles.size()/3;
498 for (G4int i=0; i<ntria; ++i)
499 {
500 G4int i3 = i*3;
501 for (G4int k=0; k<3; ++k)
502 {
503 G4int e0 = i3+k, e1 = (k<2) ? e0+1 : i3;
504 G4int k2 = k*2;
505 // set contour with original edges of triangle
506 r0[k2+0] = triangles[e0].x(); z0[k2+0] = triangles[e0].y();
507 r0[k2+1] = triangles[e1].x(); z0[k2+1] = triangles[e1].y();
508 // set shifted radii
509 r1[k2+0] = r0[k2+0];
510 r1[k2+1] = r0[k2+1];
511 if (z0[k2+1] - z0[k2+0] <= 0) continue;
512 r1[k2+0] /= cosHalf;
513 r1[k2+1] /= cosHalf;
514 }
515
516 // rotate countour, set sequence of 6-sided polygons
517 G4double sinCur = sinStart*cosHalf + cosStart*sinHalf;
518 G4double cosCur = cosStart*cosHalf - sinStart*sinHalf;
519 for (G4int j=0; j<6; ++j) pols[0][j].set(r0[j]*cosStart,r0[j]*sinStart,z0[j]);
520 for (G4int k=1; k<ksteps+1; ++k)
521 {
522 for (G4int j=0; j<6; ++j) pols[k][j].set(r1[j]*cosCur,r1[j]*sinCur,z0[j]);
523 G4double sinTmp = sinCur;
524 sinCur = sinCur*cosStep + cosCur*sinStep;
525 cosCur = cosCur*cosStep - sinTmp*sinStep;
526 }
527 for (G4int j=0; j<6; ++j) pols[ksteps+1][j].set(r0[j]*cosEnd,r0[j]*sinEnd,z0[j]);
528
529 // set sub-envelope and adjust extent
530 G4double emin,emax;
531 G4BoundingEnvelope benv(polygons);
532 if (!benv.CalculateExtent(pAxis,pVoxelLimit,pTransform,emin,emax)) continue;
533 if (emin < pMin) pMin = emin;
534 if (emax > pMax) pMax = emax;
535 if (eminlim > pMin && emaxlim < pMax) return true; // max possible extent
536 }
537 return (pMin < pMax);
538}
539
540////////////////////////////////////////////////////////////////////////
541//
542// CreatePolyhedron
543//
544G4Polyhedron* G4UPolycone::CreatePolyhedron() const
545{
546 return new G4PolyhedronPcon(wrStart, wrDelta, rzcorners);
547}
548
549#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
#define G4endl
Definition G4ios.hh:67
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)
G4PolyconeHistorical is a data structure for use in G4Polycone.
G4Polycone represents a composed closed shape (PCON) made of cones and cylinders, along the Z axis wi...
Definition G4Polycone.hh:82
G4VPVParameterisation ia an abstract base class for Parameterisation, able to compute the transformat...
virtual void ComputeDimensions(G4Box &, const G4int, const G4VPhysicalVolume *) const
G4VPhysicalVolume is an abstract base class for the representation of a positioned volume....
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