EGSnrc C++ class library  Report PIRS-898 (2021)
Iwan Kawrakow, Ernesto Mainegra-Hing, Frederic Tessier, Reid Townson and Blake Walters
egs_focal_spot_source.h
Go to the documentation of this file.
1 /*
2 ###############################################################################
3 #
4 # EGSnrc egs++ focal spot source headers
5 # Copyright (C) 2025 Marvin Apel
6 #
7 # This file is part of EGSnrc.
8 #
9 # EGSnrc is free software: you can redistribute it and/or modify it under
10 # the terms of the GNU Affero General Public License as published by the
11 # Free Software Foundation, either version 3 of the License, or (at your
12 # option) any later version.
13 #
14 # EGSnrc is distributed in the hope that it will be useful, but WITHOUT ANY
15 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
16 # FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for
17 # more details.
18 #
19 # You should have received a copy of the GNU Affero General Public License
20 # along with EGSnrc. If not, see <http://www.gnu.org/licenses/>.
21 #
22 ###############################################################################
23 #
24 # Author: Marvin Apel, 2025
25 #
26 # Contributors:
27 #
28 ###############################################################################
29 */
30 
31 
38 #ifndef EGS_FOCAL_SPOT_
39 #define EGS_FOCAL_SPOT_
40 
41 #include "egs_base_source.h"
42 #include "egs_vector.h"
43 #include "egs_rndm.h"
44 #include "egs_transformations.h"
45 
46 
47 #ifdef WIN32
48 
49  #ifdef BUILD_FOCAL_SPOT_DLL
50  #define EGS_FOCAL_SPOT_EXPORT __declspec(dllexport)
51  #else
52  #define EGS_FOCAL_SPOT_EXPORT __declspec(dllimport)
53  #endif
54  #define EGS_FOCAL_SPOT_LOCAL
55 
56 #else
57 
58  #ifdef HAVE_VISIBILITY
59  #define EGS_FOCAL_SPOT_EXPORT __attribute__ ((visibility ("default")))
60  #define EGS_FOCAL_SPOT_LOCAL __attribute__ ((visibility ("hidden")))
61  #else
62  #define EGS_FOCAL_SPOT_EXPORT
63  #define EGS_FOCAL_SPOT_LOCAL
64  #endif
65 
66 #endif
67 
99 class EGS_FOCAL_SPOT_EXPORT EGS_FocalSpot : public EGS_BaseSimpleSource {
100  bool valid;
101  bool is_deviating=false;
102  bool is_rotated=false;
103  // The beam is always pointing in the + z-direction
104  EGS_Float z_pos;
105  EGS_Float sigma_x_space;
106  EGS_Float sigma_y_space;
107  EGS_Float space_cutoff_x;
108  EGS_Float space_cutoff_y;
109  EGS_Float sigma_x_angle=0;
110  EGS_Float sigma_y_angle=0;
111  EGS_Float x_translation=0;
112  EGS_Float y_translation=0;
113  EGS_Float x_rotation=0;
114  EGS_Float y_rotation=0;
115  EGS_Float z_point_of_rotation;
116 
117 public:
118 
130  ~EGS_FocalSpot() {};
131 
132  void getPositionDirection(EGS_RandomGenerator *rndm, EGS_Vector &x, EGS_Vector &u, EGS_Float &wt) {
133  wt = 1;
134  // 1. Sample position
135  do {
136  double r = sqrt(-2*log(1-rndm->getUniform())); // store value characteristic for Gaussian Sampling
137  double phi = PI2*rndm->getUniform(); // store value for phi
138  x.x = sigma_x_space * r * cos(phi);
139  x.y = sigma_y_space * r * sin(phi);
140  // Only Accept points within cutoff limits
141  }
142  while (pow(x.x/space_cutoff_x,2) + pow(x.y/space_cutoff_y,2) > 1);
143  x.x += x_translation; // save computation time by only applying translation after finding a valid point
144  x.y += y_translation; // otherwise it will be done in the while condition above repeatedly
145  x.z = z_pos;
146 
147  // 2. Sample direction
148  switch (angle_mode) {
149  case 1: { // Deviation in x and y
150  double phi = PI2 * rndm->getUniform(); // store value of rotation arc phi !
151  do {
152  u.z = sqrt(-2*pow((sigma_x_angle*sigma_y_angle), 2)/
153  (pow(sigma_x_angle*sin(phi),2) + pow(sigma_y_angle*cos(phi),2)) * (log(1- rndm->getUniform())));
154  u.z = cos(u.z);
155  }
156  while (u.z < 0);
157 
158  double r = sqrt((1-u.z)*(1+u.z));
159  u.y = r * sin(phi);
160  u.x = r * cos(phi);
161 
162  break;
163  }
164 
165  case 2: // Deviation only in x, deviation is planar only along x-direction ! (u.y = 0 !!!)
166  do {
167  u.z = cos(sigma_x_angle * sqrt(-2*log(rndm->getUniform())));
168  }
169  while (u.z < 0);
170  u.x = sqrt((1-u.z)*(1+u.z));
171  u.x = (rndm->getUniform() < 0.5) ? u.x : -1*u.x;
172  u.y = 0;
173  break;
174 
175  case 3: // Deviation only in y, deviation is planar only along y-direction ! (u.x = 0 !!!)
176  do {
177  u.z = cos(sigma_y_angle * sqrt(-2*log(rndm->getUniform())));
178  }
179  while (u.z < 0);
180  u.y = sqrt((1-u.z)*(1+u.z));
181  u.y = (rndm->getUniform() < 0.5) ? u.y : -1*u.y;
182  u.x = 0;
183  break;
184 
185  default: // No Deviation
186  u.x = 0;
187  u.y = 0;
188  u.z = 1;
189  }
190 
191  // 3. Rotate X, Y, Z, U, V, W and project them into the z-pos plane
192  if (is_rotated) {
193  rotation.transform(x); // position: rotation + pivot translation
194  rotation.rotate(u); // direction: rotation only, no translation
195 
196  // project back onto z_pos plane (ray-plane intersection)
197  double t = (z_pos - x.z) / u.z;
198  x.x += t * u.x;
199  x.y += t * u.y;
200  x.z = z_pos;
201  }
202  };
203 
204  EGS_Float getFluence() const {
205  return count;
206  };
207 
208  bool storeFluenceState(ostream &) const {
209  return true;
210  };
211 
212  bool setFluenceState(istream &) {
213  return true;
214  };
215 
216  bool isValid() const {
217  return (valid && s != 0);
218  };
219 
220 protected:
221  static constexpr double PI = 3.141592653589793;
222  static constexpr double PI2 = 6.283185307179586;
223  static constexpr double DEGREE_TO_RAD = 0.017453292519943295;
227  EGS_AffineTransform rotation;
229  void setUp();
230 
231 };
232 
233 #endif
A class providing affine transformations.
Base class for 'simple' particle sources.
virtual void getPositionDirection(EGS_RandomGenerator *rndm, EGS_Vector &x, EGS_Vector &u, EGS_Float &wt)=0
Sample a particle position and direction.
A focal spot source.
int angle_mode
rotation about z_point_of_rotation
A class for storing information in a tree-like structure of key-value pairs. This class is used throu...
Definition: egs_input.h:182
An object factory.
Base random number generator class. All random number generators should be derived from this class.
Definition: egs_rndm.h:90
EGS_Float getUniform()
Returns a random number uniformly distributed between zero (inclusive) and 1 (exclusive).
Definition: egs_rndm.h:126
A class representing 3D vectors.
Definition: egs_vector.h:57
EGS_Float y
y-component
Definition: egs_vector.h:62
EGS_Float z
z-component
Definition: egs_vector.h:63
EGS_Float x
x-component
Definition: egs_vector.h:61
EGS_BaseSource class header file.
EGS_RandomGenerator class header file.
EGS_AffineTransform and EGS_RotationMatrix class header file.
EGS_Vector methods for the manipulation of 3D vectors in cartesian co-ordinates.