GGEMS  1.1
GPU GEant4-based Monte Carlo Simulations
GGEMSVoxelizedSolid.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 
39 GGEMSVoxelizedSolid::GGEMSVoxelizedSolid(std::string const& volume_header_filename, std::string const& range_filename, std::string const& data_reg_type)
40 : GGEMSSolid(),
41  volume_header_filename_(volume_header_filename),
42  range_filename_(range_filename)
43 {
44  GGcout("GGEMSVoxelizedSolid", "GGEMSVoxelizedSolid", 3) << "GGEMSVoxelizedSolid creating..." << GGendl;
45 
47 
48  // Loop over the device
49  for (GGsize d = 0; d < number_activated_devices_; ++d) {
50  // Allocating memory on OpenCL device
51  solid_data_[d] = opencl_manager.Allocate(nullptr, sizeof(GGEMSVoxelizedSolidData), d, CL_MEM_READ_WRITE, "GGEMSVoxelizedSolid");
52  }
53 
54  // Local axis for phantom. Voxelized solid used only for phantom
56  {
57  {1.0f, 0.0f, 0.0f},
58  {0.0f, 1.0f, 0.0f},
59  {0.0f, 0.0f, 1.0f}
60  }
61  );
62 
63  // Checking format registration
64  data_reg_type_ = data_reg_type;
65  if (!data_reg_type.empty()) {
66  if (data_reg_type == "DOSIMETRY") {
67  kernel_option_ += " -DDOSIMETRY";
68  }
69  else {
70  std::ostringstream oss(std::ostringstream::out);
71  oss << "False registration type name!!!" << std::endl;
72  oss << "Registration type is :" << std::endl;
73  oss << " - DOSIMETRY" << std::endl;
74  //oss << " - LISTMODE" << std::endl;
75  //oss << " - HISTOGRAM" << std::endl;
76  GGEMSMisc::ThrowException("GGEMSVoxelizedSolid", "GGEMSVoxelizedSolid", oss.str());
77  }
78  }
79 
80  GGcout("GGEMSVoxelizedSolid", "GGEMSVoxelizedSolid", 3) << "GGEMSVoxelizedSolid created!!!" << GGendl;
81 }
82 
86 
88 {
89  GGcout("GGEMSVoxelizedSolid", "GGEMSVoxelizedSolid", 3) << "GGEMSVoxelizedSolid erasing..." << GGendl;
90 
91  GGcout("GGEMSVoxelizedSolid", "GGEMSVoxelizedSolid", 3) << "GGEMSVoxelizedSolid erased!!!" << GGendl;
92 }
93 
97 
99 {
100  GGcout("GGEMSVoxelizedSolid", "InitializeKernel", 3) << "Initializing kernel for voxelized solid..." << GGendl;
101 
102  // Getting OpenCL manager
104 
105  // Getting the path to kernel
106  std::string openCL_kernel_path = OPENCL_KERNEL_PATH;
107  std::string particle_solid_distance_filename = openCL_kernel_path + "/ParticleSolidDistanceGGEMSVoxelizedSolid.cl";
108  std::string project_to_filename = openCL_kernel_path + "/ProjectToGGEMSVoxelizedSolid.cl";
109  std::string track_through_filename = openCL_kernel_path + "/TrackThroughGGEMSVoxelizedSolid.cl";
110 
111  // Compiling the kernels
112  opencl_manager.CompileKernel(particle_solid_distance_filename, "particle_solid_distance_ggems_voxelized_solid", kernel_particle_solid_distance_, nullptr, const_cast<char*>(kernel_option_.c_str()));
113  opencl_manager.CompileKernel(project_to_filename, "project_to_ggems_voxelized_solid", kernel_project_to_solid_, nullptr, const_cast<char*>(kernel_option_.c_str()));
114  opencl_manager.CompileKernel(track_through_filename, "track_through_ggems_voxelized_solid", kernel_track_through_solid_, nullptr, const_cast<char*>(kernel_option_.c_str()));
115 }
116 
120 
122 {
123  GGcout("GGEMSVoxelizedSolid", "Initialize", 3) << "Initializing voxelized solid..." << GGendl;
124 
125  // Initializing kernels and loading image
127  LoadVolumeImage(materials);
128 }
129 
133 
135 {
136  // Get the OpenCL manager
138 
139  // Copy information to OBB
140  GGEMSVoxelizedSolidData* solid_data_device = opencl_manager.GetDeviceBuffer<GGEMSVoxelizedSolidData>(solid_data_[thread_index], sizeof(GGEMSVoxelizedSolidData), thread_index);
141  GGfloat44* transformation_matrix_device = opencl_manager.GetDeviceBuffer<GGfloat44>(geometry_transformation_->GetTransformationMatrix(thread_index), sizeof(GGfloat44), thread_index);
142 
143  for (GGint i = 0; i < 4; ++i) {
144  solid_data_device->obb_geometry_.matrix_transformation_.m0_[i] = transformation_matrix_device->m0_[i];
145  solid_data_device->obb_geometry_.matrix_transformation_.m1_[i] = transformation_matrix_device->m1_[i];
146  solid_data_device->obb_geometry_.matrix_transformation_.m2_[i] = transformation_matrix_device->m2_[i];
147  solid_data_device->obb_geometry_.matrix_transformation_.m3_[i] = transformation_matrix_device->m3_[i];
148  }
149 
150  // Release the pointer
151  opencl_manager.ReleaseDeviceBuffer(solid_data_[thread_index], solid_data_device, thread_index);
152  opencl_manager.ReleaseDeviceBuffer(geometry_transformation_->GetTransformationMatrix(thread_index), transformation_matrix_device, thread_index);
153 }
154 
158 
160 {
161  // Get the OpenCL manager
163 
164  GGEMSVoxelizedSolidData* solid_data_device = opencl_manager.GetDeviceBuffer<GGEMSVoxelizedSolidData>(solid_data_[thread_index], sizeof(GGEMSVoxelizedSolidData), thread_index);
165 
166  GGfloat3 voxel_sizes = solid_data_device->voxel_sizes_xyz_;
167 
168  opencl_manager.ReleaseDeviceBuffer(solid_data_[thread_index], solid_data_device, thread_index);
169 
170  return voxel_sizes;
171 }
172 
176 
178 {
179  // Get the OpenCL manager
181 
182  GGEMSVoxelizedSolidData* solid_data_device = opencl_manager.GetDeviceBuffer<GGEMSVoxelizedSolidData>(solid_data_[thread_index], sizeof(GGEMSVoxelizedSolidData), thread_index);
183 
184  GGEMSOBB obb_geometry = solid_data_device->obb_geometry_;
185 
186  opencl_manager.ReleaseDeviceBuffer(solid_data_[thread_index], solid_data_device, thread_index);
187 
188  return obb_geometry;
189 }
190 
194 
195 void GGEMSVoxelizedSolid::PrintInfos(void) const
196 {
197  // Get the OpenCL manager
199 
200  // Loop over the device
201  for (GGsize d = 0; d < number_activated_devices_; ++d) {
202  // Get pointer on OpenCL device
203  GGEMSVoxelizedSolidData* solid_data_device = opencl_manager.GetDeviceBuffer<GGEMSVoxelizedSolidData>(solid_data_[d], sizeof(GGEMSVoxelizedSolidData), d);
204 
205  // Get the index of device
206  GGsize device_index = opencl_manager.GetIndexOfActivatedDevice(d);
207 
208  GGcout("GGEMSVoxelizedSolid", "PrintInfos", 0) << GGendl;
209  GGcout("GGEMSVoxelizedSolid", "PrintInfos", 0) << "GGEMSVoxelizedSolid Infos:" << GGendl;
210  GGcout("GGEMSVoxelizedSolid", "PrintInfos", 0) << "--------------------------" << GGendl;
211  GGcout("GGEMSMaterials", "PrintInfos", 0) << "Material on device: " << opencl_manager.GetDeviceName(device_index) << GGendl;
212  GGcout("GGEMSVoxelizedSolid", "PrintInfos", 0) << "* Dimension: " << solid_data_device->number_of_voxels_xyz_.x << " " << solid_data_device->number_of_voxels_xyz_.y << " " << solid_data_device->number_of_voxels_xyz_.z << GGendl;
213  GGcout("GGEMSVoxelizedSolid", "PrintInfos", 0) << "* Number of voxels: " << solid_data_device->number_of_voxels_ << GGendl;
214  GGcout("GGEMSVoxelizedSolid", "PrintInfos", 0) << "* Size of voxels: (" << solid_data_device->voxel_sizes_xyz_.x /mm << "x" << solid_data_device->voxel_sizes_xyz_.y/mm << "x" << solid_data_device->voxel_sizes_xyz_.z/mm << ") mm3" << GGendl;
215  GGcout("GGEMSVoxelizedSolid", "PrintInfos", 0) << "* Oriented bounding box (OBB) in local position:" << GGendl;
216  GGcout("GGEMSVoxelizedSolid", "PrintInfos", 0) << " - X: " << solid_data_device->obb_geometry_.border_min_xyz_.x << " <-> " << solid_data_device->obb_geometry_.border_max_xyz_.x << GGendl;
217  GGcout("GGEMSVoxelizedSolid", "PrintInfos", 0) << " - Y: " << solid_data_device->obb_geometry_.border_min_xyz_.y << " <-> " << solid_data_device->obb_geometry_.border_max_xyz_.y << GGendl;
218  GGcout("GGEMSVoxelizedSolid", "PrintInfos", 0) << " - Z: " << solid_data_device->obb_geometry_.border_min_xyz_.z << " <-> " << solid_data_device->obb_geometry_.border_max_xyz_.z << GGendl;
219  GGcout("GGEMSVoxelizedSolid", "PrintInfos", 0) << " - Transformation matrix:" << GGendl;
220  GGcout("GGEMSVoxelizedSolid", "PrintInfos", 0) << " [" << GGendl;
221  GGcout("GGEMSVoxelizedSolid", "PrintInfos", 0) << " " << solid_data_device->obb_geometry_.matrix_transformation_.m0_[0] << " " << solid_data_device->obb_geometry_.matrix_transformation_.m0_[1] << " " << solid_data_device->obb_geometry_.matrix_transformation_.m0_[2] << " " << solid_data_device->obb_geometry_.matrix_transformation_.m0_[3] << GGendl;
222  GGcout("GGEMSVoxelizedSolid", "PrintInfos", 0) << " " << solid_data_device->obb_geometry_.matrix_transformation_.m1_[0] << " " << solid_data_device->obb_geometry_.matrix_transformation_.m1_[1] << " " << solid_data_device->obb_geometry_.matrix_transformation_.m1_[2] << " " << solid_data_device->obb_geometry_.matrix_transformation_.m1_[3] << GGendl;
223  GGcout("GGEMSVoxelizedSolid", "PrintInfos", 0) << " " << solid_data_device->obb_geometry_.matrix_transformation_.m2_[0] << " " << solid_data_device->obb_geometry_.matrix_transformation_.m2_[1] << " " << solid_data_device->obb_geometry_.matrix_transformation_.m2_[2] << " " << solid_data_device->obb_geometry_.matrix_transformation_.m2_[3] << GGendl;
224  GGcout("GGEMSVoxelizedSolid", "PrintInfos", 0) << " " << solid_data_device->obb_geometry_.matrix_transformation_.m3_[0] << " " << solid_data_device->obb_geometry_.matrix_transformation_.m3_[1] << " " << solid_data_device->obb_geometry_.matrix_transformation_.m3_[2] << " " << solid_data_device->obb_geometry_.matrix_transformation_.m3_[3] << GGendl;
225  GGcout("GGEMSVoxelizedSolid", "PrintInfos", 0) << " ]" << GGendl;
226  GGcout("GGEMSVoxelizedSolid", "PrintInfos", 0) << "* Solid index: " << solid_data_device->solid_id_ << GGendl;
227  GGcout("GGEMSVoxelizedSolid", "PrintInfos", 0) << GGendl;
228 
229  // Release the pointer
230  opencl_manager.ReleaseDeviceBuffer(solid_data_[d], solid_data_device, d);
231  }
232 }
233 
237 
239 {
240  GGcout("GGEMSVoxelizedSolid", "LoadVolumeImage", 3) << "Loading volume image from mhd file..." << GGendl;
241 
242  // Read MHD input file
243  GGEMSMHDImage mhd_input_phantom;
244  // Loop over the device
245  for (GGsize d = 0; d < number_activated_devices_; ++d) {
246  mhd_input_phantom.Read(volume_header_filename_, solid_data_[d], d);
247  }
248 
249  // Get the name of raw file from mhd reader
250  std::string output_dir = mhd_input_phantom.GetOutputDirectory();
251  std::string raw_filename = output_dir + mhd_input_phantom.GetRawMDHfilename();
252 
253  // Get the type
254  std::string const kDataType = mhd_input_phantom.GetDataMHDType();
255 
256  // Convert raw data to material id data
257  if (!kDataType.compare("MET_CHAR")) {
258  ConvertImageToLabel<GGchar>(raw_filename, range_filename_, materials);
259  }
260  else if (!kDataType.compare("MET_UCHAR")) {
261  ConvertImageToLabel<GGuchar>(raw_filename, range_filename_, materials);
262  }
263  else if (!kDataType.compare("MET_SHORT")) {
264  ConvertImageToLabel<GGshort>(raw_filename, range_filename_, materials);
265  }
266  else if (!kDataType.compare("MET_USHORT")) {
267  ConvertImageToLabel<GGushort>(raw_filename, range_filename_, materials);
268  }
269  else if (!kDataType.compare("MET_INT")) {
270  ConvertImageToLabel<GGint>(raw_filename, range_filename_, materials);
271  }
272  else if (!kDataType.compare("MET_UINT")) {
273  ConvertImageToLabel<GGuint>(raw_filename, range_filename_, materials);
274  }
275  else if (!kDataType.compare("MET_FLOAT")) {
276  ConvertImageToLabel<GGfloat>(raw_filename, range_filename_, materials);
277  }
278 }
GGfloat44
struct GGfloat44_t GGfloat44
GGEMSMHDImage
I/O class handling MHD file.
Definition: GGEMSMHDImage.hh:47
GGfloat44_t::m2_
GGfloat m2_[4]
Definition: GGEMSMatrixTypes.hh:56
GGEMSVoxelizedSolidData_t
Structure storing the stack of data for voxelized solid.
Definition: GGEMSVoxelizedSolidData.hh:41
GGEMSVoxelizedSolidData
struct GGEMSVoxelizedSolidData_t GGEMSVoxelizedSolidData
GGEMSVoxelizedSolid::GGEMSVoxelizedSolid
GGEMSVoxelizedSolid(std::string const &volume_header_filename, std::string const &range_filename, std::string const &data_reg_type="")
GGEMSVoxelizedSolid constructor.
Definition: GGEMSVoxelizedSolid.cc:39
GGEMSVoxelizedSolid::GetOBBGeometry
GGEMSOBB GetOBBGeometry(GGsize const &thread_index) const
return the parameters about OBB geometry
Definition: GGEMSVoxelizedSolid.cc:177
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
GGfloat44_t::m0_
GGfloat m0_[4]
Definition: GGEMSMatrixTypes.hh:54
GGEMSVoxelizedSolid::UpdateTransformationMatrix
void UpdateTransformationMatrix(GGsize const &thread_index) override
Update transformation matrix for solid box object.
Definition: GGEMSVoxelizedSolid.cc:134
GGEMSSolid
GGEMS class for solid informations.
Definition: GGEMSSolid.hh:48
GGEMSMHDImage::GetRawMDHfilename
std::string GetRawMDHfilename(void) const
get the filename of raw data
Definition: GGEMSMHDImage.hh:154
GGEMSVoxelizedSolid::GetVoxelSizes
GGfloat3 GetVoxelSizes(GGsize const &thread_index) const
get the size of voxels in voxelized solid
Definition: GGEMSVoxelizedSolid.cc:159
GGEMSSolid::kernel_project_to_solid_
cl::Kernel ** kernel_project_to_solid_
Definition: GGEMSSolid.hh:224
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
GGfloat44_t::m1_
GGfloat m1_[4]
Definition: GGEMSMatrixTypes.hh:55
mm
__constant GGfloat mm
Definition: GGEMSSystemOfUnits.hh:54
GGEMSVoxelizedSolidData_t::obb_geometry_
GGEMSOBB obb_geometry_
Definition: GGEMSVoxelizedSolidData.hh:42
GGEMSVoxelizedSolidData_t::voxel_sizes_xyz_
GGfloat3 voxel_sizes_xyz_
Definition: GGEMSVoxelizedSolidData.hh:43
GGEMSSolid::kernel_track_through_solid_
cl::Kernel ** kernel_track_through_solid_
Definition: GGEMSSolid.hh:225
GGEMSMHDImage::GetOutputDirectory
std::string GetOutputDirectory(void) const
get the output directory
Definition: GGEMSMHDImage.hh:161
GGEMSOBB_t::matrix_transformation_
GGfloat44 matrix_transformation_
Definition: GGEMSPrimitiveGeometries.hh:42
GGsize
#define GGsize
Definition: GGEMSTypes.hh:252
GGint
#define GGint
Definition: GGEMSTypes.hh:224
GGEMSGeometryTransformation::SetAxisTransformation
void SetAxisTransformation(GGfloat33 const &axis)
Set the transformation of the frame, usefull for mirroring or convert 3D to 2D.
Definition: GGEMSGeometryTransformation.hh:126
GGEMSSolid::geometry_transformation_
GGEMSGeometryTransformation * geometry_transformation_
Definition: GGEMSSolid.hh:220
GGEMSSolid::kernel_particle_solid_distance_
cl::Kernel ** kernel_particle_solid_distance_
Definition: GGEMSSolid.hh:223
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
GGEMSVoxelizedSolid::PrintInfos
void PrintInfos(void) const override
printing infos about voxelized solid
Definition: GGEMSVoxelizedSolid.hh:96
GGfloat3
#define GGfloat3
Definition: GGEMSTypes.hh:275
GGEMSVoxelizedSolid::InitializeKernel
void InitializeKernel(void) override
Initialize kernel for particle solid distance.
Definition: GGEMSVoxelizedSolid.cc:98
GGEMSOpenCLManager::CompileKernel
void CompileKernel(std::string const &kernel_filename, std::string const &kernel_name, cl::Kernel **kernel_list, char *const custom_options=nullptr, char *const additional_options=nullptr)
Compile the OpenCL kernel on the activated device.
Definition: GGEMSOpenCLManager.cc:849
GGEMSVoxelizedSolid::volume_header_filename_
std::string volume_header_filename_
Definition: GGEMSVoxelizedSolid.hh:153
GGEMSVoxelizedSolid::Initialize
void Initialize(GGEMSMaterials *materials) override
Initialize solid for geometric navigation.
Definition: GGEMSVoxelizedSolid.cc:121
GGcout
GGEMSStream GGcout
Definition: GGEMSPrint.cc:34
GGEMSVoxelizedSolid::~GGEMSVoxelizedSolid
~GGEMSVoxelizedSolid(void)
GGEMSVoxelizedSolid destructor.
Definition: GGEMSVoxelizedSolid.cc:87
GGEMSSolid::number_activated_devices_
GGsize number_activated_devices_
Definition: GGEMSSolid.hh:217
GGEMSGeometryTransformation::GetTransformationMatrix
cl::Buffer * GetTransformationMatrix(GGsize const &index) const
return the transformation matrix
Definition: GGEMSGeometryTransformation.hh:178
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
GGEMSVoxelizedSolid.hh
GGEMS class for voxelized solid.
GGendl
#define GGendl
overload C++ std::endl
Definition: GGEMSPrint.hh:60
GGEMSMHDImage::Read
void Read(std::string const &image_mhd_header_filename, cl::Buffer *solid_data, GGsize const &thread_index)
read the mhd header
Definition: GGEMSMHDImage.cc:129
GGEMSOpenCLManager
Singleton class storing all informations about OpenCL and managing GPU/CPU devices,...
Definition: GGEMSOpenCLManager.hh:54
GGEMSOpenCLManager::GetDeviceName
std::string GetDeviceName(GGsize const &device_index) const
Get the name of the activated device.
Definition: GGEMSOpenCLManager.hh:145
GGEMSSolid::data_reg_type_
std::string data_reg_type_
Definition: GGEMSSolid.hh:229
GGEMSGeometryTransformation.hh
Class managing the geometry transformation.
GGEMSOpenCLManager::GetIndexOfActivatedDevice
GGsize GetIndexOfActivatedDevice(GGsize const &thread_index) const
get the index of activated device
Definition: GGEMSOpenCLManager.hh:175
GGEMSVoxelizedSolid::LoadVolumeImage
void LoadVolumeImage(GGEMSMaterials *materials)
load volume image to GGEMS and create a volume of label in GGEMS for voxelized solid
Definition: GGEMSVoxelizedSolid.cc:238
GGEMSMHDImage.hh
I/O class handling MHD image file.
GGEMSVoxelizedSolid::range_filename_
std::string range_filename_
Definition: GGEMSVoxelizedSolid.hh:154
GGfloat44_t::m3_
GGfloat m3_[4]
Definition: GGEMSMatrixTypes.hh:57
GGEMSSolid::kernel_option_
std::string kernel_option_
Definition: GGEMSSolid.hh:226
GGfloat44_t
Structure storing float 4 x 4 matrix.
Definition: GGEMSMatrixTypes.hh:53
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
GGEMSOpenCLManager::GetInstance
static GGEMSOpenCLManager & GetInstance(void)
Create at first time the Singleton.
Definition: GGEMSOpenCLManager.hh:72
GGEMSMHDImage::GetDataMHDType
std::string GetDataMHDType(void) const
get the mhd data type
Definition: GGEMSMHDImage.hh:147