GGEMS  1.1
GPU GEant4-based Monte Carlo Simulations
GGEMSRayTracing.hh
Go to the documentation of this file.
1 #ifndef GUARD_GGEMS_GEOMETRIES_GGEMSRAYTRACING_HH
2 #define GUARD_GGEMS_GEOMETRIES_GGEMSRAYTRACING_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 __OPENCL_C_VERSION__
35 
37 
40 
43 
47 
60 inline void TransportGetSafetyInsideAABB(GGfloat3 const* position, GGfloat const xmin, GGfloat const xmax, GGfloat const ymin, GGfloat const ymax, GGfloat const zmin, GGfloat const zmax, GGfloat const tolerance)
61 {
62  // on x
63  GGfloat safmin = fabs(position->x - xmin);
64  GGfloat safmax = fabs(position->x - xmax);
65 
66  position->x = (safmin < tolerance) ? xmin + tolerance : position->x;
67  position->x = (safmax < tolerance) ? xmax - tolerance : position->x;
68 
69  // on y
70  safmin = fabs(position->y - ymin);
71  safmax = fabs(position->y - ymax);
72 
73  position->y = (safmin < tolerance) ? ymin + tolerance : position->y;
74  position->y = (safmax < tolerance) ? ymax - tolerance : position->y;
75 
76  // on z
77  safmin = fabs(position->z - zmin);
78  safmax = fabs(position->z - zmax);
79 
80  position->z = (safmin < tolerance) ? zmin + tolerance : position->z;
81  position->z = (safmax < tolerance) ? zmax - tolerance : position->z;
82 }
83 
87 
94 inline void TransportGetSafetyInsideOBB(GGfloat3 const* position, global GGEMSOBB* obb_data)
95 {
96  // Get the position in local position
97  GGfloat3 local_position = GlobalToLocalPosition(&obb_data->matrix_transformation_, position);
98 
99  TransportGetSafetyInsideAABB(
100  &local_position,
101  obb_data->border_min_xyz_.x, obb_data->border_max_xyz_.x,
102  obb_data->border_min_xyz_.y, obb_data->border_max_xyz_.y,
103  obb_data->border_min_xyz_.z, obb_data->border_max_xyz_.z,
105  );
106 }
107 
111 
125 inline void TransportGetSafetyOutsideAABB(GGfloat3 const* position, GGfloat const xmin, GGfloat const xmax, GGfloat const ymin, GGfloat const ymax, GGfloat const zmin, GGfloat const zmax, GGfloat const tolerance)
126 {
127  // on x
128  GGfloat safmin = fabs(position->x - xmin);
129  GGfloat safmax = fabs(position->x - xmax);
130 
131  position->x = (safmin < tolerance) ? xmin - tolerance : position->x;
132  position->x = (safmax < tolerance) ? xmax + tolerance : position->x;
133 
134  // on y
135  safmin = fabs(position->y - ymin);
136  safmax = fabs(position->y - ymax);
137 
138  position->y = (safmin < tolerance) ? ymin - tolerance : position->y;
139  position->y = (safmax < tolerance) ? ymax + tolerance : position->y;
140 
141  // on z
142  safmin = fabs(position->z - zmin);
143  safmax = fabs(position->z - zmax);
144 
145  position->z = (safmin < tolerance) ? zmin - tolerance : position->z;
146  position->z = (safmax < tolerance) ? zmax + tolerance : position->z;
147 }
148 
152 
166 inline GGchar IsParticleInAABB(GGfloat3 const* position, GGfloat const x_min, GGfloat const x_max, GGfloat const y_min, GGfloat const y_max, GGfloat const z_min, GGfloat const z_max, GGfloat const tolerance)
167 {
168  if (position->s0 < (x_min + GEOMETRY_TOLERANCE) || position->s0 > (x_max - GEOMETRY_TOLERANCE)) return FALSE;
169  if (position->s1 < (y_min + GEOMETRY_TOLERANCE) || position->s1 > (y_max - GEOMETRY_TOLERANCE)) return FALSE;
170  if (position->s2 < (z_min + GEOMETRY_TOLERANCE) || position->s2 > (z_max - GEOMETRY_TOLERANCE)) return FALSE;
171 
172  return TRUE;
173 }
174 
178 
186 inline GGchar IsParticleInOBB(GGfloat3 const* position, global GGEMSOBB* obb_data)
187 {
188  // Get the position in local position
189  GGfloat3 local_position = GlobalToLocalPosition(&obb_data->matrix_transformation_, position);
190 
191  return IsParticleInAABB(
192  &local_position,
193  obb_data->border_min_xyz_.x, obb_data->border_max_xyz_.x,
194  obb_data->border_min_xyz_.y, obb_data->border_max_xyz_.y,
195  obb_data->border_min_xyz_.z, obb_data->border_max_xyz_.z,
197  );
198 }
199 
203 
218 inline GGfloat ComputeDistanceToAABB(GGfloat3 const* position, GGfloat3 const* direction, GGfloat const x_min, GGfloat const x_max, GGfloat const y_min, GGfloat const y_max, GGfloat const z_min, GGfloat const z_max, GGfloat const tolerance)
219 {
220  // Variables for algorithm
221  GGfloat idx = 0.0f;
222  GGfloat idy = 0.0f;
223  GGfloat idz = 0.0f;
224  GGfloat tmp = 0.0f;
225  GGfloat tmin = FLT_MIN;
226  GGfloat tmax = FLT_MAX;
227  GGfloat tymin = 0.0f;
228  GGfloat tymax = 0.0f;
229  GGfloat tzmin = 0.0f;
230  GGfloat tzmax = 0.0f;
231 
232  // On X axis
233  if (fabs(direction->x) < EPSILON6) {
234  if (position->x < x_min || position->x > x_max) return OUT_OF_WORLD;
235  }
236  else {
237  idx = 1.0f / direction->x;
238  tmin = (x_min - position->x) * idx;
239  tmax = (x_max - position->x) * idx;
240  if (tmin > tmax) {
241  tmp = tmin;
242  tmin = tmax;
243  tmax = tmp;
244  }
245  if (tmin > tmax) return OUT_OF_WORLD;
246  }
247 
248  // On Y axis
249  if (fabs(direction->y) < EPSILON6) {
250  if (position->y < y_min || position->y > y_max) return OUT_OF_WORLD;
251  }
252  else {
253  idy = 1.0f / direction->y;
254  tymin = (y_min - position->y) * idy;
255  tymax = (y_max - position->y) * idy;
256 
257  if (tymin > tymax) {
258  tmp = tymin;
259  tymin = tymax;
260  tymax = tmp;
261  }
262  if (tymin > tmin) tmin = tymin;
263  if (tymax < tmax) tmax = tymax;
264  if (tmin > tmax) return OUT_OF_WORLD;
265  }
266 
267  // On Z axis
268  if (fabs(direction->z) < EPSILON6) {
269  if (position->z < z_min || position->z > z_max) return OUT_OF_WORLD;
270  }
271  else {
272  idz = 1.0f / direction->z;
273  tzmin = (z_min - position->z) * idz;
274  tzmax = (z_max - position->z) * idz;
275  if (tzmin > tzmax) {
276  tmp = tzmin;
277  tzmin = tzmax;
278  tzmax = tmp;
279  }
280  if (tzmin > tmin) tmin = tzmin;
281  if (tzmax < tmax) tmax = tzmax;
282  if (tmin > tmax) return OUT_OF_WORLD;
283  }
284 
285  // Return the smaller positive value diff to zero
286  if (tmin < 0.0f && (tmax < 0.0f || tmax == 0.0f)) return OUT_OF_WORLD;
287 
288  // Checking if particle cross navigator sufficiently
289  if ((tmax-tmin) < (2.0*tolerance)) return OUT_OF_WORLD;
290 
291  if (tmin <= EPSILON6) return tmax;
292  else return tmin;
293 }
294 
298 
307 inline GGfloat ComputeDistanceToOBB(GGfloat3 const* position, GGfloat3 const* direction, global GGEMSOBB const* obb_data)
308 {
309  // Get the position in local position
310  GGfloat3 local_position = GlobalToLocalPosition(&obb_data->matrix_transformation_, position);
311  GGfloat3 local_direction = GlobalToLocalDirection(&obb_data->matrix_transformation_, direction);
312 
313  return ComputeDistanceToAABB(
314  &local_position,
315  &local_direction,
316  obb_data->border_min_xyz_.x, obb_data->border_max_xyz_.x,
317  obb_data->border_min_xyz_.y, obb_data->border_max_xyz_.y,
318  obb_data->border_min_xyz_.z, obb_data->border_max_xyz_.z,
320  );
321 }
322 
323 #endif
324 
325 #endif // End of GUARD_GGEMS_GEOMETRIES_GGEMSRAYTRACING_HH
FALSE
#define FALSE
Definition: GGEMSTypes.hh:34
EPSILON6
__constant GGfloat EPSILON6
Definition: GGEMSGeometryConstants.hh:38
GGchar
#define GGchar
Definition: GGEMSTypes.hh:196
GGEMSParticleConstants.hh
Storing particle states for GGEMS.
GGEMSVoxelizedSolidData.hh
Structure storing the stack of data for voxelized and analytical solid.
OUT_OF_WORLD
__constant GGfloat OUT_OF_WORLD
Definition: GGEMSParticleConstants.hh:41
GGEMSReferentialTransformation.hh
Definitions of functions changing referential computation.
TRUE
#define TRUE
Definition: GGEMSTypes.hh:35
GGEMSGeometryConstants.hh
Geometry tolerances for navigation.
GEOMETRY_TOLERANCE
__constant GGfloat GEOMETRY_TOLERANCE
Definition: GGEMSGeometryConstants.hh:39
GGfloat3
#define GGfloat3
Definition: GGEMSTypes.hh:275
GGEMSOBB_t
Structure storing OBB (Oriented Bounding Box) geometry.
Definition: GGEMSPrimitiveGeometries.hh:41
GGEMSMatrixOperations.hh
Definitions of functions using matrix.
GGfloat
#define GGfloat
Definition: GGEMSTypes.hh:273