Geant4  10.02.p01
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 //
27 // $Id:$
28 //
29 //
30 // Implementation of G4UGenericTrap wrapper class
31 // --------------------------------------------------------------------
32 
33 #include "G4GenericTrap.hh"
34 #include "G4UGenericTrap.hh"
35 
36 #if defined(G4GEOM_USE_USOLIDS)
37 
38 #include "G4Polyhedron.hh"
39 #include "G4PolyhedronArbitrary.hh"
40 
42 //
43 // Constructor (generic parameters)
44 //
45 G4UGenericTrap::G4UGenericTrap(const G4String& name, G4double halfZ,
46  const std::vector<G4TwoVector>& vertices)
47  : G4USolid(name, new UGenericTrap())
48 {
49  SetZHalfLength(halfZ);
50  std::vector<UVector2> v;
51  for (size_t n=0; n<vertices.size(); ++n)
52  {
53  v.push_back(UVector2(vertices[n].x(),vertices[n].y()));
54  }
55  GetShape()->SetName(name);
56  GetShape()->Initialise(v);
57 }
58 
59 
61 //
62 // Fake default constructor - sets only member data and allocates memory
63 // for usage restricted to object persistency.
64 //
65 G4UGenericTrap::G4UGenericTrap(__void__& a)
66  : G4USolid(a)
67 {
68 }
69 
70 
72 //
73 // Destructor
74 //
75 G4UGenericTrap::~G4UGenericTrap()
76 {
77 }
78 
79 
81 //
82 // Copy constructor
83 //
84 G4UGenericTrap::G4UGenericTrap(const G4UGenericTrap &source)
85  : G4USolid(source)
86 {
87 }
88 
89 
91 //
92 // Assignment operator
93 //
94 G4UGenericTrap&
95 G4UGenericTrap::operator=(const G4UGenericTrap &source)
96 {
97  if (this == &source) return *this;
98 
99  G4USolid::operator=( source );
100 
101  return *this;
102 }
103 
105 //
106 // CreatePolyhedron()
107 //
108 G4Polyhedron* G4UGenericTrap::CreatePolyhedron() const
109 {
110  // Approximation of Twisted Side
111  // Construct extra Points, if Twisted Side
112  //
113  G4PolyhedronArbitrary* polyhedron;
114  size_t nVertices, nFacets;
115  G4double fDz = GetZHalfLength();
116 
117  G4int subdivisions=0;
118  G4int i;
119  if(IsTwisted())
120  {
121  if ( GetVisSubdivisions()!= 0 )
122  {
123  subdivisions=GetVisSubdivisions();
124  }
125  else
126  {
127  // Estimation of Number of Subdivisions for smooth visualisation
128  //
129  G4double maxTwist=0.;
130  for(i=0; i<4; i++)
131  {
132  if(GetTwistAngle(i)>maxTwist) { maxTwist=GetTwistAngle(i); }
133  }
134 
135  // Computes bounding vectors for the shape
136  //
137  G4double Dx,Dy;
138  UVector3 minBox = GetShape()->GetMinimumBBox();
139  UVector3 maxBox = GetShape()->GetMaximumBBox();
140  G4ThreeVector minVec(minBox.x(), minBox.y(), minBox.z());
141  G4ThreeVector maxVec(maxBox.x(), maxBox.y(), maxBox.z());
142  Dx = 0.5*(maxVec.x()- minVec.y());
143  Dy = 0.5*(maxVec.y()- minVec.y());
144  if (Dy > Dx) { Dx=Dy; }
145 
146  subdivisions=8*G4int(maxTwist/(Dx*Dx*Dx)*fDz);
147  if (subdivisions<4) { subdivisions=4; }
148  if (subdivisions>30) { subdivisions=30; }
149  }
150  }
151  G4int sub4=4*subdivisions;
152  nVertices = 8+subdivisions*4;
153  nFacets = 6+subdivisions*4;
154  G4double cf=1./(subdivisions+1);
155  polyhedron = new G4PolyhedronArbitrary (nVertices, nFacets);
156 
157  // Add Vertex
158  //
159  for (i=0;i<4;i++)
160  {
161  polyhedron->AddVertex(G4ThreeVector(GetVertex(i).x(),
162  GetVertex(i).y(),-fDz));
163  }
164  for( i=0;i<subdivisions;i++)
165  {
166  for(G4int j=0;j<4;j++)
167  {
168  G4TwoVector u=GetVertex(j)+cf*(i+1)*( GetVertex(j+4)-GetVertex(j));
169  polyhedron->AddVertex(G4ThreeVector(u.x(),u.y(),-fDz+cf*2*fDz*(i+1)));
170  }
171  }
172  for (i=4;i<8;i++)
173  {
174  polyhedron->AddVertex(G4ThreeVector(GetVertex(i).x(),
175  GetVertex(i).y(),fDz));
176  }
177 
178  // Add Facets
179  //
180  polyhedron->AddFacet(1,4,3,2); //Z-plane
181  for (i=0;i<subdivisions+1;i++)
182  {
183  G4int is=i*4;
184  polyhedron->AddFacet(5+is,8+is,4+is,1+is);
185  polyhedron->AddFacet(8+is,7+is,3+is,4+is);
186  polyhedron->AddFacet(7+is,6+is,2+is,3+is);
187  polyhedron->AddFacet(6+is,5+is,1+is,2+is);
188  }
189  polyhedron->AddFacet(5+sub4,6+sub4,7+sub4,8+sub4); //Z-plane
190 
191  polyhedron->SetReferences();
192  polyhedron->InvertFacets();
193 
194  return (G4Polyhedron*) polyhedron;
195 }
196 
197 #endif // G4GEOM_USE_USOLIDS
CLHEP::Hep3Vector G4ThreeVector
G4String name
Definition: TRTMaterials.hh:40
G4double a
Definition: TRTMaterials.hh:39
int G4int
Definition: G4Types.hh:78
const G4int n
void AddVertex(const G4ThreeVector &v)
const G4double x[NPOINTSGL]
void AddFacet(const G4int iv1, const G4int iv2, const G4int iv3, const G4int iv4=0)
CLHEP::Hep2Vector G4TwoVector
Definition: G4TwoVector.hh:42
double G4double
Definition: G4Types.hh:76