Interpolation function.
From tabulated values of y vs x, it returns the interpolated y value corresponding to the input x value.
16 {
17
18
19
20
21
22#ifndef NDEBUG
23
24 if ( xtabulated.size() != ytabulated.size() ) {
25 std::cerr << "TRT_PAI_Process::TRT_PAI_utils::Interpolate:";
26 std::cerr << " ERROR - tabulated arrays must be of similar size"
27 << std::endl;
28 std::cerr << "xsize= " << xtabulated.size()
29 << " ysize= " << ytabulated.size()
30 << std::endl;
31
32 return 0;
33 }
34 if ( xtabulated.empty() ) {
35 std::cerr << "TRT_PAI_Process::TRT_PAI_utils::Interpolate:"
36 << "ERROR - tabulated arrays are empty"
37 << std::endl;
38 return 0;
39 }
40
41
42
43#endif
44
45
46 if ( xtabulated.size()==1 ) {
47 return ytabulated.at(1);
48 }
49
50 const std::vector<float>::const_iterator xBeginIter(xtabulated.begin());
51 const std::vector<float>::const_iterator xEndIter(xtabulated.end());
52
53 const std::vector<float>::const_iterator xLowerBoundIter(lower_bound(xBeginIter, xEndIter, xval));
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90 if ( xLowerBoundIter==xEndIter ) {
91
92
93 const std::vector<float>::const_iterator yEndIter(ytabulated.end());
95 }
96
97 if ( ((*xLowerBoundIter)-xval)<std::numeric_limits<float>::epsilon() ) {
98
99
100 std::vector<float>::const_iterator yIter(ytabulated.begin());
101 std::advance(yIter, std::distance(xBeginIter, xLowerBoundIter));
102 return *yIter;
103 }
104
105
106
107
108
109 if (xLowerBoundIter==xBeginIter) {
110
111 const std::vector<float>::const_iterator yBeginIter(ytabulated.begin());
113 }
114
115
116 std::vector<float>::const_iterator
ylo(ytabulated.begin());
117 std::advance(ylo, std::distance(xBeginIter, xLowerBoundIter));
119}
float calculate_interpolation(const float &xval, const float &x1, const float &x2, const float &y1, const float &y2)