Geant4  10.01.p03
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 84839 2014-10-21 13:44:55Z gcosmo $
27 //
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 //*******************************************************
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; };
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
110  // instead of via file
111  // allows safe/easy modification
112  //
113  // Adding Data to densities and fMateIDs
114  //
115  void SetFilename(const G4String& val) { fFilename = val; }
116  void SetSliceLocation(const G4double& val) { fSliceLocation = val; }
117  void AddMaterial(const G4String& val) { fMaterialNames.push_back(val); }
118 
119  const G4double& GetSliceLocation() const { return fSliceLocation; }
120 
121  void AddRow() { fValues.push_back(std::vector<G4double>(0));
122  fMateIDs.push_back(std::vector<G4int>(0)); }
123 
124  void AddValue(G4double val) { (fValues.size() > 0) ?
125  fValues.back().push_back(val) :
126  fValues.push_back(std::vector<G4double>(1,val)); }
127  void AddValue(const std::vector<G4double>& val) { fValues.push_back(val); }
128  void AddValue(const std::vector<std::vector<G4double> >& val) {
129  for(unsigned int i = 0; i < val.size(); ++i) { fValues.push_back(val.at(i)); }
130  }
131 
132  void AddMateID(G4int val) { (fMateIDs.size() > 0) ?
133  fMateIDs.back().push_back(val) :
134  fMateIDs.push_back(std::vector<G4int>(1,val)); }
135  void AddMateID(const std::vector<G4int>& val) { fMateIDs.push_back(val); }
136  void AddMateID(const std::vector<std::vector<G4int> >& val) {
137  for(unsigned int i = 0; i < val.size(); ++i) { fMateIDs.push_back(val.at(i)); }
138  }
139 
140  const std::vector<std::vector<G4double> >& GetValues() const { return fValues; }
141  const std::vector<std::vector<G4int> >& GetMateIDs() const { return fMateIDs; }
142 
143  void DumpToFile();
144  void ReadDataFromFile();
145 
147  if(fFilename.length() != 0) { fValues.clear(); fMateIDs.clear(); } }
148 
149  void FlipData();
150 
151 private:
152  inline G4bool IsInteger(const G4String&);
153  template <typename T>
154  inline void print(std::ostream&, const std::vector<T>&, const G4String&,
155  G4int breakLine = -1);
156  template <typename T> inline T g4s2n(const G4String&);
157  template <typename T> inline bool CheckConsistency(const T&, const T&, G4String);
158  //
159  // END NEW REVISION
160  //=======================================================================
161 
162 private:
163  G4bool CheckMaterialExists( const G4String& mateName );
164  // check that material read exists as a G4Material
165 
166 private:
167  G4int fNoVoxelX, fNoVoxelY, fNoVoxelZ; // number of voxels in each dimensions
168  G4double fMinX,fMinY,fMinZ; // minimum extension of voxels (position of wall)
169  G4double fMaxX,fMaxY,fMaxZ; // maximum extension of voxels (position of wall)
170 
171  std::vector<G4String> fMaterialNames; // list of material names
172 
174  std::vector<std::vector<G4double> > fValues;
175  std::vector<std::vector<G4int> > fMateIDs;
177 
178 };
179 
180 //============================================================================
181 // This function flips all the data
182 // Otherwise, the image is upside-down
184 {
185  std::reverse(fValues.begin(), fValues.end());
186  std::reverse(fMateIDs.begin(), fMateIDs.end());
187 }
188 //=============================================================================
190 {
191  return (str.find_first_not_of("0123456789") == std::string::npos) ? true : false;
192 }
193 //============================================================================
194 template <typename T>
196 {
197  std::istringstream iss(str);
198  T val;
199  iss >> val;
200  return val;
201 }
202 //============================================================================
203 template <typename T>
204 inline bool DicomPhantomZSliceHeader::CheckConsistency(const T& val1, const T& val2,
205  G4String category) {
206  if(val1 != val2) {
207  G4Exception("DicomPhantomSliceZHeader::CheckConsistency",
208  "Consistency Mismatch : Keeping previous value if nonzero",
209  JustWarning, category.c_str());
210  return false;
211  }
212  return true;
213 }
214 //============================================================================
215 template <typename T>
216 inline void DicomPhantomZSliceHeader::print(std::ostream& out, const std::vector<T>& val,
217  const G4String& delim, G4int breakLine)
218 {
219  for(unsigned int i = 0; i < val.size(); ++i) {
220  out << val.at(i);
221  if(breakLine < 0) {
222  if(i+1 < val.size()) { out << delim; }
223  else { out << G4endl; }
224  } else {
225  ((i != 0 && i%breakLine == 0) ? (out << G4endl) : (out << delim)); }
226  }
227 }
228 //==========================================================================
229 
230 #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)
std::vector< std::vector< G4int > > fMateIDs
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
DicomPhantomZSliceHeader class.
const std::vector< std::vector< G4double > > & GetValues() const
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)
std::vector< std::vector< G4double > > fValues
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)
G4bool IsInteger(const G4String &)
void SetNoVoxelZ(const G4int &val)
G4bool CheckMaterialExists(const G4String &mateName)
void AddMaterial(const G4String &val)
void SetMaxY(const G4double &val)
void print(std::ostream &, const std::vector< T > &, const G4String &, G4int breakLine=-1)