istereo

annotate libs/vmath/vmath.c @ 28:c0ae8e668447

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