ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/util/rfluxmtx.c
Revision: 2.32
Committed: Fri Dec 4 22:16:18 2015 UTC (8 years, 4 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.31: +15 -5 lines
Log Message:
Added support for (quoted) output commands in rfluxmtx

File Contents

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