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.3 by greg, Tue Jul 22 02:12:48 2014 UTC vs.
Revision 2.12 by greg, Mon Jul 28 16:30:18 2014 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"
# Line 53 | Line 54 | char           *kfullfn = "klems_full.cal";
54   char            *khalffn = "klems_half.cal";
55   char            *kquarterfn = "klems_quarter.cal";
56  
57 < #define PARAMSTART      "@rfluxmtx"     /* string indicating parameters */
57 >                                        /* string indicating parameters */
58 > const char      PARAMSTART[] = "@rfluxmtx";
59  
60                                  /* surface type IDs */
61   #define ST_NONE         0
# Line 66 | Line 68 | typedef struct surf_s {
68          void            *priv;          /* private data (malloc'ed) */
69          char            sname[32];      /* surface name */
70          FVECT           snrm;           /* surface normal */
71 <        double          area;           /* surface area (or solid angle) */
71 >        double          area;           /* surface area / proj. solid angle */
72          short           styp;           /* surface type */
73          short           nfargs;         /* number of real arguments */
74          double          farg[1];        /* real values (extends struct) */
# Line 95 | Line 97 | typedef struct param_s {
97  
98   PARAMS          curparams;
99   char            curmod[128];
100 + char            newparams[1024];
101  
102   typedef int     SURFSAMP(FVECT, SURF *, double);
103  
# Line 108 | Line 111 | SURFSAMP       *orig_in_surf[4] = {
111   static void
112   clear_params(PARAMS *p, int reset_only)
113   {
111        curmod[0] = '\0';
114          while (p->slist != NULL) {
115                  SURF    *sdel = p->slist;
116                  p->slist = sdel->next;
# Line 123 | Line 125 | clear_params(PARAMS *p, int reset_only)
125                  p->outfn = NULL;
126                  return;
127          }
128 <        memset(p, 0, sizeof(curparams));
128 >        memset(p, 0, sizeof(PARAMS));
129   }
130  
131   /* Get surface type from name */
# Line 308 | Line 310 | nextchar:
310  
311   /* Parse program parameters (directives) */
312   static int
313 < parse_params(char *pargs)
313 > parse_params(PARAMS *p, char *pargs)
314   {
315          char    *cp = pargs;
316          int     nparams = 0;
# Line 319 | Line 321 | parse_params(char *pargs)
321                  case 'h':
322                          if (*cp++ != '=')
323                                  break;
324 <                        curparams.hsiz = 0;
324 >                        p->hsiz = 0;
325                          i = 0;
326                          while (*cp && !isspace(*cp)) {
327                                  if (isdigit(*cp))
328 <                                        curparams.hsiz = 10*curparams.hsiz +
329 <                                                                *cp - '0';
328 <                                curparams.hemis[i++] = *cp++;
328 >                                        p->hsiz = 10*p->hsiz + *cp - '0';
329 >                                p->hemis[i++] = *cp++;
330                          }
331                          if (!i)
332                                  break;
333 <                        curparams.hemis[i] = '\0';
334 <                        curparams.hsiz += !curparams.hsiz;
333 >                        p->hemis[i] = '\0';
334 >                        p->hsiz += !p->hsiz;
335                          ++nparams;
336                          continue;
337                  case 'u':
338                          if (*cp++ != '=')
339                                  break;
340 <                        if (!get_direction(curparams.vup, cp))
340 >                        if (!get_direction(p->vup, cp))
341                                  break;
342                          ++nparams;
343                          continue;
# Line 349 | Line 350 | parse_params(char *pargs)
350                          if (!i)
351                                  break;
352                          *--cp = '\0';
353 <                        curparams.outfn = savqstr(cp-i);
353 >                        p->outfn = savqstr(cp-i);
354                          *cp++ = ' ';
355                          ++nparams;
356                          continue;
# Line 372 | Line 373 | parse_params(char *pargs)
373   static void
374   finish_receiver(void)
375   {
376 +        char    sbuf[256];
377 +        int     uniform = 0;
378          char    *calfn = NULL;
379          char    *params = NULL;
380          char    *binv = NULL;
381          char    *binf = NULL;
382          char    *nbins = NULL;
380        char    sbuf[256];
383  
384          if (!curmod[0]) {
385                  fputs(progname, stderr);
# Line 389 | Line 391 | finish_receiver(void)
391                  rcarg[nrcargs++] = "-o";
392                  rcarg[nrcargs++] = curparams.outfn;
393          }
394 <                                        /* add bin specification */
394 >                                        /* check arguments */
395          if (!curparams.hemis[0]) {
396                  fputs(progname, stderr);
397                  fputs(": missing hemisphere sampling type!\n", stderr);
# Line 400 | Line 402 | finish_receiver(void)
402                  fputs(": undefined normal for hemisphere sampling\n", stderr);
403                  exit(1);
404          }
405 <        if (normalize(curparams.vup) == 0)
405 >        if (normalize(curparams.vup) == 0) {
406                  if (fabs(curparams.nrm[2]) < .7)
407                          curparams.vup[2] = 1;
408                  else
409                          curparams.vup[1] = 1;
410 +        }
411 +                                        /* determine sample type/bin */
412          if (tolower(curparams.hemis[0]) == 'u' | curparams.hemis[0] == '1') {
413                  binv = "0";             /* uniform sampling -- one bin */
414 +                uniform = 1;
415          } else if (tolower(curparams.hemis[0]) == 's' &&
416                                  tolower(curparams.hemis[1]) == 'c') {
417                                          /* assign parameters */
# Line 416 | Line 421 | finish_receiver(void)
421                          exit(1);
422                  }
423                  calfn = shirchiufn; shirchiufn = NULL;
424 <                sprintf(sbuf, "SCdim=%d,Nx=%g,Ny=%g,Nz=%g,Ux=%g,Uy=%g,Uz=%g",
424 >                sprintf(sbuf, "SCdim=%d,rNx=%g,rNy=%g,rNz=%g,Ux=%g,Uy=%g,Uz=%g",
425                                  curparams.hsiz,
426                          curparams.nrm[0], curparams.nrm[1], curparams.nrm[2],
427                          curparams.vup[0], curparams.vup[1], curparams.vup[2]);
# Line 426 | Line 431 | finish_receiver(void)
431          } else if ((tolower(curparams.hemis[0]) == 'r') |
432                          (tolower(curparams.hemis[0]) == 't')) {
433                  calfn = reinhfn; reinhfn = NULL;
434 <                sprintf(sbuf, "MF=%d,Nx=%g,Ny=%g,Nz=%g,Ux=%g,Uy=%g,Uz=%g",
434 >                sprintf(sbuf, "MF=%d,rNx=%g,rNy=%g,rNz=%g,Ux=%g,Uy=%g,Uz=%g",
435                                  curparams.hsiz,
436                          curparams.nrm[0], curparams.nrm[1], curparams.nrm[2],
437                          curparams.vup[0], curparams.vup[1], curparams.vup[2]);
# Line 457 | Line 462 | finish_receiver(void)
462                                  progname, curparams.hemis);
463                  exit(1);
464          }
465 +        if (!uniform & (curparams.slist->styp == ST_SOURCE)) {
466 +                SURF    *sp;
467 +                for (sp = curparams.slist; sp != NULL; sp = sp->next)
468 +                        if (fabs(sp->area - PI) > 1e-3) {
469 +                                fprintf(stderr, "%s: source '%s' must be 180-degrees\n",
470 +                                                progname, sp->sname);
471 +                                exit(1);
472 +                        }
473 +        }
474          if (calfn != NULL) {            /* add cal file if needed */
475                  CHECKARGC(2);
476                  rcarg[nrcargs++] = "-f";
# Line 541 | Line 555 | ssamp_ring(FVECT orig, SURF *sp, double x)
555                  sp->priv = (void *)uva;
556          }
557          SDmultiSamp(samp2, 2, x);
558 <        samp2[0] = sp->farg[6] + sqrt(samp2[0]*sp->area*(1./PI));
558 >        samp2[0] = sqrt(samp2[0]*sp->area*(1./PI) + sp->farg[6]*sp->farg[6]);
559          samp2[1] *= 2.*PI;
560          uv[0] = samp2[0]*tcos(samp2[1]);
561          uv[1] = samp2[0]*tsin(samp2[1]);
# Line 594 | Line 608 | ssamp_poly(FVECT orig, SURF *sp, double x)
608                          }
609                          ptp->ntris = 0;
610                          v2l->p = (void *)ptp;
611 <                        if (!polyTriangulate(v2l, add_triangle))
611 >                        if (!polyTriangulate(v2l, add_triangle)) {
612 >                                fprintf(stderr,
613 >                                        "%s: cannot triangulate polygon '%s'\n",
614 >                                                progname, sp->sname);
615                                  return(0);
616 +                        }
617                          for (i = ptp->ntris; i--; ) {
618                                  int     a = ptp->tri[i].vndx[0];
619                                  int     b = ptp->tri[i].vndx[1];
# Line 644 | Line 662 | sample_origin(PARAMS *p, FVECT orig, const FVECT rdir,
662                                          /* special case for lone surface */
663          if (p->nsurfs == 1) {
664                  sp = p->slist;
665 <                if (DOT(sp->snrm, rdir) >= -FTINY)
666 <                        return(0);      /* behind surface! */
665 >                if (DOT(sp->snrm, rdir) >= -FTINY) {
666 >                        fprintf(stderr,
667 >                                "%s: internal - sample behind sender '%s'\n",
668 >                                        progname, sp->sname);
669 >                        return(0);
670 >                }
671                  return((*orig_in_surf[sp->styp])(orig, sp, x));
672          }
673          if (p->nsurfs > nall) {         /* (re)allocate surface area cache */
# Line 659 | Line 681 | sample_origin(PARAMS *p, FVECT orig, const FVECT rdir,
681                  projsa[i] = -DOT(sp->snrm, rdir) * sp->area;
682                  tarea += projsa[i] *= (double)(projsa[i] > FTINY);
683          }
684 <        if (tarea <= FTINY)             /* wrong side of sender? */
684 >        if (tarea <= FTINY) {           /* wrong side of sender? */
685 >                fputs(progname, stderr);
686 >                fputs(": internal - sample behind all sender elements!\n",
687 >                                stderr);
688                  return(0);
689 +        }
690          tarea *= x;                     /* get surface from list */
691          for (i = 0, sp = p->slist; tarea > projsa[i]; sp = sp->next)
692                  tarea -= projsa[i++];
# Line 733 | Line 759 | sample_reinhart(PARAMS *p, int b, FILE *fp)
759   #define T_NALT  7
760          static const int        tnaz[T_NALT] = {30, 30, 24, 24, 18, 12, 6};
761          const int               RowMax = T_NALT*p->hsiz + 1;
762 <        const double            RAH = (.25*PI)/(RowMax-.5);
762 >        const double            RAH = (.5*PI)/(RowMax-.5);
763   #define rnaz(r)                 (r >= RowMax-1 ? 1 : p->hsiz*tnaz[r/p->hsiz])
764          int                     n = sampcnt;
765          int                     row, col;
# Line 758 | Line 784 | sample_reinhart(PARAMS *p, int b, FILE *fp)
784                  SDmultiSamp(samp3, 3, (n+frandom())/sampcnt);
785                  alt = (row+samp3[1])*RAH;
786                  azi = (2.*PI)*(col+samp3[2]-.5)/rnaz(row);
787 <                duvw[2] = tcos(alt);    /* measured from horizon */
787 >                duvw[2] = cos(alt);     /* measured from horizon */
788                  duvw[0] = tcos(azi)*duvw[2];
789                  duvw[1] = tsin(azi)*duvw[2];
790                  duvw[2] = sqrt(1. - duvw[2]*duvw[2]);
# Line 848 | Line 874 | prepare_sampler(void)
874                  fputs(": undefined normal for sender sampling\n", stderr);
875                  return(-1);
876          }
877 <        if (normalize(curparams.vup) == 0)
877 >        if (normalize(curparams.vup) == 0) {
878                  if (fabs(curparams.nrm[2]) < .7)
879                          curparams.vup[2] = 1;
880                  else
881                          curparams.vup[1] = 1;
882 +        }
883          VCROSS(curparams.udir, curparams.vup, curparams.nrm);
884          if (normalize(curparams.udir) == 0) {
885                  fputs(progname, stderr);
# Line 976 | Line 1003 | add_surface(int st, const char *oname, FILE *fp)
1003          case ST_SOURCE:
1004                  if (snew->nfargs != 4)
1005                          goto badcount;
1006 <                VCOPY(snew->snrm, snew->farg);
1006 >                for (n = 3; n--; )      /* need to reverse "normal" */
1007 >                        snew->snrm[n] = -snew->farg[n];
1008                  if (normalize(snew->snrm) == 0)
1009                          goto badnorm;
1010 <                snew->area = 2.*PI*(1. - cos((PI/180./2.)*snew->farg[3]));
1010 >                snew->area = sin((PI/180./2.)*snew->farg[3]);
1011 >                snew->area *= PI*snew->area;
1012                  break;
1013          }
1014          if (snew->area <= FTINY) {
# Line 994 | Line 1023 | add_surface(int st, const char *oname, FILE *fp)
1023          curparams.nsurfs++;
1024          return;
1025   badcount:
1026 <        fprintf(stderr, "%s: bad argument count for surface '%s'\n",
1026 >        fprintf(stderr, "%s: bad argument count for surface element '%s'\n",
1027                          progname, oname);
1028          exit(1);
1029   badnorm:
1030 <        fprintf(stderr, "%s: bad orientation for surface '%s'\n",
1030 >        fprintf(stderr, "%s: bad orientation for surface element '%s'\n",
1031                          progname, oname);
1032          exit(1);
1033   }
# Line 1030 | Line 1059 | add_recv_object(FILE *fp)
1059                                  finish_receiver();
1060                                  clear_params(&curparams, 1);
1061                          }
1062 +                        parse_params(&curparams, newparams);
1063 +                        newparams[0] = '\0';
1064                          strcpy(curmod, thismod);
1065                  }
1066                  add_surface(st, oname, fp);     /* read & store surface */
1067                  return(1);
1068          }
1069                                          /* else skip arguments */
1070 <        if (!fscanf(fp, "%d", &n)) return;
1070 >        if (!fscanf(fp, "%d", &n)) return(0);
1071          while (n-- > 0) fscanf(fp, "%*s");
1072 <        if (!fscanf(fp, "%d", &n)) return;
1072 >        if (!fscanf(fp, "%d", &n)) return(0);
1073          while (n-- > 0) fscanf(fp, "%*d");
1074 <        if (!fscanf(fp, "%d", &n)) return;
1074 >        if (!fscanf(fp, "%d", &n)) return(0);
1075          while (n-- > 0) fscanf(fp, "%*f");
1076          return(0);
1077   }
# Line 1066 | Line 1097 | add_send_object(FILE *fp)
1097                          fputs(": cannot use source as a sender!\n", stderr);
1098                          return(-1);
1099                  }
1100 +                parse_params(&curparams, newparams);
1101 +                newparams[0] = '\0';
1102                  add_surface(st, oname, fp);     /* read & store surface */
1103                  return(0);
1104          }
1105                                          /* else skip arguments */
1106 <        if (!fscanf(fp, "%d", &n)) return;
1106 >        if (!fscanf(fp, "%d", &n)) return(0);
1107          while (n-- > 0) fscanf(fp, "%*s");
1108 <        if (!fscanf(fp, "%d", &n)) return;
1108 >        if (!fscanf(fp, "%d", &n)) return(0);
1109          while (n-- > 0) fscanf(fp, "%*d");
1110 <        if (!fscanf(fp, "%d", &n)) return;
1110 >        if (!fscanf(fp, "%d", &n)) return(0);
1111          while (n-- > 0) fscanf(fp, "%*f");
1112          return(0);
1113   }
# Line 1109 | Line 1142 | load_scene(const char *inspec, int (*ocb)(FILE *))
1142                          continue;
1143                  }
1144                  if (c == '#') {         /* parameters/comment */
1145 <                        if (fscanf(fp, "%s", inpbuf) == 1 &&
1145 >                        if ((c = getc(fp)) == EOF || ungetc(c,fp) == EOF)
1146 >                                break;
1147 >                        if (!isspace(c) && fscanf(fp, "%s", inpbuf) == 1 &&
1148                                          !strcmp(inpbuf, PARAMSTART)) {
1149                                  if (fgets(inpbuf, sizeof(inpbuf), fp) != NULL)
1150 <                                        parse_params(inpbuf);
1150 >                                        strcat(newparams, inpbuf);
1151                                  continue;
1152                          }
1153 <                        while ((c = getc(fp)) != EOF && c != '\n');
1153 >                        while ((c = getc(fp)) != EOF && c != '\n')
1154                                  ;       /* else skipping comment */
1155                          continue;
1156                  }
# Line 1139 | Line 1174 | int
1174   main(int argc, char *argv[])
1175   {
1176          char    fmtopt[6] = "-faa";     /* default output is ASCII */
1177 +        char    *xrs=NULL, *yrs=NULL, *ldopt=NULL;
1178 +        char    *iropt = NULL;
1179          char    *sendfn;
1180          char    sampcntbuf[32], nsbinbuf[32];
1181          FILE    *rcfp;
# Line 1163 | Line 1200 | main(int argc, char *argv[])
1200                          case 'f':
1201                          case 'd':
1202                          case 'c':
1203 <                                if (!(fmtopt[4] = argv[a][3]))
1204 <                                        fmtopt[4] = argv[a][2];
1205 <                                fmtopt[3] = argv[a][2];
1203 >                                if (!(fmtopt[3] = argv[a][3]))
1204 >                                        fmtopt[3] = argv[a][2];
1205 >                                fmtopt[2] = argv[a][2];
1206                                  na = 0;
1207                                  continue;       /* will pass later */
1208                          default:
1209                                  goto userr;
1210                          }
1211                          break;
1212 +                case 'x':               /* x-resolution */
1213 +                        xrs = argv[++a];
1214 +                        na = 0;
1215 +                        continue;
1216 +                case 'y':               /* y-resolution */
1217 +                        yrs = argv[++a];
1218 +                        na = 0;
1219 +                        continue;
1220                  case 'c':               /* number of samples */
1221 <                        sampcnt = atoi(argv[a+1]);
1221 >                        sampcnt = atoi(argv[++a]);
1222                          if (sampcnt <= 0)
1223                                  goto userr;
1224                          na = 0;         /* we re-add this later */
1225                          continue;
1226 +                case 'I':               /* only for pass-through mode */
1227 +                case 'i':
1228 +                        iropt = argv[a];
1229 +                        na = 0;
1230 +                        continue;
1231                  case 'V':               /* options without arguments */
1232                  case 'w':
1233                  case 'u':
1184                case 'i':
1234                  case 'h':
1235 +                case 'r':
1236                          break;
1237                  case 'n':               /* options with 1 argument */
1238                  case 's':
# Line 1193 | Line 1243 | main(int argc, char *argv[])
1243                          if (argv[a][2] != 'v') goto userr;
1244                          break;
1245                  case 'l':               /* special case */
1246 <                        if (argv[a][2] == 'd') goto userr;
1246 >                        if (argv[a][2] == 'd') {
1247 >                                ldopt = argv[a];
1248 >                                na = 0;
1249 >                                continue;
1250 >                        }
1251                          na = 2;
1252                          break;
1253                  case 'd':               /* special case */
# Line 1224 | Line 1278 | done_opts:
1278          if (sendfn[0] == '-') {         /* user wants pass-through mode? */
1279                  if (sendfn[1]) goto userr;
1280                  sendfn = NULL;
1281 +                if (iropt) {
1282 +                        CHECKARGC(1);
1283 +                        rcarg[nrcargs++] = iropt;
1284 +                }
1285 +                if (xrs) {
1286 +                        CHECKARGC(2);
1287 +                        rcarg[nrcargs++] = "-x";
1288 +                        rcarg[nrcargs++] = xrs;
1289 +                }
1290 +                if (yrs) {
1291 +                        CHECKARGC(2);
1292 +                        rcarg[nrcargs++] = "-y";
1293 +                        rcarg[nrcargs++] = yrs;
1294 +                }
1295 +                if (ldopt) {
1296 +                        CHECKARGC(1);
1297 +                        rcarg[nrcargs++] = ldopt;
1298 +                }
1299                  if (sampcnt <= 0) sampcnt = 1;
1300 <        } else {                        /* else FVECT determines input format */
1301 <                fmtopt[3] = (sizeof(RREAL)==sizeof(double)) ? 'd' : 'f';
1300 >        } else {                        /* else in sampling mode */
1301 >                if (iropt) {
1302 >                        fputs(progname, stderr);
1303 >                        fputs(": -i, -I supported for pass-through only\n", stderr);
1304 >                        return(1);
1305 >                }
1306 >                fmtopt[2] = (sizeof(RREAL)==sizeof(double)) ? 'd' : 'f';
1307                  if (sampcnt <= 0) sampcnt = 10000;
1308          }
1309          sprintf(sampcntbuf, "%d", sampcnt);
# Line 1265 | Line 1342 | done_opts:
1342          if (verbose) {
1343                  fprintf(stderr, "%s: sampling %d directions", progname, nsbins);
1344                  if (curparams.nsurfs > 1)
1345 <                        fprintf(stderr, " (%d surfaces)\n", curparams.nsurfs);
1345 >                        fprintf(stderr, " (%d elements)\n", curparams.nsurfs);
1346                  else
1347                          fputc('\n', stderr);
1348          }
# Line 1276 | Line 1353 | done_opts:
1353   userr:
1354          if (a < argc-2)
1355                  fprintf(stderr, "%s: unsupported option '%s'", progname, argv[a]);
1356 <        fprintf(stderr, "Usage: %s [rcontrib options] sender.rad receiver.rad [system.rad ..]\n",
1356 >        fprintf(stderr, "Usage: %s [-v][rcontrib options] sender.rad receiver.rad [system.rad ..]\n",
1357                                  progname);
1358          return(1);
1359   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines