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