istereo2

annotate libs/vmath/vmath.c @ 2:81d35769f546

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