Geant4  10.01.p02
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 84839 2014-10-21 13:44:55Z 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  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 " << im << " : "
94  << 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 
135  return false;
136 
137 }
138 
139 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
141 {
142  *this = *this + rhs;
143 }
144 
145 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
147  const DicomPhantomZSliceHeader& rhs )
148 {
149  //----- Check that both slices has the same dimensions
150  if( fNoVoxelX != rhs.GetNoVoxelX()
151  || fNoVoxelY != rhs.GetNoVoxelY() ) {
152  G4cerr << "DicomPhantomZSliceHeader error adding two slice headers:\
153  !!! Different number of voxels: "
154  << " X= " << fNoVoxelX << " =? " << rhs.GetNoVoxelX()
155  << " Y= " << fNoVoxelY << " =? " << rhs.GetNoVoxelY()
156  << " Z= " << fNoVoxelZ << " =? " << rhs.GetNoVoxelZ()
157  << G4endl;
158  G4Exception("DicomPhantomZSliceHeader::DicomPhantomZSliceHeader",
159  "",FatalErrorInArgument,"");
160  }
161  //----- Check that both slices has the same extensions
162  if( fMinX != rhs.GetMinX() || fMaxX != rhs.GetMaxX()
163  || fMinY != rhs.GetMinY() || fMaxY != rhs.GetMaxY() ) {
164  G4cerr << "DicomPhantomZSliceHeader error adding two slice headers:\
165  !!! Different extensions: "
166  << " Xmin= " << fMinX << " =? " << rhs.GetMinX()
167  << " Xmax= " << fMaxX << " =? " << rhs.GetMaxX()
168  << " Ymin= " << fMinY << " =? " << rhs.GetMinY()
169  << " Ymax= " << fMaxY << " =? " << rhs.GetMaxY()
170  << G4endl;
171  G4Exception("DicomPhantomZSliceHeader::operator+","",
173  }
174 
175  //----- Check that both slices has the same materials
176  std::vector<G4String> fMaterialNames2 = rhs.GetMaterialNames();
177  if( fMaterialNames.size() != fMaterialNames2.size() ) {
178  G4cerr << "DicomPhantomZSliceHeader error adding two slice headers:\
179  !!! Different number of materials: " << fMaterialNames.size() << " =? "
180  << fMaterialNames2.size() << G4endl;
181  G4Exception("DicomPhantomZSliceHeader::operator+","",
183  }
184  for( unsigned int ii = 0; ii < fMaterialNames.size(); ii++ ) {
185  if( fMaterialNames[ii] != fMaterialNames2[ii] ) {
186  G4cerr << "DicomPhantomZSliceHeader error adding two slice headers:\
187  !!! Different material number " << ii << " : "
188  << fMaterialNames[ii] << " =? "
189  << fMaterialNames2[ii] << G4endl;
190  G4Exception("DicomPhantomZSliceHeader::operator+","",
192  }
193  }
194 
195  //----- Check that the slices are contiguous in Z
196  if( std::fabs( fMinZ - rhs.GetMaxZ() ) >
198  std::fabs( fMaxZ - rhs.GetMinZ() ) >
200  G4cerr << "DicomPhantomZSliceHeader error adding two slice headers: !!!\
201  Slices are not contiguous in Z "
202  << " Zmin= " << fMinZ << " & " << rhs.GetMinZ()
203  << " Zmax= " << fMaxZ << " & " << rhs.GetMaxZ()
204  << G4endl;
205  G4Exception("DicomPhantomZSliceHeader::operator+","",
207  }
208 
209  //----- Build slice header copying first one
210  DicomPhantomZSliceHeader temp( *this );
211 
212  //----- Add data from second slice header
213  temp.SetMinZ( std::min( fMinZ, rhs.GetMinZ() ) );
214  temp.SetMaxZ( std::max( fMaxZ, rhs.GetMaxZ() ) );
215  temp.SetNoVoxelZ( fNoVoxelZ + rhs.GetNoVoxelZ() );
216 
217  return temp;
218 }
219 
220 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
222 {
223 
224  G4cout << "DicomPhantomZSliceHeader::Dumping Z Slice data to "
225  << fFilename << "..." << G4endl;
226  //sleep(5);
227 
228  // May seen counter-intuitive (dumping to file you are reading from), but
229  // the reason for this is modification slice spacing
230  if(fMateIDs.size() == 0 || fValues.size() == 0) { ReadDataFromFile(); }
231 
232 
233  std::ofstream out;
234  out.open(fFilename.c_str());
235 
236  if(!out) {
237  G4String descript = "DicomPhantomZSliceHeader::DumpToFile: could not open "
238  +fFilename;
239  G4Exception(descript.c_str(),"", FatalException, "");
240  }
241 
242 
243  out << fMaterialNames.size() << std::endl;
244  for(unsigned int i = 0; i < fMaterialNames.size(); ++i) {
245  out << i << " " << fMaterialNames.at(i) << std::endl;
246  }
247 
248  out << fNoVoxelX << " " << fNoVoxelY << " " << fNoVoxelZ << std::endl;
249  out << fMinX << " " << fMaxX << std::endl;
250  out << fMinY << " " << fMaxY << std::endl;
251  out << fMinZ << " " << fMaxZ << std::endl;
252 
253  for(unsigned int i = 0; i < fMateIDs.size(); ++i) { print(out,fMateIDs.at(i)," "); }
254  for(unsigned int i = 0; i < fValues.size(); ++i) { print(out,fValues.at(i)," ",6); }
255 
256  out.close();
257 
258 }
259 
260 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
262 {
263  std::ifstream in;
264  in.open(fFilename.c_str());
265 
266  if(!in) {
267  G4String descript = "DicomPhantomZSliceHeader::DumpToFile: could not open "
268  +fFilename;
269  G4Exception(descript.c_str(),"", FatalException, "");
270  }
271 
272  G4int nMaterials;
273  in >> nMaterials;
274 
275  fMaterialNames.resize(nMaterials,"");
276  for(G4int i = 0; i < nMaterials; ++i) {
277  G4String str1, str2;
278  in >> str1 >> str2;
279  if(!IsInteger(str1)) {
280  G4String descript = "String : " + str1 + " supposed to be integer";
281  G4Exception("DicomPhantomZSliceHeader::ReadDataFromFile - error in \
282  formatting: missing material index","", FatalException,descript.c_str());
283  }
284  G4int index = g4s2n<G4int>(str1);
285  if(index > nMaterials || index < 0) {
286  G4String descript = "Index : " + str1;
287  G4Exception("DicomPhantomZSliceHeader::ReadDataFromFile - error:\
288  bad material index","", FatalException,descript.c_str());
289  }
290  fMaterialNames[index] = str2;
291  }
292 
293  in >> fNoVoxelX >> fNoVoxelY >> fNoVoxelZ;
294 
295  G4double tmpMinX, tmpMinY, tmpMinZ;
296  G4double tmpMaxX, tmpMaxY, tmpMaxZ;
297 
298  in >> tmpMinX >> tmpMaxX;
299  in >> tmpMinY >> tmpMaxY;
300  in >> tmpMinZ >> tmpMaxZ;
301 
302  fMinX = (CheckConsistency(tmpMinX,fMinX,"Min X value")) ?
303  fMinX : ((fMinX == 0) ? tmpMinX : fMinX);
304  fMaxX = (CheckConsistency(tmpMaxX,fMaxX,"Max X value")) ?
305  fMaxX : ((fMaxX == 0) ? tmpMaxX : fMaxX);
306 
307  fMinY = (CheckConsistency(tmpMinY,fMinY,"Min Y value")) ?
308  fMinY : ((fMinY == 0) ? tmpMinY : fMinY);
309  fMaxY = (CheckConsistency(tmpMaxY,fMaxY,"Max Y value")) ?
310  fMaxY : ((fMaxY == 0) ? tmpMaxY : fMaxY);
311 
312  fMinZ = (CheckConsistency(tmpMinZ,fMinZ,"Min Z value")) ?
313  fMinZ : ((fMinZ == 0) ? tmpMinZ : fMinZ);
314  fMaxZ = (CheckConsistency(tmpMaxZ,fMaxZ,"Max Z value")) ?
315  fMaxZ : ((fMaxZ == 0) ? tmpMaxZ : fMaxZ);
316 
317  fMateIDs.clear();
318  fValues.clear();
319  fMateIDs.resize(fNoVoxelY*fNoVoxelZ,std::vector<G4int>(fNoVoxelX,0));
320  fValues.resize(fNoVoxelY*fNoVoxelZ,std::vector<G4double>(fNoVoxelX,0.));
321  for(G4int k = 0; k < fNoVoxelZ; ++k) {
322  for(G4int j = 0; j < fNoVoxelY; ++j) {
323  for(G4int i = 0; i < fNoVoxelX; ++i) {
324  G4int tmpMateID;
325  in >> tmpMateID;
326  G4int row = j*(k+1);
327  fMateIDs[row][i] = tmpMateID;
328  }
329  }
330  }
331 
332  for(G4int k = 0; k < fNoVoxelZ; ++k) {
333  for(G4int j = 0; j < fNoVoxelY; ++j) {
334  for(G4int i = 0; i < fNoVoxelX; ++i) {
335  G4double tmpValue;
336  in >> tmpValue;
337  G4int row = j*(k+1);
338  fValues[row][i] = tmpValue;
339  }
340  }
341  }
342 
343 
344  in.close();
345 }
346 
347 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
348 
349 
350 
void SetMinZ(const G4double &val)
std::vector< std::vector< G4int > > fMateIDs
const std::vector< G4String > & GetMaterialNames() const
static G4MaterialTable * GetMaterialTable()
Definition: G4Material.cc:588
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
DicomPhantomZSliceHeader class.
std::vector< G4String > fMaterialNames
void G4Exception(const char *originOfException, const char *exceptionCode, G4ExceptionSeverity severity, const char *comments)
Definition: G4Exception.cc:41
bool CheckConsistency(const T &, const T &, G4String)
T max(const T t1, const T t2)
brief Return the largest of the two arguments
std::vector< std::vector< G4double > > fValues
T min(const T t1, const T t2)
brief Return the smallest of the two arguments
#define G4endl
Definition: G4ios.hh:61
Definition of the DicomPhantomZSliceHeader class.
double G4double
Definition: G4Types.hh:76
void SetMaxZ(const G4double &val)
G4bool IsInteger(const G4String &)
void SetNoVoxelZ(const G4int &val)
G4bool CheckMaterialExists(const G4String &mateName)
static G4GeometryTolerance * GetInstance()
G4GLOB_DLL std::ostream G4cerr
void print(std::ostream &, const std::vector< T > &, const G4String &, G4int breakLine=-1)