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