1 /* === S Y N F I G ========================================================= */
2 /*! \file mod_particle/random.cpp
8 ** Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
9 ** Copyright (c) 2007 Chris Moore
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.
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.
22 /* ========================================================================= */
24 /* === H E A D E R S ======================================================= */
39 /* === M A C R O S ========================================================= */
41 /* === G L O B A L S ======================================================= */
43 /* === P R O C E D U R E S ================================================= */
45 /* === M E T H O D S ======================================================= */
48 Random::set_seed(int x)
53 for(i=0;i<POOL_SIZE;i++)
56 x_mask=rand()+rand()*RAND_MAX;
57 y_mask=rand()+rand()*RAND_MAX;
58 t_mask=rand()+rand()*RAND_MAX;
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
66 Random::operator()(const int salt,const int x,const int y,const int t)const
68 const int salt_hash(pool_[salt&(POOL_SIZE-1)]);
70 int index(((x^x_mask)+(y^y_mask)*234672+(t^t_mask)*8439573)^salt_hash);
72 index+=index*(index/POOL_SIZE);
74 return (float(pool_[index&(POOL_SIZE-1)])/float(RAND_MAX))*2.0f-1.0f;
78 Random::operator()(SmoothType smooth,int subseed,float xf,float yf,float tf)const
80 int x((int)floor(xf));
81 int y((int)floor(yf));
82 int t((int)floor(tf));
86 case SMOOTH_CUBIC: // cubic
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];
94 //precalculate indices (all clamped) and offset
95 const int xa[] = {x-1,x,x+1,x+2};
97 const int ya[] = {y-1,y,y+1,y+2};
99 const int ta[] = {t-1,t,t+1,t+2};
101 const float dx(xf-x);
102 const float dy(yf-y);
103 const float dt(tf-t);
105 //figure polynomials for each point
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
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
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
130 //evaluate polynomial for each row
131 for(int i = 0; i < 4; ++i)
133 for(int j = 0; j < 4; ++j)
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];
137 xfa[i] = tfa[0]*txf[0] + tfa[1]*txf[1] + tfa[2]*txf[2] + tfa[3]*txf[3];
140 //return the cumulative column evaluation
141 return xfa[0]*tyf[0] + xfa[1]*tyf[1] + xfa[2]*tyf[2] + xfa[3]*tyf[3];
147 case SMOOTH_FAST_SPLINE: // Fast Spline (non-animated)
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
158 float a(xf-x), b(yf-y);
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);
170 case SMOOTH_SPLINE: // Spline (animated)
172 float a(xf-x), b(yf-y), c(tf-t);
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);
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);
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);
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);
209 ret+=(*this)(subseed,i+x,j+y,h+t)*(R(i-dx)*R(j-dy)*R(h-dt));
223 int x((int)floor(xf));
224 int y((int)floor(yf));
227 a=(1.0f-cos(a*3.1415927))*0.5f;
228 b=(1.0f-cos(b*3.1415927))*0.5f;
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);
244 a=(1.0f-cos(a*3.1415927))*0.5f;
245 b=(1.0f-cos(b*3.1415927))*0.5f;
247 // We don't perform this on the time axis, otherwise we won't
249 //c=(1.0f-cos(c*3.1415927))*0.5f;
255 int x2=x+1,y2=y+1,t2=t+1;
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);
270 int x((int)floor(xf));
271 int y((int)floor(yf));
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);
294 int x2=x+1,y2=y+1,t2=t+1;
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);
308 return (*this)(subseed,x,y,t);