Use an enumeration type rather than unnamed integers to specify the different types...
[synfig.git] / synfig-core / trunk / src / modules / mod_noise / random.cpp
1 /* === S Y N F I G ========================================================= */
2 /*!     \file mod_noise/random.cpp
3 **      \brief blehh
4 **
5 **      $Id$
6 **
7 **      \legal
8 **      Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
9 **      Copyright (c) 2007 Chris Moore
10 **
11 **      This package is free software; you can redistribute it and/or
12 **      modify it under the terms of the GNU General Public License as
13 **      published by the Free Software Foundation; either version 2 of
14 **      the License, or (at your option) any later version.
15 **
16 **      This package is distributed in the hope that it will be useful,
17 **      but WITHOUT ANY WARRANTY; without even the implied warranty of
18 **      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 **      General Public License for more details.
20 **      \endlegal
21 */
22 /* ========================================================================= */
23
24 /* === H E A D E R S ======================================================= */
25
26 #ifdef USING_PCH
27 #       include "pch.h"
28 #else
29 #ifdef HAVE_CONFIG_H
30 #       include <config.h>
31 #endif
32
33 #include "random.h"
34 #include <synfig/quick_rng.h>
35 #include <cmath>
36 #include <cstdlib>
37 #endif
38
39 /* === M A C R O S ========================================================= */
40
41 #define PI      (3.1415927)
42
43 /* === G L O B A L S ======================================================= */
44
45 /* === P R O C E D U R E S ================================================= */
46
47 /* === M E T H O D S ======================================================= */
48
49 void
50 Random::set_seed(int x)
51 {
52         seed_=x;
53 }
54
55 float
56 Random::operator()(const int salt,const int x,const int y,const int t)const
57 {
58         static const unsigned int a(21870);
59         static const unsigned int b(11213);
60         static const unsigned int c(36979);
61         static const unsigned int d(31337);
62
63         quick_rng rng(
64                 ( static_cast<unsigned int>(x+y)        * a ) ^
65                 ( static_cast<unsigned int>(y+t)        * b ) ^
66                 ( static_cast<unsigned int>(t+x)        * c ) ^
67                 ( static_cast<unsigned int>(seed_+salt) * d )
68         );
69
70         return rng.f() * 2.0f - 1.0f;
71 }
72
73 float
74 Random::operator()(SmoothType smooth,int subseed,float xf,float yf,float tf)const
75 {
76         int x((int)floor(xf));
77         int y((int)floor(yf));
78         int t((int)floor(tf));
79
80         switch(smooth)
81         {
82         case SMOOTH_CUBIC:      // cubic
83                 {
84                         #define f(j,i,k)        ((*this)(subseed,i,j,k))
85                         //Using catmull rom interpolation because it doesn't blur at all
86                         // ( http://www.gamedev.net/reference/articles/article1497.asp )
87                         //bezier curve with intermediate ctrl pts: 0.5/3(p(i+1) - p(i-1)) and similar
88                         float xfa [4], tfa[4];
89
90                         //precalculate indices (all clamped) and offset
91                         const int xa[] = {x-1,x,x+1,x+2};
92
93                         const int ya[] = {y-1,y,y+1,y+2};
94
95                         const int ta[] = {t-1,t,t+1,t+2};
96
97                         const float dx(xf-x);
98                         const float dy(yf-y);
99                         const float dt(tf-t);
100
101                         //figure polynomials for each point
102                         const float txf[] =
103                         {
104                                 0.5*dx*(dx*(dx*(-1) + 2) - 1),  //-t + 2t^2 -t^3
105                                 0.5*(dx*(dx*(3*dx - 5)) + 2),   //2 - 5t^2 + 3t^3
106                                 0.5*dx*(dx*(-3*dx + 4) + 1),    //t + 4t^2 - 3t^3
107                                 0.5*dx*dx*(dx-1)                                //-t^2 + t^3
108                         };
109
110                         const float tyf[] =
111                         {
112                                 0.5*dy*(dy*(dy*(-1) + 2) - 1),  //-t + 2t^2 -t^3
113                                 0.5*(dy*(dy*(3*dy - 5)) + 2),   //2 - 5t^2 + 3t^3
114                                 0.5*dy*(dy*(-3*dy + 4) + 1),    //t + 4t^2 - 3t^3
115                                 0.5*dy*dy*(dy-1)                                //-t^2 + t^3
116                         };
117
118                         const float ttf[] =
119                         {
120                                 0.5*dt*(dt*(dt*(-1) + 2) - 1),  //-t + 2t^2 -t^3
121                                 0.5*(dt*(dt*(3*dt - 5)) + 2),   //2 - 5t^2 + 3t^3
122                                 0.5*dt*(dt*(-3*dt + 4) + 1),    //t + 4t^2 - 3t^3
123                                 0.5*dt*dt*(dt-1)                                //-t^2 + t^3
124                         };
125
126                         //evaluate polynomial for each row
127                         for(int i = 0; i < 4; ++i)
128                         {
129                                 for(int j = 0; j < 4; ++j)
130                                 {
131                                         tfa[j] = f(ya[i],xa[j],ta[0])*ttf[0] + f(ya[i],xa[j],ta[1])*ttf[1] + f(ya[i],xa[j],ta[2])*ttf[2] + f(ya[i],xa[j],ta[3])*ttf[3];
132                                 }
133                                 xfa[i] = tfa[0]*txf[0] + tfa[1]*txf[1] + tfa[2]*txf[2] + tfa[3]*txf[3];
134                         }
135
136                         //return the cumulative column evaluation
137                         return xfa[0]*tyf[0] + xfa[1]*tyf[1] + xfa[2]*tyf[2] + xfa[3]*tyf[3];
138 #undef f
139                 }
140                 break;
141
142
143         case SMOOTH_FAST_SPLINE:        // Fast Spline (non-animated)
144                 {
145 #define P(x)    (((x)>0)?((x)*(x)*(x)):0.0f)
146 #define R(x)    ( P(x+2) - 4.0f*P(x+1) + 6.0f*P(x) - 4.0f*P(x-1) )*(1.0f/6.0f)
147 #define F(i,j)  ((*this)(subseed,i+x,j+y)*(R((i)-a)*R(b-(j))))
148 #define FT(i,j,k)       ((*this)(subseed,i+x,j+y,k+t)*(R((i)-a)*R(b-(j))*R((k)-c)))
149 #define Z(i,j) ret+=F(i,j)
150 #define ZT(i,j,k) ret+=FT(i,j,k)
151 #define X(i,j)  // placeholder... To make box more symmetric
152 #define XT(i,j,k)       // placeholder... To make box more symmetric
153
154                 float a(xf-x), b(yf-y);
155
156                 // Interpolate
157                 float ret(F(0,0));
158                 Z(-1,-1); Z(-1, 0); Z(-1, 1); Z(-1, 2);
159                 Z( 0,-1); X( 0, 0); Z( 0, 1); Z( 0, 2);
160                 Z( 1,-1); Z( 1, 0); Z( 1, 1); Z( 1, 2);
161                 Z( 2,-1); Z( 2, 0); Z( 2, 1); Z( 2, 2);
162
163                 return ret;
164         }
165
166         case SMOOTH_SPLINE:     // Spline (animated)
167                 {
168                         float a(xf-x), b(yf-y), c(tf-t);
169
170                         // Interpolate
171                         float ret(FT(0,0,0));
172                         ZT(-1,-1,-1); ZT(-1, 0,-1); ZT(-1, 1,-1); ZT(-1, 2,-1);
173                         ZT( 0,-1,-1); ZT( 0, 0,-1); ZT( 0, 1,-1); ZT( 0, 2,-1);
174                         ZT( 1,-1,-1); ZT( 1, 0,-1); ZT( 1, 1,-1); ZT( 1, 2,-1);
175                         ZT( 2,-1,-1); ZT( 2, 0,-1); ZT( 2, 1,-1); ZT( 2, 2,-1);
176
177                         ZT(-1,-1, 0); ZT(-1, 0, 0); ZT(-1, 1, 0); ZT(-1, 2, 0);
178                         ZT( 0,-1, 0); XT( 0, 0, 0); ZT( 0, 1, 0); ZT( 0, 2, 0);
179                         ZT( 1,-1, 0); ZT( 1, 0, 0); ZT( 1, 1, 0); ZT( 1, 2, 0);
180                         ZT( 2,-1, 0); ZT( 2, 0, 0); ZT( 2, 1, 0); ZT( 2, 2, 0);
181
182                         ZT(-1,-1, 1); ZT(-1, 0, 1); ZT(-1, 1, 1); ZT(-1, 2, 1);
183                         ZT( 0,-1, 1); ZT( 0, 0, 1); ZT( 0, 1, 1); ZT( 0, 2, 1);
184                         ZT( 1,-1, 1); ZT( 1, 0, 1); ZT( 1, 1, 1); ZT( 1, 2, 1);
185                         ZT( 2,-1, 1); ZT( 2, 0, 1); ZT( 2, 1, 1); ZT( 2, 2, 1);
186
187                         ZT(-1,-1, 2); ZT(-1, 0, 2); ZT(-1, 1, 2); ZT(-1, 2, 2);
188                         ZT( 0,-1, 2); ZT( 0, 0, 2); ZT( 0, 1, 2); ZT( 0, 2, 2);
189                         ZT( 1,-1, 2); ZT( 1, 0, 2); ZT( 1, 1, 2); ZT( 1, 2, 2);
190                         ZT( 2,-1, 2); ZT( 2, 0, 2); ZT( 2, 1, 2); ZT( 2, 2, 2);
191
192                         return ret;
193
194 /*
195
196                         float dx=xf-x;
197                         float dy=yf-y;
198                         float dt=tf-t;
199
200                         float ret=0;
201                         int i,j,h;
202                         for(h=-1;h<=2;h++)
203                                 for(i=-1;i<=2;i++)
204                                         for(j=-1;j<=2;j++)
205                                                 ret+=(*this)(subseed,i+x,j+y,h+t)*(R(i-dx)*R(j-dy)*R(h-dt));
206                         return ret;
207 */
208                 }
209                 break;
210 #undef X
211 #undef Z
212 #undef F
213 #undef P
214 #undef R
215
216         case SMOOTH_COSINE:
217         if((float)t==tf)
218         {
219                 int x((int)floor(xf));
220                 int y((int)floor(yf));
221                 float a=xf-x;
222                 float b=yf-y;
223                 a=(1.0f-cos(a*PI))*0.5f;
224                 b=(1.0f-cos(b*PI))*0.5f;
225                 float c=1.0-a;
226                 float d=1.0-b;
227                 int x2=x+1,y2=y+1;
228                 return
229                         (*this)(subseed,x,y,t)*(c*d)+
230                         (*this)(subseed,x2,y,t)*(a*d)+
231                         (*this)(subseed,x,y2,t)*(c*b)+
232                         (*this)(subseed,x2,y2,t)*(a*b);
233         }
234         else
235         {
236                 float a=xf-x;
237                 float b=yf-y;
238                 float c=tf-t;
239
240                 a=(1.0f-cos(a*PI))*0.5f;
241                 b=(1.0f-cos(b*PI))*0.5f;
242
243                 // We don't perform this on the time axis, otherwise we won't
244                 // get smooth motion
245                 //c=(1.0f-cos(c*PI))*0.5f;
246
247                 float d=1.0-a;
248                 float e=1.0-b;
249                 float f=1.0-c;
250
251                 int x2=x+1,y2=y+1,t2=t+1;
252
253                 return
254                         (*this)(subseed,x,y,t)*(d*e*f)+
255                         (*this)(subseed,x2,y,t)*(a*e*f)+
256                         (*this)(subseed,x,y2,t)*(d*b*f)+
257                         (*this)(subseed,x2,y2,t)*(a*b*f)+
258                         (*this)(subseed,x,y,t2)*(d*e*c)+
259                         (*this)(subseed,x2,y,t2)*(a*e*c)+
260                         (*this)(subseed,x,y2,t2)*(d*b*c)+
261                         (*this)(subseed,x2,y2,t2)*(a*b*c);
262         }
263         case SMOOTH_LINEAR:
264         if((float)t==tf)
265         {
266                 int x((int)floor(xf));
267                 int y((int)floor(yf));
268                 float a=xf-x;
269                 float b=yf-y;
270                 float c=1.0-a;
271                 float d=1.0-b;
272                 int x2=x+1,y2=y+1;
273                 return
274                         (*this)(subseed,x,y,t)*(c*d)+
275                         (*this)(subseed,x2,y,t)*(a*d)+
276                         (*this)(subseed,x,y2,t)*(c*b)+
277                         (*this)(subseed,x2,y2,t)*(a*b);
278         }
279         else
280         {
281
282                 float a=xf-x;
283                 float b=yf-y;
284                 float c=tf-t;
285
286                 float d=1.0-a;
287                 float e=1.0-b;
288                 float f=1.0-c;
289
290                 int x2=x+1,y2=y+1,t2=t+1;
291
292                 return
293                         (*this)(subseed,x,y,t)*(d*e*f)+
294                         (*this)(subseed,x2,y,t)*(a*e*f)+
295                         (*this)(subseed,x,y2,t)*(d*b*f)+
296                         (*this)(subseed,x2,y2,t)*(a*b*f)+
297                         (*this)(subseed,x,y,t2)*(d*e*c)+
298                         (*this)(subseed,x2,y,t2)*(a*e*c)+
299                         (*this)(subseed,x,y2,t2)*(d*b*c)+
300                         (*this)(subseed,x2,y2,t2)*(a*b*c);
301         }
302         default:
303         case SMOOTH_DEFAULT:
304                 return (*this)(subseed,x,y,t);
305         }
306 }