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.48 by greg, Mon Jul 29 22:52:39 2019 UTC

# Line 11 | Line 11 | static const char RCSid[] = "$Id$";
11   #include <stdlib.h>
12   #include "rtio.h"
13   #include "rtmath.h"
14 + #include "paths.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 += 4;
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   /* Open a pipe to/from a command given as an argument list */
# Line 222 | Line 189 | popen_arglist(char *av[], char *mode)
189                  fputs(": command line too long in popen_arglist()\n", stderr);
190                  return(NULL);
191          }
192 <        if (verbose)
192 >        if (verbose > 0)
193                  fprintf(stderr, "%s: opening pipe %s: %s\n",
194                                  progname, (*mode=='w') ? "to" : "from", cmd);
195          return(popen(cmd, mode));
196   }
197  
198 < #ifdef _WIN32
198 > #if defined(_WIN32) || defined(_WIN64)
199   /* Execute system command (Windows version) */
200   static int
201   my_exec(char *av[])
# Line 240 | Line 207 | my_exec(char *av[])
207                  fputs(": command line too long in my_exec()\n", stderr);
208                  return(1);
209          }
210 <        if (verbose)
210 >        if (verbose > 0)
211                  fprintf(stderr, "%s: running: %s\n", progname, cmd);
212          return(system(cmd));
213   }
# Line 255 | Line 222 | my_exec(char *av[])
222                  fprintf(stderr, "%s: cannot locate %s\n", progname, av[0]);
223                  return(1);
224          }
225 <        if (verbose) {
225 >        if (verbose > 0) {
226                  char    cmd[4096];
227                  if (!convert_commandline(cmd, sizeof(cmd), av))
228                          strcpy(cmd, "COMMAND TOO LONG TO SHOW");
# Line 309 | Line 276 | nextchar:
276  
277   /* Parse program parameters (directives) */
278   static int
279 < parse_params(char *pargs)
279 > parse_params(PARAMS *p, char *pargs)
280   {
281          char    *cp = pargs;
282          int     nparams = 0;
283 +        int     quot;
284          int     i;
285  
286 <        for ( ; ; )
286 >        for ( ; ; ) {
287                  switch (*cp++) {
288                  case 'h':
289                          if (*cp++ != '=')
290                                  break;
291 <                        curparams.hsiz = 0;
291 >                        if ((*cp == '+') | (*cp == '-'))
292 >                                p->sign = *cp++;
293 >                        else
294 >                                p->sign = '+';
295 >                        p->hsiz = 0;
296                          i = 0;
297                          while (*cp && !isspace(*cp)) {
298                                  if (isdigit(*cp))
299 <                                        curparams.hsiz = 10*curparams.hsiz +
300 <                                                                *cp - '0';
329 <                                curparams.hemis[i++] = *cp++;
299 >                                        p->hsiz = 10*p->hsiz + *cp - '0';
300 >                                p->hemis[i++] = *cp++;
301                          }
302                          if (!i)
303                                  break;
304 <                        curparams.hemis[i] = '\0';
305 <                        curparams.hsiz += !curparams.hsiz;
304 >                        p->hemis[i] = '\0';
305 >                        p->hsiz += !p->hsiz;
306                          ++nparams;
307                          continue;
308                  case 'u':
309                          if (*cp++ != '=')
310                                  break;
311 <                        if (!get_direction(curparams.vup, cp))
311 >                        if (!get_direction(p->vup, cp))
312                                  break;
313 +                        while (*cp && !isspace(*cp++))
314 +                                ;
315                          ++nparams;
316                          continue;
317                  case 'o':
318                          if (*cp++ != '=')
319                                  break;
320 +                        quot = 0;
321 +                        if ((*cp == '"') | (*cp == '\''))
322 +                                quot = *cp++;
323                          i = 0;
324 <                        while (*cp && !isspace(*cp++))
325 <                                i++;
324 >                        while (*cp && (quot ? (*cp != quot) : !isspace(*cp))) {
325 >                                i++; cp++;
326 >                        }
327                          if (!i)
328                                  break;
329 <                        *--cp = '\0';
330 <                        curparams.outfn = savqstr(cp-i);
331 <                        *cp++ = ' ';
329 >                        if (!*cp) {
330 >                                if (quot)
331 >                                        break;
332 >                                cp[1] = '\0';
333 >                        }
334 >                        *cp = '\0';
335 >                        p->outfn = savqstr(cp-i);
336 >                        *cp++ = quot ? quot : ' ';
337                          ++nparams;
338                          continue;
339                  case ' ':
# Line 364 | Line 346 | parse_params(char *pargs)
346                  default:
347                          break;
348                  }
349 <        fprintf(stderr, "%s: bad parameter string '%s'\n", progname, pargs);
349 >                break;
350 >        }
351 >        fprintf(stderr, "%s: bad parameter string: %s", progname, pargs);
352          exit(1);
353          return(-1);     /* pro forma return */
354   }
# Line 402 | Line 386 | finish_receiver(void)
386                  fputs(": undefined normal for hemisphere sampling\n", stderr);
387                  exit(1);
388          }
389 <        if (normalize(curparams.vup) == 0)
389 >        if (normalize(curparams.vup) == 0) {
390                  if (fabs(curparams.nrm[2]) < .7)
391                          curparams.vup[2] = 1;
392                  else
393                          curparams.vup[1] = 1;
394 +        }
395                                          /* determine sample type/bin */
396 <        if (tolower(curparams.hemis[0]) == 'u' | curparams.hemis[0] == '1') {
397 <                binv = "0";             /* uniform sampling -- one bin */
396 >        if ((tolower(curparams.hemis[0]) == 'u') | (curparams.hemis[0] == '1')) {
397 >                sprintf(sbuf, "if(-Dx*%g-Dy*%g-Dz*%g,0,-1)",
398 >                        curparams.nrm[0], curparams.nrm[1], curparams.nrm[2]);
399 >                binv = savqstr(sbuf);
400 >                nbins = "1";            /* uniform sampling -- one bin */
401                  uniform = 1;
402          } else if (tolower(curparams.hemis[0]) == 's' &&
403                                  tolower(curparams.hemis[1]) == 'c') {
# Line 420 | Line 408 | finish_receiver(void)
408                          exit(1);
409                  }
410                  calfn = shirchiufn; shirchiufn = NULL;
411 <                sprintf(sbuf, "SCdim=%d,Nx=%g,Ny=%g,Nz=%g,Ux=%g,Uy=%g,Uz=%g",
411 >                sprintf(sbuf, "SCdim=%d,rNx=%g,rNy=%g,rNz=%g,Ux=%g,Uy=%g,Uz=%g,RHS=%c1",
412                                  curparams.hsiz,
413                          curparams.nrm[0], curparams.nrm[1], curparams.nrm[2],
414 <                        curparams.vup[0], curparams.vup[1], curparams.vup[2]);
414 >                        curparams.vup[0], curparams.vup[1], curparams.vup[2],
415 >                        curparams.sign);
416                  params = savqstr(sbuf);
417                  binv = "scbin";
418                  nbins = "SCdim*SCdim";
419          } else if ((tolower(curparams.hemis[0]) == 'r') |
420                          (tolower(curparams.hemis[0]) == 't')) {
421                  calfn = reinhfn; reinhfn = NULL;
422 <                sprintf(sbuf, "MF=%d,Nx=%g,Ny=%g,Nz=%g,Ux=%g,Uy=%g,Uz=%g",
422 >                sprintf(sbuf, "MF=%d,rNx=%g,rNy=%g,rNz=%g,Ux=%g,Uy=%g,Uz=%g,RHS=%c1",
423                                  curparams.hsiz,
424                          curparams.nrm[0], curparams.nrm[1], curparams.nrm[2],
425 <                        curparams.vup[0], curparams.vup[1], curparams.vup[2]);
425 >                        curparams.vup[0], curparams.vup[1], curparams.vup[2],
426 >                        curparams.sign);
427                  params = savqstr(sbuf);
428                  binv = "rbin";
429                  nbins = "Nrbins";
# Line 461 | Line 451 | finish_receiver(void)
451                                  progname, curparams.hemis);
452                  exit(1);
453          }
454 +        if (tolower(curparams.hemis[0]) == 'k') {
455 +                sprintf(sbuf, "RHS=%c1", curparams.sign);
456 +                params = savqstr(sbuf);
457 +        }
458          if (!uniform & (curparams.slist->styp == ST_SOURCE)) {
459                  SURF    *sp;
460                  for (sp = curparams.slist; sp != NULL; sp = sp->next)
# Line 508 | Line 502 | make_axes(FVECT uva[2], const FVECT nrm)
502   {
503          int     i;
504  
505 <        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) {
505 >        if (!getperpendicular(uva[0], nrm, 1)) {
506                  fputs(progname, stderr);
507                  fputs(": bad surface normal in make_axes!\n", stderr);
508                  exit(1);
509          }
510 <        uva[1][i] = 1.0;
523 <        VCROSS(uva[0], uva[1], nrm);
524 <        normalize(uva[0]);
525 <        VCROSS(uva[1], nrm, uva[0]);
510 >        fcross(uva[1], nrm, uva[0]);
511   }
512  
513   /* Illegal sender surfaces end up here */
# Line 554 | Line 539 | ssamp_ring(FVECT orig, SURF *sp, double x)
539                  sp->priv = (void *)uva;
540          }
541          SDmultiSamp(samp2, 2, x);
542 <        samp2[0] = sp->farg[6] + sqrt(samp2[0]*sp->area*(1./PI));
542 >        samp2[0] = sqrt(samp2[0]*sp->area*(1./PI) + sp->farg[6]*sp->farg[6]);
543          samp2[1] *= 2.*PI;
544          uv[0] = samp2[0]*tcos(samp2[1]);
545          uv[1] = samp2[0]*tsin(samp2[1]);
# Line 607 | Line 592 | ssamp_poly(FVECT orig, SURF *sp, double x)
592                          }
593                          ptp->ntris = 0;
594                          v2l->p = (void *)ptp;
595 <                        if (!polyTriangulate(v2l, add_triangle))
595 >                        if (!polyTriangulate(v2l, add_triangle)) {
596 >                                fprintf(stderr,
597 >                                        "%s: cannot triangulate polygon '%s'\n",
598 >                                                progname, sp->sname);
599                                  return(0);
600 +                        }
601                          for (i = ptp->ntris; i--; ) {
602                                  int     a = ptp->tri[i].vndx[0];
603                                  int     b = ptp->tri[i].vndx[1];
# Line 627 | Line 616 | ssamp_poly(FVECT orig, SURF *sp, double x)
616                  sp->priv = (void *)ptp;
617          }
618                                          /* pick triangle by partial area */
619 <        for (i = 0; i < ptp->ntris && x > ptp->tri[i].afrac; i++)
619 >        for (i = 0; i < ptp->ntris-1 && x > ptp->tri[i].afrac; i++)
620                  x -= ptp->tri[i].afrac;
621          SDmultiSamp(samp2, 2, x/ptp->tri[i].afrac);
622          samp2[0] *= samp2[1] = sqrt(samp2[1]);
# Line 657 | Line 646 | sample_origin(PARAMS *p, FVECT orig, const FVECT rdir,
646                                          /* special case for lone surface */
647          if (p->nsurfs == 1) {
648                  sp = p->slist;
649 <                if (DOT(sp->snrm, rdir) >= -FTINY)
650 <                        return(0);      /* behind surface! */
649 >                if (DOT(sp->snrm, rdir) >= FTINY) {
650 >                        fprintf(stderr,
651 >                                "%s: internal - sample behind sender '%s'\n",
652 >                                        progname, sp->sname);
653 >                        return(0);
654 >                }
655                  return((*orig_in_surf[sp->styp])(orig, sp, x));
656          }
657          if (p->nsurfs > nall) {         /* (re)allocate surface area cache */
658                  if (projsa) free(projsa);
659                  projsa = (double *)malloc(sizeof(double)*p->nsurfs);
660 <                if (!projsa) return(0);
660 >                if (projsa == NULL) {
661 >                        fputs(progname, stderr);
662 >                        fputs(": out of memory in sample_origin!\n", stderr);
663 >                        exit(1);
664 >                }
665                  nall = p->nsurfs;
666          }
667                                          /* compute projected areas */
# Line 672 | Line 669 | sample_origin(PARAMS *p, FVECT orig, const FVECT rdir,
669                  projsa[i] = -DOT(sp->snrm, rdir) * sp->area;
670                  tarea += projsa[i] *= (double)(projsa[i] > FTINY);
671          }
672 <        if (tarea <= FTINY)             /* wrong side of sender? */
672 >        if (tarea < 0) {                /* wrong side of sender? */
673 >                fputs(progname, stderr);
674 >                fputs(": internal - sample behind all sender elements!\n",
675 >                                stderr);
676                  return(0);
677 +        }
678          tarea *= x;                     /* get surface from list */
679          for (i = 0, sp = p->slist; tarea > projsa[i]; sp = sp->next)
680                  tarea -= projsa[i++];
# Line 703 | Line 704 | sample_uniform(PARAMS *p, int b, FILE *fp)
704                                                  duvw[2]*p->nrm[i] ;
705                  if (!sample_origin(p, orig_dir[0], orig_dir[1], samp3[0]))
706                          return(0);
707 <                if (fwrite(orig_dir, sizeof(FVECT), 2, fp) != 2)
707 >                if (putbinary(orig_dir, sizeof(FVECT), 2, fp) != 2)
708                          return(0);
709          }
710          return(1);
# Line 733 | Line 734 | sample_shirchiu(PARAMS *p, int b, FILE *fp)
734                                                  duvw[2]*p->nrm[i] ;
735                  if (!sample_origin(p, orig_dir[0], orig_dir[1], samp3[0]))
736                          return(0);
737 <                if (fwrite(orig_dir, sizeof(FVECT), 2, fp) != 2)
737 >                if (putbinary(orig_dir, sizeof(FVECT), 2, fp) != 2)
738                          return(0);
739          }
740          return(1);
# Line 746 | Line 747 | sample_reinhart(PARAMS *p, int b, FILE *fp)
747   #define T_NALT  7
748          static const int        tnaz[T_NALT] = {30, 30, 24, 24, 18, 12, 6};
749          const int               RowMax = T_NALT*p->hsiz + 1;
750 <        const double            RAH = (.25*PI)/(RowMax-.5);
750 >        const double            RAH = (.5*PI)/(RowMax-.5);
751   #define rnaz(r)                 (r >= RowMax-1 ? 1 : p->hsiz*tnaz[r/p->hsiz])
752          int                     n = sampcnt;
753          int                     row, col;
# Line 769 | Line 770 | sample_reinhart(PARAMS *p, int b, FILE *fp)
770          }
771          while (n--) {                   /* stratified sampling */
772                  SDmultiSamp(samp3, 3, (n+frandom())/sampcnt);
773 +                if (row >= RowMax-1)    /* avoid crowding at zenith */
774 +                        samp3[1] *= samp3[1];
775                  alt = (row+samp3[1])*RAH;
776                  azi = (2.*PI)*(col+samp3[2]-.5)/rnaz(row);
777 <                duvw[2] = tcos(alt);    /* measured from horizon */
778 <                duvw[0] = tcos(azi)*duvw[2];
779 <                duvw[1] = tsin(azi)*duvw[2];
777 >                duvw[2] = cos(alt);     /* measured from horizon */
778 >                duvw[0] = tsin(azi)*duvw[2];
779 >                duvw[1] = -tcos(azi)*duvw[2];
780                  duvw[2] = sqrt(1. - duvw[2]*duvw[2]);
781                  for (i = 3; i--; )
782                          orig_dir[1][i] = -duvw[0]*p->udir[i] -
# Line 781 | Line 784 | sample_reinhart(PARAMS *p, int b, FILE *fp)
784                                                  duvw[2]*p->nrm[i] ;
785                  if (!sample_origin(p, orig_dir[0], orig_dir[1], samp3[0]))
786                          return(0);
787 <                if (fwrite(orig_dir, sizeof(FVECT), 2, fp) != 2)
787 >                if (putbinary(orig_dir, sizeof(FVECT), 2, fp) != 2)
788                          return(0);
789          }
790          return(1);
# Line 824 | Line 827 | sample_klems(PARAMS *p, int b, FILE *fp)
827  
828          while (n--) {                   /* stratified sampling */
829                  SDmultiSamp(samp2, 2, (n+frandom())/sampcnt);
830 <                if (!bo_getvec(duvw, b+samp2[1], kbasis[bi]))
830 >                if (!fo_getvec(duvw, b+samp2[1], kbasis[bi]))
831                          return(0);
832                  for (i = 3; i--; )
833 <                        orig_dir[1][i] = duvw[0]*p->udir[i] +
834 <                                                duvw[1]*p->vdir[i] +
833 >                        orig_dir[1][i] = -duvw[0]*p->udir[i] -
834 >                                                duvw[1]*p->vdir[i] -
835                                                  duvw[2]*p->nrm[i] ;
836                  if (!sample_origin(p, orig_dir[0], orig_dir[1], samp2[0]))
837                          return(0);
838 <                if (fwrite(orig_dir, sizeof(FVECT), 2, fp) != 2)
838 >                if (putbinary(orig_dir, sizeof(FVECT), 2, fp) != 2)
839                          return(0);
840          }
841          return(1);
# Line 847 | Line 850 | prepare_sampler(void)
850                  fputs(": no sender surface!\n", stderr);
851                  return(-1);
852          }
853 <        if (curparams.outfn != NULL)    /* misplaced output file spec. */
853 >                                        /* misplaced output file spec. */
854 >        if ((curparams.outfn != NULL) & (verbose >= 0))
855                  fprintf(stderr, "%s: warning - ignoring output file in sender ('%s')\n",
856                                  progname, curparams.outfn);
857                                          /* check/set basis hemisphere */
# Line 861 | Line 865 | prepare_sampler(void)
865                  fputs(": undefined normal for sender sampling\n", stderr);
866                  return(-1);
867          }
868 <        if (normalize(curparams.vup) == 0)
868 >        if (normalize(curparams.vup) == 0) {
869                  if (fabs(curparams.nrm[2]) < .7)
870                          curparams.vup[2] = 1;
871                  else
872                          curparams.vup[1] = 1;
873 <        VCROSS(curparams.udir, curparams.vup, curparams.nrm);
873 >        }
874 >        fcross(curparams.udir, curparams.vup, curparams.nrm);
875          if (normalize(curparams.udir) == 0) {
876                  fputs(progname, stderr);
877                  fputs(": up vector coincides with sender normal\n", stderr);
878                  return(-1);
879          }
880 <        VCROSS(curparams.vdir, curparams.nrm, curparams.udir);
881 <        if (tolower(curparams.hemis[0]) == 'u' | curparams.hemis[0] == '1')
880 >        fcross(curparams.vdir, curparams.nrm, curparams.udir);
881 >        if (curparams.sign == '-') {    /* left-handed coordinate system? */
882 >                curparams.udir[0] *= -1.;
883 >                curparams.udir[1] *= -1.;
884 >                curparams.udir[2] *= -1.;
885 >        }
886 >        if ((tolower(curparams.hemis[0]) == 'u') | (curparams.hemis[0] == '1'))
887                  curparams.sample_basis = sample_uniform;
888          else if (tolower(curparams.hemis[0]) == 's' &&
889                                  tolower(curparams.hemis[1]) == 'c')
# Line 952 | Line 962 | add_surface(int st, const char *oname, FILE *fp)
962          snew = (SURF *)malloc(sizeof(SURF) + sizeof(double)*(n-1));
963          if (snew == NULL) {
964                  fputs(progname, stderr);
965 <                fputs(": out of memory!\n", stderr);
965 >                fputs(": out of memory in add_surface!\n", stderr);
966                  exit(1);
967          }
968          strncpy(snew->sname, oname, sizeof(snew->sname)-1);
# Line 989 | Line 999 | add_surface(int st, const char *oname, FILE *fp)
999          case ST_SOURCE:
1000                  if (snew->nfargs != 4)
1001                          goto badcount;
1002 <                VCOPY(snew->snrm, snew->farg);
1002 >                for (n = 3; n--; )      /* need to reverse "normal" */
1003 >                        snew->snrm[n] = -snew->farg[n];
1004                  if (normalize(snew->snrm) == 0)
1005                          goto badnorm;
1006                  snew->area = sin((PI/180./2.)*snew->farg[3]);
1007                  snew->area *= PI*snew->area;
1008                  break;
1009          }
1010 <        if (snew->area <= FTINY) {
1010 >        if ((snew->area <= FTINY) & (verbose >= 0)) {
1011                  fprintf(stderr, "%s: warning - zero area for surface '%s'\n",
1012                                  progname, oname);
1013                  free(snew);
# Line 1008 | Line 1019 | add_surface(int st, const char *oname, FILE *fp)
1019          curparams.nsurfs++;
1020          return;
1021   badcount:
1022 <        fprintf(stderr, "%s: bad argument count for surface '%s'\n",
1022 >        fprintf(stderr, "%s: bad argument count for surface element '%s'\n",
1023                          progname, oname);
1024          exit(1);
1025   badnorm:
1026 <        fprintf(stderr, "%s: bad orientation for surface '%s'\n",
1026 >        fprintf(stderr, "%s: bad orientation for surface element '%s'\n",
1027                          progname, oname);
1028          exit(1);
1029   }
# Line 1044 | Line 1055 | add_recv_object(FILE *fp)
1055                                  finish_receiver();
1056                                  clear_params(&curparams, 1);
1057                          }
1058 +                        parse_params(&curparams, newparams);
1059 +                        newparams[0] = '\0';
1060                          strcpy(curmod, thismod);
1061                  }
1062                  add_surface(st, oname, fp);     /* read & store surface */
1063                  return(1);
1064          }
1065                                          /* else skip arguments */
1066 <        if (!fscanf(fp, "%d", &n)) return;
1066 >        if (!fscanf(fp, "%d", &n)) return(0);
1067          while (n-- > 0) fscanf(fp, "%*s");
1068 <        if (!fscanf(fp, "%d", &n)) return;
1068 >        if (!fscanf(fp, "%d", &n)) return(0);
1069          while (n-- > 0) fscanf(fp, "%*d");
1070 <        if (!fscanf(fp, "%d", &n)) return;
1070 >        if (!fscanf(fp, "%d", &n)) return(0);
1071          while (n-- > 0) fscanf(fp, "%*f");
1072          return(0);
1073   }
# Line 1064 | Line 1077 | static int
1077   add_send_object(FILE *fp)
1078   {
1079          int             st;
1080 <        char            otype[32], oname[128];
1080 >        char            thismod[128], otype[32], oname[128];
1081          int             n;
1082  
1083 <        if (fscanf(fp, "%*s %s %s", otype, oname) != 2)
1083 >        if (fscanf(fp, "%s %s %s", thismod, otype, oname) != 3)
1084                  return(0);              /* must have hit EOF! */
1085          if (!strcmp(otype, "alias")) {
1086                  fscanf(fp, "%*s");      /* skip alias */
# Line 1080 | Line 1093 | add_send_object(FILE *fp)
1093                          fputs(": cannot use source as a sender!\n", stderr);
1094                          return(-1);
1095                  }
1096 +                if (strcmp(thismod, curmod)) {
1097 +                        if (curmod[0]) {
1098 +                                fputs(progname, stderr);
1099 +                                fputs(": warning - multiple modifiers in sender\n",
1100 +                                                stderr);
1101 +                        }
1102 +                        strcpy(curmod, thismod);
1103 +                }
1104 +                parse_params(&curparams, newparams);
1105 +                newparams[0] = '\0';
1106                  add_surface(st, oname, fp);     /* read & store surface */
1107                  return(0);
1108          }
1109                                          /* else skip arguments */
1110 <        if (!fscanf(fp, "%d", &n)) return;
1110 >        if (!fscanf(fp, "%d", &n)) return(0);
1111          while (n-- > 0) fscanf(fp, "%*s");
1112 <        if (!fscanf(fp, "%d", &n)) return;
1112 >        if (!fscanf(fp, "%d", &n)) return(0);
1113          while (n-- > 0) fscanf(fp, "%*d");
1114 <        if (!fscanf(fp, "%d", &n)) return;
1114 >        if (!fscanf(fp, "%d", &n)) return(0);
1115          while (n-- > 0) fscanf(fp, "%*f");
1116          return(0);
1117   }
# Line 1128 | Line 1151 | load_scene(const char *inspec, int (*ocb)(FILE *))
1151                          if (!isspace(c) && fscanf(fp, "%s", inpbuf) == 1 &&
1152                                          !strcmp(inpbuf, PARAMSTART)) {
1153                                  if (fgets(inpbuf, sizeof(inpbuf), fp) != NULL)
1154 <                                        parse_params(inpbuf);
1154 >                                        strcat(newparams, inpbuf);
1155                                  continue;
1156                          }
1157 <                        while ((c = getc(fp)) != EOF && c != '\n');
1157 >                        while ((c = getc(fp)) != EOF && c != '\n')
1158                                  ;       /* else skipping comment */
1159                          continue;
1160                  }
# Line 1156 | Line 1179 | main(int argc, char *argv[])
1179   {
1180          char    fmtopt[6] = "-faa";     /* default output is ASCII */
1181          char    *xrs=NULL, *yrs=NULL, *ldopt=NULL;
1182 +        char    *iropt = NULL;
1183          char    *sendfn;
1184          char    sampcntbuf[32], nsbinbuf[32];
1185          FILE    *rcfp;
# Line 1163 | Line 1187 | main(int argc, char *argv[])
1187          int     a, i;
1188                                          /* screen rcontrib options */
1189          progname = argv[0];
1190 <        for (a = 1; a < argc-2 && argv[a][0] == '-'; a++) {
1191 <                int     na = 1;         /* !! Keep consistent !! */
1192 <                switch (argv[a][1]) {
1190 >        for (a = 1; a < argc-2; a++) {
1191 >                int     na;
1192 >                                        /* check for argument expansion */
1193 >                while ((na = expandarg(&argc, &argv, a)) > 0)
1194 >                        ;
1195 >                if (na < 0) {
1196 >                        fprintf(stderr, "%s: cannot expand '%s'\n",
1197 >                                        progname, argv[a]);
1198 >                        return(1);
1199 >                }
1200 >                if (argv[a][0] != '-' || !argv[a][1])
1201 >                        break;
1202 >                na = 1;
1203 >                switch (argv[a][1]) {   /* !! Keep consistent !! */
1204                  case 'v':               /* verbose mode */
1205 <                        verbose = !verbose;
1205 >                        verbose = 1;
1206                          na = 0;
1207                          continue;
1208                  case 'f':               /* special case for -fo, -ff, etc. */
# Line 1180 | Line 1215 | main(int argc, char *argv[])
1215                          case 'f':
1216                          case 'd':
1217                          case 'c':
1218 <                                if (!(fmtopt[4] = argv[a][3]))
1219 <                                        fmtopt[4] = argv[a][2];
1220 <                                fmtopt[3] = argv[a][2];
1218 >                                if (!(fmtopt[3] = argv[a][3]))
1219 >                                        fmtopt[3] = argv[a][2];
1220 >                                fmtopt[2] = argv[a][2];
1221                                  na = 0;
1222                                  continue;       /* will pass later */
1223                          default:
# Line 1198 | Line 1233 | main(int argc, char *argv[])
1233                          na = 0;
1234                          continue;
1235                  case 'c':               /* number of samples */
1236 <                        sampcnt = atoi(argv[a+1]);
1236 >                        sampcnt = atoi(argv[++a]);
1237                          if (sampcnt <= 0)
1238                                  goto userr;
1239                          na = 0;         /* we re-add this later */
1240                          continue;
1241 <                case 'V':               /* options without arguments */
1207 <                case 'w':
1208 <                case 'u':
1241 >                case 'I':               /* only for pass-through mode */
1242                  case 'i':
1243 +                        iropt = argv[a];
1244 +                        na = 0;
1245 +                        continue;
1246 +                case 'w':               /* options without arguments */
1247 +                        if (argv[a][2] != '+') verbose = -1;
1248 +                case 'V':
1249 +                case 'u':
1250                  case 'h':
1251                  case 'r':
1252                          break;
# Line 1230 | Line 1270 | main(int argc, char *argv[])
1270                          if (argv[a][2] != 'v') na = 2;
1271                          break;
1272                  case 'a':               /* special case */
1273 <                        na = (argv[a][2] == 'v') ? 4 : 2;
1273 >                        if (argv[a][2] == 'p') {
1274 >                                na = 2; /* photon map [+ bandwidth(s)] */
1275 >                                if (a < argc-3 && atoi(argv[a+1]) > 0)
1276 >                                        na += 1 + (a < argc-4 && atoi(argv[a+2]) > 0);
1277 >                        } else
1278 >                                na = (argv[a][2] == 'v') ? 4 : 2;
1279                          break;
1280                  case 'm':               /* special case */
1281                          if (!argv[a][2]) goto userr;
1282                          na = (argv[a][2] == 'e') | (argv[a][2] == 'a') ? 4 : 2;
1283                          break;
1239                case '\0':              /* pass-through mode */
1240                        goto done_opts;
1284                  default:                /* anything else is verbotten */
1285                          goto userr;
1286                  }
# Line 1247 | Line 1290 | main(int argc, char *argv[])
1290                  while (--na)            /* + arguments if any */
1291                          rcarg[nrcargs++] = argv[++a];
1292          }
1250 done_opts:
1293          if (a > argc-2)
1294                  goto userr;             /* check at end of options */
1295          sendfn = argv[a++];             /* assign sender & receiver inputs */
1296          if (sendfn[0] == '-') {         /* user wants pass-through mode? */
1297                  if (sendfn[1]) goto userr;
1298                  sendfn = NULL;
1299 +                if (iropt) {
1300 +                        CHECKARGC(1);
1301 +                        rcarg[nrcargs++] = iropt;
1302 +                }
1303                  if (xrs) {
1304                          CHECKARGC(2);
1305                          rcarg[nrcargs++] = "-x";
# Line 1269 | Line 1315 | done_opts:
1315                          rcarg[nrcargs++] = ldopt;
1316                  }
1317                  if (sampcnt <= 0) sampcnt = 1;
1318 <        } else {                        /* else FVECT determines input format */
1319 <                fmtopt[3] = (sizeof(RREAL)==sizeof(double)) ? 'd' : 'f';
1318 >        } else {                        /* else in sampling mode */
1319 >                if (iropt) {
1320 >                        fputs(progname, stderr);
1321 >                        fputs(": -i, -I supported for pass-through only\n", stderr);
1322 >                        return(1);
1323 >                }
1324 > #ifdef SMLFLT
1325 >                fmtopt[2] = 'f';
1326 > #else
1327 >                fmtopt[2] = 'd';
1328 > #endif
1329                  if (sampcnt <= 0) sampcnt = 10000;
1330          }
1331          sprintf(sampcntbuf, "%d", sampcnt);
# Line 1289 | Line 1344 | done_opts:
1344                  return(my_exec(rcarg)); /* rcontrib does everything */
1345          }
1346          clear_params(&curparams, 0);    /* else load sender surface & params */
1347 +        curmod[0] = '\0';
1348          if (load_scene(sendfn, add_send_object) < 0)
1349                  return(1);
1350          if ((nsbins = prepare_sampler()) <= 0)
# Line 1306 | Line 1362 | done_opts:
1362   #ifdef getc_unlocked
1363          flockfile(rcfp);
1364   #endif
1365 <        if (verbose) {
1365 >        if (verbose > 0) {
1366                  fprintf(stderr, "%s: sampling %d directions", progname, nsbins);
1367                  if (curparams.nsurfs > 1)
1368 <                        fprintf(stderr, " (%d surfaces)\n", curparams.nsurfs);
1368 >                        fprintf(stderr, " (%d elements)\n", curparams.nsurfs);
1369                  else
1370                          fputc('\n', stderr);
1371          }
1372          for (i = 0; i < nsbins; i++)    /* send rcontrib ray samples */
1373                  if (!(*curparams.sample_basis)(&curparams, i, rcfp))
1374                          return(1);
1375 <        return(pclose(rcfp) == 0);      /* all finished! */
1375 >        return(pclose(rcfp) < 0);       /* all finished! */
1376   userr:
1377          if (a < argc-2)
1378                  fprintf(stderr, "%s: unsupported option '%s'", progname, argv[a]);
1379 <        fprintf(stderr, "Usage: %s [-v][rcontrib options] sender.rad receiver.rad [system.rad ..]\n",
1379 >        fprintf(stderr, "Usage: %s [-v][rcontrib options] sender.rad receiver.rad [-i system.oct] [system.rad ..]\n",
1380                                  progname);
1381          return(1);
1382   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines