ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/util/rfluxmtx.c
Revision: 2.54
Committed: Wed Dec 15 01:38:50 2021 UTC (2 years, 4 months ago) by greg
Content type: text/plain
Branch: MAIN
CVS Tags: rad5R4
Changes since 2.53: +5 -7 lines
Log Message:
refactor: removed prefix from SDdisk2square() and SDsquare2disk() & made consistent

File Contents

# User Rev Content
1 greg 2.1 #ifndef lint
2 greg 2.54 static const char RCSid[] = "$Id: rfluxmtx.c,v 2.53 2020/09/09 21:28:19 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     char *progname; /* global argv[0] */
26    
27 greg 2.16 int verbose = 0; /* verbose mode (< 0 no warnings) */
28 greg 2.1
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 greg 2.2 int sampcnt = 0; /* sample count (0==unset) */
38 greg 2.1
39 greg 2.2 char *reinhfn = "reinhartb.cal";
40     char *shirchiufn = "disk2square.cal";
41 greg 2.1 char *kfullfn = "klems_full.cal";
42     char *khalffn = "klems_half.cal";
43     char *kquarterfn = "klems_quarter.cal";
44    
45 greg 2.5 /* string indicating parameters */
46     const char PARAMSTART[] = "@rfluxmtx";
47 greg 2.1
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 greg 2.4 double area; /* surface area / proj. solid angle */
60 greg 2.1 short styp; /* surface type */
61 greg 2.2 short nfargs; /* number of real arguments */
62     double farg[1]; /* real values (extends struct) */
63 greg 2.1 } SURF; /* surface structure */
64    
65     typedef struct {
66     FVECT uva[2]; /* tangent axes */
67     int ntris; /* number of triangles */
68     struct ptri {
69 greg 2.41 double afrac; /* fraction of total area */
70 greg 2.1 short vndx[3]; /* vertex indices */
71     } tri[1]; /* triangle array (extends struct) */
72     } POLYTRIS; /* triangulated polygon */
73    
74     typedef struct param_s {
75 greg 2.26 char sign; /* '-' for axis reversal */
76     char hemis[31]; /* hemispherical sampling spec. */
77 greg 2.1 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 greg 2.26 FVECT udir, vdir; /* tangent axes */
83 greg 2.1 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 greg 2.6 char newparams[1024];
90 greg 2.1
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 greg 2.6 memset(p, 0, sizeof(PARAMS));
118 greg 2.1 }
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 greg 2.13 static char oconvbuf[2048] = "!oconv -f ";
138     char *cp = oconvbuf + 10;
139     char *recv = *av++;
140    
141     if (ac-- <= 0)
142     return(NULL);
143 greg 2.43 if (verbose < 0) { /* turn off warnings */
144 greg 2.49 strcpy(cp, "-w ");
145 greg 2.50 cp += 3;
146 greg 2.43 }
147 greg 2.48 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 greg 2.13 *cp++ = ' ';
161 greg 2.30 }
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 greg 2.1 }
170 greg 2.30 *cp++ = QUOTCHAR;
171     *cp = '\0';
172     } else
173     strcpy(cp, recv);
174 greg 2.1 return(oconvbuf);
175 greg 2.30 overrun:
176     fputs(progname, stderr);
177     fputs(": too many file arguments!\n", stderr);
178     exit(1);
179 greg 2.1 }
180    
181 greg 2.51 #if defined(_WIN32) || defined(_WIN64)
182    
183 greg 2.1 /* 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 greg 2.16 if (verbose > 0)
195 greg 2.1 fprintf(stderr, "%s: opening pipe %s: %s\n",
196     progname, (*mode=='w') ? "to" : "from", cmd);
197     return(popen(cmd, mode));
198     }
199    
200 greg 2.51 #define pclose_al pclose
201    
202 greg 2.1 /* 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 greg 2.16 if (verbose > 0)
214 greg 2.1 fprintf(stderr, "%s: running: %s\n", progname, cmd);
215     return(system(cmd));
216     }
217 greg 2.51
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 greg 2.1 /* 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 greg 2.16 if (verbose > 0) {
278 greg 2.1 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 greg 2.51
288 greg 2.1 #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 greg 2.6 parse_params(PARAMS *p, char *pargs)
333 greg 2.1 {
334     char *cp = pargs;
335     int nparams = 0;
336 greg 2.32 int quot;
337 greg 2.1 int i;
338    
339 greg 2.15 for ( ; ; ) {
340 greg 2.1 switch (*cp++) {
341     case 'h':
342     if (*cp++ != '=')
343     break;
344 greg 2.26 if ((*cp == '+') | (*cp == '-'))
345     p->sign = *cp++;
346     else
347     p->sign = '+';
348 greg 2.6 p->hsiz = 0;
349 greg 2.1 i = 0;
350     while (*cp && !isspace(*cp)) {
351     if (isdigit(*cp))
352 greg 2.6 p->hsiz = 10*p->hsiz + *cp - '0';
353     p->hemis[i++] = *cp++;
354 greg 2.1 }
355     if (!i)
356     break;
357 greg 2.6 p->hemis[i] = '\0';
358     p->hsiz += !p->hsiz;
359 greg 2.1 ++nparams;
360     continue;
361     case 'u':
362     if (*cp++ != '=')
363     break;
364 greg 2.6 if (!get_direction(p->vup, cp))
365 greg 2.1 break;
366 greg 2.15 while (*cp && !isspace(*cp++))
367     ;
368 greg 2.1 ++nparams;
369     continue;
370     case 'o':
371     if (*cp++ != '=')
372     break;
373 greg 2.32 quot = 0;
374     if ((*cp == '"') | (*cp == '\''))
375     quot = *cp++;
376 greg 2.1 i = 0;
377 greg 2.32 while (*cp && (quot ? (*cp != quot) : !isspace(*cp))) {
378     i++; cp++;
379     }
380 greg 2.1 if (!i)
381     break;
382 greg 2.32 if (!*cp) {
383     if (quot)
384     break;
385     cp[1] = '\0';
386     }
387     *cp = '\0';
388 greg 2.6 p->outfn = savqstr(cp-i);
389 greg 2.32 *cp++ = quot ? quot : ' ';
390 greg 2.1 ++nparams;
391     continue;
392     case ' ':
393     case '\t':
394     case '\r':
395 greg 2.21 case '\n':
396 greg 2.15 continue;
397 greg 2.1 case '\0':
398     return(nparams);
399     default:
400     break;
401     }
402 greg 2.15 break;
403     }
404     fprintf(stderr, "%s: bad parameter string: %s", progname, pargs);
405 greg 2.1 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 greg 2.4 char sbuf[256];
414     int uniform = 0;
415 greg 2.2 char *calfn = NULL;
416 greg 2.3 char *params = NULL;
417 greg 2.2 char *binv = NULL;
418     char *binf = NULL;
419     char *nbins = NULL;
420 greg 2.1
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 greg 2.4 /* check arguments */
432 greg 2.1 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 greg 2.10 if (normalize(curparams.vup) == 0) {
443 greg 2.1 if (fabs(curparams.nrm[2]) < .7)
444     curparams.vup[2] = 1;
445     else
446     curparams.vup[1] = 1;
447 greg 2.10 }
448 greg 2.4 /* determine sample type/bin */
449 greg 2.44 if ((tolower(curparams.hemis[0]) == 'u') | (curparams.hemis[0] == '1')) {
450 greg 2.20 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 greg 2.4 uniform = 1;
455 greg 2.1 } else if (tolower(curparams.hemis[0]) == 's' &&
456     tolower(curparams.hemis[1]) == 'c') {
457 greg 2.2 /* assign parameters */
458 greg 2.1 if (curparams.hsiz <= 1) {
459     fputs(progname, stderr);
460     fputs(": missing size for Shirley-Chiu sampling!\n", stderr);
461     exit(1);
462     }
463 greg 2.3 calfn = shirchiufn; shirchiufn = NULL;
464 greg 2.26 sprintf(sbuf, "SCdim=%d,rNx=%g,rNy=%g,rNz=%g,Ux=%g,Uy=%g,Uz=%g,RHS=%c1",
465 greg 2.2 curparams.hsiz,
466     curparams.nrm[0], curparams.nrm[1], curparams.nrm[2],
467 greg 2.26 curparams.vup[0], curparams.vup[1], curparams.vup[2],
468     curparams.sign);
469 greg 2.3 params = savqstr(sbuf);
470 greg 2.2 binv = "scbin";
471     nbins = "SCdim*SCdim";
472 greg 2.1 } else if ((tolower(curparams.hemis[0]) == 'r') |
473     (tolower(curparams.hemis[0]) == 't')) {
474 greg 2.2 calfn = reinhfn; reinhfn = NULL;
475 greg 2.26 sprintf(sbuf, "MF=%d,rNx=%g,rNy=%g,rNz=%g,Ux=%g,Uy=%g,Uz=%g,RHS=%c1",
476 greg 2.3 curparams.hsiz,
477     curparams.nrm[0], curparams.nrm[1], curparams.nrm[2],
478 greg 2.26 curparams.vup[0], curparams.vup[1], curparams.vup[2],
479     curparams.sign);
480 greg 2.3 params = savqstr(sbuf);
481     binv = "rbin";
482     nbins = "Nrbins";
483 greg 2.1 } else if (tolower(curparams.hemis[0]) == 'k' &&
484     !curparams.hemis[1] |
485     (tolower(curparams.hemis[1]) == 'f') |
486     (curparams.hemis[1] == '1')) {
487 greg 2.2 calfn = kfullfn; kfullfn = NULL;
488     binf = "kbin";
489     nbins = "Nkbins";
490 greg 2.1 } else if (tolower(curparams.hemis[0]) == 'k' &&
491     (tolower(curparams.hemis[1]) == 'h') |
492     (curparams.hemis[1] == '2')) {
493 greg 2.2 calfn = khalffn; khalffn = NULL;
494     binf = "khbin";
495     nbins = "Nkhbins";
496 greg 2.1 } else if (tolower(curparams.hemis[0]) == 'k' &&
497     (tolower(curparams.hemis[1]) == 'q') |
498     (curparams.hemis[1] == '4')) {
499 greg 2.2 calfn = kquarterfn; kquarterfn = NULL;
500     binf = "kqbin";
501     nbins = "Nkqbins";
502 greg 2.1 } else {
503     fprintf(stderr, "%s: unrecognized hemisphere sampling: h=%s\n",
504     progname, curparams.hemis);
505     exit(1);
506     }
507 greg 2.26 if (tolower(curparams.hemis[0]) == 'k') {
508     sprintf(sbuf, "RHS=%c1", curparams.sign);
509     params = savqstr(sbuf);
510     }
511 greg 2.4 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 greg 2.2 if (calfn != NULL) { /* add cal file if needed */
521     CHECKARGC(2);
522     rcarg[nrcargs++] = "-f";
523     rcarg[nrcargs++] = calfn;
524     }
525 greg 2.3 if (params != NULL) { /* parameters _after_ cal file */
526     CHECKARGC(2);
527     rcarg[nrcargs++] = "-p";
528     rcarg[nrcargs++] = params;
529     }
530 greg 2.2 if (nbins != NULL) { /* add #bins if set */
531     CHECKARGC(2);
532     rcarg[nrcargs++] = "-bn";
533     rcarg[nrcargs++] = nbins;
534     }
535 greg 2.3 if (binv != NULL) {
536 greg 2.2 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 greg 2.3 rcarg[nrcargs++] = "-b";
542 greg 2.2 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 greg 2.1 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 greg 2.29 if (!getperpendicular(uva[0], nrm, 1)) {
559 greg 2.1 fputs(progname, stderr);
560     fputs(": bad surface normal in make_axes!\n", stderr);
561     exit(1);
562     }
563 greg 2.19 fcross(uva[1], nrm, uva[0]);
564 greg 2.1 }
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 greg 2.6 samp2[0] = sqrt(samp2[0]*sp->area*(1./PI) + sp->farg[6]*sp->farg[6]);
596 greg 2.1 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 greg 2.40 sizeof(struct ptri)*(sp->nfargs/3 - 3));
629 greg 2.1 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 greg 2.7 if (!polyTriangulate(v2l, add_triangle)) {
649     fprintf(stderr,
650     "%s: cannot triangulate polygon '%s'\n",
651     progname, sp->sname);
652 greg 2.1 return(0);
653 greg 2.7 }
654 greg 2.1 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 greg 2.40 for (i = 0; i < ptp->ntris-1 && x > ptp->tri[i].afrac; i++)
673 greg 2.1 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 greg 2.31 if (DOT(sp->snrm, rdir) >= FTINY) {
703 greg 2.7 fprintf(stderr,
704     "%s: internal - sample behind sender '%s'\n",
705     progname, sp->sname);
706     return(0);
707     }
708 greg 2.1 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 greg 2.37 if (projsa == NULL) {
714     fputs(progname, stderr);
715     fputs(": out of memory in sample_origin!\n", stderr);
716     exit(1);
717     }
718 greg 2.1 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 greg 2.52 tarea += projsa[i] *= (double)(projsa[i] > 0);
724 greg 2.1 }
725 greg 2.52 if (tarea < FTINY*FTINY) { /* wrong side of sender? */
726 greg 2.7 fputs(progname, stderr);
727     fputs(": internal - sample behind all sender elements!\n",
728     stderr);
729 greg 2.1 return(0);
730 greg 2.7 }
731 greg 2.1 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 greg 2.54 FVECT duvw, orig_dir[2];
744 greg 2.1 int i;
745    
746     if (fp == NULL) /* just requesting number of bins? */
747     return(1);
748    
749     while (n--) { /* stratified hemisphere sampling */
750     SDmultiSamp(samp3, 3, (n+frandom())/sampcnt);
751 greg 2.54 square2disk(duvw, samp3[1], samp3[2]);
752 greg 2.1 duvw[2] = -sqrt(1. - duvw[0]*duvw[0] - duvw[1]*duvw[1]);
753     for (i = 3; i--; )
754     orig_dir[1][i] = duvw[0]*p->udir[i] +
755     duvw[1]*p->vdir[i] +
756     duvw[2]*p->nrm[i] ;
757     if (!sample_origin(p, orig_dir[0], orig_dir[1], samp3[0]))
758     return(0);
759 greg 2.38 if (putbinary(orig_dir, sizeof(FVECT), 2, fp) != 2)
760 greg 2.1 return(0);
761     }
762     return(1);
763     }
764    
765     /* Shirly-Chiu sample generator */
766     static int
767     sample_shirchiu(PARAMS *p, int b, FILE *fp)
768     {
769     int n = sampcnt;
770     double samp3[3];
771 greg 2.54 FVECT duvw, orig_dir[2];
772 greg 2.1 int i;
773    
774     if (fp == NULL) /* just requesting number of bins? */
775     return(p->hsiz*p->hsiz);
776    
777     while (n--) { /* stratified sampling */
778     SDmultiSamp(samp3, 3, (n+frandom())/sampcnt);
779 greg 2.54 square2disk(duvw, (b/p->hsiz + samp3[1])/curparams.hsiz,
780 greg 2.1 (b%p->hsiz + samp3[2])/curparams.hsiz);
781     duvw[2] = sqrt(1. - duvw[0]*duvw[0] - duvw[1]*duvw[1]);
782     for (i = 3; i--; )
783     orig_dir[1][i] = -duvw[0]*p->udir[i] -
784     duvw[1]*p->vdir[i] -
785     duvw[2]*p->nrm[i] ;
786     if (!sample_origin(p, orig_dir[0], orig_dir[1], samp3[0]))
787     return(0);
788 greg 2.38 if (putbinary(orig_dir, sizeof(FVECT), 2, fp) != 2)
789 greg 2.1 return(0);
790     }
791     return(1);
792     }
793    
794     /* Reinhart/Tregenza sample generator */
795     static int
796     sample_reinhart(PARAMS *p, int b, FILE *fp)
797     {
798     #define T_NALT 7
799     static const int tnaz[T_NALT] = {30, 30, 24, 24, 18, 12, 6};
800     const int RowMax = T_NALT*p->hsiz + 1;
801 greg 2.7 const double RAH = (.5*PI)/(RowMax-.5);
802 greg 2.1 #define rnaz(r) (r >= RowMax-1 ? 1 : p->hsiz*tnaz[r/p->hsiz])
803     int n = sampcnt;
804     int row, col;
805     double samp3[3];
806     double alt, azi;
807     double duvw[3];
808     FVECT orig_dir[2];
809     int i;
810    
811     if (fp == NULL) { /* just requesting number of bins? */
812     n = 0;
813     for (row = RowMax; row--; ) n += rnaz(row);
814     return(n);
815     }
816     row = 0; /* identify row & column */
817     col = b;
818     while (col >= rnaz(row)) {
819     col -= rnaz(row);
820     ++row;
821     }
822     while (n--) { /* stratified sampling */
823     SDmultiSamp(samp3, 3, (n+frandom())/sampcnt);
824 greg 2.45 if (row >= RowMax-1) /* avoid crowding at zenith */
825     samp3[1] *= samp3[1];
826 greg 2.1 alt = (row+samp3[1])*RAH;
827     azi = (2.*PI)*(col+samp3[2]-.5)/rnaz(row);
828 greg 2.7 duvw[2] = cos(alt); /* measured from horizon */
829 greg 2.18 duvw[0] = tsin(azi)*duvw[2];
830 greg 2.45 duvw[1] = -tcos(azi)*duvw[2];
831 greg 2.1 duvw[2] = sqrt(1. - duvw[2]*duvw[2]);
832     for (i = 3; i--; )
833     orig_dir[1][i] = -duvw[0]*p->udir[i] -
834     duvw[1]*p->vdir[i] -
835     duvw[2]*p->nrm[i] ;
836     if (!sample_origin(p, orig_dir[0], orig_dir[1], samp3[0]))
837     return(0);
838 greg 2.38 if (putbinary(orig_dir, sizeof(FVECT), 2, fp) != 2)
839 greg 2.1 return(0);
840     }
841     return(1);
842     #undef rnaz
843     #undef T_NALT
844     }
845    
846     /* Klems sample generator */
847     static int
848     sample_klems(PARAMS *p, int b, FILE *fp)
849     {
850     static const char bname[4][20] = {
851     "LBNL/Klems Full",
852     "LBNL/Klems Half",
853     "INTERNAL ERROR",
854     "LBNL/Klems Quarter"
855     };
856     static ANGLE_BASIS *kbasis[4];
857     const int bi = p->hemis[1] - '1';
858     int n = sampcnt;
859     double samp2[2];
860     double duvw[3];
861     FVECT orig_dir[2];
862     int i;
863    
864     if (!kbasis[bi]) { /* need to get basis, first */
865     for (i = 4; i--; )
866     if (!strcasecmp(abase_list[i].name, bname[bi])) {
867     kbasis[bi] = &abase_list[i];
868     break;
869     }
870     if (i < 0) {
871     fprintf(stderr, "%s: unknown hemisphere basis '%s'\n",
872     progname, bname[bi]);
873     return(0);
874     }
875     }
876     if (fp == NULL) /* just requesting number of bins? */
877     return(kbasis[bi]->nangles);
878    
879     while (n--) { /* stratified sampling */
880     SDmultiSamp(samp2, 2, (n+frandom())/sampcnt);
881 greg 2.27 if (!fo_getvec(duvw, b+samp2[1], kbasis[bi]))
882 greg 2.1 return(0);
883     for (i = 3; i--; )
884 greg 2.26 orig_dir[1][i] = -duvw[0]*p->udir[i] -
885     duvw[1]*p->vdir[i] -
886 greg 2.1 duvw[2]*p->nrm[i] ;
887     if (!sample_origin(p, orig_dir[0], orig_dir[1], samp2[0]))
888     return(0);
889 greg 2.38 if (putbinary(orig_dir, sizeof(FVECT), 2, fp) != 2)
890 greg 2.1 return(0);
891     }
892     return(1);
893     }
894    
895     /* Prepare hemisphere basis sampler that will send rays to rcontrib */
896     static int
897     prepare_sampler(void)
898     {
899     if (curparams.slist == NULL) { /* missing sample surface! */
900     fputs(progname, stderr);
901     fputs(": no sender surface!\n", stderr);
902     return(-1);
903     }
904 greg 2.16 /* misplaced output file spec. */
905     if ((curparams.outfn != NULL) & (verbose >= 0))
906 greg 2.1 fprintf(stderr, "%s: warning - ignoring output file in sender ('%s')\n",
907     progname, curparams.outfn);
908     /* check/set basis hemisphere */
909     if (!curparams.hemis[0]) {
910     fputs(progname, stderr);
911     fputs(": missing sender sampling type!\n", stderr);
912     return(-1);
913     }
914     if (normalize(curparams.nrm) == 0) {
915     fputs(progname, stderr);
916     fputs(": undefined normal for sender sampling\n", stderr);
917     return(-1);
918     }
919 greg 2.10 if (normalize(curparams.vup) == 0) {
920 greg 2.1 if (fabs(curparams.nrm[2]) < .7)
921     curparams.vup[2] = 1;
922     else
923     curparams.vup[1] = 1;
924 greg 2.10 }
925 greg 2.26 fcross(curparams.udir, curparams.vup, curparams.nrm);
926 greg 2.1 if (normalize(curparams.udir) == 0) {
927     fputs(progname, stderr);
928     fputs(": up vector coincides with sender normal\n", stderr);
929     return(-1);
930     }
931 greg 2.26 fcross(curparams.vdir, curparams.nrm, curparams.udir);
932     if (curparams.sign == '-') { /* left-handed coordinate system? */
933     curparams.udir[0] *= -1.;
934     curparams.udir[1] *= -1.;
935     curparams.udir[2] *= -1.;
936     }
937 greg 2.44 if ((tolower(curparams.hemis[0]) == 'u') | (curparams.hemis[0] == '1'))
938 greg 2.1 curparams.sample_basis = sample_uniform;
939     else if (tolower(curparams.hemis[0]) == 's' &&
940     tolower(curparams.hemis[1]) == 'c')
941     curparams.sample_basis = sample_shirchiu;
942     else if ((tolower(curparams.hemis[0]) == 'r') |
943     (tolower(curparams.hemis[0]) == 't'))
944     curparams.sample_basis = sample_reinhart;
945     else if (tolower(curparams.hemis[0]) == 'k') {
946     switch (curparams.hemis[1]) {
947     case '1':
948     case '2':
949     case '4':
950     break;
951     case 'f':
952     case 'F':
953     case '\0':
954     curparams.hemis[1] = '1';
955     break;
956     case 'h':
957     case 'H':
958     curparams.hemis[1] = '2';
959     break;
960     case 'q':
961     case 'Q':
962     curparams.hemis[1] = '4';
963     break;
964     default:
965     goto unrecognized;
966     }
967     curparams.hemis[2] = '\0';
968     curparams.sample_basis = sample_klems;
969     } else
970     goto unrecognized;
971     /* return number of bins */
972     return((*curparams.sample_basis)(&curparams,0,NULL));
973     unrecognized:
974     fprintf(stderr, "%s: unrecognized sender sampling: h=%s\n",
975     progname, curparams.hemis);
976     return(-1);
977     }
978    
979     /* Compute normal and area for polygon */
980     static int
981     finish_polygon(SURF *p)
982     {
983     const int nv = p->nfargs / 3;
984     FVECT e1, e2, vc;
985     int i;
986    
987     memset(p->snrm, 0, sizeof(FVECT));
988     VSUB(e1, p->farg+3, p->farg);
989     for (i = 2; i < nv; i++) {
990     VSUB(e2, p->farg+3*i, p->farg);
991     VCROSS(vc, e1, e2);
992     p->snrm[0] += vc[0];
993     p->snrm[1] += vc[1];
994     p->snrm[2] += vc[2];
995     VCOPY(e1, e2);
996     }
997     p->area = normalize(p->snrm)*0.5;
998 greg 2.52 return(p->area > FTINY*FTINY);
999 greg 2.1 }
1000    
1001     /* Add a surface to our current parameters */
1002     static void
1003     add_surface(int st, const char *oname, FILE *fp)
1004     {
1005     SURF *snew;
1006     int n;
1007     /* get floating-point arguments */
1008     if (!fscanf(fp, "%d", &n)) return;
1009     while (n-- > 0) fscanf(fp, "%*s");
1010     if (!fscanf(fp, "%d", &n)) return;
1011     while (n-- > 0) fscanf(fp, "%*d");
1012     if (!fscanf(fp, "%d", &n) || n <= 0) return;
1013     snew = (SURF *)malloc(sizeof(SURF) + sizeof(double)*(n-1));
1014     if (snew == NULL) {
1015     fputs(progname, stderr);
1016 greg 2.37 fputs(": out of memory in add_surface!\n", stderr);
1017 greg 2.1 exit(1);
1018     }
1019     strncpy(snew->sname, oname, sizeof(snew->sname)-1);
1020     snew->sname[sizeof(snew->sname)-1] = '\0';
1021     snew->styp = st;
1022     snew->priv = NULL;
1023     snew->nfargs = n;
1024     for (n = 0; n < snew->nfargs; n++)
1025     if (fscanf(fp, "%lf", &snew->farg[n]) != 1) {
1026     fprintf(stderr, "%s: error reading arguments for '%s'\n",
1027     progname, oname);
1028     exit(1);
1029     }
1030     switch (st) {
1031     case ST_RING:
1032     if (snew->nfargs != 8)
1033     goto badcount;
1034     VCOPY(snew->snrm, snew->farg+3);
1035     if (normalize(snew->snrm) == 0)
1036     goto badnorm;
1037     if (snew->farg[7] < snew->farg[6]) {
1038     double t = snew->farg[7];
1039     snew->farg[7] = snew->farg[6];
1040     snew->farg[6] = t;
1041     }
1042     snew->area = PI*(snew->farg[7]*snew->farg[7] -
1043     snew->farg[6]*snew->farg[6]);
1044     break;
1045     case ST_POLY:
1046     if (snew->nfargs < 9 || snew->nfargs % 3)
1047     goto badcount;
1048     finish_polygon(snew);
1049     break;
1050     case ST_SOURCE:
1051     if (snew->nfargs != 4)
1052     goto badcount;
1053 greg 2.8 for (n = 3; n--; ) /* need to reverse "normal" */
1054     snew->snrm[n] = -snew->farg[n];
1055 greg 2.1 if (normalize(snew->snrm) == 0)
1056     goto badnorm;
1057 greg 2.4 snew->area = sin((PI/180./2.)*snew->farg[3]);
1058     snew->area *= PI*snew->area;
1059 greg 2.1 break;
1060     }
1061 greg 2.52 if ((snew->area <= FTINY*FTINY) & (verbose >= 0)) {
1062 greg 2.1 fprintf(stderr, "%s: warning - zero area for surface '%s'\n",
1063     progname, oname);
1064     free(snew);
1065     return;
1066     }
1067     VSUM(curparams.nrm, curparams.nrm, snew->snrm, snew->area);
1068     snew->next = curparams.slist;
1069     curparams.slist = snew;
1070     curparams.nsurfs++;
1071     return;
1072     badcount:
1073 greg 2.7 fprintf(stderr, "%s: bad argument count for surface element '%s'\n",
1074 greg 2.1 progname, oname);
1075     exit(1);
1076     badnorm:
1077 greg 2.7 fprintf(stderr, "%s: bad orientation for surface element '%s'\n",
1078 greg 2.1 progname, oname);
1079     exit(1);
1080     }
1081    
1082     /* Parse a receiver object (look for modifiers to add to rcontrib command) */
1083     static int
1084     add_recv_object(FILE *fp)
1085     {
1086     int st;
1087     char thismod[128], otype[32], oname[128];
1088     int n;
1089    
1090     if (fscanf(fp, "%s %s %s", thismod, otype, oname) != 3)
1091     return(0); /* must have hit EOF! */
1092     if (!strcmp(otype, "alias")) {
1093     fscanf(fp, "%*s"); /* skip alias */
1094     return(0);
1095     }
1096     /* is it a new receiver? */
1097     if ((st = surf_type(otype)) != ST_NONE) {
1098     if (curparams.slist != NULL && (st == ST_SOURCE) ^
1099     (curparams.slist->styp == ST_SOURCE)) {
1100     fputs(progname, stderr);
1101     fputs(": cannot mix source/non-source receivers!\n", stderr);
1102     return(-1);
1103     }
1104     if (strcmp(thismod, curmod)) {
1105     if (curmod[0]) { /* output last receiver? */
1106     finish_receiver();
1107     clear_params(&curparams, 1);
1108     }
1109 greg 2.6 parse_params(&curparams, newparams);
1110     newparams[0] = '\0';
1111 greg 2.1 strcpy(curmod, thismod);
1112     }
1113     add_surface(st, oname, fp); /* read & store surface */
1114     return(1);
1115     }
1116     /* else skip arguments */
1117 greg 2.8 if (!fscanf(fp, "%d", &n)) return(0);
1118 greg 2.1 while (n-- > 0) fscanf(fp, "%*s");
1119 greg 2.10 if (!fscanf(fp, "%d", &n)) return(0);
1120 greg 2.1 while (n-- > 0) fscanf(fp, "%*d");
1121 greg 2.10 if (!fscanf(fp, "%d", &n)) return(0);
1122 greg 2.1 while (n-- > 0) fscanf(fp, "%*f");
1123     return(0);
1124     }
1125    
1126     /* Parse a sender object */
1127     static int
1128     add_send_object(FILE *fp)
1129     {
1130     int st;
1131 greg 2.24 char thismod[128], otype[32], oname[128];
1132 greg 2.1 int n;
1133    
1134 greg 2.25 if (fscanf(fp, "%s %s %s", thismod, otype, oname) != 3)
1135 greg 2.1 return(0); /* must have hit EOF! */
1136     if (!strcmp(otype, "alias")) {
1137     fscanf(fp, "%*s"); /* skip alias */
1138     return(0);
1139     }
1140     /* is it a new surface? */
1141     if ((st = surf_type(otype)) != ST_NONE) {
1142     if (st == ST_SOURCE) {
1143     fputs(progname, stderr);
1144     fputs(": cannot use source as a sender!\n", stderr);
1145     return(-1);
1146     }
1147 greg 2.24 if (strcmp(thismod, curmod)) {
1148     if (curmod[0]) {
1149     fputs(progname, stderr);
1150     fputs(": warning - multiple modifiers in sender\n",
1151     stderr);
1152     }
1153     strcpy(curmod, thismod);
1154     }
1155 greg 2.6 parse_params(&curparams, newparams);
1156     newparams[0] = '\0';
1157 greg 2.1 add_surface(st, oname, fp); /* read & store surface */
1158     return(0);
1159     }
1160     /* else skip arguments */
1161 greg 2.8 if (!fscanf(fp, "%d", &n)) return(0);
1162 greg 2.1 while (n-- > 0) fscanf(fp, "%*s");
1163 greg 2.10 if (!fscanf(fp, "%d", &n)) return(0);
1164 greg 2.1 while (n-- > 0) fscanf(fp, "%*d");
1165 greg 2.10 if (!fscanf(fp, "%d", &n)) return(0);
1166 greg 2.1 while (n-- > 0) fscanf(fp, "%*f");
1167     return(0);
1168     }
1169    
1170     /* Load a Radiance scene using the given callback function for objects */
1171     static int
1172     load_scene(const char *inspec, int (*ocb)(FILE *))
1173     {
1174     int rv = 0;
1175     char inpbuf[1024];
1176     FILE *fp;
1177     int c;
1178    
1179     if (*inspec == '!')
1180     fp = popen(inspec+1, "r");
1181     else
1182     fp = fopen(inspec, "r");
1183     if (fp == NULL) {
1184     fprintf(stderr, "%s: cannot load '%s'\n", progname, inspec);
1185     return(-1);
1186     }
1187     while ((c = getc(fp)) != EOF) { /* load receiver data */
1188     if (isspace(c)) /* skip leading white space */
1189     continue;
1190     if (c == '!') { /* read from a new command */
1191     inpbuf[0] = c;
1192     if (fgetline(inpbuf+1, sizeof(inpbuf)-1, fp) != NULL) {
1193     if ((c = load_scene(inpbuf, ocb)) < 0)
1194     return(c);
1195     rv += c;
1196     }
1197     continue;
1198     }
1199     if (c == '#') { /* parameters/comment */
1200 greg 2.5 if ((c = getc(fp)) == EOF || ungetc(c,fp) == EOF)
1201     break;
1202     if (!isspace(c) && fscanf(fp, "%s", inpbuf) == 1 &&
1203 greg 2.1 !strcmp(inpbuf, PARAMSTART)) {
1204     if (fgets(inpbuf, sizeof(inpbuf), fp) != NULL)
1205 greg 2.6 strcat(newparams, inpbuf);
1206 greg 2.1 continue;
1207     }
1208 greg 2.10 while ((c = getc(fp)) != EOF && c != '\n')
1209 greg 2.1 ; /* else skipping comment */
1210     continue;
1211     }
1212     ungetc(c, fp); /* else check object for receiver */
1213     c = (*ocb)(fp);
1214     if (c < 0)
1215     return(c);
1216     rv += c;
1217     }
1218     /* close our input stream */
1219     c = (*inspec == '!') ? pclose(fp) : fclose(fp);
1220     if (c != 0) {
1221     fprintf(stderr, "%s: error loading '%s'\n", progname, inspec);
1222     return(-1);
1223     }
1224     return(rv);
1225     }
1226    
1227     /* Get command arguments and run program */
1228     int
1229     main(int argc, char *argv[])
1230     {
1231     char fmtopt[6] = "-faa"; /* default output is ASCII */
1232 greg 2.4 char *xrs=NULL, *yrs=NULL, *ldopt=NULL;
1233 greg 2.11 char *iropt = NULL;
1234 greg 2.1 char *sendfn;
1235     char sampcntbuf[32], nsbinbuf[32];
1236     FILE *rcfp;
1237     int nsbins;
1238     int a, i;
1239     /* screen rcontrib options */
1240     progname = argv[0];
1241 greg 2.14 for (a = 1; a < argc-2; a++) {
1242     int na;
1243     /* check for argument expansion */
1244     while ((na = expandarg(&argc, &argv, a)) > 0)
1245     ;
1246     if (na < 0) {
1247     fprintf(stderr, "%s: cannot expand '%s'\n",
1248     progname, argv[a]);
1249     return(1);
1250     }
1251     if (argv[a][0] != '-' || !argv[a][1])
1252     break;
1253     na = 1;
1254     switch (argv[a][1]) { /* !! Keep consistent !! */
1255 greg 2.1 case 'v': /* verbose mode */
1256 greg 2.16 verbose = 1;
1257 greg 2.1 na = 0;
1258     continue;
1259     case 'f': /* special case for -fo, -ff, etc. */
1260     switch (argv[a][2]) {
1261     case '\0': /* cal file */
1262     goto userr;
1263     case 'o': /* force output */
1264     goto userr;
1265     case 'a': /* output format */
1266     case 'f':
1267     case 'd':
1268     case 'c':
1269 greg 2.6 if (!(fmtopt[3] = argv[a][3]))
1270     fmtopt[3] = argv[a][2];
1271     fmtopt[2] = argv[a][2];
1272 greg 2.1 na = 0;
1273     continue; /* will pass later */
1274     default:
1275     goto userr;
1276     }
1277     break;
1278 greg 2.4 case 'x': /* x-resolution */
1279     xrs = argv[++a];
1280     na = 0;
1281     continue;
1282     case 'y': /* y-resolution */
1283     yrs = argv[++a];
1284     na = 0;
1285     continue;
1286 greg 2.1 case 'c': /* number of samples */
1287 greg 2.6 sampcnt = atoi(argv[++a]);
1288 greg 2.1 if (sampcnt <= 0)
1289     goto userr;
1290     na = 0; /* we re-add this later */
1291     continue;
1292 greg 2.8 case 'I': /* only for pass-through mode */
1293 greg 2.11 case 'i':
1294     iropt = argv[a];
1295 greg 2.8 na = 0;
1296     continue;
1297 greg 2.16 case 'w': /* options without arguments */
1298 greg 2.49 if (!argv[a][2] || strchr("+1tTyY", argv[a][2]) == NULL)
1299     verbose = -1;
1300     break;
1301 greg 2.16 case 'V':
1302 greg 2.1 case 'u':
1303     case 'h':
1304 greg 2.4 case 'r':
1305 greg 2.1 break;
1306     case 'n': /* options with 1 argument */
1307     case 's':
1308     case 'o':
1309 greg 2.53 case 't':
1310 greg 2.1 na = 2;
1311     break;
1312     case 'b': /* special case */
1313     if (argv[a][2] != 'v') goto userr;
1314     break;
1315     case 'l': /* special case */
1316 greg 2.4 if (argv[a][2] == 'd') {
1317     ldopt = argv[a];
1318     na = 0;
1319     continue;
1320     }
1321 greg 2.1 na = 2;
1322     break;
1323     case 'd': /* special case */
1324     if (argv[a][2] != 'v') na = 2;
1325     break;
1326     case 'a': /* special case */
1327 greg 2.28 if (argv[a][2] == 'p') {
1328     na = 2; /* photon map [+ bandwidth(s)] */
1329     if (a < argc-3 && atoi(argv[a+1]) > 0)
1330     na += 1 + (a < argc-4 && atoi(argv[a+2]) > 0);
1331     } else
1332     na = (argv[a][2] == 'v') ? 4 : 2;
1333 greg 2.1 break;
1334     case 'm': /* special case */
1335     if (!argv[a][2]) goto userr;
1336     na = (argv[a][2] == 'e') | (argv[a][2] == 'a') ? 4 : 2;
1337     break;
1338     default: /* anything else is verbotten */
1339     goto userr;
1340     }
1341     if (na <= 0) continue;
1342     CHECKARGC(na); /* pass on option */
1343     rcarg[nrcargs++] = argv[a];
1344     while (--na) /* + arguments if any */
1345     rcarg[nrcargs++] = argv[++a];
1346     }
1347     if (a > argc-2)
1348     goto userr; /* check at end of options */
1349     sendfn = argv[a++]; /* assign sender & receiver inputs */
1350     if (sendfn[0] == '-') { /* user wants pass-through mode? */
1351     if (sendfn[1]) goto userr;
1352     sendfn = NULL;
1353 greg 2.11 if (iropt) {
1354 greg 2.8 CHECKARGC(1);
1355 greg 2.11 rcarg[nrcargs++] = iropt;
1356 greg 2.8 }
1357 greg 2.4 if (xrs) {
1358     CHECKARGC(2);
1359     rcarg[nrcargs++] = "-x";
1360     rcarg[nrcargs++] = xrs;
1361     }
1362     if (yrs) {
1363     CHECKARGC(2);
1364     rcarg[nrcargs++] = "-y";
1365     rcarg[nrcargs++] = yrs;
1366     }
1367     if (ldopt) {
1368     CHECKARGC(1);
1369     rcarg[nrcargs++] = ldopt;
1370     }
1371 greg 2.1 if (sampcnt <= 0) sampcnt = 1;
1372 greg 2.8 } else { /* else in sampling mode */
1373 greg 2.11 if (iropt) {
1374 greg 2.8 fputs(progname, stderr);
1375 greg 2.11 fputs(": -i, -I supported for pass-through only\n", stderr);
1376 greg 2.8 return(1);
1377     }
1378 greg 2.42 #ifdef SMLFLT
1379     fmtopt[2] = 'f';
1380     #else
1381     fmtopt[2] = 'd';
1382     #endif
1383 greg 2.1 if (sampcnt <= 0) sampcnt = 10000;
1384     }
1385     sprintf(sampcntbuf, "%d", sampcnt);
1386     CHECKARGC(3); /* add our format & sample count */
1387     rcarg[nrcargs++] = fmtopt;
1388     rcarg[nrcargs++] = "-c";
1389     rcarg[nrcargs++] = sampcntbuf;
1390     /* add receiver arguments to rcontrib */
1391     if (load_scene(argv[a], add_recv_object) < 0)
1392     return(1);
1393     finish_receiver();
1394     if (sendfn == NULL) { /* pass-through mode? */
1395     CHECKARGC(1); /* add octree */
1396     rcarg[nrcargs++] = oconv_command(argc-a, argv+a);
1397     rcarg[nrcargs] = NULL;
1398     return(my_exec(rcarg)); /* rcontrib does everything */
1399     }
1400     clear_params(&curparams, 0); /* else load sender surface & params */
1401 greg 2.24 curmod[0] = '\0';
1402 greg 2.1 if (load_scene(sendfn, add_send_object) < 0)
1403     return(1);
1404     if ((nsbins = prepare_sampler()) <= 0)
1405     return(1);
1406     CHECKARGC(3); /* add row count and octree */
1407     rcarg[nrcargs++] = "-y";
1408     sprintf(nsbinbuf, "%d", nsbins);
1409     rcarg[nrcargs++] = nsbinbuf;
1410     rcarg[nrcargs++] = oconv_command(argc-a, argv+a);
1411     rcarg[nrcargs] = NULL;
1412     /* open pipe to rcontrib process */
1413     if ((rcfp = popen_arglist(rcarg, "w")) == NULL)
1414     return(1);
1415     SET_FILE_BINARY(rcfp);
1416     #ifdef getc_unlocked
1417     flockfile(rcfp);
1418     #endif
1419 greg 2.22 if (verbose > 0) {
1420 greg 2.1 fprintf(stderr, "%s: sampling %d directions", progname, nsbins);
1421     if (curparams.nsurfs > 1)
1422 greg 2.7 fprintf(stderr, " (%d elements)\n", curparams.nsurfs);
1423 greg 2.1 else
1424     fputc('\n', stderr);
1425     }
1426     for (i = 0; i < nsbins; i++) /* send rcontrib ray samples */
1427     if (!(*curparams.sample_basis)(&curparams, i, rcfp))
1428     return(1);
1429 greg 2.51 return(pclose_al(rcfp) < 0); /* all finished! */
1430 greg 2.1 userr:
1431     if (a < argc-2)
1432     fprintf(stderr, "%s: unsupported option '%s'", progname, argv[a]);
1433 greg 2.13 fprintf(stderr, "Usage: %s [-v][rcontrib options] sender.rad receiver.rad [-i system.oct] [system.rad ..]\n",
1434 greg 2.1 progname);
1435     return(1);
1436     }