Geant4 11.4.0
Toolkit for the simulation of the passage of particles through matter
Loading...
Searching...
No Matches
G4UGenericTrap.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 G4UGenericTrap wrapper class
27//
28// 30.10.13 G.Cosmo, CERN
29// --------------------------------------------------------------------
30
31#include "G4GenericTrap.hh"
32#include "G4UGenericTrap.hh"
33
34#if ( defined(G4GEOM_USE_USOLIDS) || defined(G4GEOM_USE_PARTIAL_USOLIDS) )
35
36#include "G4AffineTransform.hh"
38#include "G4BoundingEnvelope.hh"
39
40#include "G4Polyhedron.hh"
41
42using namespace CLHEP;
43
44////////////////////////////////////////////////////////////////////////
45//
46// Constructor (generic parameters)
47//
48G4UGenericTrap::G4UGenericTrap(const G4String& name, G4double halfZ,
49 const std::vector<G4TwoVector>& vertices)
50 : Base_t(name), fVisSubdivisions(0)
51{
52 SetZHalfLength(halfZ);
53 Initialise(vertices);
54}
55
56
57//////////////////////////////////////////////////////////////////////////
58//
59// Copy constructor
60//
61G4UGenericTrap::G4UGenericTrap(const G4UGenericTrap& source)
62 : Base_t(source), fVisSubdivisions(source.fVisSubdivisions),
63 fVertices(source.fVertices)
64{
65}
66
67
68//////////////////////////////////////////////////////////////////////////
69//
70// Assignment operator
71//
72G4UGenericTrap&
73G4UGenericTrap::operator=(const G4UGenericTrap& source)
74{
75 if (this == &source) return *this;
76
77 Base_t::operator=( source );
78 fVertices = source.fVertices;
79 fVisSubdivisions = source.fVisSubdivisions;
80
81 return *this;
82}
83
84//////////////////////////////////////////////////////////////////////////
85//
86// Accessors & modifiers
87//
88G4double G4UGenericTrap::GetZHalfLength() const
89{
90 return GetDZ();
91}
92G4int G4UGenericTrap::GetNofVertices() const
93{
94 return fVertices.size();
95}
96G4TwoVector G4UGenericTrap::GetVertex(G4int index) const
97{
98 return { vecgeom::UnplacedGenTrap::GetVertex(index).x(), vecgeom::UnplacedGenTrap::GetVertex(index).y() };
99}
100const std::vector<G4TwoVector>& G4UGenericTrap::GetVertices() const
101{
102 return fVertices;
103}
104G4double G4UGenericTrap::GetTwistAngle(G4int index) const
105{
106 return GetTwist(index);
107}
108G4bool G4UGenericTrap::IsTwisted() const
109{
110 return !IsPlanar();
111}
112G4int G4UGenericTrap::GetVisSubdivisions() const
113{
114 return fVisSubdivisions;
115}
116
117void G4UGenericTrap::SetVisSubdivisions(G4int subdiv)
118{
119 fVisSubdivisions = subdiv;
120}
121
122void G4UGenericTrap::SetZHalfLength(G4double halfZ)
123{
124 SetDZ(halfZ);
125}
126
127void G4UGenericTrap::Initialise(const std::vector<G4TwoVector>& v)
128{
129 G4double verticesx[8], verticesy[8];
130 for (G4int i=0; i<8; ++i)
131 {
132 fVertices.push_back(v[i]);
133 verticesx[i] = v[i].x();
134 verticesy[i] = v[i].y();
135 }
136 Initialize(verticesx, verticesy, GetZHalfLength());
137}
138
139/////////////////////////////////////////////////////////////////////////
140//
141// Get bounding box
142
143void G4UGenericTrap::BoundingLimits(G4ThreeVector& pMin,
144 G4ThreeVector& pMax) const
145{
146 U3Vector vmin, vmax;
147 Extent(vmin,vmax);
148 pMin.set(vmin.x(),vmin.y(),vmin.z());
149 pMax.set(vmax.x(),vmax.y(),vmax.z());
150
151 // Check correctness of the bounding box
152 //
153 if (pMin.x() >= pMax.x() || pMin.y() >= pMax.y() || pMin.z() >= pMax.z())
154 {
155 std::ostringstream message;
156 message << "Bad bounding box (min >= max) for solid: "
157 << GetName() << " !"
158 << "\npMin = " << pMin
159 << "\npMax = " << pMax;
160 G4Exception("G4UGenericTrap::BoundingLimits()", "GeomMgt0001",
161 JustWarning, message);
162 StreamInfo(G4cout);
163 }
164}
165
166//////////////////////////////////////////////////////////////////////////
167//
168// Calculate extent under transform and specified limit
169
170G4bool
171G4UGenericTrap::CalculateExtent(const EAxis pAxis,
172 const G4VoxelLimits& pVoxelLimit,
173 const G4AffineTransform& pTransform,
174 G4double& pMin, G4double& pMax) const
175{
176 G4ThreeVector bmin, bmax;
177 G4bool exist;
178
179 // Check bounding box (bbox)
180 //
181 BoundingLimits(bmin,bmax);
182 G4BoundingEnvelope bbox(bmin,bmax);
183#ifdef G4BBOX_EXTENT
184 return bbox.CalculateExtent(pAxis,pVoxelLimit,pTransform,pMin,pMax);
185#endif
186 if (bbox.BoundingBoxVsVoxelLimits(pAxis,pVoxelLimit,pTransform,pMin,pMax))
187 {
188 return exist = pMin < pMax;
189 }
190
191 // Set bounding envelope (benv) and calculate extent
192 //
193 // To build the bounding envelope with plane faces each side face of
194 // the trapezoid is subdivided in triangles. Subdivision is done by
195 // duplication of vertices in the bases in a way that the envelope be
196 // a convex polyhedron (some faces of the envelope can be degenerate)
197 //
198 G4double dz = GetZHalfLength();
199 G4ThreeVectorList baseA(8), baseB(8);
200 for (G4int i=0; i<4; ++i)
201 {
202 G4TwoVector va = GetVertex(i);
203 G4TwoVector vb = GetVertex(i+4);
204 baseA[2*i].set(va.x(),va.y(),-dz);
205 baseB[2*i].set(vb.x(),vb.y(), dz);
206 }
207 for (G4int i=0; i<4; ++i)
208 {
209 G4int k1=2*i, k2=(2*i+2)%8;
210 G4double ax = (baseA[k2].x()-baseA[k1].x());
211 G4double ay = (baseA[k2].y()-baseA[k1].y());
212 G4double bx = (baseB[k2].x()-baseB[k1].x());
213 G4double by = (baseB[k2].y()-baseB[k1].y());
214 G4double znorm = ax*by - ay*bx;
215 baseA[k1+1] = (znorm < 0.0) ? baseA[k2] : baseA[k1];
216 baseB[k1+1] = (znorm < 0.0) ? baseB[k1] : baseB[k2];
217 }
218
219 std::vector<const G4ThreeVectorList *> polygons(2);
220 polygons[0] = &baseA;
221 polygons[1] = &baseB;
222
223 G4BoundingEnvelope benv(bmin,bmax,polygons);
224 exist = benv.CalculateExtent(pAxis,pVoxelLimit,pTransform,pMin,pMax);
225 return exist;
226}
227
228//////////////////////////////////////////////////////////////////////////
229//
230// CreatePolyhedron()
231//
232G4Polyhedron* G4UGenericTrap::CreatePolyhedron() const
233{
234 // Approximation of Twisted Side
235 // Construct extra Points, if Twisted Side
236 //
237 G4Polyhedron* polyhedron;
238 G4int nVertices, nFacets;
239 G4double fDz = GetZHalfLength();
240
241 G4int subdivisions = 0;
242 if (IsTwisted())
243 {
244 if (GetVisSubdivisions() != 0)
245 {
246 subdivisions = GetVisSubdivisions();
247 }
248 else
249 {
250 // Estimation of Number of Subdivisions for smooth visualisation
251 //
252 G4double maxTwist = 0.;
253 for(G4int i = 0; i < 4; ++i)
254 {
255 if (GetTwistAngle(i) > maxTwist) { maxTwist = GetTwistAngle(i); }
256 }
257
258 // Computes bounding vectors for the shape
259 //
260 G4double Dx, Dy;
261 G4ThreeVector minVec, maxVec;
262 BoundingLimits(minVec, maxVec);
263 Dx = 0.5*(maxVec.x() - minVec.y());
264 Dy = 0.5*(maxVec.y() - minVec.y());
265 if (Dy > Dx) { Dx = Dy; }
266
267 subdivisions = 8*G4int(maxTwist/(Dx*Dx*Dx)*fDz);
268 if (subdivisions < 4) { subdivisions = 4; }
269 if (subdivisions > 30) { subdivisions = 30; }
270 }
271 }
272 G4int sub4 = 4*subdivisions;
273 nVertices = 8 + subdivisions*4;
274 nFacets = 6 + subdivisions*4;
275 G4double cf = 1./(subdivisions + 1);
276 polyhedron = new G4Polyhedron(nVertices, nFacets);
277
278 // Set vertices
279 //
280 G4int icur = 0;
281 for (G4int i = 0; i < 4; ++i)
282 {
283 G4ThreeVector v(GetVertex(i).x(),GetVertex(i).y(),-fDz);
284 polyhedron->SetVertex(++icur, v);
285 }
286 for (G4int i = 0; i < subdivisions; ++i)
287 {
288 for (G4int j = 0; j < 4; ++j)
289 {
290 G4TwoVector u = GetVertex(j)+cf*(i+1)*( GetVertex(j+4)-GetVertex(j));
291 G4ThreeVector v(u.x(),u.y(),-fDz+cf*2*fDz*(i+1));
292 polyhedron->SetVertex(++icur, v);
293 }
294 }
295 for (G4int i = 4; i < 8; ++i)
296 {
297 G4ThreeVector v(GetVertex(i).x(),GetVertex(i).y(),fDz);
298 polyhedron->SetVertex(++icur, v);
299 }
300
301 // Set facets
302 //
303 icur = 0;
304 polyhedron->SetFacet(++icur, 1, 4, 3, 2); // Z-plane
305 for (G4int i = 0; i < subdivisions + 1; ++i)
306 {
307 G4int is = i*4;
308 polyhedron->SetFacet(++icur, 5+is, 8+is, 4+is, 1+is);
309 polyhedron->SetFacet(++icur, 8+is, 7+is, 3+is, 4+is);
310 polyhedron->SetFacet(++icur, 7+is, 6+is, 2+is, 3+is);
311 polyhedron->SetFacet(++icur, 6+is, 5+is, 1+is, 2+is);
312 }
313 polyhedron->SetFacet(++icur, 5+sub4, 6+sub4, 7+sub4, 8+sub4); // Z-plane
314
315 polyhedron->SetReferences();
316 polyhedron->InvertFacets();
317
318 return polyhedron;
319}
320
321#endif // G4GEOM_USE_USOLIDS
std::vector< G4ThreeVector > G4ThreeVectorList
@ JustWarning
void G4Exception(const char *originOfException, const char *exceptionCode, G4ExceptionSeverity severity, const char *description)
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...
G4VoxelLimits represents limitation/restrictions of space, where restrictions are only made perpendic...
void SetVertex(G4int index, const G4Point3D &v)
void SetFacet(G4int index, G4int iv1, G4int iv2, G4int iv3, G4int iv4=0)
EAxis
Definition geomdefs.hh:54
const char * name(G4int ptype)