ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/util/rfluxmtx.c
Revision: 2.13
Committed: Wed Aug 6 00:59:53 2014 UTC (9 years, 7 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.12: +12 -9 lines
Log Message:
Altered rfluxmtx so it could accept a starting octree

File Contents

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