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: testG4Navigator4.cc,v 1.5 2006/06/29 18:37:23 gunter Exp $ |
---|
28 | // GEANT4 tag $Name: geant4-09-04-beta-cand-01 $ |
---|
29 | // |
---|
30 | // |
---|
31 | // |
---|
32 | // Locate & Step within simple replicated geometry, both |
---|
33 | // with and without voxels. |
---|
34 | |
---|
35 | #include <assert.h> |
---|
36 | #include "ApproxEqual.hh" |
---|
37 | |
---|
38 | // Global defs |
---|
39 | #include "globals.hh" |
---|
40 | |
---|
41 | #include "G4Navigator.hh" |
---|
42 | |
---|
43 | #include "G4LogicalVolume.hh" |
---|
44 | #include "G4VPhysicalVolume.hh" |
---|
45 | #include "G4PVPlacement.hh" |
---|
46 | #include "G4PVReplica.hh" |
---|
47 | #include "G4Box.hh" |
---|
48 | |
---|
49 | #include "G4GeometryManager.hh" |
---|
50 | #include "G4PhysicalVolumeStore.hh" |
---|
51 | |
---|
52 | #include "G4RotationMatrix.hh" |
---|
53 | #include "G4ThreeVector.hh" |
---|
54 | |
---|
55 | G4VPhysicalVolume* BuildGeometry() |
---|
56 | { |
---|
57 | |
---|
58 | G4Box *myBigBox= new G4Box ("cube",50,50,50); |
---|
59 | G4Box *myBox=new G4Box("large cube",25,25,25); |
---|
60 | G4Box *mySlab=new G4Box("slab",6.25,25,25); |
---|
61 | G4Box *myBox2=new G4Box("small cube",10,10,10); |
---|
62 | G4Box *mySlab2=new G4Box("small slab",10,2.5,10); |
---|
63 | G4Box *mySlab3=new G4Box("oblong",2.5,2.5,10); |
---|
64 | |
---|
65 | G4Box *myTargetBox=new G4Box ("small cube",2,2,2); |
---|
66 | |
---|
67 | G4LogicalVolume *worldLog=new G4LogicalVolume(myBigBox,0, |
---|
68 | "World",0,0,0); |
---|
69 | // Logical with no material,field, |
---|
70 | // sensitive detector or user limits |
---|
71 | |
---|
72 | G4PVPlacement *worldPhys=new G4PVPlacement(0,G4ThreeVector(0,0,0), |
---|
73 | "World",worldLog, |
---|
74 | 0,false,0); |
---|
75 | // Note: no mother pointer set |
---|
76 | |
---|
77 | // Box containing x replicas centred on origin |
---|
78 | G4LogicalVolume *boxLog=new G4LogicalVolume(myBox,0, |
---|
79 | "Container",0,0,0); |
---|
80 | |
---|
81 | G4PVPlacement *boxPhys=new G4PVPlacement(0,G4ThreeVector(0,0,0), |
---|
82 | "Container",boxLog, |
---|
83 | worldPhys,false,0); |
---|
84 | G4LogicalVolume *repLog=new G4LogicalVolume(mySlab,0, |
---|
85 | "Slab",0,0,0); |
---|
86 | // G4PVReplica *slabPhys= |
---|
87 | new G4PVReplica("Slab", |
---|
88 | repLog,boxPhys, |
---|
89 | kXAxis,4,12.5); |
---|
90 | |
---|
91 | // Box containing y then x replicas centred on 0,0,35 (touches x replica |
---|
92 | // box (above) |
---|
93 | |
---|
94 | G4LogicalVolume *box2Log=new G4LogicalVolume(myBox2,0, |
---|
95 | "Container2",0,0,0); |
---|
96 | |
---|
97 | G4PVPlacement *box2Phys=new G4PVPlacement(0,G4ThreeVector(0,0,35), |
---|
98 | "Container2",box2Log, |
---|
99 | worldPhys,false,0); |
---|
100 | G4LogicalVolume *rep2Log=new G4LogicalVolume(mySlab2,0, |
---|
101 | "Slab2",0,0,0); |
---|
102 | G4PVReplica *slab2Phys=new G4PVReplica("Slab2", |
---|
103 | rep2Log,box2Phys, |
---|
104 | kYAxis,4,5); |
---|
105 | G4LogicalVolume *rep3Log=new G4LogicalVolume(mySlab3,0, |
---|
106 | "Slab3",0,0,0); |
---|
107 | // G4PVReplica *slab3Phys= |
---|
108 | new G4PVReplica("Slab3", |
---|
109 | rep3Log,slab2Phys, |
---|
110 | kXAxis,4,5); |
---|
111 | |
---|
112 | |
---|
113 | // Box containing y then x replicas centred (35,35,35) |
---|
114 | // (touches x replica box at one vertex) |
---|
115 | // Cube positioned at centre of x replicas |
---|
116 | |
---|
117 | G4LogicalVolume *box3Log=new G4LogicalVolume(myBox2,0, |
---|
118 | "Container3",0,0,0); |
---|
119 | |
---|
120 | G4RotationMatrix *rot=new G4RotationMatrix(); |
---|
121 | rot->rotateZ(pi/2); |
---|
122 | G4PVPlacement *box3Phys=new G4PVPlacement(rot,G4ThreeVector(35,35,35), |
---|
123 | "Container3",box3Log, |
---|
124 | worldPhys,false,0); |
---|
125 | G4LogicalVolume *rep4Log=new G4LogicalVolume(mySlab2,0, |
---|
126 | "Slab4",0,0,0); |
---|
127 | G4PVReplica *slab4Phys=new G4PVReplica("Slab4", |
---|
128 | rep4Log,box3Phys, |
---|
129 | kYAxis,4,5); |
---|
130 | G4LogicalVolume *rep5Log=new G4LogicalVolume(mySlab3,0, |
---|
131 | "Slab5",0,0,0); |
---|
132 | G4PVReplica *slab5Phys=new G4PVReplica("Slab5", |
---|
133 | rep5Log,slab4Phys, |
---|
134 | kXAxis,4,5); |
---|
135 | G4LogicalVolume *targetLog=new G4LogicalVolume(myTargetBox,0, |
---|
136 | "Target",0,0,0); |
---|
137 | // G4PVPlacement *targetPhys= |
---|
138 | new G4PVPlacement(0,G4ThreeVector(0,0,0), |
---|
139 | "Target",targetLog, |
---|
140 | slab5Phys,false,0); |
---|
141 | |
---|
142 | return worldPhys; |
---|
143 | } |
---|
144 | |
---|
145 | |
---|
146 | // |
---|
147 | // Test LocateGlobalPointAndSetup |
---|
148 | // |
---|
149 | G4bool testG4NavigatorLocate(G4VPhysicalVolume *pTopNode) |
---|
150 | { |
---|
151 | MyNavigator myNav; |
---|
152 | G4VPhysicalVolume *located; |
---|
153 | myNav.SetWorldVolume(pTopNode); |
---|
154 | |
---|
155 | assert(!myNav.LocateGlobalPointAndSetup(G4ThreeVector(kInfinity,0,0),0,false)); |
---|
156 | located=myNav.LocateGlobalPointAndSetup(G4ThreeVector(30,0,0),0,false); |
---|
157 | assert(located->GetName()=="World"); |
---|
158 | located=myNav.LocateGlobalPointAndSetup(G4ThreeVector(1,0,0),0,false); |
---|
159 | assert(located->GetName()=="Slab"); |
---|
160 | located=myNav.LocateGlobalPointAndSetup(G4ThreeVector(2,0,0)); |
---|
161 | assert(located->GetName()=="Slab"); |
---|
162 | located=myNav.LocateGlobalPointAndSetup(G4ThreeVector(30,0,0)); |
---|
163 | assert(located->GetName()=="World"); |
---|
164 | |
---|
165 | |
---|
166 | located=myNav.LocateGlobalPointAndSetup(G4ThreeVector(-19.75,10,-10)); |
---|
167 | assert(located->GetName()=="Slab"); |
---|
168 | assert(ApproxEqual(myNav.CurrentLocalCoordinate(), |
---|
169 | G4ThreeVector(-1,10,-10))); |
---|
170 | located=myNav.LocateGlobalPointAndSetup(G4ThreeVector(-7.25,10,-10)); |
---|
171 | assert(located->GetName()=="Slab"); |
---|
172 | assert(ApproxEqual(myNav.CurrentLocalCoordinate(), |
---|
173 | G4ThreeVector(-1,10,-10))); |
---|
174 | located=myNav.LocateGlobalPointAndSetup(G4ThreeVector(5.25,10,-10)); |
---|
175 | assert(located->GetName()=="Slab"); |
---|
176 | assert(ApproxEqual(myNav.CurrentLocalCoordinate(), |
---|
177 | G4ThreeVector(-1,10,-10))); |
---|
178 | located=myNav.LocateGlobalPointAndSetup(G4ThreeVector(17.75,10,-10)); |
---|
179 | assert(located->GetName()=="Slab"); |
---|
180 | assert(ApproxEqual(myNav.CurrentLocalCoordinate(), |
---|
181 | G4ThreeVector(-1,10,-10))); |
---|
182 | |
---|
183 | located=myNav.LocateGlobalPointAndSetup(G4ThreeVector(-7.5,7.5,45),0,false); |
---|
184 | assert(located->GetName()=="Slab3"); |
---|
185 | assert(ApproxEqual(myNav.CurrentLocalCoordinate(), |
---|
186 | G4ThreeVector(0,0,10))); |
---|
187 | located=myNav.LocateGlobalPointAndSetup(G4ThreeVector(-1.5,7.5,45),0,false); |
---|
188 | assert(located->GetName()=="Slab3"); |
---|
189 | assert(ApproxEqual(myNav.CurrentLocalCoordinate(), |
---|
190 | G4ThreeVector(1,0,10))); |
---|
191 | located=myNav.LocateGlobalPointAndSetup(G4ThreeVector(4.5,7.5,45),0,false); |
---|
192 | assert(located->GetName()=="Slab3"); |
---|
193 | assert(ApproxEqual(myNav.CurrentLocalCoordinate(), |
---|
194 | G4ThreeVector(2,0,10))); |
---|
195 | located=myNav.LocateGlobalPointAndSetup(G4ThreeVector(10,7.5,45),0,false); |
---|
196 | assert(located->GetName()=="Slab3"); |
---|
197 | assert(ApproxEqual(myNav.CurrentLocalCoordinate(), |
---|
198 | G4ThreeVector(2.5,0,10))); |
---|
199 | |
---|
200 | |
---|
201 | located=myNav.LocateGlobalPointAndSetup(G4ThreeVector(-7.5,-7.5,45)); |
---|
202 | assert(located->GetName()=="Slab3"); |
---|
203 | assert(ApproxEqual(myNav.CurrentLocalCoordinate(), |
---|
204 | G4ThreeVector(0,0,10))); |
---|
205 | located=myNav.LocateGlobalPointAndSetup(G4ThreeVector(-1.5,-1.5,45)); |
---|
206 | assert(located->GetName()=="Slab3"); |
---|
207 | assert(ApproxEqual(myNav.CurrentLocalCoordinate(), |
---|
208 | G4ThreeVector(1,1,10))); |
---|
209 | located=myNav.LocateGlobalPointAndSetup(G4ThreeVector(4.5,4.5,45)); |
---|
210 | assert(located->GetName()=="Slab3"); |
---|
211 | assert(ApproxEqual(myNav.CurrentLocalCoordinate(), |
---|
212 | G4ThreeVector(2,2,10))); |
---|
213 | located=myNav.LocateGlobalPointAndSetup(G4ThreeVector(10,10,45)); |
---|
214 | assert(located->GetName()=="Slab3"); |
---|
215 | assert(ApproxEqual(myNav.CurrentLocalCoordinate(), |
---|
216 | G4ThreeVector(2.5,2.5,10))); |
---|
217 | |
---|
218 | // Check found to be inside daughter volumes of replicas |
---|
219 | located=myNav.LocateGlobalPointAndSetup(G4ThreeVector(36,36,36),0,false); |
---|
220 | // G4cout << "At " << G4ThreeVector(36,36,36) << G4endl << myNav; |
---|
221 | assert(located->GetName()=="Target"); |
---|
222 | assert(ApproxEqual(myNav.CurrentLocalCoordinate(), |
---|
223 | G4ThreeVector(1.5,-1.5,1))); |
---|
224 | return true; |
---|
225 | } |
---|
226 | |
---|
227 | // |
---|
228 | // Test ComputeStep |
---|
229 | // |
---|
230 | G4bool testG4NavigatorSteps(G4VPhysicalVolume *pTopNode) |
---|
231 | { |
---|
232 | MyNavigator myNav; |
---|
233 | G4VPhysicalVolume *located; |
---|
234 | G4double Step,physStep,safety; |
---|
235 | G4ThreeVector pos,dir,origin,xHat(1,0,0),yHat(0,1,0),zHat(0,0,1); |
---|
236 | G4ThreeVector mxHat(-1,0,0),myHat(0,-1,0),mzHat(0,0,-1); |
---|
237 | myNav.SetWorldVolume(pTopNode); |
---|
238 | |
---|
239 | pos=G4ThreeVector(1,0,0); |
---|
240 | dir=yHat; |
---|
241 | physStep=kInfinity; |
---|
242 | located=myNav.LocateGlobalPointAndSetup(pos); |
---|
243 | assert(located->GetName()=="Slab"); |
---|
244 | Step=myNav.ComputeStep(pos,dir,physStep,safety); |
---|
245 | assert(ApproxEqual(Step,25)); |
---|
246 | assert(ApproxEqual(safety,1)); |
---|
247 | pos+=Step*dir; |
---|
248 | myNav.SetGeometricallyLimitedStep(); |
---|
249 | located=myNav.LocateGlobalPointAndSetup(pos); |
---|
250 | assert(located->GetName()=="World"); |
---|
251 | Step=myNav.ComputeStep(pos,dir,physStep,safety); |
---|
252 | assert(ApproxEqual(Step,25)); |
---|
253 | assert(safety==0); |
---|
254 | |
---|
255 | pos=G4ThreeVector(1,1,-50); |
---|
256 | dir=zHat; |
---|
257 | located=myNav.LocateGlobalPointAndSetup(pos); |
---|
258 | assert(located->GetName()=="World"); |
---|
259 | Step=myNav.ComputeStep(pos,dir,physStep,safety); |
---|
260 | assert(ApproxEqual(Step,25)); |
---|
261 | assert(ApproxEqual(safety,0)); |
---|
262 | pos+=Step*dir; |
---|
263 | myNav.SetGeometricallyLimitedStep(); |
---|
264 | located=myNav.LocateGlobalPointAndSetup(pos); |
---|
265 | // G4cout << "At " << pos << G4endl << myNav; |
---|
266 | assert(located->GetName()=="Slab"); |
---|
267 | Step=myNav.ComputeStep(pos,dir,physStep,safety); |
---|
268 | assert(ApproxEqual(Step,50)); |
---|
269 | assert(ApproxEqual(safety,0)); |
---|
270 | pos+=Step*dir; |
---|
271 | myNav.SetGeometricallyLimitedStep(); |
---|
272 | located=myNav.LocateGlobalPointAndSetup(pos); |
---|
273 | // G4cout << "At " << pos << G4endl << myNav; |
---|
274 | assert(located->GetName()=="Slab3"); |
---|
275 | Step=myNav.ComputeStep(pos,dir,physStep,safety); |
---|
276 | assert(ApproxEqual(Step,20)); |
---|
277 | assert(ApproxEqual(safety,0)); |
---|
278 | |
---|
279 | Step=10; |
---|
280 | pos+=Step*dir; |
---|
281 | located=myNav.LocateGlobalPointAndSetup(pos); |
---|
282 | // G4cout << "At " << pos << G4endl << myNav; |
---|
283 | assert(located->GetName()=="Slab3"); |
---|
284 | |
---|
285 | dir=myHat; |
---|
286 | Step=myNav.ComputeStep(pos,dir,physStep,safety); |
---|
287 | assert(ApproxEqual(Step,1)); |
---|
288 | assert(ApproxEqual(safety,1)); |
---|
289 | pos+=Step*dir; |
---|
290 | myNav.SetGeometricallyLimitedStep(); |
---|
291 | located=myNav.LocateGlobalPointAndSetup(pos); |
---|
292 | // G4cout << "At " << pos << G4endl << myNav; |
---|
293 | assert(located->GetName()=="Slab3"); |
---|
294 | Step=myNav.ComputeStep(pos,dir,physStep,safety); |
---|
295 | assert(ApproxEqual(Step,5)); |
---|
296 | assert(ApproxEqual(safety,0)); |
---|
297 | pos+=Step*dir; |
---|
298 | myNav.SetGeometricallyLimitedStep(); |
---|
299 | located=myNav.LocateGlobalPointAndSetup(pos); |
---|
300 | // G4cout << "At " << pos << G4endl << myNav; |
---|
301 | assert(located->GetName()=="Slab3"); |
---|
302 | Step=myNav.ComputeStep(pos,dir,physStep,safety); |
---|
303 | assert(ApproxEqual(Step,5)); |
---|
304 | assert(ApproxEqual(safety,0)); |
---|
305 | pos+=Step*dir; |
---|
306 | myNav.SetGeometricallyLimitedStep(); |
---|
307 | located=myNav.LocateGlobalPointAndSetup(pos); |
---|
308 | // G4cout << "At " << pos << G4endl << myNav; |
---|
309 | assert(located->GetName()=="World"); |
---|
310 | |
---|
311 | |
---|
312 | pos=G4ThreeVector(36,36,36); |
---|
313 | dir=yHat; |
---|
314 | physStep=kInfinity; |
---|
315 | located=myNav.LocateGlobalPointAndSetup(pos); |
---|
316 | // G4cout << "At " << pos << G4endl << myNav; |
---|
317 | assert(located->GetName()=="Target"); |
---|
318 | Step=myNav.ComputeStep(pos,dir,physStep,safety); |
---|
319 | assert(ApproxEqual(Step,3.5)); |
---|
320 | assert(ApproxEqual(safety,0.5)); |
---|
321 | |
---|
322 | pos+=Step*dir; |
---|
323 | myNav.SetGeometricallyLimitedStep(); |
---|
324 | located=myNav.LocateGlobalPointAndSetup(pos); |
---|
325 | // G4cout << "At " << pos << G4endl << myNav; |
---|
326 | assert(located->GetName()=="Slab5"); |
---|
327 | Step=myNav.ComputeStep(pos,dir,physStep,safety); |
---|
328 | assert(ApproxEqual(Step,0.5)); |
---|
329 | assert(ApproxEqual(safety,0)); |
---|
330 | |
---|
331 | pos+=Step*dir; |
---|
332 | myNav.SetGeometricallyLimitedStep(); |
---|
333 | located=myNav.LocateGlobalPointAndSetup(pos); |
---|
334 | // G4cout << "At " << pos << G4endl << myNav; |
---|
335 | assert(located->GetName()=="Slab5"); |
---|
336 | Step=myNav.ComputeStep(pos,dir,physStep,safety); |
---|
337 | assert(ApproxEqual(Step,0.5)); |
---|
338 | assert(ApproxEqual(safety,0)); |
---|
339 | |
---|
340 | pos+=Step*dir; |
---|
341 | myNav.SetGeometricallyLimitedStep(); |
---|
342 | located=myNav.LocateGlobalPointAndSetup(pos); |
---|
343 | // G4cout << "At " << pos << G4endl << myNav; |
---|
344 | assert(located->GetName()=="Target"); |
---|
345 | Step=myNav.ComputeStep(pos,dir,physStep,safety); |
---|
346 | assert(ApproxEqual(Step,4)); |
---|
347 | assert(ApproxEqual(safety,0)); |
---|
348 | |
---|
349 | pos+=Step*dir; |
---|
350 | myNav.SetGeometricallyLimitedStep(); |
---|
351 | located=myNav.LocateGlobalPointAndSetup(pos); |
---|
352 | // G4cout << "At " << pos << G4endl << myNav; |
---|
353 | assert(located->GetName()=="Slab5"); |
---|
354 | assert(ApproxEqual(myNav.CurrentLocalCoordinate(), |
---|
355 | G4ThreeVector(-2,-1.5,1))); |
---|
356 | Step=myNav.ComputeStep(pos,dir,physStep,safety); |
---|
357 | assert(ApproxEqual(Step,0.5)); |
---|
358 | assert(ApproxEqual(safety,0)); |
---|
359 | |
---|
360 | pos+=Step*dir; |
---|
361 | myNav.SetGeometricallyLimitedStep(); |
---|
362 | located=myNav.LocateGlobalPointAndSetup(pos); |
---|
363 | assert(located->GetName()=="World"); |
---|
364 | Step=myNav.ComputeStep(pos,dir,physStep,safety); |
---|
365 | assert(ApproxEqual(Step,5)); |
---|
366 | assert(ApproxEqual(safety,0)); |
---|
367 | |
---|
368 | return true; |
---|
369 | } |
---|
370 | |
---|
371 | int main() |
---|
372 | { |
---|
373 | G4VPhysicalVolume *myTopNode; |
---|
374 | myTopNode=BuildGeometry(); // Build the geometry |
---|
375 | G4GeometryManager::GetInstance()->CloseGeometry(false); |
---|
376 | testG4NavigatorLocate(myTopNode); |
---|
377 | testG4NavigatorSteps(myTopNode); |
---|
378 | // Repeat tests but with full voxels |
---|
379 | G4GeometryManager::GetInstance()->OpenGeometry(); |
---|
380 | G4GeometryManager::GetInstance()->CloseGeometry(true); |
---|
381 | testG4NavigatorLocate(myTopNode); |
---|
382 | testG4NavigatorSteps(myTopNode); |
---|
383 | |
---|
384 | G4GeometryManager::GetInstance()->OpenGeometry(); |
---|
385 | G4PhysicalVolumeStore *ps=G4PhysicalVolumeStore::GetInstance(); |
---|
386 | for (G4int i=ps->size()-1;i>=0;i--) |
---|
387 | { |
---|
388 | // Delete any rotation matrices |
---|
389 | delete (*ps)[i]->GetRotation(); |
---|
390 | } |
---|
391 | return 0; |
---|
392 | } |
---|