]> git.sesse.net Git - nageru/blob - piecewise_interpolator.cpp
Release Nageru 1.6.1.
[nageru] / piecewise_interpolator.cpp
1 #include "piecewise_interpolator.h"
2
3 #include <assert.h>
4
5 double PiecewiseInterpolator::fraction_to_db(double db) const
6 {
7         if (db >= control_points[0].fraction) {
8                 return control_points[0].db_value;
9         }
10         if (db <= control_points.back().fraction) {
11                 return control_points.back().db_value;
12         }
13         for (unsigned i = 1; i < control_points.size(); ++i) {
14                 const double x0 = control_points[i].fraction;
15                 const double x1 = control_points[i - 1].fraction;
16                 const double y0 = control_points[i].db_value;
17                 const double y1 = control_points[i - 1].db_value;
18                 if (db >= x0 && db <= x1) {
19                         const double t = (db - x0) / (x1 - x0);
20                         return y0 + t * (y1 - y0);
21                 }
22         }
23         assert(false);
24 }
25
26 double PiecewiseInterpolator::db_to_fraction(double x) const
27 {
28         if (x >= control_points[0].db_value) {
29                 return control_points[0].fraction;
30         }
31         if (x <= control_points.back().db_value) {
32                 return control_points.back().fraction;
33         }
34         for (unsigned i = 1; i < control_points.size(); ++i) {
35                 const double x0 = control_points[i].db_value;
36                 const double x1 = control_points[i - 1].db_value;
37                 const double y0 = control_points[i].fraction;
38                 const double y1 = control_points[i - 1].fraction;
39                 if (x >= x0 && x <= x1) {
40                         const double t = (x - x0) / (x1 - x0);
41                         return y0 + t * (y1 - y0);
42                 }
43         }
44         assert(false);
45 }
46