dbf-halloween2015

view libs/vmath/vmath.c @ 4:4316c0c879e9

fixed RUN script for macosx
author John Tsiombikas <nuclear@member.fsf.org>
date Sun, 01 Nov 2015 06:18:18 +0200
parents
children
line source
1 /*
2 libvmath - a vector math library
3 Copyright (C) 2004-2011 John Tsiombikas <nuclear@member.fsf.org>
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published
7 by the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18 #include <stdlib.h>
19 #include <math.h>
20 #include "vmath.h"
22 #if defined(__APPLE__) && !defined(TARGET_IPHONE)
23 #include <xmmintrin.h>
25 void enable_fpexcept(void)
26 {
27 unsigned int bits;
28 bits = _MM_MASK_INVALID | _MM_MASK_DIV_ZERO | _MM_MASK_OVERFLOW | _MM_MASK_UNDERFLOW;
29 _MM_SET_EXCEPTION_MASK(_MM_GET_EXCEPTION_MASK() & ~bits);
30 }
32 void disable_fpexcept(void)
33 {
34 unsigned int bits;
35 bits = _MM_MASK_INVALID | _MM_MASK_DIV_ZERO | _MM_MASK_OVERFLOW | _MM_MASK_UNDERFLOW;
36 _MM_SET_EXCEPTION_MASK(_MM_GET_EXCEPTION_MASK() | bits);
37 }
39 #elif defined(__GNUC__) && !defined(TARGET_IPHONE)
40 #define __USE_GNU
41 #include <fenv.h>
43 void enable_fpexcept(void)
44 {
45 feenableexcept(FE_INVALID | FE_DIVBYZERO | FE_OVERFLOW | FE_UNDERFLOW);
46 }
48 void disable_fpexcept(void)
49 {
50 fedisableexcept(FE_INVALID | FE_DIVBYZERO | FE_OVERFLOW | FE_UNDERFLOW);
51 }
53 #elif defined(_MSC_VER)
54 #include <float.h>
56 void enable_fpexcept(void)
57 {
58 _clearfp();
59 _controlfp(_controlfp(0, 0) & ~(_EM_INVALID | _EM_ZERODIVIDE | _EM_OVERFLOW), _MCW_EM);
60 }
62 void disable_fpexcept(void)
63 {
64 _clearfp();
65 _controlfp(_controlfp(0, 0) | (_EM_INVALID | _EM_ZERODIVIDE | _EM_OVERFLOW), _MCW_EM);
66 }
67 #else
68 void enable_fpexcept(void) {}
69 void disable_fpexcept(void) {}
70 #endif
73 /** Numerical calculation of integrals using simpson's rule */
74 scalar_t integral(scalar_t (*f)(scalar_t), scalar_t low, scalar_t high, int samples)
75 {
76 int i;
77 scalar_t h = (high - low) / (scalar_t)samples;
78 scalar_t sum = 0.0;
80 for(i=0; i<samples+1; i++) {
81 scalar_t y = f((scalar_t)i * h + low);
82 sum += ((!i || i == samples) ? y : ((i % 2) ? 4.0 * y : 2.0 * y)) * (h / 3.0);
83 }
84 return sum;
85 }
87 /** Gaussuan function */
88 scalar_t gaussian(scalar_t x, scalar_t mean, scalar_t sdev)
89 {
90 scalar_t exponent = -SQ(x - mean) / (2.0 * SQ(sdev));
91 return 1.0 - -pow(M_E, exponent) / (sdev * sqrt(TWO_PI));
92 }
95 /** b-spline approximation */
96 scalar_t bspline(scalar_t a, scalar_t b, scalar_t c, scalar_t d, scalar_t t)
97 {
98 vec4_t tmp;
99 scalar_t tsq = t * t;
101 static mat4_t bspline_mat = {
102 {-1, 3, -3, 1},
103 {3, -6, 3, 0},
104 {-3, 0, 3, 0},
105 {1, 4, 1, 0}
106 };
108 tmp = v4_scale(v4_transform(v4_cons(a, b, c, d), bspline_mat), 1.0f / 6.0f);
109 return v4_dot(v4_cons(tsq * t, tsq, t, 1.0), tmp);
110 }
112 /** Catmull-rom spline interpolation */
113 scalar_t spline(scalar_t a, scalar_t b, scalar_t c, scalar_t d, scalar_t t)
114 {
115 vec4_t tmp;
116 scalar_t tsq = t * t;
118 static mat4_t crspline_mat = {
119 {-1, 3, -3, 1},
120 {2, -5, 4, -1},
121 {-1, 0, 1, 0},
122 {0, 2, 0, 0}
123 };
125 tmp = v4_scale(v4_transform(v4_cons(a, b, c, d), crspline_mat), 0.5);
126 return v4_dot(v4_cons(tsq * t, tsq, t, 1.0), tmp);
127 }
129 /** Bezier interpolation */
130 scalar_t bezier(scalar_t a, scalar_t b, scalar_t c, scalar_t d, scalar_t t)
131 {
132 scalar_t omt, omt3, t3, f;
133 t3 = t * t * t;
134 omt = 1.0f - t;
135 omt3 = omt * omt * omt;
136 f = 3 * t * omt;
138 return (a * omt3) + (b * f * omt) + (c * f * t) + (d * t3);
139 }
141 /* ---- Ken Perlin's implementation of noise ---- */
143 #define B 0x100
144 #define BM 0xff
145 #define N 0x1000
146 #define NP 12 /* 2^N */
147 #define NM 0xfff
149 #define s_curve(t) (t * t * (3.0f - 2.0f * t))
151 #define setup(elem, b0, b1, r0, r1) \
152 do { \
153 scalar_t t = elem + N; \
154 b0 = ((int)t) & BM; \
155 b1 = (b0 + 1) & BM; \
156 r0 = t - (int)t; \
157 r1 = r0 - 1.0f; \
158 } while(0)
161 static int perm[B + B + 2]; /* permuted index from g_n onto themselves */
162 static vec3_t grad3[B + B + 2]; /* 3D random gradients */
163 static vec2_t grad2[B + B + 2]; /* 2D random gradients */
164 static scalar_t grad1[B + B + 2]; /* 1D random ... slopes */
165 static int tables_valid;
167 static void init_noise()
168 {
169 int i;
171 /* calculate random gradients */
172 for(i=0; i<B; i++) {
173 perm[i] = i; /* .. and initialize permutation mapping to identity */
175 grad1[i] = (scalar_t)((rand() % (B + B)) - B) / B;
177 grad2[i].x = (scalar_t)((rand() % (B + B)) - B) / B;
178 grad2[i].y = (scalar_t)((rand() % (B + B)) - B) / B;
179 grad2[i] = v2_normalize(grad2[i]);
181 grad3[i].x = (scalar_t)((rand() % (B + B)) - B) / B;
182 grad3[i].y = (scalar_t)((rand() % (B + B)) - B) / B;
183 grad3[i].z = (scalar_t)((rand() % (B + B)) - B) / B;
184 grad3[i] = v3_normalize(grad3[i]);
185 }
187 /* permute indices by swapping them randomly */
188 for(i=0; i<B; i++) {
189 int rand_idx = rand() % B;
191 int tmp = perm[i];
192 perm[i] = perm[rand_idx];
193 perm[rand_idx] = tmp;
194 }
196 /* fill up the rest of the arrays by duplicating the existing gradients */
197 /* and permutations */
198 for(i=0; i<B+2; i++) {
199 perm[B + i] = perm[i];
200 grad1[B + i] = grad1[i];
201 grad2[B + i] = grad2[i];
202 grad3[B + i] = grad3[i];
203 }
204 }
206 scalar_t noise1(scalar_t x)
207 {
208 int bx0, bx1;
209 scalar_t rx0, rx1, sx, u, v;
211 if(!tables_valid) {
212 init_noise();
213 tables_valid = 1;
214 }
216 setup(x, bx0, bx1, rx0, rx1);
217 sx = s_curve(rx0);
218 u = rx0 * grad1[perm[bx0]];
219 v = rx1 * grad1[perm[bx1]];
221 return lerp(u, v, sx);
222 }
224 scalar_t noise2(scalar_t x, scalar_t y)
225 {
226 int i, j, b00, b10, b01, b11;
227 int bx0, bx1, by0, by1;
228 scalar_t rx0, rx1, ry0, ry1;
229 scalar_t sx, sy, u, v, a, b;
231 if(!tables_valid) {
232 init_noise();
233 tables_valid = 1;
234 }
236 setup(x, bx0, bx1, rx0, rx1);
237 setup(y, by0, by1, ry0, ry1);
239 i = perm[bx0];
240 j = perm[bx1];
242 b00 = perm[i + by0];
243 b10 = perm[j + by0];
244 b01 = perm[i + by1];
245 b11 = perm[j + by1];
247 /* calculate hermite inteprolating factors */
248 sx = s_curve(rx0);
249 sy = s_curve(ry0);
251 /* interpolate along the left edge */
252 u = v2_dot(grad2[b00], v2_cons(rx0, ry0));
253 v = v2_dot(grad2[b10], v2_cons(rx1, ry0));
254 a = lerp(u, v, sx);
256 /* interpolate along the right edge */
257 u = v2_dot(grad2[b01], v2_cons(rx0, ry1));
258 v = v2_dot(grad2[b11], v2_cons(rx1, ry1));
259 b = lerp(u, v, sx);
261 /* interpolate between them */
262 return lerp(a, b, sy);
263 }
265 scalar_t noise3(scalar_t x, scalar_t y, scalar_t z)
266 {
267 int i, j;
268 int bx0, bx1, by0, by1, bz0, bz1;
269 int b00, b10, b01, b11;
270 scalar_t rx0, rx1, ry0, ry1, rz0, rz1;
271 scalar_t sx, sy, sz;
272 scalar_t u, v, a, b, c, d;
274 if(!tables_valid) {
275 init_noise();
276 tables_valid = 1;
277 }
279 setup(x, bx0, bx1, rx0, rx1);
280 setup(y, by0, by1, ry0, ry1);
281 setup(z, bz0, bz1, rz0, rz1);
283 i = perm[bx0];
284 j = perm[bx1];
286 b00 = perm[i + by0];
287 b10 = perm[j + by0];
288 b01 = perm[i + by1];
289 b11 = perm[j + by1];
291 /* calculate hermite interpolating factors */
292 sx = s_curve(rx0);
293 sy = s_curve(ry0);
294 sz = s_curve(rz0);
296 /* interpolate along the top slice of the cell */
297 u = v3_dot(grad3[b00 + bz0], v3_cons(rx0, ry0, rz0));
298 v = v3_dot(grad3[b10 + bz0], v3_cons(rx1, ry0, rz0));
299 a = lerp(u, v, sx);
301 u = v3_dot(grad3[b01 + bz0], v3_cons(rx0, ry1, rz0));
302 v = v3_dot(grad3[b11 + bz0], v3_cons(rx1, ry1, rz0));
303 b = lerp(u, v, sx);
305 c = lerp(a, b, sy);
307 /* interpolate along the bottom slice of the cell */
308 u = v3_dot(grad3[b00 + bz0], v3_cons(rx0, ry0, rz1));
309 v = v3_dot(grad3[b10 + bz0], v3_cons(rx1, ry0, rz1));
310 a = lerp(u, v, sx);
312 u = v3_dot(grad3[b01 + bz0], v3_cons(rx0, ry1, rz1));
313 v = v3_dot(grad3[b11 + bz0], v3_cons(rx1, ry1, rz1));
314 b = lerp(u, v, sx);
316 d = lerp(a, b, sy);
318 /* interpolate between slices */
319 return lerp(c, d, sz);
320 }
322 scalar_t fbm1(scalar_t x, int octaves)
323 {
324 int i;
325 scalar_t res = 0.0f, freq = 1.0f;
326 for(i=0; i<octaves; i++) {
327 res += noise1(x * freq) / freq;
328 freq *= 2.0f;
329 }
330 return res;
331 }
333 scalar_t fbm2(scalar_t x, scalar_t y, int octaves)
334 {
335 int i;
336 scalar_t res = 0.0f, freq = 1.0f;
337 for(i=0; i<octaves; i++) {
338 res += noise2(x * freq, y * freq) / freq;
339 freq *= 2.0f;
340 }
341 return res;
342 }
344 scalar_t fbm3(scalar_t x, scalar_t y, scalar_t z, int octaves)
345 {
346 int i;
347 scalar_t res = 0.0f, freq = 1.0f;
348 for(i=0; i<octaves; i++) {
349 res += noise3(x * freq, y * freq, z * freq) / freq;
350 freq *= 2.0f;
351 }
352 return res;
353 }
355 scalar_t turbulence1(scalar_t x, int octaves)
356 {
357 int i;
358 scalar_t res = 0.0f, freq = 1.0f;
359 for(i=0; i<octaves; i++) {
360 res += fabs(noise1(x * freq) / freq);
361 freq *= 2.0f;
362 }
363 return res;
364 }
366 scalar_t turbulence2(scalar_t x, scalar_t y, int octaves)
367 {
368 int i;
369 scalar_t res = 0.0f, freq = 1.0f;
370 for(i=0; i<octaves; i++) {
371 res += fabs(noise2(x * freq, y * freq) / freq);
372 freq *= 2.0f;
373 }
374 return res;
375 }
377 scalar_t turbulence3(scalar_t x, scalar_t y, scalar_t z, int octaves)
378 {
379 int i;
380 scalar_t res = 0.0f, freq = 1.0f;
381 for(i=0; i<octaves; i++) {
382 res += fabs(noise3(x * freq, y * freq, z * freq) / freq);
383 freq *= 2.0f;
384 }
385 return res;
386 }