ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/util/rfluxmtx.c
Revision: 2.60
Committed: Tue Jun 3 21:31:51 2025 UTC (45 hours, 28 minutes ago) by greg
Content type: text/plain
Branch: MAIN
CVS Tags: HEAD
Changes since 2.59: +3 -4 lines
Log Message:
refactor: More consistent use of global char * progname and fixargv0()

File Contents

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