ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/Development/ray/src/util/rfluxmtx.c
Revision: 2.66
Committed: Thu Oct 23 23:34:00 2025 UTC (2 weeks, 2 days ago) by greg
Content type: text/plain
Branch: MAIN
CVS Tags: HEAD
Changes since 2.65: +4 -2 lines
Log Message:
feat(rfluxmtx): Permit passing of -e and -f options to rcontrib

File Contents

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