ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/util/rfluxmtx.c
Revision: 2.4
Committed: Tue Jul 22 21:55:31 2014 UTC (9 years, 8 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.3: +47 -6 lines
Log Message:
Created man page for rfluxmtx and added some error checks

File Contents

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