Debian Codegraph @

src/engine/platform/sound/sid2/spline.h

raw
1 // ---------------------------------------------------------------------------
2 // This file is part of reSID, a MOS6581_2 SID2 emulator engine.
3 // Copyright (C) 2004 Dag Lem <[email protected]>
4 //
5 // This program is free software; you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation; either version 2 of the License, or
8 // (at your option) any later version.
9 //
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with this program; if not, write to the Free Software
17 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 // ---------------------------------------------------------------------------
19
20
21 // Our objective is to construct a smooth interpolating single-valued function
22 // y = f(x).
23 //
24 // Catmull-Rom splines are widely used for interpolation, however these are
25 // parametric curves [x2(t) y2(t) ...] and can not be used to directly calculate
26 // y = f(x).
27 // For a discussion of Catmull-Rom splines see Catmull, E., and R. Rom,
28 // "A Class of Local Interpolating Splines", Computer Aided Geometric Design.
29 //
30 // Natural cubic splines are single-valued functions, and have been used in
31 // several applications e.g. to specify gamma curves for image display.
32 // These splines do not afford local control, and a set of linear equations
33 // including all interpolation points must be solved before any point on the
34 // curve can be calculated. The lack of local control makes the splines
35 // more difficult to handle than e.g. Catmull-Rom splines, and real-time
36 // interpolation of a stream of data points is not possible.
37 // For a discussion of natural cubic splines, see e.g. Kreyszig, E., "Advanced
38 // Engineering Mathematics".
39 //
40 // Our approach is to approximate the properties of Catmull-Rom splines for
41 // piecewice cubic polynomials f(x) = ax^3 + bx^2 + cx + d as follows:
42 // Each curve segment is specified by four interpolation points,
43 // p0, p1, p2, p3.
44 // The curve between p1 and p2 must interpolate2 both p1 and p2, and in addition
45 // f'(p1.x) = k1 = (p2.y - p0.y)/(p2.x - p0.x) and
46 // f'(p2.x) = k2 = (p3.y - p1.y)/(p3.x - p1.x).
47 //
48 // The constraints are expressed by the following system of linear equations
49 //
50 // [ 1 xi xi^2 xi^3 ] [ d ] [ yi ]
51 // [ 1 2*xi 3*xi^2 ] * [ c ] = [ ki ]
52 // [ 1 xj xj^2 xj^3 ] [ b ] [ yj ]
53 // [ 1 2*xj 3*xj^2 ] [ a ] [ kj ]
54 //
55 // Solving using Gaussian elimination and back substitution, setting
56 // dy = yj - yi, dx = xj - xi, we get
57 //
58 // a = ((ki + kj) - 2*dy/dx)/(dx*dx);
59 // b = ((kj - ki)/dx - 3*(xi + xj)*a)/2;
60 // c = ki - (3*xi*a + 2*b)*xi;
61 // d = yi - ((xi*a + b)*xi + c)*xi;
62 //
63 // Having calculated the coefficients of the cubic polynomial we have the
64 // choice of evaluation by brute force
65 //
66 // for (x = x1; x <= x2; x += res) {
67 // y = ((a*x + b)*x + c)*x + d;
68 // plot(x, y);
69 // }
70 //
71 // or by forward differencing
72 //
73 // y = ((a*x1 + b)*x1 + c)*x1 + d;
74 // dy = (3*a*(x1 + res) + 2*b)*x1*res + ((a*res + b)*res + c)*res;
75 // d2y = (6*a*(x1 + res) + 2*b)*res*res;
76 // d3y = 6*a*res*res*res;
77 //
78 // for (x = x1; x <= x2; x += res) {
79 // plot(x, y);
80 // y += dy; dy += d2y; d2y += d3y;
81 // }
82 //
83 // See Foley, Van Dam, Feiner, Hughes, "Computer Graphics, Principles and
84 // Practice" for a discussion of forward differencing.
85 //
86 // If we have a set of interpolation points p0, ..., pn, we may specify
87 // curve segments between p0 and p1, and between pn-1 and pn by using the
88 // following constraints:
89 // f''(p0.x) = 0 and
90 // f''(pn.x) = 0.
91 //
92 // Substituting the results for a and b in
93 //
94 // 2*b + 6*a*xi = 0
95 //
96 // we get
97 //
98 // ki = (3*dy/dx - kj)/2;
99 //
100 // or by substituting the results for a and b in
101 //
102 // 2*b + 6*a*xj = 0
103 //
104 // we get
105 //
106 // kj = (3*dy/dx - ki)/2;
107 //
108 // Finally, if we have only two interpolation points, the cubic polynomial
109 // will degenerate to a straight line if we set
110 //
111 // ki = kj = dy/dx;
112 //
113
114
115 #if SPLINE_BRUTE_FORCE
116 #define interpolate_segment2 interpolate_brute_force2
117 #else
118 #define interpolate_segment2 interpolate_forward_difference2
119 #endif
120
121
122 // ----------------------------------------------------------------------------
123 // Calculation of coefficients.
124 // ----------------------------------------------------------------------------
125 inline
126 void cubic_coefficients2(double x1, double y1, double x2, double y2,
127 double k1, double k2,
128 double& a, double& b, double& c, double& d)
129 {
130 double dx = x2 - x1, dy = y2 - y1;
131
132 a = ((k1 + k2) - 2*dy/dx)/(dx*dx);
133 b = ((k2 - k1)/dx - 3*(x1 + x2)*a)/2;
134 c = k1 - (3*x1*a + 2*b)*x1;
135 d = y1 - ((x1*a + b)*x1 + c)*x1;
136 }
137
138 // ----------------------------------------------------------------------------
139 // Evaluation of cubic polynomial by brute force.
140 // ----------------------------------------------------------------------------
141 template<class PointPlotter2>
142 inline
143 void interpolate_brute_force2(double x1, double y1, double x2, double y2,
144 double k1, double k2,
145 PointPlotter2 plot, double res)
146 {
147 double a, b, c, d;
148 cubic_coefficients2(x1, y1, x2, y2, k1, k2, a, b, c, d);
149
150 // Calculate each point.
151 for (double x = x1; x <= x2; x += res) {
152 double y = ((a*x + b)*x + c)*x + d;
153 plot(x, y);
154 }
155 }
156
157 // ----------------------------------------------------------------------------
158 // Evaluation of cubic polynomial by forward differencing.
159 // ----------------------------------------------------------------------------
160 template<class PointPlotter2>
161 inline
162 void interpolate_forward_difference2(double x1, double y1, double x2, double y2,
163 double k1, double k2,
164 PointPlotter2 plot, double res)
165 {
166 double a, b, c, d;
167 cubic_coefficients2(x1, y1, x2, y2, k1, k2, a, b, c, d);
168
169 double y = ((a*x1 + b)*x1 + c)*x1 + d;
170 double dy = (3*a*(x1 + res) + 2*b)*x1*res + ((a*res + b)*res + c)*res;
171 double d2y = (6*a*(x1 + res) + 2*b)*res*res;
172 double d3y = 6*a*res*res*res;
173
174 // Calculate each point.
175 for (double x = x1; x <= x2; x += res) {
176 plot(x, y);
177 y += dy; dy += d2y; d2y += d3y;
178 }
179 }
180
181 template<class PointIter>
182 inline
183 double x2(PointIter p)
184 {
185 return (*p)[0];
186 }
187
188 template<class PointIter>
189 inline
190 double y2(PointIter p)
191 {
192 return (*p)[1];
193 }
194
195 // ----------------------------------------------------------------------------
196 // Evaluation of complete interpolating function.
197 // Note that since each curve segment is controlled by four points, the
198 // end points will not be interpolated. If extra control points are not
199 // desirable, the end points can simply be repeated to ensure interpolation.
200 // Note also that points of non-differentiability and discontinuity can be
201 // introduced by repeating points.
202 // ----------------------------------------------------------------------------
203 template<class PointIter, class PointPlotter2>
204 inline
205 void interpolate2(PointIter p0, PointIter pn, PointPlotter2 plot, double res)
206 {
207 double k1, k2;
208
209 // Set up points for first curve segment.
210 PointIter p1 = p0; ++p1;
211 PointIter p2 = p1; ++p2;
212 PointIter p3 = p2; ++p3;
213
214 // Draw each curve segment.
215 for (; p2 != pn; ++p0, ++p1, ++p2, ++p3) {
216 // p1 and p2 equal; single point.
217 if (x2(p1) == x2(p2)) {
218 continue;
219 }
220 // Both end points repeated; straight line.
221 if (x2(p0) == x2(p1) && x2(p2) == x2(p3)) {
222 k1 = k2 = (y2(p2) - y2(p1))/(x2(p2) - x2(p1));
223 }
224 // p0 and p1 equal; use f''(x1) = 0.
225 else if (x2(p0) == x2(p1)) {
226 k2 = (y2(p3) - y2(p1))/(x2(p3) - x2(p1));
227 k1 = (3*(y2(p2) - y2(p1))/(x2(p2) - x2(p1)) - k2)/2;
228 }
229 // p2 and p3 equal; use f''(x2) = 0.
230 else if (x2(p2) == x2(p3)) {
231 k1 = (y2(p2) - y2(p0))/(x2(p2) - x2(p0));
232 k2 = (3*(y2(p2) - y2(p1))/(x2(p2) - x2(p1)) - k1)/2;
233 }
234 // Normal curve.
235 else {
236 k1 = (y2(p2) - y2(p0))/(x2(p2) - x2(p0));
237 k2 = (y2(p3) - y2(p1))/(x2(p3) - x2(p1));
238 }
239
240 interpolate_segment2(x2(p1), y2(p1), x2(p2), y2(p2), k1, k2, plot, res);
241 }
242 }
243
244 // ----------------------------------------------------------------------------
245 // Class for plotting integers into an array.
246 // ----------------------------------------------------------------------------
247 template<class F>
248 class PointPlotter2
249 {
250 protected:
251 F* f;
252
253 public:
254 PointPlotter2(F* arr) : f(arr)
255 {
256 }
257
258 void operator ()(double x, double y)
259 {
260 // Clamp negative values to zero.
261 if (y < 0) {
262 y = 0;
263 }
264
265 f[F(x)] = F(y);
266 }
267 };