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3 ] [ 10.5 ] [ 10.5.p1 ] [ 10.6 ] [ 10.6.p1 ] [ 10.6.p2 ] [ 10.6.p3 ] [ 10.7 ] [ 10.7.p1 ] [ 10.7.p2 ] [ 10.7.p3 ] [ 10.7.p4 ] [ 11.0 ] [ 11.0.p1 ] [ 11.0.p2 ] [ 11.0.p3, ] [ 11.0.p4 ] [ 11.1 ] [ 11.1.1 ] [ 11.1.2 ] [ 11.1.3 ] [ 11.2 ] [ 11.2.1 ] [ 11.2.2 ] [ 11.3.0 ]

  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 //
 27 /// \file medical/DICOM/include/DicomPhantomZSliceHeader.hh
 28 /// \brief Definition of the DicomPhantomZSliceHeader class
 29 //
 30 
 31 #ifndef DicomPhantomZSliceHeader_h
 32 #define DicomPhantomZSliceHeader_h 1
 33 
 34 #include "globals.hh"
 35 
 36 #include <fstream>
 37 #include <vector>
 38 
 39 class G4material;
 40 
 41 //*******************************************************
 42 /// DicomPhantomZSliceHeader class
 43 ///
 44 /// Contains the meta data information corresponding to one or several
 45 /// Z slices (number of voxels, dimension)
 46 ///
 47 /// History: 30.11.07  First version
 48 /// \author  P. Arce
 49 //*******************************************************
 50 
 51 class DicomPhantomZSliceHeader
 52 {
 53   public:
 54     DicomPhantomZSliceHeader(const G4String&);
 55 
 56     DicomPhantomZSliceHeader(const DicomPhantomZSliceHeader&) = default;
 57     // build object copying an existing one (except Z dimensions)
 58 
 59     DicomPhantomZSliceHeader(std::ifstream& fin);
 60     // build object reading data from a file
 61 
 62     ~DicomPhantomZSliceHeader();
 63 
 64     // Get and set methods
 65     G4int GetNoVoxelsX() const { return fNoVoxelsX; };
 66     G4int GetNoVoxelsY() const { return fNoVoxelsY; };
 67     G4int GetNoVoxelsZ() const { return fNoVoxelsZ; };
 68     G4int GetNoVoxels() const { return fNoVoxelsX * fNoVoxelsY * fNoVoxelsZ; };
 69 
 70     G4double GetMinX() const { return fMinX; };
 71     G4double GetMinY() const { return fMinY; };
 72     G4double GetMinZ() const { return fMinZ; };
 73     G4double GetMaxX() const { return fMaxX; };
 74     G4double GetMaxY() const { return fMaxY; };
 75     G4double GetMaxZ() const { return fMaxZ; };
 76 
 77     G4double GetVoxelHalfX() const { return (fMaxX - fMinX) / fNoVoxelsX / 2.; };
 78     G4double GetVoxelHalfY() const { return (fMaxY - fMinY) / fNoVoxelsY / 2.; };
 79     G4double GetVoxelHalfZ() const { return (fMaxZ - fMinZ) / fNoVoxelsZ / 2.; };
 80 
 81     const std::vector<G4String>& GetMaterialNames() const { return fMaterialNames; };
 82 
 83     void SetNoVoxelsX(const G4int& val) { fNoVoxelsX = val; }
 84     void SetNoVoxelsY(const G4int& val) { fNoVoxelsY = val; }
 85     void SetNoVoxelsZ(const G4int& val) { fNoVoxelsZ = 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     void operator+=(const DicomPhantomZSliceHeader& rhs);
 97     DicomPhantomZSliceHeader operator+(const DicomPhantomZSliceHeader& rhs);
 98     // add two slices that have the same dimensions, merging them in Z
 99 
100     //=================================================================
101     //  NEW REVISION ( Jonathan Madsen - jonathan.madsen@cern.ch )
102     //  December 2012
103     //
104     //  New data handling format -> movement away from file-based to class based
105     //      -- If density and mate ID data is not present -> read from file
106     //
107     //  REASONING:
108     //      --  DICOM data can contain inconsistencies, handling via class
109     //    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()
121     {
122       fValues.push_back(std::vector<G4double>(0));
123       fMateIDs.push_back(std::vector<G4int>(0));
124     }
125 
126     void AddValue(G4double val)
127     {
128       (fValues.size() > 0) ? fValues.back().push_back(val)
129                            : fValues.push_back(std::vector<G4double>(1, val));
130     }
131     void AddValue(const std::vector<G4double>& val) { fValues.push_back(val); }
132     void AddValue(const std::vector<std::vector<G4double>>& val)
133     {
134       for (unsigned int i = 0; i < val.size(); ++i) {
135         fValues.push_back(val.at(i));
136       }
137     }
138 
139     void AddMateID(G4int val)
140     {
141       (fMateIDs.size() > 0) ? fMateIDs.back().push_back(val)
142                             : fMateIDs.push_back(std::vector<G4int>(1, val));
143     }
144     void AddMateID(const std::vector<G4int>& val) { fMateIDs.push_back(val); }
145     void AddMateID(const std::vector<std::vector<G4int>>& val)
146     {
147       for (unsigned int i = 0; i < val.size(); ++i) {
148         fMateIDs.push_back(val.at(i));
149       }
150     }
151 
152     const std::vector<std::vector<G4double>>& GetValues() const { return fValues; }
153     const std::vector<std::vector<G4int>>& GetMateIDs() const { return fMateIDs; }
154 
155     void DumpToFile();
156     void ReadDataFromFile();
157 
158     void DumpExcessMemory()
159     {
160       if (fFilename.length() != 0) {
161         fValues.clear();
162         fMateIDs.clear();
163       }
164     }
165 
166     void FlipData();
167 
168   private:
169     inline G4bool IsInteger(const G4String&);
170     template<typename T>
171     inline void Print(std::ostream&, const std::vector<T>&, const G4String&, G4int breakLine = -1);
172     template<typename T>
173     inline T G4s2n(const G4String&);
174     template<typename T>
175     inline bool CheckConsistency(const T&, const T&, G4String);
176     //
177     //  END NEW REVISION
178     //=======================================================================
179 
180   private:
181     G4bool CheckMaterialExists(const G4String& mateName);
182     // check that material read exists as a G4Material
183 
184   private:
185     G4int fNoVoxelsX, fNoVoxelsY, fNoVoxelsZ;  // number of voxels in each dimensions
186     G4double fMinX, fMinY, fMinZ;  // minimum extension of voxels (position of wall)
187     G4double fMaxX, fMaxY, fMaxZ;  // maximum extension of voxels (position of wall)
188 
189     std::vector<G4String> fMaterialNames;  // list of material names
190 
191     G4String fFilename;
192     std::vector<std::vector<G4double>> fValues;
193     std::vector<std::vector<G4int>> fMateIDs;
194     G4double fSliceLocation;
195 };
196 
197 //============================================================================
198 // This function flips all the data
199 // Otherwise, the image is upside-down
200 inline void DicomPhantomZSliceHeader::FlipData()
201 {
202   std::reverse(fValues.begin(), fValues.end());
203   std::reverse(fMateIDs.begin(), fMateIDs.end());
204 }
205 //=============================================================================
206 inline G4bool DicomPhantomZSliceHeader::IsInteger(const G4String& str)
207 {
208   return (str.find_first_not_of("0123456789") == std::string::npos) ? true : false;
209 }
210 //============================================================================
211 template<typename T>
212 inline T DicomPhantomZSliceHeader::G4s2n(const G4String& str)
213 {
214   std::istringstream iss(str);
215   T val;
216   iss >> val;
217   return val;
218 }
219 
220 //============================================================================
221 template<typename T>
222 inline bool DicomPhantomZSliceHeader::CheckConsistency(const T& val1, const T& val2,
223                                                        G4String category)
224 {
225   if (val1 != val2) {
226     G4Exception("DicomPhantomSliceZHeader::CheckConsistency",
227                 "Consistency Mismatch : Keeping previous value if nonzero", JustWarning,
228                 category.c_str());
229     return false;
230   }
231   return true;
232 }
233 //============================================================================
234 template<typename T>
235 inline void DicomPhantomZSliceHeader::Print(std::ostream& out, const std::vector<T>& val,
236                                             const G4String& delim, G4int breakLine)
237 {
238   for (unsigned int i = 0; i < val.size(); ++i) {
239     out << val.at(i);
240     if (breakLine < 0) {
241       if (i + 1 < val.size()) {
242         out << delim;
243       }
244       else {
245         out << G4endl;
246       }
247     }
248     else {
249       ((i != 0 && i % breakLine == 0) ? (out << G4endl) : (out << delim));
250     }
251   }
252 }
253 //==========================================================================
254 
255 #endif
256