File indexing completed on 2025-02-23 09:21:45
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031 #include "DicomNestedPhantomParameterisation.hh"
0032
0033 #include "DicomHandler.hh"
0034
0035 #include "G4Box.hh"
0036 #include "G4LogicalVolume.hh"
0037 #include "G4Material.hh"
0038 #include "G4ThreeVector.hh"
0039 #include "G4VPhysicalVolume.hh"
0040 #include "G4VTouchable.hh"
0041 #include "G4VVisManager.hh"
0042 #include "G4VisAttributes.hh"
0043
0044
0045
0046 G4String DicomNestedPhantomParameterisation::fDefaultColorFile =
0047 DicomHandler::GetDicomDataPath() + "/ColourMap.dat";
0048
0049
0050 DicomNestedPhantomParameterisation::DicomNestedPhantomParameterisation(
0051 const G4ThreeVector& voxelSize, std::vector<G4Material*>& mat, G4int fnZ_, G4int fnY_, G4int fnX_,
0052 G4String colorfile)
0053 :
0054 fdX(voxelSize.x()),
0055 fdY(voxelSize.y()),
0056 fdZ(voxelSize.z()),
0057 fnX(fnX_),
0058 fnY(fnY_),
0059 fnZ(fnZ_),
0060 fMaterials(mat),
0061 fMaterialIndices(0)
0062 {
0063 ReadColourData(colorfile);
0064 }
0065
0066
0067 DicomNestedPhantomParameterisation::~DicomNestedPhantomParameterisation() {}
0068
0069
0070 void DicomNestedPhantomParameterisation::ReadColourData(G4String colourFile)
0071 {
0072
0073 G4VisAttributes* blankAtt = new G4VisAttributes;
0074 blankAtt->SetVisibility(FALSE);
0075 fColours["Default"] = blankAtt;
0076
0077 std::ifstream fin(colourFile.c_str());
0078 G4int nMate;
0079 G4String mateName;
0080 G4double cred, cgreen, cblue, copacity;
0081 fin >> nMate;
0082 for (G4int ii = 0; ii < nMate; ii++) {
0083 fin >> mateName;
0084 if (fin.eof()) break;
0085 fin >> cred >> cgreen >> cblue >> copacity;
0086 G4Colour colour(cred, cgreen, cblue, copacity);
0087 G4VisAttributes* visAtt = new G4VisAttributes(colour);
0088 visAtt->SetVisibility(true);
0089 fColours[mateName] = visAtt;
0090 fColours2[ii] = new G4VisAttributes(*visAtt);
0091 }
0092 }
0093
0094
0095 void DicomNestedPhantomParameterisation::SetNoVoxels(unsigned int nx, unsigned int ny,
0096 unsigned int nz)
0097 {
0098 fnX = nx;
0099 fnY = ny;
0100 fnZ = nz;
0101 }
0102
0103
0104 G4Material* DicomNestedPhantomParameterisation::ComputeMaterial(G4VPhysicalVolume* physVol,
0105 const G4int iz,
0106 const G4VTouchable* parentTouch)
0107 {
0108
0109
0110 if (parentTouch == nullptr) return fMaterials[0];
0111
0112
0113
0114
0115 G4int ix = parentTouch->GetReplicaNumber(0);
0116 G4int iy = parentTouch->GetReplicaNumber(1);
0117
0118 G4int copyID = ix + fnX * iy + fnX * fnY * iz;
0119
0120 std::size_t matIndex = GetMaterialIndex(copyID);
0121 G4Material* mate = fMaterials[matIndex];
0122
0123 if (G4VVisManager::GetConcreteInstance() && physVol) {
0124 G4String mateName = fMaterials.at(matIndex)->GetName();
0125 std::string::size_type iuu = mateName.find("__");
0126 if (iuu != std::string::npos) mateName = mateName.substr(0, iuu);
0127
0128 if (0 < fColours.count(mateName))
0129 physVol->GetLogicalVolume()->SetVisAttributes(fColours.find(mateName)->second);
0130 else {
0131 bool found = false;
0132 for (const auto& itr : fColours) {
0133 G4String mat_color = itr.first;
0134 auto len = mat_color.length();
0135 if (mateName.find(mat_color) == 0 && mateName.length() > len && mateName[len] == '_') {
0136 physVol->GetLogicalVolume()->SetVisAttributes(fColours.find(mat_color)->second);
0137 found = true;
0138 }
0139 if (found) break;
0140 }
0141 if (!found) {
0142 static uintmax_t n = 0;
0143 if (n++ < 100)
0144 G4cout << "Unknown material name " << mateName << " for index " << matIndex << G4endl;
0145 if (fColours2.find(matIndex) != fColours2.end())
0146 physVol->GetLogicalVolume()->SetVisAttributes(fColours2.find(matIndex)->second);
0147 else
0148 physVol->GetLogicalVolume()->SetVisAttributes(fColours.begin()->second);
0149 }
0150 }
0151 physVol->GetLogicalVolume()->SetMaterial(mate);
0152 }
0153
0154 return mate;
0155 }
0156
0157
0158 unsigned int DicomNestedPhantomParameterisation::GetMaterialIndex(unsigned int copyNo) const
0159 {
0160
0161 return unsigned(fMaterialIndices[copyNo]);
0162 }
0163
0164
0165
0166
0167
0168
0169 G4int DicomNestedPhantomParameterisation::GetNumberOfMaterials() const
0170 {
0171 return G4int(fMaterials.size());
0172 }
0173
0174
0175
0176
0177
0178
0179 G4Material* DicomNestedPhantomParameterisation::GetMaterial(G4int i) const
0180 {
0181 return fMaterials[i];
0182 }
0183
0184
0185
0186
0187 void DicomNestedPhantomParameterisation::ComputeTransformation(const G4int copyNo,
0188 G4VPhysicalVolume* physVol) const
0189 {
0190
0191
0192
0193 physVol->SetTranslation(
0194 G4ThreeVector(0., 0., (2. * static_cast<double>(copyNo) + 1.) * fdZ - fdZ * fnZ));
0195 }
0196
0197
0198
0199
0200
0201 void DicomNestedPhantomParameterisation::ComputeDimensions(G4Box& box, const G4int,
0202 const G4VPhysicalVolume*) const
0203 {
0204 box.SetXHalfLength(fdX);
0205 box.SetYHalfLength(fdY);
0206 box.SetZHalfLength(fdZ);
0207 }