EGSnrc C++ class library  Report PIRS-898 (2021)
Iwan Kawrakow, Ernesto Mainegra-Hing, Frederic Tessier, Reid Townson and Blake Walters
egs_interpolator.h
Go to the documentation of this file.
1 /*
2 ###############################################################################
3 #
4 # EGSnrc egs++ interpolator headers
5 # Copyright (C) 2015 National Research Council Canada
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: Iwan Kawrakow, 2005
25 #
26 # Contributors:
27 #
28 ###############################################################################
29 */
30 
31 
37 #ifndef EGS_INTERPOLATOR_
38 #define EGS_INTERPOLATOR_
39 
40 #include "egs_libconfig.h"
41 
42 typedef EGS_Float(*EGS_InterpolatorFuncion)(EGS_Float,void *);
43 
50 
51 public:
52 
55 
61  EGS_Interpolator(int nbin, EGS_Float Xmin, EGS_Float Xmax,
62  const EGS_Float *values);
63 
71  EGS_Interpolator(int nbin, EGS_Float Xmin, EGS_Float Xmax,
72  EGS_InterpolatorFuncion func,
73  void *data);
74 
83  EGS_Interpolator(EGS_Float Xmin, EGS_Float Xmax,
84  EGS_InterpolatorFuncion func, void *data,
85  int nmax = 1024, EGS_Float accu = 1e-4);
86 
95  EGS_Interpolator(int nbin, EGS_Float Xmin, EGS_Float Xmax,
96  EGS_Float *a, EGS_Float *b);
97 
100 
108  void initialize(int nbin, EGS_Float Xmin, EGS_Float Xmax,
109  const EGS_Float *values);
110 
118  void initialize(int nbin, EGS_Float Xmin, EGS_Float Xmax,
119  EGS_InterpolatorFuncion func,
120  void *data);
121 
122  /* \brief Initialize the interpolator
123 
124  See the \link
125  EGS_Interpolator::EGS_Interpolator(int,EGS_Float,EGS_Float,EGS_InterpolatorFuncion,void*,int,EGS_Float)
126  constructor with the same argument list \endlink for more details
127  */
128  void initialize(EGS_Float Xmin, EGS_Float Xmax,
129  EGS_InterpolatorFuncion func, void *data,
130  int nmax = 1024, EGS_Float accu = 1e-4);
131 
139  void initialize(int nbin, EGS_Float Xmin, EGS_Float Xmax,
140  EGS_Float *a, EGS_Float *b);
141 
150  inline EGS_Float interpolate(EGS_Float x) const {
151  if (x > xmin && x < xmax) {
152  int i = (int)(ax + bx*x);
153  if (i < 0) {
154  i=0;
155  }
156  return a[i] + b[i]*x;
157  }
158  else if (x <= xmin) {
159  return fmin;
160  }
161  else {
162  return fmax;
163  }
164  };
165 
173  inline EGS_Float interpolateFast(EGS_Float x) const {
174  int i = (int)(ax + bx*x);
175  return a[i] + b[i]*x;
176  };
177 
182  inline int getIndex(EGS_Float x) const {
183  if (x > xmin && x < xmax) {
184  return (int)(ax + bx*x);
185  }
186  else if (x <= xmin) {
187  return 0;
188  }
189  else {
190  return n-1;
191  }
192  };
193 
201  inline int getIndexFast(EGS_Float x) const {
202  return (int)(ax + bx*x);
203  };
204 
221  inline EGS_Float interpolateFast(int i, EGS_Float x) const {
222  return a[i] + b[i]*x;
223  };
224 
226  EGS_Float getXmin() const {
227  return xmin;
228  };
230  EGS_Float getXmax() const {
231  return xmax;
232  };
234  inline EGS_Float get_a(int i) const {
235  return a[i];
236  };
238  inline EGS_Float get_b(int i) const {
239  return b[i];
240  };
241 
242 
243 private:
244 
245  int n;
246  EGS_Float ax, bx;
247  EGS_Float *a, *b;
248  EGS_Float xmin,xmax;
249  EGS_Float fmin,fmax;
250  bool own_data;
252  void clear();
253  void check(int nbin, EGS_Float Xmin, EGS_Float Xmax);
254 };
255 
256 #endif
EGS_Float interpolateFast(EGS_Float x) const
Interpolate the function value at x.
#define EGS_EXPORT
Export symbols from the egspp library.
Definition: egs_libconfig.h:90
int getIndex(EGS_Float x) const
Get the interpolation index corresponding to x.
int getIndexFast(EGS_Float x) const
Get the interpolation index corresponding to x.
EGS_Float get_b(int i) const
Get i-th interval interpolation parameter b[i].
EGS_Float interpolate(EGS_Float x) const
Interpolate the function value at x.
EGS_Float getXmax() const
Get the upper interpolation interval limit.
A class for fast run-time interpolations.
Defines the EGS_EXPORT and EGS_LOCAL macros.
EGS_Float getXmin() const
Get the lower interpolation interval limit.
EGS_Float interpolateFast(int i, EGS_Float x) const
Interpolate the function value at x assuming that belongs to the interpolation interval i...
EGS_Float get_a(int i) const
Get i-th interval interpolation parameter a[i].