GGEMS  1.1
GPU GEant4-based Monte Carlo Simulations
GGEMSGeometryTransformation.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 
31 #include <limits>
32 
35 
39 
41 {
42  GGcout("GGEMSGeometryTransformation", "GGEMSGeometryTransformation", 3) << "GGEMSGeometryTransformation creating..." << GGendl;
43 
44  // Initialize the position with min. float
45  position_.x = std::numeric_limits<float>::min();
46  position_.y = std::numeric_limits<float>::min();
47  position_.z = std::numeric_limits<float>::min();
48 
49  // Initialize the rotation with min. float
50  rotation_.x = std::numeric_limits<float>::min();
51  rotation_.y = std::numeric_limits<float>::min();
52  rotation_.z = std::numeric_limits<float>::min();
53 
54  // Initialize the local axis
55  local_axis_ =
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  // Initializing translation matrix
64  {
65  {1.0f, 0.0f, 0.0f, 0.0f},
66  {0.0f, 1.0f, 0.0f, 0.0f},
67  {0.0f, 0.0f, 1.0f, 0.0f},
68  {0.0f, 0.0f, 0.0f, 1.0f}
69  };
70 
71  // Initializing rotation matrix
73  {
74  {1.0f, 0.0f, 0.0f, 0.0f},
75  {0.0f, 1.0f, 0.0f, 0.0f},
76  {0.0f, 0.0f, 1.0f, 0.0f},
77  {0.0f, 0.0f, 0.0f, 1.0f}
78  };
79 
80  // Initializing orthographic projection matrix
82  {
83  {1.0f, 0.0f, 0.0f, 0.0f},
84  {0.0f, 1.0f, 0.0f, 0.0f},
85  {0.0f, 0.0f, 1.0f, 0.0f},
86  {0.0f, 0.0f, 0.0f, 1.0f}
87  };
88 
89  // Get OpenCL manager
91 
92  // Get number of activated device
94 
95  // Allocating buffer on each activated device
97  for (GGsize i = 0; i < number_activated_devices_; ++i) {
98  matrix_transformation_[i] = opencl_manager.Allocate(nullptr, sizeof(GGfloat44), i, CL_MEM_READ_WRITE, "GGEMSGeometryTransformation");
99 
100  // Initializing
101  GGfloat44* matrix_transformation_device = opencl_manager.GetDeviceBuffer<GGfloat44>(matrix_transformation_[i], sizeof(GGfloat44), i);
102 
103  // Copying
104  for (GGint j = 0; j < 4; ++j) {
105  matrix_transformation_device->m0_[j] = matrix_orthographic_projection_.m0_[j];
106  matrix_transformation_device->m1_[j] = matrix_orthographic_projection_.m1_[j];
107  matrix_transformation_device->m2_[j] = matrix_orthographic_projection_.m2_[j];
108  matrix_transformation_device->m3_[j] = matrix_orthographic_projection_.m3_[j];
109  }
110 
111  // Release the pointer, mandatory step!!!
112  opencl_manager.ReleaseDeviceBuffer(matrix_transformation_[i], matrix_transformation_device, i);
113  }
114 
115  GGcout("GGEMSGeometryTransformation", "GGEMSGeometryTransformation", 3) << "GGEMSGeometryTransformation created!!!" << GGendl;
116 }
117 
121 
123 {
124  GGcout("GGEMSGeometryTransformation", "~GGEMSGeometryTransformation", 3) << "GGEMSGeometryTransformation erasing..." << GGendl;
125 
127 
129  for (GGsize i = 0; i < number_activated_devices_; ++i) {
130  opencl_manager.Deallocate(matrix_transformation_[i], sizeof(GGfloat44), i);
131  }
132  delete[] matrix_transformation_;
133  matrix_transformation_ = nullptr;
134  }
135 
136  GGcout("GGEMSGeometryTransformation", "~GGEMSGeometryTransformation", 3) << "GGEMSGeometryTransformation erased!!!" << GGendl;
137 }
138 
142 
144 {
145  // Fill the position buffer first
146  position_.x = tx;
147  position_.y = ty;
148  position_.z = tz;
149 
150  // Filling the translation matrix
152  {
153  {1.0f, 0.0f, 0.0f, position_.s0},
154  {0.0f, 1.0f, 0.0f, position_.s1},
155  {0.0f, 0.0f, 1.0f, position_.s2},
156  {0.0f, 0.0f, 0.0f, 1.0f}
157  };
158 
159  // Get OpenCL manager
161 
162  // Setting translation on each device
163  for (GGsize i = 0; i < number_activated_devices_; ++i) {
164  // Get the pointer on device
165  GGfloat44* matrix_transformation_device = opencl_manager.GetDeviceBuffer<GGfloat44>(matrix_transformation_[i], sizeof(GGfloat44), i);
166 
167  // Compute a temporary matrix then copy it on OpenCL device
168  GGfloat44 matrix_tmp = GGfloat44MultGGfloat44(&matrix_translation_, matrix_transformation_device);
169 
170  // Copy step
171  for (GGint j = 0; j < 4; ++j) {
172  matrix_transformation_device->m0_[j] = matrix_tmp.m0_[j];
173  matrix_transformation_device->m1_[j] = matrix_tmp.m1_[j];
174  matrix_transformation_device->m2_[j] = matrix_tmp.m2_[j];
175  matrix_transformation_device->m3_[j] = matrix_tmp.m3_[j];
176  }
177 
178  // Release the pointer, mandatory step!!!
179  opencl_manager.ReleaseDeviceBuffer(matrix_transformation_[i], matrix_transformation_device, i);
180  }
181 }
182 
186 
188 {
189  SetTranslation(txyz.s0, txyz.s1, txyz.s2);
190 }
191 
195 
196 void GGEMSGeometryTransformation::SetRotation(GGfloat const& rx, GGfloat const& ry, GGfloat const& rz)
197 {
198  // Filling the rotation buffer
199  rotation_.x = rx;
200  rotation_.y = ry;
201  rotation_.z = rz;
202 
203  // Definition of cosinus and sinus
204  GGfloat cosinus = 0.0, sinus = 0.0;
205 
206  // X axis
207  cosinus = std::cos(rx);
208  sinus = std::sin(rx);
209 
210  GGfloat44 rotation_x =
211  {
212  {1.0f, 0.0f, 0.0f, 0.0f},
213  {0.0f, cosinus, -sinus, 0.0f},
214  {0.0f, sinus, cosinus, 0.0f},
215  {0.0f, 0.0f, 0.0f, 1.0f}
216  };
217 
218  // Y axis
219  cosinus = std::cos(ry);
220  sinus = std::sin(ry);
221 
222  GGfloat44 rotation_y =
223  {
224  {cosinus, 0.0f, sinus, 0.0f},
225  {0.0f, 1.0f, 0.0f, 0.0f},
226  {-sinus, 0.0f, cosinus, 0.0f},
227  {0.0f, 0.0f, 0.0f, 1.0f}
228  };
229 
230  // Z axis
231  cosinus = std::cos(rz);
232  sinus = std::sin(rz);
233 
234  GGfloat44 rotation_z =
235  {
236  {cosinus, -sinus, 0.0f, 0.0f},
237  {sinus, cosinus, 0.0f, 0.0f},
238  {0.0f, 0.0f, 1.0f, 0.0f},
239  {0.0f, 0.0f, 0.0f, 1.0f}
240  };
241 
242  // Get the total rotation matrix
243  matrix_rotation_ = GGfloat44MultGGfloat44(&rotation_y, &rotation_x);
245 
246  // Get OpenCL manager
248 
249  // Setting translation on each device
250  for (GGsize i = 0; i < number_activated_devices_; ++i) {
251  // Update the transformation matrix on OpenCL device
252  // Get the pointer on device
253  GGfloat44* matrix_transformation_device = opencl_manager.GetDeviceBuffer<GGfloat44>(matrix_transformation_[i], sizeof(GGfloat44), i);
254 
255  // // Compute a temporary matrix then copy it on OpenCL device
256  GGfloat44 matrix_tmp = GGfloat44MultGGfloat44(&matrix_rotation_, matrix_transformation_device);
257 
258  // Copy step
259  for (GGint j = 0; j < 4; ++j) {
260  matrix_transformation_device->m0_[j] = matrix_tmp.m0_[j];
261  matrix_transformation_device->m1_[j] = matrix_tmp.m1_[j];
262  matrix_transformation_device->m2_[j] = matrix_tmp.m2_[j];
263  matrix_transformation_device->m3_[j] = matrix_tmp.m3_[j];
264  }
265 
266  // Release the pointer, mandatory step!!!
267  opencl_manager.ReleaseDeviceBuffer(matrix_transformation_[i], matrix_transformation_device, i);
268  }
269 }
270 
274 
276 {
277  SetRotation(rxyz.s0, rxyz.s1, rxyz.s2);
278 }
279 
283 
285 {
287  {
288  {m0.s0, m0.s1, m0.s2},
289  {m1.s0, m1.s1, m1.s1},
290  {m2.s0, m2.s1, m2.s2}
291  }
292  );
293 }
294 
298 
300 {
301  // Filling the local axis buffer first
302  local_axis_ =
303  {
304  {axis.m0_[0], axis.m0_[1], axis.m0_[2]},
305  {axis.m1_[0], axis.m1_[1], axis.m1_[2]},
306  {axis.m2_[0], axis.m2_[1], axis.m2_[2]}
307  };
308 
310  {
311  {axis.m0_[0], axis.m0_[1], axis.m0_[2], 0.0f},
312  {axis.m1_[0], axis.m1_[1], axis.m1_[2], 0.0f},
313  {axis.m2_[0], axis.m2_[1], axis.m2_[2], 0.0f},
314  {0.0f, 0.0f, 0.0f, 1.0f}
315  };
316 
317  // Get OpenCL manager
319 
320  // Setting translation on each device
321  for (GGsize i = 0; i < number_activated_devices_; ++i) {
322  // Initialize to 0
323  GGfloat44* matrix_transformation_device = opencl_manager.GetDeviceBuffer<GGfloat44>(matrix_transformation_[i], sizeof(GGfloat44), i);
324 
325  // Copy step
326  for (GGint j = 0; j < 4; ++j) {
327  matrix_transformation_device->m0_[j] = matrix_orthographic_projection_.m0_[j];
328  matrix_transformation_device->m1_[j] = matrix_orthographic_projection_.m1_[j];
329  matrix_transformation_device->m2_[j] = matrix_orthographic_projection_.m2_[j];
330  matrix_transformation_device->m3_[j] = matrix_orthographic_projection_.m3_[j];
331  }
332 
333  // Release the pointer, mandatory step!!!
334  opencl_manager.ReleaseDeviceBuffer(matrix_transformation_[i], matrix_transformation_device, i);
335  }
336 }
GGfloat44
struct GGfloat44_t GGfloat44
GGfloat44_t::m2_
GGfloat m2_[4]
Definition: GGEMSMatrixTypes.hh:56
GGEMSGeometryTransformation::GGEMSGeometryTransformation
GGEMSGeometryTransformation(void)
GGEMSGeometryTransformation constructor.
Definition: GGEMSGeometryTransformation.cc:40
GGEMSOpenCLManager::GetNumberOfActivatedDevice
GGsize GetNumberOfActivatedDevice(void) const
get the number of activated devices
Definition: GGEMSOpenCLManager.hh:167
GGEMSGeometryTransformation::rotation_
GGfloat3 rotation_
Definition: GGEMSGeometryTransformation.hh:182
GGfloat44_t::m0_
GGfloat m0_[4]
Definition: GGEMSMatrixTypes.hh:54
GGEMSGeometryTransformation::matrix_rotation_
GGfloat44 matrix_rotation_
Definition: GGEMSGeometryTransformation.hh:185
GGEMSGeometryTransformation::SetTranslation
void SetTranslation(GGfloat const &tx, GGfloat const &ty, GGfloat const &tz)
Set the translation in X, Y and Z.
Definition: GGEMSGeometryTransformation.cc:143
GGEMSGeometryTransformation::matrix_transformation_
cl::Buffer ** matrix_transformation_
Definition: GGEMSGeometryTransformation.hh:187
GGEMSGeometryTransformation::position_
GGfloat3 position_
Definition: GGEMSGeometryTransformation.hh:181
GGEMSGeometryTransformation::number_activated_devices_
GGsize number_activated_devices_
Definition: GGEMSGeometryTransformation.hh:188
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
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
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
GGfloat33_t::m0_
GGfloat m0_[3]
Definition: GGEMSMatrixTypes.hh:43
GGEMSGeometryTransformation::SetRotation
void SetRotation(GGfloat const &rx, GGfloat const &ry, GGfloat const &rz)
Set the Rotation in X, Y and Z around global axis.
Definition: GGEMSGeometryTransformation.hh:103
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
GGEMSGeometryTransformation::local_axis_
GGfloat33 local_axis_
Definition: GGEMSGeometryTransformation.hh:183
GGfloat3
#define GGfloat3
Definition: GGEMSTypes.hh:275
GGfloat33_t
Structure storing float 3 x 3 matrix.
Definition: GGEMSMatrixTypes.hh:42
GGcout
GGEMSStream GGcout
Definition: GGEMSPrint.cc:34
GGfloat33_t::m1_
GGfloat m1_[3]
Definition: GGEMSMatrixTypes.hh:44
m2
__constant GGfloat m2
Definition: GGEMSSystemOfUnits.hh:61
GGEMSGeometryTransformation::matrix_orthographic_projection_
GGfloat44 matrix_orthographic_projection_
Definition: GGEMSGeometryTransformation.hh:186
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
GGEMSMatrixOperations.hh
Definitions of functions using matrix.
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
GGEMSGeometryTransformation::~GGEMSGeometryTransformation
~GGEMSGeometryTransformation(void)
GGEMSGeometryTransformation destructor.
Definition: GGEMSGeometryTransformation.cc:122
GGfloat44MultGGfloat44
GGfloat44 GGfloat44MultGGfloat44(GGfloat44 const *A, GGfloat44 const *B)
Perform the matrix (4x4) multiplication.
Definition: GGEMSMatrixOperations.hh:155
GGfloat33_t::m2_
GGfloat m2_[3]
Definition: GGEMSMatrixTypes.hh:45
GGEMSGeometryTransformation.hh
Class managing the geometry transformation.
GGfloat44_t::m3_
GGfloat m3_[4]
Definition: GGEMSMatrixTypes.hh:57
GGfloat44_t
Structure storing float 4 x 4 matrix.
Definition: GGEMSMatrixTypes.hh:53
GGEMSGeometryTransformation::matrix_translation_
GGfloat44 matrix_translation_
Definition: GGEMSGeometryTransformation.hh:184
GGfloat
#define GGfloat
Definition: GGEMSTypes.hh:273
GGEMSOpenCLManager::GetInstance
static GGEMSOpenCLManager & GetInstance(void)
Create at first time the Singleton.
Definition: GGEMSOpenCLManager.hh:72