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p3 ] [ 10.5 ] [ 10.5.p1 ] [ 10.6 ] [ 10.6.p1 ] [ 10.6.p2 ] [ 10.6.p3 ] [ 10.7 ] [ 10.7.p1 ] [ 10.7.p2 ] [ 10.7.p3 ] [ 10.7.p4 ] [ 11.0 ] [ 11.0.p1 ] [ 11.0.p2 ] [ 11.0.p3, ] [ 11.0.p4 ] [ 11.1 ] [ 11.1.1 ] [ 11.1.2 ] [ 11.1.3 ] [ 11.2 ] [ 11.2.1 ] [ 11.2.2 ] [ 11.3.0 ]

  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