1 #include "CurveTest.hpp"
2 #include "evoral/ControlList.hpp"
3 #include "evoral/Curve.hpp"
6 CPPUNIT_TEST_SUITE_REGISTRATION (CurveTest);
8 #if defined(PLATFORM_WINDOWS) && defined(COMPILER_MINGW)
9 /* cppunit-1.13.2 uses assertion_traits<double>
10 * sprintf( , "%.*g", precision, x)
11 * to format a double. The actual comparison is performed on a string.
12 * This is problematic with mingw/windows|wine, "%.*g" formatting fails.
14 * This quick hack compares float, however float compatisons are at most Y.MMMM+eXX,
15 * the max precision needs to be limited. to the last mantissa digit.
17 * Anyway, actual maths is verified with Linux and OSX unit-tests,
18 * and this needs to go to https://sourceforge.net/p/cppunit/bugs/
20 #define MAXPREC(P) ((P) < .0005 ? .0005 : (P))
21 #define CPPUNIT_ASSERT_DOUBLES_EQUAL_MESSAGE(M,A,B,P) CPPUNIT_ASSERT_EQUAL_MESSAGE(M, (float)rint ((A) / MAXPREC(P)),(float)rint ((B) / MAXPREC(P)))
22 #define CPPUNIT_ASSERT_DOUBLES_EQUAL(A,B,P) CPPUNIT_ASSERT_EQUAL((float)rint ((A) / MAXPREC(P)),(float)rint ((B) / MAXPREC(P)))
25 using namespace Evoral;
27 // linear y = Y0 + YS * x ; with x = i * (X1 - X0) + X0; and i = [0..1023]
28 #define VEC1024LINCMP(X0, X1, Y0, YS) \
29 cl->curve ().get_vector ((X0), (X1), vec, 1024); \
30 for (int i = 0; i < 1024; ++i) { \
32 snprintf (msg, 64, "at i=%d (x0=%.1f, x1=%.1f, y0=%.1f, ys=%.3f)", \
34 CPPUNIT_ASSERT_DOUBLES_EQUAL_MESSAGE ( \
36 (Y0) + i * (YS), vec[i], \
42 CurveTest::twoPointLinear ()
46 boost::shared_ptr<Evoral::ControlList> cl = TestCtrlList();
49 cl->set_interpolation (ControlList::Linear);
51 // add two points to curve
52 cl->fast_simple_add ( 0.0 , 2048.0);
53 cl->fast_simple_add (8192.0 , 4096.0);
55 cl->curve ().get_vector (1024.0, 2047.0, vec, 1024);
57 VEC1024LINCMP (1024.0, 2047.0, 2304.f, .25f);
58 VEC1024LINCMP (2048.0, 2559.5, 2560.f, .125f);
59 VEC1024LINCMP ( 0.0, 4092.0, 2048.f, 1.f);
61 // greetings to tartina
62 cl->curve ().get_vector (2048.0, 2048.0, vec, 1);
63 CPPUNIT_ASSERT_EQUAL_MESSAGE ("veclen=1 @ 2048..2048", 2560.f, vec[0]);
65 /* XXX WHAT DO WE EXPECT WITH veclen=1 AND x1 > x0 ? */
67 /* .. interpolated value at (x1+x0)/2 */
68 cl->curve ().get_vector (2048.0, 2049.0, vec, 1);
69 CPPUNIT_ASSERT_EQUAL_MESSAGE ("veclen=1 @ 2048-2049", 2560.125f, vec[0]);
71 cl->curve ().get_vector (2048.0, 2056.0, vec, 1);
72 CPPUNIT_ASSERT_EQUAL_MESSAGE ("veclen=1 @ 2048-2049", 2561.f, vec[0]);
75 cl->curve ().get_vector (2048.0, 2049.0, vec, 1);
76 CPPUNIT_ASSERT_EQUAL_MESSAGE ("veclen=1 , 2048..2049", 2560.f, vec[0]);
78 cl->curve ().get_vector (2048.0, 2056.0, vec, 1);
79 CPPUNIT_ASSERT_EQUAL_MESSAGE ("veclen=1 , 2048..2049", 2560.f, vec[0]);
82 cl->curve ().get_vector (2048.0, 2048.0, vec, 2);
83 CPPUNIT_ASSERT_EQUAL_MESSAGE ("veclen=2 , 2048..2048 @ 0", 2560.f, vec[0]);
84 CPPUNIT_ASSERT_EQUAL_MESSAGE ("veclen=2 , 2048..2048 @ 1", 2560.f, vec[1]);
86 cl->curve ().get_vector (2048.0, 2056.0, vec, 2);
87 CPPUNIT_ASSERT_EQUAL_MESSAGE ("veclen=2 , 2048..2056 @ 0", 2560.f, vec[0]);
88 CPPUNIT_ASSERT_EQUAL_MESSAGE ("veclen=2 , 2048..2056 @ 0", 2562.f, vec[1]);
90 cl->curve ().get_vector (2048.0, 2056.0, vec, 3);
91 CPPUNIT_ASSERT_EQUAL_MESSAGE ("veclen=3 , 2048..2056 @ 0", 2560.f, vec[0]);
92 CPPUNIT_ASSERT_EQUAL_MESSAGE ("veclen=3 , 2048..2056 @ 1", 2561.f, vec[1]);
93 CPPUNIT_ASSERT_EQUAL_MESSAGE ("veclen=3 , 2048..2056 @ 2", 2562.f, vec[2]);
95 /* check out-of range..
96 * we expect the first and last value - no interpolation
98 cl->curve ().get_vector (-1, -1, vec, 1);
99 CPPUNIT_ASSERT_EQUAL_MESSAGE ("veclen=1 @ -1", 2048.f, vec[0]);
101 cl->curve ().get_vector (9999.0, 9999.0, vec, 1);
102 CPPUNIT_ASSERT_EQUAL_MESSAGE ("veclen=1 @ 9999", 4096.f, vec[0]);
104 cl->curve ().get_vector (-999.0, 0, vec, 13);
105 for (int i = 0; i < 13; ++i) {
106 CPPUNIT_ASSERT_EQUAL_MESSAGE ("veclen=13 @ -999..0", 2048.f, vec[i]);
109 cl->curve ().get_vector (9998.0, 9999.0, vec, 8);
110 for (int i = 0; i < 8; ++i) {
111 CPPUNIT_ASSERT_EQUAL_MESSAGE ("veclen=8 @ 9998..9999", 4096.f, vec[i]);
116 CurveTest::threePointLinear ()
120 boost::shared_ptr<Evoral::ControlList> cl = TestCtrlList();
123 cl->set_interpolation (ControlList::Linear);
125 // add 3 points to curve
126 cl->fast_simple_add ( 0.0 , 2.0);
127 cl->fast_simple_add ( 100.0 , 4.0);
128 cl->fast_simple_add ( 200.0 , 0.0);
130 cl->curve ().get_vector (50.0, 60.0, vec, 1);
131 CPPUNIT_ASSERT_EQUAL_MESSAGE ("veclen=1 @ 50", 3.f, vec[0]);
133 cl->curve ().get_vector (100.0, 100.0, vec, 1);
134 CPPUNIT_ASSERT_EQUAL_MESSAGE ("veclen=1 @ 100", 4.f, vec[0]);
136 cl->curve ().get_vector (150.0, 150.0, vec, 1);
137 CPPUNIT_ASSERT_EQUAL_MESSAGE ("veclen=1 @ 150", 2.f, vec[0]);
139 cl->curve ().get_vector (130.0, 150.0, vec, 3);
140 CPPUNIT_ASSERT_EQUAL_MESSAGE ("veclen=3 130..150 @ 0", 2.8f, vec[0]);
141 CPPUNIT_ASSERT_EQUAL_MESSAGE ("veclen=3 130..150 @ 2", 2.4f, vec[1]);
142 CPPUNIT_ASSERT_EQUAL_MESSAGE ("veclen=3 130..150 @ 3", 2.0f, vec[2]);
144 cl->curve ().get_vector (80.0, 160.0, vec, 3);
145 CPPUNIT_ASSERT_EQUAL_MESSAGE ("veclen=3 80..160 @ 0", 3.6f, vec[0]);
146 CPPUNIT_ASSERT_EQUAL_MESSAGE ("veclen=3 80..160 @ 2", 3.2f, vec[1]);
147 CPPUNIT_ASSERT_EQUAL_MESSAGE ("veclen=3 80..160 @ 3", 1.6f, vec[2]);
151 CurveTest::threePointDiscete ()
153 boost::shared_ptr<Evoral::ControlList> cl = TestCtrlList();
154 cl->set_interpolation (ControlList::Discrete);
156 // add 3 points to curve
157 cl->fast_simple_add ( 0.0 , 2.0);
158 cl->fast_simple_add ( 100.0 , 4.0);
159 cl->fast_simple_add ( 200.0 , 0.0);
161 CPPUNIT_ASSERT_EQUAL(2.0, cl->unlocked_eval(80.));
162 CPPUNIT_ASSERT_EQUAL(4.0, cl->unlocked_eval(120.));
163 CPPUNIT_ASSERT_EQUAL(4.0, cl->unlocked_eval(160.));
165 cl->set_interpolation (ControlList::Linear);
167 CPPUNIT_ASSERT_EQUAL(3.6, cl->unlocked_eval(80.));
168 CPPUNIT_ASSERT_EQUAL(3.2, cl->unlocked_eval(120.));
169 CPPUNIT_ASSERT_EQUAL(1.6, cl->unlocked_eval(160.));
173 CurveTest::ctrlListEval ()
175 boost::shared_ptr<Evoral::ControlList> cl = TestCtrlList();
177 cl->fast_simple_add ( 0.0 , 2.0);
179 cl->set_interpolation (ControlList::Discrete);
180 CPPUNIT_ASSERT_EQUAL(2.0, cl->unlocked_eval(80.));
181 CPPUNIT_ASSERT_EQUAL(2.0, cl->unlocked_eval(120.));
182 CPPUNIT_ASSERT_EQUAL(2.0, cl->unlocked_eval(160.));
184 cl->set_interpolation (ControlList::Linear);
185 CPPUNIT_ASSERT_EQUAL(2.0, cl->unlocked_eval(80.));
186 CPPUNIT_ASSERT_EQUAL(2.0, cl->unlocked_eval(120.));
187 CPPUNIT_ASSERT_EQUAL(2.0, cl->unlocked_eval(160.));
189 cl->fast_simple_add ( 100.0 , 4.0);
191 cl->set_interpolation (ControlList::Discrete);
192 CPPUNIT_ASSERT_EQUAL(2.0, cl->unlocked_eval(80.));
193 CPPUNIT_ASSERT_EQUAL(4.0, cl->unlocked_eval(120.));
194 CPPUNIT_ASSERT_EQUAL(4.0, cl->unlocked_eval(160.));
196 cl->set_interpolation (ControlList::Linear);
197 CPPUNIT_ASSERT_EQUAL(3.6, cl->unlocked_eval(80.));
198 CPPUNIT_ASSERT_EQUAL(4.0, cl->unlocked_eval(120.));
199 CPPUNIT_ASSERT_EQUAL(4.0, cl->unlocked_eval(160.));
201 cl->fast_simple_add ( 200.0 , 0.0);
203 cl->set_interpolation (ControlList::Discrete);
204 CPPUNIT_ASSERT_EQUAL(2.0, cl->unlocked_eval(80.));
205 CPPUNIT_ASSERT_EQUAL(4.0, cl->unlocked_eval(120.));
206 CPPUNIT_ASSERT_EQUAL(4.0, cl->unlocked_eval(160.));
208 cl->set_interpolation (ControlList::Linear);
209 CPPUNIT_ASSERT_EQUAL(3.6, cl->unlocked_eval(80.));
210 CPPUNIT_ASSERT_EQUAL(3.2, cl->unlocked_eval(120.));
211 CPPUNIT_ASSERT_EQUAL(1.6, cl->unlocked_eval(160.));
213 cl->fast_simple_add ( 300.0 , 8.0);
215 cl->set_interpolation (ControlList::Discrete);
216 CPPUNIT_ASSERT_EQUAL(2.0, cl->unlocked_eval(80.));
217 CPPUNIT_ASSERT_EQUAL(4.0, cl->unlocked_eval(120.));
218 CPPUNIT_ASSERT_EQUAL(4.0, cl->unlocked_eval(160.));
219 CPPUNIT_ASSERT_EQUAL(0.0, cl->unlocked_eval(250.));
220 CPPUNIT_ASSERT_EQUAL(8.0, cl->unlocked_eval(999.));
222 cl->set_interpolation (ControlList::Linear);
223 CPPUNIT_ASSERT_EQUAL(3.6, cl->unlocked_eval(80.));
224 CPPUNIT_ASSERT_EQUAL(3.2, cl->unlocked_eval(120.));
225 CPPUNIT_ASSERT_EQUAL(1.6, cl->unlocked_eval(160.));
226 CPPUNIT_ASSERT_EQUAL(4.0, cl->unlocked_eval(250.));
227 CPPUNIT_ASSERT_EQUAL(8.0, cl->unlocked_eval(999.));
229 cl->fast_simple_add ( 400.0 , 9.0);
231 cl->set_interpolation (ControlList::Discrete);
232 CPPUNIT_ASSERT_EQUAL(2.0, cl->unlocked_eval(80.));
233 CPPUNIT_ASSERT_EQUAL(4.0, cl->unlocked_eval(120.));
234 CPPUNIT_ASSERT_EQUAL(4.0, cl->unlocked_eval(160.));
235 CPPUNIT_ASSERT_EQUAL(0.0, cl->unlocked_eval(250.));
236 CPPUNIT_ASSERT_EQUAL(8.0, cl->unlocked_eval(350.));
237 CPPUNIT_ASSERT_EQUAL(9.0, cl->unlocked_eval(999.));
239 cl->set_interpolation (ControlList::Linear);
240 CPPUNIT_ASSERT_EQUAL(3.6, cl->unlocked_eval(80.));
241 CPPUNIT_ASSERT_EQUAL(3.2, cl->unlocked_eval(120.));
242 CPPUNIT_ASSERT_EQUAL(1.6, cl->unlocked_eval(160.));
243 CPPUNIT_ASSERT_EQUAL(4.0, cl->unlocked_eval(250.));
244 CPPUNIT_ASSERT_EQUAL(8.5, cl->unlocked_eval(350.));
245 CPPUNIT_ASSERT_EQUAL(9.0, cl->unlocked_eval(999.));
249 CurveTest::constrainedCubic ()
256 static const struct point data[] = {
257 /* values from worked example in www.korf.co.uk/spline.pdf */
268 Evoral::Parameter p(type);
269 Evoral::ParameterDescriptor pd;
270 Evoral::ControlList l(p,pd);
273 l.set_interpolation(Evoral::ControlList::Curved);
275 for (i=0; i<sizeof(data)/sizeof(data[0]); i++) {
276 l.add (data[i].x, data[i].y);
279 Evoral::Curve curve(l);
282 curve.get_vector(-10, 110, f, 121);
284 const float *g = &f[10]; /* so g starts at x==0 */
286 /* given points - should be exactly equal */
287 CPPUNIT_ASSERT_EQUAL( 30.0f, g[-10]);
288 CPPUNIT_ASSERT_EQUAL( 30.0f, g[ 0]);
289 CPPUNIT_ASSERT_EQUAL(130.0f, g[ 10]);
290 CPPUNIT_ASSERT_EQUAL(150.0f, g[ 30]);
291 CPPUNIT_ASSERT_EQUAL(150.0f, g[ 40]);
292 CPPUNIT_ASSERT_EQUAL(150.0f, g[ 50]);
293 CPPUNIT_ASSERT_EQUAL(320.0f, g[100]);
294 CPPUNIT_ASSERT_EQUAL(320.0f, g[110]);
297 First segment, i=1, for 0 <= x <= 10
298 f'1(x1) = 2/((x2 – x1)/(y2 – y1) + (x1 – x0)/(y1 – y0))
299 = 2/((30 – 10)/(150 – 130) + (10 – 0)/(130 – 30))
301 f'1(x0) = 3/2*(y1 – y0)/(x1 – x0) - f'1(x1)/2
302 = 3/2*(130 – 30)/(10 – 0) – 1.818/2
304 f"1(x0) = -2*(f'1(x1) + 2* f'1(x0))/(x1 – x0) + 6*(y1 – y0)/ (x1 – x0)^2
305 = -2*(1.8181 + 2*14.0909)/(10 – 0) + 6*(130 – 30)/(10 – 0)^2
307 f"1(x1) = 2*(2*f'1(x1) + f'1(x0))/(x1 – x0) - 6*(y1 – y0)/ (x1 – x0)^2
308 = 2*(2*1.818 + 14.0909)/(10 – 0) – 6*(130 – 30)/(10 – 0)^2
310 d1 = 1/6 * (f"1(x1) - f"1(x0))/(x1 – x0)
311 = 1/6 * (-2.4545 – 0)/(10 – 0)
313 c1 = 1/2 * (x1*f"1(x0) – x0*f"1(x1))/(x1 – x0)
314 = 1/2 * (10*0 – 0*1.8181)/(10 – 0)
316 b1 = ((y1 – y0) – c1*(x21 – x20) – d1*( x31 – x30))/(x1 – x0)
317 = ((130 – 30) – 0*(102 – 02) + 0.0409*(103 – 03))/(10 – 0)
319 a1 = y0 – b1*x0 – c1*x20 – d1*x30
321 y1 = 30 + 14.09x - 0.0409x3 for 0 <= x <= 10
324 Second segment, i=2, for 10 <= x <= 30
325 f'2(x2) = 2/((x3 – x2)/(y3 – y2) + (x2 – x1)/(y2 – y1))
326 = 2/((50 – 30)/(150 – 150) + (30 – 10)/(150 – 130))
328 f'2(x1) = 2/((x2 – x1)/(y2 – y1) + (x1 – x0)/(y1 – y0))
331 f"2(x1) = -2*(f'2(x2) + 2* f'2(x1))/(x2 – x1) + 6*(y2 – y1)/ (x2 – x1)^2
332 = -2*(0 + 2*1.8181)/(30 – 10) + 6*(150 – 130)/(30 – 10)2
334 f"2(x2) = 2*(2*f'2(x2) + f'2(x1))/(x2 – x1) - 6*(y2 – y1)/ (x2 – x1)^2
335 = 2*(2*0 + 1.8181)/(30 – 10) – 6*(150 – 130)/(30 – 10)^2
338 d2 = 1/6 * (f"2(x2) - f"2(x1))/(x2 – x1)
339 = 1/6 * (-0.11818 + 0.063636)/(30 – 10)
341 c2 = 1/2 * (x2*f"2(x1) – x1*f"2(x2))/(x2 – x1)
342 = 1/2 * (-30*0.063636 + 10*0.11818)/(30 – 10)
344 b2 = ((y2 – y1) – c2*(x2^2 – x1^2) – d2*( x2^3 – x1^3))/(x2 – x1)
345 = ((150 – 130) + 0.01818*(302 – 102) + 0.0004545*(303 – 103))/(30 – 10)
347 a2 = y1 – b2*x1 – c2*x1^2 – d2*x1^3
348 = 130 – 2.31818*10 + 0.01818*102 + 0.0004545*103
350 y2 = 109.09 + 2.31818x - 0.01818x^2 - 0.0004545x^3 for 10 <= x <= 30
355 long double a1, b1, c1, d1, a2, b2, c2, d2, fdx0, fddx0, fdx1, fdx2, fddx1, fddx2;
356 double x0 = data[0].x;
357 double y0 = data[0].y;
358 double x1 = data[1].x;
359 double y1 = data[1].y;
360 double x2 = data[2].x;
361 double y2 = data[2].y;
362 double x3 = data[3].x;
363 double y3 = data[3].y;
365 double dx0 = x1 - x0;
366 double dy0 = y1 - y0;
367 double dx1 = x2 - x1;
368 double dy1 = y2 - y1;
369 double dx2 = x3 - x2;
370 double dy2 = y3 - y2;
372 // First (leftmost) segment
373 fdx1 = 2.0 / ( dx1 / dy1 + dx0 / dy0 );
374 fdx0 = 3.0 / 2.0 * dy0 / dx0 - fdx1 / 2.0;
376 fddx0 = -2.0 * (fdx1 + 2.0 * fdx0) / dx0 + 6.0 * dy0 / (dx0*dx0);
377 fddx1 = 2.0 * (2.0 * fdx1 + fdx0) / dx0 - 6.0 * dy0 / (dx0*dx0);
378 d1 = 1.0 / 6.0 * (fddx1 - fddx0) / dx0;
379 c1 = 1.0 / 2.0 * (x1 * fddx0 - x0 * fddx1) / dx0;
380 b1 = (dy0 - c1 * (x1* x1 - x0*x0) - d1 * (x1*x1*x1 - x0*x0*x0)) / dx0;
381 a1 = y0 - b1*x0 - c1*x0*x0 - d1*x0*x0*x0;
383 // printf("dx0=%f, dy0=%f, dx1=%f, dy1=%f\n", dx0, dy0, dx1, dy1);
384 // printf("fdx0=%Lf, fdx1=%Lf, fddx0=%Lf, fddx1=%Lf\n", fdx0, fdx1, fddx0, fddx1);
385 // printf("a1=%Lf, b1=%Lf, c1=%Lf, d1=%Lf\n", a1, b1, c1, d1);
387 // values from worked example: deltas rather arbitrary, I'm afraid
388 CPPUNIT_ASSERT_DOUBLES_EQUAL(30.0, a1, 0.1);
389 CPPUNIT_ASSERT_DOUBLES_EQUAL(14.09, b1, 0.01);
390 CPPUNIT_ASSERT_DOUBLES_EQUAL(0.0, c1, 0.1);
391 CPPUNIT_ASSERT_DOUBLES_EQUAL(-0.0409, d1, 0.0001);
393 for (x = 0; x <= 10; x++) {
394 double v = a1 + b1*x + c1*x*x + d1*x*x*x;
396 snprintf(msg, 64, "interpolating %d: v=%f, x=%f...\n", x, v, g[x]);
397 CPPUNIT_ASSERT_DOUBLES_EQUAL(v, g[x], 0.000004);
401 fdx2 = 2.0 / ( dx2 / dy2 + dx1 / dy1 );
403 fddx1 = -2.0 * (fdx2 + 2.0 * fdx1) / dx1 + 6.0 * dy1 / (dx1*dx1);
404 fddx2 = 2.0 * (2.0 * fdx2 + fdx1) / dx1 - 6.0 * dy1 / (dx1*dx1);
405 d2 = 1.0 / 6.0 * (fddx2 - fddx1) / dx1;
406 c2 = 1.0 / 2.0 * (x2 * fddx1 - x1 * fddx2) / dx1;
407 b2 = (dy1 - c2 * (x2*x2 - x1*x1) - d2 * (x2*x2*x2 - x1*x1*x1)) / dx1;
408 a2 = y1 - b2*x1 - c2*x1*x1 - d2*x1*x1*x1;
410 // printf("dx0=%f, dy0=%f, dx1=%f, dy1=%f dx2=%f, dy2=%f\n", dx0, dy0, dx1, dy1, dx2, dy2);
411 // printf("fdx1=%Lf, fdx2=%Lf, fddx1=%Lf, fddx2=%Lf\n", fdx1, fdx2, fddx1, fddx2);
412 // printf("a2=%Lf, b2=%Lf, c2=%Lf, d2=%Lf\n", a2, b2, c2, d2);
414 // values from worked example: deltas rather arbitrary, I'm afraid
415 CPPUNIT_ASSERT_DOUBLES_EQUAL(109.09, a2, 0.01);
416 CPPUNIT_ASSERT_DOUBLES_EQUAL(2.31818, b2, 0.00001);
417 CPPUNIT_ASSERT_DOUBLES_EQUAL(-0.01818, c2, 0.00001);
418 CPPUNIT_ASSERT_DOUBLES_EQUAL(-0.0004545, d2, 0.0000001);
420 for (x = 10; x <= 30; x++) {
421 double v = a2 + b2*x + c2*x*x + d2*x*x*x;
423 snprintf(msg, 64, "interpolating %d: v=%f, x=%f...\n", x, v, g[x]);
424 CPPUNIT_ASSERT_DOUBLES_EQUAL(v, g[x], 0.000008);