ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/util/rfluxmtx.c
Revision: 2.53
Committed: Wed Sep 9 21:28:19 2020 UTC (3 years, 7 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.52: +2 -1 lines
Log Message:
feat(rcontrib,rfluxmtx): Added -t option to specify progress report interval

File Contents

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