ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/src/util/rfluxmtx.c
Revision: 2.61
Committed: Fri Oct 17 17:24:47 2025 UTC (11 days, 22 hours ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.60: +21 -14 lines
Log Message:
feat(rfluxmtx): Made -v and -w settings independent

File Contents

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