Remove .gitignore do nothing is ignored.
[synfig.git] / synfig-core / trunk / src / modules / mod_particle / random.cpp
index c31fe44..ebdff92 100644 (file)
@@ -1,20 +1,22 @@
 /* === S Y N F I G ========================================================= */
-/*!    \file noise.cpp
+/*!    \file mod_particle/random.cpp
 **     \brief blehh
 **
-**     $Id: random.cpp,v 1.1.1.1 2005/01/04 01:23:14 darco Exp $
+**     $Id$
 **
 **     \legal
-**     Copyright (c) 2002 Robert B. Quattlebaum Jr.
+**     Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
+**     Copyright (c) 2007 Chris Moore
 **
-**     This software and associated documentation
-**     are CONFIDENTIAL and PROPRIETARY property of
-**     the above-mentioned copyright holder.
+**     This package is free software; you can redistribute it and/or
+**     modify it under the terms of the GNU General Public License as
+**     published by the Free Software Foundation; either version 2 of
+**     the License, or (at your option) any later version.
 **
-**     You may not copy, print, publish, or in any
-**     other way distribute this software without
-**     a prior written agreement with
-**     the copyright holder.
+**     This package is distributed in the hope that it will be useful,
+**     but WITHOUT ANY WARRANTY; without even the implied warranty of
+**     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+**     General Public License for more details.
 **     \endlegal
 */
 /* ========================================================================= */
@@ -36,6 +38,8 @@
 
 /* === M A C R O S ========================================================= */
 
+#define PI     (3.1415927)
+
 /* === G L O B A L S ======================================================= */
 
 /* === P R O C E D U R E S ================================================= */
@@ -55,21 +59,25 @@ Random::set_seed(int x)
        y_mask=rand()+rand()*RAND_MAX;
        t_mask=rand()+rand()*RAND_MAX;
 }
-       
+
+// this picks one of the POOL_SIZE (256) preset values out of the pool
+// and scales it to be in the range (-1, 1).  is that what it was
+// intended to do?  the distribution is pretty terrible, too, with
+// some elements being picked a hundred times more often than others
 float
 Random::operator()(const int salt,const int x,const int y,const int t)const
 {
        const int salt_hash(pool_[salt&(POOL_SIZE-1)]);
-               
+
        int index(((x^x_mask)+(y^y_mask)*234672+(t^t_mask)*8439573)^salt_hash);
-       
+
        index+=index*(index/POOL_SIZE);
-                       
+
        return (float(pool_[index&(POOL_SIZE-1)])/float(RAND_MAX))*2.0f-1.0f;
 }
 
 float
-Random::operator()(int smooth,int subseed,float xf,float yf,float tf)const
+Random::operator()(SmoothType smooth,int subseed,float xf,float yf,float tf)const
 {
        int x((int)floor(xf));
        int y((int)floor(yf));
@@ -77,34 +85,35 @@ Random::operator()(int smooth,int subseed,float xf,float yf,float tf)const
 
        switch(smooth)
        {
-       case 4: // cubic
+       case SMOOTH_CUBIC:      // cubic
                {
                        #define f(j,i,k)        ((*this)(subseed,i,j,k))
                        //Using catmull rom interpolation because it doesn't blur at all
+                       // ( http://www.gamedev.net/reference/articles/article1497.asp )
                        //bezier curve with intermediate ctrl pts: 0.5/3(p(i+1) - p(i-1)) and similar
                        float xfa [4], tfa[4];
-                       
+
                        //precalculate indices (all clamped) and offset
                        const int xa[] = {x-1,x,x+1,x+2};
-                       
+
                        const int ya[] = {y-1,y,y+1,y+2};
 
                        const int ta[] = {t-1,t,t+1,t+2};
-                       
+
                        const float dx(xf-x);
                        const float dy(yf-y);
                        const float dt(tf-t);
-                       
+
                        //figure polynomials for each point
-                       const float txf[] = 
+                       const float txf[] =
                        {
                                0.5*dx*(dx*(dx*(-1) + 2) - 1),  //-t + 2t^2 -t^3
                                0.5*(dx*(dx*(3*dx - 5)) + 2),   //2 - 5t^2 + 3t^3
                                0.5*dx*(dx*(-3*dx + 4) + 1),    //t + 4t^2 - 3t^3
                                0.5*dx*dx*(dx-1)                                //-t^2 + t^3
                        };
-                       
-                       const float tyf[] = 
+
+                       const float tyf[] =
                        {
                                0.5*dy*(dy*(dy*(-1) + 2) - 1),  //-t + 2t^2 -t^3
                                0.5*(dy*(dy*(3*dy - 5)) + 2),   //2 - 5t^2 + 3t^3
@@ -112,15 +121,15 @@ Random::operator()(int smooth,int subseed,float xf,float yf,float tf)const
                                0.5*dy*dy*(dy-1)                                //-t^2 + t^3
                        };
 
-                       const float ttf[] = 
+                       const float ttf[] =
                        {
                                0.5*dt*(dt*(dt*(-1) + 2) - 1),  //-t + 2t^2 -t^3
                                0.5*(dt*(dt*(3*dt - 5)) + 2),   //2 - 5t^2 + 3t^3
                                0.5*dt*(dt*(-3*dt + 4) + 1),    //t + 4t^2 - 3t^3
                                0.5*dt*dt*(dt-1)                                //-t^2 + t^3
                        };
-                       
-                       //evaluate polynomial for each row              
+
+                       //evaluate polynomial for each row
                        for(int i = 0; i < 4; ++i)
                        {
                                for(int j = 0; j < 4; ++j)
@@ -129,7 +138,7 @@ Random::operator()(int smooth,int subseed,float xf,float yf,float tf)const
                                }
                                xfa[i] = tfa[0]*txf[0] + tfa[1]*txf[1] + tfa[2]*txf[2] + tfa[3]*txf[3];
                        }
-                       
+
                        //return the cumulative column evaluation
                        return xfa[0]*tyf[0] + xfa[1]*tyf[1] + xfa[2]*tyf[2] + xfa[3]*tyf[3];
 #undef f
@@ -137,7 +146,7 @@ Random::operator()(int smooth,int subseed,float xf,float yf,float tf)const
                break;
 
 
-       case 5: // Fast Spline (non-animated)
+       case SMOOTH_FAST_SPLINE:        // Fast Spline (non-animated)
                {
 #define P(x)   (((x)>0)?((x)*(x)*(x)):0.0f)
 #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,11 +154,11 @@ Random::operator()(int smooth,int subseed,float xf,float yf,float tf)const
 #define FT(i,j,k)      ((*this)(subseed,i+x,j+y,k+t)*(R((i)-a)*R(b-(j))*R((k)-c)))
 #define Z(i,j) ret+=F(i,j)
 #define ZT(i,j,k) ret+=FT(i,j,k)
-#define X(i,j) // placeholder... To make box more symetric
-#define XT(i,j,k)      // placeholder... To make box more symetric
-       
+#define X(i,j) // placeholder... To make box more symmetric
+#define XT(i,j,k)      // placeholder... To make box more symmetric
+
                float a(xf-x), b(yf-y);
-               
+
                // Interpolate
                float ret(F(0,0));
                Z(-1,-1); Z(-1, 0); Z(-1, 1); Z(-1, 2);
@@ -159,11 +168,11 @@ Random::operator()(int smooth,int subseed,float xf,float yf,float tf)const
 
                return ret;
        }
-               
-       case 3: // Spline (animated)
+
+       case SMOOTH_SPLINE:     // Spline (animated)
                {
                        float a(xf-x), b(yf-y), c(tf-t);
-                       
+
                        // Interpolate
                        float ret(FT(0,0,0));
                        ZT(-1,-1,-1); ZT(-1, 0,-1); ZT(-1, 1,-1); ZT(-1, 2,-1);
@@ -185,7 +194,7 @@ Random::operator()(int smooth,int subseed,float xf,float yf,float tf)const
                        ZT( 0,-1, 2); ZT( 0, 0, 2); ZT( 0, 1, 2); ZT( 0, 2, 2);
                        ZT( 1,-1, 2); ZT( 1, 0, 2); ZT( 1, 1, 2); ZT( 1, 2, 2);
                        ZT( 2,-1, 2); ZT( 2, 0, 2); ZT( 2, 1, 2); ZT( 2, 2, 2);
-                       
+
                        return ret;
 
 /*
@@ -210,15 +219,15 @@ Random::operator()(int smooth,int subseed,float xf,float yf,float tf)const
 #undef P
 #undef R
 
-       case 2: // Cosine
+       case SMOOTH_COSINE:
        if((float)t==tf)
        {
                int x((int)floor(xf));
                int y((int)floor(yf));
                float a=xf-x;
                float b=yf-y;
-               a=(1.0f-cos(a*3.1415927))*0.5f;
-               b=(1.0f-cos(b*3.1415927))*0.5f;
+               a=(1.0f-cos(a*PI))*0.5f;
+               b=(1.0f-cos(b*PI))*0.5f;
                float c=1.0-a;
                float d=1.0-b;
                int x2=x+1,y2=y+1;
@@ -234,19 +243,19 @@ Random::operator()(int smooth,int subseed,float xf,float yf,float tf)const
                float b=yf-y;
                float c=tf-t;
 
-               a=(1.0f-cos(a*3.1415927))*0.5f;
-               b=(1.0f-cos(b*3.1415927))*0.5f;
-               
+               a=(1.0f-cos(a*PI))*0.5f;
+               b=(1.0f-cos(b*PI))*0.5f;
+
                // We don't perform this on the time axis, otherwise we won't
                // get smooth motion
-               //c=(1.0f-cos(c*3.1415927))*0.5f;
-               
+               //c=(1.0f-cos(c*PI))*0.5f;
+
                float d=1.0-a;
                float e=1.0-b;
                float f=1.0-c;
-               
+
                int x2=x+1,y2=y+1,t2=t+1;
-               
+
                return
                        (*this)(subseed,x,y,t)*(d*e*f)+
                        (*this)(subseed,x2,y,t)*(a*e*f)+
@@ -257,7 +266,7 @@ Random::operator()(int smooth,int subseed,float xf,float yf,float tf)const
                        (*this)(subseed,x,y2,t2)*(d*b*c)+
                        (*this)(subseed,x2,y2,t2)*(a*b*c);
        }
-       case 1: // Linear
+       case SMOOTH_LINEAR:
        if((float)t==tf)
        {
                int x((int)floor(xf));
@@ -275,17 +284,17 @@ Random::operator()(int smooth,int subseed,float xf,float yf,float tf)const
        }
        else
        {
-               
+
                float a=xf-x;
                float b=yf-y;
                float c=tf-t;
-               
+
                float d=1.0-a;
                float e=1.0-b;
                float f=1.0-c;
-               
+
                int x2=x+1,y2=y+1,t2=t+1;
-               
+
                return
                        (*this)(subseed,x,y,t)*(d*e*f)+
                        (*this)(subseed,x2,y,t)*(a*e*f)+
@@ -297,7 +306,7 @@ Random::operator()(int smooth,int subseed,float xf,float yf,float tf)const
                        (*this)(subseed,x2,y2,t2)*(a*b*c);
        }
        default:
-       case 0:
+       case SMOOTH_DEFAULT:
                return (*this)(subseed,x,y,t);
        }
 }