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

File Contents

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