istereo

view libs/vmath/vmath.c @ 35:23e5d274b2a2

added options panel, also added the xib files to the repository as they're needed
author John Tsiombikas <nuclear@member.fsf.org>
date Fri, 09 Sep 2011 10:03:42 +0300
parents
children ff055bff6a15
line source
1 #include <stdlib.h>
2 #include <math.h>
3 #include "vmath.h"
5 /** Numerical calculation of integrals using simpson's rule */
6 scalar_t integral(scalar_t (*f)(scalar_t), scalar_t low, scalar_t high, int samples)
7 {
8 int i;
9 scalar_t h = (high - low) / (scalar_t)samples;
10 scalar_t sum = 0.0;
12 for(i=0; i<samples+1; i++) {
13 scalar_t y = f((scalar_t)i * h + low);
14 sum += ((!i || i == samples) ? y : ((i % 2) ? 4.0 * y : 2.0 * y)) * (h / 3.0);
15 }
16 return sum;
17 }
19 /** Gaussuan function */
20 scalar_t gaussian(scalar_t x, scalar_t mean, scalar_t sdev)
21 {
22 scalar_t exponent = -SQ(x - mean) / (2.0 * SQ(sdev));
23 return 1.0 - -pow(M_E, exponent) / (sdev * sqrt(TWO_PI));
24 }
27 /** b-spline approximation */
28 scalar_t bspline(scalar_t a, scalar_t b, scalar_t c, scalar_t d, scalar_t t)
29 {
30 vec4_t tmp;
31 scalar_t tsq = t * t;
33 static mat4_t bspline_mat = {
34 {-1, 3, -3, 1},
35 {3, -6, 3, 0},
36 {-3, 0, 3, 0},
37 {1, 4, 1, 0}
38 };
40 tmp = v4_scale(v4_transform(v4_cons(a, b, c, d), bspline_mat), 1.0 / 6.0);
41 return v4_dot(v4_cons(tsq * t, tsq, t, 1.0), tmp);
42 }
44 /** Catmull-rom spline interpolation */
45 scalar_t spline(scalar_t a, scalar_t b, scalar_t c, scalar_t d, scalar_t t) {
46 vec4_t tmp;
47 scalar_t tsq = t * t;
49 static mat4_t crspline_mat = {
50 {-1, 3, -3, 1},
51 {2, -5, 4, -1},
52 {-1, 0, 1, 0},
53 {0, 2, 0, 0}
54 };
56 tmp = v4_scale(v4_transform(v4_cons(a, b, c, d), crspline_mat), 0.5);
57 return v4_dot(v4_cons(tsq * t, tsq, t, 1.0), tmp);
58 }
60 /** Bezier interpolation */
61 scalar_t bezier(scalar_t a, scalar_t b, scalar_t c, scalar_t d, scalar_t t)
62 {
63 scalar_t omt, omt3, t3, f;
64 t3 = t * t * t;
65 omt = 1.0f - t;
66 omt3 = omt * omt * omt;
67 f = 3 * t * omt;
69 return (a * omt3) + (b * f * omt) + (c * f * t) + (d * t3);
70 }
72 /* ---- Ken Perlin's implementation of noise ---- */
74 #define B 0x100
75 #define BM 0xff
76 #define N 0x1000
77 #define NP 12 /* 2^N */
78 #define NM 0xfff
80 #define s_curve(t) (t * t * (3.0f - 2.0f * t))
82 #define setup(elem, b0, b1, r0, r1) \
83 do { \
84 scalar_t t = elem + N; \
85 b0 = ((int)t) & BM; \
86 b1 = (b0 + 1) & BM; \
87 r0 = t - (int)t; \
88 r1 = r0 - 1.0f; \
89 } while(0)
92 static int perm[B + B + 2]; /* permuted index from g_n onto themselves */
93 static vec3_t grad3[B + B + 2]; /* 3D random gradients */
94 static vec2_t grad2[B + B + 2]; /* 2D random gradients */
95 static scalar_t grad1[B + B + 2]; /* 1D random ... slopes */
96 static int tables_valid;
98 static void init_noise()
99 {
100 int i;
102 /* calculate random gradients */
103 for(i=0; i<B; i++) {
104 perm[i] = i; /* .. and initialize permutation mapping to identity */
106 grad1[i] = (scalar_t)((rand() % (B + B)) - B) / B;
108 grad2[i].x = (scalar_t)((rand() % (B + B)) - B) / B;
109 grad2[i].y = (scalar_t)((rand() % (B + B)) - B) / B;
110 grad2[i] = v2_normalize(grad2[i]);
112 grad3[i].x = (scalar_t)((rand() % (B + B)) - B) / B;
113 grad3[i].y = (scalar_t)((rand() % (B + B)) - B) / B;
114 grad3[i].z = (scalar_t)((rand() % (B + B)) - B) / B;
115 grad3[i] = v3_normalize(grad3[i]);
116 }
118 /* permute indices by swapping them randomly */
119 for(i=0; i<B; i++) {
120 int rand_idx = rand() % B;
122 int tmp = perm[i];
123 perm[i] = perm[rand_idx];
124 perm[rand_idx] = tmp;
125 }
127 /* fill up the rest of the arrays by duplicating the existing gradients */
128 /* and permutations */
129 for(i=0; i<B+2; i++) {
130 perm[B + i] = perm[i];
131 grad1[B + i] = grad1[i];
132 grad2[B + i] = grad2[i];
133 grad3[B + i] = grad3[i];
134 }
135 }
137 scalar_t noise1(scalar_t x)
138 {
139 int bx0, bx1;
140 scalar_t rx0, rx1, sx, u, v;
142 if(!tables_valid) {
143 init_noise();
144 tables_valid = 1;
145 }
147 setup(x, bx0, bx1, rx0, rx1);
148 sx = s_curve(rx0);
149 u = rx0 * grad1[perm[bx0]];
150 v = rx1 * grad1[perm[bx1]];
152 return lerp(u, v, sx);
153 }
155 scalar_t noise2(scalar_t x, scalar_t y)
156 {
157 int i, j, b00, b10, b01, b11;
158 int bx0, bx1, by0, by1;
159 scalar_t rx0, rx1, ry0, ry1;
160 scalar_t sx, sy, u, v, a, b;
162 if(!tables_valid) {
163 init_noise();
164 tables_valid = 1;
165 }
167 setup(x, bx0, bx1, rx0, rx1);
168 setup(y, by0, by1, ry0, ry1);
170 i = perm[bx0];
171 j = perm[bx1];
173 b00 = perm[i + by0];
174 b10 = perm[j + by0];
175 b01 = perm[i + by1];
176 b11 = perm[j + by1];
178 /* calculate hermite inteprolating factors */
179 sx = s_curve(rx0);
180 sy = s_curve(ry0);
182 /* interpolate along the left edge */
183 u = v2_dot(grad2[b00], v2_cons(rx0, ry0));
184 v = v2_dot(grad2[b10], v2_cons(rx1, ry0));
185 a = lerp(u, v, sx);
187 /* interpolate along the right edge */
188 u = v2_dot(grad2[b01], v2_cons(rx0, ry1));
189 v = v2_dot(grad2[b11], v2_cons(rx1, ry1));
190 b = lerp(u, v, sx);
192 /* interpolate between them */
193 return lerp(a, b, sy);
194 }
196 scalar_t noise3(scalar_t x, scalar_t y, scalar_t z)
197 {
198 int i, j;
199 int bx0, bx1, by0, by1, bz0, bz1;
200 int b00, b10, b01, b11;
201 scalar_t rx0, rx1, ry0, ry1, rz0, rz1;
202 scalar_t sx, sy, sz;
203 scalar_t u, v, a, b, c, d;
205 if(!tables_valid) {
206 init_noise();
207 tables_valid = 1;
208 }
210 setup(x, bx0, bx1, rx0, rx1);
211 setup(y, by0, by1, ry0, ry1);
212 setup(z, bz0, bz1, rz0, rz1);
214 i = perm[bx0];
215 j = perm[bx1];
217 b00 = perm[i + by0];
218 b10 = perm[j + by0];
219 b01 = perm[i + by1];
220 b11 = perm[j + by1];
222 /* calculate hermite interpolating factors */
223 sx = s_curve(rx0);
224 sy = s_curve(ry0);
225 sz = s_curve(rz0);
227 /* interpolate along the top slice of the cell */
228 u = v3_dot(grad3[b00 + bz0], v3_cons(rx0, ry0, rz0));
229 v = v3_dot(grad3[b10 + bz0], v3_cons(rx1, ry0, rz0));
230 a = lerp(u, v, sx);
232 u = v3_dot(grad3[b01 + bz0], v3_cons(rx0, ry1, rz0));
233 v = v3_dot(grad3[b11 + bz0], v3_cons(rx1, ry1, rz0));
234 b = lerp(u, v, sx);
236 c = lerp(a, b, sy);
238 /* interpolate along the bottom slice of the cell */
239 u = v3_dot(grad3[b00 + bz0], v3_cons(rx0, ry0, rz1));
240 v = v3_dot(grad3[b10 + bz0], v3_cons(rx1, ry0, rz1));
241 a = lerp(u, v, sx);
243 u = v3_dot(grad3[b01 + bz0], v3_cons(rx0, ry1, rz1));
244 v = v3_dot(grad3[b11 + bz0], v3_cons(rx1, ry1, rz1));
245 b = lerp(u, v, sx);
247 d = lerp(a, b, sy);
249 /* interpolate between slices */
250 return lerp(c, d, sz);
251 }
253 scalar_t fbm1(scalar_t x, int octaves)
254 {
255 int i;
256 scalar_t res = 0.0f, freq = 1.0f;
257 for(i=0; i<octaves; i++) {
258 res += noise1(x * freq) / freq;
259 freq *= 2.0f;
260 }
261 return res;
262 }
264 scalar_t fbm2(scalar_t x, scalar_t y, int octaves)
265 {
266 int i;
267 scalar_t res = 0.0f, freq = 1.0f;
268 for(i=0; i<octaves; i++) {
269 res += noise2(x * freq, y * freq) / freq;
270 freq *= 2.0f;
271 }
272 return res;
273 }
275 scalar_t fbm3(scalar_t x, scalar_t y, scalar_t z, int octaves)
276 {
277 int i;
278 scalar_t res = 0.0f, freq = 1.0f;
279 for(i=0; i<octaves; i++) {
280 res += noise3(x * freq, y * freq, z * freq) / freq;
281 freq *= 2.0f;
282 }
283 return res;
284 }
286 scalar_t turbulence1(scalar_t x, int octaves)
287 {
288 int i;
289 scalar_t res = 0.0f, freq = 1.0f;
290 for(i=0; i<octaves; i++) {
291 res += fabs(noise1(x * freq) / freq);
292 freq *= 2.0f;
293 }
294 return res;
295 }
297 scalar_t turbulence2(scalar_t x, scalar_t y, int octaves)
298 {
299 int i;
300 scalar_t res = 0.0f, freq = 1.0f;
301 for(i=0; i<octaves; i++) {
302 res += fabs(noise2(x * freq, y * freq) / freq);
303 freq *= 2.0f;
304 }
305 return res;
306 }
308 scalar_t turbulence3(scalar_t x, scalar_t y, scalar_t z, int octaves)
309 {
310 int i;
311 scalar_t res = 0.0f, freq = 1.0f;
312 for(i=0; i<octaves; i++) {
313 res += fabs(noise3(x * freq, y * freq, z * freq) / freq);
314 freq *= 2.0f;
315 }
316 return res;
317 }