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.41 by greg, Sat Aug 20 03:43:24 2016 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  
20 #ifdef getc_unlocked            /* avoid horrendous overhead of flockfile */
21 #undef getc
22 #define getc    getc_unlocked
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
21   #define MAXRCARG        512
22  
23   char            *progname;              /* global argv[0] */
24  
25 < int             verbose = 0;            /* verbose mode? */
25 > int             verbose = 0;            /* verbose mode (< 0 no warnings) */
26  
27   char            *rcarg[MAXRCARG+1] = {"rcontrib", "-fo+"};
28   int             nrcargs = 2;
# Line 77 | Line 64 | typedef struct {
64          FVECT   uva[2];                 /* tangent axes */
65          int     ntris;                  /* number of triangles */
66          struct ptri {
67 <                float   afrac;                  /* fraction of total area */
67 >                double  afrac;                  /* fraction of total area */
68                  short   vndx[3];                /* vertex indices */
69          }       tri[1];                 /* triangle array (extends struct) */
70   } POLYTRIS;                     /* triangulated polygon */
71  
72   typedef struct param_s {
73 <        char            hemis[32];      /* hemispherical sampling spec. */
73 >        char            sign;           /* '-' for axis reversal */
74 >        char            hemis[31];      /* hemispherical sampling spec. */
75          int             hsiz;           /* hemisphere basis size */
76          int             nsurfs;         /* number of surfaces */
77          SURF            *slist;         /* list of surfaces */
78          FVECT           vup;            /* up vector (zero if unset) */
79          FVECT           nrm;            /* average normal direction */
80 <        FVECT           udir, vdir;     /* v-up tangent axes */
80 >        FVECT           udir, vdir;     /* tangent axes */
81          char            *outfn;         /* output file name (receiver) */
82          int             (*sample_basis)(struct param_s *p, int, FILE *);
83   } PARAMS;                       /* sender/receiver parameters */
84  
85   PARAMS          curparams;
86   char            curmod[128];
87 + char            newparams[1024];
88  
89   typedef int     SURFSAMP(FVECT, SURF *, double);
90  
# Line 109 | Line 98 | SURFSAMP       *orig_in_surf[4] = {
98   static void
99   clear_params(PARAMS *p, int reset_only)
100   {
112        curmod[0] = '\0';
101          while (p->slist != NULL) {
102                  SURF    *sdel = p->slist;
103                  p->slist = sdel->next;
# Line 124 | Line 112 | clear_params(PARAMS *p, int reset_only)
112                  p->outfn = NULL;
113                  return;
114          }
115 <        memset(p, 0, sizeof(curparams));
115 >        memset(p, 0, sizeof(PARAMS));
116   }
117  
118   /* Get surface type from name */
# Line 144 | Line 132 | surf_type(const char *otype)
132   static char *
133   oconv_command(int ac, char *av[])
134   {
135 <        static char     oconvbuf[2048] = "!oconv -f";
136 <        char            *cp = oconvbuf + 9;
137 <
135 >        static char     oconvbuf[2048] = "!oconv -f ";
136 >        char            *cp = oconvbuf + 10;
137 >        char            *recv = *av++;
138 >        
139 >        if (ac-- <= 0)
140 >                return(NULL);
141          while (ac-- > 0) {
151                if (cp >= oconvbuf+(sizeof(oconvbuf)-32)) {
152                        fputs(progname, stderr);
153                        fputs(": too many file arguments!\n", stderr);
154                        exit(1);
155                }
156                *cp++ = ' ';
142                  strcpy(cp, *av++);
143                  while (*cp) cp++;
144 +                *cp++ = ' ';
145 +                if (cp >= oconvbuf+(sizeof(oconvbuf)-32))
146 +                        goto overrun;
147          }
148 <        *cp = '\0';
148 >                                /* receiver goes last */
149 >        if (matchany(recv, SPECIALS)) {
150 >                *cp++ = QUOTCHAR;
151 >                while (*recv) {
152 >                        if (cp >= oconvbuf+(sizeof(oconvbuf)-3))
153 >                                goto overrun;
154 >                        *cp++ = *recv++;
155 >                }
156 >                *cp++ = QUOTCHAR;
157 >                *cp = '\0';
158 >        } else
159 >                strcpy(cp, recv);
160          return(oconvbuf);
161 + overrun:
162 +        fputs(progname, stderr);
163 +        fputs(": too many file arguments!\n", stderr);
164 +        exit(1);
165   }
166  
164 /* Check if any of the characters in str2 are found in str1 */
165 static int
166 matchany(const char *str1, const char *str2)
167 {
168        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;
202                } else {
203                        strcpy(cp, *av);
204                        cp += n;
205                }
206                *cp++ = ' ';
207        }
208        if (cp <= cmd)
209                return(NULL);
210        *--cp = '\0';
211        return(cmd);
212 }
213
167   /* Open a pipe to/from a command given as an argument list */
168   static FILE *
169   popen_arglist(char *av[], char *mode)
# Line 222 | Line 175 | popen_arglist(char *av[], char *mode)
175                  fputs(": command line too long in popen_arglist()\n", stderr);
176                  return(NULL);
177          }
178 <        if (verbose)
178 >        if (verbose > 0)
179                  fprintf(stderr, "%s: opening pipe %s: %s\n",
180                                  progname, (*mode=='w') ? "to" : "from", cmd);
181          return(popen(cmd, mode));
182   }
183  
184 < #ifdef _WIN32
184 > #if defined(_WIN32) || defined(_WIN64)
185   /* Execute system command (Windows version) */
186   static int
187   my_exec(char *av[])
# Line 240 | Line 193 | my_exec(char *av[])
193                  fputs(": command line too long in my_exec()\n", stderr);
194                  return(1);
195          }
196 <        if (verbose)
196 >        if (verbose > 0)
197                  fprintf(stderr, "%s: running: %s\n", progname, cmd);
198          return(system(cmd));
199   }
# Line 255 | Line 208 | my_exec(char *av[])
208                  fprintf(stderr, "%s: cannot locate %s\n", progname, av[0]);
209                  return(1);
210          }
211 <        if (verbose) {
211 >        if (verbose > 0) {
212                  char    cmd[4096];
213                  if (!convert_commandline(cmd, sizeof(cmd), av))
214                          strcpy(cmd, "COMMAND TOO LONG TO SHOW");
# Line 309 | Line 262 | nextchar:
262  
263   /* Parse program parameters (directives) */
264   static int
265 < parse_params(char *pargs)
265 > parse_params(PARAMS *p, char *pargs)
266   {
267          char    *cp = pargs;
268          int     nparams = 0;
269 +        int     quot;
270          int     i;
271  
272 <        for ( ; ; )
272 >        for ( ; ; ) {
273                  switch (*cp++) {
274                  case 'h':
275                          if (*cp++ != '=')
276                                  break;
277 <                        curparams.hsiz = 0;
277 >                        if ((*cp == '+') | (*cp == '-'))
278 >                                p->sign = *cp++;
279 >                        else
280 >                                p->sign = '+';
281 >                        p->hsiz = 0;
282                          i = 0;
283                          while (*cp && !isspace(*cp)) {
284                                  if (isdigit(*cp))
285 <                                        curparams.hsiz = 10*curparams.hsiz +
286 <                                                                *cp - '0';
329 <                                curparams.hemis[i++] = *cp++;
285 >                                        p->hsiz = 10*p->hsiz + *cp - '0';
286 >                                p->hemis[i++] = *cp++;
287                          }
288                          if (!i)
289                                  break;
290 <                        curparams.hemis[i] = '\0';
291 <                        curparams.hsiz += !curparams.hsiz;
290 >                        p->hemis[i] = '\0';
291 >                        p->hsiz += !p->hsiz;
292                          ++nparams;
293                          continue;
294                  case 'u':
295                          if (*cp++ != '=')
296                                  break;
297 <                        if (!get_direction(curparams.vup, cp))
297 >                        if (!get_direction(p->vup, cp))
298                                  break;
299 +                        while (*cp && !isspace(*cp++))
300 +                                ;
301                          ++nparams;
302                          continue;
303                  case 'o':
304                          if (*cp++ != '=')
305                                  break;
306 +                        quot = 0;
307 +                        if ((*cp == '"') | (*cp == '\''))
308 +                                quot = *cp++;
309                          i = 0;
310 <                        while (*cp && !isspace(*cp++))
311 <                                i++;
310 >                        while (*cp && (quot ? (*cp != quot) : !isspace(*cp))) {
311 >                                i++; cp++;
312 >                        }
313                          if (!i)
314                                  break;
315 <                        *--cp = '\0';
316 <                        curparams.outfn = savqstr(cp-i);
317 <                        *cp++ = ' ';
315 >                        if (!*cp) {
316 >                                if (quot)
317 >                                        break;
318 >                                cp[1] = '\0';
319 >                        }
320 >                        *cp = '\0';
321 >                        p->outfn = savqstr(cp-i);
322 >                        *cp++ = quot ? quot : ' ';
323                          ++nparams;
324                          continue;
325                  case ' ':
# Line 364 | Line 332 | parse_params(char *pargs)
332                  default:
333                          break;
334                  }
335 <        fprintf(stderr, "%s: bad parameter string '%s'\n", progname, pargs);
335 >                break;
336 >        }
337 >        fprintf(stderr, "%s: bad parameter string: %s", progname, pargs);
338          exit(1);
339          return(-1);     /* pro forma return */
340   }
# Line 402 | Line 372 | finish_receiver(void)
372                  fputs(": undefined normal for hemisphere sampling\n", stderr);
373                  exit(1);
374          }
375 <        if (normalize(curparams.vup) == 0)
375 >        if (normalize(curparams.vup) == 0) {
376                  if (fabs(curparams.nrm[2]) < .7)
377                          curparams.vup[2] = 1;
378                  else
379                          curparams.vup[1] = 1;
380 +        }
381                                          /* determine sample type/bin */
382          if (tolower(curparams.hemis[0]) == 'u' | curparams.hemis[0] == '1') {
383 <                binv = "0";             /* uniform sampling -- one bin */
383 >                sprintf(sbuf, "if(-Dx*%g-Dy*%g-Dz*%g,0,-1)",
384 >                        curparams.nrm[0], curparams.nrm[1], curparams.nrm[2]);
385 >                binv = savqstr(sbuf);
386 >                nbins = "1";            /* uniform sampling -- one bin */
387                  uniform = 1;
388          } else if (tolower(curparams.hemis[0]) == 's' &&
389                                  tolower(curparams.hemis[1]) == 'c') {
# Line 420 | Line 394 | finish_receiver(void)
394                          exit(1);
395                  }
396                  calfn = shirchiufn; shirchiufn = NULL;
397 <                sprintf(sbuf, "SCdim=%d,Nx=%g,Ny=%g,Nz=%g,Ux=%g,Uy=%g,Uz=%g",
397 >                sprintf(sbuf, "SCdim=%d,rNx=%g,rNy=%g,rNz=%g,Ux=%g,Uy=%g,Uz=%g,RHS=%c1",
398                                  curparams.hsiz,
399                          curparams.nrm[0], curparams.nrm[1], curparams.nrm[2],
400 <                        curparams.vup[0], curparams.vup[1], curparams.vup[2]);
400 >                        curparams.vup[0], curparams.vup[1], curparams.vup[2],
401 >                        curparams.sign);
402                  params = savqstr(sbuf);
403                  binv = "scbin";
404                  nbins = "SCdim*SCdim";
405          } else if ((tolower(curparams.hemis[0]) == 'r') |
406                          (tolower(curparams.hemis[0]) == 't')) {
407                  calfn = reinhfn; reinhfn = NULL;
408 <                sprintf(sbuf, "MF=%d,Nx=%g,Ny=%g,Nz=%g,Ux=%g,Uy=%g,Uz=%g",
408 >                sprintf(sbuf, "MF=%d,rNx=%g,rNy=%g,rNz=%g,Ux=%g,Uy=%g,Uz=%g,RHS=%c1",
409                                  curparams.hsiz,
410                          curparams.nrm[0], curparams.nrm[1], curparams.nrm[2],
411 <                        curparams.vup[0], curparams.vup[1], curparams.vup[2]);
411 >                        curparams.vup[0], curparams.vup[1], curparams.vup[2],
412 >                        curparams.sign);
413                  params = savqstr(sbuf);
414                  binv = "rbin";
415                  nbins = "Nrbins";
# Line 461 | Line 437 | finish_receiver(void)
437                                  progname, curparams.hemis);
438                  exit(1);
439          }
440 +        if (tolower(curparams.hemis[0]) == 'k') {
441 +                sprintf(sbuf, "RHS=%c1", curparams.sign);
442 +                params = savqstr(sbuf);
443 +        }
444          if (!uniform & (curparams.slist->styp == ST_SOURCE)) {
445                  SURF    *sp;
446                  for (sp = curparams.slist; sp != NULL; sp = sp->next)
# Line 508 | Line 488 | make_axes(FVECT uva[2], const FVECT nrm)
488   {
489          int     i;
490  
491 <        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) {
491 >        if (!getperpendicular(uva[0], nrm, 1)) {
492                  fputs(progname, stderr);
493                  fputs(": bad surface normal in make_axes!\n", stderr);
494                  exit(1);
495          }
496 <        uva[1][i] = 1.0;
523 <        VCROSS(uva[0], uva[1], nrm);
524 <        normalize(uva[0]);
525 <        VCROSS(uva[1], nrm, uva[0]);
496 >        fcross(uva[1], nrm, uva[0]);
497   }
498  
499   /* Illegal sender surfaces end up here */
# Line 554 | Line 525 | ssamp_ring(FVECT orig, SURF *sp, double x)
525                  sp->priv = (void *)uva;
526          }
527          SDmultiSamp(samp2, 2, x);
528 <        samp2[0] = sp->farg[6] + sqrt(samp2[0]*sp->area*(1./PI));
528 >        samp2[0] = sqrt(samp2[0]*sp->area*(1./PI) + sp->farg[6]*sp->farg[6]);
529          samp2[1] *= 2.*PI;
530          uv[0] = samp2[0]*tcos(samp2[1]);
531          uv[1] = samp2[0]*tsin(samp2[1]);
# Line 607 | Line 578 | ssamp_poly(FVECT orig, SURF *sp, double x)
578                          }
579                          ptp->ntris = 0;
580                          v2l->p = (void *)ptp;
581 <                        if (!polyTriangulate(v2l, add_triangle))
581 >                        if (!polyTriangulate(v2l, add_triangle)) {
582 >                                fprintf(stderr,
583 >                                        "%s: cannot triangulate polygon '%s'\n",
584 >                                                progname, sp->sname);
585                                  return(0);
586 +                        }
587                          for (i = ptp->ntris; i--; ) {
588                                  int     a = ptp->tri[i].vndx[0];
589                                  int     b = ptp->tri[i].vndx[1];
# Line 627 | Line 602 | ssamp_poly(FVECT orig, SURF *sp, double x)
602                  sp->priv = (void *)ptp;
603          }
604                                          /* pick triangle by partial area */
605 <        for (i = 0; i < ptp->ntris && x > ptp->tri[i].afrac; i++)
605 >        for (i = 0; i < ptp->ntris-1 && x > ptp->tri[i].afrac; i++)
606                  x -= ptp->tri[i].afrac;
607          SDmultiSamp(samp2, 2, x/ptp->tri[i].afrac);
608          samp2[0] *= samp2[1] = sqrt(samp2[1]);
# Line 657 | Line 632 | sample_origin(PARAMS *p, FVECT orig, const FVECT rdir,
632                                          /* special case for lone surface */
633          if (p->nsurfs == 1) {
634                  sp = p->slist;
635 <                if (DOT(sp->snrm, rdir) >= -FTINY)
636 <                        return(0);      /* behind surface! */
635 >                if (DOT(sp->snrm, rdir) >= FTINY) {
636 >                        fprintf(stderr,
637 >                                "%s: internal - sample behind sender '%s'\n",
638 >                                        progname, sp->sname);
639 >                        return(0);
640 >                }
641                  return((*orig_in_surf[sp->styp])(orig, sp, x));
642          }
643          if (p->nsurfs > nall) {         /* (re)allocate surface area cache */
644                  if (projsa) free(projsa);
645                  projsa = (double *)malloc(sizeof(double)*p->nsurfs);
646 <                if (!projsa) return(0);
646 >                if (projsa == NULL) {
647 >                        fputs(progname, stderr);
648 >                        fputs(": out of memory in sample_origin!\n", stderr);
649 >                        exit(1);
650 >                }
651                  nall = p->nsurfs;
652          }
653                                          /* compute projected areas */
# Line 672 | Line 655 | sample_origin(PARAMS *p, FVECT orig, const FVECT rdir,
655                  projsa[i] = -DOT(sp->snrm, rdir) * sp->area;
656                  tarea += projsa[i] *= (double)(projsa[i] > FTINY);
657          }
658 <        if (tarea <= FTINY)             /* wrong side of sender? */
658 >        if (tarea <= FTINY) {           /* wrong side of sender? */
659 >                fputs(progname, stderr);
660 >                fputs(": internal - sample behind all sender elements!\n",
661 >                                stderr);
662                  return(0);
663 +        }
664          tarea *= x;                     /* get surface from list */
665          for (i = 0, sp = p->slist; tarea > projsa[i]; sp = sp->next)
666                  tarea -= projsa[i++];
# Line 703 | Line 690 | sample_uniform(PARAMS *p, int b, FILE *fp)
690                                                  duvw[2]*p->nrm[i] ;
691                  if (!sample_origin(p, orig_dir[0], orig_dir[1], samp3[0]))
692                          return(0);
693 <                if (fwrite(orig_dir, sizeof(FVECT), 2, fp) != 2)
693 >                if (putbinary(orig_dir, sizeof(FVECT), 2, fp) != 2)
694                          return(0);
695          }
696          return(1);
# Line 733 | Line 720 | sample_shirchiu(PARAMS *p, int b, FILE *fp)
720                                                  duvw[2]*p->nrm[i] ;
721                  if (!sample_origin(p, orig_dir[0], orig_dir[1], samp3[0]))
722                          return(0);
723 <                if (fwrite(orig_dir, sizeof(FVECT), 2, fp) != 2)
723 >                if (putbinary(orig_dir, sizeof(FVECT), 2, fp) != 2)
724                          return(0);
725          }
726          return(1);
# Line 746 | Line 733 | sample_reinhart(PARAMS *p, int b, FILE *fp)
733   #define T_NALT  7
734          static const int        tnaz[T_NALT] = {30, 30, 24, 24, 18, 12, 6};
735          const int               RowMax = T_NALT*p->hsiz + 1;
736 <        const double            RAH = (.25*PI)/(RowMax-.5);
736 >        const double            RAH = (.5*PI)/(RowMax-.5);
737   #define rnaz(r)                 (r >= RowMax-1 ? 1 : p->hsiz*tnaz[r/p->hsiz])
738          int                     n = sampcnt;
739          int                     row, col;
# Line 771 | Line 758 | sample_reinhart(PARAMS *p, int b, FILE *fp)
758                  SDmultiSamp(samp3, 3, (n+frandom())/sampcnt);
759                  alt = (row+samp3[1])*RAH;
760                  azi = (2.*PI)*(col+samp3[2]-.5)/rnaz(row);
761 <                duvw[2] = tcos(alt);    /* measured from horizon */
762 <                duvw[0] = tcos(azi)*duvw[2];
763 <                duvw[1] = tsin(azi)*duvw[2];
761 >                duvw[2] = cos(alt);     /* measured from horizon */
762 >                duvw[0] = tsin(azi)*duvw[2];
763 >                duvw[1] = tcos(azi)*duvw[2];
764                  duvw[2] = sqrt(1. - duvw[2]*duvw[2]);
765                  for (i = 3; i--; )
766                          orig_dir[1][i] = -duvw[0]*p->udir[i] -
# Line 781 | Line 768 | sample_reinhart(PARAMS *p, int b, FILE *fp)
768                                                  duvw[2]*p->nrm[i] ;
769                  if (!sample_origin(p, orig_dir[0], orig_dir[1], samp3[0]))
770                          return(0);
771 <                if (fwrite(orig_dir, sizeof(FVECT), 2, fp) != 2)
771 >                if (putbinary(orig_dir, sizeof(FVECT), 2, fp) != 2)
772                          return(0);
773          }
774          return(1);
# Line 824 | Line 811 | sample_klems(PARAMS *p, int b, FILE *fp)
811  
812          while (n--) {                   /* stratified sampling */
813                  SDmultiSamp(samp2, 2, (n+frandom())/sampcnt);
814 <                if (!bo_getvec(duvw, b+samp2[1], kbasis[bi]))
814 >                if (!fo_getvec(duvw, b+samp2[1], kbasis[bi]))
815                          return(0);
816                  for (i = 3; i--; )
817 <                        orig_dir[1][i] = duvw[0]*p->udir[i] +
818 <                                                duvw[1]*p->vdir[i] +
817 >                        orig_dir[1][i] = -duvw[0]*p->udir[i] -
818 >                                                duvw[1]*p->vdir[i] -
819                                                  duvw[2]*p->nrm[i] ;
820                  if (!sample_origin(p, orig_dir[0], orig_dir[1], samp2[0]))
821                          return(0);
822 <                if (fwrite(orig_dir, sizeof(FVECT), 2, fp) != 2)
822 >                if (putbinary(orig_dir, sizeof(FVECT), 2, fp) != 2)
823                          return(0);
824          }
825          return(1);
# Line 847 | Line 834 | prepare_sampler(void)
834                  fputs(": no sender surface!\n", stderr);
835                  return(-1);
836          }
837 <        if (curparams.outfn != NULL)    /* misplaced output file spec. */
837 >                                        /* misplaced output file spec. */
838 >        if ((curparams.outfn != NULL) & (verbose >= 0))
839                  fprintf(stderr, "%s: warning - ignoring output file in sender ('%s')\n",
840                                  progname, curparams.outfn);
841                                          /* check/set basis hemisphere */
# Line 861 | Line 849 | prepare_sampler(void)
849                  fputs(": undefined normal for sender sampling\n", stderr);
850                  return(-1);
851          }
852 <        if (normalize(curparams.vup) == 0)
852 >        if (normalize(curparams.vup) == 0) {
853                  if (fabs(curparams.nrm[2]) < .7)
854                          curparams.vup[2] = 1;
855                  else
856                          curparams.vup[1] = 1;
857 <        VCROSS(curparams.udir, curparams.vup, curparams.nrm);
857 >        }
858 >        fcross(curparams.udir, curparams.vup, curparams.nrm);
859          if (normalize(curparams.udir) == 0) {
860                  fputs(progname, stderr);
861                  fputs(": up vector coincides with sender normal\n", stderr);
862                  return(-1);
863          }
864 <        VCROSS(curparams.vdir, curparams.nrm, curparams.udir);
864 >        fcross(curparams.vdir, curparams.nrm, curparams.udir);
865 >        if (curparams.sign == '-') {    /* left-handed coordinate system? */
866 >                curparams.udir[0] *= -1.;
867 >                curparams.udir[1] *= -1.;
868 >                curparams.udir[2] *= -1.;
869 >        }
870          if (tolower(curparams.hemis[0]) == 'u' | curparams.hemis[0] == '1')
871                  curparams.sample_basis = sample_uniform;
872          else if (tolower(curparams.hemis[0]) == 's' &&
# Line 952 | Line 946 | add_surface(int st, const char *oname, FILE *fp)
946          snew = (SURF *)malloc(sizeof(SURF) + sizeof(double)*(n-1));
947          if (snew == NULL) {
948                  fputs(progname, stderr);
949 <                fputs(": out of memory!\n", stderr);
949 >                fputs(": out of memory in add_surface!\n", stderr);
950                  exit(1);
951          }
952          strncpy(snew->sname, oname, sizeof(snew->sname)-1);
# Line 989 | Line 983 | add_surface(int st, const char *oname, FILE *fp)
983          case ST_SOURCE:
984                  if (snew->nfargs != 4)
985                          goto badcount;
986 <                VCOPY(snew->snrm, snew->farg);
986 >                for (n = 3; n--; )      /* need to reverse "normal" */
987 >                        snew->snrm[n] = -snew->farg[n];
988                  if (normalize(snew->snrm) == 0)
989                          goto badnorm;
990                  snew->area = sin((PI/180./2.)*snew->farg[3]);
991                  snew->area *= PI*snew->area;
992                  break;
993          }
994 <        if (snew->area <= FTINY) {
994 >        if ((snew->area <= FTINY) & (verbose >= 0)) {
995                  fprintf(stderr, "%s: warning - zero area for surface '%s'\n",
996                                  progname, oname);
997                  free(snew);
# Line 1008 | Line 1003 | add_surface(int st, const char *oname, FILE *fp)
1003          curparams.nsurfs++;
1004          return;
1005   badcount:
1006 <        fprintf(stderr, "%s: bad argument count for surface '%s'\n",
1006 >        fprintf(stderr, "%s: bad argument count for surface element '%s'\n",
1007                          progname, oname);
1008          exit(1);
1009   badnorm:
1010 <        fprintf(stderr, "%s: bad orientation for surface '%s'\n",
1010 >        fprintf(stderr, "%s: bad orientation for surface element '%s'\n",
1011                          progname, oname);
1012          exit(1);
1013   }
# Line 1044 | Line 1039 | add_recv_object(FILE *fp)
1039                                  finish_receiver();
1040                                  clear_params(&curparams, 1);
1041                          }
1042 +                        parse_params(&curparams, newparams);
1043 +                        newparams[0] = '\0';
1044                          strcpy(curmod, thismod);
1045                  }
1046                  add_surface(st, oname, fp);     /* read & store surface */
1047                  return(1);
1048          }
1049                                          /* else skip arguments */
1050 <        if (!fscanf(fp, "%d", &n)) return;
1050 >        if (!fscanf(fp, "%d", &n)) return(0);
1051          while (n-- > 0) fscanf(fp, "%*s");
1052 <        if (!fscanf(fp, "%d", &n)) return;
1052 >        if (!fscanf(fp, "%d", &n)) return(0);
1053          while (n-- > 0) fscanf(fp, "%*d");
1054 <        if (!fscanf(fp, "%d", &n)) return;
1054 >        if (!fscanf(fp, "%d", &n)) return(0);
1055          while (n-- > 0) fscanf(fp, "%*f");
1056          return(0);
1057   }
# Line 1064 | Line 1061 | static int
1061   add_send_object(FILE *fp)
1062   {
1063          int             st;
1064 <        char            otype[32], oname[128];
1064 >        char            thismod[128], otype[32], oname[128];
1065          int             n;
1066  
1067 <        if (fscanf(fp, "%*s %s %s", otype, oname) != 2)
1067 >        if (fscanf(fp, "%s %s %s", thismod, otype, oname) != 3)
1068                  return(0);              /* must have hit EOF! */
1069          if (!strcmp(otype, "alias")) {
1070                  fscanf(fp, "%*s");      /* skip alias */
# Line 1080 | Line 1077 | add_send_object(FILE *fp)
1077                          fputs(": cannot use source as a sender!\n", stderr);
1078                          return(-1);
1079                  }
1080 +                if (strcmp(thismod, curmod)) {
1081 +                        if (curmod[0]) {
1082 +                                fputs(progname, stderr);
1083 +                                fputs(": warning - multiple modifiers in sender\n",
1084 +                                                stderr);
1085 +                        }
1086 +                        strcpy(curmod, thismod);
1087 +                }
1088 +                parse_params(&curparams, newparams);
1089 +                newparams[0] = '\0';
1090                  add_surface(st, oname, fp);     /* read & store surface */
1091                  return(0);
1092          }
1093                                          /* else skip arguments */
1094 <        if (!fscanf(fp, "%d", &n)) return;
1094 >        if (!fscanf(fp, "%d", &n)) return(0);
1095          while (n-- > 0) fscanf(fp, "%*s");
1096 <        if (!fscanf(fp, "%d", &n)) return;
1096 >        if (!fscanf(fp, "%d", &n)) return(0);
1097          while (n-- > 0) fscanf(fp, "%*d");
1098 <        if (!fscanf(fp, "%d", &n)) return;
1098 >        if (!fscanf(fp, "%d", &n)) return(0);
1099          while (n-- > 0) fscanf(fp, "%*f");
1100          return(0);
1101   }
# Line 1128 | Line 1135 | load_scene(const char *inspec, int (*ocb)(FILE *))
1135                          if (!isspace(c) && fscanf(fp, "%s", inpbuf) == 1 &&
1136                                          !strcmp(inpbuf, PARAMSTART)) {
1137                                  if (fgets(inpbuf, sizeof(inpbuf), fp) != NULL)
1138 <                                        parse_params(inpbuf);
1138 >                                        strcat(newparams, inpbuf);
1139                                  continue;
1140                          }
1141 <                        while ((c = getc(fp)) != EOF && c != '\n');
1141 >                        while ((c = getc(fp)) != EOF && c != '\n')
1142                                  ;       /* else skipping comment */
1143                          continue;
1144                  }
# Line 1156 | Line 1163 | main(int argc, char *argv[])
1163   {
1164          char    fmtopt[6] = "-faa";     /* default output is ASCII */
1165          char    *xrs=NULL, *yrs=NULL, *ldopt=NULL;
1166 +        char    *iropt = NULL;
1167          char    *sendfn;
1168          char    sampcntbuf[32], nsbinbuf[32];
1169          FILE    *rcfp;
# Line 1163 | Line 1171 | main(int argc, char *argv[])
1171          int     a, i;
1172                                          /* screen rcontrib options */
1173          progname = argv[0];
1174 <        for (a = 1; a < argc-2 && argv[a][0] == '-'; a++) {
1175 <                int     na = 1;         /* !! Keep consistent !! */
1176 <                switch (argv[a][1]) {
1174 >        for (a = 1; a < argc-2; a++) {
1175 >                int     na;
1176 >                                        /* check for argument expansion */
1177 >                while ((na = expandarg(&argc, &argv, a)) > 0)
1178 >                        ;
1179 >                if (na < 0) {
1180 >                        fprintf(stderr, "%s: cannot expand '%s'\n",
1181 >                                        progname, argv[a]);
1182 >                        return(1);
1183 >                }
1184 >                if (argv[a][0] != '-' || !argv[a][1])
1185 >                        break;
1186 >                na = 1;
1187 >                switch (argv[a][1]) {   /* !! Keep consistent !! */
1188                  case 'v':               /* verbose mode */
1189 <                        verbose = !verbose;
1189 >                        verbose = 1;
1190                          na = 0;
1191                          continue;
1192                  case 'f':               /* special case for -fo, -ff, etc. */
# Line 1180 | Line 1199 | main(int argc, char *argv[])
1199                          case 'f':
1200                          case 'd':
1201                          case 'c':
1202 <                                if (!(fmtopt[4] = argv[a][3]))
1203 <                                        fmtopt[4] = argv[a][2];
1204 <                                fmtopt[3] = argv[a][2];
1202 >                                if (!(fmtopt[3] = argv[a][3]))
1203 >                                        fmtopt[3] = argv[a][2];
1204 >                                fmtopt[2] = argv[a][2];
1205                                  na = 0;
1206                                  continue;       /* will pass later */
1207                          default:
# Line 1198 | Line 1217 | main(int argc, char *argv[])
1217                          na = 0;
1218                          continue;
1219                  case 'c':               /* number of samples */
1220 <                        sampcnt = atoi(argv[a+1]);
1220 >                        sampcnt = atoi(argv[++a]);
1221                          if (sampcnt <= 0)
1222                                  goto userr;
1223                          na = 0;         /* we re-add this later */
1224                          continue;
1225 <                case 'V':               /* options without arguments */
1207 <                case 'w':
1208 <                case 'u':
1225 >                case 'I':               /* only for pass-through mode */
1226                  case 'i':
1227 +                        iropt = argv[a];
1228 +                        na = 0;
1229 +                        continue;
1230 +                case 'w':               /* options without arguments */
1231 +                        if (argv[a][2] != '+') verbose = -1;
1232 +                case 'V':
1233 +                case 'u':
1234                  case 'h':
1235                  case 'r':
1236                          break;
# Line 1230 | Line 1254 | main(int argc, char *argv[])
1254                          if (argv[a][2] != 'v') na = 2;
1255                          break;
1256                  case 'a':               /* special case */
1257 <                        na = (argv[a][2] == 'v') ? 4 : 2;
1257 >                        if (argv[a][2] == 'p') {
1258 >                                na = 2; /* photon map [+ bandwidth(s)] */
1259 >                                if (a < argc-3 && atoi(argv[a+1]) > 0)
1260 >                                        na += 1 + (a < argc-4 && atoi(argv[a+2]) > 0);
1261 >                        } else
1262 >                                na = (argv[a][2] == 'v') ? 4 : 2;
1263                          break;
1264                  case 'm':               /* special case */
1265                          if (!argv[a][2]) goto userr;
1266                          na = (argv[a][2] == 'e') | (argv[a][2] == 'a') ? 4 : 2;
1267                          break;
1239                case '\0':              /* pass-through mode */
1240                        goto done_opts;
1268                  default:                /* anything else is verbotten */
1269                          goto userr;
1270                  }
# Line 1247 | Line 1274 | main(int argc, char *argv[])
1274                  while (--na)            /* + arguments if any */
1275                          rcarg[nrcargs++] = argv[++a];
1276          }
1250 done_opts:
1277          if (a > argc-2)
1278                  goto userr;             /* check at end of options */
1279          sendfn = argv[a++];             /* assign sender & receiver inputs */
1280          if (sendfn[0] == '-') {         /* user wants pass-through mode? */
1281                  if (sendfn[1]) goto userr;
1282                  sendfn = NULL;
1283 +                if (iropt) {
1284 +                        CHECKARGC(1);
1285 +                        rcarg[nrcargs++] = iropt;
1286 +                }
1287                  if (xrs) {
1288                          CHECKARGC(2);
1289                          rcarg[nrcargs++] = "-x";
# Line 1269 | Line 1299 | done_opts:
1299                          rcarg[nrcargs++] = ldopt;
1300                  }
1301                  if (sampcnt <= 0) sampcnt = 1;
1302 <        } else {                        /* else FVECT determines input format */
1303 <                fmtopt[3] = (sizeof(RREAL)==sizeof(double)) ? 'd' : 'f';
1302 >        } else {                        /* else in sampling mode */
1303 >                if (iropt) {
1304 >                        fputs(progname, stderr);
1305 >                        fputs(": -i, -I supported for pass-through only\n", stderr);
1306 >                        return(1);
1307 >                }
1308 >                fmtopt[2] = (sizeof(RREAL)==sizeof(double)) ? 'd' : 'f';
1309                  if (sampcnt <= 0) sampcnt = 10000;
1310          }
1311          sprintf(sampcntbuf, "%d", sampcnt);
# Line 1289 | Line 1324 | done_opts:
1324                  return(my_exec(rcarg)); /* rcontrib does everything */
1325          }
1326          clear_params(&curparams, 0);    /* else load sender surface & params */
1327 +        curmod[0] = '\0';
1328          if (load_scene(sendfn, add_send_object) < 0)
1329                  return(1);
1330          if ((nsbins = prepare_sampler()) <= 0)
# Line 1306 | Line 1342 | done_opts:
1342   #ifdef getc_unlocked
1343          flockfile(rcfp);
1344   #endif
1345 <        if (verbose) {
1345 >        if (verbose > 0) {
1346                  fprintf(stderr, "%s: sampling %d directions", progname, nsbins);
1347                  if (curparams.nsurfs > 1)
1348 <                        fprintf(stderr, " (%d surfaces)\n", curparams.nsurfs);
1348 >                        fprintf(stderr, " (%d elements)\n", curparams.nsurfs);
1349                  else
1350                          fputc('\n', stderr);
1351          }
1352          for (i = 0; i < nsbins; i++)    /* send rcontrib ray samples */
1353                  if (!(*curparams.sample_basis)(&curparams, i, rcfp))
1354                          return(1);
1355 <        return(pclose(rcfp) == 0);      /* all finished! */
1355 >        return(pclose(rcfp) < 0);       /* all finished! */
1356   userr:
1357          if (a < argc-2)
1358                  fprintf(stderr, "%s: unsupported option '%s'", progname, argv[a]);
1359 <        fprintf(stderr, "Usage: %s [-v][rcontrib options] sender.rad receiver.rad [system.rad ..]\n",
1359 >        fprintf(stderr, "Usage: %s [-v][rcontrib options] sender.rad receiver.rad [-i system.oct] [system.rad ..]\n",
1360                                  progname);
1361          return(1);
1362   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines