Geant4  v4-10.4-release
 모두 클래스 네임스페이스들 파일들 함수 변수 타입정의 열거형 타입 열거형 멤버 Friends 매크로 그룹들 페이지들
DicomPhantomZSliceHeader.cc
이 파일의 문서화 페이지로 가기
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 // $Id: DicomPhantomZSliceHeader.cc 107363 2017-11-09 10:51:28Z gcosmo $
27 //
30 //
31 
32 #include "globals.hh"
33 #include "G4LogicalVolume.hh"
34 #include "G4MaterialTable.hh"
35 #include "G4Material.hh"
36 #include "G4GeometryTolerance.hh"
37 #include "G4NistManager.hh"
38 
40 
41 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo..
43 : fNoVoxelX(0),fNoVoxelY(0),fNoVoxelZ(0),
44  fMinX(0),fMinY(0),fMinZ(0),
45  fMaxX(0),fMaxY(0),fMaxZ(0),
46  fFilename(fname),fSliceLocation(0)
47 {
48 
49 }
50 
51 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo..
53 {
54 
55 }
56 
57 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo..
59  const DicomPhantomZSliceHeader& rhs )
60 {
61  fNoVoxelX = rhs.GetNoVoxelX();
62  fNoVoxelY = rhs.GetNoVoxelY();
63  fNoVoxelZ = rhs.GetNoVoxelZ();
64  fMinX = rhs.GetMinX();
65  fMaxX = rhs.GetMaxX();
66  fMinY = rhs.GetMinY();
67  fMaxY = rhs.GetMaxY();
68  fMinZ = rhs.GetMinZ();
69  fMaxZ = rhs.GetMaxZ();
71  fFilename = rhs.fFilename;
72  fValues = rhs.fValues;
73  fMateIDs = rhs.fMateIDs;
75 }
76 
77 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo...
79 {
80  //----- Read material indices and names
81  G4int nmate;
82  G4String mateindex;
83  G4String matename;
84  fin >> nmate;
85 #ifdef G4VERBOSE
86  G4cout << " DicomPhantomZSliceHeader reading number of materials "
87  << nmate << G4endl;
88 #endif
89 
90  for( G4int im = 0; im < nmate; im++ ){
91  fin >> mateindex >> matename;
92 #ifdef G4VERBOSE
93  //G4cout << " DicomPhantomZSliceHeader reading material "
94  // << im << " : "<< mateindex << " " << matename << G4endl;
95 #endif
96 
97  if( ! CheckMaterialExists( matename ) ) {
98  G4Exception("DicomPhantomZSliceHeader::DicomPhantomZSliceHeader",
99  "A material is found in file that is not built in the C++ code",
100  FatalErrorInArgument, matename.c_str());
101  }
102 
103  fMaterialNames.push_back(matename);
104  }
105 
106  //----- Read number of voxels
107  fin >> fNoVoxelX >> fNoVoxelY >> fNoVoxelZ;
108 #ifdef G4VERBOSE
109  G4cout << " Number of voxels " << fNoVoxelX << " " << fNoVoxelY
110  << " " << fNoVoxelZ << G4endl;
111 #endif
112 
113  //----- Read minimal and maximal extensions (= walls of phantom)
114  fin >> fMinX >> fMaxX;
115  fin >> fMinY >> fMaxY;
116  fin >> fMinZ >> fMaxZ;
117 #ifdef G4VERBOSE
118  G4cout << " Extension in X " << fMinX << " " << fMaxX << G4endl
119  << " Extension in Y " << fMinY << " " << fMaxY << G4endl
120  << " Extension in Z " << fMinZ << " " << fMaxZ << G4endl;
121 #endif
122 
123  fSliceLocation = 0.5*(fMinZ + fMaxZ);
124 }
125 
126 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....
128 {
130  std::vector<G4Material*>::const_iterator matite;
131  for( matite = matTab->begin(); matite != matTab->end(); ++matite ) {
132  if( (*matite)->GetName() == mateName ) { return true; }
133  }
134 
136  if( g4mate ) {
137  return false;
138  } else {
139  return true;
140  }
141 
142 }
143 
144 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....
146 {
147  *this = *this + rhs;
148 }
149 
150 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo..
152  const DicomPhantomZSliceHeader& rhs )
153 {
154  //----- Check that both slices has the same dimensions
155  if( fNoVoxelX != rhs.GetNoVoxelX()
156  || fNoVoxelY != rhs.GetNoVoxelY() ) {
157  G4cerr << "DicomPhantomZSliceHeader error adding two slice headers:\
158  !!! Different number of voxels: "
159  << " X= " << fNoVoxelX << " =? " << rhs.GetNoVoxelX()
160  << " Y= " << fNoVoxelY << " =? " << rhs.GetNoVoxelY()
161  << " Z= " << fNoVoxelZ << " =? " << rhs.GetNoVoxelZ()
162  << G4endl;
163  G4Exception("DicomPhantomZSliceHeader::DicomPhantomZSliceHeader",
164  "",FatalErrorInArgument,"");
165  }
166  //----- Check that both slices has the same extensions
167  if( fMinX != rhs.GetMinX() || fMaxX != rhs.GetMaxX()
168  || fMinY != rhs.GetMinY() || fMaxY != rhs.GetMaxY() ) {
169  G4cerr << "DicomPhantomZSliceHeader error adding two slice headers:\
170  !!! Different extensions: "
171  << " Xmin= " << fMinX << " =? " << rhs.GetMinX()
172  << " Xmax= " << fMaxX << " =? " << rhs.GetMaxX()
173  << " Ymin= " << fMinY << " =? " << rhs.GetMinY()
174  << " Ymax= " << fMaxY << " =? " << rhs.GetMaxY()
175  << G4endl;
176  G4Exception("DicomPhantomZSliceHeader::operator+","",
178  }
179 
180  //----- Check that both slices have the same materials
181  std::vector<G4String> fMaterialNames2 = rhs.GetMaterialNames();
182  if( fMaterialNames.size() != fMaterialNames2.size() ) {
183  G4cerr << "DicomPhantomZSliceHeader error adding two slice headers:\
184  !!! Different number of materials: " << fMaterialNames.size() << " =? "
185  << fMaterialNames2.size() << G4endl;
186  G4Exception("DicomPhantomZSliceHeader::operator+","",
188  }
189  for( unsigned int ii = 0; ii < fMaterialNames.size(); ii++ ) {
190  if( fMaterialNames[ii] != fMaterialNames2[ii] ) {
191  G4cerr << "DicomPhantomZSliceHeader error adding two slice headers:\
192  !!! Different material number " << ii << " : "
193  << fMaterialNames[ii] << " =? "
194  << fMaterialNames2[ii] << G4endl;
195  G4Exception("DicomPhantomZSliceHeader::operator+","",
197  }
198  }
199 
200  //----- Check that the slices are contiguous in Z
201  if( std::fabs( fMinZ - rhs.GetMaxZ() ) >
203  std::fabs( fMaxZ - rhs.GetMinZ() ) >
205  G4cerr << "DicomPhantomZSliceHeader error adding two slice headers:!!!\
206  Slices are not contiguous in Z "
207  << " Zmin= " << fMinZ << " & " << rhs.GetMinZ()
208  << " Zmax= " << fMaxZ << " & " << rhs.GetMaxZ()
209  << G4endl;
210  G4Exception("DicomPhantomZSliceHeader::operator+","",
212  }
213 
214  //----- Build slice header copying first one
215  DicomPhantomZSliceHeader temp( *this );
216 
217  //----- Add data from second slice header
218  temp.SetMinZ( std::min( fMinZ, rhs.GetMinZ() ) );
219  temp.SetMaxZ( std::max( fMaxZ, rhs.GetMaxZ() ) );
220  temp.SetNoVoxelZ( fNoVoxelZ + rhs.GetNoVoxelZ() );
221 
222  return temp;
223 }
224 
225 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo..
227 {
228 
229  G4cout << "DicomPhantomZSliceHeader::Dumping Z Slice data to "
230  << fFilename << "..." << G4endl;
231  //sleep(5);
232 
233  // May seen counter-intuitive (dumping to file you are reading from), but
234  // the reason for this is modification slice spacing
235  if(fMateIDs.size() == 0 || fValues.size() == 0) { ReadDataFromFile(); }
236 
237 
238  std::ofstream out;
239  out.open(fFilename.c_str());
240 
241  if(!out) {
242  G4String descript = "DicomPhantomZSliceHeader::DumpToFile: could not open "
243  +fFilename;
244  G4Exception(descript.c_str(),"", FatalException, "");
245  }
246 
247  out << fMaterialNames.size() << std::endl;
248  for(unsigned int i = 0; i < fMaterialNames.size(); ++i) {
249  out << i << " " << fMaterialNames.at(i) << std::endl;
250  }
251 
252  out << fNoVoxelX << " " << fNoVoxelY << " " << fNoVoxelZ << std::endl;
253  out << fMinX << " " << fMaxX << std::endl;
254  out << fMinY << " " << fMaxY << std::endl;
255  out << fMinZ << " " << fMaxZ << std::endl;
256 
257  for(unsigned int i = 0; i < fMateIDs.size(); ++i)
258  { Print(out,fMateIDs.at(i)," "); }
259 
260  for(unsigned int i = 0; i < fValues.size(); ++i)
261  { Print(out,fValues.at(i)," ",6); }
262 
263  out.close();
264 
265 }
266 
267 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo.....
269 {
270  std::ifstream in;
271  in.open(fFilename.c_str());
272 
273  if(!in) {
274  G4String descript = "DicomPhantomZSliceHeader::DumpToFile: could not open "
275  +fFilename;
276  G4Exception(descript.c_str(),"", FatalException, "");
277  }
278 
279  G4int nMaterials;
280  in >> nMaterials;
281 
282  fMaterialNames.resize(nMaterials,"");
283  for(G4int i = 0; i < nMaterials; ++i) {
284  G4String str1, str2;
285  in >> str1 >> str2;
286  if(!IsInteger(str1)) {
287  G4String descript = "String : " + str1 + " supposed to be integer";
288  G4Exception("DicomPhantomZSliceHeader::ReadDataFromFile - error in \
289  formatting: missing material index","", FatalException,descript.c_str());
290  }
291  G4int index = G4s2n<G4int>(str1);
292  if(index > nMaterials || index < 0) {
293  G4String descript = "Index : " + str1;
294  G4Exception("DicomPhantomZSliceHeader::ReadDataFromFile - error:\
295  bad material index","", FatalException,descript.c_str());
296  }
297  fMaterialNames[index] = str2;
298  }
299 
300  in >> fNoVoxelX >> fNoVoxelY >> fNoVoxelZ;
301 
302  G4double tmpMinX, tmpMinY, tmpMinZ;
303  G4double tmpMaxX, tmpMaxY, tmpMaxZ;
304 
305  in >> tmpMinX >> tmpMaxX;
306  in >> tmpMinY >> tmpMaxY;
307  in >> tmpMinZ >> tmpMaxZ;
308 
309  fMinX = (CheckConsistency(tmpMinX,fMinX,"Min X value")) ?
310  fMinX : ((fMinX == 0) ? tmpMinX : fMinX);
311  fMaxX = (CheckConsistency(tmpMaxX,fMaxX,"Max X value")) ?
312  fMaxX : ((fMaxX == 0) ? tmpMaxX : fMaxX);
313 
314  fMinY = (CheckConsistency(tmpMinY,fMinY,"Min Y value")) ?
315  fMinY : ((fMinY == 0) ? tmpMinY : fMinY);
316  fMaxY = (CheckConsistency(tmpMaxY,fMaxY,"Max Y value")) ?
317  fMaxY : ((fMaxY == 0) ? tmpMaxY : fMaxY);
318 
319  fMinZ = (CheckConsistency(tmpMinZ,fMinZ,"Min Z value")) ?
320  fMinZ : ((fMinZ == 0) ? tmpMinZ : fMinZ);
321  fMaxZ = (CheckConsistency(tmpMaxZ,fMaxZ,"Max Z value")) ?
322  fMaxZ : ((fMaxZ == 0) ? tmpMaxZ : fMaxZ);
323 
324  fMateIDs.clear();
325  fValues.clear();
326  fMateIDs.resize(fNoVoxelY*fNoVoxelZ,std::vector<G4int>(fNoVoxelX,0));
327  fValues.resize(fNoVoxelY*fNoVoxelZ,std::vector<G4double>(fNoVoxelX,0.));
328  for(G4int k = 0; k < fNoVoxelZ; ++k) {
329  for(G4int j = 0; j < fNoVoxelY; ++j) {
330  for(G4int i = 0; i < fNoVoxelX; ++i) {
331  G4int tmpMateID;
332  in >> tmpMateID;
333  G4int row = j*(k+1);
334  fMateIDs[row][i] = tmpMateID;
335  }
336  }
337  }
338 
339  for(G4int k = 0; k < fNoVoxelZ; ++k) {
340  for(G4int j = 0; j < fNoVoxelY; ++j) {
341  for(G4int i = 0; i < fNoVoxelX; ++i) {
342  G4double tmpValue;
343  in >> tmpValue;
344  G4int row = j*(k+1);
345  fValues[row][i] = tmpValue;
346  }
347  }
348  }
349 
350  in.close();
351 }
352 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....
T max(const T t1, const T t2)
brief Return the largest of the two arguments
void SetMaxZ(const G4double &val)
void Print(std::ostream &, const std::vector< T > &, const G4String &, G4int breakLine=-1)
static G4MaterialTable * GetMaterialTable()
Definition: G4Material.cc:593
#define G4endl
Definition: G4ios.hh:61
void operator+=(const DicomPhantomZSliceHeader &rhs)
G4double GetRadialTolerance() const
void SetNoVoxelZ(const G4int &val)
std::vector< std::vector< G4int > > fMateIDs
fin
Definition: AddMC.C:2
double G4double
Definition: G4Types.hh:76
bool G4bool
Definition: G4Types.hh:79
std::vector< std::vector< G4double > > fValues
G4Material * FindOrBuildMaterial(const G4String &name, G4bool isotopes=true, G4bool warning=false)
DicomPhantomZSliceHeader(const G4String &)
G4GLOB_DLL std::ostream G4cerr
std::vector< G4Material * > G4MaterialTable
const std::vector< G4String > & GetMaterialNames() const
Definition of the DicomPhantomZSliceHeader class.
void G4Exception(const char *originOfException, const char *exceptionCode, G4ExceptionSeverity severity, const char *description)
Definition: G4Exception.hh:65
int G4int
Definition: G4Types.hh:78
ifstream in
Definition: comparison.C:7
std::vector< G4String > fMaterialNames
G4bool CheckMaterialExists(const G4String &mateName)
G4GLOB_DLL std::ostream G4cout
bool CheckConsistency(const T &, const T &, G4String)
static G4GeometryTolerance * GetInstance()
void SetMinZ(const G4double &val)
G4bool IsInteger(const G4String &)
T min(const T t1, const T t2)
brief Return the smallest of the two arguments
static G4NistManager * Instance()
DicomPhantomZSliceHeader operator+(const DicomPhantomZSliceHeader &rhs)