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