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.57 by greg, Tue Mar 19 10:48:05 2024 UTC

# Line 11 | Line 11 | static const char RCSid[] = "$Id$";
11   #include <stdlib.h>
12   #include "rtio.h"
13   #include "rtmath.h"
14 + #include "rtprocess.h"
15   #include "bsdf.h"
16   #include "bsdf_m.h"
17   #include "random.h"
18   #include "triangulate.h"
19   #include "platform.h"
20  
21 < #ifdef getc_unlocked            /* avoid horrendous overhead of flockfile */
22 < #undef getc
22 < #define getc    getc_unlocked
21 > #ifndef MAXRCARG
22 > #define MAXRCARG        10000
23   #endif
24  
25 #ifdef _WIN32
26 #define SPECIALS        " \t\"$*?"
27 #define QUOTCHAR        '"'
28 #else
29 #define SPECIALS        " \t\n'\"()${}*?[];|&"
30 #define QUOTCHAR        '\''
31 #define ALTQUOT         '"'
32 #endif
33
34 #define MAXRCARG        512
35
25   char            *progname;              /* global argv[0] */
26  
27 < int             verbose = 0;            /* verbose mode? */
27 > int             verbose = 0;            /* verbose mode (< 0 no warnings) */
28  
29   char            *rcarg[MAXRCARG+1] = {"rcontrib", "-fo+"};
30   int             nrcargs = 2;
# Line 52 | Line 41 | char           *shirchiufn = "disk2square.cal";
41   char            *kfullfn = "klems_full.cal";
42   char            *khalffn = "klems_half.cal";
43   char            *kquarterfn = "klems_quarter.cal";
44 + char            *ciefn = "cieskyscan.cal";
45  
46                                          /* string indicating parameters */
47   const char      PARAMSTART[] = "@rfluxmtx";
# Line 77 | Line 67 | typedef struct {
67          FVECT   uva[2];                 /* tangent axes */
68          int     ntris;                  /* number of triangles */
69          struct ptri {
70 <                float   afrac;                  /* fraction of total area */
70 >                double  afrac;                  /* fraction of total area */
71                  short   vndx[3];                /* vertex indices */
72          }       tri[1];                 /* triangle array (extends struct) */
73   } POLYTRIS;                     /* triangulated polygon */
74  
75   typedef struct param_s {
76 <        char            hemis[32];      /* hemispherical sampling spec. */
76 >        char            sign;           /* '-' for axis reversal */
77 >        char            hemis[31];      /* hemispherical sampling spec. */
78          int             hsiz;           /* hemisphere basis size */
79          int             nsurfs;         /* number of surfaces */
80          SURF            *slist;         /* list of surfaces */
81          FVECT           vup;            /* up vector (zero if unset) */
82          FVECT           nrm;            /* average normal direction */
83 <        FVECT           udir, vdir;     /* v-up tangent axes */
83 >        FVECT           udir, vdir;     /* tangent axes */
84          char            *outfn;         /* output file name (receiver) */
85          int             (*sample_basis)(struct param_s *p, int, FILE *);
86   } PARAMS;                       /* sender/receiver parameters */
87  
88   PARAMS          curparams;
89   char            curmod[128];
90 + char            newparams[1024];
91  
92   typedef int     SURFSAMP(FVECT, SURF *, double);
93  
# Line 109 | Line 101 | SURFSAMP       *orig_in_surf[4] = {
101   static void
102   clear_params(PARAMS *p, int reset_only)
103   {
112        curmod[0] = '\0';
104          while (p->slist != NULL) {
105                  SURF    *sdel = p->slist;
106                  p->slist = sdel->next;
# Line 124 | Line 115 | clear_params(PARAMS *p, int reset_only)
115                  p->outfn = NULL;
116                  return;
117          }
118 <        memset(p, 0, sizeof(curparams));
118 >        memset(p, 0, sizeof(PARAMS));
119   }
120  
121   /* Get surface type from name */
# Line 144 | Line 135 | surf_type(const char *otype)
135   static char *
136   oconv_command(int ac, char *av[])
137   {
138 <        static char     oconvbuf[2048] = "!oconv -f";
139 <        char            *cp = oconvbuf + 9;
140 <
141 <        while (ac-- > 0) {
142 <                if (cp >= oconvbuf+(sizeof(oconvbuf)-32)) {
143 <                        fputs(progname, stderr);
144 <                        fputs(": too many file arguments!\n", stderr);
145 <                        exit(1);
146 <                }
156 <                *cp++ = ' ';
157 <                strcpy(cp, *av++);
158 <                while (*cp) cp++;
138 >        static char     oconvbuf[2048] = "!oconv -f ";
139 >        char            *cp = oconvbuf + 10;
140 >        char            *recv = *av++;
141 >        
142 >        if (ac-- <= 0)
143 >                return(NULL);
144 >        if (verbose < 0) {      /* turn off warnings */
145 >                strcpy(cp, "-w ");
146 >                cp += 3;
147          }
148 <        *cp = '\0';
149 <        return(oconvbuf);
150 < }
151 <
152 < /* Check if any of the characters in str2 are found in str1 */
153 < static int
154 < matchany(const char *str1, const char *str2)
155 < {
156 <        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;
148 >        while (ac-- > 0) {      /* copy each argument */
149 >                int     len = strlen(*av);
150 >                if (cp+len+4 >= oconvbuf+sizeof(oconvbuf))
151 >                        goto overrun;
152 >                if (matchany(*av, SPECIALS)) {
153 >                        *cp++ = QUOTCHAR;
154 >                        strcpy(cp, *av++);
155 >                        cp += len;
156 >                        *cp++ = QUOTCHAR;
157                  } else {
158 <                        strcpy(cp, *av);
159 <                        cp += n;
158 >                        strcpy(cp, *av++);
159 >                        cp += len;
160                  }
161                  *cp++ = ' ';
162          }
163 <        if (cp <= cmd)
164 <                return(NULL);
165 <        *--cp = '\0';
166 <        return(cmd);
163 >                                /* receiver goes last */
164 >        if (matchany(recv, SPECIALS)) {
165 >                *cp++ = QUOTCHAR;
166 >                while (*recv) {
167 >                        if (cp >= oconvbuf+(sizeof(oconvbuf)-3))
168 >                                goto overrun;
169 >                        *cp++ = *recv++;
170 >                }
171 >                *cp++ = QUOTCHAR;
172 >                *cp = '\0';
173 >        } else
174 >                strcpy(cp, recv);
175 >        return(oconvbuf);
176 > overrun:
177 >        fputs(progname, stderr);
178 >        fputs(": too many file arguments!\n", stderr);
179 >        exit(1);
180   }
181  
182 + #if defined(_WIN32) || defined(_WIN64)
183 +
184   /* Open a pipe to/from a command given as an argument list */
185   static FILE *
186   popen_arglist(char *av[], char *mode)
# Line 222 | Line 192 | popen_arglist(char *av[], char *mode)
192                  fputs(": command line too long in popen_arglist()\n", stderr);
193                  return(NULL);
194          }
195 <        if (verbose)
195 >        if (verbose > 0)
196                  fprintf(stderr, "%s: opening pipe %s: %s\n",
197                                  progname, (*mode=='w') ? "to" : "from", cmd);
198          return(popen(cmd, mode));
199   }
200  
201 < #ifdef _WIN32
201 > #define pclose_al       pclose
202 >
203   /* Execute system command (Windows version) */
204   static int
205   my_exec(char *av[])
# Line 240 | Line 211 | my_exec(char *av[])
211                  fputs(": command line too long in my_exec()\n", stderr);
212                  return(1);
213          }
214 <        if (verbose)
214 >        if (verbose > 0)
215                  fprintf(stderr, "%s: running: %s\n", progname, cmd);
216          return(system(cmd));
217   }
218 < #else
218 >
219 > #else   /* UNIX */
220 >
221 > static SUBPROC  rt_proc = SP_INACTIVE;  /* we only support one of these */
222 >
223 > /* Open a pipe to a command using an argument list */
224 > static FILE *
225 > popen_arglist(char *av[], char *mode)
226 > {
227 >        int     fd;
228 >
229 >        if (rt_proc.pid > 0) {
230 >                fprintf(stderr, "%s: only one i/o pipe at a time!\n", progname);
231 >                return(NULL);
232 >        }
233 >        if (verbose > 0) {
234 >                char    cmd[4096];
235 >                if (!convert_commandline(cmd, sizeof(cmd), av))
236 >                        strcpy(cmd, "COMMAND TOO LONG TO SHOW");
237 >                fprintf(stderr, "%s: opening pipe %s: %s\n",
238 >                                progname, (*mode=='w') ? "to" : "from", cmd);
239 >        }
240 >        if (*mode == 'w') {
241 >                fd = rt_proc.w = dup(fileno(stdout));
242 >                rt_proc.flags |= PF_FILT_OUT;
243 >        } else if (*mode == 'r') {
244 >                fd = rt_proc.r = dup(fileno(stdin));
245 >                rt_proc.flags |= PF_FILT_INP;
246 >        }
247 >        if (fd < 0 || open_process(&rt_proc, av) <= 0) {
248 >                perror(av[0]);
249 >                return(NULL);
250 >        }
251 >        return(fdopen(fd, mode));
252 > }
253 >
254 > /* Close command pipe (returns -1 on error to match pclose) */
255 > static int
256 > pclose_al(FILE *fp)
257 > {
258 >        int     prob = (fclose(fp) == EOF);
259 >
260 >        if (rt_proc.pid <= 0)
261 >                return(-1);
262 >
263 >        prob |= (close_process(&rt_proc) != 0);
264 >
265 >        return(-prob);
266 > }
267 >
268   /* Execute system command in our stead (Unix version) */
269   static int
270   my_exec(char *av[])
# Line 255 | Line 275 | my_exec(char *av[])
275                  fprintf(stderr, "%s: cannot locate %s\n", progname, av[0]);
276                  return(1);
277          }
278 <        if (verbose) {
278 >        if (verbose > 0) {
279                  char    cmd[4096];
280                  if (!convert_commandline(cmd, sizeof(cmd), av))
281                          strcpy(cmd, "COMMAND TOO LONG TO SHOW");
# Line 265 | Line 285 | my_exec(char *av[])
285          perror(compath);
286          return(1);
287   }
288 +
289   #endif
290  
291   /* Get normalized direction vector from string specification */
# Line 309 | Line 330 | nextchar:
330  
331   /* Parse program parameters (directives) */
332   static int
333 < parse_params(char *pargs)
333 > parse_params(PARAMS *p, char *pargs)
334   {
335          char    *cp = pargs;
336          int     nparams = 0;
337 +        int     quot;
338          int     i;
339  
340 <        for ( ; ; )
340 >        for ( ; ; ) {
341                  switch (*cp++) {
342                  case 'h':
343                          if (*cp++ != '=')
344                                  break;
345 <                        curparams.hsiz = 0;
345 >                        if ((*cp == '+') | (*cp == '-'))
346 >                                p->sign = *cp++;
347 >                        else
348 >                                p->sign = '+';
349 >                        p->hsiz = 0;
350                          i = 0;
351                          while (*cp && !isspace(*cp)) {
352                                  if (isdigit(*cp))
353 <                                        curparams.hsiz = 10*curparams.hsiz +
354 <                                                                *cp - '0';
329 <                                curparams.hemis[i++] = *cp++;
353 >                                        p->hsiz = 10*p->hsiz + *cp - '0';
354 >                                p->hemis[i++] = *cp++;
355                          }
356                          if (!i)
357                                  break;
358 <                        curparams.hemis[i] = '\0';
359 <                        curparams.hsiz += !curparams.hsiz;
358 >                        p->hemis[i] = '\0';
359 >                        p->hsiz += !p->hsiz;
360                          ++nparams;
361                          continue;
362                  case 'u':
363                          if (*cp++ != '=')
364                                  break;
365 <                        if (!get_direction(curparams.vup, cp))
365 >                        if (!get_direction(p->vup, cp))
366                                  break;
367 +                        while (*cp && !isspace(*cp++))
368 +                                ;
369                          ++nparams;
370                          continue;
371                  case 'o':
372                          if (*cp++ != '=')
373                                  break;
374 +                        quot = 0;
375 +                        if ((*cp == '"') | (*cp == '\''))
376 +                                quot = *cp++;
377                          i = 0;
378 <                        while (*cp && !isspace(*cp++))
379 <                                i++;
378 >                        while (*cp && (quot ? (*cp != quot) : !isspace(*cp))) {
379 >                                i++; cp++;
380 >                        }
381                          if (!i)
382                                  break;
383 <                        *--cp = '\0';
384 <                        curparams.outfn = savqstr(cp-i);
385 <                        *cp++ = ' ';
383 >                        if (!*cp) {
384 >                                if (quot)
385 >                                        break;
386 >                                cp[1] = '\0';
387 >                        }
388 >                        *cp = '\0';
389 >                        p->outfn = savqstr(cp-i);
390 >                        *cp++ = quot ? quot : ' ';
391                          ++nparams;
392                          continue;
393                  case ' ':
# Line 364 | Line 400 | parse_params(char *pargs)
400                  default:
401                          break;
402                  }
403 <        fprintf(stderr, "%s: bad parameter string '%s'\n", progname, pargs);
403 >                break;
404 >        }
405 >        fprintf(stderr, "%s: bad parameter string: %s", progname, pargs);
406          exit(1);
407          return(-1);     /* pro forma return */
408   }
# Line 402 | Line 440 | finish_receiver(void)
440                  fputs(": undefined normal for hemisphere sampling\n", stderr);
441                  exit(1);
442          }
443 <        if (normalize(curparams.vup) == 0)
443 >        if (normalize(curparams.vup) == 0) {
444                  if (fabs(curparams.nrm[2]) < .7)
445                          curparams.vup[2] = 1;
446                  else
447                          curparams.vup[1] = 1;
448 +        }
449                                          /* determine sample type/bin */
450 <        if (tolower(curparams.hemis[0]) == 'u' | curparams.hemis[0] == '1') {
451 <                binv = "0";             /* uniform sampling -- one bin */
450 >        if ((tolower(curparams.hemis[0]) == 'u') | (curparams.hemis[0] == '1')) {
451 >                sprintf(sbuf, "if(-Dx*%g-Dy*%g-Dz*%g,0,-1)",
452 >                        curparams.nrm[0], curparams.nrm[1], curparams.nrm[2]);
453 >                binv = savqstr(sbuf);
454 >                nbins = "1";            /* uniform sampling -- one bin */
455                  uniform = 1;
456          } else if (tolower(curparams.hemis[0]) == 's' &&
457                                  tolower(curparams.hemis[1]) == 'c') {
# Line 420 | Line 462 | finish_receiver(void)
462                          exit(1);
463                  }
464                  calfn = shirchiufn; shirchiufn = NULL;
465 <                sprintf(sbuf, "SCdim=%d,Nx=%g,Ny=%g,Nz=%g,Ux=%g,Uy=%g,Uz=%g",
465 >                sprintf(sbuf, "SCdim=%d,rNx=%g,rNy=%g,rNz=%g,Ux=%g,Uy=%g,Uz=%g,RHS=%c1",
466                                  curparams.hsiz,
467                          curparams.nrm[0], curparams.nrm[1], curparams.nrm[2],
468 <                        curparams.vup[0], curparams.vup[1], curparams.vup[2]);
468 >                        curparams.vup[0], curparams.vup[1], curparams.vup[2],
469 >                        curparams.sign);
470                  params = savqstr(sbuf);
471                  binv = "scbin";
472                  nbins = "SCdim*SCdim";
473          } else if ((tolower(curparams.hemis[0]) == 'r') |
474                          (tolower(curparams.hemis[0]) == 't')) {
475                  calfn = reinhfn; reinhfn = NULL;
476 <                sprintf(sbuf, "MF=%d,Nx=%g,Ny=%g,Nz=%g,Ux=%g,Uy=%g,Uz=%g",
476 >                sprintf(sbuf, "MF=%d,rNx=%g,rNy=%g,rNz=%g,Ux=%g,Uy=%g,Uz=%g,RHS=%c1",
477                                  curparams.hsiz,
478                          curparams.nrm[0], curparams.nrm[1], curparams.nrm[2],
479 <                        curparams.vup[0], curparams.vup[1], curparams.vup[2]);
479 >                        curparams.vup[0], curparams.vup[1], curparams.vup[2],
480 >                        curparams.sign);
481                  params = savqstr(sbuf);
482                  binv = "rbin";
483                  nbins = "Nrbins";
# Line 456 | Line 500 | finish_receiver(void)
500                  calfn = kquarterfn; kquarterfn = NULL;
501                  binf = "kqbin";
502                  nbins = "Nkqbins";
503 +        } else if (!strcasecmp(curparams.hemis, "cie")) {
504 +                calfn = ciefn; ciefn = NULL;
505 +                sprintf(sbuf, "rNx=%g,rNy=%g,rNz=%g,Ux=%g,Uy=%g,Uz=%g,RHS=%c1",
506 +                        curparams.nrm[0], curparams.nrm[1], curparams.nrm[2],
507 +                        curparams.vup[0], curparams.vup[1], curparams.vup[2],
508 +                        curparams.sign);
509 +                binv = "cbin";
510 +                nbins = "Ncbins";
511          } else {
512                  fprintf(stderr, "%s: unrecognized hemisphere sampling: h=%s\n",
513                                  progname, curparams.hemis);
514                  exit(1);
515          }
516 +        if (tolower(curparams.hemis[0]) == 'k') {
517 +                sprintf(sbuf, "RHS=%c1", curparams.sign);
518 +                params = savqstr(sbuf);
519 +        }
520          if (!uniform & (curparams.slist->styp == ST_SOURCE)) {
521                  SURF    *sp;
522                  for (sp = curparams.slist; sp != NULL; sp = sp->next)
# Line 508 | Line 564 | make_axes(FVECT uva[2], const FVECT nrm)
564   {
565          int     i;
566  
567 <        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) {
567 >        if (!getperpendicular(uva[0], nrm, 1)) {
568                  fputs(progname, stderr);
569                  fputs(": bad surface normal in make_axes!\n", stderr);
570                  exit(1);
571          }
572 <        uva[1][i] = 1.0;
523 <        VCROSS(uva[0], uva[1], nrm);
524 <        normalize(uva[0]);
525 <        VCROSS(uva[1], nrm, uva[0]);
572 >        fcross(uva[1], nrm, uva[0]);
573   }
574  
575   /* Illegal sender surfaces end up here */
# Line 554 | Line 601 | ssamp_ring(FVECT orig, SURF *sp, double x)
601                  sp->priv = (void *)uva;
602          }
603          SDmultiSamp(samp2, 2, x);
604 <        samp2[0] = sp->farg[6] + sqrt(samp2[0]*sp->area*(1./PI));
604 >        samp2[0] = sqrt(samp2[0]*sp->area*(1./PI) + sp->farg[6]*sp->farg[6]);
605          samp2[1] *= 2.*PI;
606          uv[0] = samp2[0]*tcos(samp2[1]);
607          uv[1] = samp2[0]*tsin(samp2[1]);
# Line 607 | Line 654 | ssamp_poly(FVECT orig, SURF *sp, double x)
654                          }
655                          ptp->ntris = 0;
656                          v2l->p = (void *)ptp;
657 <                        if (!polyTriangulate(v2l, add_triangle))
657 >                        if (!polyTriangulate(v2l, add_triangle)) {
658 >                                fprintf(stderr,
659 >                                        "%s: cannot triangulate polygon '%s'\n",
660 >                                                progname, sp->sname);
661                                  return(0);
662 +                        }
663                          for (i = ptp->ntris; i--; ) {
664                                  int     a = ptp->tri[i].vndx[0];
665                                  int     b = ptp->tri[i].vndx[1];
# Line 627 | Line 678 | ssamp_poly(FVECT orig, SURF *sp, double x)
678                  sp->priv = (void *)ptp;
679          }
680                                          /* pick triangle by partial area */
681 <        for (i = 0; i < ptp->ntris && x > ptp->tri[i].afrac; i++)
681 >        for (i = 0; i < ptp->ntris-1 && x > ptp->tri[i].afrac; i++)
682                  x -= ptp->tri[i].afrac;
683          SDmultiSamp(samp2, 2, x/ptp->tri[i].afrac);
684          samp2[0] *= samp2[1] = sqrt(samp2[1]);
# Line 657 | Line 708 | sample_origin(PARAMS *p, FVECT orig, const FVECT rdir,
708                                          /* special case for lone surface */
709          if (p->nsurfs == 1) {
710                  sp = p->slist;
711 <                if (DOT(sp->snrm, rdir) >= -FTINY)
712 <                        return(0);      /* behind surface! */
711 >                if (DOT(sp->snrm, rdir) >= FTINY) {
712 >                        fprintf(stderr,
713 >                                "%s: internal - sample behind sender '%s'\n",
714 >                                        progname, sp->sname);
715 >                        return(0);
716 >                }
717                  return((*orig_in_surf[sp->styp])(orig, sp, x));
718          }
719          if (p->nsurfs > nall) {         /* (re)allocate surface area cache */
720                  if (projsa) free(projsa);
721                  projsa = (double *)malloc(sizeof(double)*p->nsurfs);
722 <                if (!projsa) return(0);
722 >                if (projsa == NULL) {
723 >                        fputs(progname, stderr);
724 >                        fputs(": out of memory in sample_origin!\n", stderr);
725 >                        exit(1);
726 >                }
727                  nall = p->nsurfs;
728          }
729                                          /* compute projected areas */
730          for (i = 0, sp = p->slist; sp != NULL; i++, sp = sp->next) {
731                  projsa[i] = -DOT(sp->snrm, rdir) * sp->area;
732 <                tarea += projsa[i] *= (double)(projsa[i] > FTINY);
732 >                tarea += projsa[i] *= (double)(projsa[i] > 0);
733          }
734 <        if (tarea <= FTINY)             /* wrong side of sender? */
734 >        if (tarea < FTINY*FTINY) {      /* wrong side of sender? */
735 >                fputs(progname, stderr);
736 >                fputs(": internal - sample behind all sender elements!\n",
737 >                                stderr);
738                  return(0);
739 +        }
740          tarea *= x;                     /* get surface from list */
741          for (i = 0, sp = p->slist; tarea > projsa[i]; sp = sp->next)
742                  tarea -= projsa[i++];
# Line 686 | Line 749 | sample_uniform(PARAMS *p, int b, FILE *fp)
749   {
750          int     n = sampcnt;
751          double  samp3[3];
752 <        double  duvw[3];
690 <        FVECT   orig_dir[2];
752 >        FVECT   duvw, orig_dir[2];
753          int     i;
754  
755          if (fp == NULL)                 /* just requesting number of bins? */
# Line 695 | Line 757 | sample_uniform(PARAMS *p, int b, FILE *fp)
757  
758          while (n--) {                   /* stratified hemisphere sampling */
759                  SDmultiSamp(samp3, 3, (n+frandom())/sampcnt);
760 <                SDsquare2disk(duvw, samp3[1], samp3[2]);
760 >                square2disk(duvw, samp3[1], samp3[2]);
761                  duvw[2] = -sqrt(1. - duvw[0]*duvw[0] - duvw[1]*duvw[1]);
762                  for (i = 3; i--; )
763                          orig_dir[1][i] = duvw[0]*p->udir[i] +
# Line 703 | Line 765 | sample_uniform(PARAMS *p, int b, FILE *fp)
765                                                  duvw[2]*p->nrm[i] ;
766                  if (!sample_origin(p, orig_dir[0], orig_dir[1], samp3[0]))
767                          return(0);
768 <                if (fwrite(orig_dir, sizeof(FVECT), 2, fp) != 2)
768 >                if (putbinary(orig_dir, sizeof(FVECT), 2, fp) != 2)
769                          return(0);
770          }
771          return(1);
# Line 715 | Line 777 | sample_shirchiu(PARAMS *p, int b, FILE *fp)
777   {
778          int     n = sampcnt;
779          double  samp3[3];
780 <        double  duvw[3];
719 <        FVECT   orig_dir[2];
780 >        FVECT   duvw, orig_dir[2];
781          int     i;
782  
783          if (fp == NULL)                 /* just requesting number of bins? */
# Line 724 | Line 785 | sample_shirchiu(PARAMS *p, int b, FILE *fp)
785  
786          while (n--) {                   /* stratified sampling */
787                  SDmultiSamp(samp3, 3, (n+frandom())/sampcnt);
788 <                SDsquare2disk(duvw, (b/p->hsiz + samp3[1])/curparams.hsiz,
788 >                square2disk(duvw, (b/p->hsiz + samp3[1])/curparams.hsiz,
789                                  (b%p->hsiz + samp3[2])/curparams.hsiz);
790                  duvw[2] = sqrt(1. - duvw[0]*duvw[0] - duvw[1]*duvw[1]);
791                  for (i = 3; i--; )
# Line 733 | Line 794 | sample_shirchiu(PARAMS *p, int b, FILE *fp)
794                                                  duvw[2]*p->nrm[i] ;
795                  if (!sample_origin(p, orig_dir[0], orig_dir[1], samp3[0]))
796                          return(0);
797 <                if (fwrite(orig_dir, sizeof(FVECT), 2, fp) != 2)
797 >                if (putbinary(orig_dir, sizeof(FVECT), 2, fp) != 2)
798                          return(0);
799          }
800          return(1);
# Line 746 | Line 807 | sample_reinhart(PARAMS *p, int b, FILE *fp)
807   #define T_NALT  7
808          static const int        tnaz[T_NALT] = {30, 30, 24, 24, 18, 12, 6};
809          const int               RowMax = T_NALT*p->hsiz + 1;
810 <        const double            RAH = (.25*PI)/(RowMax-.5);
810 >        const double            RAH = (.5*PI)/(RowMax-.5);
811   #define rnaz(r)                 (r >= RowMax-1 ? 1 : p->hsiz*tnaz[r/p->hsiz])
812          int                     n = sampcnt;
813          int                     row, col;
# Line 769 | Line 830 | sample_reinhart(PARAMS *p, int b, FILE *fp)
830          }
831          while (n--) {                   /* stratified sampling */
832                  SDmultiSamp(samp3, 3, (n+frandom())/sampcnt);
833 +                if (row >= RowMax-1)    /* avoid crowding at zenith */
834 +                        samp3[1] *= samp3[1];
835                  alt = (row+samp3[1])*RAH;
836                  azi = (2.*PI)*(col+samp3[2]-.5)/rnaz(row);
837 <                duvw[2] = tcos(alt);    /* measured from horizon */
838 <                duvw[0] = tcos(azi)*duvw[2];
839 <                duvw[1] = tsin(azi)*duvw[2];
837 >                duvw[2] = cos(alt);     /* measured from horizon */
838 >                duvw[0] = tsin(azi)*duvw[2];
839 >                duvw[1] = -tcos(azi)*duvw[2];
840                  duvw[2] = sqrt(1. - duvw[2]*duvw[2]);
841                  for (i = 3; i--; )
842                          orig_dir[1][i] = -duvw[0]*p->udir[i] -
# Line 781 | Line 844 | sample_reinhart(PARAMS *p, int b, FILE *fp)
844                                                  duvw[2]*p->nrm[i] ;
845                  if (!sample_origin(p, orig_dir[0], orig_dir[1], samp3[0]))
846                          return(0);
847 <                if (fwrite(orig_dir, sizeof(FVECT), 2, fp) != 2)
847 >                if (putbinary(orig_dir, sizeof(FVECT), 2, fp) != 2)
848                          return(0);
849          }
850          return(1);
# Line 824 | Line 887 | sample_klems(PARAMS *p, int b, FILE *fp)
887  
888          while (n--) {                   /* stratified sampling */
889                  SDmultiSamp(samp2, 2, (n+frandom())/sampcnt);
890 <                if (!bo_getvec(duvw, b+samp2[1], kbasis[bi]))
890 >                if (!fo_getvec(duvw, b+samp2[1], kbasis[bi]))
891                          return(0);
892                  for (i = 3; i--; )
893 <                        orig_dir[1][i] = duvw[0]*p->udir[i] +
894 <                                                duvw[1]*p->vdir[i] +
893 >                        orig_dir[1][i] = -duvw[0]*p->udir[i] -
894 >                                                duvw[1]*p->vdir[i] -
895                                                  duvw[2]*p->nrm[i] ;
896                  if (!sample_origin(p, orig_dir[0], orig_dir[1], samp2[0]))
897                          return(0);
898 <                if (fwrite(orig_dir, sizeof(FVECT), 2, fp) != 2)
898 >                if (putbinary(orig_dir, sizeof(FVECT), 2, fp) != 2)
899                          return(0);
900          }
901          return(1);
# Line 847 | Line 910 | prepare_sampler(void)
910                  fputs(": no sender surface!\n", stderr);
911                  return(-1);
912          }
913 <        if (curparams.outfn != NULL)    /* misplaced output file spec. */
913 >                                        /* misplaced output file spec. */
914 >        if ((curparams.outfn != NULL) & (verbose >= 0))
915                  fprintf(stderr, "%s: warning - ignoring output file in sender ('%s')\n",
916                                  progname, curparams.outfn);
917                                          /* check/set basis hemisphere */
# Line 861 | Line 925 | prepare_sampler(void)
925                  fputs(": undefined normal for sender sampling\n", stderr);
926                  return(-1);
927          }
928 <        if (normalize(curparams.vup) == 0)
928 >        if (normalize(curparams.vup) == 0) {
929                  if (fabs(curparams.nrm[2]) < .7)
930                          curparams.vup[2] = 1;
931                  else
932                          curparams.vup[1] = 1;
933 <        VCROSS(curparams.udir, curparams.vup, curparams.nrm);
933 >        }
934 >        fcross(curparams.udir, curparams.vup, curparams.nrm);
935          if (normalize(curparams.udir) == 0) {
936                  fputs(progname, stderr);
937                  fputs(": up vector coincides with sender normal\n", stderr);
938                  return(-1);
939          }
940 <        VCROSS(curparams.vdir, curparams.nrm, curparams.udir);
941 <        if (tolower(curparams.hemis[0]) == 'u' | curparams.hemis[0] == '1')
940 >        fcross(curparams.vdir, curparams.nrm, curparams.udir);
941 >        if (curparams.sign == '-') {    /* left-handed coordinate system? */
942 >                curparams.udir[0] *= -1.;
943 >                curparams.udir[1] *= -1.;
944 >                curparams.udir[2] *= -1.;
945 >        }
946 >        if ((tolower(curparams.hemis[0]) == 'u') | (curparams.hemis[0] == '1'))
947                  curparams.sample_basis = sample_uniform;
948          else if (tolower(curparams.hemis[0]) == 's' &&
949                                  tolower(curparams.hemis[1]) == 'c')
# Line 934 | Line 1004 | finish_polygon(SURF *p)
1004                  VCOPY(e1, e2);
1005          }
1006          p->area = normalize(p->snrm)*0.5;
1007 <        return(p->area > FTINY);
1007 >        return(p->area > FTINY*FTINY);
1008   }
1009  
1010   /* Add a surface to our current parameters */
# Line 952 | Line 1022 | add_surface(int st, const char *oname, FILE *fp)
1022          snew = (SURF *)malloc(sizeof(SURF) + sizeof(double)*(n-1));
1023          if (snew == NULL) {
1024                  fputs(progname, stderr);
1025 <                fputs(": out of memory!\n", stderr);
1025 >                fputs(": out of memory in add_surface!\n", stderr);
1026                  exit(1);
1027          }
1028          strncpy(snew->sname, oname, sizeof(snew->sname)-1);
# Line 989 | Line 1059 | add_surface(int st, const char *oname, FILE *fp)
1059          case ST_SOURCE:
1060                  if (snew->nfargs != 4)
1061                          goto badcount;
1062 <                VCOPY(snew->snrm, snew->farg);
1062 >                for (n = 3; n--; )      /* need to reverse "normal" */
1063 >                        snew->snrm[n] = -snew->farg[n];
1064                  if (normalize(snew->snrm) == 0)
1065                          goto badnorm;
1066                  snew->area = sin((PI/180./2.)*snew->farg[3]);
1067                  snew->area *= PI*snew->area;
1068                  break;
1069          }
1070 <        if (snew->area <= FTINY) {
1070 >        if ((snew->area <= FTINY*FTINY) & (verbose >= 0)) {
1071                  fprintf(stderr, "%s: warning - zero area for surface '%s'\n",
1072                                  progname, oname);
1073                  free(snew);
# Line 1008 | Line 1079 | add_surface(int st, const char *oname, FILE *fp)
1079          curparams.nsurfs++;
1080          return;
1081   badcount:
1082 <        fprintf(stderr, "%s: bad argument count for surface '%s'\n",
1082 >        fprintf(stderr, "%s: bad argument count for surface element '%s'\n",
1083                          progname, oname);
1084          exit(1);
1085   badnorm:
1086 <        fprintf(stderr, "%s: bad orientation for surface '%s'\n",
1086 >        fprintf(stderr, "%s: bad orientation for surface element '%s'\n",
1087                          progname, oname);
1088          exit(1);
1089   }
# Line 1044 | Line 1115 | add_recv_object(FILE *fp)
1115                                  finish_receiver();
1116                                  clear_params(&curparams, 1);
1117                          }
1118 +                        parse_params(&curparams, newparams);
1119 +                        newparams[0] = '\0';
1120                          strcpy(curmod, thismod);
1121                  }
1122                  add_surface(st, oname, fp);     /* read & store surface */
1123                  return(1);
1124          }
1125                                          /* else skip arguments */
1126 <        if (!fscanf(fp, "%d", &n)) return;
1126 >        if (!fscanf(fp, "%d", &n)) return(0);
1127          while (n-- > 0) fscanf(fp, "%*s");
1128 <        if (!fscanf(fp, "%d", &n)) return;
1128 >        if (!fscanf(fp, "%d", &n)) return(0);
1129          while (n-- > 0) fscanf(fp, "%*d");
1130 <        if (!fscanf(fp, "%d", &n)) return;
1130 >        if (!fscanf(fp, "%d", &n)) return(0);
1131          while (n-- > 0) fscanf(fp, "%*f");
1132          return(0);
1133   }
# Line 1064 | Line 1137 | static int
1137   add_send_object(FILE *fp)
1138   {
1139          int             st;
1140 <        char            otype[32], oname[128];
1140 >        char            thismod[128], otype[32], oname[128];
1141          int             n;
1142  
1143 <        if (fscanf(fp, "%*s %s %s", otype, oname) != 2)
1143 >        if (fscanf(fp, "%s %s %s", thismod, otype, oname) != 3)
1144                  return(0);              /* must have hit EOF! */
1145          if (!strcmp(otype, "alias")) {
1146                  fscanf(fp, "%*s");      /* skip alias */
# Line 1080 | Line 1153 | add_send_object(FILE *fp)
1153                          fputs(": cannot use source as a sender!\n", stderr);
1154                          return(-1);
1155                  }
1156 +                if (strcmp(thismod, curmod)) {
1157 +                        if (curmod[0]) {
1158 +                                fputs(progname, stderr);
1159 +                                fputs(": warning - multiple modifiers in sender\n",
1160 +                                                stderr);
1161 +                        }
1162 +                        strcpy(curmod, thismod);
1163 +                }
1164 +                parse_params(&curparams, newparams);
1165 +                newparams[0] = '\0';
1166                  add_surface(st, oname, fp);     /* read & store surface */
1167                  return(0);
1168          }
1169                                          /* else skip arguments */
1170 <        if (!fscanf(fp, "%d", &n)) return;
1170 >        if (!fscanf(fp, "%d", &n)) return(0);
1171          while (n-- > 0) fscanf(fp, "%*s");
1172 <        if (!fscanf(fp, "%d", &n)) return;
1172 >        if (!fscanf(fp, "%d", &n)) return(0);
1173          while (n-- > 0) fscanf(fp, "%*d");
1174 <        if (!fscanf(fp, "%d", &n)) return;
1174 >        if (!fscanf(fp, "%d", &n)) return(0);
1175          while (n-- > 0) fscanf(fp, "%*f");
1176          return(0);
1177   }
# Line 1128 | Line 1211 | load_scene(const char *inspec, int (*ocb)(FILE *))
1211                          if (!isspace(c) && fscanf(fp, "%s", inpbuf) == 1 &&
1212                                          !strcmp(inpbuf, PARAMSTART)) {
1213                                  if (fgets(inpbuf, sizeof(inpbuf), fp) != NULL)
1214 <                                        parse_params(inpbuf);
1214 >                                        strcat(newparams, inpbuf);
1215                                  continue;
1216                          }
1217 <                        while ((c = getc(fp)) != EOF && c != '\n');
1217 >                        while ((c = getc(fp)) != EOF && c != '\n')
1218                                  ;       /* else skipping comment */
1219                          continue;
1220                  }
# Line 1156 | Line 1239 | main(int argc, char *argv[])
1239   {
1240          char    fmtopt[6] = "-faa";     /* default output is ASCII */
1241          char    *xrs=NULL, *yrs=NULL, *ldopt=NULL;
1242 +        char    *iropt = NULL;
1243          char    *sendfn;
1244          char    sampcntbuf[32], nsbinbuf[32];
1245          FILE    *rcfp;
# Line 1163 | Line 1247 | main(int argc, char *argv[])
1247          int     a, i;
1248                                          /* screen rcontrib options */
1249          progname = argv[0];
1250 <        for (a = 1; a < argc-2 && argv[a][0] == '-'; a++) {
1251 <                int     na = 1;         /* !! Keep consistent !! */
1252 <                switch (argv[a][1]) {
1250 >        for (a = 1; a < argc-2; a++) {
1251 >                int     na;
1252 >                                        /* check for argument expansion */
1253 >                while ((na = expandarg(&argc, &argv, a)) > 0)
1254 >                        ;
1255 >                if (na < 0) {
1256 >                        fprintf(stderr, "%s: cannot expand '%s'\n",
1257 >                                        progname, argv[a]);
1258 >                        return(1);
1259 >                }
1260 >                if (argv[a][0] != '-' || !argv[a][1])
1261 >                        break;
1262 >                na = 1;
1263 >                switch (argv[a][1]) {   /* !! Keep consistent !! */
1264                  case 'v':               /* verbose mode */
1265 <                        verbose = !verbose;
1265 >                        verbose = 1;
1266                          na = 0;
1267                          continue;
1268                  case 'f':               /* special case for -fo, -ff, etc. */
# Line 1180 | Line 1275 | main(int argc, char *argv[])
1275                          case 'f':
1276                          case 'd':
1277                          case 'c':
1278 <                                if (!(fmtopt[4] = argv[a][3]))
1279 <                                        fmtopt[4] = argv[a][2];
1280 <                                fmtopt[3] = argv[a][2];
1278 >                                if (!(fmtopt[3] = argv[a][3]))
1279 >                                        fmtopt[3] = argv[a][2];
1280 >                                fmtopt[2] = argv[a][2];
1281                                  na = 0;
1282                                  continue;       /* will pass later */
1283                          default:
# Line 1197 | Line 1292 | main(int argc, char *argv[])
1292                          yrs = argv[++a];
1293                          na = 0;
1294                          continue;
1295 <                case 'c':               /* number of samples */
1296 <                        sampcnt = atoi(argv[a+1]);
1297 <                        if (sampcnt <= 0)
1298 <                                goto userr;
1299 <                        na = 0;         /* we re-add this later */
1295 >                case 'c':               /* spectral sampling or count */
1296 >                        switch (argv[a][2]) {
1297 >                        case 's':
1298 >                                na = 2;
1299 >                                break;
1300 >                        case 'w':
1301 >                                na = 3;
1302 >                                break;
1303 >                        case '\0':
1304 >                                sampcnt = atoi(argv[++a]);
1305 >                                if (sampcnt <= 0)
1306 >                                        goto userr;
1307 >                                na = 0;         /* we re-add this later */
1308 >                                continue;
1309 >                        }
1310 >                        break;
1311 >                case 'I':               /* only for pass-through mode */
1312 >                case 'i':
1313 >                        iropt = argv[a];
1314 >                        na = 0;
1315                          continue;
1316 <                case 'V':               /* options without arguments */
1317 <                case 'w':
1316 >                case 'w':               /* options without arguments */
1317 >                        if (!argv[a][2] || strchr("+1tTyY", argv[a][2]) == NULL)
1318 >                                verbose = -1;
1319 >                        break;
1320 >                case 'V':
1321                  case 'u':
1209                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 1230 | 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;
1239                case '\0':              /* pass-through mode */
1240                        goto done_opts;
1357                  default:                /* anything else is verbotten */
1358                          goto userr;
1359                  }
# Line 1247 | Line 1363 | main(int argc, char *argv[])
1363                  while (--na)            /* + arguments if any */
1364                          rcarg[nrcargs++] = argv[++a];
1365          }
1250 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 (iropt) {
1373 +                        CHECKARGC(1);
1374 +                        rcarg[nrcargs++] = iropt;
1375 +                }
1376                  if (xrs) {
1377                          CHECKARGC(2);
1378                          rcarg[nrcargs++] = "-x";
# Line 1269 | Line 1388 | done_opts:
1388                          rcarg[nrcargs++] = ldopt;
1389                  }
1390                  if (sampcnt <= 0) sampcnt = 1;
1391 <        } else {                        /* else FVECT determines input format */
1392 <                fmtopt[3] = (sizeof(RREAL)==sizeof(double)) ? 'd' : 'f';
1391 >        } else {                        /* else in sampling mode */
1392 >                if (iropt) {
1393 >                        fputs(progname, stderr);
1394 >                        fputs(": -i, -I supported for pass-through only\n", stderr);
1395 >                        return(1);
1396 >                }
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 1289 | 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)
# Line 1306 | Line 1435 | done_opts:
1435   #ifdef getc_unlocked
1436          flockfile(rcfp);
1437   #endif
1438 <        if (verbose) {
1438 >        if (verbose > 0) {
1439                  fprintf(stderr, "%s: sampling %d directions", progname, nsbins);
1440                  if (curparams.nsurfs > 1)
1441 <                        fprintf(stderr, " (%d surfaces)\n", curparams.nsurfs);
1441 >                        fprintf(stderr, " (%d elements)\n", curparams.nsurfs);
1442                  else
1443                          fputc('\n', stderr);
1444          }
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
> Changed lines