source: trunk/source/persistency/gdml/src/G4STRead.cc@ 1315

Last change on this file since 1315 was 1228, checked in by garnier, 16 years ago

update geant4.9.3 tag

File size: 9.2 KB
Line 
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// $Id: G4STRead.cc,v 1.4 2009/04/24 15:34:20 gcosmo Exp $
27// GEANT4 tag $Name: geant4-09-03 $
28//
29// class G4STRead Implementation
30//
31// History:
32// - Created. Zoltan Torzsok, November 2007
33// -------------------------------------------------------------------------
34
35#include <fstream>
36
37#include "G4STRead.hh"
38
39#include "G4Material.hh"
40#include "G4Box.hh"
41#include "G4QuadrangularFacet.hh"
42#include "G4TriangularFacet.hh"
43#include "G4TessellatedSolid.hh"
44#include "G4LogicalVolume.hh"
45#include "G4PVPlacement.hh"
46
47void G4STRead::TessellatedRead(const std::string& line)
48{
49 if (tessellatedList.size()>0)
50 {
51 tessellatedList.back()->SetSolidClosed(true);
52 // Finish the previous solid at first!
53 }
54
55 std::istringstream stream(line.substr(2));
56
57 G4String name;
58 stream >> name;
59
60 G4TessellatedSolid* tessellated = new G4TessellatedSolid(name);
61 volumeMap[tessellated] =
62 new G4LogicalVolume(tessellated, solid_material, name+"_LV" , 0, 0, 0);
63 tessellatedList.push_back(tessellated);
64
65 G4cout << "G4STRead: Reading solid: " << name << G4endl;
66}
67
68void G4STRead::FacetRead(const std::string& line)
69{
70 if (tessellatedList.size()==0)
71 {
72 G4Exception("G4STRead::FacetRead()", "ReadError", FatalException,
73 "A solid must be defined before defining a facet!");
74 }
75
76 if (line[2]=='3') // Triangular facet
77 {
78 G4double x1,y1,z1;
79 G4double x2,y2,z2;
80 G4double x3,y3,z3;
81
82 std::istringstream stream(line.substr(4));
83 stream >> x1 >> y1 >> z1 >> x2 >> y2 >> z2 >> x3 >> y3 >> z3;
84 tessellatedList.back()->
85 AddFacet(new G4TriangularFacet(G4ThreeVector(x1,y1,z1),
86 G4ThreeVector(x2,y2,z2),
87 G4ThreeVector(x3,y3,z3), ABSOLUTE));
88 }
89 else if (line[2]=='4') // Quadrangular facet
90 {
91 G4double x1,y1,z1;
92 G4double x2,y2,z2;
93 G4double x3,y3,z3;
94 G4double x4,y4,z4;
95
96 std::istringstream stream(line.substr(4));
97 stream >> x1 >> y1 >> z1 >> x2 >> y2 >> z2
98 >> x3 >> y3 >> z3 >> x4 >> y4 >> z4;
99 tessellatedList.back()->
100 AddFacet(new G4QuadrangularFacet(G4ThreeVector(x1,y1,z1),
101 G4ThreeVector(x2,y2,z2),
102 G4ThreeVector(x3,y3,z3),
103 G4ThreeVector(x4,y4,z4), ABSOLUTE));
104 }
105 else
106 {
107 G4Exception("G4STRead::FacetRead()", "ReadError", FatalException,
108 "Number of vertices per facet should be either 3 or 4!");
109 }
110}
111
112void G4STRead::PhysvolRead(const std::string& line)
113{
114 G4int level;
115 G4String name;
116 G4double r1,r2,r3;
117 G4double r4,r5,r6;
118 G4double r7,r8,r9;
119 G4double pX,pY,pZ;
120 G4double n1,n2,n3,n4,n5;
121
122 std::istringstream stream(line.substr(2));
123 stream >> level >> name >> r1 >> r2 >> r3 >> n1 >> r4 >> r5 >> r6
124 >> n2 >> r7 >> r8 >> r9 >> n3 >> pX >> pY >> pZ >> n4 >> n5;
125
126 name.resize(name.rfind("_"));
127
128 G4cout << "G4STRead: Placing tessellated solid: " << name << G4endl;
129
130 G4TessellatedSolid* tessellated = 0;
131
132 for (size_t i=0; i<tessellatedList.size(); i++)
133 { // Find the volume for this physvol!
134 if (tessellatedList[i]->GetName() == G4String(name))
135 {
136 tessellated = tessellatedList[i];
137 break;
138 }
139 }
140
141 if (tessellated == 0)
142 {
143 G4String error_msg = "Referenced solid '" + name + "' not found!";
144 G4Exception("G4STRead::PhysvolRead()", "ReadError",
145 FatalException, error_msg);
146 }
147 if (volumeMap.find(tessellated) == volumeMap.end())
148 {
149 G4String error_msg = "Referenced solid '" + name
150 + "' is not associated with a logical volume!";
151 G4Exception("G4STRead::PhysvolRead()", "InvalidSetup",
152 FatalException, error_msg);
153 }
154 const G4RotationMatrix rot(G4ThreeVector(r1,r2,r3),
155 G4ThreeVector(r4,r5,r6),
156 G4ThreeVector(r7,r8,r9));
157 const G4ThreeVector pos(pX,pY,pZ);
158
159 new G4PVPlacement(G4Transform3D(rot.inverse(),pos),
160 volumeMap[tessellated], name+"_PV", world_volume, 0, 0);
161 // Note: INVERSE of rotation is needed!!!
162
163 G4double minx,miny,minz;
164 G4double maxx,maxy,maxz;
165 const G4VoxelLimits limits;
166
167 tessellated->CalculateExtent(kXAxis,limits,
168 G4AffineTransform(rot,pos),minx,maxx);
169 tessellated->CalculateExtent(kYAxis,limits,
170 G4AffineTransform(rot,pos),miny,maxy);
171 tessellated->CalculateExtent(kZAxis,limits,
172 G4AffineTransform(rot,pos),minz,maxz);
173
174 if (world_extent.x() < std::fabs(minx))
175 { world_extent.setX(std::fabs(minx)); }
176 if (world_extent.y() < std::fabs(miny))
177 { world_extent.setY(std::fabs(miny)); }
178 if (world_extent.z() < std::fabs(minz))
179 { world_extent.setZ(std::fabs(minz)); }
180 if (world_extent.x() < std::fabs(maxx))
181 { world_extent.setX(std::fabs(maxx)); }
182 if (world_extent.y() < std::fabs(maxy))
183 { world_extent.setY(std::fabs(maxy)); }
184 if (world_extent.z() < std::fabs(maxz))
185 { world_extent.setZ(std::fabs(maxz)); }
186}
187
188void G4STRead::ReadGeom(const G4String& name)
189{
190 G4cout << "G4STRead: Reading '" << name << "'..." << G4endl;
191
192 std::ifstream GeomFile(name);
193
194 if (!GeomFile)
195 {
196 G4String error_msg = "Cannot open file: " + name;
197 G4Exception("G4STRead::ReadGeom()", "ReadError",
198 FatalException, error_msg);
199 }
200
201 tessellatedList.clear();
202 volumeMap.clear();
203 std::string line;
204
205 while (getline(GeomFile,line))
206 {
207 if (line[0] == 'f') { TessellatedRead(line); } else
208 if (line[0] == 'p') { FacetRead(line); }
209 }
210
211 if (tessellatedList.size()>0) // Finish the last solid!
212 {
213 tessellatedList.back()->SetSolidClosed(true);
214 }
215
216 G4cout << "G4STRead: Reading '" << name << "' done." << G4endl;
217}
218
219void G4STRead::ReadTree(const G4String& name)
220{
221 G4cout << "G4STRead: Reading '" << name << "'..." << G4endl;
222
223 std::ifstream TreeFile(name);
224
225 if (!TreeFile)
226 {
227 G4String error_msg = "Cannot open file: " + name;
228 G4Exception("G4STRead::ReadTree()", "ReadError",
229 FatalException, error_msg);
230 }
231
232 std::string line;
233
234 while (getline(TreeFile,line))
235 {
236 if (line[0] == 'g') { PhysvolRead(line); }
237 }
238
239 G4cout << "G4STRead: Reading '" << name << "' done." << G4endl;
240}
241
242G4LogicalVolume*
243G4STRead::Read(const G4String& name, G4Material* mediumMaterial,
244 G4Material* solidMaterial)
245{
246 if (mediumMaterial == 0)
247 {
248 G4Exception("G4STRead::Read()", "InvalidSetup", FatalException,
249 "Pointer to medium material is not valid!");
250 }
251 if (solidMaterial == 0)
252 {
253 G4Exception("G4STRead::Read()", "InvalidSetup", FatalException,
254 "Pointer to solid material is not valid!");
255 }
256
257 solid_material = solidMaterial;
258
259 world_box = new G4Box("TessellatedWorldBox",kInfinity,kInfinity,kInfinity);
260 // We don't know the extent of the world yet!
261 world_volume = new G4LogicalVolume(world_box, mediumMaterial,
262 "TessellatedWorldLV", 0, 0, 0);
263 world_extent = G4ThreeVector(0,0,0);
264
265 ReadGeom(name+".geom");
266 ReadTree(name+".tree");
267
268 // Now setting the world extent ...
269 //
270 if (world_box->GetXHalfLength() > world_extent.x())
271 { world_box->SetXHalfLength(world_extent.x()); }
272 if (world_box->GetYHalfLength() > world_extent.y())
273 { world_box->SetYHalfLength(world_extent.y()); }
274 if (world_box->GetZHalfLength() > world_extent.z())
275 { world_box->SetZHalfLength(world_extent.z()); }
276
277 return world_volume;
278}
Note: See TracBrowser for help on using the repository browser.