dungeon_crawler

annotate prototype/vmath/vmath.c @ 1:96de911d05d4

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