ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/Development/ray/src/util/rfluxmtx.c
Revision: 2.64
Committed: Wed Oct 22 17:04:14 2025 UTC (4 weeks, 5 days ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.63: +2 -2 lines
Log Message:
fix(rfluxmtx): Missing newline in error message

File Contents

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