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