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.6 2010/11/02 10:39:27 gcosmo Exp $ |
---|
27 | // GEANT4 tag $Name: gdml-V09-03-09 $ |
---|
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 | |
---|
47 | void 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 | |
---|
68 | void 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 | |
---|
112 | void 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 | std::string::size_type idx = name.rfind("_"); |
---|
126 | if (idx!=std::string::npos) |
---|
127 | { |
---|
128 | name.resize(idx); |
---|
129 | } |
---|
130 | else |
---|
131 | { |
---|
132 | G4Exception("G4STRead::PhysvolRead()", "ReadError", |
---|
133 | FatalException, "Invalid input stream!"); |
---|
134 | return; |
---|
135 | } |
---|
136 | |
---|
137 | G4cout << "G4STRead: Placing tessellated solid: " << name << G4endl; |
---|
138 | |
---|
139 | G4TessellatedSolid* tessellated = 0; |
---|
140 | |
---|
141 | for (size_t i=0; i<tessellatedList.size(); i++) |
---|
142 | { // Find the volume for this physvol! |
---|
143 | if (tessellatedList[i]->GetName() == G4String(name)) |
---|
144 | { |
---|
145 | tessellated = tessellatedList[i]; |
---|
146 | break; |
---|
147 | } |
---|
148 | } |
---|
149 | |
---|
150 | if (tessellated == 0) |
---|
151 | { |
---|
152 | G4String error_msg = "Referenced solid '" + name + "' not found!"; |
---|
153 | G4Exception("G4STRead::PhysvolRead()", "ReadError", |
---|
154 | FatalException, error_msg); |
---|
155 | } |
---|
156 | if (volumeMap.find(tessellated) == volumeMap.end()) |
---|
157 | { |
---|
158 | G4String error_msg = "Referenced solid '" + name |
---|
159 | + "' is not associated with a logical volume!"; |
---|
160 | G4Exception("G4STRead::PhysvolRead()", "InvalidSetup", |
---|
161 | FatalException, error_msg); |
---|
162 | } |
---|
163 | const G4RotationMatrix rot(G4ThreeVector(r1,r2,r3), |
---|
164 | G4ThreeVector(r4,r5,r6), |
---|
165 | G4ThreeVector(r7,r8,r9)); |
---|
166 | const G4ThreeVector pos(pX,pY,pZ); |
---|
167 | |
---|
168 | new G4PVPlacement(G4Transform3D(rot.inverse(),pos), |
---|
169 | volumeMap[tessellated], name+"_PV", world_volume, 0, 0); |
---|
170 | // Note: INVERSE of rotation is needed!!! |
---|
171 | |
---|
172 | G4double minx,miny,minz; |
---|
173 | G4double maxx,maxy,maxz; |
---|
174 | const G4VoxelLimits limits; |
---|
175 | |
---|
176 | tessellated->CalculateExtent(kXAxis,limits, |
---|
177 | G4AffineTransform(rot,pos),minx,maxx); |
---|
178 | tessellated->CalculateExtent(kYAxis,limits, |
---|
179 | G4AffineTransform(rot,pos),miny,maxy); |
---|
180 | tessellated->CalculateExtent(kZAxis,limits, |
---|
181 | G4AffineTransform(rot,pos),minz,maxz); |
---|
182 | |
---|
183 | if (world_extent.x() < std::fabs(minx)) |
---|
184 | { world_extent.setX(std::fabs(minx)); } |
---|
185 | if (world_extent.y() < std::fabs(miny)) |
---|
186 | { world_extent.setY(std::fabs(miny)); } |
---|
187 | if (world_extent.z() < std::fabs(minz)) |
---|
188 | { world_extent.setZ(std::fabs(minz)); } |
---|
189 | if (world_extent.x() < std::fabs(maxx)) |
---|
190 | { world_extent.setX(std::fabs(maxx)); } |
---|
191 | if (world_extent.y() < std::fabs(maxy)) |
---|
192 | { world_extent.setY(std::fabs(maxy)); } |
---|
193 | if (world_extent.z() < std::fabs(maxz)) |
---|
194 | { world_extent.setZ(std::fabs(maxz)); } |
---|
195 | } |
---|
196 | |
---|
197 | void G4STRead::ReadGeom(const G4String& name) |
---|
198 | { |
---|
199 | G4cout << "G4STRead: Reading '" << name << "'..." << G4endl; |
---|
200 | |
---|
201 | std::ifstream GeomFile(name); |
---|
202 | |
---|
203 | if (!GeomFile) |
---|
204 | { |
---|
205 | G4String error_msg = "Cannot open file: " + name; |
---|
206 | G4Exception("G4STRead::ReadGeom()", "ReadError", |
---|
207 | FatalException, error_msg); |
---|
208 | } |
---|
209 | |
---|
210 | tessellatedList.clear(); |
---|
211 | volumeMap.clear(); |
---|
212 | std::string line; |
---|
213 | |
---|
214 | while (getline(GeomFile,line)) |
---|
215 | { |
---|
216 | if (line[0] == 'f') { TessellatedRead(line); } else |
---|
217 | if (line[0] == 'p') { FacetRead(line); } |
---|
218 | } |
---|
219 | |
---|
220 | if (tessellatedList.size()>0) // Finish the last solid! |
---|
221 | { |
---|
222 | tessellatedList.back()->SetSolidClosed(true); |
---|
223 | } |
---|
224 | |
---|
225 | G4cout << "G4STRead: Reading '" << name << "' done." << G4endl; |
---|
226 | } |
---|
227 | |
---|
228 | void G4STRead::ReadTree(const G4String& name) |
---|
229 | { |
---|
230 | G4cout << "G4STRead: Reading '" << name << "'..." << G4endl; |
---|
231 | |
---|
232 | std::ifstream TreeFile(name); |
---|
233 | |
---|
234 | if (!TreeFile) |
---|
235 | { |
---|
236 | G4String error_msg = "Cannot open file: " + name; |
---|
237 | G4Exception("G4STRead::ReadTree()", "ReadError", |
---|
238 | FatalException, error_msg); |
---|
239 | } |
---|
240 | |
---|
241 | std::string line; |
---|
242 | |
---|
243 | while (getline(TreeFile,line)) |
---|
244 | { |
---|
245 | if (line[0] == 'g') { PhysvolRead(line); } |
---|
246 | } |
---|
247 | |
---|
248 | G4cout << "G4STRead: Reading '" << name << "' done." << G4endl; |
---|
249 | } |
---|
250 | |
---|
251 | G4LogicalVolume* |
---|
252 | G4STRead::Read(const G4String& name, G4Material* mediumMaterial, |
---|
253 | G4Material* solidMaterial) |
---|
254 | { |
---|
255 | if (mediumMaterial == 0) |
---|
256 | { |
---|
257 | G4Exception("G4STRead::Read()", "InvalidSetup", FatalException, |
---|
258 | "Pointer to medium material is not valid!"); |
---|
259 | } |
---|
260 | if (solidMaterial == 0) |
---|
261 | { |
---|
262 | G4Exception("G4STRead::Read()", "InvalidSetup", FatalException, |
---|
263 | "Pointer to solid material is not valid!"); |
---|
264 | } |
---|
265 | |
---|
266 | solid_material = solidMaterial; |
---|
267 | |
---|
268 | world_box = new G4Box("TessellatedWorldBox",kInfinity,kInfinity,kInfinity); |
---|
269 | // We don't know the extent of the world yet! |
---|
270 | world_volume = new G4LogicalVolume(world_box, mediumMaterial, |
---|
271 | "TessellatedWorldLV", 0, 0, 0); |
---|
272 | world_extent = G4ThreeVector(0,0,0); |
---|
273 | |
---|
274 | ReadGeom(name+".geom"); |
---|
275 | ReadTree(name+".tree"); |
---|
276 | |
---|
277 | // Now setting the world extent ... |
---|
278 | // |
---|
279 | if (world_box->GetXHalfLength() > world_extent.x()) |
---|
280 | { world_box->SetXHalfLength(world_extent.x()); } |
---|
281 | if (world_box->GetYHalfLength() > world_extent.y()) |
---|
282 | { world_box->SetYHalfLength(world_extent.y()); } |
---|
283 | if (world_box->GetZHalfLength() > world_extent.z()) |
---|
284 | { world_box->SetZHalfLength(world_extent.z()); } |
---|
285 | |
---|
286 | return world_volume; |
---|
287 | } |
---|