Geant4 11.4.0
Toolkit for the simulation of the passage of particles through matter
Loading...
Searching...
No Matches
G4UTrap.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 for G4UTrap wrapper class
27//
28// 13.09.13 G.Cosmo, CERN
29// --------------------------------------------------------------------
30
31#include "G4Trap.hh"
32#include "G4UTrap.hh"
33
34#if ( defined(G4GEOM_USE_USOLIDS) || defined(G4GEOM_USE_PARTIAL_USOLIDS) )
35
36#include "G4AffineTransform.hh"
38#include "G4BoundingEnvelope.hh"
39
40using namespace CLHEP;
41
42/////////////////////////////////////////////////////////////////////////
43//
44// Constructors
45//
46G4UTrap::G4UTrap( const G4String& pName,
47 G4double pdz,
48 G4double pTheta, G4double pPhi,
49 G4double pdy1, G4double pdx1, G4double pdx2,
50 G4double pAlp1,
51 G4double pdy2, G4double pdx3, G4double pdx4,
52 G4double pAlp2 )
53 : Base_t(pName, pdz, pTheta, pPhi, pdy1, pdx1, pdx2,
54 pAlp1, pdy2, pdx3, pdx4, pAlp2)
55{
56 G4ThreeVector pt[8];
57 CheckParameters();
58 GetVertices(pt);
59 CheckPlanarity(pt);
60}
61
62G4UTrap::G4UTrap( const G4String& pName,
63 const G4ThreeVector pt[8] )
64 : Base_t(pName)
65{
66 // Start with check of centering - the center of gravity trap line
67 // should cross the origin of frame
68 if ( pt[0].z() >= 0
69 || pt[0].z() != pt[1].z()
70 || pt[0].z() != pt[2].z()
71 || pt[0].z() != pt[3].z()
72
73 || pt[4].z() <= 0
74 || pt[4].z() != pt[5].z()
75 || pt[4].z() != pt[6].z()
76 || pt[4].z() != pt[7].z()
77
78 || std::abs( pt[0].z() + pt[4].z() ) >= kCarTolerance
79
80 || pt[0].y() != pt[1].y()
81 || pt[2].y() != pt[3].y()
82 || pt[4].y() != pt[5].y()
83 || pt[6].y() != pt[7].y()
84
85 || std::abs(pt[0].y()+pt[2].y()+pt[4].y()+pt[6].y()) >= kCarTolerance
86 || std::abs(pt[0].x()+pt[1].x()+pt[4].x()+pt[5].x() +
87 pt[2].x()+pt[3].x()+pt[6].x()+pt[7].x()) >= kCarTolerance )
88 {
89 std::ostringstream message;
90 message << "Invalid vertice coordinates for Solid: " << GetName();
91 G4Exception("G4UTrap::G4UTrap()", "GeomSolids0002",
92 FatalException, message);
93 }
94
95 SetPlanes(pt);
96 CheckParameters();
97 CheckPlanarity(pt);
98}
99
100// Constructor for Right Angular Wedge from STEP; this is different from
101// the UnplacedTrapezoid constructor taking four double's and constructing
102// a Trd1.
103G4UTrap::G4UTrap( const G4String& pName,
104 G4double pZ,
105 G4double pY,
106 G4double pX, G4double pLTX )
107 : Base_t(pName, 0.5*pZ, /*theta=*/0, /*phi=*/0, /*dy1=*/0.5*pY,
108 /*dx1=*/0.5*pX, /*dx2=*/0.5*pLTX, /*Alpha1=*/0.5*(pLTX - pX)/pY,
109 /*dy2=*/0.5*pY, /*dx3=*/0.5*pX, /*dx4=*/0.5*pLTX,
110 /*Alpha2=*/0.5*(pLTX - pX)/pY)
111{
112 CheckParameters();
113}
114
115G4UTrap::G4UTrap( const G4String& pName,
116 G4double pdx1, G4double pdx2,
117 G4double pdy1, G4double pdy2,
118 G4double pdz )
119 : Base_t(pName, pdx1, pdx2, pdy1, pdy2, pdz)
120{
121 CheckParameters();
122}
123
124G4UTrap::G4UTrap(const G4String& pName,
125 G4double pdx, G4double pdy, G4double pdz,
126 G4double pAlpha, G4double pTheta, G4double pPhi )
127 : Base_t(pName, pdx, pdy, pdz, pAlpha, pTheta, pPhi)
128{
129 CheckParameters();
130}
131
132G4UTrap::G4UTrap( const G4String& pName )
133 : Base_t(pName)
134{
135}
136
137//////////////////////////////////////////////////////////////////////////
138//
139// Copy constructor
140//
141G4UTrap::G4UTrap(const G4UTrap& rhs)
142 : Base_t(rhs)
143{
144}
145
146//////////////////////////////////////////////////////////////////////////
147//
148// Assignment operator
149//
150G4UTrap& G4UTrap::operator = (const G4UTrap& rhs)
151{
152 // Check assignment to self
153 //
154 if (this == &rhs) { return *this; }
155
156 // Copy base class data
157 //
158 Base_t::operator=(rhs);
159
160 return *this;
161}
162
163//////////////////////////////////////////////////////////////////////////
164//
165// Accessors
166//
167G4double G4UTrap::GetZHalfLength() const
168{
169 return GetDz();
170}
171G4double G4UTrap::GetYHalfLength1() const
172{
173 return GetDy1();
174}
175G4double G4UTrap::GetXHalfLength1() const
176{
177 return GetDx1();
178}
179G4double G4UTrap::GetXHalfLength2() const
180{
181 return GetDx2();
182}
183G4double G4UTrap::GetTanAlpha1() const
184{
185 return Base_t::GetTanAlpha1();
186}
187G4double G4UTrap::GetYHalfLength2() const
188{
189 return GetDy2();
190}
191G4double G4UTrap::GetXHalfLength3() const
192{
193 return GetDx3();
194}
195G4double G4UTrap::GetXHalfLength4() const
196{
197 return GetDx4();
198}
199G4double G4UTrap::GetTanAlpha2() const
200{
201 return Base_t::GetTanAlpha2();
202}
203G4double G4UTrap::GetPhi() const
204{
205 return Base_t::GetPhi();
206}
207G4double G4UTrap::GetTheta() const
208{
209 return Base_t::GetTheta();
210}
211G4double G4UTrap::GetAlpha1() const
212{
213 return Base_t::GetAlpha1();
214}
215G4double G4UTrap::GetAlpha2() const
216{
217 return Base_t::GetAlpha2();
218}
219TrapSidePlane G4UTrap::GetSidePlane(G4int n) const
220{
221 TrapSidePlane plane;
222 plane.a = GetStruct().GetPlane(n).fA;
223 plane.b = GetStruct().GetPlane(n).fB;
224 plane.c = GetStruct().GetPlane(n).fC;
225 plane.d = GetStruct().GetPlane(n).fD;
226 return plane;
227}
228G4ThreeVector G4UTrap::GetSymAxis() const
229{
230 G4double tanThetaSphi = GetTanThetaSinPhi();
231 G4double tanThetaCphi = GetTanThetaCosPhi();
232 G4double tan2Theta = tanThetaSphi*tanThetaSphi + tanThetaCphi*tanThetaCphi;
233 G4double cosTheta = 1.0 / std::sqrt(1 + tan2Theta);
234 return {tanThetaCphi*cosTheta, tanThetaSphi*cosTheta, cosTheta};
235}
236
237//////////////////////////////////////////////////////////////////////////
238//
239// Modifier
240//
241void G4UTrap::SetAllParameters(G4double pDz, G4double pTheta, G4double pPhi,
242 G4double pDy1, G4double pDx1, G4double pDx2,
243 G4double pAlp1,
244 G4double pDy2, G4double pDx3, G4double pDx4,
245 G4double pAlp2)
246{
247 SetDz(pDz);
248 SetDy1(pDy1);
249 SetDy2(pDy2);
250 SetDx1(pDx1);
251 SetDx2(pDx2);
252 SetDx3(pDx3);
253 SetDx4(pDx4);
254 SetTanAlpha1(std::tan(pAlp1));
255 SetTanAlpha2(std::tan(pAlp2));
256 // last two will also reset cached variables
257 SetTheta(pTheta);
258 SetPhi(pPhi);
259 fRebuildPolyhedron = true;
260
261 G4ThreeVector pt[8];
262 CheckParameters();
263 GetVertices(pt);
264 CheckPlanarity(pt);
265}
266
267/////////////////////////////////////////////////////////////////////////
268//
269// Set parameters using eight vertices
270//
271void G4UTrap::SetPlanes(const G4ThreeVector pt[8])
272{
273 U3Vector upt[8];
274 for (unsigned int i=0; i<8; ++i)
275 {
276 upt[i] = U3Vector(pt[i].x(), pt[i].y(), pt[i].z());
277 }
278 fromCornersToParameters(upt);
279 fRebuildPolyhedron = true;
280}
281
282/////////////////////////////////////////////////////////////////////////
283//
284// Check dimensions
285//
286void G4UTrap::CheckParameters() const
287{
288 G4double fDz = GetZHalfLength();
289 G4double fDy1 = GetYHalfLength1();
290 G4double fDx1 = GetXHalfLength1();
291 G4double fDx2 = GetXHalfLength2();
292 G4double fDy2 = GetYHalfLength2();
293 G4double fDx3 = GetXHalfLength3();
294 G4double fDx4 = GetXHalfLength4();
295
296 if (fDz<=0 ||
297 fDy1<=0 || fDx1<=0 || fDx2<=0 ||
298 fDy2<=0 || fDx3<=0 || fDx4<=0)
299 {
300 std::ostringstream message;
301 message << "Invalid Length Parameters for Solid: " << GetName()
302 << "\n X - " <<fDx1<<", "<<fDx2<<", "<<fDx3<<", "<<fDx4
303 << "\n Y - " <<fDy1<<", "<<fDy2
304 << "\n Z - " <<fDz;
305 G4Exception("G4UTrap::CheckParameters()", "GeomSolids0002",
306 FatalException, message);
307 }
308}
309
310/////////////////////////////////////////////////////////////////////////
311//
312// Compute coordinates of vertices
313//
314void G4UTrap::GetVertices(G4ThreeVector pt[8]) const
315{
316 G4double fDz = GetZHalfLength();
317 G4double fDy1 = GetYHalfLength1();
318 G4double fDx1 = GetXHalfLength1();
319 G4double fDx2 = GetXHalfLength2();
320 G4double fDy2 = GetYHalfLength2();
321 G4double fDx3 = GetXHalfLength3();
322 G4double fDx4 = GetXHalfLength4();
323 G4double fTalpha1 = GetTanAlpha1();
324 G4double fTalpha2 = GetTanAlpha2();
325
326 G4double DzTthetaCphi = fDz*GetTanThetaCosPhi();
327 G4double DzTthetaSphi = fDz*GetTanThetaSinPhi();
328 G4double Dy1Talpha1 = fDy1*fTalpha1;
329 G4double Dy2Talpha2 = fDy2*fTalpha2;
330
331 pt[0].set(-DzTthetaCphi-Dy1Talpha1-fDx1,-DzTthetaSphi-fDy1,-fDz);
332 pt[1].set(-DzTthetaCphi-Dy1Talpha1+fDx1,-DzTthetaSphi-fDy1,-fDz);
333 pt[2].set(-DzTthetaCphi+Dy1Talpha1-fDx2,-DzTthetaSphi+fDy1,-fDz);
334 pt[3].set(-DzTthetaCphi+Dy1Talpha1+fDx2,-DzTthetaSphi+fDy1,-fDz);
335 pt[4].set( DzTthetaCphi-Dy2Talpha2-fDx3, DzTthetaSphi-fDy2, fDz);
336 pt[5].set( DzTthetaCphi-Dy2Talpha2+fDx3, DzTthetaSphi-fDy2, fDz);
337 pt[6].set( DzTthetaCphi+Dy2Talpha2-fDx4, DzTthetaSphi+fDy2, fDz);
338 pt[7].set( DzTthetaCphi+Dy2Talpha2+fDx4, DzTthetaSphi+fDy2, fDz);
339}
340
341/////////////////////////////////////////////////////////////////////////
342//
343// Check planarity of lateral planes
344//
345void G4UTrap::CheckPlanarity(const G4ThreeVector pt[8]) const
346{
347 constexpr G4int iface[4][4] = { {0,4,5,1}, {2,3,7,6}, {0,2,6,4}, {1,5,7,3} };
348 const static G4String side[4] = { "~-Y", "~+Y", "~-X", "~+X" };
349
350 for (G4int i=0; i<4; ++i)
351 {
352 TrapSidePlane plane = GetSidePlane(i);
353 G4double dmax = 0;
354 for (G4int k=0; k<4; ++k)
355 {
356 const G4ThreeVector p = pt[iface[i][k]];
357 G4double dist = plane.a*p.x() + plane.b*p.y() + plane.c*p.z() + plane.d;
358 if (std::abs(dist) > std::abs(dmax)) dmax = dist;
359 }
360 if (std::abs(dmax) > 1000 * kCarTolerance)
361 {
362 std::ostringstream message;
363 message << "Side face " << side[i] << " is not planar for solid: "
364 << GetName() << "\nDiscrepancy: " << dmax/mm << " mm\n";
365 StreamInfo(message);
366 G4Exception("G4UTrap::CheckPlanarity()", "GeomSolids0002",
367 FatalException, message);
368 }
369 }
370}
371
372/////////////////////////////////////////////////////////////////////////
373//
374// Dispatch to parameterisation for replication mechanism dimension
375// computation & modification.
376//
377void G4UTrap::ComputeDimensions( G4VPVParameterisation* p,
378 const G4int n,
379 const G4VPhysicalVolume* pRep)
380{
381 p->ComputeDimensions(*(G4Trap*)this,n,pRep);
382}
383
384//////////////////////////////////////////////////////////////////////////
385//
386// Make a clone of the object
387//
388G4VSolid* G4UTrap::Clone() const
389{
390 return new G4UTrap(*this);
391}
392
393//////////////////////////////////////////////////////////////////////////
394//
395// Get bounding box
396
397void G4UTrap::BoundingLimits(G4ThreeVector& pMin, G4ThreeVector& pMax) const
398{
399 static G4bool checkBBox = true;
400
401 TrapSidePlane planes[4];
402 for (G4int i=0; i<4; ++i) { planes[i] = GetSidePlane(i); }
403
404 G4double xmin = kInfinity, xmax = -kInfinity;
405 G4double ymin = kInfinity, ymax = -kInfinity;
406 G4double dz = GetZHalfLength();
407 for (G4int i=0; i<8; ++i)
408 {
409 G4int iy = (i==0 || i==1 || i==4 || i==5) ? 0 : 1;
410 G4int ix = (i==0 || i==2 || i==4 || i==6) ? 2 : 3;
411 G4double z = (i < 4) ? -dz : dz;
412 G4double y = -(planes[iy].c*z + planes[iy].d)/planes[iy].b;
413 G4double x = -(planes[ix].b*y + planes[ix].c*z + planes[ix].d)/planes[ix].a;
414 if (x < xmin) xmin = x;
415 if (x > xmax) xmax = x;
416 if (y < ymin) ymin = y;
417 if (y > ymax) ymax = y;
418 }
419
420 pMin.set(xmin,ymin,-dz);
421 pMax.set(xmax,ymax, dz);
422
423 // Check correctness of the bounding box
424 //
425 if (pMin.x() >= pMax.x() || pMin.y() >= pMax.y() || pMin.z() >= pMax.z())
426 {
427 std::ostringstream message;
428 message << "Bad bounding box (min >= max) for solid: "
429 << GetName() << " !"
430 << "\npMin = " << pMin
431 << "\npMax = " << pMax;
432 G4Exception("G4UTrap::BoundingLimits()", "GeomMgt0001",
433 JustWarning, message);
434 StreamInfo(G4cout);
435 }
436
437 // Check consistency of bounding boxes
438 //
439 if (checkBBox)
440 {
441 G4double tolerance = kCarTolerance;
442 U3Vector vmin, vmax;
443 Extent(vmin,vmax);
444 if (std::abs(pMin.x()-vmin.x()) > tolerance ||
445 std::abs(pMin.y()-vmin.y()) > tolerance ||
446 std::abs(pMin.z()-vmin.z()) > tolerance ||
447 std::abs(pMax.x()-vmax.x()) > tolerance ||
448 std::abs(pMax.y()-vmax.y()) > tolerance ||
449 std::abs(pMax.z()-vmax.z()) > tolerance)
450 {
451 std::ostringstream message;
452 message << "Inconsistency in bounding boxes for solid: "
453 << GetName() << " !"
454 << "\nBBox min: wrapper = " << pMin << " solid = " << vmin
455 << "\nBBox max: wrapper = " << pMax << " solid = " << vmax;
456 G4Exception("G4UTrap::BoundingLimits()", "GeomMgt0001",
457 JustWarning, message);
458 checkBBox = false;
459 }
460 }
461}
462
463//////////////////////////////////////////////////////////////////////////
464//
465// Calculate extent under transform and specified limit
466
467G4bool
468G4UTrap::CalculateExtent(const EAxis pAxis,
469 const G4VoxelLimits& pVoxelLimit,
470 const G4AffineTransform& pTransform,
471 G4double& pMin, G4double& pMax) const
472{
473 G4ThreeVector bmin, bmax;
474 G4bool exist;
475
476 // Check bounding box (bbox)
477 //
478 BoundingLimits(bmin,bmax);
479 G4BoundingEnvelope bbox(bmin,bmax);
480#ifdef G4BBOX_EXTENT
481 if (true) return bbox.CalculateExtent(pAxis,pVoxelLimit,pTransform,pMin,pMax);
482#endif
483 if (bbox.BoundingBoxVsVoxelLimits(pAxis,pVoxelLimit,pTransform,pMin,pMax))
484 {
485 return exist = pMin < pMax;
486 }
487
488 // Set bounding envelope (benv) and calculate extent
489 //
490 TrapSidePlane planes[4];
491 for (G4int i=0; i<4; ++i) { planes[i] = GetSidePlane(i); }
492
493 G4ThreeVector pt[8];
494 G4double dz = GetZHalfLength();
495 for (G4int i=0; i<8; ++i)
496 {
497 G4int iy = (i==0 || i==1 || i==4 || i==5) ? 0 : 1;
498 G4int ix = (i==0 || i==2 || i==4 || i==6) ? 2 : 3;
499 G4double z = (i < 4) ? -dz : dz;
500 G4double y = -(planes[iy].c*z + planes[iy].d)/planes[iy].b;
501 G4double x = -(planes[ix].b*y + planes[ix].c*z + planes[ix].d)/planes[ix].a;
502 pt[i].set(x,y,z);
503 }
504
505 G4ThreeVectorList baseA(4), baseB(4);
506 baseA[0] = pt[0];
507 baseA[1] = pt[1];
508 baseA[2] = pt[3];
509 baseA[3] = pt[2];
510
511 baseB[0] = pt[4];
512 baseB[1] = pt[5];
513 baseB[2] = pt[7];
514 baseB[3] = pt[6];
515
516 std::vector<const G4ThreeVectorList *> polygons(2);
517 polygons[0] = &baseA;
518 polygons[1] = &baseB;
519
520 G4BoundingEnvelope benv(bmin,bmax,polygons);
521 exist = benv.CalculateExtent(pAxis,pVoxelLimit,pTransform,pMin,pMax);
522 return exist;
523}
524
525//////////////////////////////////////////////////////////////////////////
526//
527// Create polyhedron for visualization
528//
529G4Polyhedron* G4UTrap::CreatePolyhedron() const
530{
531 return new G4PolyhedronTrap(GetZHalfLength(), GetTheta(), GetPhi(),
532 GetYHalfLength1(),
533 GetXHalfLength1(), GetXHalfLength2(), GetAlpha1(),
534 GetYHalfLength2(),
535 GetXHalfLength3(), GetXHalfLength4(), GetAlpha2());
536}
537
538#endif // G4GEOM_USE_USOLIDS
const G4double kCarTolerance
std::vector< G4ThreeVector > G4ThreeVectorList
@ JustWarning
@ FatalException
void G4Exception(const char *originOfException, const char *exceptionCode, G4ExceptionSeverity severity, const char *description)
CLHEP::Hep3Vector G4ThreeVector
double G4double
Definition G4Types.hh:83
bool G4bool
Definition G4Types.hh:86
int G4int
Definition G4Types.hh:85
G4GLOB_DLL std::ostream G4cout
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...
G4Trap is a general trapezoid: the faces perpendicular to the Z planes are trapezia,...
Definition G4Trap.hh:116
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...
EAxis
Definition geomdefs.hh:54
G4double b
Definition G4Trap.hh:90
G4double c
Definition G4Trap.hh:90
G4double d
Definition G4Trap.hh:90
G4double a
Definition G4Trap.hh:90