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

Comparing ray/src/util/rfluxmtx.c (file contents):
Revision 2.8 by greg, Fri Jul 25 15:46:10 2014 UTC vs.
Revision 2.62 by greg, Mon Oct 20 18:55:19 2025 UTC

# Line 11 | Line 11 | static const char RCSid[] = "$Id$";
11   #include <stdlib.h>
12   #include "rtio.h"
13   #include "rtmath.h"
14 + #include "rtprocess.h"
15   #include "bsdf.h"
16   #include "bsdf_m.h"
17   #include "random.h"
18   #include "triangulate.h"
19   #include "platform.h"
20  
21 < #ifdef getc_unlocked            /* avoid horrendous overhead of flockfile */
22 < #undef getc
22 < #define getc    getc_unlocked
21 > #ifndef MAXRCARG
22 > #define MAXRCARG        10000
23   #endif
24  
25 < #ifdef _WIN32
26 < #define SPECIALS        " \t\"$*?"
27 < #define QUOTCHAR        '"'
28 < #else
29 < #define SPECIALS        " \t\n'\"()${}*?[];|&"
30 < #define QUOTCHAR        '\''
31 < #define ALTQUOT         '"'
32 < #endif
25 > #define         NOWARN          1
26 > #define         VERBO           2
27  
28 < #define MAXRCARG        512
28 > int             verbose = 0;            /* verbose/warning mode */
29  
36 char            *progname;              /* global argv[0] */
37
38 int             verbose = 0;            /* verbose mode? */
39
30   char            *rcarg[MAXRCARG+1] = {"rcontrib", "-fo+"};
31   int             nrcargs = 2;
32  
# Line 52 | Line 42 | char           *shirchiufn = "disk2square.cal";
42   char            *kfullfn = "klems_full.cal";
43   char            *khalffn = "klems_half.cal";
44   char            *kquarterfn = "klems_quarter.cal";
45 + char            *ciefn = "cieskyscan.cal";
46  
47                                          /* string indicating parameters */
48 < const char      PARAMSTART[] = "@rfluxmtx";
48 > #define PARAMSTART      "@rfluxmtx"
49  
50                                  /* surface type IDs */
51   #define ST_NONE         0
# Line 77 | Line 68 | typedef struct {
68          FVECT   uva[2];                 /* tangent axes */
69          int     ntris;                  /* number of triangles */
70          struct ptri {
71 <                float   afrac;                  /* fraction of total area */
71 >                double  afrac;                  /* fraction of total area */
72                  short   vndx[3];                /* vertex indices */
73          }       tri[1];                 /* triangle array (extends struct) */
74   } POLYTRIS;                     /* triangulated polygon */
75  
76   typedef struct param_s {
77 <        char            hemis[32];      /* hemispherical sampling spec. */
77 >        char            sign;           /* '-' for axis reversal */
78 >        char            hemis[31];      /* hemispherical sampling spec. */
79          int             hsiz;           /* hemisphere basis size */
80          int             nsurfs;         /* number of surfaces */
81          SURF            *slist;         /* list of surfaces */
82          FVECT           vup;            /* up vector (zero if unset) */
83          FVECT           nrm;            /* average normal direction */
84 <        FVECT           udir, vdir;     /* v-up tangent axes */
84 >        FVECT           udir, vdir;     /* tangent axes */
85          char            *outfn;         /* output file name (receiver) */
86          int             (*sample_basis)(struct param_s *p, int, FILE *);
87   } PARAMS;                       /* sender/receiver parameters */
# Line 118 | Line 110 | clear_params(PARAMS *p, int reset_only)
110                  free(sdel);
111          }
112          if (reset_only) {
113 +                p->slist = NULL;
114                  p->nsurfs = 0;
115                  memset(p->nrm, 0, sizeof(FVECT));
116                  memset(p->vup, 0, sizeof(FVECT));
# Line 144 | Line 137 | surf_type(const char *otype)
137   static char *
138   oconv_command(int ac, char *av[])
139   {
140 <        static char     oconvbuf[2048] = "!oconv -f";
141 <        char            *cp = oconvbuf + 9;
142 <
143 <        while (ac-- > 0) {
144 <                if (cp >= oconvbuf+(sizeof(oconvbuf)-32)) {
145 <                        fputs(progname, stderr);
146 <                        fputs(": too many file arguments!\n", stderr);
147 <                        exit(1);
148 <                }
156 <                *cp++ = ' ';
157 <                strcpy(cp, *av++);
158 <                while (*cp) cp++;
140 >        static char     oconvbuf[2048] = "!oconv -f ";
141 >        char            *cp = oconvbuf + 10;
142 >        char            *recv = *av++;
143 >        
144 >        if (ac-- <= 0)
145 >                return(NULL);
146 >        if (verbose & NOWARN) { /* warnings off? */
147 >                strcpy(cp, "-w ");
148 >                cp += 3;
149          }
150 <        *cp = '\0';
151 <        return(oconvbuf);
152 < }
153 <
154 < /* Check if any of the characters in str2 are found in str1 */
155 < static int
156 < matchany(const char *str1, const char *str2)
157 < {
158 <        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;
150 >        while (ac-- > 0) {      /* copy each argument */
151 >                int     len = strlen(*av);
152 >                if (cp+len+4 >= oconvbuf+sizeof(oconvbuf))
153 >                        goto overrun;
154 >                if (matchany(*av, SPECIALS)) {
155 >                        *cp++ = QUOTCHAR;
156 >                        strcpy(cp, *av++);
157 >                        cp += len;
158 >                        *cp++ = QUOTCHAR;
159                  } else {
160 <                        strcpy(cp, *av);
161 <                        cp += n;
160 >                        strcpy(cp, *av++);
161 >                        cp += len;
162                  }
163                  *cp++ = ' ';
164          }
165 <        if (cp <= cmd)
166 <                return(NULL);
167 <        *--cp = '\0';
168 <        return(cmd);
165 >                                /* receiver goes last */
166 >        if (matchany(recv, SPECIALS)) {
167 >                *cp++ = QUOTCHAR;
168 >                while (*recv) {
169 >                        if (cp >= oconvbuf+(sizeof(oconvbuf)-3))
170 >                                goto overrun;
171 >                        *cp++ = *recv++;
172 >                }
173 >                *cp++ = QUOTCHAR;
174 >                *cp = '\0';
175 >        } else
176 >                strcpy(cp, recv);
177 >        return(oconvbuf);
178 > overrun:
179 >        fputs(progname, stderr);
180 >        fputs(": too many file arguments!\n", stderr);
181 >        exit(1);
182   }
183  
184 + #if defined(_WIN32) || defined(_WIN64)
185 +
186   /* Open a pipe to/from a command given as an argument list */
187   static FILE *
188   popen_arglist(char *av[], char *mode)
# Line 222 | Line 194 | popen_arglist(char *av[], char *mode)
194                  fputs(": command line too long in popen_arglist()\n", stderr);
195                  return(NULL);
196          }
197 <        if (verbose)
197 >        if (verbose & VERBO)
198                  fprintf(stderr, "%s: opening pipe %s: %s\n",
199                                  progname, (*mode=='w') ? "to" : "from", cmd);
200          return(popen(cmd, mode));
201   }
202  
203 < #ifdef _WIN32
203 > #define pclose_al       pclose
204 >
205   /* Execute system command (Windows version) */
206   static int
207   my_exec(char *av[])
# Line 240 | Line 213 | my_exec(char *av[])
213                  fputs(": command line too long in my_exec()\n", stderr);
214                  return(1);
215          }
216 <        if (verbose)
216 >        if (verbose & VERBO)
217                  fprintf(stderr, "%s: running: %s\n", progname, cmd);
218          return(system(cmd));
219   }
220 < #else
220 >
221 > #else   /* UNIX */
222 >
223 > static SUBPROC  rt_proc = SP_INACTIVE;  /* we only support one of these */
224 >
225 > /* Open a pipe to a command using an argument list */
226 > static FILE *
227 > popen_arglist(char *av[], char *mode)
228 > {
229 >        int     fd;
230 >
231 >        if (rt_proc.pid > 0) {
232 >                fprintf(stderr, "%s: only one i/o pipe at a time!\n", progname);
233 >                return(NULL);
234 >        }
235 >        if (verbose & VERBO) {
236 >                char    cmd[4096];
237 >                if (!convert_commandline(cmd, sizeof(cmd), av))
238 >                        strcpy(cmd, "COMMAND TOO LONG TO SHOW");
239 >                fprintf(stderr, "%s: opening pipe %s: %s\n",
240 >                                progname, (*mode=='w') ? "to" : "from", cmd);
241 >        }
242 >        if (*mode == 'w') {
243 >                fd = rt_proc.w = dup(fileno(stdout));
244 >                rt_proc.flags |= PF_FILT_OUT;
245 >        } else if (*mode == 'r') {
246 >                fd = rt_proc.r = dup(fileno(stdin));
247 >                rt_proc.flags |= PF_FILT_INP;
248 >        }
249 >        if (fd < 0 || open_process(&rt_proc, av) <= 0) {
250 >                perror(av[0]);
251 >                return(NULL);
252 >        }
253 >        return(fdopen(fd, mode));
254 > }
255 >
256 > /* Close command pipe (returns -1 on error to match pclose) */
257 > static int
258 > pclose_al(FILE *fp)
259 > {
260 >        int     prob = (fclose(fp) == EOF);
261 >
262 >        if (rt_proc.pid <= 0)
263 >                return(-1);
264 >
265 >        prob |= (close_process(&rt_proc) != 0);
266 >
267 >        return(-prob);
268 > }
269 >
270   /* Execute system command in our stead (Unix version) */
271   static int
272   my_exec(char *av[])
# Line 255 | Line 277 | my_exec(char *av[])
277                  fprintf(stderr, "%s: cannot locate %s\n", progname, av[0]);
278                  return(1);
279          }
280 <        if (verbose) {
280 >        if (verbose & VERBO) {
281                  char    cmd[4096];
282                  if (!convert_commandline(cmd, sizeof(cmd), av))
283                          strcpy(cmd, "COMMAND TOO LONG TO SHOW");
# Line 265 | Line 287 | my_exec(char *av[])
287          perror(compath);
288          return(1);
289   }
290 +
291   #endif
292  
293   /* Get normalized direction vector from string specification */
# Line 313 | Line 336 | parse_params(PARAMS *p, char *pargs)
336   {
337          char    *cp = pargs;
338          int     nparams = 0;
339 +        int     quot;
340          int     i;
341  
342 <        for ( ; ; )
342 >        for ( ; ; ) {
343                  switch (*cp++) {
344                  case 'h':
345                          if (*cp++ != '=')
346                                  break;
347 +                        if ((*cp == '+') | (*cp == '-'))
348 +                                p->sign = *cp++;
349 +                        else
350 +                                p->sign = '+';
351                          p->hsiz = 0;
352                          i = 0;
353                          while (*cp && !isspace(*cp)) {
# Line 338 | Line 366 | parse_params(PARAMS *p, char *pargs)
366                                  break;
367                          if (!get_direction(p->vup, cp))
368                                  break;
369 +                        while (*cp && !isspace(*cp++))
370 +                                ;
371                          ++nparams;
372                          continue;
373                  case 'o':
374                          if (*cp++ != '=')
375                                  break;
376 +                        quot = 0;
377 +                        if ((*cp == '"') | (*cp == '\''))
378 +                                quot = *cp++;
379                          i = 0;
380 <                        while (*cp && !isspace(*cp++))
381 <                                i++;
380 >                        while (*cp && (quot ? (*cp != quot) : !isspace(*cp))) {
381 >                                i++; cp++;
382 >                        }
383                          if (!i)
384                                  break;
385 <                        *--cp = '\0';
385 >                        if (!*cp) {
386 >                                if (quot)
387 >                                        break;
388 >                                cp[1] = '\0';
389 >                        }
390 >                        *cp = '\0';
391                          p->outfn = savqstr(cp-i);
392 <                        *cp++ = ' ';
392 >                        *cp++ = quot ? quot : ' ';
393                          ++nparams;
394                          continue;
395                  case ' ':
# Line 363 | Line 402 | parse_params(PARAMS *p, char *pargs)
402                  default:
403                          break;
404                  }
405 <        fprintf(stderr, "%s: bad parameter string '%s'\n", progname, pargs);
405 >                break;
406 >        }
407 >        fprintf(stderr, "%s: bad parameter string: %s", progname, pargs);
408          exit(1);
409          return(-1);     /* pro forma return */
410   }
# Line 401 | Line 442 | finish_receiver(void)
442                  fputs(": undefined normal for hemisphere sampling\n", stderr);
443                  exit(1);
444          }
445 <        if (normalize(curparams.vup) == 0)
445 >        if (normalize(curparams.vup) == 0) {
446                  if (fabs(curparams.nrm[2]) < .7)
447                          curparams.vup[2] = 1;
448                  else
449                          curparams.vup[1] = 1;
450 +        }
451                                          /* determine sample type/bin */
452 <        if (tolower(curparams.hemis[0]) == 'u' | curparams.hemis[0] == '1') {
453 <                binv = "0";             /* uniform sampling -- one bin */
452 >        if ((tolower(curparams.hemis[0]) == 'u') | (curparams.hemis[0] == '1')) {
453 >                sprintf(sbuf, "if(-Dx*%g-Dy*%g-Dz*%g,0,-1)",
454 >                        curparams.nrm[0], curparams.nrm[1], curparams.nrm[2]);
455 >                binv = savqstr(sbuf);
456 >                nbins = "1";            /* uniform sampling -- one bin */
457                  uniform = 1;
458          } else if (tolower(curparams.hemis[0]) == 's' &&
459                                  tolower(curparams.hemis[1]) == 'c') {
# Line 419 | Line 464 | finish_receiver(void)
464                          exit(1);
465                  }
466                  calfn = shirchiufn; shirchiufn = NULL;
467 <                sprintf(sbuf, "SCdim=%d,Nx=%g,Ny=%g,Nz=%g,Ux=%g,Uy=%g,Uz=%g",
467 >                sprintf(sbuf, "SCdim=%d,rNx=%g,rNy=%g,rNz=%g,Ux=%g,Uy=%g,Uz=%g,RHS=%c1",
468                                  curparams.hsiz,
469                          curparams.nrm[0], curparams.nrm[1], curparams.nrm[2],
470 <                        curparams.vup[0], curparams.vup[1], curparams.vup[2]);
470 >                        curparams.vup[0], curparams.vup[1], curparams.vup[2],
471 >                        curparams.sign);
472                  params = savqstr(sbuf);
473                  binv = "scbin";
474                  nbins = "SCdim*SCdim";
475          } else if ((tolower(curparams.hemis[0]) == 'r') |
476                          (tolower(curparams.hemis[0]) == 't')) {
477                  calfn = reinhfn; reinhfn = NULL;
478 <                sprintf(sbuf, "MF=%d,Nx=%g,Ny=%g,Nz=%g,Ux=%g,Uy=%g,Uz=%g",
478 >                sprintf(sbuf, "MF=%d,rNx=%g,rNy=%g,rNz=%g,Ux=%g,Uy=%g,Uz=%g,RHS=%c1",
479                                  curparams.hsiz,
480                          curparams.nrm[0], curparams.nrm[1], curparams.nrm[2],
481 <                        curparams.vup[0], curparams.vup[1], curparams.vup[2]);
481 >                        curparams.vup[0], curparams.vup[1], curparams.vup[2],
482 >                        curparams.sign);
483                  params = savqstr(sbuf);
484                  binv = "rbin";
485                  nbins = "Nrbins";
# Line 455 | Line 502 | finish_receiver(void)
502                  calfn = kquarterfn; kquarterfn = NULL;
503                  binf = "kqbin";
504                  nbins = "Nkqbins";
505 +        } else if (!strcasecmp(curparams.hemis, "cie")) {
506 +                calfn = ciefn; ciefn = NULL;
507 +                sprintf(sbuf, "rNx=%g,rNy=%g,rNz=%g,Ux=%g,Uy=%g,Uz=%g,RHS=%c1",
508 +                        curparams.nrm[0], curparams.nrm[1], curparams.nrm[2],
509 +                        curparams.vup[0], curparams.vup[1], curparams.vup[2],
510 +                        curparams.sign);
511 +                params = savqstr(sbuf);
512 +                binv = "cbin";
513 +                nbins = "Ncbins";
514          } else {
515                  fprintf(stderr, "%s: unrecognized hemisphere sampling: h=%s\n",
516                                  progname, curparams.hemis);
517                  exit(1);
518          }
519 <        if (!uniform & (curparams.slist->styp == ST_SOURCE)) {
519 >        if (tolower(curparams.hemis[0]) == 'k') {
520 >                sprintf(sbuf, "RHS=%c1", curparams.sign);
521 >                params = savqstr(sbuf);
522 >        }
523 >        if (!uniform) {
524                  SURF    *sp;
525                  for (sp = curparams.slist; sp != NULL; sp = sp->next)
526 <                        if (fabs(sp->area - PI) > 1e-3) {
526 >                        if (sp->styp == ST_SOURCE && fabs(sp->area - PI) > 1e-3) {
527                                  fprintf(stderr, "%s: source '%s' must be 180-degrees\n",
528                                                  progname, sp->sname);
529                                  exit(1);
# Line 507 | Line 567 | make_axes(FVECT uva[2], const FVECT nrm)
567   {
568          int     i;
569  
570 <        uva[1][0] = 0.5 - frandom();
511 <        uva[1][1] = 0.5 - frandom();
512 <        uva[1][2] = 0.5 - frandom();
513 <        for (i = 3; i--; )
514 <                if ((-0.6 < nrm[i]) & (nrm[i] < 0.6))
515 <                        break;
516 <        if (i < 0) {
570 >        if (!getperpendicular(uva[0], nrm, 1)) {
571                  fputs(progname, stderr);
572                  fputs(": bad surface normal in make_axes!\n", stderr);
573                  exit(1);
574          }
575 <        uva[1][i] = 1.0;
522 <        VCROSS(uva[0], uva[1], nrm);
523 <        normalize(uva[0]);
524 <        VCROSS(uva[1], nrm, uva[0]);
575 >        fcross(uva[1], nrm, uva[0]);
576   }
577  
578   /* Illegal sender surfaces end up here */
# Line 630 | Line 681 | ssamp_poly(FVECT orig, SURF *sp, double x)
681                  sp->priv = (void *)ptp;
682          }
683                                          /* pick triangle by partial area */
684 <        for (i = 0; i < ptp->ntris && x > ptp->tri[i].afrac; i++)
684 >        for (i = 0; i < ptp->ntris-1 && x > ptp->tri[i].afrac; i++)
685                  x -= ptp->tri[i].afrac;
686          SDmultiSamp(samp2, 2, x/ptp->tri[i].afrac);
687          samp2[0] *= samp2[1] = sqrt(samp2[1]);
# Line 660 | Line 711 | sample_origin(PARAMS *p, FVECT orig, const FVECT rdir,
711                                          /* special case for lone surface */
712          if (p->nsurfs == 1) {
713                  sp = p->slist;
714 <                if (DOT(sp->snrm, rdir) >= -FTINY) {
714 >                if (DOT(sp->snrm, rdir) >= FTINY) {
715                          fprintf(stderr,
716                                  "%s: internal - sample behind sender '%s'\n",
717                                          progname, sp->sname);
# Line 671 | Line 722 | sample_origin(PARAMS *p, FVECT orig, const FVECT rdir,
722          if (p->nsurfs > nall) {         /* (re)allocate surface area cache */
723                  if (projsa) free(projsa);
724                  projsa = (double *)malloc(sizeof(double)*p->nsurfs);
725 <                if (!projsa) return(0);
725 >                if (projsa == NULL) {
726 >                        fputs(progname, stderr);
727 >                        fputs(": out of memory in sample_origin!\n", stderr);
728 >                        exit(1);
729 >                }
730                  nall = p->nsurfs;
731          }
732                                          /* compute projected areas */
733          for (i = 0, sp = p->slist; sp != NULL; i++, sp = sp->next) {
734                  projsa[i] = -DOT(sp->snrm, rdir) * sp->area;
735 <                tarea += projsa[i] *= (double)(projsa[i] > FTINY);
735 >                tarea += projsa[i] *= (double)(projsa[i] > 0);
736          }
737 <        if (tarea <= FTINY) {           /* wrong side of sender? */
737 >        if (tarea < FTINY*FTINY) {      /* wrong side of sender? */
738                  fputs(progname, stderr);
739                  fputs(": internal - sample behind all sender elements!\n",
740                                  stderr);
# Line 697 | Line 752 | sample_uniform(PARAMS *p, int b, FILE *fp)
752   {
753          int     n = sampcnt;
754          double  samp3[3];
755 <        double  duvw[3];
701 <        FVECT   orig_dir[2];
755 >        FVECT   duvw, orig_dir[2];
756          int     i;
757  
758          if (fp == NULL)                 /* just requesting number of bins? */
# Line 706 | Line 760 | sample_uniform(PARAMS *p, int b, FILE *fp)
760  
761          while (n--) {                   /* stratified hemisphere sampling */
762                  SDmultiSamp(samp3, 3, (n+frandom())/sampcnt);
763 <                SDsquare2disk(duvw, samp3[1], samp3[2]);
763 >                square2disk(duvw, samp3[1], samp3[2]);
764                  duvw[2] = -sqrt(1. - duvw[0]*duvw[0] - duvw[1]*duvw[1]);
765                  for (i = 3; i--; )
766                          orig_dir[1][i] = duvw[0]*p->udir[i] +
# Line 714 | Line 768 | sample_uniform(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 726 | Line 780 | sample_shirchiu(PARAMS *p, int b, FILE *fp)
780   {
781          int     n = sampcnt;
782          double  samp3[3];
783 <        double  duvw[3];
730 <        FVECT   orig_dir[2];
783 >        FVECT   duvw, orig_dir[2];
784          int     i;
785  
786          if (fp == NULL)                 /* just requesting number of bins? */
# Line 735 | Line 788 | sample_shirchiu(PARAMS *p, int b, FILE *fp)
788  
789          while (n--) {                   /* stratified sampling */
790                  SDmultiSamp(samp3, 3, (n+frandom())/sampcnt);
791 <                SDsquare2disk(duvw, (b/p->hsiz + samp3[1])/curparams.hsiz,
791 >                square2disk(duvw, (b/p->hsiz + samp3[1])/curparams.hsiz,
792                                  (b%p->hsiz + samp3[2])/curparams.hsiz);
793                  duvw[2] = sqrt(1. - duvw[0]*duvw[0] - duvw[1]*duvw[1]);
794                  for (i = 3; i--; )
# Line 744 | Line 797 | sample_shirchiu(PARAMS *p, int b, FILE *fp)
797                                                  duvw[2]*p->nrm[i] ;
798                  if (!sample_origin(p, orig_dir[0], orig_dir[1], samp3[0]))
799                          return(0);
800 <                if (fwrite(orig_dir, sizeof(FVECT), 2, fp) != 2)
800 >                if (putbinary(orig_dir, sizeof(FVECT), 2, fp) != 2)
801                          return(0);
802          }
803          return(1);
# Line 780 | Line 833 | sample_reinhart(PARAMS *p, int b, FILE *fp)
833          }
834          while (n--) {                   /* stratified sampling */
835                  SDmultiSamp(samp3, 3, (n+frandom())/sampcnt);
836 +                if (row >= RowMax-1)    /* avoid crowding at zenith */
837 +                        samp3[1] *= samp3[1];
838                  alt = (row+samp3[1])*RAH;
839                  azi = (2.*PI)*(col+samp3[2]-.5)/rnaz(row);
840                  duvw[2] = cos(alt);     /* measured from horizon */
841 <                duvw[0] = tcos(azi)*duvw[2];
842 <                duvw[1] = tsin(azi)*duvw[2];
841 >                duvw[0] = tsin(azi)*duvw[2];
842 >                duvw[1] = -tcos(azi)*duvw[2];
843                  duvw[2] = sqrt(1. - duvw[2]*duvw[2]);
844                  for (i = 3; i--; )
845                          orig_dir[1][i] = -duvw[0]*p->udir[i] -
# Line 792 | Line 847 | sample_reinhart(PARAMS *p, int b, FILE *fp)
847                                                  duvw[2]*p->nrm[i] ;
848                  if (!sample_origin(p, orig_dir[0], orig_dir[1], samp3[0]))
849                          return(0);
850 <                if (fwrite(orig_dir, sizeof(FVECT), 2, fp) != 2)
850 >                if (putbinary(orig_dir, sizeof(FVECT), 2, fp) != 2)
851                          return(0);
852          }
853          return(1);
# Line 835 | Line 890 | sample_klems(PARAMS *p, int b, FILE *fp)
890  
891          while (n--) {                   /* stratified sampling */
892                  SDmultiSamp(samp2, 2, (n+frandom())/sampcnt);
893 <                if (!bo_getvec(duvw, b+samp2[1], kbasis[bi]))
893 >                if (!fo_getvec(duvw, b+samp2[1], kbasis[bi]))
894                          return(0);
895                  for (i = 3; i--; )
896 <                        orig_dir[1][i] = duvw[0]*p->udir[i] +
897 <                                                duvw[1]*p->vdir[i] +
896 >                        orig_dir[1][i] = -duvw[0]*p->udir[i] -
897 >                                                duvw[1]*p->vdir[i] -
898                                                  duvw[2]*p->nrm[i] ;
899                  if (!sample_origin(p, orig_dir[0], orig_dir[1], samp2[0]))
900                          return(0);
901 <                if (fwrite(orig_dir, sizeof(FVECT), 2, fp) != 2)
901 >                if (putbinary(orig_dir, sizeof(FVECT), 2, fp) != 2)
902                          return(0);
903          }
904          return(1);
# Line 851 | Line 906 | sample_klems(PARAMS *p, int b, FILE *fp)
906  
907   /* Prepare hemisphere basis sampler that will send rays to rcontrib */
908   static int
909 < prepare_sampler(void)
909 > prepare_sampler(PARAMS *p)
910   {
911 <        if (curparams.slist == NULL) {  /* missing sample surface! */
912 <                fputs(progname, stderr);
858 <                fputs(": no sender surface!\n", stderr);
911 >        if (p->slist == NULL) {         /* missing sample surface! */
912 >                error(USER, "no sender surface");
913                  return(-1);
914          }
915 <        if (curparams.outfn != NULL)    /* misplaced output file spec. */
916 <                fprintf(stderr, "%s: warning - ignoring output file in sender ('%s')\n",
917 <                                progname, curparams.outfn);
915 >                                        /* misplaced output file spec. */
916 >        if (p->outfn[0]) {
917 >                sprintf(errmsg, "ignoring output file in sender ('%s')",
918 >                                p->outfn);
919 >                error(WARNING, errmsg);
920 >        }
921                                          /* check/set basis hemisphere */
922 <        if (!curparams.hemis[0]) {
923 <                fputs(progname, stderr);
867 <                fputs(": missing sender sampling type!\n", stderr);
922 >        if (!p->hemis[0]) {
923 >                error(USER, "missing sender sampling type");
924                  return(-1);
925          }
926 <        if (normalize(curparams.nrm) == 0) {
927 <                fputs(progname, stderr);
872 <                fputs(": undefined normal for sender sampling\n", stderr);
926 >        if (normalize(p->nrm) == 0) {
927 >                error(USER, "undefined normal for sender sampling");
928                  return(-1);
929          }
930 <        if (normalize(curparams.vup) == 0)
931 <                if (fabs(curparams.nrm[2]) < .7)
932 <                        curparams.vup[2] = 1;
930 >        if (normalize(p->vup) == 0) {
931 >                if (fabs(p->nrm[2]) < .7)
932 >                        p->vup[2] = 1;
933                  else
934 <                        curparams.vup[1] = 1;
935 <        VCROSS(curparams.udir, curparams.vup, curparams.nrm);
936 <        if (normalize(curparams.udir) == 0) {
937 <                fputs(progname, stderr);
938 <                fputs(": up vector coincides with sender normal\n", stderr);
934 >                        p->vup[1] = 1;
935 >        }
936 >        fcross(p->udir, p->vup, p->nrm);
937 >        if (normalize(p->udir) == 0) {
938 >                error(USER, "up vector coincides with sender normal");
939                  return(-1);
940          }
941 <        VCROSS(curparams.vdir, curparams.nrm, curparams.udir);
942 <        if (tolower(curparams.hemis[0]) == 'u' | curparams.hemis[0] == '1')
943 <                curparams.sample_basis = sample_uniform;
944 <        else if (tolower(curparams.hemis[0]) == 's' &&
945 <                                tolower(curparams.hemis[1]) == 'c')
946 <                curparams.sample_basis = sample_shirchiu;
947 <        else if ((tolower(curparams.hemis[0]) == 'r') |
948 <                        (tolower(curparams.hemis[0]) == 't'))
949 <                curparams.sample_basis = sample_reinhart;
950 <        else if (tolower(curparams.hemis[0]) == 'k') {
951 <                switch (curparams.hemis[1]) {
941 >        fcross(p->vdir, p->nrm, p->udir);
942 >        if (p->sign == '-') {           /* left-handed coordinate system? */
943 >                p->udir[0] *= -1.;
944 >                p->udir[1] *= -1.;
945 >                p->udir[2] *= -1.;
946 >        }
947 >        if ((tolower(p->hemis[0]) == 'u') | (p->hemis[0] == '1'))
948 >                p->sample_basis = sample_uniform;
949 >        else if (tolower(p->hemis[0]) == 's' &&
950 >                                tolower(p->hemis[1]) == 'c')
951 >                p->sample_basis = sample_shirchiu;
952 >        else if ((tolower(p->hemis[0]) == 'r') |
953 >                        (tolower(p->hemis[0]) == 't'))
954 >                p->sample_basis = sample_reinhart;
955 >        else if (tolower(p->hemis[0]) == 'k') {
956 >                switch (p->hemis[1]) {
957                  case '1':
958                  case '2':
959                  case '4':
# Line 901 | Line 961 | prepare_sampler(void)
961                  case 'f':
962                  case 'F':
963                  case '\0':
964 <                        curparams.hemis[1] = '1';
964 >                        p->hemis[1] = '1';
965                          break;
966                  case 'h':
967                  case 'H':
968 <                        curparams.hemis[1] = '2';
968 >                        p->hemis[1] = '2';
969                          break;
970                  case 'q':
971                  case 'Q':
972 <                        curparams.hemis[1] = '4';
972 >                        p->hemis[1] = '4';
973                          break;
974                  default:
975                          goto unrecognized;
976                  }
977 <                curparams.hemis[2] = '\0';
978 <                curparams.sample_basis = sample_klems;
977 >                p->hemis[2] = '\0';
978 >                p->sample_basis = sample_klems;
979          } else
980                  goto unrecognized;
981                                          /* return number of bins */
982 <        return((*curparams.sample_basis)(&curparams,0,NULL));
982 >        return((*p->sample_basis)(p,0,NULL));
983   unrecognized:
984          fprintf(stderr, "%s: unrecognized sender sampling: h=%s\n",
985 <                        progname, curparams.hemis);
985 >                        progname, p->hemis);
986          return(-1);
987   }
988  
# Line 945 | Line 1005 | finish_polygon(SURF *p)
1005                  VCOPY(e1, e2);
1006          }
1007          p->area = normalize(p->snrm)*0.5;
1008 <        return(p->area > FTINY);
1008 >        return(p->area > FTINY*FTINY);
1009   }
1010  
1011   /* Add a surface to our current parameters */
# Line 963 | Line 1023 | add_surface(int st, const char *oname, FILE *fp)
1023          snew = (SURF *)malloc(sizeof(SURF) + sizeof(double)*(n-1));
1024          if (snew == NULL) {
1025                  fputs(progname, stderr);
1026 <                fputs(": out of memory!\n", stderr);
1026 >                fputs(": out of memory in add_surface!\n", stderr);
1027                  exit(1);
1028          }
1029          strncpy(snew->sname, oname, sizeof(snew->sname)-1);
# Line 1008 | Line 1068 | add_surface(int st, const char *oname, FILE *fp)
1068                  snew->area *= PI*snew->area;
1069                  break;
1070          }
1071 <        if (snew->area <= FTINY) {
1071 >        if ((snew->area <= FTINY*FTINY) & !(verbose & NOWARN)) {
1072                  fprintf(stderr, "%s: warning - zero area for surface '%s'\n",
1073                                  progname, oname);
1074                  free(snew);
# Line 1045 | Line 1105 | add_recv_object(FILE *fp)
1105          }
1106                                          /* is it a new receiver? */
1107          if ((st = surf_type(otype)) != ST_NONE) {
1048                if (curparams.slist != NULL && (st == ST_SOURCE) ^
1049                                (curparams.slist->styp == ST_SOURCE)) {
1050                        fputs(progname, stderr);
1051                        fputs(": cannot mix source/non-source receivers!\n", stderr);
1052                        return(-1);
1053                }
1108                  if (strcmp(thismod, curmod)) {
1109                          if (curmod[0]) {        /* output last receiver? */
1110                                  finish_receiver();
# Line 1066 | Line 1120 | add_recv_object(FILE *fp)
1120                                          /* else skip arguments */
1121          if (!fscanf(fp, "%d", &n)) return(0);
1122          while (n-- > 0) fscanf(fp, "%*s");
1123 <        if (!fscanf(fp, "%d", &n)) return;
1123 >        if (!fscanf(fp, "%d", &n)) return(0);
1124          while (n-- > 0) fscanf(fp, "%*d");
1125 <        if (!fscanf(fp, "%d", &n)) return;
1125 >        if (!fscanf(fp, "%d", &n)) return(0);
1126          while (n-- > 0) fscanf(fp, "%*f");
1127          return(0);
1128   }
# Line 1078 | Line 1132 | static int
1132   add_send_object(FILE *fp)
1133   {
1134          int             st;
1135 <        char            otype[32], oname[128];
1135 >        char            thismod[128], otype[32], oname[128];
1136          int             n;
1137  
1138 <        if (fscanf(fp, "%*s %s %s", otype, oname) != 2)
1138 >        if (fscanf(fp, "%s %s %s", thismod, otype, oname) != 3)
1139                  return(0);              /* must have hit EOF! */
1140          if (!strcmp(otype, "alias")) {
1141                  fscanf(fp, "%*s");      /* skip alias */
# Line 1094 | Line 1148 | add_send_object(FILE *fp)
1148                          fputs(": cannot use source as a sender!\n", stderr);
1149                          return(-1);
1150                  }
1151 +                if (strcmp(thismod, curmod)) {
1152 +                        if (curmod[0]) {
1153 +                                fputs(progname, stderr);
1154 +                                fputs(": warning - multiple modifiers in sender\n",
1155 +                                                stderr);
1156 +                        }
1157 +                        strcpy(curmod, thismod);
1158 +                }
1159                  parse_params(&curparams, newparams);
1160                  newparams[0] = '\0';
1161                  add_surface(st, oname, fp);     /* read & store surface */
# Line 1102 | Line 1164 | add_send_object(FILE *fp)
1164                                          /* else skip arguments */
1165          if (!fscanf(fp, "%d", &n)) return(0);
1166          while (n-- > 0) fscanf(fp, "%*s");
1167 <        if (!fscanf(fp, "%d", &n)) return;
1167 >        if (!fscanf(fp, "%d", &n)) return(0);
1168          while (n-- > 0) fscanf(fp, "%*d");
1169 <        if (!fscanf(fp, "%d", &n)) return;
1169 >        if (!fscanf(fp, "%d", &n)) return(0);
1170          while (n-- > 0) fscanf(fp, "%*f");
1171          return(0);
1172   }
# Line 1147 | Line 1209 | load_scene(const char *inspec, int (*ocb)(FILE *))
1209                                          strcat(newparams, inpbuf);
1210                                  continue;
1211                          }
1212 <                        while ((c = getc(fp)) != EOF && c != '\n');
1212 >                        while ((c = getc(fp)) != EOF && c != '\n')
1213                                  ;       /* else skipping comment */
1214                          continue;
1215                  }
# Line 1172 | Line 1234 | main(int argc, char *argv[])
1234   {
1235          char    fmtopt[6] = "-faa";     /* default output is ASCII */
1236          char    *xrs=NULL, *yrs=NULL, *ldopt=NULL;
1237 <        int     wantIrradiance = 0;
1237 >        char    *iropt = NULL;
1238          char    *sendfn;
1239          char    sampcntbuf[32], nsbinbuf[32];
1240          FILE    *rcfp;
1241          int     nsbins;
1242          int     a, i;
1243 +                                        /* set global progname */
1244 +        fixargv0(argv[0]);
1245                                          /* screen rcontrib options */
1246 <        progname = argv[0];
1247 <        for (a = 1; a < argc-2 && argv[a][0] == '-'; a++) {
1248 <                int     na = 1;         /* !! Keep consistent !! */
1249 <                switch (argv[a][1]) {
1246 >        for (a = 1; a < argc-2; a++) {
1247 >                int     na;
1248 >                                        /* check for argument expansion */
1249 >                while ((na = expandarg(&argc, &argv, a)) > 0)
1250 >                        ;
1251 >                if (na < 0) {
1252 >                        fprintf(stderr, "%s: cannot expand '%s'\n",
1253 >                                        progname, argv[a]);
1254 >                        return(1);
1255 >                }
1256 >                if (argv[a][0] != '-' || !argv[a][1])
1257 >                        break;
1258 >                na = 1;
1259 >                switch (argv[a][1]) {   /* !! Keep consistent !! */
1260                  case 'v':               /* verbose mode */
1261 <                        verbose = !verbose;
1261 >                        verbose ^= VERBO;
1262                          na = 0;
1263                          continue;
1264                  case 'f':               /* special case for -fo, -ff, etc. */
# Line 1214 | Line 1288 | main(int argc, char *argv[])
1288                          yrs = argv[++a];
1289                          na = 0;
1290                          continue;
1291 <                case 'c':               /* number of samples */
1292 <                        sampcnt = atoi(argv[++a]);
1293 <                        if (sampcnt <= 0)
1294 <                                goto userr;
1295 <                        na = 0;         /* we re-add this later */
1296 <                        continue;
1291 >                case 'c':               /* spectral sampling or count */
1292 >                        switch (argv[a][2]) {
1293 >                        case 's':
1294 >                                na = 2;
1295 >                                break;
1296 >                        case 'w':
1297 >                                na = 3;
1298 >                                break;
1299 >                        case '\0':
1300 >                                sampcnt = atoi(argv[++a]);
1301 >                                if (sampcnt <= 0)
1302 >                                        goto userr;
1303 >                                na = 0;         /* we re-add this later */
1304 >                                continue;
1305 >                        }
1306 >                        break;
1307                  case 'I':               /* only for pass-through mode */
1308 <                        wantIrradiance = 1;
1308 >                case 'i':
1309 >                        iropt = argv[a];
1310                          na = 0;
1311                          continue;
1312 <                case 'V':               /* options without arguments */
1313 <                case 'w':
1312 >                case 'w':               /* options without arguments */
1313 >                        if (!argv[a][2])
1314 >                                verbose ^= NOWARN;
1315 >                        else if (strchr("+1tTyY", argv[a][2]) != NULL)
1316 >                                verbose &= ~NOWARN;
1317 >                        else
1318 >                                verbose |= NOWARN;
1319 >                        break;
1320 >                case 'V':
1321                  case 'u':
1230                case 'i':
1322                  case 'h':
1323                  case 'r':
1324                          break;
1325                  case 'n':               /* options with 1 argument */
1326                  case 's':
1327                  case 'o':
1328 +                case 't':
1329                          na = 2;
1330                          break;
1331                  case 'b':               /* special case */
# Line 1251 | Line 1343 | main(int argc, char *argv[])
1343                          if (argv[a][2] != 'v') na = 2;
1344                          break;
1345                  case 'a':               /* special case */
1346 <                        na = (argv[a][2] == 'v') ? 4 : 2;
1346 >                        if (argv[a][2] == 'p') {
1347 >                                na = 2; /* photon map [+ bandwidth(s)] */
1348 >                                if (a < argc-3 && atoi(argv[a+1]) > 0)
1349 >                                        na += 1 + (a < argc-4 && atoi(argv[a+2]) > 0);
1350 >                        } else
1351 >                                na = (argv[a][2] == 'v') ? 4 : 2;
1352                          break;
1353                  case 'm':               /* special case */
1354                          if (!argv[a][2]) goto userr;
1355                          na = (argv[a][2] == 'e') | (argv[a][2] == 'a') ? 4 : 2;
1356                          break;
1260                case '\0':              /* pass-through mode */
1261                        goto done_opts;
1357                  default:                /* anything else is verbotten */
1358                          goto userr;
1359                  }
# Line 1268 | Line 1363 | main(int argc, char *argv[])
1363                  while (--na)            /* + arguments if any */
1364                          rcarg[nrcargs++] = argv[++a];
1365          }
1271 done_opts:
1366          if (a > argc-2)
1367                  goto userr;             /* check at end of options */
1368          sendfn = argv[a++];             /* assign sender & receiver inputs */
1369          if (sendfn[0] == '-') {         /* user wants pass-through mode? */
1370                  if (sendfn[1]) goto userr;
1371                  sendfn = NULL;
1372 <                if (wantIrradiance) {
1372 >                if (iropt) {
1373                          CHECKARGC(1);
1374 <                        rcarg[nrcargs++] = "-I";
1374 >                        rcarg[nrcargs++] = iropt;
1375                  }
1376                  if (xrs) {
1377                          CHECKARGC(2);
# Line 1295 | Line 1389 | done_opts:
1389                  }
1390                  if (sampcnt <= 0) sampcnt = 1;
1391          } else {                        /* else in sampling mode */
1392 <                if (wantIrradiance) {
1392 >                if (iropt) {
1393                          fputs(progname, stderr);
1394 <                        fputs(": -I supported for pass-through only\n", stderr);
1394 >                        fputs(": -i, -I supported for pass-through only\n", stderr);
1395                          return(1);
1396                  }
1397 <                fmtopt[2] = (sizeof(RREAL)==sizeof(double)) ? 'd' : 'f';
1397 > #ifdef SMLFLT
1398 >                fmtopt[2] = 'f';
1399 > #else
1400 >                fmtopt[2] = 'd';
1401 > #endif
1402                  if (sampcnt <= 0) sampcnt = 10000;
1403          }
1404          sprintf(sampcntbuf, "%d", sampcnt);
# Line 1319 | Line 1417 | done_opts:
1417                  return(my_exec(rcarg)); /* rcontrib does everything */
1418          }
1419          clear_params(&curparams, 0);    /* else load sender surface & params */
1420 +        curmod[0] = '\0';
1421          if (load_scene(sendfn, add_send_object) < 0)
1422                  return(1);
1423 <        if ((nsbins = prepare_sampler()) <= 0)
1423 >        if ((nsbins = prepare_sampler(&curparams)) <= 0)
1424                  return(1);
1425          CHECKARGC(3);                   /* add row count and octree */
1426          rcarg[nrcargs++] = "-y";
# Line 1336 | Line 1435 | done_opts:
1435   #ifdef getc_unlocked
1436          flockfile(rcfp);
1437   #endif
1438 <        if (verbose) {
1438 >        if (verbose & VERBO) {
1439                  fprintf(stderr, "%s: sampling %d directions", progname, nsbins);
1440                  if (curparams.nsurfs > 1)
1441                          fprintf(stderr, " (%d elements)\n", curparams.nsurfs);
# Line 1346 | Line 1445 | done_opts:
1445          for (i = 0; i < nsbins; i++)    /* send rcontrib ray samples */
1446                  if (!(*curparams.sample_basis)(&curparams, i, rcfp))
1447                          return(1);
1448 <        return(pclose(rcfp) == 0);      /* all finished! */
1448 >        return(pclose_al(rcfp) < 0);    /* all finished! */
1449   userr:
1450          if (a < argc-2)
1451                  fprintf(stderr, "%s: unsupported option '%s'", progname, argv[a]);
1452 <        fprintf(stderr, "Usage: %s [-v][rcontrib options] sender.rad receiver.rad [system.rad ..]\n",
1452 >        fprintf(stderr, "Usage: %s [-v][rcontrib options] sender.rad receiver.rad [-i system.oct] [system.rad ..]\n",
1453                                  progname);
1454          return(1);
1455   }

Diff Legend

Removed lines
+ Added lines
< Changed lines (old)
> Changed lines (new)