Fix bugs in previous commit that caused FTBFS in synfig and ETL FTBFS with older...
[synfig.git] / synfig-core / tags / synfig_0_61_07_rc2 / 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 **      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 <cmath>
35 #include <cstdlib>
36
37 #endif
38
39 /* === M A C R O S ========================================================= */
40
41 /* === G L O B A L S ======================================================= */
42
43 /* === P R O C E D U R E S ================================================= */
44
45 /* === M E T H O D S ======================================================= */
46
47 void
48 Random::set_seed(int x)
49 {
50         seed_=x;
51         srand(x);
52         int i;
53         for(i=0;i<POOL_SIZE;i++)
54                 pool_[i]=rand();
55
56         x_mask=rand()+rand()*RAND_MAX;
57         y_mask=rand()+rand()*RAND_MAX;
58         t_mask=rand()+rand()*RAND_MAX;
59 }
60
61 // this picks one of the POOL_SIZE (256) preset values out of the pool
62 // and scales it to be in the range (-1, 1).  is that what it was
63 // intended to do?  the distribution is pretty terrible, too, with
64 // some elements being picked a hundred times more often than others
65 float
66 Random::operator()(const int salt,const int x,const int y,const int t)const
67 {
68         const int salt_hash(pool_[salt&(POOL_SIZE-1)]);
69
70         int index(((x^x_mask)+(y^y_mask)*234672+(t^t_mask)*8439573)^salt_hash);
71
72         index+=index*(index/POOL_SIZE);
73
74         return (float(pool_[index&(POOL_SIZE-1)])/float(RAND_MAX))*2.0f-1.0f;
75 }
76
77 float
78 Random::operator()(SmoothType smooth,int subseed,float xf,float yf,float tf)const
79 {
80         int x((int)floor(xf));
81         int y((int)floor(yf));
82         int t((int)floor(tf));
83
84         switch(smooth)
85         {
86         case SMOOTH_CUBIC:      // cubic
87                 {
88                         #define f(j,i,k)        ((*this)(subseed,i,j,k))
89                         //Using catmull rom interpolation because it doesn't blur at all
90                         // ( http://www.gamedev.net/reference/articles/article1497.asp )
91                         //bezier curve with intermediate ctrl pts: 0.5/3(p(i+1) - p(i-1)) and similar
92                         float xfa [4], tfa[4];
93
94                         //precalculate indices (all clamped) and offset
95                         const int xa[] = {x-1,x,x+1,x+2};
96
97                         const int ya[] = {y-1,y,y+1,y+2};
98
99                         const int ta[] = {t-1,t,t+1,t+2};
100
101                         const float dx(xf-x);
102                         const float dy(yf-y);
103                         const float dt(tf-t);
104
105                         //figure polynomials for each point
106                         const float txf[] =
107                         {
108                                 0.5*dx*(dx*(dx*(-1) + 2) - 1),  //-t + 2t^2 -t^3
109                                 0.5*(dx*(dx*(3*dx - 5)) + 2),   //2 - 5t^2 + 3t^3
110                                 0.5*dx*(dx*(-3*dx + 4) + 1),    //t + 4t^2 - 3t^3
111                                 0.5*dx*dx*(dx-1)                                //-t^2 + t^3
112                         };
113
114                         const float tyf[] =
115                         {
116                                 0.5*dy*(dy*(dy*(-1) + 2) - 1),  //-t + 2t^2 -t^3
117                                 0.5*(dy*(dy*(3*dy - 5)) + 2),   //2 - 5t^2 + 3t^3
118                                 0.5*dy*(dy*(-3*dy + 4) + 1),    //t + 4t^2 - 3t^3
119                                 0.5*dy*dy*(dy-1)                                //-t^2 + t^3
120                         };
121
122                         const float ttf[] =
123                         {
124                                 0.5*dt*(dt*(dt*(-1) + 2) - 1),  //-t + 2t^2 -t^3
125                                 0.5*(dt*(dt*(3*dt - 5)) + 2),   //2 - 5t^2 + 3t^3
126                                 0.5*dt*(dt*(-3*dt + 4) + 1),    //t + 4t^2 - 3t^3
127                                 0.5*dt*dt*(dt-1)                                //-t^2 + t^3
128                         };
129
130                         //evaluate polynomial for each row
131                         for(int i = 0; i < 4; ++i)
132                         {
133                                 for(int j = 0; j < 4; ++j)
134                                 {
135                                         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];
136                                 }
137                                 xfa[i] = tfa[0]*txf[0] + tfa[1]*txf[1] + tfa[2]*txf[2] + tfa[3]*txf[3];
138                         }
139
140                         //return the cumulative column evaluation
141                         return xfa[0]*tyf[0] + xfa[1]*tyf[1] + xfa[2]*tyf[2] + xfa[3]*tyf[3];
142 #undef f
143                 }
144                 break;
145
146
147         case SMOOTH_FAST_SPLINE:        // Fast Spline (non-animated)
148                 {
149 #define P(x)    (((x)>0)?((x)*(x)*(x)):0.0f)
150 #define R(x)    ( P(x+2) - 4.0f*P(x+1) + 6.0f*P(x) - 4.0f*P(x-1) )*(1.0f/6.0f)
151 #define F(i,j)  ((*this)(subseed,i+x,j+y)*(R((i)-a)*R(b-(j))))
152 #define FT(i,j,k)       ((*this)(subseed,i+x,j+y,k+t)*(R((i)-a)*R(b-(j))*R((k)-c)))
153 #define Z(i,j) ret+=F(i,j)
154 #define ZT(i,j,k) ret+=FT(i,j,k)
155 #define X(i,j)  // placeholder... To make box more symmetric
156 #define XT(i,j,k)       // placeholder... To make box more symmetric
157
158                 float a(xf-x), b(yf-y);
159
160                 // Interpolate
161                 float ret(F(0,0));
162                 Z(-1,-1); Z(-1, 0); Z(-1, 1); Z(-1, 2);
163                 Z( 0,-1); X( 0, 0); Z( 0, 1); Z( 0, 2);
164                 Z( 1,-1); Z( 1, 0); Z( 1, 1); Z( 1, 2);
165                 Z( 2,-1); Z( 2, 0); Z( 2, 1); Z( 2, 2);
166
167                 return ret;
168         }
169
170         case SMOOTH_SPLINE:     // Spline (animated)
171                 {
172                         float a(xf-x), b(yf-y), c(tf-t);
173
174                         // Interpolate
175                         float ret(FT(0,0,0));
176                         ZT(-1,-1,-1); ZT(-1, 0,-1); ZT(-1, 1,-1); ZT(-1, 2,-1);
177                         ZT( 0,-1,-1); ZT( 0, 0,-1); ZT( 0, 1,-1); ZT( 0, 2,-1);
178                         ZT( 1,-1,-1); ZT( 1, 0,-1); ZT( 1, 1,-1); ZT( 1, 2,-1);
179                         ZT( 2,-1,-1); ZT( 2, 0,-1); ZT( 2, 1,-1); ZT( 2, 2,-1);
180
181                         ZT(-1,-1, 0); ZT(-1, 0, 0); ZT(-1, 1, 0); ZT(-1, 2, 0);
182                         ZT( 0,-1, 0); XT( 0, 0, 0); ZT( 0, 1, 0); ZT( 0, 2, 0);
183                         ZT( 1,-1, 0); ZT( 1, 0, 0); ZT( 1, 1, 0); ZT( 1, 2, 0);
184                         ZT( 2,-1, 0); ZT( 2, 0, 0); ZT( 2, 1, 0); ZT( 2, 2, 0);
185
186                         ZT(-1,-1, 1); ZT(-1, 0, 1); ZT(-1, 1, 1); ZT(-1, 2, 1);
187                         ZT( 0,-1, 1); ZT( 0, 0, 1); ZT( 0, 1, 1); ZT( 0, 2, 1);
188                         ZT( 1,-1, 1); ZT( 1, 0, 1); ZT( 1, 1, 1); ZT( 1, 2, 1);
189                         ZT( 2,-1, 1); ZT( 2, 0, 1); ZT( 2, 1, 1); ZT( 2, 2, 1);
190
191                         ZT(-1,-1, 2); ZT(-1, 0, 2); ZT(-1, 1, 2); ZT(-1, 2, 2);
192                         ZT( 0,-1, 2); ZT( 0, 0, 2); ZT( 0, 1, 2); ZT( 0, 2, 2);
193                         ZT( 1,-1, 2); ZT( 1, 0, 2); ZT( 1, 1, 2); ZT( 1, 2, 2);
194                         ZT( 2,-1, 2); ZT( 2, 0, 2); ZT( 2, 1, 2); ZT( 2, 2, 2);
195
196                         return ret;
197
198 /*
199
200                         float dx=xf-x;
201                         float dy=yf-y;
202                         float dt=tf-t;
203
204                         float ret=0;
205                         int i,j,h;
206                         for(h=-1;h<=2;h++)
207                                 for(i=-1;i<=2;i++)
208                                         for(j=-1;j<=2;j++)
209                                                 ret+=(*this)(subseed,i+x,j+y,h+t)*(R(i-dx)*R(j-dy)*R(h-dt));
210                         return ret;
211 */
212                 }
213                 break;
214 #undef X
215 #undef Z
216 #undef F
217 #undef P
218 #undef R
219
220         case SMOOTH_COSINE:
221         if((float)t==tf)
222         {
223                 int x((int)floor(xf));
224                 int y((int)floor(yf));
225                 float a=xf-x;
226                 float b=yf-y;
227                 a=(1.0f-cos(a*3.1415927))*0.5f;
228                 b=(1.0f-cos(b*3.1415927))*0.5f;
229                 float c=1.0-a;
230                 float d=1.0-b;
231                 int x2=x+1,y2=y+1;
232                 return
233                         (*this)(subseed,x,y,t)*(c*d)+
234                         (*this)(subseed,x2,y,t)*(a*d)+
235                         (*this)(subseed,x,y2,t)*(c*b)+
236                         (*this)(subseed,x2,y2,t)*(a*b);
237         }
238         else
239         {
240                 float a=xf-x;
241                 float b=yf-y;
242                 float c=tf-t;
243
244                 a=(1.0f-cos(a*3.1415927))*0.5f;
245                 b=(1.0f-cos(b*3.1415927))*0.5f;
246
247                 // We don't perform this on the time axis, otherwise we won't
248                 // get smooth motion
249                 //c=(1.0f-cos(c*3.1415927))*0.5f;
250
251                 float d=1.0-a;
252                 float e=1.0-b;
253                 float f=1.0-c;
254
255                 int x2=x+1,y2=y+1,t2=t+1;
256
257                 return
258                         (*this)(subseed,x,y,t)*(d*e*f)+
259                         (*this)(subseed,x2,y,t)*(a*e*f)+
260                         (*this)(subseed,x,y2,t)*(d*b*f)+
261                         (*this)(subseed,x2,y2,t)*(a*b*f)+
262                         (*this)(subseed,x,y,t2)*(d*e*c)+
263                         (*this)(subseed,x2,y,t2)*(a*e*c)+
264                         (*this)(subseed,x,y2,t2)*(d*b*c)+
265                         (*this)(subseed,x2,y2,t2)*(a*b*c);
266         }
267         case SMOOTH_LINEAR:
268         if((float)t==tf)
269         {
270                 int x((int)floor(xf));
271                 int y((int)floor(yf));
272                 float a=xf-x;
273                 float b=yf-y;
274                 float c=1.0-a;
275                 float d=1.0-b;
276                 int x2=x+1,y2=y+1;
277                 return
278                         (*this)(subseed,x,y,t)*(c*d)+
279                         (*this)(subseed,x2,y,t)*(a*d)+
280                         (*this)(subseed,x,y2,t)*(c*b)+
281                         (*this)(subseed,x2,y2,t)*(a*b);
282         }
283         else
284         {
285
286                 float a=xf-x;
287                 float b=yf-y;
288                 float c=tf-t;
289
290                 float d=1.0-a;
291                 float e=1.0-b;
292                 float f=1.0-c;
293
294                 int x2=x+1,y2=y+1,t2=t+1;
295
296                 return
297                         (*this)(subseed,x,y,t)*(d*e*f)+
298                         (*this)(subseed,x2,y,t)*(a*e*f)+
299                         (*this)(subseed,x,y2,t)*(d*b*f)+
300                         (*this)(subseed,x2,y2,t)*(a*b*f)+
301                         (*this)(subseed,x,y,t2)*(d*e*c)+
302                         (*this)(subseed,x2,y,t2)*(a*e*c)+
303                         (*this)(subseed,x,y2,t2)*(d*b*c)+
304                         (*this)(subseed,x2,y2,t2)*(a*b*c);
305         }
306         default:
307         case SMOOTH_DEFAULT:
308                 return (*this)(subseed,x,y,t);
309         }
310 }