Blender V2.61 - r43446

jitter.c

Go to the documentation of this file.
00001 /*
00002  * Jitter offset table
00003  *
00004  *
00005  * ***** BEGIN GPL LICENSE BLOCK *****
00006  *
00007  * This program is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU General Public License
00009  * as published by the Free Software Foundation; either version 2
00010  * of the License, or (at your option) any later version. 
00011  *
00012  * This program is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  * GNU General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU General Public License
00018  * along with this program; if not, write to the Free Software Foundation,
00019  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00020  *
00021  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
00022  * All rights reserved.
00023  *
00024  * The Original Code is: all of this file.
00025  *
00026  * Contributor(s): none yet.
00027  *
00028  * ***** END GPL LICENSE BLOCK *****
00029  */
00030 
00036 #include <math.h>
00037 #include <string.h>
00038 #include "MEM_guardedalloc.h"
00039 
00040 #include "BLI_rand.h"
00041 #include "BLI_jitter.h"
00042 
00043 
00044 void BLI_jitterate1(float *jit1, float *jit2, int num, float rad1)
00045 {
00046     int i , j , k;
00047     float vecx, vecy, dvecx, dvecy, x, y, len;
00048 
00049     for (i = 2*num-2; i>=0 ; i-=2) {
00050         dvecx = dvecy = 0.0;
00051         x = jit1[i];
00052         y = jit1[i+1];
00053         for (j = 2*num-2; j>=0 ; j-=2) {
00054             if (i != j){
00055                 vecx = jit1[j] - x - 1.0f;
00056                 vecy = jit1[j+1] - y - 1.0f;
00057                 for (k = 3; k>0 ; k--){
00058                     if( fabsf(vecx)<rad1 && fabsf(vecy)<rad1) {
00059                         len=  sqrt(vecx*vecx + vecy*vecy);
00060                         if(len>0 && len<rad1) {
00061                             len= len/rad1;
00062                             dvecx += vecx/len;
00063                             dvecy += vecy/len;
00064                         }
00065                     }
00066                     vecx += 1.0f;
00067 
00068                     if( fabsf(vecx)<rad1 && fabsf(vecy)<rad1) {
00069                         len=  sqrt(vecx*vecx + vecy*vecy);
00070                         if(len>0 && len<rad1) {
00071                             len= len/rad1;
00072                             dvecx += vecx/len;
00073                             dvecy += vecy/len;
00074                         }
00075                     }
00076                     vecx += 1.0f;
00077 
00078                     if( fabsf(vecx)<rad1 && fabsf(vecy)<rad1) {
00079                         len=  sqrt(vecx*vecx + vecy*vecy);
00080                         if(len>0 && len<rad1) {
00081                             len= len/rad1;
00082                             dvecx += vecx/len;
00083                             dvecy += vecy/len;
00084                         }
00085                     }
00086                     vecx -= 2.0f;
00087                     vecy += 1.0f;
00088                 }
00089             }
00090         }
00091 
00092         x -= dvecx/18.0f;
00093         y -= dvecy/18.0f;
00094         x -= floorf(x) ;
00095         y -= floorf(y);
00096         jit2[i] = x;
00097         jit2[i+1] = y;
00098     }
00099     memcpy(jit1,jit2,2 * num * sizeof(float));
00100 }
00101 
00102 void BLI_jitterate2(float *jit1, float *jit2, int num, float rad2)
00103 {
00104     int i, j;
00105     float vecx, vecy, dvecx, dvecy, x, y;
00106 
00107     for (i=2*num -2; i>= 0 ; i-=2){
00108         dvecx = dvecy = 0.0;
00109         x = jit1[i];
00110         y = jit1[i+1];
00111         for (j =2*num -2; j>= 0 ; j-=2){
00112             if (i != j){
00113                 vecx = jit1[j] - x - 1.0f;
00114                 vecy = jit1[j+1] - y - 1.0f;
00115 
00116                 if( fabsf(vecx)<rad2) dvecx+= vecx*rad2;
00117                 vecx += 1.0f;
00118                 if( fabsf(vecx)<rad2) dvecx+= vecx*rad2;
00119                 vecx += 1.0f;
00120                 if( fabsf(vecx)<rad2) dvecx+= vecx*rad2;
00121 
00122                 if( fabsf(vecy)<rad2) dvecy+= vecy*rad2;
00123                 vecy += 1.0f;
00124                 if( fabsf(vecy)<rad2) dvecy+= vecy*rad2;
00125                 vecy += 1.0f;
00126                 if( fabsf(vecy)<rad2) dvecy+= vecy*rad2;
00127 
00128             }
00129         }
00130 
00131         x -= dvecx/2.0f;
00132         y -= dvecy/2.0f;
00133         x -= floorf(x) ;
00134         y -= floorf(y);
00135         jit2[i] = x;
00136         jit2[i+1] = y;
00137     }
00138     memcpy(jit1,jit2,2 * num * sizeof(float));
00139 }
00140 
00141 
00142 void BLI_initjit(float *jitarr, int num)
00143 {
00144     float *jit2, x, rad1, rad2, rad3;
00145     int i;
00146 
00147     if(num==0) return;
00148 
00149     jit2= MEM_mallocN(12 + 2*sizeof(float)*num, "initjit");
00150     rad1= 1.0f/sqrtf((float)num);
00151     rad2= 1.0f/((float)num);
00152     rad3= sqrtf((float)num)/((float)num);
00153 
00154     BLI_srand(31415926 + num);
00155     x= 0;
00156     for(i=0; i<2*num; i+=2) {
00157         jitarr[i]= x+ rad1*(float)(0.5-BLI_drand());
00158         jitarr[i+1]= ((float)i/2)/num +rad1*(float)(0.5-BLI_drand());
00159         x+= rad3;
00160         x -= floorf(x);
00161     }
00162 
00163     for (i=0 ; i<24 ; i++) {
00164         BLI_jitterate1(jitarr, jit2, num, rad1);
00165         BLI_jitterate1(jitarr, jit2, num, rad1);
00166         BLI_jitterate2(jitarr, jit2, num, rad2);
00167     }
00168 
00169     MEM_freeN(jit2);
00170     
00171     /* finally, move jittertab to be centered around (0,0) */
00172     for(i=0; i<2*num; i+=2) {
00173         jitarr[i] -= 0.5f;
00174         jitarr[i+1] -= 0.5f;
00175     }
00176     
00177 }
00178 
00179 
00180 /* eof */