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

Applications | User Support | Publications | Collaboration