[1316] | 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: testG4Navigator2.cc,v 1.6 2007/03/23 18:33:08 japost Exp $ |
---|
| 28 | // GEANT4 tag $Name: geant4-09-04-beta-cand-01 $ |
---|
| 29 | // |
---|
| 30 | // |
---|
| 31 | // Locate & Step within simple rotated boxlike geometry, both |
---|
| 32 | // with and without voxels. |
---|
| 33 | |
---|
| 34 | #include <assert.h> |
---|
| 35 | #include "ApproxEqual.hh" |
---|
| 36 | |
---|
| 37 | // Global defs |
---|
| 38 | #include "globals.hh" |
---|
| 39 | |
---|
| 40 | #include "G4Navigator.hh" |
---|
| 41 | |
---|
| 42 | #include "G4LogicalVolume.hh" |
---|
| 43 | #include "G4VPhysicalVolume.hh" |
---|
| 44 | #include "G4PVPlacement.hh" |
---|
| 45 | // #include "G4PVParameterised.hh" |
---|
| 46 | // #include "G4VPVParameterisation.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("cuboid",5,10,15); |
---|
| 60 | G4LogicalVolume *worldLog=new G4LogicalVolume(myBigBox,0, |
---|
| 61 | "World",0,0,0); |
---|
| 62 | // Logical with no material,field, |
---|
| 63 | // sensitive detector or user limits |
---|
| 64 | |
---|
| 65 | G4PVPlacement *worldPhys=new G4PVPlacement(0,G4ThreeVector(0,0,0), |
---|
| 66 | "World",worldLog, |
---|
| 67 | 0,false,0); |
---|
| 68 | // Note: no mother pointer set |
---|
| 69 | |
---|
| 70 | G4LogicalVolume *boxLog=new G4LogicalVolume(myBox,0, |
---|
| 71 | "Crystal Box",0,0,0); |
---|
| 72 | |
---|
| 73 | G4RotationMatrix *rot1=new G4RotationMatrix(); |
---|
| 74 | rot1->rotateZ(pi*0.5); |
---|
| 75 | // G4PVPlacement *boxPhys1= |
---|
| 76 | new G4PVPlacement(rot1,G4ThreeVector(0,0,0), |
---|
| 77 | "Target 1",boxLog, |
---|
| 78 | worldPhys,false,0); |
---|
| 79 | |
---|
| 80 | G4RotationMatrix *rot2=new G4RotationMatrix(); |
---|
| 81 | rot2->rotateX(pi*0.5); |
---|
| 82 | // G4PVPlacement *boxPhys2= |
---|
| 83 | new G4PVPlacement(rot2,G4ThreeVector(-30,0,0), |
---|
| 84 | "Target 2",boxLog, |
---|
| 85 | worldPhys,false,0); |
---|
| 86 | G4RotationMatrix *rot3=new G4RotationMatrix(); |
---|
| 87 | rot3->rotateY(pi*0.5); |
---|
| 88 | // G4PVPlacement *boxPhys3= |
---|
| 89 | new G4PVPlacement(rot3,G4ThreeVector(30,0,0), |
---|
| 90 | "Target 3",boxLog, |
---|
| 91 | worldPhys,false,0); |
---|
| 92 | return worldPhys; |
---|
| 93 | } |
---|
| 94 | |
---|
| 95 | |
---|
| 96 | // |
---|
| 97 | // Test LocateGlobalPointAndSetup |
---|
| 98 | // |
---|
| 99 | G4bool testG4NavigatorLocate(G4VPhysicalVolume *pTopNode) |
---|
| 100 | { |
---|
| 101 | G4Navigator myNav; |
---|
| 102 | G4VPhysicalVolume *located; |
---|
| 103 | myNav.SetWorldVolume(pTopNode); |
---|
| 104 | |
---|
| 105 | assert(!myNav.LocateGlobalPointAndSetup(G4ThreeVector(kInfinity,0,0),0,false)); |
---|
| 106 | located=myNav.LocateGlobalPointAndSetup(G4ThreeVector(0,0,0),0,false); |
---|
| 107 | assert(located->GetName()=="Target 1"); |
---|
| 108 | located=myNav.LocateGlobalPointAndSetup(G4ThreeVector(10,0,0),0,false); |
---|
| 109 | assert(located->GetName()=="Target 1"); |
---|
| 110 | located=myNav.LocateGlobalPointAndSetup(G4ThreeVector(0,10,0),0,false); |
---|
| 111 | assert(located->GetName()=="World"); |
---|
| 112 | located=myNav.LocateGlobalPointAndSetup(G4ThreeVector(0,5,0),0,false); |
---|
| 113 | assert(located->GetName()=="Target 1"); |
---|
| 114 | |
---|
| 115 | located=myNav.LocateGlobalPointAndSetup(G4ThreeVector(-30,0,0),0,false); |
---|
| 116 | assert(located->GetName()=="Target 2"); |
---|
| 117 | located=myNav.LocateGlobalPointAndSetup(G4ThreeVector(-30,10,15),0,false); |
---|
| 118 | assert(located->GetName()=="World"); |
---|
| 119 | located=myNav.LocateGlobalPointAndSetup(G4ThreeVector(-35,15,10),0,false); |
---|
| 120 | assert(located->GetName()=="Target 2"); |
---|
| 121 | located=myNav.LocateGlobalPointAndSetup(G4ThreeVector(-25,-15,-10),0,false); |
---|
| 122 | assert(located->GetName()=="Target 2"); |
---|
| 123 | |
---|
| 124 | located=myNav.LocateGlobalPointAndSetup(G4ThreeVector(30,0,0),0,false); |
---|
| 125 | assert(located->GetName()=="Target 3"); |
---|
| 126 | located=myNav.LocateGlobalPointAndSetup(G4ThreeVector(30,15,5),0,false); |
---|
| 127 | assert(located->GetName()=="World"); |
---|
| 128 | located=myNav.LocateGlobalPointAndSetup(G4ThreeVector(15,10,5),0,false); |
---|
| 129 | assert(located->GetName()=="Target 3"); |
---|
| 130 | located=myNav.LocateGlobalPointAndSetup(G4ThreeVector(45,-10,-5),0,false); |
---|
| 131 | assert(located->GetName()=="Target 3"); |
---|
| 132 | |
---|
| 133 | return true; |
---|
| 134 | } |
---|
| 135 | |
---|
| 136 | // Test ComputeStep |
---|
| 137 | // |
---|
| 138 | G4bool testG4NavigatorSteps(G4VPhysicalVolume *pTopNode) |
---|
| 139 | { |
---|
| 140 | MyNavigator myNav; |
---|
| 141 | G4VPhysicalVolume *located; |
---|
| 142 | G4double Step,physStep,safety; |
---|
| 143 | G4ThreeVector pos,dir,origin,xHat(1,0,0),yHat(0,1,0),zHat(0,0,1); |
---|
| 144 | G4ThreeVector mxHat(-1,0,0),myHat(0,-1,0),mzHat(0,0,-1); |
---|
| 145 | myNav.SetWorldVolume(pTopNode); |
---|
| 146 | |
---|
| 147 | pos=origin; |
---|
| 148 | dir=xHat; |
---|
| 149 | located=myNav.LocateGlobalPointAndSetup(pos,0,false); |
---|
| 150 | assert(located->GetName()=="Target 1"); |
---|
| 151 | physStep=kInfinity; |
---|
| 152 | Step=myNav.ComputeStep(origin,dir,physStep,safety); |
---|
| 153 | assert(ApproxEqual(Step,10)); |
---|
| 154 | |
---|
| 155 | pos+=Step*dir; |
---|
| 156 | myNav.SetGeometricallyLimitedStep(); |
---|
| 157 | located=myNav.LocateGlobalPointAndSetup(pos); |
---|
| 158 | assert(located->GetName()=="World"); |
---|
| 159 | Step=myNav.ComputeStep(pos,dir,physStep,safety); |
---|
| 160 | assert(ApproxEqual(Step,5)); |
---|
| 161 | |
---|
| 162 | pos+=Step*dir; |
---|
| 163 | myNav.SetGeometricallyLimitedStep(); |
---|
| 164 | located=myNav.LocateGlobalPointAndSetup(pos); |
---|
| 165 | assert(located->GetName()=="Target 3"); |
---|
| 166 | Step=myNav.ComputeStep(pos,dir,physStep,safety); |
---|
| 167 | assert(ApproxEqual(Step,30)); |
---|
| 168 | |
---|
| 169 | pos+=Step*dir; |
---|
| 170 | myNav.SetGeometricallyLimitedStep(); |
---|
| 171 | located=myNav.LocateGlobalPointAndSetup(pos); |
---|
| 172 | assert(located->GetName()=="World"); |
---|
| 173 | Step=myNav.ComputeStep(pos,dir,physStep,safety); |
---|
| 174 | assert(ApproxEqual(Step,5)); |
---|
| 175 | pos+=Step*dir; |
---|
| 176 | |
---|
| 177 | myNav.SetGeometricallyLimitedStep(); |
---|
| 178 | located=myNav.LocateGlobalPointAndSetup(pos); |
---|
| 179 | assert(located==0); |
---|
| 180 | return true; |
---|
| 181 | } |
---|
| 182 | |
---|
| 183 | // |
---|
| 184 | // |
---|
| 185 | // |
---|
| 186 | G4double |
---|
| 187 | CheckNextStep_And_TakeIt( G4Navigator &Navigator, |
---|
| 188 | const G4ThreeVector &position, |
---|
| 189 | const G4ThreeVector &direction, |
---|
| 190 | G4double proposedStep, |
---|
| 191 | G4double &resNewSafety, // Output !! |
---|
| 192 | G4double expectedStep) |
---|
| 193 | { |
---|
| 194 | // G4double Step1= 0.0, Step2= 0.0; |
---|
| 195 | G4double Step= 0.0; |
---|
| 196 | G4double safety1=0.0, safety2=0.0; |
---|
| 197 | // Check new ComputeStep method |
---|
| 198 | Step=Navigator.CheckNextStep(position,direction,proposedStep,safety1); |
---|
| 199 | assert(ApproxEqual(Step,expectedStep)); |
---|
| 200 | |
---|
| 201 | Step=Navigator.ComputeStep(position,direction,proposedStep,safety2); |
---|
| 202 | assert(ApproxEqual(Step,expectedStep)); |
---|
| 203 | |
---|
| 204 | assert(ApproxEqual(safety1,safety2)); |
---|
| 205 | |
---|
| 206 | resNewSafety= safety2; |
---|
| 207 | return Step; |
---|
| 208 | } |
---|
| 209 | |
---|
| 210 | // |
---|
| 211 | |
---|
| 212 | // |
---|
| 213 | // Test CheckNextSteps |
---|
| 214 | // |
---|
| 215 | G4bool testG4NavigatorCheckNextSteps(G4VPhysicalVolume *pTopNode) |
---|
| 216 | { |
---|
| 217 | MyNavigator myNav; |
---|
| 218 | G4VPhysicalVolume *located; |
---|
| 219 | G4double Step,physStep,safety; |
---|
| 220 | G4ThreeVector pos,dir,origin,xHat(1,0,0),yHat(0,1,0),zHat(0,0,1); |
---|
| 221 | G4ThreeVector mxHat(-1,0,0),myHat(0,-1,0),mzHat(0,0,-1); |
---|
| 222 | myNav.SetWorldVolume(pTopNode); |
---|
| 223 | |
---|
| 224 | G4double expectedStep=0.0; |
---|
| 225 | |
---|
| 226 | pos=origin; |
---|
| 227 | dir=xHat; |
---|
| 228 | located=myNav.LocateGlobalPointAndSetup(pos,0,false); |
---|
| 229 | assert(located->GetName()=="Target 1"); |
---|
| 230 | physStep=kInfinity; |
---|
| 231 | // Step=myNav.ComputeStep(origin,dir,physStep,safety); |
---|
| 232 | // assert(ApproxEqual(Step,10)); |
---|
| 233 | Step= CheckNextStep_And_TakeIt(myNav,pos,dir,physStep,safety,expectedStep=10.0); |
---|
| 234 | pos+=Step*dir; |
---|
| 235 | myNav.SetGeometricallyLimitedStep(); |
---|
| 236 | located=myNav.LocateGlobalPointAndSetup(pos); |
---|
| 237 | assert(located->GetName()=="World"); |
---|
| 238 | // Step=myNav.ComputeStep(pos,dir,physStep,safety); |
---|
| 239 | // assert(ApproxEqual(Step,5)); |
---|
| 240 | Step= CheckNextStep_And_TakeIt(myNav,pos,dir,physStep,safety,expectedStep=5.0); |
---|
| 241 | pos+=Step*dir; |
---|
| 242 | myNav.SetGeometricallyLimitedStep(); |
---|
| 243 | located=myNav.LocateGlobalPointAndSetup(pos); |
---|
| 244 | assert(located->GetName()=="Target 3"); |
---|
| 245 | |
---|
| 246 | // Step=myNav.ComputeStep(pos,dir,physStep,safety); |
---|
| 247 | // assert(ApproxEqual(Step,30)); |
---|
| 248 | Step= CheckNextStep_And_TakeIt(myNav,pos,dir,physStep,safety,expectedStep=30.0); |
---|
| 249 | pos+=Step*dir; |
---|
| 250 | |
---|
| 251 | myNav.SetGeometricallyLimitedStep(); |
---|
| 252 | located=myNav.LocateGlobalPointAndSetup(pos); |
---|
| 253 | assert(located->GetName()=="World"); |
---|
| 254 | |
---|
| 255 | // Step=myNav.ComputeStep(pos,dir,physStep,safety); |
---|
| 256 | // assert(ApproxEqual(Step,5)); |
---|
| 257 | Step= CheckNextStep_And_TakeIt(myNav,pos,dir,physStep,safety,expectedStep=5.0); |
---|
| 258 | // ** New: Check new ComputeStep method |
---|
| 259 | // Step=myNav.CheckNextStep(pos,dir,physStep,safety); |
---|
| 260 | // assert(ApproxEqual(Step,5)); |
---|
| 261 | |
---|
| 262 | pos+=Step*dir; |
---|
| 263 | myNav.SetGeometricallyLimitedStep(); |
---|
| 264 | located=myNav.LocateGlobalPointAndSetup(pos); |
---|
| 265 | assert(located==0); |
---|
| 266 | return true; |
---|
| 267 | } |
---|
| 268 | |
---|
| 269 | int main() |
---|
| 270 | { |
---|
| 271 | G4VPhysicalVolume *myTopNode; |
---|
| 272 | myTopNode=BuildGeometry(); // Build the geometry |
---|
| 273 | G4GeometryManager::GetInstance()->CloseGeometry(false); |
---|
| 274 | testG4NavigatorLocate(myTopNode); |
---|
| 275 | testG4NavigatorSteps(myTopNode); |
---|
| 276 | testG4NavigatorCheckNextSteps(myTopNode); |
---|
| 277 | |
---|
| 278 | // Repeat tests but with full voxels |
---|
| 279 | G4GeometryManager::GetInstance()->OpenGeometry(); |
---|
| 280 | G4GeometryManager::GetInstance()->CloseGeometry(true); |
---|
| 281 | testG4NavigatorLocate(myTopNode); |
---|
| 282 | testG4NavigatorSteps(myTopNode); |
---|
| 283 | testG4NavigatorCheckNextSteps(myTopNode); |
---|
| 284 | |
---|
| 285 | G4GeometryManager::GetInstance()->OpenGeometry(); |
---|
| 286 | |
---|
| 287 | G4PhysicalVolumeStore *ps=G4PhysicalVolumeStore::GetInstance(); |
---|
| 288 | for (G4int i=ps->size()-1;i>=0;i--) |
---|
| 289 | { |
---|
| 290 | // Delete any rotation matrices |
---|
| 291 | delete (*ps)[i]->GetRotation(); |
---|
| 292 | } |
---|
| 293 | return 0; |
---|
| 294 | } |
---|