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.23 by greg, Wed Feb 18 06:18:38 2015 UTC vs.
Revision 2.49 by greg, Tue Dec 10 19:15:54 2019 UTC

# Line 11 | Line 11 | static const char RCSid[] = "$Id$";
11   #include <stdlib.h>
12   #include "rtio.h"
13   #include "rtmath.h"
14 < #include "rtprocess.h"
14 > #include "paths.h"
15   #include "bsdf.h"
16   #include "bsdf_m.h"
17   #include "random.h"
18   #include "triangulate.h"
19   #include "platform.h"
20  
21 < #ifdef getc_unlocked            /* avoid horrendous overhead of flockfile */
22 < #undef getc
23 < #define getc    getc_unlocked
21 > #ifndef MAXRCARG
22 > #define MAXRCARG        10000
23   #endif
24  
26 #ifdef _WIN32
27 #define SPECIALS        " \t\"$*?"
28 #define QUOTCHAR        '"'
29 #else
30 #define SPECIALS        " \t\n'\"()${}*?[];|&"
31 #define QUOTCHAR        '\''
32 #define ALTQUOT         '"'
33 #endif
34
35 #define MAXRCARG        512
36
25   char            *progname;              /* global argv[0] */
26  
27   int             verbose = 0;            /* verbose mode (< 0 no warnings) */
# Line 78 | Line 66 | typedef struct {
66          FVECT   uva[2];                 /* tangent axes */
67          int     ntris;                  /* number of triangles */
68          struct ptri {
69 <                float   afrac;                  /* fraction of total area */
69 >                double  afrac;                  /* fraction of total area */
70                  short   vndx[3];                /* vertex indices */
71          }       tri[1];                 /* triangle array (extends struct) */
72   } POLYTRIS;                     /* triangulated polygon */
73  
74   typedef struct param_s {
75 <        char            hemis[32];      /* hemispherical sampling spec. */
75 >        char            sign;           /* '-' for axis reversal */
76 >        char            hemis[31];      /* hemispherical sampling spec. */
77          int             hsiz;           /* hemisphere basis size */
78          int             nsurfs;         /* number of surfaces */
79          SURF            *slist;         /* list of surfaces */
80          FVECT           vup;            /* up vector (zero if unset) */
81          FVECT           nrm;            /* average normal direction */
82 <        FVECT           udir, vdir;     /* v-up tangent axes */
82 >        FVECT           udir, vdir;     /* tangent axes */
83          char            *outfn;         /* output file name (receiver) */
84          int             (*sample_basis)(struct param_s *p, int, FILE *);
85   } PARAMS;                       /* sender/receiver parameters */
# Line 151 | Line 140 | oconv_command(int ac, char *av[])
140          
141          if (ac-- <= 0)
142                  return(NULL);
143 <        while (ac-- > 0) {
144 <                strcpy(cp, *av++);
145 <                while (*cp) cp++;
157 <                *cp++ = ' ';
158 <                if (cp >= oconvbuf+(sizeof(oconvbuf)-32)) {
159 <                        fputs(progname, stderr);
160 <                        fputs(": too many file arguments!\n", stderr);
161 <                        exit(1);
162 <                }
143 >        if (verbose < 0) {      /* turn off warnings */
144 >                strcpy(cp, "-w ");
145 >                cp += 4;
146          }
147 <        strcpy(cp, recv);       /* receiver goes last */
148 <        return(oconvbuf);
149 < }
150 <
151 < /* Check if any of the characters in str2 are found in str1 */
152 < static int
153 < matchany(const char *str1, const char *str2)
154 < {
155 <        while (*str1) {
173 <                const char      *cp = str2;
174 <                while (*cp)
175 <                        if (*cp++ == *str1)
176 <                                return(*str1);
177 <                ++str1;
178 <        }
179 <        return(0);
180 < }
181 <
182 <
183 < /* Convert a set of arguments into a command line for pipe() or system() */
184 < static char *
185 < convert_commandline(char *cmd, const int len, char *av[])
186 < {
187 <        int     match;
188 <        char    *cp;
189 <
190 <        for (cp = cmd; *av != NULL; av++) {
191 <                const int       n = strlen(*av);
192 <                if (cp+n >= cmd+(len-3)) {
193 <                        fputs(progname, stderr);
194 <                        return(NULL);
195 <                }
196 <                if ((match = matchany(*av, SPECIALS))) {
197 <                        const int       quote =
198 < #ifdef ALTQUOT
199 <                                (match == QUOTCHAR) ? ALTQUOT :
200 < #endif
201 <                                        QUOTCHAR;
202 <                        *cp++ = quote;
203 <                        strcpy(cp, *av);
204 <                        cp += n;
205 <                        *cp++ = quote;
147 >        while (ac-- > 0) {      /* copy each argument */
148 >                int     len = strlen(*av);
149 >                if (cp+len+4 >= oconvbuf+sizeof(oconvbuf))
150 >                        goto overrun;
151 >                if (matchany(*av, SPECIALS)) {
152 >                        *cp++ = QUOTCHAR;
153 >                        strcpy(cp, *av++);
154 >                        cp += len;
155 >                        *cp++ = QUOTCHAR;
156                  } else {
157 <                        strcpy(cp, *av);
158 <                        cp += n;
157 >                        strcpy(cp, *av++);
158 >                        cp += len;
159                  }
160                  *cp++ = ' ';
161          }
162 <        if (cp <= cmd)
163 <                return(NULL);
164 <        *--cp = '\0';
165 <        return(cmd);
162 >                                /* receiver goes last */
163 >        if (matchany(recv, SPECIALS)) {
164 >                *cp++ = QUOTCHAR;
165 >                while (*recv) {
166 >                        if (cp >= oconvbuf+(sizeof(oconvbuf)-3))
167 >                                goto overrun;
168 >                        *cp++ = *recv++;
169 >                }
170 >                *cp++ = QUOTCHAR;
171 >                *cp = '\0';
172 >        } else
173 >                strcpy(cp, recv);
174 >        return(oconvbuf);
175 > overrun:
176 >        fputs(progname, stderr);
177 >        fputs(": too many file arguments!\n", stderr);
178 >        exit(1);
179   }
180  
181   /* Open a pipe to/from a command given as an argument list */
# Line 232 | Line 195 | popen_arglist(char *av[], char *mode)
195          return(popen(cmd, mode));
196   }
197  
198 < #ifdef _WIN32
198 > #if defined(_WIN32) || defined(_WIN64)
199   /* Execute system command (Windows version) */
200   static int
201   my_exec(char *av[])
# Line 317 | Line 280 | parse_params(PARAMS *p, char *pargs)
280   {
281          char    *cp = pargs;
282          int     nparams = 0;
283 +        int     quot;
284          int     i;
285  
286          for ( ; ; ) {
# Line 324 | Line 288 | parse_params(PARAMS *p, char *pargs)
288                  case 'h':
289                          if (*cp++ != '=')
290                                  break;
291 +                        if ((*cp == '+') | (*cp == '-'))
292 +                                p->sign = *cp++;
293 +                        else
294 +                                p->sign = '+';
295                          p->hsiz = 0;
296                          i = 0;
297                          while (*cp && !isspace(*cp)) {
# Line 349 | Line 317 | parse_params(PARAMS *p, char *pargs)
317                  case 'o':
318                          if (*cp++ != '=')
319                                  break;
320 +                        quot = 0;
321 +                        if ((*cp == '"') | (*cp == '\''))
322 +                                quot = *cp++;
323                          i = 0;
324 <                        while (*cp && !isspace(*cp++))
325 <                                i++;
324 >                        while (*cp && (quot ? (*cp != quot) : !isspace(*cp))) {
325 >                                i++; cp++;
326 >                        }
327                          if (!i)
328                                  break;
329 <                        *--cp = '\0';
329 >                        if (!*cp) {
330 >                                if (quot)
331 >                                        break;
332 >                                cp[1] = '\0';
333 >                        }
334 >                        *cp = '\0';
335                          p->outfn = savqstr(cp-i);
336 <                        *cp++ = ' ';
336 >                        *cp++ = quot ? quot : ' ';
337                          ++nparams;
338                          continue;
339                  case ' ':
# Line 416 | Line 393 | finish_receiver(void)
393                          curparams.vup[1] = 1;
394          }
395                                          /* determine sample type/bin */
396 <        if (tolower(curparams.hemis[0]) == 'u' | curparams.hemis[0] == '1') {
396 >        if ((tolower(curparams.hemis[0]) == 'u') | (curparams.hemis[0] == '1')) {
397                  sprintf(sbuf, "if(-Dx*%g-Dy*%g-Dz*%g,0,-1)",
398                          curparams.nrm[0], curparams.nrm[1], curparams.nrm[2]);
399                  binv = savqstr(sbuf);
# Line 431 | Line 408 | finish_receiver(void)
408                          exit(1);
409                  }
410                  calfn = shirchiufn; shirchiufn = NULL;
411 <                sprintf(sbuf, "SCdim=%d,rNx=%g,rNy=%g,rNz=%g,Ux=%g,Uy=%g,Uz=%g",
411 >                sprintf(sbuf, "SCdim=%d,rNx=%g,rNy=%g,rNz=%g,Ux=%g,Uy=%g,Uz=%g,RHS=%c1",
412                                  curparams.hsiz,
413                          curparams.nrm[0], curparams.nrm[1], curparams.nrm[2],
414 <                        curparams.vup[0], curparams.vup[1], curparams.vup[2]);
414 >                        curparams.vup[0], curparams.vup[1], curparams.vup[2],
415 >                        curparams.sign);
416                  params = savqstr(sbuf);
417                  binv = "scbin";
418                  nbins = "SCdim*SCdim";
419          } else if ((tolower(curparams.hemis[0]) == 'r') |
420                          (tolower(curparams.hemis[0]) == 't')) {
421                  calfn = reinhfn; reinhfn = NULL;
422 <                sprintf(sbuf, "MF=%d,rNx=%g,rNy=%g,rNz=%g,Ux=%g,Uy=%g,Uz=%g",
422 >                sprintf(sbuf, "MF=%d,rNx=%g,rNy=%g,rNz=%g,Ux=%g,Uy=%g,Uz=%g,RHS=%c1",
423                                  curparams.hsiz,
424                          curparams.nrm[0], curparams.nrm[1], curparams.nrm[2],
425 <                        curparams.vup[0], curparams.vup[1], curparams.vup[2]);
425 >                        curparams.vup[0], curparams.vup[1], curparams.vup[2],
426 >                        curparams.sign);
427                  params = savqstr(sbuf);
428                  binv = "rbin";
429                  nbins = "Nrbins";
# Line 472 | Line 451 | finish_receiver(void)
451                                  progname, curparams.hemis);
452                  exit(1);
453          }
454 +        if (tolower(curparams.hemis[0]) == 'k') {
455 +                sprintf(sbuf, "RHS=%c1", curparams.sign);
456 +                params = savqstr(sbuf);
457 +        }
458          if (!uniform & (curparams.slist->styp == ST_SOURCE)) {
459                  SURF    *sp;
460                  for (sp = curparams.slist; sp != NULL; sp = sp->next)
# Line 519 | Line 502 | make_axes(FVECT uva[2], const FVECT nrm)
502   {
503          int     i;
504  
505 <        if (!getperpendicular(uva[0], nrm)) {
505 >        if (!getperpendicular(uva[0], nrm, 1)) {
506                  fputs(progname, stderr);
507                  fputs(": bad surface normal in make_axes!\n", stderr);
508                  exit(1);
# Line 633 | Line 616 | ssamp_poly(FVECT orig, SURF *sp, double x)
616                  sp->priv = (void *)ptp;
617          }
618                                          /* pick triangle by partial area */
619 <        for (i = 0; i < ptp->ntris && x > ptp->tri[i].afrac; i++)
619 >        for (i = 0; i < ptp->ntris-1 && x > ptp->tri[i].afrac; i++)
620                  x -= ptp->tri[i].afrac;
621          SDmultiSamp(samp2, 2, x/ptp->tri[i].afrac);
622          samp2[0] *= samp2[1] = sqrt(samp2[1]);
# Line 663 | Line 646 | sample_origin(PARAMS *p, FVECT orig, const FVECT rdir,
646                                          /* special case for lone surface */
647          if (p->nsurfs == 1) {
648                  sp = p->slist;
649 <                if (DOT(sp->snrm, rdir) >= -FTINY) {
649 >                if (DOT(sp->snrm, rdir) >= FTINY) {
650                          fprintf(stderr,
651                                  "%s: internal - sample behind sender '%s'\n",
652                                          progname, sp->sname);
# Line 674 | Line 657 | sample_origin(PARAMS *p, FVECT orig, const FVECT rdir,
657          if (p->nsurfs > nall) {         /* (re)allocate surface area cache */
658                  if (projsa) free(projsa);
659                  projsa = (double *)malloc(sizeof(double)*p->nsurfs);
660 <                if (!projsa) return(0);
660 >                if (projsa == NULL) {
661 >                        fputs(progname, stderr);
662 >                        fputs(": out of memory in sample_origin!\n", stderr);
663 >                        exit(1);
664 >                }
665                  nall = p->nsurfs;
666          }
667                                          /* compute projected areas */
# Line 682 | Line 669 | sample_origin(PARAMS *p, FVECT orig, const FVECT rdir,
669                  projsa[i] = -DOT(sp->snrm, rdir) * sp->area;
670                  tarea += projsa[i] *= (double)(projsa[i] > FTINY);
671          }
672 <        if (tarea <= FTINY) {           /* wrong side of sender? */
672 >        if (tarea < 0) {                /* wrong side of sender? */
673                  fputs(progname, stderr);
674                  fputs(": internal - sample behind all sender elements!\n",
675                                  stderr);
# Line 717 | Line 704 | sample_uniform(PARAMS *p, int b, FILE *fp)
704                                                  duvw[2]*p->nrm[i] ;
705                  if (!sample_origin(p, orig_dir[0], orig_dir[1], samp3[0]))
706                          return(0);
707 <                if (fwrite(orig_dir, sizeof(FVECT), 2, fp) != 2)
707 >                if (putbinary(orig_dir, sizeof(FVECT), 2, fp) != 2)
708                          return(0);
709          }
710          return(1);
# Line 747 | Line 734 | sample_shirchiu(PARAMS *p, int b, FILE *fp)
734                                                  duvw[2]*p->nrm[i] ;
735                  if (!sample_origin(p, orig_dir[0], orig_dir[1], samp3[0]))
736                          return(0);
737 <                if (fwrite(orig_dir, sizeof(FVECT), 2, fp) != 2)
737 >                if (putbinary(orig_dir, sizeof(FVECT), 2, fp) != 2)
738                          return(0);
739          }
740          return(1);
# Line 783 | Line 770 | sample_reinhart(PARAMS *p, int b, FILE *fp)
770          }
771          while (n--) {                   /* stratified sampling */
772                  SDmultiSamp(samp3, 3, (n+frandom())/sampcnt);
773 +                if (row >= RowMax-1)    /* avoid crowding at zenith */
774 +                        samp3[1] *= samp3[1];
775                  alt = (row+samp3[1])*RAH;
776                  azi = (2.*PI)*(col+samp3[2]-.5)/rnaz(row);
777                  duvw[2] = cos(alt);     /* measured from horizon */
778                  duvw[0] = tsin(azi)*duvw[2];
779 <                duvw[1] = tcos(azi)*duvw[2];
779 >                duvw[1] = -tcos(azi)*duvw[2];
780                  duvw[2] = sqrt(1. - duvw[2]*duvw[2]);
781                  for (i = 3; i--; )
782                          orig_dir[1][i] = -duvw[0]*p->udir[i] -
# Line 795 | Line 784 | sample_reinhart(PARAMS *p, int b, FILE *fp)
784                                                  duvw[2]*p->nrm[i] ;
785                  if (!sample_origin(p, orig_dir[0], orig_dir[1], samp3[0]))
786                          return(0);
787 <                if (fwrite(orig_dir, sizeof(FVECT), 2, fp) != 2)
787 >                if (putbinary(orig_dir, sizeof(FVECT), 2, fp) != 2)
788                          return(0);
789          }
790          return(1);
# Line 838 | Line 827 | sample_klems(PARAMS *p, int b, FILE *fp)
827  
828          while (n--) {                   /* stratified sampling */
829                  SDmultiSamp(samp2, 2, (n+frandom())/sampcnt);
830 <                if (!bi_getvec(duvw, b+samp2[1], kbasis[bi]))
830 >                if (!fo_getvec(duvw, b+samp2[1], kbasis[bi]))
831                          return(0);
832                  for (i = 3; i--; )
833 <                        orig_dir[1][i] = duvw[0]*p->udir[i] +
834 <                                                duvw[1]*p->vdir[i] +
833 >                        orig_dir[1][i] = -duvw[0]*p->udir[i] -
834 >                                                duvw[1]*p->vdir[i] -
835                                                  duvw[2]*p->nrm[i] ;
836                  if (!sample_origin(p, orig_dir[0], orig_dir[1], samp2[0]))
837                          return(0);
838 <                if (fwrite(orig_dir, sizeof(FVECT), 2, fp) != 2)
838 >                if (putbinary(orig_dir, sizeof(FVECT), 2, fp) != 2)
839                          return(0);
840          }
841          return(1);
# Line 882 | Line 871 | prepare_sampler(void)
871                  else
872                          curparams.vup[1] = 1;
873          }
874 <        VCROSS(curparams.udir, curparams.vup, curparams.nrm);
874 >        fcross(curparams.udir, curparams.vup, curparams.nrm);
875          if (normalize(curparams.udir) == 0) {
876                  fputs(progname, stderr);
877                  fputs(": up vector coincides with sender normal\n", stderr);
878                  return(-1);
879          }
880 <        VCROSS(curparams.vdir, curparams.nrm, curparams.udir);
881 <        if (tolower(curparams.hemis[0]) == 'u' | curparams.hemis[0] == '1')
880 >        fcross(curparams.vdir, curparams.nrm, curparams.udir);
881 >        if (curparams.sign == '-') {    /* left-handed coordinate system? */
882 >                curparams.udir[0] *= -1.;
883 >                curparams.udir[1] *= -1.;
884 >                curparams.udir[2] *= -1.;
885 >        }
886 >        if ((tolower(curparams.hemis[0]) == 'u') | (curparams.hemis[0] == '1'))
887                  curparams.sample_basis = sample_uniform;
888          else if (tolower(curparams.hemis[0]) == 's' &&
889                                  tolower(curparams.hemis[1]) == 'c')
# Line 968 | Line 962 | add_surface(int st, const char *oname, FILE *fp)
962          snew = (SURF *)malloc(sizeof(SURF) + sizeof(double)*(n-1));
963          if (snew == NULL) {
964                  fputs(progname, stderr);
965 <                fputs(": out of memory!\n", stderr);
965 >                fputs(": out of memory in add_surface!\n", stderr);
966                  exit(1);
967          }
968          strncpy(snew->sname, oname, sizeof(snew->sname)-1);
# Line 1083 | Line 1077 | static int
1077   add_send_object(FILE *fp)
1078   {
1079          int             st;
1080 <        char            otype[32], oname[128];
1080 >        char            thismod[128], otype[32], oname[128];
1081          int             n;
1082  
1083 <        if (fscanf(fp, "%*s %s %s", otype, oname) != 2)
1083 >        if (fscanf(fp, "%s %s %s", thismod, otype, oname) != 3)
1084                  return(0);              /* must have hit EOF! */
1085          if (!strcmp(otype, "alias")) {
1086                  fscanf(fp, "%*s");      /* skip alias */
# Line 1099 | Line 1093 | add_send_object(FILE *fp)
1093                          fputs(": cannot use source as a sender!\n", stderr);
1094                          return(-1);
1095                  }
1096 +                if (strcmp(thismod, curmod)) {
1097 +                        if (curmod[0]) {
1098 +                                fputs(progname, stderr);
1099 +                                fputs(": warning - multiple modifiers in sender\n",
1100 +                                                stderr);
1101 +                        }
1102 +                        strcpy(curmod, thismod);
1103 +                }
1104                  parse_params(&curparams, newparams);
1105                  newparams[0] = '\0';
1106                  add_surface(st, oname, fp);     /* read & store surface */
# Line 1242 | Line 1244 | main(int argc, char *argv[])
1244                          na = 0;
1245                          continue;
1246                  case 'w':               /* options without arguments */
1247 <                        if (argv[a][2] != '+') verbose = -1;
1247 >                        if (!argv[a][2] || strchr("+1tTyY", argv[a][2]) == NULL)
1248 >                                verbose = -1;
1249 >                        break;
1250                  case 'V':
1251                  case 'u':
1252                  case 'h':
# Line 1268 | Line 1272 | main(int argc, char *argv[])
1272                          if (argv[a][2] != 'v') na = 2;
1273                          break;
1274                  case 'a':               /* special case */
1275 <                        na = (argv[a][2] == 'v') ? 4 : 2;
1275 >                        if (argv[a][2] == 'p') {
1276 >                                na = 2; /* photon map [+ bandwidth(s)] */
1277 >                                if (a < argc-3 && atoi(argv[a+1]) > 0)
1278 >                                        na += 1 + (a < argc-4 && atoi(argv[a+2]) > 0);
1279 >                        } else
1280 >                                na = (argv[a][2] == 'v') ? 4 : 2;
1281                          break;
1282                  case 'm':               /* special case */
1283                          if (!argv[a][2]) goto userr;
# Line 1314 | Line 1323 | main(int argc, char *argv[])
1323                          fputs(": -i, -I supported for pass-through only\n", stderr);
1324                          return(1);
1325                  }
1326 <                fmtopt[2] = (sizeof(RREAL)==sizeof(double)) ? 'd' : 'f';
1326 > #ifdef SMLFLT
1327 >                fmtopt[2] = 'f';
1328 > #else
1329 >                fmtopt[2] = 'd';
1330 > #endif
1331                  if (sampcnt <= 0) sampcnt = 10000;
1332          }
1333          sprintf(sampcntbuf, "%d", sampcnt);
# Line 1333 | Line 1346 | main(int argc, char *argv[])
1346                  return(my_exec(rcarg)); /* rcontrib does everything */
1347          }
1348          clear_params(&curparams, 0);    /* else load sender surface & params */
1349 +        curmod[0] = '\0';
1350          if (load_scene(sendfn, add_send_object) < 0)
1351                  return(1);
1352          if ((nsbins = prepare_sampler()) <= 0)

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines