Initial revision
[blender.git] / source / blender / render / intern / source / jitter.c
1 /**
2  * Jitter offset table
3  *
4  * $Id$
5  *
6  * ***** BEGIN GPL/BL DUAL 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. The Blender
12  * Foundation also sells licenses for use in proprietary software under
13  * the Blender License.  See http://www.blender.org/BL/ for information
14  * about this.
15  *
16  * This program 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
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software Foundation,
23  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
24  *
25  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
26  * All rights reserved.
27  *
28  * The Original Code is: all of this file.
29  *
30  * Contributor(s): none yet.
31  *
32  * ***** END GPL/BL DUAL LICENSE BLOCK *****
33  */
34
35 #include <math.h>
36 #include <stdlib.h>
37 #include <string.h>
38 #include "MEM_guardedalloc.h"
39
40 #include "BLI_arithb.h"
41 #include "BLI_rand.h"
42 #include "render.h"
43 #include "render_intern.h"
44 #include "jitter.h"
45
46 float jit[64][2];
47
48 void init_render_jit(int nr);
49
50
51 void RE_jitterate1(float *jit1, float *jit2, int num, float rad1)
52 {
53         int i , j , k;
54         float vecx, vecy, dvecx, dvecy, x, y, len;
55
56         for (i = 2*num-2; i>=0 ; i-=2) {
57                 dvecx = dvecy = 0.0;
58                 x = jit1[i];
59                 y = jit1[i+1];
60                 for (j = 2*num-2; j>=0 ; j-=2) {
61                         if (i != j){
62                                 vecx = jit1[j] - x - 1.0;
63                                 vecy = jit1[j+1] - y - 1.0;
64                                 for (k = 3; k>0 ; k--){
65                                         if( fabs(vecx)<rad1 && fabs(vecy)<rad1) {
66                                                 len=  sqrt(vecx*vecx + vecy*vecy);
67                                                 if(len>0 && len<rad1) {
68                                                         len= len/rad1;
69                                                         dvecx += vecx/len;
70                                                         dvecy += vecy/len;
71                                                 }
72                                         }
73                                         vecx += 1.0;
74
75                                         if( fabs(vecx)<rad1 && fabs(vecy)<rad1) {
76                                                 len=  sqrt(vecx*vecx + vecy*vecy);
77                                                 if(len>0 && len<rad1) {
78                                                         len= len/rad1;
79                                                         dvecx += vecx/len;
80                                                         dvecy += vecy/len;
81                                                 }
82                                         }
83                                         vecx += 1.0;
84
85                                         if( fabs(vecx)<rad1 && fabs(vecy)<rad1) {
86                                                 len=  sqrt(vecx*vecx + vecy*vecy);
87                                                 if(len>0 && len<rad1) {
88                                                         len= len/rad1;
89                                                         dvecx += vecx/len;
90                                                         dvecy += vecy/len;
91                                                 }
92                                         }
93                                         vecx -= 2.0;
94                                         vecy += 1.0;
95                                 }
96                         }
97                 }
98
99                 x -= dvecx/18.0 ;
100                 y -= dvecy/18.0;
101                 x -= floor(x) ;
102                 y -= floor(y);
103                 jit2[i] = x;
104                 jit2[i+1] = y;
105         }
106         memcpy(jit1,jit2,2 * num * sizeof(float));
107 }
108
109 void RE_jitterate2(float *jit1, float *jit2, int num, float rad2)
110 {
111         int i, j;
112         float vecx, vecy, dvecx, dvecy, x, y;
113
114         for (i=2*num -2; i>= 0 ; i-=2){
115                 dvecx = dvecy = 0.0;
116                 x = jit1[i];
117                 y = jit1[i+1];
118                 for (j =2*num -2; j>= 0 ; j-=2){
119                         if (i != j){
120                                 vecx = jit1[j] - x - 1.0;
121                                 vecy = jit1[j+1] - y - 1.0;
122
123                                 if( fabs(vecx)<rad2) dvecx+= vecx*rad2;
124                                 vecx += 1.0;
125                                 if( fabs(vecx)<rad2) dvecx+= vecx*rad2;
126                                 vecx += 1.0;
127                                 if( fabs(vecx)<rad2) dvecx+= vecx*rad2;
128
129                                 if( fabs(vecy)<rad2) dvecy+= vecy*rad2;
130                                 vecy += 1.0;
131                                 if( fabs(vecy)<rad2) dvecy+= vecy*rad2;
132                                 vecy += 1.0;
133                                 if( fabs(vecy)<rad2) dvecy+= vecy*rad2;
134
135                         }
136                 }
137
138                 x -= dvecx/2 ;
139                 y -= dvecy/2;
140                 x -= floor(x) ;
141                 y -= floor(y);
142                 jit2[i] = x;
143                 jit2[i+1] = y;
144         }
145         memcpy(jit1,jit2,2 * num * sizeof(float));
146 }
147
148
149 void initjit(float *jit, int num)
150 {
151         float *jit2, x, rad1, rad2, rad3;
152         int i;
153
154         if(num==0) return;
155
156         jit2= MEM_mallocN(12 + 2*sizeof(float)*num, "initjit");
157         rad1=  1.0/sqrt((float)num);
158         rad2= 1.0/((float)num);
159         rad3= sqrt((float)num)/((float)num);
160
161         BLI_srand(31415926 + num);
162         x= 0;
163         for(i=0; i<2*num; i+=2) {
164                 jit[i]= x+ rad1*(0.5-BLI_drand());
165                 jit[i+1]= ((float)i/2)/num +rad1*(0.5-BLI_drand());
166                 x+= rad3;
167                 x -= floor(x);
168         }
169
170         for (i=0 ; i<24 ; i++) {
171                 RE_jitterate1(jit, jit2, num, rad1);
172                 RE_jitterate1(jit, jit2, num, rad1);
173                 RE_jitterate2(jit, jit2, num, rad2);
174         }
175
176         MEM_freeN(jit2);
177 }
178
179 void init_render_jit(int nr)
180 {
181         static int lastjit= 0;
182
183         if(lastjit==nr) return;
184
185         memset(jit, 0, 64*2*4);
186         initjit(jit[0], nr);
187
188         lastjit= nr;
189
190 }
191
192 /* eof */