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