GGEMS  1.1
GPU GEant4-based Monte Carlo Simulations
GGEMSMHDImage.hh
Go to the documentation of this file.
1 #ifndef GUARD_GGEMS_IO_GGEMSMHDIMAGE_HH
2 #define GUARD_GGEMS_IO_GGEMSMHDIMAGE_HH
3 
4 // ************************************************************************
5 // * This file is part of GGEMS. *
6 // * *
7 // * GGEMS is free software: you can redistribute it and/or modify *
8 // * it under the terms of the GNU General Public License as published by *
9 // * the Free Software Foundation, either version 3 of the License, or *
10 // * (at your option) any later version. *
11 // * *
12 // * GGEMS is distributed in the hope that it will be useful, *
13 // * but WITHOUT ANY WARRANTY; without even the implied warranty of *
14 // * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
15 // * GNU General Public License for more details. *
16 // * *
17 // * You should have received a copy of the GNU General Public License *
18 // * along with GGEMS. If not, see <https://www.gnu.org/licenses/>. *
19 // * *
20 // ************************************************************************
21 
34 #ifdef _MSC_VER
35 #pragma warning(disable: 4251) // Deleting warning exporting STL members!!!
36 #endif
37 
38 #include <fstream>
39 
41 
46 class GGEMS_EXPORT GGEMSMHDImage
47 {
48  public:
52  GGEMSMHDImage(void);
53 
57  ~GGEMSMHDImage(void);
58 
59  public:
65  GGEMSMHDImage(GGEMSMHDImage const& mhd) = delete;
66 
72  GGEMSMHDImage& operator=(GGEMSMHDImage const& mhd) = delete;
73 
79  GGEMSMHDImage(GGEMSMHDImage const&& mhd) = delete;
80 
86  GGEMSMHDImage& operator=(GGEMSMHDImage const&& mhd) = delete;
87 
93  void SetOutputFileName(std::string const& basename);
94 
102  void Read(std::string const& image_mhd_header_filename, cl::Buffer* solid_data, GGsize const& thread_index);
103 
110  void Write(cl::Buffer* image, GGsize const& thread_index) const;
111 
118  template<typename T>
119  void Write(T* image);
120 
126  void SetElementSizes(GGfloat3 const& element_sizes);
127 
133  void SetDimensions(GGsize3 const& dimensions);
134 
140  void SetDataType(std::string const& data_type);
141 
147  inline std::string GetDataMHDType(void) const {return mhd_data_type_;};
148 
154  inline std::string GetRawMDHfilename(void) const {return mhd_raw_file_;};
155 
161  inline std::string GetOutputDirectory(void) const {return output_dir_;};
162 
163  private:
168  void CheckParameters(void) const;
169 
177  template <typename T>
178  void WriteRaw(cl::Buffer* image, GGsize const& thread_index) const;
179 
180  private:
181  std::string mhd_header_file_;
182  std::string mhd_raw_file_;
183  std::string output_dir_;
184  std::string mhd_data_type_;
187 };
188 
192 
193 template <typename T>
195 {
196  GGcout("GGEMSMHDImage", "Write", 1) << "Writing MHD Image " << mhd_header_file_ << "..." << GGendl;
197 
198  // Checking parameters before to write
199  CheckParameters();
200 
201  // header data
202  std::ofstream out_header_stream(mhd_header_file_, std::ios::out);
203  out_header_stream << "ObjectType = Image" << std::endl;
204  out_header_stream << "BinaryDataByteOrderMSB = False" << std::endl;
205  out_header_stream << "NDims = 3" << std::endl;
206  out_header_stream << "ElementSpacing = " << element_sizes_.x << " " << element_sizes_.y << " " << element_sizes_.z << std::endl;
207  out_header_stream << "DimSize = " << dimensions_.x_ << " " << dimensions_.y_ << " " << dimensions_.z_ << std::endl;
208  out_header_stream << "ElementType = " << mhd_data_type_ << std::endl;
209  out_header_stream << "ElementDataFile = " << mhd_raw_file_ << std::endl;
210  out_header_stream.close();
211 
212  // raw data
213  std::ofstream out_raw_stream(output_dir_+mhd_raw_file_, std::ios::out | std::ios::binary);
214 
215  // Writing data on file
216  out_raw_stream.write(reinterpret_cast<char*>(image), static_cast<std::streamsize>(dimensions_.x_ * dimensions_.y_* dimensions_.z_ * sizeof(T)));
217 
218  out_raw_stream.close();
219 }
220 
224 
225 template <typename T>
226 void GGEMSMHDImage::WriteRaw(cl::Buffer* image, GGsize const& thread_index) const
227 {
228  // Get the OpenCL manager
230 
231  // raw data
232  std::ofstream out_raw_stream(output_dir_+mhd_raw_file_, std::ios::out | std::ios::binary);
233 
234  // Mapping data
235  T* data_image_device = opencl_manager.GetDeviceBuffer<T>(image, dimensions_.x_ * dimensions_.y_ * dimensions_.z_ * sizeof(T), thread_index);
236 
237  // Writing data on file
238  out_raw_stream.write(reinterpret_cast<char*>(data_image_device), static_cast<std::streamsize>(dimensions_.x_ * dimensions_.y_* dimensions_.z_ * sizeof(T)));
239 
240  // Release the pointers
241  opencl_manager.ReleaseDeviceBuffer(image, data_image_device, thread_index);
242  out_raw_stream.close();
243 }
244 
245 #endif // End of GUARD_GGEMS_IO_GGEMSMHDIMAGE_HH
GGEMSMHDImage
I/O class handling MHD file.
Definition: GGEMSMHDImage.hh:47
GGsize3_t
Structure storing 3 GGsize values.
Definition: GGEMSTypes.hh:267
GGEMSMHDImage::CheckParameters
void CheckParameters(void) const
Check parameters before read/write MHD file.
Definition: GGEMSMHDImage.hh:161
GGEMSMHDImage::mhd_data_type_
std::string mhd_data_type_
Definition: GGEMSMHDImage.hh:184
GGEMSMHDImage::GetRawMDHfilename
std::string GetRawMDHfilename(void) const
get the filename of raw data
Definition: GGEMSMHDImage.hh:154
GGEMSOpenCLManager::ReleaseDeviceBuffer
void ReleaseDeviceBuffer(cl::Buffer *const device_ptr, T *host_ptr, GGsize const &thread_index)
Get the device pointer on host to write on it. Mandatory after a GetDeviceBufferWrite ou GetDeviceBuf...
Definition: GGEMSOpenCLManager.hh:495
GGEMSMHDImage::operator=
GGEMSMHDImage & operator=(GGEMSMHDImage const &&mhd)=delete
Avoid copy of the class by rvalue reference.
GGEMSMHDImage::operator=
GGEMSMHDImage & operator=(GGEMSMHDImage const &mhd)=delete
Avoid assignement of the class by reference.
GGEMSMHDImage::WriteRaw
void WriteRaw(cl::Buffer *image, GGsize const &thread_index) const
write the raw data to file
Definition: GGEMSMHDImage.hh:226
GGEMSMHDImage::GetOutputDirectory
std::string GetOutputDirectory(void) const
get the output directory
Definition: GGEMSMHDImage.hh:161
GGsize
#define GGsize
Definition: GGEMSTypes.hh:252
GGEMSMHDImage::Write
void Write(cl::Buffer *image, GGsize const &thread_index) const
Write mhd header/raw file.
Definition: GGEMSMHDImage.cc:223
GGEMSMHDImage::mhd_raw_file_
std::string mhd_raw_file_
Definition: GGEMSMHDImage.hh:182
GGsize3_t::z_
GGsize z_
Definition: GGEMSTypes.hh:270
GGEMSMHDImage::GGEMSMHDImage
GGEMSMHDImage(GGEMSMHDImage const &&mhd)=delete
Avoid copy of the class by rvalue reference.
GGfloat3
#define GGfloat3
Definition: GGEMSTypes.hh:275
GGcout
GGEMSStream GGcout
Definition: GGEMSPrint.cc:34
GGEMSMHDImage::GGEMSMHDImage
GGEMSMHDImage(GGEMSMHDImage const &mhd)=delete
Avoid copy of the class by reference.
GGEMSOpenCLManager.hh
Singleton class storing all informations about OpenCL and managing GPU/CPU devices,...
GGsize3_t::x_
GGsize x_
Definition: GGEMSTypes.hh:268
GGEMSOpenCLManager::GetDeviceBuffer
T * GetDeviceBuffer(cl::Buffer *device_ptr, GGsize const &size, GGsize const &thread_index)
Get the device pointer on host to write on it. ReleaseDeviceBuffer must be used after this method!...
Definition: GGEMSOpenCLManager.hh:480
GGendl
#define GGendl
overload C++ std::endl
Definition: GGEMSPrint.hh:60
GGEMSMHDImage::output_dir_
std::string output_dir_
Definition: GGEMSMHDImage.hh:183
GGEMSOpenCLManager
Singleton class storing all informations about OpenCL and managing GPU/CPU devices,...
Definition: GGEMSOpenCLManager.hh:54
GGEMSMHDImage::mhd_header_file_
std::string mhd_header_file_
Definition: GGEMSMHDImage.hh:181
T
__constant GGfloat T
Definition: GGEMSSystemOfUnits.hh:150
GGEMSMHDImage::dimensions_
GGsize3 dimensions_
Definition: GGEMSMHDImage.hh:186
GGEMSOpenCLManager::GetInstance
static GGEMSOpenCLManager & GetInstance(void)
Create at first time the Singleton.
Definition: GGEMSOpenCLManager.hh:72
GGsize3_t::y_
GGsize y_
Definition: GGEMSTypes.hh:269
GGEMSMHDImage::GetDataMHDType
std::string GetDataMHDType(void) const
get the mhd data type
Definition: GGEMSMHDImage.hh:147
GGEMSMHDImage::element_sizes_
GGfloat3 element_sizes_
Definition: GGEMSMHDImage.hh:185