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.5 by greg, Tue Jul 22 23:21:56 2014 UTC vs.
Revision 2.53 by greg, Wed Sep 9 21:28:19 2020 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"
18   #include "triangulate.h"
19   #include "platform.h"
20  
21 < #ifdef getc_unlocked            /* avoid horrendous overhead of flockfile */
22 < #undef getc
22 < #define getc    getc_unlocked
21 > #ifndef MAXRCARG
22 > #define MAXRCARG        10000
23   #endif
24  
25 #ifdef _WIN32
26 #define SPECIALS        " \t\"$*?"
27 #define QUOTCHAR        '"'
28 #else
29 #define SPECIALS        " \t\n'\"()${}*?[];|&"
30 #define QUOTCHAR        '\''
31 #define ALTQUOT         '"'
32 #endif
33
34 #define MAXRCARG        512
35
25   char            *progname;              /* global argv[0] */
26  
27 < int             verbose = 0;            /* verbose mode? */
27 > int             verbose = 0;            /* verbose mode (< 0 no warnings) */
28  
29   char            *rcarg[MAXRCARG+1] = {"rcontrib", "-fo+"};
30   int             nrcargs = 2;
# Line 77 | Line 66 | typedef struct {
66          FVECT   uva[2];                 /* tangent axes */
67          int     ntris;                  /* number of triangles */
68          struct ptri {
69 <                float   afrac;                  /* fraction of total area */
69 >                double  afrac;                  /* fraction of total area */
70                  short   vndx[3];                /* vertex indices */
71          }       tri[1];                 /* triangle array (extends struct) */
72   } POLYTRIS;                     /* triangulated polygon */
73  
74   typedef struct param_s {
75 <        char            hemis[32];      /* hemispherical sampling spec. */
75 >        char            sign;           /* '-' for axis reversal */
76 >        char            hemis[31];      /* hemispherical sampling spec. */
77          int             hsiz;           /* hemisphere basis size */
78          int             nsurfs;         /* number of surfaces */
79          SURF            *slist;         /* list of surfaces */
80          FVECT           vup;            /* up vector (zero if unset) */
81          FVECT           nrm;            /* average normal direction */
82 <        FVECT           udir, vdir;     /* v-up tangent axes */
82 >        FVECT           udir, vdir;     /* tangent axes */
83          char            *outfn;         /* output file name (receiver) */
84          int             (*sample_basis)(struct param_s *p, int, FILE *);
85   } PARAMS;                       /* sender/receiver parameters */
86  
87   PARAMS          curparams;
88   char            curmod[128];
89 + char            newparams[1024];
90  
91   typedef int     SURFSAMP(FVECT, SURF *, double);
92  
# Line 109 | Line 100 | SURFSAMP       *orig_in_surf[4] = {
100   static void
101   clear_params(PARAMS *p, int reset_only)
102   {
112        curmod[0] = '\0';
103          while (p->slist != NULL) {
104                  SURF    *sdel = p->slist;
105                  p->slist = sdel->next;
# Line 124 | Line 114 | clear_params(PARAMS *p, int reset_only)
114                  p->outfn = NULL;
115                  return;
116          }
117 <        memset(p, 0, sizeof(curparams));
117 >        memset(p, 0, sizeof(PARAMS));
118   }
119  
120   /* Get surface type from name */
# Line 144 | Line 134 | surf_type(const char *otype)
134   static char *
135   oconv_command(int ac, char *av[])
136   {
137 <        static char     oconvbuf[2048] = "!oconv -f";
138 <        char            *cp = oconvbuf + 9;
139 <
140 <        while (ac-- > 0) {
141 <                if (cp >= oconvbuf+(sizeof(oconvbuf)-32)) {
142 <                        fputs(progname, stderr);
143 <                        fputs(": too many file arguments!\n", stderr);
144 <                        exit(1);
145 <                }
156 <                *cp++ = ' ';
157 <                strcpy(cp, *av++);
158 <                while (*cp) cp++;
137 >        static char     oconvbuf[2048] = "!oconv -f ";
138 >        char            *cp = oconvbuf + 10;
139 >        char            *recv = *av++;
140 >        
141 >        if (ac-- <= 0)
142 >                return(NULL);
143 >        if (verbose < 0) {      /* turn off warnings */
144 >                strcpy(cp, "-w ");
145 >                cp += 3;
146          }
147 <        *cp = '\0';
148 <        return(oconvbuf);
149 < }
150 <
151 < /* Check if any of the characters in str2 are found in str1 */
152 < static int
153 < matchany(const char *str1, const char *str2)
154 < {
155 <        while (*str1) {
169 <                const char      *cp = str2;
170 <                while (*cp)
171 <                        if (*cp++ == *str1)
172 <                                return(*str1);
173 <                ++str1;
174 <        }
175 <        return(0);
176 < }
177 <
178 <
179 < /* Convert a set of arguments into a command line for pipe() or system() */
180 < static char *
181 < convert_commandline(char *cmd, const int len, char *av[])
182 < {
183 <        int     match;
184 <        char    *cp;
185 <
186 <        for (cp = cmd; *av != NULL; av++) {
187 <                const int       n = strlen(*av);
188 <                if (cp+n >= cmd+(len-3)) {
189 <                        fputs(progname, stderr);
190 <                        return(NULL);
191 <                }
192 <                if ((match = matchany(*av, SPECIALS))) {
193 <                        const int       quote =
194 < #ifdef ALTQUOT
195 <                                (match == QUOTCHAR) ? ALTQUOT :
196 < #endif
197 <                                        QUOTCHAR;
198 <                        *cp++ = quote;
199 <                        strcpy(cp, *av);
200 <                        cp += n;
201 <                        *cp++ = quote;
147 >        while (ac-- > 0) {      /* copy each argument */
148 >                int     len = strlen(*av);
149 >                if (cp+len+4 >= oconvbuf+sizeof(oconvbuf))
150 >                        goto overrun;
151 >                if (matchany(*av, SPECIALS)) {
152 >                        *cp++ = QUOTCHAR;
153 >                        strcpy(cp, *av++);
154 >                        cp += len;
155 >                        *cp++ = QUOTCHAR;
156                  } else {
157 <                        strcpy(cp, *av);
158 <                        cp += n;
157 >                        strcpy(cp, *av++);
158 >                        cp += len;
159                  }
160                  *cp++ = ' ';
161          }
162 <        if (cp <= cmd)
163 <                return(NULL);
164 <        *--cp = '\0';
165 <        return(cmd);
162 >                                /* receiver goes last */
163 >        if (matchany(recv, SPECIALS)) {
164 >                *cp++ = QUOTCHAR;
165 >                while (*recv) {
166 >                        if (cp >= oconvbuf+(sizeof(oconvbuf)-3))
167 >                                goto overrun;
168 >                        *cp++ = *recv++;
169 >                }
170 >                *cp++ = QUOTCHAR;
171 >                *cp = '\0';
172 >        } else
173 >                strcpy(cp, recv);
174 >        return(oconvbuf);
175 > overrun:
176 >        fputs(progname, stderr);
177 >        fputs(": too many file arguments!\n", stderr);
178 >        exit(1);
179   }
180  
181 + #if defined(_WIN32) || defined(_WIN64)
182 +
183   /* Open a pipe to/from a command given as an argument list */
184   static FILE *
185   popen_arglist(char *av[], char *mode)
# Line 222 | Line 191 | popen_arglist(char *av[], char *mode)
191                  fputs(": command line too long in popen_arglist()\n", stderr);
192                  return(NULL);
193          }
194 <        if (verbose)
194 >        if (verbose > 0)
195                  fprintf(stderr, "%s: opening pipe %s: %s\n",
196                                  progname, (*mode=='w') ? "to" : "from", cmd);
197          return(popen(cmd, mode));
198   }
199  
200 < #ifdef _WIN32
200 > #define pclose_al       pclose
201 >
202   /* Execute system command (Windows version) */
203   static int
204   my_exec(char *av[])
# Line 240 | Line 210 | my_exec(char *av[])
210                  fputs(": command line too long in my_exec()\n", stderr);
211                  return(1);
212          }
213 <        if (verbose)
213 >        if (verbose > 0)
214                  fprintf(stderr, "%s: running: %s\n", progname, cmd);
215          return(system(cmd));
216   }
217 < #else
217 >
218 > #else   /* UNIX */
219 >
220 > static SUBPROC  rt_proc = SP_INACTIVE;  /* we only support one of these */
221 >
222 > /* Open a pipe to a command using an argument list */
223 > static FILE *
224 > popen_arglist(char *av[], char *mode)
225 > {
226 >        int     fd;
227 >
228 >        if (rt_proc.pid > 0) {
229 >                fprintf(stderr, "%s: only one i/o pipe at a time!\n", progname);
230 >                return(NULL);
231 >        }
232 >        if (verbose > 0) {
233 >                char    cmd[4096];
234 >                if (!convert_commandline(cmd, sizeof(cmd), av))
235 >                        strcpy(cmd, "COMMAND TOO LONG TO SHOW");
236 >                fprintf(stderr, "%s: opening pipe %s: %s\n",
237 >                                progname, (*mode=='w') ? "to" : "from", cmd);
238 >        }
239 >        if (*mode == 'w') {
240 >                fd = rt_proc.w = dup(fileno(stdout));
241 >                rt_proc.flags |= PF_FILT_OUT;
242 >        } else if (*mode == 'r') {
243 >                fd = rt_proc.r = dup(fileno(stdin));
244 >                rt_proc.flags |= PF_FILT_INP;
245 >        }
246 >        if (fd < 0 || open_process(&rt_proc, av) <= 0) {
247 >                perror(av[0]);
248 >                return(NULL);
249 >        }
250 >        return(fdopen(fd, mode));
251 > }
252 >
253 > /* Close command pipe (returns -1 on error to match pclose) */
254 > static int
255 > pclose_al(FILE *fp)
256 > {
257 >        int     prob = (fclose(fp) == EOF);
258 >
259 >        if (rt_proc.pid <= 0)
260 >                return(-1);
261 >
262 >        prob |= (close_process(&rt_proc) != 0);
263 >
264 >        return(-prob);
265 > }
266 >
267   /* Execute system command in our stead (Unix version) */
268   static int
269   my_exec(char *av[])
# Line 255 | Line 274 | my_exec(char *av[])
274                  fprintf(stderr, "%s: cannot locate %s\n", progname, av[0]);
275                  return(1);
276          }
277 <        if (verbose) {
277 >        if (verbose > 0) {
278                  char    cmd[4096];
279                  if (!convert_commandline(cmd, sizeof(cmd), av))
280                          strcpy(cmd, "COMMAND TOO LONG TO SHOW");
# Line 265 | Line 284 | my_exec(char *av[])
284          perror(compath);
285          return(1);
286   }
287 +
288   #endif
289  
290   /* Get normalized direction vector from string specification */
# Line 309 | Line 329 | nextchar:
329  
330   /* Parse program parameters (directives) */
331   static int
332 < parse_params(char *pargs)
332 > parse_params(PARAMS *p, char *pargs)
333   {
334          char    *cp = pargs;
335          int     nparams = 0;
336 +        int     quot;
337          int     i;
338  
339 <        for ( ; ; )
339 >        for ( ; ; ) {
340                  switch (*cp++) {
341                  case 'h':
342                          if (*cp++ != '=')
343                                  break;
344 <                        curparams.hsiz = 0;
344 >                        if ((*cp == '+') | (*cp == '-'))
345 >                                p->sign = *cp++;
346 >                        else
347 >                                p->sign = '+';
348 >                        p->hsiz = 0;
349                          i = 0;
350                          while (*cp && !isspace(*cp)) {
351                                  if (isdigit(*cp))
352 <                                        curparams.hsiz = 10*curparams.hsiz +
353 <                                                                *cp - '0';
329 <                                curparams.hemis[i++] = *cp++;
352 >                                        p->hsiz = 10*p->hsiz + *cp - '0';
353 >                                p->hemis[i++] = *cp++;
354                          }
355                          if (!i)
356                                  break;
357 <                        curparams.hemis[i] = '\0';
358 <                        curparams.hsiz += !curparams.hsiz;
357 >                        p->hemis[i] = '\0';
358 >                        p->hsiz += !p->hsiz;
359                          ++nparams;
360                          continue;
361                  case 'u':
362                          if (*cp++ != '=')
363                                  break;
364 <                        if (!get_direction(curparams.vup, cp))
364 >                        if (!get_direction(p->vup, cp))
365                                  break;
366 +                        while (*cp && !isspace(*cp++))
367 +                                ;
368                          ++nparams;
369                          continue;
370                  case 'o':
371                          if (*cp++ != '=')
372                                  break;
373 +                        quot = 0;
374 +                        if ((*cp == '"') | (*cp == '\''))
375 +                                quot = *cp++;
376                          i = 0;
377 <                        while (*cp && !isspace(*cp++))
378 <                                i++;
377 >                        while (*cp && (quot ? (*cp != quot) : !isspace(*cp))) {
378 >                                i++; cp++;
379 >                        }
380                          if (!i)
381                                  break;
382 <                        *--cp = '\0';
383 <                        curparams.outfn = savqstr(cp-i);
384 <                        *cp++ = ' ';
382 >                        if (!*cp) {
383 >                                if (quot)
384 >                                        break;
385 >                                cp[1] = '\0';
386 >                        }
387 >                        *cp = '\0';
388 >                        p->outfn = savqstr(cp-i);
389 >                        *cp++ = quot ? quot : ' ';
390                          ++nparams;
391                          continue;
392                  case ' ':
# Line 364 | Line 399 | parse_params(char *pargs)
399                  default:
400                          break;
401                  }
402 <        fprintf(stderr, "%s: bad parameter string '%s'\n", progname, pargs);
402 >                break;
403 >        }
404 >        fprintf(stderr, "%s: bad parameter string: %s", progname, pargs);
405          exit(1);
406          return(-1);     /* pro forma return */
407   }
# Line 402 | Line 439 | finish_receiver(void)
439                  fputs(": undefined normal for hemisphere sampling\n", stderr);
440                  exit(1);
441          }
442 <        if (normalize(curparams.vup) == 0)
442 >        if (normalize(curparams.vup) == 0) {
443                  if (fabs(curparams.nrm[2]) < .7)
444                          curparams.vup[2] = 1;
445                  else
446                          curparams.vup[1] = 1;
447 +        }
448                                          /* determine sample type/bin */
449 <        if (tolower(curparams.hemis[0]) == 'u' | curparams.hemis[0] == '1') {
450 <                binv = "0";             /* uniform sampling -- one bin */
449 >        if ((tolower(curparams.hemis[0]) == 'u') | (curparams.hemis[0] == '1')) {
450 >                sprintf(sbuf, "if(-Dx*%g-Dy*%g-Dz*%g,0,-1)",
451 >                        curparams.nrm[0], curparams.nrm[1], curparams.nrm[2]);
452 >                binv = savqstr(sbuf);
453 >                nbins = "1";            /* uniform sampling -- one bin */
454                  uniform = 1;
455          } else if (tolower(curparams.hemis[0]) == 's' &&
456                                  tolower(curparams.hemis[1]) == 'c') {
# Line 420 | Line 461 | finish_receiver(void)
461                          exit(1);
462                  }
463                  calfn = shirchiufn; shirchiufn = NULL;
464 <                sprintf(sbuf, "SCdim=%d,Nx=%g,Ny=%g,Nz=%g,Ux=%g,Uy=%g,Uz=%g",
464 >                sprintf(sbuf, "SCdim=%d,rNx=%g,rNy=%g,rNz=%g,Ux=%g,Uy=%g,Uz=%g,RHS=%c1",
465                                  curparams.hsiz,
466                          curparams.nrm[0], curparams.nrm[1], curparams.nrm[2],
467 <                        curparams.vup[0], curparams.vup[1], curparams.vup[2]);
467 >                        curparams.vup[0], curparams.vup[1], curparams.vup[2],
468 >                        curparams.sign);
469                  params = savqstr(sbuf);
470                  binv = "scbin";
471                  nbins = "SCdim*SCdim";
472          } else if ((tolower(curparams.hemis[0]) == 'r') |
473                          (tolower(curparams.hemis[0]) == 't')) {
474                  calfn = reinhfn; reinhfn = NULL;
475 <                sprintf(sbuf, "MF=%d,Nx=%g,Ny=%g,Nz=%g,Ux=%g,Uy=%g,Uz=%g",
475 >                sprintf(sbuf, "MF=%d,rNx=%g,rNy=%g,rNz=%g,Ux=%g,Uy=%g,Uz=%g,RHS=%c1",
476                                  curparams.hsiz,
477                          curparams.nrm[0], curparams.nrm[1], curparams.nrm[2],
478 <                        curparams.vup[0], curparams.vup[1], curparams.vup[2]);
478 >                        curparams.vup[0], curparams.vup[1], curparams.vup[2],
479 >                        curparams.sign);
480                  params = savqstr(sbuf);
481                  binv = "rbin";
482                  nbins = "Nrbins";
# Line 461 | Line 504 | finish_receiver(void)
504                                  progname, curparams.hemis);
505                  exit(1);
506          }
507 +        if (tolower(curparams.hemis[0]) == 'k') {
508 +                sprintf(sbuf, "RHS=%c1", curparams.sign);
509 +                params = savqstr(sbuf);
510 +        }
511          if (!uniform & (curparams.slist->styp == ST_SOURCE)) {
512                  SURF    *sp;
513                  for (sp = curparams.slist; sp != NULL; sp = sp->next)
# Line 508 | Line 555 | make_axes(FVECT uva[2], const FVECT nrm)
555   {
556          int     i;
557  
558 <        uva[1][0] = 0.5 - frandom();
512 <        uva[1][1] = 0.5 - frandom();
513 <        uva[1][2] = 0.5 - frandom();
514 <        for (i = 3; i--; )
515 <                if ((-0.6 < nrm[i]) & (nrm[i] < 0.6))
516 <                        break;
517 <        if (i < 0) {
558 >        if (!getperpendicular(uva[0], nrm, 1)) {
559                  fputs(progname, stderr);
560                  fputs(": bad surface normal in make_axes!\n", stderr);
561                  exit(1);
562          }
563 <        uva[1][i] = 1.0;
523 <        VCROSS(uva[0], uva[1], nrm);
524 <        normalize(uva[0]);
525 <        VCROSS(uva[1], nrm, uva[0]);
563 >        fcross(uva[1], nrm, uva[0]);
564   }
565  
566   /* Illegal sender surfaces end up here */
# Line 554 | Line 592 | ssamp_ring(FVECT orig, SURF *sp, double x)
592                  sp->priv = (void *)uva;
593          }
594          SDmultiSamp(samp2, 2, x);
595 <        samp2[0] = sp->farg[6] + sqrt(samp2[0]*sp->area*(1./PI));
595 >        samp2[0] = sqrt(samp2[0]*sp->area*(1./PI) + sp->farg[6]*sp->farg[6]);
596          samp2[1] *= 2.*PI;
597          uv[0] = samp2[0]*tcos(samp2[1]);
598          uv[1] = samp2[0]*tsin(samp2[1]);
# Line 607 | Line 645 | ssamp_poly(FVECT orig, SURF *sp, double x)
645                          }
646                          ptp->ntris = 0;
647                          v2l->p = (void *)ptp;
648 <                        if (!polyTriangulate(v2l, add_triangle))
648 >                        if (!polyTriangulate(v2l, add_triangle)) {
649 >                                fprintf(stderr,
650 >                                        "%s: cannot triangulate polygon '%s'\n",
651 >                                                progname, sp->sname);
652                                  return(0);
653 +                        }
654                          for (i = ptp->ntris; i--; ) {
655                                  int     a = ptp->tri[i].vndx[0];
656                                  int     b = ptp->tri[i].vndx[1];
# Line 627 | Line 669 | ssamp_poly(FVECT orig, SURF *sp, double x)
669                  sp->priv = (void *)ptp;
670          }
671                                          /* pick triangle by partial area */
672 <        for (i = 0; i < ptp->ntris && x > ptp->tri[i].afrac; i++)
672 >        for (i = 0; i < ptp->ntris-1 && x > ptp->tri[i].afrac; i++)
673                  x -= ptp->tri[i].afrac;
674          SDmultiSamp(samp2, 2, x/ptp->tri[i].afrac);
675          samp2[0] *= samp2[1] = sqrt(samp2[1]);
# Line 657 | Line 699 | sample_origin(PARAMS *p, FVECT orig, const FVECT rdir,
699                                          /* special case for lone surface */
700          if (p->nsurfs == 1) {
701                  sp = p->slist;
702 <                if (DOT(sp->snrm, rdir) >= -FTINY)
703 <                        return(0);      /* behind surface! */
702 >                if (DOT(sp->snrm, rdir) >= FTINY) {
703 >                        fprintf(stderr,
704 >                                "%s: internal - sample behind sender '%s'\n",
705 >                                        progname, sp->sname);
706 >                        return(0);
707 >                }
708                  return((*orig_in_surf[sp->styp])(orig, sp, x));
709          }
710          if (p->nsurfs > nall) {         /* (re)allocate surface area cache */
711                  if (projsa) free(projsa);
712                  projsa = (double *)malloc(sizeof(double)*p->nsurfs);
713 <                if (!projsa) return(0);
713 >                if (projsa == NULL) {
714 >                        fputs(progname, stderr);
715 >                        fputs(": out of memory in sample_origin!\n", stderr);
716 >                        exit(1);
717 >                }
718                  nall = p->nsurfs;
719          }
720                                          /* compute projected areas */
721          for (i = 0, sp = p->slist; sp != NULL; i++, sp = sp->next) {
722                  projsa[i] = -DOT(sp->snrm, rdir) * sp->area;
723 <                tarea += projsa[i] *= (double)(projsa[i] > FTINY);
723 >                tarea += projsa[i] *= (double)(projsa[i] > 0);
724          }
725 <        if (tarea <= FTINY)             /* wrong side of sender? */
725 >        if (tarea < FTINY*FTINY) {      /* wrong side of sender? */
726 >                fputs(progname, stderr);
727 >                fputs(": internal - sample behind all sender elements!\n",
728 >                                stderr);
729                  return(0);
730 +        }
731          tarea *= x;                     /* get surface from list */
732          for (i = 0, sp = p->slist; tarea > projsa[i]; sp = sp->next)
733                  tarea -= projsa[i++];
# Line 703 | Line 757 | sample_uniform(PARAMS *p, int b, FILE *fp)
757                                                  duvw[2]*p->nrm[i] ;
758                  if (!sample_origin(p, orig_dir[0], orig_dir[1], samp3[0]))
759                          return(0);
760 <                if (fwrite(orig_dir, sizeof(FVECT), 2, fp) != 2)
760 >                if (putbinary(orig_dir, sizeof(FVECT), 2, fp) != 2)
761                          return(0);
762          }
763          return(1);
# Line 733 | Line 787 | sample_shirchiu(PARAMS *p, int b, FILE *fp)
787                                                  duvw[2]*p->nrm[i] ;
788                  if (!sample_origin(p, orig_dir[0], orig_dir[1], samp3[0]))
789                          return(0);
790 <                if (fwrite(orig_dir, sizeof(FVECT), 2, fp) != 2)
790 >                if (putbinary(orig_dir, sizeof(FVECT), 2, fp) != 2)
791                          return(0);
792          }
793          return(1);
# Line 746 | Line 800 | sample_reinhart(PARAMS *p, int b, FILE *fp)
800   #define T_NALT  7
801          static const int        tnaz[T_NALT] = {30, 30, 24, 24, 18, 12, 6};
802          const int               RowMax = T_NALT*p->hsiz + 1;
803 <        const double            RAH = (.25*PI)/(RowMax-.5);
803 >        const double            RAH = (.5*PI)/(RowMax-.5);
804   #define rnaz(r)                 (r >= RowMax-1 ? 1 : p->hsiz*tnaz[r/p->hsiz])
805          int                     n = sampcnt;
806          int                     row, col;
# Line 769 | Line 823 | sample_reinhart(PARAMS *p, int b, FILE *fp)
823          }
824          while (n--) {                   /* stratified sampling */
825                  SDmultiSamp(samp3, 3, (n+frandom())/sampcnt);
826 +                if (row >= RowMax-1)    /* avoid crowding at zenith */
827 +                        samp3[1] *= samp3[1];
828                  alt = (row+samp3[1])*RAH;
829                  azi = (2.*PI)*(col+samp3[2]-.5)/rnaz(row);
830 <                duvw[2] = tcos(alt);    /* measured from horizon */
831 <                duvw[0] = tcos(azi)*duvw[2];
832 <                duvw[1] = tsin(azi)*duvw[2];
830 >                duvw[2] = cos(alt);     /* measured from horizon */
831 >                duvw[0] = tsin(azi)*duvw[2];
832 >                duvw[1] = -tcos(azi)*duvw[2];
833                  duvw[2] = sqrt(1. - duvw[2]*duvw[2]);
834                  for (i = 3; i--; )
835                          orig_dir[1][i] = -duvw[0]*p->udir[i] -
# Line 781 | Line 837 | sample_reinhart(PARAMS *p, int b, FILE *fp)
837                                                  duvw[2]*p->nrm[i] ;
838                  if (!sample_origin(p, orig_dir[0], orig_dir[1], samp3[0]))
839                          return(0);
840 <                if (fwrite(orig_dir, sizeof(FVECT), 2, fp) != 2)
840 >                if (putbinary(orig_dir, sizeof(FVECT), 2, fp) != 2)
841                          return(0);
842          }
843          return(1);
# Line 824 | Line 880 | sample_klems(PARAMS *p, int b, FILE *fp)
880  
881          while (n--) {                   /* stratified sampling */
882                  SDmultiSamp(samp2, 2, (n+frandom())/sampcnt);
883 <                if (!bo_getvec(duvw, b+samp2[1], kbasis[bi]))
883 >                if (!fo_getvec(duvw, b+samp2[1], kbasis[bi]))
884                          return(0);
885                  for (i = 3; i--; )
886 <                        orig_dir[1][i] = duvw[0]*p->udir[i] +
887 <                                                duvw[1]*p->vdir[i] +
886 >                        orig_dir[1][i] = -duvw[0]*p->udir[i] -
887 >                                                duvw[1]*p->vdir[i] -
888                                                  duvw[2]*p->nrm[i] ;
889                  if (!sample_origin(p, orig_dir[0], orig_dir[1], samp2[0]))
890                          return(0);
891 <                if (fwrite(orig_dir, sizeof(FVECT), 2, fp) != 2)
891 >                if (putbinary(orig_dir, sizeof(FVECT), 2, fp) != 2)
892                          return(0);
893          }
894          return(1);
# Line 847 | Line 903 | prepare_sampler(void)
903                  fputs(": no sender surface!\n", stderr);
904                  return(-1);
905          }
906 <        if (curparams.outfn != NULL)    /* misplaced output file spec. */
906 >                                        /* misplaced output file spec. */
907 >        if ((curparams.outfn != NULL) & (verbose >= 0))
908                  fprintf(stderr, "%s: warning - ignoring output file in sender ('%s')\n",
909                                  progname, curparams.outfn);
910                                          /* check/set basis hemisphere */
# Line 861 | Line 918 | prepare_sampler(void)
918                  fputs(": undefined normal for sender sampling\n", stderr);
919                  return(-1);
920          }
921 <        if (normalize(curparams.vup) == 0)
921 >        if (normalize(curparams.vup) == 0) {
922                  if (fabs(curparams.nrm[2]) < .7)
923                          curparams.vup[2] = 1;
924                  else
925                          curparams.vup[1] = 1;
926 <        VCROSS(curparams.udir, curparams.vup, curparams.nrm);
926 >        }
927 >        fcross(curparams.udir, curparams.vup, curparams.nrm);
928          if (normalize(curparams.udir) == 0) {
929                  fputs(progname, stderr);
930                  fputs(": up vector coincides with sender normal\n", stderr);
931                  return(-1);
932          }
933 <        VCROSS(curparams.vdir, curparams.nrm, curparams.udir);
934 <        if (tolower(curparams.hemis[0]) == 'u' | curparams.hemis[0] == '1')
933 >        fcross(curparams.vdir, curparams.nrm, curparams.udir);
934 >        if (curparams.sign == '-') {    /* left-handed coordinate system? */
935 >                curparams.udir[0] *= -1.;
936 >                curparams.udir[1] *= -1.;
937 >                curparams.udir[2] *= -1.;
938 >        }
939 >        if ((tolower(curparams.hemis[0]) == 'u') | (curparams.hemis[0] == '1'))
940                  curparams.sample_basis = sample_uniform;
941          else if (tolower(curparams.hemis[0]) == 's' &&
942                                  tolower(curparams.hemis[1]) == 'c')
# Line 934 | Line 997 | finish_polygon(SURF *p)
997                  VCOPY(e1, e2);
998          }
999          p->area = normalize(p->snrm)*0.5;
1000 <        return(p->area > FTINY);
1000 >        return(p->area > FTINY*FTINY);
1001   }
1002  
1003   /* Add a surface to our current parameters */
# Line 952 | Line 1015 | add_surface(int st, const char *oname, FILE *fp)
1015          snew = (SURF *)malloc(sizeof(SURF) + sizeof(double)*(n-1));
1016          if (snew == NULL) {
1017                  fputs(progname, stderr);
1018 <                fputs(": out of memory!\n", stderr);
1018 >                fputs(": out of memory in add_surface!\n", stderr);
1019                  exit(1);
1020          }
1021          strncpy(snew->sname, oname, sizeof(snew->sname)-1);
# Line 989 | Line 1052 | add_surface(int st, const char *oname, FILE *fp)
1052          case ST_SOURCE:
1053                  if (snew->nfargs != 4)
1054                          goto badcount;
1055 <                VCOPY(snew->snrm, snew->farg);
1055 >                for (n = 3; n--; )      /* need to reverse "normal" */
1056 >                        snew->snrm[n] = -snew->farg[n];
1057                  if (normalize(snew->snrm) == 0)
1058                          goto badnorm;
1059                  snew->area = sin((PI/180./2.)*snew->farg[3]);
1060                  snew->area *= PI*snew->area;
1061                  break;
1062          }
1063 <        if (snew->area <= FTINY) {
1063 >        if ((snew->area <= FTINY*FTINY) & (verbose >= 0)) {
1064                  fprintf(stderr, "%s: warning - zero area for surface '%s'\n",
1065                                  progname, oname);
1066                  free(snew);
# Line 1008 | Line 1072 | add_surface(int st, const char *oname, FILE *fp)
1072          curparams.nsurfs++;
1073          return;
1074   badcount:
1075 <        fprintf(stderr, "%s: bad argument count for surface '%s'\n",
1075 >        fprintf(stderr, "%s: bad argument count for surface element '%s'\n",
1076                          progname, oname);
1077          exit(1);
1078   badnorm:
1079 <        fprintf(stderr, "%s: bad orientation for surface '%s'\n",
1079 >        fprintf(stderr, "%s: bad orientation for surface element '%s'\n",
1080                          progname, oname);
1081          exit(1);
1082   }
# Line 1044 | Line 1108 | add_recv_object(FILE *fp)
1108                                  finish_receiver();
1109                                  clear_params(&curparams, 1);
1110                          }
1111 +                        parse_params(&curparams, newparams);
1112 +                        newparams[0] = '\0';
1113                          strcpy(curmod, thismod);
1114                  }
1115                  add_surface(st, oname, fp);     /* read & store surface */
1116                  return(1);
1117          }
1118                                          /* else skip arguments */
1119 <        if (!fscanf(fp, "%d", &n)) return;
1119 >        if (!fscanf(fp, "%d", &n)) return(0);
1120          while (n-- > 0) fscanf(fp, "%*s");
1121 <        if (!fscanf(fp, "%d", &n)) return;
1121 >        if (!fscanf(fp, "%d", &n)) return(0);
1122          while (n-- > 0) fscanf(fp, "%*d");
1123 <        if (!fscanf(fp, "%d", &n)) return;
1123 >        if (!fscanf(fp, "%d", &n)) return(0);
1124          while (n-- > 0) fscanf(fp, "%*f");
1125          return(0);
1126   }
# Line 1064 | Line 1130 | static int
1130   add_send_object(FILE *fp)
1131   {
1132          int             st;
1133 <        char            otype[32], oname[128];
1133 >        char            thismod[128], otype[32], oname[128];
1134          int             n;
1135  
1136 <        if (fscanf(fp, "%*s %s %s", otype, oname) != 2)
1136 >        if (fscanf(fp, "%s %s %s", thismod, otype, oname) != 3)
1137                  return(0);              /* must have hit EOF! */
1138          if (!strcmp(otype, "alias")) {
1139                  fscanf(fp, "%*s");      /* skip alias */
# Line 1080 | Line 1146 | add_send_object(FILE *fp)
1146                          fputs(": cannot use source as a sender!\n", stderr);
1147                          return(-1);
1148                  }
1149 +                if (strcmp(thismod, curmod)) {
1150 +                        if (curmod[0]) {
1151 +                                fputs(progname, stderr);
1152 +                                fputs(": warning - multiple modifiers in sender\n",
1153 +                                                stderr);
1154 +                        }
1155 +                        strcpy(curmod, thismod);
1156 +                }
1157 +                parse_params(&curparams, newparams);
1158 +                newparams[0] = '\0';
1159                  add_surface(st, oname, fp);     /* read & store surface */
1160                  return(0);
1161          }
1162                                          /* else skip arguments */
1163 <        if (!fscanf(fp, "%d", &n)) return;
1163 >        if (!fscanf(fp, "%d", &n)) return(0);
1164          while (n-- > 0) fscanf(fp, "%*s");
1165 <        if (!fscanf(fp, "%d", &n)) return;
1165 >        if (!fscanf(fp, "%d", &n)) return(0);
1166          while (n-- > 0) fscanf(fp, "%*d");
1167 <        if (!fscanf(fp, "%d", &n)) return;
1167 >        if (!fscanf(fp, "%d", &n)) return(0);
1168          while (n-- > 0) fscanf(fp, "%*f");
1169          return(0);
1170   }
# Line 1128 | Line 1204 | load_scene(const char *inspec, int (*ocb)(FILE *))
1204                          if (!isspace(c) && fscanf(fp, "%s", inpbuf) == 1 &&
1205                                          !strcmp(inpbuf, PARAMSTART)) {
1206                                  if (fgets(inpbuf, sizeof(inpbuf), fp) != NULL)
1207 <                                        parse_params(inpbuf);
1207 >                                        strcat(newparams, inpbuf);
1208                                  continue;
1209                          }
1210 <                        while ((c = getc(fp)) != EOF && c != '\n');
1210 >                        while ((c = getc(fp)) != EOF && c != '\n')
1211                                  ;       /* else skipping comment */
1212                          continue;
1213                  }
# Line 1156 | Line 1232 | main(int argc, char *argv[])
1232   {
1233          char    fmtopt[6] = "-faa";     /* default output is ASCII */
1234          char    *xrs=NULL, *yrs=NULL, *ldopt=NULL;
1235 +        char    *iropt = NULL;
1236          char    *sendfn;
1237          char    sampcntbuf[32], nsbinbuf[32];
1238          FILE    *rcfp;
# Line 1163 | Line 1240 | main(int argc, char *argv[])
1240          int     a, i;
1241                                          /* screen rcontrib options */
1242          progname = argv[0];
1243 <        for (a = 1; a < argc-2 && argv[a][0] == '-'; a++) {
1244 <                int     na = 1;         /* !! Keep consistent !! */
1245 <                switch (argv[a][1]) {
1243 >        for (a = 1; a < argc-2; a++) {
1244 >                int     na;
1245 >                                        /* check for argument expansion */
1246 >                while ((na = expandarg(&argc, &argv, a)) > 0)
1247 >                        ;
1248 >                if (na < 0) {
1249 >                        fprintf(stderr, "%s: cannot expand '%s'\n",
1250 >                                        progname, argv[a]);
1251 >                        return(1);
1252 >                }
1253 >                if (argv[a][0] != '-' || !argv[a][1])
1254 >                        break;
1255 >                na = 1;
1256 >                switch (argv[a][1]) {   /* !! Keep consistent !! */
1257                  case 'v':               /* verbose mode */
1258 <                        verbose = !verbose;
1258 >                        verbose = 1;
1259                          na = 0;
1260                          continue;
1261                  case 'f':               /* special case for -fo, -ff, etc. */
# Line 1180 | Line 1268 | main(int argc, char *argv[])
1268                          case 'f':
1269                          case 'd':
1270                          case 'c':
1271 <                                if (!(fmtopt[4] = argv[a][3]))
1272 <                                        fmtopt[4] = argv[a][2];
1273 <                                fmtopt[3] = argv[a][2];
1271 >                                if (!(fmtopt[3] = argv[a][3]))
1272 >                                        fmtopt[3] = argv[a][2];
1273 >                                fmtopt[2] = argv[a][2];
1274                                  na = 0;
1275                                  continue;       /* will pass later */
1276                          default:
# Line 1198 | Line 1286 | main(int argc, char *argv[])
1286                          na = 0;
1287                          continue;
1288                  case 'c':               /* number of samples */
1289 <                        sampcnt = atoi(argv[a+1]);
1289 >                        sampcnt = atoi(argv[++a]);
1290                          if (sampcnt <= 0)
1291                                  goto userr;
1292                          na = 0;         /* we re-add this later */
1293                          continue;
1294 <                case 'V':               /* options without arguments */
1207 <                case 'w':
1208 <                case 'u':
1294 >                case 'I':               /* only for pass-through mode */
1295                  case 'i':
1296 +                        iropt = argv[a];
1297 +                        na = 0;
1298 +                        continue;
1299 +                case 'w':               /* options without arguments */
1300 +                        if (!argv[a][2] || strchr("+1tTyY", argv[a][2]) == NULL)
1301 +                                verbose = -1;
1302 +                        break;
1303 +                case 'V':
1304 +                case 'u':
1305                  case 'h':
1306                  case 'r':
1307                          break;
1308                  case 'n':               /* options with 1 argument */
1309                  case 's':
1310                  case 'o':
1311 +                case 't':
1312                          na = 2;
1313                          break;
1314                  case 'b':               /* special case */
# Line 1230 | Line 1326 | main(int argc, char *argv[])
1326                          if (argv[a][2] != 'v') na = 2;
1327                          break;
1328                  case 'a':               /* special case */
1329 <                        na = (argv[a][2] == 'v') ? 4 : 2;
1329 >                        if (argv[a][2] == 'p') {
1330 >                                na = 2; /* photon map [+ bandwidth(s)] */
1331 >                                if (a < argc-3 && atoi(argv[a+1]) > 0)
1332 >                                        na += 1 + (a < argc-4 && atoi(argv[a+2]) > 0);
1333 >                        } else
1334 >                                na = (argv[a][2] == 'v') ? 4 : 2;
1335                          break;
1336                  case 'm':               /* special case */
1337                          if (!argv[a][2]) goto userr;
1338                          na = (argv[a][2] == 'e') | (argv[a][2] == 'a') ? 4 : 2;
1339                          break;
1239                case '\0':              /* pass-through mode */
1240                        goto done_opts;
1340                  default:                /* anything else is verbotten */
1341                          goto userr;
1342                  }
# Line 1247 | Line 1346 | main(int argc, char *argv[])
1346                  while (--na)            /* + arguments if any */
1347                          rcarg[nrcargs++] = argv[++a];
1348          }
1250 done_opts:
1349          if (a > argc-2)
1350                  goto userr;             /* check at end of options */
1351          sendfn = argv[a++];             /* assign sender & receiver inputs */
1352          if (sendfn[0] == '-') {         /* user wants pass-through mode? */
1353                  if (sendfn[1]) goto userr;
1354                  sendfn = NULL;
1355 +                if (iropt) {
1356 +                        CHECKARGC(1);
1357 +                        rcarg[nrcargs++] = iropt;
1358 +                }
1359                  if (xrs) {
1360                          CHECKARGC(2);
1361                          rcarg[nrcargs++] = "-x";
# Line 1269 | Line 1371 | done_opts:
1371                          rcarg[nrcargs++] = ldopt;
1372                  }
1373                  if (sampcnt <= 0) sampcnt = 1;
1374 <        } else {                        /* else FVECT determines input format */
1375 <                fmtopt[3] = (sizeof(RREAL)==sizeof(double)) ? 'd' : 'f';
1374 >        } else {                        /* else in sampling mode */
1375 >                if (iropt) {
1376 >                        fputs(progname, stderr);
1377 >                        fputs(": -i, -I supported for pass-through only\n", stderr);
1378 >                        return(1);
1379 >                }
1380 > #ifdef SMLFLT
1381 >                fmtopt[2] = 'f';
1382 > #else
1383 >                fmtopt[2] = 'd';
1384 > #endif
1385                  if (sampcnt <= 0) sampcnt = 10000;
1386          }
1387          sprintf(sampcntbuf, "%d", sampcnt);
# Line 1289 | Line 1400 | done_opts:
1400                  return(my_exec(rcarg)); /* rcontrib does everything */
1401          }
1402          clear_params(&curparams, 0);    /* else load sender surface & params */
1403 +        curmod[0] = '\0';
1404          if (load_scene(sendfn, add_send_object) < 0)
1405                  return(1);
1406          if ((nsbins = prepare_sampler()) <= 0)
# Line 1306 | Line 1418 | done_opts:
1418   #ifdef getc_unlocked
1419          flockfile(rcfp);
1420   #endif
1421 <        if (verbose) {
1421 >        if (verbose > 0) {
1422                  fprintf(stderr, "%s: sampling %d directions", progname, nsbins);
1423                  if (curparams.nsurfs > 1)
1424 <                        fprintf(stderr, " (%d surfaces)\n", curparams.nsurfs);
1424 >                        fprintf(stderr, " (%d elements)\n", curparams.nsurfs);
1425                  else
1426                          fputc('\n', stderr);
1427          }
1428          for (i = 0; i < nsbins; i++)    /* send rcontrib ray samples */
1429                  if (!(*curparams.sample_basis)(&curparams, i, rcfp))
1430                          return(1);
1431 <        return(pclose(rcfp) == 0);      /* all finished! */
1431 >        return(pclose_al(rcfp) < 0);    /* all finished! */
1432   userr:
1433          if (a < argc-2)
1434                  fprintf(stderr, "%s: unsupported option '%s'", progname, argv[a]);
1435 <        fprintf(stderr, "Usage: %s [-v][rcontrib options] sender.rad receiver.rad [system.rad ..]\n",
1435 >        fprintf(stderr, "Usage: %s [-v][rcontrib options] sender.rad receiver.rad [-i system.oct] [system.rad ..]\n",
1436                                  progname);
1437          return(1);
1438   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines