remove whitespace at eol
[goodguy/history.git] / cinelerra-5.1 / cinelerra / floatauto.C
1
2 /*
3  * CINELERRA
4  * Copyright (C) 2008 Adam Williams <broadcast at earthling dot net>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  *
20  */
21
22 #include "autos.h"
23 #include "clip.h"
24 #include "edl.h"
25 #include "filexml.h"
26 #include "floatauto.h"
27 #include "floatautos.h"
28 #include "language.h"
29 #include "localsession.h"
30 #include "transportque.inc"
31 #include "automation.inc"
32
33 FloatAuto::FloatAuto(EDL *edl, FloatAutos *autos)
34  : Auto(edl, (Autos*)autos)
35 {
36         value = 0;
37         control_in_value = 0;
38         control_out_value = 0;
39         control_in_position = 0;
40         control_out_position = 0;
41         pos_valid = -1; //"dirty"
42         curve_mode = FREE;
43 //  note: in most cases the curve_mode value is set
44 //        by the method interpolate_from() rsp. copy_from()
45 }
46
47 FloatAuto::~FloatAuto()
48 {
49         // as we are going away, the neighbouring float auto nodes
50         // need to re-adjust their ctrl point positions and curves
51         if (next)
52                 ((FloatAuto*)next)->curve_dirty();
53         if (previous)
54                 ((FloatAuto*)previous)->curve_dirty();
55 }
56
57 int FloatAuto::operator==(Auto &that)
58 {
59         return identical((FloatAuto*)&that);
60 }
61
62
63 int FloatAuto::operator==(FloatAuto &that)
64 {
65         return identical((FloatAuto*)&that);
66 }
67
68 int FloatAuto::identical(FloatAuto *src)
69 {
70         return EQUIV(value, src->value) &&
71                 EQUIV(control_in_value, src->control_in_value) &&
72                 EQUIV(control_out_value, src->control_out_value);
73                 // ctrl positions ignored, as they may depend on neighbours
74                 // curve_mode is ignored, no recalculations
75 }
76
77 /* Note: the following is essentially display-code and has been moved to:
78  *  TrackCanvas::value_to_percentage(float auto_value, int autogrouptype)
79  *
80 float FloatAuto::value_to_percentage()
81 {
82 }
83 float FloatAuto::value_to_percentage()
84 {
85 }
86 float FloatAuto::value_to_percentage()
87 {
88 }
89 */
90
91
92 void FloatAuto::copy_from(Auto *that)
93 {
94         copy_from((FloatAuto*)that);
95 }
96
97 void FloatAuto::copy_from(FloatAuto *that)
98 {
99         Auto::copy_from(that);
100         this->value = that->value;
101         this->control_in_value = that->control_in_value;
102         this->control_out_value = that->control_out_value;
103         this->control_in_position = that->control_in_position;
104         this->control_out_position = that->control_out_position;
105         this->curve_mode = that->curve_mode;
106 // note: literate copy, no recalculations
107 }
108
109 inline
110 void FloatAuto::handle_automatic_curve_after_copy()
111 // in most cases, we don't want to use the manual curve modes
112 // of the left neighbour used as a template for interpolation.
113 // Rather, we (re)set to automatically smoothed curves. Note
114 // auto generated nodes (while tweaking values) indeed are
115 // inserted by using this "interpolation" approach, thus making
116 // this defaulting to auto-smooth curves very important.
117 {
118         if(curve_mode == FREE || curve_mode == TFREE)
119         {
120                 this->curve_mode = SMOOTH;
121         }
122 }
123
124
125 int FloatAuto::interpolate_from(Auto *a1, Auto *a2, int64_t pos, Auto *templ)
126 // bézier interpolates this->value and curves for the given position
127 // between the positions of a1 and a2. If a1 or a2 are omitted, they default
128 // to this->previous and this->next. If this FloatAuto has automatic curves,
129 // this may trigger re-adjusting of this and its neighbours in this->autos.
130 // Note while a1 and a2 need not be members of this->autos, automatic
131 // readjustments are always done to the neighbours in this->autos.
132 // If the template is given, it will be used to fill out this
133 // objects fields prior to interpolating.
134 {
135         if(!a1) a1 = previous;
136         if(!a2) a2 = next;
137         Auto::interpolate_from(a1, a2, pos, templ);
138         if( !templ ) handle_automatic_curve_after_copy();
139         if( curve_mode == SMOOTH && a1 && a2 &&
140             a1->is_floatauto() && a2->is_floatauto() &&
141             a1->position <= pos && pos <= a2->position ) {
142                 // set this->value using bézier interpolation if possible
143                 FloatAuto *left = (FloatAuto*)a1;
144                 FloatAuto *right = (FloatAuto*)a2;
145                 float new_value = FloatAutos::calculate_bezier(left, right, pos);
146                 float new_slope = FloatAutos::calculate_bezier_derivation(left, right, pos);
147                 this->adjust_to_new_coordinates(pos, new_value); // this may trigger smoothing
148                 this->set_control_in_value(new_slope * control_in_position);
149                 this->set_control_out_value(new_slope * control_out_position);
150                 return 1; //return true: interpolated indeed...
151         }
152
153         adjust_ctrl_positions(); // implies adjust_curves()
154         return 0; // unable to interpolate
155 }
156
157
158 void FloatAuto::change_curve_mode(t_mode new_mode)
159 {
160         if(new_mode == TFREE && !(control_in_position && control_out_position))
161                 new_mode = FREE; // only if curves on both sides...
162
163         curve_mode = new_mode;
164         adjust_curves();
165 }
166
167 void FloatAuto::toggle_curve_mode()
168 {
169         switch (curve_mode) {
170         case SMOOTH:    change_curve_mode(TFREE);  break;
171         case LINEAR:    change_curve_mode(FREE);   break;
172         case TFREE :    change_curve_mode(LINEAR); break;
173         case FREE  :    change_curve_mode(SMOOTH); break;
174         }
175 }
176
177
178 void FloatAuto::set_value(float newvalue)
179 {
180         this->value=newvalue;
181         this->adjust_curves();
182         if(previous) ((FloatAuto*)previous)->adjust_curves();
183         if(next)     ((FloatAuto*)next)->adjust_curves();
184 }
185
186 void FloatAuto::set_control_in_value(float newvalue)
187 {
188         switch(curve_mode) {
189         case TFREE:     control_out_value = control_out_position*newvalue / control_in_position;
190         case FREE:      control_in_value = newvalue;
191         default:        return; // otherwise calculated automatically...
192         }
193 }
194
195 void FloatAuto::set_control_out_value(float newvalue)
196 {
197         switch(curve_mode) {
198         case TFREE:     control_in_value = control_in_position*newvalue / control_out_position;
199         case FREE:      control_out_value=newvalue;
200         default:        return;
201         }
202 }
203
204
205
206 inline int sgn(float value) { return (value == 0)?  0 : (value < 0) ? -1 : 1; }
207
208 inline float weighted_mean(float v1, float v2, float w1, float w2){
209         if(0.000001 > fabs(w1 + w2))
210                 return 0;
211         else
212                 return (w1 * v1 + w2 * v2) / (w1 + w2);
213 }
214
215
216
217
218 void FloatAuto::adjust_curves()
219 // recalculates curves if current mode
220 // implies automatic adjustment of curves
221 {
222         if(!autos) return;
223
224         if(curve_mode == SMOOTH) {
225                 // normally, one would use the slope of chord between the neighbours.
226                 // but this could cause the curve to overshot extremal automation nodes.
227                 // (e.g when setting a fade node at zero, the curve could go negative)
228                 // we can interpret the slope of chord as a weighted mean value, where
229                 // the length of the interval is used as weight; we just use other
230                 // weights: intervall length /and/ reciprocal of slope. So, if the
231                 // connection to one of the neighbours has very low slope this will
232                 // dominate the calculated curve slope at this automation node.
233                 // if the slope goes beyond the zero line, e.g if left connection
234                 // has positive and right connection has negative slope, then
235                 // we force the calculated curve to be horizontal.
236                 float s, dxl, dxr, sl, sr;
237                 calculate_slope((FloatAuto*) previous, this, sl, dxl);
238                 calculate_slope(this, (FloatAuto*) next, sr, dxr);
239
240                 if(0 < sgn(sl) * sgn(sr))
241                 {
242                         float wl = fabs(dxl) * (fabs(1.0/sl) + 1);
243                         float wr = fabs(dxr) * (fabs(1.0/sr) + 1);
244                         s = weighted_mean(sl, sr, wl, wr);
245                 }
246                 else s = 0; // fixed hoizontal curve
247
248                 control_in_value = s * control_in_position;
249                 control_out_value = s * control_out_position;
250         }
251
252         else if(curve_mode == LINEAR) {
253                 float g, dx;
254                 if(previous)
255                 {
256                         calculate_slope(this, (FloatAuto*)previous, g, dx);
257                         control_in_value = g * dx / 3;
258                 }
259                 if(next)
260                 {
261                         calculate_slope(this, (FloatAuto*)next, g, dx);
262                         control_out_value = g * dx / 3;
263         }       }
264
265         else if(curve_mode == TFREE && control_in_position && control_out_position) {
266                 float gl = control_in_value / control_in_position;
267                 float gr = control_out_value / control_out_position;
268                 float wl = fabs(control_in_value);
269                 float wr = fabs(control_out_value);
270                 float g = weighted_mean(gl, gr, wl, wr);
271
272                 control_in_value = g * control_in_position;
273                 control_out_value = g * control_out_position;
274         }
275 }
276
277 inline void FloatAuto::calculate_slope(FloatAuto *left, FloatAuto *right, float &dvdx, float &dx)
278 {
279         dvdx=0; dx=0;
280         if(!left || !right) return;
281
282         dx = right->position - left->position;
283         float dv = right->value - left->value;
284         dvdx = (dx == 0) ? 0 : dv/dx;
285 }
286
287
288 void FloatAuto::adjust_ctrl_positions(FloatAuto *prev, FloatAuto *next)
289 // recalculates location of ctrl points to be
290 // always 1/3 and 2/3 of the distance to the
291 // next neighbours. The reason is: for this special
292 // distance the bézier function yields x(t) = t, i.e.
293 // we can use the y(t) as if it was a simple function y(x).
294
295 // This adjustment is done only on demand and involves
296 // updating neighbours and adjust_curves() as well.
297 {
298         if(!prev && !next)
299         {       // use current siblings
300                 prev = (FloatAuto*)this->previous;
301                 next = (FloatAuto*)this->next;
302         }
303
304         if(prev)
305         {       set_ctrl_positions(prev, this);
306                 prev->adjust_curves();
307         }
308         else // disable curve on left side
309                 control_in_position = 0;
310
311         if(next)
312         {       set_ctrl_positions(this, next);
313                 next->adjust_curves();
314         }
315         else // disable right curve
316                 control_out_position = 0;
317
318         this->adjust_curves();
319         pos_valid = position;
320 // curves up-to-date
321 }
322
323
324
325 inline void redefine_curve(int64_t &old_pos, int64_t new_pos, float &ctrl_val)
326 {
327         if(old_pos != 0)
328                 ctrl_val *= (float)new_pos / old_pos;
329         old_pos = new_pos;
330 }
331
332
333 inline void FloatAuto::set_ctrl_positions(FloatAuto *prev, FloatAuto* next)
334 {
335         int64_t distance = next->position - prev->position;
336         redefine_curve(prev->control_out_position, +distance / 3,  prev->control_out_value);
337         redefine_curve(next->control_in_position,  -distance / 3,  next->control_in_value);
338 }
339
340
341
342 void FloatAuto::adjust_to_new_coordinates(int64_t position, float value)
343 // define new position and value in one step, do necessary re-adjustments
344 {
345         this->value = value;
346         this->position = position;
347         adjust_ctrl_positions();
348 }
349
350
351
352 int FloatAuto::value_to_str(char *string, float value)
353 {
354         int j = 0, i = 0;
355         if(value > 0)
356                 sprintf(string, "+%.2f", value);
357         else
358                 sprintf(string, "%.2f", value);
359
360 // fix number
361         if(value == 0)
362         {
363                 j = 0;
364                 string[1] = 0;
365         }
366         else
367         if(value < 1 && value > -1)
368         {
369                 j = 1;
370                 string[j] = string[0];
371         }
372         else
373         {
374                 j = 0;
375                 string[3] = 0;
376         }
377
378         while(string[j] != 0) string[i++] = string[j++];
379         string[i] = 0;
380
381         return 0;
382 }
383
384 void FloatAuto::copy(int64_t start, int64_t end, FileXML *file, int default_auto)
385 {
386         file->tag.set_title("AUTO");
387         if(default_auto)
388                 file->tag.set_property("POSITION", 0);
389         else
390                 file->tag.set_property("POSITION", position - start);
391         file->tag.set_property("VALUE", value);
392         file->tag.set_property("CONTROL_IN_VALUE", control_in_value / 2.0); // compatibility, see below
393         file->tag.set_property("CONTROL_OUT_VALUE", control_out_value / 2.0);
394         file->tag.set_property("TANGENT_MODE", (int)curve_mode);
395         file->append_tag();
396         file->tag.set_title("/AUTO");
397         file->append_tag();
398         file->append_newline();
399 }
400
401 void FloatAuto::load(FileXML *file)
402 {
403         value = file->tag.get_property("VALUE", value);
404         control_in_value = file->tag.get_property("CONTROL_IN_VALUE", control_in_value);
405         control_out_value = file->tag.get_property("CONTROL_OUT_VALUE", control_out_value);
406         curve_mode = (t_mode)file->tag.get_property("TANGENT_MODE", (int)FREE);
407
408         // Compatibility to old session data format:
409         // Versions previous to the bezier auto patch (Jun 2006) applied a factor 2
410         // to the y-coordinates of ctrl points while calculating the bezier function.
411         // To retain compatibility, we now apply this factor while loading
412         control_in_value *= 2.0;
413         control_out_value *= 2.0;
414
415 // restore ctrl positions and adjust curves if necessary
416         adjust_ctrl_positions();
417 }
418
419 const char *FloatAuto::curve_name(int curve_mode)
420 {
421         switch( curve_mode ) {
422         case FloatAuto::SMOOTH: return _("Smooth");
423         case FloatAuto::LINEAR: return _("Linear");
424         case FloatAuto::TFREE:  return _("Tangent");
425         case FloatAuto::FREE:   return _("Disjoint");
426         }
427         return _("Error");
428 }
429