Geant4  10.03.p02
 All Classes 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 92820 2015-09-17 15:22:14Z 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; };
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  void SetNoVoxelX(const G4int& val) { fNoVoxelX = val; }
83  void SetNoVoxelY(const G4int& val) { fNoVoxelY = val; }
84  void SetNoVoxelZ(const G4int& val) { fNoVoxelZ = val; }
85 
86  void SetMinX(const G4double& val) { fMinX = val; };
87  void SetMaxX(const G4double& val) { fMaxX = val; };
88  void SetMinY(const G4double& val) { fMinY = val; };
89  void SetMaxY(const G4double& val) { fMaxY = val; };
90  void SetMinZ(const G4double& val) { fMinZ = val; };
91  void SetMaxZ(const G4double& val) { fMaxZ = val; };
92 
93  void SetMaterialNames(std::vector<G4String>& mn ){ fMaterialNames = mn; }
94 
95  void operator+=( const DicomPhantomZSliceHeader& rhs );
97  // add two slices that have the same dimensions, merging them in Z
98 
99  //=================================================================
100  // NEW REVISION ( Jonathan Madsen - jonathan.madsen@cern.ch )
101  // December 2012
102  //
103  // New data handling format -> movement away from file-based to class based
104  // -- If density and mate ID data is not present -> read from file
105  //
106  // REASONING:
107  // -- DICOM data can contain inconsistencies, handling via class
108  // instead of via file
109  // allows safe/easy modification
110  //
111  // Adding Data to densities and fMateIDs
112  //
113  void SetFilename(const G4String& val) { fFilename = val; }
114  void SetSliceLocation(const G4double& val) { fSliceLocation = val; }
115  void AddMaterial(const G4String& val) { fMaterialNames.push_back(val); }
116 
117  const G4double& GetSliceLocation() const { return fSliceLocation; }
118 
119  void AddRow() { fValues.push_back(std::vector<G4double>(0));
120  fMateIDs.push_back(std::vector<G4int>(0)); }
121 
122  void AddValue(G4double val) { (fValues.size() > 0) ?
123  fValues.back().push_back(val) :
124  fValues.push_back(std::vector<G4double>(1,val)); }
125  void AddValue(const std::vector<G4double>& val) { fValues.push_back(val); }
126  void AddValue(const std::vector<std::vector<G4double> >& val) {
127  for(unsigned int i = 0; i < val.size(); ++i) { fValues.push_back(val.at(i)); }
128  }
129 
130  void AddMateID(G4int val) { (fMateIDs.size() > 0) ?
131  fMateIDs.back().push_back(val) :
132  fMateIDs.push_back(std::vector<G4int>(1,val)); }
133  void AddMateID(const std::vector<G4int>& val) { fMateIDs.push_back(val); }
134  void AddMateID(const std::vector<std::vector<G4int> >& val) {
135  for(unsigned int i = 0; i < val.size(); ++i) { fMateIDs.push_back(val.at(i)); }
136  }
137 
138  const std::vector<std::vector<G4double> >& GetValues() const { return fValues; }
139  const std::vector<std::vector<G4int> >& GetMateIDs() const { return fMateIDs; }
140 
141  void DumpToFile();
142  void ReadDataFromFile();
143 
145  if(fFilename.length() != 0) { fValues.clear(); fMateIDs.clear(); } }
146 
147  void FlipData();
148 
149 private:
150  inline G4bool IsInteger(const G4String&);
151  template <typename T>
152  inline void Print(std::ostream&, const std::vector<T>&, const G4String&,
153  G4int breakLine = -1);
154  template <typename T> inline T G4s2n(const G4String&);
155  template <typename T> inline bool CheckConsistency(const T&, const T&, G4String);
156  //
157  // END NEW REVISION
158  //=======================================================================
159 
160 private:
161  G4bool CheckMaterialExists( const G4String& mateName );
162  // check that material read exists as a G4Material
163 
164 private:
165  G4int fNoVoxelX, fNoVoxelY, fNoVoxelZ; // number of voxels in each dimensions
166  G4double fMinX,fMinY,fMinZ; // minimum extension of voxels (position of wall)
167  G4double fMaxX,fMaxY,fMaxZ; // maximum extension of voxels (position of wall)
168 
169  std::vector<G4String> fMaterialNames; // list of material names
170 
171  G4String fFilename;
172  std::vector<std::vector<G4double> > fValues;
173  std::vector<std::vector<G4int> > fMateIDs;
174  G4double fSliceLocation;
175 
176 };
177 
178 //============================================================================
179 // This function flips all the data
180 // Otherwise, the image is upside-down
182 {
183  std::reverse(fValues.begin(), fValues.end());
184  std::reverse(fMateIDs.begin(), fMateIDs.end());
185 }
186 //=============================================================================
187 inline G4bool DicomPhantomZSliceHeader::IsInteger(const G4String& str)
188 {
189  return (str.find_first_not_of("0123456789") == std::string::npos) ? true : false;
190 }
191 //============================================================================
192 template <typename T>
193 inline T DicomPhantomZSliceHeader::G4s2n(const G4String& str)
194 {
195  std::istringstream iss(str);
196  T val;
197  iss >> val;
198  return val;
199 }
200 
201 //============================================================================
202 template <typename T>
203 inline bool DicomPhantomZSliceHeader::CheckConsistency(const T& val1, const T& val2,
204  G4String category) {
205  if(val1 != val2) {
206  G4Exception("DicomPhantomSliceZHeader::CheckConsistency",
207  "Consistency Mismatch : Keeping previous value if nonzero",
208  JustWarning, category.c_str());
209  return false;
210  }
211  return true;
212 }
213 //============================================================================
214 template <typename T>
215 inline void DicomPhantomZSliceHeader::Print(std::ostream& out, const std::vector<T>& val,
216  const G4String& delim, G4int breakLine)
217 {
218  for(unsigned int i = 0; i < val.size(); ++i) {
219  out << val.at(i);
220  if(breakLine < 0) {
221  if(i+1 < val.size()) { out << delim; }
222  else { out << G4endl; }
223  } else {
224  ((i != 0 && i%breakLine == 0) ? (out << G4endl) : (out << delim)); }
225  }
226 }
227 //==========================================================================
228 
229 #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)