istereo

annotate libs/vmath/vmath.c @ 39:ff055bff6a15

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