1 |
#ifndef lint |
2 |
static const char RCSid[] = "$Id: rfluxmtx.c,v 2.52 2020/09/07 04:06:17 greg Exp $"; |
3 |
#endif |
4 |
/* |
5 |
* Calculate flux transfer matrix or matrices using rcontrib |
6 |
*/ |
7 |
|
8 |
#include "copyright.h" |
9 |
|
10 |
#include <ctype.h> |
11 |
#include <stdlib.h> |
12 |
#include "rtio.h" |
13 |
#include "rtmath.h" |
14 |
#include "rtprocess.h" |
15 |
#include "bsdf.h" |
16 |
#include "bsdf_m.h" |
17 |
#include "random.h" |
18 |
#include "triangulate.h" |
19 |
#include "platform.h" |
20 |
|
21 |
#ifndef MAXRCARG |
22 |
#define MAXRCARG 10000 |
23 |
#endif |
24 |
|
25 |
char *progname; /* global argv[0] */ |
26 |
|
27 |
int verbose = 0; /* verbose mode (< 0 no warnings) */ |
28 |
|
29 |
char *rcarg[MAXRCARG+1] = {"rcontrib", "-fo+"}; |
30 |
int nrcargs = 2; |
31 |
|
32 |
const char overflowerr[] = "%s: too many arguments!\n"; |
33 |
|
34 |
#define CHECKARGC(n) if (nrcargs >= MAXRCARG-(n)) \ |
35 |
{ fprintf(stderr, overflowerr, progname); exit(1); } |
36 |
|
37 |
int sampcnt = 0; /* sample count (0==unset) */ |
38 |
|
39 |
char *reinhfn = "reinhartb.cal"; |
40 |
char *shirchiufn = "disk2square.cal"; |
41 |
char *kfullfn = "klems_full.cal"; |
42 |
char *khalffn = "klems_half.cal"; |
43 |
char *kquarterfn = "klems_quarter.cal"; |
44 |
|
45 |
/* string indicating parameters */ |
46 |
const char PARAMSTART[] = "@rfluxmtx"; |
47 |
|
48 |
/* surface type IDs */ |
49 |
#define ST_NONE 0 |
50 |
#define ST_POLY 1 |
51 |
#define ST_RING 2 |
52 |
#define ST_SOURCE 3 |
53 |
|
54 |
typedef struct surf_s { |
55 |
struct surf_s *next; /* next surface in list */ |
56 |
void *priv; /* private data (malloc'ed) */ |
57 |
char sname[32]; /* surface name */ |
58 |
FVECT snrm; /* surface normal */ |
59 |
double area; /* surface area / proj. solid angle */ |
60 |
short styp; /* surface type */ |
61 |
short nfargs; /* number of real arguments */ |
62 |
double farg[1]; /* real values (extends struct) */ |
63 |
} SURF; /* surface structure */ |
64 |
|
65 |
typedef struct { |
66 |
FVECT uva[2]; /* tangent axes */ |
67 |
int ntris; /* number of triangles */ |
68 |
struct ptri { |
69 |
double afrac; /* fraction of total area */ |
70 |
short vndx[3]; /* vertex indices */ |
71 |
} tri[1]; /* triangle array (extends struct) */ |
72 |
} POLYTRIS; /* triangulated polygon */ |
73 |
|
74 |
typedef struct param_s { |
75 |
char sign; /* '-' for axis reversal */ |
76 |
char hemis[31]; /* hemispherical sampling spec. */ |
77 |
int hsiz; /* hemisphere basis size */ |
78 |
int nsurfs; /* number of surfaces */ |
79 |
SURF *slist; /* list of surfaces */ |
80 |
FVECT vup; /* up vector (zero if unset) */ |
81 |
FVECT nrm; /* average normal direction */ |
82 |
FVECT udir, vdir; /* tangent axes */ |
83 |
char *outfn; /* output file name (receiver) */ |
84 |
int (*sample_basis)(struct param_s *p, int, FILE *); |
85 |
} PARAMS; /* sender/receiver parameters */ |
86 |
|
87 |
PARAMS curparams; |
88 |
char curmod[128]; |
89 |
char newparams[1024]; |
90 |
|
91 |
typedef int SURFSAMP(FVECT, SURF *, double); |
92 |
|
93 |
static SURFSAMP ssamp_bad, ssamp_poly, ssamp_ring; |
94 |
|
95 |
SURFSAMP *orig_in_surf[4] = { |
96 |
ssamp_bad, ssamp_poly, ssamp_ring, ssamp_bad |
97 |
}; |
98 |
|
99 |
/* Clear parameter set */ |
100 |
static void |
101 |
clear_params(PARAMS *p, int reset_only) |
102 |
{ |
103 |
while (p->slist != NULL) { |
104 |
SURF *sdel = p->slist; |
105 |
p->slist = sdel->next; |
106 |
if (sdel->priv != NULL) |
107 |
free(sdel->priv); |
108 |
free(sdel); |
109 |
} |
110 |
if (reset_only) { |
111 |
p->nsurfs = 0; |
112 |
memset(p->nrm, 0, sizeof(FVECT)); |
113 |
memset(p->vup, 0, sizeof(FVECT)); |
114 |
p->outfn = NULL; |
115 |
return; |
116 |
} |
117 |
memset(p, 0, sizeof(PARAMS)); |
118 |
} |
119 |
|
120 |
/* Get surface type from name */ |
121 |
static int |
122 |
surf_type(const char *otype) |
123 |
{ |
124 |
if (!strcmp(otype, "polygon")) |
125 |
return(ST_POLY); |
126 |
if (!strcmp(otype, "ring")) |
127 |
return(ST_RING); |
128 |
if (!strcmp(otype, "source")) |
129 |
return(ST_SOURCE); |
130 |
return(ST_NONE); |
131 |
} |
132 |
|
133 |
/* Add arguments to oconv command */ |
134 |
static char * |
135 |
oconv_command(int ac, char *av[]) |
136 |
{ |
137 |
static char oconvbuf[2048] = "!oconv -f "; |
138 |
char *cp = oconvbuf + 10; |
139 |
char *recv = *av++; |
140 |
|
141 |
if (ac-- <= 0) |
142 |
return(NULL); |
143 |
if (verbose < 0) { /* turn off warnings */ |
144 |
strcpy(cp, "-w "); |
145 |
cp += 3; |
146 |
} |
147 |
while (ac-- > 0) { /* copy each argument */ |
148 |
int len = strlen(*av); |
149 |
if (cp+len+4 >= oconvbuf+sizeof(oconvbuf)) |
150 |
goto overrun; |
151 |
if (matchany(*av, SPECIALS)) { |
152 |
*cp++ = QUOTCHAR; |
153 |
strcpy(cp, *av++); |
154 |
cp += len; |
155 |
*cp++ = QUOTCHAR; |
156 |
} else { |
157 |
strcpy(cp, *av++); |
158 |
cp += len; |
159 |
} |
160 |
*cp++ = ' '; |
161 |
} |
162 |
/* receiver goes last */ |
163 |
if (matchany(recv, SPECIALS)) { |
164 |
*cp++ = QUOTCHAR; |
165 |
while (*recv) { |
166 |
if (cp >= oconvbuf+(sizeof(oconvbuf)-3)) |
167 |
goto overrun; |
168 |
*cp++ = *recv++; |
169 |
} |
170 |
*cp++ = QUOTCHAR; |
171 |
*cp = '\0'; |
172 |
} else |
173 |
strcpy(cp, recv); |
174 |
return(oconvbuf); |
175 |
overrun: |
176 |
fputs(progname, stderr); |
177 |
fputs(": too many file arguments!\n", stderr); |
178 |
exit(1); |
179 |
} |
180 |
|
181 |
#if defined(_WIN32) || defined(_WIN64) |
182 |
|
183 |
/* Open a pipe to/from a command given as an argument list */ |
184 |
static FILE * |
185 |
popen_arglist(char *av[], char *mode) |
186 |
{ |
187 |
char cmd[10240]; |
188 |
|
189 |
if (!convert_commandline(cmd, sizeof(cmd), av)) { |
190 |
fputs(progname, stderr); |
191 |
fputs(": command line too long in popen_arglist()\n", stderr); |
192 |
return(NULL); |
193 |
} |
194 |
if (verbose > 0) |
195 |
fprintf(stderr, "%s: opening pipe %s: %s\n", |
196 |
progname, (*mode=='w') ? "to" : "from", cmd); |
197 |
return(popen(cmd, mode)); |
198 |
} |
199 |
|
200 |
#define pclose_al pclose |
201 |
|
202 |
/* Execute system command (Windows version) */ |
203 |
static int |
204 |
my_exec(char *av[]) |
205 |
{ |
206 |
char cmd[10240]; |
207 |
|
208 |
if (!convert_commandline(cmd, sizeof(cmd), av)) { |
209 |
fputs(progname, stderr); |
210 |
fputs(": command line too long in my_exec()\n", stderr); |
211 |
return(1); |
212 |
} |
213 |
if (verbose > 0) |
214 |
fprintf(stderr, "%s: running: %s\n", progname, cmd); |
215 |
return(system(cmd)); |
216 |
} |
217 |
|
218 |
#else /* UNIX */ |
219 |
|
220 |
static SUBPROC rt_proc = SP_INACTIVE; /* we only support one of these */ |
221 |
|
222 |
/* Open a pipe to a command using an argument list */ |
223 |
static FILE * |
224 |
popen_arglist(char *av[], char *mode) |
225 |
{ |
226 |
int fd; |
227 |
|
228 |
if (rt_proc.pid > 0) { |
229 |
fprintf(stderr, "%s: only one i/o pipe at a time!\n", progname); |
230 |
return(NULL); |
231 |
} |
232 |
if (verbose > 0) { |
233 |
char cmd[4096]; |
234 |
if (!convert_commandline(cmd, sizeof(cmd), av)) |
235 |
strcpy(cmd, "COMMAND TOO LONG TO SHOW"); |
236 |
fprintf(stderr, "%s: opening pipe %s: %s\n", |
237 |
progname, (*mode=='w') ? "to" : "from", cmd); |
238 |
} |
239 |
if (*mode == 'w') { |
240 |
fd = rt_proc.w = dup(fileno(stdout)); |
241 |
rt_proc.flags |= PF_FILT_OUT; |
242 |
} else if (*mode == 'r') { |
243 |
fd = rt_proc.r = dup(fileno(stdin)); |
244 |
rt_proc.flags |= PF_FILT_INP; |
245 |
} |
246 |
if (fd < 0 || open_process(&rt_proc, av) <= 0) { |
247 |
perror(av[0]); |
248 |
return(NULL); |
249 |
} |
250 |
return(fdopen(fd, mode)); |
251 |
} |
252 |
|
253 |
/* Close command pipe (returns -1 on error to match pclose) */ |
254 |
static int |
255 |
pclose_al(FILE *fp) |
256 |
{ |
257 |
int prob = (fclose(fp) == EOF); |
258 |
|
259 |
if (rt_proc.pid <= 0) |
260 |
return(-1); |
261 |
|
262 |
prob |= (close_process(&rt_proc) != 0); |
263 |
|
264 |
return(-prob); |
265 |
} |
266 |
|
267 |
/* Execute system command in our stead (Unix version) */ |
268 |
static int |
269 |
my_exec(char *av[]) |
270 |
{ |
271 |
char *compath; |
272 |
|
273 |
if ((compath = getpath((char *)av[0], getenv("PATH"), X_OK)) == NULL) { |
274 |
fprintf(stderr, "%s: cannot locate %s\n", progname, av[0]); |
275 |
return(1); |
276 |
} |
277 |
if (verbose > 0) { |
278 |
char cmd[4096]; |
279 |
if (!convert_commandline(cmd, sizeof(cmd), av)) |
280 |
strcpy(cmd, "COMMAND TOO LONG TO SHOW"); |
281 |
fprintf(stderr, "%s: running: %s\n", progname, cmd); |
282 |
} |
283 |
execv(compath, av); /* successful call never returns */ |
284 |
perror(compath); |
285 |
return(1); |
286 |
} |
287 |
|
288 |
#endif |
289 |
|
290 |
/* Get normalized direction vector from string specification */ |
291 |
static int |
292 |
get_direction(FVECT dv, const char *s) |
293 |
{ |
294 |
int sign = 1; |
295 |
int axis = 0; |
296 |
|
297 |
memset(dv, 0, sizeof(FVECT)); |
298 |
nextchar: |
299 |
switch (*s) { |
300 |
case '+': |
301 |
++s; |
302 |
goto nextchar; |
303 |
case '-': |
304 |
sign = -sign; |
305 |
++s; |
306 |
goto nextchar; |
307 |
case 'z': |
308 |
case 'Z': |
309 |
++axis; |
310 |
case 'y': |
311 |
case 'Y': |
312 |
++axis; |
313 |
case 'x': |
314 |
case 'X': |
315 |
dv[axis] = sign; |
316 |
return(!s[1] | isspace(s[1])); |
317 |
default: |
318 |
break; |
319 |
} |
320 |
#ifdef SMLFLT |
321 |
if (sscanf(s, "%f,%f,%f", &dv[0], &dv[1], &dv[2]) != 3) |
322 |
#else |
323 |
if (sscanf(s, "%lf,%lf,%lf", &dv[0], &dv[1], &dv[2]) != 3) |
324 |
#endif |
325 |
return(0); |
326 |
dv[0] *= (RREAL)sign; |
327 |
return(normalize(dv) > 0); |
328 |
} |
329 |
|
330 |
/* Parse program parameters (directives) */ |
331 |
static int |
332 |
parse_params(PARAMS *p, char *pargs) |
333 |
{ |
334 |
char *cp = pargs; |
335 |
int nparams = 0; |
336 |
int quot; |
337 |
int i; |
338 |
|
339 |
for ( ; ; ) { |
340 |
switch (*cp++) { |
341 |
case 'h': |
342 |
if (*cp++ != '=') |
343 |
break; |
344 |
if ((*cp == '+') | (*cp == '-')) |
345 |
p->sign = *cp++; |
346 |
else |
347 |
p->sign = '+'; |
348 |
p->hsiz = 0; |
349 |
i = 0; |
350 |
while (*cp && !isspace(*cp)) { |
351 |
if (isdigit(*cp)) |
352 |
p->hsiz = 10*p->hsiz + *cp - '0'; |
353 |
p->hemis[i++] = *cp++; |
354 |
} |
355 |
if (!i) |
356 |
break; |
357 |
p->hemis[i] = '\0'; |
358 |
p->hsiz += !p->hsiz; |
359 |
++nparams; |
360 |
continue; |
361 |
case 'u': |
362 |
if (*cp++ != '=') |
363 |
break; |
364 |
if (!get_direction(p->vup, cp)) |
365 |
break; |
366 |
while (*cp && !isspace(*cp++)) |
367 |
; |
368 |
++nparams; |
369 |
continue; |
370 |
case 'o': |
371 |
if (*cp++ != '=') |
372 |
break; |
373 |
quot = 0; |
374 |
if ((*cp == '"') | (*cp == '\'')) |
375 |
quot = *cp++; |
376 |
i = 0; |
377 |
while (*cp && (quot ? (*cp != quot) : !isspace(*cp))) { |
378 |
i++; cp++; |
379 |
} |
380 |
if (!i) |
381 |
break; |
382 |
if (!*cp) { |
383 |
if (quot) |
384 |
break; |
385 |
cp[1] = '\0'; |
386 |
} |
387 |
*cp = '\0'; |
388 |
p->outfn = savqstr(cp-i); |
389 |
*cp++ = quot ? quot : ' '; |
390 |
++nparams; |
391 |
continue; |
392 |
case ' ': |
393 |
case '\t': |
394 |
case '\r': |
395 |
case '\n': |
396 |
continue; |
397 |
case '\0': |
398 |
return(nparams); |
399 |
default: |
400 |
break; |
401 |
} |
402 |
break; |
403 |
} |
404 |
fprintf(stderr, "%s: bad parameter string: %s", progname, pargs); |
405 |
exit(1); |
406 |
return(-1); /* pro forma return */ |
407 |
} |
408 |
|
409 |
/* Add receiver arguments (directives) corresponding to the current modifier */ |
410 |
static void |
411 |
finish_receiver(void) |
412 |
{ |
413 |
char sbuf[256]; |
414 |
int uniform = 0; |
415 |
char *calfn = NULL; |
416 |
char *params = NULL; |
417 |
char *binv = NULL; |
418 |
char *binf = NULL; |
419 |
char *nbins = NULL; |
420 |
|
421 |
if (!curmod[0]) { |
422 |
fputs(progname, stderr); |
423 |
fputs(": missing receiver surface!\n", stderr); |
424 |
exit(1); |
425 |
} |
426 |
if (curparams.outfn != NULL) { /* add output file spec. */ |
427 |
CHECKARGC(2); |
428 |
rcarg[nrcargs++] = "-o"; |
429 |
rcarg[nrcargs++] = curparams.outfn; |
430 |
} |
431 |
/* check arguments */ |
432 |
if (!curparams.hemis[0]) { |
433 |
fputs(progname, stderr); |
434 |
fputs(": missing hemisphere sampling type!\n", stderr); |
435 |
exit(1); |
436 |
} |
437 |
if (normalize(curparams.nrm) == 0) { |
438 |
fputs(progname, stderr); |
439 |
fputs(": undefined normal for hemisphere sampling\n", stderr); |
440 |
exit(1); |
441 |
} |
442 |
if (normalize(curparams.vup) == 0) { |
443 |
if (fabs(curparams.nrm[2]) < .7) |
444 |
curparams.vup[2] = 1; |
445 |
else |
446 |
curparams.vup[1] = 1; |
447 |
} |
448 |
/* determine sample type/bin */ |
449 |
if ((tolower(curparams.hemis[0]) == 'u') | (curparams.hemis[0] == '1')) { |
450 |
sprintf(sbuf, "if(-Dx*%g-Dy*%g-Dz*%g,0,-1)", |
451 |
curparams.nrm[0], curparams.nrm[1], curparams.nrm[2]); |
452 |
binv = savqstr(sbuf); |
453 |
nbins = "1"; /* uniform sampling -- one bin */ |
454 |
uniform = 1; |
455 |
} else if (tolower(curparams.hemis[0]) == 's' && |
456 |
tolower(curparams.hemis[1]) == 'c') { |
457 |
/* assign parameters */ |
458 |
if (curparams.hsiz <= 1) { |
459 |
fputs(progname, stderr); |
460 |
fputs(": missing size for Shirley-Chiu sampling!\n", stderr); |
461 |
exit(1); |
462 |
} |
463 |
calfn = shirchiufn; shirchiufn = NULL; |
464 |
sprintf(sbuf, "SCdim=%d,rNx=%g,rNy=%g,rNz=%g,Ux=%g,Uy=%g,Uz=%g,RHS=%c1", |
465 |
curparams.hsiz, |
466 |
curparams.nrm[0], curparams.nrm[1], curparams.nrm[2], |
467 |
curparams.vup[0], curparams.vup[1], curparams.vup[2], |
468 |
curparams.sign); |
469 |
params = savqstr(sbuf); |
470 |
binv = "scbin"; |
471 |
nbins = "SCdim*SCdim"; |
472 |
} else if ((tolower(curparams.hemis[0]) == 'r') | |
473 |
(tolower(curparams.hemis[0]) == 't')) { |
474 |
calfn = reinhfn; reinhfn = NULL; |
475 |
sprintf(sbuf, "MF=%d,rNx=%g,rNy=%g,rNz=%g,Ux=%g,Uy=%g,Uz=%g,RHS=%c1", |
476 |
curparams.hsiz, |
477 |
curparams.nrm[0], curparams.nrm[1], curparams.nrm[2], |
478 |
curparams.vup[0], curparams.vup[1], curparams.vup[2], |
479 |
curparams.sign); |
480 |
params = savqstr(sbuf); |
481 |
binv = "rbin"; |
482 |
nbins = "Nrbins"; |
483 |
} else if (tolower(curparams.hemis[0]) == 'k' && |
484 |
!curparams.hemis[1] | |
485 |
(tolower(curparams.hemis[1]) == 'f') | |
486 |
(curparams.hemis[1] == '1')) { |
487 |
calfn = kfullfn; kfullfn = NULL; |
488 |
binf = "kbin"; |
489 |
nbins = "Nkbins"; |
490 |
} else if (tolower(curparams.hemis[0]) == 'k' && |
491 |
(tolower(curparams.hemis[1]) == 'h') | |
492 |
(curparams.hemis[1] == '2')) { |
493 |
calfn = khalffn; khalffn = NULL; |
494 |
binf = "khbin"; |
495 |
nbins = "Nkhbins"; |
496 |
} else if (tolower(curparams.hemis[0]) == 'k' && |
497 |
(tolower(curparams.hemis[1]) == 'q') | |
498 |
(curparams.hemis[1] == '4')) { |
499 |
calfn = kquarterfn; kquarterfn = NULL; |
500 |
binf = "kqbin"; |
501 |
nbins = "Nkqbins"; |
502 |
} else { |
503 |
fprintf(stderr, "%s: unrecognized hemisphere sampling: h=%s\n", |
504 |
progname, curparams.hemis); |
505 |
exit(1); |
506 |
} |
507 |
if (tolower(curparams.hemis[0]) == 'k') { |
508 |
sprintf(sbuf, "RHS=%c1", curparams.sign); |
509 |
params = savqstr(sbuf); |
510 |
} |
511 |
if (!uniform & (curparams.slist->styp == ST_SOURCE)) { |
512 |
SURF *sp; |
513 |
for (sp = curparams.slist; sp != NULL; sp = sp->next) |
514 |
if (fabs(sp->area - PI) > 1e-3) { |
515 |
fprintf(stderr, "%s: source '%s' must be 180-degrees\n", |
516 |
progname, sp->sname); |
517 |
exit(1); |
518 |
} |
519 |
} |
520 |
if (calfn != NULL) { /* add cal file if needed */ |
521 |
CHECKARGC(2); |
522 |
rcarg[nrcargs++] = "-f"; |
523 |
rcarg[nrcargs++] = calfn; |
524 |
} |
525 |
if (params != NULL) { /* parameters _after_ cal file */ |
526 |
CHECKARGC(2); |
527 |
rcarg[nrcargs++] = "-p"; |
528 |
rcarg[nrcargs++] = params; |
529 |
} |
530 |
if (nbins != NULL) { /* add #bins if set */ |
531 |
CHECKARGC(2); |
532 |
rcarg[nrcargs++] = "-bn"; |
533 |
rcarg[nrcargs++] = nbins; |
534 |
} |
535 |
if (binv != NULL) { |
536 |
CHECKARGC(2); /* assign bin variable */ |
537 |
rcarg[nrcargs++] = "-b"; |
538 |
rcarg[nrcargs++] = binv; |
539 |
} else if (binf != NULL) { |
540 |
CHECKARGC(2); /* assign bin function */ |
541 |
rcarg[nrcargs++] = "-b"; |
542 |
sprintf(sbuf, "%s(%g,%g,%g,%g,%g,%g)", binf, |
543 |
curparams.nrm[0], curparams.nrm[1], curparams.nrm[2], |
544 |
curparams.vup[0], curparams.vup[1], curparams.vup[2]); |
545 |
rcarg[nrcargs++] = savqstr(sbuf); |
546 |
} |
547 |
CHECKARGC(2); /* modifier argument goes last */ |
548 |
rcarg[nrcargs++] = "-m"; |
549 |
rcarg[nrcargs++] = savqstr(curmod); |
550 |
} |
551 |
|
552 |
/* Make randomly oriented tangent plane axes for given normal direction */ |
553 |
static void |
554 |
make_axes(FVECT uva[2], const FVECT nrm) |
555 |
{ |
556 |
int i; |
557 |
|
558 |
if (!getperpendicular(uva[0], nrm, 1)) { |
559 |
fputs(progname, stderr); |
560 |
fputs(": bad surface normal in make_axes!\n", stderr); |
561 |
exit(1); |
562 |
} |
563 |
fcross(uva[1], nrm, uva[0]); |
564 |
} |
565 |
|
566 |
/* Illegal sender surfaces end up here */ |
567 |
static int |
568 |
ssamp_bad(FVECT orig, SURF *sp, double x) |
569 |
{ |
570 |
fprintf(stderr, "%s: illegal sender surface '%s'\n", |
571 |
progname, sp->sname); |
572 |
return(0); |
573 |
} |
574 |
|
575 |
/* Generate origin on ring surface from uniform random variable */ |
576 |
static int |
577 |
ssamp_ring(FVECT orig, SURF *sp, double x) |
578 |
{ |
579 |
FVECT *uva = (FVECT *)sp->priv; |
580 |
double samp2[2]; |
581 |
double uv[2]; |
582 |
int i; |
583 |
|
584 |
if (uva == NULL) { /* need tangent axes */ |
585 |
uva = (FVECT *)malloc(sizeof(FVECT)*2); |
586 |
if (uva == NULL) { |
587 |
fputs(progname, stderr); |
588 |
fputs(": out of memory in ssamp_ring!\n", stderr); |
589 |
return(0); |
590 |
} |
591 |
make_axes(uva, sp->snrm); |
592 |
sp->priv = (void *)uva; |
593 |
} |
594 |
SDmultiSamp(samp2, 2, x); |
595 |
samp2[0] = sqrt(samp2[0]*sp->area*(1./PI) + sp->farg[6]*sp->farg[6]); |
596 |
samp2[1] *= 2.*PI; |
597 |
uv[0] = samp2[0]*tcos(samp2[1]); |
598 |
uv[1] = samp2[0]*tsin(samp2[1]); |
599 |
for (i = 3; i--; ) |
600 |
orig[i] = sp->farg[i] + uv[0]*uva[0][i] + uv[1]*uva[1][i]; |
601 |
return(1); |
602 |
} |
603 |
|
604 |
/* Add triangle to polygon's list (call-back function) */ |
605 |
static int |
606 |
add_triangle(const Vert2_list *tp, int a, int b, int c) |
607 |
{ |
608 |
POLYTRIS *ptp = (POLYTRIS *)tp->p; |
609 |
struct ptri *trip = ptp->tri + ptp->ntris++; |
610 |
|
611 |
trip->vndx[0] = a; |
612 |
trip->vndx[1] = b; |
613 |
trip->vndx[2] = c; |
614 |
return(1); |
615 |
} |
616 |
|
617 |
/* Generate origin on polygon surface from uniform random variable */ |
618 |
static int |
619 |
ssamp_poly(FVECT orig, SURF *sp, double x) |
620 |
{ |
621 |
POLYTRIS *ptp = (POLYTRIS *)sp->priv; |
622 |
double samp2[2]; |
623 |
double *v0, *v1, *v2; |
624 |
int i; |
625 |
|
626 |
if (ptp == NULL) { /* need to triangulate */ |
627 |
ptp = (POLYTRIS *)malloc(sizeof(POLYTRIS) + |
628 |
sizeof(struct ptri)*(sp->nfargs/3 - 3)); |
629 |
if (ptp == NULL) |
630 |
goto memerr; |
631 |
if (sp->nfargs == 3) { /* simple case */ |
632 |
ptp->ntris = 1; |
633 |
ptp->tri[0].vndx[0] = 0; |
634 |
ptp->tri[0].vndx[1] = 1; |
635 |
ptp->tri[0].vndx[2] = 2; |
636 |
ptp->tri[0].afrac = 1; |
637 |
} else { |
638 |
Vert2_list *v2l = polyAlloc(sp->nfargs/3); |
639 |
if (v2l == NULL) |
640 |
goto memerr; |
641 |
make_axes(ptp->uva, sp->snrm); |
642 |
for (i = v2l->nv; i--; ) { |
643 |
v2l->v[i].mX = DOT(sp->farg+3*i, ptp->uva[0]); |
644 |
v2l->v[i].mY = DOT(sp->farg+3*i, ptp->uva[1]); |
645 |
} |
646 |
ptp->ntris = 0; |
647 |
v2l->p = (void *)ptp; |
648 |
if (!polyTriangulate(v2l, add_triangle)) { |
649 |
fprintf(stderr, |
650 |
"%s: cannot triangulate polygon '%s'\n", |
651 |
progname, sp->sname); |
652 |
return(0); |
653 |
} |
654 |
for (i = ptp->ntris; i--; ) { |
655 |
int a = ptp->tri[i].vndx[0]; |
656 |
int b = ptp->tri[i].vndx[1]; |
657 |
int c = ptp->tri[i].vndx[2]; |
658 |
ptp->tri[i].afrac = |
659 |
(v2l->v[a].mX*v2l->v[b].mY - |
660 |
v2l->v[b].mX*v2l->v[a].mY + |
661 |
v2l->v[b].mX*v2l->v[c].mY - |
662 |
v2l->v[c].mX*v2l->v[b].mY + |
663 |
v2l->v[c].mX*v2l->v[a].mY - |
664 |
v2l->v[a].mX*v2l->v[c].mY) / |
665 |
(2.*sp->area); |
666 |
} |
667 |
polyFree(v2l); |
668 |
} |
669 |
sp->priv = (void *)ptp; |
670 |
} |
671 |
/* pick triangle by partial area */ |
672 |
for (i = 0; i < ptp->ntris-1 && x > ptp->tri[i].afrac; i++) |
673 |
x -= ptp->tri[i].afrac; |
674 |
SDmultiSamp(samp2, 2, x/ptp->tri[i].afrac); |
675 |
samp2[0] *= samp2[1] = sqrt(samp2[1]); |
676 |
samp2[1] = 1. - samp2[1]; |
677 |
v0 = sp->farg + 3*ptp->tri[i].vndx[0]; |
678 |
v1 = sp->farg + 3*ptp->tri[i].vndx[1]; |
679 |
v2 = sp->farg + 3*ptp->tri[i].vndx[2]; |
680 |
for (i = 3; i--; ) |
681 |
orig[i] = v0[i] + samp2[0]*(v1[i] - v0[i]) |
682 |
+ samp2[1]*(v2[i] - v0[i]) ; |
683 |
return(1); |
684 |
memerr: |
685 |
fputs(progname, stderr); |
686 |
fputs(": out of memory in ssamp_poly!\n", stderr); |
687 |
return(0); |
688 |
} |
689 |
|
690 |
/* Compute sample origin based on projected areas of sender subsurfaces */ |
691 |
static int |
692 |
sample_origin(PARAMS *p, FVECT orig, const FVECT rdir, double x) |
693 |
{ |
694 |
static double *projsa; |
695 |
static int nall; |
696 |
double tarea = 0; |
697 |
int i; |
698 |
SURF *sp; |
699 |
/* special case for lone surface */ |
700 |
if (p->nsurfs == 1) { |
701 |
sp = p->slist; |
702 |
if (DOT(sp->snrm, rdir) >= FTINY) { |
703 |
fprintf(stderr, |
704 |
"%s: internal - sample behind sender '%s'\n", |
705 |
progname, sp->sname); |
706 |
return(0); |
707 |
} |
708 |
return((*orig_in_surf[sp->styp])(orig, sp, x)); |
709 |
} |
710 |
if (p->nsurfs > nall) { /* (re)allocate surface area cache */ |
711 |
if (projsa) free(projsa); |
712 |
projsa = (double *)malloc(sizeof(double)*p->nsurfs); |
713 |
if (projsa == NULL) { |
714 |
fputs(progname, stderr); |
715 |
fputs(": out of memory in sample_origin!\n", stderr); |
716 |
exit(1); |
717 |
} |
718 |
nall = p->nsurfs; |
719 |
} |
720 |
/* compute projected areas */ |
721 |
for (i = 0, sp = p->slist; sp != NULL; i++, sp = sp->next) { |
722 |
projsa[i] = -DOT(sp->snrm, rdir) * sp->area; |
723 |
tarea += projsa[i] *= (double)(projsa[i] > 0); |
724 |
} |
725 |
if (tarea < FTINY*FTINY) { /* wrong side of sender? */ |
726 |
fputs(progname, stderr); |
727 |
fputs(": internal - sample behind all sender elements!\n", |
728 |
stderr); |
729 |
return(0); |
730 |
} |
731 |
tarea *= x; /* get surface from list */ |
732 |
for (i = 0, sp = p->slist; tarea > projsa[i]; sp = sp->next) |
733 |
tarea -= projsa[i++]; |
734 |
return((*orig_in_surf[sp->styp])(orig, sp, tarea/projsa[i])); |
735 |
} |
736 |
|
737 |
/* Uniform sample generator */ |
738 |
static int |
739 |
sample_uniform(PARAMS *p, int b, FILE *fp) |
740 |
{ |
741 |
int n = sampcnt; |
742 |
double samp3[3]; |
743 |
double duvw[3]; |
744 |
FVECT orig_dir[2]; |
745 |
int i; |
746 |
|
747 |
if (fp == NULL) /* just requesting number of bins? */ |
748 |
return(1); |
749 |
|
750 |
while (n--) { /* stratified hemisphere sampling */ |
751 |
SDmultiSamp(samp3, 3, (n+frandom())/sampcnt); |
752 |
SDsquare2disk(duvw, samp3[1], samp3[2]); |
753 |
duvw[2] = -sqrt(1. - duvw[0]*duvw[0] - duvw[1]*duvw[1]); |
754 |
for (i = 3; i--; ) |
755 |
orig_dir[1][i] = duvw[0]*p->udir[i] + |
756 |
duvw[1]*p->vdir[i] + |
757 |
duvw[2]*p->nrm[i] ; |
758 |
if (!sample_origin(p, orig_dir[0], orig_dir[1], samp3[0])) |
759 |
return(0); |
760 |
if (putbinary(orig_dir, sizeof(FVECT), 2, fp) != 2) |
761 |
return(0); |
762 |
} |
763 |
return(1); |
764 |
} |
765 |
|
766 |
/* Shirly-Chiu sample generator */ |
767 |
static int |
768 |
sample_shirchiu(PARAMS *p, int b, FILE *fp) |
769 |
{ |
770 |
int n = sampcnt; |
771 |
double samp3[3]; |
772 |
double duvw[3]; |
773 |
FVECT orig_dir[2]; |
774 |
int i; |
775 |
|
776 |
if (fp == NULL) /* just requesting number of bins? */ |
777 |
return(p->hsiz*p->hsiz); |
778 |
|
779 |
while (n--) { /* stratified sampling */ |
780 |
SDmultiSamp(samp3, 3, (n+frandom())/sampcnt); |
781 |
SDsquare2disk(duvw, (b/p->hsiz + samp3[1])/curparams.hsiz, |
782 |
(b%p->hsiz + samp3[2])/curparams.hsiz); |
783 |
duvw[2] = sqrt(1. - duvw[0]*duvw[0] - duvw[1]*duvw[1]); |
784 |
for (i = 3; i--; ) |
785 |
orig_dir[1][i] = -duvw[0]*p->udir[i] - |
786 |
duvw[1]*p->vdir[i] - |
787 |
duvw[2]*p->nrm[i] ; |
788 |
if (!sample_origin(p, orig_dir[0], orig_dir[1], samp3[0])) |
789 |
return(0); |
790 |
if (putbinary(orig_dir, sizeof(FVECT), 2, fp) != 2) |
791 |
return(0); |
792 |
} |
793 |
return(1); |
794 |
} |
795 |
|
796 |
/* Reinhart/Tregenza sample generator */ |
797 |
static int |
798 |
sample_reinhart(PARAMS *p, int b, FILE *fp) |
799 |
{ |
800 |
#define T_NALT 7 |
801 |
static const int tnaz[T_NALT] = {30, 30, 24, 24, 18, 12, 6}; |
802 |
const int RowMax = T_NALT*p->hsiz + 1; |
803 |
const double RAH = (.5*PI)/(RowMax-.5); |
804 |
#define rnaz(r) (r >= RowMax-1 ? 1 : p->hsiz*tnaz[r/p->hsiz]) |
805 |
int n = sampcnt; |
806 |
int row, col; |
807 |
double samp3[3]; |
808 |
double alt, azi; |
809 |
double duvw[3]; |
810 |
FVECT orig_dir[2]; |
811 |
int i; |
812 |
|
813 |
if (fp == NULL) { /* just requesting number of bins? */ |
814 |
n = 0; |
815 |
for (row = RowMax; row--; ) n += rnaz(row); |
816 |
return(n); |
817 |
} |
818 |
row = 0; /* identify row & column */ |
819 |
col = b; |
820 |
while (col >= rnaz(row)) { |
821 |
col -= rnaz(row); |
822 |
++row; |
823 |
} |
824 |
while (n--) { /* stratified sampling */ |
825 |
SDmultiSamp(samp3, 3, (n+frandom())/sampcnt); |
826 |
if (row >= RowMax-1) /* avoid crowding at zenith */ |
827 |
samp3[1] *= samp3[1]; |
828 |
alt = (row+samp3[1])*RAH; |
829 |
azi = (2.*PI)*(col+samp3[2]-.5)/rnaz(row); |
830 |
duvw[2] = cos(alt); /* measured from horizon */ |
831 |
duvw[0] = tsin(azi)*duvw[2]; |
832 |
duvw[1] = -tcos(azi)*duvw[2]; |
833 |
duvw[2] = sqrt(1. - duvw[2]*duvw[2]); |
834 |
for (i = 3; i--; ) |
835 |
orig_dir[1][i] = -duvw[0]*p->udir[i] - |
836 |
duvw[1]*p->vdir[i] - |
837 |
duvw[2]*p->nrm[i] ; |
838 |
if (!sample_origin(p, orig_dir[0], orig_dir[1], samp3[0])) |
839 |
return(0); |
840 |
if (putbinary(orig_dir, sizeof(FVECT), 2, fp) != 2) |
841 |
return(0); |
842 |
} |
843 |
return(1); |
844 |
#undef rnaz |
845 |
#undef T_NALT |
846 |
} |
847 |
|
848 |
/* Klems sample generator */ |
849 |
static int |
850 |
sample_klems(PARAMS *p, int b, FILE *fp) |
851 |
{ |
852 |
static const char bname[4][20] = { |
853 |
"LBNL/Klems Full", |
854 |
"LBNL/Klems Half", |
855 |
"INTERNAL ERROR", |
856 |
"LBNL/Klems Quarter" |
857 |
}; |
858 |
static ANGLE_BASIS *kbasis[4]; |
859 |
const int bi = p->hemis[1] - '1'; |
860 |
int n = sampcnt; |
861 |
double samp2[2]; |
862 |
double duvw[3]; |
863 |
FVECT orig_dir[2]; |
864 |
int i; |
865 |
|
866 |
if (!kbasis[bi]) { /* need to get basis, first */ |
867 |
for (i = 4; i--; ) |
868 |
if (!strcasecmp(abase_list[i].name, bname[bi])) { |
869 |
kbasis[bi] = &abase_list[i]; |
870 |
break; |
871 |
} |
872 |
if (i < 0) { |
873 |
fprintf(stderr, "%s: unknown hemisphere basis '%s'\n", |
874 |
progname, bname[bi]); |
875 |
return(0); |
876 |
} |
877 |
} |
878 |
if (fp == NULL) /* just requesting number of bins? */ |
879 |
return(kbasis[bi]->nangles); |
880 |
|
881 |
while (n--) { /* stratified sampling */ |
882 |
SDmultiSamp(samp2, 2, (n+frandom())/sampcnt); |
883 |
if (!fo_getvec(duvw, b+samp2[1], kbasis[bi])) |
884 |
return(0); |
885 |
for (i = 3; i--; ) |
886 |
orig_dir[1][i] = -duvw[0]*p->udir[i] - |
887 |
duvw[1]*p->vdir[i] - |
888 |
duvw[2]*p->nrm[i] ; |
889 |
if (!sample_origin(p, orig_dir[0], orig_dir[1], samp2[0])) |
890 |
return(0); |
891 |
if (putbinary(orig_dir, sizeof(FVECT), 2, fp) != 2) |
892 |
return(0); |
893 |
} |
894 |
return(1); |
895 |
} |
896 |
|
897 |
/* Prepare hemisphere basis sampler that will send rays to rcontrib */ |
898 |
static int |
899 |
prepare_sampler(void) |
900 |
{ |
901 |
if (curparams.slist == NULL) { /* missing sample surface! */ |
902 |
fputs(progname, stderr); |
903 |
fputs(": no sender surface!\n", stderr); |
904 |
return(-1); |
905 |
} |
906 |
/* misplaced output file spec. */ |
907 |
if ((curparams.outfn != NULL) & (verbose >= 0)) |
908 |
fprintf(stderr, "%s: warning - ignoring output file in sender ('%s')\n", |
909 |
progname, curparams.outfn); |
910 |
/* check/set basis hemisphere */ |
911 |
if (!curparams.hemis[0]) { |
912 |
fputs(progname, stderr); |
913 |
fputs(": missing sender sampling type!\n", stderr); |
914 |
return(-1); |
915 |
} |
916 |
if (normalize(curparams.nrm) == 0) { |
917 |
fputs(progname, stderr); |
918 |
fputs(": undefined normal for sender sampling\n", stderr); |
919 |
return(-1); |
920 |
} |
921 |
if (normalize(curparams.vup) == 0) { |
922 |
if (fabs(curparams.nrm[2]) < .7) |
923 |
curparams.vup[2] = 1; |
924 |
else |
925 |
curparams.vup[1] = 1; |
926 |
} |
927 |
fcross(curparams.udir, curparams.vup, curparams.nrm); |
928 |
if (normalize(curparams.udir) == 0) { |
929 |
fputs(progname, stderr); |
930 |
fputs(": up vector coincides with sender normal\n", stderr); |
931 |
return(-1); |
932 |
} |
933 |
fcross(curparams.vdir, curparams.nrm, curparams.udir); |
934 |
if (curparams.sign == '-') { /* left-handed coordinate system? */ |
935 |
curparams.udir[0] *= -1.; |
936 |
curparams.udir[1] *= -1.; |
937 |
curparams.udir[2] *= -1.; |
938 |
} |
939 |
if ((tolower(curparams.hemis[0]) == 'u') | (curparams.hemis[0] == '1')) |
940 |
curparams.sample_basis = sample_uniform; |
941 |
else if (tolower(curparams.hemis[0]) == 's' && |
942 |
tolower(curparams.hemis[1]) == 'c') |
943 |
curparams.sample_basis = sample_shirchiu; |
944 |
else if ((tolower(curparams.hemis[0]) == 'r') | |
945 |
(tolower(curparams.hemis[0]) == 't')) |
946 |
curparams.sample_basis = sample_reinhart; |
947 |
else if (tolower(curparams.hemis[0]) == 'k') { |
948 |
switch (curparams.hemis[1]) { |
949 |
case '1': |
950 |
case '2': |
951 |
case '4': |
952 |
break; |
953 |
case 'f': |
954 |
case 'F': |
955 |
case '\0': |
956 |
curparams.hemis[1] = '1'; |
957 |
break; |
958 |
case 'h': |
959 |
case 'H': |
960 |
curparams.hemis[1] = '2'; |
961 |
break; |
962 |
case 'q': |
963 |
case 'Q': |
964 |
curparams.hemis[1] = '4'; |
965 |
break; |
966 |
default: |
967 |
goto unrecognized; |
968 |
} |
969 |
curparams.hemis[2] = '\0'; |
970 |
curparams.sample_basis = sample_klems; |
971 |
} else |
972 |
goto unrecognized; |
973 |
/* return number of bins */ |
974 |
return((*curparams.sample_basis)(&curparams,0,NULL)); |
975 |
unrecognized: |
976 |
fprintf(stderr, "%s: unrecognized sender sampling: h=%s\n", |
977 |
progname, curparams.hemis); |
978 |
return(-1); |
979 |
} |
980 |
|
981 |
/* Compute normal and area for polygon */ |
982 |
static int |
983 |
finish_polygon(SURF *p) |
984 |
{ |
985 |
const int nv = p->nfargs / 3; |
986 |
FVECT e1, e2, vc; |
987 |
int i; |
988 |
|
989 |
memset(p->snrm, 0, sizeof(FVECT)); |
990 |
VSUB(e1, p->farg+3, p->farg); |
991 |
for (i = 2; i < nv; i++) { |
992 |
VSUB(e2, p->farg+3*i, p->farg); |
993 |
VCROSS(vc, e1, e2); |
994 |
p->snrm[0] += vc[0]; |
995 |
p->snrm[1] += vc[1]; |
996 |
p->snrm[2] += vc[2]; |
997 |
VCOPY(e1, e2); |
998 |
} |
999 |
p->area = normalize(p->snrm)*0.5; |
1000 |
return(p->area > FTINY*FTINY); |
1001 |
} |
1002 |
|
1003 |
/* Add a surface to our current parameters */ |
1004 |
static void |
1005 |
add_surface(int st, const char *oname, FILE *fp) |
1006 |
{ |
1007 |
SURF *snew; |
1008 |
int n; |
1009 |
/* get floating-point arguments */ |
1010 |
if (!fscanf(fp, "%d", &n)) return; |
1011 |
while (n-- > 0) fscanf(fp, "%*s"); |
1012 |
if (!fscanf(fp, "%d", &n)) return; |
1013 |
while (n-- > 0) fscanf(fp, "%*d"); |
1014 |
if (!fscanf(fp, "%d", &n) || n <= 0) return; |
1015 |
snew = (SURF *)malloc(sizeof(SURF) + sizeof(double)*(n-1)); |
1016 |
if (snew == NULL) { |
1017 |
fputs(progname, stderr); |
1018 |
fputs(": out of memory in add_surface!\n", stderr); |
1019 |
exit(1); |
1020 |
} |
1021 |
strncpy(snew->sname, oname, sizeof(snew->sname)-1); |
1022 |
snew->sname[sizeof(snew->sname)-1] = '\0'; |
1023 |
snew->styp = st; |
1024 |
snew->priv = NULL; |
1025 |
snew->nfargs = n; |
1026 |
for (n = 0; n < snew->nfargs; n++) |
1027 |
if (fscanf(fp, "%lf", &snew->farg[n]) != 1) { |
1028 |
fprintf(stderr, "%s: error reading arguments for '%s'\n", |
1029 |
progname, oname); |
1030 |
exit(1); |
1031 |
} |
1032 |
switch (st) { |
1033 |
case ST_RING: |
1034 |
if (snew->nfargs != 8) |
1035 |
goto badcount; |
1036 |
VCOPY(snew->snrm, snew->farg+3); |
1037 |
if (normalize(snew->snrm) == 0) |
1038 |
goto badnorm; |
1039 |
if (snew->farg[7] < snew->farg[6]) { |
1040 |
double t = snew->farg[7]; |
1041 |
snew->farg[7] = snew->farg[6]; |
1042 |
snew->farg[6] = t; |
1043 |
} |
1044 |
snew->area = PI*(snew->farg[7]*snew->farg[7] - |
1045 |
snew->farg[6]*snew->farg[6]); |
1046 |
break; |
1047 |
case ST_POLY: |
1048 |
if (snew->nfargs < 9 || snew->nfargs % 3) |
1049 |
goto badcount; |
1050 |
finish_polygon(snew); |
1051 |
break; |
1052 |
case ST_SOURCE: |
1053 |
if (snew->nfargs != 4) |
1054 |
goto badcount; |
1055 |
for (n = 3; n--; ) /* need to reverse "normal" */ |
1056 |
snew->snrm[n] = -snew->farg[n]; |
1057 |
if (normalize(snew->snrm) == 0) |
1058 |
goto badnorm; |
1059 |
snew->area = sin((PI/180./2.)*snew->farg[3]); |
1060 |
snew->area *= PI*snew->area; |
1061 |
break; |
1062 |
} |
1063 |
if ((snew->area <= FTINY*FTINY) & (verbose >= 0)) { |
1064 |
fprintf(stderr, "%s: warning - zero area for surface '%s'\n", |
1065 |
progname, oname); |
1066 |
free(snew); |
1067 |
return; |
1068 |
} |
1069 |
VSUM(curparams.nrm, curparams.nrm, snew->snrm, snew->area); |
1070 |
snew->next = curparams.slist; |
1071 |
curparams.slist = snew; |
1072 |
curparams.nsurfs++; |
1073 |
return; |
1074 |
badcount: |
1075 |
fprintf(stderr, "%s: bad argument count for surface element '%s'\n", |
1076 |
progname, oname); |
1077 |
exit(1); |
1078 |
badnorm: |
1079 |
fprintf(stderr, "%s: bad orientation for surface element '%s'\n", |
1080 |
progname, oname); |
1081 |
exit(1); |
1082 |
} |
1083 |
|
1084 |
/* Parse a receiver object (look for modifiers to add to rcontrib command) */ |
1085 |
static int |
1086 |
add_recv_object(FILE *fp) |
1087 |
{ |
1088 |
int st; |
1089 |
char thismod[128], otype[32], oname[128]; |
1090 |
int n; |
1091 |
|
1092 |
if (fscanf(fp, "%s %s %s", thismod, otype, oname) != 3) |
1093 |
return(0); /* must have hit EOF! */ |
1094 |
if (!strcmp(otype, "alias")) { |
1095 |
fscanf(fp, "%*s"); /* skip alias */ |
1096 |
return(0); |
1097 |
} |
1098 |
/* is it a new receiver? */ |
1099 |
if ((st = surf_type(otype)) != ST_NONE) { |
1100 |
if (curparams.slist != NULL && (st == ST_SOURCE) ^ |
1101 |
(curparams.slist->styp == ST_SOURCE)) { |
1102 |
fputs(progname, stderr); |
1103 |
fputs(": cannot mix source/non-source receivers!\n", stderr); |
1104 |
return(-1); |
1105 |
} |
1106 |
if (strcmp(thismod, curmod)) { |
1107 |
if (curmod[0]) { /* output last receiver? */ |
1108 |
finish_receiver(); |
1109 |
clear_params(&curparams, 1); |
1110 |
} |
1111 |
parse_params(&curparams, newparams); |
1112 |
newparams[0] = '\0'; |
1113 |
strcpy(curmod, thismod); |
1114 |
} |
1115 |
add_surface(st, oname, fp); /* read & store surface */ |
1116 |
return(1); |
1117 |
} |
1118 |
/* else skip arguments */ |
1119 |
if (!fscanf(fp, "%d", &n)) return(0); |
1120 |
while (n-- > 0) fscanf(fp, "%*s"); |
1121 |
if (!fscanf(fp, "%d", &n)) return(0); |
1122 |
while (n-- > 0) fscanf(fp, "%*d"); |
1123 |
if (!fscanf(fp, "%d", &n)) return(0); |
1124 |
while (n-- > 0) fscanf(fp, "%*f"); |
1125 |
return(0); |
1126 |
} |
1127 |
|
1128 |
/* Parse a sender object */ |
1129 |
static int |
1130 |
add_send_object(FILE *fp) |
1131 |
{ |
1132 |
int st; |
1133 |
char thismod[128], otype[32], oname[128]; |
1134 |
int n; |
1135 |
|
1136 |
if (fscanf(fp, "%s %s %s", thismod, otype, oname) != 3) |
1137 |
return(0); /* must have hit EOF! */ |
1138 |
if (!strcmp(otype, "alias")) { |
1139 |
fscanf(fp, "%*s"); /* skip alias */ |
1140 |
return(0); |
1141 |
} |
1142 |
/* is it a new surface? */ |
1143 |
if ((st = surf_type(otype)) != ST_NONE) { |
1144 |
if (st == ST_SOURCE) { |
1145 |
fputs(progname, stderr); |
1146 |
fputs(": cannot use source as a sender!\n", stderr); |
1147 |
return(-1); |
1148 |
} |
1149 |
if (strcmp(thismod, curmod)) { |
1150 |
if (curmod[0]) { |
1151 |
fputs(progname, stderr); |
1152 |
fputs(": warning - multiple modifiers in sender\n", |
1153 |
stderr); |
1154 |
} |
1155 |
strcpy(curmod, thismod); |
1156 |
} |
1157 |
parse_params(&curparams, newparams); |
1158 |
newparams[0] = '\0'; |
1159 |
add_surface(st, oname, fp); /* read & store surface */ |
1160 |
return(0); |
1161 |
} |
1162 |
/* else skip arguments */ |
1163 |
if (!fscanf(fp, "%d", &n)) return(0); |
1164 |
while (n-- > 0) fscanf(fp, "%*s"); |
1165 |
if (!fscanf(fp, "%d", &n)) return(0); |
1166 |
while (n-- > 0) fscanf(fp, "%*d"); |
1167 |
if (!fscanf(fp, "%d", &n)) return(0); |
1168 |
while (n-- > 0) fscanf(fp, "%*f"); |
1169 |
return(0); |
1170 |
} |
1171 |
|
1172 |
/* Load a Radiance scene using the given callback function for objects */ |
1173 |
static int |
1174 |
load_scene(const char *inspec, int (*ocb)(FILE *)) |
1175 |
{ |
1176 |
int rv = 0; |
1177 |
char inpbuf[1024]; |
1178 |
FILE *fp; |
1179 |
int c; |
1180 |
|
1181 |
if (*inspec == '!') |
1182 |
fp = popen(inspec+1, "r"); |
1183 |
else |
1184 |
fp = fopen(inspec, "r"); |
1185 |
if (fp == NULL) { |
1186 |
fprintf(stderr, "%s: cannot load '%s'\n", progname, inspec); |
1187 |
return(-1); |
1188 |
} |
1189 |
while ((c = getc(fp)) != EOF) { /* load receiver data */ |
1190 |
if (isspace(c)) /* skip leading white space */ |
1191 |
continue; |
1192 |
if (c == '!') { /* read from a new command */ |
1193 |
inpbuf[0] = c; |
1194 |
if (fgetline(inpbuf+1, sizeof(inpbuf)-1, fp) != NULL) { |
1195 |
if ((c = load_scene(inpbuf, ocb)) < 0) |
1196 |
return(c); |
1197 |
rv += c; |
1198 |
} |
1199 |
continue; |
1200 |
} |
1201 |
if (c == '#') { /* parameters/comment */ |
1202 |
if ((c = getc(fp)) == EOF || ungetc(c,fp) == EOF) |
1203 |
break; |
1204 |
if (!isspace(c) && fscanf(fp, "%s", inpbuf) == 1 && |
1205 |
!strcmp(inpbuf, PARAMSTART)) { |
1206 |
if (fgets(inpbuf, sizeof(inpbuf), fp) != NULL) |
1207 |
strcat(newparams, inpbuf); |
1208 |
continue; |
1209 |
} |
1210 |
while ((c = getc(fp)) != EOF && c != '\n') |
1211 |
; /* else skipping comment */ |
1212 |
continue; |
1213 |
} |
1214 |
ungetc(c, fp); /* else check object for receiver */ |
1215 |
c = (*ocb)(fp); |
1216 |
if (c < 0) |
1217 |
return(c); |
1218 |
rv += c; |
1219 |
} |
1220 |
/* close our input stream */ |
1221 |
c = (*inspec == '!') ? pclose(fp) : fclose(fp); |
1222 |
if (c != 0) { |
1223 |
fprintf(stderr, "%s: error loading '%s'\n", progname, inspec); |
1224 |
return(-1); |
1225 |
} |
1226 |
return(rv); |
1227 |
} |
1228 |
|
1229 |
/* Get command arguments and run program */ |
1230 |
int |
1231 |
main(int argc, char *argv[]) |
1232 |
{ |
1233 |
char fmtopt[6] = "-faa"; /* default output is ASCII */ |
1234 |
char *xrs=NULL, *yrs=NULL, *ldopt=NULL; |
1235 |
char *iropt = NULL; |
1236 |
char *sendfn; |
1237 |
char sampcntbuf[32], nsbinbuf[32]; |
1238 |
FILE *rcfp; |
1239 |
int nsbins; |
1240 |
int a, i; |
1241 |
/* screen rcontrib options */ |
1242 |
progname = argv[0]; |
1243 |
for (a = 1; a < argc-2; a++) { |
1244 |
int na; |
1245 |
/* check for argument expansion */ |
1246 |
while ((na = expandarg(&argc, &argv, a)) > 0) |
1247 |
; |
1248 |
if (na < 0) { |
1249 |
fprintf(stderr, "%s: cannot expand '%s'\n", |
1250 |
progname, argv[a]); |
1251 |
return(1); |
1252 |
} |
1253 |
if (argv[a][0] != '-' || !argv[a][1]) |
1254 |
break; |
1255 |
na = 1; |
1256 |
switch (argv[a][1]) { /* !! Keep consistent !! */ |
1257 |
case 'v': /* verbose mode */ |
1258 |
verbose = 1; |
1259 |
na = 0; |
1260 |
continue; |
1261 |
case 'f': /* special case for -fo, -ff, etc. */ |
1262 |
switch (argv[a][2]) { |
1263 |
case '\0': /* cal file */ |
1264 |
goto userr; |
1265 |
case 'o': /* force output */ |
1266 |
goto userr; |
1267 |
case 'a': /* output format */ |
1268 |
case 'f': |
1269 |
case 'd': |
1270 |
case 'c': |
1271 |
if (!(fmtopt[3] = argv[a][3])) |
1272 |
fmtopt[3] = argv[a][2]; |
1273 |
fmtopt[2] = argv[a][2]; |
1274 |
na = 0; |
1275 |
continue; /* will pass later */ |
1276 |
default: |
1277 |
goto userr; |
1278 |
} |
1279 |
break; |
1280 |
case 'x': /* x-resolution */ |
1281 |
xrs = argv[++a]; |
1282 |
na = 0; |
1283 |
continue; |
1284 |
case 'y': /* y-resolution */ |
1285 |
yrs = argv[++a]; |
1286 |
na = 0; |
1287 |
continue; |
1288 |
case 'c': /* number of samples */ |
1289 |
sampcnt = atoi(argv[++a]); |
1290 |
if (sampcnt <= 0) |
1291 |
goto userr; |
1292 |
na = 0; /* we re-add this later */ |
1293 |
continue; |
1294 |
case 'I': /* only for pass-through mode */ |
1295 |
case 'i': |
1296 |
iropt = argv[a]; |
1297 |
na = 0; |
1298 |
continue; |
1299 |
case 'w': /* options without arguments */ |
1300 |
if (!argv[a][2] || strchr("+1tTyY", argv[a][2]) == NULL) |
1301 |
verbose = -1; |
1302 |
break; |
1303 |
case 'V': |
1304 |
case 'u': |
1305 |
case 'h': |
1306 |
case 'r': |
1307 |
break; |
1308 |
case 'n': /* options with 1 argument */ |
1309 |
case 's': |
1310 |
case 'o': |
1311 |
case 't': |
1312 |
na = 2; |
1313 |
break; |
1314 |
case 'b': /* special case */ |
1315 |
if (argv[a][2] != 'v') goto userr; |
1316 |
break; |
1317 |
case 'l': /* special case */ |
1318 |
if (argv[a][2] == 'd') { |
1319 |
ldopt = argv[a]; |
1320 |
na = 0; |
1321 |
continue; |
1322 |
} |
1323 |
na = 2; |
1324 |
break; |
1325 |
case 'd': /* special case */ |
1326 |
if (argv[a][2] != 'v') na = 2; |
1327 |
break; |
1328 |
case 'a': /* special case */ |
1329 |
if (argv[a][2] == 'p') { |
1330 |
na = 2; /* photon map [+ bandwidth(s)] */ |
1331 |
if (a < argc-3 && atoi(argv[a+1]) > 0) |
1332 |
na += 1 + (a < argc-4 && atoi(argv[a+2]) > 0); |
1333 |
} else |
1334 |
na = (argv[a][2] == 'v') ? 4 : 2; |
1335 |
break; |
1336 |
case 'm': /* special case */ |
1337 |
if (!argv[a][2]) goto userr; |
1338 |
na = (argv[a][2] == 'e') | (argv[a][2] == 'a') ? 4 : 2; |
1339 |
break; |
1340 |
default: /* anything else is verbotten */ |
1341 |
goto userr; |
1342 |
} |
1343 |
if (na <= 0) continue; |
1344 |
CHECKARGC(na); /* pass on option */ |
1345 |
rcarg[nrcargs++] = argv[a]; |
1346 |
while (--na) /* + arguments if any */ |
1347 |
rcarg[nrcargs++] = argv[++a]; |
1348 |
} |
1349 |
if (a > argc-2) |
1350 |
goto userr; /* check at end of options */ |
1351 |
sendfn = argv[a++]; /* assign sender & receiver inputs */ |
1352 |
if (sendfn[0] == '-') { /* user wants pass-through mode? */ |
1353 |
if (sendfn[1]) goto userr; |
1354 |
sendfn = NULL; |
1355 |
if (iropt) { |
1356 |
CHECKARGC(1); |
1357 |
rcarg[nrcargs++] = iropt; |
1358 |
} |
1359 |
if (xrs) { |
1360 |
CHECKARGC(2); |
1361 |
rcarg[nrcargs++] = "-x"; |
1362 |
rcarg[nrcargs++] = xrs; |
1363 |
} |
1364 |
if (yrs) { |
1365 |
CHECKARGC(2); |
1366 |
rcarg[nrcargs++] = "-y"; |
1367 |
rcarg[nrcargs++] = yrs; |
1368 |
} |
1369 |
if (ldopt) { |
1370 |
CHECKARGC(1); |
1371 |
rcarg[nrcargs++] = ldopt; |
1372 |
} |
1373 |
if (sampcnt <= 0) sampcnt = 1; |
1374 |
} else { /* else in sampling mode */ |
1375 |
if (iropt) { |
1376 |
fputs(progname, stderr); |
1377 |
fputs(": -i, -I supported for pass-through only\n", stderr); |
1378 |
return(1); |
1379 |
} |
1380 |
#ifdef SMLFLT |
1381 |
fmtopt[2] = 'f'; |
1382 |
#else |
1383 |
fmtopt[2] = 'd'; |
1384 |
#endif |
1385 |
if (sampcnt <= 0) sampcnt = 10000; |
1386 |
} |
1387 |
sprintf(sampcntbuf, "%d", sampcnt); |
1388 |
CHECKARGC(3); /* add our format & sample count */ |
1389 |
rcarg[nrcargs++] = fmtopt; |
1390 |
rcarg[nrcargs++] = "-c"; |
1391 |
rcarg[nrcargs++] = sampcntbuf; |
1392 |
/* add receiver arguments to rcontrib */ |
1393 |
if (load_scene(argv[a], add_recv_object) < 0) |
1394 |
return(1); |
1395 |
finish_receiver(); |
1396 |
if (sendfn == NULL) { /* pass-through mode? */ |
1397 |
CHECKARGC(1); /* add octree */ |
1398 |
rcarg[nrcargs++] = oconv_command(argc-a, argv+a); |
1399 |
rcarg[nrcargs] = NULL; |
1400 |
return(my_exec(rcarg)); /* rcontrib does everything */ |
1401 |
} |
1402 |
clear_params(&curparams, 0); /* else load sender surface & params */ |
1403 |
curmod[0] = '\0'; |
1404 |
if (load_scene(sendfn, add_send_object) < 0) |
1405 |
return(1); |
1406 |
if ((nsbins = prepare_sampler()) <= 0) |
1407 |
return(1); |
1408 |
CHECKARGC(3); /* add row count and octree */ |
1409 |
rcarg[nrcargs++] = "-y"; |
1410 |
sprintf(nsbinbuf, "%d", nsbins); |
1411 |
rcarg[nrcargs++] = nsbinbuf; |
1412 |
rcarg[nrcargs++] = oconv_command(argc-a, argv+a); |
1413 |
rcarg[nrcargs] = NULL; |
1414 |
/* open pipe to rcontrib process */ |
1415 |
if ((rcfp = popen_arglist(rcarg, "w")) == NULL) |
1416 |
return(1); |
1417 |
SET_FILE_BINARY(rcfp); |
1418 |
#ifdef getc_unlocked |
1419 |
flockfile(rcfp); |
1420 |
#endif |
1421 |
if (verbose > 0) { |
1422 |
fprintf(stderr, "%s: sampling %d directions", progname, nsbins); |
1423 |
if (curparams.nsurfs > 1) |
1424 |
fprintf(stderr, " (%d elements)\n", curparams.nsurfs); |
1425 |
else |
1426 |
fputc('\n', stderr); |
1427 |
} |
1428 |
for (i = 0; i < nsbins; i++) /* send rcontrib ray samples */ |
1429 |
if (!(*curparams.sample_basis)(&curparams, i, rcfp)) |
1430 |
return(1); |
1431 |
return(pclose_al(rcfp) < 0); /* all finished! */ |
1432 |
userr: |
1433 |
if (a < argc-2) |
1434 |
fprintf(stderr, "%s: unsupported option '%s'", progname, argv[a]); |
1435 |
fprintf(stderr, "Usage: %s [-v][rcontrib options] sender.rad receiver.rad [-i system.oct] [system.rad ..]\n", |
1436 |
progname); |
1437 |
return(1); |
1438 |
} |