svn merge -r 23207:23528 https://svn.blender.org/svnroot/bf-blender/trunk/blender
[blender.git] / source / blender / blenlib / intern / jitter.c
1 /**
2  * Jitter offset table
3  *
4  * $Id$
5  *
6  * ***** BEGIN GPL LICENSE BLOCK *****
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * as published by the Free Software Foundation; either version 2
11  * of the License, or (at your option) any later version. 
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software Foundation,
20  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
21  *
22  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
23  * All rights reserved.
24  *
25  * The Original Code is: all of this file.
26  *
27  * Contributor(s): none yet.
28  *
29  * ***** END GPL LICENSE BLOCK *****
30  */
31
32 #include <math.h>
33 #include <stdlib.h>
34 #include <string.h>
35 #include "MEM_guardedalloc.h"
36
37 #include "BLI_arithb.h"
38 #include "BLI_rand.h"
39 #include "BLI_jitter.h"
40
41
42 void BLI_jitterate1(float *jit1, float *jit2, int num, float rad1)
43 {
44         int i , j , k;
45         float vecx, vecy, dvecx, dvecy, x, y, len;
46
47         for (i = 2*num-2; i>=0 ; i-=2) {
48                 dvecx = dvecy = 0.0;
49                 x = jit1[i];
50                 y = jit1[i+1];
51                 for (j = 2*num-2; j>=0 ; j-=2) {
52                         if (i != j){
53                                 vecx = jit1[j] - x - 1.0;
54                                 vecy = jit1[j+1] - y - 1.0;
55                                 for (k = 3; k>0 ; k--){
56                                         if( fabs(vecx)<rad1 && fabs(vecy)<rad1) {
57                                                 len=  sqrt(vecx*vecx + vecy*vecy);
58                                                 if(len>0 && len<rad1) {
59                                                         len= len/rad1;
60                                                         dvecx += vecx/len;
61                                                         dvecy += vecy/len;
62                                                 }
63                                         }
64                                         vecx += 1.0;
65
66                                         if( fabs(vecx)<rad1 && fabs(vecy)<rad1) {
67                                                 len=  sqrt(vecx*vecx + vecy*vecy);
68                                                 if(len>0 && len<rad1) {
69                                                         len= len/rad1;
70                                                         dvecx += vecx/len;
71                                                         dvecy += vecy/len;
72                                                 }
73                                         }
74                                         vecx += 1.0;
75
76                                         if( fabs(vecx)<rad1 && fabs(vecy)<rad1) {
77                                                 len=  sqrt(vecx*vecx + vecy*vecy);
78                                                 if(len>0 && len<rad1) {
79                                                         len= len/rad1;
80                                                         dvecx += vecx/len;
81                                                         dvecy += vecy/len;
82                                                 }
83                                         }
84                                         vecx -= 2.0;
85                                         vecy += 1.0;
86                                 }
87                         }
88                 }
89
90                 x -= dvecx/18.0 ;
91                 y -= dvecy/18.0;
92                 x -= floor(x) ;
93                 y -= floor(y);
94                 jit2[i] = x;
95                 jit2[i+1] = y;
96         }
97         memcpy(jit1,jit2,2 * num * sizeof(float));
98 }
99
100 void BLI_jitterate2(float *jit1, float *jit2, int num, float rad2)
101 {
102         int i, j;
103         float vecx, vecy, dvecx, dvecy, x, y;
104
105         for (i=2*num -2; i>= 0 ; i-=2){
106                 dvecx = dvecy = 0.0;
107                 x = jit1[i];
108                 y = jit1[i+1];
109                 for (j =2*num -2; j>= 0 ; j-=2){
110                         if (i != j){
111                                 vecx = jit1[j] - x - 1.0;
112                                 vecy = jit1[j+1] - y - 1.0;
113
114                                 if( fabs(vecx)<rad2) dvecx+= vecx*rad2;
115                                 vecx += 1.0;
116                                 if( fabs(vecx)<rad2) dvecx+= vecx*rad2;
117                                 vecx += 1.0;
118                                 if( fabs(vecx)<rad2) dvecx+= vecx*rad2;
119
120                                 if( fabs(vecy)<rad2) dvecy+= vecy*rad2;
121                                 vecy += 1.0;
122                                 if( fabs(vecy)<rad2) dvecy+= vecy*rad2;
123                                 vecy += 1.0;
124                                 if( fabs(vecy)<rad2) dvecy+= vecy*rad2;
125
126                         }
127                 }
128
129                 x -= dvecx/2 ;
130                 y -= dvecy/2;
131                 x -= floor(x) ;
132                 y -= floor(y);
133                 jit2[i] = x;
134                 jit2[i+1] = y;
135         }
136         memcpy(jit1,jit2,2 * num * sizeof(float));
137 }
138
139
140 void BLI_initjit(float *jitarr, int num)
141 {
142         float *jit2, x, rad1, rad2, rad3;
143         int i;
144
145         if(num==0) return;
146
147         jit2= MEM_mallocN(12 + 2*sizeof(float)*num, "initjit");
148         rad1=  1.0/sqrt((float)num);
149         rad2= 1.0/((float)num);
150         rad3= sqrt((float)num)/((float)num);
151
152         BLI_srand(31415926 + num);
153         x= 0;
154         for(i=0; i<2*num; i+=2) {
155                 jitarr[i]= x+ rad1*(0.5-BLI_drand());
156                 jitarr[i+1]= ((float)i/2)/num +rad1*(0.5-BLI_drand());
157                 x+= rad3;
158                 x -= floor(x);
159         }
160
161         for (i=0 ; i<24 ; i++) {
162                 BLI_jitterate1(jitarr, jit2, num, rad1);
163                 BLI_jitterate1(jitarr, jit2, num, rad1);
164                 BLI_jitterate2(jitarr, jit2, num, rad2);
165         }
166
167         MEM_freeN(jit2);
168         
169         /* finally, move jittertab to be centered around (0,0) */
170         for(i=0; i<2*num; i+=2) {
171                 jitarr[i] -= 0.5;
172                 jitarr[i+1] -= 0.5;
173         }
174         
175 }
176
177
178 /* eof */