GGEMS  1.1
GPU GEant4-based Monte Carlo Simulations
GGEMSVoxelizedSolid.hh
Go to the documentation of this file.
1 #ifndef GUARD_GGEMS_GEOMETRIES_GGEMSVOXELIZEDSOLID_HH
2 #define GUARD_GGEMS_GEOMETRIES_GGEMSVOXELIZEDSOLID_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 
36 
41 class GGEMS_EXPORT GGEMSVoxelizedSolid : public GGEMSSolid
42 {
43  public:
50  GGEMSVoxelizedSolid(std::string const& volume_header_filename, std::string const& range_filename, std::string const& data_reg_type = "");
51 
55  ~GGEMSVoxelizedSolid(void);
56 
62  GGEMSVoxelizedSolid(GGEMSVoxelizedSolid const& voxelized_solid) = delete;
63 
69  GGEMSVoxelizedSolid& operator=(GGEMSVoxelizedSolid const& voxelized_solid) = delete;
70 
76  GGEMSVoxelizedSolid(GGEMSVoxelizedSolid const&& voxelized_solid) = delete;
77 
83  GGEMSVoxelizedSolid& operator=(GGEMSVoxelizedSolid const&& voxelized_solid) = delete;
84 
90  void Initialize(GGEMSMaterials* materials) override;
91 
96  void EnableScatter(void) override {;};
97 
102  void PrintInfos(void) const override;
103 
109  void LoadVolumeImage(GGEMSMaterials* materials);
110 
116  void UpdateTransformationMatrix(GGsize const& thread_index) override;
117 
124  GGfloat3 GetVoxelSizes(GGsize const& thread_index) const;
125 
132  GGEMSOBB GetOBBGeometry(GGsize const& thread_index) const;
133 
134  private:
143  template <typename T>
144  void ConvertImageToLabel(std::string const& raw_data_filename, std::string const& range_data_filename, GGEMSMaterials* materials);
145 
150  void InitializeKernel(void) override;
151 
152  private:
154  std::string range_filename_;
155 };
156 
160 
161 template <typename T>
162 void GGEMSVoxelizedSolid::ConvertImageToLabel(std::string const& raw_data_filename, std::string const& range_data_filename, GGEMSMaterials* materials)
163 {
164  GGcout("GGEMSVoxelizedSolid", "ConvertImageToLabel", 3) << "Converting image material data to label data..." << GGendl;
165 
166  // Get the OpenCL manager
168 
169  for (GGsize d = 0; d < number_activated_devices_; ++d) {
170  // Get pointer on OpenCL device
171  GGEMSVoxelizedSolidData* solid_data_device = opencl_manager.GetDeviceBuffer<GGEMSVoxelizedSolidData>(solid_data_[d], sizeof(GGEMSVoxelizedSolidData), d);
172 
173  // Get information about mhd file
174  GGsize number_of_voxels = static_cast<GGsize>(solid_data_device->number_of_voxels_);
175 
176  // Release the pointer
177  opencl_manager.ReleaseDeviceBuffer(solid_data_[d], solid_data_device, d);
178 
179  // Checking if file exists
180  std::ifstream in_raw_stream(raw_data_filename, std::ios::in | std::ios::binary);
181  GGEMSFileStream::CheckInputStream(in_raw_stream, raw_data_filename);
182 
183  // Reading data to a tmp buffer
184  std::vector<T> tmp_raw_data;
185  tmp_raw_data.resize(number_of_voxels);
186  in_raw_stream.read(reinterpret_cast<char*>(&tmp_raw_data[0]), static_cast<std::streamsize>(number_of_voxels * sizeof(T)));
187 
188  // Closing file
189  in_raw_stream.close();
190 
191  // Allocating memory on OpenCL device
192  label_data_[d] = opencl_manager.Allocate(nullptr, number_of_voxels * sizeof(GGuchar), d, CL_MEM_READ_WRITE, "GGEMSVoxelizedSolid");
193 
194  // Get pointer on OpenCL device
195  GGuchar* label_data_device = opencl_manager.GetDeviceBuffer<GGuchar>(label_data_[d], number_of_voxels * sizeof(GGuchar), d);
196 
197  // Set value to max of GGuchar
198  std::fill(label_data_device, label_data_device + number_of_voxels, std::numeric_limits<GGuchar>::max());
199 
200  // Opening range data file
201  std::ifstream in_range_stream(range_data_filename, std::ios::in);
202  GGEMSFileStream::CheckInputStream(in_range_stream, range_data_filename);
203 
204  // Values in the range file
205  GGfloat first_label_value = 0.0f;
206  GGfloat last_label_value = 0.0f;
207  GGuchar label_index = 0;
208  std::string material_name("");
209 
210  // Reading range file
211  std::string line("");
212  while (std::getline(in_range_stream, line)) {
213  // Check if blank line
214  if (GGEMSTextReader::IsBlankLine(line)) continue;
215 
216  // Getting the value in string stream
217  std::istringstream iss = GGEMSRangeReader::ReadRangeMaterial(line);
218  iss >> first_label_value >> last_label_value >> material_name;
219 
220  // Adding the material only once
221  if (d == 0) materials->AddMaterial(material_name);
222 
223  // Setting the label
224  for (GGsize i = 0; i < number_of_voxels; ++i) {
225  // Getting the value of phantom
226  GGfloat value = static_cast<GGfloat>(tmp_raw_data[i]);
227  if (((value == first_label_value) && (value == last_label_value)) || ((value >= first_label_value) && (value < last_label_value))) {
228  label_data_device[i] = label_index;
229  }
230  }
231 
232  // Increment the label index
233  ++label_index;
234  }
235 
236  // Final loop checking if a value is still max of GGuchar
237  bool all_converted = true;
238  for (GGsize i = 0; i < number_of_voxels; ++i) {
239  if (label_data_device[i] == std::numeric_limits<GGuchar>::max()) all_converted = false;
240  }
241 
242  // Closing file
243  in_range_stream.close();
244  tmp_raw_data.clear();
245 
246  // Release the pointer
247  opencl_manager.ReleaseDeviceBuffer(label_data_[d], label_data_device, d);
248 
249  // Checking if all voxels converted
250  if (all_converted) {
251  GGcout("GGEMSVoxelizedSolid", "ConvertImageToLabel", 2) << "All your voxels are converted to label..." << GGendl;
252  }
253  else {
254  GGEMSMisc::ThrowException("GGEMSVoxelizedSolid", "ConvertImageToLabel", "Errors(s) in the range data file!!!");
255  }
256  }
257 }
258 
259 #endif // End of GUARD_GGEMS_GEOMETRIES_GGEMSVOXELIZEDSOLID_HH
GGEMSSolid::label_data_
cl::Buffer ** label_data_
Definition: GGEMSSolid.hh:216
GGEMSVoxelizedSolidData_t::number_of_voxels_
GGint number_of_voxels_
Definition: GGEMSVoxelizedSolidData.hh:46
GGEMSVoxelizedSolidData_t
Structure storing the stack of data for voxelized solid.
Definition: GGEMSVoxelizedSolidData.hh:41
GGEMSVoxelizedSolid::operator=
GGEMSVoxelizedSolid & operator=(GGEMSVoxelizedSolid const &voxelized_solid)=delete
Avoid assignement by reference.
GGEMSVoxelizedSolidData
struct GGEMSVoxelizedSolidData_t GGEMSVoxelizedSolidData
GGEMSSolid::solid_data_
cl::Buffer ** solid_data_
Definition: GGEMSSolid.hh:215
GGEMSMaterials
GGEMS class handling material(s) for a specific navigator.
Definition: GGEMSMaterials.hh:49
GGEMSVoxelizedSolid::ConvertImageToLabel
void ConvertImageToLabel(std::string const &raw_data_filename, std::string const &range_data_filename, GGEMSMaterials *materials)
convert image data to label data
Definition: GGEMSVoxelizedSolid.hh:162
GGEMSSolid
GGEMS class for solid informations.
Definition: GGEMSSolid.hh:48
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
GGEMSVoxelizedSolidData.hh
Structure storing the stack of data for voxelized and analytical solid.
GGEMSVoxelizedSolid
GGEMS class for voxelized solid.
Definition: GGEMSVoxelizedSolid.hh:42
GGsize
#define GGsize
Definition: GGEMSTypes.hh:252
GGEMSRangeReader::ReadRangeMaterial
std::istringstream ReadRangeMaterial(std::string &line)
get string stream of value for material range
Definition: GGEMSTextReader.cc:202
GGEMSSolid::PrintInfos
virtual void PrintInfos(void) const =0
printing infos about solid
GGEMSVoxelizedSolid::EnableScatter
void EnableScatter(void) override
Activate scatter registration.
Definition: GGEMSVoxelizedSolid.hh:96
GGEMSOpenCLManager::Allocate
cl::Buffer * Allocate(void *host_ptr, GGsize const &size, GGsize const &thread_index, cl_mem_flags flags, std::string const &class_name="Undefined")
Allocation of OpenCL memory.
Definition: GGEMSOpenCLManager.cc:945
GGfloat3
#define GGfloat3
Definition: GGEMSTypes.hh:275
GGEMSSolid.hh
GGEMS class for solid. This class store geometry about phantom or detector.
GGEMSVoxelizedSolid::volume_header_filename_
std::string volume_header_filename_
Definition: GGEMSVoxelizedSolid.hh:153
GGcout
GGEMSStream GGcout
Definition: GGEMSPrint.cc:34
GGEMSSolid::UpdateTransformationMatrix
virtual void UpdateTransformationMatrix(GGsize const &thread_index)=0
Update transformation matrix for solid object.
GGEMSTextReader::IsBlankLine
bool IsBlankLine(std::string const &line)
check if the line is blank or not
Definition: GGEMSTextReader.cc:146
GGEMSSolid::number_activated_devices_
GGsize number_activated_devices_
Definition: GGEMSSolid.hh:217
GGEMSSolid::Initialize
virtual void Initialize(GGEMSMaterials *materials)=0
Initialize solid for geometric navigation.
GGEMSOBB_t
Structure storing OBB (Oriented Bounding Box) geometry.
Definition: GGEMSPrimitiveGeometries.hh:41
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
GGEMSOpenCLManager
Singleton class storing all informations about OpenCL and managing GPU/CPU devices,...
Definition: GGEMSOpenCLManager.hh:54
GGEMSVoxelizedSolid::GGEMSVoxelizedSolid
GGEMSVoxelizedSolid(GGEMSVoxelizedSolid const &&voxelized_solid)=delete
Avoid copy by rvalue reference.
GGEMSFileStream::CheckInputStream
void CheckInputStream(std::ifstream const &input_stream, std::string const &filename)
check the input stream during the opening
Definition: GGEMSTools.cc:42
GGuchar
#define GGuchar
Definition: GGEMSTypes.hh:203
GGEMSVoxelizedSolid::GGEMSVoxelizedSolid
GGEMSVoxelizedSolid(GGEMSVoxelizedSolid const &voxelized_solid)=delete
Avoid copy by reference.
GGEMSMaterials::AddMaterial
void AddMaterial(std::string const &material_name)
Add a material associated to a phantom.
Definition: GGEMSMaterials.cc:102
GGEMSVoxelizedSolid::range_filename_
std::string range_filename_
Definition: GGEMSVoxelizedSolid.hh:154
GGEMSVoxelizedSolid::operator=
GGEMSVoxelizedSolid & operator=(GGEMSVoxelizedSolid const &&voxelized_solid)=delete
Avoid copy by rvalue reference.
T
__constant GGfloat T
Definition: GGEMSSystemOfUnits.hh:150
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
GGEMSSolid::InitializeKernel
virtual void InitializeKernel(void)=0
Initialize kernel for particle solid distance.
GGfloat
#define GGfloat
Definition: GGEMSTypes.hh:273
GGEMSOpenCLManager::GetInstance
static GGEMSOpenCLManager & GetInstance(void)
Create at first time the Singleton.
Definition: GGEMSOpenCLManager.hh:72