]> git.sesse.net Git - casparcg/blob - common/tweener.cpp
[general] Changed permissions of files that should be marked as executable but wasn't.
[casparcg] / common / tweener.cpp
1 /*
2 * Copyright (c) 2011 Sveriges Television AB <info@casparcg.com>
3 *
4 * This file is part of CasparCG (www.casparcg.com).
5 *
6 * CasparCG 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 3 of the License, or
9 * (at your option) any later version.
10 *
11 * CasparCG 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 CasparCG. If not, see <http://www.gnu.org/licenses/>.
18 *
19 * Author: Robert Nagy, ronag89@gmail.com
20 */
21
22
23 // The following code is based on Tweener for actionscript, http://code.google.com/p/tweener/
24 //
25 //Disclaimer for Robert Penner's Easing Equations license:
26 //
27 //TERMS OF USE - EASING EQUATIONS
28 //
29 //Open source under the BSD License.
30 //
31 //Copyright � 2001 Robert Penner
32 //All rights reserved.
33 //
34 //Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
35 //
36 //    * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
37 //    * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
38 //    * Neither the name of the author nor the names of contributors may be used to endorse or promote products derived from this software without specific prior written permission.
39 //
40 //THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
41 #include "stdafx.h"
42
43 #include "tweener.h"
44
45 #include "except.h"
46 #include "linq.h"
47
48 #include <boost/regex.hpp>
49 #include <boost/lexical_cast.hpp>
50
51 #include <unordered_map>
52 #include <string>
53 #include <locale>
54 #include <functional>
55 #include <algorithm>
56 #include <vector>
57
58 namespace caspar {
59
60 typedef std::function<double(double, double, double, double)> tweener_t;
61                         
62 static const double PI = std::atan(1.0)*4.0;
63 static const double H_PI = std::atan(1.0)*2.0;
64
65 double ease_none (double t, double b, double c, double d, const std::vector<double>& params) 
66 {
67         return c*t/d + b;
68 }
69
70 double ease_in_quad (double t, double b, double c, double d, const std::vector<double>& params) 
71 {
72         return c*(t/=d)*t + b;
73 }
74         
75 double ease_out_quad (double t, double b, double c, double d, const std::vector<double>& params) 
76 {
77         return -c *(t/=d)*(t-2) + b;
78 }       
79
80 double ease_in_out_quad (double t, double b, double c, double d, const std::vector<double>& params)
81 {
82         if ((t/=d/2) < 1) 
83                 return c/2*t*t + b;
84
85         return -c/2 * ((--t)*(t-2) - 1) + b;
86 }       
87
88 double ease_out_in_quad (double t, double b, double c, double d, const std::vector<double>& params)
89 {
90         if (t < d/2) 
91                 return ease_out_quad (t*2, b, c/2, d, params);
92
93         return ease_in_quad((t*2)-d, b+c/2, c/2, d, params);
94 }
95         
96 double ease_in_cubic (double t, double b, double c, double d, const std::vector<double>& params) 
97 {
98         return c*(t/=d)*t*t + b;
99 }       
100
101 double ease_out_cubic (double t, double b, double c, double d, const std::vector<double>& params) 
102 {
103         return c*((t=t/d-1)*t*t + 1) + b;
104 }
105         
106 double ease_in_out_cubic (double t, double b, double c, double d, const std::vector<double>& params) 
107 {
108         if ((t/=d/2) < 1) 
109                 return c/2*t*t*t + b;
110
111         return c/2*((t-=2)*t*t + 2) + b;
112 }
113         
114 double ease_out_in_cubic (double t, double b, double c, double d, const std::vector<double>& params) 
115 {
116         if (t < d/2) return ease_out_cubic (t*2, b, c/2, d, params);
117         return ease_in_cubic((t*2)-d, b+c/2, c/2, d, params);
118 }
119         
120 double ease_in_quart (double t, double b, double c, double d, const std::vector<double>& params) 
121 {
122         return c*(t/=d)*t*t*t + b;
123 }
124         
125 double ease_out_quart (double t, double b, double c, double d, const std::vector<double>& params) 
126 {
127         return -c * ((t=t/d-1)*t*t*t - 1) + b;
128 }       
129
130 double ease_in_out_quart (double t, double b, double c, double d, const std::vector<double>& params) 
131 {
132         if ((t/=d/2) < 1)
133                 return c/2*t*t*t*t + b;
134
135         return -c/2 * ((t-=2)*t*t*t - 2) + b;
136 }       
137
138 double ease_out_in_quart (double t, double b, double c, double d, const std::vector<double>& params) 
139 {
140         if (t < d/2)
141                 return ease_out_quart (t*2, b, c/2, d, params);
142
143         return ease_in_quart((t*2)-d, b+c/2, c/2, d, params);
144 }       
145
146 double ease_in_quint (double t, double b, double c, double d, const std::vector<double>& params) 
147 {
148         return c*(t/=d)*t*t*t*t + b;
149 }
150         
151 double ease_out_quint (double t, double b, double c, double d, const std::vector<double>& params) 
152 {
153         return c*((t=t/d-1)*t*t*t*t + 1) + b;
154 }
155         
156 double ease_in_out_quint (double t, double b, double c, double d, const std::vector<double>& params) 
157 {
158         if ((t/=d/2) < 1) 
159                 return c/2*t*t*t*t*t + b;
160
161         return c/2*((t-=2)*t*t*t*t + 2) + b;
162 }
163         
164 double ease_out_in_quint (double t, double b, double c, double d, const std::vector<double>& params) 
165 {
166         if (t < d/2) 
167                 return ease_out_quint (t*2, b, c/2, d, params);
168
169         return ease_in_quint((t*2)-d, b+c/2, c/2, d, params);
170 }       
171
172 double ease_in_sine (double t, double b, double c, double d, const std::vector<double>& params) 
173 {
174         return -c * std::cos(t/d * (PI/2)) + c + b;
175 }       
176
177 double ease_out_sine (double t, double b, double c, double d, const std::vector<double>& params) 
178 {
179         return c * std::sin(t/d * (PI/2)) + b;
180 }       
181
182 double ease_in_out_sine (double t, double b, double c, double d, const std::vector<double>& params) 
183 {
184         return -c/2 * (std::cos(PI*t/d) - 1) + b;
185 }       
186
187 double ease_out_in_sine (double t, double b, double c, double d, const std::vector<double>& params)
188 {
189         if (t < d/2) 
190                 return ease_out_sine (t*2, b, c/2, d, params);
191         
192         return ease_in_sine((t*2)-d, b+c/2, c/2, d, params);
193 }       
194
195 double ease_in_expo (double t, double b, double c, double d, const std::vector<double>& params) 
196 {
197         return (t==0) ? b : c * std::pow(2, 10 * (t/d - 1)) + b - c * 0.001;
198 }       
199
200 double ease_out_expo (double t, double b, double c, double d, const std::vector<double>& params) 
201 {
202         return (t==d) ? b+c : c * 1.001 * (-std::pow(2, -10 * t/d) + 1) + b;
203 }
204         
205 double ease_in_out_expo (double t, double b, double c, double d, const std::vector<double>& params) 
206 {
207         if (t==0) 
208                 return b;
209         if (t==d) 
210                 return b+c;
211         if ((t/=d/2) < 1) 
212                 return c/2 * std::pow(2, 10 * (t - 1)) + b - c * 0.0005;
213
214         return c/2 * 1.0005 * (-std::pow(2, -10 * --t) + 2) + b;
215 }
216         
217 double ease_out_in_expo (double t, double b, double c, double d, const std::vector<double>& params) 
218 {
219         if (t < d/2) 
220                 return ease_out_expo (t*2, b, c/2, d, params);
221
222         return ease_in_expo((t*2)-d, b+c/2, c/2, d, params);
223 }
224         
225 double ease_in_circ (double t, double b, double c, double d, const std::vector<double>& params) 
226 {
227         return -c * (std::sqrt(1 - (t/=d)*t) - 1) + b;
228 }
229         
230 double ease_out_circ (double t, double b, double c, double d, const std::vector<double>& params) 
231 {
232         return c * std::sqrt(1 - (t=t/d-1)*t) + b;
233 }
234         
235 double ease_in_out_circ (double t, double b, double c, double d, const std::vector<double>& params) 
236 {
237         if ((t/=d/2) < 1) 
238                 return -c/2 * (std::sqrt(1 - t*t) - 1) + b;
239
240         return c/2 * (std::sqrt(1 - (t-=2)*t) + 1) + b;
241 }
242         
243 double ease_out_in_circ (double t, double b, double c, double d, const std::vector<double>& params) 
244 {
245         if (t < d/2) return ease_out_circ(t*2, b, c/2, d, params);
246         return ease_in_circ((t*2)-d, b+c/2, c/2, d, params);
247 }
248         
249 double ease_in_elastic (double t, double b, double c, double d, const std::vector<double>& params)
250 {
251         if (t==0) return b;
252         if ((t/=d)==1) return b+c;
253         //var p:Number = !Boolean(p_params) || isNaN(p_params.period) ? d*.3 : p_params.period;
254         //var s:Number;
255         //var a:Number = !Boolean(p_params) || isNaN(p_params.amplitude) ? 0 : p_params.amplitude;
256         double p = params.size() > 0 ? params[0] : d*0.3;
257         double s;
258         double a = params.size() > 1 ? params[1] : 0.0;
259         if (a == 0.0 || a < std::abs(c)) 
260         {
261                 a = c;
262                 s = p/4;
263         } 
264         else 
265                 s = p/(2*PI) * std::asin (c/a);
266         
267         return -(a*std::pow(2,10*(t-=1)) * std::sin( (t*d-s)*(2*PI)/p )) + b;
268 }
269         
270 double ease_out_elastic (double t, double b, double c, double d, const std::vector<double>& params) 
271 {
272         if (t==0) 
273                 return b;
274         if ((t/=d)==1) 
275                 return b+c;
276         //var p:Number = !Boolean(p_params) || isNaN(p_params.period) ? d*.3 : p_params.period;
277         //var s:Number;
278         //var a:Number = !Boolean(p_params) || isNaN(p_params.amplitude) ? 0 : p_params.amplitude;
279         double p = params.size() > 0 ? params[0] : d*0.3;
280         double s;
281         double a = params.size() > 1 ? params[1] : 0.0;
282         if (a == 0.0 || a < std::abs(c))
283         {
284                 a = c;
285                 s = p/4;
286         } 
287         else 
288                 s = p/(2*PI) * std::asin (c/a);
289         
290         return (a*std::pow(2,-10*t) * std::sin( (t*d-s)*(2*PI)/p ) + c + b);
291 }       
292
293 double ease_in_out_elastic (double t, double b, double c, double d, const std::vector<double>& params) 
294 {
295         if (t==0)
296                 return b;
297         if ((t/=d/2)==2) 
298                 return b+c;
299         //var p:Number = !Boolean(p_params) || isNaN(p_params.period) ? d*(.3*1.5) : p_params.period;
300         //var s:Number;
301         //var a:Number = !Boolean(p_params) || isNaN(p_params.amplitude) ? 0 : p_params.amplitude;
302         double p = params.size() > 0 ? params[0] : d*0.3*1.5;
303         double s;
304         double a = params.size() > 1 ? params[1] : 0.0;
305         if (a == 0.0 || a < std::abs(c)) 
306         {
307                 a = c;
308                 s = p/4;
309         }
310         else
311                 s = p/(2*PI) * std::asin (c/a);
312         
313         if (t < 1) 
314                 return -.5*(a*std::pow(2,10*(t-=1)) * std::sin( (t*d-s)*(2*PI)/p )) + b;
315
316         return a*std::pow(2,-10*(t-=1)) * std::sin( (t*d-s)*(2*PI)/p )*.5 + c + b;
317 }
318         
319 double ease_out_in_elastic (double t, double b, double c, double d, const std::vector<double>& params) 
320 {
321         if (t < d/2) return ease_out_elastic (t*2, b, c/2, d, params);
322         return ease_in_elastic((t*2)-d, b+c/2, c/2, d, params);
323 }
324         
325 double ease_in_back (double t, double b, double c, double d, const std::vector<double>& params) 
326 {
327         //var s:Number = !Boolean(p_params) || isNaN(p_params.overshoot) ? 1.70158 : p_params.overshoot;
328         double s = params.size() > 0 ? params[0] : 1.70158;
329         return c*(t/=d)*t*((s+1)*t - s) + b;
330 }
331         
332 double ease_out_back (double t, double b, double c, double d, const std::vector<double>& params)
333 {
334         //var s:Number = !Boolean(p_params) || isNaN(p_params.overshoot) ? 1.70158 : p_params.overshoot;
335         double s = params.size() > 0 ? params[0] : 1.70158;
336         return c*((t=t/d-1)*t*((s+1)*t + s) + 1) + b;
337 }
338         
339 double ease_in_out_back (double t, double b, double c, double d, const std::vector<double>& params)
340 {
341         //var s:Number = !Boolean(p_params) || isNaN(p_params.overshoot) ? 1.70158 : p_params.overshoot;
342         double s = params.size() > 0 ? params[0] : 1.70158;
343         if ((t/=d/2) < 1) return c/2*(t*t*(((s*=(1.525))+1)*t - s)) + b;
344         return c/2*((t-=2)*t*(((s*=(1.525))+1)*t + s) + 2) + b;
345 }
346         
347 double ease_out_int_back (double t, double b, double c, double d, const std::vector<double>& params)
348 {
349         if (t < d/2) return ease_out_back (t*2, b, c/2, d, params);
350         return ease_in_back((t*2)-d, b+c/2, c/2, d, params);
351 }
352         
353 double ease_out_bounce (double t, double b, double c, double d, const std::vector<double>& params) 
354 {
355         if ((t/=d) < (1/2.75))
356                 return c*(7.5625*t*t) + b;
357         else if (t < (2/2.75))
358                 return c*(7.5625*(t-=(1.5/2.75))*t + .75) + b;
359         else if (t < (2.5/2.75))
360                 return c*(7.5625*(t-=(2.25/2.75))*t + .9375) + b;
361         else 
362                 return c*(7.5625*(t-=(2.625/2.75))*t + .984375) + b;    
363 }
364         
365 double ease_in_bounce (double t, double b, double c, double d, const std::vector<double>& params)
366 {
367         return c - ease_out_bounce (d-t, 0, c, d, params) + b;
368 }
369
370 double ease_in_out_bounce (double t, double b, double c, double d, const std::vector<double>& params) 
371 {
372         if (t < d/2) return ease_in_bounce (t*2, 0, c, d, params) * .5 + b;
373         else return ease_out_bounce (t*2-d, 0, c, d, params) * .5 + c*.5 + b;
374 }
375         
376
377 double ease_out_in_bounce (double t, double b, double c, double d, const std::vector<double>& params) 
378 {
379         if (t < d/2) return ease_out_bounce (t*2, b, c/2, d, params);
380         return ease_in_bounce((t*2)-d, b+c/2, c/2, d, params);
381 }
382
383 typedef std::function<double(double, double, double, double, const std::vector<double>&)> tween_t;      
384
385 const std::unordered_map<std::wstring, tween_t>& get_tweens()
386 {
387         static const std::unordered_map<std::wstring, tween_t> tweens = {
388                 {L"",                 ease_none },
389                 {L"linear",           ease_none },
390                 {L"easenone",         ease_none },
391                 {L"easeinquad",       ease_in_quad },
392                 {L"easeoutquad",      ease_out_quad },
393                 {L"easeinoutquad",    ease_in_out_quad },
394                 {L"easeoutinquad",    ease_out_in_quad },
395                 {L"easeincubic",      ease_in_cubic },
396                 {L"easeoutcubic",     ease_out_cubic },
397                 {L"easeinoutcubic",   ease_in_out_cubic },
398                 {L"easeoutincubic",   ease_out_in_cubic },
399                 {L"easeinquart",      ease_in_quart },
400                 {L"easeoutquart",     ease_out_quart },
401                 {L"easeinoutquart",   ease_in_out_quart },
402                 {L"easeoutinquart",   ease_out_in_quart },
403                 {L"easeinquint",      ease_in_quint },
404                 {L"easeoutquint",     ease_out_quint },
405                 {L"easeinoutquint",   ease_in_out_quint },
406                 {L"easeoutinquint",   ease_out_in_quint },
407                 {L"easeinsine",       ease_in_sine },
408                 {L"easeoutsine",      ease_out_sine },
409                 {L"easeinoutsine",    ease_in_out_sine },
410                 {L"easeoutinsine",    ease_out_in_sine },
411                 {L"easeinexpo",       ease_in_expo },
412                 {L"easeoutexpo",      ease_out_expo },
413                 {L"easeinoutexpo",    ease_in_out_expo },
414                 {L"easeoutinexpo",    ease_out_in_expo },
415                 {L"easeincirc",       ease_in_circ },
416                 {L"easeoutcirc",      ease_out_circ },
417                 {L"easeinoutcirc",    ease_in_out_circ },
418                 {L"easeoutincirc",    ease_out_in_circ },
419                 {L"easeinelastic",    ease_in_elastic },
420                 {L"easeoutelastic",   ease_out_elastic },
421                 {L"easeinoutelastic", ease_in_out_elastic },
422                 {L"easeoutinelastic", ease_out_in_elastic },
423                 {L"easeinback",       ease_in_back },
424                 {L"easeoutback",      ease_out_back },
425                 {L"easeinoutback",    ease_in_out_back },
426                 {L"easeoutintback",   ease_out_int_back },
427                 {L"easeoutbounce",    ease_out_bounce },
428                 {L"easeinbounce",     ease_in_bounce },
429                 {L"easeinoutbounce",  ease_in_out_bounce },
430                 {L"easeoutinbounce",  ease_out_in_bounce }
431         };
432
433         return tweens;
434 }
435
436 tweener_t get_tweener(std::wstring name)
437 {
438         std::transform(name.begin(), name.end(), name.begin(), std::towlower);
439
440         if(name == L"linear")
441                 return [](double t, double b, double c, double d){return ease_none(t, b, c, d, std::vector<double>());};
442         
443         std::vector<double> params;
444         
445         static const boost::wregex expr(LR"((?<NAME>\w*)(:(?<V0>\d+\.?\d?))?(:(?<V1>\d+\.?\d?))?)"); // boost::regex has no repeated captures?
446         boost::wsmatch what;
447         if(boost::regex_match(name, what, expr))
448         {
449                 name = what["NAME"].str();
450                 if(what["V0"].matched)
451                         params.push_back(boost::lexical_cast<double>(what["V0"].str()));
452                 if(what["V1"].matched)
453                         params.push_back(boost::lexical_cast<double>(what["V1"].str()));
454         }
455                 
456         auto it = get_tweens().find(name);
457         if(it == get_tweens().end())
458                 CASPAR_THROW_EXCEPTION(user_error() << msg_info(L"Could not find tween " + name));
459         
460         auto tween = it->second;
461         return [=](double t, double b, double c, double d)
462         {
463                 return tween(t, b, c, d, params);
464         };
465 };
466
467 tweener::tweener(const std::wstring& name)
468         : func_(get_tweener(name))
469         , name_(name)
470 {
471 }
472
473 double tweener::operator()(double t, double b , double c, double d) const
474 {
475         return func_(t, b, c, d);
476 }
477
478 bool tweener::operator==(const tweener& other) const
479 {
480         return name_ == other.name_;
481 }
482
483 bool tweener::operator!=(const tweener& other) const
484 {
485         return !(*this == other);
486 }
487
488 const std::vector<std::wstring>& tweener::names()
489 {
490         /*static const auto names = cpplinq::from(get_tweens())
491                 .select(keys())
492                 .to_vector();*/
493
494         static const auto result = []
495         {
496                 std::vector<std::wstring> tweens;
497
498                 for (auto& tween : get_tweens())
499                         tweens.push_back(tween.first);
500
501                 return tweens;
502         }();
503         //static const std::vector<std::wstring> result;
504
505         return result;
506 }
507
508 }