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: Ernesto Mainegra-Hing
27 # Reid Townson
28 #
29 ###############################################################################
30 */
31 
32 
38 #ifndef EGS_INTERPOLATOR_
39 #define EGS_INTERPOLATOR_
40 
41 #include "egs_libconfig.h"
42 
43 typedef EGS_Float(*EGS_InterpolatorFuncion)(EGS_Float,void *);
44 
51 
52 public:
53 
56 
62  EGS_Interpolator(int nbin, EGS_Float Xmin, EGS_Float Xmax,
63  const EGS_Float *values);
64 
72  EGS_Interpolator(int nbin, EGS_Float Xmin, EGS_Float Xmax,
73  EGS_InterpolatorFuncion func,
74  void *data);
75 
84  EGS_Interpolator(EGS_Float Xmin, EGS_Float Xmax,
85  EGS_InterpolatorFuncion func, void *data,
86  int nmax = 1024, EGS_Float accu = 1e-4);
87 
96  EGS_Interpolator(int nbin, EGS_Float Xmin, EGS_Float Xmax,
97  EGS_Float *a, EGS_Float *b);
98 
100  ~EGS_Interpolator();
101 
109  void initialize(int nbin, EGS_Float Xmin, EGS_Float Xmax,
110  const EGS_Float *values);
111 
119  void initialize(int nbin, EGS_Float Xmin, EGS_Float Xmax,
120  EGS_InterpolatorFuncion func,
121  void *data);
122 
123  /* \brief Initialize the interpolator
124 
125  See the \link
126  EGS_Interpolator::EGS_Interpolator(int,EGS_Float,EGS_Float,EGS_InterpolatorFuncion,void*,int,EGS_Float)
127  constructor with the same argument list \endlink for more details
128  */
129  void initialize(EGS_Float Xmin, EGS_Float Xmax,
130  EGS_InterpolatorFuncion func, void *data,
131  int nmax = 1024, EGS_Float accu = 1e-4);
132 
140  void initialize(int nbin, EGS_Float Xmin, EGS_Float Xmax,
141  EGS_Float *a, EGS_Float *b);
142 
151  inline EGS_Float interpolate(EGS_Float x) const {
152  if (x > xmin && x < xmax) {
153  int i = (int)(ax + bx*x);
154  if (i < 0) {
155  i=0;
156  }
157  return a[i] + b[i]*x;
158  }
159  else if (x <= xmin) {
160  return fmin;
161  }
162  else {
163  return fmax;
164  }
165  };
166 
174  inline EGS_Float interpolateFast(EGS_Float x) const {
175  int i = (int)(ax + bx*x);
176  return a[i] + b[i]*x;
177  };
178 
183  inline int getIndex(EGS_Float x) const {
184  if (x > xmin && x < xmax) {
185  return (int)(ax + bx*x);
186  }
187  else if (x <= xmin) {
188  return 0;
189  }
190  else {
191  return n-1;
192  }
193  };
194 
202  inline int getIndexFast(EGS_Float x) const {
203  return (int)(ax + bx*x);
204  };
205 
222  inline EGS_Float interpolateFast(int i, EGS_Float x) const {
223  return a[i] + b[i]*x;
224  };
225 
227  EGS_Float getXmin() const {
228  return xmin;
229  };
231  EGS_Float getXmax() const {
232  return xmax;
233  };
235  inline EGS_Float get_a(int i) const {
236  return a[i];
237  };
239  inline EGS_Float get_b(int i) const {
240  return b[i];
241  };
242 
243 
244 private:
245 
246  int n;
247  EGS_Float ax, bx;
248  EGS_Float *a, *b;
249  EGS_Float xmin,xmax;
250  EGS_Float fmin,fmax;
251  bool own_data;
253  void clear();
254  void check(int nbin, EGS_Float Xmin, EGS_Float Xmax);
255 };
256 
257 #endif
A class for fast run-time interpolations.
int getIndex(EGS_Float x) const
Get the interpolation index corresponding to x.
EGS_Float interpolate(EGS_Float x) const
Interpolate the function value at x.
EGS_Float getXmax() const
Get the upper interpolation interval limit.
EGS_Float get_b(int i) const
Get i-th interval interpolation parameter b[i].
EGS_Float get_a(int i) const
Get i-th interval interpolation parameter a[i].
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 getXmin() const
Get the lower interpolation interval limit.
EGS_Float interpolateFast(EGS_Float x) const
Interpolate the function value at x.
int getIndexFast(EGS_Float x) const
Get the interpolation index corresponding to x.
Defines the EGS_EXPORT and EGS_LOCAL macros.
#define EGS_EXPORT
Export symbols from the egspp library.
Definition: egs_libconfig.h:90