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.12 by greg, Mon Jul 28 16:30:18 2014 UTC vs.
Revision 2.60 by greg, Tue Jun 3 21:31:51 2025 UTC

# Line 18 | Line 18 | static const char RCSid[] = "$Id$";
18   #include "triangulate.h"
19   #include "platform.h"
20  
21 < #ifdef getc_unlocked            /* avoid horrendous overhead of flockfile */
22 < #undef getc
23 < #define getc    getc_unlocked
21 > #ifndef MAXRCARG
22 > #define MAXRCARG        10000
23   #endif
24  
25 < #ifdef _WIN32
27 < #define SPECIALS        " \t\"$*?"
28 < #define QUOTCHAR        '"'
29 < #else
30 < #define SPECIALS        " \t\n'\"()${}*?[];|&"
31 < #define QUOTCHAR        '\''
32 < #define ALTQUOT         '"'
33 < #endif
25 > int             verbose = 0;            /* verbose mode (< 0 no warnings) */
26  
35 #define MAXRCARG        512
36
37 char            *progname;              /* global argv[0] */
38
39 int             verbose = 0;            /* verbose mode? */
40
27   char            *rcarg[MAXRCARG+1] = {"rcontrib", "-fo+"};
28   int             nrcargs = 2;
29  
# Line 53 | Line 39 | char           *shirchiufn = "disk2square.cal";
39   char            *kfullfn = "klems_full.cal";
40   char            *khalffn = "klems_half.cal";
41   char            *kquarterfn = "klems_quarter.cal";
42 + char            *ciefn = "cieskyscan.cal";
43  
44                                          /* string indicating parameters */
45   const char      PARAMSTART[] = "@rfluxmtx";
# Line 78 | Line 65 | typedef struct {
65          FVECT   uva[2];                 /* tangent axes */
66          int     ntris;                  /* number of triangles */
67          struct ptri {
68 <                float   afrac;                  /* fraction of total area */
68 >                double  afrac;                  /* fraction of total area */
69                  short   vndx[3];                /* vertex indices */
70          }       tri[1];                 /* triangle array (extends struct) */
71   } POLYTRIS;                     /* triangulated polygon */
72  
73   typedef struct param_s {
74 <        char            hemis[32];      /* hemispherical sampling spec. */
74 >        char            sign;           /* '-' for axis reversal */
75 >        char            hemis[31];      /* hemispherical sampling spec. */
76          int             hsiz;           /* hemisphere basis size */
77          int             nsurfs;         /* number of surfaces */
78          SURF            *slist;         /* list of surfaces */
79          FVECT           vup;            /* up vector (zero if unset) */
80          FVECT           nrm;            /* average normal direction */
81 <        FVECT           udir, vdir;     /* v-up tangent axes */
81 >        FVECT           udir, vdir;     /* tangent axes */
82          char            *outfn;         /* output file name (receiver) */
83          int             (*sample_basis)(struct param_s *p, int, FILE *);
84   } PARAMS;                       /* sender/receiver parameters */
# Line 145 | Line 133 | surf_type(const char *otype)
133   static char *
134   oconv_command(int ac, char *av[])
135   {
136 <        static char     oconvbuf[2048] = "!oconv -f";
137 <        char            *cp = oconvbuf + 9;
138 <
139 <        while (ac-- > 0) {
140 <                if (cp >= oconvbuf+(sizeof(oconvbuf)-32)) {
141 <                        fputs(progname, stderr);
142 <                        fputs(": too many file arguments!\n", stderr);
143 <                        exit(1);
144 <                }
157 <                *cp++ = ' ';
158 <                strcpy(cp, *av++);
159 <                while (*cp) cp++;
136 >        static char     oconvbuf[2048] = "!oconv -f ";
137 >        char            *cp = oconvbuf + 10;
138 >        char            *recv = *av++;
139 >        
140 >        if (ac-- <= 0)
141 >                return(NULL);
142 >        if (verbose < 0) {      /* turn off warnings */
143 >                strcpy(cp, "-w ");
144 >                cp += 3;
145          }
146 <        *cp = '\0';
147 <        return(oconvbuf);
148 < }
149 <
150 < /* Check if any of the characters in str2 are found in str1 */
151 < static int
152 < matchany(const char *str1, const char *str2)
153 < {
154 <        while (*str1) {
170 <                const char      *cp = str2;
171 <                while (*cp)
172 <                        if (*cp++ == *str1)
173 <                                return(*str1);
174 <                ++str1;
175 <        }
176 <        return(0);
177 < }
178 <
179 <
180 < /* Convert a set of arguments into a command line for pipe() or system() */
181 < static char *
182 < convert_commandline(char *cmd, const int len, char *av[])
183 < {
184 <        int     match;
185 <        char    *cp;
186 <
187 <        for (cp = cmd; *av != NULL; av++) {
188 <                const int       n = strlen(*av);
189 <                if (cp+n >= cmd+(len-3)) {
190 <                        fputs(progname, stderr);
191 <                        return(NULL);
192 <                }
193 <                if ((match = matchany(*av, SPECIALS))) {
194 <                        const int       quote =
195 < #ifdef ALTQUOT
196 <                                (match == QUOTCHAR) ? ALTQUOT :
197 < #endif
198 <                                        QUOTCHAR;
199 <                        *cp++ = quote;
200 <                        strcpy(cp, *av);
201 <                        cp += n;
202 <                        *cp++ = quote;
146 >        while (ac-- > 0) {      /* copy each argument */
147 >                int     len = strlen(*av);
148 >                if (cp+len+4 >= oconvbuf+sizeof(oconvbuf))
149 >                        goto overrun;
150 >                if (matchany(*av, SPECIALS)) {
151 >                        *cp++ = QUOTCHAR;
152 >                        strcpy(cp, *av++);
153 >                        cp += len;
154 >                        *cp++ = QUOTCHAR;
155                  } else {
156 <                        strcpy(cp, *av);
157 <                        cp += n;
156 >                        strcpy(cp, *av++);
157 >                        cp += len;
158                  }
159                  *cp++ = ' ';
160          }
161 <        if (cp <= cmd)
162 <                return(NULL);
163 <        *--cp = '\0';
164 <        return(cmd);
161 >                                /* receiver goes last */
162 >        if (matchany(recv, SPECIALS)) {
163 >                *cp++ = QUOTCHAR;
164 >                while (*recv) {
165 >                        if (cp >= oconvbuf+(sizeof(oconvbuf)-3))
166 >                                goto overrun;
167 >                        *cp++ = *recv++;
168 >                }
169 >                *cp++ = QUOTCHAR;
170 >                *cp = '\0';
171 >        } else
172 >                strcpy(cp, recv);
173 >        return(oconvbuf);
174 > overrun:
175 >        fputs(progname, stderr);
176 >        fputs(": too many file arguments!\n", stderr);
177 >        exit(1);
178   }
179  
180 + #if defined(_WIN32) || defined(_WIN64)
181 +
182   /* Open a pipe to/from a command given as an argument list */
183   static FILE *
184   popen_arglist(char *av[], char *mode)
# Line 223 | Line 190 | popen_arglist(char *av[], char *mode)
190                  fputs(": command line too long in popen_arglist()\n", stderr);
191                  return(NULL);
192          }
193 <        if (verbose)
193 >        if (verbose > 0)
194                  fprintf(stderr, "%s: opening pipe %s: %s\n",
195                                  progname, (*mode=='w') ? "to" : "from", cmd);
196          return(popen(cmd, mode));
197   }
198  
199 < #ifdef _WIN32
199 > #define pclose_al       pclose
200 >
201   /* Execute system command (Windows version) */
202   static int
203   my_exec(char *av[])
# Line 241 | Line 209 | my_exec(char *av[])
209                  fputs(": command line too long in my_exec()\n", stderr);
210                  return(1);
211          }
212 <        if (verbose)
212 >        if (verbose > 0)
213                  fprintf(stderr, "%s: running: %s\n", progname, cmd);
214          return(system(cmd));
215   }
216 < #else
216 >
217 > #else   /* UNIX */
218 >
219 > static SUBPROC  rt_proc = SP_INACTIVE;  /* we only support one of these */
220 >
221 > /* Open a pipe to a command using an argument list */
222 > static FILE *
223 > popen_arglist(char *av[], char *mode)
224 > {
225 >        int     fd;
226 >
227 >        if (rt_proc.pid > 0) {
228 >                fprintf(stderr, "%s: only one i/o pipe at a time!\n", progname);
229 >                return(NULL);
230 >        }
231 >        if (verbose > 0) {
232 >                char    cmd[4096];
233 >                if (!convert_commandline(cmd, sizeof(cmd), av))
234 >                        strcpy(cmd, "COMMAND TOO LONG TO SHOW");
235 >                fprintf(stderr, "%s: opening pipe %s: %s\n",
236 >                                progname, (*mode=='w') ? "to" : "from", cmd);
237 >        }
238 >        if (*mode == 'w') {
239 >                fd = rt_proc.w = dup(fileno(stdout));
240 >                rt_proc.flags |= PF_FILT_OUT;
241 >        } else if (*mode == 'r') {
242 >                fd = rt_proc.r = dup(fileno(stdin));
243 >                rt_proc.flags |= PF_FILT_INP;
244 >        }
245 >        if (fd < 0 || open_process(&rt_proc, av) <= 0) {
246 >                perror(av[0]);
247 >                return(NULL);
248 >        }
249 >        return(fdopen(fd, mode));
250 > }
251 >
252 > /* Close command pipe (returns -1 on error to match pclose) */
253 > static int
254 > pclose_al(FILE *fp)
255 > {
256 >        int     prob = (fclose(fp) == EOF);
257 >
258 >        if (rt_proc.pid <= 0)
259 >                return(-1);
260 >
261 >        prob |= (close_process(&rt_proc) != 0);
262 >
263 >        return(-prob);
264 > }
265 >
266   /* Execute system command in our stead (Unix version) */
267   static int
268   my_exec(char *av[])
# Line 256 | Line 273 | my_exec(char *av[])
273                  fprintf(stderr, "%s: cannot locate %s\n", progname, av[0]);
274                  return(1);
275          }
276 <        if (verbose) {
276 >        if (verbose > 0) {
277                  char    cmd[4096];
278                  if (!convert_commandline(cmd, sizeof(cmd), av))
279                          strcpy(cmd, "COMMAND TOO LONG TO SHOW");
# Line 266 | Line 283 | my_exec(char *av[])
283          perror(compath);
284          return(1);
285   }
286 +
287   #endif
288  
289   /* Get normalized direction vector from string specification */
# Line 314 | Line 332 | parse_params(PARAMS *p, char *pargs)
332   {
333          char    *cp = pargs;
334          int     nparams = 0;
335 +        int     quot;
336          int     i;
337  
338 <        for ( ; ; )
338 >        for ( ; ; ) {
339                  switch (*cp++) {
340                  case 'h':
341                          if (*cp++ != '=')
342                                  break;
343 +                        if ((*cp == '+') | (*cp == '-'))
344 +                                p->sign = *cp++;
345 +                        else
346 +                                p->sign = '+';
347                          p->hsiz = 0;
348                          i = 0;
349                          while (*cp && !isspace(*cp)) {
# Line 339 | Line 362 | parse_params(PARAMS *p, char *pargs)
362                                  break;
363                          if (!get_direction(p->vup, cp))
364                                  break;
365 +                        while (*cp && !isspace(*cp++))
366 +                                ;
367                          ++nparams;
368                          continue;
369                  case 'o':
370                          if (*cp++ != '=')
371                                  break;
372 +                        quot = 0;
373 +                        if ((*cp == '"') | (*cp == '\''))
374 +                                quot = *cp++;
375                          i = 0;
376 <                        while (*cp && !isspace(*cp++))
377 <                                i++;
376 >                        while (*cp && (quot ? (*cp != quot) : !isspace(*cp))) {
377 >                                i++; cp++;
378 >                        }
379                          if (!i)
380                                  break;
381 <                        *--cp = '\0';
381 >                        if (!*cp) {
382 >                                if (quot)
383 >                                        break;
384 >                                cp[1] = '\0';
385 >                        }
386 >                        *cp = '\0';
387                          p->outfn = savqstr(cp-i);
388 <                        *cp++ = ' ';
388 >                        *cp++ = quot ? quot : ' ';
389                          ++nparams;
390                          continue;
391                  case ' ':
# Line 364 | Line 398 | parse_params(PARAMS *p, char *pargs)
398                  default:
399                          break;
400                  }
401 <        fprintf(stderr, "%s: bad parameter string '%s'\n", progname, pargs);
401 >                break;
402 >        }
403 >        fprintf(stderr, "%s: bad parameter string: %s", progname, pargs);
404          exit(1);
405          return(-1);     /* pro forma return */
406   }
# Line 409 | Line 445 | finish_receiver(void)
445                          curparams.vup[1] = 1;
446          }
447                                          /* determine sample type/bin */
448 <        if (tolower(curparams.hemis[0]) == 'u' | curparams.hemis[0] == '1') {
449 <                binv = "0";             /* uniform sampling -- one bin */
448 >        if ((tolower(curparams.hemis[0]) == 'u') | (curparams.hemis[0] == '1')) {
449 >                sprintf(sbuf, "if(-Dx*%g-Dy*%g-Dz*%g,0,-1)",
450 >                        curparams.nrm[0], curparams.nrm[1], curparams.nrm[2]);
451 >                binv = savqstr(sbuf);
452 >                nbins = "1";            /* uniform sampling -- one bin */
453                  uniform = 1;
454          } else if (tolower(curparams.hemis[0]) == 's' &&
455                                  tolower(curparams.hemis[1]) == 'c') {
# Line 421 | Line 460 | finish_receiver(void)
460                          exit(1);
461                  }
462                  calfn = shirchiufn; shirchiufn = NULL;
463 <                sprintf(sbuf, "SCdim=%d,rNx=%g,rNy=%g,rNz=%g,Ux=%g,Uy=%g,Uz=%g",
463 >                sprintf(sbuf, "SCdim=%d,rNx=%g,rNy=%g,rNz=%g,Ux=%g,Uy=%g,Uz=%g,RHS=%c1",
464                                  curparams.hsiz,
465                          curparams.nrm[0], curparams.nrm[1], curparams.nrm[2],
466 <                        curparams.vup[0], curparams.vup[1], curparams.vup[2]);
466 >                        curparams.vup[0], curparams.vup[1], curparams.vup[2],
467 >                        curparams.sign);
468                  params = savqstr(sbuf);
469                  binv = "scbin";
470                  nbins = "SCdim*SCdim";
471          } else if ((tolower(curparams.hemis[0]) == 'r') |
472                          (tolower(curparams.hemis[0]) == 't')) {
473                  calfn = reinhfn; reinhfn = NULL;
474 <                sprintf(sbuf, "MF=%d,rNx=%g,rNy=%g,rNz=%g,Ux=%g,Uy=%g,Uz=%g",
474 >                sprintf(sbuf, "MF=%d,rNx=%g,rNy=%g,rNz=%g,Ux=%g,Uy=%g,Uz=%g,RHS=%c1",
475                                  curparams.hsiz,
476                          curparams.nrm[0], curparams.nrm[1], curparams.nrm[2],
477 <                        curparams.vup[0], curparams.vup[1], curparams.vup[2]);
477 >                        curparams.vup[0], curparams.vup[1], curparams.vup[2],
478 >                        curparams.sign);
479                  params = savqstr(sbuf);
480                  binv = "rbin";
481                  nbins = "Nrbins";
# Line 457 | Line 498 | finish_receiver(void)
498                  calfn = kquarterfn; kquarterfn = NULL;
499                  binf = "kqbin";
500                  nbins = "Nkqbins";
501 +        } else if (!strcasecmp(curparams.hemis, "cie")) {
502 +                calfn = ciefn; ciefn = NULL;
503 +                sprintf(sbuf, "rNx=%g,rNy=%g,rNz=%g,Ux=%g,Uy=%g,Uz=%g,RHS=%c1",
504 +                        curparams.nrm[0], curparams.nrm[1], curparams.nrm[2],
505 +                        curparams.vup[0], curparams.vup[1], curparams.vup[2],
506 +                        curparams.sign);
507 +                binv = "cbin";
508 +                nbins = "Ncbins";
509          } else {
510                  fprintf(stderr, "%s: unrecognized hemisphere sampling: h=%s\n",
511                                  progname, curparams.hemis);
512                  exit(1);
513          }
514 <        if (!uniform & (curparams.slist->styp == ST_SOURCE)) {
514 >        if (tolower(curparams.hemis[0]) == 'k') {
515 >                sprintf(sbuf, "RHS=%c1", curparams.sign);
516 >                params = savqstr(sbuf);
517 >        }
518 >        if (!uniform) {
519                  SURF    *sp;
520                  for (sp = curparams.slist; sp != NULL; sp = sp->next)
521 <                        if (fabs(sp->area - PI) > 1e-3) {
521 >                        if (sp->styp == ST_SOURCE && fabs(sp->area - PI) > 1e-3) {
522                                  fprintf(stderr, "%s: source '%s' must be 180-degrees\n",
523                                                  progname, sp->sname);
524                                  exit(1);
# Line 509 | Line 562 | make_axes(FVECT uva[2], const FVECT nrm)
562   {
563          int     i;
564  
565 <        uva[1][0] = 0.5 - frandom();
513 <        uva[1][1] = 0.5 - frandom();
514 <        uva[1][2] = 0.5 - frandom();
515 <        for (i = 3; i--; )
516 <                if ((-0.6 < nrm[i]) & (nrm[i] < 0.6))
517 <                        break;
518 <        if (i < 0) {
565 >        if (!getperpendicular(uva[0], nrm, 1)) {
566                  fputs(progname, stderr);
567                  fputs(": bad surface normal in make_axes!\n", stderr);
568                  exit(1);
569          }
570 <        uva[1][i] = 1.0;
524 <        VCROSS(uva[0], uva[1], nrm);
525 <        normalize(uva[0]);
526 <        VCROSS(uva[1], nrm, uva[0]);
570 >        fcross(uva[1], nrm, uva[0]);
571   }
572  
573   /* Illegal sender surfaces end up here */
# Line 632 | Line 676 | ssamp_poly(FVECT orig, SURF *sp, double x)
676                  sp->priv = (void *)ptp;
677          }
678                                          /* pick triangle by partial area */
679 <        for (i = 0; i < ptp->ntris && x > ptp->tri[i].afrac; i++)
679 >        for (i = 0; i < ptp->ntris-1 && x > ptp->tri[i].afrac; i++)
680                  x -= ptp->tri[i].afrac;
681          SDmultiSamp(samp2, 2, x/ptp->tri[i].afrac);
682          samp2[0] *= samp2[1] = sqrt(samp2[1]);
# Line 662 | Line 706 | sample_origin(PARAMS *p, FVECT orig, const FVECT rdir,
706                                          /* special case for lone surface */
707          if (p->nsurfs == 1) {
708                  sp = p->slist;
709 <                if (DOT(sp->snrm, rdir) >= -FTINY) {
709 >                if (DOT(sp->snrm, rdir) >= FTINY) {
710                          fprintf(stderr,
711                                  "%s: internal - sample behind sender '%s'\n",
712                                          progname, sp->sname);
# Line 673 | Line 717 | sample_origin(PARAMS *p, FVECT orig, const FVECT rdir,
717          if (p->nsurfs > nall) {         /* (re)allocate surface area cache */
718                  if (projsa) free(projsa);
719                  projsa = (double *)malloc(sizeof(double)*p->nsurfs);
720 <                if (!projsa) return(0);
720 >                if (projsa == NULL) {
721 >                        fputs(progname, stderr);
722 >                        fputs(": out of memory in sample_origin!\n", stderr);
723 >                        exit(1);
724 >                }
725                  nall = p->nsurfs;
726          }
727                                          /* compute projected areas */
728          for (i = 0, sp = p->slist; sp != NULL; i++, sp = sp->next) {
729                  projsa[i] = -DOT(sp->snrm, rdir) * sp->area;
730 <                tarea += projsa[i] *= (double)(projsa[i] > FTINY);
730 >                tarea += projsa[i] *= (double)(projsa[i] > 0);
731          }
732 <        if (tarea <= FTINY) {           /* wrong side of sender? */
732 >        if (tarea < FTINY*FTINY) {      /* wrong side of sender? */
733                  fputs(progname, stderr);
734                  fputs(": internal - sample behind all sender elements!\n",
735                                  stderr);
# Line 699 | Line 747 | sample_uniform(PARAMS *p, int b, FILE *fp)
747   {
748          int     n = sampcnt;
749          double  samp3[3];
750 <        double  duvw[3];
703 <        FVECT   orig_dir[2];
750 >        FVECT   duvw, orig_dir[2];
751          int     i;
752  
753          if (fp == NULL)                 /* just requesting number of bins? */
# Line 708 | Line 755 | sample_uniform(PARAMS *p, int b, FILE *fp)
755  
756          while (n--) {                   /* stratified hemisphere sampling */
757                  SDmultiSamp(samp3, 3, (n+frandom())/sampcnt);
758 <                SDsquare2disk(duvw, samp3[1], samp3[2]);
758 >                square2disk(duvw, samp3[1], samp3[2]);
759                  duvw[2] = -sqrt(1. - duvw[0]*duvw[0] - duvw[1]*duvw[1]);
760                  for (i = 3; i--; )
761                          orig_dir[1][i] = duvw[0]*p->udir[i] +
# Line 716 | Line 763 | sample_uniform(PARAMS *p, int b, FILE *fp)
763                                                  duvw[2]*p->nrm[i] ;
764                  if (!sample_origin(p, orig_dir[0], orig_dir[1], samp3[0]))
765                          return(0);
766 <                if (fwrite(orig_dir, sizeof(FVECT), 2, fp) != 2)
766 >                if (putbinary(orig_dir, sizeof(FVECT), 2, fp) != 2)
767                          return(0);
768          }
769          return(1);
# Line 728 | Line 775 | sample_shirchiu(PARAMS *p, int b, FILE *fp)
775   {
776          int     n = sampcnt;
777          double  samp3[3];
778 <        double  duvw[3];
732 <        FVECT   orig_dir[2];
778 >        FVECT   duvw, orig_dir[2];
779          int     i;
780  
781          if (fp == NULL)                 /* just requesting number of bins? */
# Line 737 | Line 783 | sample_shirchiu(PARAMS *p, int b, FILE *fp)
783  
784          while (n--) {                   /* stratified sampling */
785                  SDmultiSamp(samp3, 3, (n+frandom())/sampcnt);
786 <                SDsquare2disk(duvw, (b/p->hsiz + samp3[1])/curparams.hsiz,
786 >                square2disk(duvw, (b/p->hsiz + samp3[1])/curparams.hsiz,
787                                  (b%p->hsiz + samp3[2])/curparams.hsiz);
788                  duvw[2] = sqrt(1. - duvw[0]*duvw[0] - duvw[1]*duvw[1]);
789                  for (i = 3; i--; )
# Line 746 | Line 792 | sample_shirchiu(PARAMS *p, int b, FILE *fp)
792                                                  duvw[2]*p->nrm[i] ;
793                  if (!sample_origin(p, orig_dir[0], orig_dir[1], samp3[0]))
794                          return(0);
795 <                if (fwrite(orig_dir, sizeof(FVECT), 2, fp) != 2)
795 >                if (putbinary(orig_dir, sizeof(FVECT), 2, fp) != 2)
796                          return(0);
797          }
798          return(1);
# Line 782 | Line 828 | sample_reinhart(PARAMS *p, int b, FILE *fp)
828          }
829          while (n--) {                   /* stratified sampling */
830                  SDmultiSamp(samp3, 3, (n+frandom())/sampcnt);
831 +                if (row >= RowMax-1)    /* avoid crowding at zenith */
832 +                        samp3[1] *= samp3[1];
833                  alt = (row+samp3[1])*RAH;
834                  azi = (2.*PI)*(col+samp3[2]-.5)/rnaz(row);
835                  duvw[2] = cos(alt);     /* measured from horizon */
836 <                duvw[0] = tcos(azi)*duvw[2];
837 <                duvw[1] = tsin(azi)*duvw[2];
836 >                duvw[0] = tsin(azi)*duvw[2];
837 >                duvw[1] = -tcos(azi)*duvw[2];
838                  duvw[2] = sqrt(1. - duvw[2]*duvw[2]);
839                  for (i = 3; i--; )
840                          orig_dir[1][i] = -duvw[0]*p->udir[i] -
# Line 794 | Line 842 | sample_reinhart(PARAMS *p, int b, FILE *fp)
842                                                  duvw[2]*p->nrm[i] ;
843                  if (!sample_origin(p, orig_dir[0], orig_dir[1], samp3[0]))
844                          return(0);
845 <                if (fwrite(orig_dir, sizeof(FVECT), 2, fp) != 2)
845 >                if (putbinary(orig_dir, sizeof(FVECT), 2, fp) != 2)
846                          return(0);
847          }
848          return(1);
# Line 837 | Line 885 | sample_klems(PARAMS *p, int b, FILE *fp)
885  
886          while (n--) {                   /* stratified sampling */
887                  SDmultiSamp(samp2, 2, (n+frandom())/sampcnt);
888 <                if (!bo_getvec(duvw, b+samp2[1], kbasis[bi]))
888 >                if (!fo_getvec(duvw, b+samp2[1], kbasis[bi]))
889                          return(0);
890                  for (i = 3; i--; )
891 <                        orig_dir[1][i] = duvw[0]*p->udir[i] +
892 <                                                duvw[1]*p->vdir[i] +
891 >                        orig_dir[1][i] = -duvw[0]*p->udir[i] -
892 >                                                duvw[1]*p->vdir[i] -
893                                                  duvw[2]*p->nrm[i] ;
894                  if (!sample_origin(p, orig_dir[0], orig_dir[1], samp2[0]))
895                          return(0);
896 <                if (fwrite(orig_dir, sizeof(FVECT), 2, fp) != 2)
896 >                if (putbinary(orig_dir, sizeof(FVECT), 2, fp) != 2)
897                          return(0);
898          }
899          return(1);
# Line 860 | Line 908 | prepare_sampler(void)
908                  fputs(": no sender surface!\n", stderr);
909                  return(-1);
910          }
911 <        if (curparams.outfn != NULL)    /* misplaced output file spec. */
911 >                                        /* misplaced output file spec. */
912 >        if ((curparams.outfn != NULL) & (verbose >= 0))
913                  fprintf(stderr, "%s: warning - ignoring output file in sender ('%s')\n",
914                                  progname, curparams.outfn);
915                                          /* check/set basis hemisphere */
# Line 880 | Line 929 | prepare_sampler(void)
929                  else
930                          curparams.vup[1] = 1;
931          }
932 <        VCROSS(curparams.udir, curparams.vup, curparams.nrm);
932 >        fcross(curparams.udir, curparams.vup, curparams.nrm);
933          if (normalize(curparams.udir) == 0) {
934                  fputs(progname, stderr);
935                  fputs(": up vector coincides with sender normal\n", stderr);
936                  return(-1);
937          }
938 <        VCROSS(curparams.vdir, curparams.nrm, curparams.udir);
939 <        if (tolower(curparams.hemis[0]) == 'u' | curparams.hemis[0] == '1')
938 >        fcross(curparams.vdir, curparams.nrm, curparams.udir);
939 >        if (curparams.sign == '-') {    /* left-handed coordinate system? */
940 >                curparams.udir[0] *= -1.;
941 >                curparams.udir[1] *= -1.;
942 >                curparams.udir[2] *= -1.;
943 >        }
944 >        if ((tolower(curparams.hemis[0]) == 'u') | (curparams.hemis[0] == '1'))
945                  curparams.sample_basis = sample_uniform;
946          else if (tolower(curparams.hemis[0]) == 's' &&
947                                  tolower(curparams.hemis[1]) == 'c')
# Line 948 | Line 1002 | finish_polygon(SURF *p)
1002                  VCOPY(e1, e2);
1003          }
1004          p->area = normalize(p->snrm)*0.5;
1005 <        return(p->area > FTINY);
1005 >        return(p->area > FTINY*FTINY);
1006   }
1007  
1008   /* Add a surface to our current parameters */
# Line 966 | Line 1020 | add_surface(int st, const char *oname, FILE *fp)
1020          snew = (SURF *)malloc(sizeof(SURF) + sizeof(double)*(n-1));
1021          if (snew == NULL) {
1022                  fputs(progname, stderr);
1023 <                fputs(": out of memory!\n", stderr);
1023 >                fputs(": out of memory in add_surface!\n", stderr);
1024                  exit(1);
1025          }
1026          strncpy(snew->sname, oname, sizeof(snew->sname)-1);
# Line 1011 | Line 1065 | add_surface(int st, const char *oname, FILE *fp)
1065                  snew->area *= PI*snew->area;
1066                  break;
1067          }
1068 <        if (snew->area <= FTINY) {
1068 >        if ((snew->area <= FTINY*FTINY) & (verbose >= 0)) {
1069                  fprintf(stderr, "%s: warning - zero area for surface '%s'\n",
1070                                  progname, oname);
1071                  free(snew);
# Line 1048 | Line 1102 | add_recv_object(FILE *fp)
1102          }
1103                                          /* is it a new receiver? */
1104          if ((st = surf_type(otype)) != ST_NONE) {
1051                if (curparams.slist != NULL && (st == ST_SOURCE) ^
1052                                (curparams.slist->styp == ST_SOURCE)) {
1053                        fputs(progname, stderr);
1054                        fputs(": cannot mix source/non-source receivers!\n", stderr);
1055                        return(-1);
1056                }
1105                  if (strcmp(thismod, curmod)) {
1106                          if (curmod[0]) {        /* output last receiver? */
1107                                  finish_receiver();
# Line 1081 | Line 1129 | static int
1129   add_send_object(FILE *fp)
1130   {
1131          int             st;
1132 <        char            otype[32], oname[128];
1132 >        char            thismod[128], otype[32], oname[128];
1133          int             n;
1134  
1135 <        if (fscanf(fp, "%*s %s %s", otype, oname) != 2)
1135 >        if (fscanf(fp, "%s %s %s", thismod, otype, oname) != 3)
1136                  return(0);              /* must have hit EOF! */
1137          if (!strcmp(otype, "alias")) {
1138                  fscanf(fp, "%*s");      /* skip alias */
# Line 1097 | Line 1145 | add_send_object(FILE *fp)
1145                          fputs(": cannot use source as a sender!\n", stderr);
1146                          return(-1);
1147                  }
1148 +                if (strcmp(thismod, curmod)) {
1149 +                        if (curmod[0]) {
1150 +                                fputs(progname, stderr);
1151 +                                fputs(": warning - multiple modifiers in sender\n",
1152 +                                                stderr);
1153 +                        }
1154 +                        strcpy(curmod, thismod);
1155 +                }
1156                  parse_params(&curparams, newparams);
1157                  newparams[0] = '\0';
1158                  add_surface(st, oname, fp);     /* read & store surface */
# Line 1181 | Line 1237 | main(int argc, char *argv[])
1237          FILE    *rcfp;
1238          int     nsbins;
1239          int     a, i;
1240 +                                        /* set global progname */
1241 +        fixargv0(argv[0]);
1242                                          /* screen rcontrib options */
1243 <        progname = argv[0];
1244 <        for (a = 1; a < argc-2 && argv[a][0] == '-'; a++) {
1245 <                int     na = 1;         /* !! Keep consistent !! */
1246 <                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 1217 | Line 1285 | main(int argc, char *argv[])
1285                          yrs = argv[++a];
1286                          na = 0;
1287                          continue;
1288 <                case 'c':               /* number of samples */
1289 <                        sampcnt = atoi(argv[++a]);
1290 <                        if (sampcnt <= 0)
1291 <                                goto userr;
1292 <                        na = 0;         /* we re-add this later */
1293 <                        continue;
1288 >                case 'c':               /* spectral sampling or count */
1289 >                        switch (argv[a][2]) {
1290 >                        case 's':
1291 >                                na = 2;
1292 >                                break;
1293 >                        case 'w':
1294 >                                na = 3;
1295 >                                break;
1296 >                        case '\0':
1297 >                                sampcnt = atoi(argv[++a]);
1298 >                                if (sampcnt <= 0)
1299 >                                        goto userr;
1300 >                                na = 0;         /* we re-add this later */
1301 >                                continue;
1302 >                        }
1303 >                        break;
1304                  case 'I':               /* only for pass-through mode */
1305                  case 'i':
1306                          iropt = argv[a];
1307                          na = 0;
1308                          continue;
1309 <                case 'V':               /* options without arguments */
1310 <                case 'w':
1309 >                case 'w':               /* options without arguments */
1310 >                        if (!argv[a][2] || strchr("+1tTyY", argv[a][2]) == NULL)
1311 >                                verbose = -1;
1312 >                        break;
1313 >                case 'V':
1314                  case 'u':
1315                  case 'h':
1316                  case 'r':
# Line 1237 | Line 1318 | main(int argc, char *argv[])
1318                  case 'n':               /* options with 1 argument */
1319                  case 's':
1320                  case 'o':
1321 +                case 't':
1322                          na = 2;
1323                          break;
1324                  case 'b':               /* special case */
# Line 1254 | Line 1336 | main(int argc, char *argv[])
1336                          if (argv[a][2] != 'v') na = 2;
1337                          break;
1338                  case 'a':               /* special case */
1339 <                        na = (argv[a][2] == 'v') ? 4 : 2;
1339 >                        if (argv[a][2] == 'p') {
1340 >                                na = 2; /* photon map [+ bandwidth(s)] */
1341 >                                if (a < argc-3 && atoi(argv[a+1]) > 0)
1342 >                                        na += 1 + (a < argc-4 && atoi(argv[a+2]) > 0);
1343 >                        } else
1344 >                                na = (argv[a][2] == 'v') ? 4 : 2;
1345                          break;
1346                  case 'm':               /* special case */
1347                          if (!argv[a][2]) goto userr;
1348                          na = (argv[a][2] == 'e') | (argv[a][2] == 'a') ? 4 : 2;
1349                          break;
1263                case '\0':              /* pass-through mode */
1264                        goto done_opts;
1350                  default:                /* anything else is verbotten */
1351                          goto userr;
1352                  }
# Line 1271 | Line 1356 | main(int argc, char *argv[])
1356                  while (--na)            /* + arguments if any */
1357                          rcarg[nrcargs++] = argv[++a];
1358          }
1274 done_opts:
1359          if (a > argc-2)
1360                  goto userr;             /* check at end of options */
1361          sendfn = argv[a++];             /* assign sender & receiver inputs */
# Line 1303 | Line 1387 | done_opts:
1387                          fputs(": -i, -I supported for pass-through only\n", stderr);
1388                          return(1);
1389                  }
1390 <                fmtopt[2] = (sizeof(RREAL)==sizeof(double)) ? 'd' : 'f';
1390 > #ifdef SMLFLT
1391 >                fmtopt[2] = 'f';
1392 > #else
1393 >                fmtopt[2] = 'd';
1394 > #endif
1395                  if (sampcnt <= 0) sampcnt = 10000;
1396          }
1397          sprintf(sampcntbuf, "%d", sampcnt);
# Line 1322 | Line 1410 | done_opts:
1410                  return(my_exec(rcarg)); /* rcontrib does everything */
1411          }
1412          clear_params(&curparams, 0);    /* else load sender surface & params */
1413 +        curmod[0] = '\0';
1414          if (load_scene(sendfn, add_send_object) < 0)
1415                  return(1);
1416          if ((nsbins = prepare_sampler()) <= 0)
# Line 1339 | Line 1428 | done_opts:
1428   #ifdef getc_unlocked
1429          flockfile(rcfp);
1430   #endif
1431 <        if (verbose) {
1431 >        if (verbose > 0) {
1432                  fprintf(stderr, "%s: sampling %d directions", progname, nsbins);
1433                  if (curparams.nsurfs > 1)
1434                          fprintf(stderr, " (%d elements)\n", curparams.nsurfs);
# Line 1349 | Line 1438 | done_opts:
1438          for (i = 0; i < nsbins; i++)    /* send rcontrib ray samples */
1439                  if (!(*curparams.sample_basis)(&curparams, i, rcfp))
1440                          return(1);
1441 <        return(pclose(rcfp) == 0);      /* all finished! */
1441 >        return(pclose_al(rcfp) < 0);    /* all finished! */
1442   userr:
1443          if (a < argc-2)
1444                  fprintf(stderr, "%s: unsupported option '%s'", progname, argv[a]);
1445 <        fprintf(stderr, "Usage: %s [-v][rcontrib options] sender.rad receiver.rad [system.rad ..]\n",
1445 >        fprintf(stderr, "Usage: %s [-v][rcontrib options] sender.rad receiver.rad [-i system.oct] [system.rad ..]\n",
1446                                  progname);
1447          return(1);
1448   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines