Geant4.10
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
DicomPhantomZSliceHeader.hh
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.hh 73076 2013-08-16 07:45:30Z gcosmo $
27 //
28 /// \file medical/DICOM/include/DicomPhantomZSliceHeader.hh
29 /// \brief Definition of the DicomPhantomZSliceHeader class
30 //
31 
32 #ifndef DicomPhantomZSliceHeader_h
33 #define DicomPhantomZSliceHeader_h 1
34 
35 #include "globals.hh"
36 class G4material;
37 #include <fstream>
38 #include <vector>
39 
40 //*******************************************************
41 /// DicomPhantomZSliceHeader class
42 ///
43 /// Contains the meta data information corresponding to one or several
44 /// Z slices (number of voxels, dimension)
45 ///
46 /// History: 30.11.07 First version
47 /// \author P. Arce
48 //*******************************************************
49 
51 {
52 public:
54 
56  // build object copying an existing one (except Z dimensions)
57 
58  DicomPhantomZSliceHeader( std::ifstream& fin );
59  // build object reading data from a file
60 
62 
63  // Get and set methods
64  G4int GetNoVoxelX() const { return fNoVoxelX; };
65  G4int GetNoVoxelY() const { return fNoVoxelY; };
66  G4int GetNoVoxelZ() const { return fNoVoxelZ; };
67  G4int GetNoVoxels() const { return fNoVoxelX*fNoVoxelY*fNoVoxelZ; };
68 
69  G4double GetMinX() const { return fMinX; };
70  G4double GetMinY() const { return fMinY; };
71  G4double GetMinZ() const { return fMinZ; };
72  G4double GetMaxX() const { return fMaxX; };
73  G4double GetMaxY() const { return fMaxY; };
74  G4double GetMaxZ() const { return fMaxZ; };
75 
76  G4double GetVoxelHalfX() const { return (fMaxX-fMinX)/fNoVoxelX/2.; };
77  G4double GetVoxelHalfY() const { return (fMaxY-fMinY)/fNoVoxelY/2.; };
78  G4double GetVoxelHalfZ() const { return (fMaxZ-fMinZ)/fNoVoxelZ/2.; };
79 
80  const std::vector<G4String>& GetMaterialNames() const { return fMaterialNames; };
81 
82 
83  void SetNoVoxelX(const G4int& val) { fNoVoxelX = val; }
84  void SetNoVoxelY(const G4int& val) { fNoVoxelY = val; }
85  void SetNoVoxelZ(const G4int& val) { fNoVoxelZ = val; }
86 
87  void SetMinX(const G4double& val) { fMinX = val; };
88  void SetMaxX(const G4double& val) { fMaxX = val; };
89  void SetMinY(const G4double& val) { fMinY = val; };
90  void SetMaxY(const G4double& val) { fMaxY = val; };
91  void SetMinZ(const G4double& val) { fMinZ = val; };
92  void SetMaxZ(const G4double& val) { fMaxZ = val; };
93 
94  void SetMaterialNames(std::vector<G4String>& mn ){ fMaterialNames = mn; }
95 
96 
97  void operator+=( const DicomPhantomZSliceHeader& rhs );
99  // add two slices that have the same dimensions, merging them in Z
100 
101  //===========================================================================================
102  // NEW REVISION ( Jonathan Madsen - jonathan.madsen@cern.ch )
103  // December 2012
104  //
105  // New data handling format -> movement away from file-based to class based
106  // -- If density and mate ID data is not present -> read from file
107  //
108  // REASONING:
109  // -- DICOM data can contain inconsistencies, handling via class instead of via file
110  // allows safe/easy modification
111  //
112  // Adding Data to densities and fMateIDs
113  //
114  void SetFilename(const G4String& val) { fFilename = val; }
115  void SetSliceLocation(const G4double& val) { fSliceLocation = val; }
116  void AddMaterial(const G4String& val) { fMaterialNames.push_back(val); }
117 
118  const G4double& GetSliceLocation() const { return fSliceLocation; }
119 
120  void AddRow() { fValues.push_back(std::vector<G4double>(0)); fMateIDs.push_back(std::vector<G4int>(0)); }
121 
122  void AddValue(G4double val) { (fValues.size() > 0) ? fValues.back().push_back(val) : fValues.push_back(std::vector<G4double>(1,val)); }
123  void AddValue(const std::vector<G4double>& val) { fValues.push_back(val); }
124  void AddValue(const std::vector<std::vector<G4double> >& val) {
125  for(unsigned int i = 0; i < val.size(); ++i) { fValues.push_back(val.at(i)); }
126  }
127 
128  void AddMateID(G4int val) { (fMateIDs.size() > 0) ? fMateIDs.back().push_back(val) : fMateIDs.push_back(std::vector<G4int>(1,val)); }
129  void AddMateID(const std::vector<G4int>& val) { fMateIDs.push_back(val); }
130  void AddMateID(const std::vector<std::vector<G4int> >& val) {
131  for(unsigned int i = 0; i < val.size(); ++i) { fMateIDs.push_back(val.at(i)); }
132  }
133 
134  const std::vector<std::vector<G4double> >& GetValues() const { return fValues; }
135  const std::vector<std::vector<G4int> >& GetMateIDs() const { return fMateIDs; }
136 
137  void DumpToFile();
138  void ReadDataFromFile();
139 
140  void DumpExcessMemory() { if(fFilename.length() != 0) { fValues.clear(); fMateIDs.clear(); } }
141 
142  void FlipData();
143 
144 private:
145  inline G4bool IsInteger(const G4String&);
146  template <typename T> inline void print(std::ostream&, const std::vector<T>&, const G4String&, G4int breakLine = -1);
147  template <typename T> inline T g4s2n(const G4String&);
148  template <typename T> inline bool CheckConsistency(const T&, const T&, G4String);
149  //
150  // END NEW REVISION
151  //===========================================================================================
152 
153 private:
154  G4bool CheckMaterialExists( const G4String& mateName );
155  // check that material read exists as a G4Material
156 
157 private:
158  G4int fNoVoxelX, fNoVoxelY, fNoVoxelZ; // number of voxels in each dimensions
159  G4double fMinX,fMinY,fMinZ; // minimum extension of voxels (position of wall)
160  G4double fMaxX,fMaxY,fMaxZ; // maximum extension of voxels (position of wall)
161 
162  std::vector<G4String> fMaterialNames; // list of material names
163 
164  G4String fFilename;
165  std::vector<std::vector<G4double> > fValues;
166  std::vector<std::vector<G4int> > fMateIDs;
167  G4double fSliceLocation;
168 
169 };
170 
171 //===========================================================================================
172 // This function flips all the data
173 // Otherwise, the image is upside-down
175 {
176  std::reverse(fValues.begin(), fValues.end());
177  std::reverse(fMateIDs.begin(), fMateIDs.end());
178 }
179 //===========================================================================================
180 inline G4bool DicomPhantomZSliceHeader::IsInteger(const G4String& str)
181 {
182  return (str.find_first_not_of("0123456789") == std::string::npos) ? true : false;
183 }
184 //===========================================================================================
185 template <typename T>
186 inline T DicomPhantomZSliceHeader::g4s2n(const G4String& str)
187 {
188  std::istringstream iss(str);
189  T val;
190  iss >> val;
191  return val;
192 }
193 //===========================================================================================
194 template <typename T>
195 inline bool DicomPhantomZSliceHeader::CheckConsistency(const T& val1, const T& val2, G4String category) {
196  if(val1 != val2) {
197  G4Exception("DicomPhantomSliceZHeader::CheckConsistency", "Consistency Mismatch : Keeping previous value if nonzero",
198  JustWarning, category.c_str());
199  return false;
200  }
201  return true;
202 }
203 //===========================================================================================
204 template <typename T>
205 inline void DicomPhantomZSliceHeader::print(std::ostream& out, const std::vector<T>& val, const G4String& delim, G4int breakLine)
206 {
207  for(unsigned int i = 0; i < val.size(); ++i) {
208  out << val.at(i);
209  if(breakLine < 0) {
210  if(i+1 < val.size()) { out << delim; }
211  else { out << G4endl; }
212  } else {
213  ((i != 0 && i%breakLine == 0) ? (out << G4endl) : (out << delim)); }
214  }
215 }
216 //===========================================================================================
217 
218 
219 #endif
void SetMaterialNames(std::vector< G4String > &mn)
void AddMateID(const std::vector< G4int > &val)
void SetMinZ(const G4double &val)
void AddValue(const std::vector< std::vector< G4double > > &val)
void SetMinX(const G4double &val)
void SetNoVoxelY(const G4int &val)
const std::vector< G4String > & GetMaterialNames() const
const G4double & GetSliceLocation() const
void SetNoVoxelX(const G4int &val)
DicomPhantomZSliceHeader operator+(const DicomPhantomZSliceHeader &rhs)
const std::vector< std::vector< G4int > > & GetMateIDs() const
void SetMinY(const G4double &val)
int G4int
Definition: G4Types.hh:78
DicomPhantomZSliceHeader(const G4String &)
void SetFilename(const G4String &val)
void operator+=(const DicomPhantomZSliceHeader &rhs)
void SetMaxX(const G4double &val)
void SetSliceLocation(const G4double &val)
bool G4bool
Definition: G4Types.hh:79
const std::vector< std::vector< G4double > > & GetValues() const
void G4Exception(const char *originOfException, const char *exceptionCode, G4ExceptionSeverity severity, const char *comments)
Definition: G4Exception.cc:41
void AddMateID(const std::vector< std::vector< G4int > > &val)
#define G4endl
Definition: G4ios.hh:61
void AddValue(const std::vector< G4double > &val)
double G4double
Definition: G4Types.hh:76
void SetMaxZ(const G4double &val)
void SetNoVoxelZ(const G4int &val)
void AddMaterial(const G4String &val)
void SetMaxY(const G4double &val)