Geant4_10
DicomPhantomZSliceHeader.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 // $Id: DicomPhantomZSliceHeader.cc 73076 2013-08-16 07:45:30Z 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 
39 
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 {
60  fNoVoxelX = rhs.GetNoVoxelX();
61  fNoVoxelY = rhs.GetNoVoxelY();
62  fNoVoxelZ = rhs.GetNoVoxelZ();
63  fMinX = rhs.GetMinX();
64  fMaxX = rhs.GetMaxX();
65  fMinY = rhs.GetMinY();
66  fMaxY = rhs.GetMaxY();
67  fMinZ = rhs.GetMinZ();
68  fMaxZ = rhs.GetMaxZ();
69  fMaterialNames = rhs.GetMaterialNames();
70  fFilename = rhs.fFilename;
71  fValues = rhs.fValues;
72  fMateIDs = rhs.fMateIDs;
73  fSliceLocation = rhs.fSliceLocation;
74 }
75 
76 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
78 {
79  //----- Read material indices and names
80  G4int nmate;
81  G4String mateindex;
82  G4String matename;
83  fin >> nmate;
84 #ifdef G4VERBOSE
85  G4cout << " DicomPhantomZSliceHeader reading number of materials " << nmate << G4endl;
86 #endif
87 
88  for( G4int im = 0; im < nmate; im++ ){
89  fin >> mateindex >> matename;
90 #ifdef G4VERBOSE
91  G4cout << " DicomPhantomZSliceHeader reading material " << im << " : "
92  << mateindex << " " << matename << G4endl;
93 #endif
94 
95  if( ! CheckMaterialExists( matename ) ) {
96  G4Exception("DicomPhantomZSliceHeader::DicomPhantomZSliceHeader",
97  "A material is found in file that is not built in the C++ code",
98  FatalErrorInArgument, matename.c_str());
99  }
100 
101  fMaterialNames.push_back(matename);
102  }
103 
104  //----- Read number of voxels
105  fin >> fNoVoxelX >> fNoVoxelY >> fNoVoxelZ;
106 #ifdef G4VERBOSE
107  G4cout << " Number of voxels " << fNoVoxelX << " " << fNoVoxelY << " " << fNoVoxelZ << G4endl;
108 #endif
109 
110  //----- Read minimal and maximal extensions (= walls of phantom)
111  fin >> fMinX >> fMaxX;
112  fin >> fMinY >> fMaxY;
113  fin >> fMinZ >> fMaxZ;
114 #ifdef G4VERBOSE
115  G4cout << " Extension in X " << fMinX << " " << fMaxX << G4endl
116  << " Extension in Y " << fMinY << " " << fMaxY << G4endl
117  << " Extension in Z " << fMinZ << " " << fMaxZ << G4endl;
118 #endif
119 
120  fSliceLocation = 0.5*(fMinZ + fMaxZ);
121 }
122 
123 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
124 G4bool DicomPhantomZSliceHeader::CheckMaterialExists( const G4String& mateName )
125 {
127  std::vector<G4Material*>::const_iterator matite;
128  for( matite = matTab->begin(); matite != matTab->end(); ++matite ) {
129  if( (*matite)->GetName() == mateName ) { return true; }
130  }
131 
132  return false;
133 
134 }
135 
136 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
138 {
139  *this = *this + rhs;
140 }
141 
142 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
144 {
145  //----- Check that both slices has the same dimensions
146  if( fNoVoxelX != rhs.GetNoVoxelX()
147  || fNoVoxelY != rhs.GetNoVoxelY() ) {
148  G4cerr << "DicomPhantomZSliceHeader error adding two slice headers:\
149  !!! Different number of voxels: "
150  << " X= " << fNoVoxelX << " =? " << rhs.GetNoVoxelX()
151  << " Y= " << fNoVoxelY << " =? " << rhs.GetNoVoxelY()
152  << " Z= " << fNoVoxelZ << " =? " << rhs.GetNoVoxelZ()
153  << G4endl;
154  G4Exception("DicomPhantomZSliceHeader::DicomPhantomZSliceHeader",
155  "",FatalErrorInArgument,"");
156  }
157  //----- Check that both slices has the same extensions
158  if( fMinX != rhs.GetMinX() || fMaxX != rhs.GetMaxX()
159  || fMinY != rhs.GetMinY() || fMaxY != rhs.GetMaxY() ) {
160  G4cerr << "DicomPhantomZSliceHeader error adding two slice headers:\
161  !!! Different extensions: "
162  << " Xmin= " << fMinX << " =? " << rhs.GetMinX()
163  << " Xmax= " << fMaxX << " =? " << rhs.GetMaxX()
164  << " Ymin= " << fMinY << " =? " << rhs.GetMinY()
165  << " Ymax= " << fMaxY << " =? " << rhs.GetMaxY()
166  << G4endl;
167  G4Exception("DicomPhantomZSliceHeader::operator+","",FatalErrorInArgument,"");
168  }
169 
170  //----- Check that both slices has the same materials
171  std::vector<G4String> fMaterialNames2 = rhs.GetMaterialNames();
172  if( fMaterialNames.size() != fMaterialNames2.size() ) {
173  G4cerr << "DicomPhantomZSliceHeader error adding two slice headers:\
174  !!! Different number of materials: " << fMaterialNames.size() << " =? "
175  << fMaterialNames2.size() << G4endl;
176  G4Exception("DicomPhantomZSliceHeader::operator+","",FatalErrorInArgument,"");
177  }
178  for( unsigned int ii = 0; ii < fMaterialNames.size(); ii++ ) {
179  if( fMaterialNames[ii] != fMaterialNames2[ii] ) {
180  G4cerr << "DicomPhantomZSliceHeader error adding two slice headers:\
181  !!! Different material number " << ii << " : " << fMaterialNames[ii] << " =? "
182  << fMaterialNames2[ii] << G4endl;
183  G4Exception("DicomPhantomZSliceHeader::operator+","",FatalErrorInArgument,"");
184  }
185  }
186 
187  //----- Check that the slices are contiguous in Z
188  if( std::fabs( fMinZ - rhs.GetMaxZ() ) >
190  std::fabs( fMaxZ - rhs.GetMinZ() ) >
192  G4cerr << "DicomPhantomZSliceHeader error adding two slice headers: !!!\
193  Slices are not contiguous in Z "
194  << " Zmin= " << fMinZ << " & " << rhs.GetMinZ()
195  << " Zmax= " << fMaxZ << " & " << rhs.GetMaxZ()
196  << G4endl;
197  G4Exception("DicomPhantomZSliceHeader::operator+","",FatalErrorInArgument,"");
198  }
199 
200  //----- Build slice header copying first one
201  DicomPhantomZSliceHeader temp( *this );
202 
203  //----- Add data from second slice header
204  temp.SetMinZ( std::min( fMinZ, rhs.GetMinZ() ) );
205  temp.SetMaxZ( std::max( fMaxZ, rhs.GetMaxZ() ) );
206  temp.SetNoVoxelZ( fNoVoxelZ + rhs.GetNoVoxelZ() );
207 
208  return temp;
209 }
210 
211 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
213 {
214 
215  G4cout << "DicomPhantomZSliceHeader::Dumping Z Slice data to " << fFilename << "..." << G4endl;
216  //sleep(5);
217 
218  // May seen counter-intuitive (dumping to file you are reading from), but
219  // the reason for this is modification slice spacing
220  if(fMateIDs.size() == 0 || fValues.size() == 0) { ReadDataFromFile(); }
221 
222 
223  std::ofstream out;
224  out.open(fFilename.c_str());
225 
226  if(!out) {
227  G4String descript = "DicomPhantomZSliceHeader::DumpToFile: could not open "+fFilename;
228  G4Exception(descript.c_str(),"", FatalException, "");
229  }
230 
231 
232  out << fMaterialNames.size() << std::endl;
233  for(unsigned int i = 0; i < fMaterialNames.size(); ++i) {
234  out << i << " " << fMaterialNames.at(i) << std::endl;
235  }
236 
237  out << fNoVoxelX << " " << fNoVoxelY << " " << fNoVoxelZ << std::endl;
238  out << fMinX << " " << fMaxX << std::endl;
239  out << fMinY << " " << fMaxY << std::endl;
240  out << fMinZ << " " << fMaxZ << std::endl;
241 
242  for(unsigned int i = 0; i < fMateIDs.size(); ++i) { print(out,fMateIDs.at(i)," "); }
243  for(unsigned int i = 0; i < fValues.size(); ++i) { print(out,fValues.at(i)," ",6); }
244 
245  out.close();
246 
247 }
248 
249 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
251 {
252  std::ifstream in;
253  in.open(fFilename.c_str());
254 
255  if(!in) {
256  G4String descript = "DicomPhantomZSliceHeader::DumpToFile: could not open "+fFilename;
257  G4Exception(descript.c_str(),"", FatalException, "");
258  }
259 
260  G4int nMaterials;
261  in >> nMaterials;
262 
263  fMaterialNames.resize(nMaterials,"");
264  for(G4int i = 0; i < nMaterials; ++i) {
265  G4String str1, str2;
266  in >> str1 >> str2;
267  if(!IsInteger(str1)) {
268  G4String descript = "String : " + str1 + " supposed to be integer";
269  G4Exception("DicomPhantomZSliceHeader::ReadDataFromFile - error in \
270  formatting: missing material index","", FatalException,descript.c_str());
271  }
272  G4int index = g4s2n<G4int>(str1);
273  if(index > nMaterials || index < 0) {
274  G4String descript = "Index : " + str1;
275  G4Exception("DicomPhantomZSliceHeader::ReadDataFromFile - error:\
276  bad material index","", FatalException,descript.c_str());
277  }
278  fMaterialNames[index] = str2;
279  }
280 
281  in >> fNoVoxelX >> fNoVoxelY >> fNoVoxelZ;
282 
283  G4double tmpMinX, tmpMinY, tmpMinZ;
284  G4double tmpMaxX, tmpMaxY, tmpMaxZ;
285 
286  in >> tmpMinX >> tmpMaxX;
287  in >> tmpMinY >> tmpMaxY;
288  in >> tmpMinZ >> tmpMaxZ;
289 
290  fMinX = (CheckConsistency(tmpMinX,fMinX,"Min X value")) ?
291  fMinX : ((fMinX == 0) ? tmpMinX : fMinX);
292  fMaxX = (CheckConsistency(tmpMaxX,fMaxX,"Max X value")) ?
293  fMaxX : ((fMaxX == 0) ? tmpMaxX : fMaxX);
294 
295  fMinY = (CheckConsistency(tmpMinY,fMinY,"Min Y value")) ?
296  fMinY : ((fMinY == 0) ? tmpMinY : fMinY);
297  fMaxY = (CheckConsistency(tmpMaxY,fMaxY,"Max Y value")) ?
298  fMaxY : ((fMaxY == 0) ? tmpMaxY : fMaxY);
299 
300  fMinZ = (CheckConsistency(tmpMinZ,fMinZ,"Min Z value")) ?
301  fMinZ : ((fMinZ == 0) ? tmpMinZ : fMinZ);
302  fMaxZ = (CheckConsistency(tmpMaxZ,fMaxZ,"Max Z value")) ?
303  fMaxZ : ((fMaxZ == 0) ? tmpMaxZ : fMaxZ);
304 
305  fMateIDs.clear();
306  fValues.clear();
307  fMateIDs.resize(fNoVoxelY*fNoVoxelZ,std::vector<G4int>(fNoVoxelX,0));
308  fValues.resize(fNoVoxelY*fNoVoxelZ,std::vector<G4double>(fNoVoxelX,0.));
309  for(G4int k = 0; k < fNoVoxelZ; ++k) {
310  for(G4int j = 0; j < fNoVoxelY; ++j) {
311  for(G4int i = 0; i < fNoVoxelX; ++i) {
312  G4int tmpMateID;
313  in >> tmpMateID;
314  G4int row = j*(k+1);
315  fMateIDs[row][i] = tmpMateID;
316  }
317  }
318  }
319 
320  for(G4int k = 0; k < fNoVoxelZ; ++k) {
321  for(G4int j = 0; j < fNoVoxelY; ++j) {
322  for(G4int i = 0; i < fNoVoxelX; ++i) {
323  G4double tmpValue;
324  in >> tmpValue;
325  G4int row = j*(k+1);
326  fValues[row][i] = tmpValue;
327  }
328  }
329  }
330 
331 
332  in.close();
333 }
334 
335 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
336 
337 
338 
fin
Definition: AddMC.C:2
void SetMinZ(const G4double &val)
Int_t index
Definition: macro.C:9
ifstream in
Definition: comparison.C:7
const std::vector< G4String > & GetMaterialNames() const
static G4MaterialTable * GetMaterialTable()
Definition: G4Material.cc:564
std::vector< G4Material * > G4MaterialTable
DicomPhantomZSliceHeader operator+(const DicomPhantomZSliceHeader &rhs)
int G4int
Definition: G4Types.hh:78
DicomPhantomZSliceHeader(const G4String &)
void operator+=(const DicomPhantomZSliceHeader &rhs)
G4GLOB_DLL std::ostream G4cout
G4double GetRadialTolerance() const
bool G4bool
Definition: G4Types.hh:79
void G4Exception(const char *originOfException, const char *exceptionCode, G4ExceptionSeverity severity, const char *comments)
Definition: G4Exception.cc:41
T max(const T t1, const T t2)
brief Return the largest of the two arguments
T min(const T t1, const T t2)
brief Return the smallest of the two arguments
#define G4endl
Definition: G4ios.hh:61
string fname
Definition: test.py:308
Definition of the DicomPhantomZSliceHeader class.
double G4double
Definition: G4Types.hh:76
void SetMaxZ(const G4double &val)
void SetNoVoxelZ(const G4int &val)
static G4GeometryTolerance * GetInstance()
G4GLOB_DLL std::ostream G4cerr