Geant4 Cross Reference |
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 /// \file medical/DICOM/src/DicomNestedPhantomParameterisation.cc 27 /// \brief Implementation of the DicomNestedPhantomParameterisation class 28 // 29 // 30 31 #include "DicomNestedPhantomParameterisation.hh" 32 33 #include "DicomHandler.hh" 34 35 #include "G4Box.hh" 36 #include "G4LogicalVolume.hh" 37 #include "G4Material.hh" 38 #include "G4ThreeVector.hh" 39 #include "G4VPhysicalVolume.hh" 40 #include "G4VTouchable.hh" 41 #include "G4VVisManager.hh" 42 #include "G4VisAttributes.hh" 43 44 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo...... 45 46 G4String DicomNestedPhantomParameterisation::fDefaultColorFile = 47 DicomHandler::GetDicomDataPath() + "/ColourMap.dat"; 48 49 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo...... 50 DicomNestedPhantomParameterisation::DicomNestedPhantomParameterisation( 51 const G4ThreeVector& voxelSize, std::vector<G4Material*>& mat, G4int fnZ_, G4int fnY_, G4int fnX_, 52 G4String colorfile) 53 : // G4VNestedParameterisation(), 54 fdX(voxelSize.x()), 55 fdY(voxelSize.y()), 56 fdZ(voxelSize.z()), 57 fnX(fnX_), 58 fnY(fnY_), 59 fnZ(fnZ_), 60 fMaterials(mat), 61 fMaterialIndices(0) 62 { 63 ReadColourData(colorfile); 64 } 65 66 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo...... 67 DicomNestedPhantomParameterisation::~DicomNestedPhantomParameterisation() {} 68 69 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo...... 70 void DicomNestedPhantomParameterisation::ReadColourData(G4String colourFile) 71 { 72 //----- Add a G4VisAttributes for materials not defined in file; 73 G4VisAttributes* blankAtt = new G4VisAttributes; 74 blankAtt->SetVisibility(FALSE); 75 fColours["Default"] = blankAtt; 76 77 std::ifstream fin(colourFile.c_str()); 78 G4int nMate; 79 G4String mateName; 80 G4double cred, cgreen, cblue, copacity; 81 fin >> nMate; 82 for (G4int ii = 0; ii < nMate; ii++) { 83 fin >> mateName; 84 if (fin.eof()) break; 85 fin >> cred >> cgreen >> cblue >> copacity; 86 G4Colour colour(cred, cgreen, cblue, copacity); 87 G4VisAttributes* visAtt = new G4VisAttributes(colour); 88 visAtt->SetVisibility(true); 89 fColours[mateName] = visAtt; 90 fColours2[ii] = new G4VisAttributes(*visAtt); 91 } 92 } 93 94 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo...... 95 void DicomNestedPhantomParameterisation::SetNoVoxels(unsigned int nx, unsigned int ny, 96 unsigned int nz) 97 { 98 fnX = nx; 99 fnY = ny; 100 fnZ = nz; 101 } 102 103 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo...... 104 G4Material* DicomNestedPhantomParameterisation::ComputeMaterial(G4VPhysicalVolume* physVol, 105 const G4int iz, 106 const G4VTouchable* parentTouch) 107 { 108 // protection for initialization and vis at idle state 109 // 110 if (parentTouch == nullptr) return fMaterials[0]; 111 112 // Copy number of voxels. 113 // Copy number of X and Y are obtained from replication number. 114 // Copy nymber of Z is the copy number of current voxel. 115 G4int ix = parentTouch->GetReplicaNumber(0); 116 G4int iy = parentTouch->GetReplicaNumber(1); 117 118 G4int copyID = ix + fnX * iy + fnX * fnY * iz; 119 120 std::size_t matIndex = GetMaterialIndex(copyID); 121 G4Material* mate = fMaterials[matIndex]; 122 123 if (G4VVisManager::GetConcreteInstance() && physVol) { 124 G4String mateName = fMaterials.at(matIndex)->GetName(); 125 std::string::size_type iuu = mateName.find("__"); 126 if (iuu != std::string::npos) mateName = mateName.substr(0, iuu); 127 128 if (0 < fColours.count(mateName)) 129 physVol->GetLogicalVolume()->SetVisAttributes(fColours.find(mateName)->second); 130 else { 131 bool found = false; 132 for (const auto& itr : fColours) { 133 G4String mat_color = itr.first; 134 auto len = mat_color.length(); 135 if (mateName.find(mat_color) == 0 && mateName.length() > len && mateName[len] == '_') { 136 physVol->GetLogicalVolume()->SetVisAttributes(fColours.find(mat_color)->second); 137 found = true; 138 } 139 if (found) break; 140 } 141 if (!found) { 142 static uintmax_t n = 0; 143 if (n++ < 100) 144 G4cout << "Unknown material name " << mateName << " for index " << matIndex << G4endl; 145 if (fColours2.find(matIndex) != fColours2.end()) 146 physVol->GetLogicalVolume()->SetVisAttributes(fColours2.find(matIndex)->second); 147 else 148 physVol->GetLogicalVolume()->SetVisAttributes(fColours.begin()->second); 149 } 150 } 151 physVol->GetLogicalVolume()->SetMaterial(mate); 152 } 153 154 return mate; 155 } 156 157 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo...... 158 unsigned int DicomNestedPhantomParameterisation::GetMaterialIndex(unsigned int copyNo) const 159 { 160 // return *(fMaterialIndices+copyNo); 161 return unsigned(fMaterialIndices[copyNo]); 162 } 163 164 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo...... 165 // Number of Materials 166 // Material scanner is required for preparing physics tables and so on before 167 // starting simulation, so that G4 has to know number of materials. 168 // 169 G4int DicomNestedPhantomParameterisation::GetNumberOfMaterials() const 170 { 171 return G4int(fMaterials.size()); 172 } 173 174 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo...... 175 // 176 // GetMaterial 177 // This is needed for material scanner and realizing geometry. 178 // 179 G4Material* DicomNestedPhantomParameterisation::GetMaterial(G4int i) const 180 { 181 return fMaterials[i]; 182 } 183 184 // 185 // Transformation of voxels. 186 // 187 void DicomNestedPhantomParameterisation::ComputeTransformation(const G4int copyNo, 188 G4VPhysicalVolume* physVol) const 189 { 190 // Position of voxels. 191 // x and y positions are already defined in DetectorConstruction by using 192 // replicated volume. Here only we need to define is z positions of voxels. 193 physVol->SetTranslation( 194 G4ThreeVector(0., 0., (2. * static_cast<double>(copyNo) + 1.) * fdZ - fdZ * fnZ)); 195 } 196 197 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo...... 198 // 199 // Dimensions are always same in this RE02 example. 200 // 201 void DicomNestedPhantomParameterisation::ComputeDimensions(G4Box& box, const G4int, 202 const G4VPhysicalVolume*) const 203 { 204 box.SetXHalfLength(fdX); 205 box.SetYHalfLength(fdY); 206 box.SetZHalfLength(fdZ); 207 } 208