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