30 |
|
r0*((t-2.0)*t+1.0)*t + \ |
31 |
|
r1*(t-1.0)*t*t ) |
32 |
|
|
33 |
< |
double *noise3(), noise3coef(), argument(), frand(); |
33 |
> |
static char noise_name[4][8] = {"noise3a", "noise3b", "noise3c", "noise3"}; |
34 |
> |
static char fnoise_name[] = "fnoise3"; |
35 |
> |
static char hermite_name[] = "hermite"; |
36 |
|
|
37 |
+ |
double *noise3(), fnoise3(), argument(), frand(); |
38 |
+ |
|
39 |
|
static long xlim[3][2]; |
40 |
|
static double xarg[3]; |
41 |
|
|
42 |
< |
#define EPSILON .005 /* error allowed in fractal */ |
42 |
> |
#define EPSILON .0001 /* error allowed in fractal */ |
43 |
|
|
44 |
< |
#define frand3(x,y,z) frand((long)((12.38*(x)-22.30*(y)-42.63*(z))/EPSILON)) |
44 |
> |
#define frand3(x,y,z) frand(17*(x)+23*(y)+29*(z)) |
45 |
|
|
42 |
– |
double fnoise3(); |
46 |
|
|
47 |
< |
|
48 |
< |
double |
49 |
< |
l_noise3() /* compute 3-dimensional noise function */ |
47 |
> |
static double |
48 |
> |
l_noise3(nam) /* compute a noise function */ |
49 |
> |
register char *nam; |
50 |
|
{ |
51 |
< |
return(noise3coef(D)); |
51 |
> |
register int i; |
52 |
> |
double x[3]; |
53 |
> |
/* get point */ |
54 |
> |
x[0] = argument(1); |
55 |
> |
x[1] = argument(2); |
56 |
> |
x[2] = argument(3); |
57 |
> |
/* make appropriate call */ |
58 |
> |
if (nam == fnoise_name) |
59 |
> |
return(fnoise3(x)); |
60 |
> |
i = 4; |
61 |
> |
while (i--) |
62 |
> |
if (nam == noise_name[i]) |
63 |
> |
return(noise3(x)[i]); |
64 |
> |
eputs(nam); |
65 |
> |
eputs(": called l_noise3!\n"); |
66 |
> |
quit(1); |
67 |
|
} |
68 |
|
|
69 |
|
|
70 |
|
double |
71 |
< |
l_noise3a() /* compute x slope of noise function */ |
71 |
> |
l_hermite() /* library call for hermite interpolation */ |
72 |
|
{ |
73 |
< |
return(noise3coef(A)); |
73 |
> |
double t; |
74 |
> |
|
75 |
> |
t = argument(5); |
76 |
> |
return( hermite(argument(1), argument(2), |
77 |
> |
argument(3), argument(4), t) ); |
78 |
|
} |
79 |
|
|
80 |
|
|
81 |
< |
double |
60 |
< |
l_noise3b() /* compute y slope of noise function */ |
81 |
> |
setnoisefuncs() /* add noise functions to library */ |
82 |
|
{ |
83 |
< |
return(noise3coef(B)); |
63 |
< |
} |
83 |
> |
register int i; |
84 |
|
|
85 |
< |
|
86 |
< |
double |
87 |
< |
l_noise3c() /* compute z slope of noise function */ |
88 |
< |
{ |
89 |
< |
return(noise3coef(C)); |
85 |
> |
funset(hermite_name, 5, ':', l_hermite); |
86 |
> |
funset(fnoise_name, 3, ':', l_noise3); |
87 |
> |
i = 4; |
88 |
> |
while (i--) |
89 |
> |
funset(noise_name[i], 3, ':', l_noise3); |
90 |
|
} |
91 |
|
|
92 |
|
|
73 |
– |
double |
74 |
– |
l_fnoise3() /* compute fractal noise function */ |
75 |
– |
{ |
76 |
– |
double x[3]; |
77 |
– |
|
78 |
– |
x[0] = argument(1); |
79 |
– |
x[1] = argument(2); |
80 |
– |
x[2] = argument(3); |
81 |
– |
|
82 |
– |
return(fnoise3(x)); |
83 |
– |
} |
84 |
– |
|
85 |
– |
|
86 |
– |
static double |
87 |
– |
noise3coef(coef) /* return coefficient of noise function */ |
88 |
– |
int coef; |
89 |
– |
{ |
90 |
– |
double x[3]; |
91 |
– |
|
92 |
– |
x[0] = argument(1); |
93 |
– |
x[1] = argument(2); |
94 |
– |
x[2] = argument(3); |
95 |
– |
|
96 |
– |
return(noise3(x)[coef]); |
97 |
– |
} |
98 |
– |
|
99 |
– |
|
93 |
|
double * |
94 |
|
noise3(xnew) /* compute the noise function */ |
95 |
|
register double xnew[3]; |
146 |
|
|
147 |
|
|
148 |
|
double |
156 |
– |
l_hermite() /* library call for hermite interpolation */ |
157 |
– |
{ |
158 |
– |
double t; |
159 |
– |
|
160 |
– |
t = argument(5); |
161 |
– |
return( hermite(argument(1), argument(2), |
162 |
– |
argument(3), argument(4), t) ); |
163 |
– |
} |
164 |
– |
|
165 |
– |
|
166 |
– |
double |
149 |
|
fnoise3(p) /* compute fractal noise function */ |
150 |
< |
register double p[3]; |
150 |
> |
double p[3]; |
151 |
|
{ |
152 |
|
double floor(); |
153 |
< |
double v[3], beg[3], fval[8], s, fc; |
154 |
< |
int closing, branch; |
153 |
> |
long t[3], v[3], beg[3]; |
154 |
> |
double fval[8], fc; |
155 |
> |
int branch; |
156 |
> |
register long s; |
157 |
|
register int i, j; |
158 |
|
/* get starting cube */ |
159 |
< |
for (i = 0; i < 3; i++) |
160 |
< |
beg[i] = floor(p[i]); |
159 |
> |
s = (long)(1.0/EPSILON); |
160 |
> |
for (i = 0; i < 3; i++) { |
161 |
> |
t[i] = s*p[i]; |
162 |
> |
beg[i] = s*floor(p[i]); |
163 |
> |
} |
164 |
|
for (j = 0; j < 8; j++) { |
165 |
|
for (i = 0; i < 3; i++) { |
166 |
|
v[i] = beg[i]; |
167 |
|
if (j & 1<<i) |
168 |
< |
v[i] += 1.0; |
168 |
> |
v[i] += s; |
169 |
|
} |
170 |
|
fval[j] = frand3(v[0],v[1],v[2]); |
171 |
|
} |
185 |
– |
s = 1.0; |
172 |
|
/* compute fractal */ |
173 |
|
for ( ; ; ) { |
174 |
< |
s *= 0.5; |
174 |
> |
fc = 0.0; |
175 |
> |
for (j = 0; j < 8; j++) |
176 |
> |
fc += fval[j]; |
177 |
> |
fc *= 0.125; |
178 |
> |
if ((s >>= 1) == 0) |
179 |
> |
return(fc); /* close enough */ |
180 |
|
branch = 0; |
190 |
– |
closing = 0; |
181 |
|
for (i = 0; i < 3; i++) { /* do center */ |
182 |
|
v[i] = beg[i] + s; |
183 |
< |
if (p[i] > v[i]) { |
183 |
> |
if (t[i] > v[i]) { |
184 |
|
branch |= 1<<i; |
185 |
< |
if (p[i] - v[i] > EPSILON) |
196 |
< |
closing++; |
197 |
< |
} else if (v[i] - p[i] > EPSILON) |
198 |
< |
closing++; |
185 |
> |
} |
186 |
|
} |
187 |
< |
fc = 0.0; |
201 |
< |
for (j = 0; j < 8; j++) |
202 |
< |
fc += fval[j]; |
203 |
< |
fc = 0.125*fc + s*frand3(v[0],v[1],v[2]); |
204 |
< |
if (closing == 0) |
205 |
< |
return(fc); /* close enough */ |
187 |
> |
fc += s*EPSILON*frand3(v[0],v[1],v[2]); |
188 |
|
fval[~branch & 7] = fc; |
189 |
|
for (i = 0; i < 3; i++) { /* do faces */ |
190 |
|
if (branch & 1<<i) |
195 |
|
for (j = 0; j < 8; j++) |
196 |
|
if (~(j^branch) & 1<<i) |
197 |
|
fc += fval[j]; |
198 |
< |
fc = 0.25*fc + s*frand3(v[0],v[1],v[2]); |
198 |
> |
fc = 0.25*fc + s*EPSILON*frand3(v[0],v[1],v[2]); |
199 |
|
fval[~(branch^1<<i) & 7] = fc; |
200 |
|
v[i] = beg[i] + s; |
201 |
|
} |
212 |
|
v[j] -= s; |
213 |
|
fc = fval[branch & ~(1<<i)]; |
214 |
|
fc += fval[branch | 1<<i]; |
215 |
< |
fc = 0.5*fc + s*frand3(v[0],v[1],v[2]); |
215 |
> |
fc = 0.5*fc + s*EPSILON*frand3(v[0],v[1],v[2]); |
216 |
|
fval[branch^1<<i] = fc; |
217 |
|
j = (i+1)%3; |
218 |
|
v[j] = beg[j] + s; |