PluginInfo::type added to copy constructor. But why is the copy constructor defined...
[ardour.git] / libs / ardour / curve.cc
1 /*
2     Copyright (C) 2001-2003 Paul Davis 
3
4     Contains ideas derived from "Constrained Cubic Spline Interpolation" 
5     by CJC Kruger (www.korf.co.uk/spline.pdf).
6
7     This program is free software; you can redistribute it and/or modify
8     it under the terms of the GNU General Public License as published by
9     the Free Software Foundation; either version 2 of the License, or
10     (at your option) any later version.
11
12     This program is distributed in the hope that it will be useful,
13     but WITHOUT ANY WARRANTY; without even the implied warranty of
14     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15     GNU General Public License for more details.
16
17     You should have received a copy of the GNU General Public License
18     along with this program; if not, write to the Free Software
19     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20
21 */
22
23 #include <iostream>
24 #include <float.h>
25 #include <cmath>
26 #include <climits>
27 #include <cfloat>
28 #include <cmath>
29
30 #include <glibmm/thread.h>
31 #include <sigc++/bind.h>
32
33 #include "ardour/curve.h"
34
35 #include "i18n.h"
36
37 using namespace std;
38 using namespace ARDOUR;
39 using namespace sigc;
40 using namespace PBD;
41
42 Curve::Curve (double minv, double maxv, double canv, bool nostate)
43         : AutomationList (canv)
44 {
45         min_yval = minv;
46         max_yval = maxv;
47 }
48
49 Curve::Curve (const Curve& other)
50         : AutomationList (other)
51 {
52         min_yval = other.min_yval;
53         max_yval = other.max_yval;
54 }
55
56 Curve::Curve (const Curve& other, double start, double end)
57         : AutomationList (other, start, end)
58 {
59         min_yval = other.min_yval;
60         max_yval = other.max_yval;
61 }
62
63 Curve::Curve (const XMLNode& node)
64         : AutomationList (node)
65 {
66 }
67
68 Curve::~Curve ()
69 {
70 }
71
72 Curve&
73 Curve::operator= (const Curve& other)
74 {
75         if (this != &other) {
76                 *((AutomationList*)this) = other;
77                 min_yval = other.min_yval;
78                 max_yval = other.max_yval;
79         }
80         return *this;
81 }
82
83 void
84 Curve::solve ()
85 {
86         uint64_t npoints;
87
88         if (!_dirty) {
89                 return;
90         }
91         
92         if ((npoints = events.size()) > 2) {
93                 
94                 /* Compute coefficients needed to efficiently compute a constrained spline
95                    curve. See "Constrained Cubic Spline Interpolation" by CJC Kruger
96                    (www.korf.co.uk/spline.pdf) for more details.
97                 */
98
99                 double x[npoints];
100                 double y[npoints];
101                 uint64_t i;
102                 AutomationEventList::iterator xx;
103
104                 for (i = 0, xx = events.begin(); xx != events.end(); ++xx, ++i) {
105                         x[i] = (double) (*xx)->when;
106                         y[i] = (double) (*xx)->value;
107                 }
108
109                 double lp0, lp1, fpone;
110
111                 lp0 =(x[1] - x[0])/(y[1] - y[0]);
112                 lp1 = (x[2] - x[1])/(y[2] - y[1]);
113
114                 if (lp0*lp1 < 0) {
115                         fpone = 0;
116                 } else {
117                         fpone = 2 / (lp1 + lp0);
118                 }
119
120                 double fplast = 0;
121
122                 for (i = 0, xx = events.begin(); xx != events.end(); ++xx, ++i) {
123                         
124                         CurvePoint* cp = dynamic_cast<CurvePoint*>(*xx);
125
126                         if (cp == 0) {
127                                 fatal  << _("programming error: ")
128                                        << X_("non-CurvePoint event found in event list for a Curve")
129                                        << endmsg;
130                                 /*NOTREACHED*/
131                         }
132                         
133                         double xdelta;   /* gcc is wrong about possible uninitialized use */
134                         double xdelta2;  /* ditto */
135                         double ydelta;   /* ditto */
136                         double fppL, fppR;
137                         double fpi;
138
139                         if (i > 0) {
140                                 xdelta = x[i] - x[i-1];
141                                 xdelta2 = xdelta * xdelta;
142                                 ydelta = y[i] - y[i-1];
143                         }
144
145                         /* compute (constrained) first derivatives */
146                         
147                         if (i == 0) {
148
149                                 /* first segment */
150                                 
151                                 fplast = ((3 * (y[1] - y[0]) / (2 * (x[1] - x[0]))) - (fpone * 0.5));
152
153                                 /* we don't store coefficients for i = 0 */
154
155                                 continue;
156
157                         } else if (i == npoints - 1) {
158
159                                 /* last segment */
160
161                                 fpi = ((3 * ydelta) / (2 * xdelta)) - (fplast * 0.5);
162                                 
163                         } else {
164
165                                 /* all other segments */
166
167                                 double slope_before = ((x[i+1] - x[i]) / (y[i+1] - y[i]));
168                                 double slope_after = (xdelta / ydelta);
169
170                                 if (slope_after * slope_before < 0.0) {
171                                         /* slope changed sign */
172                                         fpi = 0.0;
173                                 } else {
174                                         fpi = 2 / (slope_before + slope_after);
175                                 }
176                                 
177                         }
178
179                         /* compute second derivative for either side of control point `i' */
180                         
181                         fppL = (((-2 * (fpi + (2 * fplast))) / (xdelta))) +
182                                 ((6 * ydelta) / xdelta2);
183                         
184                         fppR = (2 * ((2 * fpi) + fplast) / xdelta) -
185                                 ((6 * ydelta) / xdelta2);
186                         
187                         /* compute polynomial coefficients */
188
189                         double b, c, d;
190
191                         d = (fppR - fppL) / (6 * xdelta);   
192                         c = ((x[i] * fppL) - (x[i-1] * fppR))/(2 * xdelta);
193                         
194                         double xim12, xim13;
195                         double xi2, xi3;
196                         
197                         xim12 = x[i-1] * x[i-1];  /* "x[i-1] squared" */
198                         xim13 = xim12 * x[i-1];   /* "x[i-1] cubed" */
199                         xi2 = x[i] * x[i];        /* "x[i] squared" */
200                         xi3 = xi2 * x[i];         /* "x[i] cubed" */
201                         
202                         b = (ydelta - (c * (xi2 - xim12)) - (d * (xi3 - xim13))) / xdelta;
203
204                         /* store */
205
206                         cp->coeff[0] = y[i-1] - (b * x[i-1]) - (c * xim12) - (d * xim13);
207                         cp->coeff[1] = b;
208                         cp->coeff[2] = c;
209                         cp->coeff[3] = d;
210
211                         fplast = fpi;
212                 }
213                 
214         }
215
216         _dirty = false;
217 }
218
219 bool
220 Curve::rt_safe_get_vector (double x0, double x1, float *vec, int64_t veclen)
221 {
222         Glib::Mutex::Lock lm (lock, Glib::TRY_LOCK);
223
224         if (!lm.locked()) {
225                 return false;
226         } else {
227                 _get_vector (x0, x1, vec, veclen);
228                 return true;
229         }
230 }
231
232 void
233 Curve::get_vector (double x0, double x1, float *vec, int64_t veclen)
234 {
235         Glib::Mutex::Lock lm (lock);
236         _get_vector (x0, x1, vec, veclen);
237 }
238
239 void
240 Curve::_get_vector (double x0, double x1, float *vec, int64_t veclen)
241 {
242         double rx, dx, lx, hx, max_x, min_x;
243         int64_t i;
244         int64_t original_veclen;
245         int64_t npoints;
246
247         if ((npoints = events.size()) == 0) {
248                  for (i = 0; i < veclen; ++i) {
249                          vec[i] = default_value;
250                  }
251                  return;
252         }
253
254         /* events is now known not to be empty */
255
256         max_x = events.back()->when;
257         min_x = events.front()->when;
258
259         lx = max (min_x, x0);
260
261         if (x1 < 0) {
262                 x1 = events.back()->when;
263         }
264
265         hx = min (max_x, x1);
266
267         original_veclen = veclen;
268
269         if (x0 < min_x) {
270
271                 /* fill some beginning section of the array with the 
272                    initial (used to be default) value 
273                 */
274
275                 double frac = (min_x - x0) / (x1 - x0);
276                 int64_t subveclen = (int64_t) floor (veclen * frac);
277                 
278                 subveclen = min (subveclen, veclen);
279
280                 for (i = 0; i < subveclen; ++i) {
281                         vec[i] = events.front()->value;
282                 }
283
284                 veclen -= subveclen;
285                 vec += subveclen;
286         }
287
288         if (veclen && x1 > max_x) {
289
290                 /* fill some end section of the array with the default or final value */
291
292                 double frac = (x1 - max_x) / (x1 - x0);
293
294                 int64_t subveclen = (int64_t) floor (original_veclen * frac);
295
296                 float val;
297                 
298                 subveclen = min (subveclen, veclen);
299
300                 val = events.back()->value;
301
302                 i = veclen - subveclen;
303
304                 for (i = veclen - subveclen; i < veclen; ++i) {
305                         vec[i] = val;
306                 }
307
308                 veclen -= subveclen;
309         }
310
311         if (veclen == 0) {
312                 return;
313         }
314
315         if (npoints == 1 ) {
316         
317                 for (i = 0; i < veclen; ++i) {
318                         vec[i] = events.front()->value;
319                 }
320                 return;
321         }
322  
323  
324         if (npoints == 2) {
325  
326                 /* linear interpolation between 2 points */
327  
328                 /* XXX I'm not sure that this is the right thing to
329                    do here. but its not a common case for the envisaged
330                    uses.
331                 */
332         
333                 if (veclen > 1) {
334                         dx = (hx - lx) / (veclen - 1) ;
335                 } else {
336                         dx = 0; // not used
337                 }
338         
339                 double slope = (events.back()->value - events.front()->value)/  
340                         (events.back()->when - events.front()->when);
341                 double yfrac = dx*slope;
342  
343                 vec[0] = events.front()->value + slope * (lx - events.front()->when);
344  
345                 for (i = 1; i < veclen; ++i) {
346                         vec[i] = vec[i-1] + yfrac;
347                 }
348  
349                 return;
350         }
351  
352         if (_dirty) {
353                 solve ();
354         }
355
356         rx = lx;
357
358         if (veclen > 1) {
359
360                 /* note: if there are veclen elements in the output,
361                    there are only veclen-1 steps between them.
362                 */
363
364                 dx = (hx - lx) / (veclen-1);
365
366                 for (i = 0; i < veclen; ++i, rx += dx) {
367                         vec[i] = multipoint_eval (rx);
368                 }
369         }
370 }
371
372 double
373 Curve::unlocked_eval (double x)
374 {
375         if (_dirty) {
376                 solve ();
377         }
378
379         return shared_eval (x);
380 }
381
382 double
383 Curve::multipoint_eval (double x)
384 {       
385         pair<AutomationEventList::iterator,AutomationEventList::iterator> range;
386
387         if ((lookup_cache.left < 0) ||
388             ((lookup_cache.left > x) || 
389              (lookup_cache.range.first == events.end()) || 
390              ((*lookup_cache.range.second)->when < x))) {
391                 
392                 TimeComparator cmp;
393                 ControlEvent cp (x, 0.0);
394
395                 lookup_cache.range = equal_range (events.begin(), events.end(), &cp, cmp);
396         }
397
398         range = lookup_cache.range;
399
400         /* EITHER 
401            
402            a) x is an existing control point, so first == existing point, second == next point
403
404            OR
405
406            b) x is between control points, so range is empty (first == second, points to where
407                to insert x)
408            
409         */
410
411         if (range.first == range.second) {
412
413                 /* x does not exist within the list as a control point */
414                 
415                 lookup_cache.left = x;
416
417                 if (range.first == events.begin()) {
418                         /* we're before the first point */
419                         // return default_value;
420                         events.front()->value;
421                 }
422                 
423                 if (range.second == events.end()) {
424                         /* we're after the last point */
425                         return events.back()->value;
426                 }
427
428                 double x2 = x * x;
429                 CurvePoint* cp = dynamic_cast<CurvePoint*> (*range.second);
430
431                 return cp->coeff[0] + (cp->coeff[1] * x) + (cp->coeff[2] * x2) + (cp->coeff[3] * x2 * x);
432         } 
433
434         /* x is a control point in the data */
435         /* invalidate the cached range because its not usable */
436         lookup_cache.left = -1;
437         return (*range.first)->value;
438 }
439
440 ControlEvent*
441 Curve::point_factory (double when, double val) const
442 {
443         return new CurvePoint (when, val);
444 }
445
446 ControlEvent*
447 Curve::point_factory (const ControlEvent& other) const
448 {
449         return new CurvePoint (other.when, other.value);
450 }
451
452 extern "C" {
453
454 void 
455 curve_get_vector_from_c (void *arg, double x0, double x1, float* vec, int64_t vecsize)
456 {
457         static_cast<Curve*>(arg)->get_vector (x0, x1, vec, vecsize);
458 }
459
460 }