ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/util/rfluxmtx.c
(Generate patch)

Comparing ray/src/util/rfluxmtx.c (file contents):
Revision 2.1 by greg, Sat Jul 19 01:19:49 2014 UTC vs.
Revision 2.32 by greg, Fri Dec 4 22:16:18 2015 UTC

# Line 11 | Line 11 | static const char RCSid[] = "$Id$";
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"
# Line 23 | Line 24 | static const char RCSid[] = "$Id$";
24   #endif
25  
26   #ifdef _WIN32
27 < #define SPECIALS        " \t$*?"
27 > #define SPECIALS        " \t\"$*?"
28   #define QUOTCHAR        '"'
29   #else
30 < #define SPECIALS        " \t\n'\"()${}*?[]"
30 > #define SPECIALS        " \t\n'\"()${}*?[];|&"
31   #define QUOTCHAR        '\''
32   #define ALTQUOT         '"'
33   #endif
# Line 35 | Line 36 | static const char RCSid[] = "$Id$";
36  
37   char            *progname;              /* global argv[0] */
38  
39 < int             verbose = 0;            /* verbose mode? */
39 > int             verbose = 0;            /* verbose mode (< 0 no warnings) */
40  
41   char            *rcarg[MAXRCARG+1] = {"rcontrib", "-fo+"};
42   int             nrcargs = 2;
# Line 45 | Line 46 | const char     overflowerr[] = "%s: too many arguments!\n"
46   #define CHECKARGC(n)    if (nrcargs >= MAXRCARG-(n)) \
47          { fprintf(stderr, overflowerr, progname); exit(1); }
48  
49 < int             sampcnt = 0;            /* sample count (unset) */
49 > int             sampcnt = 0;            /* sample count (0==unset) */
50  
51 < char            *reinhfn = "reinhart.cal";
52 < char            *shirchufn = "disk2square.cal";
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 < #define PARAMSTART      "@rfluxmtx"     /* string indicating parameters */
57 >                                        /* string indicating parameters */
58 > const char      PARAMSTART[] = "@rfluxmtx";
59  
60                                  /* surface type IDs */
61   #define ST_NONE         0
# Line 66 | Line 68 | typedef struct surf_s {
68          void            *priv;          /* private data (malloc'ed) */
69          char            sname[32];      /* surface name */
70          FVECT           snrm;           /* surface normal */
71 <        double          area;           /* surface area (or solid angle) */
71 >        double          area;           /* surface area / proj. solid angle */
72          short           styp;           /* surface type */
73 <        short           nfargs;         /* number of arguments */
74 <        double          farg[1];        /* arguments (extends struct) */
73 >        short           nfargs;         /* number of real arguments */
74 >        double          farg[1];        /* real values (extends struct) */
75   } SURF;                         /* surface structure */
76  
77   typedef struct {
# Line 82 | Line 84 | typedef struct {
84   } POLYTRIS;                     /* triangulated polygon */
85  
86   typedef struct param_s {
87 <        char            hemis[32];      /* hemispherical sampling spec. */
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;     /* v-up tangent axes */
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  
# Line 108 | Line 112 | SURFSAMP       *orig_in_surf[4] = {
112   static void
113   clear_params(PARAMS *p, int reset_only)
114   {
111        curmod[0] = '\0';
115          while (p->slist != NULL) {
116                  SURF    *sdel = p->slist;
117                  p->slist = sdel->next;
# Line 123 | Line 126 | clear_params(PARAMS *p, int reset_only)
126                  p->outfn = NULL;
127                  return;
128          }
129 <        memset(p, 0, sizeof(curparams));
129 >        memset(p, 0, sizeof(PARAMS));
130   }
131  
132   /* Get surface type from name */
# Line 139 | Line 142 | surf_type(const char *otype)
142          return(ST_NONE);
143   }
144  
142 /* Add arguments to oconv command */
143 static char *
144 oconv_command(int ac, char *av[])
145 {
146        static char     oconvbuf[2048] = "!oconv -f";
147        char            *cp = oconvbuf + 9;
148
149        while (ac-- > 0) {
150                if (cp >= oconvbuf+(sizeof(oconvbuf)-32)) {
151                        fputs(progname, stderr);
152                        fputs(": too many file arguments!\n", stderr);
153                        exit(1);
154                }
155                *cp++ = ' ';
156                strcpy(cp, *av++);
157                while (*cp) cp++;
158        }
159        *cp = '\0';
160        return(oconvbuf);
161 }
162
145   /* Check if any of the characters in str2 are found in str1 */
146   static int
147   matchany(const char *str1, const char *str2)
# Line 174 | Line 156 | matchany(const char *str1, const char *str2)
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 *
# Line 188 | Line 205 | convert_commandline(char *cmd, const int len, char *av
205                          fputs(progname, stderr);
206                          return(NULL);
207                  }
208 <                if ((match = matchany(*av, SPECIALS))) {
208 >                if (matchany(*av, SPECIALS)) {
209                          const int       quote =
210   #ifdef ALTQUOT
211 <                                (match == QUOTCHAR) ? ALTQUOT :
211 >                                strchr(*av, QUOTCHAR) ? ALTQUOT :
212   #endif
213                                          QUOTCHAR;
214                          *cp++ = quote;
# Line 221 | Line 238 | popen_arglist(char *av[], char *mode)
238                  fputs(": command line too long in popen_arglist()\n", stderr);
239                  return(NULL);
240          }
241 <        if (verbose)
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));
# Line 239 | Line 256 | my_exec(char *av[])
256                  fputs(": command line too long in my_exec()\n", stderr);
257                  return(1);
258          }
259 <        if (verbose)
259 >        if (verbose > 0)
260                  fprintf(stderr, "%s: running: %s\n", progname, cmd);
261          return(system(cmd));
262   }
# Line 254 | Line 271 | my_exec(char *av[])
271                  fprintf(stderr, "%s: cannot locate %s\n", progname, av[0]);
272                  return(1);
273          }
274 <        if (verbose) {
274 >        if (verbose > 0) {
275                  char    cmd[4096];
276                  if (!convert_commandline(cmd, sizeof(cmd), av))
277                          strcpy(cmd, "COMMAND TOO LONG TO SHOW");
# Line 308 | Line 325 | nextchar:
325  
326   /* Parse program parameters (directives) */
327   static int
328 < parse_params(char *pargs)
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 ( ; ; )
335 >        for ( ; ; ) {
336                  switch (*cp++) {
337                  case 'h':
338                          if (*cp++ != '=')
339                                  break;
340 <                        curparams.hsiz = 0;
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 <                                        curparams.hsiz = 10*curparams.hsiz +
349 <                                                                *cp - '0';
328 <                                curparams.hemis[i++] = *cp++;
348 >                                        p->hsiz = 10*p->hsiz + *cp - '0';
349 >                                p->hemis[i++] = *cp++;
350                          }
351                          if (!i)
352                                  break;
353 <                        curparams.hemis[i] = '\0';
354 <                        curparams.hsiz += !curparams.hsiz;
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(curparams.vup, cp))
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 && !isspace(*cp++))
374 <                                i++;
373 >                        while (*cp && (quot ? (*cp != quot) : !isspace(*cp))) {
374 >                                i++; cp++;
375 >                        }
376                          if (!i)
377                                  break;
378 <                        *--cp = '\0';
379 <                        curparams.outfn = savqstr(cp-i);
380 <                        *cp++ = ' ';
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 ' ':
# Line 363 | Line 395 | parse_params(char *pargs)
395                  default:
396                          break;
397                  }
398 <        fprintf(stderr, "%s: bad parameter string '%s'\n", progname, pargs);
398 >                break;
399 >        }
400 >        fprintf(stderr, "%s: bad parameter string: %s", progname, pargs);
401          exit(1);
402          return(-1);     /* pro forma return */
403   }
# Line 372 | Line 406 | parse_params(char *pargs)
406   static void
407   finish_receiver(void)
408   {
409 <        char    binbuf[128];
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);
# Line 384 | Line 424 | finish_receiver(void)
424                  rcarg[nrcargs++] = "-o";
425                  rcarg[nrcargs++] = curparams.outfn;
426          }
427 <                                        /* add bin specification */
427 >                                        /* check arguments */
428          if (!curparams.hemis[0]) {
429                  fputs(progname, stderr);
430                  fputs(": missing hemisphere sampling type!\n", stderr);
# Line 395 | Line 435 | finish_receiver(void)
435                  fputs(": undefined normal for hemisphere sampling\n", stderr);
436                  exit(1);
437          }
438 <        if (normalize(curparams.vup) == 0)
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 <                CHECKARGC(2);
447 <                rcarg[nrcargs++] = "-b";
448 <                rcarg[nrcargs++] = "0";
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 <                if (shirchufn != NULL) {
410 <                        CHECKARGC(2);
411 <                        rcarg[nrcargs++] = "-f";
412 <                        rcarg[nrcargs++] = shirchufn;
413 <                        shirchufn = NULL;
414 <                }
415 < fputs("Shirley-Chiu unimplemented...\n",stderr); exit(1);
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 <                if (reinhfn != NULL) {
471 <                        CHECKARGC(2);
472 <                        rcarg[nrcargs++] = "-f";
473 <                        rcarg[nrcargs++] = reinhfn;
474 <                        reinhfn = NULL;
475 <                }
476 < fputs("Reinhart/Tregenza unimplemented...\n",stderr); exit(1);
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 <                if (kfullfn != NULL) {
484 <                        CHECKARGC(2);
485 <                        rcarg[nrcargs++] = "-f";
437 <                        rcarg[nrcargs++] = kfullfn;
438 <                        kfullfn = NULL;
439 <                }
440 <                CHECKARGC(4);
441 <                sprintf(binbuf, "kbin(%g,%g,%g,%g,%g,%g)",
442 <                        curparams.nrm[0], curparams.nrm[1], curparams.nrm[2],
443 <                        curparams.vup[0], curparams.vup[1], curparams.vup[2]);
444 <                rcarg[nrcargs++] = "-b";
445 <                rcarg[nrcargs++] = savqstr(binbuf);
446 <                rcarg[nrcargs++] = "-bn";
447 <                rcarg[nrcargs++] = "Nkbins";
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 <                if (khalffn != NULL) {
490 <                        CHECKARGC(2);
491 <                        rcarg[nrcargs++] = "-f";
454 <                        rcarg[nrcargs++] = khalffn;
455 <                        khalffn = NULL;
456 <                }
457 <                CHECKARGC(4);
458 <                sprintf(binbuf, "khbin(%g,%g,%g,%g,%g,%g)",
459 <                        curparams.nrm[0], curparams.nrm[1], curparams.nrm[2],
460 <                        curparams.vup[0], curparams.vup[1], curparams.vup[2]);
461 <                rcarg[nrcargs++] = "-b";
462 <                rcarg[nrcargs++] = savqstr(binbuf);
463 <                rcarg[nrcargs++] = "-bn";
464 <                rcarg[nrcargs++] = "Nkhbins";
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 <                if (kquarterfn != NULL) {
496 <                        CHECKARGC(2);
497 <                        rcarg[nrcargs++] = "-f";
471 <                        rcarg[nrcargs++] = kquarterfn;
472 <                        kquarterfn = NULL;
473 <                }
474 <                CHECKARGC(4);
475 <                sprintf(binbuf, "kqbin(%g,%g,%g,%g,%g,%g)",
476 <                        curparams.nrm[0], curparams.nrm[1], curparams.nrm[2],
477 <                        curparams.vup[0], curparams.vup[1], curparams.vup[2]);
478 <                rcarg[nrcargs++] = "-b";
479 <                rcarg[nrcargs++] = savqstr(binbuf);
480 <                rcarg[nrcargs++] = "-bn";
481 <                rcarg[nrcargs++] = "Nkqbins";
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 <        CHECKARGC(2);                   /* modifier argument goes last */
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   }
# Line 495 | Line 551 | make_axes(FVECT uva[2], const FVECT nrm)
551   {
552          int     i;
553  
554 <        uva[1][0] = 0.5 - frandom();
499 <        uva[1][1] = 0.5 - frandom();
500 <        uva[1][2] = 0.5 - frandom();
501 <        for (i = 3; i--; )
502 <                if ((-0.6 < nrm[i]) & (nrm[i] < 0.6))
503 <                        break;
504 <        if (i < 0) {
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 <        uva[1][i] = 1.0;
510 <        VCROSS(uva[0], uva[1], nrm);
511 <        normalize(uva[0]);
512 <        VCROSS(uva[1], nrm, uva[0]);
559 >        fcross(uva[1], nrm, uva[0]);
560   }
561  
562   /* Illegal sender surfaces end up here */
# Line 541 | Line 588 | ssamp_ring(FVECT orig, SURF *sp, double x)
588                  sp->priv = (void *)uva;
589          }
590          SDmultiSamp(samp2, 2, x);
591 <        samp2[0] = sp->farg[6] + sqrt(samp2[0]*sp->area*(1./PI));
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]);
# Line 594 | Line 641 | ssamp_poly(FVECT orig, SURF *sp, double x)
641                          }
642                          ptp->ntris = 0;
643                          v2l->p = (void *)ptp;
644 <                        if (!polyTriangulate(v2l, add_triangle))
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];
# Line 644 | Line 695 | sample_origin(PARAMS *p, FVECT orig, const FVECT rdir,
695                                          /* special case for lone surface */
696          if (p->nsurfs == 1) {
697                  sp = p->slist;
698 <                if (DOT(sp->snrm, rdir) >= -FTINY)
699 <                        return(0);      /* behind surface! */
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 */
# Line 659 | Line 714 | sample_origin(PARAMS *p, FVECT orig, const FVECT rdir,
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? */
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++];
# Line 733 | Line 792 | sample_reinhart(PARAMS *p, int b, FILE *fp)
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 = (.25*PI)/(RowMax-.5);
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;
# Line 758 | Line 817 | sample_reinhart(PARAMS *p, int b, FILE *fp)
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] = tcos(alt);    /* measured from horizon */
821 <                duvw[0] = tcos(azi)*duvw[2];
822 <                duvw[1] = tsin(azi)*duvw[2];
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] -
# Line 811 | Line 870 | sample_klems(PARAMS *p, int b, FILE *fp)
870  
871          while (n--) {                   /* stratified sampling */
872                  SDmultiSamp(samp2, 2, (n+frandom())/sampcnt);
873 <                if (!bo_getvec(duvw, b+samp2[1], kbasis[bi]))
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] +
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);
# Line 834 | Line 893 | prepare_sampler(void)
893                  fputs(": no sender surface!\n", stderr);
894                  return(-1);
895          }
896 <        if (curparams.outfn != NULL)    /* misplaced output file spec. */
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 */
# Line 848 | Line 908 | prepare_sampler(void)
908                  fputs(": undefined normal for sender sampling\n", stderr);
909                  return(-1);
910          }
911 <        if (normalize(curparams.vup) == 0)
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 <        VCROSS(curparams.udir, curparams.vup, curparams.nrm);
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 <        VCROSS(curparams.vdir, curparams.nrm, curparams.udir);
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' &&
# Line 976 | Line 1042 | add_surface(int st, const char *oname, FILE *fp)
1042          case ST_SOURCE:
1043                  if (snew->nfargs != 4)
1044                          goto badcount;
1045 <                VCOPY(snew->snrm, snew->farg);
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 = 2.*PI*(1. - cos((PI/180./2.)*snew->farg[3]));
1049 >                snew->area = sin((PI/180./2.)*snew->farg[3]);
1050 >                snew->area *= PI*snew->area;
1051                  break;
1052          }
1053 <        if (snew->area <= FTINY) {
1053 >        if ((snew->area <= FTINY) & (verbose >= 0)) {
1054                  fprintf(stderr, "%s: warning - zero area for surface '%s'\n",
1055                                  progname, oname);
1056                  free(snew);
# Line 994 | Line 1062 | add_surface(int st, const char *oname, FILE *fp)
1062          curparams.nsurfs++;
1063          return;
1064   badcount:
1065 <        fprintf(stderr, "%s: bad argument count for surface '%s'\n",
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 '%s'\n",
1069 >        fprintf(stderr, "%s: bad orientation for surface element '%s'\n",
1070                          progname, oname);
1071          exit(1);
1072   }
# Line 1030 | Line 1098 | add_recv_object(FILE *fp)
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;
1109 >        if (!fscanf(fp, "%d", &n)) return(0);
1110          while (n-- > 0) fscanf(fp, "%*s");
1111 <        if (!fscanf(fp, "%d", &n)) return;
1111 >        if (!fscanf(fp, "%d", &n)) return(0);
1112          while (n-- > 0) fscanf(fp, "%*d");
1113 <        if (!fscanf(fp, "%d", &n)) return;
1113 >        if (!fscanf(fp, "%d", &n)) return(0);
1114          while (n-- > 0) fscanf(fp, "%*f");
1115          return(0);
1116   }
# Line 1050 | Line 1120 | static int
1120   add_send_object(FILE *fp)
1121   {
1122          int             st;
1123 <        char            otype[32], oname[128];
1123 >        char            thismod[128], otype[32], oname[128];
1124          int             n;
1125  
1126 <        if (fscanf(fp, "%*s %s %s", otype, oname) != 2)
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 */
# Line 1066 | Line 1136 | add_send_object(FILE *fp)
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;
1153 >        if (!fscanf(fp, "%d", &n)) return(0);
1154          while (n-- > 0) fscanf(fp, "%*s");
1155 <        if (!fscanf(fp, "%d", &n)) return;
1155 >        if (!fscanf(fp, "%d", &n)) return(0);
1156          while (n-- > 0) fscanf(fp, "%*d");
1157 <        if (!fscanf(fp, "%d", &n)) return;
1157 >        if (!fscanf(fp, "%d", &n)) return(0);
1158          while (n-- > 0) fscanf(fp, "%*f");
1159          return(0);
1160   }
# Line 1109 | Line 1189 | load_scene(const char *inspec, int (*ocb)(FILE *))
1189                          continue;
1190                  }
1191                  if (c == '#') {         /* parameters/comment */
1192 <                        if (fscanf(fp, "%s", inpbuf) == 1 &&
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 <                                        parse_params(inpbuf);
1197 >                                        strcat(newparams, inpbuf);
1198                                  continue;
1199                          }
1200 <                        while ((c = getc(fp)) != EOF && c != '\n');
1200 >                        while ((c = getc(fp)) != EOF && c != '\n')
1201                                  ;       /* else skipping comment */
1202                          continue;
1203                  }
# Line 1139 | Line 1221 | 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;
# Line 1146 | Line 1230 | main(int argc, char *argv[])
1230          int     a, i;
1231                                          /* screen rcontrib options */
1232          progname = argv[0];
1233 <        for (a = 1; a < argc-2 && argv[a][0] == '-'; a++) {
1234 <                int     na = 1;         /* !! Keep consistent !! */
1235 <                switch (argv[a][1]) {
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 = !verbose;
1248 >                        verbose = 1;
1249                          na = 0;
1250                          continue;
1251                  case 'f':               /* special case for -fo, -ff, etc. */
# Line 1163 | Line 1258 | main(int argc, char *argv[])
1258                          case 'f':
1259                          case 'd':
1260                          case 'c':
1261 <                                if (!(fmtopt[4] = argv[a][3]))
1262 <                                        fmtopt[4] = argv[a][2];
1263 <                                fmtopt[3] = argv[a][2];
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+1]);
1279 >                        sampcnt = atoi(argv[++a]);
1280                          if (sampcnt <= 0)
1281                                  goto userr;
1282                          na = 0;         /* we re-add this later */
1283                          continue;
1284 <                case 'V':               /* options without arguments */
1182 <                case 'w':
1183 <                case 'u':
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':
# Line 1193 | Line 1302 | main(int argc, char *argv[])
1302                          if (argv[a][2] != 'v') goto userr;
1303                          break;
1304                  case 'l':               /* special case */
1305 <                        if (argv[a][2] == 'd') goto userr;
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 <                        na = (argv[a][2] == 'v') ? 4 : 2;
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;
1209                case '\0':              /* pass-through mode */
1210                        goto done_opts;
1327                  default:                /* anything else is verbotten */
1328                          goto userr;
1329                  }
# Line 1217 | Line 1333 | main(int argc, char *argv[])
1333                  while (--na)            /* + arguments if any */
1334                          rcarg[nrcargs++] = argv[++a];
1335          }
1220 done_opts:
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 FVECT determines input format */
1362 <                fmtopt[3] = (sizeof(RREAL)==sizeof(double)) ? 'd' : 'f';
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);
# Line 1245 | Line 1383 | done_opts:
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)
# Line 1262 | Line 1401 | done_opts:
1401   #ifdef getc_unlocked
1402          flockfile(rcfp);
1403   #endif
1404 <        if (verbose) {
1404 >        if (verbose > 0) {
1405                  fprintf(stderr, "%s: sampling %d directions", progname, nsbins);
1406                  if (curparams.nsurfs > 1)
1407 <                        fprintf(stderr, " (%d surfaces)\n", curparams.nsurfs);
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! */
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 [rcontrib options] sender.rad receiver.rad [system.rad ..]\n",
1418 >        fprintf(stderr, "Usage: %s [-v][rcontrib options] sender.rad receiver.rad [-i system.oct] [system.rad ..]\n",
1419                                  progname);
1420          return(1);
1421   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines