1 |
– |
/* Copyright (c) 1991 Regents of the University of California */ |
2 |
– |
|
1 |
|
#ifndef lint |
2 |
< |
static char SCCSid[] = "$SunId$ LBL"; |
2 |
> |
static const char RCSid[] = "$Id$"; |
3 |
|
#endif |
6 |
– |
|
4 |
|
/* |
5 |
|
* Routines to do the actual calculation for mkillum |
6 |
|
*/ |
7 |
|
|
8 |
< |
#include "mkillum.h" |
8 |
> |
#include <string.h> |
9 |
|
|
10 |
+ |
#include "mkillum.h" |
11 |
|
#include "face.h" |
14 |
– |
|
12 |
|
#include "cone.h" |
16 |
– |
|
13 |
|
#include "random.h" |
14 |
|
|
15 |
|
|
22 |
|
sprintf(errmsg, "(%s): cannot make illum for %s \"%s\"", |
23 |
|
nm, ofun[ob->otype].funame, ob->oname); |
24 |
|
error(WARNING, errmsg); |
25 |
< |
if (!(il->flags & IL_LIGHT)) |
30 |
< |
printobj(il->altmat, ob); |
25 |
> |
printobj(il->altmat, ob); |
26 |
|
} |
27 |
|
|
28 |
|
|
33 |
|
char *nm; |
34 |
|
{ |
35 |
|
#define MAXMISS (5*n*il->nsamps) |
36 |
< |
int dim[4]; |
37 |
< |
int n, nalt, nazi; |
36 |
> |
int dim[3]; |
37 |
> |
int n, nalt, nazi, h; |
38 |
|
float *distarr; |
39 |
< |
double r1, r2; |
39 |
> |
double sp[2], r1, r2; |
40 |
|
FVECT dn, org, dir; |
41 |
|
FVECT u, v; |
42 |
|
double ur[2], vr[2]; |
51 |
|
return; |
52 |
|
} |
53 |
|
/* set up sampling */ |
54 |
< |
n = PI * il->sampdens; |
55 |
< |
nalt = sqrt(n/PI) + .5; |
56 |
< |
nazi = PI*nalt + .5; |
54 |
> |
if (il->sampdens <= 0) |
55 |
> |
nalt = nazi = 1; |
56 |
> |
else { |
57 |
> |
n = PI * il->sampdens; |
58 |
> |
nalt = sqrt(n/PI) + .5; |
59 |
> |
nazi = PI*nalt + .5; |
60 |
> |
} |
61 |
|
n = nalt*nazi; |
62 |
|
distarr = (float *)calloc(n, 3*sizeof(float)); |
63 |
|
if (distarr == NULL) |
64 |
|
error(SYSTEM, "out of memory in o_face"); |
65 |
< |
mkaxes(u, v, fa->norm); |
65 |
> |
/* take first edge longer than sqrt(area) */ |
66 |
> |
for (j = fa->nv-1, i = 0; i < fa->nv; j = i++) { |
67 |
> |
u[0] = VERTEX(fa,i)[0] - VERTEX(fa,j)[0]; |
68 |
> |
u[1] = VERTEX(fa,i)[1] - VERTEX(fa,j)[1]; |
69 |
> |
u[2] = VERTEX(fa,i)[2] - VERTEX(fa,j)[2]; |
70 |
> |
if ((r1 = DOT(u,u)) >= fa->area-FTINY) |
71 |
> |
break; |
72 |
> |
} |
73 |
> |
if (i < fa->nv) { /* got one! -- let's align our axes */ |
74 |
> |
r2 = 1.0/sqrt(r1); |
75 |
> |
u[0] *= r2; u[1] *= r2; u[2] *= r2; |
76 |
> |
fcross(v, fa->norm, u); |
77 |
> |
} else /* oh well, we'll just have to wing it */ |
78 |
> |
mkaxes(u, v, fa->norm); |
79 |
> |
/* now, find limits in (u,v) coordinates */ |
80 |
|
ur[0] = vr[0] = FHUGE; |
81 |
|
ur[1] = vr[1] = -FHUGE; |
82 |
|
for (i = 0; i < fa->nv; i++) { |
94 |
|
for (dim[2] = 0; dim[2] < nazi; dim[2]++) |
95 |
|
for (i = 0; i < il->nsamps; i++) { |
96 |
|
/* random direction */ |
97 |
< |
dim[3] = 1; |
98 |
< |
r1 = (dim[1]+urand(urind(ilhash(dim,4),i)))/nalt; |
99 |
< |
dim[3] = 2; |
100 |
< |
r2 = (dim[2]+urand(urind(ilhash(dim,4),i)))/nalt; |
97 |
> |
h = ilhash(dim, 3) + i; |
98 |
> |
multisamp(sp, 2, urand(h)); |
99 |
> |
r1 = (dim[1] + sp[0])/nalt; |
100 |
> |
r2 = (dim[2] + sp[1] - .5)/nazi; |
101 |
|
flatdir(dn, r1, r2); |
102 |
|
for (j = 0; j < 3; j++) |
103 |
< |
dir[j] = dn[0]*u[j] + dn[1]*v[j] - dn[2]*fa->norm[j]; |
103 |
> |
dir[j] = -dn[0]*u[j] - dn[1]*v[j] - dn[2]*fa->norm[j]; |
104 |
|
/* random location */ |
105 |
|
do { |
106 |
< |
dim[3] = 3; |
107 |
< |
r1 = ur[0] + (ur[1]-ur[0]) * |
108 |
< |
urand(urind(ilhash(dim,4),i+nmisses)); |
96 |
< |
dim[3] = 4; |
97 |
< |
r2 = vr[0] + (vr[1]-vr[0]) * |
98 |
< |
urand(urind(ilhash(dim,4),i+nmisses)); |
106 |
> |
multisamp(sp, 2, urand(h+4862+nmisses)); |
107 |
> |
r1 = ur[0] + (ur[1]-ur[0]) * sp[0]; |
108 |
> |
r2 = vr[0] + (vr[1]-vr[0]) * sp[1]; |
109 |
|
for (j = 0; j < 3; j++) |
110 |
|
org[j] = r1*u[j] + r2*v[j] |
111 |
|
+ fa->offset*fa->norm[j]; |
114 |
|
objerror(ob, WARNING, "bad aspect"); |
115 |
|
rt->nrays = 0; |
116 |
|
freeface(ob); |
117 |
< |
free((char *)distarr); |
117 |
> |
free((void *)distarr); |
118 |
|
o_default(ob, il, rt, nm); |
119 |
|
return; |
120 |
|
} |
121 |
|
for (j = 0; j < 3; j++) |
122 |
|
org[j] += .001*fa->norm[j]; |
123 |
|
/* send sample */ |
124 |
< |
raysamp(distarr+dim[1]*nazi+dim[2], org, dir, rt); |
124 |
> |
raysamp(distarr+3*(dim[1]*nazi+dim[2]), org, dir, rt); |
125 |
|
} |
126 |
|
rayflush(rt); |
127 |
< |
/* write out the face w/ distribution */ |
128 |
< |
flatout(il, distarr, nalt, nazi, u, v, fa->norm); |
129 |
< |
illumout(il, ob); |
127 |
> |
/* write out the face and its distribution */ |
128 |
> |
if (average(il, distarr, nalt*nazi)) { |
129 |
> |
if (il->sampdens > 0) |
130 |
> |
flatout(il, distarr, nalt, nazi, u, v, fa->norm); |
131 |
> |
illumout(il, ob); |
132 |
> |
} else |
133 |
> |
printobj(il->altmat, ob); |
134 |
|
/* clean up */ |
135 |
|
freeface(ob); |
136 |
< |
free((char *)distarr); |
136 |
> |
free((void *)distarr); |
137 |
|
#undef MAXMISS |
138 |
|
} |
139 |
|
|
144 |
|
struct rtproc *rt; |
145 |
|
char *nm; |
146 |
|
{ |
147 |
< |
int dim[4]; |
147 |
> |
int dim[3]; |
148 |
|
int n, nalt, nazi; |
149 |
|
float *distarr; |
150 |
< |
double r1, r2; |
150 |
> |
double sp[4], r1, r2, r3; |
151 |
|
FVECT org, dir; |
152 |
|
FVECT u, v; |
153 |
|
register int i, j; |
155 |
|
if (ob->oargs.nfargs != 4) |
156 |
|
objerror(ob, USER, "bad # of arguments"); |
157 |
|
/* set up sampling */ |
158 |
< |
n = 4.*PI * il->sampdens; |
159 |
< |
nalt = sqrt(n/PI) + .5; |
160 |
< |
nazi = PI*nalt + .5; |
158 |
> |
if (il->sampdens <= 0) |
159 |
> |
nalt = nazi = 1; |
160 |
> |
else { |
161 |
> |
n = 4.*PI * il->sampdens; |
162 |
> |
nalt = sqrt(2./PI*n) + .5; |
163 |
> |
nazi = PI/2.*nalt + .5; |
164 |
> |
} |
165 |
|
n = nalt*nazi; |
166 |
|
distarr = (float *)calloc(n, 3*sizeof(float)); |
167 |
|
if (distarr == NULL) |
171 |
|
for (dim[1] = 0; dim[1] < nalt; dim[1]++) |
172 |
|
for (dim[2] = 0; dim[2] < nazi; dim[2]++) |
173 |
|
for (i = 0; i < il->nsamps; i++) { |
174 |
+ |
/* next sample point */ |
175 |
+ |
multisamp(sp, 4, urand(ilhash(dim,3)+i)); |
176 |
|
/* random direction */ |
177 |
< |
dim[3] = 1; |
178 |
< |
r1 = (dim[1]+urand(urind(ilhash(dim,4),i)))/nalt; |
159 |
< |
dim[3] = 2; |
160 |
< |
r2 = (dim[2]+urand(urind(ilhash(dim,4),i)))/nalt; |
177 |
> |
r1 = (dim[1] + sp[0])/nalt; |
178 |
> |
r2 = (dim[2] + sp[1] - .5)/nazi; |
179 |
|
rounddir(dir, r1, r2); |
180 |
|
/* random location */ |
181 |
|
mkaxes(u, v, dir); /* yuck! */ |
182 |
< |
dim[3] = 3; |
183 |
< |
r1 = sqrt(urand(urind(ilhash(dim,4),i))); |
184 |
< |
dim[3] = 4; |
185 |
< |
r2 = 2.*PI*urand(urind(ilhash(dim,4),i)); |
186 |
< |
for (j = 0; j < 3; j++) |
187 |
< |
org[j] = ob->oargs.farg[j] + ob->oargs.farg[3] * |
188 |
< |
( r1*cos(r2)*u[j] + r1*sin(r2)*v[j] |
189 |
< |
- sqrt(1.01-r1*r1)*dir[j] ); |
190 |
< |
|
182 |
> |
r3 = sqrt(sp[2]); |
183 |
> |
r2 = 2.*PI*sp[3]; |
184 |
> |
r1 = r3*ob->oargs.farg[3]*cos(r2); |
185 |
> |
r2 = r3*ob->oargs.farg[3]*sin(r2); |
186 |
> |
r3 = ob->oargs.farg[3]*sqrt(1.01-r3*r3); |
187 |
> |
for (j = 0; j < 3; j++) { |
188 |
> |
org[j] = ob->oargs.farg[j] + r1*u[j] + r2*v[j] + |
189 |
> |
r3*dir[j]; |
190 |
> |
dir[j] = -dir[j]; |
191 |
> |
} |
192 |
|
/* send sample */ |
193 |
< |
raysamp(distarr+dim[1]*nazi+dim[2], org, dir, rt); |
193 |
> |
raysamp(distarr+3*(dim[1]*nazi+dim[2]), org, dir, rt); |
194 |
|
} |
195 |
|
rayflush(rt); |
196 |
< |
/* write out the sphere w/ distribution */ |
197 |
< |
roundout(il, distarr, nalt, nazi); |
198 |
< |
illumout(il, ob); |
196 |
> |
/* write out the sphere and its distribution */ |
197 |
> |
if (average(il, distarr, nalt*nazi)) { |
198 |
> |
if (il->sampdens > 0) |
199 |
> |
roundout(il, distarr, nalt, nazi); |
200 |
> |
else |
201 |
> |
objerror(ob, WARNING, "diffuse distribution"); |
202 |
> |
illumout(il, ob); |
203 |
> |
} else |
204 |
> |
printobj(il->altmat, ob); |
205 |
|
/* clean up */ |
206 |
< |
free((char *)distarr); |
206 |
> |
free((void *)distarr); |
207 |
|
} |
208 |
|
|
209 |
|
|
213 |
|
struct rtproc *rt; |
214 |
|
char *nm; |
215 |
|
{ |
216 |
< |
int dim[4]; |
216 |
> |
int dim[3]; |
217 |
|
int n, nalt, nazi; |
218 |
|
float *distarr; |
219 |
< |
double r1, r2; |
219 |
> |
double sp[4], r1, r2, r3; |
220 |
|
FVECT dn, org, dir; |
221 |
|
FVECT u, v; |
222 |
|
register CONE *co; |
224 |
|
/* get/check arguments */ |
225 |
|
co = getcone(ob, 0); |
226 |
|
/* set up sampling */ |
227 |
< |
n = PI * il->sampdens; |
228 |
< |
nalt = sqrt(n/PI) + .5; |
229 |
< |
nazi = PI*nalt + .5; |
227 |
> |
if (il->sampdens <= 0) |
228 |
> |
nalt = nazi = 1; |
229 |
> |
else { |
230 |
> |
n = PI * il->sampdens; |
231 |
> |
nalt = sqrt(n/PI) + .5; |
232 |
> |
nazi = PI*nalt + .5; |
233 |
> |
} |
234 |
|
n = nalt*nazi; |
235 |
|
distarr = (float *)calloc(n, 3*sizeof(float)); |
236 |
|
if (distarr == NULL) |
241 |
|
for (dim[1] = 0; dim[1] < nalt; dim[1]++) |
242 |
|
for (dim[2] = 0; dim[2] < nazi; dim[2]++) |
243 |
|
for (i = 0; i < il->nsamps; i++) { |
244 |
+ |
/* next sample point */ |
245 |
+ |
multisamp(sp, 4, urand(ilhash(dim,3)+i)); |
246 |
|
/* random direction */ |
247 |
< |
dim[3] = 1; |
248 |
< |
r1 = (dim[1]+urand(urind(ilhash(dim,4),i)))/nalt; |
218 |
< |
dim[3] = 2; |
219 |
< |
r2 = (dim[2]+urand(urind(ilhash(dim,4),i)))/nalt; |
247 |
> |
r1 = (dim[1] + sp[0])/nalt; |
248 |
> |
r2 = (dim[2] + sp[1] - .5)/nazi; |
249 |
|
flatdir(dn, r1, r2); |
250 |
|
for (j = 0; j < 3; j++) |
251 |
< |
dir[j] = dn[0]*u[j] + dn[1]*v[j] - dn[2]*co->ad[j]; |
251 |
> |
dir[j] = -dn[0]*u[j] - dn[1]*v[j] - dn[2]*co->ad[j]; |
252 |
|
/* random location */ |
253 |
< |
dim[3] = 3; |
254 |
< |
r1 = sqrt(CO_R0(co)*CO_R0(co) + |
255 |
< |
urand(urind(ilhash(dim,4),i))* |
256 |
< |
(CO_R1(co)*CO_R1(co) - CO_R0(co)*CO_R0(co))); |
257 |
< |
dim[3] = 4; |
229 |
< |
r2 = 2.*PI*urand(urind(ilhash(dim,4),i)); |
253 |
> |
r3 = sqrt(CO_R0(co)*CO_R0(co) + |
254 |
> |
sp[2]*(CO_R1(co)*CO_R1(co) - CO_R0(co)*CO_R0(co))); |
255 |
> |
r2 = 2.*PI*sp[3]; |
256 |
> |
r1 = r3*cos(r2); |
257 |
> |
r2 = r3*sin(r2); |
258 |
|
for (j = 0; j < 3; j++) |
259 |
< |
org[j] = CO_P0(co)[j] + |
260 |
< |
r1*cos(r2)*u[j] + r1*sin(r2)*v[j] |
233 |
< |
+ .001*co->ad[j]; |
259 |
> |
org[j] = CO_P0(co)[j] + r1*u[j] + r2*v[j] + |
260 |
> |
.001*co->ad[j]; |
261 |
|
|
262 |
|
/* send sample */ |
263 |
< |
raysamp(distarr+dim[1]*nazi+dim[2], org, dir, rt); |
263 |
> |
raysamp(distarr+3*(dim[1]*nazi+dim[2]), org, dir, rt); |
264 |
|
} |
265 |
|
rayflush(rt); |
266 |
< |
/* write out the ring w/ distribution */ |
267 |
< |
flatout(il, distarr, nalt, nazi, u, v, co->ad); |
268 |
< |
illumout(il, ob); |
266 |
> |
/* write out the ring and its distribution */ |
267 |
> |
if (average(il, distarr, nalt*nazi)) { |
268 |
> |
if (il->sampdens > 0) |
269 |
> |
flatout(il, distarr, nalt, nazi, u, v, co->ad); |
270 |
> |
illumout(il, ob); |
271 |
> |
} else |
272 |
> |
printobj(il->altmat, ob); |
273 |
|
/* clean up */ |
274 |
|
freecone(ob); |
275 |
< |
free((char *)distarr); |
275 |
> |
free((void *)distarr); |
276 |
|
} |
277 |
|
|
278 |
|
|
299 |
|
|
300 |
|
if (rt->nrays <= 0) |
301 |
|
return; |
302 |
< |
i = 6*rt->nrays + 3; |
303 |
< |
rt->buf[i++] = 0.; rt->buf[i++] = 0.; rt->buf[i] = 0.; |
304 |
< |
if ( process(rt->pd, (char *)rt->buf, (char *)rt->buf, |
305 |
< |
3*sizeof(float)*rt->nrays, |
302 |
> |
memset(rt->buf+6*rt->nrays, '\0', 6*sizeof(float)); |
303 |
> |
errno = 0; |
304 |
> |
if ( process(&(rt->pd), (char *)rt->buf, (char *)rt->buf, |
305 |
> |
3*sizeof(float)*(rt->nrays+1), |
306 |
|
6*sizeof(float)*(rt->nrays+1)) < |
307 |
< |
3*sizeof(float)*rt->nrays ) |
307 |
> |
3*sizeof(float)*(rt->nrays+1) ) |
308 |
|
error(SYSTEM, "error reading from rtrace process"); |
309 |
|
i = rt->nrays; |
310 |
|
while (i--) { |
356 |
|
d2 = 2.*PI * azi; |
357 |
|
dv[0] = d1*cos(d2); |
358 |
|
dv[1] = d1*sin(d2); |
359 |
< |
dv[2] = sqrt(1. - d1*d1); |
359 |
> |
dv[2] = sqrt(1. - alt); |
360 |
|
} |