ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/util/rfluxmtx.c
Revision: 2.6
Committed: Wed Jul 23 00:40:17 2014 UTC (9 years, 9 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.5: +22 -19 lines
Log Message:
Fixed most of the other bugs

File Contents

# User Rev Content
1 greg 2.1 #ifndef lint
2 greg 2.6 static const char RCSid[] = "$Id: rfluxmtx.c,v 2.5 2014/07/22 23:21:56 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 greg 2.5 /* string indicating parameters */
57     const char PARAMSTART[] = "@rfluxmtx";
58 greg 2.1
59     /* surface type IDs */
60     #define ST_NONE 0
61     #define ST_POLY 1
62     #define ST_RING 2
63     #define ST_SOURCE 3
64    
65     typedef struct surf_s {
66     struct surf_s *next; /* next surface in list */
67     void *priv; /* private data (malloc'ed) */
68     char sname[32]; /* surface name */
69     FVECT snrm; /* surface normal */
70 greg 2.4 double area; /* surface area / proj. solid angle */
71 greg 2.1 short styp; /* surface type */
72 greg 2.2 short nfargs; /* number of real arguments */
73     double farg[1]; /* real values (extends struct) */
74 greg 2.1 } SURF; /* surface structure */
75    
76     typedef struct {
77     FVECT uva[2]; /* tangent axes */
78     int ntris; /* number of triangles */
79     struct ptri {
80     float afrac; /* fraction of total area */
81     short vndx[3]; /* vertex indices */
82     } tri[1]; /* triangle array (extends struct) */
83     } POLYTRIS; /* triangulated polygon */
84    
85     typedef struct param_s {
86     char hemis[32]; /* hemispherical sampling spec. */
87     int hsiz; /* hemisphere basis size */
88     int nsurfs; /* number of surfaces */
89     SURF *slist; /* list of surfaces */
90     FVECT vup; /* up vector (zero if unset) */
91     FVECT nrm; /* average normal direction */
92     FVECT udir, vdir; /* v-up tangent axes */
93     char *outfn; /* output file name (receiver) */
94     int (*sample_basis)(struct param_s *p, int, FILE *);
95     } PARAMS; /* sender/receiver parameters */
96    
97     PARAMS curparams;
98     char curmod[128];
99 greg 2.6 char newparams[1024];
100 greg 2.1
101     typedef int SURFSAMP(FVECT, SURF *, double);
102    
103     static SURFSAMP ssamp_bad, ssamp_poly, ssamp_ring;
104    
105     SURFSAMP *orig_in_surf[4] = {
106     ssamp_bad, ssamp_poly, ssamp_ring, ssamp_bad
107     };
108    
109     /* Clear parameter set */
110     static void
111     clear_params(PARAMS *p, int reset_only)
112     {
113     while (p->slist != NULL) {
114     SURF *sdel = p->slist;
115     p->slist = sdel->next;
116     if (sdel->priv != NULL)
117     free(sdel->priv);
118     free(sdel);
119     }
120     if (reset_only) {
121     p->nsurfs = 0;
122     memset(p->nrm, 0, sizeof(FVECT));
123     memset(p->vup, 0, sizeof(FVECT));
124     p->outfn = NULL;
125     return;
126     }
127 greg 2.6 memset(p, 0, sizeof(PARAMS));
128 greg 2.1 }
129    
130     /* Get surface type from name */
131     static int
132     surf_type(const char *otype)
133     {
134     if (!strcmp(otype, "polygon"))
135     return(ST_POLY);
136     if (!strcmp(otype, "ring"))
137     return(ST_RING);
138     if (!strcmp(otype, "source"))
139     return(ST_SOURCE);
140     return(ST_NONE);
141     }
142    
143     /* Add arguments to oconv command */
144     static char *
145     oconv_command(int ac, char *av[])
146     {
147     static char oconvbuf[2048] = "!oconv -f";
148     char *cp = oconvbuf + 9;
149    
150     while (ac-- > 0) {
151     if (cp >= oconvbuf+(sizeof(oconvbuf)-32)) {
152     fputs(progname, stderr);
153     fputs(": too many file arguments!\n", stderr);
154     exit(1);
155     }
156     *cp++ = ' ';
157     strcpy(cp, *av++);
158     while (*cp) cp++;
159     }
160     *cp = '\0';
161     return(oconvbuf);
162     }
163    
164     /* Check if any of the characters in str2 are found in str1 */
165     static int
166     matchany(const char *str1, const char *str2)
167     {
168     while (*str1) {
169     const char *cp = str2;
170     while (*cp)
171     if (*cp++ == *str1)
172     return(*str1);
173     ++str1;
174     }
175     return(0);
176     }
177    
178    
179     /* Convert a set of arguments into a command line for pipe() or system() */
180     static char *
181     convert_commandline(char *cmd, const int len, char *av[])
182     {
183     int match;
184     char *cp;
185    
186     for (cp = cmd; *av != NULL; av++) {
187     const int n = strlen(*av);
188     if (cp+n >= cmd+(len-3)) {
189     fputs(progname, stderr);
190     return(NULL);
191     }
192     if ((match = matchany(*av, SPECIALS))) {
193     const int quote =
194     #ifdef ALTQUOT
195     (match == QUOTCHAR) ? ALTQUOT :
196     #endif
197     QUOTCHAR;
198     *cp++ = quote;
199     strcpy(cp, *av);
200     cp += n;
201     *cp++ = quote;
202     } else {
203     strcpy(cp, *av);
204     cp += n;
205     }
206     *cp++ = ' ';
207     }
208     if (cp <= cmd)
209     return(NULL);
210     *--cp = '\0';
211     return(cmd);
212     }
213    
214     /* Open a pipe to/from a command given as an argument list */
215     static FILE *
216     popen_arglist(char *av[], char *mode)
217     {
218     char cmd[10240];
219    
220     if (!convert_commandline(cmd, sizeof(cmd), av)) {
221     fputs(progname, stderr);
222     fputs(": command line too long in popen_arglist()\n", stderr);
223     return(NULL);
224     }
225     if (verbose)
226     fprintf(stderr, "%s: opening pipe %s: %s\n",
227     progname, (*mode=='w') ? "to" : "from", cmd);
228     return(popen(cmd, mode));
229     }
230    
231     #ifdef _WIN32
232     /* Execute system command (Windows version) */
233     static int
234     my_exec(char *av[])
235     {
236     char cmd[10240];
237    
238     if (!convert_commandline(cmd, sizeof(cmd), av)) {
239     fputs(progname, stderr);
240     fputs(": command line too long in my_exec()\n", stderr);
241     return(1);
242     }
243     if (verbose)
244     fprintf(stderr, "%s: running: %s\n", progname, cmd);
245     return(system(cmd));
246     }
247     #else
248     /* Execute system command in our stead (Unix version) */
249     static int
250     my_exec(char *av[])
251     {
252     char *compath;
253    
254     if ((compath = getpath((char *)av[0], getenv("PATH"), X_OK)) == NULL) {
255     fprintf(stderr, "%s: cannot locate %s\n", progname, av[0]);
256     return(1);
257     }
258     if (verbose) {
259     char cmd[4096];
260     if (!convert_commandline(cmd, sizeof(cmd), av))
261     strcpy(cmd, "COMMAND TOO LONG TO SHOW");
262     fprintf(stderr, "%s: running: %s\n", progname, cmd);
263     }
264     execv(compath, av); /* successful call never returns */
265     perror(compath);
266     return(1);
267     }
268     #endif
269    
270     /* Get normalized direction vector from string specification */
271     static int
272     get_direction(FVECT dv, const char *s)
273     {
274     int sign = 1;
275     int axis = 0;
276    
277     memset(dv, 0, sizeof(FVECT));
278     nextchar:
279     switch (*s) {
280     case '+':
281     ++s;
282     goto nextchar;
283     case '-':
284     sign = -sign;
285     ++s;
286     goto nextchar;
287     case 'z':
288     case 'Z':
289     ++axis;
290     case 'y':
291     case 'Y':
292     ++axis;
293     case 'x':
294     case 'X':
295     dv[axis] = sign;
296     return(!s[1] | isspace(s[1]));
297     default:
298     break;
299     }
300     #ifdef SMLFLT
301     if (sscanf(s, "%f,%f,%f", &dv[0], &dv[1], &dv[2]) != 3)
302     #else
303     if (sscanf(s, "%lf,%lf,%lf", &dv[0], &dv[1], &dv[2]) != 3)
304     #endif
305     return(0);
306     dv[0] *= (RREAL)sign;
307     return(normalize(dv) > 0);
308     }
309    
310     /* Parse program parameters (directives) */
311     static int
312 greg 2.6 parse_params(PARAMS *p, char *pargs)
313 greg 2.1 {
314     char *cp = pargs;
315     int nparams = 0;
316     int i;
317    
318     for ( ; ; )
319     switch (*cp++) {
320     case 'h':
321     if (*cp++ != '=')
322     break;
323 greg 2.6 p->hsiz = 0;
324 greg 2.1 i = 0;
325     while (*cp && !isspace(*cp)) {
326     if (isdigit(*cp))
327 greg 2.6 p->hsiz = 10*p->hsiz + *cp - '0';
328     p->hemis[i++] = *cp++;
329 greg 2.1 }
330     if (!i)
331     break;
332 greg 2.6 p->hemis[i] = '\0';
333     p->hsiz += !p->hsiz;
334 greg 2.1 ++nparams;
335     continue;
336     case 'u':
337     if (*cp++ != '=')
338     break;
339 greg 2.6 if (!get_direction(p->vup, cp))
340 greg 2.1 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 greg 2.6 p->outfn = savqstr(cp-i);
353 greg 2.1 *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 greg 2.6 samp2[0] = sqrt(samp2[0]*sp->area*(1./PI) + sp->farg[6]*sp->farg[6]);
557 greg 2.1 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 greg 2.6 parse_params(&curparams, newparams);
1047     newparams[0] = '\0';
1048 greg 2.1 strcpy(curmod, thismod);
1049     }
1050     add_surface(st, oname, fp); /* read & store surface */
1051     return(1);
1052     }
1053     /* else skip arguments */
1054     if (!fscanf(fp, "%d", &n)) return;
1055     while (n-- > 0) fscanf(fp, "%*s");
1056     if (!fscanf(fp, "%d", &n)) return;
1057     while (n-- > 0) fscanf(fp, "%*d");
1058     if (!fscanf(fp, "%d", &n)) return;
1059     while (n-- > 0) fscanf(fp, "%*f");
1060     return(0);
1061     }
1062    
1063     /* Parse a sender object */
1064     static int
1065     add_send_object(FILE *fp)
1066     {
1067     int st;
1068     char otype[32], oname[128];
1069     int n;
1070    
1071     if (fscanf(fp, "%*s %s %s", otype, oname) != 2)
1072     return(0); /* must have hit EOF! */
1073     if (!strcmp(otype, "alias")) {
1074     fscanf(fp, "%*s"); /* skip alias */
1075     return(0);
1076     }
1077     /* is it a new surface? */
1078     if ((st = surf_type(otype)) != ST_NONE) {
1079     if (st == ST_SOURCE) {
1080     fputs(progname, stderr);
1081     fputs(": cannot use source as a sender!\n", stderr);
1082     return(-1);
1083     }
1084 greg 2.6 parse_params(&curparams, newparams);
1085     newparams[0] = '\0';
1086 greg 2.1 add_surface(st, oname, fp); /* read & store surface */
1087     return(0);
1088     }
1089     /* else skip arguments */
1090     if (!fscanf(fp, "%d", &n)) return;
1091     while (n-- > 0) fscanf(fp, "%*s");
1092     if (!fscanf(fp, "%d", &n)) return;
1093     while (n-- > 0) fscanf(fp, "%*d");
1094     if (!fscanf(fp, "%d", &n)) return;
1095     while (n-- > 0) fscanf(fp, "%*f");
1096     return(0);
1097     }
1098    
1099     /* Load a Radiance scene using the given callback function for objects */
1100     static int
1101     load_scene(const char *inspec, int (*ocb)(FILE *))
1102     {
1103     int rv = 0;
1104     char inpbuf[1024];
1105     FILE *fp;
1106     int c;
1107    
1108     if (*inspec == '!')
1109     fp = popen(inspec+1, "r");
1110     else
1111     fp = fopen(inspec, "r");
1112     if (fp == NULL) {
1113     fprintf(stderr, "%s: cannot load '%s'\n", progname, inspec);
1114     return(-1);
1115     }
1116     while ((c = getc(fp)) != EOF) { /* load receiver data */
1117     if (isspace(c)) /* skip leading white space */
1118     continue;
1119     if (c == '!') { /* read from a new command */
1120     inpbuf[0] = c;
1121     if (fgetline(inpbuf+1, sizeof(inpbuf)-1, fp) != NULL) {
1122     if ((c = load_scene(inpbuf, ocb)) < 0)
1123     return(c);
1124     rv += c;
1125     }
1126     continue;
1127     }
1128     if (c == '#') { /* parameters/comment */
1129 greg 2.5 if ((c = getc(fp)) == EOF || ungetc(c,fp) == EOF)
1130     break;
1131     if (!isspace(c) && fscanf(fp, "%s", inpbuf) == 1 &&
1132 greg 2.1 !strcmp(inpbuf, PARAMSTART)) {
1133     if (fgets(inpbuf, sizeof(inpbuf), fp) != NULL)
1134 greg 2.6 strcat(newparams, inpbuf);
1135 greg 2.1 continue;
1136     }
1137     while ((c = getc(fp)) != EOF && c != '\n');
1138     ; /* else skipping comment */
1139     continue;
1140     }
1141     ungetc(c, fp); /* else check object for receiver */
1142     c = (*ocb)(fp);
1143     if (c < 0)
1144     return(c);
1145     rv += c;
1146     }
1147     /* close our input stream */
1148     c = (*inspec == '!') ? pclose(fp) : fclose(fp);
1149     if (c != 0) {
1150     fprintf(stderr, "%s: error loading '%s'\n", progname, inspec);
1151     return(-1);
1152     }
1153     return(rv);
1154     }
1155    
1156     /* Get command arguments and run program */
1157     int
1158     main(int argc, char *argv[])
1159     {
1160     char fmtopt[6] = "-faa"; /* default output is ASCII */
1161 greg 2.4 char *xrs=NULL, *yrs=NULL, *ldopt=NULL;
1162 greg 2.1 char *sendfn;
1163     char sampcntbuf[32], nsbinbuf[32];
1164     FILE *rcfp;
1165     int nsbins;
1166     int a, i;
1167     /* screen rcontrib options */
1168     progname = argv[0];
1169     for (a = 1; a < argc-2 && argv[a][0] == '-'; a++) {
1170     int na = 1; /* !! Keep consistent !! */
1171     switch (argv[a][1]) {
1172     case 'v': /* verbose mode */
1173     verbose = !verbose;
1174     na = 0;
1175     continue;
1176     case 'f': /* special case for -fo, -ff, etc. */
1177     switch (argv[a][2]) {
1178     case '\0': /* cal file */
1179     goto userr;
1180     case 'o': /* force output */
1181     goto userr;
1182     case 'a': /* output format */
1183     case 'f':
1184     case 'd':
1185     case 'c':
1186 greg 2.6 if (!(fmtopt[3] = argv[a][3]))
1187     fmtopt[3] = argv[a][2];
1188     fmtopt[2] = argv[a][2];
1189 greg 2.1 na = 0;
1190     continue; /* will pass later */
1191     default:
1192     goto userr;
1193     }
1194     break;
1195 greg 2.4 case 'x': /* x-resolution */
1196     xrs = argv[++a];
1197     na = 0;
1198     continue;
1199     case 'y': /* y-resolution */
1200     yrs = argv[++a];
1201     na = 0;
1202     continue;
1203 greg 2.1 case 'c': /* number of samples */
1204 greg 2.6 sampcnt = atoi(argv[++a]);
1205 greg 2.1 if (sampcnt <= 0)
1206     goto userr;
1207     na = 0; /* we re-add this later */
1208     continue;
1209     case 'V': /* options without arguments */
1210     case 'w':
1211     case 'u':
1212     case 'i':
1213     case 'h':
1214 greg 2.4 case 'r':
1215 greg 2.1 break;
1216     case 'n': /* options with 1 argument */
1217     case 's':
1218     case 'o':
1219     na = 2;
1220     break;
1221     case 'b': /* special case */
1222     if (argv[a][2] != 'v') goto userr;
1223     break;
1224     case 'l': /* special case */
1225 greg 2.4 if (argv[a][2] == 'd') {
1226     ldopt = argv[a];
1227     na = 0;
1228     continue;
1229     }
1230 greg 2.1 na = 2;
1231     break;
1232     case 'd': /* special case */
1233     if (argv[a][2] != 'v') na = 2;
1234     break;
1235     case 'a': /* special case */
1236     na = (argv[a][2] == 'v') ? 4 : 2;
1237     break;
1238     case 'm': /* special case */
1239     if (!argv[a][2]) goto userr;
1240     na = (argv[a][2] == 'e') | (argv[a][2] == 'a') ? 4 : 2;
1241     break;
1242     case '\0': /* pass-through mode */
1243     goto done_opts;
1244     default: /* anything else is verbotten */
1245     goto userr;
1246     }
1247     if (na <= 0) continue;
1248     CHECKARGC(na); /* pass on option */
1249     rcarg[nrcargs++] = argv[a];
1250     while (--na) /* + arguments if any */
1251     rcarg[nrcargs++] = argv[++a];
1252     }
1253     done_opts:
1254     if (a > argc-2)
1255     goto userr; /* check at end of options */
1256     sendfn = argv[a++]; /* assign sender & receiver inputs */
1257     if (sendfn[0] == '-') { /* user wants pass-through mode? */
1258     if (sendfn[1]) goto userr;
1259     sendfn = NULL;
1260 greg 2.4 if (xrs) {
1261     CHECKARGC(2);
1262     rcarg[nrcargs++] = "-x";
1263     rcarg[nrcargs++] = xrs;
1264     }
1265     if (yrs) {
1266     CHECKARGC(2);
1267     rcarg[nrcargs++] = "-y";
1268     rcarg[nrcargs++] = yrs;
1269     }
1270     if (ldopt) {
1271     CHECKARGC(1);
1272     rcarg[nrcargs++] = ldopt;
1273     }
1274 greg 2.1 if (sampcnt <= 0) sampcnt = 1;
1275     } else { /* else FVECT determines input format */
1276 greg 2.6 fmtopt[2] = (sizeof(RREAL)==sizeof(double)) ? 'd' : 'f';
1277 greg 2.1 if (sampcnt <= 0) sampcnt = 10000;
1278     }
1279     sprintf(sampcntbuf, "%d", sampcnt);
1280     CHECKARGC(3); /* add our format & sample count */
1281     rcarg[nrcargs++] = fmtopt;
1282     rcarg[nrcargs++] = "-c";
1283     rcarg[nrcargs++] = sampcntbuf;
1284     /* add receiver arguments to rcontrib */
1285     if (load_scene(argv[a], add_recv_object) < 0)
1286     return(1);
1287     finish_receiver();
1288     if (sendfn == NULL) { /* pass-through mode? */
1289     CHECKARGC(1); /* add octree */
1290     rcarg[nrcargs++] = oconv_command(argc-a, argv+a);
1291     rcarg[nrcargs] = NULL;
1292     return(my_exec(rcarg)); /* rcontrib does everything */
1293     }
1294     clear_params(&curparams, 0); /* else load sender surface & params */
1295     if (load_scene(sendfn, add_send_object) < 0)
1296     return(1);
1297     if ((nsbins = prepare_sampler()) <= 0)
1298     return(1);
1299     CHECKARGC(3); /* add row count and octree */
1300     rcarg[nrcargs++] = "-y";
1301     sprintf(nsbinbuf, "%d", nsbins);
1302     rcarg[nrcargs++] = nsbinbuf;
1303     rcarg[nrcargs++] = oconv_command(argc-a, argv+a);
1304     rcarg[nrcargs] = NULL;
1305     /* open pipe to rcontrib process */
1306     if ((rcfp = popen_arglist(rcarg, "w")) == NULL)
1307     return(1);
1308     SET_FILE_BINARY(rcfp);
1309     #ifdef getc_unlocked
1310     flockfile(rcfp);
1311     #endif
1312     if (verbose) {
1313     fprintf(stderr, "%s: sampling %d directions", progname, nsbins);
1314     if (curparams.nsurfs > 1)
1315     fprintf(stderr, " (%d surfaces)\n", curparams.nsurfs);
1316     else
1317     fputc('\n', stderr);
1318     }
1319     for (i = 0; i < nsbins; i++) /* send rcontrib ray samples */
1320     if (!(*curparams.sample_basis)(&curparams, i, rcfp))
1321     return(1);
1322     return(pclose(rcfp) == 0); /* all finished! */
1323     userr:
1324     if (a < argc-2)
1325     fprintf(stderr, "%s: unsupported option '%s'", progname, argv[a]);
1326 greg 2.5 fprintf(stderr, "Usage: %s [-v][rcontrib options] sender.rad receiver.rad [system.rad ..]\n",
1327 greg 2.1 progname);
1328     return(1);
1329     }