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.46 by greg, Mon Oct 1 15:51:48 2018 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 +        if (verbose < 0) {      /* turn off warnings */
144 +                strcpy(cp, "-w- ");
145 +                cp += 4;
146 +        }
147          while (ac-- > 0) {
148                  strcpy(cp, *av++);
149                  while (*cp) cp++;
150                  *cp++ = ' ';
151 <                if (cp >= oconvbuf+(sizeof(oconvbuf)-32)) {
152 <                        fputs(progname, stderr);
160 <                        fputs(": too many file arguments!\n", stderr);
161 <                        exit(1);
162 <                }
151 >                if (cp >= oconvbuf+(sizeof(oconvbuf)-32))
152 >                        goto overrun;
153          }
154 <        strcpy(cp, recv);       /* receiver goes last */
154 >                                /* receiver goes last */
155 >        if (matchany(recv, SPECIALS)) {
156 >                *cp++ = QUOTCHAR;
157 >                while (*recv) {
158 >                        if (cp >= oconvbuf+(sizeof(oconvbuf)-3))
159 >                                goto overrun;
160 >                        *cp++ = *recv++;
161 >                }
162 >                *cp++ = QUOTCHAR;
163 >                *cp = '\0';
164 >        } else
165 >                strcpy(cp, recv);
166          return(oconvbuf);
167 + overrun:
168 +        fputs(progname, stderr);
169 +        fputs(": too many file arguments!\n", stderr);
170 +        exit(1);
171   }
172  
168 /* Check if any of the characters in str2 are found in str1 */
169 static int
170 matchany(const char *str1, const char *str2)
171 {
172        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;
206                } else {
207                        strcpy(cp, *av);
208                        cp += n;
209                }
210                *cp++ = ' ';
211        }
212        if (cp <= cmd)
213                return(NULL);
214        *--cp = '\0';
215        return(cmd);
216 }
217
173   /* Open a pipe to/from a command given as an argument list */
174   static FILE *
175   popen_arglist(char *av[], char *mode)
# Line 232 | Line 187 | popen_arglist(char *av[], char *mode)
187          return(popen(cmd, mode));
188   }
189  
190 < #ifdef _WIN32
190 > #if defined(_WIN32) || defined(_WIN64)
191   /* Execute system command (Windows version) */
192   static int
193   my_exec(char *av[])
# Line 317 | Line 272 | parse_params(PARAMS *p, char *pargs)
272   {
273          char    *cp = pargs;
274          int     nparams = 0;
275 +        int     quot;
276          int     i;
277  
278          for ( ; ; ) {
# Line 324 | Line 280 | parse_params(PARAMS *p, char *pargs)
280                  case 'h':
281                          if (*cp++ != '=')
282                                  break;
283 +                        if ((*cp == '+') | (*cp == '-'))
284 +                                p->sign = *cp++;
285 +                        else
286 +                                p->sign = '+';
287                          p->hsiz = 0;
288                          i = 0;
289                          while (*cp && !isspace(*cp)) {
# Line 349 | Line 309 | parse_params(PARAMS *p, char *pargs)
309                  case 'o':
310                          if (*cp++ != '=')
311                                  break;
312 +                        quot = 0;
313 +                        if ((*cp == '"') | (*cp == '\''))
314 +                                quot = *cp++;
315                          i = 0;
316 <                        while (*cp && !isspace(*cp++))
317 <                                i++;
316 >                        while (*cp && (quot ? (*cp != quot) : !isspace(*cp))) {
317 >                                i++; cp++;
318 >                        }
319                          if (!i)
320                                  break;
321 <                        *--cp = '\0';
321 >                        if (!*cp) {
322 >                                if (quot)
323 >                                        break;
324 >                                cp[1] = '\0';
325 >                        }
326 >                        *cp = '\0';
327                          p->outfn = savqstr(cp-i);
328 <                        *cp++ = ' ';
328 >                        *cp++ = quot ? quot : ' ';
329                          ++nparams;
330                          continue;
331                  case ' ':
# Line 416 | Line 385 | finish_receiver(void)
385                          curparams.vup[1] = 1;
386          }
387                                          /* determine sample type/bin */
388 <        if (tolower(curparams.hemis[0]) == 'u' | curparams.hemis[0] == '1') {
388 >        if ((tolower(curparams.hemis[0]) == 'u') | (curparams.hemis[0] == '1')) {
389                  sprintf(sbuf, "if(-Dx*%g-Dy*%g-Dz*%g,0,-1)",
390                          curparams.nrm[0], curparams.nrm[1], curparams.nrm[2]);
391                  binv = savqstr(sbuf);
# Line 431 | Line 400 | finish_receiver(void)
400                          exit(1);
401                  }
402                  calfn = shirchiufn; shirchiufn = NULL;
403 <                sprintf(sbuf, "SCdim=%d,rNx=%g,rNy=%g,rNz=%g,Ux=%g,Uy=%g,Uz=%g",
403 >                sprintf(sbuf, "SCdim=%d,rNx=%g,rNy=%g,rNz=%g,Ux=%g,Uy=%g,Uz=%g,RHS=%c1",
404                                  curparams.hsiz,
405                          curparams.nrm[0], curparams.nrm[1], curparams.nrm[2],
406 <                        curparams.vup[0], curparams.vup[1], curparams.vup[2]);
406 >                        curparams.vup[0], curparams.vup[1], curparams.vup[2],
407 >                        curparams.sign);
408                  params = savqstr(sbuf);
409                  binv = "scbin";
410                  nbins = "SCdim*SCdim";
411          } else if ((tolower(curparams.hemis[0]) == 'r') |
412                          (tolower(curparams.hemis[0]) == 't')) {
413                  calfn = reinhfn; reinhfn = NULL;
414 <                sprintf(sbuf, "MF=%d,rNx=%g,rNy=%g,rNz=%g,Ux=%g,Uy=%g,Uz=%g",
414 >                sprintf(sbuf, "MF=%d,rNx=%g,rNy=%g,rNz=%g,Ux=%g,Uy=%g,Uz=%g,RHS=%c1",
415                                  curparams.hsiz,
416                          curparams.nrm[0], curparams.nrm[1], curparams.nrm[2],
417 <                        curparams.vup[0], curparams.vup[1], curparams.vup[2]);
417 >                        curparams.vup[0], curparams.vup[1], curparams.vup[2],
418 >                        curparams.sign);
419                  params = savqstr(sbuf);
420                  binv = "rbin";
421                  nbins = "Nrbins";
# Line 472 | Line 443 | finish_receiver(void)
443                                  progname, curparams.hemis);
444                  exit(1);
445          }
446 +        if (tolower(curparams.hemis[0]) == 'k') {
447 +                sprintf(sbuf, "RHS=%c1", curparams.sign);
448 +                params = savqstr(sbuf);
449 +        }
450          if (!uniform & (curparams.slist->styp == ST_SOURCE)) {
451                  SURF    *sp;
452                  for (sp = curparams.slist; sp != NULL; sp = sp->next)
# Line 519 | Line 494 | make_axes(FVECT uva[2], const FVECT nrm)
494   {
495          int     i;
496  
497 <        if (!getperpendicular(uva[0], nrm)) {
497 >        if (!getperpendicular(uva[0], nrm, 1)) {
498                  fputs(progname, stderr);
499                  fputs(": bad surface normal in make_axes!\n", stderr);
500                  exit(1);
# Line 633 | Line 608 | ssamp_poly(FVECT orig, SURF *sp, double x)
608                  sp->priv = (void *)ptp;
609          }
610                                          /* pick triangle by partial area */
611 <        for (i = 0; i < ptp->ntris && x > ptp->tri[i].afrac; i++)
611 >        for (i = 0; i < ptp->ntris-1 && x > ptp->tri[i].afrac; i++)
612                  x -= ptp->tri[i].afrac;
613          SDmultiSamp(samp2, 2, x/ptp->tri[i].afrac);
614          samp2[0] *= samp2[1] = sqrt(samp2[1]);
# Line 663 | Line 638 | sample_origin(PARAMS *p, FVECT orig, const FVECT rdir,
638                                          /* special case for lone surface */
639          if (p->nsurfs == 1) {
640                  sp = p->slist;
641 <                if (DOT(sp->snrm, rdir) >= -FTINY) {
641 >                if (DOT(sp->snrm, rdir) >= FTINY) {
642                          fprintf(stderr,
643                                  "%s: internal - sample behind sender '%s'\n",
644                                          progname, sp->sname);
# Line 674 | Line 649 | sample_origin(PARAMS *p, FVECT orig, const FVECT rdir,
649          if (p->nsurfs > nall) {         /* (re)allocate surface area cache */
650                  if (projsa) free(projsa);
651                  projsa = (double *)malloc(sizeof(double)*p->nsurfs);
652 <                if (!projsa) return(0);
652 >                if (projsa == NULL) {
653 >                        fputs(progname, stderr);
654 >                        fputs(": out of memory in sample_origin!\n", stderr);
655 >                        exit(1);
656 >                }
657                  nall = p->nsurfs;
658          }
659                                          /* compute projected areas */
# Line 717 | Line 696 | sample_uniform(PARAMS *p, int b, FILE *fp)
696                                                  duvw[2]*p->nrm[i] ;
697                  if (!sample_origin(p, orig_dir[0], orig_dir[1], samp3[0]))
698                          return(0);
699 <                if (fwrite(orig_dir, sizeof(FVECT), 2, fp) != 2)
699 >                if (putbinary(orig_dir, sizeof(FVECT), 2, fp) != 2)
700                          return(0);
701          }
702          return(1);
# Line 747 | Line 726 | sample_shirchiu(PARAMS *p, int b, FILE *fp)
726                                                  duvw[2]*p->nrm[i] ;
727                  if (!sample_origin(p, orig_dir[0], orig_dir[1], samp3[0]))
728                          return(0);
729 <                if (fwrite(orig_dir, sizeof(FVECT), 2, fp) != 2)
729 >                if (putbinary(orig_dir, sizeof(FVECT), 2, fp) != 2)
730                          return(0);
731          }
732          return(1);
# Line 783 | Line 762 | sample_reinhart(PARAMS *p, int b, FILE *fp)
762          }
763          while (n--) {                   /* stratified sampling */
764                  SDmultiSamp(samp3, 3, (n+frandom())/sampcnt);
765 +                if (row >= RowMax-1)    /* avoid crowding at zenith */
766 +                        samp3[1] *= samp3[1];
767                  alt = (row+samp3[1])*RAH;
768                  azi = (2.*PI)*(col+samp3[2]-.5)/rnaz(row);
769                  duvw[2] = cos(alt);     /* measured from horizon */
770                  duvw[0] = tsin(azi)*duvw[2];
771 <                duvw[1] = tcos(azi)*duvw[2];
771 >                duvw[1] = -tcos(azi)*duvw[2];
772                  duvw[2] = sqrt(1. - duvw[2]*duvw[2]);
773                  for (i = 3; i--; )
774                          orig_dir[1][i] = -duvw[0]*p->udir[i] -
# Line 795 | Line 776 | sample_reinhart(PARAMS *p, int b, FILE *fp)
776                                                  duvw[2]*p->nrm[i] ;
777                  if (!sample_origin(p, orig_dir[0], orig_dir[1], samp3[0]))
778                          return(0);
779 <                if (fwrite(orig_dir, sizeof(FVECT), 2, fp) != 2)
779 >                if (putbinary(orig_dir, sizeof(FVECT), 2, fp) != 2)
780                          return(0);
781          }
782          return(1);
# Line 838 | Line 819 | sample_klems(PARAMS *p, int b, FILE *fp)
819  
820          while (n--) {                   /* stratified sampling */
821                  SDmultiSamp(samp2, 2, (n+frandom())/sampcnt);
822 <                if (!bi_getvec(duvw, b+samp2[1], kbasis[bi]))
822 >                if (!fo_getvec(duvw, b+samp2[1], kbasis[bi]))
823                          return(0);
824                  for (i = 3; i--; )
825 <                        orig_dir[1][i] = duvw[0]*p->udir[i] +
826 <                                                duvw[1]*p->vdir[i] +
825 >                        orig_dir[1][i] = -duvw[0]*p->udir[i] -
826 >                                                duvw[1]*p->vdir[i] -
827                                                  duvw[2]*p->nrm[i] ;
828                  if (!sample_origin(p, orig_dir[0], orig_dir[1], samp2[0]))
829                          return(0);
830 <                if (fwrite(orig_dir, sizeof(FVECT), 2, fp) != 2)
830 >                if (putbinary(orig_dir, sizeof(FVECT), 2, fp) != 2)
831                          return(0);
832          }
833          return(1);
# Line 882 | Line 863 | prepare_sampler(void)
863                  else
864                          curparams.vup[1] = 1;
865          }
866 <        VCROSS(curparams.udir, curparams.vup, curparams.nrm);
866 >        fcross(curparams.udir, curparams.vup, curparams.nrm);
867          if (normalize(curparams.udir) == 0) {
868                  fputs(progname, stderr);
869                  fputs(": up vector coincides with sender normal\n", stderr);
870                  return(-1);
871          }
872 <        VCROSS(curparams.vdir, curparams.nrm, curparams.udir);
873 <        if (tolower(curparams.hemis[0]) == 'u' | curparams.hemis[0] == '1')
872 >        fcross(curparams.vdir, curparams.nrm, curparams.udir);
873 >        if (curparams.sign == '-') {    /* left-handed coordinate system? */
874 >                curparams.udir[0] *= -1.;
875 >                curparams.udir[1] *= -1.;
876 >                curparams.udir[2] *= -1.;
877 >        }
878 >        if ((tolower(curparams.hemis[0]) == 'u') | (curparams.hemis[0] == '1'))
879                  curparams.sample_basis = sample_uniform;
880          else if (tolower(curparams.hemis[0]) == 's' &&
881                                  tolower(curparams.hemis[1]) == 'c')
# Line 968 | Line 954 | add_surface(int st, const char *oname, FILE *fp)
954          snew = (SURF *)malloc(sizeof(SURF) + sizeof(double)*(n-1));
955          if (snew == NULL) {
956                  fputs(progname, stderr);
957 <                fputs(": out of memory!\n", stderr);
957 >                fputs(": out of memory in add_surface!\n", stderr);
958                  exit(1);
959          }
960          strncpy(snew->sname, oname, sizeof(snew->sname)-1);
# Line 1083 | Line 1069 | static int
1069   add_send_object(FILE *fp)
1070   {
1071          int             st;
1072 <        char            otype[32], oname[128];
1072 >        char            thismod[128], otype[32], oname[128];
1073          int             n;
1074  
1075 <        if (fscanf(fp, "%*s %s %s", otype, oname) != 2)
1075 >        if (fscanf(fp, "%s %s %s", thismod, otype, oname) != 3)
1076                  return(0);              /* must have hit EOF! */
1077          if (!strcmp(otype, "alias")) {
1078                  fscanf(fp, "%*s");      /* skip alias */
# Line 1099 | Line 1085 | add_send_object(FILE *fp)
1085                          fputs(": cannot use source as a sender!\n", stderr);
1086                          return(-1);
1087                  }
1088 +                if (strcmp(thismod, curmod)) {
1089 +                        if (curmod[0]) {
1090 +                                fputs(progname, stderr);
1091 +                                fputs(": warning - multiple modifiers in sender\n",
1092 +                                                stderr);
1093 +                        }
1094 +                        strcpy(curmod, thismod);
1095 +                }
1096                  parse_params(&curparams, newparams);
1097                  newparams[0] = '\0';
1098                  add_surface(st, oname, fp);     /* read & store surface */
# Line 1268 | Line 1262 | main(int argc, char *argv[])
1262                          if (argv[a][2] != 'v') na = 2;
1263                          break;
1264                  case 'a':               /* special case */
1265 <                        na = (argv[a][2] == 'v') ? 4 : 2;
1265 >                        if (argv[a][2] == 'p') {
1266 >                                na = 2; /* photon map [+ bandwidth(s)] */
1267 >                                if (a < argc-3 && atoi(argv[a+1]) > 0)
1268 >                                        na += 1 + (a < argc-4 && atoi(argv[a+2]) > 0);
1269 >                        } else
1270 >                                na = (argv[a][2] == 'v') ? 4 : 2;
1271                          break;
1272                  case 'm':               /* special case */
1273                          if (!argv[a][2]) goto userr;
# Line 1314 | Line 1313 | main(int argc, char *argv[])
1313                          fputs(": -i, -I supported for pass-through only\n", stderr);
1314                          return(1);
1315                  }
1316 <                fmtopt[2] = (sizeof(RREAL)==sizeof(double)) ? 'd' : 'f';
1316 > #ifdef SMLFLT
1317 >                fmtopt[2] = 'f';
1318 > #else
1319 >                fmtopt[2] = 'd';
1320 > #endif
1321                  if (sampcnt <= 0) sampcnt = 10000;
1322          }
1323          sprintf(sampcntbuf, "%d", sampcnt);
# Line 1333 | Line 1336 | main(int argc, char *argv[])
1336                  return(my_exec(rcarg)); /* rcontrib does everything */
1337          }
1338          clear_params(&curparams, 0);    /* else load sender surface & params */
1339 +        curmod[0] = '\0';
1340          if (load_scene(sendfn, add_send_object) < 0)
1341                  return(1);
1342          if ((nsbins = prepare_sampler()) <= 0)

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines