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 #include <stdlib.h>
|
nuclear@1
|
19 #include <math.h>
|
nuclear@1
|
20 #include "vmath.h"
|
nuclear@1
|
21
|
nuclear@1
|
22 #if defined(__APPLE__) && !defined(TARGET_IPHONE)
|
nuclear@1
|
23 #include <xmmintrin.h>
|
nuclear@1
|
24
|
nuclear@1
|
25 void enable_fpexcept(void)
|
nuclear@1
|
26 {
|
nuclear@1
|
27 unsigned int bits;
|
nuclear@1
|
28 bits = _MM_MASK_INVALID | _MM_MASK_DIV_ZERO | _MM_MASK_OVERFLOW | _MM_MASK_UNDERFLOW;
|
nuclear@1
|
29 _MM_SET_EXCEPTION_MASK(_MM_GET_EXCEPTION_MASK() & ~bits);
|
nuclear@1
|
30 }
|
nuclear@1
|
31
|
nuclear@1
|
32 void disable_fpexcept(void)
|
nuclear@1
|
33 {
|
nuclear@1
|
34 unsigned int bits;
|
nuclear@1
|
35 bits = _MM_MASK_INVALID | _MM_MASK_DIV_ZERO | _MM_MASK_OVERFLOW | _MM_MASK_UNDERFLOW;
|
nuclear@1
|
36 _MM_SET_EXCEPTION_MASK(_MM_GET_EXCEPTION_MASK() | bits);
|
nuclear@1
|
37 }
|
nuclear@1
|
38
|
nuclear@1
|
39 #elif defined(__GNUC__) && !defined(TARGET_IPHONE)
|
nuclear@1
|
40 #define __USE_GNU
|
nuclear@1
|
41 #include <fenv.h>
|
nuclear@1
|
42
|
nuclear@1
|
43 void enable_fpexcept(void)
|
nuclear@1
|
44 {
|
nuclear@1
|
45 feenableexcept(FE_INVALID | FE_DIVBYZERO | FE_OVERFLOW | FE_UNDERFLOW);
|
nuclear@1
|
46 }
|
nuclear@1
|
47
|
nuclear@1
|
48 void disable_fpexcept(void)
|
nuclear@1
|
49 {
|
nuclear@1
|
50 fedisableexcept(FE_INVALID | FE_DIVBYZERO | FE_OVERFLOW | FE_UNDERFLOW);
|
nuclear@1
|
51 }
|
nuclear@1
|
52
|
nuclear@1
|
53 #elif defined(_MSC_VER)
|
nuclear@1
|
54 #include <float.h>
|
nuclear@1
|
55
|
nuclear@1
|
56 void enable_fpexcept(void)
|
nuclear@1
|
57 {
|
nuclear@1
|
58 _clearfp();
|
nuclear@1
|
59 _controlfp(_controlfp(0, 0) & ~(_EM_INVALID | _EM_ZERODIVIDE | _EM_OVERFLOW), _MCW_EM);
|
nuclear@1
|
60 }
|
nuclear@1
|
61
|
nuclear@1
|
62 void disable_fpexcept(void)
|
nuclear@1
|
63 {
|
nuclear@1
|
64 _clearfp();
|
nuclear@1
|
65 _controlfp(_controlfp(0, 0) | (_EM_INVALID | _EM_ZERODIVIDE | _EM_OVERFLOW), _MCW_EM);
|
nuclear@1
|
66 }
|
nuclear@1
|
67 #else
|
nuclear@1
|
68 void enable_fpexcept(void) {}
|
nuclear@1
|
69 void disable_fpexcept(void) {}
|
nuclear@1
|
70 #endif
|
nuclear@1
|
71
|
nuclear@1
|
72
|
nuclear@1
|
73 /** Numerical calculation of integrals using simpson's rule */
|
nuclear@1
|
74 scalar_t integral(scalar_t (*f)(scalar_t), scalar_t low, scalar_t high, int samples)
|
nuclear@1
|
75 {
|
nuclear@1
|
76 int i;
|
nuclear@1
|
77 scalar_t h = (high - low) / (scalar_t)samples;
|
nuclear@1
|
78 scalar_t sum = 0.0;
|
nuclear@1
|
79
|
nuclear@1
|
80 for(i=0; i<samples+1; i++) {
|
nuclear@1
|
81 scalar_t y = f((scalar_t)i * h + low);
|
nuclear@1
|
82 sum += ((!i || i == samples) ? y : ((i % 2) ? 4.0 * y : 2.0 * y)) * (h / 3.0);
|
nuclear@1
|
83 }
|
nuclear@1
|
84 return sum;
|
nuclear@1
|
85 }
|
nuclear@1
|
86
|
nuclear@1
|
87 /** Gaussuan function */
|
nuclear@1
|
88 scalar_t gaussian(scalar_t x, scalar_t mean, scalar_t sdev)
|
nuclear@1
|
89 {
|
nuclear@1
|
90 scalar_t exponent = -SQ(x - mean) / (2.0 * SQ(sdev));
|
nuclear@1
|
91 return 1.0 - -pow(M_E, exponent) / (sdev * sqrt(TWO_PI));
|
nuclear@1
|
92 }
|
nuclear@1
|
93
|
nuclear@1
|
94
|
nuclear@1
|
95 /** b-spline approximation */
|
nuclear@1
|
96 scalar_t bspline(scalar_t a, scalar_t b, scalar_t c, scalar_t d, scalar_t t)
|
nuclear@1
|
97 {
|
nuclear@1
|
98 vec4_t tmp;
|
nuclear@1
|
99 scalar_t tsq = t * t;
|
nuclear@1
|
100
|
nuclear@1
|
101 static mat4_t bspline_mat = {
|
nuclear@1
|
102 {-1, 3, -3, 1},
|
nuclear@1
|
103 {3, -6, 3, 0},
|
nuclear@1
|
104 {-3, 0, 3, 0},
|
nuclear@1
|
105 {1, 4, 1, 0}
|
nuclear@1
|
106 };
|
nuclear@1
|
107
|
nuclear@1
|
108 tmp = v4_scale(v4_transform(v4_cons(a, b, c, d), bspline_mat), 1.0f / 6.0f);
|
nuclear@1
|
109 return v4_dot(v4_cons(tsq * t, tsq, t, 1.0), tmp);
|
nuclear@1
|
110 }
|
nuclear@1
|
111
|
nuclear@1
|
112 /** Catmull-rom spline interpolation */
|
nuclear@1
|
113 scalar_t spline(scalar_t a, scalar_t b, scalar_t c, scalar_t d, scalar_t t)
|
nuclear@1
|
114 {
|
nuclear@1
|
115 vec4_t tmp;
|
nuclear@1
|
116 scalar_t tsq = t * t;
|
nuclear@1
|
117
|
nuclear@1
|
118 static mat4_t crspline_mat = {
|
nuclear@1
|
119 {-1, 3, -3, 1},
|
nuclear@1
|
120 {2, -5, 4, -1},
|
nuclear@1
|
121 {-1, 0, 1, 0},
|
nuclear@1
|
122 {0, 2, 0, 0}
|
nuclear@1
|
123 };
|
nuclear@1
|
124
|
nuclear@1
|
125 tmp = v4_scale(v4_transform(v4_cons(a, b, c, d), crspline_mat), 0.5);
|
nuclear@1
|
126 return v4_dot(v4_cons(tsq * t, tsq, t, 1.0), tmp);
|
nuclear@1
|
127 }
|
nuclear@1
|
128
|
nuclear@1
|
129 /** Bezier interpolation */
|
nuclear@1
|
130 scalar_t bezier(scalar_t a, scalar_t b, scalar_t c, scalar_t d, scalar_t t)
|
nuclear@1
|
131 {
|
nuclear@1
|
132 scalar_t omt, omt3, t3, f;
|
nuclear@1
|
133 t3 = t * t * t;
|
nuclear@1
|
134 omt = 1.0f - t;
|
nuclear@1
|
135 omt3 = omt * omt * omt;
|
nuclear@1
|
136 f = 3 * t * omt;
|
nuclear@1
|
137
|
nuclear@1
|
138 return (a * omt3) + (b * f * omt) + (c * f * t) + (d * t3);
|
nuclear@1
|
139 }
|
nuclear@1
|
140
|
nuclear@1
|
141 /* ---- Ken Perlin's implementation of noise ---- */
|
nuclear@1
|
142
|
nuclear@1
|
143 #define B 0x100
|
nuclear@1
|
144 #define BM 0xff
|
nuclear@1
|
145 #define N 0x1000
|
nuclear@1
|
146 #define NP 12 /* 2^N */
|
nuclear@1
|
147 #define NM 0xfff
|
nuclear@1
|
148
|
nuclear@1
|
149 #define s_curve(t) (t * t * (3.0f - 2.0f * t))
|
nuclear@1
|
150
|
nuclear@1
|
151 #define setup(elem, b0, b1, r0, r1) \
|
nuclear@1
|
152 do { \
|
nuclear@1
|
153 scalar_t t = elem + N; \
|
nuclear@1
|
154 b0 = ((int)t) & BM; \
|
nuclear@1
|
155 b1 = (b0 + 1) & BM; \
|
nuclear@1
|
156 r0 = t - (int)t; \
|
nuclear@1
|
157 r1 = r0 - 1.0f; \
|
nuclear@1
|
158 } while(0)
|
nuclear@1
|
159
|
nuclear@1
|
160
|
nuclear@1
|
161 static int perm[B + B + 2]; /* permuted index from g_n onto themselves */
|
nuclear@1
|
162 static vec3_t grad3[B + B + 2]; /* 3D random gradients */
|
nuclear@1
|
163 static vec2_t grad2[B + B + 2]; /* 2D random gradients */
|
nuclear@1
|
164 static scalar_t grad1[B + B + 2]; /* 1D random ... slopes */
|
nuclear@1
|
165 static int tables_valid;
|
nuclear@1
|
166
|
nuclear@1
|
167 static void init_noise()
|
nuclear@1
|
168 {
|
nuclear@1
|
169 int i;
|
nuclear@1
|
170
|
nuclear@1
|
171 /* calculate random gradients */
|
nuclear@1
|
172 for(i=0; i<B; i++) {
|
nuclear@1
|
173 perm[i] = i; /* .. and initialize permutation mapping to identity */
|
nuclear@1
|
174
|
nuclear@1
|
175 grad1[i] = (scalar_t)((rand() % (B + B)) - B) / B;
|
nuclear@1
|
176
|
nuclear@1
|
177 grad2[i].x = (scalar_t)((rand() % (B + B)) - B) / B;
|
nuclear@1
|
178 grad2[i].y = (scalar_t)((rand() % (B + B)) - B) / B;
|
nuclear@1
|
179 grad2[i] = v2_normalize(grad2[i]);
|
nuclear@1
|
180
|
nuclear@1
|
181 grad3[i].x = (scalar_t)((rand() % (B + B)) - B) / B;
|
nuclear@1
|
182 grad3[i].y = (scalar_t)((rand() % (B + B)) - B) / B;
|
nuclear@1
|
183 grad3[i].z = (scalar_t)((rand() % (B + B)) - B) / B;
|
nuclear@1
|
184 grad3[i] = v3_normalize(grad3[i]);
|
nuclear@1
|
185 }
|
nuclear@1
|
186
|
nuclear@1
|
187 /* permute indices by swapping them randomly */
|
nuclear@1
|
188 for(i=0; i<B; i++) {
|
nuclear@1
|
189 int rand_idx = rand() % B;
|
nuclear@1
|
190
|
nuclear@1
|
191 int tmp = perm[i];
|
nuclear@1
|
192 perm[i] = perm[rand_idx];
|
nuclear@1
|
193 perm[rand_idx] = tmp;
|
nuclear@1
|
194 }
|
nuclear@1
|
195
|
nuclear@1
|
196 /* fill up the rest of the arrays by duplicating the existing gradients */
|
nuclear@1
|
197 /* and permutations */
|
nuclear@1
|
198 for(i=0; i<B+2; i++) {
|
nuclear@1
|
199 perm[B + i] = perm[i];
|
nuclear@1
|
200 grad1[B + i] = grad1[i];
|
nuclear@1
|
201 grad2[B + i] = grad2[i];
|
nuclear@1
|
202 grad3[B + i] = grad3[i];
|
nuclear@1
|
203 }
|
nuclear@1
|
204 }
|
nuclear@1
|
205
|
nuclear@1
|
206 scalar_t noise1(scalar_t x)
|
nuclear@1
|
207 {
|
nuclear@1
|
208 int bx0, bx1;
|
nuclear@1
|
209 scalar_t rx0, rx1, sx, u, v;
|
nuclear@1
|
210
|
nuclear@1
|
211 if(!tables_valid) {
|
nuclear@1
|
212 init_noise();
|
nuclear@1
|
213 tables_valid = 1;
|
nuclear@1
|
214 }
|
nuclear@1
|
215
|
nuclear@1
|
216 setup(x, bx0, bx1, rx0, rx1);
|
nuclear@1
|
217 sx = s_curve(rx0);
|
nuclear@1
|
218 u = rx0 * grad1[perm[bx0]];
|
nuclear@1
|
219 v = rx1 * grad1[perm[bx1]];
|
nuclear@1
|
220
|
nuclear@1
|
221 return lerp(u, v, sx);
|
nuclear@1
|
222 }
|
nuclear@1
|
223
|
nuclear@1
|
224 scalar_t noise2(scalar_t x, scalar_t y)
|
nuclear@1
|
225 {
|
nuclear@1
|
226 int i, j, b00, b10, b01, b11;
|
nuclear@1
|
227 int bx0, bx1, by0, by1;
|
nuclear@1
|
228 scalar_t rx0, rx1, ry0, ry1;
|
nuclear@1
|
229 scalar_t sx, sy, u, v, a, b;
|
nuclear@1
|
230
|
nuclear@1
|
231 if(!tables_valid) {
|
nuclear@1
|
232 init_noise();
|
nuclear@1
|
233 tables_valid = 1;
|
nuclear@1
|
234 }
|
nuclear@1
|
235
|
nuclear@1
|
236 setup(x, bx0, bx1, rx0, rx1);
|
nuclear@1
|
237 setup(y, by0, by1, ry0, ry1);
|
nuclear@1
|
238
|
nuclear@1
|
239 i = perm[bx0];
|
nuclear@1
|
240 j = perm[bx1];
|
nuclear@1
|
241
|
nuclear@1
|
242 b00 = perm[i + by0];
|
nuclear@1
|
243 b10 = perm[j + by0];
|
nuclear@1
|
244 b01 = perm[i + by1];
|
nuclear@1
|
245 b11 = perm[j + by1];
|
nuclear@1
|
246
|
nuclear@1
|
247 /* calculate hermite inteprolating factors */
|
nuclear@1
|
248 sx = s_curve(rx0);
|
nuclear@1
|
249 sy = s_curve(ry0);
|
nuclear@1
|
250
|
nuclear@1
|
251 /* interpolate along the left edge */
|
nuclear@1
|
252 u = v2_dot(grad2[b00], v2_cons(rx0, ry0));
|
nuclear@1
|
253 v = v2_dot(grad2[b10], v2_cons(rx1, ry0));
|
nuclear@1
|
254 a = lerp(u, v, sx);
|
nuclear@1
|
255
|
nuclear@1
|
256 /* interpolate along the right edge */
|
nuclear@1
|
257 u = v2_dot(grad2[b01], v2_cons(rx0, ry1));
|
nuclear@1
|
258 v = v2_dot(grad2[b11], v2_cons(rx1, ry1));
|
nuclear@1
|
259 b = lerp(u, v, sx);
|
nuclear@1
|
260
|
nuclear@1
|
261 /* interpolate between them */
|
nuclear@1
|
262 return lerp(a, b, sy);
|
nuclear@1
|
263 }
|
nuclear@1
|
264
|
nuclear@1
|
265 scalar_t noise3(scalar_t x, scalar_t y, scalar_t z)
|
nuclear@1
|
266 {
|
nuclear@1
|
267 int i, j;
|
nuclear@1
|
268 int bx0, bx1, by0, by1, bz0, bz1;
|
nuclear@1
|
269 int b00, b10, b01, b11;
|
nuclear@1
|
270 scalar_t rx0, rx1, ry0, ry1, rz0, rz1;
|
nuclear@1
|
271 scalar_t sx, sy, sz;
|
nuclear@1
|
272 scalar_t u, v, a, b, c, d;
|
nuclear@1
|
273
|
nuclear@1
|
274 if(!tables_valid) {
|
nuclear@1
|
275 init_noise();
|
nuclear@1
|
276 tables_valid = 1;
|
nuclear@1
|
277 }
|
nuclear@1
|
278
|
nuclear@1
|
279 setup(x, bx0, bx1, rx0, rx1);
|
nuclear@1
|
280 setup(y, by0, by1, ry0, ry1);
|
nuclear@1
|
281 setup(z, bz0, bz1, rz0, rz1);
|
nuclear@1
|
282
|
nuclear@1
|
283 i = perm[bx0];
|
nuclear@1
|
284 j = perm[bx1];
|
nuclear@1
|
285
|
nuclear@1
|
286 b00 = perm[i + by0];
|
nuclear@1
|
287 b10 = perm[j + by0];
|
nuclear@1
|
288 b01 = perm[i + by1];
|
nuclear@1
|
289 b11 = perm[j + by1];
|
nuclear@1
|
290
|
nuclear@1
|
291 /* calculate hermite interpolating factors */
|
nuclear@1
|
292 sx = s_curve(rx0);
|
nuclear@1
|
293 sy = s_curve(ry0);
|
nuclear@1
|
294 sz = s_curve(rz0);
|
nuclear@1
|
295
|
nuclear@1
|
296 /* interpolate along the top slice of the cell */
|
nuclear@1
|
297 u = v3_dot(grad3[b00 + bz0], v3_cons(rx0, ry0, rz0));
|
nuclear@1
|
298 v = v3_dot(grad3[b10 + bz0], v3_cons(rx1, ry0, rz0));
|
nuclear@1
|
299 a = lerp(u, v, sx);
|
nuclear@1
|
300
|
nuclear@1
|
301 u = v3_dot(grad3[b01 + bz0], v3_cons(rx0, ry1, rz0));
|
nuclear@1
|
302 v = v3_dot(grad3[b11 + bz0], v3_cons(rx1, ry1, rz0));
|
nuclear@1
|
303 b = lerp(u, v, sx);
|
nuclear@1
|
304
|
nuclear@1
|
305 c = lerp(a, b, sy);
|
nuclear@1
|
306
|
nuclear@1
|
307 /* interpolate along the bottom slice of the cell */
|
nuclear@1
|
308 u = v3_dot(grad3[b00 + bz0], v3_cons(rx0, ry0, rz1));
|
nuclear@1
|
309 v = v3_dot(grad3[b10 + bz0], v3_cons(rx1, ry0, rz1));
|
nuclear@1
|
310 a = lerp(u, v, sx);
|
nuclear@1
|
311
|
nuclear@1
|
312 u = v3_dot(grad3[b01 + bz0], v3_cons(rx0, ry1, rz1));
|
nuclear@1
|
313 v = v3_dot(grad3[b11 + bz0], v3_cons(rx1, ry1, rz1));
|
nuclear@1
|
314 b = lerp(u, v, sx);
|
nuclear@1
|
315
|
nuclear@1
|
316 d = lerp(a, b, sy);
|
nuclear@1
|
317
|
nuclear@1
|
318 /* interpolate between slices */
|
nuclear@1
|
319 return lerp(c, d, sz);
|
nuclear@1
|
320 }
|
nuclear@1
|
321
|
nuclear@1
|
322 scalar_t fbm1(scalar_t x, int octaves)
|
nuclear@1
|
323 {
|
nuclear@1
|
324 int i;
|
nuclear@1
|
325 scalar_t res = 0.0f, freq = 1.0f;
|
nuclear@1
|
326 for(i=0; i<octaves; i++) {
|
nuclear@1
|
327 res += noise1(x * freq) / freq;
|
nuclear@1
|
328 freq *= 2.0f;
|
nuclear@1
|
329 }
|
nuclear@1
|
330 return res;
|
nuclear@1
|
331 }
|
nuclear@1
|
332
|
nuclear@1
|
333 scalar_t fbm2(scalar_t x, scalar_t y, int octaves)
|
nuclear@1
|
334 {
|
nuclear@1
|
335 int i;
|
nuclear@1
|
336 scalar_t res = 0.0f, freq = 1.0f;
|
nuclear@1
|
337 for(i=0; i<octaves; i++) {
|
nuclear@1
|
338 res += noise2(x * freq, y * freq) / freq;
|
nuclear@1
|
339 freq *= 2.0f;
|
nuclear@1
|
340 }
|
nuclear@1
|
341 return res;
|
nuclear@1
|
342 }
|
nuclear@1
|
343
|
nuclear@1
|
344 scalar_t fbm3(scalar_t x, scalar_t y, scalar_t z, int octaves)
|
nuclear@1
|
345 {
|
nuclear@1
|
346 int i;
|
nuclear@1
|
347 scalar_t res = 0.0f, freq = 1.0f;
|
nuclear@1
|
348 for(i=0; i<octaves; i++) {
|
nuclear@1
|
349 res += noise3(x * freq, y * freq, z * freq) / freq;
|
nuclear@1
|
350 freq *= 2.0f;
|
nuclear@1
|
351 }
|
nuclear@1
|
352 return res;
|
nuclear@1
|
353 }
|
nuclear@1
|
354
|
nuclear@1
|
355 scalar_t turbulence1(scalar_t x, int octaves)
|
nuclear@1
|
356 {
|
nuclear@1
|
357 int i;
|
nuclear@1
|
358 scalar_t res = 0.0f, freq = 1.0f;
|
nuclear@1
|
359 for(i=0; i<octaves; i++) {
|
nuclear@1
|
360 res += fabs(noise1(x * freq) / freq);
|
nuclear@1
|
361 freq *= 2.0f;
|
nuclear@1
|
362 }
|
nuclear@1
|
363 return res;
|
nuclear@1
|
364 }
|
nuclear@1
|
365
|
nuclear@1
|
366 scalar_t turbulence2(scalar_t x, scalar_t y, int octaves)
|
nuclear@1
|
367 {
|
nuclear@1
|
368 int i;
|
nuclear@1
|
369 scalar_t res = 0.0f, freq = 1.0f;
|
nuclear@1
|
370 for(i=0; i<octaves; i++) {
|
nuclear@1
|
371 res += fabs(noise2(x * freq, y * freq) / freq);
|
nuclear@1
|
372 freq *= 2.0f;
|
nuclear@1
|
373 }
|
nuclear@1
|
374 return res;
|
nuclear@1
|
375 }
|
nuclear@1
|
376
|
nuclear@1
|
377 scalar_t turbulence3(scalar_t x, scalar_t y, scalar_t z, int octaves)
|
nuclear@1
|
378 {
|
nuclear@1
|
379 int i;
|
nuclear@1
|
380 scalar_t res = 0.0f, freq = 1.0f;
|
nuclear@1
|
381 for(i=0; i<octaves; i++) {
|
nuclear@1
|
382 res += fabs(noise3(x * freq, y * freq, z * freq) / freq);
|
nuclear@1
|
383 freq *= 2.0f;
|
nuclear@1
|
384 }
|
nuclear@1
|
385 return res;
|
nuclear@1
|
386 }
|