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 #include "DicomPhantomParameterisationColour.hh"
0031
0032 #include "DicomHandler.hh"
0033
0034 #include "G4LogicalVolume.hh"
0035 #include "G4Material.hh"
0036 #include "G4VPhysicalVolume.hh"
0037 #include "G4VVisManager.hh"
0038 #include "G4VisAttributes.hh"
0039 #include "globals.hh"
0040
0041
0042
0043 G4String DicomPhantomParameterisationColour::fDefaultColorFile =
0044 DicomHandler::GetDicomDataPath() + "/ColourMap.dat";
0045
0046
0047 DicomPhantomParameterisationColour::DicomPhantomParameterisationColour(G4String colourFile)
0048 : G4PhantomParameterisation()
0049 {
0050 ReadColourData(colourFile);
0051 SetSkipEqualMaterials(false);
0052 }
0053
0054
0055 DicomPhantomParameterisationColour::~DicomPhantomParameterisationColour() {}
0056
0057
0058 void DicomPhantomParameterisationColour::ReadColourData(G4String colourFile)
0059 {
0060
0061 G4VisAttributes* blankAtt = new G4VisAttributes;
0062 blankAtt->SetVisibility(FALSE);
0063 fColours["Default"] = blankAtt;
0064
0065
0066 std::ifstream fin(colourFile.c_str());
0067 G4int nMate;
0068 G4String mateName;
0069 G4double cred, cgreen, cblue, copacity;
0070 fin >> nMate;
0071 for (G4int ii = 0; ii < nMate; ii++) {
0072 fin >> mateName;
0073 if (fin.eof()) break;
0074 fin >> cred >> cgreen >> cblue >> copacity;
0075 G4Colour colour(cred, cgreen, cblue, copacity);
0076 G4VisAttributes* visAtt = new G4VisAttributes(colour);
0077 visAtt->SetVisibility(true);
0078 fColours[mateName] = visAtt;
0079 fColours2[ii] = new G4VisAttributes(*visAtt);
0080 }
0081 }
0082
0083
0084 G4Material* DicomPhantomParameterisationColour::ComputeMaterial(const G4int copyNo,
0085 G4VPhysicalVolume* physVol,
0086 const G4VTouchable*)
0087 {
0088 G4Material* mate = G4PhantomParameterisation::ComputeMaterial(copyNo, physVol, 0);
0089
0090 if (G4VVisManager::GetConcreteInstance() && physVol) {
0091 G4String mateName = mate->GetName();
0092 std::string::size_type iuu = mateName.find("__");
0093 if (iuu != std::string::npos) mateName = mateName.substr(0, iuu);
0094
0095 if (0 < fColours.count(mateName))
0096 physVol->GetLogicalVolume()->SetVisAttributes(fColours.find(mateName)->second);
0097 else {
0098 bool found = false;
0099 for (const auto& itr : fColours) {
0100 G4String mat_color = itr.first;
0101 auto len = mat_color.length();
0102 if (mateName.find(mat_color) == 0 && mateName.length() > len && mateName[len] == '_') {
0103 physVol->GetLogicalVolume()->SetVisAttributes(fColours.find(mat_color)->second);
0104 found = true;
0105 }
0106 if (found) break;
0107 }
0108 if (!found) {
0109 G4int matIndex = G4int(GetMaterialIndex(copyNo));
0110 static uintmax_t n = 0;
0111 if (n++ < 100)
0112 G4cout << "Unknown material name " << mateName << " for index " << matIndex << G4endl;
0113 if (fColours2.find(matIndex) != fColours2.end())
0114 physVol->GetLogicalVolume()->SetVisAttributes(fColours2.find(matIndex)->second);
0115 else
0116 physVol->GetLogicalVolume()->SetVisAttributes(fColours.begin()->second);
0117 }
0118 }
0119 physVol->GetLogicalVolume()->SetMaterial(mate);
0120 }
0121
0122 return mate;
0123 }