GGEMS  1.1
GPU GEant4-based Monte Carlo Simulations
GGEMSVolumeCreatorManager.cc
Go to the documentation of this file.
1 // ************************************************************************
2 // * This file is part of GGEMS. *
3 // * *
4 // * GGEMS is free software: you can redistribute it and/or modify *
5 // * it under the terms of the GNU General Public License as published by *
6 // * the Free Software Foundation, either version 3 of the License, or *
7 // * (at your option) any later version. *
8 // * *
9 // * GGEMS is distributed in the hope that it will be useful, *
10 // * but WITHOUT ANY WARRANTY; without even the implied warranty of *
11 // * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
12 // * GNU General Public License for more details. *
13 // * *
14 // * You should have received a copy of the GNU General Public License *
15 // * along with GGEMS. If not, see <https://www.gnu.org/licenses/>. *
16 // * *
17 // ************************************************************************
18 
34 
38 
40 : number_elements_(0),
41  data_type_("MET_FLOAT"),
42  output_image_filename_(""),
43  output_range_to_material_filename_(""),
44  voxelized_volume_(nullptr)
45 {
46  GGcout("GGEMSVolumeCreatorManager", "GGEMSVolumeCreatorManager", 3) << "GGEMSVolumeCreatorManager creating..." << GGendl;
47 
48  element_sizes_.x = 0.0f;
49  element_sizes_.y = 0.0f;
50  element_sizes_.z = 0.0f;
51 
55 
56  GGcout("GGEMSVolumeCreatorManager", "GGEMSVolumeCreatorManager", 3) << "GGEMSVolumeCreatorManager created!!!" << GGendl;
57 }
58 
62 
64 {
65  GGcout("GGEMSVolumeCreatorManager", "~GGEMSVolumeCreatorManager", 3) << "GGEMSVolumeCreatorManager erasing..." << GGendl;
66 
67  GGcout("GGEMSVolumeCreatorManager", "~GGEMSVolumeCreatorManager", 3) << "GGEMSVolumeCreatorManager erased!!!" << GGendl;
68 }
69 
73 
75 {
76  GGcout("GGEMSVolumeCreatorManager", "Clean", 3) << "GGEMSVolumeCreatorManager cleaning..." << GGendl;
77 
79 
80  if (voxelized_volume_) {
81  opencl_manager.Deallocate(voxelized_volume_, number_elements_ * sizeof(T), 0);
82  voxelized_volume_ = nullptr;
83  }
84 
85  GGcout("GGEMSVolumeCreatorManager", "Clean", 3) << "GGEMSVolumeCreatorManager cleaned!!!" << GGendl;
86 }
87 
91 
92 void GGEMSVolumeCreatorManager::SetElementSizes(GGfloat const& voxel_width, GGfloat const& voxel_height, GGfloat const& voxel_depth, std::string const& unit)
93 {
94  element_sizes_.x = DistanceUnit(voxel_width, unit);
95  element_sizes_.y = DistanceUnit(voxel_height, unit);
96  element_sizes_.z = DistanceUnit(voxel_depth, unit);
97 }
98 
102 
103 void GGEMSVolumeCreatorManager::SetVolumeDimensions(GGsize const& volume_width, GGsize const& volume_height, GGsize const& volume_depth)
104 {
105  volume_dimensions_.x_ = volume_width;
106  volume_dimensions_.y_ = volume_height;
107  volume_dimensions_.z_ = volume_depth;
108  number_elements_ = volume_width * volume_height * volume_depth;
109 }
110 
114 
115 void GGEMSVolumeCreatorManager::SetMaterial(std::string const& material)
116 {
117  // Store the material in map
118  label_to_material_.insert(std::make_pair(0.0f, material));
119 }
120 
124 
125 void GGEMSVolumeCreatorManager::SetDataType(std::string const& data_type)
126 {
127  data_type_ = data_type;
128 
129  // Convert raw data to material id data
130  if (data_type_.compare("MET_CHAR") && data_type_.compare("MET_UCHAR") && data_type_.compare("MET_SHORT") && data_type_.compare("MET_USHORT") && data_type_.compare("MET_INT") && data_type_.compare("MET_UINT") && data_type_.compare("MET_FLOAT")) {
131  std::ostringstream oss(std::ostringstream::out);
132  oss << "Your type in not compatible. The type has to be:" << std::endl;
133  oss << " - MET_CHAR" << std::endl;
134  oss << " - MET_UCHAR" << std::endl;
135  oss << " - MET_SHORT" << std::endl;
136  oss << " - MET_USHORT" << std::endl;
137  oss << " - MET_INT" << std::endl;
138  oss << " - MET_UINT" << std::endl;
139  oss << " - MET_FLOAT" << std::endl;
140  GGEMSMisc::ThrowException("GGEMSVolumeCreatorManager", "SetDataType", oss.str());
141  }
142 }
143 
147 
148 void GGEMSVolumeCreatorManager::AddLabelAndMaterial(GGfloat const& label, std::string const& material)
149 {
150  GGcout("GGEMSVolumeCreatorManager", "AddLabelAndMaterial", 3) << "Adding new material and label..." << GGendl;
151 
152  // Insert label and check if the label exists already
153  auto const [iter, success] = label_to_material_.insert(std::make_pair(label, material));
154  if (!success) {
155  std::ostringstream oss(std::ostringstream::out);
156  oss << "The label: " << iter->first << " already exists...";
157  GGEMSMisc::ThrowException("GGEMSVolumeCreatorManager", "AddLabelAndMaterial", oss.str());
158  }
159 }
160 
164 
165 void GGEMSVolumeCreatorManager::SetOutputImageFilename(std::string const& output_image_filename)
166 {
167  output_image_filename_ = output_image_filename;
168 }
169 
173 
174 void GGEMSVolumeCreatorManager::SetRangeToMaterialDataFilename(std::string const& output_range_to_material_filename)
175 {
176  output_range_to_material_filename_ = output_range_to_material_filename;
177 }
178 
182 
184 {
185  GGcout("GGEMSVolumeCreatorManager", "CheckParameters", 3) << "Checking parameters for phantom creator manager..." << GGendl;
186 
187  // Checking phantom dimensions
188  if (volume_dimensions_.x_ == 0 && volume_dimensions_.y_ == 0 && volume_dimensions_.z_ == 0) {
189  GGEMSMisc::ThrowException("GGEMSVolumeCreatorManager", "CheckParameters", "Phantom dimensions have to be > 0!!!");
190  }
191 
192  // Checking size of voxels
193  if (element_sizes_.x == 0.0f && element_sizes_.y == 0.0f && element_sizes_.z == 0.0f) {
194  GGEMSMisc::ThrowException("GGEMSVolumeCreatorManager", "CheckParameters", "Phantom voxel sizes have to be > 0.0!!!");
195  }
196 
197  // Checking output name
198  if (output_image_filename_.empty()) {
199  GGEMSMisc::ThrowException("GGEMSVolumeCreatorManager", "CheckParameters", "A output image filename has to be done to phantom manager!!!");
200  }
201 
202  // Checking range to material data name
204  GGEMSMisc::ThrowException("GGEMSVolumeCreatorManager", "CheckParameters", "A output range to material data filename has to be done to phantom manager!!!");
205  }
206 }
207 
211 
213 {
214  GGcout("GGEMSVolumeCreatorManager", "Initialize", 3) << "Initializing phantom creator manager..." << GGendl;
215 
216  // Check mandatory parameters
217  CheckParameters();
218 
219  if (!data_type_.compare("MET_CHAR")) AllocateImage<char>();
220  else if (!data_type_.compare("MET_UCHAR")) AllocateImage<unsigned char>();
221  else if (!data_type_.compare("MET_SHORT")) AllocateImage<GGshort>();
222  else if (!data_type_.compare("MET_USHORT")) AllocateImage<GGushort>();
223  else if (!data_type_.compare("MET_INT")) AllocateImage<GGint>();
224  else if (!data_type_.compare("MET_UINT")) AllocateImage<GGuint>();
225  else if (!data_type_.compare("MET_FLOAT")) AllocateImage<GGfloat>();
226 }
227 
231 
233 {
234  // Writing output image
235  WriteMHDImage();
236 
237  // Writing the range to material file
239 }
240 
244 
246 {
247  GGcout("GGEMSVolumeCreatorManager", "WriteRangeToMaterialFile", 3) << "Writing range to material text file..." << GGendl;
248 
249  GGcout("GGEMSVolumeCreatorManager", "WriteRangeToMaterialFile", 0) << "List of label and material:" << GGendl;
250  for(auto&& i : label_to_material_) {
251  GGcout("GGEMSVolumeCreatorManager", "WriteRangeToMaterialFile", 0) << " * Material: " << i.second << ", label: " << i.first << GGendl;
252  }
253 
254  // Write file
255  std::ofstream range_to_data_stream(output_range_to_material_filename_, std::ios::out);
256  for(auto&& i : label_to_material_) {
257  range_to_data_stream << i.first << " " << i.first << " " << i.second << std::endl;
258  }
259  range_to_data_stream.close();
260 }
261 
265 
267 {
268  GGcout("GGEMSVolumeCreatorManager", "WriteMHDImage", 3) << "Writing MHD output file..." << GGendl;
269 
270  // Write MHD file
271  GGEMSMHDImage mhdImage;
273  mhdImage.SetDataType(data_type_);
276  mhdImage.Write(voxelized_volume_, 0);
277 }
278 
282 
284 {
286 }
287 
291 
292 void set_volume_dimension_volume_creator_manager(GGEMSVolumeCreatorManager* volume_creator_manager, GGsize const volume_width, GGsize const volume_height, GGsize const volume_depth)
293 {
294  volume_creator_manager->SetVolumeDimensions(volume_width, volume_height, volume_depth);
295 }
296 
300 
301 void set_element_sizes_volume_creator_manager(GGEMSVolumeCreatorManager* volume_creator_manager, GGfloat const voxel_width, GGfloat const voxel_height, GGfloat const voxel_depth, char const* unit)
302 {
303  volume_creator_manager->SetElementSizes(voxel_width, voxel_height, voxel_depth, unit);
304 }
305 
309 
310 void set_output_image_filename_volume_creator_manager(GGEMSVolumeCreatorManager* volume_creator_manager, char const* output_image_filename)
311 {
312  volume_creator_manager->SetOutputImageFilename(output_image_filename);
313 }
314 
318 
319 void set_output_range_to_material_filename_volume_creator_manager(GGEMSVolumeCreatorManager* volume_creator_manager,char const* output_range_to_material_filename)
320 {
321  volume_creator_manager->SetRangeToMaterialDataFilename(output_range_to_material_filename);
322 }
323 
327 
329 {
330  volume_creator_manager->Initialize();
331 }
332 
336 
338 {
339  volume_creator_manager->Write();
340 }
341 
345 
346 void set_material_volume_creator_manager(GGEMSVolumeCreatorManager* volume_creator_manager, char const* material)
347 {
348  volume_creator_manager->SetMaterial(material);
349 }
350 
354 
355 void set_data_type_volume_creator_manager(GGEMSVolumeCreatorManager* volume_creator_manager, char const* data_type)
356 {
357  volume_creator_manager->SetDataType(data_type);
358 }
GGEMSVolumeCreatorManager::element_sizes_
GGfloat3 element_sizes_
Definition: GGEMSVolumeCreatorManager.hh:238
GGEMSMHDImage
I/O class handling MHD file.
Definition: GGEMSMHDImage.hh:47
GGEMSVolumeCreatorManager::AddLabelAndMaterial
void AddLabelAndMaterial(GGfloat const &label, std::string const &material)
add the label and the material
Definition: GGEMSVolumeCreatorManager.cc:148
initialize_volume_creator_manager
void initialize_volume_creator_manager(GGEMSVolumeCreatorManager *volume_creator_manager)
Initialize the volume creator manager.
Definition: GGEMSVolumeCreatorManager.cc:328
GGEMSVolumeCreatorManager::SetElementSizes
void SetElementSizes(GGfloat const &voxel_width, GGfloat const &voxel_height, GGfloat const &voxel_depth, std::string const &unit="mm")
Set the size of the elements for the voxelized volume.
Definition: GGEMSVolumeCreatorManager.cc:92
GGEMSVolumeCreatorManager::WriteMHDImage
void WriteMHDImage(void) const
Write output MHD file.
Definition: GGEMSVolumeCreatorManager.cc:266
set_material_volume_creator_manager
void set_material_volume_creator_manager(GGEMSVolumeCreatorManager *volume_creator_manager, char const *material)
set the material of the global (background volume)
Definition: GGEMSVolumeCreatorManager.cc:346
GGEMSVolumeCreatorManager::GetInstance
static GGEMSVolumeCreatorManager & GetInstance(void)
Create at first time the Singleton.
Definition: GGEMSVolumeCreatorManager.hh:67
GGEMSMHDImage::SetDataType
void SetDataType(std::string const &data_type)
set the type of data
Definition: GGEMSMHDImage.cc:111
GGEMSVolumeCreatorManager::~GGEMSVolumeCreatorManager
~GGEMSVolumeCreatorManager(void)
Unable the destructor for the user.
Definition: GGEMSVolumeCreatorManager.cc:63
GGEMSVolumeCreatorManager::SetMaterial
void SetMaterial(std::string const &material="Air")
set the material, Air by default
Definition: GGEMSVolumeCreatorManager.hh:133
GGEMSMHDImage::SetElementSizes
void SetElementSizes(GGfloat3 const &element_sizes)
set the size of the elements
Definition: GGEMSMHDImage.cc:102
set_element_sizes_volume_creator_manager
void set_element_sizes_volume_creator_manager(GGEMSVolumeCreatorManager *volume_creator_manager, GGfloat const voxel_width, GGfloat const voxel_height, GGfloat const voxel_depth, char const *unit)
Set the size of the elements for the voxelized volume.
Definition: GGEMSVolumeCreatorManager.cc:301
GGEMSVolumeCreatorManager::WriteRangeToMaterialFile
void WriteRangeToMaterialFile(void)
Write the file with range to material data.
Definition: GGEMSVolumeCreatorManager.cc:245
write_volume_creator_manager
void write_volume_creator_manager(GGEMSVolumeCreatorManager *volume_creator_manager)
Save the voxelized volume to raw data in mhd file.
Definition: GGEMSVolumeCreatorManager.cc:337
GGEMSOpenCLManager::Deallocate
void Deallocate(cl::Buffer *buffer, GGsize size, GGsize const &thread_index, std::string const &class_name="Undefined")
Deallocation of OpenCL memory.
Definition: GGEMSOpenCLManager.cc:981
GGEMSVolumeCreatorManager::number_elements_
GGsize number_elements_
Definition: GGEMSVolumeCreatorManager.hh:240
GGEMSVolumeCreatorManager::Write
void Write(void)
Save the voxelized volume to raw data in mhd file.
Definition: GGEMSVolumeCreatorManager.cc:232
GGEMSVolumeCreatorManager::volume_dimensions_
GGsize3 volume_dimensions_
Definition: GGEMSVolumeCreatorManager.hh:239
GGsize
#define GGsize
Definition: GGEMSTypes.hh:252
GGEMSVolumeCreatorManager::Clean
void Clean(void)
clean OpenCL data
Definition: GGEMSVolumeCreatorManager.cc:74
GGEMSMHDImage::Write
void Write(cl::Buffer *image, GGsize const &thread_index) const
Write mhd header/raw file.
Definition: GGEMSMHDImage.cc:223
GGEMSMHDImage::SetOutputFileName
void SetOutputFileName(std::string const &basename)
set the output filename (*.mhd)
Definition: GGEMSMHDImage.cc:77
set_output_range_to_material_filename_volume_creator_manager
void set_output_range_to_material_filename_volume_creator_manager(GGEMSVolumeCreatorManager *volume_creator_manager, char const *output_range_to_material_filename)
Set the filename of range to material data.
Definition: GGEMSVolumeCreatorManager.cc:319
GGsize3_t::z_
GGsize z_
Definition: GGEMSTypes.hh:270
GGEMSVolumeCreatorManager::voxelized_volume_
cl::Buffer * voxelized_volume_
Definition: GGEMSVolumeCreatorManager.hh:244
GGEMSVolumeCreatorManager
Singleton class handling convertion from analytical volume to voxelized volume.
Definition: GGEMSVolumeCreatorManager.hh:49
GGEMSVolumeCreatorManager.hh
Singleton class generating voxelized volume from analytical volume.
GGEMSVolumeCreatorManager::SetVolumeDimensions
void SetVolumeDimensions(GGsize const &volume_width, GGsize const &volume_height, GGsize const &volume_depth)
Set the dimension of the volume for the voxelized volume.
Definition: GGEMSVolumeCreatorManager.cc:103
GGEMSVolumeCreatorManager::output_range_to_material_filename_
std::string output_range_to_material_filename_
Definition: GGEMSVolumeCreatorManager.hh:243
set_output_image_filename_volume_creator_manager
void set_output_image_filename_volume_creator_manager(GGEMSVolumeCreatorManager *volume_creator_manager, char const *output_image_filename)
Set the filename of MHD output.
Definition: GGEMSVolumeCreatorManager.cc:310
GGcout
GGEMSStream GGcout
Definition: GGEMSPrint.cc:34
set_volume_dimension_volume_creator_manager
void set_volume_dimension_volume_creator_manager(GGEMSVolumeCreatorManager *volume_creator_manager, GGsize const volume_width, GGsize const volume_height, GGsize const volume_depth)
Set the dimension of the volume for the voxelized volume.
Definition: GGEMSVolumeCreatorManager.cc:292
GGEMSVolumeCreatorManager::Initialize
void Initialize(void)
Initialize the volume Creator manager.
Definition: GGEMSVolumeCreatorManager.cc:212
GGEMSMHDImage::SetDimensions
void SetDimensions(GGsize3 const &dimensions)
set the dimensions of the image
Definition: GGEMSMHDImage.cc:120
GGEMSVolumeCreatorManager::label_to_material_
LabelToMaterialMap label_to_material_
Definition: GGEMSVolumeCreatorManager.hh:245
GGEMSVolumeCreatorManager::GGEMSVolumeCreatorManager
GGEMSVolumeCreatorManager(void)
Unable the constructor for the user.
Definition: GGEMSVolumeCreatorManager.cc:39
GGEMSSystemOfUnits.hh
Namespace storing all the usefull physical units.
GGEMSVolumeCreatorManager::output_image_filename_
std::string output_image_filename_
Definition: GGEMSVolumeCreatorManager.hh:242
GGsize3_t::x_
GGsize x_
Definition: GGEMSTypes.hh:268
GGendl
#define GGendl
overload C++ std::endl
Definition: GGEMSPrint.hh:60
GGEMSOpenCLManager
Singleton class storing all informations about OpenCL and managing GPU/CPU devices,...
Definition: GGEMSOpenCLManager.hh:54
GGEMSVolumeCreatorManager::data_type_
std::string data_type_
Definition: GGEMSVolumeCreatorManager.hh:241
GGEMSVolumeCreatorManager::SetOutputImageFilename
void SetOutputImageFilename(std::string const &output_image_filename)
Set the filename of MHD output.
Definition: GGEMSVolumeCreatorManager.cc:165
GGEMSVolumeCreatorManager::SetRangeToMaterialDataFilename
void SetRangeToMaterialDataFilename(std::string const &output_range_to_material_filename)
Set the filename of range to material data.
Definition: GGEMSVolumeCreatorManager.cc:174
set_data_type_volume_creator_manager
void set_data_type_volume_creator_manager(GGEMSVolumeCreatorManager *volume_creator_manager, char const *data_type)
set the type of data
Definition: GGEMSVolumeCreatorManager.cc:355
GGEMSVolumeCreatorManager::CheckParameters
void CheckParameters(void) const
Check the mandatory parameters.
Definition: GGEMSVolumeCreatorManager.cc:183
GGEMSMHDImage.hh
I/O class handling MHD image file.
T
__constant GGfloat T
Definition: GGEMSSystemOfUnits.hh:150
get_instance_volume_creator_manager
GGEMSVolumeCreatorManager * get_instance_volume_creator_manager(void)
Get the GGEMSVolumeCreatorManager pointer for python user.
Definition: GGEMSVolumeCreatorManager.cc:283
GGEMSVolumeCreatorManager::SetDataType
void SetDataType(std::string const &data_type="MET_FLOAT")
set the type of data
Definition: GGEMSVolumeCreatorManager.cc:125
GGEMSMisc::ThrowException
void ThrowException(std::string const &class_name, std::string const &method_name, std::string const &message)
Throw a C++ exception.
Definition: GGEMSTools.cc:61
DistanceUnit
T DistanceUnit(T const &value, std::string const &unit)
Choose best distance unit.
Definition: GGEMSSystemOfUnits.hh:243
GGfloat
#define GGfloat
Definition: GGEMSTypes.hh:273
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