14 |
|
* 5/19/88 Added fractal noise function |
15 |
|
*/ |
16 |
|
|
17 |
+ |
#include <math.h> |
18 |
|
|
19 |
|
#define A 0 |
20 |
|
#define B 1 |
26 |
|
#define rand3c(x,y,z) frand(89*(x)+97*(y)+101*(z)) |
27 |
|
#define rand3d(x,y,z) frand(103*(x)+107*(y)+109*(z)) |
28 |
|
|
29 |
< |
#define hermite(p0,p1,r0,r1,t) ( p0*((2.0*t-3.0)*t*t+1.0) + \ |
30 |
< |
p1*(-2.0*t+3.0)*t*t + \ |
31 |
< |
r0*((t-2.0)*t+1.0)*t + \ |
32 |
< |
r1*(t-1.0)*t*t ) |
29 |
> |
#define hpoly1(t) ((2.0*t-3.0)*t*t+1.0) |
30 |
> |
#define hpoly2(t) (-2.0*t+3.0)*t*t |
31 |
> |
#define hpoly3(t) ((t-2.0)*t+1.0)*t |
32 |
> |
#define hpoly4(t) (t-1.0)*t*t |
33 |
|
|
34 |
< |
static char *noise_name[4] = {"noise3a", "noise3b", "noise3c", "noise3"}; |
34 |
> |
#define hermite(p0,p1,r0,r1,t) ( p0*hpoly1(t) + \ |
35 |
> |
p1*hpoly2(t) + \ |
36 |
> |
r0*hpoly3(t) + \ |
37 |
> |
r1*hpoly4(t) ) |
38 |
> |
|
39 |
> |
static char noise_name[4][8] = {"noise3a", "noise3b", "noise3c", "noise3"}; |
40 |
|
static char fnoise_name[] = "fnoise3"; |
41 |
|
static char hermite_name[] = "hermite"; |
42 |
|
|
43 |
|
double *noise3(), fnoise3(), argument(), frand(); |
44 |
+ |
static interpolate(); |
45 |
|
|
46 |
|
static long xlim[3][2]; |
47 |
|
static double xarg[3]; |
48 |
|
|
49 |
< |
#define EPSILON .0001 /* error allowed in fractal */ |
49 |
> |
#define EPSILON .001 /* error allowed in fractal */ |
50 |
|
|
51 |
|
#define frand3(x,y,z) frand(17*(x)+23*(y)+29*(z)) |
52 |
|
|
68 |
|
while (i--) |
69 |
|
if (nam == noise_name[i]) |
70 |
|
return(noise3(x)[i]); |
71 |
< |
eputs("Bad call to l_noise3()!\n"); |
71 |
> |
eputs(nam); |
72 |
> |
eputs(": called l_noise3!\n"); |
73 |
|
quit(1); |
74 |
|
} |
75 |
|
|
101 |
|
noise3(xnew) /* compute the noise function */ |
102 |
|
register double xnew[3]; |
103 |
|
{ |
96 |
– |
extern double floor(); |
104 |
|
static double x[3] = {-100000.0, -100000.0, -100000.0}; |
105 |
|
static double f[4]; |
106 |
|
|
123 |
|
double f[4]; |
124 |
|
register int i, n; |
125 |
|
{ |
126 |
< |
double f0[4], f1[4]; |
126 |
> |
double f0[4], f1[4], hp1, hp2; |
127 |
|
|
128 |
|
if (n == 0) { |
129 |
|
f[A] = rand3a(xlim[0][i&1],xlim[1][i>>1&1],xlim[2][i>>2]); |
134 |
|
n--; |
135 |
|
interpolate(f0, i, n); |
136 |
|
interpolate(f1, i | 1<<n, n); |
137 |
< |
f[A] = (1.0-xarg[n])*f0[A] + xarg[n]*f1[A]; |
138 |
< |
f[B] = (1.0-xarg[n])*f0[B] + xarg[n]*f1[B]; |
139 |
< |
f[C] = (1.0-xarg[n])*f0[C] + xarg[n]*f1[C]; |
140 |
< |
f[D] = hermite(f0[D], f1[D], f0[n], f1[n], xarg[n]); |
137 |
> |
hp1 = hpoly1(xarg[n]); hp2 = hpoly2(xarg[n]); |
138 |
> |
f[A] = f0[A]*hp1 + f1[A]*hp2; |
139 |
> |
f[B] = f0[B]*hp1 + f1[B]*hp2; |
140 |
> |
f[C] = f0[C]*hp1 + f1[C]*hp2; |
141 |
> |
f[D] = f0[D]*hp1 + f1[D]*hp2 + |
142 |
> |
f0[n]*hpoly3(xarg[n]) + f1[n]*hpoly4(xarg[n]); |
143 |
|
} |
144 |
|
} |
145 |
|
|
157 |
|
fnoise3(p) /* compute fractal noise function */ |
158 |
|
double p[3]; |
159 |
|
{ |
151 |
– |
double floor(); |
160 |
|
long t[3], v[3], beg[3]; |
161 |
|
double fval[8], fc; |
162 |
|
int branch; |