]> git.sesse.net Git - casparcg/blob - common/tweener.cpp
Remove most of boost::lexical_cast.
[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
50 #include <unordered_map>
51 #include <string>
52 #include <locale>
53 #include <functional>
54 #include <algorithm>
55 #include <vector>
56
57 namespace caspar {
58
59 typedef std::function<double(double, double, double, double)> tweener_t;
60                         
61 static const double PI = std::atan(1.0)*4.0;
62 static const double H_PI = std::atan(1.0)*2.0;
63
64 double ease_none (double t, double b, double c, double d, const std::vector<double>& params) 
65 {
66         return c*t/d + b;
67 }
68
69 double ease_in_quad (double t, double b, double c, double d, const std::vector<double>& params) 
70 {
71         return c*(t/=d)*t + b;
72 }
73         
74 double ease_out_quad (double t, double b, double c, double d, const std::vector<double>& params) 
75 {
76         return -c *(t/=d)*(t-2) + b;
77 }       
78
79 double ease_in_out_quad (double t, double b, double c, double d, const std::vector<double>& params)
80 {
81         if ((t/=d/2) < 1) 
82                 return c/2*t*t + b;
83
84         return -c/2 * ((--t)*(t-2) - 1) + b;
85 }       
86
87 double ease_out_in_quad (double t, double b, double c, double d, const std::vector<double>& params)
88 {
89         if (t < d/2) 
90                 return ease_out_quad (t*2, b, c/2, d, params);
91
92         return ease_in_quad((t*2)-d, b+c/2, c/2, d, params);
93 }
94         
95 double ease_in_cubic (double t, double b, double c, double d, const std::vector<double>& params) 
96 {
97         return c*(t/=d)*t*t + b;
98 }       
99
100 double ease_out_cubic (double t, double b, double c, double d, const std::vector<double>& params) 
101 {
102         return c*((t=t/d-1)*t*t + 1) + b;
103 }
104         
105 double ease_in_out_cubic (double t, double b, double c, double d, const std::vector<double>& params) 
106 {
107         if ((t/=d/2) < 1) 
108                 return c/2*t*t*t + b;
109
110         return c/2*((t-=2)*t*t + 2) + b;
111 }
112         
113 double ease_out_in_cubic (double t, double b, double c, double d, const std::vector<double>& params) 
114 {
115         if (t < d/2) return ease_out_cubic (t*2, b, c/2, d, params);
116         return ease_in_cubic((t*2)-d, b+c/2, c/2, d, params);
117 }
118         
119 double ease_in_quart (double t, double b, double c, double d, const std::vector<double>& params) 
120 {
121         return c*(t/=d)*t*t*t + b;
122 }
123         
124 double ease_out_quart (double t, double b, double c, double d, const std::vector<double>& params) 
125 {
126         return -c * ((t=t/d-1)*t*t*t - 1) + b;
127 }       
128
129 double ease_in_out_quart (double t, double b, double c, double d, const std::vector<double>& params) 
130 {
131         if ((t/=d/2) < 1)
132                 return c/2*t*t*t*t + b;
133
134         return -c/2 * ((t-=2)*t*t*t - 2) + b;
135 }       
136
137 double ease_out_in_quart (double t, double b, double c, double d, const std::vector<double>& params) 
138 {
139         if (t < d/2)
140                 return ease_out_quart (t*2, b, c/2, d, params);
141
142         return ease_in_quart((t*2)-d, b+c/2, c/2, d, params);
143 }       
144
145 double ease_in_quint (double t, double b, double c, double d, const std::vector<double>& params) 
146 {
147         return c*(t/=d)*t*t*t*t + b;
148 }
149         
150 double ease_out_quint (double t, double b, double c, double d, const std::vector<double>& params) 
151 {
152         return c*((t=t/d-1)*t*t*t*t + 1) + b;
153 }
154         
155 double ease_in_out_quint (double t, double b, double c, double d, const std::vector<double>& params) 
156 {
157         if ((t/=d/2) < 1) 
158                 return c/2*t*t*t*t*t + b;
159
160         return c/2*((t-=2)*t*t*t*t + 2) + b;
161 }
162         
163 double ease_out_in_quint (double t, double b, double c, double d, const std::vector<double>& params) 
164 {
165         if (t < d/2) 
166                 return ease_out_quint (t*2, b, c/2, d, params);
167
168         return ease_in_quint((t*2)-d, b+c/2, c/2, d, params);
169 }       
170
171 double ease_in_sine (double t, double b, double c, double d, const std::vector<double>& params) 
172 {
173         return -c * std::cos(t/d * (PI/2)) + c + b;
174 }       
175
176 double ease_out_sine (double t, double b, double c, double d, const std::vector<double>& params) 
177 {
178         return c * std::sin(t/d * (PI/2)) + b;
179 }       
180
181 double ease_in_out_sine (double t, double b, double c, double d, const std::vector<double>& params) 
182 {
183         return -c/2 * (std::cos(PI*t/d) - 1) + b;
184 }       
185
186 double ease_out_in_sine (double t, double b, double c, double d, const std::vector<double>& params)
187 {
188         if (t < d/2) 
189                 return ease_out_sine (t*2, b, c/2, d, params);
190         
191         return ease_in_sine((t*2)-d, b+c/2, c/2, d, params);
192 }       
193
194 double ease_in_expo (double t, double b, double c, double d, const std::vector<double>& params) 
195 {
196         return (t==0) ? b : c * std::pow(2, 10 * (t/d - 1)) + b - c * 0.001;
197 }       
198
199 double ease_out_expo (double t, double b, double c, double d, const std::vector<double>& params) 
200 {
201         return (t==d) ? b+c : c * 1.001 * (-std::pow(2, -10 * t/d) + 1) + b;
202 }
203         
204 double ease_in_out_expo (double t, double b, double c, double d, const std::vector<double>& params) 
205 {
206         if (t==0) 
207                 return b;
208         if (t==d) 
209                 return b+c;
210         if ((t/=d/2) < 1) 
211                 return c/2 * std::pow(2, 10 * (t - 1)) + b - c * 0.0005;
212
213         return c/2 * 1.0005 * (-std::pow(2, -10 * --t) + 2) + b;
214 }
215         
216 double ease_out_in_expo (double t, double b, double c, double d, const std::vector<double>& params) 
217 {
218         if (t < d/2) 
219                 return ease_out_expo (t*2, b, c/2, d, params);
220
221         return ease_in_expo((t*2)-d, b+c/2, c/2, d, params);
222 }
223         
224 double ease_in_circ (double t, double b, double c, double d, const std::vector<double>& params) 
225 {
226         return -c * (std::sqrt(1 - (t/=d)*t) - 1) + b;
227 }
228         
229 double ease_out_circ (double t, double b, double c, double d, const std::vector<double>& params) 
230 {
231         return c * std::sqrt(1 - (t=t/d-1)*t) + b;
232 }
233         
234 double ease_in_out_circ (double t, double b, double c, double d, const std::vector<double>& params) 
235 {
236         if ((t/=d/2) < 1) 
237                 return -c/2 * (std::sqrt(1 - t*t) - 1) + b;
238
239         return c/2 * (std::sqrt(1 - (t-=2)*t) + 1) + b;
240 }
241         
242 double ease_out_in_circ (double t, double b, double c, double d, const std::vector<double>& params) 
243 {
244         if (t < d/2) return ease_out_circ(t*2, b, c/2, d, params);
245         return ease_in_circ((t*2)-d, b+c/2, c/2, d, params);
246 }
247         
248 double ease_in_elastic (double t, double b, double c, double d, const std::vector<double>& params)
249 {
250         if (t==0) return b;
251         if ((t/=d)==1) return b+c;
252         //var p:Number = !Boolean(p_params) || isNaN(p_params.period) ? d*.3 : p_params.period;
253         //var s:Number;
254         //var a:Number = !Boolean(p_params) || isNaN(p_params.amplitude) ? 0 : p_params.amplitude;
255         double p = params.size() > 0 ? params[0] : d*0.3;
256         double s;
257         double a = params.size() > 1 ? params[1] : 0.0;
258         if (a == 0.0 || a < std::abs(c)) 
259         {
260                 a = c;
261                 s = p/4;
262         } 
263         else 
264                 s = p/(2*PI) * std::asin (c/a);
265         
266         return -(a*std::pow(2,10*(t-=1)) * std::sin( (t*d-s)*(2*PI)/p )) + b;
267 }
268         
269 double ease_out_elastic (double t, double b, double c, double d, const std::vector<double>& params) 
270 {
271         if (t==0) 
272                 return b;
273         if ((t/=d)==1) 
274                 return b+c;
275         //var p:Number = !Boolean(p_params) || isNaN(p_params.period) ? d*.3 : p_params.period;
276         //var s:Number;
277         //var a:Number = !Boolean(p_params) || isNaN(p_params.amplitude) ? 0 : p_params.amplitude;
278         double p = params.size() > 0 ? params[0] : d*0.3;
279         double s;
280         double a = params.size() > 1 ? params[1] : 0.0;
281         if (a == 0.0 || a < std::abs(c))
282         {
283                 a = c;
284                 s = p/4;
285         } 
286         else 
287                 s = p/(2*PI) * std::asin (c/a);
288         
289         return (a*std::pow(2,-10*t) * std::sin( (t*d-s)*(2*PI)/p ) + c + b);
290 }       
291
292 double ease_in_out_elastic (double t, double b, double c, double d, const std::vector<double>& params) 
293 {
294         if (t==0)
295                 return b;
296         if ((t/=d/2)==2) 
297                 return b+c;
298         //var p:Number = !Boolean(p_params) || isNaN(p_params.period) ? d*(.3*1.5) : p_params.period;
299         //var s:Number;
300         //var a:Number = !Boolean(p_params) || isNaN(p_params.amplitude) ? 0 : p_params.amplitude;
301         double p = params.size() > 0 ? params[0] : d*0.3*1.5;
302         double s;
303         double a = params.size() > 1 ? params[1] : 0.0;
304         if (a == 0.0 || a < std::abs(c)) 
305         {
306                 a = c;
307                 s = p/4;
308         }
309         else
310                 s = p/(2*PI) * std::asin (c/a);
311         
312         if (t < 1) 
313                 return -.5*(a*std::pow(2,10*(t-=1)) * std::sin( (t*d-s)*(2*PI)/p )) + b;
314
315         return a*std::pow(2,-10*(t-=1)) * std::sin( (t*d-s)*(2*PI)/p )*.5 + c + b;
316 }
317         
318 double ease_out_in_elastic (double t, double b, double c, double d, const std::vector<double>& params) 
319 {
320         if (t < d/2) return ease_out_elastic (t*2, b, c/2, d, params);
321         return ease_in_elastic((t*2)-d, b+c/2, c/2, d, params);
322 }
323         
324 double ease_in_back (double t, double b, double c, double d, const std::vector<double>& params) 
325 {
326         //var s:Number = !Boolean(p_params) || isNaN(p_params.overshoot) ? 1.70158 : p_params.overshoot;
327         double s = params.size() > 0 ? params[0] : 1.70158;
328         return c*(t/=d)*t*((s+1)*t - s) + b;
329 }
330         
331 double ease_out_back (double t, double b, double c, double d, const std::vector<double>& params)
332 {
333         //var s:Number = !Boolean(p_params) || isNaN(p_params.overshoot) ? 1.70158 : p_params.overshoot;
334         double s = params.size() > 0 ? params[0] : 1.70158;
335         return c*((t=t/d-1)*t*((s+1)*t + s) + 1) + b;
336 }
337         
338 double ease_in_out_back (double t, double b, double c, double d, const std::vector<double>& params)
339 {
340         //var s:Number = !Boolean(p_params) || isNaN(p_params.overshoot) ? 1.70158 : p_params.overshoot;
341         double s = params.size() > 0 ? params[0] : 1.70158;
342         if ((t/=d/2) < 1) return c/2*(t*t*(((s*=(1.525))+1)*t - s)) + b;
343         return c/2*((t-=2)*t*(((s*=(1.525))+1)*t + s) + 2) + b;
344 }
345         
346 double ease_out_int_back (double t, double b, double c, double d, const std::vector<double>& params)
347 {
348         if (t < d/2) return ease_out_back (t*2, b, c/2, d, params);
349         return ease_in_back((t*2)-d, b+c/2, c/2, d, params);
350 }
351         
352 double ease_out_bounce (double t, double b, double c, double d, const std::vector<double>& params) 
353 {
354         if ((t/=d) < (1/2.75))
355                 return c*(7.5625*t*t) + b;
356         else if (t < (2/2.75))
357                 return c*(7.5625*(t-=(1.5/2.75))*t + .75) + b;
358         else if (t < (2.5/2.75))
359                 return c*(7.5625*(t-=(2.25/2.75))*t + .9375) + b;
360         else 
361                 return c*(7.5625*(t-=(2.625/2.75))*t + .984375) + b;    
362 }
363         
364 double ease_in_bounce (double t, double b, double c, double d, const std::vector<double>& params)
365 {
366         return c - ease_out_bounce (d-t, 0, c, d, params) + b;
367 }
368
369 double ease_in_out_bounce (double t, double b, double c, double d, const std::vector<double>& params) 
370 {
371         if (t < d/2) return ease_in_bounce (t*2, 0, c, d, params) * .5 + b;
372         else return ease_out_bounce (t*2-d, 0, c, d, params) * .5 + c*.5 + b;
373 }
374         
375
376 double ease_out_in_bounce (double t, double b, double c, double d, const std::vector<double>& params) 
377 {
378         if (t < d/2) return ease_out_bounce (t*2, b, c/2, d, params);
379         return ease_in_bounce((t*2)-d, b+c/2, c/2, d, params);
380 }
381
382 typedef std::function<double(double, double, double, double, const std::vector<double>&)> tween_t;      
383
384 const std::unordered_map<std::wstring, tween_t>& get_tweens()
385 {
386         static const std::unordered_map<std::wstring, tween_t> tweens = {
387                 {L"",                 ease_none },
388                 {L"linear",           ease_none },
389                 {L"easenone",         ease_none },
390                 {L"easeinquad",       ease_in_quad },
391                 {L"easeoutquad",      ease_out_quad },
392                 {L"easeinoutquad",    ease_in_out_quad },
393                 {L"easeoutinquad",    ease_out_in_quad },
394                 {L"easeincubic",      ease_in_cubic },
395                 {L"easeoutcubic",     ease_out_cubic },
396                 {L"easeinoutcubic",   ease_in_out_cubic },
397                 {L"easeoutincubic",   ease_out_in_cubic },
398                 {L"easeinquart",      ease_in_quart },
399                 {L"easeoutquart",     ease_out_quart },
400                 {L"easeinoutquart",   ease_in_out_quart },
401                 {L"easeoutinquart",   ease_out_in_quart },
402                 {L"easeinquint",      ease_in_quint },
403                 {L"easeoutquint",     ease_out_quint },
404                 {L"easeinoutquint",   ease_in_out_quint },
405                 {L"easeoutinquint",   ease_out_in_quint },
406                 {L"easeinsine",       ease_in_sine },
407                 {L"easeoutsine",      ease_out_sine },
408                 {L"easeinoutsine",    ease_in_out_sine },
409                 {L"easeoutinsine",    ease_out_in_sine },
410                 {L"easeinexpo",       ease_in_expo },
411                 {L"easeoutexpo",      ease_out_expo },
412                 {L"easeinoutexpo",    ease_in_out_expo },
413                 {L"easeoutinexpo",    ease_out_in_expo },
414                 {L"easeincirc",       ease_in_circ },
415                 {L"easeoutcirc",      ease_out_circ },
416                 {L"easeinoutcirc",    ease_in_out_circ },
417                 {L"easeoutincirc",    ease_out_in_circ },
418                 {L"easeinelastic",    ease_in_elastic },
419                 {L"easeoutelastic",   ease_out_elastic },
420                 {L"easeinoutelastic", ease_in_out_elastic },
421                 {L"easeoutinelastic", ease_out_in_elastic },
422                 {L"easeinback",       ease_in_back },
423                 {L"easeoutback",      ease_out_back },
424                 {L"easeinoutback",    ease_in_out_back },
425                 {L"easeoutintback",   ease_out_int_back },
426                 {L"easeoutbounce",    ease_out_bounce },
427                 {L"easeinbounce",     ease_in_bounce },
428                 {L"easeinoutbounce",  ease_in_out_bounce },
429                 {L"easeoutinbounce",  ease_out_in_bounce }
430         };
431
432         return tweens;
433 }
434
435 tweener_t get_tweener(std::wstring name)
436 {
437         std::transform(name.begin(), name.end(), name.begin(), std::towlower);
438
439         if(name == L"linear")
440                 return [](double t, double b, double c, double d){return ease_none(t, b, c, d, std::vector<double>());};
441         
442         std::vector<double> params;
443         
444         static const boost::wregex expr(LR"((?<NAME>\w*)(:(?<V0>\d+\.?\d?))?(:(?<V1>\d+\.?\d?))?)"); // boost::regex has no repeated captures?
445         boost::wsmatch what;
446         if(boost::regex_match(name, what, expr))
447         {
448                 name = what["NAME"].str();
449                 if(what["V0"].matched)
450                         params.push_back(std::stod(what["V0"].str()));
451                 if(what["V1"].matched)
452                         params.push_back(std::stod(what["V1"].str()));
453         }
454                 
455         auto it = get_tweens().find(name);
456         if(it == get_tweens().end())
457                 CASPAR_THROW_EXCEPTION(user_error() << msg_info(L"Could not find tween " + name));
458         
459         auto tween = it->second;
460         return [=](double t, double b, double c, double d)
461         {
462                 return tween(t, b, c, d, params);
463         };
464 };
465
466 tweener::tweener(const std::wstring& name)
467         : func_(get_tweener(name))
468         , name_(name)
469 {
470 }
471
472 double tweener::operator()(double t, double b , double c, double d) const
473 {
474         return func_(t, b, c, d);
475 }
476
477 bool tweener::operator==(const tweener& other) const
478 {
479         return name_ == other.name_;
480 }
481
482 bool tweener::operator!=(const tweener& other) const
483 {
484         return !(*this == other);
485 }
486
487 const std::vector<std::wstring>& tweener::names()
488 {
489         /*static const auto names = cpplinq::from(get_tweens())
490                 .select(keys())
491                 .to_vector();*/
492
493         static const auto result = []
494         {
495                 std::vector<std::wstring> tweens;
496
497                 for (auto& tween : get_tweens())
498                         tweens.push_back(tween.first);
499
500                 return tweens;
501         }();
502         //static const std::vector<std::wstring> result;
503
504         return result;
505 }
506
507 }