applied patches from Huub Schaeks
[swftools.git] / src / swfc-interpolation.c
1 /* swfc- Compiles swf code (.sc) files into .swf files.
2
3    Part of the swftools package.
4
5    Copyright (c) 2007 Huub Schaeks <huub@h-schaeks.speedlinq.nl>
6    Copyright (c) 2007 Matthias Kramm <kramm@quiss.org>
7  
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 2 of the License, or
11    (at your option) any later version.
12
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17
18    You should have received a copy of the GNU General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
21
22 #include <stdlib.h>
23 #include <math.h>
24 #include <memory.h>
25 #include "interpolation.h"
26
27 static inline float poly(float fraction, float start, float delta, int degree)
28 {
29         return delta * pow(fraction, degree) + start;
30 }
31
32 float linear(float fraction, float start, float delta)
33 {
34         return poly(fraction, start, delta, 1);
35 }
36
37 float quadIn(float fraction, float start, float delta)
38 {
39         return poly(fraction, start, delta, 2);
40 }
41
42 float quadOut(float fraction, float start, float delta)
43 {
44         return quadIn(1 - fraction, start + delta, -delta);
45 }
46
47 float quadInOut(float fraction, float start, float delta)
48 {
49         if (fraction < 0.5)
50                 return quadIn(2 * fraction, start, delta / 2);
51         return quadOut(2 * fraction - 1, start + delta / 2, delta / 2);
52 }
53
54 float cubicIn(float fraction, float start, float delta)
55 {
56         return poly(fraction, start, delta, 3);
57 }
58
59 float cubicOut(float fraction, float start, float delta)
60 {
61         return cubicIn(1 - fraction, start + delta, -delta);
62 }
63
64 float cubicInOut(float fraction, float start, float delta)
65 {
66         if (fraction < 0.5)
67                 return cubicIn(2 * fraction, start, delta / 2);
68         return cubicOut(2 * fraction - 1, start + delta / 2, delta / 2);
69 }
70
71 float quartIn(float fraction, float start, float delta)
72 {
73         return poly(fraction, start, delta, 4);
74 }
75
76 float quartOut(float fraction, float start, float delta)
77 {
78         return quartIn(1 - fraction, start + delta, -delta);
79 }
80
81 float quartInOut(float fraction, float start, float delta)
82 {
83         if (fraction < 0.5)
84                 return quartIn(2 * fraction, start, delta / 2);
85         return quartOut(2 * fraction - 1, start + delta / 2, delta / 2);
86 }
87
88 float quintIn(float fraction, float start, float delta)
89 {
90         return poly(fraction, start, delta, 5);
91 }
92
93 float quintOut(float fraction, float start, float delta)
94 {
95         return quintIn(1 - fraction, start + delta, -delta);
96 }
97
98 float quintInOut(float fraction, float start, float delta)
99 {
100         if (fraction < 0.5)
101                 return quintIn(2 * fraction, start, delta / 2);
102         return quintOut(2 * fraction - 1, start + delta / 2, delta / 2);
103 }
104
105 float circleIn(float fraction, float start, float delta)
106 {
107         return delta * (1 - sqrt(1 - fraction * fraction)) + start;
108 }
109
110 float circleOut(float fraction, float start, float delta)
111 {
112         return circleIn(1 - fraction, start + delta, -delta);
113 }
114
115 float circleInOut(float fraction, float start, float delta)
116 {
117         if (fraction < 0.5)
118                 return circleIn(2 * fraction, start, delta / 2);
119         return circleOut(2 * fraction - 1, start + delta / 2, delta / 2);
120 }
121
122 float exponentialIn(float fraction, float start, float delta)
123 {
124         if (fraction == 0)
125                 return start;
126         return delta * pow(2, 10 * (fraction - 1)) + start;
127 }
128
129 float exponentialOut(float fraction, float start, float delta)
130 {
131         return exponentialIn(1 - fraction, start + delta, -delta);
132 }
133
134 float exponentialInOut(float fraction, float start, float delta)
135 {
136         if (fraction < 0.5)
137                 return exponentialIn(2 * fraction, start, delta / 2);
138         return exponentialOut(2 * fraction - 1, start + delta / 2, delta / 2);
139 }
140
141 float sineIn(float fraction, float start, float delta)
142 {
143         return delta * (1 - cos(fraction * PI/2)) + start;
144 }
145
146 float sineOut(float fraction, float start, float delta)
147 {
148         return sineIn(1 - fraction, start + delta, -delta);
149 }
150
151 float sineInOut(float fraction, float start, float delta)
152 {
153         if (fraction < 0.5)
154                 return sineIn(2 * fraction, start, delta / 2);
155         return sineOut(2 * fraction - 1, start + delta / 2, delta / 2);
156 }
157
158 float elasticIn(float fraction, float start, float delta, float amplitude, int bounces, float damping)
159 {
160         if (fraction == 0 || delta == 0)
161                 return start;
162         if (fraction == 1)
163                 return start + delta;
164         if (amplitude < fabs(delta))
165                 amplitude = delta;
166         float period = 1 / (bounces + 0.25);
167 //      float s = asin(delta / amplitude) - 2 * PI / period;
168         return amplitude * pow(2, damping * (fraction - 1)) * sin(fraction * (2 * PI) / period /*+ fraction * s*/) + start;
169 }
170
171 float elasticOut(float fraction, float start, float delta, float amplitude, int bounces, float damping)
172 {
173         return elasticIn(1 - fraction, start + delta, -delta, amplitude, bounces, damping);
174 }
175         
176 float elasticInOut(float fraction, float start, float delta, float amplitude, int bounces, float damping)
177 {
178         if (fraction < 0.5)
179                 return elasticIn(2 * fraction, start, delta / 2, amplitude, bounces, damping);
180         return elasticOut(2 * fraction - 1, start + delta / 2, delta / 2, amplitude, bounces, damping);
181 }
182
183 float backIn(float fraction, float start, float delta, float speed)
184 {
185         return delta * fraction * fraction * ((speed + 1) * fraction - speed) + start;
186 }
187
188 float backOut(float fraction, float start, float delta, float speed)
189 {
190         return backIn(1 - fraction, start + delta, -delta, speed);
191 }
192
193 float backInOut(float fraction, float start, float delta, float speed)
194 {
195         if (fraction < 0.5)
196                 return backIn(2 * fraction, start, delta / 2, speed);
197         return backOut(2 * fraction - 1, start + delta / 2, delta / 2, speed);
198 }
199
200 /* when applied to movement bounceIn the object 'hits the floor' bounces times 
201  * (after leaving the floor first) before gently reaching the final position at the top of the final bounce
202  * Each bounce takes growth times a long as the previous, except for the last one which lasts only half 
203  * that time. The heights of the intermediate bounces are determined by the damping parameter.
204  * Set damping to 0 for an undamped movement.*/
205
206 float bounceIn(float fraction, float start, float delta, int bounces, float growth, float damping)
207 {
208         if (fraction == 0 || delta == 0)
209                 return start;
210         if (fraction == 1)
211                 return start + delta;
212         float w0;
213         if (growth == 1.0)
214                 w0 = 1 / (bounces + 0.5);
215         else
216         {
217                 float gN = pow(growth, bounces);
218                 w0 = 1 / ((gN - 1) / (growth - 1) + gN / 2 );
219         }
220         float bounceStart = 0;
221         int i;
222         float w = w0;
223         for (i = 0; i <= bounces; i++)
224         {
225                 float bounceEnd = bounceStart + w;
226                 if (fraction >= bounceStart && fraction < bounceEnd)
227                 {
228                         float half = (bounceEnd + bounceStart) / 2;
229                         float top = delta / pow(2, damping * ((bounces - i)));
230                         fraction -= half;
231                         fraction /= (w / 2);
232                         return (1 - fraction * fraction) * top + start;
233                 }
234                 bounceStart = bounceEnd;
235                 w = w * growth;
236         }
237 }
238
239 /* bounceOut is a time-reversed bounceIn; therefore each bounce takes 1/growth times as long as
240  * the previous, which I think fits the idea when applied to movement */
241
242 float bounceOut(float fraction, float start, float delta, int bounces, float growth, float damping)
243 {
244         return bounceIn(1 - fraction, start + delta, -delta, bounces, growth, damping);
245 }
246
247 /* since bounceIn and bounceOut are combined, if growth > 1 then the bounce-times will increase in
248  * the first half and decrease in the second half */
249
250 float bounceInOut(float fraction, float start, float delta, int bounces, float growth, float damping)
251 {
252         if (fraction < 0.5)
253                 return bounceIn(2 * fraction, start, delta / 2, bounces, growth, damping);
254         return bounceOut(2 * fraction - 1, start + delta / 2, delta / 2, bounces, growth, damping);
255 }
256 /* fastBounce(In/Out) doesn't end or start in a horizontal slope (= gentle end or start) as
257  * bounce(In/Out) do which means fastBounceInOut doesn't have the 'delay' in the middle */
258 float fastBounceIn(float fraction, float start, float delta, int bounces, float growth, float damping)
259 {
260         if (fraction == 0 || delta == 0)
261                 return start;
262         if (fraction == 1)
263                 return start + delta;
264         float w0;
265         if (growth == 1.0)
266                 w0 = 1 / (bounces + 0.25); /* in general (bounces + 1 / (2 * f)) */
267         else
268         {
269                 float gN = pow(growth, bounces);
270                 w0 = 1 / ((gN - 1) / (growth - 1) + gN / 4 /* in general: gN / (2 * f) */ );
271         }
272         float bounceStart = 0;
273         int i;
274         float w = w0;
275         for (i = 0; i <= bounces; i++)
276         {
277                 float bounceEnd = bounceStart + w;
278                 if (fraction >= bounceStart && fraction < bounceEnd)
279                 {
280                         float half = (bounceEnd + bounceStart) / 2;
281                         float top = delta / 0.75/* in general: (1 - (1 / f) * (1 / f)) */ / pow(2, damping * ((bounces - i)));
282                         fraction -= half;
283                         fraction /= (w / 2);
284                         return (1 - fraction * fraction) * top + start;
285                 }
286                 bounceStart = bounceEnd;
287                 w = w * growth;
288         }
289 }
290
291 float fastBounceOut(float fraction, float start, float delta, int bounces, float growth, float damping)
292 {
293         return fastBounceIn(1 - fraction, start + delta, -delta, bounces, growth, damping);
294 }
295
296 float fastBounceInOut(float fraction, float start, float delta, int bounces, float growth, float damping)
297 {
298         if (fraction < 0.5)
299                 return fastBounceIn(2 * fraction, start, delta / 2, bounces, growth, damping);
300         return fastBounceOut(2 * fraction - 1, start + delta / 2, delta / 2, bounces, growth, damping);
301 }