VGM Version 5.3
Loading...
Searching...
No Matches
ExtrudedSolid.cxx
Go to the documentation of this file.
1// $Id$
2
3// -----------------------------------------------------------------------
4// The Geant4GM package of the Virtual Geometry Model
5// Copyright (C) 2007, Ivana Hrivnacova
6// All rights reserved.
7//
8// For the licensing terms see vgm/LICENSE.
9// Contact: ivana@ipno.in2p3.fr
10// -----------------------------------------------------------------------
11
12//
13// Class ExtrudedSolid
14// --------------------
15// VGM implementation for Geant4 ExtrudedSolid solid.
16//
17// Author: Ivana Hrivnacova; IPN Orsay
18
21
22#include "ClhepVGM/Units.h"
23
24#include "G4ExtrudedSolid.hh"
25#include "G4ReflectedSolid.hh"
26#include "G4UnionSolid.hh"
27
28#include <iostream>
29#include <math.h>
30#include <set>
31
32//_____________________________________________________________________________
34 std::vector<VGM::TwoVector> polygon,
35 std::vector<std::vector<double> > zsections)
36 : VGM::ISolid(),
37 VGM::IExtrudedSolid(),
38 BaseVGM::VExtrudedSolid(),
39 fName(name),
40 fExtrudedSolid(0),
41 fZSections(zsections),
42 fConstituents()
43{
48
49 if (zsections.size() < 2) {
50 std::cerr << "+++ Error +++" << std::endl;
51 std::cerr << " Number of z-sections = " << zsections.size()
52 << " has to be >= 2" << std::endl;
53 exit(1);
54 }
55
56 // Fill polygon
57 std::vector<G4TwoVector> g4Polygon;
58 for (G4int i = 0; i < G4int(polygon.size()); ++i) {
59 g4Polygon.push_back(
60 G4TwoVector(polygon[i].first / ClhepVGM::Units::Length(),
61 polygon[i].second / ClhepVGM::Units::Length()));
62 }
63
64 // Check if solid has to be broken in more constituents
65 // ( If some z-sections are at the same z-position )
66 //
67 std::set<unsigned int> toBreak;
68 for (unsigned int i = 1; i < zsections.size(); ++i)
69 if (fabs(zsections[i][0] - zsections[i - 1][0]) < 1e-09)
70 toBreak.insert(i - 1);
71 toBreak.insert(zsections.size() - 1);
72
73 // Create solid constituents
74 //
75 std::vector<G4ExtrudedSolid::ZSection> g4Zsections;
76 for (unsigned int iz = 0; iz < zsections.size(); ++iz) {
77 // Fill z-sections for this constituent
78 g4Zsections.push_back(
79 G4ExtrudedSolid::ZSection(zsections[iz][0] / ClhepVGM::Units::Length(),
80 G4TwoVector(zsections[iz][1] / ClhepVGM::Units::Length(),
81 zsections[iz][2] / ClhepVGM::Units::Length()),
82 zsections[iz][3]));
83
84 if (toBreak.find(iz) != toBreak.end()) {
85 // Create G4ExtrudedSolid
86 // G4cout << "Go to create constituent with " << g4Zsections.size()
87 // << " z-sections" << G4endl;
88
89 G4ExtrudedSolid* xtru =
90 new G4ExtrudedSolid(fName, g4Polygon, g4Zsections);
91 fConstituents.push_back(xtru);
92 g4Zsections.clear();
93 }
94 }
95
96 // Compose boolean solid if solid had to be broken
97 //
98 G4VSolid* xtruA = *fConstituents.begin();
99
100 std::vector<G4ExtrudedSolid*>::const_iterator it;
101 for (it = fConstituents.begin() + 1; it != fConstituents.end(); ++it) {
102 G4ExtrudedSolid* xtruB = *it;
103 G4VSolid* unionAB =
104 new G4UnionSolid(fName, xtruA, xtruB, 0, G4ThreeVector());
105 xtruA = unionAB;
106 }
107 fExtrudedSolid = xtruA;
108
109 // G4cout << *fExtrudedSolid << G4endl;
110
111 Geant4GM::SolidMap::Instance()->AddSolid(this, fExtrudedSolid);
112}
113
114//_____________________________________________________________________________
116 G4ExtrudedSolid* xtru, G4ReflectedSolid* reflXtru)
117 : VGM::ISolid(),
118 VGM::IExtrudedSolid(),
119 BaseVGM::VExtrudedSolid(),
120 fName(xtru->GetName()),
121 fExtrudedSolid(xtru),
122 fZSections(),
123 fConstituents()
124{
126
127 G4double sign = 1.;
128 if (reflXtru) sign = -1;
129
130 // Fill z planes parameters
131 for (G4int iz = 0; iz < xtru->GetNofZSections(); ++iz) {
132 ZSectionType zsection(4);
133 zsection[0] = sign * xtru->GetZSection(iz).fZ / ClhepVGM::Units::Length();
134 zsection[1] = xtru->GetZSection(iz).fOffset.x() / ClhepVGM::Units::Length();
135 zsection[2] = xtru->GetZSection(iz).fOffset.y() / ClhepVGM::Units::Length();
136 zsection[3] = xtru->GetZSection(iz).fScale;
137 if (!reflXtru)
138 fZSections.push_back(zsection);
139 else
140 fZSections.insert(fZSections.begin(), zsection);
141 }
142
143 // Fill constituent
144 fConstituents.push_back(xtru);
145
146 if (reflXtru)
147 Geant4GM::SolidMap::Instance()->AddSolid(this, reflXtru);
148 else
150}
151
152//_____________________________________________________________________________
154 : VGM::ISolid(),
155 VGM::IExtrudedSolid(),
156 BaseVGM::VExtrudedSolid(),
157 fName(),
158 fExtrudedSolid(0),
159 fZSections(),
160 fConstituents()
161{
163}
164
165//_____________________________________________________________________________
167 : VGM::ISolid(rhs),
168 VGM::IExtrudedSolid(rhs),
169 BaseVGM::VExtrudedSolid(rhs),
170 fName(),
171 fExtrudedSolid(0),
172 fZSections(),
173 fConstituents()
174{
176}
177
178//_____________________________________________________________________________
183
184/*
185//_____________________________________________________________________________
186void Geant4GM::ExtrudedSolid::CreateFinalSolid()
187{
189
190 G4cout << "Geant4GM::ExtrudedSolid::CreateFinalSolid" << G4endl;
191
192 G4VSolid* xtruA = *fConstituents.begin();
193 G4double hzTotal = (*fConstituents.begin())->GetZHalfLength();
194
195 // compose boolean solid if more then 1 constituent
196 std::vector<G4ExtrudedSolid*>::const_iterator it;
197 for ( it = fConstituents.begin()+1; it != fConstituents.end(); ++it ) {
198 G4ExtrudedSolid* xtruB = *it;
199 G4double dz = hzTotal + xtruB->GetZHalfLength();
200 G4VSolid* unionAB
201 = new G4UnionSolid(fName, xtruA, xtruB, 0, G4ThreeVector(0., 0., dz));
202 xtruA = unionAB;
203 hzTotal += xtruB->GetZHalfLength();
204 }
205
206 fExtrudedSolid = xtruA;
207 fZDisplacement = fZDisplacement - hzTotal; // CHECK !!!
208
209 G4cout << *fExtrudedSolid << G4endl;
210
211 // Reset VGM polygon data
212 dynamic_cast<Geant4GM::Polygon*>(fPolygon)
213 ->SetExtrudedSolid(*fConstituents.begin());
214
215 Geant4GM::SolidMap::Instance()->AddSolid(this, fExtrudedSolid);
216}
217
218//_____________________________________________________________________________
219void Geant4GM::ExtrudedSolid::AddZPlane(double zpos,
220 VGM::TwoVector offset, double scale)
221{
222
223 if ( fZSections.size() > 0) {
224
225 // Set polygon
226 std::vector<G4TwoVector> polygon;
227 if ( fConstituents.size() == 0 ) {
228 // Fill it from VGM object if not yet defined
229 for ( G4int i=0; i<fPolygon->NofVertices(); i++ ) {
230 polygon.push_back(
231 G4TwoVector(
232 fPolygon->Vertex(i).first / ClhepVGM::Units::Length(),
233 fPolygon->Vertex(i).second / ClhepVGM::Units::Length()));
234 }
235 }
236 else {
237 // Get polygon from existing ExtrudedSolid solid
238 polygon = (*fConstituents.begin())->GetPolygon();
239 }
240
241 ZPlaneType zplane = fZSections[fZSections.size()-1];
242 G4double hz = (zpos -zplane[0])/2.0 / ClhepVGM::Units::Length();
243 if ( hz > 0. ) {
244 G4TwoVector off1 = G4TwoVector(
245 zplane[1]/ ClhepVGM::Units::Length(),
246 zplane[2]/ ClhepVGM::Units::Length());
247 G4double scale1 = zplane[3];
248 G4TwoVector off2 = G4TwoVector(
249 offset.first / ClhepVGM::Units::Length(),
250 offset.second/ ClhepVGM::Units::Length());
251 G4double scale2 = scale;
252
253 // Create xtru for 2 planes and save it in the map
254 G4ExtrudedSolid* xtru
255 = new G4ExtrudedSolid(fName, polygon, hz, off1, scale1, off2, scale2);
256 fConstituents.push_back(xtru);
257 }
258 }
259 else {
260 // Keep starting position of the solid
261 // fZDisplacement = zpos;
262 }
263
264 // Keep data of this z plane
265 ZPlaneType zplane(4);
266 zplane[0] = zpos;
267 zplane[1] = offset.first;
268 zplane[2] = offset.second;
269 zplane[3] = scale;
270 fZSections.push_back(zplane);
271
272
273 // If the last z plane create the final solid
274 if ( G4int(fZSections.size()) == fNofZSections ) CreateFinalSolid();
275}
276*/
277
278//_____________________________________________________________________________
280{
281 return fExtrudedSolid->GetName();
282}
283
284//_____________________________________________________________________________
286{
287 return fConstituents[0]->GetNofVertices();
288}
289
290//_____________________________________________________________________________
292{
293 if (index < 0 || index > NofVertices()) {
294 std::cerr << "+++ Error +++" << std::endl;
295 std::cerr << " Wrong vertex index: " << index << std::endl;
296 exit(1);
297 }
298
299 return VGM::TwoVector(
300 fConstituents[0]->GetVertex(index).x() * ClhepVGM::Units::Length(),
301 fConstituents[0]->GetVertex(index).y() * ClhepVGM::Units::Length());
302}
303
304//_____________________________________________________________________________
305int Geant4GM::ExtrudedSolid::NofZSections() const { return fZSections.size(); }
306
307//_____________________________________________________________________________
309{
310 if (iz < 0 || iz > NofZSections()) {
311 std::cerr << "+++ Error +++" << std::endl;
312 std::cerr << " Wrong index: " << iz << std::endl;
313 exit(1);
314 }
315
316 ZSectionType zplane = fZSections[iz];
317 return zplane[0] * ClhepVGM::Units::Length();
318}
319
320//_____________________________________________________________________________
322{
323 if (iz < 0 || iz > NofZSections()) {
324 std::cerr << "+++ Error +++" << std::endl;
325 std::cerr << " Wrong index: " << iz << std::endl;
326 exit(1);
327 }
328
329 ZSectionType zplane = fZSections[iz];
330 return VGM::TwoVector(zplane[1] * ClhepVGM::Units::Length(),
331 zplane[2] * ClhepVGM::Units::Length());
332}
333
334//_____________________________________________________________________________
336{
337 if (iz < 0 || iz > NofZSections()) {
338 std::cerr << "+++ Error +++" << std::endl;
339 std::cerr << " Wrong index: " << iz << std::endl;
340 exit(1);
341 }
342
343 ZSectionType zplane = fZSections[iz];
344 return zplane[3];
345}
static double Length()
Return CLHEP default length unit in VGM units.
Definition Units.cxx:83
VGM implementation for Geant4 xtru solid.
virtual std::string Name() const
Return the name of this solid.
virtual double ZPosition(int iz) const
Return the z position of the iz-th plane in mm.
virtual VGM::TwoVector Offset(int iz) const
Return the polygon offset in iz-th side.
virtual int NofZSections() const
Return the number of planes perpendicular to the z axis.
VGM::TwoVector Vertex(int index) const
Return the index-th vertex of outline polygon.
int NofVertices() const
Return the number of vertices of outline polygon.
virtual double Scale(int iz) const
Return the polygon scale in iz-th side.
static SolidMap * Instance()
Definition SolidMap.cxx:28
void AddSolid(VGM::ISolid *, G4VSolid *)
Definition SolidMap.cxx:59
BaseVGM utilities.
Definition utilities.h:23
VGM interfaces.
Definition VMedium.h:28
std::pair< double, double > TwoVector
Definition TwoVector.h:28