Geant4  10.02.p02
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) || defined(G4GEOM_USE_PARTIAL_USOLIDS) )
37 
38 #include "G4Polyhedron.hh"
39 #include "G4PolyhedronArbitrary.hh"
40 
41 #include "G4AutoLock.hh"
42 namespace { G4Mutex UGenericTrapMutex = G4MUTEX_INITIALIZER; }
43 
45 //
46 // Constructor (generic parameters)
47 //
48 G4UGenericTrap::G4UGenericTrap(const G4String& name, G4double halfZ,
49  const std::vector<G4TwoVector>& vertices)
50  : G4USolid(name, new UGenericTrap())
51 {
52  SetZHalfLength(halfZ);
53  std::vector<UVector2> v;
54  for (size_t n=0; n<vertices.size(); ++n)
55  {
56  v.push_back(UVector2(vertices[n].x(),vertices[n].y()));
57  }
58  GetShape()->SetName(name);
59  GetShape()->Initialise(v);
60 }
61 
62 
64 //
65 // Fake default constructor - sets only member data and allocates memory
66 // for usage restricted to object persistency.
67 //
68 G4UGenericTrap::G4UGenericTrap(__void__& a)
69  : G4USolid(a)
70 {
71 }
72 
73 
75 //
76 // Destructor
77 //
78 G4UGenericTrap::~G4UGenericTrap()
79 {
80 }
81 
82 
84 //
85 // Copy constructor
86 //
87 G4UGenericTrap::G4UGenericTrap(const G4UGenericTrap &source)
88  : G4USolid(source)
89 {
90 }
91 
92 
94 //
95 // Assignment operator
96 //
97 G4UGenericTrap&
98 G4UGenericTrap::operator=(const G4UGenericTrap &source)
99 {
100  if (this == &source) return *this;
101 
102  G4USolid::operator=( source );
103 
104  return *this;
105 }
106 
108 //
109 // Accessors & modifiers
110 //
111 G4double G4UGenericTrap::GetZHalfLength() const
112 {
113  return GetShape()->GetZHalfLength();
114 }
115 G4int G4UGenericTrap::GetNofVertices() const
116 {
117  return GetShape()->GetNofVertices();
118 }
119 G4TwoVector G4UGenericTrap::GetVertex(G4int index) const
120 {
121  UVector2 v = GetShape()->GetVertex(index);
122  return G4TwoVector(v.x, v.y);
123 }
124 const std::vector<G4TwoVector>& G4UGenericTrap::GetVertices() const
125 {
126  G4AutoLock l(&UGenericTrapMutex);
127  std::vector<UVector2> v = GetShape()->GetVertices();
128  static std::vector<G4TwoVector> vertices; vertices.clear();
129  for (size_t n=0; n<v.size(); ++n)
130  {
131  vertices.push_back(G4TwoVector(v[n].x,v[n].y));
132  }
133  return vertices;
134 }
135 G4double G4UGenericTrap::GetTwistAngle(G4int index) const
136 {
137  return GetShape()->GetTwistAngle(index);
138 }
139 G4bool G4UGenericTrap::IsTwisted() const
140 {
141  return GetShape()->IsTwisted();
142 }
143 G4int G4UGenericTrap::GetVisSubdivisions() const
144 {
145  return GetShape()->GetVisSubdivisions();
146 }
147 
148 void G4UGenericTrap::SetVisSubdivisions(G4int subdiv)
149 {
150  GetShape()->SetVisSubdivisions(subdiv);
151 }
152 
153 void G4UGenericTrap::SetZHalfLength(G4double halfZ)
154 {
155  GetShape()->SetZHalfLength(halfZ);
156 }
157 
159 //
160 // CreatePolyhedron()
161 //
162 G4Polyhedron* G4UGenericTrap::CreatePolyhedron() const
163 {
164  // Approximation of Twisted Side
165  // Construct extra Points, if Twisted Side
166  //
167  G4PolyhedronArbitrary* polyhedron;
168  size_t nVertices, nFacets;
169  G4double fDz = GetZHalfLength();
170 
171  G4int subdivisions=0;
172  G4int i;
173  if(IsTwisted())
174  {
175  if ( GetVisSubdivisions()!= 0 )
176  {
177  subdivisions=GetVisSubdivisions();
178  }
179  else
180  {
181  // Estimation of Number of Subdivisions for smooth visualisation
182  //
183  G4double maxTwist=0.;
184  for(i=0; i<4; i++)
185  {
186  if(GetTwistAngle(i)>maxTwist) { maxTwist=GetTwistAngle(i); }
187  }
188 
189  // Computes bounding vectors for the shape
190  //
191  G4double Dx,Dy;
192  UVector3 minBox = GetShape()->GetMinimumBBox();
193  UVector3 maxBox = GetShape()->GetMaximumBBox();
194  G4ThreeVector minVec(minBox.x(), minBox.y(), minBox.z());
195  G4ThreeVector maxVec(maxBox.x(), maxBox.y(), maxBox.z());
196  Dx = 0.5*(maxVec.x()- minVec.y());
197  Dy = 0.5*(maxVec.y()- minVec.y());
198  if (Dy > Dx) { Dx=Dy; }
199 
200  subdivisions=8*G4int(maxTwist/(Dx*Dx*Dx)*fDz);
201  if (subdivisions<4) { subdivisions=4; }
202  if (subdivisions>30) { subdivisions=30; }
203  }
204  }
205  G4int sub4=4*subdivisions;
206  nVertices = 8+subdivisions*4;
207  nFacets = 6+subdivisions*4;
208  G4double cf=1./(subdivisions+1);
209  polyhedron = new G4PolyhedronArbitrary (nVertices, nFacets);
210 
211  // Add Vertex
212  //
213  for (i=0;i<4;i++)
214  {
215  polyhedron->AddVertex(G4ThreeVector(GetVertex(i).x(),
216  GetVertex(i).y(),-fDz));
217  }
218  for( i=0;i<subdivisions;i++)
219  {
220  for(G4int j=0;j<4;j++)
221  {
222  G4TwoVector u=GetVertex(j)+cf*(i+1)*( GetVertex(j+4)-GetVertex(j));
223  polyhedron->AddVertex(G4ThreeVector(u.x(),u.y(),-fDz+cf*2*fDz*(i+1)));
224  }
225  }
226  for (i=4;i<8;i++)
227  {
228  polyhedron->AddVertex(G4ThreeVector(GetVertex(i).x(),
229  GetVertex(i).y(),fDz));
230  }
231 
232  // Add Facets
233  //
234  polyhedron->AddFacet(1,4,3,2); //Z-plane
235  for (i=0;i<subdivisions+1;i++)
236  {
237  G4int is=i*4;
238  polyhedron->AddFacet(5+is,8+is,4+is,1+is);
239  polyhedron->AddFacet(8+is,7+is,3+is,4+is);
240  polyhedron->AddFacet(7+is,6+is,2+is,3+is);
241  polyhedron->AddFacet(6+is,5+is,1+is,2+is);
242  }
243  polyhedron->AddFacet(5+sub4,6+sub4,7+sub4,8+sub4); //Z-plane
244 
245  polyhedron->SetReferences();
246  polyhedron->InvertFacets();
247 
248  return (G4Polyhedron*) polyhedron;
249 }
250 
251 #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
#define G4MUTEX_INITIALIZER
Definition: G4Threading.hh:175
bool G4bool
Definition: G4Types.hh:79
const G4int n
void AddVertex(const G4ThreeVector &v)
G4int G4Mutex
Definition: G4Threading.hh:173
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