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